Skip to content

Commit

Permalink
*: replace compareDatum by compare and ignore warnings is collate is …
Browse files Browse the repository at this point in the history
…empty (#30105)
  • Loading branch information
wjhuang2016 committed Nov 29, 2021
1 parent 8550dbb commit eb7672f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
)

// NewOne stands for a number 1.
Expand Down Expand Up @@ -345,7 +346,7 @@ func (c *Constant) Equal(ctx sessionctx.Context, b Expression) bool {
if err1 != nil || err2 != nil {
return false
}
con, err := c.Value.CompareDatum(ctx.GetSessionVars().StmtCtx, &y.Value)
con, err := c.Value.Compare(ctx.GetSessionVars().StmtCtx, &y.Value, collate.GetBinaryCollator())
if err != nil || con != 0 {
return false
}
Expand Down
4 changes: 3 additions & 1 deletion expression/constant_propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/disjointset"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -59,7 +60,8 @@ func (s *basePropConstSolver) tryToUpdateEQList(col *Column, con *Constant) (boo
id := s.getColID(col)
oldCon := s.eqList[id]
if oldCon != nil {
return false, !oldCon.Equal(s.ctx, con)
res, err := oldCon.Value.Compare(s.ctx.GetSessionVars().StmtCtx, &con.Value, collate.GetCollator(col.GetType().Collate))
return false, res != 0 || err != nil
}
s.eqList[id] = con
return true, false
Expand Down
1 change: 1 addition & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6835,6 +6835,7 @@ func (s *testIntegrationSerialSuite) TestCollateConstantPropagation(c *C) {
tk.MustExec("insert into t2 values ('A');")
tk.MustExec("set names utf8 collate utf8_general_ci;")
tk.MustQuery("select * from t1, t2 where t1.a=t2.a and t1.a= 'a';").Check(testkit.Rows("a A"))
tk.MustQuery("select * from t1 where a='a' and a = 'A'").Check(testkit.Rows("a"))
}

func (s *testIntegrationSuite2) TestIssue17791(c *C) {
Expand Down
8 changes: 5 additions & 3 deletions expression/partition_pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/disjointset"
)

Expand Down Expand Up @@ -60,8 +61,8 @@ func (p *hashPartitionPruner) reduceColumnEQ() bool {
father := p.unionSet.FindRoot(i)
if p.constantMap[i] != nil {
if p.constantMap[father] != nil {
// May has conflict here.
if !p.constantMap[father].Equal(p.ctx, p.constantMap[i]) {
// May has conflict here. We can choose collation from lhs or rhs, they should be equal. Exception is that `NULL` values.
if eq, err := p.constantMap[father].Value.Compare(p.ctx.GetSessionVars().StmtCtx, &p.constantMap[i].Value, collate.GetCollator(p.constantMap[i].GetType().Collate)); eq != 0 || err != nil {
return true
}
} else {
Expand Down Expand Up @@ -95,7 +96,8 @@ func (p *hashPartitionPruner) reduceConstantEQ() bool {
if col != nil {
id := p.getColID(col)
if p.constantMap[id] != nil {
if p.constantMap[id].Equal(p.ctx, cond) {
// We can choose collation from lhs or rhs, they should be equal. Exception is that `NULL` values.
if eq, err := p.constantMap[id].Value.Compare(p.ctx.GetSessionVars().StmtCtx, &cond.Value, collate.GetCollator(cond.GetType().Collate)); eq == 0 && err == nil {
continue
}
return true
Expand Down
12 changes: 7 additions & 5 deletions util/collate/collate.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ func GetCollator(collate string) Collator {
if atomic.LoadInt32(&newCollationEnabled) == 1 {
ctor, ok := newCollatorMap[collate]
if !ok {
logutil.BgLogger().Warn(
"Unable to get collator by name, use binCollator instead.",
zap.String("name", collate),
zap.Stack("stack"))
return newCollatorMap["utf8mb4_bin"]
if collate != "" {
logutil.BgLogger().Warn(
"Unable to get collator by name, use binCollator instead.",
zap.String("name", collate),
zap.Stack("stack"))
}
return newCollatorMap[charset.CollationUTF8MB4]
}
return ctor
}
Expand Down

0 comments on commit eb7672f

Please sign in to comment.