Skip to content

Commit ecfe90e

Browse files
authored
executor: fix index out of range panic of cte when max_chunk_size is samll (#48839) (#49004)
close #48808
1 parent 64bd0d5 commit ecfe90e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

executor/cte.go

+10
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,16 @@ func (p *cteProducer) computeChunkHash(chk *chunk.Chunk) (sel []int, err error)
544544
hashBitMap[val] = true
545545
}
546546
} else {
547+
// Length of p.sel is init as MaxChunkSize, but the row num of chunk may still exceeds MaxChunkSize.
548+
// So needs to handle here to make sure len(p.sel) == chk.NumRows().
549+
if len(p.sel) < numRows {
550+
tmpSel := make([]int, numRows-len(p.sel))
551+
for i := 0; i < len(tmpSel); i++ {
552+
tmpSel[i] = i + len(p.sel)
553+
}
554+
p.sel = append(p.sel, tmpSel...)
555+
}
556+
547557
// All rows is selected, sel will be [0....numRows).
548558
// e.sel is setup when building executor.
549559
sel = p.sel

executor/cte_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,19 @@ func TestCTEShareCorColumn(t *testing.T) {
505505
tk.MustExec("insert into t1 values(1), (2);")
506506
tk.MustQuery("SELECT * FROM t1 dt WHERE EXISTS( WITH RECURSIVE qn AS (SELECT a AS b UNION ALL SELECT b+1 FROM qn WHERE b=0 or b = 1) SELECT * FROM qn dtqn1 where exists (select /*+ NO_DECORRELATE() */ b from qn where dtqn1.b+1));").Check(testkit.Rows("1", "2"))
507507
}
508+
509+
func TestCTESmallChunkSize(t *testing.T) {
510+
store := testkit.CreateMockStore(t)
511+
tk := testkit.NewTestKit(t, store)
512+
tk.MustExec("use test;")
513+
tk.MustExec("drop table if exists t1")
514+
tk.MustExec("create table t1(c1 int);")
515+
insertStr := "insert into t1 values (0)"
516+
for i := 1; i < 300; i++ {
517+
insertStr += fmt.Sprintf(", (%d)", i)
518+
}
519+
tk.MustExec(insertStr)
520+
tk.MustExec("set @@tidb_max_chunk_size = 32;")
521+
tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 c1 from cte1 limit 1 offset 100) select * from cte1;").Check(testkit.Rows("100"))
522+
tk.MustExec("set @@tidb_max_chunk_size = default;")
523+
}

0 commit comments

Comments
 (0)