github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_first_row.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/types" 21 "github.com/whtcorpsinc/milevadb/types/json" 22 "github.com/whtcorpsinc/milevadb/soliton/chunk" 23 "github.com/whtcorpsinc/milevadb/soliton/stringutil" 24 ) 25 26 const ( 27 // DefPartialResult4FirstEventIntSize is the size of partialResult4FirstEventInt 28 DefPartialResult4FirstEventIntSize = int64(unsafe.Sizeof(partialResult4FirstEventInt{})) 29 // DefPartialResult4FirstEventFloat32Size is the size of partialResult4FirstEventFloat32 30 DefPartialResult4FirstEventFloat32Size = int64(unsafe.Sizeof(partialResult4FirstEventFloat32{})) 31 // DefPartialResult4FirstEventFloat64Size is the size of partialResult4FirstEventFloat64 32 DefPartialResult4FirstEventFloat64Size = int64(unsafe.Sizeof(partialResult4FirstEventFloat64{})) 33 // DefPartialResult4FirstEventStringSize is the size of partialResult4FirstEventString 34 DefPartialResult4FirstEventStringSize = int64(unsafe.Sizeof(partialResult4FirstEventString{})) 35 // DefPartialResult4FirstEventTimeSize is the size of partialResult4FirstEventTime 36 DefPartialResult4FirstEventTimeSize = int64(unsafe.Sizeof(partialResult4FirstEventTime{})) 37 // DefPartialResult4FirstEventDurationSize is the size of partialResult4FirstEventDuration 38 DefPartialResult4FirstEventDurationSize = int64(unsafe.Sizeof(partialResult4FirstEventDuration{})) 39 // DefPartialResult4FirstEventJSONSize is the size of partialResult4FirstEventJSON 40 DefPartialResult4FirstEventJSONSize = int64(unsafe.Sizeof(partialResult4FirstEventJSON{})) 41 // DefPartialResult4FirstEventDecimalSize is the size of partialResult4FirstEventDecimal 42 DefPartialResult4FirstEventDecimalSize = int64(unsafe.Sizeof(partialResult4FirstEventDecimal{})) 43 // DefPartialResult4FirstEventEnumSize is the size of partialResult4FirstEventEnum 44 DefPartialResult4FirstEventEnumSize = int64(unsafe.Sizeof(partialResult4FirstEventEnum{})) 45 // DefPartialResult4FirstEventSetSize is the size of partialResult4FirstEventSet 46 DefPartialResult4FirstEventSetSize = int64(unsafe.Sizeof(partialResult4FirstEventSet{})) 47 ) 48 49 type basePartialResult4FirstEvent struct { 50 // isNull indicates whether the first event is null. 51 isNull bool 52 // gotFirstEvent indicates whether the first event has been got, 53 // if so, we would avoid evaluating the values of the remained rows. 54 gotFirstEvent bool 55 } 56 57 type partialResult4FirstEventInt struct { 58 basePartialResult4FirstEvent 59 60 val int64 61 } 62 63 type partialResult4FirstEventFloat32 struct { 64 basePartialResult4FirstEvent 65 66 val float32 67 } 68 69 type partialResult4FirstEventDecimal struct { 70 basePartialResult4FirstEvent 71 72 val types.MyDecimal 73 } 74 75 type partialResult4FirstEventFloat64 struct { 76 basePartialResult4FirstEvent 77 78 val float64 79 } 80 81 type partialResult4FirstEventString struct { 82 basePartialResult4FirstEvent 83 84 val string 85 } 86 87 type partialResult4FirstEventTime struct { 88 basePartialResult4FirstEvent 89 90 val types.Time 91 } 92 93 type partialResult4FirstEventDuration struct { 94 basePartialResult4FirstEvent 95 96 val types.Duration 97 } 98 99 type partialResult4FirstEventJSON struct { 100 basePartialResult4FirstEvent 101 102 val json.BinaryJSON 103 } 104 105 type partialResult4FirstEventEnum struct { 106 basePartialResult4FirstEvent 107 108 val types.Enum 109 } 110 111 type partialResult4FirstEventSet struct { 112 basePartialResult4FirstEvent 113 114 val types.Set 115 } 116 117 type firstEvent4Int struct { 118 baseAggFunc 119 } 120 121 func (e *firstEvent4Int) AllocPartialResult() (pr PartialResult, memDelta int64) { 122 return PartialResult(new(partialResult4FirstEventInt)), DefPartialResult4FirstEventIntSize 123 } 124 125 func (e *firstEvent4Int) ResetPartialResult(pr PartialResult) { 126 p := (*partialResult4FirstEventInt)(pr) 127 p.val, p.isNull, p.gotFirstEvent = 0, false, false 128 } 129 130 func (e *firstEvent4Int) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 131 p := (*partialResult4FirstEventInt)(pr) 132 if p.gotFirstEvent { 133 return memDelta, nil 134 } 135 if len(rowsInGroup) > 0 { 136 input, isNull, err := e.args[0].EvalInt(sctx, rowsInGroup[0]) 137 if err != nil { 138 return memDelta, err 139 } 140 p.gotFirstEvent, p.isNull, p.val = true, isNull, input 141 } 142 return memDelta, nil 143 } 144 145 func (*firstEvent4Int) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 146 p1, p2 := (*partialResult4FirstEventInt)(src), (*partialResult4FirstEventInt)(dst) 147 if !p2.gotFirstEvent { 148 *p2 = *p1 149 } 150 return memDelta, nil 151 } 152 153 func (e *firstEvent4Int) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 154 p := (*partialResult4FirstEventInt)(pr) 155 if p.isNull || !p.gotFirstEvent { 156 chk.AppendNull(e.ordinal) 157 return nil 158 } 159 chk.AppendInt64(e.ordinal, p.val) 160 return nil 161 } 162 163 type firstEvent4Float32 struct { 164 baseAggFunc 165 } 166 167 func (e *firstEvent4Float32) AllocPartialResult() (pr PartialResult, memDelta int64) { 168 return PartialResult(new(partialResult4FirstEventFloat32)), DefPartialResult4FirstEventFloat32Size 169 } 170 171 func (e *firstEvent4Float32) ResetPartialResult(pr PartialResult) { 172 p := (*partialResult4FirstEventFloat32)(pr) 173 p.isNull, p.gotFirstEvent = false, false 174 } 175 176 func (e *firstEvent4Float32) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 177 p := (*partialResult4FirstEventFloat32)(pr) 178 if p.gotFirstEvent { 179 return memDelta, nil 180 } 181 if len(rowsInGroup) > 0 { 182 input, isNull, err := e.args[0].EvalReal(sctx, rowsInGroup[0]) 183 if err != nil { 184 return memDelta, err 185 } 186 p.gotFirstEvent, p.isNull, p.val = true, isNull, float32(input) 187 } 188 return memDelta, nil 189 } 190 191 func (*firstEvent4Float32) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 192 p1, p2 := (*partialResult4FirstEventFloat32)(src), (*partialResult4FirstEventFloat32)(dst) 193 if !p2.gotFirstEvent { 194 *p2 = *p1 195 } 196 return memDelta, nil 197 } 198 199 func (e *firstEvent4Float32) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 200 p := (*partialResult4FirstEventFloat32)(pr) 201 if p.isNull || !p.gotFirstEvent { 202 chk.AppendNull(e.ordinal) 203 return nil 204 } 205 chk.AppendFloat32(e.ordinal, p.val) 206 return nil 207 } 208 209 type firstEvent4Float64 struct { 210 baseAggFunc 211 } 212 213 func (e *firstEvent4Float64) AllocPartialResult() (pr PartialResult, memDelta int64) { 214 return PartialResult(new(partialResult4FirstEventFloat64)), DefPartialResult4FirstEventFloat64Size 215 } 216 217 func (e *firstEvent4Float64) ResetPartialResult(pr PartialResult) { 218 p := (*partialResult4FirstEventFloat64)(pr) 219 p.isNull, p.gotFirstEvent = false, false 220 } 221 222 func (e *firstEvent4Float64) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 223 p := (*partialResult4FirstEventFloat64)(pr) 224 if p.gotFirstEvent { 225 return memDelta, nil 226 } 227 if len(rowsInGroup) > 0 { 228 input, isNull, err := e.args[0].EvalReal(sctx, rowsInGroup[0]) 229 if err != nil { 230 return memDelta, err 231 } 232 p.gotFirstEvent, p.isNull, p.val = true, isNull, input 233 } 234 return memDelta, nil 235 } 236 237 func (*firstEvent4Float64) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 238 p1, p2 := (*partialResult4FirstEventFloat64)(src), (*partialResult4FirstEventFloat64)(dst) 239 if !p2.gotFirstEvent { 240 *p2 = *p1 241 } 242 return memDelta, nil 243 } 244 245 func (e *firstEvent4Float64) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 246 p := (*partialResult4FirstEventFloat64)(pr) 247 if p.isNull || !p.gotFirstEvent { 248 chk.AppendNull(e.ordinal) 249 return nil 250 } 251 chk.AppendFloat64(e.ordinal, p.val) 252 return nil 253 } 254 255 type firstEvent4String struct { 256 baseAggFunc 257 } 258 259 func (e *firstEvent4String) AllocPartialResult() (pr PartialResult, memDelta int64) { 260 return PartialResult(new(partialResult4FirstEventString)), DefPartialResult4FirstEventStringSize 261 } 262 263 func (e *firstEvent4String) ResetPartialResult(pr PartialResult) { 264 p := (*partialResult4FirstEventString)(pr) 265 p.isNull, p.gotFirstEvent = false, false 266 } 267 268 func (e *firstEvent4String) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 269 p := (*partialResult4FirstEventString)(pr) 270 if p.gotFirstEvent { 271 return memDelta, nil 272 } 273 if len(rowsInGroup) > 0 { 274 input, isNull, err := e.args[0].EvalString(sctx, rowsInGroup[0]) 275 if err != nil { 276 return memDelta, err 277 } 278 p.gotFirstEvent, p.isNull, p.val = true, isNull, stringutil.Copy(input) 279 memDelta += int64(len(input)) 280 } 281 return memDelta, nil 282 } 283 284 func (*firstEvent4String) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 285 p1, p2 := (*partialResult4FirstEventString)(src), (*partialResult4FirstEventString)(dst) 286 if !p2.gotFirstEvent { 287 *p2 = *p1 288 } 289 return memDelta, nil 290 } 291 292 func (e *firstEvent4String) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 293 p := (*partialResult4FirstEventString)(pr) 294 if p.isNull || !p.gotFirstEvent { 295 chk.AppendNull(e.ordinal) 296 return nil 297 } 298 chk.AppendString(e.ordinal, p.val) 299 return nil 300 } 301 302 type firstEvent4Time struct { 303 baseAggFunc 304 } 305 306 func (e *firstEvent4Time) AllocPartialResult() (pr PartialResult, memDelta int64) { 307 return PartialResult(new(partialResult4FirstEventTime)), DefPartialResult4FirstEventTimeSize 308 } 309 310 func (e *firstEvent4Time) ResetPartialResult(pr PartialResult) { 311 p := (*partialResult4FirstEventTime)(pr) 312 p.isNull, p.gotFirstEvent = false, false 313 } 314 315 func (e *firstEvent4Time) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 316 p := (*partialResult4FirstEventTime)(pr) 317 if p.gotFirstEvent { 318 return memDelta, nil 319 } 320 if len(rowsInGroup) > 0 { 321 input, isNull, err := e.args[0].EvalTime(sctx, rowsInGroup[0]) 322 if err != nil { 323 return memDelta, err 324 } 325 p.gotFirstEvent, p.isNull, p.val = true, isNull, input 326 } 327 return memDelta, nil 328 } 329 330 func (*firstEvent4Time) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 331 p1, p2 := (*partialResult4FirstEventTime)(src), (*partialResult4FirstEventTime)(dst) 332 if !p2.gotFirstEvent { 333 *p2 = *p1 334 } 335 return memDelta, nil 336 } 337 338 func (e *firstEvent4Time) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 339 p := (*partialResult4FirstEventTime)(pr) 340 if p.isNull || !p.gotFirstEvent { 341 chk.AppendNull(e.ordinal) 342 return nil 343 } 344 chk.AppendTime(e.ordinal, p.val) 345 return nil 346 } 347 348 type firstEvent4Duration struct { 349 baseAggFunc 350 } 351 352 func (e *firstEvent4Duration) AllocPartialResult() (pr PartialResult, memDelta int64) { 353 return PartialResult(new(partialResult4FirstEventDuration)), DefPartialResult4FirstEventDurationSize 354 } 355 356 func (e *firstEvent4Duration) ResetPartialResult(pr PartialResult) { 357 p := (*partialResult4FirstEventDuration)(pr) 358 p.isNull, p.gotFirstEvent = false, false 359 } 360 361 func (e *firstEvent4Duration) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 362 p := (*partialResult4FirstEventDuration)(pr) 363 if p.gotFirstEvent { 364 return memDelta, nil 365 } 366 if len(rowsInGroup) > 0 { 367 input, isNull, err := e.args[0].EvalDuration(sctx, rowsInGroup[0]) 368 if err != nil { 369 return memDelta, err 370 } 371 p.gotFirstEvent, p.isNull, p.val = true, isNull, input 372 } 373 return memDelta, nil 374 } 375 376 func (*firstEvent4Duration) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 377 p1, p2 := (*partialResult4FirstEventDuration)(src), (*partialResult4FirstEventDuration)(dst) 378 if !p2.gotFirstEvent { 379 *p2 = *p1 380 } 381 return memDelta, nil 382 } 383 384 func (e *firstEvent4Duration) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 385 p := (*partialResult4FirstEventDuration)(pr) 386 if p.isNull || !p.gotFirstEvent { 387 chk.AppendNull(e.ordinal) 388 return nil 389 } 390 chk.AppendDuration(e.ordinal, p.val) 391 return nil 392 } 393 394 type firstEvent4JSON struct { 395 baseAggFunc 396 } 397 398 func (e *firstEvent4JSON) AllocPartialResult() (pr PartialResult, memDelta int64) { 399 return PartialResult(new(partialResult4FirstEventJSON)), DefPartialResult4FirstEventJSONSize 400 } 401 402 func (e *firstEvent4JSON) ResetPartialResult(pr PartialResult) { 403 p := (*partialResult4FirstEventJSON)(pr) 404 p.isNull, p.gotFirstEvent = false, false 405 } 406 407 func (e *firstEvent4JSON) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 408 p := (*partialResult4FirstEventJSON)(pr) 409 if p.gotFirstEvent { 410 return memDelta, nil 411 } 412 if len(rowsInGroup) > 0 { 413 input, isNull, err := e.args[0].EvalJSON(sctx, rowsInGroup[0]) 414 if err != nil { 415 return memDelta, err 416 } 417 p.gotFirstEvent, p.isNull, p.val = true, isNull, input.Copy() 418 memDelta += int64(len(input.Value)) 419 } 420 return memDelta, nil 421 } 422 func (*firstEvent4JSON) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 423 p1, p2 := (*partialResult4FirstEventJSON)(src), (*partialResult4FirstEventJSON)(dst) 424 if !p2.gotFirstEvent { 425 *p2 = *p1 426 } 427 return memDelta, nil 428 } 429 430 func (e *firstEvent4JSON) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 431 p := (*partialResult4FirstEventJSON)(pr) 432 if p.isNull || !p.gotFirstEvent { 433 chk.AppendNull(e.ordinal) 434 return nil 435 } 436 chk.AppendJSON(e.ordinal, p.val) 437 return nil 438 } 439 440 type firstEvent4Decimal struct { 441 baseAggFunc 442 } 443 444 func (e *firstEvent4Decimal) AllocPartialResult() (pr PartialResult, memDelta int64) { 445 return PartialResult(new(partialResult4FirstEventDecimal)), DefPartialResult4FirstEventDecimalSize 446 } 447 448 func (e *firstEvent4Decimal) ResetPartialResult(pr PartialResult) { 449 p := (*partialResult4FirstEventDecimal)(pr) 450 p.isNull, p.gotFirstEvent = false, false 451 } 452 453 func (e *firstEvent4Decimal) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 454 p := (*partialResult4FirstEventDecimal)(pr) 455 if p.gotFirstEvent { 456 return memDelta, nil 457 } 458 if len(rowsInGroup) > 0 { 459 input, isNull, err := e.args[0].EvalDecimal(sctx, rowsInGroup[0]) 460 if err != nil { 461 return memDelta, err 462 } 463 p.gotFirstEvent, p.isNull = true, isNull 464 if input != nil { 465 p.val = *input 466 } 467 } 468 return memDelta, nil 469 } 470 471 func (e *firstEvent4Decimal) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 472 p := (*partialResult4FirstEventDecimal)(pr) 473 if p.isNull || !p.gotFirstEvent { 474 chk.AppendNull(e.ordinal) 475 return nil 476 } 477 chk.AppendMyDecimal(e.ordinal, &p.val) 478 return nil 479 } 480 481 func (*firstEvent4Decimal) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 482 p1, p2 := (*partialResult4FirstEventDecimal)(src), (*partialResult4FirstEventDecimal)(dst) 483 if !p2.gotFirstEvent { 484 *p2 = *p1 485 } 486 return memDelta, nil 487 } 488 489 type firstEvent4Enum struct { 490 baseAggFunc 491 } 492 493 func (e *firstEvent4Enum) AllocPartialResult() (pr PartialResult, memDelta int64) { 494 return PartialResult(new(partialResult4FirstEventEnum)), DefPartialResult4FirstEventEnumSize 495 } 496 497 func (e *firstEvent4Enum) ResetPartialResult(pr PartialResult) { 498 p := (*partialResult4FirstEventEnum)(pr) 499 p.isNull, p.gotFirstEvent = false, false 500 } 501 502 func (e *firstEvent4Enum) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 503 p := (*partialResult4FirstEventEnum)(pr) 504 if p.gotFirstEvent { 505 return memDelta, nil 506 } 507 for _, event := range rowsInGroup { 508 d, err := e.args[0].Eval(event) 509 if err != nil { 510 return memDelta, err 511 } 512 p.gotFirstEvent, p.isNull, p.val = true, d.IsNull(), d.GetMysqlEnum().Copy() 513 memDelta += int64(len(p.val.Name)) 514 break 515 } 516 return memDelta, nil 517 } 518 519 func (*firstEvent4Enum) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 520 p1, p2 := (*partialResult4FirstEventEnum)(src), (*partialResult4FirstEventEnum)(dst) 521 if !p2.gotFirstEvent { 522 *p2 = *p1 523 } 524 return memDelta, nil 525 } 526 527 func (e *firstEvent4Enum) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 528 p := (*partialResult4FirstEventEnum)(pr) 529 if p.isNull || !p.gotFirstEvent { 530 chk.AppendNull(e.ordinal) 531 return nil 532 } 533 chk.AppendEnum(e.ordinal, p.val) 534 return nil 535 } 536 537 type firstEvent4Set struct { 538 baseAggFunc 539 } 540 541 func (e *firstEvent4Set) AllocPartialResult() (pr PartialResult, memDelta int64) { 542 return PartialResult(new(partialResult4FirstEventSet)), DefPartialResult4FirstEventSetSize 543 } 544 545 func (e *firstEvent4Set) ResetPartialResult(pr PartialResult) { 546 p := (*partialResult4FirstEventSet)(pr) 547 p.isNull, p.gotFirstEvent = false, false 548 } 549 550 func (e *firstEvent4Set) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 551 p := (*partialResult4FirstEventSet)(pr) 552 if p.gotFirstEvent { 553 return memDelta, nil 554 } 555 for _, event := range rowsInGroup { 556 d, err := e.args[0].Eval(event) 557 if err != nil { 558 return memDelta, err 559 } 560 p.gotFirstEvent, p.isNull, p.val = true, d.IsNull(), d.GetMysqlSet().Copy() 561 memDelta += int64(len(p.val.Name)) 562 break 563 } 564 return memDelta, nil 565 } 566 567 func (*firstEvent4Set) MergePartialResult(sctx stochastikctx.Context, src, dst PartialResult) (memDelta int64, err error) { 568 p1, p2 := (*partialResult4FirstEventSet)(src), (*partialResult4FirstEventSet)(dst) 569 if !p2.gotFirstEvent { 570 *p2 = *p1 571 } 572 return memDelta, nil 573 } 574 575 func (e *firstEvent4Set) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 576 p := (*partialResult4FirstEventSet)(pr) 577 if p.isNull || !p.gotFirstEvent { 578 chk.AppendNull(e.ordinal) 579 return nil 580 } 581 chk.AppendSet(e.ordinal, p.val) 582 return nil 583 }