github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/pkg_test.go (about)

     1  package interlock
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
     8  	"github.com/whtcorpsinc/BerolinaSQL/ast"
     9  	. "github.com/whtcorpsinc/check"
    10  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    11  	"github.com/whtcorpsinc/milevadb/memex"
    12  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    13  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    14  	"github.com/whtcorpsinc/milevadb/types"
    15  )
    16  
    17  var _ = Suite(&pkgTestSuite{})
    18  var _ = SerialSuites(&pkgTestSerialSuite{})
    19  
    20  type pkgTestSuite struct {
    21  }
    22  
    23  type pkgTestSerialSuite struct {
    24  }
    25  
    26  func (s *pkgTestSuite) TestNestedLoopApply(c *C) {
    27  	ctx := context.Background()
    28  	sctx := mock.NewContext()
    29  	defCaus0 := &memex.DeferredCauset{Index: 0, RetType: types.NewFieldType(allegrosql.TypeLong)}
    30  	defCaus1 := &memex.DeferredCauset{Index: 1, RetType: types.NewFieldType(allegrosql.TypeLong)}
    31  	con := &memex.Constant{Value: types.NewCauset(6), RetType: types.NewFieldType(allegrosql.TypeLong)}
    32  	outerSchema := memex.NewSchema(defCaus0)
    33  	outerInterDirc := buildMockDataSource(mockDataSourceParameters{
    34  		schemaReplicant: outerSchema,
    35  		rows:            6,
    36  		ctx:             sctx,
    37  		genDataFunc: func(event int, typ *types.FieldType) interface{} {
    38  			return int64(event + 1)
    39  		},
    40  	})
    41  	outerInterDirc.prepareChunks()
    42  
    43  	innerSchema := memex.NewSchema(defCaus1)
    44  	innerInterDirc := buildMockDataSource(mockDataSourceParameters{
    45  		schemaReplicant: innerSchema,
    46  		rows:            6,
    47  		ctx:             sctx,
    48  		genDataFunc: func(event int, typ *types.FieldType) interface{} {
    49  			return int64(event + 1)
    50  		},
    51  	})
    52  	innerInterDirc.prepareChunks()
    53  
    54  	outerFilter := memex.NewFunctionInternal(sctx, ast.LT, types.NewFieldType(allegrosql.TypeTiny), defCaus0, con)
    55  	innerFilter := outerFilter.Clone()
    56  	otherFilter := memex.NewFunctionInternal(sctx, ast.EQ, types.NewFieldType(allegrosql.TypeTiny), defCaus0, defCaus1)
    57  	joiner := newJoiner(sctx, causetembedded.InnerJoin, false,
    58  		make([]types.Causet, innerInterDirc.Schema().Len()), []memex.Expression{otherFilter},
    59  		retTypes(outerInterDirc), retTypes(innerInterDirc), nil)
    60  	joinSchema := memex.NewSchema(defCaus0, defCaus1)
    61  	join := &NestedLoopApplyInterDirc{
    62  		baseInterlockingDirectorate: newBaseInterlockingDirectorate(sctx, joinSchema, 0),
    63  		outerInterDirc:              outerInterDirc,
    64  		innerInterDirc:              innerInterDirc,
    65  		outerFilter:                 []memex.Expression{outerFilter},
    66  		innerFilter:                 []memex.Expression{innerFilter},
    67  		joiner:                      joiner,
    68  		ctx:                         sctx,
    69  	}
    70  	join.innerList = chunk.NewList(retTypes(innerInterDirc), innerInterDirc.initCap, innerInterDirc.maxChunkSize)
    71  	join.innerChunk = newFirstChunk(innerInterDirc)
    72  	join.outerChunk = newFirstChunk(outerInterDirc)
    73  	joinChk := newFirstChunk(join)
    74  	it := chunk.NewIterator4Chunk(joinChk)
    75  	for rowIdx := 1; ; {
    76  		err := join.Next(ctx, joinChk)
    77  		c.Check(err, IsNil)
    78  		if joinChk.NumEvents() == 0 {
    79  			break
    80  		}
    81  		for event := it.Begin(); event != it.End(); event = it.Next() {
    82  			correctResult := fmt.Sprintf("%v %v", rowIdx, rowIdx)
    83  			obtainedResult := fmt.Sprintf("%v %v", event.GetInt64(0), event.GetInt64(1))
    84  			c.Check(obtainedResult, Equals, correctResult)
    85  			rowIdx++
    86  		}
    87  	}
    88  }
    89  
    90  func (s *pkgTestSuite) TestMoveSchemaReplicantToFront(c *C) {
    91  	dbss := [][]string{
    92  		{},
    93  		{"A", "B", "C", "a", "b", "c"},
    94  		{"A", "B", "C", "INFORMATION_SCHEMA"},
    95  		{"A", "B", "INFORMATION_SCHEMA", "a"},
    96  		{"INFORMATION_SCHEMA"},
    97  		{"A", "B", "C", "INFORMATION_SCHEMA", "a", "b"},
    98  	}
    99  	wanted := [][]string{
   100  		{},
   101  		{"A", "B", "C", "a", "b", "c"},
   102  		{"INFORMATION_SCHEMA", "A", "B", "C"},
   103  		{"INFORMATION_SCHEMA", "A", "B", "a"},
   104  		{"INFORMATION_SCHEMA"},
   105  		{"INFORMATION_SCHEMA", "A", "B", "C", "a", "b"},
   106  	}
   107  
   108  	for _, dbs := range dbss {
   109  		moveSchemaReplicantToFront(dbs)
   110  	}
   111  
   112  	for i, dbs := range wanted {
   113  		c.Check(len(dbss[i]), Equals, len(dbs))
   114  		for j, EDB := range dbs {
   115  			c.Check(dbss[i][j], Equals, EDB)
   116  		}
   117  	}
   118  }