-
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: wrap arguments when new built-in function #3520
Conversation
expression/builtin.go
Outdated
return baseBuiltinFunc{ | ||
args: args, | ||
argValues: make([]types.Datum, len(args)), | ||
ctx: ctx, | ||
deterministic: true, | ||
tp: tp, | ||
} | ||
tp: retType}, errors.Trace(err) |
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.
At this line, err must be nil
PTAL @hanfei1991 |
LGTM |
PTAL @shenli |
expression/builtin.go
Outdated
// thus the `tp` attribute is needed for `baseBuiltinFunc` | ||
// to obtain the FieldType of a ScalarFunction. | ||
func newBaseBuiltinFuncWithTp(args []Expression, tp *types.FieldType, ctx context.Context) baseBuiltinFunc { | ||
// newBaseBuiltinFuncWithTp create a built-in function signature with specified types of arguments and return type. |
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/create/creates
s/return type/ returns a type
expression/builtin.go
Outdated
// Every built-in function needs determined argTps and retType when we create it. | ||
func newBaseBuiltinFuncWithTp(args []Expression, retType *types.FieldType, ctx context.Context, argTps ...argTp) (bf baseBuiltinFunc, err error) { | ||
if len(args) != len(argTps) { | ||
panic("unexpected length of args and argTps") |
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.
Why use panic
?
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.
len(args) must equals to len(argTps),
and argTps
is set by the coder,
so here I use panic to give warning.
args[i], err = WrapWithCastAsTime(args[i], types.NewFieldType(mysql.TypeDatetime), ctx) | ||
case tpDuration: | ||
args[i], err = WrapWithCastAsDuration(args[i], ctx) | ||
} |
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.
The err
value will be covered.
expression/expression.go
Outdated
// but what we actually get is store as float64 in Datum. | ||
// So if we wrap `CastDecimalAsInt` upon the result, we'll get <nil> when call `arg.EvalDecimal()`. | ||
// This will be fixed after all built-in functions be rewrite correctlly. | ||
// return val.GetMysqlDecimal(), false, nil |
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.
Complete this comment.
case *builtinValuesSig: | ||
switch sf.FuncName.L { | ||
case ast.Cast: | ||
newFunc, _ := buildCastFunction(sf.GetArgs()[0], sf.GetType(), sf.GetCtx()) |
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.
Why omit this error?
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 a Clone
func of ScalarFunction,
the error will never happen.
PTAL @zimulala |
@@ -171,7 +171,7 @@ func (s *testEvaluatorSuite) TestConcat(c *C) { | |||
} | |||
fc := funcs[fcName].(*concatFunctionClass) | |||
for _, t := range typeCases { | |||
retType := fc.inferType(t.args) | |||
retType, _ := fc.inferType(t.args) |
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.
Check this result?
@@ -249,7 +253,7 @@ func (b *castAsStringFunctionClass) getFunction(args []Expression, ctx context.C | |||
sig = &builtinCastStringAsStringSig{bf} | |||
} | |||
} | |||
return sig, errors.Trace(b.verifyArgs(args)) | |||
return sig.setSelf(sig), errors.Trace(b.verifyArgs(args)) |
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.
Why do we need to set self-value?
If we really need to do that,line 237 also need to be set.
LGTM |
Types of every arguments and return type should be determined
when building built-in function signatures,
before this commit, we wrap cast in
runtime
which is not efficient,this commit wrap cast for args when getFunction.
PTAL @hanfei1991 @shenli