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

expression, session: handle CASE WHEN specially when folding constant during outerJoin Simplification #11105

Merged
merged 22 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
48 changes: 48 additions & 0 deletions expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package expression

import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
Expand All @@ -27,6 +28,7 @@ func init() {
specialFoldHandler = map[string]func(*ScalarFunction) (Expression, bool){
ast.If: ifFoldHandler,
ast.Ifnull: ifNullFoldHandler,
ast.Case: caseWhenHandler,
}
}

Expand Down Expand Up @@ -78,6 +80,52 @@ func ifNullFoldHandler(expr *ScalarFunction) (Expression, bool) {
return expr, isDeferredConst
}

func caseWhenHandler(expr *ScalarFunction) (Expression, bool) {
args, l := expr.GetArgs(), len(expr.GetArgs())
isFirstCondition := true
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ isFirstCondition/ hasNonConstCondition ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've modified as your suggestion. PTAL @XuHuaiyu

isDeferredConst := false
Copy link
Contributor

Choose a reason for hiding this comment

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

isFirstCondition, isDeferredConst := true, false

for i := 0; i < l-1; i += 2 {
foldedArg, isDeferred := foldConstant(args[i])
expr.GetArgs()[i] = foldedArg
isDeferredConst = isDeferredConst || isDeferred
if _, isConst := foldedArg.(*Constant); isConst {
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ isConst/ isConst && isFirstCondition ?

condition, isNull, err := args[i].EvalInt(expr.GetCtx(), chunk.Row{})
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ condition/ val

if err != nil {
return expr, false
}
if isFirstCondition && condition != 0 && !isNull {
return retProcess(args[i+1], expr.GetType(), isDeferredConst)
}
} else {
isFirstCondition = false
}
foldedArg1, isDeferred1 := foldConstant(args[i+1])
Copy link
Contributor

Choose a reason for hiding this comment

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

expr.GetArgs()[i+1], isDeferred := xxx

expr.GetArgs()[i+1] = foldedArg1
isDeferredConst = isDeferredConst || isDeferred1
}

if l%2 == 1 && isFirstCondition {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean the else expr? Please add some comments.

return retProcess(args[l-1], expr.GetType(), isDeferredConst)
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not need an individual function retProcess since it's a small function.

} else if l%2 == 1 {
foldedArg, isDeferred := foldConstant(args[l-1])
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ foldedArg/ expr.GetArgs[l-1]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

‘isDeferred‘ is a new variable here。if s/ foldedArg/ expr.GetArgs[l-1],I need to declare this variable.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can define isDeferred in line 84.

expr.GetArgs()[l-1] = foldedArg
isDeferredConst = isDeferredConst || isDeferred
}

return expr, isDeferredConst
}

func retProcess(expr Expression, retType *types.FieldType, isDeferredConst bool) (Expression, bool) {
foldedExpr, isDeferred := foldConstant(expr)
isDeferredConst = isDeferredConst || isDeferred
if fc, isConst := foldedExpr.(*Constant); isConst {
XuHuaiyu marked this conversation as resolved.
Show resolved Hide resolved
fc.RetType = retType
return fc, isDeferredConst
}

return foldedExpr, isDeferredConst
}

func foldConstant(expr Expression) (Expression, bool) {
switch x := expr.(type) {
case *ScalarFunction:
Expand Down
13 changes: 13 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2722,3 +2722,16 @@ 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) {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
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 t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1' order by t1.id").Check(testkit.Rows("1", "2"))
}