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 invalid convert type in between...and... (#19820) #21503

Merged
merged 5 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func temporalWithDateAsNumEvalType(argTp *types.FieldType) (argEvalType types.Ev
return
}

// GetCmpTp4MinMax gets compare type for GREATEST and LEAST and BETWEEN (mainly for datetime).
// GetCmpTp4MinMax gets compare type for GREATEST and LEAST and BETWEEN
func GetCmpTp4MinMax(args []Expression) (argTp types.EvalType) {
datetimeFound, isAllStr := false, true
cmpEvalType, isStr, isTemporalWithDate := temporalWithDateAsNumEvalType(args[0].GetType())
Expand Down
12 changes: 12 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5206,6 +5206,18 @@ func (s *testIntegrationSuite) TestIssue18850(c *C) {
tk.MustQuery("select /*+ HASH_JOIN(t, t1) */ * from t join t1 on t.b = t1.b1;").Check(testkit.Rows("1 A 1 A"))
}

func (s *testIntegrationSuite) TestIssue11177(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("SELECT 'lvuleck' BETWEEN '2008-09-16 22:23:50' AND 0;").Check(testkit.Rows("0"))
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1265 Data Truncated", "Warning 1265 Data Truncated"))
tk.MustQuery("SELECT 'aa' BETWEEN 'bb' AND 0;").Check(testkit.Rows("1"))
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1265 Data Truncated", "Warning 1265 Data Truncated"))
tk.MustQuery("select 1 between 0 and b'110';").Check(testkit.Rows("1"))
tk.MustQuery("show warnings;").Check(testkit.Rows())
tk.MustQuery("select 'b' between 'a' and b'110';").Check(testkit.Rows("0"))
tk.MustQuery("show warnings;").Check(testkit.Rows())
}

func (s *testIntegrationSuite) TestIssue19804(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
Expand Down
42 changes: 35 additions & 7 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,20 +1304,48 @@ func (er *expressionRewriter) rowToScalarFunc(v *ast.RowExpr) {
er.ctxStack = append(er.ctxStack, function)
}

func (er *expressionRewriter) wrapExpWithCast() (expr, lexp, rexp expression.Expression) {
stkLen := len(er.ctxStack)
expr, lexp, rexp = er.ctxStack[stkLen-3], er.ctxStack[stkLen-2], er.ctxStack[stkLen-1]
var castFunc func(sessionctx.Context, expression.Expression) expression.Expression
switch expression.GetCmpTp4MinMax([]expression.Expression{expr, lexp, rexp}) {
case types.ETInt:
castFunc = expression.WrapWithCastAsInt
case types.ETReal:
castFunc = expression.WrapWithCastAsReal
case types.ETDecimal:
castFunc = expression.WrapWithCastAsDecimal
case types.ETString:
castFunc = func(ctx sessionctx.Context, e expression.Expression) expression.Expression {
// string kind expression do not need cast
if e.GetType().EvalType().IsStringKind() {
return e
}
return expression.WrapWithCastAsString(ctx, e)
}
case types.ETDatetime:
expr = expression.WrapWithCastAsTime(er.sctx, expr, types.NewFieldType(mysql.TypeDatetime))
lexp = expression.WrapWithCastAsTime(er.sctx, lexp, types.NewFieldType(mysql.TypeDatetime))
rexp = expression.WrapWithCastAsTime(er.sctx, rexp, types.NewFieldType(mysql.TypeDatetime))
return
default:
return
}

expr = castFunc(er.sctx, expr)
lexp = castFunc(er.sctx, lexp)
rexp = castFunc(er.sctx, rexp)
return
}

func (er *expressionRewriter) betweenToExpression(v *ast.BetweenExpr) {
stkLen := len(er.ctxStack)
er.err = expression.CheckArgsNotMultiColumnRow(er.ctxStack[stkLen-3:]...)
if er.err != nil {
return
}

expr, lexp, rexp := er.ctxStack[stkLen-3], er.ctxStack[stkLen-2], er.ctxStack[stkLen-1]

if expression.GetCmpTp4MinMax([]expression.Expression{expr, lexp, rexp}) == types.ETDatetime {
expr = expression.WrapWithCastAsTime(er.sctx, expr, types.NewFieldType(mysql.TypeDatetime))
lexp = expression.WrapWithCastAsTime(er.sctx, lexp, types.NewFieldType(mysql.TypeDatetime))
rexp = expression.WrapWithCastAsTime(er.sctx, rexp, types.NewFieldType(mysql.TypeDatetime))
}
expr, lexp, rexp := er.wrapExpWithCast()

var op string
var l, r expression.Expression
Expand Down