Skip to content

Commit

Permalink
executor: fix index out of range panic of cte when max_chunk_size is …
Browse files Browse the repository at this point in the history
…samll (#48839) (#49001)

close #48808
  • Loading branch information
ti-chi-bot authored Dec 4, 2023
1 parent 9c074a2 commit 6ed9445
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions executor/cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,16 @@ func (p *cteProducer) computeChunkHash(chk *chunk.Chunk) (sel []int, err error)
hashBitMap[val] = true
}
} else {
// Length of p.sel is init as MaxChunkSize, but the row num of chunk may still exceeds MaxChunkSize.
// So needs to handle here to make sure len(p.sel) == chk.NumRows().
if len(p.sel) < numRows {
tmpSel := make([]int, numRows-len(p.sel))
for i := 0; i < len(tmpSel); i++ {
tmpSel[i] = i + len(p.sel)
}
p.sel = append(p.sel, tmpSel...)
}

// All rows is selected, sel will be [0....numRows).
// e.sel is setup when building executor.
sel = p.sel
Expand Down
17 changes: 17 additions & 0 deletions executor/cte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,20 @@ func TestCTEDelSpillFile(t *testing.T) {
tk.MustExec("insert into t1 (c1, c2) with recursive cte1 as (select c1 from t2 union select cte1.c1 + 1 from cte1 where cte1.c1 < 100000) select cte1.c1, cte1.c1+1 from cte1;")
require.Nil(t, tk.Session().GetSessionVars().StmtCtx.CTEStorageMap)
}

func TestCTESmallChunkSize(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(c1 int);")
insertStr := "insert into t1 values (0)"
for i := 1; i < 300; i++ {
insertStr += fmt.Sprintf(", (%d)", i)
}
tk.MustExec(insertStr)
tk.MustExec("set @@tidb_max_chunk_size = 32;")
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"))
tk.MustExec("set @@tidb_max_chunk_size = default;")
}

0 comments on commit 6ed9445

Please sign in to comment.