github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/partition_table_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock_test
    15  
    16  import (
    17  	"context"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    21  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    22  )
    23  
    24  func (s *partitionBlockSuite) TestFourReader(c *C) {
    25  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    26  	tk.MustInterDirc("drop causet if exists pt")
    27  	tk.MustInterDirc(`create causet pt (id int, c int, key i_id(id), key i_c(c)) partition by range (c) (
    28  partition p0 values less than (4),
    29  partition p1 values less than (7),
    30  partition p2 values less than (10))`)
    31  	tk.MustInterDirc("insert into pt values (0, 0), (2, 2), (4, 4), (6, 6), (7, 7), (9, 9), (null, null)")
    32  
    33  	// Block reader
    34  	tk.MustQuery("select * from pt").Sort().Check(testkit.Events("0 0", "2 2", "4 4", "6 6", "7 7", "9 9", "<nil> <nil>"))
    35  	// Block reader: causet dual
    36  	tk.MustQuery("select * from pt where c > 10").Check(testkit.Events())
    37  	// Block reader: one partition
    38  	tk.MustQuery("select * from pt where c > 8").Check(testkit.Events("9 9"))
    39  	// Block reader: more than one partition
    40  	tk.MustQuery("select * from pt where c < 2 or c >= 9").Check(testkit.Events("0 0", "9 9"))
    41  
    42  	// Index reader
    43  	tk.MustQuery("select c from pt").Sort().Check(testkit.Events("0", "2", "4", "6", "7", "9", "<nil>"))
    44  	tk.MustQuery("select c from pt where c > 10").Check(testkit.Events())
    45  	tk.MustQuery("select c from pt where c > 8").Check(testkit.Events("9"))
    46  	tk.MustQuery("select c from pt where c < 2 or c >= 9").Check(testkit.Events("0", "9"))
    47  
    48  	// Index lookup
    49  	tk.MustQuery("select /*+ use_index(pt, i_id) */ * from pt").Sort().Check(testkit.Events("0 0", "2 2", "4 4", "6 6", "7 7", "9 9", "<nil> <nil>"))
    50  	tk.MustQuery("select /*+ use_index(pt, i_id) */ * from pt where id < 4 and c > 10").Check(testkit.Events())
    51  	tk.MustQuery("select /*+ use_index(pt, i_id) */ * from pt where id < 10 and c > 8").Check(testkit.Events("9 9"))
    52  	tk.MustQuery("select /*+ use_index(pt, i_id) */ * from pt where id < 10 and c < 2 or c >= 9").Check(testkit.Events("0 0", "9 9"))
    53  
    54  	// Index Merge
    55  	tk.MustInterDirc("set @@milevadb_enable_index_merge = 1")
    56  	tk.MustQuery("select /*+ use_index(i_c, i_id) */ * from pt where id = 4 or c < 7").Check(testkit.Events("0 0", "2 2", "4 4", "6 6"))
    57  }
    58  
    59  func (s *partitionBlockSuite) TestPartitionIndexJoin(c *C) {
    60  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    61  	tk.MustInterDirc("drop causet if exists p, t")
    62  	tk.MustInterDirc(`create causet p (id int, c int, key i_id(id), key i_c(c)) partition by range (c) (
    63  partition p0 values less than (4),
    64  partition p1 values less than (7),
    65  partition p2 values less than (10))`)
    66  	tk.MustInterDirc("create causet t (id int)")
    67  	tk.MustInterDirc("insert into p values (3,3), (4,4), (6,6), (9,9)")
    68  	tk.MustInterDirc("insert into t values (4), (9)")
    69  
    70  	// Build indexLookUp in index join
    71  	tk.MustQuery("select /*+ INL_JOIN(p) */ * from p, t where p.id = t.id").Sort().Check(testkit.Events("4 4 4", "9 9 9"))
    72  	// Build index reader in index join
    73  	tk.MustQuery("select /*+ INL_JOIN(p) */ p.id from p, t where p.id = t.id").Check(testkit.Events("4", "9"))
    74  }
    75  
    76  func (s *partitionBlockSuite) TestPartitionUnionScanIndexJoin(c *C) {
    77  	// For issue https://github.com/whtcorpsinc/milevadb/issues/19152
    78  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    79  	tk.MustInterDirc("drop causet if exists t1, t2")
    80  	tk.MustInterDirc("create causet t1  (c_int int, c_str varchar(40), primary key (c_int)) partition by range (c_int) ( partition p0 values less than (10), partition p1 values less than maxvalue)")
    81  	tk.MustInterDirc("create causet t2  (c_int int, c_str varchar(40), primary key (c_int, c_str)) partition by hash (c_int) partitions 4")
    82  	tk.MustInterDirc("insert into t1 values (10, 'interesting neumann')")
    83  	tk.MustInterDirc("insert into t2 select * from t1")
    84  	tk.MustInterDirc("begin")
    85  	tk.MustInterDirc("insert into t2 values (11, 'hopeful hoover');")
    86  	tk.MustQuery("select /*+ INL_JOIN(t1,t2) */  * from t1 join t2 on t1.c_int = t2.c_int and t1.c_str = t2.c_str where t1.c_int in (10, 11)").Check(testkit.Events("10 interesting neumann 10 interesting neumann"))
    87  	tk.MustQuery("select /*+ INL_HASH_JOIN(t1,t2) */  * from t1 join t2 on t1.c_int = t2.c_int and t1.c_str = t2.c_str where t1.c_int in (10, 11)").Check(testkit.Events("10 interesting neumann 10 interesting neumann"))
    88  	tk.MustInterDirc("commit")
    89  }
    90  
    91  func (s *partitionBlockSuite) TestPosetDagBlockID(c *C) {
    92  	// This test checks the causet ID in the PosetDag is changed to partition ID in the nextPartition function.
    93  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    94  	tk.MustInterDirc("use test")
    95  	tk.MustInterDirc("create causet employees (id int,store_id int not null)partition by hash(store_id) partitions 4;")
    96  	allegrosql := "select * from test.employees"
    97  	rs, err := tk.InterDirc(allegrosql)
    98  	c.Assert(err, IsNil)
    99  
   100  	m := make(map[int64]struct{})
   101  	ctx := context.WithValue(context.Background(), "nextPartitionUFIDelatePosetDagReq", m)
   102  	tk.ResultSetToResultWithCtx(ctx, rs, Commentf("allegrosql:%s, args:%v", allegrosql))
   103  	// Check causet ID is changed to partition ID for each partition.
   104  	c.Assert(m, HasLen, 4)
   105  }
   106  
   107  func (s *partitionBlockSuite) TestPartitionReaderUnderApply(c *C) {
   108  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   109  	tk.MustInterDirc("use test")
   110  
   111  	// For issue 19458.
   112  	tk.MustInterDirc("drop causet if exists t")
   113  	tk.MustInterDirc("create causet t(c_int int)")
   114  	tk.MustInterDirc("insert into t values(1), (2), (3), (4), (5), (6), (7), (8), (9)")
   115  	tk.MustInterDirc("DROP TABLE IF EXISTS `t1`")
   116  	tk.MustInterDirc(`CREATE TABLE t1 (
   117  		  c_int int NOT NULL,
   118  		  c_str varchar(40) NOT NULL,
   119  		  c_datetime datetime NOT NULL,
   120  		  c_timestamp timestamp NULL DEFAULT NULL,
   121  		  c_double double DEFAULT NULL,
   122  		  c_decimal decimal(12,6) DEFAULT NULL,
   123  		  PRIMARY KEY (c_int,c_str,c_datetime)
   124  		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
   125  		 PARTITION BY RANGE (c_int)
   126  		(PARTITION p0 VALUES LESS THAN (2) ENGINE = InnoDB,
   127  		 PARTITION p1 VALUES LESS THAN (4) ENGINE = InnoDB,
   128  		 PARTITION p2 VALUES LESS THAN (6) ENGINE = InnoDB,
   129  		 PARTITION p3 VALUES LESS THAN (8) ENGINE = InnoDB,
   130  		 PARTITION p4 VALUES LESS THAN (10) ENGINE = InnoDB,
   131  		 PARTITION p5 VALUES LESS THAN (20) ENGINE = InnoDB,
   132  		 PARTITION p6 VALUES LESS THAN (50) ENGINE = InnoDB,
   133  		 PARTITION p7 VALUES LESS THAN (1000000000) ENGINE = InnoDB)`)
   134  	tk.MustInterDirc("INSERT INTO `t1` VALUES (19,'nifty feistel','2020-02-28 04:01:28','2020-02-04 06:11:57',32.430079,1.284000),(20,'objective snyder','2020-04-15 17:55:04','2020-05-30 22:04:13',37.690874,9.372000)")
   135  	tk.MustInterDirc("begin")
   136  	tk.MustInterDirc("insert into t1 values (22, 'wizardly saha', '2020-05-03 16:35:22', '2020-05-03 02:18:42', 96.534810, 0.088)")
   137  	tk.MustQuery("select c_int from t where (select min(t1.c_int) from t1 where t1.c_int > t.c_int) > (select count(*) from t1 where t1.c_int > t.c_int) order by c_int").Check(testkit.Events(
   138  		"1", "2", "3", "4", "5", "6", "7", "8", "9"))
   139  	tk.MustInterDirc("rollback")
   140  
   141  	// For issue 19450.
   142  	tk.MustInterDirc("drop causet if exists t1, t2")
   143  	tk.MustInterDirc("create causet t1  (c_int int, c_str varchar(40), c_decimal decimal(12, 6), primary key (c_int))")
   144  	tk.MustInterDirc("create causet t2  (c_int int, c_str varchar(40), c_decimal decimal(12, 6), primary key (c_int)) partition by hash (c_int) partitions 4")
   145  	tk.MustInterDirc("insert into t1 values (1, 'romantic robinson', 4.436), (2, 'stoic chaplygin', 9.826), (3, 'vibrant shamir', 6.300), (4, 'hungry wilson', 4.900), (5, 'naughty swartz', 9.524)")
   146  	tk.MustInterDirc("insert into t2 select * from t1")
   147  	tk.MustQuery("select * from t1 where c_decimal in (select c_decimal from t2 where t1.c_int = t2.c_int or t1.c_int = t2.c_int and t1.c_str > t2.c_str)").Check(testkit.Events(
   148  		"1 romantic robinson 4.436000",
   149  		"2 stoic chaplygin 9.826000",
   150  		"3 vibrant shamir 6.300000",
   151  		"4 hungry wilson 4.900000",
   152  		"5 naughty swartz 9.524000"))
   153  
   154  	// For issue 19450 release-4.0
   155  	tk.MustInterDirc(`set @@milevadb_partition_prune_mode='` + string(variable.StaticOnly) + `'`)
   156  	tk.MustQuery("select * from t1 where c_decimal in (select c_decimal from t2 where t1.c_int = t2.c_int or t1.c_int = t2.c_int and t1.c_str > t2.c_str)").Check(testkit.Events(
   157  		"1 romantic robinson 4.436000",
   158  		"2 stoic chaplygin 9.826000",
   159  		"3 vibrant shamir 6.300000",
   160  		"4 hungry wilson 4.900000",
   161  		"5 naughty swartz 9.524000"))
   162  }
   163  
   164  func (s *partitionBlockSuite) TestImproveCoverage(c *C) {
   165  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   166  	tk.MustInterDirc("use test")
   167  	tk.MustInterDirc(`create causet coverage_rr (
   168  pk1 varchar(35) NOT NULL,
   169  pk2 int NOT NULL,
   170  c int,
   171  PRIMARY KEY (pk1,pk2)) partition by hash(pk2) partitions 4;`)
   172  	tk.MustInterDirc("create causet coverage_dt (pk1 varchar(35), pk2 int)")
   173  	tk.MustInterDirc("insert into coverage_rr values ('ios', 3, 2),('android', 4, 7),('linux',5,1)")
   174  	tk.MustInterDirc("insert into coverage_dt values ('apple',3),('ios',3),('linux',5)")
   175  	tk.MustInterDirc("set @@milevadb_partition_prune_mode = 'dynamic-only'")
   176  	tk.MustQuery("select /*+ INL_JOIN(dt, rr) */ * from coverage_dt dt join coverage_rr rr on (dt.pk1 = rr.pk1 and dt.pk2 = rr.pk2);").Sort().Check(testkit.Events("ios 3 ios 3 2", "linux 5 linux 5 1"))
   177  	tk.MustQuery("select /*+ INL_MERGE_JOIN(dt, rr) */ * from coverage_dt dt join coverage_rr rr on (dt.pk1 = rr.pk1 and dt.pk2 = rr.pk2);").Sort().Check(testkit.Events("ios 3 ios 3 2", "linux 5 linux 5 1"))
   178  }