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

planner: remove correlated column sort items (#9435) #9440

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
25 changes: 25 additions & 0 deletions cmd/explaintest/r/topn_push_down.result
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,28 @@ id count task operator info
Projection_7 1.00 root 1
└─Limit_8 1.00 root offset:0, count:1
└─TableDual_11 1.00 root rows:1
drop table if exists t1;
drop table if exists t2;
create table t1(a bigint, b bigint);
create table t2(a bigint, b bigint);
desc select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b limit 1);
id count task operator info
Apply_14 10000.00 root semi join, inner:Limit_17, equal:[eq(test.t1.a, test.t2.a)]
├─TableReader_16 10000.00 root data:TableScan_15
│ └─TableScan_15 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
└─Limit_17 1.00 root offset:0, count:1
└─TableReader_23 1.00 root data:Limit_22
└─Limit_22 1.00 cop offset:0, count:1
└─Selection_21 1.00 cop gt(test.t2.b, test.t1.b)
└─TableScan_20 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo
desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1);
id count task operator info
Apply_16 10000.00 root semi join, inner:Projection_19, equal:[eq(test.t1.a, x.a)]
├─TableReader_18 10000.00 root data:TableScan_17
│ └─TableScan_17 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
└─Projection_19 1.00 root test.t2.a, test.t1.b
└─Limit_20 1.00 root offset:0, count:1
└─TableReader_26 1.00 root data:Limit_25
└─Limit_25 1.00 cop offset:0, count:1
└─Selection_24 1.00 cop gt(test.t2.b, test.t1.b)
└─TableScan_23 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo
9 changes: 9 additions & 0 deletions cmd/explaintest/t/topn_push_down.test
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,13 @@ WHERE
ORDER BY te.expect_time asc
LIMIT 0, 5;

-- test order by constant
desc select 1 as a from dual order by a limit 1;

-- test order by correlated column
drop table if exists t1;
drop table if exists t2;
create table t1(a bigint, b bigint);
create table t2(a bigint, b bigint);
desc select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b limit 1);
desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1);
4 changes: 2 additions & 2 deletions planner/core/rule_topn_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (p *LogicalProjection) pushDownTopN(topN *LogicalTopN) LogicalPlan {

// remove meaningless constant sort items.
for i := len(topN.ByItems) - 1; i >= 0; i-- {
_, isConst := topN.ByItems[i].Expr.(*expression.Constant)
if isConst {
switch topN.ByItems[i].Expr.(type) {
case *expression.Constant, *expression.CorrelatedColumn:
topN.ByItems = append(topN.ByItems[:i], topN.ByItems[i+1:]...)
}
}
Expand Down