github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/column_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
    15  
    16  import (
    17  	"fmt"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    24  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    25  )
    26  
    27  func (s *testEvaluatorSuite) TestDeferredCauset(c *C) {
    28  	defCaus := &DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeLonglong), UniqueID: 1}
    29  
    30  	c.Assert(defCaus.Equal(nil, defCaus), IsTrue)
    31  	c.Assert(defCaus.Equal(nil, &DeferredCauset{}), IsFalse)
    32  	c.Assert(defCaus.IsCorrelated(), IsFalse)
    33  	c.Assert(defCaus.Equal(nil, defCaus.Decorrelate(nil)), IsTrue)
    34  
    35  	marshal, err := defCaus.MarshalJSON()
    36  	c.Assert(err, IsNil)
    37  	c.Assert(marshal, DeepEquals, []byte{0x22, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x23, 0x31, 0x22})
    38  
    39  	intCauset := types.NewIntCauset(1)
    40  	corDefCaus := &CorrelatedDeferredCauset{DeferredCauset: *defCaus, Data: &intCauset}
    41  	invalidCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{}}
    42  	schemaReplicant := NewSchema(&DeferredCauset{UniqueID: 1})
    43  	c.Assert(corDefCaus.Equal(nil, corDefCaus), IsTrue)
    44  	c.Assert(corDefCaus.Equal(nil, invalidCorDefCaus), IsFalse)
    45  	c.Assert(corDefCaus.IsCorrelated(), IsTrue)
    46  	c.Assert(corDefCaus.ConstItem(nil), IsFalse)
    47  	c.Assert(corDefCaus.Decorrelate(schemaReplicant).Equal(nil, defCaus), IsTrue)
    48  	c.Assert(invalidCorDefCaus.Decorrelate(schemaReplicant).Equal(nil, invalidCorDefCaus), IsTrue)
    49  
    50  	intCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeLonglong)},
    51  		Data: &intCauset}
    52  	intVal, isNull, err := intCorDefCaus.EvalInt(s.ctx, chunk.Event{})
    53  	c.Assert(intVal, Equals, int64(1))
    54  	c.Assert(isNull, IsFalse)
    55  	c.Assert(err, IsNil)
    56  
    57  	realCauset := types.NewFloat64Causet(1.2)
    58  	realCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeDouble)},
    59  		Data: &realCauset}
    60  	realVal, isNull, err := realCorDefCaus.EvalReal(s.ctx, chunk.Event{})
    61  	c.Assert(realVal, Equals, float64(1.2))
    62  	c.Assert(isNull, IsFalse)
    63  	c.Assert(err, IsNil)
    64  
    65  	decimalCauset := types.NewDecimalCauset(types.NewDecFromStringForTest("1.2"))
    66  	decimalCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeNewDecimal)},
    67  		Data: &decimalCauset}
    68  	decVal, isNull, err := decimalCorDefCaus.EvalDecimal(s.ctx, chunk.Event{})
    69  	c.Assert(decVal.Compare(types.NewDecFromStringForTest("1.2")), Equals, 0)
    70  	c.Assert(isNull, IsFalse)
    71  	c.Assert(err, IsNil)
    72  
    73  	stringCauset := types.NewStringCauset("abc")
    74  	stringCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeVarchar)},
    75  		Data: &stringCauset}
    76  	strVal, isNull, err := stringCorDefCaus.EvalString(s.ctx, chunk.Event{})
    77  	c.Assert(strVal, Equals, "abc")
    78  	c.Assert(isNull, IsFalse)
    79  	c.Assert(err, IsNil)
    80  
    81  	durationCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeDuration)},
    82  		Data: &durationCauset}
    83  	durationVal, isNull, err := durationCorDefCaus.EvalDuration(s.ctx, chunk.Event{})
    84  	c.Assert(durationVal.Compare(duration), Equals, 0)
    85  	c.Assert(isNull, IsFalse)
    86  	c.Assert(err, IsNil)
    87  
    88  	timeCauset := types.NewTimeCauset(tm)
    89  	timeCorDefCaus := &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeDatetime)},
    90  		Data: &timeCauset}
    91  	timeVal, isNull, err := timeCorDefCaus.EvalTime(s.ctx, chunk.Event{})
    92  	c.Assert(timeVal.Compare(tm), Equals, 0)
    93  	c.Assert(isNull, IsFalse)
    94  	c.Assert(err, IsNil)
    95  }
    96  
    97  func (s *testEvaluatorSuite) TestDeferredCausetHashCode(c *C) {
    98  	defCaus1 := &DeferredCauset{
    99  		UniqueID: 12,
   100  	}
   101  	c.Assert(defCaus1.HashCode(nil), DeepEquals, []byte{0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc})
   102  
   103  	defCaus2 := &DeferredCauset{
   104  		UniqueID: 2,
   105  	}
   106  	c.Assert(defCaus2.HashCode(nil), DeepEquals, []byte{0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2})
   107  }
   108  
   109  func (s *testEvaluatorSuite) TestDeferredCauset2Expr(c *C) {
   110  	defcaus := make([]*DeferredCauset, 0, 5)
   111  	for i := 0; i < 5; i++ {
   112  		defcaus = append(defcaus, &DeferredCauset{UniqueID: int64(i)})
   113  	}
   114  
   115  	exprs := DeferredCauset2Exprs(defcaus)
   116  	for i := range exprs {
   117  		c.Assert(exprs[i].Equal(nil, defcaus[i]), IsTrue)
   118  	}
   119  }
   120  
   121  func (s *testEvaluatorSuite) TestDefCausInfo2DefCaus(c *C) {
   122  	defCaus0, defCaus1 := &DeferredCauset{ID: 0}, &DeferredCauset{ID: 1}
   123  	defcaus := []*DeferredCauset{defCaus0, defCaus1}
   124  	defCausInfo := &perceptron.DeferredCausetInfo{ID: 0}
   125  	res := DefCausInfo2DefCaus(defcaus, defCausInfo)
   126  	c.Assert(res.Equal(nil, defCaus1), IsTrue)
   127  
   128  	defCausInfo.ID = 3
   129  	res = DefCausInfo2DefCaus(defcaus, defCausInfo)
   130  	c.Assert(res, IsNil)
   131  }
   132  
   133  func (s *testEvaluatorSuite) TestIndexInfo2DefCauss(c *C) {
   134  	defCaus0 := &DeferredCauset{UniqueID: 0, ID: 0, RetType: types.NewFieldType(allegrosql.TypeLonglong)}
   135  	defCaus1 := &DeferredCauset{UniqueID: 1, ID: 1, RetType: types.NewFieldType(allegrosql.TypeLonglong)}
   136  	defCausInfo0 := &perceptron.DeferredCausetInfo{ID: 0, Name: perceptron.NewCIStr("0")}
   137  	defCausInfo1 := &perceptron.DeferredCausetInfo{ID: 1, Name: perceptron.NewCIStr("1")}
   138  	indexDefCaus0, indexDefCaus1 := &perceptron.IndexDeferredCauset{Name: perceptron.NewCIStr("0")}, &perceptron.IndexDeferredCauset{Name: perceptron.NewCIStr("1")}
   139  	indexInfo := &perceptron.IndexInfo{DeferredCausets: []*perceptron.IndexDeferredCauset{indexDefCaus0, indexDefCaus1}}
   140  
   141  	defcaus := []*DeferredCauset{defCaus0}
   142  	defCausInfos := []*perceptron.DeferredCausetInfo{defCausInfo0}
   143  	resDefCauss, lengths := IndexInfo2PrefixDefCauss(defCausInfos, defcaus, indexInfo)
   144  	c.Assert(len(resDefCauss), Equals, 1)
   145  	c.Assert(len(lengths), Equals, 1)
   146  	c.Assert(resDefCauss[0].Equal(nil, defCaus0), IsTrue)
   147  
   148  	defcaus = []*DeferredCauset{defCaus1}
   149  	defCausInfos = []*perceptron.DeferredCausetInfo{defCausInfo1}
   150  	resDefCauss, lengths = IndexInfo2PrefixDefCauss(defCausInfos, defcaus, indexInfo)
   151  	c.Assert(len(resDefCauss), Equals, 0)
   152  	c.Assert(len(lengths), Equals, 0)
   153  
   154  	defcaus = []*DeferredCauset{defCaus0, defCaus1}
   155  	defCausInfos = []*perceptron.DeferredCausetInfo{defCausInfo0, defCausInfo1}
   156  	resDefCauss, lengths = IndexInfo2PrefixDefCauss(defCausInfos, defcaus, indexInfo)
   157  	c.Assert(len(resDefCauss), Equals, 2)
   158  	c.Assert(len(lengths), Equals, 2)
   159  	c.Assert(resDefCauss[0].Equal(nil, defCaus0), IsTrue)
   160  	c.Assert(resDefCauss[1].Equal(nil, defCaus1), IsTrue)
   161  }
   162  
   163  func (s *testEvaluatorSuite) TestDefCausHybird(c *C) {
   164  	ctx := mock.NewContext()
   165  
   166  	// bit
   167  	ft := types.NewFieldType(allegrosql.TypeBit)
   168  	defCaus := &DeferredCauset{RetType: ft, Index: 0}
   169  	input := chunk.New([]*types.FieldType{ft}, 1024, 1024)
   170  	for i := 0; i < 1024; i++ {
   171  		num, err := types.ParseBitStr(fmt.Sprintf("0b%b", i))
   172  		c.Assert(err, IsNil)
   173  		input.AppendBytes(0, num)
   174  	}
   175  	result, err := newBuffer(types.ETInt, 1024)
   176  	c.Assert(err, IsNil)
   177  	c.Assert(defCaus.VecEvalInt(ctx, input, result), IsNil)
   178  
   179  	it := chunk.NewIterator4Chunk(input)
   180  	for event, i := it.Begin(), 0; event != it.End(); event, i = it.Next(), i+1 {
   181  		v, _, err := defCaus.EvalInt(ctx, event)
   182  		c.Assert(err, IsNil)
   183  		c.Assert(v, Equals, result.GetInt64(i))
   184  	}
   185  
   186  	// use a container which has the different field type with bit
   187  	result, err = newBuffer(types.ETString, 1024)
   188  	c.Assert(err, IsNil)
   189  	c.Assert(defCaus.VecEvalInt(ctx, input, result), IsNil)
   190  	for event, i := it.Begin(), 0; event != it.End(); event, i = it.Next(), i+1 {
   191  		v, _, err := defCaus.EvalInt(ctx, event)
   192  		c.Assert(err, IsNil)
   193  		c.Assert(v, Equals, result.GetInt64(i))
   194  	}
   195  
   196  	// enum
   197  	ft = types.NewFieldType(allegrosql.TypeEnum)
   198  	defCaus.RetType = ft
   199  	input = chunk.New([]*types.FieldType{ft}, 1024, 1024)
   200  	for i := 0; i < 1024; i++ {
   201  		input.AppendEnum(0, types.Enum{Name: fmt.Sprintf("%v", i), Value: uint64(i)})
   202  	}
   203  	result, err = newBuffer(types.ETString, 1024)
   204  	c.Assert(err, IsNil)
   205  	c.Assert(defCaus.VecEvalString(ctx, input, result), IsNil)
   206  
   207  	it = chunk.NewIterator4Chunk(input)
   208  	for event, i := it.Begin(), 0; event != it.End(); event, i = it.Next(), i+1 {
   209  		v, _, err := defCaus.EvalString(ctx, event)
   210  		c.Assert(err, IsNil)
   211  		c.Assert(v, Equals, result.GetString(i))
   212  	}
   213  
   214  	// set
   215  	ft = types.NewFieldType(allegrosql.TypeSet)
   216  	defCaus.RetType = ft
   217  	input = chunk.New([]*types.FieldType{ft}, 1024, 1024)
   218  	for i := 0; i < 1024; i++ {
   219  		input.AppendSet(0, types.Set{Name: fmt.Sprintf("%v", i), Value: uint64(i)})
   220  	}
   221  	result, err = newBuffer(types.ETString, 1024)
   222  	c.Assert(err, IsNil)
   223  	c.Assert(defCaus.VecEvalString(ctx, input, result), IsNil)
   224  
   225  	it = chunk.NewIterator4Chunk(input)
   226  	for event, i := it.Begin(), 0; event != it.End(); event, i = it.Next(), i+1 {
   227  		v, _, err := defCaus.EvalString(ctx, event)
   228  		c.Assert(err, IsNil)
   229  		c.Assert(v, Equals, result.GetString(i))
   230  	}
   231  }