github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_math_vec.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 memex 15 16 import ( 17 "fmt" 18 "hash/crc32" 19 "math" 20 "strconv" 21 22 "github.com/cznic/mathutil" 23 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 24 "github.com/whtcorpsinc/milevadb/types" 25 "github.com/whtcorpsinc/milevadb/soliton/chunk" 26 ) 27 28 func (b *builtinLog1ArgSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 29 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 30 return err 31 } 32 f64s := result.Float64s() 33 for i := 0; i < len(f64s); i++ { 34 if result.IsNull(i) { 35 continue 36 } 37 if f64s[i] <= 0 { 38 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(ErrInvalidArgumentForLogarithm) 39 result.SetNull(i, true) 40 } else { 41 f64s[i] = math.Log(f64s[i]) 42 } 43 } 44 return nil 45 } 46 47 func (b *builtinLog1ArgSig) vectorized() bool { 48 return true 49 } 50 51 func (b *builtinLog2Sig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 52 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 53 return err 54 } 55 f64s := result.Float64s() 56 for i := 0; i < len(f64s); i++ { 57 if result.IsNull(i) { 58 continue 59 } 60 if f64s[i] <= 0 { 61 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(ErrInvalidArgumentForLogarithm) 62 result.SetNull(i, true) 63 } else { 64 f64s[i] = math.Log2(f64s[i]) 65 } 66 } 67 return nil 68 } 69 70 func (b *builtinLog2Sig) vectorized() bool { 71 return true 72 } 73 74 func (b *builtinLog10Sig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 75 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 76 return err 77 } 78 f64s := result.Float64s() 79 for i := 0; i < len(f64s); i++ { 80 if result.IsNull(i) { 81 continue 82 } 83 if f64s[i] <= 0 { 84 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(ErrInvalidArgumentForLogarithm) 85 result.SetNull(i, true) 86 } else { 87 f64s[i] = math.Log10(f64s[i]) 88 } 89 } 90 return nil 91 } 92 93 func (b *builtinLog10Sig) vectorized() bool { 94 return true 95 } 96 97 func (b *builtinSqrtSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 98 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 99 return err 100 } 101 f64s := result.Float64s() 102 for i := 0; i < len(f64s); i++ { 103 if result.IsNull(i) { 104 continue 105 } 106 if f64s[i] < 0 { 107 result.SetNull(i, true) 108 } else { 109 f64s[i] = math.Sqrt(f64s[i]) 110 } 111 } 112 return nil 113 } 114 115 func (b *builtinSqrtSig) vectorized() bool { 116 return true 117 } 118 119 func (b *builtinAcosSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 120 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 121 return err 122 } 123 f64s := result.Float64s() 124 for i := 0; i < len(f64s); i++ { 125 if result.IsNull(i) { 126 continue 127 } 128 if f64s[i] < -1 || f64s[i] > 1 { 129 result.SetNull(i, true) 130 } else { 131 f64s[i] = math.Acos(f64s[i]) 132 } 133 } 134 return nil 135 } 136 137 func (b *builtinAcosSig) vectorized() bool { 138 return true 139 } 140 141 func (b *builtinAsinSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 142 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 143 return err 144 } 145 f64s := result.Float64s() 146 for i := 0; i < len(f64s); i++ { 147 if result.IsNull(i) { 148 continue 149 } 150 if f64s[i] < -1 || f64s[i] > 1 { 151 result.SetNull(i, true) 152 } else { 153 f64s[i] = math.Asin(f64s[i]) 154 } 155 } 156 return nil 157 } 158 159 func (b *builtinAsinSig) vectorized() bool { 160 return true 161 } 162 163 func (b *builtinAtan1ArgSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 164 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 165 return err 166 } 167 f64s := result.Float64s() 168 for i := 0; i < len(f64s); i++ { 169 if result.IsNull(i) { 170 continue 171 } 172 f64s[i] = math.Atan(f64s[i]) 173 } 174 return nil 175 } 176 177 func (b *builtinAtan1ArgSig) vectorized() bool { 178 return true 179 } 180 181 func (b *builtinAtan2ArgsSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 182 n := input.NumEvents() 183 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 184 return err 185 } 186 187 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 188 if err != nil { 189 return err 190 } 191 defer b.bufSlabPredictor.put(buf) 192 if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil { 193 return err 194 } 195 196 f64s := result.Float64s() 197 arg := buf.Float64s() 198 199 result.MergeNulls(buf) 200 201 for i := 0; i < n; i++ { 202 if result.IsNull(i) { 203 continue 204 } 205 f64s[i] = math.Atan2(f64s[i], arg[i]) 206 } 207 return nil 208 } 209 210 func (b *builtinAtan2ArgsSig) vectorized() bool { 211 return true 212 } 213 214 func (b *builtinCosSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 215 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 216 return err 217 } 218 f64s := result.Float64s() 219 for i := 0; i < len(f64s); i++ { 220 if result.IsNull(i) { 221 continue 222 } 223 f64s[i] = math.Cos(f64s[i]) 224 } 225 return nil 226 } 227 228 func (b *builtinCosSig) vectorized() bool { 229 return true 230 } 231 232 func (b *builtinCotSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 233 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 234 return err 235 } 236 f64s := result.Float64s() 237 for i := 0; i < len(f64s); i++ { 238 if result.IsNull(i) { 239 continue 240 } 241 tan := math.Tan(f64s[i]) 242 if tan != 0 { 243 cot := 1 / tan 244 if !math.IsInf(cot, 0) && !math.IsNaN(cot) { 245 f64s[i] = cot 246 } 247 continue 248 } 249 if err := types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("cot(%s)", strconv.FormatFloat(f64s[i], 'f', -1, 64))); err != nil { 250 return err 251 } 252 } 253 return nil 254 } 255 256 func (b *builtinCotSig) vectorized() bool { 257 return true 258 } 259 260 func (b *builtinDegreesSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 261 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 262 return err 263 } 264 f64s := result.Float64s() 265 for i := 0; i < len(f64s); i++ { 266 if result.IsNull(i) { 267 continue 268 } 269 f64s[i] = f64s[i] * 180 / math.Pi 270 } 271 return nil 272 } 273 274 func (b *builtinDegreesSig) vectorized() bool { 275 return true 276 } 277 278 func (b *builtinExpSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 279 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 280 return err 281 } 282 f64s := result.Float64s() 283 for i := 0; i < len(f64s); i++ { 284 if result.IsNull(i) { 285 continue 286 } 287 exp := math.Exp(f64s[i]) 288 if math.IsInf(exp, 0) || math.IsNaN(exp) { 289 s := fmt.Sprintf("exp(%s)", b.args[0].String()) 290 if err := types.ErrOverflow.GenWithStackByArgs("DOUBLE", s); err != nil { 291 return err 292 } 293 } 294 f64s[i] = exp 295 } 296 return nil 297 } 298 299 func (b *builtinExpSig) vectorized() bool { 300 return true 301 } 302 303 func (b *builtinRadiansSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 304 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 305 return err 306 } 307 f64s := result.Float64s() 308 for i := 0; i < len(f64s); i++ { 309 if result.IsNull(i) { 310 continue 311 } 312 f64s[i] = f64s[i] * math.Pi / 180 313 } 314 return nil 315 } 316 317 func (b *builtinRadiansSig) vectorized() bool { 318 return true 319 } 320 321 func (b *builtinSinSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 322 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 323 return err 324 } 325 f64s := result.Float64s() 326 for i := 0; i < len(f64s); i++ { 327 if result.IsNull(i) { 328 continue 329 } 330 f64s[i] = math.Sin(f64s[i]) 331 } 332 return nil 333 } 334 335 func (b *builtinSinSig) vectorized() bool { 336 return true 337 } 338 339 func (b *builtinTanSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 340 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 341 return err 342 } 343 f64s := result.Float64s() 344 for i := 0; i < len(f64s); i++ { 345 if result.IsNull(i) { 346 continue 347 } 348 f64s[i] = math.Tan(f64s[i]) 349 } 350 return nil 351 } 352 353 func (b *builtinTanSig) vectorized() bool { 354 return true 355 } 356 357 func (b *builtinAbsDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 358 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 359 return err 360 } 361 zero := new(types.MyDecimal) 362 buf := new(types.MyDecimal) 363 d64s := result.Decimals() 364 for i := 0; i < len(d64s); i++ { 365 if result.IsNull(i) { 366 continue 367 } 368 if d64s[i].IsNegative() { 369 if err := types.DecimalSub(zero, &d64s[i], buf); err != nil { 370 return err 371 } 372 d64s[i] = *buf 373 } 374 } 375 return nil 376 } 377 378 func (b *builtinAbsDecSig) vectorized() bool { 379 return true 380 } 381 382 func (b *builtinRoundDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 383 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 384 return err 385 } 386 d64s := result.Decimals() 387 buf := new(types.MyDecimal) 388 for i := 0; i < len(d64s); i++ { 389 if result.IsNull(i) { 390 continue 391 } 392 if err := d64s[i].Round(buf, 0, types.ModeHalfEven); err != nil { 393 return err 394 } 395 d64s[i] = *buf 396 } 397 return nil 398 } 399 400 func (b *builtinRoundDecSig) vectorized() bool { 401 return true 402 } 403 404 func (b *builtinPowSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 405 n := input.NumEvents() 406 buf1, err := b.bufSlabPredictor.get(types.ETReal, n) 407 if err != nil { 408 return err 409 } 410 defer b.bufSlabPredictor.put(buf1) 411 if err := b.args[0].VecEvalReal(b.ctx, input, buf1); err != nil { 412 return err 413 } 414 415 if err := b.args[1].VecEvalReal(b.ctx, input, result); err != nil { 416 return err 417 } 418 419 x := buf1.Float64s() 420 y := result.Float64s() 421 result.MergeNulls(buf1) 422 f64s := result.Float64s() 423 for i := 0; i < n; i++ { 424 if result.IsNull(i) { 425 continue 426 } 427 power := math.Pow(x[i], y[i]) 428 if math.IsInf(power, -1) || math.IsInf(power, 1) || math.IsNaN(power) { 429 return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("pow(%s, %s)", strconv.FormatFloat(x[i], 'f', -1, 64), strconv.FormatFloat(y[i], 'f', -1, 64))) 430 } 431 f64s[i] = power 432 } 433 return nil 434 } 435 436 func (b *builtinPowSig) vectorized() bool { 437 return true 438 } 439 440 func (b *builtinFloorRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 441 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 442 return err 443 } 444 f64s := result.Float64s() 445 for i := 0; i < len(f64s); i++ { 446 if result.IsNull(i) { 447 continue 448 } 449 f64s[i] = math.Floor(f64s[i]) 450 } 451 return nil 452 } 453 454 func (b *builtinFloorRealSig) vectorized() bool { 455 return true 456 } 457 458 func (b *builtinLog2ArgsSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 459 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 460 return err 461 } 462 n := input.NumEvents() 463 buf1, err := b.bufSlabPredictor.get(types.ETReal, n) 464 if err != nil { 465 return err 466 } 467 defer b.bufSlabPredictor.put(buf1) 468 if err := b.args[1].VecEvalReal(b.ctx, input, buf1); err != nil { 469 return err 470 } 471 472 d := result.Float64s() 473 x := buf1.Float64s() 474 result.MergeNulls(buf1) 475 for i := 0; i < n; i++ { 476 if result.IsNull(i) { 477 continue 478 } 479 if d[i] <= 0 || d[i] == 1 || x[i] <= 0 { 480 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(ErrInvalidArgumentForLogarithm) 481 result.SetNull(i, true) 482 } 483 d[i] = math.Log(x[i]) / math.Log(d[i]) 484 } 485 return nil 486 } 487 488 func (b *builtinLog2ArgsSig) vectorized() bool { 489 return true 490 } 491 492 func (b *builtinCeilRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 493 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 494 return err 495 } 496 f64s := result.Float64s() 497 for i := 0; i < len(f64s); i++ { 498 if result.IsNull(i) { 499 continue 500 } 501 f64s[i] = math.Ceil(f64s[i]) 502 } 503 return nil 504 } 505 506 func (b *builtinCeilRealSig) vectorized() bool { 507 return true 508 } 509 510 func (b *builtinRoundRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 511 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 512 return err 513 } 514 f64s := result.Float64s() 515 for i := 0; i < len(f64s); i++ { 516 if result.IsNull(i) { 517 continue 518 } 519 f64s[i] = types.Round(f64s[i], 0) 520 } 521 return nil 522 } 523 524 func (b *builtinRoundRealSig) vectorized() bool { 525 return true 526 } 527 528 func (b *builtinRoundWithFracRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 529 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 530 return err 531 } 532 n := input.NumEvents() 533 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 534 if err != nil { 535 return err 536 } 537 defer b.bufSlabPredictor.put(buf1) 538 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 539 return err 540 } 541 542 x := result.Float64s() 543 d := buf1.Int64s() 544 result.MergeNulls(buf1) 545 for i := 0; i < n; i++ { 546 if result.IsNull(i) { 547 continue 548 } 549 x[i] = types.Round(x[i], int(d[i])) 550 } 551 return nil 552 } 553 554 func (b *builtinRoundWithFracRealSig) vectorized() bool { 555 return true 556 } 557 558 func (b *builtinTruncateRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 559 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 560 return err 561 } 562 n := input.NumEvents() 563 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 564 if err != nil { 565 return err 566 } 567 defer b.bufSlabPredictor.put(buf1) 568 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 569 return err 570 } 571 572 result.MergeNulls(buf1) 573 x := result.Float64s() 574 d := buf1.Int64s() 575 for i := 0; i < n; i++ { 576 if result.IsNull(i) { 577 continue 578 } 579 x[i] = types.Truncate(x[i], int(d[i])) 580 } 581 return nil 582 } 583 584 func (b *builtinTruncateRealSig) vectorized() bool { 585 return true 586 } 587 588 func (b *builtinAbsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 589 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 590 return err 591 } 592 f64s := result.Float64s() 593 for i := 0; i < len(f64s); i++ { 594 f64s[i] = math.Abs(f64s[i]) 595 } 596 return nil 597 } 598 599 func (b *builtinAbsRealSig) vectorized() bool { 600 return true 601 } 602 603 func (b *builtinAbsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 604 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 605 return err 606 } 607 i64s := result.Int64s() 608 for i := 0; i < len(i64s); i++ { 609 if result.IsNull(i) { 610 continue 611 } 612 if i64s[i] == math.MinInt64 { 613 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("abs(%d)", i64s[i])) 614 } 615 if i64s[i] < 0 { 616 i64s[i] = -i64s[i] 617 } 618 } 619 return nil 620 } 621 622 func (b *builtinAbsIntSig) vectorized() bool { 623 return true 624 } 625 626 func (b *builtinRoundIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 627 return b.args[0].VecEvalInt(b.ctx, input, result) 628 } 629 630 func (b *builtinRoundIntSig) vectorized() bool { 631 return true 632 } 633 634 func (b *builtinRoundWithFracIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 635 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 636 return err 637 } 638 639 n := input.NumEvents() 640 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 641 if err != nil { 642 return err 643 } 644 defer b.bufSlabPredictor.put(buf) 645 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 646 return err 647 } 648 649 i64s := result.Int64s() 650 frac := buf.Int64s() 651 result.MergeNulls(buf) 652 for i := 0; i < n; i++ { 653 if result.IsNull(i) { 654 continue 655 } 656 i64s[i] = int64(types.Round(float64(i64s[i]), int(frac[i]))) 657 } 658 return nil 659 } 660 661 func (b *builtinRoundWithFracIntSig) vectorized() bool { 662 return true 663 } 664 func (b *builtinCRC32Sig) vectorized() bool { 665 return true 666 } 667 668 func (b *builtinCRC32Sig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 669 n := input.NumEvents() 670 buf, err := b.bufSlabPredictor.get(types.ETString, n) 671 if err != nil { 672 return err 673 } 674 defer b.bufSlabPredictor.put(buf) 675 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 676 return err 677 } 678 result.ResizeInt64(n, false) 679 i64s := result.Int64s() 680 result.MergeNulls(buf) 681 for i := range i64s { 682 if !buf.IsNull(i) { 683 i64s[i] = int64(crc32.ChecksumIEEE(buf.GetBytes(i))) 684 } 685 } 686 return nil 687 } 688 689 func (b *builtinPISig) vectorized() bool { 690 return true 691 } 692 693 func (b *builtinPISig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 694 n := input.NumEvents() 695 result.ResizeFloat64(n, false) 696 f64s := result.Float64s() 697 for i := range f64s { 698 f64s[i] = math.Pi 699 } 700 return nil 701 } 702 703 func (b *builtinRandSig) vectorized() bool { 704 return true 705 } 706 707 func (b *builtinRandSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 708 n := input.NumEvents() 709 result.ResizeFloat64(n, false) 710 f64s := result.Float64s() 711 b.mu.Lock() 712 for i := range f64s { 713 f64s[i] = b.mysqlRng.Gen() 714 } 715 b.mu.Unlock() 716 return nil 717 } 718 719 func (b *builtinRandWithSeedFirstGenSig) vectorized() bool { 720 return true 721 } 722 723 func (b *builtinRandWithSeedFirstGenSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 724 n := input.NumEvents() 725 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 726 if err != nil { 727 return err 728 } 729 defer b.bufSlabPredictor.put(buf) 730 if err = b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 731 return err 732 } 733 734 result.ResizeFloat64(n, false) 735 i64s := buf.Int64s() 736 f64s := result.Float64s() 737 for i := 0; i < n; i++ { 738 // When the seed is null we need to use 0 as the seed. 739 // The behavior same as MyALLEGROSQL. 740 rng := NewWithSeed(0) 741 if !buf.IsNull(i) { 742 rng = NewWithSeed(i64s[i]) 743 } 744 f64s[i] = rng.Gen() 745 } 746 return nil 747 } 748 749 func (b *builtinCeilIntToDecSig) vectorized() bool { 750 return true 751 } 752 753 func (b *builtinCeilIntToDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 754 n := input.NumEvents() 755 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 756 if err != nil { 757 return err 758 } 759 defer b.bufSlabPredictor.put(buf) 760 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 761 return err 762 } 763 764 result.ResizeDecimal(n, false) 765 result.MergeNulls(buf) 766 767 i64s := buf.Int64s() 768 d := result.Decimals() 769 isUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 770 for i := 0; i < n; i++ { 771 if result.IsNull(i) { 772 continue 773 } 774 if isUnsigned || i64s[i] >= 0 { 775 d[i] = *types.NewDecFromUint(uint64(i64s[i])) 776 continue 777 } 778 d[i] = *types.NewDecFromInt(i64s[i]) 779 } 780 return nil 781 } 782 783 func (b *builtinTruncateIntSig) vectorized() bool { 784 return true 785 } 786 787 func (b *builtinTruncateIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 788 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 789 return err 790 } 791 792 n := input.NumEvents() 793 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 794 if err != nil { 795 return err 796 } 797 defer b.bufSlabPredictor.put(buf) 798 799 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 800 return err 801 } 802 result.MergeNulls(buf) 803 i64s := result.Int64s() 804 buf64s := buf.Int64s() 805 806 if allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) { 807 return nil 808 } 809 810 for i := 0; i < len(i64s); i++ { 811 if result.IsNull(i) { 812 continue 813 } 814 if buf64s[i] < 0 { 815 // -MinInt = MinInt, special case 816 if buf64s[i] == mathutil.MinInt { 817 i64s[i] = 0 818 } else { 819 shift := int64(math.Pow10(int(-buf64s[i]))) 820 i64s[i] = i64s[i] / shift * shift 821 } 822 } 823 } 824 return nil 825 } 826 827 func (b *builtinTruncateUintSig) vectorized() bool { 828 return true 829 } 830 831 func (b *builtinTruncateUintSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 832 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 833 return err 834 } 835 836 n := input.NumEvents() 837 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 838 if err != nil { 839 return err 840 } 841 defer b.bufSlabPredictor.put(buf) 842 843 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 844 return err 845 } 846 result.MergeNulls(buf) 847 i64s := result.Int64s() 848 buf64s := buf.Int64s() 849 850 if allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) { 851 return nil 852 } 853 854 for i := 0; i < n; i++ { 855 if result.IsNull(i) { 856 continue 857 } 858 859 if buf64s[i] < 0 { 860 // -MinInt = MinInt, special case 861 if buf64s[i] == mathutil.MinInt { 862 i64s[i] = 0 863 } else { 864 shift := uint64(math.Pow10(int(-buf64s[i]))) 865 i64s[i] = int64(uint64(i64s[i]) / shift * shift) 866 } 867 } 868 } 869 return nil 870 } 871 872 func (b *builtinCeilDecToDecSig) vectorized() bool { 873 return true 874 } 875 876 func (b *builtinCeilDecToDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 877 n := input.NumEvents() 878 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 879 return err 880 } 881 ds := result.Decimals() 882 883 for i := 0; i < n; i++ { 884 if result.IsNull(i) { 885 continue 886 } 887 rst := new(types.MyDecimal) 888 if err := ds[i].Round(rst, 0, types.ModeTruncate); err != nil { 889 return err 890 } 891 if !ds[i].IsNegative() && rst.Compare(&ds[i]) != 0 { 892 if err := types.DecimalAdd(rst, types.NewDecFromInt(1), rst); err != nil { 893 return err 894 } 895 } 896 ds[i] = *rst 897 } 898 return nil 899 } 900 901 func (b *builtinFloorDecToDecSig) vectorized() bool { 902 return true 903 } 904 905 func (b *builtinFloorDecToDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 906 n := input.NumEvents() 907 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 908 return err 909 } 910 ds := result.Decimals() 911 912 for i := 0; i < n; i++ { 913 if result.IsNull(i) { 914 continue 915 } 916 rst := new(types.MyDecimal) 917 if !ds[i].IsNegative() { 918 if err := ds[i].Round(rst, 0, types.ModeTruncate); err != nil { 919 return err 920 } 921 } else { 922 if err := ds[i].Round(rst, 0, types.ModeTruncate); err != nil { 923 return err 924 } 925 if rst.Compare(&ds[i]) != 0 { 926 if err := types.DecimalSub(rst, types.NewDecFromInt(1), rst); err != nil { 927 return err 928 } 929 } 930 } 931 ds[i] = *rst 932 } 933 return nil 934 } 935 936 func (b *builtinTruncateDecimalSig) vectorized() bool { 937 return true 938 } 939 940 func (b *builtinTruncateDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 941 n := input.NumEvents() 942 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 943 return err 944 } 945 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 946 if err != nil { 947 return err 948 } 949 defer b.bufSlabPredictor.put(buf) 950 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 951 return err 952 } 953 result.MergeNulls(buf) 954 ds := result.Decimals() 955 i64s := buf.Int64s() 956 ft := b.getRetTp().Decimal 957 for i := 0; i < n; i++ { 958 if result.IsNull(i) { 959 continue 960 } 961 result := new(types.MyDecimal) 962 if err := ds[i].Round(result, mathutil.Min(int(i64s[i]), ft), types.ModeTruncate); err != nil { 963 return err 964 } 965 ds[i] = *result 966 } 967 return nil 968 } 969 970 func (b *builtinRoundWithFracDecSig) vectorized() bool { 971 return true 972 } 973 974 func (b *builtinRoundWithFracDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 975 n := input.NumEvents() 976 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 977 return err 978 } 979 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 980 if err != nil { 981 return err 982 } 983 defer b.bufSlabPredictor.put(buf) 984 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 985 return err 986 } 987 988 result.MergeNulls(buf) 989 tmp := new(types.MyDecimal) 990 d64s := result.Decimals() 991 i64s := buf.Int64s() 992 for i := 0; i < n; i++ { 993 if result.IsNull(i) { 994 continue 995 } 996 // TODO: reuse d64[i] and remove the temporary variable tmp. 997 if err := d64s[i].Round(tmp, mathutil.Min(int(i64s[i]), b.tp.Decimal), types.ModeHalfEven); err != nil { 998 return err 999 } 1000 d64s[i] = *tmp 1001 } 1002 return nil 1003 } 1004 1005 func (b *builtinFloorIntToDecSig) vectorized() bool { 1006 return true 1007 } 1008 1009 func (b *builtinFloorIntToDecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1010 n := input.NumEvents() 1011 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 1012 if err != nil { 1013 return err 1014 } 1015 defer b.bufSlabPredictor.put(buf) 1016 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 1017 return err 1018 } 1019 1020 result.ResizeDecimal(n, false) 1021 result.MergeNulls(buf) 1022 1023 i64s := buf.Int64s() 1024 d := result.Decimals() 1025 isUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 1026 for i := 0; i < n; i++ { 1027 if result.IsNull(i) { 1028 continue 1029 } 1030 if isUnsigned || i64s[i] >= 0 { 1031 d[i] = *types.NewDecFromUint(uint64(i64s[i])) 1032 continue 1033 } 1034 d[i] = *types.NewDecFromInt(i64s[i]) 1035 } 1036 return nil 1037 } 1038 1039 func (b *builtinSignSig) vectorized() bool { 1040 return true 1041 } 1042 1043 func (b *builtinSignSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1044 n := input.NumEvents() 1045 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 1046 if err != nil { 1047 return err 1048 } 1049 defer b.bufSlabPredictor.put(buf) 1050 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 1051 return err 1052 } 1053 args := buf.Float64s() 1054 result.ResizeInt64(n, false) 1055 result.MergeNulls(buf) 1056 i64s := result.Int64s() 1057 for i := 0; i < len(i64s); i++ { 1058 if result.IsNull(i) { 1059 continue 1060 } 1061 if args[i] > 0 { 1062 i64s[i] = 1 1063 } else if args[i] < 0 { 1064 i64s[i] = -1 1065 } else { 1066 i64s[i] = 0 1067 } 1068 } 1069 return nil 1070 } 1071 1072 func (b *builtinConvSig) vectorized() bool { 1073 return true 1074 } 1075 1076 func (b *builtinConvSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1077 n := input.NumEvents() 1078 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 1079 if err != nil { 1080 return err 1081 } 1082 defer b.bufSlabPredictor.put(buf1) 1083 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1084 if err != nil { 1085 return err 1086 } 1087 defer b.bufSlabPredictor.put(buf2) 1088 buf3, err := b.bufSlabPredictor.get(types.ETInt, n) 1089 if err != nil { 1090 return err 1091 } 1092 defer b.bufSlabPredictor.put(buf3) 1093 if err := b.args[0].VecEvalString(b.ctx, input, buf1); err != nil { 1094 return err 1095 } 1096 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 1097 return err 1098 } 1099 if err := b.args[2].VecEvalInt(b.ctx, input, buf3); err != nil { 1100 return err 1101 } 1102 result.ReserveString(n) 1103 fromBase := buf2.Int64s() 1104 toBase := buf3.Int64s() 1105 for i := 0; i < n; i++ { 1106 if buf1.IsNull(i) || buf2.IsNull(i) || buf3.IsNull(i) { 1107 result.AppendNull() 1108 continue 1109 } 1110 res, isNull, err := b.conv(buf1.GetString(i), fromBase[i], toBase[i]) 1111 if err != nil { 1112 return err 1113 } 1114 if isNull { 1115 result.AppendNull() 1116 continue 1117 } 1118 result.AppendString(res) 1119 } 1120 return nil 1121 } 1122 1123 func (b *builtinAbsUIntSig) vectorized() bool { 1124 return true 1125 } 1126 1127 func (b *builtinAbsUIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1128 return b.args[0].VecEvalInt(b.ctx, input, result) 1129 } 1130 1131 func (b *builtinCeilDecToIntSig) vectorized() bool { 1132 return true 1133 } 1134 1135 func (b *builtinCeilDecToIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1136 n := input.NumEvents() 1137 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1138 if err != nil { 1139 return err 1140 } 1141 defer b.bufSlabPredictor.put(buf) 1142 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1143 return err 1144 } 1145 result.ResizeInt64(n, false) 1146 result.MergeNulls(buf) 1147 1148 d := buf.Decimals() 1149 i64s := result.Int64s() 1150 for i := 0; i < n; i++ { 1151 if result.IsNull(i) { 1152 continue 1153 } 1154 i64s[i], err = d[i].ToInt() 1155 if err == types.ErrTruncated { 1156 err = nil 1157 if !d[i].IsNegative() { 1158 i64s[i] = i64s[i] + 1 1159 } 1160 } 1161 if err != nil { 1162 return err 1163 } 1164 } 1165 1166 return nil 1167 } 1168 1169 func (b *builtinCeilIntToIntSig) vectorized() bool { 1170 return true 1171 } 1172 1173 func (b *builtinCeilIntToIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1174 return b.args[0].VecEvalInt(b.ctx, input, result) 1175 } 1176 1177 func (b *builtinFloorIntToIntSig) vectorized() bool { 1178 return true 1179 } 1180 1181 func (b *builtinFloorIntToIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1182 return b.args[0].VecEvalInt(b.ctx, input, result) 1183 } 1184 1185 func (b *builtinFloorDecToIntSig) vectorized() bool { 1186 return true 1187 } 1188 1189 func (b *builtinFloorDecToIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1190 n := input.NumEvents() 1191 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1192 if err != nil { 1193 return err 1194 } 1195 defer b.bufSlabPredictor.put(buf) 1196 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1197 return err 1198 } 1199 result.ResizeInt64(n, false) 1200 result.MergeNulls(buf) 1201 1202 d := buf.Decimals() 1203 i64s := result.Int64s() 1204 for i := 0; i < n; i++ { 1205 if result.IsNull(i) { 1206 continue 1207 } 1208 i64s[i], err = d[i].ToInt() 1209 if err == types.ErrTruncated { 1210 err = nil 1211 if d[i].IsNegative() { 1212 i64s[i]-- 1213 } 1214 } 1215 if err != nil { 1216 return err 1217 } 1218 } 1219 return nil 1220 }