github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_op_vec_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  	"math"
    18  	"testing"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    22  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    23  	"github.com/whtcorpsinc/milevadb/types"
    24  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    25  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    26  )
    27  
    28  var vecBuiltinOpCases = map[string][]vecExprBenchCase{
    29  	ast.IsTruthWithoutNull: {
    30  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETReal}},
    31  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal}},
    32  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}},
    33  	},
    34  	ast.IsFalsity: {
    35  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETReal}},
    36  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal}},
    37  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}},
    38  	},
    39  	ast.LogicOr: {
    40  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOFIDelataGeners()},
    41  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal, types.ETReal}},
    42  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETDuration}},
    43  	},
    44  	ast.LogicXor: {
    45  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOFIDelataGeners()},
    46  	},
    47  	ast.Xor: {
    48  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOFIDelataGeners()},
    49  	},
    50  	ast.LogicAnd: {
    51  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOFIDelataGeners()},
    52  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal, types.ETReal}},
    53  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETDuration}},
    54  	},
    55  	ast.Or: {
    56  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOFIDelataGeners()},
    57  	},
    58  	ast.BitNeg: {
    59  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}},
    60  	},
    61  	ast.UnaryNot: {
    62  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETReal}},
    63  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal}},
    64  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}},
    65  	},
    66  	ast.And: {
    67  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}, geners: makeBinaryLogicOFIDelataGeners()},
    68  	},
    69  	ast.RightShift: {
    70  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}},
    71  	},
    72  	ast.LeftShift: {
    73  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt, types.ETInt}},
    74  	},
    75  	ast.UnaryMinus: {
    76  		{retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETReal}},
    77  		{retEvalType: types.ETDecimal, childrenTypes: []types.EvalType{types.ETDecimal}},
    78  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}},
    79  		{
    80  			retEvalType:        types.ETInt,
    81  			childrenTypes:      []types.EvalType{types.ETInt},
    82  			childrenFieldTypes: []*types.FieldType{{Tp: allegrosql.TypeLonglong, Flag: allegrosql.UnsignedFlag}},
    83  			geners:             []dataGenerator{newRangeInt64Gener(0, math.MaxInt64)},
    84  		},
    85  	},
    86  	ast.IsNull: {
    87  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETReal}},
    88  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETInt}},
    89  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDecimal}},
    90  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDuration}},
    91  		{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDatetime}},
    92  	},
    93  }
    94  
    95  // givenValsGener returns the items sequentially from the slice given at
    96  // the construction time. If this slice is exhausted, it falls back to
    97  // the fallback generator.
    98  type givenValsGener struct {
    99  	given    []interface{}
   100  	idx      int
   101  	fallback dataGenerator
   102  }
   103  
   104  func (g *givenValsGener) gen() interface{} {
   105  	if g.idx >= len(g.given) {
   106  		return g.fallback.gen()
   107  	}
   108  	v := g.given[g.idx]
   109  	g.idx++
   110  	return v
   111  }
   112  
   113  func makeGivenValsOrDefaultGener(vals []interface{}, eType types.EvalType) *givenValsGener {
   114  	g := &givenValsGener{}
   115  	g.given = vals
   116  	g.fallback = newDefaultGener(0.2, eType)
   117  	return g
   118  }
   119  
   120  func makeBinaryLogicOFIDelataGeners() []dataGenerator {
   121  	// TODO: rename this to makeBinaryOFIDelataGenerator, since the BIT ops are also using it?
   122  	pairs := [][]interface{}{
   123  		{nil, nil},
   124  		{0, nil},
   125  		{nil, 0},
   126  		{1, nil},
   127  		{nil, 1},
   128  		{0, 0},
   129  		{0, 1},
   130  		{1, 0},
   131  		{1, 1},
   132  		{-1, 1},
   133  	}
   134  
   135  	maybeToInt64 := func(v interface{}) interface{} {
   136  		if v == nil {
   137  			return nil
   138  		}
   139  		return int64(v.(int))
   140  	}
   141  
   142  	n := len(pairs)
   143  	arg0s := make([]interface{}, n)
   144  	arg1s := make([]interface{}, n)
   145  	for i, p := range pairs {
   146  		arg0s[i] = maybeToInt64(p[0])
   147  		arg1s[i] = maybeToInt64(p[1])
   148  	}
   149  	return []dataGenerator{
   150  		makeGivenValsOrDefaultGener(arg0s, types.ETInt),
   151  		makeGivenValsOrDefaultGener(arg1s, types.ETInt)}
   152  }
   153  
   154  func (s *testEvaluatorSuite) TestVectorizedBuiltinOpFunc(c *C) {
   155  	testVectorizedBuiltinFunc(c, vecBuiltinOpCases)
   156  }
   157  
   158  func BenchmarkVectorizedBuiltinOpFunc(b *testing.B) {
   159  	benchmarkVectorizedBuiltinFunc(b, vecBuiltinOpCases)
   160  }
   161  
   162  func (s *testEvaluatorSuite) TestBuiltinUnaryMinusIntSig(c *C) {
   163  	ctx := mock.NewContext()
   164  	ft := eType2FieldType(types.ETInt)
   165  	defCaus0 := &DeferredCauset{RetType: ft, Index: 0}
   166  	f, err := funcs[ast.UnaryMinus].getFunction(ctx, []Expression{defCaus0})
   167  	c.Assert(err, IsNil)
   168  	input := chunk.NewChunkWithCapacity([]*types.FieldType{ft}, 1024)
   169  	result := chunk.NewDeferredCauset(ft, 1024)
   170  
   171  	c.Assert(allegrosql.HasUnsignedFlag(defCaus0.GetType().Flag), IsFalse)
   172  	input.AppendInt64(0, 233333)
   173  	c.Assert(f.vecEvalInt(input, result), IsNil)
   174  	c.Assert(result.GetInt64(0), Equals, int64(-233333))
   175  	input.Reset()
   176  	input.AppendInt64(0, math.MinInt64)
   177  	c.Assert(f.vecEvalInt(input, result), NotNil)
   178  	input.DeferredCauset(0).SetNull(0, true)
   179  	c.Assert(f.vecEvalInt(input, result), IsNil)
   180  	c.Assert(result.IsNull(0), IsTrue)
   181  
   182  	defCaus0.GetType().Flag |= allegrosql.UnsignedFlag
   183  	c.Assert(allegrosql.HasUnsignedFlag(defCaus0.GetType().Flag), IsTrue)
   184  	input.Reset()
   185  	input.AppendUint64(0, 233333)
   186  	c.Assert(f.vecEvalInt(input, result), IsNil)
   187  	c.Assert(result.GetInt64(0), Equals, int64(-233333))
   188  	input.Reset()
   189  	input.AppendUint64(0, -(math.MinInt64)+1)
   190  	c.Assert(f.vecEvalInt(input, result), NotNil)
   191  	input.DeferredCauset(0).SetNull(0, true)
   192  	c.Assert(f.vecEvalInt(input, result), IsNil)
   193  	c.Assert(result.IsNull(0), IsTrue)
   194  }