github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/causet/embedded/indexmerge_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 embedded
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL"
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/milevadb/causet/soliton"
    23  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    24  	"github.com/whtcorpsinc/milevadb/soliton/hint"
    25  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    26  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    27  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    28  )
    29  
    30  var _ = Suite(&testIndexMergeSuite{})
    31  
    32  type testIndexMergeSuite struct {
    33  	*BerolinaSQL.BerolinaSQL
    34  
    35  	is  schemareplicant.SchemaReplicant
    36  	ctx stochastikctx.Context
    37  
    38  	testdata solitonutil.TestData
    39  }
    40  
    41  func (s *testIndexMergeSuite) SetUpSuite(c *C) {
    42  	s.is = schemareplicant.MockSchemaReplicant([]*perceptron.BlockInfo{MockSignedBlock(), MockView()})
    43  	s.ctx = MockContext()
    44  	s.BerolinaSQL = BerolinaSQL.New()
    45  	var err error
    46  	s.testdata, err = solitonutil.LoadTestSuiteData("testdata", "index_merge_suite")
    47  	c.Assert(err, IsNil)
    48  }
    49  
    50  func (s *testIndexMergeSuite) TearDownSuite(c *C) {
    51  	c.Assert(s.testdata.GenerateOutputIfNeeded(), IsNil)
    52  }
    53  
    54  func getIndexMergePathDigest(paths []*soliton.AccessPath, startIndex int) string {
    55  	if len(paths) == startIndex {
    56  		return "[]"
    57  	}
    58  	idxMergeDisgest := "["
    59  	for i := startIndex; i < len(paths); i++ {
    60  		if i != startIndex {
    61  			idxMergeDisgest += ","
    62  		}
    63  		path := paths[i]
    64  		idxMergeDisgest += "{Idxs:["
    65  		for j := 0; j < len(path.PartialIndexPaths); j++ {
    66  			if j > 0 {
    67  				idxMergeDisgest += ","
    68  			}
    69  			idxMergeDisgest += path.PartialIndexPaths[j].Index.Name.L
    70  		}
    71  		idxMergeDisgest += "],TbFilters:["
    72  		for j := 0; j < len(path.BlockFilters); j++ {
    73  			if j > 0 {
    74  				idxMergeDisgest += ","
    75  			}
    76  			idxMergeDisgest += path.BlockFilters[j].String()
    77  		}
    78  		idxMergeDisgest += "]}"
    79  	}
    80  	idxMergeDisgest += "]"
    81  	return idxMergeDisgest
    82  }
    83  
    84  func (s *testIndexMergeSuite) TestIndexMergePathGeneration(c *C) {
    85  	defer testleak.AfterTest(c)()
    86  	var input, output []string
    87  	s.testdata.GetTestCases(c, &input, &output)
    88  	ctx := context.TODO()
    89  	for i, tc := range input {
    90  		comment := Commentf("case:%v allegrosql:%s", i, tc)
    91  		stmt, err := s.ParseOneStmt(tc, "", "")
    92  		c.Assert(err, IsNil, comment)
    93  		Preprocess(s.ctx, stmt, s.is)
    94  		builder := NewCausetBuilder(MockContext(), s.is, &hint.BlockHintProcessor{})
    95  		p, err := builder.Build(ctx, stmt)
    96  		if err != nil {
    97  			s.testdata.OnRecord(func() {
    98  				output[i] = err.Error()
    99  			})
   100  			c.Assert(err.Error(), Equals, output[i], comment)
   101  			continue
   102  		}
   103  		c.Assert(err, IsNil)
   104  		p, err = logicalOptimize(ctx, builder.optFlag, p.(LogicalCauset))
   105  		c.Assert(err, IsNil)
   106  		lp := p.(LogicalCauset)
   107  		c.Assert(err, IsNil)
   108  		var ds *DataSource
   109  		for ds == nil {
   110  			switch v := lp.(type) {
   111  			case *DataSource:
   112  				ds = v
   113  			default:
   114  				lp = lp.Children()[0]
   115  			}
   116  		}
   117  		ds.ctx.GetStochastikVars().SetEnableIndexMerge(true)
   118  		idxMergeStartIndex := len(ds.possibleAccessPaths)
   119  		_, err = lp.recursiveDeriveStats(nil)
   120  		c.Assert(err, IsNil)
   121  		result := getIndexMergePathDigest(ds.possibleAccessPaths, idxMergeStartIndex)
   122  		s.testdata.OnRecord(func() {
   123  			output[i] = result
   124  		})
   125  		c.Assert(result, Equals, output[i], comment)
   126  	}
   127  }