Skip to content

Commit

Permalink
Merge branch 'master' into m/deadlock-table
Browse files Browse the repository at this point in the history
  • Loading branch information
MyonKeminta authored May 19, 2021
2 parents 97a9d1e + 49d1eaa commit 816a573
Show file tree
Hide file tree
Showing 36 changed files with 1,860 additions and 198 deletions.
76 changes: 76 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8218,3 +8218,79 @@ func (s *testSerialSuite) TestDeadlockTable(c *C) {
id2+"/2022-06-11 02:03:04.987654/1/203/<nil>/<nil>/<nil>/201",
))
}

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

tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a enum('a','b','c'), b enum('a','b','c'), c int, index idx(b,a));")
tk.MustExec("insert into t values(1,1,1),(2,2,2),(3,3,3);")

checkFuncPushDown := func(rows [][]interface{}, keyWord string) bool {
for _, line := range rows {
// Agg/Expr push down
if line[2].(string) == "cop[tikv]" && strings.Contains(line[4].(string), keyWord) {
return true
}
// access index
if line[2].(string) == "cop[tikv]" && strings.Contains(line[3].(string), keyWord) {
return true
}
}
return false
}

// Test agg(enum) push down
tk.MustExec("insert into mysql.expr_pushdown_blacklist(name) values('enum');")
tk.MustExec("admin reload expr_pushdown_blacklist;")
rows := tk.MustQuery("desc format='brief' select /*+ HASH_AGG() */ max(a) from t;").Rows()
c.Assert(checkFuncPushDown(rows, "max"), IsFalse)
rows = tk.MustQuery("desc format='brief' select /*+ STREAM_AGG() */ max(a) from t;").Rows()
c.Assert(checkFuncPushDown(rows, "max"), IsFalse)

tk.MustExec("delete from mysql.expr_pushdown_blacklist;")
tk.MustExec("admin reload expr_pushdown_blacklist;")
rows = tk.MustQuery("desc format='brief' select /*+ HASH_AGG() */ max(a) from t;").Rows()
c.Assert(checkFuncPushDown(rows, "max"), IsTrue)
rows = tk.MustQuery("desc format='brief' select /*+ STREAM_AGG() */ max(a) from t;").Rows()
c.Assert(checkFuncPushDown(rows, "max"), IsTrue)

// Test expr(enum) push down
tk.MustExec("insert into mysql.expr_pushdown_blacklist(name) values('enum');")
tk.MustExec("admin reload expr_pushdown_blacklist;")
rows = tk.MustQuery("desc format='brief' select * from t where a + b;").Rows()
c.Assert(checkFuncPushDown(rows, "plus"), IsFalse)
rows = tk.MustQuery("desc format='brief' select * from t where a + b;").Rows()
c.Assert(checkFuncPushDown(rows, "plus"), IsFalse)

tk.MustExec("delete from mysql.expr_pushdown_blacklist;")
tk.MustExec("admin reload expr_pushdown_blacklist;")
rows = tk.MustQuery("desc format='brief' select * from t where a + b;").Rows()
c.Assert(checkFuncPushDown(rows, "plus"), IsTrue)
rows = tk.MustQuery("desc format='brief' select * from t where a + b;").Rows()
c.Assert(checkFuncPushDown(rows, "plus"), IsTrue)

// Test enum index
tk.MustExec("insert into mysql.expr_pushdown_blacklist(name) values('enum');")
tk.MustExec("admin reload expr_pushdown_blacklist;")
rows = tk.MustQuery("desc format='brief' select * from t where b = 1;").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b)"), IsFalse)
rows = tk.MustQuery("desc format='brief' select * from t where b = 'a';").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b)"), IsFalse)
rows = tk.MustQuery("desc format='brief' select * from t where b > 1;").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b)"), IsFalse)
rows = tk.MustQuery("desc format='brief' select * from t where b > 'a';").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b)"), IsFalse)

tk.MustExec("delete from mysql.expr_pushdown_blacklist;")
tk.MustExec("admin reload expr_pushdown_blacklist;")
rows = tk.MustQuery("desc format='brief' select * from t where b = 1 and a = 1;").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b, a)"), IsTrue)
rows = tk.MustQuery("desc format='brief' select * from t where b = 'a' and a = 'a';").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b, a)"), IsTrue)
rows = tk.MustQuery("desc format='brief' select * from t where b = 1 and a > 1;").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b, a)"), IsTrue)
rows = tk.MustQuery("desc format='brief' select * from t where b = 1 and a > 'a'").Rows()
c.Assert(checkFuncPushDown(rows, "index:idx(b, a)"), IsTrue)
}
172 changes: 172 additions & 0 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,101 @@ func (s *partitionTableSuite) TestDirectReadingWithAgg(c *C) {
}
}

