Skip to content

Commit

Permalink
tests: add some test cases for global index (#53630)
Browse files Browse the repository at this point in the history
ref #45133
  • Loading branch information
Defined2014 authored May 29, 2024
1 parent 2b68ccf commit c79716e
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 329 deletions.
10 changes: 7 additions & 3 deletions pkg/ddl/tests/partition/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ func TestCreateTableWithKeyPartition(t *testing.T) {
tk.MustExec(`drop table if exists tm2`)
tk.MustGetErrMsg(`create table tm2 (a char(5), unique key(a(5))) partition by key() partitions 5`,
"Table partition metadata not correct, neither partition expression or list of partition columns")
tk.MustExec(`create table tm2 (a char(5) not null, unique key(a(5))) partition by key() partitions 5`)
}

func TestDropPartitionWithGlobalIndex(t *testing.T) {
Expand Down Expand Up @@ -1512,20 +1513,20 @@ func TestGlobalIndexUpdateInTruncatePartition(t *testing.T) {
originalHook := dom.DDL().GetHook()
defer dom.DDL().SetHook(originalHook)

var err error
hook := &callback.TestDDLCallback{Do: dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
assert.Equal(t, model.ActionTruncateTablePartition, job.Type)
if job.SchemaState == model.StateDeleteOnly {
tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")
err = tk1.ExecToErr("update test_global set a = 2 where a = 11")
err := tk1.ExecToErr("update test_global set a = 2 where a = 11")
assert.NotNil(t, err)
}
}
dom.DDL().SetHook(hook)

tk.MustExec("alter table test_global truncate partition p1")
tk.MustQuery("select * from test_global use index(idx_b) order by a").Check(testkit.Rows("11 11 11", "12 12 12"))
}

func TestGlobalIndexUpdateInTruncatePartition4Hash(t *testing.T) {
Expand Down Expand Up @@ -1564,7 +1565,7 @@ func TestGlobalIndexUpdateInTruncatePartition4Hash(t *testing.T) {
tk.MustExec("alter table test_global truncate partition p1")
}

func TestGlobalIndexReaderInTruncatePartition(t *testing.T) {
func TestGlobalIndexReaderAndIndexLookUpInTruncatePartition(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -1591,6 +1592,9 @@ func TestGlobalIndexReaderInTruncatePartition(t *testing.T) {
tk1.MustExec("use test")

tk1.MustQuery("select b from test_global use index(idx_b)").Sort().Check(testkit.Rows("11", "12"))
tk1.MustQuery("select * from test_global use index(idx_b)").Sort().Check(testkit.Rows("11 11 11", "12 12 12"))
tk1.MustQuery("select * from test_global use index(idx_b) order by a").Check(testkit.Rows("11 11 11", "12 12 12"))
tk1.MustQuery("select * from test_global use index(idx_b) order by b").Check(testkit.Rows("11 11 11", "12 12 12"))
}
}
dom.DDL().SetHook(hook)
Expand Down
37 changes: 0 additions & 37 deletions pkg/executor/point_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,40 +375,3 @@ func TestWithTiDBSnapshot(t *testing.T) {

tk.MustQuery("select * from xx").Check(testkit.Rows("1", "7"))
}

func TestGlobalIndexPointGet(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_enable_global_index=true")
defer func() {
tk.MustExec("set tidb_enable_global_index=default")
}()

tk.MustExec(`CREATE TABLE t ( a int, b int, c int default 0)
PARTITION BY RANGE (a) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (30),
PARTITION p3 VALUES LESS THAN (40))`)
tk.MustExec("INSERT INTO t(a, b) values(1, 1), (2, 2), (3, 3), (15, 15), (25, 25), (35, 35)")
tk.MustExec("ALTER TABLE t ADD UNIQUE INDEX idx(b)")
tk.MustExec("analyze table t")

tk.MustQuery("select * from t use index(idx) where b in (15, 25, 35)").Sort().Check(testkit.Rows("15 15 0", "25 25 0", "35 35 0"))
tk.MustQuery("explain select * from t use index(idx) where b in (15, 25, 35)").Check(
testkit.Rows("Batch_Point_Get_1 3.00 root table:t, index:idx(b) keep order:false, desc:false"))

tk.MustQuery("select * from t use index(idx) where b in (select b from t use index(idx) where b>10)").Sort().Check(testkit.Rows("15 15 0", "25 25 0", "35 35 0"))

tk.MustQuery("select * from t use index(idx) where b = 15").Check(testkit.Rows("15 15 0"))
tk.MustQuery("explain select * from t use index(idx) where b = 15").Check(
testkit.Rows("Point_Get_1 1.00 root table:t, index:idx(b) "))

tk.MustQuery("select * from t use index(idx) where b in (select b from t use index(idx) where b > 10)").Sort().Check(
testkit.Rows("15 15 0", "25 25 0", "35 35 0"))

tk.MustQuery("explain format='brief' select * from t partition(p1) use index(idx) where b = 3").Check(testkit.Rows("Point_Get 1.00 root table:t, index:idx(b) "))
tk.MustQuery("select * from t partition(p1) use index(idx) where b = 3").Check(testkit.Rows())
tk.MustQuery("select * from t partition(p1) use index(idx) where b in (15, 25, 35)").Check(testkit.Rows("15 15 0"))
}
58 changes: 0 additions & 58 deletions tests/integrationtest/r/ddl/global_index.result

This file was deleted.

50 changes: 50 additions & 0 deletions tests/integrationtest/r/globalindex/aggregate.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
set tidb_enable_global_index=true;
drop table if exists p;
create table p (id int, c int, unique index idx(id)) partition by range (c) (
partition p0 values less than (4),
partition p1 values less than (7),
partition p2 values less than (10));
insert into p values (1,3), (2,3), (3,4), (4,4), (5,6), (7,9), (8,9);
explain format='brief' select count(*), max(id), min(id) from p use index(idx);
id estRows task access object operator info
HashAgg 1.00 root funcs:count(Column#9)->Column#4, funcs:max(Column#10)->Column#5, funcs:min(Column#11)->Column#6
└─IndexReader 1.00 root partition:all index:HashAgg
└─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#9, funcs:max(globalindex__aggregate.p.id)->Column#10, funcs:min(globalindex__aggregate.p.id)->Column#11
└─IndexFullScan 10000.00 cop[tikv] table:p, index:idx(id) keep order:false, stats:pseudo
select count(*), max(id), min(id) from p use index(idx);
count(*) max(id) min(id)
7 8 1
explain format='brief' select count(*), max(id), min(id) from p partition(p0) use index(idx);
id estRows task access object operator info
HashAgg 1.00 root NULL funcs:count(Column#9)->Column#4, funcs:max(Column#10)->Column#5, funcs:min(Column#11)->Column#6
└─IndexReader 1.00 root partition:p0 index:HashAgg
└─HashAgg 1.00 cop[tikv] NULL funcs:count(1)->Column#9, funcs:max(globalindex__aggregate.p.id)->Column#10, funcs:min(globalindex__aggregate.p.id)->Column#11
└─Selection 10000.00 cop[tikv] NULL in(_tidb_pid, pid0)
└─IndexFullScan 10000.00 cop[tikv] table:p, index:idx(id) keep order:false, stats:pseudo
select count(*), max(id), min(id) from p partition(p0) use index(idx);
count(*) max(id) min(id)
2 2 1
explain format='brief' select avg(id), max(id), min(id) from p use index(idx) group by c;
id estRows task access object operator info
HashAgg 8000.00 root group by:globalindex__aggregate.p.c, funcs:avg(Column#9, Column#10)->Column#4, funcs:max(Column#11)->Column#5, funcs:min(Column#12)->Column#6
└─IndexLookUp 8000.00 root partition:all
├─IndexFullScan(Build) 10000.00 cop[tikv] table:p, index:idx(id) keep order:false, stats:pseudo
└─HashAgg(Probe) 8000.00 cop[tikv] group by:globalindex__aggregate.p.c, funcs:count(globalindex__aggregate.p.id)->Column#9, funcs:sum(globalindex__aggregate.p.id)->Column#10, funcs:max(globalindex__aggregate.p.id)->Column#11, funcs:min(globalindex__aggregate.p.id)->Column#12
└─TableRowIDScan 10000.00 cop[tikv] table:p keep order:false, stats:pseudo
select avg(id), max(id), min(id) from p use index(idx) group by c;
avg(id) max(id) min(id)
1.5000 2 1
3.5000 4 3
5.0000 5 5
7.5000 8 7
explain format='brief' select avg(id), max(id), min(id) from p partition(p0) use index(idx) group by c;
id estRows task access object operator info
HashAgg 8000.00 root NULL group by:globalindex__aggregate.p.c, funcs:avg(Column#9, Column#10)->Column#4, funcs:max(Column#11)->Column#5, funcs:min(Column#12)->Column#6
└─IndexLookUp 8000.00 root partition:p0 NULL
├─Selection(Build) 10000.00 cop[tikv] NULL in(_tidb_pid, pid0)
│ └─IndexFullScan 10000.00 cop[tikv] table:p, index:idx(id) keep order:false, stats:pseudo
└─HashAgg(Probe) 8000.00 cop[tikv] NULL group by:globalindex__aggregate.p.c, funcs:count(globalindex__aggregate.p.id)->Column#9, funcs:sum(globalindex__aggregate.p.id)->Column#10, funcs:max(globalindex__aggregate.p.id)->Column#11, funcs:min(globalindex__aggregate.p.id)->Column#12
└─TableRowIDScan 10000.00 cop[tikv] table:p keep order:false, stats:pseudo
select avg(id), max(id), min(id) from p partition(p0) use index(idx) group by c;
avg(id) max(id) min(id)
1.5000 2 1
39 changes: 39 additions & 0 deletions tests/integrationtest/r/globalindex/expression_index.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
set tidb_enable_global_index=true;
drop table if exists t;
CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`b` char(11) DEFAULT NULL,
UNIQUE KEY `idx` ((lower(`b`)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY HASH (`a`) PARTITIONS 5;
insert into t values (1, 'a'), (2, 'b'), (3, 'C'), (4, 'd'), (5, 'x');
insert into t values (3, 'c');
Error 1062 (23000): Duplicate entry 'c' for key 't.idx'
explain format='brief' select * from t use index(idx) where lower(b) = 'c';
id estRows task access object operator info
Projection 1.00 root globalindex__expression_index.t.a, globalindex__expression_index.t.b
└─Point_Get 1.00 root table:t, index:idx(lower(`b`))
select * from t use index(idx) where lower(b) = 'c';
a b
3 C
explain format='brief' select * from t use index(idx) where lower(b) > 'c' order by lower(b);
id estRows task access object operator info
Projection 3333.33 root globalindex__expression_index.t.a, globalindex__expression_index.t.b
└─Projection 3333.33 root globalindex__expression_index.t.a, globalindex__expression_index.t.b, lower(globalindex__expression_index.t.b)
└─IndexLookUp 3333.33 root partition:all
├─IndexRangeScan(Build) 3333.33 cop[tikv] table:t, index:idx(lower(`b`)) range:("c",+inf], keep order:true, stats:pseudo
└─TableRowIDScan(Probe) 3333.33 cop[tikv] table:t keep order:false, stats:pseudo
select * from t use index(idx) where lower(b) > 'c' order by lower(b);
a b
4 d
5 x
explain format='brief' select * from t partition(p0) use index(idx) where lower(b) > 'c';
id estRows task access object operator info
Projection 3333.33 root NULL globalindex__expression_index.t.a, globalindex__expression_index.t.b
└─IndexLookUp 3333.33 root partition:p0 NULL
├─Selection(Build) 3333.33 cop[tikv] NULL in(_tidb_pid, pid0)
│ └─IndexRangeScan 3333.33 cop[tikv] table:t, index:idx(lower(`b`)) range:("c",+inf], keep order:false, stats:pseudo
└─TableRowIDScan(Probe) 3333.33 cop[tikv] table:t keep order:false, stats:pseudo
select * from t partition(p0) use index(idx) where lower(b) > 'c';
a b
5 x
Loading

0 comments on commit c79716e

Please sign in to comment.