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

Issue:
Migration applying fails when global depends on an altered type
- EdgeDB Version: 2.2@sha256:cf3499f2dacf740fee19d0a6758a5f725449da69cf9f3ba96d4e37258d20d503 (Docker)
- OS Version: Windows 10 21H2 (build 19044.2006)

Error:
```
edgedb error: InternalServerError: relation "edgedbpub.e93a7d31-383c-11ed-9af0-912f6f0aae4b" 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: relation "edgedbpub.e93a7d31-383c-11ed-9af0-912f6f0aae4b" does not exist
```

Steps to Reproduce:
1. Create and apply initial migration (listed below without + fields)
2. Add the `password` field (or any other) to the `User` type
3. Create and try to apply migration with the new schema

Schema:
```diff
module default {
    global userId -> uuid;
    global currentUser := (select User filter .id ?= global userId);

    type User {
      required property username -> str;
+     required property password -> str;
    };
};
```


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
@@ -1848,6 +1848,7 @@ def as_inherited_ref_cmd(
             )
         ):
             cmd.set_attribute_value('from_alias', True)
+            cmd.set_object_aux_data('from_alias', True)
 
         return cmd
 


Test patch:
diff --git a/tests/test_edgeql_ddl.py b/tests/test_edgeql_ddl.py
--- a/tests/test_edgeql_ddl.py
+++ b/tests/test_edgeql_ddl.py
@@ -14705,6 +14705,17 @@ async def test_edgeql_ddl_rebase_views_02(self):
             [{}],
         )
 
+    async def test_edgeql_ddl_alias_and_create_set_required(self):
+        await self.con.execute(r"""
+            create type T;
+            create alias A := T;
+            alter type T {
+                create required property bar -> str {
+                    set required using ('!')
+                }
+            };
+        """)
+
 
 class TestConsecutiveMigrations(tb.DDLTestCase):
     TRANSACTION_ISOLATION = False
Repository: edgedb/edgedb

Issue:
ISE when casting result of `json_set`
```
edgedb> select <tuple<str>>json_set(to_json('["b"]'), "0", value := <json>"a");
edgedb error: InternalServerError: type "edgedbpub.7f1aef75-4be4-11ed-a553-a70cebd6e089_domain" 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: type "edgedbpub.7f1aef75-4be4-11ed-a553-a70cebd6e089_domain" does not exist
```


Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/pathctx.py b/edb/edgeql/compiler/pathctx.py
--- a/edb/edgeql/compiler/pathctx.py
+++ b/edb/edgeql/compiler/pathctx.py
@@ -31,6 +31,7 @@
 
 from edb.schema import name as s_name
 from edb.schema import pointers as s_pointers
+from edb.schema import schema as s_schema
 from edb.schema import types as s_types
 
 from . import context
@@ -76,7 +77,17 @@ def get_expression_path_id(
         ctx: context.ContextLevel) -> irast.PathId:
     if alias is None:
         alias = ctx.aliases.get('expr')
-    typename = s_name.QualName(module='__derived__', name=alias)
+    name = stype.get_name(ctx.env.schema)
+    # If the type comes from one of our standard modules, we need to
+    # make our derived typename also be from a standard module, so that
+    # if we need to generate a cast on the backend we can detect that
+    # it is a stdlib type and not a user one.
+    in_std_module = (
+        isinstance(name, s_name.QualName)
+        and s_name.UnqualName(name.module) in s_schema.STD_MODULES
+    )
+    modname = '__std_derived__' if in_std_module else '__derived__'
+    typename = s_name.QualName(module=modname, name=alias)
     return get_path_id(stype, typename=typename, ctx=ctx)
 
 
