Skip to content

Commit 22f1286

Browse files
authored
executor: fix batchPoint for partition table with duplicate rows (#46808)
close #46779
1 parent eab042a commit 22f1286

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

executor/batch_point_get_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,32 @@ func TestPointGetForTemporaryTable(t *testing.T) {
370370
tk.MustQuery("select * from t1 where id = 1").Check(testkit.Rows("1 1"))
371371
tk.MustQuery("select * from t1 where id = 2").Check(testkit.Rows())
372372
}
373+
374+
func TestBatchPointGetIssue46779(t *testing.T) {
375+
store := testkit.CreateMockStore(t)
376+
377+
tk := testkit.NewTestKit(t, store)
378+
tk.MustExec("use test")
379+
tk.MustExec("drop table if exists t1")
380+
tk.MustExec("CREATE TABLE t1 (id int, c varchar(128), primary key (id)) PARTITION BY HASH (id) PARTITIONS 3;")
381+
tk.MustExec(`insert into t1 values (1, "a"), (11, "b"), (21, "c")`)
382+
query := "select * from t1 where id in (1, 1, 11)"
383+
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
384+
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b"))
385+
query = "select * from t1 where id in (1, 11, 11, 21)"
386+
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
387+
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b", "21 c"))
388+
389+
tk.MustExec("drop table if exists t2")
390+
tk.MustExec(`CREATE TABLE t2 (id int, c varchar(128), primary key (id)) partition by range (id)(
391+
partition p0 values less than (10),
392+
partition p1 values less than (20),
393+
partition p2 values less than (30));`)
394+
tk.MustExec(`insert into t2 values (1, "a"), (11, "b"), (21, "c")`)
395+
query = "select * from t2 where id in (1, 1, 11)"
396+
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
397+
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b"))
398+
require.True(t, tk.HasPlan(query, "Batch_Point_Get")) // check if BatchPointGet is used
399+
query = "select * from t2 where id in (1, 11, 11, 21)"
400+
tk.MustQuery(query).Sort().Check(testkit.Rows("1 a", "11 b", "21 c"))
401+
}

executor/builder.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,16 +5226,22 @@ func (b *executorBuilder) buildBatchPointGet(plan *plannercore.BatchPointGetPlan
52265226
// `SELECT a FROM t WHERE a IN (1, 1, 2, 1, 2)` should not return duplicated rows
52275227
handles := make([]kv.Handle, 0, len(plan.Handles))
52285228
dedup := kv.NewHandleMap()
5229+
// Used for clear paritionIDs of duplicated rows.
5230+
dupPartPos := 0
52295231
if plan.IndexInfo == nil {
5230-
for _, handle := range plan.Handles {
5232+
for idx, handle := range plan.Handles {
52315233
if _, found := dedup.Get(handle); found {
52325234
continue
52335235
}
52345236
dedup.Set(handle, true)
52355237
handles = append(handles, handle)
5238+
if len(plan.PartitionIDs) > 0 {
5239+
e.planPhysIDs[dupPartPos] = e.planPhysIDs[idx]
5240+
dupPartPos++
5241+
}
52365242
}
52375243
} else {
5238-
for _, value := range plan.IndexValues {
5244+
for idx, value := range plan.IndexValues {
52395245
if datumsContainNull(value) {
52405246
continue
52415247
}
@@ -5257,9 +5263,16 @@ func (b *executorBuilder) buildBatchPointGet(plan *plannercore.BatchPointGetPlan
52575263
}
52585264
dedup.Set(handle, true)
52595265
handles = append(handles, handle)
5266+
if len(plan.PartitionIDs) > 0 {
5267+
e.planPhysIDs[dupPartPos] = e.planPhysIDs[idx]
5268+
dupPartPos++
5269+
}
52605270
}
52615271
}
52625272
e.handles = handles
5273+
if dupPartPos > 0 {
5274+
e.planPhysIDs = e.planPhysIDs[:dupPartPos]
5275+
}
52635276
capacity = len(e.handles)
52645277
}
52655278
e.Base().SetInitCap(capacity)

0 commit comments

Comments
 (0)