Skip to content

Commit

Permalink
expression: fix incorrect collation when cast non-string type arg to …
Browse files Browse the repository at this point in the history
…string type (#19186) (#22599)
  • Loading branch information
ti-srebot authored Jan 28, 2021
1 parent f74dd5d commit 33fdb15
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
3 changes: 1 addition & 2 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
Expand Down Expand Up @@ -1900,7 +1899,7 @@ func WrapWithCastAsString(ctx sessionctx.Context, expr Expression) Expression {
argLen = -1
}
tp := types.NewFieldType(mysql.TypeVarString)
tp.Charset, tp.Collate = charset.GetDefaultCharsetAndCollate()
tp.Charset, tp.Collate = expr.CharsetAndCollation(ctx)
tp.Flen, tp.Decimal = argLen, types.UnspecifiedLength
return BuildCastFunction(ctx, expr, tp)
}
Expand Down
8 changes: 7 additions & 1 deletion expression/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,18 @@ func deriveCoercibilityForScarlarFunc(sf *ScalarFunction) Coercibility {
if sf.RetType.EvalType() != types.ETString {
return CoercibilityNumeric
}
coer := CoercibilityCoercible
coer := CoercibilityIgnorable
for _, arg := range sf.GetArgs() {
if arg.Coercibility() < coer {
coer = arg.Coercibility()
}
}

// it is weird if a ScalarFunction is CoercibilityNumeric but return string type
if coer == CoercibilityNumeric {
return CoercibilityCoercible
}

return coer
}

Expand Down
7 changes: 7 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7674,7 +7674,14 @@ func (s *testIntegrationSerialSuite) TestIssue19116(c *C) {
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci;")
tk.MustQuery("select collation(concat(1 collate `binary`));").Check(testkit.Rows("binary"))
tk.MustQuery("select coercibility(concat(1 collate `binary`));").Check(testkit.Rows("0"))
tk.MustQuery("select collation(concat(NULL,NULL));").Check(testkit.Rows("binary"))
tk.MustQuery("select coercibility(concat(NULL,NULL));").Check(testkit.Rows("6"))
tk.MustQuery("select collation(concat(1,1));").Check(testkit.Rows("utf8mb4_general_ci"))
tk.MustQuery("select coercibility(concat(1,1));").Check(testkit.Rows("4"))
tk.MustQuery("select collation(1);").Check(testkit.Rows("binary"))
tk.MustQuery("select coercibility(1);").Check(testkit.Rows("5"))
tk.MustQuery("select coercibility(1=1);").Check(testkit.Rows("5"))
}
1 change: 1 addition & 0 deletions expression/simple_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func (sr *simpleRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok boo
arg.GetType().Collate = v.Collate
}
sr.stack[len(sr.stack)-1].SetCoercibility(CoercibilityExplicit)
sr.stack[len(sr.stack)-1].SetCharsetAndCollation(arg.GetType().Charset, arg.GetType().Collate)
default:
sr.err = errors.Errorf("UnknownType: %T", v)
return retNode, false
Expand Down
1 change: 1 addition & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ func (er *expressionRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok
arg.GetType().Collate = v.Collate
}
er.ctxStack[len(er.ctxStack)-1].SetCoercibility(expression.CoercibilityExplicit)
er.ctxStack[len(er.ctxStack)-1].SetCharsetAndCollation(arg.GetType().Charset, arg.GetType().Collate)
default:
er.err = errors.Errorf("UnknownType: %T", v)
return retNode, false
Expand Down

0 comments on commit 33fdb15

Please sign in to comment.