github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_bitfuncs.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 "math" 18 "unsafe" 19 20 "github.com/whtcorpsinc/milevadb/stochastikctx" 21 "github.com/whtcorpsinc/milevadb/soliton/chunk" 22 ) 23 24 const ( 25 // DefPartialResult4BitFuncSize the size of partialResult4BitFunc 26 DefPartialResult4BitFuncSize = int64(unsafe.Sizeof(partialResult4BitFunc(0))) 27 ) 28 29 type baseBitAggFunc struct { 30 baseAggFunc 31 } 32 33 type partialResult4BitFunc = uint64 34 35 func (e *baseBitAggFunc) AllocPartialResult() (pr PartialResult, memDelta int64) { 36 return PartialResult(new(partialResult4BitFunc)), DefPartialResult4BitFuncSize 37 } 38 39 func (e *baseBitAggFunc) ResetPartialResult(pr PartialResult) { 40 p := (*partialResult4BitFunc)(pr) 41 *p = 0 42 } 43 44 func (e *baseBitAggFunc) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 45 p := (*partialResult4BitFunc)(pr) 46 chk.AppendUint64(e.ordinal, *p) 47 return nil 48 } 49 50 type bitOrUint64 struct { 51 baseBitAggFunc 52 } 53 54 func (e *bitOrUint64) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 55 p := (*partialResult4BitFunc)(pr) 56 for _, event := range rowsInGroup { 57 inputValue, isNull, err := e.args[0].EvalInt(sctx, event) 58 if err != nil { 59 return memDelta, err 60 } 61 if isNull { 62 continue 63 } 64 *p |= uint64(inputValue) 65 } 66 return memDelta, nil 67 } 68 69 func (*bitOrUint64) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 70 p1, p2 := (*partialResult4BitFunc)(src), (*partialResult4BitFunc)(dst) 71 *p2 |= *p1 72 return memDelta, nil 73 } 74 75 type bitXorUint64 struct { 76 baseBitAggFunc 77 } 78 79 func (e *bitXorUint64) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 80 p := (*partialResult4BitFunc)(pr) 81 for _, event := range rowsInGroup { 82 inputValue, isNull, err := e.args[0].EvalInt(sctx, event) 83 if err != nil { 84 return memDelta, err 85 } 86 if isNull { 87 continue 88 } 89 *p ^= uint64(inputValue) 90 } 91 return memDelta, nil 92 } 93 94 func (e *bitXorUint64) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 95 p := (*partialResult4BitFunc)(pr) 96 for i := uint64(0); i < shiftStart; i++ { 97 inputValue, isNull, err := e.args[0].EvalInt(sctx, rows[lastStart+i]) 98 if err != nil { 99 return err 100 } 101 if isNull { 102 continue 103 } 104 *p ^= uint64(inputValue) 105 } 106 for i := uint64(0); i < shiftEnd; i++ { 107 inputValue, isNull, err := e.args[0].EvalInt(sctx, rows[lastEnd+i]) 108 if err != nil { 109 return err 110 } 111 if isNull { 112 continue 113 } 114 *p ^= uint64(inputValue) 115 } 116 return nil 117 } 118 119 func (*bitXorUint64) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 120 p1, p2 := (*partialResult4BitFunc)(src), (*partialResult4BitFunc)(dst) 121 *p2 ^= *p1 122 return memDelta, nil 123 } 124 125 type bitAndUint64 struct { 126 baseBitAggFunc 127 } 128 129 func (e *bitAndUint64) AllocPartialResult() (pr PartialResult, memDelta int64) { 130 p := new(partialResult4BitFunc) 131 *p = math.MaxUint64 132 return PartialResult(p), DefPartialResult4BitFuncSize 133 } 134 135 func (e *bitAndUint64) ResetPartialResult(pr PartialResult) { 136 p := (*partialResult4BitFunc)(pr) 137 *p = math.MaxUint64 138 } 139 140 func (e *bitAndUint64) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 141 p := (*partialResult4BitFunc)(pr) 142 for _, event := range rowsInGroup { 143 inputValue, isNull, err := e.args[0].EvalInt(sctx, event) 144 if err != nil { 145 return memDelta, err 146 } 147 if isNull { 148 continue 149 } 150 151 *p &= uint64(inputValue) 152 } 153 return memDelta, nil 154 } 155 156 func (*bitAndUint64) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 157 p1, p2 := (*partialResult4BitFunc)(src), (*partialResult4BitFunc)(dst) 158 *p2 &= *p1 159 return memDelta, nil 160 }