func (s *partitionTableSuite) TestIdexMerge(c *C) {
if israce.RaceEnabled {
c.Skip("exhaustive types test, skip race test")
}

tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create database test_idx_merge")
tk.MustExec("use test_idx_merge")
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")

// list partition table
tk.MustExec(`create table tlist(a int, b int, primary key(a) clustered, index idx_b(b)) partition by list(a)(
partition p0 values in (1, 2, 3, 4),
partition p1 values in (5, 6, 7, 8),
partition p2 values in (9, 10, 11, 12));`)

// range partition table
tk.MustExec(`create table trange(a int, b int, primary key(a) clustered, index idx_b(b)) partition by range(a) (
partition p0 values less than(300),
partition p1 values less than (500),
partition p2 values less than(1100));`)

// hash partition table
tk.MustExec(`create table thash(a int, b int, primary key(a) clustered, index idx_b(b)) partition by hash(a) partitions 4;`)

// regular table
tk.MustExec("create table tregular1(a int, b int, primary key(a) clustered)")
tk.MustExec("create table tregular2(a int, b int, primary key(a) clustered)")

// generate some random data to be inserted
vals := make([]string, 0, 2000)
for i := 0; i < 2000; i++ {
vals = append(vals, fmt.Sprintf("(%v, %v)", rand.Intn(1100), rand.Intn(2000)))
}

tk.MustExec("insert ignore into trange values " + strings.Join(vals, ","))
tk.MustExec("insert ignore into thash values " + strings.Join(vals, ","))
tk.MustExec("insert ignore into tregular1 values " + strings.Join(vals, ","))

vals = make([]string, 0, 2000)
for i := 0; i < 2000; i++ {
vals = append(vals, fmt.Sprintf("(%v, %v)", rand.Intn(12)+1, rand.Intn(20)))
}

tk.MustExec("insert ignore into tlist values " + strings.Join(vals, ","))
tk.MustExec("insert ignore into tregular2 values " + strings.Join(vals, ","))

// test range partition
for i := 0; i < 100; i++ {
x1 := rand.Intn(1099)
x2 := rand.Intn(1099)

queryPartition1 := fmt.Sprintf("select /*+ use_index_merge(trange) */ * from trange where a > %v or b < %v;", x1, x2)
queryRegular1 := fmt.Sprintf("select /*+ use_index_merge(tregular1) */ * from tregular1 where a > %v or b < %v;", x1, x2)
c.Assert(tk.HasPlan(queryPartition1, "IndexMerge"), IsTrue) // check if IndexLookUp is used
tk.MustQuery(queryPartition1).Sort().Check(tk.MustQuery(queryRegular1).Sort().Rows())

queryPartition2 := fmt.Sprintf("select /*+ use_index_merge(trange) */ * from trange where a > %v or b > %v;", x1, x2)
queryRegular2 := fmt.Sprintf("select /*+ use_index_merge(tregular1) */ * from tregular1 where a > %v or b > %v;", x1, x2)
c.Assert(tk.HasPlan(queryPartition2, "IndexMerge"), IsTrue) // check if IndexLookUp is used
tk.MustQuery(queryPartition2).Sort().Check(tk.MustQuery(queryRegular2).Sort().Rows())
}

// test hash partition
for i := 0; i < 100; i++ {
x1 := rand.Intn(1099)
x2 := rand.Intn(1099)

queryPartition1 := fmt.Sprintf("select /*+ use_index_merge(thash) */ * from thash where a > %v or b < %v;", x1, x2)
queryRegular1 := fmt.Sprintf("select /*+ use_index_merge(tregualr1) */ * from tregular1 where a > %v or b < %v;", x1, x2)
c.Assert(tk.HasPlan(queryPartition1, "IndexMerge"), IsTrue) // check if IndexLookUp is used
tk.MustQuery(queryPartition1).Sort().Check(tk.MustQuery(queryRegular1).Sort().Rows())

queryPartition2 := fmt.Sprintf("select /*+ use_index_merge(thash) */ * from thash where a > %v or b > %v;", x1, x2)
queryRegular2 := fmt.Sprintf("select /*+ use_index_merge(tregular1) */ * from tregular1 where a > %v or b > %v;", x1, x2)
c.Assert(tk.HasPlan(queryPartition2, "IndexMerge"), IsTrue) // check if IndexLookUp is used
tk.MustQuery(queryPartition2).Sort().Check(tk.MustQuery(queryRegular2).Sort().Rows())
}

// test list partition
for i := 0; i < 100; i++ {
x1 := rand.Intn(12) + 1
x2 := rand.Intn(12) + 1
queryPartition1 := fmt.Sprintf("select /*+ use_index_merge(tlist) */ * from tlist where a > %v or b < %v;", x1, x2)
queryRegular1 := fmt.Sprintf("select /*+ use_index_merge(tregular2) */ * from tregular2 where a > %v or b < %v;", x1, x2)
c.Assert(tk.HasPlan(queryPartition1, "IndexMerge"), IsTrue) // check if IndexLookUp is used
tk.MustQuery(queryPartition1).Sort().Check(tk.MustQuery(queryRegular1).Sort().Rows())

queryPartition2 := fmt.Sprintf("select /*+ use_index_merge(tlist) */ * from tlist where a > %v or b > %v;", x1, x2)
queryRegular2 := fmt.Sprintf("select /*+ use_index_merge(tregular2) */ * from tregular2 where a > %v or b > %v;", x1, x2)
c.Assert(tk.HasPlan(queryPartition2, "IndexMerge"), IsTrue) // check if IndexLookUp is used
tk.MustQuery(queryPartition2).Sort().Check(tk.MustQuery(queryRegular2).Sort().Rows())
}
}

func (s *globalIndexSuite) TestGlobalIndexScan(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists p")
Expand Down Expand Up @@ -1357,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 816a573

Please sign in to comment.