diff --git a/edb/schema/schema.py b/edb/schema/schema.py
--- a/edb/schema/schema.py
+++ b/edb/schema/schema.py
@@ -54,6 +54,7 @@
     ]
 
 STD_MODULES = (
+    sn.UnqualName('__std_derived__'),
     sn.UnqualName('std'),
     sn.UnqualName('schema'),
     sn.UnqualName('math'),


Test patch:
diff --git a/tests/test_edgeql_casts.py b/tests/test_edgeql_casts.py
--- a/tests/test_edgeql_casts.py
+++ b/tests/test_edgeql_casts.py
@@ -2783,6 +2783,23 @@ async def test_edgeql_casts_custom_scalars_02(self):
                 SELECT <array<foo>><array<bar>>['test']
             """)
 
+    async def test_edgeql_casts_std_enum_01(self):
+        await self.assert_query_result(
+            '''
+            select <schema::Cardinality>{}
+            ''',
+            [],
+        )
+
+    async def test_edgeql_casts_json_set_02(self):
+        await self.assert_query_result(
+            '''
+            select <tuple<str>>json_set(
+                to_json('["b"]'), "0", value := <json>"a");
+            ''',
+            [('a',)],
+        )
+
     async def test_edgeql_casts_all_null(self):
         # For *every* concrete cast, try casting a value we know
         # will be represented as NULL.
Repository: edgedb/edgedb

Issue:
Incorrect error message hint for InvalidValueError of std::duration
```
edgedb> select <duration>"1 day";
edgedb error: InvalidValueError: invalid input syntax for type std::duration: '1 day'
  Hint: Units bigger than days cannot be used for std::duration.
```

**Expected**: Hint should say "hours", not "days".


Discussion:

Resolving patch:
diff --git a/edb/pgsql/metaschema.py b/edb/pgsql/metaschema.py
--- a/edb/pgsql/metaschema.py
+++ b/edb/pgsql/metaschema.py
@@ -2473,7 +2473,7 @@ class DurationInFunction(dbops.Function):
                         || quote_literal(val)
                     ),
                     detail => (
-                        '{"hint":"Units bigger than hours cannot be used '
+                        '{"hint":"Day, month and year units cannot be used '
                         || 'for std::duration."}'
                     )
                 )


Test patch:
Repository: edgedb/edgedb

Issue:
operator does not exist: text = uuid
<!-- Please search existing issues to avoid creating duplicates. -->
- OS Version: ubuntu 22.04

Steps to Reproduce:
```
$ edgedb
EdgeDB 2.6+ba1661b (repl 2.1.0-dev.865+40c556b)
Type \help for help, \quit to quit.

edgedb> insert User;
{default::User {id: 0890545c-5f90-11ed-b76c-cfa33c8c7cb7}}

edgedb> insert User;
{default::User {id: 0acb5a5a-5f90-11ed-b76c-93d2fb46bccc}}

edgedb> insert Message;
edgedb error: InternalServerError: operator does not exist: text = uuid
  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: operator does not exist: text = uuid
```

## Schema
```
module default {
  type User {}

  type Message {
    link sender -> User;
    link recipient -> User;

    access policy at_least_sender_or_recipient
    allow all
    using (count((.sender union .recipient)) > 0)
  }
};
```



Discussion:

Resolving patch:
diff --git a/edb/pgsql/compiler/pathctx.py b/edb/pgsql/compiler/pathctx.py
--- a/edb/pgsql/compiler/pathctx.py
+++ b/edb/pgsql/compiler/pathctx.py
@@ -1196,6 +1196,13 @@ def _get_path_output(
             if alias is None:
                 alias = get_path_output_alias(path_id, aspect, env=env)
 
+            if isinstance(ref, pgast.NullConstant):
+                pg_type = pg_types.pg_type_from_ir_typeref(path_id.target)
+
+                ref = pgast.TypeCast(
+                    arg=ref, type_name=pgast.TypeName(name=pg_type)
+                )
+
             restarget = pgast.ResTarget(
                 name=alias, val=ref, ser_safe=getattr(ref, 'ser_safe', False))
             rel.target_list.append(restarget)


Test patch:
diff --git a/tests/test_edgeql_policies.py b/tests/test_edgeql_policies.py
--- a/tests/test_edgeql_policies.py
+++ b/tests/test_edgeql_policies.py
@@ -83,6 +83,13 @@ class TestEdgeQLPolicies(tb.QueryTestCase):
                     or (global cur_user in .name) ?? false
                 );
             };
+
+            create type Message {
+                create link attachment -> Issue;
+                create access policy has_attachment
+                    allow all
+                    using (count(.attachment) > 0);
+            };
         '''
     ]
 
@@ -472,6 +479,11 @@ async def test_edgeql_policies_09(self):
                 unless conflict on (.name) else (select X)
             ''')
 
+    async def test_edgeql_policies_10(self):
+        # see issue https://github.com/edgedb/edgedb/issues/4646
+        async with self.assertRaisesRegexTx(edgedb.AccessPolicyError, ''):
+            await self.con.execute('insert Message {}')
+
     async def test_edgeql_policies_order_01(self):
         await self.con.execute('''
             insert CurOnly { name := "!" }
Repository: edgedb/edgedb

Issue:
Busted cardinality inference when filtering multisets
If we have a filter on an exclusive constraint on a set with duplicates, we can incorrectly infer a single cardinality.

```
with X := {User, User},
select { alice := (select X filter .name = 'Alice') }; 
```
gives the dreaded
```
edgedb error: InternalServerError: more than one row returned by a subquery used as an expression
```

Conceptually the solution is to only apply the filter checks when multiplicity inference says it is a proper set, but we need to make sure there isn't a mutual recursion problem there.


Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/inference/cardinality.py b/edb/edgeql/compiler/inference/cardinality.py
--- a/edb/edgeql/compiler/inference/cardinality.py
+++ b/edb/edgeql/compiler/inference/cardinality.py
@@ -50,6 +50,7 @@
 from . import context as inference_context
 from . import utils as inf_utils
 from . import volatility
+from . import multiplicity
 
 from .. import context
 
@@ -1144,8 +1145,9 @@ def _infer_stmt_cardinality(
     scope_tree: irast.ScopeTreeNode,
     ctx: inference_context.InfCtx,
 ) -> qltypes.Cardinality:
+    result = ir.subject if isinstance(ir, irast.MutatingStmt) else ir.result
     result_card = infer_cardinality(
-        ir.subject if isinstance(ir, irast.MutatingStmt) else ir.result,
+        result,
         is_mutation=isinstance(ir, irast.MutatingStmt),
         scope_tree=scope_tree,
         ctx=ctx,
@@ -1158,8 +1160,15 @@ def _infer_stmt_cardinality(
         result_card = cartesian_cardinality([result_card, AT_MOST_ONE])
 
     if result_card.is_multi() and ir.where:
-        result_card = _analyse_filter_clause(
-            ir.result, result_card, ir.where, scope_tree, ctx)
+        result_mult = multiplicity.infer_multiplicity(
+            result, scope_tree=scope_tree, ctx=ctx)
+
+        # We can only apply filter clause restrictions when the result
+        # is a unique set, because if the set has duplicates we can
+        # also pick out duplicates.
+        if result_mult.is_unique():
+            result_card = _analyse_filter_clause(
+                ir.result, result_card, ir.where, scope_tree, ctx)
 
     _infer_matset_cardinality(
         ir.materialized_sets, scope_tree=scope_tree, ctx=ctx)


Test patch:
diff --git a/tests/test_edgeql_ir_card_inference.py b/tests/test_edgeql_ir_card_inference.py
--- a/tests/test_edgeql_ir_card_inference.py
+++ b/tests/test_edgeql_ir_card_inference.py
@@ -1044,6 +1044,22 @@ def test_edgeql_ir_card_inference_119(self):
     def test_edgeql_ir_card_inference_120(self):
         """
         select 1 + ({2, 3} except {3, 4})
+% OK %
+        MANY
+        """
+
+    def test_edgeql_ir_card_inference_121(self):
+        """
+        with X := {User, User},
+        select X filter .name = 'Alice'
+% OK %
+        MANY
+        """
+
+    def test_edgeql_ir_card_inference_122(self):
+        """
+        with X := {User, User},
+        update X filter .name = 'Alice' set { }
 % OK %
         MANY
         """
Repository: edgedb/edgedb

Issue:
Reset schema
Closes #4351 
`RESET SCHEMA TO INITIAL` leaves database in broken state
Somehow the schema version id (`__schema_version__`/`edgedb."_SchemaSchemaVersion"`) stored in the database is getting fouled up after running `RESET SCHEMA TO INITIAL`, such that subsequent DDL commands produce
```
edgedb.errors.TransactionSerializationError: Cannot serialize DDL: 0f51bd2b-72aa-11ed-9fc6-61c4bb93e1e1
```

It seems that `RESET SCHEMA TO INITIAL` leaves the database in a broken state where no DDL can be run successfully. This doesn't cause any problems in the normal testing mode because `test_edgeql_migration_reset_schema` happens to be the last test in that test suite that actually tries to modify the database.
(I ran into this trying to debug the NOCREATEDB test mode, which needs to reuse the same database for everything and where this ends up as a huge problem)

This can be observed by adding a `await self.con.query('create module default')` to the end of `test_edgeql_migration_reset_schema`.

(Related to that, reset schema should probably include an empty `module default` in the schema)


Discussion:

Resolving patch:
diff --git a/edb/server/compiler/ddl.py b/edb/server/compiler/ddl.py
--- a/edb/server/compiler/ddl.py
+++ b/edb/server/compiler/ddl.py
@@ -1103,10 +1103,22 @@ def _reset_schema(
         s_schema.FlatSchema(),
         current_tx.get_global_schema(),
     )
+    empty_schema = s_ddl.apply_sdl(  # type: ignore
+        qlast.Schema(
+            declarations=[
+                qlast.ModuleDeclaration(
+                    name=qlast.ObjectRef(name='default'),
+                    declarations=[],
+                )
+            ]
+        ),
+        base_schema=empty_schema,
+        current_schema=empty_schema,
+    )
 
     sqls: List[bytes] = []
 
-    # diff and create migation that drops all objects
+    # diff and create migration that drops all objects
     diff = s_ddl.delta_schemas(schema, empty_schema)
     new_ddl: Tuple[qlast.DDLCommand, ...] = tuple(
         s_ddl.ddlast_from_delta(schema, empty_schema, diff),  # type: ignore
@@ -1139,6 +1151,6 @@ def _reset_schema(
         cacheable=False,
         modaliases=None,
         single_unit=True,
-        user_schema=empty_schema.get_top_schema(),
+        user_schema=current_tx.get_user_schema(),
         cached_reflection=(current_tx.get_cached_reflection_if_updated()),
     )


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
@@ -11497,6 +11497,15 @@ async def test_edgeql_migration_reset_schema(self):
         ''')
         self.assertEqual(res, [])
 
+        await self.migrate(r'''
+            type SomethingElse;
+        ''')
+
+        res = await self.con.query('''
+            select schema::Migration { script, name };
+        ''')
+        self.assertEqual(len(res), 1)
+
 
 class EdgeQLMigrationRewriteTestCase(EdgeQLDataMigrationTestCase):
     DEFAULT_MODULE = 'default'
Repository: edgedb/edgedb

Issue:
`random` is apparently called only run once per type in an update query
<!-- Please search existing issues to avoid creating duplicates. -->

<!--
For the EdgeDB Version: run `edgedb query 'select sys::get_version_as_str()'` from your project directory (or run `select sys::get_version_as_str();` in the EdgeDB interactive shell).
For the EdgeDB CLI Version: Run `edgedb --version` from anywhere
-->

- EdgeDB Version: 2.7+a86e274
- EdgeDB CLI Version: 2.2.6+7eabbf9
- OS Version: macOS 13.0.1

Steps to Reproduce:

1. Create multiple objects, each containing a numeric type
2. Run an `update` query for the type, setting the values of the numeric type to a random value of that type (e.g., `<int16>round(random() * 5)` for a random `int16` 0 to 5).
3. Observe that the random values produced by the `update` are all identical

Note that if you create several types extended from a single type and run your update query on the parent type, each of the sub-types will get a distinct random value, but that same value will be used across all objects of that sub-type.



Discussion:

Resolving patch:
diff --git a/edb/pgsql/compiler/dml.py b/edb/pgsql/compiler/dml.py
--- a/edb/pgsql/compiler/dml.py
+++ b/edb/pgsql/compiler/dml.py
@@ -1359,6 +1359,9 @@ def process_update_body(
         subctx.expr_exposed = False
         subctx.enclosing_cte_iterator = iterator
 
+        clauses.setup_iterator_volatility(
+            iterator, is_cte=True, ctx=subctx)
+
         for shape_el, shape_op in ir_stmt.subject.shape:
             if shape_op == qlast.ShapeOp.MATERIALIZE:
                 continue


Test patch:
diff --git a/tests/test_edgeql_update.py b/tests/test_edgeql_update.py
--- a/tests/test_edgeql_update.py
+++ b/tests/test_edgeql_update.py
@@ -3671,3 +3671,29 @@ async def test_edgeql_update_inject_intersection_02(self):
             """,
             [],
         )
