Skip to content

Commit

Permalink
Revert "planner: donot prune all columns for Projection (#24024) (#24093
Browse files Browse the repository at this point in the history
)" (#24202)
  • Loading branch information
guo-shaoge committed Apr 22, 2021
1 parent 343d56f commit 1145e34
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 261 deletions.
18 changes: 9 additions & 9 deletions cmd/explaintest/r/explain_easy.result
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
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
8 changes: 4 additions & 4 deletions executor/executor_test.go
Expand Up @@ -6898,12 +6898,12 @@ func (s *testSuiteP2) TestApplyCache(c *C) {
tk.MustExec("insert into t values (1),(1),(1),(1),(1),(1),(1),(1),(1);")
tk.MustExec("analyze table t;")
result := tk.MustQuery("explain analyze SELECT count(1) FROM (SELECT (SELECT min(a) FROM t as t2 WHERE t2.a > t1.a) AS a from t as t1) t;")
c.Assert(result.Rows()[2][0], Equals, " └─Apply_41")
c.Assert(result.Rows()[1][0], Equals, "└─Apply_39")
var (
ind int
flag bool
)
value := (result.Rows()[2][5]).(string)
value := (result.Rows()[1][5]).(string)
for ind = 0; ind < len(value)-5; ind++ {
if value[ind:ind+5] == "cache" {
flag = true
Expand All @@ -6918,9 +6918,9 @@ func (s *testSuiteP2) TestApplyCache(c *C) {
tk.MustExec("insert into t values (1),(2),(3),(4),(5),(6),(7),(8),(9);")
tk.MustExec("analyze table t;")
result = tk.MustQuery("explain analyze SELECT count(1) FROM (SELECT (SELECT min(a) FROM t as t2 WHERE t2.a > t1.a) AS a from t as t1) t;")
c.Assert(result.Rows()[2][0], Equals, " └─Apply_41")
c.Assert(result.Rows()[1][0], Equals, "└─Apply_39")
flag = false
value = (result.Rows()[2][5]).(string)
value = (result.Rows()[1][5]).(string)
for ind = 0; ind < len(value)-5; ind++ {
if value[ind:ind+5] == "cache" {
flag = true
Expand Down
25 changes: 0 additions & 25 deletions planner/core/integration_test.go
Expand Up @@ -3019,31 +3019,6 @@ func (s *testIntegrationSerialSuite) TestMppAggWithJoin(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
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
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
Expand Up @@ -253,11 +253,5 @@
"desc format = 'brief' select * from (select id from t group by id) C join (select sum(value),id from t group by id)B on C.id=B.id",
"desc format = 'brief' select * from (select id from t group by id) C join (select sum(b),id from (select t.id, t1.id as b from t join (select id, count(*) as c from t group by id) t1 on t.id=t1.id)A group by id)B on C.id=b.id"
]
},
{
"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"
]
}
]

0 comments on commit 1145e34

Please sign in to comment.