github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_count.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/stochastikctx" 20 "github.com/whtcorpsinc/milevadb/soliton/chunk" 21 ) 22 23 const ( 24 // DefPartialResult4CountSize is the size of partialResult4Count 25 DefPartialResult4CountSize = int64(unsafe.Sizeof(partialResult4Count(0))) 26 ) 27 28 type baseCount struct { 29 baseAggFunc 30 } 31 32 type partialResult4Count = int64 33 34 func (e *baseCount) AllocPartialResult() (pr PartialResult, memDelta int64) { 35 return PartialResult(new(partialResult4Count)), DefPartialResult4CountSize 36 } 37 38 func (e *baseCount) ResetPartialResult(pr PartialResult) { 39 p := (*partialResult4Count)(pr) 40 *p = 0 41 } 42 43 func (e *baseCount) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 44 p := (*partialResult4Count)(pr) 45 chk.AppendInt64(e.ordinal, *p) 46 return nil 47 } 48 49 type countOriginal4Int struct { 50 baseCount 51 } 52 53 func (e *countOriginal4Int) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 54 p := (*partialResult4Count)(pr) 55 56 for _, event := range rowsInGroup { 57 _, isNull, err := e.args[0].EvalInt(sctx, event) 58 if err != nil { 59 return 0, err 60 } 61 if isNull { 62 continue 63 } 64 65 *p++ 66 } 67 68 return 0, nil 69 } 70 71 func (e *countOriginal4Int) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 72 p := (*partialResult4Count)(pr) 73 for i := uint64(0); i < shiftStart; i++ { 74 _, isNull, err := e.args[0].EvalInt(sctx, rows[lastStart+i]) 75 if err != nil { 76 return err 77 } 78 if isNull { 79 continue 80 } 81 *p-- 82 } 83 for i := uint64(0); i < shiftEnd; i++ { 84 _, isNull, err := e.args[0].EvalInt(sctx, rows[lastEnd+i]) 85 if err != nil { 86 return err 87 } 88 if isNull { 89 continue 90 } 91 *p++ 92 } 93 return nil 94 } 95 96 type countOriginal4Real struct { 97 baseCount 98 } 99 100 func (e *countOriginal4Real) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 101 p := (*partialResult4Count)(pr) 102 103 for _, event := range rowsInGroup { 104 _, isNull, err := e.args[0].EvalReal(sctx, event) 105 if err != nil { 106 return 0, err 107 } 108 if isNull { 109 continue 110 } 111 112 *p++ 113 } 114 115 return 0, nil 116 } 117 118 func (e *countOriginal4Real) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 119 p := (*partialResult4Count)(pr) 120 for i := uint64(0); i < shiftStart; i++ { 121 _, isNull, err := e.args[0].EvalReal(sctx, rows[lastStart+i]) 122 if err != nil { 123 return err 124 } 125 if isNull { 126 continue 127 } 128 *p-- 129 } 130 for i := uint64(0); i < shiftEnd; i++ { 131 _, isNull, err := e.args[0].EvalReal(sctx, rows[lastEnd+i]) 132 if err != nil { 133 return err 134 } 135 if isNull { 136 continue 137 } 138 *p++ 139 } 140 return nil 141 } 142 143 type countOriginal4Decimal struct { 144 baseCount 145 } 146 147 func (e *countOriginal4Decimal) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 148 p := (*partialResult4Count)(pr) 149 150 for _, event := range rowsInGroup { 151 _, isNull, err := e.args[0].EvalDecimal(sctx, event) 152 if err != nil { 153 return 0, err 154 } 155 if isNull { 156 continue 157 } 158 159 *p++ 160 } 161 162 return 0, nil 163 } 164 165 func (e *countOriginal4Decimal) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 166 p := (*partialResult4Count)(pr) 167 for i := uint64(0); i < shiftStart; i++ { 168 _, isNull, err := e.args[0].EvalDecimal(sctx, rows[lastStart+i]) 169 if err != nil { 170 return err 171 } 172 if isNull { 173 continue 174 } 175 *p-- 176 } 177 for i := uint64(0); i < shiftEnd; i++ { 178 _, isNull, err := e.args[0].EvalDecimal(sctx, rows[lastEnd+i]) 179 if err != nil { 180 return err 181 } 182 if isNull { 183 continue 184 } 185 *p++ 186 } 187 return nil 188 } 189 190 type countOriginal4Time struct { 191 baseCount 192 } 193 194 func (e *countOriginal4Time) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 195 p := (*partialResult4Count)(pr) 196 197 for _, event := range rowsInGroup { 198 _, isNull, err := e.args[0].EvalTime(sctx, event) 199 if err != nil { 200 return 0, err 201 } 202 if isNull { 203 continue 204 } 205 206 *p++ 207 } 208 209 return 0, nil 210 } 211 212 func (e *countOriginal4Time) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 213 p := (*partialResult4Count)(pr) 214 for i := uint64(0); i < shiftStart; i++ { 215 _, isNull, err := e.args[0].EvalTime(sctx, rows[lastStart+i]) 216 if err != nil { 217 return err 218 } 219 if isNull { 220 continue 221 } 222 *p-- 223 } 224 for i := uint64(0); i < shiftEnd; i++ { 225 _, isNull, err := e.args[0].EvalTime(sctx, rows[lastEnd+i]) 226 if err != nil { 227 return err 228 } 229 if isNull { 230 continue 231 } 232 *p++ 233 } 234 return nil 235 } 236 237 type countOriginal4Duration struct { 238 baseCount 239 } 240 241 func (e *countOriginal4Duration) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 242 p := (*partialResult4Count)(pr) 243 244 for _, event := range rowsInGroup { 245 _, isNull, err := e.args[0].EvalDuration(sctx, event) 246 if err != nil { 247 return 0, err 248 } 249 if isNull { 250 continue 251 } 252 253 *p++ 254 } 255 256 return 0, nil 257 } 258 259 func (e *countOriginal4Duration) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 260 p := (*partialResult4Count)(pr) 261 for i := uint64(0); i < shiftStart; i++ { 262 _, isNull, err := e.args[0].EvalDuration(sctx, rows[lastStart+i]) 263 if err != nil { 264 return err 265 } 266 if isNull { 267 continue 268 } 269 *p-- 270 } 271 for i := uint64(0); i < shiftEnd; i++ { 272 _, isNull, err := e.args[0].EvalDuration(sctx, rows[lastEnd+i]) 273 if err != nil { 274 return err 275 } 276 if isNull { 277 continue 278 } 279 *p++ 280 } 281 return nil 282 } 283 284 type countOriginal4JSON struct { 285 baseCount 286 } 287 288 func (e *countOriginal4JSON) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 289 p := (*partialResult4Count)(pr) 290 291 for _, event := range rowsInGroup { 292 _, isNull, err := e.args[0].EvalJSON(sctx, event) 293 if err != nil { 294 return 0, err 295 } 296 if isNull { 297 continue 298 } 299 300 *p++ 301 } 302 303 return 0, nil 304 } 305 306 func (e *countOriginal4JSON) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 307 p := (*partialResult4Count)(pr) 308 for i := uint64(0); i < shiftStart; i++ { 309 _, isNull, err := e.args[0].EvalJSON(sctx, rows[lastStart+i]) 310 if err != nil { 311 return err 312 } 313 if isNull { 314 continue 315 } 316 *p-- 317 } 318 for i := uint64(0); i < shiftEnd; i++ { 319 _, isNull, err := e.args[0].EvalJSON(sctx, rows[lastEnd+i]) 320 if err != nil { 321 return err 322 } 323 if isNull { 324 continue 325 } 326 *p++ 327 } 328 return nil 329 } 330 331 type countOriginal4String struct { 332 baseCount 333 } 334 335 func (e *countOriginal4String) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 336 p := (*partialResult4Count)(pr) 337 338 for _, event := range rowsInGroup { 339 _, isNull, err := e.args[0].EvalString(sctx, event) 340 if err != nil { 341 return 0, err 342 } 343 if isNull { 344 continue 345 } 346 347 *p++ 348 } 349 350 return 0, nil 351 } 352 353 func (e *countOriginal4String) Slide(sctx stochastikctx.Context, rows []chunk.Event, lastStart, lastEnd uint64, shiftStart, shiftEnd uint64, pr PartialResult) error { 354 p := (*partialResult4Count)(pr) 355 for i := uint64(0); i < shiftStart; i++ { 356 _, isNull, err := e.args[0].EvalString(sctx, rows[lastStart+i]) 357 if err != nil { 358 return err 359 } 360 if isNull { 361 continue 362 } 363 *p-- 364 } 365 for i := uint64(0); i < shiftEnd; i++ { 366 _, isNull, err := e.args[0].EvalString(sctx, rows[lastEnd+i]) 367 if err != nil { 368 return err 369 } 370 if isNull { 371 continue 372 } 373 *p++ 374 } 375 return nil 376 } 377 378 type countPartial struct { 379 baseCount 380 } 381 382 func (e *countPartial) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 383 p := (*partialResult4Count)(pr) 384 for _, event := range rowsInGroup { 385 input, isNull, err := e.args[0].EvalInt(sctx, event) 386 if err != nil { 387 return 0, err 388 } 389 if isNull { 390 continue 391 } 392 393 *p += input 394 } 395 return 0, nil 396 } 397 398 func (*countPartial) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 399 p1, p2 := (*partialResult4Count)(src), (*partialResult4Count)(dst) 400 *p2 += *p1 401 return 0, nil 402 }