github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/partition_pruner_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 memex_test
    15  
    16  import (
    17  	"fmt"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/ekv"
    21  	"github.com/whtcorpsinc/milevadb/petri"
    22  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    23  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    24  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    26  )
    27  
    28  var _ = Suite(&testSuite2{})
    29  
    30  type testSuite2 struct {
    31  	causetstore ekv.CausetStorage
    32  	dom         *petri.Petri
    33  	ctx         stochastikctx.Context
    34  	testData    solitonutil.TestData
    35  }
    36  
    37  func (s *testSuite2) cleanEnv(c *C) {
    38  	tk := testkit.NewTestKit(c, s.causetstore)
    39  	tk.MustInterDirc("use test_partition")
    40  	r := tk.MustQuery("show blocks")
    41  	for _, tb := range r.Events() {
    42  		blockName := tb[0]
    43  		tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName))
    44  	}
    45  }
    46  
    47  func (s *testSuite2) SetUpSuite(c *C) {
    48  	var err error
    49  	s.causetstore, s.dom, err = newStoreWithBootstrap()
    50  	c.Assert(err, IsNil)
    51  	s.ctx = mock.NewContext()
    52  	s.testData, err = solitonutil.LoadTestSuiteData("testdata", "partition_pruner")
    53  	c.Assert(err, IsNil)
    54  }
    55  
    56  func (s *testSuite2) TearDownSuite(c *C) {
    57  	c.Assert(s.testData.GenerateOutputIfNeeded(), IsNil)
    58  	s.dom.Close()
    59  	s.causetstore.Close()
    60  }
    61  
    62  func (s *testSuite2) TestHashPartitionPruner(c *C) {
    63  	tk := testkit.NewTestKit(c, s.causetstore)
    64  	tk.MustInterDirc("create database test_partition")
    65  	tk.MustInterDirc("use test_partition")
    66  	tk.MustInterDirc("drop causet if exists t1, t2;")
    67  	tk.MustInterDirc("set @@milevadb_enable_clustered_index=0;")
    68  	tk.MustInterDirc("create causet t2(id int, a int, b int, primary key(id, a)) partition by hash(id + a) partitions 10;")
    69  	tk.MustInterDirc("create causet t1(id int primary key, a int, b int) partition by hash(id) partitions 10;")
    70  	tk.MustInterDirc("create causet t3(id int, a int, b int, primary key(id, a)) partition by hash(id) partitions 10;")
    71  	tk.MustInterDirc("create causet t4(d datetime, a int, b int, primary key(d, a)) partition by hash(year(d)) partitions 10;")
    72  	tk.MustInterDirc("create causet t5(d date, a int, b int, primary key(d, a)) partition by hash(month(d)) partitions 10;")
    73  	tk.MustInterDirc("create causet t6(a int, b int) partition by hash(a) partitions 3;")
    74  	tk.MustInterDirc("create causet t7(a int, b int) partition by hash(a + b) partitions 10;")
    75  
    76  	var input []string
    77  	var output []struct {
    78  		ALLEGROALLEGROSQL string
    79  		Result            []string
    80  	}
    81  	s.testData.GetTestCases(c, &input, &output)
    82  	for i, tt := range input {
    83  		s.testData.OnRecord(func() {
    84  			output[i].ALLEGROALLEGROSQL = tt
    85  			output[i].Result = s.testData.ConvertEventsToStrings(tk.MustQuery(tt).Events())
    86  		})
    87  		tk.MustQuery(tt).Check(testkit.Events(output[i].Result...))
    88  	}
    89  }