Skip to content

Commit

Permalink
executor: handle collate for min/max(enum/set column) (#31819) (#31858)
Browse files Browse the repository at this point in the history
close #31638
  • Loading branch information
ti-srebot authored Apr 15, 2022
1 parent ae5b88b commit 47867bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -392,7 +393,8 @@ func buildMaxMin(aggFuncDesc *aggregation.AggFuncDesc, ordinal int, isMax bool)
args: aggFuncDesc.Args,
ordinal: ordinal,
},
isMax: isMax,
isMax: isMax,
collator: collate.GetCollator(aggFuncDesc.RetTp.Collate),
}
frac := base.args[0].GetType().Decimal
if frac == -1 {
Expand Down
12 changes: 7 additions & 5 deletions executor/aggfuncs/func_max_min.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/stringutil"
)

Expand Down Expand Up @@ -232,7 +233,8 @@ type partialResult4MaxMinSet struct {

type baseMaxMinAggFunc struct {
baseAggFunc
isMax bool
isMax bool
collator collate.Collator
}

type maxMin4Int struct {
Expand Down Expand Up @@ -1485,7 +1487,7 @@ func (e *maxMin4Enum) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup [
continue
}
en := d.GetMysqlEnum()
if e.isMax && en.Name > p.val.Name || !e.isMax && en.Name < p.val.Name {
if e.isMax && e.collator.Compare(en.Name, p.val.Name) > 0 || !e.isMax && e.collator.Compare(en.Name, p.val.Name) < 0 {
oldMem := len(p.val.Name)
newMem := len(en.Name)
memDelta += int64(newMem - oldMem)
Expand All @@ -1504,7 +1506,7 @@ func (e *maxMin4Enum) MergePartialResult(sctx sessionctx.Context, src, dst Parti
*p2 = *p1
return 0, nil
}
if e.isMax && p1.val.Name > p2.val.Name || !e.isMax && p1.val.Name < p2.val.Name {
if e.isMax && e.collator.Compare(p1.val.Name, p2.val.Name) > 0 || !e.isMax && e.collator.Compare(p1.val.Name, p2.val.Name) < 0 {
p2.val, p2.isNull = p1.val, false
}
return 0, nil
Expand Down Expand Up @@ -1552,7 +1554,7 @@ func (e *maxMin4Set) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []
continue
}
s := d.GetMysqlSet()
if e.isMax && s.Name > p.val.Name || !e.isMax && s.Name < p.val.Name {
if e.isMax && e.collator.Compare(s.Name, p.val.Name) > 0 || !e.isMax && e.collator.Compare(s.Name, p.val.Name) < 0 {
oldMem := len(p.val.Name)
newMem := len(s.Name)
memDelta += int64(newMem - oldMem)
Expand All @@ -1571,7 +1573,7 @@ func (e *maxMin4Set) MergePartialResult(sctx sessionctx.Context, src, dst Partia
*p2 = *p1
return 0, nil
}
if e.isMax && p1.val.Name > p2.val.Name || !e.isMax && p1.val.Name < p2.val.Name {
if e.isMax && e.collator.Compare(p1.val.Name, p2.val.Name) > 0 || !e.isMax && e.collator.Compare(p1.val.Name, p2.val.Name) < 0 {
p2.val, p2.isNull = p1.val, false
}
return 0, nil
Expand Down

0 comments on commit 47867bb

Please sign in to comment.