Skip to content

Commit

Permalink
executor, util: fix UnionScan Next() skip reading data when passed ch…
Browse files Browse the repository at this point in the history
…unk capacity is 0 (#36961) (#37332)

close #36903
  • Loading branch information
ti-srebot authored Sep 28, 2022
1 parent 0ebc01d commit f9b13b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions executor/union_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ func (us *UnionScanExec) open(ctx context.Context) error {
func (us *UnionScanExec) Next(ctx context.Context, req *chunk.Chunk) error {
us.memBuf.RLock()
defer us.memBuf.RUnlock()

// Assume req.Capacity() > 0 after GrowAndReset(), if this assumption fail,
// the for-loop may exit without read one single row!
req.GrowAndReset(us.maxChunkSize)

mutableRow := chunk.MutRowFromTypes(retTypes(us))
for i, batchSize := 0, req.Capacity(); i < batchSize; i++ {
row, err := us.getOneRow(ctx)
Expand Down
14 changes: 14 additions & 0 deletions executor/union_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,17 @@ func TestForApplyAndUnionScan(t *testing.T) {
tk.MustQuery("select c_int, c_str from t where (select count(*) from t1 where t1.c_int in (t.c_int, t.c_int + 2, t.c_int + 10)) > 2").Check(testkit.Rows())
tk.MustExec("rollback")
}

func TestIssue36903(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 t_vwvgdc")

tk.MustExec("CREATE TABLE t_vwvgdc (wkey int, pkey int NOT NULL, c_rdsfbc double DEFAULT NULL, PRIMARY KEY (`pkey`));")
tk.MustExec("insert into t_vwvgdc values (2, 15000, 61.75);")
tk.MustExec("BEGIN OPTIMISTIC;")
tk.MustExec("insert into t_vwvgdc (wkey, pkey, c_rdsfbc) values (155, 228000, 99.50);")
tk.MustQuery("select pkey from t_vwvgdc where 0 <> 0 union select pkey from t_vwvgdc;")
}
6 changes: 5 additions & 1 deletion util/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ func reCalcCapacity(c *Chunk, maxChunkSize int) int {
if c.NumRows() < c.capacity {
return c.capacity
}
return mathutil.Min(c.capacity*2, maxChunkSize)
newCapacity := c.capacity * 2
if newCapacity == 0 {
newCapacity = InitialCapacity
}
return mathutil.Min(newCapacity, maxChunkSize)
}

// Capacity returns the capacity of the Chunk.
Expand Down

0 comments on commit f9b13b7

Please sign in to comment.