Skip to content

Commit

Permalink
partition: fix partitioning pruning includes an impossible partition (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyfhust authored Jul 25, 2023
1 parent 56610b1 commit b4183e1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions planner/core/integration_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ func TestRangeMultiColumnsPruning(t *testing.T) {
// WAS HERE, Why is the start return TRUE making this to work and FALSE disapear?
tk.MustQuery(`select a,b,c from t where a = 0 AND c = "Wow"`).Check(testkit.Rows("0 2020-01-01 00:00:00 Wow"))
tk.MustQuery(`explain format = 'brief' select a,b,c from t where a = 0 AND c = "Wow"`).Check(testkit.Rows(
`IndexReader 0.50 root partition:p3,p4,p5,p6,p7,p8 index:Selection`,
`IndexReader 0.50 root partition:p3,p4,p5,p6,p7 index:Selection`,
`└─Selection 0.50 cop[tikv] eq(rcolumnsmulti.t.c, "Wow")`,
` └─IndexRangeScan 1.00 cop[tikv] table:t, index:a(a, b, c) range:[0,0], keep order:false`))
}
Expand Down Expand Up @@ -1284,11 +1284,11 @@ func TestRangeColumnsExpr(t *testing.T) {
"└─Selection 0.05 cop[tikv] eq(rce.t.a, 5), eq(rce.t.c, 3)",
" └─TableFullScan 21.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a = 4 and c = 3`).Check(testkit.Rows(
"TableReader 0.43 root partition:p1,p2,p3,p4,p5,p6,p7,p8,p9 data:Selection",
"TableReader 0.43 root partition:p1,p2,p3,p4,p5,p6,p7,p8 data:Selection",
"└─Selection 0.43 cop[tikv] eq(rce.t.a, 4), eq(rce.t.c, 3)",
" └─TableFullScan 21.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a in (4,14) and c = 3`).Check(testkit.Rows(
"TableReader 0.57 root partition:p1,p2,p3,p4,p5,p6,p7,p8,p9,p11,p12 data:Selection",
"TableReader 0.57 root partition:p1,p2,p3,p4,p5,p6,p7,p8,p11,p12 data:Selection",
"└─Selection 0.57 cop[tikv] eq(rce.t.c, 3), in(rce.t.a, 4, 14)",
" └─TableFullScan 21.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a in (4,14) and b in (null,10)`).Check(testkit.Rows(
Expand Down
26 changes: 26 additions & 0 deletions planner/core/partition_pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,32 @@ func TestIssue33231(t *testing.T) {
Check(testkit.Rows("6 beautiful curran 6 vigorous rhodes", "7 affectionate curie 7 sweet aryabhata", "7 epic kalam 7 sweet aryabhata"))
}

func TestIssue42273(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database issue42273")
defer tk.MustExec("drop database issue42273")

tk.MustExec("use issue42273")
tk.MustExec(`CREATE TABLE t(a tinyint unsigned, b tinyint unsigned) PARTITION BY RANGE COLUMNS (a,b)(
PARTITION p0 VALUES LESS THAN (10,255),
PARTITION p1 VALUES LESS THAN (20,MAXVALUE),
PARTITION p2 VALUES LESS THAN (30,255),
PARTITION p3 VALUES LESS THAN (MAXVALUE, 0))`)
tk.MustExec("insert into t values(20, 30)")
tk.MustExec(`analyze table t`) // Creates global stats for the table and enables the dynamic pruning
tk.MustQuery(`explain format='brief' select * from t where a = 20`).Check(testkit.Rows(
"TableReader 1.00 root partition:p1 data:Selection",
"└─Selection 1.00 cop[tikv] eq(issue42273.t.a, 20)",
" └─TableFullScan 1.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format='brief' select * from t where a > 10 and a <= 20`).Check(testkit.Rows(
"TableReader 1.00 root partition:p1 data:Selection",
"└─Selection 1.00 cop[tikv] gt(issue42273.t.a, 10), le(issue42273.t.a, 20)",
" └─TableFullScan 1.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`select * from t where a = 20`).Check(testkit.Rows("20 30"))
tk.MustQuery(`select * from t where a > 10 and a <= 20`).Check(testkit.Rows("20 30"))
}

func TestIssue43459(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/partition_pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func TestPartitionRangeColumnsForExpr(t *testing.T) {
{"c = 3", partitionRangeOR{{0, len(partDefs)}}},
{"b > 3 AND c = 3", partitionRangeOR{{0, len(partDefs)}}},
{"a = 5 AND c = 3", partitionRangeOR{{9, 10}}},
{"a = 4 AND c = 3", partitionRangeOR{{1, 10}}},
{"a = 4 AND c = 3", partitionRangeOR{{1, 9}}},
{"b > 3", partitionRangeOR{{0, len(partDefs)}}},
{"a > 3", partitionRangeOR{{1, len(partDefs)}}},
{"a < 3", partitionRangeOR{{0, 1}}},
Expand Down
8 changes: 8 additions & 0 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,14 @@ func maxCmp(ctx sessionctx.Context, hiVal []types.Datum, columnsPruner *rangeCol
return false
}
}
// All hiVal == columnsPruner.lessThan
if len(hiVal) < len(columnsPruner.lessThan[i]) {
// Not all columns given
if columnsPruner.lessThan[i][len(hiVal)] == nil {
// MAXVALUE
return true
}
}
// if point is included, then false, due to LESS THAN
return hiExclude
}
Expand Down

0 comments on commit b4183e1

Please sign in to comment.