Skip to content

Commit

Permalink
Fix ai::to_context duplication (#7464)
Browse files Browse the repository at this point in the history
The problem is that when we use the new inlined `body` field of
`FunctionCall`, the backend compiler still always compiles the
arguments, and since the arguments weren't registered in the scope
tree, the compiled arguments might not get picked up.

Fix this by always registering arguments when we produce an inlined body.
(We could *not* compile the arguments unconditionally for inlined
functions like this, but we'd still need to register the sets in case
the argument is used twice).

Fixes #7463.
  • Loading branch information
msullivan authored Jun 17, 2024
1 parent 00ef373 commit 772e120
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions edb/edgeql/compiler/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ def compile_FunctionCall(
else:
res = fcall

if isinstance(res, irast.FunctionCall) and res.body:
# If we are generating a special-cased inlined function call,
# make sure to register all the arguments in the scope tree
# to ensure that the compiled arguments get picked up when
# compiling the body.
for arg in res.args.values():
pathctx.register_set_in_scope(
arg.expr,
optional=arg.param_typemod == ft.TypeModifier.OptionalType,
ctx=ctx,
)

ir_set = setgen.ensure_set(res, typehint=rtype, path_id=path_id, ctx=ctx)
return stmt.maybe_add_view(ir_set, ctx=ctx)

Expand Down
3 changes: 3 additions & 0 deletions edb/edgeql/compiler/inference/multiplicity.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def __infer_func_call(
for g_arg in ir.global_args:
_infer_set(g_arg, scope_tree=scope_tree, ctx=ctx)

if ir.body:
infer_multiplicity(ir.body, scope_tree=scope_tree, ctx=ctx)

if card.is_single():
return UNIQUE
elif str(ir.func_shortname) == 'std::assert_distinct':
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/ext_ai.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Astronomy {
type Stuff extending Astronomy {
content2: str;
deferred index ext::ai::index(embedding_model := 'text-embedding-test')
on (.content ++ .content2);
on ({.content} ++ {.content2});
};

type Star extending Astronomy;
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ext_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ async def test_ext_ai_indexing_02(self):
variables=dict(qv=qv),
)

await self.assert_query_result(
'''
select _ := ext::ai::to_context((select Stuff))
order by _
''',
[
'Skies on Earth are blue',
'Skies on Mars are red',
],
)

async for tr in self.try_until_succeeds(
ignore=(AssertionError,),
timeout=10.0,
Expand Down Expand Up @@ -261,6 +272,17 @@ async def test_ext_ai_indexing_03(self):
""",
)

await self.assert_query_result(
'''
select _ := ext::ai::to_context((select Star))
order by _
''',
[
'Skies on Earth are blue',
'Skies on Mars are red',
],
)

async for tr in self.try_until_succeeds(
ignore=(AssertionError,),
timeout=10.0,
Expand Down

0 comments on commit 772e120

Please sign in to comment.