github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_value_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 . "github.com/whtcorpsinc/check" 18 "github.com/whtcorpsinc/BerolinaSQL/ast" 19 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 20 "github.com/whtcorpsinc/milevadb/interlock/aggfuncs" 21 "github.com/whtcorpsinc/milevadb/types" 22 "github.com/whtcorpsinc/milevadb/soliton/chunk" 23 ) 24 25 func getEvaluatedMemDelta(event *chunk.Event, dataType *types.FieldType) (memDelta int64) { 26 switch dataType.Tp { 27 case allegrosql.TypeString: 28 memDelta = int64(len(event.GetString(0))) 29 case allegrosql.TypeJSON: 30 memDelta = int64(len(event.GetJSON(0).Value)) 31 } 32 return 33 } 34 35 func lastValueEvaluateEventUFIDelateMemDeltaGens(srcChk *chunk.Chunk, dataType *types.FieldType) (memDeltas []int64, err error) { 36 memDeltas = make([]int64, 0) 37 lastMemDelta := int64(0) 38 for i := 0; i < srcChk.NumEvents(); i++ { 39 event := srcChk.GetEvent(0) 40 curMemDelta := getEvaluatedMemDelta(&event, dataType) 41 memDeltas = append(memDeltas, curMemDelta-lastMemDelta) 42 lastMemDelta = curMemDelta 43 } 44 return memDeltas, nil 45 } 46 47 func nthValueEvaluateEventUFIDelateMemDeltaGens(nth int) uFIDelateMemDeltaGens { 48 return func(srcChk *chunk.Chunk, dataType *types.FieldType) (memDeltas []int64, err error) { 49 memDeltas = make([]int64, 0) 50 for i := 0; i < srcChk.NumEvents(); i++ { 51 memDeltas = append(memDeltas, int64(0)) 52 } 53 if nth < srcChk.NumEvents() { 54 event := srcChk.GetEvent(nth - 1) 55 memDeltas[nth-1] = getEvaluatedMemDelta(&event, dataType) 56 } 57 return memDeltas, nil 58 } 59 } 60 61 func (s *testSuite) TestMemValue(c *C) { 62 firstMemDeltaGens := nthValueEvaluateEventUFIDelateMemDeltaGens(1) 63 secondMemDeltaGens := nthValueEvaluateEventUFIDelateMemDeltaGens(2) 64 fifthMemDeltaGens := nthValueEvaluateEventUFIDelateMemDeltaGens(5) 65 tests := []windowMemTest{ 66 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeLonglong, 0, 2, 1, 67 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4IntSize, firstMemDeltaGens), 68 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeFloat, 0, 2, 1, 69 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4Float32Size, firstMemDeltaGens), 70 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeDouble, 0, 2, 1, 71 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4Float64Size, firstMemDeltaGens), 72 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeNewDecimal, 0, 2, 1, 73 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4DecimalSize, firstMemDeltaGens), 74 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeString, 0, 2, 1, 75 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4StringSize, firstMemDeltaGens), 76 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeDate, 0, 2, 1, 77 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4TimeSize, firstMemDeltaGens), 78 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeDuration, 0, 2, 1, 79 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4DurationSize, firstMemDeltaGens), 80 buildWindowMemTester(ast.WindowFuncFirstValue, allegrosql.TypeJSON, 0, 2, 1, 81 aggfuncs.DefPartialResult4FirstValueSize+aggfuncs.DefValue4JSONSize, firstMemDeltaGens), 82 buildWindowMemTester(ast.WindowFuncLastValue, allegrosql.TypeLonglong, 1, 2, 0, 83 aggfuncs.DefPartialResult4LastValueSize+aggfuncs.DefValue4IntSize, lastValueEvaluateEventUFIDelateMemDeltaGens), 84 buildWindowMemTester(ast.WindowFuncLastValue, allegrosql.TypeString, 1, 2, 0, 85 aggfuncs.DefPartialResult4LastValueSize+aggfuncs.DefValue4StringSize, lastValueEvaluateEventUFIDelateMemDeltaGens), 86 buildWindowMemTester(ast.WindowFuncLastValue, allegrosql.TypeJSON, 1, 2, 0, 87 aggfuncs.DefPartialResult4LastValueSize+aggfuncs.DefValue4JSONSize, lastValueEvaluateEventUFIDelateMemDeltaGens), 88 buildWindowMemTester(ast.WindowFuncNthValue, allegrosql.TypeLonglong, 2, 3, 0, 89 aggfuncs.DefPartialResult4NthValueSize+aggfuncs.DefValue4IntSize, secondMemDeltaGens), 90 buildWindowMemTester(ast.WindowFuncNthValue, allegrosql.TypeLonglong, 5, 3, 0, 91 aggfuncs.DefPartialResult4NthValueSize+aggfuncs.DefValue4IntSize, fifthMemDeltaGens), 92 buildWindowMemTester(ast.WindowFuncNthValue, allegrosql.TypeJSON, 2, 3, 0, 93 aggfuncs.DefPartialResult4NthValueSize+aggfuncs.DefValue4JSONSize, secondMemDeltaGens), 94 buildWindowMemTester(ast.WindowFuncNthValue, allegrosql.TypeString, 5, 3, 0, 95 aggfuncs.DefPartialResult4NthValueSize+aggfuncs.DefValue4StringSize, fifthMemDeltaGens), 96 } 97 for _, test := range tests { 98 s.testWindowAggMemFunc(c, test) 99 } 100 }