Skip to content

Commit

Permalink
planner: fix incorrect duration between compare (#22830) (#23233)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored May 11, 2021
1 parent 7059d9a commit db79876
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
18 changes: 11 additions & 7 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,18 @@ func ResolveType4Between(args [3]Expression) types.EvalType {

hasTemporal := false
if cmpTp == types.ETString {
for _, arg := range args {
if types.IsTypeTemporal(arg.GetType().Tp) {
hasTemporal = true
break
if args[0].GetType().Tp == mysql.TypeDuration {
cmpTp = types.ETDuration
} else {
for _, arg := range args {
if types.IsTypeTemporal(arg.GetType().Tp) {
hasTemporal = true
break
}
}
if hasTemporal {
cmpTp = types.ETDatetime
}
}
if hasTemporal {
cmpTp = types.ETDatetime
}
}

Expand Down
5 changes: 5 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,11 @@ func (er *expressionRewriter) wrapExpWithCast() (expr, lexp, rexp expression.Exp
}
return expression.WrapWithCastAsString(ctx, e)
}
case types.ETDuration:
expr = expression.WrapWithCastAsTime(er.sctx, expr, types.NewFieldType(mysql.TypeDuration))
lexp = expression.WrapWithCastAsTime(er.sctx, lexp, types.NewFieldType(mysql.TypeDuration))
rexp = expression.WrapWithCastAsTime(er.sctx, rexp, types.NewFieldType(mysql.TypeDuration))
return
case types.ETDatetime:
expr = expression.WrapWithCastAsTime(er.sctx, expr, types.NewFieldType(mysql.TypeDatetime))
lexp = expression.WrapWithCastAsTime(er.sctx, lexp, types.NewFieldType(mysql.TypeDatetime))
Expand Down
18 changes: 18 additions & 0 deletions planner/core/expression_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,21 @@ func (s *testExpressionRewriterSuite) TestCompareMultiFieldsInSubquery(c *C) {
tk.MustQuery("SELECT * FROM t3 WHERE (SELECT c1, c2 FROM t3 LIMIT 1) != ALL(SELECT c1, c2 FROM t4);").Check(testkit.Rows())

}

func (s *testExpressionRewriterSuite) TestIssue22818(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()

tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a time);")
tk.MustExec("insert into t values(\"23:22:22\");")
tk.MustQuery("select * from t where a between \"23:22:22\" and \"23:22:22\"").Check(
testkit.Rows("23:22:22"))
}

0 comments on commit db79876

Please sign in to comment.