Skip to content

Commit

Permalink
planner: fix the issue that planner may cache invalid plans for joins…
Browse files Browse the repository at this point in the history
… in some cases (#28432) (#28444)
  • Loading branch information
ti-srebot authored Nov 19, 2021
1 parent f0a1084 commit 2d5f913
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
43 changes: 43 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,46 @@ func (s *testPrepareSuite) TestPlanCacheWithDifferentVariableTypes(c *C) {
}
}
}

func (s *testPrepareSuite) TestIssue28087And28162(c *C) {
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()
orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(true)

// issue 28087
tk.MustExec(`use test`)
tk.MustExec(`drop table if exists IDT_26207`)
tk.MustExec(`CREATE TABLE IDT_26207 (col1 bit(1))`)
tk.MustExec(`insert into IDT_26207 values(0x0), (0x1)`)
tk.MustExec(`prepare stmt from 'select t1.col1 from IDT_26207 as t1 left join IDT_26207 as t2 on t1.col1 = t2.col1 where t1.col1 in (?, ?, ?)'`)
tk.MustExec(`set @a=0x01, @b=0x01, @c=0x01`)
tk.MustQuery(`execute stmt using @a,@b,@c`).Check(testkit.Rows("\x01"))
tk.MustExec(`set @a=0x00, @b=0x00, @c=0x01`)
tk.MustQuery(`execute stmt using @a,@b,@c`).Check(testkit.Rows("\x00", "\x01"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))

// issue 28162
tk.MustExec(`drop table if exists IDT_MC21780`)
tk.MustExec(`CREATE TABLE IDT_MC21780 (
COL1 timestamp NULL DEFAULT NULL,
COL2 timestamp NULL DEFAULT NULL,
COL3 timestamp NULL DEFAULT NULL,
KEY U_M_COL (COL1,COL2)
)`)
tk.MustExec(`insert into IDT_MC21780 values("1970-12-18 10:53:28", "1970-12-18 10:53:28", "1970-12-18 10:53:28")`)
tk.MustExec(`prepare stmt from 'select/*+ hash_join(t1) */ * from IDT_MC21780 t1 join IDT_MC21780 t2 on t1.col1 = t2.col1 where t1. col1 < ? and t2. col1 in (?, ?, ?);'`)
tk.MustExec(`set @a="2038-01-19 03:14:07", @b="2038-01-19 03:14:07", @c="2038-01-19 03:14:07", @d="2038-01-19 03:14:07"`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Check(testkit.Rows())
tk.MustExec(`set @a="1976-09-09 20:21:11", @b="2021-07-14 09:28:16", @c="1982-01-09 03:36:39", @d="1970-12-18 10:53:28"`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Check(testkit.Rows("1970-12-18 10:53:28 1970-12-18 10:53:28 1970-12-18 10:53:28 1970-12-18 10:53:28 1970-12-18 10:53:28 1970-12-18 10:53:28"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}
12 changes: 12 additions & 0 deletions expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,18 @@ func (c *Constant) HashCode(sc *stmtctx.StatementContext) []byte {
if len(c.hashcode) > 0 {
return c.hashcode
}

if c.DeferredExpr != nil {
c.hashcode = c.DeferredExpr.HashCode(sc)
return c.hashcode
}

if c.ParamMarker != nil {
c.hashcode = append(c.hashcode, parameterFlag)
c.hashcode = codec.EncodeInt(c.hashcode, int64(c.ParamMarker.order))
return c.hashcode
}

_, err := c.Eval(chunk.Row{})
if err != nil {
terror.Log(err)
Expand Down
1 change: 1 addition & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
constantFlag byte = 0
columnFlag byte = 1
scalarFunctionFlag byte = 3
parameterFlag byte = 4
)

// EvalAstExpr evaluates ast expression directly.
Expand Down

0 comments on commit 2d5f913

Please sign in to comment.