From b4183e1dc9bb01fb81d3aa79ca4b5b74387c6c2a Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Tue, 25 Jul 2023 16:04:33 +0800 Subject: [PATCH] partition: fix partitioning pruning includes an impossible partition (#42356) close pingcap/tidb#42273 --- planner/core/integration_partition_test.go | 6 ++--- planner/core/partition_pruner_test.go | 26 ++++++++++++++++++++++ planner/core/partition_pruning_test.go | 2 +- planner/core/rule_partition_processor.go | 8 +++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/planner/core/integration_partition_test.go b/planner/core/integration_partition_test.go index 5af37d080189a..13d58bd86fcd2 100644 --- a/planner/core/integration_partition_test.go +++ b/planner/core/integration_partition_test.go @@ -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`)) } @@ -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( diff --git a/planner/core/partition_pruner_test.go b/planner/core/partition_pruner_test.go index 1b1f60d46325a..54845643cb717 100644 --- a/planner/core/partition_pruner_test.go +++ b/planner/core/partition_pruner_test.go @@ -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) diff --git a/planner/core/partition_pruning_test.go b/planner/core/partition_pruning_test.go index 3059567da6d5a..f11c4209d6543 100644 --- a/planner/core/partition_pruning_test.go +++ b/planner/core/partition_pruning_test.go @@ -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}}}, diff --git a/planner/core/rule_partition_processor.go b/planner/core/rule_partition_processor.go index 7b14cd38e91ba..363835ae58c4a 100644 --- a/planner/core/rule_partition_processor.go +++ b/planner/core/rule_partition_processor.go @@ -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 }