Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix wrong plan when apply offset limit in PushDown Project limit #54654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.Lists;
import com.starrocks.sql.optimizer.OptExpression;
import com.starrocks.sql.optimizer.OptimizerContext;
import com.starrocks.sql.optimizer.operator.Operator;
import com.starrocks.sql.optimizer.operator.OperatorType;
import com.starrocks.sql.optimizer.operator.logical.LogicalLimitOperator;
import com.starrocks.sql.optimizer.operator.pattern.Pattern;
Expand Down Expand Up @@ -54,6 +55,10 @@ public boolean check(OptExpression input, OptimizerContext context) {
@Override
public List<OptExpression> transform(OptExpression project, OptimizerContext context) {
LogicalLimitOperator limit = (LogicalLimitOperator) project.getInputs().get(0).getOp();
// clear the project limit when limit has offset
if (project.getOp().hasLimit() && limit.hasOffset()) {
project.getOp().setLimit(Operator.DEFAULT_LIMIT);
}
return Lists.newArrayList(OptExpression.create(limit,
OptExpression.create(project.getOp(), project.getInputs().get(0).getInputs())));
}
Expand Down
16 changes: 16 additions & 0 deletions fe/fe-core/src/test/java/com/starrocks/sql/plan/LimitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,22 @@ public void testOffsetWithSubTopN() throws Exception {
" limit: 400");
}

@Test
public void testPushDownLimitToTopN() throws Exception {
connectContext.getSessionVariable().setOptimizerExecuteTimeout(3000000);
String sql;
String plan;
sql = "select c0 from (select * from ( select v1 c0, v2 c1 from t0 order by c0 asc limit 1000, 600 ) l " +
"union all select 0 as c0, '0' as c1 ) b limit 100;";
plan = getFragmentPlan(sql);
assertContains(plan, " 2:TOP-N\n" +
" | order by: <slot 1> 1: v1 ASC\n" +
" | offset: 0\n" +
" | limit: 1100\n" +
" | \n" +
" 1:OlapScanNode");
}

@Test
public void testUnionLimit() throws Exception {
String queryStr = "select 1 from (select 4, 3 from t0 union all select 2, 3 ) as a limit 3";
Expand Down
Loading