Skip to content

Commit

Permalink
expression: handle duration type infer in least and greatest (pingcap…
Browse files Browse the repository at this point in the history
…#22271)

Signed-off-by: iosmanthus <myosmanthustree@gmail.com>
  • Loading branch information
iosmanthus authored and qw4990 committed Jan 27, 2021
1 parent 3f33bdb commit 4f84fae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 12 additions & 5 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,15 @@ func resolveType4Extremum(args []Expression) types.EvalType {
if aggType.EvalType().IsStringKind() {
for i := range args {
item := args[i].GetType()
if types.IsTemporalWithDate(item.Tp) {
temporalItem = item
// Find the temporal value in the arguments but prefer DateTime value.
if types.IsTypeTemporal(item.Tp) {
if temporalItem == nil || item.Tp == mysql.TypeDatetime {
temporalItem = item
}
}
}

if !types.IsTemporalWithDate(aggType.Tp) && temporalItem != nil {
if !types.IsTypeTemporal(aggType.Tp) && temporalItem != nil {
aggType.Tp = temporalItem.Tp
}
// TODO: String charset, collation checking are needed.
Expand Down Expand Up @@ -444,6 +447,8 @@ func (c *greatestFunctionClass) getFunction(ctx sessionctx.Context, args []Expre
if tp == types.ETDatetime || tp == types.ETTimestamp {
cmpAsDatetime = true
tp = types.ETString
} else if tp == types.ETDuration {
tp = types.ETString
} else if tp == types.ETJson {
unsupportedJSONComparison(ctx, args)
tp = types.ETString
Expand Down Expand Up @@ -656,9 +661,11 @@ func (c *leastFunctionClass) getFunction(ctx sessionctx.Context, args []Expressi
}
tp := resolveType4Extremum(args)
cmpAsDatetime := false
if tp == types.ETDatetime {
if tp == types.ETDatetime || tp == types.ETTimestamp {
cmpAsDatetime = true
tp = types.ETString
} else if tp == types.ETDuration {
tp = types.ETString
} else if tp == types.ETJson {
unsupportedJSONComparison(ctx, args)
tp = types.ETString
Expand Down Expand Up @@ -687,7 +694,7 @@ func (c *leastFunctionClass) getFunction(ctx sessionctx.Context, args []Expressi
case types.ETString:
sig = &builtinLeastStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_LeastString)
case types.ETDatetime:
case types.ETDatetime, types.ETTimestamp:
sig = &builtinLeastTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_LeastTime)
}
Expand Down
4 changes: 4 additions & 0 deletions expression/builtin_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ func (s *testEvaluatorSuite) TestGreatestLeastFunc(c *C) {
[]interface{}{duration, "123"},
"12:59:59", "123", false, false,
},
{
[]interface{}{duration, duration},
"12:59:59", "12:59:59", false, false,
},
{
[]interface{}{"123", nil, "123"},
nil, nil, true, false,
Expand Down

0 comments on commit 4f84fae

Please sign in to comment.