internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: edgedb/edgedb

Issue:
std::array_get doesn't return if input is result of a sub-query
- EdgeDB Version: EdgeDB CLI 1.1.2+f38a6e0 & Instance 1.4+5ffe34b
- OS Version: Windows 10 21H2

## Summary
`array_get` doesn't return anything even if the index is valid when its first argument is a postrequisite of a query.

Take this query for example:
```sql
select array_get([1,2,3], 1);
```
The expected result should be `2`, running it produces the expected output
```sql
{2}
```

Now, lets make the first argument for `array_get` another function.
```sql
select array_get(array_agg({1,2,3}), 1);
```
We should expect the result to be the same, running it shows us the expected output:
```sql
{2}
```

Let's now add a sub-query within the function call, the produced result should be the same:
```sql
select array_get(array_agg((select x := {1,2,3} filter x > 0)), 1);
```
The result of the `array_get` function now just returns `{}` instead of the expected `{2}`
```sql
{}
```
Removing the `array_get` function from the query shows us that infact the result of the sub query & the `array_agg` function is a valid array:
```sql
edgedb> select array_agg((select x := {1,2,3} filter x > 0));
{[1, 2, 3]}
```

This might be related to #4090.




Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/func.py b/edb/edgeql/compiler/func.py
--- a/edb/edgeql/compiler/func.py
+++ b/edb/edgeql/compiler/func.py
@@ -895,8 +895,11 @@ def finalize_args(
             # cast the arguments so that the backend has no
             # wiggle room to apply its own (potentially different)
             # casting.
+            orig_arg = arg
             arg = casts.compile_cast(
                 arg, paramtype, srcctx=None, ctx=ctx)
+            if ctx.path_scope.is_optional(orig_arg.path_id):
+                pathctx.register_set_in_scope(arg, optional=True, ctx=ctx)
 
         args.append(
             irast.CallArg(expr=arg, expr_type_path_id=arg_type_path_id,


Test patch:
diff --git a/tests/test_edgeql_functions.py b/tests/test_edgeql_functions.py
--- a/tests/test_edgeql_functions.py
+++ b/tests/test_edgeql_functions.py
@@ -984,6 +984,15 @@ async def test_edgeql_functions_array_get_07(self):
             {'1', '2', '3', '4'},
         )
 
+    async def test_edgeql_functions_array_get_08(self):
+        await self.assert_query_result(
+            r'''
+                select array_get(
+                    array_agg((select x := {1,2,3} filter x > 0)), 1);
+            ''',
+            [2],
+        )
+
     @test.xerror(
         "Known collation issue on Heroku Postgres",
         unless=os.getenv("EDGEDB_TEST_BACKEND_VENDOR") != "heroku-postgres"
Repository: edgedb/edgedb

Issue:
ISE when setting wrong lprop in DML
```
create type Tgt;
create type Src { create link t -> Tgt { create property y -> str } };
insert Src { t := assert_single(Tgt { @x := "..." }) };
```
gives
```
edgedb error: InternalServerError: column "41908a06-a3f9-11ec-9859-e36a19a1319b" of relation "3f7ac01d-a3f9-11ec-b7aa-4341f5e8dceb" does not exist
  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:
      edb.errors.InternalServerError: column "41908a06-a3f9-11ec-9859-e36a19a1319b" of relation "3f7ac01d-a3f9-11ec-b7aa-4341f5e8dceb" does not exist
```


Discussion:
Probably the right thing to do here is to produce a real error?

Resolving patch:
diff --git a/edb/edgeql/compiler/viewgen.py b/edb/edgeql/compiler/viewgen.py
--- a/edb/edgeql/compiler/viewgen.py
+++ b/edb/edgeql/compiler/viewgen.py
@@ -746,8 +746,13 @@ def _normalize_view_ptr_expr(
                 base_ptrcls = ptrcls.get_bases(
                     ctx.env.schema).first(ctx.env.schema)
             except errors.InvalidReferenceError:
-                # This is a NEW computable pointer, it's fine.
-                pass
+                # Check if we aren't inside of modifying statement
+                # for link property, otherwise this is a NEW
+                # computable pointer, it's fine.
+                if (view_rptr is not None
+                        and view_rptr.exprtype.is_mutation()
+                        and is_linkprop):
+                    raise
 
         qlexpr = astutils.ensure_qlstmt(compexpr)
 


Test patch:
diff --git a/tests/test_edgeql_linkprops.py b/tests/test_edgeql_linkprops.py
--- a/tests/test_edgeql_linkprops.py
+++ b/tests/test_edgeql_linkprops.py
@@ -1318,3 +1318,23 @@ async def test_edgeql_props_bogus_01(self):
                     };
                 '''
             )
+
+    async def test_edgeql_props_modification_01(self):
+        await self.con.execute(r'''
+            CREATE TYPE Tgt;
+            CREATE TYPE Src {
+                CREATE LINK l -> Tgt {
+                    CREATE PROPERTY x -> str;
+                };
+            };
+        ''')
+
+        with self.assertRaisesRegex(
+                edgedb.InvalidReferenceError,
+                r"link 'l' of object type 'default::Src' has no property 'y'",
+                _hint="did you mean 'x'?"):
+            await self.con.query(
+                r'''
+                    insert Src { l := assert_single(Tgt { @y := "..." }) };
+                '''
+            )
Repository: edgedb/edgedb

Issue:
ISE when using query variables in scripts
<!-- Please search existing issues to avoid creating duplicates. -->
- EdgeDB Version: RC3
- OS Version: macOS Catalina

Steps to Reproduce:

Attempting to execute this query with the latest `edgedb-js`:

```ts
await client.query(
  `
  select <str>$val;
  select <int64>$num;
`,
  {val: "asdf", num: 1234}
)
```

Results in 

```
InternalServerError: bind message supplies 0 parameters, but prepared statement "" requires 1
    at RawConnection._parseErrorMessage (/Users/colinmcd94/Documents/edgedb/js/dist/baseConn.js:317:21)
    at RawConnection._executeFlow (/Users/colinmcd94/Documents/edgedb/js/dist/baseConn.js:925:34)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at RawConnection.fetch (/Users/colinmcd94/Documents/edgedb/js/dist/baseConn.js:976:17)
    at ClientConnectionHolder.retryingFetch (/Users/colinmcd94/Documents/edgedb/js/dist/client.js:183:26)
    at Client.query (/Users/colinmcd94/Documents/edgedb/js/dist/client.js:484:20)
    at run (/Users/colinmcd94/Documents/edgedb/js/qb/playground.ts:11:5)
```




Discussion:

Resolving patch:
diff --git a/edb/server/compiler/compiler.py b/edb/server/compiler/compiler.py
--- a/edb/server/compiler/compiler.py
+++ b/edb/server/compiler/compiler.py
@@ -752,7 +752,8 @@ def _extract_params(
         else:
             first_extra = None
 
-        user_params = first_extra if first_extra is not None else len(params)
+        total_params = len(script_info.params) if script_info else len(params)
+        user_params = first_extra if first_extra is not None else total_params
 
         if script_info is not None:
             outer_mapping = {n: i for i, n in enumerate(script_info.params)}


Test patch:
diff --git a/tests/test_server_proto.py b/tests/test_server_proto.py
--- a/tests/test_server_proto.py
+++ b/tests/test_server_proto.py
@@ -830,6 +830,29 @@ async def test_server_proto_args_10(self):
             ),
             edgedb.Set(('!?',)))
 
+    async def test_server_proto_args_11(self):
+        async with self._run_and_rollback():
+            self.assertEqual(
+                await self.con.query(
+                    '''
+                        insert Tmp { tmp := <str>$0 };
+                        select Tmp.tmp ++ <str>$1;
+                    ''',
+                    "?", "!"),
+                edgedb.Set(["?!"]),
+            )
+
+        async with self._run_and_rollback():
+            self.assertEqual(
+                await self.con.query(
+                    '''
+                        insert Tmp { tmp := <str>$foo };
+                        select Tmp.tmp ++ <str>$bar;
+                    ''',
+                    foo="?", bar="!"),
+                edgedb.Set(["?!"]),
+            )
+
     async def test_server_proto_wait_cancel_01(self):
         # Test that client protocol handles waits interrupted
         # by closing.
Repository: edgedb/edgedb

Issue:
ISE when grouping by non-existent identifier
- EdgeDB Version: 3.0-dev.6803+local

Steps to Reproduce:
```
_example> group Movie by title;

edgedb error: InternalServerError: title
```



Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/stmt.py b/edb/edgeql/compiler/stmt.py
--- a/edb/edgeql/compiler/stmt.py
+++ b/edb/edgeql/compiler/stmt.py
@@ -27,6 +27,7 @@
 import textwrap
 
 from edb import errors
+from edb.common import ast
 from edb.common import context as pctx
 from edb.common.typeutils import not_none
 
@@ -363,6 +364,17 @@ def compile_InternalGroupQuery(
                 stmt.grouping_binding = _make_group_binding(
                     grouping_stype, expr.grouping_alias, ctx=topctx)
 
+        # Check that the by clause is legit
+        by_refs: List[qlast.ObjectRef] = ast.find_children(
+            stmt.by, lambda n: isinstance(n, qlast.ObjectRef))
+        for by_ref in by_refs:
+            if by_ref.name not in stmt.using:
+                raise errors.InvalidReferenceError(
+                    f"variable '{by_ref.name}' referenced in BY but not "
+                    f"declared in USING",
+                    context=by_ref.context,
+                )
+
         # compile the output
         # newscope because we don't want the result to get assigned the
         # same statement scope as the subject and elements, which we


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
@@ -788,7 +788,7 @@ async def test_edgeql_group_by_group_by_03c(self):
             '''
         )
 
-    async def test_edgeql_group_id_errors(self):
+    async def test_edgeql_group_errors_id(self):
         async with self.assertRaisesRegexTx(
             edgedb.UnsupportedFeatureError,
             r"may not name a grouping alias 'id'"
@@ -806,6 +806,15 @@ async def test_edgeql_group_id_errors(self):
                 group cards::Card{name} by .id
             ''')
 
+    async def test_edgeql_group_errors_ref(self):
+        async with self.assertRaisesRegexTx(
+            edgedb.InvalidReferenceError,
+            r"variable 'name' referenced in BY but not declared in USING"
+        ):
+            await self.con.execute('''
+                group User by name
+            ''')
+
     async def test_edgeql_group_tuple_01(self):
         await self.con.execute('''
             create type tup {
Repository: edgedb/edgedb

Issue:
Can't create required global with computation in default
This works

```
required global somestr -> str {
  default := '00000000-0000-0000-0000-000000000000';
};
```

But for some reason this doesn't:

```
required global someuuid -> uuid {
  default := <uuid>'00000000-0000-0000-0000-000000000000';
};
```

```
$ edgedb migration create
error: required globals must have a default
   ┌─ dbschema/default.esdl:16:3
   │  
16 │ ╭   required global someuuid -> uuid {
17 │ │     default := <uuid>'00000000-0000-0000-0000-000000000000';
18 │ │   };
   │ ╰───^ error
```


Discussion:
```
required global somestr -> str {
  default := '?' ++ '!';
};
```
fails also.

It's the cast that is breaking it.

The issue is that `declarative` is splitting the commands into a `create` and then an `alter` that sets the default, but only when there is something nontrivial as the default

Resolving patch:
diff --git a/edb/edgeql/declarative.py b/edb/edgeql/declarative.py
--- a/edb/edgeql/declarative.py
+++ b/edb/edgeql/declarative.py
@@ -1024,7 +1024,8 @@ def _register_item(
             elif (isinstance(cmd, qlast.SetField)
                   and not cmd.special_syntax
                   and not isinstance(cmd.value, qlast.BaseConstant)
-                  and not isinstance(op, qlast.CreateAlias)):
+                  and not isinstance(
+                      op, (qlast.CreateAlias, qlast.CreateGlobal))):
                 subcmds.append(cmd)
             else:
                 commands.append(cmd)


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -5860,7 +5860,7 @@ def test_schema_migrations_equivalence_globals_01(self):
             }
         """, r"""
             required global foo -> int64 {
-                default := 0;
+                default := 0 + 1;
             }
         """])
Repository: edgedb/edgedb

Issue:
`Unexpected EXPRESSION` during `edgedb migration create`
Starting schema:

```
type Test2 {
  property name -> str;
  access policy asdf allow all using true;
}
```

Create/apply migration

```
$ edgedb migration create
did you create object type 'default::Test2'? [y,n,l,c,b,s,q,?]
> y
Created dbschema/migrations/00042.edgeql, id: m1yrbvsi62ewhzkvl44oxo6ogbd7xx42rqdqxdq7kdkn7sf3ppjeza
$ edgedb migrate         
Applied m1yrbvsi62ewhzkvl44oxo6ogbd7xx42rqdqxdq7kdkn7sf3ppjeza (00042.edgeql)
```

Remove the `using` clause:

```diff
  type Test2 {
    property name -> str;
-   access policy asdf allow all using true;
+   access policy asdf allow all;
  }
```

Create migration

```
$ edgedb migration create
did you alter access policy 'asdf' of object type 'default::Test2'? [y,n,l,c,b,s,q,?]
> y
error: EdgeQLSyntaxError: Unexpected 'EXPRESSION'
  ┌─ query:2:36
  │
2 │     ALTER ACCESS POLICY asdf RESET EXPRESSION;
  │                                    ^^^^^^^^^^ error
Rolling back last operation...
```

You can work around this by answering `n`, in which case the migration will drop the old policy and add a new one with the same name.


Discussion:

Resolving patch:
diff --git a/edb/edgeql/parser/grammar/ddl.py b/edb/edgeql/parser/grammar/ddl.py
--- a/edb/edgeql/parser/grammar/ddl.py
+++ b/edb/edgeql/parser/grammar/ddl.py
@@ -1812,7 +1812,7 @@ def reduce_USING_ParenExpr(self, *kids):
             special_syntax=True,
         )
 
-    def reduce_RESET_USING(self, *kids):
+    def reduce_RESET_EXPRESSION(self, *kids):
         self.val = qlast.SetField(
             name='expr',
             value=None,


Test patch:
diff --git a/tests/test_edgeql_data_migration.py b/tests/test_edgeql_data_migration.py
--- a/tests/test_edgeql_data_migration.py
+++ b/tests/test_edgeql_data_migration.py
@@ -11073,6 +11073,19 @@ async def test_edgeql_migration_rename_with_stuff_01(self):
             """
         )
 
+    async def test_edgeql_migration_access_policy_01(self):
+        await self.migrate(r"""
+            type Test2 {
+                access policy asdf allow all using (true);
+            }
+        """)
+
+        await self.migrate(r"""
+            type Test2 {
+                access policy asdf allow all;
+            }
+        """)
+
     async def test_edgeql_migration_recovery(self):
         await self.con.execute(r"""
             START MIGRATION TO {
Repository: edgedb/edgedb

Issue:
links off of global variables don't get semijoined
```
global_semi> create type X;
OK: CREATE TYPE
global_semi> create type Y { create link x -> X };
insert OK: CREATE TYPE
global_semi> insert X;
[{"id": "2008da90-3877-11ed-9bb0-a773f2cf7623"}]
global_semi> insert Y { x := assert_single(X) };
[{"id": "24a14e52-3877-11ed-9bb0-fb0d250ace11"}]
global_semi> insert Y { x := assert_single(X) };
[{"id": "24a14e53-3877-11ed-9bb0-6fdc29f2a593"}]
global_semi> select Y.x;
[{"id": "2008da90-3877-11ed-9bb0-a773f2cf7623"}]
global_semi> create global Yg := Y;
OK: CREATE GLOBAL
global_semi> select (global Yg).x;
[
  {"id": "2008da90-3877-11ed-9bb0-a773f2cf7623"},
  {"id": "2008da90-3877-11ed-9bb0-a773f2cf7623"}
]
```


Discussion:
Looks like this is contributed to https://github.com/edgedb/edgedb/issues/4384

Resolving patch:
diff --git a/edb/edgeql/compiler/expr.py b/edb/edgeql/compiler/expr.py
--- a/edb/edgeql/compiler/expr.py
+++ b/edb/edgeql/compiler/expr.py
@@ -471,7 +471,10 @@ def compile_GlobalExpr(
     if glob.is_computable(ctx.env.schema):
         obj_ref = s_utils.name_to_ast_ref(
             glob.get_target(ctx.env.schema).get_name(ctx.env.schema))
-        return dispatch.compile(qlast.Path(steps=[obj_ref]), ctx=ctx)
+        # Wrap the reference in a subquery so that it does not get
+        # factored out or go directly into the scope tree.
+        qry = qlast.SelectQuery(result=qlast.Path(steps=[obj_ref]))
+        return dispatch.compile(qry, ctx=ctx)
 
     objctx = ctx.env.options.schema_object_context
     if objctx in (s_constr.Constraint, s_indexes.Index):


Test patch:
diff --git a/tests/test_edgeql_globals.py b/tests/test_edgeql_globals.py
--- a/tests/test_edgeql_globals.py
+++ b/tests/test_edgeql_globals.py
@@ -372,6 +372,21 @@ async def test_edgeql_globals_11(self):
             []
         )
 
+    async def test_edgeql_globals_12(self):
+        await self.con.execute('''
+            create global Ug := User
+        ''')
+
+        await self.assert_query_result(
+            r'''select count((global Ug).deck)''',
+            [9]
+        )
+
+        await self.assert_query_result(
+            r'''select count(((global Ug).id, (global Ug).name))''',
+            [16]
+        )
+
     async def test_edgeql_globals_state_cardinality(self):
         await self.con.execute('''
             set global cur_user := {};
Repository: edgedb/edgedb

Issue:
GROUP - Can't compile ref to visible binding
- EdgeDB Version: `2.1.0-dev.827+ea32987`
- OS Version: `20.04.1-Ubuntu`

Using example database, the query:
```
with grouped := (group Movie by .release_year)
for item in grouped union (select item.elements { title } )
```

gives
```
InternalServerError: Can't compile ref to visible binding ns~1@@(__derived__::g~1)
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
```

note that if you simply remove the reference to `title`, it works.
```
with grouped := (group Movie by .release_year)
for item in grouped union (select item.elements )
```






Discussion:
Thanks for the report!

As a workaround, don't bind the `group` result to a variable, just stick it into the select.
That's what I ended up doing! I wanted to create a free object with some other properties but will omit that for now :)
```
with grouped := (for x in Movie union { multi elements := x })
for item in grouped union (select item.elements { title } );
```
fails in a similar way that hopefully shares the root cause.

Resolving patch:
diff --git a/edb/edgeql/compiler/viewgen.py b/edb/edgeql/compiler/viewgen.py
--- a/edb/edgeql/compiler/viewgen.py
+++ b/edb/edgeql/compiler/viewgen.py
@@ -179,15 +179,15 @@ def _process_view(
     is_mutation = exprtype.is_insert() or exprtype.is_update()
     is_defining_shape = ctx.expr_exposed or is_mutation
 
-    orig_ir_set = ir_set
     ir_set = setgen.ensure_set(ir_set, type_override=view_scls, ctx=ctx)
-    # Materialize based on the original base pointer.
-    # This seems like a hack.
-    if orig_ir_set.rptr and orig_ir_set.rptr.ptrref.base_ptr:
-        ctx.env.schema, root_old_ptrcls = typeutils.ptrcls_from_ptrref(
-            orig_ir_set.rptr.ptrref.base_ptr, schema=ctx.env.schema
+    # Maybe rematerialize the set. The old ir_set might have already
+    # been materialized, but the new version would be missing from the
+    # use_sets.
+    if ir_set.rptr:
+        ctx.env.schema, remat_ptrcls = typeutils.ptrcls_from_ptrref(
+            ir_set.rptr.ptrref, schema=ctx.env.schema
         )
-        setgen.maybe_materialize(root_old_ptrcls, ir_set, ctx=ctx)
+        setgen.maybe_materialize(remat_ptrcls, ir_set, ctx=ctx)
 
     if view_rptr is not None and view_rptr.ptrcls is None:
         target_scls = stype if is_mutation else view_scls


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
@@ -735,9 +735,6 @@ async def _test_edgeql_group_by_group_by_03(self, qry):
 
         await self.assert_query_result(qry, res)
 
-    @test.xerror('''
-        Can't compile ref to visible binding ns~9@@(__derived__::g~2)
-    ''')
     async def test_edgeql_group_by_group_by_03a(self):
         await self._test_edgeql_group_by_group_by_03(
             '''
Repository: edgedb/edgedb

Issue:
Hung schema state involving alias
If we migrate to
```
module default {

  # XXX: If this is declared *after* User... it works??
  alias UserAlias := User;

  type User {
    multi link ml -> Target {
      property lp -> str;
    };
  }

  type Target;

}
```
And then try running another migration, it will fail.
The target schema it computes is missing `@lp` on the `UserAlias`.
Somehow, sinking the UserAlias after User gets it unstuck?


Discussion:

Resolving patch:
diff --git a/edb/schema/pointers.py b/edb/schema/pointers.py
--- a/edb/schema/pointers.py
+++ b/edb/schema/pointers.py
@@ -777,7 +777,8 @@ def allow_ref_propagation(
     ) -> bool:
         object_type = self.get_source(schema)
         if isinstance(object_type, s_types.Type):
-            return not object_type.is_view(schema)
+            return (
+                not object_type.is_view(schema) or refdict.attr == 'pointers')
         else:
             return True
 


Test patch:
diff --git a/tests/test_edgeql_data_migration.py b/tests/test_edgeql_data_migration.py
--- a/tests/test_edgeql_data_migration.py
+++ b/tests/test_edgeql_data_migration.py
@@ -11140,6 +11140,19 @@ async def test_edgeql_migration_link_to_sub_with_ref_01(self):
             }
         """)
 
+    async def test_edgeql_migration_alias_linkprop_01(self):
+        await self.migrate(r"""
+            alias UserAlias := User;
+
+            type User {
+              multi link ml -> Target {
+                property lp -> str;
+              };
+            }
+
+            type Target;
+        """)
+
 
 class TestEdgeQLDataMigrationNonisolated(EdgeQLDataMigrationTestCase):
     TRANSACTION_ISOLATION = False