+
+    async def test_edgeql_update_volatility_01(self):
+        # random should be executed once for each object
+        await self.con.execute(r"""
+            update UpdateTest set { comment := <str>random() };
+        """)
+
+        await self.assert_query_result(
+            r"""
+                select count(distinct UpdateTest.comment) = count(UpdateTest)
+            """,
+            [True],
+        )
+
+    async def test_edgeql_update_volatility_02(self):
+        # random should be executed once for each object
+        await self.con.execute(r"""
+            update UpdateTest set { str_tags := <str>random() };
+        """)
+
+        await self.assert_query_result(
+            r"""
+                select count(distinct UpdateTest.str_tags) = count(UpdateTest)
+            """,
+            [True],
+        )
Repository: edgedb/edgedb

Issue:
`InvalidReferenceError` on group queries generated by ts querybuilder
- EdgeDB Version: EdgeDB 3.0-dev.7275+local

Steps to Reproduce:

```
_example> WITH
.........   __scope_0_defaultMovie := DETACHED default::Movie,
.........   __scope_0_defaultMovie_groups := (
.........     GROUP __scope_0_defaultMovie
.........     USING
.........       release_year := __scope_0_defaultMovie.release_year
.........     BY release_year
......... )
......... SELECT __scope_0_defaultMovie_groups {
.........   key: {release_year},
.........   grouping,
.........   elements: {
.........     release_year,
.........     title,
.........     multi actors := (
.........       WITH
.........         __scope_1_defaultPerson := __scope_0_defaultMovie_groups.elements.actors
.........       SELECT __scope_1_defaultPerson {
.........         name
.........       }
.........     )
.........   }
......... };
error: InvalidReferenceError: object type or alias 'default::__scope_0_defaultMovie_groups' does not exist
   ┌─ query:17:36
   │
17 │         __scope_1_defaultPerson := __scope_0_defaultMovie_groups.elements.actors
   │                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error

```

