github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/causet/embedded/integration_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 cascades_test
    15  
    16  import (
    17  	"fmt"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    21  	"github.com/whtcorpsinc/milevadb/ekv"
    22  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    23  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    24  	"github.com/whtcorpsinc/milevadb/stochastik"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    26  )
    27  
    28  var _ = Suite(&testIntegrationSuite{})
    29  
    30  type testIntegrationSuite struct {
    31  	causetstore ekv.CausetStorage
    32  	testData    solitonutil.TestData
    33  }
    34  
    35  func newStoreWithBootstrap() (ekv.CausetStorage, error) {
    36  	causetstore, err := mockstore.NewMockStore()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	_, err = stochastik.BootstrapStochastik(causetstore)
    41  	return causetstore, err
    42  }
    43  
    44  func (s *testIntegrationSuite) SetUpSuite(c *C) {
    45  	var err error
    46  	s.causetstore, err = newStoreWithBootstrap()
    47  	c.Assert(err, IsNil)
    48  	s.testData, err = solitonutil.LoadTestSuiteData("testdata", "integration_suite")
    49  	c.Assert(err, IsNil)
    50  }
    51  
    52  func (s *testIntegrationSuite) TearDownSuite(c *C) {
    53  	c.Assert(s.testData.GenerateOutputIfNeeded(), IsNil)
    54  	s.causetstore.Close()
    55  }
    56  
    57  func (s *testIntegrationSuite) TestSimpleProjDual(c *C) {
    58  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    59  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
    60  	tk.MustQuery("explain select 1").Check(testkit.Rows(
    61  		"Projection_3 1.00 root  1->DeferredCauset#1",
    62  		"└─BlockDual_4 1.00 root  rows:1",
    63  	))
    64  	tk.MustQuery("select 1").Check(testkit.Rows(
    65  		"1",
    66  	))
    67  }
    68  
    69  func (s *testIntegrationSuite) TestPKIsHandleRangeScan(c *C) {
    70  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    71  	tk.MustInterDirc("drop causet if exists t")
    72  	tk.MustInterDirc("create causet t(a int primary key, b int)")
    73  	tk.MustInterDirc("insert into t values(1,2),(3,4),(5,6)")
    74  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
    75  
    76  	var input []string
    77  	var output []struct {
    78  		ALLEGROALLEGROSQL string
    79  		Causet            []string
    80  		Result            []string
    81  	}
    82  	s.testData.GetTestCases(c, &input, &output)
    83  	for i, allegrosql := range input {
    84  		s.testData.OnRecord(func() {
    85  			output[i].ALLEGROALLEGROSQL = allegrosql
    86  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
    87  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
    88  		})
    89  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
    90  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
    91  	}
    92  }
    93  
    94  func (s *testIntegrationSuite) TestIndexScan(c *C) {
    95  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    96  	tk.MustInterDirc("drop causet if exists t")
    97  	tk.MustInterDirc("create causet t(a int primary key, b int, c int, d int, index idx_b(b), index idx_c_b(c, b))")
    98  	tk.MustInterDirc("insert into t values(1,2,3,100),(4,5,6,200),(7,8,9,300)")
    99  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   100  	var input []string
   101  	var output []struct {
   102  		ALLEGROALLEGROSQL string
   103  		Causet            []string
   104  		Result            []string
   105  	}
   106  	s.testData.GetTestCases(c, &input, &output)
   107  	for i, allegrosql := range input {
   108  		s.testData.OnRecord(func() {
   109  			output[i].ALLEGROALLEGROSQL = allegrosql
   110  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   111  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   112  		})
   113  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   114  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   115  	}
   116  }
   117  
   118  func (s *testIntegrationSuite) TestBasicShow(c *C) {
   119  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   120  	tk.MustInterDirc("drop causet if exists t")
   121  	tk.MustInterDirc("create causet t(a int primary key, b int)")
   122  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   123  	tk.MustQuery("desc t").Check(testkit.Rows(
   124  		"a int(11) NO PRI <nil> ",
   125  		"b int(11) YES  <nil> ",
   126  	))
   127  }
   128  
   129  func (s *testIntegrationSuite) TestSort(c *C) {
   130  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   131  	tk.MustInterDirc("drop causet if exists t")
   132  	tk.MustInterDirc("create causet t(a int primary key, b int)")
   133  	tk.MustInterDirc("insert into t values (1, 11), (4, 44), (2, 22), (3, 33)")
   134  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   135  	var input []string
   136  	var output []struct {
   137  		ALLEGROALLEGROSQL string
   138  		Causet            []string
   139  		Result            []string
   140  	}
   141  	s.testData.GetTestCases(c, &input, &output)
   142  	for i, allegrosql := range input {
   143  		s.testData.OnRecord(func() {
   144  			output[i].ALLEGROALLEGROSQL = allegrosql
   145  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   146  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   147  		})
   148  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   149  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   150  	}
   151  }
   152  
   153  func (s *testIntegrationSuite) TestAggregation(c *C) {
   154  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   155  	tk.MustInterDirc("drop causet if exists t")
   156  	tk.MustInterDirc("create causet t(a int primary key, b int)")
   157  	tk.MustInterDirc("insert into t values (1, 11), (4, 44), (2, 22), (3, 33)")
   158  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   159  	tk.MustInterDirc("set stochastik milevadb_interlock_concurrency = 4")
   160  	tk.MustInterDirc("set @@stochastik.milevadb_hash_join_concurrency = 5")
   161  	tk.MustInterDirc("set @@stochastik.milevadb_allegrosql_scan_concurrency = 15;")
   162  	var input []string
   163  	var output []struct {
   164  		ALLEGROALLEGROSQL string
   165  		Causet            []string
   166  		Result            []string
   167  	}
   168  	s.testData.GetTestCases(c, &input, &output)
   169  	for i, allegrosql := range input {
   170  		s.testData.OnRecord(func() {
   171  			output[i].ALLEGROALLEGROSQL = allegrosql
   172  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   173  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   174  		})
   175  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   176  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   177  	}
   178  }
   179  
   180  func (s *testIntegrationSuite) TestPushdownDistinctEnable(c *C) {
   181  	var (
   182  		input  []string
   183  		output []struct {
   184  			ALLEGROALLEGROSQL string
   185  			Causet            []string
   186  			Result            []string
   187  		}
   188  	)
   189  	s.testData.GetTestCases(c, &input, &output)
   190  	vars := []string{
   191  		fmt.Sprintf("set @@stochastik.%s = 1", variable.MilevaDBOptDistinctAggPushDown),
   192  	}
   193  	s.doTestPushdownDistinct(c, vars, input, output)
   194  }
   195  
   196  func (s *testIntegrationSuite) TestPushdownDistinctDisable(c *C) {
   197  	var (
   198  		input  []string
   199  		output []struct {
   200  			ALLEGROALLEGROSQL string
   201  			Causet            []string
   202  			Result            []string
   203  		}
   204  	)
   205  	s.testData.GetTestCases(c, &input, &output)
   206  	vars := []string{
   207  		fmt.Sprintf("set @@stochastik.%s = 0", variable.MilevaDBOptDistinctAggPushDown),
   208  	}
   209  	s.doTestPushdownDistinct(c, vars, input, output)
   210  }
   211  
   212  func (s *testIntegrationSuite) doTestPushdownDistinct(c *C, vars, input []string, output []struct {
   213  	ALLEGROALLEGROSQL string
   214  	Causet            []string
   215  	Result            []string
   216  }) {
   217  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   218  	tk.MustInterDirc("drop causet if exists t")
   219  	tk.MustInterDirc("create causet t(a int, b int, c int, index(c))")
   220  	tk.MustInterDirc("insert into t values (1, 1, 1), (1, 1, 3), (1, 2, 3), (2, 1, 3), (1, 2, NULL);")
   221  	tk.MustInterDirc("set stochastik sql_mode=''")
   222  	tk.MustInterDirc(fmt.Sprintf("set stochastik %s=1", variable.MilevaDBHashAggPartialConcurrency))
   223  	tk.MustInterDirc(fmt.Sprintf("set stochastik %s=1", variable.MilevaDBHashAggFinalConcurrency))
   224  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   225  
   226  	for _, v := range vars {
   227  		tk.MustInterDirc(v)
   228  	}
   229  
   230  	for i, ts := range input {
   231  		s.testData.OnRecord(func() {
   232  			output[i].ALLEGROALLEGROSQL = ts
   233  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + ts).Rows())
   234  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(ts).Sort().Rows())
   235  		})
   236  		tk.MustQuery("explain " + ts).Check(testkit.Rows(output[i].Causet...))
   237  		tk.MustQuery(ts).Sort().Check(testkit.Rows(output[i].Result...))
   238  	}
   239  }
   240  
   241  func (s *testIntegrationSuite) TestSimpleCausets(c *C) {
   242  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   243  	tk.MustInterDirc("drop causet if exists t")
   244  	tk.MustInterDirc("create causet t(a int primary key, b int)")
   245  	tk.MustInterDirc("insert into t values (1, 11), (4, 44), (2, 22), (3, 33)")
   246  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   247  	var input []string
   248  	var output []struct {
   249  		ALLEGROALLEGROSQL string
   250  		Causet            []string
   251  		Result            []string
   252  	}
   253  	s.testData.GetTestCases(c, &input, &output)
   254  	for i, allegrosql := range input {
   255  		s.testData.OnRecord(func() {
   256  			output[i].ALLEGROALLEGROSQL = allegrosql
   257  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   258  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   259  		})
   260  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   261  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   262  	}
   263  }
   264  
   265  func (s *testIntegrationSuite) TestJoin(c *C) {
   266  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   267  	tk.MustInterDirc("set @@stochastik.milevadb_interlock_concurrency = 4;")
   268  	tk.MustInterDirc("set @@stochastik.milevadb_hash_join_concurrency = 5;")
   269  	tk.MustInterDirc("set @@stochastik.milevadb_allegrosql_scan_concurrency = 15;")
   270  	tk.MustInterDirc("drop causet if exists t1")
   271  	tk.MustInterDirc("drop causet if exists t2")
   272  	tk.MustInterDirc("create causet t1(a int primary key, b int)")
   273  	tk.MustInterDirc("create causet t2(a int primary key, b int)")
   274  	tk.MustInterDirc("insert into t1 values (1, 11), (4, 44), (2, 22), (3, 33)")
   275  	tk.MustInterDirc("insert into t2 values (1, 111), (2, 222), (3, 333), (5, 555)")
   276  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   277  	var input []string
   278  	var output []struct {
   279  		ALLEGROALLEGROSQL string
   280  		Causet            []string
   281  		Result            []string
   282  	}
   283  	s.testData.GetTestCases(c, &input, &output)
   284  	for i, allegrosql := range input {
   285  		s.testData.OnRecord(func() {
   286  			output[i].ALLEGROALLEGROSQL = allegrosql
   287  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   288  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   289  		})
   290  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   291  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   292  	}
   293  }
   294  
   295  func (s *testIntegrationSuite) TestApply(c *C) {
   296  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   297  	tk.MustInterDirc("drop causet if exists t1, t2")
   298  	tk.MustInterDirc("create causet t1(a int primary key, b int)")
   299  	tk.MustInterDirc("create causet t2(a int primary key, b int)")
   300  	tk.MustInterDirc("insert into t1 values (1, 11), (4, 44), (2, 22), (3, 33)")
   301  	tk.MustInterDirc("insert into t2 values (1, 11), (2, 22), (3, 33)")
   302  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   303  	var input []string
   304  	var output []struct {
   305  		ALLEGROALLEGROSQL string
   306  		Causet            []string
   307  		Result            []string
   308  	}
   309  	s.testData.GetTestCases(c, &input, &output)
   310  	for i, allegrosql := range input {
   311  		s.testData.OnRecord(func() {
   312  			output[i].ALLEGROALLEGROSQL = allegrosql
   313  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   314  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   315  		})
   316  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   317  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   318  	}
   319  }
   320  
   321  func (s *testIntegrationSuite) TestMemBlockScan(c *C) {
   322  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   323  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1")
   324  	var input []string
   325  	var output []struct {
   326  		ALLEGROALLEGROSQL string
   327  		Causet            []string
   328  		Result            []string
   329  	}
   330  	s.testData.GetTestCases(c, &input, &output)
   331  	for i, allegrosql := range input {
   332  		s.testData.OnRecord(func() {
   333  			output[i].ALLEGROALLEGROSQL = allegrosql
   334  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   335  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   336  		})
   337  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   338  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   339  	}
   340  }
   341  
   342  func (s *testIntegrationSuite) TestTopN(c *C) {
   343  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   344  	tk.MustInterDirc("drop causet if exists t;")
   345  	tk.MustInterDirc("create causet t(a int primary key, b int);")
   346  	tk.MustInterDirc("insert into t values (1, 11), (4, 44), (2, 22), (3, 33);")
   347  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1;")
   348  	var input []string
   349  	var output []struct {
   350  		ALLEGROALLEGROSQL string
   351  		Causet            []string
   352  		Result            []string
   353  	}
   354  	s.testData.GetTestCases(c, &input, &output)
   355  	for i, allegrosql := range input {
   356  		s.testData.OnRecord(func() {
   357  			output[i].ALLEGROALLEGROSQL = allegrosql
   358  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   359  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   360  		})
   361  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   362  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   363  	}
   364  }
   365  
   366  func (s *testIntegrationSuite) TestCascadeCausetAppendHashedPartBlock(c *C) {
   367  	tk := testkit.NewTestKit(c, s.causetstore)
   368  	tk.MustInterDirc("use test")
   369  	tk.MustInterDirc("drop causet if exists pt1")
   370  	tk.MustInterDirc("create causet pt1(a bigint, b bigint) partition by hash(a) partitions 4")
   371  	tk.MustInterDirc(`insert into pt1 values(1,10)`)
   372  	tk.MustInterDirc(`insert into pt1 values(2,20)`)
   373  	tk.MustInterDirc(`insert into pt1 values(3,30)`)
   374  	tk.MustInterDirc(`insert into pt1 values(4,40)`)
   375  	tk.MustInterDirc(`insert into pt1 values(5,50)`)
   376  
   377  	tk.MustInterDirc("set @@milevadb_enable_cascades_causet = 1")
   378  	var input []string
   379  	var output []struct {
   380  		ALLEGROALLEGROSQL string
   381  		Causet            []string
   382  		Result            []string
   383  	}
   384  	s.testData.GetTestCases(c, &input, &output)
   385  	for i, allegrosql := range input {
   386  		s.testData.OnRecord(func() {
   387  			output[i].ALLEGROALLEGROSQL = allegrosql
   388  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   389  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   390  		})
   391  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   392  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   393  	}
   394  }
   395  
   396  func (s *testIntegrationSuite) TestInlineProjection(c *C) {
   397  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   398  	tk.MustInterDirc("drop causet if exists t1, t2;")
   399  	tk.MustInterDirc("create causet t1(a bigint, b bigint, index idx_a(a), index idx_b(b));")
   400  	tk.MustInterDirc("create causet t2(a bigint, b bigint, index idx_a(a), index idx_b(b));")
   401  	tk.MustInterDirc("insert into t1 values (1, 1), (2, 2);")
   402  	tk.MustInterDirc("insert into t2 values (1, 1), (3, 3);")
   403  	tk.MustInterDirc("set stochastik milevadb_enable_cascades_causet = 1;")
   404  	var input []string
   405  	var output []struct {
   406  		ALLEGROALLEGROSQL string
   407  		Causet            []string
   408  		Result            []string
   409  	}
   410  	s.testData.GetTestCases(c, &input, &output)
   411  	for i, allegrosql := range input {
   412  		s.testData.OnRecord(func() {
   413  			output[i].ALLEGROALLEGROSQL = allegrosql
   414  			output[i].Causet = s.testData.ConvertRowsToStrings(tk.MustQuery("explain " + allegrosql).Rows())
   415  			output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(allegrosql).Rows())
   416  		})
   417  		tk.MustQuery("explain " + allegrosql).Check(testkit.Rows(output[i].Causet...))
   418  		tk.MustQuery(allegrosql).Check(testkit.Rows(output[i].Result...))
   419  	}
   420  }