Skip to content

Commit

Permalink
executor: fix 'index out of range' issue in index lookup join (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta authored and lzmhhh123 committed Oct 29, 2021
1 parent fc92ef9 commit 6a4eb7e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions executor/index_lookup_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,11 @@ func (iw *innerWorker) constructLookupContent(task *lookUpJoinTask) ([]*indexJoi
// Store the encoded lookup key in chunk, so we can use it to lookup the matched inners directly.
task.encodedLookUpKeys[chkIdx].AppendBytes(0, keyBuf)
if iw.hasPrefixCol {
for i := range iw.outerCtx.keyCols {
for i, outerOffset := range iw.outerCtx.keyCols {
// If it's a prefix column. Try to fix it.
if iw.colLens[iw.keyCols[i]] != types.UnspecifiedLength {
ranger.CutDatumByPrefixLen(&dLookUpKey[i], iw.colLens[iw.keyCols[i]], iw.rowTypes[iw.keyCols[i]])
joinKeyColPrefixLen := iw.colLens[outerOffset]
if joinKeyColPrefixLen != types.UnspecifiedLength {
ranger.CutDatumByPrefixLen(&dLookUpKey[i], joinKeyColPrefixLen, iw.rowTypes[iw.keyCols[i]])
}
}
// dLookUpKey is sorted and deduplicated at sortAndDedupLookUpContents.
Expand Down
33 changes: 33 additions & 0 deletions executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,36 @@ func (s *testSuite5) TestIssue23656(c *C) {
"1 clever jang 1 clever jang",
"2 blissful aryabhata 2 blissful aryabhata"))
}

func (s *testSuite5) TestIssue23722(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b char(10), c blob, primary key (c(5)) clustered);")
tk.MustExec("insert into t values (20301,'Charlie',x'7a');")
tk.MustQuery("select * from t;").Check(testkit.Rows("20301 Charlie z"))
tk.MustQuery("select * from t where c in (select c from t where t.c >= 'a');").Check(testkit.Rows("20301 Charlie z"))

// Test lookup content exceeds primary key prefix.
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b char(10), c varchar(255), primary key (c(5)) clustered);")
tk.MustExec("insert into t values (20301,'Charlie','aaaaaaa');")
tk.MustQuery("select * from t;").Check(testkit.Rows("20301 Charlie aaaaaaa"))
tk.MustQuery("select * from t where c in (select c from t where t.c >= 'a');").Check(testkit.Rows("20301 Charlie aaaaaaa"))

// Test the original case.
tk.MustExec("drop table if exists t;")
tk.MustExec(`CREATE TABLE t (
col_15 decimal(49,3),
col_16 smallint(5),
col_17 char(118) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT 'tLOOjbIXuuLKPFjkLo',
col_18 set('Alice','Bob','Charlie','David') NOT NULL,
col_19 tinyblob,
PRIMARY KEY (col_19(5),col_16) /*T![clustered_index] NONCLUSTERED */,
UNIQUE KEY idx_10 (col_19(5),col_16)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`)
tk.MustExec("INSERT INTO `t` VALUES (38799.400,20301,'KETeFZhkoxnwMAhA','Charlie',x'7a7968584570705a647179714e56');")
tk.MustQuery("select t.* from t where col_19 in " +
"( select col_19 from t where t.col_18 <> 'David' and t.col_19 >= 'jDzNn' ) " +
"order by col_15 , col_16 , col_17 , col_18 , col_19;").Check(testkit.Rows("38799.400 20301 KETeFZhkoxnwMAhA Charlie zyhXEppZdqyqNV"))
}

0 comments on commit 6a4eb7e

Please sign in to comment.