Skip to content

Commit

Permalink
fix issue:pingcap#11102
Browse files Browse the repository at this point in the history
Unexcepted result in `SELECT ... CASE WHEN ... ELSE NULL...`
  • Loading branch information
wangxiaoyong committed Jul 5, 2019
1 parent 21d2590 commit 831f7d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ import (
// specialFoldHandler stores functions for special UDF to constant fold
var specialFoldHandler = map[string]func(*ScalarFunction) (Expression, bool){}

// specialNullRejectCheck stores function names for special UDF to skip constant fold.
var specialNullRejectCheck = map[string]struct{}{}

func init() {
specialFoldHandler = map[string]func(*ScalarFunction) (Expression, bool){
ast.If: ifFoldHandler,
ast.Ifnull: ifNullFoldHandler,
}

specialNullRejectCheck = map[string]struct{}{
ast.NullEQ: struct{}{},
ast.Case: struct{}{},
}
}

// FoldConstant does constant folding optimization on an expression excluding deferred ones.
Expand Down Expand Up @@ -104,7 +112,8 @@ func foldConstant(expr Expression) (Expression, bool) {
isDeferredConst = isDeferredConst || isDeferred
}
if !allConstArg {
if !hasNullArg || !sc.InNullRejectCheck || x.FuncName.L == ast.NullEQ {
_, ok := specialNullRejectCheck[x.FuncName.L]
if !hasNullArg || !sc.InNullRejectCheck || ok {
return expr, isDeferredConst
}
constArgs := make([]Expression, len(args))
Expand Down
15 changes: 15 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2722,3 +2722,18 @@ func (s *testSessionSuite) TestLoadClientInteractive(c *C) {
tk.Se.GetSessionVars().ClientCapability = tk.Se.GetSessionVars().ClientCapability | mysql.ClientInteractive
tk.MustQuery("select @@wait_timeout").Check(testkit.Rows("28800"))
}

func (s *testSessionSuite) TestFuncCaseWithLeftJoin(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

tk.MustExec("create table kankan1(id int, name text)")
tk.MustExec("insert into kankan1 values(1, 'a')")
tk.MustExec("insert into kankan1 values(2, 'a')")

tk.MustExec("create table kankan2(id int, h1 text)")
tk.MustExec("insert into kankan2 values(2, 'z')")

tk.MustQuery( "select * from (select t1.id, t2.h1, case when t1.name='b' then 'case2' when t1.name='a' then " +
"'case1' else null end as flag from kankan1 t1 left join kankan2 t2 on t1.id = t2.id) t3 where t3.flag='case1' " +
"order by t3.id").Check(testkit.Rows("1 <nil> case1", "2 z case1"))
}

0 comments on commit 831f7d3

Please sign in to comment.