Skip to content

Commit

Permalink
plan: prune columns for LogicalTableDual (#7725)
Browse files Browse the repository at this point in the history
  • Loading branch information
eurekaka authored Sep 17, 2018
1 parent d47e303 commit 44e0578
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cmd/explaintest/r/explain_easy_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ TableReader_7 0.50 root data:Selection_6
└─TableScan_5 1.00 cop table:t1, range:[1,1], keep order:false
explain select c1 from t1 where c1 in (select c2 from t2);
id count task operator info
Projection_10 0.00 root test.t1.c1
└─TableDual_11 0.00 root rows:0
TableDual_11 0.00 root rows:0
explain select * from information_schema.columns;
id count task operator info
MemTableScan_4 10000.00 root
Expand Down
3 changes: 1 addition & 2 deletions plan/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ func (s *testAnalyzeSuite) TestTableDual(c *C) {
c.Assert(h.Update(dom.InfoSchema()), IsNil)

testKit.MustQuery(`explain select * from t where 1 = 0`).Check(testkit.Rows(
`Projection_5 0.00 root test.t.a`,
`└─TableDual_6 0.00 root rows:0`,
`TableDual_6 0.00 root rows:0`,
))

testKit.MustQuery(`explain select * from t where 1 = 1 limit 0`).Check(testkit.Rows(
Expand Down
4 changes: 2 additions & 2 deletions plan/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ func (s *testPlanSuite) TestRefine(c *C) {
},
{
sql: `select a from t where c = 1.9 and d > 3`,
best: "Dual->Projection",
best: "Dual",
},
{
sql: `select a from t where c < 1.1`,
Expand All @@ -1144,7 +1144,7 @@ func (s *testPlanSuite) TestRefine(c *C) {
},
{
sql: `select a from t where c = 123456789098765432101234`,
best: "Dual->Projection",
best: "Dual",
},
{
sql: `select a from t where c = 'hanfei'`,
Expand Down
15 changes: 15 additions & 0 deletions plan/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ func (ds *DataSource) PruneColumns(parentUsedCols []*expression.Column) {
}
}

// PruneColumns implements LogicalPlan interface.
func (p *LogicalTableDual) PruneColumns(parentUsedCols []*expression.Column) {
used := getUsedList(parentUsedCols, p.schema)
for i := len(used) - 1; i >= 0; i-- {
if !used[i] {
p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...)
}
}
for k, cols := range p.schema.TblID2Handle {
if p.schema.ColumnIndex(cols[0]) == -1 {
delete(p.schema.TblID2Handle, k)
}
}
}

func (p *LogicalJoin) extractUsedCols(parentUsedCols []*expression.Column) (leftCols []*expression.Column, rightCols []*expression.Column) {
for _, eqCond := range p.EqualConditions {
parentUsedCols = append(parentUsedCols, expression.ExtractColumns(eqCond)...)
Expand Down

0 comments on commit 44e0578

Please sign in to comment.