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: support JSON for builtin function COALESCE #9087

Merged
merged 6 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ func (c *coalesceFunctionClass) getFunction(ctx sessionctx.Context, args []Expre
}
sig = &builtinCoalesceDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CoalesceDuration)
case types.ETJson:
sig = &builtinCoalesceJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CoalesceJson)
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to update getSignatureByPB as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

getSignatureByPB is only used in mocktikv when this function is pushed to the coprocessor. I think we can do it later when we push it?

}

return sig, nil
Expand Down Expand Up @@ -339,6 +342,28 @@ func (b *builtinCoalesceDurationSig) evalDuration(row chunk.Row) (res types.Dura
return res, isNull, err
}

// builtinCoalesceJSONSig is buitin function coalesce signature which return type json.
// See http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce
type builtinCoalesceJSONSig struct {
baseBuiltinFunc
}

func (b *builtinCoalesceJSONSig) Clone() builtinFunc {
newSig := &builtinCoalesceJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

func (b *builtinCoalesceJSONSig) evalJSON(row chunk.Row) (res json.BinaryJSON, isNull bool, err error) {
for _, a := range b.getArgs() {
res, isNull, err = a.EvalJSON(b.ctx, row)
if err != nil || !isNull {
break
}
}
return res, isNull, err
}

// temporalWithDateAsNumEvalType makes DATE, DATETIME, TIMESTAMP pretend to be numbers rather than strings.
func temporalWithDateAsNumEvalType(argTp *types.FieldType) (argEvalType types.EvalType, isStr bool, isTemporalWithDate bool) {
argEvalType = argTp.EvalType()
Expand Down
4 changes: 4 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,10 @@ func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {
// for coalesce
result = tk.MustQuery("select coalesce(NULL), coalesce(NULL, NULL), coalesce(NULL, NULL, NULL);")
result.Check(testkit.Rows("<nil> <nil> <nil>"))
tk.MustQuery(`select coalesce(cast(1 as json), cast(2 as json));`).Check(testkit.Rows(`1`))
tk.MustQuery(`select coalesce(NULL, cast(2 as json));`).Check(testkit.Rows(`2`))
tk.MustQuery(`select coalesce(cast(1 as json), NULL);`).Check(testkit.Rows(`1`))
tk.MustQuery(`select coalesce(NULL, NULL);`).Check(testkit.Rows(`<nil>`))

tk.MustExec("drop table if exists t2")
tk.MustExec("create table t2(a int, b double, c datetime, d time, e char(20), f bit(10))")
Expand Down