Skip to content

Commit

Permalink
Revert "planner: donot prune all columns for Projection (pingcap#24024)"
Browse files Browse the repository at this point in the history
This reverts commit 4cd7fb0.
  • Loading branch information
guo-shaoge committed Apr 22, 2021
1 parent 6f4d8a3 commit 5229998
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 262 deletions.
18 changes: 9 additions & 9 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,29 @@ test t4 1 expr_idx 1 NULL NULL (`a` + `b` + 1) 2 YES NO
explain format = 'brief' select count(1) from (select count(1) from (select * from t1 where c3 = 100) k) k2;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#5
└─StreamAgg 1.00 root funcs:firstrow(Column#13)->Column#11
└─StreamAgg 1.00 root funcs:firstrow(Column#9)->Column#7
└─TableReader 1.00 root data:StreamAgg
└─StreamAgg 1.00 cop[tikv] funcs:firstrow(1)->Column#13
└─StreamAgg 1.00 cop[tikv] funcs:firstrow(1)->Column#9
└─Selection 10.00 cop[tikv] eq(test.t1.c3, 100)
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
explain format = 'brief' select 1 from (select count(c2), count(c3) from t1) k;
id estRows task access object operator info
Projection 1.00 root 1->Column#6
└─StreamAgg 1.00 root funcs:firstrow(Column#16)->Column#11
└─StreamAgg 1.00 root funcs:firstrow(Column#14)->Column#9
└─TableReader 1.00 root data:StreamAgg
└─StreamAgg 1.00 cop[tikv] funcs:firstrow(1)->Column#16
└─StreamAgg 1.00 cop[tikv] funcs:firstrow(1)->Column#14
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
explain format = 'brief' select count(1) from (select max(c2), count(c3) as m from t1) k;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#6
└─StreamAgg 1.00 root funcs:firstrow(Column#15)->Column#10
└─StreamAgg 1.00 root funcs:firstrow(Column#13)->Column#8
└─TableReader 1.00 root data:StreamAgg
└─StreamAgg 1.00 cop[tikv] funcs:firstrow(1)->Column#15
└─StreamAgg 1.00 cop[tikv] funcs:firstrow(1)->Column#13
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
explain format = 'brief' select count(1) from (select count(c2) from t1 group by c3) k;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#5
└─HashAgg 8000.00 root group by:test.t1.c3, funcs:firstrow(1)->Column#9
└─HashAgg 8000.00 root group by:test.t1.c3, funcs:firstrow(1)->Column#7
└─TableReader 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
set @@session.tidb_opt_insubq_to_join_and_agg=0;
Expand Down Expand Up @@ -498,8 +498,8 @@ PRIMARY KEY (`id`)
explain format = 'brief' SELECT COUNT(1) FROM (SELECT COALESCE(b.region_name, '不详') region_name, SUM(a.registration_num) registration_num FROM (SELECT stat_date, show_date, region_id, 0 registration_num FROM test01 WHERE period = 1 AND stat_date >= 20191202 AND stat_date <= 20191202 UNION ALL SELECT stat_date, show_date, region_id, registration_num registration_num FROM test01 WHERE period = 1 AND stat_date >= 20191202 AND stat_date <= 20191202) a LEFT JOIN test02 b ON a.region_id = b.id WHERE registration_num > 0 AND a.stat_date >= '20191202' AND a.stat_date <= '20191202' GROUP BY a.stat_date , a.show_date , COALESCE(b.region_name, '不详') ) JLS;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#22
└─HashAgg 8000.00 root group by:Column#34, Column#35, Column#36, funcs:firstrow(1)->Column#33
└─Projection 10000.01 root Column#14, Column#15, coalesce(test.test02.region_name, 不详)->Column#36
└─HashAgg 8000.00 root group by:Column#32, Column#33, Column#34, funcs:firstrow(1)->Column#31
└─Projection 10000.01 root Column#14, Column#15, coalesce(test.test02.region_name, 不详)->Column#34
└─HashJoin 10000.01 root left outer join, equal:[eq(Column#16, test.test02.id)]
├─TableReader(Build) 10000.00 root data:TableFullScan
│ └─TableFullScan 10000.00 cop[tikv] table:b keep order:false, stats:pseudo
Expand Down
15 changes: 7 additions & 8 deletions cmd/explaintest/r/explain_join_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ load stats 's/explain_join_stats_lo.json';
explain format = 'brief' select count(*) from e, lo where lo.a=e.a and e.b=22336;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#5
└─Projection 19977.00 root 1->Column#6
└─HashJoin 19977.00 root inner join, equal:[eq(test.lo.a, test.e.a)]
├─TableReader(Build) 250.00 root data:TableFullScan
│ └─TableFullScan 250.00 cop[tikv] table:lo keep order:false
└─IndexLookUp(Probe) 19977.00 root
├─IndexRangeScan(Build) 19977.00 cop[tikv] table:e, index:idx_b(b) range:[22336,22336], keep order:false
└─Selection(Probe) 19977.00 cop[tikv] not(isnull(test.e.a))
└─TableRowIDScan 19977.00 cop[tikv] table:e keep order:false
└─HashJoin 19977.00 root inner join, equal:[eq(test.lo.a, test.e.a)]
├─TableReader(Build) 250.00 root data:TableFullScan
│ └─TableFullScan 250.00 cop[tikv] table:lo keep order:false
└─IndexLookUp(Probe) 19977.00 root
├─IndexRangeScan(Build) 19977.00 cop[tikv] table:e, index:idx_b(b) range:[22336,22336], keep order:false
└─Selection(Probe) 19977.00 cop[tikv] not(isnull(test.e.a))
└─TableRowIDScan 19977.00 cop[tikv] table:e keep order:false
explain format = 'brief' select /*+ TIDB_INLJ(e) */ count(*) from e, lo where lo.a=e.a and e.b=22336;
id estRows task access object operator info
StreamAgg 1.00 root funcs:count(1)->Column#5
Expand Down
25 changes: 0 additions & 25 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3284,31 +3284,6 @@ func (s *testIntegrationSerialSuite) TestLimitIndexLookUpKeepOrder(c *C) {
}
}

// Apply operator may got panic because empty Projection is eliminated.
func (s *testIntegrationSerialSuite) TestIssue23887(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int, b int);")
tk.MustExec("insert into t values(1, 2), (3, 4);")
var input []string
var output []struct {
SQL string
Plan []string
Res []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain format = 'brief' " + tt).Rows())
output[i].Res = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Sort().Rows())
})
tk.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...))
tk.MustQuery(tt).Sort().Check(testkit.Rows(output[i].Res...))
}
}

func (s *testIntegrationSuite) TestDecorrelateInnerJoinInSubquery(c *C) {
tk := testkit.NewTestKit(c, s.store)

Expand Down
12 changes: 0 additions & 12 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ func (p *LogicalProjection) PruneColumns(parentUsedCols []*expression.Column) er
p.Exprs = append(p.Exprs[:i], p.Exprs[i+1:]...)
}
}
// Here we add a constant 1 to avoid Projection operator is eliminated. (#23887)
if len(p.Exprs) == 0 {
constOne := expression.NewOne()
p.schema.Append(&expression.Column{
UniqueID: p.ctx.GetSessionVars().AllocPlanColumnID(),
RetType: constOne.GetType(),
})
p.Exprs = append(p.Exprs, &expression.Constant{
Value: constOne.Value,
RetType: constOne.GetType(),
})
}
selfUsedCols := make([]*expression.Column, 0, len(p.Exprs))
selfUsedCols = expression.ExtractColumnsFromExpressions(selfUsedCols, p.Exprs, nil)
return child.PruneColumns(selfUsedCols)
Expand Down
3 changes: 3 additions & 0 deletions planner/core/rule_eliminate_projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func canProjectionBeEliminatedStrict(p *PhysicalProjection) bool {
if p.CalculateNoDelay {
return false
}
if p.Schema().Len() == 0 {
return true
}
child := p.Children()[0]
if p.Schema().Len() != child.Schema().Len() {
return false
Expand Down
6 changes: 0 additions & 6 deletions planner/core/testdata/integration_serial_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,5 @@
"desc format = 'brief' select * from t where a = 1 and b > 2 and b < 10 and d = 10 order by b,c limit 10",
"desc format = 'brief' select * from t where a = 1 and b > 2 and b < 10 and d = 10 order by b desc, c desc limit 10"
]
},
{
"name": "TestIssue23887",
"cases": [
"select (2) in (select b from t) from (select t.a < (select t.a from t t1 limit 1) from t) t"
]
}
]
Loading

0 comments on commit 5229998

Please sign in to comment.