github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/aggfuncs.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 15 16 import ( 17 "unsafe" 18 19 "github.com/whtcorpsinc/milevadb/memex" 20 "github.com/whtcorpsinc/milevadb/stochastikctx" 21 "github.com/whtcorpsinc/milevadb/soliton/chunk" 22 ) 23 24 // All the AggFunc implementations are listed here for navigation. 25 var ( 26 // All the AggFunc implementations for "COUNT" are listed here. 27 _ AggFunc = (*countPartial)(nil) 28 _ AggFunc = (*countOriginal4Int)(nil) 29 _ AggFunc = (*countOriginal4Real)(nil) 30 _ AggFunc = (*countOriginal4Decimal)(nil) 31 _ AggFunc = (*countOriginal4Time)(nil) 32 _ AggFunc = (*countOriginal4Duration)(nil) 33 _ AggFunc = (*countOriginal4JSON)(nil) 34 _ AggFunc = (*countOriginal4String)(nil) 35 _ AggFunc = (*countOriginalWithDistinct4Int)(nil) 36 _ AggFunc = (*countOriginalWithDistinct4Real)(nil) 37 _ AggFunc = (*countOriginalWithDistinct4Decimal)(nil) 38 _ AggFunc = (*countOriginalWithDistinct4Duration)(nil) 39 _ AggFunc = (*countOriginalWithDistinct4String)(nil) 40 _ AggFunc = (*countOriginalWithDistinct)(nil) 41 42 // All the AggFunc implementations for "APPROX_COUNT_DISTINCT" are listed here. 43 _ AggFunc = (*approxCountDistinctOriginal)(nil) 44 _ AggFunc = (*approxCountDistinctPartial1)(nil) 45 _ AggFunc = (*approxCountDistinctPartial2)(nil) 46 _ AggFunc = (*approxCountDistinctFinal)(nil) 47 48 // All the AggFunc implementations for "FIRSTROW" are listed here. 49 _ AggFunc = (*firstEvent4Decimal)(nil) 50 _ AggFunc = (*firstEvent4Int)(nil) 51 _ AggFunc = (*firstEvent4Time)(nil) 52 _ AggFunc = (*firstEvent4String)(nil) 53 _ AggFunc = (*firstEvent4Duration)(nil) 54 _ AggFunc = (*firstEvent4Float32)(nil) 55 _ AggFunc = (*firstEvent4Float64)(nil) 56 _ AggFunc = (*firstEvent4JSON)(nil) 57 _ AggFunc = (*firstEvent4Enum)(nil) 58 _ AggFunc = (*firstEvent4Set)(nil) 59 60 // All the AggFunc implementations for "MAX"/"MIN" are listed here. 61 _ AggFunc = (*maxMin4Int)(nil) 62 _ AggFunc = (*maxMin4Uint)(nil) 63 _ AggFunc = (*maxMin4Float32)(nil) 64 _ AggFunc = (*maxMin4Float64)(nil) 65 _ AggFunc = (*maxMin4Decimal)(nil) 66 _ AggFunc = (*maxMin4String)(nil) 67 _ AggFunc = (*maxMin4Duration)(nil) 68 _ AggFunc = (*maxMin4JSON)(nil) 69 _ AggFunc = (*maxMin4Enum)(nil) 70 _ AggFunc = (*maxMin4Set)(nil) 71 72 // All the AggFunc implementations for "AVG" are listed here. 73 _ AggFunc = (*avgOriginal4Decimal)(nil) 74 _ AggFunc = (*avgOriginal4DistinctDecimal)(nil) 75 _ AggFunc = (*avgPartial4Decimal)(nil) 76 77 _ AggFunc = (*avgOriginal4Float64)(nil) 78 _ AggFunc = (*avgPartial4Float64)(nil) 79 _ AggFunc = (*avgOriginal4DistinctFloat64)(nil) 80 81 // All the AggFunc implementations for "SUM" are listed here. 82 _ AggFunc = (*sum4DistinctFloat64)(nil) 83 _ AggFunc = (*sum4DistinctDecimal)(nil) 84 _ AggFunc = (*sum4Decimal)(nil) 85 _ AggFunc = (*sum4Float64)(nil) 86 87 // All the AggFunc implementations for "GROUP_CONCAT" are listed here. 88 _ AggFunc = (*groupConcatDistinct)(nil) 89 _ AggFunc = (*groupConcat)(nil) 90 91 // All the AggFunc implementations for "BIT_OR" are listed here. 92 _ AggFunc = (*bitOrUint64)(nil) 93 94 // All the AggFunc implementations for "BIT_XOR" are listed here. 95 _ AggFunc = (*bitXorUint64)(nil) 96 97 // All the AggFunc implementations for "BIT_AND" are listed here. 98 _ AggFunc = (*bitAndUint64)(nil) 99 100 // All the AggFunc implementations for "JSON_OBJECTAGG" are listed here 101 _ AggFunc = (*jsonObjectAgg)(nil) 102 ) 103 104 const ( 105 // DefUint32Size is the size of uint32 106 DefUint32Size = int64(unsafe.Sizeof(uint32(0))) 107 // DefUint64Size is the size of uint64 108 DefUint64Size = int64(unsafe.Sizeof(uint64(0))) 109 // DefInt64Size is the size of int64 110 DefInt64Size = int64(unsafe.Sizeof(int64(0))) 111 // DefFloat64Size is the size of float64 112 DefFloat64Size = int64(unsafe.Sizeof(float64(0))) 113 // DefTimeSize is the size of time 114 DefTimeSize = int64(16) 115 // DefEventSize is the size of event 116 DefEventSize = int64(unsafe.Sizeof(chunk.Event{})) 117 ) 118 119 // PartialResult represents data structure to causetstore the partial result for the 120 // aggregate functions. Here we use unsafe.Pointer to allow the partial result 121 // to be any type. 122 type PartialResult unsafe.Pointer 123 124 // AggFunc is the interface to evaluate the aggregate functions. 125 type AggFunc interface { 126 // AllocPartialResult allocates a specific data structure to causetstore the 127 // partial result, initializes it, and converts it to PartialResult to 128 // return back. The second returned value is the memDelta used to trace 129 // memory usage. Aggregate operator implementation, no matter it's a hash 130 // or stream, should hold this allocated PartialResult for the further 131 // operations like: "ResetPartialResult", "UFIDelatePartialResult". 132 AllocPartialResult() (pr PartialResult, memDelta int64) 133 134 // ResetPartialResult resets the partial result to the original state for a 135 // specific aggregate function. It converts the input PartialResult to the 136 // specific data structure which stores the partial result and then reset 137 // every field to the proper original state. 138 ResetPartialResult(pr PartialResult) 139 140 // UFIDelatePartialResult uFIDelates the specific partial result for an aggregate 141 // function using the input rows which all belonging to the same data group. 142 // It converts the PartialResult to the specific data structure which stores 143 // the partial result and then iterates on the input rows and uFIDelate that 144 // partial result according to the functionality and the state of the 145 // aggregate function. The returned value is the memDelta used to trace memory 146 // usage. 147 UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) 148 149 // MergePartialResult will be called in the final phase when parallelly 150 // executing. It converts the PartialResult `src`, `dst` to the same specific 151 // data structure which stores the partial results, and then evaluate the 152 // final result using the partial results as input values. The returned value 153 // is the memDelta used to trace memory usage. 154 MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) 155 156 // AppendFinalResult2Chunk finalizes the partial result and append the 157 // final result to the input chunk. Like other operations, it converts the 158 // input PartialResult to the specific data structure which stores the 159 // partial result and then calculates the final result and append that 160 // final result to the chunk provided. 161 AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error 162 } 163 164 type baseAggFunc struct { 165 // args stores the input arguments for an aggregate function, we should 166 // call arg.EvalXXX to get the actual input data for this function. 167 args []memex.Expression 168 169 // ordinal stores the ordinal of the defCausumns in the output chunk, which is 170 // used to append the final result of this function. 171 ordinal int 172 } 173 174 func (*baseAggFunc) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 175 return 0, nil 176 } 177 178 // SlidingWindowAggFunc is the interface to evaluate the aggregate functions using sliding window. 179 type SlidingWindowAggFunc interface { 180 // Slide evaluates the aggregate functions using a sliding window. The input 181 // lastStart and lastEnd are the interval of the former sliding window, 182 // shiftStart, shiftEnd mean the sliding window offset. Note that the input 183 // PartialResult stores the intermediate result which will be used in the next 184 // sliding window, ensure call ResetPartialResult after a frame are evaluated 185 // completely. 186 Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error 187 }