github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/joiner_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
    15  
    16  import (
    17  	"math/rand"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/milevadb/causet/embedded"
    22  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    23  	"github.com/whtcorpsinc/milevadb/types"
    24  )
    25  
    26  var _ = Suite(&testSuiteJoiner{})
    27  
    28  type testSuiteJoiner struct{}
    29  
    30  func (s *testSuiteJoiner) SetUpSuite(c *C) {
    31  }
    32  
    33  func (s *testSuiteJoiner) TestRequiredEvents(c *C) {
    34  	joinTypes := []embedded.JoinType{embedded.InnerJoin, embedded.LeftOuterJoin, embedded.RightOuterJoin}
    35  	lTypes := [][]byte{
    36  		{allegrosql.TypeLong},
    37  		{allegrosql.TypeFloat},
    38  		{allegrosql.TypeLong, allegrosql.TypeFloat},
    39  	}
    40  	rTypes := lTypes
    41  
    42  	convertTypes := func(mysqlTypes []byte) []*types.FieldType {
    43  		fieldTypes := make([]*types.FieldType, 0, len(mysqlTypes))
    44  		for _, t := range mysqlTypes {
    45  			fieldTypes = append(fieldTypes, types.NewFieldType(t))
    46  		}
    47  		return fieldTypes
    48  	}
    49  
    50  	for _, joinType := range joinTypes {
    51  		for _, ltype := range lTypes {
    52  			for _, rtype := range rTypes {
    53  				maxChunkSize := defaultCtx().GetStochastikVars().MaxChunkSize
    54  				lfields := convertTypes(ltype)
    55  				rfields := convertTypes(rtype)
    56  				outerEvent := genTestChunk(maxChunkSize, 1, lfields).GetEvent(0)
    57  				innerChk := genTestChunk(maxChunkSize, maxChunkSize, rfields)
    58  				var defaultInner []types.Causet
    59  				for i, f := range rfields {
    60  					defaultInner = append(defaultInner, innerChk.GetEvent(0).GetCauset(i, f))
    61  				}
    62  				joiner := newJoiner(defaultCtx(), joinType, false, defaultInner, nil, lfields, rfields, nil)
    63  
    64  				fields := make([]*types.FieldType, 0, len(lfields)+len(rfields))
    65  				fields = append(fields, rfields...)
    66  				fields = append(fields, lfields...)
    67  				result := chunk.New(fields, maxChunkSize, maxChunkSize)
    68  
    69  				for i := 0; i < 10; i++ {
    70  					required := rand.Int()%maxChunkSize + 1
    71  					result.SetRequiredEvents(required, maxChunkSize)
    72  					result.Reset()
    73  					it := chunk.NewIterator4Chunk(innerChk)
    74  					it.Begin()
    75  					_, _, err := joiner.tryToMatchInners(outerEvent, it, result)
    76  					c.Assert(err, IsNil)
    77  					c.Assert(result.NumEvents(), Equals, required)
    78  				}
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func genTestChunk(maxChunkSize int, numEvents int, fields []*types.FieldType) *chunk.Chunk {
    85  	chk := chunk.New(fields, maxChunkSize, maxChunkSize)
    86  	for numEvents > 0 {
    87  		numEvents--
    88  		for defCaus, field := range fields {
    89  			switch field.Tp {
    90  			case allegrosql.TypeLong:
    91  				chk.AppendInt64(defCaus, 0)
    92  			case allegrosql.TypeFloat:
    93  				chk.AppendFloat32(defCaus, 0)
    94  			default:
    95  				panic("not support")
    96  			}
    97  		}
    98  	}
    99  	return chk
   100  }