Schema: UI `_example` schema


Discussion:

Resolving patch:
diff --git a/edb/edgeql/desugar_group.py b/edb/edgeql/desugar_group.py
--- a/edb/edgeql/desugar_group.py
+++ b/edb/edgeql/desugar_group.py
@@ -174,7 +174,7 @@ def _count_alias_uses(
     for child in ast.find_children(node, lambda x: isinstance(x, qlast.Path)):
         match child:
             case qlast.Path(steps=[
-                qlast.ObjectRef(module=None, name=alias2)
+                qlast.ObjectRef(module=None, name=alias2), *_,
             ]) if alias == alias2:
                 uses += 1
     return uses


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
@@ -1329,3 +1329,19 @@ async def test_edgeql_group_enumerate_02(self):
                 }
             ]),
         )
+
+    async def test_edgeql_group_uses_name_01(self):
+        # Make sure that our crappy optimizations don't break anything
+        await self.con.query(
+            r'''
+            WITH g := (GROUP cards::Card BY .cost)
+            SELECT g {
+              key: {cost},
+              grouping,
+              elements: {
+                name,
+                multi owners := g.elements.owners { name },
+              }
+            };
+            ''',
+        )
Repository: edgedb/edgedb

Issue:
No query results when optional tuple arg is not provided
- EdgeDB Version: EdgeDB 3.0-dev.7275+local
- EdgeDB CLI Version: n/a

