Skip to content

Commit

Permalink
planner: fix list partition with non point range locates to a wrong p…
Browse files Browse the repository at this point in the history
…artition (#57072)

close #57062
  • Loading branch information
Defined2014 authored Nov 11, 2024
1 parent b5cf2c3 commit d7fccb3
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ func TestListPartitionPruning(t *testing.T) {
tk.MustExec("create database list_partition_pruning")
tk.MustExec("use list_partition_pruning")
tk.MustExec("drop table if exists tlist")
tk.MustExec(`create table tlist (a int) partition by list (a) (
tk.MustExec(`create table tlist (a int, b int) partition by list (a) (
partition p0 values in (0, 1, 2),
partition p1 values in (3, 4, 5),
partition p2 values in (6, 7, 8),
partition p3 values in (9, 10, 11))`)
tk.MustExec(`create table tcollist (a int) partition by list columns(a) (
partition p3 values in (9, 10, 11),
partition p4 values in (-1))`)
tk.MustExec(`create table tcollist (a int, b int) partition by list columns(a) (
partition p0 values in (0, 1, 2),
partition p1 values in (3, 4, 5),
partition p2 values in (6, 7, 8),
partition p3 values in (9, 10, 11))`)
partition p3 values in (9, 10, 11),
partition p4 values in (-1))`)
tk.MustExec(`analyze table tlist`)
tk.MustExec(`analyze table tcollist`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
"explain format = 'brief' replace tlist select * from tlist where a in (2)",
"explain format = 'brief' replace tlist select * from tlist where a in (0, 5)",
"explain format = 'brief' replace tcollist select * from tcollist where a in (2)",
"explain format = 'brief' replace tcollist select * from tcollist where a in (0, 5)"
"explain format = 'brief' replace tcollist select * from tcollist where a in (0, 5)",
// for non partition column
"explain format = 'brief' select * from tlist where b > 0",
"explain format = 'brief' select * from tcollist where b > 0",
"explain format = 'brief' select * from tlist where b < 0",
"explain format = 'brief' select * from tcollist where b < 0"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,35 @@
{
"SQL": "explain format = 'brief' select * from tlist where a not in (0, 1, 2, 3, 4, 5, 6, 7, 8)",
"DynamicPlan": [
"TableReader 3583.33 root partition:p3 data:Selection",
"TableReader 3583.33 root partition:p3,p4 data:Selection",
"└─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tlist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
" └─TableFullScan 10000.00 cop[tikv] table:tlist keep order:false, stats:pseudo"
],
"StaticPlan": [
"TableReader 3583.33 root data:Selection",
"└─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tlist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p3 keep order:false, stats:pseudo"
"PartitionUnion 7166.67 root ",
"├─TableReader 3583.33 root data:Selection",
"│ └─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tlist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p3 keep order:false, stats:pseudo",
"└─TableReader 3583.33 root data:Selection",
" └─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tlist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p4 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tcollist where a not in (0, 1, 2, 3, 4, 5, 6, 7, 8)",
"DynamicPlan": [
"TableReader 3583.33 root partition:p3 data:Selection",
"TableReader 3583.33 root partition:p3,p4 data:Selection",
"└─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tcollist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist keep order:false, stats:pseudo"
],
"StaticPlan": [
"TableReader 3583.33 root data:Selection",
"└─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tcollist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p3 keep order:false, stats:pseudo"
"PartitionUnion 7166.67 root ",
"├─TableReader 3583.33 root data:Selection",
"│ └─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tcollist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p3 keep order:false, stats:pseudo",
"└─TableReader 3583.33 root data:Selection",
" └─Selection 3583.33 cop[tikv] not(in(list_partition_pruning.tcollist.a, 0, 1, 2, 3, 4, 5, 6, 7, 8))",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p4 keep order:false, stats:pseudo"
]
},
{
Expand Down Expand Up @@ -151,41 +159,47 @@
{
"SQL": "explain format = 'brief' select * from tlist where a <= 6",
"DynamicPlan": [
"TableReader 3323.33 root partition:p0,p1,p2 data:Selection",
"TableReader 3323.33 root partition:p0,p1,p2,p4 data:Selection",
"└─Selection 3323.33 cop[tikv] le(list_partition_pruning.tlist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 9970.00 root ",
"PartitionUnion 13293.33 root ",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tlist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tlist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p1 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tlist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p2 keep order:false, stats:pseudo",
"└─TableReader 3323.33 root data:Selection",
" └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tlist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p2 keep order:false, stats:pseudo"
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p4 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tcollist where a <= 6",
"DynamicPlan": [
"TableReader 3323.33 root partition:p0,p1,p2 data:Selection",
"TableReader 3323.33 root partition:p0,p1,p2,p4 data:Selection",
"└─Selection 3323.33 cop[tikv] le(list_partition_pruning.tcollist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 9970.00 root ",
"PartitionUnion 13293.33 root ",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tcollist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tcollist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p1 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tcollist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p2 keep order:false, stats:pseudo",
"└─TableReader 3323.33 root data:Selection",
" └─Selection 3323.33 cop[tikv] le(list_partition_pruning.tcollist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p2 keep order:false, stats:pseudo"
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p4 keep order:false, stats:pseudo"
]
},
{
Expand Down Expand Up @@ -217,35 +231,41 @@
{
"SQL": "explain format = 'brief' select * from tlist where a < 6",
"DynamicPlan": [
"TableReader 3323.33 root partition:p0,p1 data:Selection",
"TableReader 3323.33 root partition:p0,p1,p4 data:Selection",
"└─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 6646.67 root ",
"PartitionUnion 9970.00 root ",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p1 keep order:false, stats:pseudo",
"└─TableReader 3323.33 root data:Selection",
" └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p1 keep order:false, stats:pseudo"
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p4 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tcollist where a < 6",
"DynamicPlan": [
"TableReader 3323.33 root partition:p0,p1 data:Selection",
"TableReader 3323.33 root partition:p0,p1,p4 data:Selection",
"└─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 6646.67 root ",
"PartitionUnion 9970.00 root ",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.a, 6)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p1 keep order:false, stats:pseudo",
"└─TableReader 3323.33 root data:Selection",
" └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.a, 6)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p1 keep order:false, stats:pseudo"
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p4 keep order:false, stats:pseudo"
]
},
{
Expand Down Expand Up @@ -659,6 +679,110 @@
" └─Selection 20.00 cop[tikv] in(list_partition_pruning.tcollist.a, 0, 5)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p1 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tlist where b > 0",
"DynamicPlan": [
"TableReader 3333.33 root partition:all data:Selection",
"└─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tlist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 16666.67 root ",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p1 keep order:false, stats:pseudo",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p2 keep order:false, stats:pseudo",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p3 keep order:false, stats:pseudo",
"└─TableReader 3333.33 root data:Selection",
" └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tlist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p4 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tcollist where b > 0",
"DynamicPlan": [
"TableReader 3333.33 root partition:all data:Selection",
"└─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tcollist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 16666.67 root ",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p1 keep order:false, stats:pseudo",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p2 keep order:false, stats:pseudo",
"├─TableReader 3333.33 root data:Selection",
"│ └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p3 keep order:false, stats:pseudo",
"└─TableReader 3333.33 root data:Selection",
" └─Selection 3333.33 cop[tikv] gt(list_partition_pruning.tcollist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p4 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tlist where b < 0",
"DynamicPlan": [
"TableReader 3323.33 root partition:all data:Selection",
"└─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 16616.67 root ",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p1 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p2 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p3 keep order:false, stats:pseudo",
"└─TableReader 3323.33 root data:Selection",
" └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tlist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tlist, partition:p4 keep order:false, stats:pseudo"
]
},
{
"SQL": "explain format = 'brief' select * from tcollist where b < 0",
"DynamicPlan": [
"TableReader 3323.33 root partition:all data:Selection",
"└─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist keep order:false, stats:pseudo"
],
"StaticPlan": [
"PartitionUnion 16616.67 root ",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p0 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p1 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p2 keep order:false, stats:pseudo",
"├─TableReader 3323.33 root data:Selection",
"│ └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.b, 0)",
"│ └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p3 keep order:false, stats:pseudo",
"└─TableReader 3323.33 root data:Selection",
" └─Selection 3323.33 cop[tikv] lt(list_partition_pruning.tcollist.b, 0)",
" └─TableFullScan 10000.00 cop[tikv] table:tcollist, partition:p4 keep order:false, stats:pseudo"
]
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ func (l *listPartitionPruner) findUsedListPartitions(conds []expression.Expressi
used := make(map[int]struct{}, len(ranges))
tc := l.ctx.GetSessionVars().StmtCtx.TypeCtx()
for _, r := range ranges {
if len(r.HighVal) != len(exprCols) {
if len(r.HighVal) != len(exprCols) || r.IsFullRange(false) {
return l.fullRange, nil
}
var idxs map[int]struct{}
Expand Down
Loading

0 comments on commit d7fccb3

Please sign in to comment.