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: handle duration type infer in least and greatest #22271

Merged
merged 46 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
dbde297
fix type infer for tidb's builin comparision
iosmanthus Nov 19, 2020
bc96775
recover tests
iosmanthus Nov 19, 2020
6e3f42b
fix least/greatest time sig
iosmanthus Nov 19, 2020
81d2695
fix least/greatest tests
iosmanthus Nov 23, 2020
27260de
fix between's unit tests
iosmanthus Nov 23, 2020
3bd76e6
fix offset issue of result type infer
iosmanthus Nov 24, 2020
b75c72c
fix between type infer
iosmanthus Nov 25, 2020
105ae59
fix go linter warnings
iosmanthus Nov 25, 2020
eada947
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Nov 25, 2020
a557aa0
fix IsTypeInteger redeclared
iosmanthus Nov 25, 2020
df08d5c
fix make check
iosmanthus Nov 25, 2020
c9df2d1
add unit tests for #issues/19534
iosmanthus Nov 25, 2020
fc83f3e
forbid json comparison in least/greatest and emit warnings
iosmanthus Nov 25, 2020
866a757
fix make check
iosmanthus Nov 25, 2020
54ebcb6
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Nov 30, 2020
b862617
Merge branch 'master' into fix-least-and-greatest
iosmanthus Dec 2, 2020
c11f925
Merge branch 'master' into fix-least-and-greatest
zhouqiang-cl Dec 4, 2020
d3e984b
vectorize the scalar version of 'least/greatest time' function
iosmanthus Dec 10, 2020
2c8a389
Merge branch 'fix-least-and-greatest' of github.com:iosmanthus/tidb i…
iosmanthus Dec 10, 2020
8387a31
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Dec 10, 2020
7f9532e
go fmt
iosmanthus Dec 10, 2020
61f4d68
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Dec 10, 2020
bc2f43f
address @XuHuaiyu's comments
iosmanthus Dec 17, 2020
ce9e269
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Dec 17, 2020
245a92c
gofmt
iosmanthus Dec 17, 2020
b265b8f
remove extra condition in unsupportedJSONComparison
iosmanthus Dec 21, 2020
4dbdfe3
use IsStringLike() instead of ResultMergeType
iosmanthus Dec 21, 2020
5ae26ae
Merge branch 'master' into fix-least-and-greatest
XuHuaiyu Dec 21, 2020
bb3c249
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Dec 21, 2020
5a58c5c
fix nil pointer access in getBaseCmpType
iosmanthus Dec 21, 2020
0432977
Merge branch 'fix-least-and-greatest' of github.com:iosmanthus/tidb i…
iosmanthus Dec 21, 2020
8a755f4
Merge branch 'master' into fix-least-and-greatest
ti-srebot Dec 22, 2020
406c3c9
catch unhandled type case in least/greatest
iosmanthus Jan 7, 2021
080a39f
catch unhandled type case in greatest
iosmanthus Jan 7, 2021
734ed1f
add duration test cases for least/greatest
iosmanthus Jan 7, 2021
d3b0651
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Jan 7, 2021
45f2413
Merge branch 'master' into fix-least-and-greatest
iosmanthus Jan 7, 2021
3b1adbc
Merge branch 'master' of github.com:pingcap/tidb into fix-least-and-g…
iosmanthus Jan 13, 2021
54337c9
add some comments to resolveType4Extremum
iosmanthus Jan 13, 2021
7f5ac3f
Merge branch 'fix-least-and-greatest' of github.com:iosmanthus/tidb i…
iosmanthus Jan 13, 2021
956622c
Merge branch 'master' into fix-least-and-greatest
iosmanthus Jan 13, 2021
ca72705
merge upstream/master
iosmanthus Jan 13, 2021
541a4c2
Merge branch 'master' into fix-least-and-greatest
iosmanthus Jan 13, 2021
a97570e
address @wsh's comments
iosmanthus Jan 15, 2021
aa575c7
Merge branch 'fix-least-and-greatest' of github.com:iosmanthus/tidb i…
iosmanthus Jan 15, 2021
b7681ca
Merge branch 'master' into fix-least-and-greatest
ti-srebot Jan 18, 2021
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
21 changes: 14 additions & 7 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.IsTemporal(item.Tp) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add some comments about why do we need to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.

if temporalItem == nil || temporalItem.Tp == mysql.TypeDatetime {
wshwsh12 marked this conversation as resolved.
Show resolved Hide resolved
temporalItem = item
}
}
}

if !types.IsTemporalWithDate(aggType.Tp) && temporalItem != nil {
if !types.IsTemporal(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 All @@ -469,7 +474,7 @@ func (c *greatestFunctionClass) getFunction(ctx sessionctx.Context, args []Expre
case types.ETDecimal:
sig = &builtinGreatestDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_GreatestDecimal)
case types.ETString:
case types.ETString, types.ETDuration:
wshwsh12 marked this conversation as resolved.
Show resolved Hide resolved
sig = &builtinGreatestStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_GreatestString)
case types.ETDatetime, types.ETTimestamp:
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 All @@ -684,10 +691,10 @@ func (c *leastFunctionClass) getFunction(ctx sessionctx.Context, args []Expressi
case types.ETDecimal:
sig = &builtinLeastDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_LeastDecimal)
case types.ETString:
case types.ETString, types.ETDuration:
wshwsh12 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -310,6 +310,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
6 changes: 6 additions & 0 deletions types/etc.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func IsTemporalWithDate(tp byte) bool {
return IsTypeTime(tp)
}

// IsTemporal returns a boolean indicating
// if the tp is time type.
func IsTemporal(tp byte) bool {
wshwsh12 marked this conversation as resolved.
Show resolved Hide resolved
return tp == mysql.TypeDatetime || tp == mysql.TypeDate || tp == mysql.TypeTimestamp || tp == mysql.TypeDuration || tp == mysql.TypeNewDate
}

// IsBinaryStr returns a boolean indicating
// whether the field type is a binary string type.
func IsBinaryStr(ft *FieldType) bool {
Expand Down