-
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
plan:fix case-when and coalesce type inferer #2918
Conversation
plan/typeinferer.go
Outdated
return &currType | ||
} | ||
|
||
func aggClassType(args []ast.ExprNode, flag *uint) types.TypeClass { |
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.
Rename to 'aggTypeClass' it better.
plan/typeinferer.go
Outdated
@@ -544,6 +557,20 @@ func (v *typeInferrer) convertValueToColumnTypeIfNeeded(x *ast.PatternInExpr) { | |||
} | |||
|
|||
func aggArgsType(args []ast.ExprNode) *types.FieldType { |
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.
Rename to aggFieldType
is better.
plan/typeinferer.go
Outdated
if unsigned { | ||
ft.Flag |= mysql.UnsignedFlag | ||
*flag |= mysql.UnsignedFlag | ||
} |
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.
} else {
*flag &= ^mysql.UnsignedFlag
}
plan/typeinferer.go
Outdated
@@ -411,7 +411,22 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) { | |||
chs = v.defaultCharset | |||
tp.Flen = 40 | |||
case ast.Coalesce: | |||
var gotBinString bool |
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.
This can be computed and returned in aggClassType
.
plan/typeinferer.go
Outdated
currType.Charset = t.Charset | ||
currType.Collate = t.Collate | ||
var gotBinString = false | ||
exprs := make([]ast.ExprNode, len(x.WhenClauses)) |
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.
Need a larger capacity slice for else.
plan/typeinferer.go
Outdated
for i, w := range x.WhenClauses { | ||
exprs[i] = w.Result | ||
ft := w.Result.GetType() | ||
if ft.ToClass() == types.ClassString && mysql.HasBinaryFlag(ft.Flag) { |
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.
We can do it in aggClassType
PTAL @coocood |
plan/typeinferer.go
Outdated
for _, arg := range args { | ||
argFieldType := arg.GetType() | ||
if argFieldType.Tp == mysql.TypeNull { | ||
continue | ||
} | ||
if argFieldType.ToClass() == types.ClassString && mysql.HasBinaryFlag(argFieldType.Flag) { |
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.
argTypeClass := argFieldType.ToClass()
Then it can be used multiple times.
plan/typeinferer.go
Outdated
ft.Charset, ft.Collate = types.DefaultCharsetForType(ft.Tp) | ||
return ft | ||
if tpClass == types.ClassString && !gotBinString { | ||
*flag &= ^uint(mysql.BinaryFlag) |
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.
This is repeated work.
Better define a function that turn flag on and off like
func setTypeFlag(*flag uint, flagItem uint, on bool)
LGTM |
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.
LGTM
Conflicts: plan/typeinferer_test.go �:q!
Conflicts: plan/typeinferer_test.go �:q!
81a0375
to
0f21c32
Compare
fix issue #2911
refer to MySQL:
https://github.com/mysql/mysql-server/blob/5.7/sql/item_cmpfunc.cc#L4341
https://github.com/mysql/mysql-server/blob/5.7/sql/item_cmpfunc.cc#L4037
PTAL @coocood @shenli @hanfei1991 @tiancaiamao @zimulala