Skip to content

Commit

Permalink
expression: fix behavior for CASE WHEN (not_int) (#15309) (#15359)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Mar 14, 2020
1 parent 2ea2b8c commit 9855c8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 11 additions & 8 deletions expression/builtin_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ func (c *caseWhenFunctionClass) getFunction(ctx sessionctx.Context, args []Expre
}
// Set retType to BINARY(0) if all arguments are of type NULL.
if fieldTp.Tp == mysql.TypeNull {
fieldTp.Flen, fieldTp.Decimal = 0, -1
fieldTp.Flen, fieldTp.Decimal = 0, types.UnspecifiedLength
types.SetBinChsClnFlag(fieldTp)
}
argTps := make([]types.EvalType, 0, l)
for i := 0; i < l-1; i += 2 {
if args[i], err = wrapWithIsTrue(ctx, true, args[i]); err != nil {
return nil, err
}
argTps = append(argTps, types.ETInt, tp)
}
if l%2 == 1 {
Expand Down Expand Up @@ -221,7 +224,7 @@ func (b *builtinCaseWhenIntSig) Clone() builtinFunc {
}

// evalInt evals a builtinCaseWhenIntSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenIntSig) evalInt(row chunk.Row) (ret int64, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down Expand Up @@ -257,7 +260,7 @@ func (b *builtinCaseWhenRealSig) Clone() builtinFunc {
}

// evalReal evals a builtinCaseWhenRealSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenRealSig) evalReal(row chunk.Row) (ret float64, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down Expand Up @@ -293,7 +296,7 @@ func (b *builtinCaseWhenDecimalSig) Clone() builtinFunc {
}

// evalDecimal evals a builtinCaseWhenDecimalSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenDecimalSig) evalDecimal(row chunk.Row) (ret *types.MyDecimal, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down Expand Up @@ -329,7 +332,7 @@ func (b *builtinCaseWhenStringSig) Clone() builtinFunc {
}

// evalString evals a builtinCaseWhenStringSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenStringSig) evalString(row chunk.Row) (ret string, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down Expand Up @@ -365,7 +368,7 @@ func (b *builtinCaseWhenTimeSig) Clone() builtinFunc {
}

// evalTime evals a builtinCaseWhenTimeSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenTimeSig) evalTime(row chunk.Row) (ret types.Time, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down Expand Up @@ -401,7 +404,7 @@ func (b *builtinCaseWhenDurationSig) Clone() builtinFunc {
}

// evalDuration evals a builtinCaseWhenDurationSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenDurationSig) evalDuration(row chunk.Row) (ret types.Duration, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down Expand Up @@ -437,7 +440,7 @@ func (b *builtinCaseWhenJSONSig) Clone() builtinFunc {
}

// evalJSON evals a builtinCaseWhenJSONSig.
// See https://dev.mysql.com/doc/refman/5.7/en/case.html
// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case
func (b *builtinCaseWhenJSONSig) evalJSON(row chunk.Row) (ret json.BinaryJSON, isNull bool, err error) {
var condition int64
args, l := b.getArgs(), len(b.getArgs())
Expand Down
2 changes: 2 additions & 0 deletions expression/builtin_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (s *testEvaluatorSuite) TestCaseWhen(c *C) {
{[]interface{}{nil, 1, false, 2, 3}, 3},
{[]interface{}{1, jsonInt.GetMysqlJSON(), nil}, 3},
{[]interface{}{0, jsonInt.GetMysqlJSON(), nil}, nil},
{[]interface{}{0.1, 1, 2}, 1},
{[]interface{}{0.0, 1, 0.1, 2}, 2},
}
fc := funcs[ast.Case]
for _, t := range tbl {
Expand Down

0 comments on commit 9855c8f

Please sign in to comment.