Skip to content

Commit

Permalink
planner: add range partition boundaries tests with BETWEEN expression (
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonss authored May 19, 2021
1 parent 9fe83e7 commit d1a5fa3
Show file tree
Hide file tree
Showing 3 changed files with 1,372 additions and 28 deletions.
102 changes: 74 additions & 28 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,38 @@ func (s *globalIndexSuite) TestIssue21731(c *C) {
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));")
}

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

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 {
// Just verify SELECT (also avoid double INSERTs during record)
output[i].Res = nil
output[i].Plan = nil
}
})
if isSelect {
tk.UsedPartitions(tt).Check(testkit.Rows(output[i].Plan...))
tk.MustQuery(tt).Sort().Check(testkit.Rows(output[i].Res...))
} else {
tk.MustExec(tt)
}
}
}

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

Expand All @@ -1589,12 +1621,6 @@ PARTITION BY RANGE (a) (
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)

Expand All @@ -1620,26 +1646,46 @@ PARTITION BY RANGE (a) (
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...))
}
func (s *testSuiteWithData) TestRangePartitionBoundariesBetweenM(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("CREATE DATABASE IF NOT EXISTS TestRangePartitionBoundariesBetweenM")
defer tk.MustExec("DROP DATABASE TestRangePartitionBoundariesBetweenM")
tk.MustExec("USE TestRangePartitionBoundariesBetweenM")
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)
}

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

tk.MustExec("CREATE DATABASE IF NOT EXISTS TestRangePartitionBoundariesBetweenS")
defer tk.MustExec("DROP DATABASE TestRangePartitionBoundariesBetweenS")
tk.MustExec("USE TestRangePartitionBoundariesBetweenS")
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)
}
131 changes: 131 additions & 0 deletions executor/testdata/executor_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,136 @@
"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"
]
},
{
"name": "TestRangePartitionBoundariesBetweenM",
"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 BETWEEN -2147483648 AND -2147483649",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2147483648",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2147483647",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2147483646",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2147483638",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2146483650",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2146483649",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2146483648",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2146483647",
"SELECT * FROM t WHERE a BETWEEN -2147483648 AND -2146483646",
"SELECT * FROM t WHERE a BETWEEN 0 AND -1",
"SELECT * FROM t WHERE a BETWEEN 0 AND 0",
"SELECT * FROM t WHERE a BETWEEN 0 AND 1",
"SELECT * FROM t WHERE a BETWEEN 0 AND 2",
"SELECT * FROM t WHERE a BETWEEN 0 AND 10",
"SELECT * FROM t WHERE a BETWEEN 0 AND 999998",
"SELECT * FROM t WHERE a BETWEEN 0 AND 999999",
"SELECT * FROM t WHERE a BETWEEN 0 AND 1000000",
"SELECT * FROM t WHERE a BETWEEN 0 AND 1000001",
"SELECT * FROM t WHERE a BETWEEN 0 AND 1000002",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 999997",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 999998",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 999999",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 1000000",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 1000008",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 1999996",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 1999997",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 1999998",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 1999999",
"SELECT * FROM t WHERE a BETWEEN 999998 AND 2000000",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 999998",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 999999",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 1000000",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 1000001",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 1000009",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 1999997",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 1999998",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 1999999",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 2000000",
"SELECT * FROM t WHERE a BETWEEN 999999 AND 2000001",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 999999",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 1000000",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 1000001",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 1000002",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 1000010",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 1999998",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 1999999",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 2000000",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 2000001",
"SELECT * FROM t WHERE a BETWEEN 1000000 AND 2000002",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 1000000",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 1000001",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 1000002",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 1000003",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 1000011",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 1999999",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 2000000",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 2000001",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 2000002",
"SELECT * FROM t WHERE a BETWEEN 1000001 AND 2000003",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 1000001",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 1000002",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 1000003",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 1000004",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 1000012",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 2000000",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 2000001",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 2000002",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 2000003",
"SELECT * FROM t WHERE a BETWEEN 1000002 AND 2000004",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 2999999",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 3000000",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 3000001",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 3000002",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 3000010",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 3999998",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 3999999",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 4000000",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 4000001",
"SELECT * FROM t WHERE a BETWEEN 3000000 AND 4000002",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 3000000",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 3000001",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 3000002",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 3000003",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 3000011",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 3999999",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 4000000",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 4000001",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 4000002",
"SELECT * FROM t WHERE a BETWEEN 3000001 AND 4000003"
]
},
{
"name": "TestRangePartitionBoundariesBetweenS",
"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 BETWEEN 2 AND -1",
"SELECT * FROM t WHERE a BETWEEN -1 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 0",
"SELECT * FROM t WHERE a BETWEEN 0 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 1",
"SELECT * FROM t WHERE a BETWEEN 1 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 2",
"SELECT * FROM t WHERE a BETWEEN 2 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 3",
"SELECT * FROM t WHERE a BETWEEN 3 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 4",
"SELECT * FROM t WHERE a BETWEEN 4 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 5",
"SELECT * FROM t WHERE a BETWEEN 5 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 6",
"SELECT * FROM t WHERE a BETWEEN 6 AND 4",
"SELECT * FROM t WHERE a BETWEEN 2 AND 7",
"SELECT * FROM t WHERE a BETWEEN 7 AND 4"
]
}
]
Loading

0 comments on commit d1a5fa3

Please sign in to comment.