diff --git a/planner/core/integration_partition_test.go b/planner/core/integration_partition_test.go index 5fd57cc33ff14..3748902f45860 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 dd413e0c5940d..aa4b27cdf15b4 100644 --- a/planner/core/partition_pruner_test.go +++ b/planner/core/partition_pruner_test.go @@ -453,3 +453,96 @@ func TestIssue33231(t *testing.T) { Sort(). Check(testkit.Rows("6 beautiful curran 6 vigorous rhodes", "7 affectionate curie 7 sweet aryabhata", "7 epic kalam 7 sweet aryabhata")) } +<<<<<<< HEAD +======= + +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) + tk.MustExec("create database issue43459") + defer tk.MustExec("drop database issue43459") + tk.MustExec("use issue43459") + tk.MustExec("set @@session.tidb_partition_prune_mode = 'dynamic';") + tk.MustExec(`CREATE TABLE test1 (ID varchar(50) NOT NULL, + PARTITION_NO int(11) NOT NULL DEFAULT '0', + CREATE_TIME datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (ID,PARTITION_NO,CREATE_TIME), + KEY index_partition_no (PARTITION_NO) + ) PARTITION BY RANGE COLUMNS(PARTITION_NO,CREATE_TIME) + (PARTITION 2023p1 VALUES LESS THAN (200000,'2023-01-01 00:00:00'), + PARTITION 2023p2 VALUES LESS THAN (300000,'2023-01-01 00:00:00')) `) + checkFn := func() { + tk.MustExec(`insert into test1 values("1", 200000, "2022-12-29 12:00:00"), ("2",200000,"2023-01-01")`) + tk.MustExec(`analyze table test1`) + tk.MustQuery(`explain select * from test1 where partition_no > 199999`).CheckContain(`partition:all`) + tk.MustQuery(`explain select * from test1 where partition_no = 200000`).CheckContain("partition:all") + tk.MustQuery(`explain select * from test1 where partition_no >= 200000`).CheckContain("partition:all") + tk.MustQuery(`explain select * from test1 where partition_no < 200000`).CheckContain("partition:2023p1") + tk.MustQuery(`explain select * from test1 where partition_no <= 200000`).CheckContain("partition:all") + tk.MustQuery(`explain select * from test1 where partition_no > 200000`).CheckContain("partition:2023p2") + } + checkFn() + tk.MustQuery(`select * from test1 partition (2023p1)`).Check(testkit.Rows("" + + "1 200000 2022-12-29 12:00:00")) + tk.MustQuery(`select * from test1 partition (2023p2)`).Check(testkit.Rows("" + + "2 200000 2023-01-01 00:00:00")) + tk.MustQuery(`select * from test1`).Sort().Check(testkit.Rows(""+ + "1 200000 2022-12-29 12:00:00", + "2 200000 2023-01-01 00:00:00")) + tk.MustQuery(`select * from test1 where partition_no = 200000`).Sort().Check(testkit.Rows(""+ + "1 200000 2022-12-29 12:00:00", + "2 200000 2023-01-01 00:00:00")) + tk.MustQuery(`select * from test1 where partition_no >= 200000`).Sort().Check(testkit.Rows(""+ + "1 200000 2022-12-29 12:00:00", + "2 200000 2023-01-01 00:00:00")) + tk.MustExec(`drop table test1`) + tk.MustExec(`CREATE TABLE test1 (ID varchar(50) NOT NULL, + PARTITION_NO int(11) NOT NULL DEFAULT '0', + CREATE_TIME date NOT NULL DEFAULT CURRENT_DATE, + PRIMARY KEY (ID,PARTITION_NO,CREATE_TIME), + KEY index_partition_no (PARTITION_NO) + ) PARTITION BY RANGE COLUMNS(PARTITION_NO,CREATE_TIME) + (PARTITION 2023p1 VALUES LESS THAN (200000,'2023-01-01 00:00:00'), + PARTITION 2023p2 VALUES LESS THAN (300000,'2023-01-01 00:00:00')) `) + checkFn() + tk.MustQuery(`select * from test1 partition (2023p1)`).Check(testkit.Rows("" + + "1 200000 2022-12-29")) + tk.MustQuery(`select * from test1 partition (2023p2)`).Check(testkit.Rows("" + + "2 200000 2023-01-01")) + tk.MustQuery(`select * from test1`).Sort().Check(testkit.Rows(""+ + "1 200000 2022-12-29", + "2 200000 2023-01-01")) + tk.MustQuery(`select * from test1 where partition_no = 200000`).Sort().Check(testkit.Rows(""+ + "1 200000 2022-12-29", + "2 200000 2023-01-01")) + tk.MustQuery(`select * from test1 where partition_no >= 200000`).Sort().Check(testkit.Rows(""+ + "1 200000 2022-12-29", + "2 200000 2023-01-01")) +} +>>>>>>> b4183e1dc9b (partition: fix partitioning pruning includes an impossible partition (#42356)) 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 1df3c512e99ca..12ba73606f52c 100644 --- a/planner/core/rule_partition_processor.go +++ b/planner/core/rule_partition_processor.go @@ -1150,6 +1150,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 }