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

*: replace compareDatum by compare and ignore warnings is collate is empty #30105

Merged
merged 8 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
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 @@ -6832,6 +6832,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 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