github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_first_row_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 aggfuncs_test
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  
    23  	"github.com/whtcorpsinc/milevadb/interlock/aggfuncs"
    24  	"github.com/whtcorpsinc/milevadb/types"
    25  	"github.com/whtcorpsinc/milevadb/types/json"
    26  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    27  )
    28  
    29  func (s *testSuite) TestMergePartialResult4FirstEvent(c *C) {
    30  	elems := []string{"a", "b", "c", "d", "e"}
    31  	enumA, _ := types.ParseEnumName(elems, "a", allegrosql.DefaultDefCauslationName)
    32  	enumC, _ := types.ParseEnumName(elems, "c", allegrosql.DefaultDefCauslationName)
    33  
    34  	setA, _ := types.ParseSetName(elems, "a", allegrosql.DefaultDefCauslationName)
    35  	setAB, _ := types.ParseSetName(elems, "a,b", allegrosql.DefaultDefCauslationName)
    36  
    37  	tests := []aggTest{
    38  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeLonglong, 5, 0, 2, 0),
    39  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeFloat, 5, 0.0, 2.0, 0.0),
    40  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeDouble, 5, 0.0, 2.0, 0.0),
    41  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeNewDecimal, 5, types.NewDecFromInt(0), types.NewDecFromInt(2), types.NewDecFromInt(0)),
    42  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeString, 5, "0", "2", "0"),
    43  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeDate, 5, types.TimeFromDays(365), types.TimeFromDays(367), types.TimeFromDays(365)),
    44  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeDuration, 5, types.Duration{Duration: time.Duration(0)}, types.Duration{Duration: time.Duration(2)}, types.Duration{Duration: time.Duration(0)}),
    45  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeJSON, 5, json.CreateBinary(int64(0)), json.CreateBinary(int64(2)), json.CreateBinary(int64(0))),
    46  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeEnum, 5, enumA, enumC, enumA),
    47  		builPosetDaggTester(ast.AggFuncFirstEvent, allegrosql.TypeSet, 5, setA, setAB, setA),
    48  	}
    49  	for _, test := range tests {
    50  		s.testMergePartialResult(c, test)
    51  	}
    52  }
    53  
    54  func (s *testSuite) TestMemFirstEvent(c *C) {
    55  	tests := []aggMemTest{
    56  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeLonglong, 5,
    57  			aggfuncs.DefPartialResult4FirstEventIntSize, defaultUFIDelateMemDeltaGens, false),
    58  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeFloat, 5,
    59  			aggfuncs.DefPartialResult4FirstEventFloat32Size, defaultUFIDelateMemDeltaGens, false),
    60  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeDouble, 5,
    61  			aggfuncs.DefPartialResult4FirstEventFloat64Size, defaultUFIDelateMemDeltaGens, false),
    62  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeNewDecimal, 5,
    63  			aggfuncs.DefPartialResult4FirstEventDecimalSize, defaultUFIDelateMemDeltaGens, false),
    64  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeString, 5,
    65  			aggfuncs.DefPartialResult4FirstEventStringSize, firstEventUFIDelateMemDeltaGens, false),
    66  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeDate, 5,
    67  			aggfuncs.DefPartialResult4FirstEventTimeSize, defaultUFIDelateMemDeltaGens, false),
    68  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeDuration, 5,
    69  			aggfuncs.DefPartialResult4FirstEventDurationSize, defaultUFIDelateMemDeltaGens, false),
    70  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeJSON, 5,
    71  			aggfuncs.DefPartialResult4FirstEventJSONSize, firstEventUFIDelateMemDeltaGens, false),
    72  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeEnum, 5,
    73  			aggfuncs.DefPartialResult4FirstEventEnumSize, firstEventUFIDelateMemDeltaGens, false),
    74  		builPosetDaggMemTester(ast.AggFuncFirstEvent, allegrosql.TypeSet, 5,
    75  			aggfuncs.DefPartialResult4FirstEventSetSize, firstEventUFIDelateMemDeltaGens, false),
    76  	}
    77  	for _, test := range tests {
    78  		s.testAggMemFunc(c, test)
    79  	}
    80  }
    81  
    82  func firstEventUFIDelateMemDeltaGens(srcChk *chunk.Chunk, dataType *types.FieldType) (memDeltas []int64, err error) {
    83  	for i := 0; i < srcChk.NumEvents(); i++ {
    84  		event := srcChk.GetEvent(i)
    85  		if i > 0 {
    86  			memDeltas = append(memDeltas, int64(0))
    87  			continue
    88  		}
    89  		switch dataType.Tp {
    90  		case allegrosql.TypeString:
    91  			val := event.GetString(0)
    92  			memDeltas = append(memDeltas, int64(len(val)))
    93  		case allegrosql.TypeJSON:
    94  			jsonVal := event.GetJSON(0)
    95  			memDeltas = append(memDeltas, int64(len(string(jsonVal.Value))))
    96  		case allegrosql.TypeEnum:
    97  			enum := event.GetEnum(0)
    98  			memDeltas = append(memDeltas, int64(len(enum.Name)))
    99  		case allegrosql.TypeSet:
   100  			typeSet := event.GetSet(0)
   101  			memDeltas = append(memDeltas, int64(len(typeSet.Name)))
   102  		}
   103  	}
   104  	return memDeltas, nil
   105  }