diff --git a/edb/edgeql/compiler/__init__.py b/edb/edgeql/compiler/__init__.py index a9cad542157..3deed153e48 100644 --- a/edb/edgeql/compiler/__init__.py +++ b/edb/edgeql/compiler/__init__.py @@ -275,6 +275,10 @@ def compile_ast_to_ir( ctx = stmtctx_mod.init_context(schema=schema, options=options) + if isinstance(tree, qlast.Expr) and ctx.implicit_limit: + tree = qlast.SelectQuery(result=tree, implicit=True) + tree.limit = qlast.Constant.integer(ctx.implicit_limit) + if not script_info: script_info = stmtctx_mod.preprocess_script([tree], ctx=ctx) diff --git a/edb/edgeql/compiler/stmt.py b/edb/edgeql/compiler/stmt.py index df222c89f0e..e7377c0ec87 100644 --- a/edb/edgeql/compiler/stmt.py +++ b/edb/edgeql/compiler/stmt.py @@ -127,14 +127,6 @@ def compile_SelectQuery( ) ) - if ( - (ctx.expr_exposed or sctx.stmt is ctx.toplevel_stmt) - and ctx.implicit_limit - and expr.limit is None - and not ctx.inhibit_implicit_limit - ): - expr.limit = qlast.Constant.integer(ctx.implicit_limit) - stmt.result = compile_result_clause( expr.result, view_scls=ctx.view_scls, diff --git a/edb/edgeql/compiler/viewgen.py b/edb/edgeql/compiler/viewgen.py index fcf793c93a9..2a176789e40 100644 --- a/edb/edgeql/compiler/viewgen.py +++ b/edb/edgeql/compiler/viewgen.py @@ -1534,7 +1534,6 @@ def _normalize_view_ptr_expr( if ( (ctx.expr_exposed or ctx.stmt is ctx.toplevel_stmt) - and not qlexpr.limit and ctx.implicit_limit and not base_is_singleton ): @@ -1659,10 +1658,6 @@ def _normalize_view_ptr_expr( if ( (ctx.expr_exposed or ctx.stmt is ctx.toplevel_stmt) and ctx.implicit_limit - and isinstance(qlexpr, ( - qlast.SelectQuery, qlast.DeleteQuery, qlast.ShapeElement - )) - and not qlexpr.limit ): qlexpr = qlast.SelectQuery(result=qlexpr, implicit=True) qlexpr.limit = qlast.Constant.integer(ctx.implicit_limit) diff --git a/tests/test_edgeql_scope.py b/tests/test_edgeql_scope.py index 023218f8b80..913753d0b38 100644 --- a/tests/test_edgeql_scope.py +++ b/tests/test_edgeql_scope.py @@ -4177,3 +4177,120 @@ async def test_edgeql_scope_mat_issue_6060(self): ''', [{"minCost": 1}, {"minCost": 1}, {"minCost": 1}, {"minCost": 2}], ) + + async def test_edgeql_scope_implicit_limit_01(self): + # implicit limit should interact correctly with offset + await self.assert_query_result( + r''' + select Card { name } order by .name offset 3 + ''', + [ + {"name": "Dwarf"}, + {"name": "Giant eagle"}, + {"name": "Giant turtle"}, + {"name": "Golem"}, + ], + implicit_limit=4, + ) + + await self.assert_query_result( + r''' + with W := Card + select W { name } order by .name offset 3; + ''', + [ + {"name": "Dwarf"}, + {"name": "Giant eagle"}, + {"name": "Giant turtle"}, + {"name": "Golem"}, + ], + implicit_limit=4, + ) + + await self.assert_query_result( + r''' + select Card { name } order by .name offset 3 limit 2 + ''', + [ + {"name": "Dwarf"}, + {"name": "Giant eagle"}, + ], + implicit_limit=4, + ) + + await self.assert_query_result( + r''' + select User { deck: {name} order by .name offset 3 } + filter .name = 'Carol'; + ''', + [ + { + "deck": [ + {"name": "Giant eagle"}, + {"name": "Giant turtle"}, + {"name": "Golem"}, + ] + } + ], + implicit_limit=3, + ) + + async def test_edgeql_scope_implicit_limit_02(self): + # explicit limit shouldn't override implicit + await self.assert_query_result( + r''' + select User { deck: {name} order by .name offset 3 limit 100} + filter .name = 'Carol'; + ''', + [ + { + "deck": [ + {"name": "Giant eagle"}, + {"name": "Giant turtle"}, + {"name": "Golem"}, + ] + } + ], + implicit_limit=3, + ) + + await self.assert_query_result( + r''' + select User { cards := ( + select .deck {name} order by .name offset 3 limit 100)} + filter .name = 'Carol'; + ''', + [ + { + "cards": [ + {"name": "Giant eagle"}, + {"name": "Giant turtle"}, + {"name": "Golem"}, + ] + } + ], + implicit_limit=3, + ) + + await self.assert_query_result( + r''' + select Card { name } order by .name offset 3 limit 100 + ''', + [ + {"name": "Dwarf"}, + {"name": "Giant eagle"}, + {"name": "Giant turtle"}, + {"name": "Golem"}, + ], + implicit_limit=4, + ) + + await self.assert_query_result( + r''' + select Card { name } order by .name offset 3 limit 1 + ''', + [ + {"name": "Dwarf"}, + ], + implicit_limit=4, + )