Skip to content

Commit

Permalink
Fix unused WITH DML when body is an INSERT (#6287)
Browse files Browse the repository at this point in the history
The problem was cardinality inference wasn't getting run on it.  If
the body of the WITH was something other than an INSERT, cardinality
inference happened somewhat by accident, since multiplicity inference
was done properly.

Fixes #6279.
  • Loading branch information
msullivan committed Oct 13, 2023
1 parent e2d18c6 commit dda8942
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions edb/edgeql/compiler/inference/cardinality.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,9 @@ def _infer_stmt_cardinality(
scope_tree: irast.ScopeTreeNode,
ctx: inference_context.InfCtx,
) -> qltypes.Cardinality:
for part in (ir.bindings or []):
infer_cardinality(part, scope_tree=scope_tree, ctx=ctx)

result = ir.subject if isinstance(ir, irast.MutatingStmt) else ir.result
result_card = infer_cardinality(
result,
Expand Down Expand Up @@ -1278,6 +1281,8 @@ def __infer_insert_stmt(
scope_tree: irast.ScopeTreeNode,
ctx: inference_context.InfCtx,
) -> qltypes.Cardinality:
for part in (ir.bindings or []):
infer_cardinality(part, scope_tree=scope_tree, ctx=ctx)

infer_cardinality(
ir.subject, is_mutation=True, scope_tree=scope_tree, ctx=ctx
Expand Down
7 changes: 6 additions & 1 deletion edb/edgeql/compiler/inference/multiplicity.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def _infer_stmt_multiplicity(
scope_tree: irast.ScopeTreeNode,
ctx: inf_ctx.InfCtx,
) -> inf_ctx.MultiplicityInfo:
# WITH block bindings need to be validated, they don't have to
# WITH block bindings need to be validated; they don't have to
# have multiplicity UNIQUE, but their sub-expressions must be valid.
for part in (ir.bindings or []):
infer_multiplicity(part, scope_tree=scope_tree, ctx=ctx)
Expand Down Expand Up @@ -649,6 +649,11 @@ def __infer_insert_stmt(
scope_tree: irast.ScopeTreeNode,
ctx: inf_ctx.InfCtx,
) -> inf_ctx.MultiplicityInfo:
# WITH block bindings need to be validated, they don't have to
# have multiplicity UNIQUE, but their sub-expressions must be valid.
for part in (ir.bindings or []):
infer_multiplicity(part, scope_tree=scope_tree, ctx=ctx)

# INSERT will always return a proper set, but we still want to
# process the sub-expressions.
infer_multiplicity(
Expand Down
29 changes: 29 additions & 0 deletions tests/test_edgeql_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@ async def test_edgeql_insert_unused_01(self):
]
)

await self.con.execute(r"""
with _ := (
INSERT InsertTest {
name := 'insert simple 01',
l2 := (select 1 filter true),
}
),
INSERT InsertTest {
name := 'insert simple 01',
l2 := 2,
}
""")
await self.assert_query_result(
r"""
SELECT
InsertTest {
l2
}
FILTER
InsertTest.name = 'insert simple 01'
ORDER BY .l2
""",
[
{'l2': 0},
{'l2': 1},
{'l2': 2},
]
)

async def test_edgeql_insert_nested_01(self):
await self.con.execute('''
INSERT Subordinate {
Expand Down

0 comments on commit dda8942

Please sign in to comment.