Skip to content

Commit

Permalink
planner/core: Key partitioning uses an column index, which did not ma…
Browse files Browse the repository at this point in the history
…tch value pairs in fast plan (#50210) (#50603)

close #50206
  • Loading branch information
ti-chi-bot authored Jan 25, 2024
1 parent e90e302 commit bab8117
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pkg/planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1864,9 +1864,14 @@ func getPartitionInfo(ctx sessionctx.Context, tbl *model.TableInfo, pairs []name
case model.PartitionTypeKey:
// The key partition table supports FastPlan when it contains only one partition column
if len(pi.Columns) == 1 {
// We need to change the partition column index!
col := &expression.Column{}
*col = *partitionExpr.KeyPartCols[0]
col.Index = 0
pe := &tables.ForKeyPruning{KeyPartCols: []*expression.Column{col}}
for i, pair := range pairs {
if pi.Columns[0].L == pair.colName {
pos, err := partitionExpr.LocateKeyPartition(pi.Num, []types.Datum{pair.value})
pos, err := pe.LocateKeyPartition(pi.Num, []types.Datum{pair.value})
if err != nil {
return nil, 0, 0, false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/table/tables/test/partition/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"partition_test.go",
],
flaky = True,
shard_count = 17,
shard_count = 18,
deps = [
"//pkg/ddl",
"//pkg/domain",
Expand Down
10 changes: 10 additions & 0 deletions pkg/table/tables/test/partition/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3047,3 +3047,13 @@ func randStr(n int, r *rand.Rand) string {
}
return sb.String()
}

func TestPointGetKeyPartitioning(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`CREATE TABLE t (a VARCHAR(30) NOT NULL, b VARCHAR(45) NOT NULL,
c VARCHAR(45) NOT NULL, PRIMARY KEY (b, a)) PARTITION BY KEY(b) PARTITIONS 5`)
tk.MustExec(`INSERT INTO t VALUES ('Aa', 'Ab', 'Ac'), ('Ba', 'Bb', 'Bc')`)
tk.MustQuery(`SELECT * FROM t WHERE b = 'Ab'`).Check(testkit.Rows("Aa Ab Ac"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -873,3 +873,12 @@ HashJoin 1.22 root inner join, equal:[eq(issue42135.tx1.id, issue42135.tx2.id)
└─TableReader 9.99 root data:Selection
└─Selection 9.99 cop[tikv] eq(issue42135.tx2.rid, "1"), not(isnull(issue42135.tx2.id))
└─TableFullScan 10000.00 cop[tikv] table:tx2, partition:p2 keep order:false, stats:pseudo
create table t (a varchar(255), b int primary key nonclustered, key (a)) partition by key(b) partitions 3;
insert into t values ('Ab', 1),('abc',2),('BC',3),('AC',4),('BA',5),('cda',6);
analyze table t;
select * from t where b = 2 and a like 'abc';
a b
abc 2
select * from t where b = 2;
a b
abc 2
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,11 @@ analyze table tx2;
select * from tx1 inner join tx2 on tx1.ID=tx2.ID and tx1.ltype=tx2.ltype where tx2.rid='1';
explain format='brief' select * from tx1 inner join tx2 on tx1.ID=tx2.ID and tx1.ltype=tx2.ltype where tx2.rid='1';

# Test issue 50206
create table t (a varchar(255), b int primary key nonclustered, key (a)) partition by key(b) partitions 3;
insert into t values ('Ab', 1),('abc',2),('BC',3),('AC',4),('BA',5),('cda',6);
analyze table t;
# non Fast Plan works!
select * from t where b = 2 and a like 'abc';
# Fast plan had a bug
select * from t where b = 2;

0 comments on commit bab8117

Please sign in to comment.