Skip to content

Commit

Permalink
add unit test and explain test
Browse files Browse the repository at this point in the history
  • Loading branch information
eurekaka committed Sep 9, 2018
1 parent fbef1a3 commit 0304e81
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ 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
explain select null or a > 1 from t;
id count task operator info
Projection_3 10000.00 root or(NULL, gt(test.t.a, 1))
└─TableReader_5 10000.00 root data:TableScan_4
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
drop table if exists ta, tb;
create table ta (a varchar(20));
create table tb (a varchar(20));
Expand Down
1 change: 1 addition & 0 deletions cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ 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;
explain select null or a > 1 from t;

drop table if exists ta, tb;
create table ta (a varchar(20));
Expand Down
62 changes: 54 additions & 8 deletions plan/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,8 @@ func mockContext() sessionctx.Context {
func (s *testPlanSuite) TestPredicatePushDown(c *C) {
defer testleak.AfterTest(c)()
tests := []struct {
sql string
first string
best string
sql string
best string
}{
{
sql: "select count(*) from t a, t b where a.a = b.a",
Expand Down Expand Up @@ -567,11 +566,6 @@ func (s *testPlanSuite) TestOuterWherePredicatePushDown(c *C) {
},
}
for _, ca := range tests {
comment := Commentf("for %s", ca.sql)
stmt, err := s.ParseOneStmt(ca.sql, "", "")
c.Assert(err, IsNil, comment)
p, err := BuildLogicalPlan(s.ctx, stmt, s.is)
c.Assert(err, IsNil, comment)
p, err = logicalOptimize(flagPredicatePushDown|flagDecorrelate|flagPrunColumns, p.(LogicalPlan))
c.Assert(err, IsNil, comment)
proj, ok := p.(*LogicalProjection)
Expand All @@ -593,6 +587,58 @@ func (s *testPlanSuite) TestOuterWherePredicatePushDown(c *C) {
}
}

func (s *testPlanSuite) TestSimplifyOuterJoin(c *C) {
defer testleak.AfterTest(c)()
tests := []struct {
sql string
best string
joinType string
}{
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b where t1.c > 1 or t2.c > 1;",
best: "Join{DataScan(t1)->DataScan(t2)}(t1.b,t2.b)->Sel([or(gt(t1.c, 1), gt(t2.c, 1))])->Projection",
joinType: "left outer join",
},
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b where t1.c > 1 and t2.c > 1;",
best: "Join{DataScan(t1)->DataScan(t2)}(t1.b,t2.b)->Projection",
joinType: "inner join",
},
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b where not (t1.c > 1 or t2.c > 1);",
best: "Join{DataScan(t1)->DataScan(t2)}(t1.b,t2.b)->Projection",
joinType: "inner join",
},
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b where not (t1.c > 1 and t2.c > 1);",
best: "Join{DataScan(t1)->DataScan(t2)}(t1.b,t2.b)->Sel([not(and(le(t1.c, 1), le(t2.c, 1)))])->Projection",
joinType: "left outer join",
},
{
sql: "select * from t t1 left join t t2 on t1.b > 1 where t1.c = t2.c;",
best: "Join{DataScan(t1)->DataScan(t2)}(t1.c,t2.c)->Projection",
joinType: "inner join",
},
}
for _, ca := range tests {
comment := Commentf("for %s", ca.sql)
stmt, err := s.ParseOneStmt(ca.sql, "", "")
c.Assert(err, IsNil, comment)
p, err := BuildLogicalPlan(s.ctx, stmt, s.is)
c.Assert(err, IsNil, comment)
p, err = logicalOptimize(flagPredicatePushDown|flagPrunColumns, p.(LogicalPlan))
c.Assert(err, IsNil, comment)
c.Assert(ToString(p), Equals, ca.best, comment)
join, ok := p.(LogicalPlan).Children()[0].(*LogicalJoin)
if !ok {
join, ok = p.(LogicalPlan).Children()[0].Children()[0].(*LogicalJoin)
c.Assert(ok, IsTrue, comment)
}
joinType := fmt.Sprintf("%s", join.JoinType.String())
c.Assert(joinType, Equals, ca.joinType, comment)
}
}

func newPartitionInfoSchema(definitions []model.PartitionDefinition) infoschema.InfoSchema {
tableInfo := *MockTable()
cols := make([]*model.ColumnInfo, 0, len(tableInfo.Columns))
Expand Down

0 comments on commit 0304e81

Please sign in to comment.