Skip to content

Commit

Permalink
planner: add partitioning pruning tests for range partitioning (#24554)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonss committed May 19, 2021
1 parent 0a1c3c0 commit 883c7fb
Show file tree
Hide file tree
Showing 4 changed files with 993 additions and 0 deletions.
77 changes: 77 additions & 0 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,3 +1452,80 @@ func (s *globalIndexSuite) TestIssue21731(c *C) {
tk.MustExec("drop table if exists p, t")
tk.MustExec("create table t (a int, b int, unique index idx(a)) partition by list columns(b) (partition p0 values in (1), partition p1 values in (2));")
}

func (s *testSuiteWithData) TestRangePartitionBoundariesEq(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("SET @@tidb_partition_prune_mode = 'dynamic'")
tk.MustExec("CREATE DATABASE TestRangePartitionBoundaries")
defer tk.MustExec("DROP DATABASE TestRangePartitionBoundaries")
tk.MustExec("USE TestRangePartitionBoundaries")
tk.MustExec("DROP TABLE IF EXISTS t")
tk.MustExec(`CREATE TABLE t
(a INT, b varchar(255))
PARTITION BY RANGE (a) (
PARTITION p0 VALUES LESS THAN (1000000),
PARTITION p1 VALUES LESS THAN (2000000),
PARTITION p2 VALUES LESS THAN (3000000));
`)

var input []string
var output []testOutput
s.testData.GetTestCases(c, &input, &output)
s.verifyPartitionResult(tk, input, output)
}

type testOutput struct {
SQL string
Plan []string
Res []string
}

func (s *testSuiteWithData) TestRangePartitionBoundariesNe(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("SET @@tidb_partition_prune_mode = 'dynamic'")
tk.MustExec("CREATE DATABASE TestRangePartitionBoundariesNe")
defer tk.MustExec("DROP DATABASE TestRangePartitionBoundariesNe")
tk.MustExec("USE TestRangePartitionBoundariesNe")
tk.MustExec("DROP TABLE IF EXISTS t")
tk.MustExec(`CREATE TABLE t
(a INT, b varchar(255))
PARTITION BY RANGE (a) (
PARTITION p0 VALUES LESS THAN (1),
PARTITION p1 VALUES LESS THAN (2),
PARTITION p2 VALUES LESS THAN (3),
PARTITION p3 VALUES LESS THAN (4),
PARTITION p4 VALUES LESS THAN (5),
PARTITION p5 VALUES LESS THAN (6),
PARTITION p6 VALUES LESS THAN (7))`)

var input []string
var output []testOutput
s.testData.GetTestCases(c, &input, &output)
s.verifyPartitionResult(tk, input, output)
}

func (s *testSuiteWithData) verifyPartitionResult(tk *testkit.TestKit, input []string, output []testOutput) {
for i, tt := range input {
var isSelect bool = false
if strings.HasPrefix(strings.ToLower(tt), "select ") {
isSelect = true
}
s.testData.OnRecord(func() {
output[i].SQL = tt
if isSelect {
output[i].Plan = s.testData.ConvertRowsToStrings(tk.UsedPartitions(tt).Rows())
output[i].Res = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Sort().Rows())
} else {
// to avoid double execution of INSERT (and INSERT does not return anything)
output[i].Res = nil
output[i].Plan = nil
}
})
if isSelect {
tk.UsedPartitions(tt).Check(testkit.Rows(output[i].Plan...))
}
tk.MayQuery(tt).Sort().Check(testkit.Rows(output[i].Res...))
}
}
91 changes: 91 additions & 0 deletions executor/testdata/executor_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,96 @@
"select count(*) from t as t1 left join t as t2 on t1.c1 = t2.c1 where t1.c1 != NULL",
"select * from t as t1 left join t as t2 on t1.c1 = t2.c1 where t1.c1 is not NULL"
]
},
{
"name": "TestRangePartitionBoundariesEq",
"cases": [
"INSERT INTO t VALUES (999998, '999998 Filler ...'), (999999, '999999 Filler ...'), (1000000, '1000000 Filler ...'), (1000001, '1000001 Filler ...'), (1000002, '1000002 Filler ...')",
"INSERT INTO t VALUES (1999998, '1999998 Filler ...'), (1999999, '1999999 Filler ...'), (2000000, '2000000 Filler ...'), (2000001, '2000001 Filler ...'), (2000002, '2000002 Filler ...')",
"INSERT INTO t VALUES (2999998, '2999998 Filler ...'), (2999999, '2999999 Filler ...')",
"INSERT INTO t VALUES (-2147483648, 'MIN_INT filler...'), (0, '0 Filler...')",
"ANALYZE TABLE t",
"SELECT * FROM t WHERE a = -2147483648",
"SELECT * FROM t WHERE a IN (-2147483648)",
"SELECT * FROM t WHERE a = 0",
"SELECT * FROM t WHERE a IN (0)",
"SELECT * FROM t WHERE a = 999998",
"SELECT * FROM t WHERE a IN (999998)",
"SELECT * FROM t WHERE a = 999999",
"SELECT * FROM t WHERE a IN (999999)",
"SELECT * FROM t WHERE a = 1000000",
"SELECT * FROM t WHERE a IN (1000000)",
"SELECT * FROM t WHERE a = 1000001",
"SELECT * FROM t WHERE a IN (1000001)",
"SELECT * FROM t WHERE a = 1000002",
"SELECT * FROM t WHERE a IN (1000002)",
"SELECT * FROM t WHERE a = 3000000",
"SELECT * FROM t WHERE a IN (3000000)",
"SELECT * FROM t WHERE a = 3000001",
"SELECT * FROM t WHERE a IN (3000001)",
"SELECT * FROM t WHERE a IN (-2147483648, -2147483647)",
"SELECT * FROM t WHERE a IN (-2147483647, -2147483646)",
"SELECT * FROM t WHERE a IN (999997, 999998, 999999)",
"SELECT * FROM t WHERE a IN (999998, 999999, 1000000)",
"SELECT * FROM t WHERE a IN (999999, 1000000, 1000001)",
"SELECT * FROM t WHERE a IN (1000000, 1000001, 1000002)",
"SELECT * FROM t WHERE a IN (1999997, 1999998, 1999999)",
"SELECT * FROM t WHERE a IN (1999998, 1999999, 2000000)",
"SELECT * FROM t WHERE a IN (1999999, 2000000, 2000001)",
"SELECT * FROM t WHERE a IN (2000000, 2000001, 2000002)",
"SELECT * FROM t WHERE a IN (2999997, 2999998, 2999999)",
"SELECT * FROM t WHERE a IN (2999998, 2999999, 3000000)",
"SELECT * FROM t WHERE a IN (2999999, 3000000, 3000001)",
"SELECT * FROM t WHERE a IN (3000000, 3000001, 3000002)"
]
},
{
"name": "TestRangePartitionBoundariesNe",
"cases": [
"INSERT INTO t VALUES (0, '0 Filler...')",
"INSERT INTO t VALUES (1, '1 Filler...')",
"INSERT INTO t VALUES (2, '2 Filler...')",
"INSERT INTO t VALUES (3, '3 Filler...')",
"INSERT INTO t VALUES (4, '4 Filler...')",
"INSERT INTO t VALUES (5, '5 Filler...')",
"INSERT INTO t VALUES (6, '6 Filler...')",
"ANALYZE TABLE t",
"SELECT * FROM t WHERE a != -1",
"SELECT * FROM t WHERE 1 = 1 AND a != -1",
"SELECT * FROM t WHERE a NOT IN (-2, -1)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1",
"SELECT * FROM t WHERE a != 0",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0",
"SELECT * FROM t WHERE a != 1",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1",
"SELECT * FROM t WHERE a != 2",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1 AND a != 2",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1, 2)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1 OR a = 2",
"SELECT * FROM t WHERE a != 3",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1 AND a != 2 AND a != 3",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1, 2, 3)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1 OR a = 2 OR a = 3",
"SELECT * FROM t WHERE a != 4",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1 AND a != 2 AND a != 3 AND a != 4",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1, 2, 3, 4)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1 OR a = 2 OR a = 3 OR a = 4",
"SELECT * FROM t WHERE a != 5",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1 AND a != 2 AND a != 3 AND a != 4 AND a != 5",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1, 2, 3, 4, 5)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1 OR a = 2 OR a = 3 OR a = 4 OR a = 5",
"SELECT * FROM t WHERE a != 6",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1 AND a != 2 AND a != 3 AND a != 4 AND a != 5 AND a != 6",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1, 2, 3, 4, 5, 6)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1 OR a = 2 OR a = 3 OR a = 4 OR a = 5 OR a = 6",
"SELECT * FROM t WHERE a != 7",
"SELECT * FROM t WHERE 1 = 1 AND a != -1 AND a != 0 AND a != 1 AND a != 2 AND a != 3 AND a != 4 AND a != 5 AND a != 6 AND a != 7",
"SELECT * FROM t WHERE a NOT IN (-2, -1, 0, 1, 2, 3, 4, 5, 6, 7)",
"SELECT * FROM t WHERE 1 = 0 OR a = -1 OR a = 0 OR a = 1 OR a = 2 OR a = 3 OR a = 4 OR a = 5 OR a = 6 OR a = 7"
]
}
]
Loading

0 comments on commit 883c7fb

Please sign in to comment.