Skip to content

Commit

Permalink
planner, expression: fix simplify outer join with cast (#12701)
Browse files Browse the repository at this point in the history
  • Loading branch information
justarandomstring authored and sre-bot committed Oct 15, 2019
1 parent 8a8f41d commit 15984f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func EvaluateExprWithNull(ctx sessionctx.Context, schema *Schema, expr Expressio
for i, arg := range x.GetArgs() {
args[i] = EvaluateExprWithNull(ctx, schema, arg)
}
return NewFunctionInternal(ctx, x.FuncName.L, types.NewFieldType(mysql.TypeTiny), args...)
return NewFunctionInternal(ctx, x.FuncName.L, x.RetType, args...)
case *Column:
if !schema.Contains(x) {
return x
Expand Down
37 changes: 37 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,40 @@ func testDAGPlanBuilderSplitAvg(c *C, root core.PhysicalPlan) {
testDAGPlanBuilderSplitAvg(c, son)
}
}

func (s *testPlanSuite) TestSimplifyOuterJoinWithCast(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
se, err := session.CreateSession4Test(store)
c.Assert(err, IsNil)
_, err = se.Execute(context.Background(), "use test")
c.Assert(err, IsNil)

var input []string
var output []struct {
SQL string
Best string
}
s.testData.GetTestCases(c, &input, &output)
for i, test := range input {
comment := Commentf("case:%v sql:%s", i, test)
stmt, err := s.ParseOneStmt(test, "", "")
c.Assert(err, IsNil, comment)

p, err := planner.Optimize(context.Background(), se, stmt, s.is)
c.Assert(err, IsNil)
s.testData.OnRecord(func() {
output[i].SQL = test
output[i].Best = core.ToString(p)
})
c.Assert(core.ToString(p), Equals, output[i].Best)

warnings := se.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(warnings, HasLen, 0, comment)
}
}
6 changes: 6 additions & 0 deletions planner/core/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,5 +481,11 @@
"cases": [
"select t1.a, (select count(t2.a) from t t2 where t2.g in (select t3.d from t t3 where t3.c = t1.a)) as agg_col from t t1;"
]
},
{
"name": "TestSimplifyOuterJoinWithCast",
"cases": [
"select * from t t1 left join t t2 on t1.a = t2.a where cast(t1.c_str as float) = '2.3'"
]
}
]
9 changes: 9 additions & 0 deletions planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1241,5 +1241,14 @@
"Best": "Apply{IndexReader(Index(t.f)[[NULL,+inf]])->IndexMergeJoin{IndexReader(Index(t.c_d_e)[[NULL,+inf]]->HashAgg)->HashAgg->IndexReader(Index(t.g)[[NULL,+inf]])}(Column#29,Column#23)}->HashAgg"
}
]
},
{
"Name": "TestSimplifyOuterJoinWithCast",
"Cases": [
{
"SQL": "select * from t t1 left join t t2 on t1.a = t2.a where cast(t1.c_str as float) = '2.3'",
"Best": "MergeLeftOuterJoin{TableReader(Table(t))->Sel([eq(cast(Column#6), 2.3)])->TableReader(Table(t))}(Column#1,Column#13)"
}
]
}
]

0 comments on commit 15984f6

Please sign in to comment.