Skip to content

Commit

Permalink
planner: fix error caused by different length between inner and outer…
Browse files Browse the repository at this point in the history
… OrderByItems (#8273)
  • Loading branch information
mtunique authored and zz-jason committed Nov 13, 2018
1 parent a28a3a7 commit 19c01f4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,17 @@ Projection_4 2666.67 root test.t.a
└─Selection_6 2666.67 cop gt(test.t.a, 0)
└─TableScan_5 3333.33 cop table:t, range:(0,+inf], keep order:false, stats:pseudo
drop table if exists t;
create table t(a int, b int, c int);
explain select * from (select * from t order by (select 2)) t order by a, b;
id count task operator info
Sort_12 10000.00 root t.a:asc, t.b:asc
└─TableReader_18 10000.00 root data:TableScan_17
└─TableScan_17 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
explain select * from (select * from t order by c) t order by a, b;
id count task operator info
Sort_6 10000.00 root t.a:asc, t.b:asc
└─Sort_9 10000.00 root test.t.c:asc
└─TableReader_12 10000.00 root data:TableScan_11
└─TableScan_11 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
drop table if exists t;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
4 changes: 4 additions & 0 deletions cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ explain select * from t where _tidb_rowid > 0;
explain select a, _tidb_rowid from t where a > 0;
explain select * from t where _tidb_rowid > 0 and a > 0;
drop table if exists t;
create table t(a int, b int, c int);
explain select * from (select * from t order by (select 2)) t order by a, b;
explain select * from (select * from t order by c) t order by a, b;
drop table if exists t;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
3 changes: 3 additions & 0 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ func (lt *LogicalTopN) getPhysLimits() []PhysicalPlan {

// Check if this prop's columns can match by items totally.
func matchItems(p *property.PhysicalProperty, items []*ByItems) bool {
if len(items) < len(p.Cols) {
return false
}
for i, col := range p.Cols {
sortItem := items[i]
if sortItem.Desc != p.Desc || !sortItem.Expr.Equal(nil, col) {
Expand Down
9 changes: 9 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ func (s *testPlanSuite) TestDAGPlanBuilderSimpleCase(c *C) {
sql: "select * from (select * from t use index() order by b) t left join t t1 on t.a=t1.a limit 10",
best: "IndexJoin{TableReader(Table(t)->TopN([test.t.b],0,10))->TopN([test.t.b],0,10)->TableReader(Table(t))}(test.t.a,t1.a)->Limit",
},
// Test embedded ORDER BY which imposes on different number of columns than outer query.
{
sql: "select * from ((SELECT 1 a,3 b) UNION (SELECT 2,1) ORDER BY (SELECT 2)) t order by a,b",
best: "UnionAll{Dual->Projection->Dual->Projection}->HashAgg->Sort",
},
{
sql: "select * from ((SELECT 1 a,6 b) UNION (SELECT 2,5) UNION (SELECT 2, 4) ORDER BY 1) t order by 1, 2",
best: "UnionAll{Dual->Projection->Dual->Projection->Dual->Projection}->HashAgg->Sort->Sort",
},
}
for i, tt := range tests {
comment := Commentf("case:%v sql:%s", i, tt.sql)
Expand Down

0 comments on commit 19c01f4

Please sign in to comment.