Repository: edgedb/edgedb

Issue:
InternalServerError: No method to generate code for TupleVar
- EdgeDB Version: 3.0-dev.7093+local
- EdgeDB CLI Version: 2.3.0

Steps to Reproduce:

```
with items := {'a', 'b', 'c', 'd'}
group enumerate(items)
using groupIndex := .0 // 2
by groupIndex;
```
```
InternalServerError: No method to generate code for TupleVar
  Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
  Server traceback:
      Traceback (most recent call last):
        File "/home/james/dev/edgedb/edb/server/compiler_pool/worker_proc.py", line 55, in worker
          res = meth(*args)
        File "/home/james/dev/edgedb/edb/server/compiler_pool/worker.py", line 161, in compile
          units, cstate = COMPILER.compile(
        File "/home/james/dev/edgedb/edb/server/compiler/compiler.py", line 552, in compile
          unit_group = compile(ctx=ctx, source=source)
        File "/home/james/dev/edgedb/edb/server/compiler/compiler.py", line 1604, in compile
          return _try_compile(ctx=ctx, source=source)
        File "/home/james/dev/edgedb/edb/server/compiler/compiler.py", line 1669, in _try_compile
          comp, capabilities = _compile_dispatch_ql(
        File "/home/james/dev/edgedb/edb/server/compiler/compiler.py", line 1578, in _compile_dispatch_ql
          query = _compile_ql_query(ctx, ql, script_info=script_info)
        File "/home/james/dev/edgedb/edb/server/compiler/compiler.py", line 1173, in _compile_ql_query
          sql_text, argmap = pg_compiler.compile_ir_to_sql(
        File "/home/james/dev/edgedb/edb/pgsql/compiler/__init__.py", line 176, in compile_ir_to_sql
          sql_text = run_codegen(qtree, pretty=pretty)
        File "/home/james/dev/edgedb/edb/pgsql/compiler/__init__.py", line 201, in run_codegen
          codegen.visit(qtree)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 240, in visit_SelectStmt
          self.visit_list(node.from_clause)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 586, in visit_JoinExpr
          self.visit(node.larg)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 530, in visit_RangeSubselect
          self.visit(node.subquery)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 240, in visit_SelectStmt
          self.visit_list(node.from_clause)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 605, in visit_JoinExpr
          self.visit(node.rarg)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 530, in visit_RangeSubselect
          self.visit(node.subquery)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 240, in visit_SelectStmt
          self.visit_list(node.from_clause)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 586, in visit_JoinExpr
          self.visit(node.larg)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 530, in visit_RangeSubselect
          self.visit(node.subquery)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 227, in visit_SelectStmt
          self.visit_list(node.target_list)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 474, in visit_ResTarget
          self.visit(node.val)
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 240, in visit_SelectStmt
          self.visit_list(node.from_clause)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 543, in visit_RangeFunction
          self.visit_list(node.functions)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 692, in visit_FuncCall
          self.visit_list(node.args, newlines=False)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 674, in visit_ArrayExpr
          self.visit_list(node.elements, newlines=False)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 97, in visit_list
          self.visit(item, **kwargs)  # type: ignore
        File "/home/james/dev/edgedb/edb/common/ast/visitor.py", line 160, in visit
          return self.node_visit(node)
        File "/home/james/dev/edgedb/edb/common/ast/codegen.py", line 53, in node_visit
          return visitor(node)
        File "/home/james/dev/edgedb/edb/pgsql/codegen.py", line 111, in generic_visit
          raise SQLSourceGeneratorError(
      edb.pgsql.codegen.SQLSourceGeneratorError: No method to generate code for TupleVar
```



Discussion:

Resolving patch:
diff --git a/edb/pgsql/compiler/relgen.py b/edb/pgsql/compiler/relgen.py
--- a/edb/pgsql/compiler/relgen.py
+++ b/edb/pgsql/compiler/relgen.py
@@ -2592,7 +2592,7 @@ def process_set_as_simple_enumerate(
             newctx.rel, ir_set.path_id, set_expr, aspect='value', env=ctx.env)
 
     aspects = pathctx.list_path_aspects(
-        newctx.rel, ir_arg.path_id, env=ctx.env)
+        newctx.rel, ir_arg.path_id, env=ctx.env) | {'source'}
 
     pathctx.put_path_id_map(newctx.rel, expr.tuple_path_ids[1], ir_arg.path_id)
 


Test patch:
diff --git a/tests/test_edgeql_group.py b/tests/test_edgeql_group.py
--- a/tests/test_edgeql_group.py
+++ b/tests/test_edgeql_group.py
@@ -1254,3 +1254,45 @@ async def test_edgeql_group_binding_complex_01(self):
           ''',
             tb.bag([{"sum": -7}, {"sum": 5}, {"sum": -23}]),
         )
+
+    async def test_edgeql_group_enumerate_01(self):
+        await self.assert_query_result(
+            '''
+                group enumerate({'a', 'b', 'c', 'd'})
+                using groupIndex := .0 // 2
+                by groupIndex;
+            ''',
+            tb.bag([
+                {
+                    "elements": tb.bag([[0, "a"], [1, "b"]]),
+                    "grouping": ["groupIndex"],
+                    "key": {"groupIndex": 0}
+                },
+                {
+                    "elements": tb.bag([[2, "c"], [3, "d"]]),
+                    "grouping": ["groupIndex"],
+                    "key": {"groupIndex": 1}
+                }
+            ]),
+        )
+
+    async def test_edgeql_group_enumerate_02(self):
+        await self.assert_query_result(
+            '''
+                group enumerate(array_unpack(['a', 'b', 'c', 'd']))
+                using groupIndex := .0 // 2
+                by groupIndex;
+            ''',
+            tb.bag([
+                {
+                    "elements": tb.bag([[0, "a"], [1, "b"]]),
+                    "grouping": ["groupIndex"],
+                    "key": {"groupIndex": 0}
+                },
+                {
+                    "elements": tb.bag([[2, "c"], [3, "d"]]),
+                    "grouping": ["groupIndex"],
+                    "key": {"groupIndex": 1}
+                }
+            ]),
+        )