Skip to content

[ESQ|L] Append an implicit limit to subqueries with unbounded sort#141025

Merged
fang-xing-esql merged 6 commits intoelastic:mainfrom
fang-xing-esql:append-implicit-limit-to-unbounded-sort-in-subquery
Jan 28, 2026
Merged

[ESQ|L] Append an implicit limit to subqueries with unbounded sort#141025
fang-xing-esql merged 6 commits intoelastic:mainfrom
fang-xing-esql:append-implicit-limit-to-unbounded-sort-in-subquery

Conversation

@fang-xing-esql
Copy link
Member

The implicit limit appended to each subquery is removed by #139058, however there are a couple of cases where we need to append an implicit limit to a subquery, they are described in the above PR. knn has been supported, and this PR is mainly to support unbounded sort in a subquery by appending an implicit limit, which is more convenient when the view definition does not have an explicit limit after a sort.

@fang-xing-esql fang-xing-esql marked this pull request as ready for review January 22, 2026 01:33
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jan 22, 2026
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

Copy link
Contributor

@craigtaverner craigtaverner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although I can imagine possibly simpler code.

return subquery;
}

private static LogicalPlan appendLimitIfNeededForOrderBy(LogicalPlan subquery, LogicalOptimizerContext context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think that a transformDown would be simpler code than this approach with forEachDownMayReturnEarly, and later planWithLimit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEachDownMayReturnEarly exits early if it finds a limit before finding a sort, I'll add another check, if an unbounded sort is found, it exits early as well.

@fang-xing-esql fang-xing-esql merged commit 097ebea into elastic:main Jan 28, 2026
35 checks passed
@fang-xing-esql
Copy link
Member Author

LGTM, although I can imagine possibly simpler code.

Thanks for the review @craigtaverner ! I modified the appendLimitIfNeededForOrderBy to exit early as soon as a sort is found.

@fang-xing-esql fang-xing-esql deleted the append-implicit-limit-to-unbounded-sort-in-subquery branch January 30, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants