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

plan: fix predicate push down for UnionScan #7695

Merged
merged 4 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,16 @@ create table t(a bigint primary key);
explain select * from t where a = 1 and a = 2;
id count task operator info
TableDual_5 0.00 root rows:0
drop table if exists ta, tb;
create table ta (a varchar(20));
create table tb (a varchar(20));
begin;
insert tb values ('1');
explain select * from ta where a = 1;
id count task operator info
Projection_5 8000.00 root test.ta.a
└─Selection_6 8000.00 root eq(cast(test.ta.a), 1)
└─UnionScan_7 10000.00 root eq(cast(test.ta.a), 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not Eval it in getSnapshotRow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's out of the scope of UnionScan's responsibility

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why not removing all the filters inside the UnionScan?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be refactored later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter a = 1, which is Selection_6, should be added as a parent of TableReader_9.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better, we can do it in the next PR.

└─TableReader_9 10000.00 root data:TableScan_8
└─TableScan_8 10000.00 cop table:ta, range:[-inf,+inf], keep order:false, stats:pseudo
rollback;
8 changes: 8 additions & 0 deletions cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ explain select * from t where b = 1 and b = 2;
drop table if exists t;
create table t(a bigint primary key);
explain select * from t where a = 1 and a = 2;

drop table if exists ta, tb;
create table ta (a varchar(20));
create table tb (a varchar(20));
begin;
insert tb values ('1');
explain select * from ta where a = 1;
rollback;
13 changes: 13 additions & 0 deletions executor/union_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,16 @@ func (s *testSuite) TestDirtyTransaction(c *C) {
tk.MustQuery("select * from t use index(idx) where c > 1 and d = 4").Check(testkit.Rows("1 2 3 4"))
tk.MustExec("commit")
}

func (s *testSuite) TestUnionScanWithCastCondition(c *C) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add another explain test.

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table ta (a varchar(20))")
tk.MustExec("insert ta values ('1'), ('2')")
tk.MustExec("create table tb (a varchar(20))")
tk.MustExec("begin")
tk.MustQuery("select * from ta where a = 1").Check(testkit.Rows("1"))
tk.MustExec("insert tb values ('0')")
tk.MustQuery("select * from ta where a = 1").Check(testkit.Rows("1"))
tk.MustExec("rollback")
}
5 changes: 3 additions & 2 deletions plan/rule_predicate_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression)

// PredicatePushDown implements LogicalPlan PredicatePushDown interface.
func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) {
p.children[0].PredicatePushDown(predicates)
retainedPredicates, _ := p.children[0].PredicatePushDown(predicates)
p.conditions = make([]expression.Expression, 0, len(predicates))
for _, cond := range predicates {
p.conditions = append(p.conditions, cond)
}
return nil, p
// The conditions in UnionScan is only used for added rows, so parent Selection should not be removed.
return retainedPredicates, p
}

// PredicatePushDown implements LogicalPlan PredicatePushDown interface.
Expand Down