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

planner: fix the coercibility of the cast function (#21705) #21714

Merged
merged 4 commits into from Jan 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion expression/collation.go
Expand Up @@ -91,7 +91,7 @@ const (
CoercibilityExplicit Coercibility = 0
// CoercibilityNone is derived from the concatenation of two strings with different collations.
CoercibilityNone Coercibility = 1
// CoercibilityImplicit is derived from a column or a stored routine parameter or local variable.
// CoercibilityImplicit is derived from a column or a stored routine parameter or local variable or cast() function.
CoercibilityImplicit Coercibility = 2
// CoercibilitySysconst is derived from a “system constant” (the string returned by functions such as USER() or VERSION()).
CoercibilitySysconst Coercibility = 3
Expand Down
8 changes: 8 additions & 0 deletions expression/integration_test.go
Expand Up @@ -7458,6 +7458,14 @@ func (s *testIntegrationSerialSuite) TestCollationIndexJoin(c *C) {
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 Optimizer Hint /*+ INL_MERGE_JOIN(t2) */ is inapplicable"))
}

func (s *testIntegrationSuite2) TestCastCoer(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustQuery("select coercibility(binary('a'))").Check(testkit.Rows("2"))
tk.MustQuery("select coercibility(cast('a' as char(10)))").Check(testkit.Rows("2"))
tk.MustQuery("select coercibility(convert('abc', char(10)));").Check(testkit.Rows("2"))
}

func (s *testIntegrationSuite) TestDatetimeUserVariable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @p = now()")
Expand Down
4 changes: 4 additions & 0 deletions planner/core/expression_rewriter.go
Expand Up @@ -1040,6 +1040,10 @@ func (er *expressionRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok
return retNode, false
}

if v.Tp.EvalType() == types.ETString {
arg.SetCoercibility(expression.CoercibilityImplicit)
}

er.ctxStack[len(er.ctxStack)-1] = expression.BuildCastFunction(er.sctx, arg, v.Tp)
er.ctxNameStk[len(er.ctxNameStk)-1] = types.EmptyName
case *ast.PatternLikeExpr:
Expand Down