Skip to content

Commit

Permalink
planner/core: fix partition selection on PointGet/BatchPointGet (#19146
Browse files Browse the repository at this point in the history
…) (#19164)
  • Loading branch information
tiancaiamao authored Aug 18, 2020
1 parent 201153e commit e196a3e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
31 changes: 28 additions & 3 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,12 @@ func newBatchPointGetPlan(
names []*types.FieldName, whereColNames []string,
) *BatchPointGetPlan {
statsInfo := &property.StatsInfo{RowCount: float64(len(patternInExpr.List))}
partitionColName := getHashPartitionColumnName(ctx, tbl)
if tbl.GetPartitionInfo() != nil && partitionColName == nil {
return nil
var partitionColName *ast.ColumnName
if tbl.GetPartitionInfo() != nil {
partitionColName = getHashPartitionColumnName(ctx, tbl)
if partitionColName == nil {
return nil
}
}
if handleCol != nil {
var handles = make([]kv.Handle, len(patternInExpr.List))
Expand Down Expand Up @@ -606,6 +609,10 @@ func tryWhereIn2BatchPointGet(ctx sessionctx.Context, selStmt *ast.SelectStmt) *
if tbl == nil {
return nil
}
// Skip the optimization with partition selection.
if len(tblName.PartitionNames) > 0 {
return nil
}

for _, col := range tbl.Columns {
if col.IsGenerated() || col.State != model.StatePublic {
Expand Down Expand Up @@ -734,6 +741,14 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
if partitionInfo == nil {
return nil
}
// Take partition selection into consideration.
if len(tblName.PartitionNames) > 0 {
if !partitionNameInSet(partitionInfo.Name, tblName.PartitionNames) {
p := newPointGetPlan(ctx, tblName.Schema.O, schema, tbl, names)
p.IsTableDual = true
return p
}
}
}

handlePair, fieldType := findPKHandle(tbl, pairs)
Expand Down Expand Up @@ -782,6 +797,16 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
return nil
}

func partitionNameInSet(name model.CIStr, pnames []model.CIStr) bool {
for _, pname := range pnames {
// Case insensitive, create table partition p0, query using P0 is OK.
if name.L == pname.L {
return true
}
}
return false
}

func newPointGetPlan(ctx sessionctx.Context, dbName string, schema *expression.Schema, tbl *model.TableInfo, names []*types.FieldName) *PointGetPlan {
p := &PointGetPlan{
basePlan: newBasePlan(ctx, plancodec.TypePointGet, 0),
Expand Down
19 changes: 19 additions & 0 deletions planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,22 @@ func (s *testPointGetSuite) TestBatchPointGetPartition(c *C) {
tk.MustExec("delete from t where (a,b) in ((1,1),(2,2),(3,3),(4,4))")
tk.MustQuery("select * from t where (a, b) in ((1, 1), (2, 2), (3, 3), (4, 4))").Check(testkit.Rows())
}

func (s *testPointGetSuite) TestIssue19141(c *C) {
// For issue 19141, fix partition selection on batch point get.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t19141 (c_int int, primary key (c_int)) partition by hash ( c_int ) partitions 4")
tk.MustExec("insert into t19141 values (1), (2), (3), (4)")
tk.MustQuery("select * from t19141 partition (p0)").Check(testkit.Rows("4"))
tk.MustQuery("select * from t19141 partition (p0) where c_int = 1").Check(testkit.Rows())
tk.MustExec("update t19141 partition (p0) set c_int = -c_int where c_int = 1") // TableDual after partition selection.
tk.MustQuery("select * from t19141 order by c_int").Check(testkit.Rows("1", "2", "3", "4"))

// Bach point get
tk.MustQuery("select * from t19141 partition (p0, p2) where c_int in (1,2,3)").Check(testkit.Rows("2"))
tk.MustExec("update t19141 partition (p1) set c_int = -c_int where c_int in (2,3)") // No data changed
tk.MustQuery("select * from t19141 order by c_int").Check(testkit.Rows("1", "2", "3", "4"))
tk.MustExec("delete from t19141 partition (p0) where c_int in (2,3)") // No data changed
tk.MustQuery("select * from t19141 order by c_int").Check(testkit.Rows("1", "2", "3", "4"))
}

0 comments on commit e196a3e

Please sign in to comment.