Skip to content

Conversation

@qianheng-aws
Copy link
Collaborator

@qianheng-aws qianheng-aws commented Oct 24, 2025

Description

Support push down sort after limit if there is only limit pushed, since PPL or other database all don't promise sequence for only limit operator. Thus it should be acceptable to transform limit + sort to sort + limit.

However, we should avoid pushing down sort, if there is existing sort before limit. In such case, users intense to retrieve the Top-K values on the first sort fields from our index. The order will be overridden by the second sort if we keep pushing down it. So this PR prevents this case by detecting whether top-k is pushed down already.

Since #4501 has always introduced a limit before sort for join or subsearch, which will block sort push down. This PR will also enhance these scenarios, especially for left join -- both sides will have limit and sort pushed down then. See CalciteExplainIT::testExplainScalarCorrelatedSubqueryInSelect

Related Issues

Resolves #4570

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@qianheng-aws qianheng-aws added the enhancement New feature or request label Oct 24, 2025
@LantaoJin LantaoJin changed the title Support push down sort after limit Support push down sort after system limit Oct 24, 2025
Signed-off-by: Heng Qian <[email protected]>
# Conflicts:
#	opensearch/src/main/java/org/opensearch/sql/opensearch/planner/physical/OpenSearchIndexScanRule.java
#	opensearch/src/main/java/org/opensearch/sql/opensearch/planner/physical/OpenSearchSortIndexScanRule.java
#	opensearch/src/main/java/org/opensearch/sql/opensearch/storage/scan/PushDownContext.java
}

@Test
public void testHeadThenSort() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] Better to check NoPushDownIT case. We may need different branch for these two test cases

Signed-off-by: Heng Qian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] system limit from plugins.ppl.join.subsearch_maxout prevents sort push down for sort merge join

2 participants