Skip to content

Commit

Permalink
cherry pick pingcap#17855 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: sre-bot <sre-bot@pingcap.com>
  • Loading branch information
Reminiscent authored and sre-bot committed Jun 15, 2020
1 parent 1b12e01 commit efb54b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
51 changes: 51 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6539,3 +6539,54 @@ func (s *testIntegrationSuite) TestIssue17287(c *C) {
tk.MustQuery("execute stmt7 using @val1;").Check(testkit.Rows("1589873945"))
tk.MustQuery("execute stmt7 using @val2;").Check(testkit.Rows("1589873946"))
}
<<<<<<< HEAD
=======

func (s *testIntegrationSuite) TestIssue17727(c *C) {
tk := testkit.NewTestKit(c, s.store)
orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(true)
var err error
tk.Se, err = session.CreateSession4TestWithOpt(s.store, &session.Opt{
PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
})
c.Assert(err, IsNil)

tk.MustExec("use test;")
tk.MustExec("DROP TABLE IF EXISTS t1;")
tk.MustExec("CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment, a timestamp NOT NULL);")
tk.MustExec("INSERT INTO t1 VALUES (null, '2020-05-30 20:30:00');")
tk.MustExec("PREPARE mystmt FROM 'SELECT * FROM t1 WHERE UNIX_TIMESTAMP(a) >= ?';")
tk.MustExec("SET @a=1590868800;")
tk.MustQuery("EXECUTE mystmt USING @a;").Check(testkit.Rows())
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))

tk.MustExec("SET @a=1590868801;")
tk.MustQuery("EXECUTE mystmt USING @a;").Check(testkit.Rows())
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))

tk.MustExec("prepare stmt from 'select unix_timestamp(?)';")
tk.MustExec("set @a = '2020-05-30 20:30:00';")
tk.MustQuery("execute stmt using @a;").Check(testkit.Rows("1590841800"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("0"))

tk.MustExec("set @a = '2020-06-12 13:47:58';")
tk.MustQuery("execute stmt using @a;").Check(testkit.Rows("1591940878"))
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
}

func (s *testIntegrationSerialSuite) TestIssue17891(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int, value set ('a','b','c') charset utf8mb4 collate utf8mb4_bin default 'a,b ');")
tk.MustExec("drop table t")
tk.MustExec("create table test(id int, value set ('a','b','c') charset utf8mb4 collate utf8mb4_general_ci default 'a,B ,C');")
}
>>>>>>> 8a33340... expression: fix the panic when we use `unix_time` expression in `prepare and execute` statement. (#17855)
13 changes: 10 additions & 3 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,9 +1526,16 @@ func (er *expressionRewriter) funcCallToExpression(v *ast.FuncCallExpr) {
var function expression.Expression
er.ctxStackPop(len(v.Args))
if _, ok := expression.DeferredFunctions[v.FnName.L]; er.useCache() && ok {
function, er.err = expression.NewFunctionBase(er.sctx, v.FnName.L, &v.Type, args...)
c := &expression.Constant{Value: types.NewDatum(nil), RetType: function.GetType().Clone(), DeferredExpr: function}
er.ctxStackAppend(c, types.EmptyName)
// When the expression is unix_timestamp and the number of argument is not zero,
// we deal with it as normal expression.
if v.FnName.L == ast.UnixTimestamp && len(v.Args) != 0 {
function, er.err = er.newFunction(v.FnName.L, &v.Type, args...)
er.ctxStackAppend(function, types.EmptyName)
} else {
function, er.err = expression.NewFunctionBase(er.sctx, v.FnName.L, &v.Type, args...)
c := &expression.Constant{Value: types.NewDatum(nil), RetType: function.GetType().Clone(), DeferredExpr: function}
er.ctxStackAppend(c, types.EmptyName)
}
} else {
function, er.err = er.newFunction(v.FnName.L, &v.Type, args...)
er.ctxStackAppend(function, types.EmptyName)
Expand Down

0 comments on commit efb54b0

Please sign in to comment.