Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partition: fix partitioning pruning includes an impossible partition (#42356) #45555

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
93 changes: 93 additions & 0 deletions planner/core/partition_pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
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 @@ -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
}
Expand Down