-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Changes from 9 commits
7b00dbb
f933936
9516ac2
c975d44
2f67f9d
0aaf491
760d28a
c7300dc
0f16a6d
33565a0
ee0bc17
ad3c534
14b3adf
f79b669
563c36a
1842158
97e81bd
f9ed083
265ae1e
aec52d1
6057126
f1cb400
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -27,6 +28,7 @@ func init() { | |
specialFoldHandler = map[string]func(*ScalarFunction) (Expression, bool){ | ||
ast.If: ifFoldHandler, | ||
ast.Ifnull: ifNullFoldHandler, | ||
ast.Case: caseWhenHandler, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
isDeferredConst := false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean the |
||
return retProcess(args[l-1], expr.GetType(), isDeferredConst) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not need an individual function |
||
} else if l%2 == 1 { | ||
foldedArg, isDeferred := foldConstant(args[l-1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/ foldedArg/ expr.GetArgs[l-1] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ isFirstCondition/ hasNonConstCondition ?
There was a problem hiding this comment.
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