Skip to content

Commit

Permalink
executor: handle collate for min/max(enum/set column) (#31819) (#31859)
Browse files Browse the repository at this point in the history
close #31638
  • Loading branch information
ti-srebot authored Jun 16, 2022
1 parent 2dee0e6 commit f329221
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 @@ -27,6 +27,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 @@ -393,7 +394,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 @@ -22,6 +22,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 @@ -233,7 +234,8 @@ type partialResult4MaxMinSet struct {

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

type maxMin4Int struct {
Expand Down Expand Up @@ -1486,7 +1488,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 @@ -1505,7 +1507,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 @@ -1553,7 +1555,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 @@ -1572,7 +1574,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 f329221

Please sign in to comment.