Skip to content

Commit

Permalink
expression: support JSON for builtin function COALESCE (#9087) (#9198)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason committed Jan 28, 2019
1 parent fda9e84 commit 721193c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,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)
}

return sig, nil
Expand Down Expand Up @@ -340,6 +343,28 @@ func (b *builtinCoalesceDurationSig) evalDuration(row chunk.Row) (res types.Dura
return res, isNull, errors.Trace(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 @@ -2790,6 +2790,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

0 comments on commit 721193c

Please sign in to comment.