Skip to content

Commit

Permalink
Avoid computing globals json if not needed for inlined function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnwpark committed Oct 25, 2024
1 parent e6a7414 commit f68a2e9
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 1 deletion.
20 changes: 19 additions & 1 deletion edb/edgeql/compiler/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,25 @@ def compile_FunctionCall(
else:
tuple_path_ids = []

global_args = get_globals(expr, matched_call, candidates=funcs, ctx=ctx)
global_args = None
if inline_func:
def find_using_global(node: irast.FunctionCall) -> bool:
return node.global_args is not None and len(node.global_args) > 0

if ast.find_children(
inline_func,
irast.FunctionCall,
test_func=find_using_global,
terminate_early=True,
):
global_args = get_globals(
expr, matched_call, candidates=funcs, ctx=ctx
)

else:
global_args = get_globals(
expr, matched_call, candidates=funcs, ctx=ctx
)

fcall = irast.FunctionCall(
args=final_args,
Expand Down
235 changes: 235 additions & 0 deletions tests/test_edgeql_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13284,6 +13284,241 @@ async def test_edgeql_functions_inline_nested_global_04(self):
sort=True,
)

async def test_edgeql_functions_inline_nested_global_05(self):
# Use computed global in inner non-inlined function
# - inlined > non-inlined
await self.con.execute('''
create global a := 1;
create function inner(x: int64) -> int64 {
using (global a + x);
};
create function foo(x: int64) -> int64 {
set is_inlined := true;
using (inner(x));
};
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[2],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[2, 3, 4],
sort=True,
)

async def test_edgeql_functions_inline_nested_global_06(self):
# Use non-computed global in inner non-inlined function
# - inlined > non-inlined
await self.con.execute('''
create global a -> int64;
create function inner(x: int64) -> optional int64 {
using (global a + x);
};
create function foo(x: int64) -> optional int64 {
set is_inlined := true;
using (inner(x));
};
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[],
sort=True,
)

await self.con.execute('''
set global a := 1;
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[2],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[2, 3, 4],
sort=True,
)

async def test_edgeql_functions_inline_nested_global_07(self):
# Use computed global in deeply nested inner non-inlined function
# - inlined > inlined > inlined > non-inlined
await self.con.execute('''
create global a := 1;
create function inner1(x: int64) -> int64 {
using (global a + x);
};
create function inner2(x: int64) -> int64 {
set is_inlined := true;
using (inner1(x));
};
create function inner3(x: int64) -> int64 {
set is_inlined := true;
using (inner2(x));
};
create function foo(x: int64) -> int64 {
set is_inlined := true;
using (inner3(x));
};
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[2],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[2, 3, 4],
sort=True,
)

async def test_edgeql_functions_inline_nested_global_08(self):
# Use computed global in deeply nested inner non-inlined function
# - inlined > inlined > inlined > non-inlined
await self.con.execute('''
create global a -> int64;
create function inner1(x: int64) -> optional int64 {
using (global a + x);
};
create function inner2(x: int64) -> optional int64 {
set is_inlined := true;
using (inner1(x));
};
create function inner3(x: int64) -> optional int64 {
set is_inlined := true;
using (inner2(x));
};
create function foo(x: int64) -> optional int64 {
set is_inlined := true;
using (inner3(x));
};
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[],
sort=True,
)

await self.con.execute('''
set global a := 1;
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[2],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[2, 3, 4],
sort=True,
)

async def test_edgeql_functions_inline_nested_global_09(self):
# Use computed global nested in non-inlined function
# - non-inlined > inlined > non-inlined
await self.con.execute('''
create global a := 1;
create function inner1(x: int64) -> int64 {
using (global a + x);
};
create function inner2(x: int64) -> int64 {
set is_inlined := true;
using (inner1(x));
};
create function foo(x: int64) -> int64 {
using (inner2(x));
};
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[2],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[2, 3, 4],
sort=True,
)

async def test_edgeql_functions_inline_nested_global_10(self):
# Use non-computed global nested in non-inlined function
# - non-inlined > inlined > non-inlined
await self.con.execute('''
create global a -> int64;
create function inner1(x: int64) -> optional int64 {
using (global a + x);
};
create function inner2(x: int64) -> optional int64 {
set is_inlined := true;
using (inner1(x));
};
create function foo(x: int64) -> optional int64 {
using (inner2(x));
};
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[],
sort=True,
)

await self.con.execute('''
set global a := 1;
''')
await self.assert_query_result(
'select foo(<int64>{})',
[],
)
await self.assert_query_result(
'select foo(1)',
[2],
)
await self.assert_query_result(
'select foo({1, 2, 3})',
[2, 3, 4],
sort=True,
)

async def test_edgeql_functions_inline_modifying_cardinality_01(self):
await self.con.execute('''
create function foo(x: int64) -> int64 {
Expand Down

0 comments on commit f68a2e9

Please sign in to comment.