Steps to Reproduce:

```dart
import 'package:edgedb/edgedb.dart';

final client = createClient(instanceName: '_localdev', database: '_example');

void main() async {
  print(await client.query(
      r'''
      select Movie {
        title,
        computed := <optional tuple<str, int64>>$test
      } filter .actors.name = <str>$name
      ''',
      {
        'name': 'Lee Pace',
        'test': ['test str', 123]
      }));
  // => [{title: Guardians of the Galaxy, computed: [test str, 123]}]
  // expected result

  print(await client.query(
      r'''
      select Movie {
        title,
        computed := <optional tuple<str, int64>>$test
      } filter .actors.name = <str>$name
      ''',
      {
        'name': 'Lee Pace',
      }));
  // => []
  // when optional tuple is not provided, no result at all
  // expected result:
  // [{title: Guardians of the Galaxy, computed: null}]

  print(await client.query(
      r'''
      select Movie {
        title,
        computed := <optional str>$test
      } filter .actors.name = <str>$name
      ''',
      {
        'name': 'Lee Pace',
      }));
  // => [{title: Guardians of the Galaxy, computed: null}]
  // appears to work correctly with simple optional scalar arg

  client.close();
}
```

Schema: UI '_example' schema



Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/tuple_args.py b/edb/edgeql/compiler/tuple_args.py
--- a/edb/edgeql/compiler/tuple_args.py
+++ b/edb/edgeql/compiler/tuple_args.py
@@ -236,6 +236,10 @@ def make_decoder(
         qlast.TypeCast(
             expr=qlast.Parameter(name=param.name),
             type=_ref_to_ast(param.ir_type, ctx=ctx),
+            cardinality_mod=(
+                qlast.CardinalityModifier.Optional if not param.required
+                else None
+            ),
         )
         for param in qparams
     ]


Test patch:
diff --git a/tests/test_edgeql_casts.py b/tests/test_edgeql_casts.py
--- a/tests/test_edgeql_casts.py
+++ b/tests/test_edgeql_casts.py
@@ -2767,6 +2767,23 @@ async def test_edgeql_casts_tuple_params_07(self):
             variables=(('a', True),)
         )
 
+    async def test_edgeql_casts_tuple_params_08(self):
+        await self.assert_query_result(
+            '''
+            select { x := <optional tuple<str, str>>$0, y := <str>$1 };
+            ''',
+            [{'x': None, 'y': "test"}],
+            variables=(None, 'test'),
+        )
+
+        await self.assert_query_result(
+            '''
+            select { x := <optional tuple<str, str>>$0, y := <int64>$1 };
+            ''',
+            [{'x': None, 'y': 11111}],
+            variables=(None, 11111),
+        )
+
     async def test_edgeql_cast_empty_set_to_array_01(self):
         await self.assert_query_result(
             r'''