Skip to content

Commit

Permalink
[5.x] Fix make sure collection types land in env.created_schema_types (
Browse files Browse the repository at this point in the history
…#7375)

Co-authored-by: Aljaž Mur Eržen <[email protected]>
  • Loading branch information
msullivan and aljazerzen authored May 23, 2024
1 parent 5aac5b3 commit 6663b43
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 15 deletions.
11 changes: 8 additions & 3 deletions edb/edgeql/compiler/setgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ def new_tuple_set(
) -> irast.Set:

element_types = {el.name: get_set_type(el.val, ctx=ctx) for el in elements}
ctx.env.schema, stype = s_types.Tuple.create(
ctx.env.schema, stype, has_been_created = s_types.Tuple.create(
ctx.env.schema, element_types=element_types, named=named)
if has_been_created:
ctx.env.created_schema_objects.add(stype)
result_path_id = pathctx.get_expression_path_id(stype, ctx=ctx)

final_elems = []
Expand Down Expand Up @@ -284,8 +286,11 @@ def new_array_set(

if stype is None:
assert element_type
ctx.env.schema, stype = s_types.Array.from_subtypes(
ctx.env.schema, [element_type])
ctx.env.schema, stype, has_created = s_types.Array.create(
ctx.env.schema, element_type=element_type, dimensions=[-1]
)
if has_created:
ctx.env.created_schema_objects.add(stype)
typeref = typegen.type_to_typeref(stype, env=ctx.env)
arr = irast.Array(elements=elements, typeref=typeref)
return ensure_set(arr, type_override=stype, ctx=ctx)
Expand Down
9 changes: 6 additions & 3 deletions edb/edgeql/compiler/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,14 @@ def compile_InternalGroupQuery(
viewgen.late_compile_view_shapes(stmt.group_binding, ctx=topctx)

if expr.grouping_alias:
ctx.env.schema, grouping_stype = s_types.Array.create(
ctx.env.schema, grouping_stype, created = s_types.Array.create(
ctx.env.schema,
element_type=ctx.env.schema.get(
'std::str', type=s_types.Type)
element_type=(
ctx.env.schema.get('std::str', type=s_types.Type)
)
)
if created:
ctx.env.created_schema_objects.add(grouping_stype)
stmt.grouping_binding = _make_group_binding(
grouping_stype, expr.grouping_alias, ctx=topctx)

Expand Down
2 changes: 2 additions & 0 deletions edb/pgsql/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,6 @@ def _setup_patches(patches: list[tuple[str, str]]) -> list[tuple[str, str]]:
SET force_return_cast := true;
};
'''),
# === 5.4
('repair', ''), # for #7375
])
2 changes: 1 addition & 1 deletion edb/schema/rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def compile_expr_field(
)
# __specified__
bool_type = schema.get("std::bool", type=s_types.Type)
schema, specified_type = s_types.Tuple.create(
schema, specified_type, _ = s_types.Tuple.create(
schema,
named=True,
element_types={
Expand Down
20 changes: 14 additions & 6 deletions edb/schema/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ def create(
dimensions: Sequence[int] = (),
element_type: Any,
**kwargs: Any,
) -> typing.Tuple[s_schema.Schema, Array_T]:
) -> typing.Tuple[s_schema.Schema, Array_T, bool]:
if not dimensions:
dimensions = [-1]

Expand All @@ -1266,7 +1266,10 @@ def create(
else:
result = schema.get_global(cls, name, default=None)

has_been_created = False

if result is None:
has_been_created = True
schema, result = super().create_in_schema(
schema,
id=id,
Expand All @@ -1278,7 +1281,7 @@ def create(
# Compute material type so that we can retrieve it safely later
schema, _ = result.material_type(schema)

return schema, result
return schema, result, has_been_created

def get_generated_name(self, schema: s_schema.Schema) -> s_name.UnqualName:
return type(self).generate_name(
Expand Down Expand Up @@ -1450,13 +1453,14 @@ def from_subtypes(
# One-dimensional unbounded array.
dimensions = [-1]

return cls.create(
schema, ty, _ = cls.create(
schema,
element_type=stype,
dimensions=dimensions,
name=name,
**kwargs,
)
return schema, ty

@classmethod
def create_shell(
Expand Down Expand Up @@ -1646,7 +1650,7 @@ def create(
element_types: Mapping[str, Type],
named: bool = False,
**kwargs: Any,
) -> typing.Tuple[s_schema.Schema, Tuple_T]:
) -> typing.Tuple[s_schema.Schema, Tuple_T, bool]:
el_types = so.ObjectDict[str, Type].create(schema, element_types)
if name is None:
name = cls.generate_name(
Expand All @@ -1659,7 +1663,10 @@ def create(
else:
result = schema.get_global(cls, name, default=None)

has_been_created = False

if result is None:
has_been_created = True
schema, result = super().create_in_schema(
schema,
id=id,
Expand All @@ -1671,7 +1678,7 @@ def create(
# Compute material type so that we can retrieve it safely later
schema, _ = result.material_type(schema)

return schema, result
return schema, result, has_been_created

def get_generated_name(self, schema: s_schema.Schema) -> s_name.UnqualName:
els = {n: st.get_name(schema) for n, st in self.iter_subtypes(schema)}
Expand Down Expand Up @@ -1789,8 +1796,9 @@ def from_subtypes(
types = subtypes
else:
types = {str(i): type for i, type in enumerate(subtypes)}
return cls.create(
schema, ty, _ = cls.create(
schema, element_types=types, named=named, name=name, **kwargs)
return schema, ty

@classmethod
def create_shell(
Expand Down
10 changes: 10 additions & 0 deletions tests/schemas/cards.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,13 @@ alias UserAlias := (
alias SpecialCardAlias := SpecialCard {
el_cost := (.element, .cost)
};

# This is pulled from test_edgeql_ddl_collection_cleanup_05 to test
# that schemas with tuple aliases like this work properly after the
# fix in #7375.
# This works because one of the patches tests creates the cards database
# before the upgrade and then migrates to it after.
# (See .github/scripts/patches/create-databases.py)
scalar type alias_test_a extending str;
scalar type alias_test_b extending str;
alias alias_test_Bar := (<alias_test_a>"", <alias_test_b>"");
4 changes: 2 additions & 2 deletions tests/test_edgeql_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14496,7 +14496,7 @@ async def test_edgeql_ddl_collection_cleanup_05(self):

self.assertEqual(
await self.con.query_single(count_query),
orig_count + 1,
orig_count + 2, # one for tuple<str, str>, one for TupleExprAlias
)

await self.con.execute(r"""
Expand All @@ -14505,7 +14505,7 @@ async def test_edgeql_ddl_collection_cleanup_05(self):

self.assertEqual(
await self.con.query_single(count_query),
orig_count + 1,
orig_count + 2,
)

await self.con.execute(r"""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_edgeql_expr_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,3 +1114,16 @@ async def test_edgeql_aliases_detached_01(self):
""",
[4]
)

async def test_edgeql_aliases_coll_types_01(self):
await self.con.execute(
r"""
create type X;
create global y := (select
(a := 'hello', b := [(select X limit 1)])
);
create alias z := (
a := 'hello', b := [(select X limit 1)]
);
"""
)

0 comments on commit 6663b43

Please sign in to comment.