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 2 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 time
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
// See http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce
type builtinCoalesceJsonSig struct {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
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
17 changes: 17 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3857,3 +3857,20 @@ func (s *testIntegrationSuite) TestCastAsTime(c *C) {
err = tk.ExecToErr(`select cast(col5 as time(31)) from t where col1 is null;`)
c.Assert(err.Error(), Equals, "[expression:1426]Too big precision 31 specified for column 'CAST'. Maximum is 6.")
}

func (s *testIntegrationSuite) TestCoalesce(c *C) {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
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>`,
))
}