github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_arithmetic_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 "math" 19 20 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 21 "github.com/whtcorpsinc/BerolinaSQL/terror" 22 "github.com/whtcorpsinc/milevadb/types" 23 "github.com/whtcorpsinc/milevadb/soliton/chunk" 24 ) 25 26 func (b *builtinArithmeticMultiplyRealSig) vectorized() bool { 27 return true 28 } 29 30 func (b *builtinArithmeticMultiplyRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 31 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 32 return err 33 } 34 n := input.NumEvents() 35 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 36 if err != nil { 37 return err 38 } 39 defer b.bufSlabPredictor.put(buf) 40 if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil { 41 return err 42 } 43 44 result.MergeNulls(buf) 45 x := result.Float64s() 46 y := buf.Float64s() 47 for i := 0; i < n; i++ { 48 if result.IsNull(i) { 49 continue 50 } 51 x[i] = x[i] * y[i] 52 if math.IsInf(x[i], 0) { 53 return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s * %s)", b.args[0].String(), b.args[1].String())) 54 } 55 } 56 return nil 57 } 58 59 func (b *builtinArithmeticDivideDecimalSig) vectorized() bool { 60 return true 61 } 62 63 func (b *builtinArithmeticDivideDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 64 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 65 return err 66 } 67 n := input.NumEvents() 68 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 69 if err != nil { 70 return err 71 } 72 defer b.bufSlabPredictor.put(buf) 73 if err := b.args[1].VecEvalDecimal(b.ctx, input, buf); err != nil { 74 return err 75 } 76 77 result.MergeNulls(buf) 78 x := result.Decimals() 79 y := buf.Decimals() 80 var to types.MyDecimal 81 var frac int 82 sc := b.ctx.GetStochastikVars().StmtCtx 83 for i := 0; i < n; i++ { 84 if result.IsNull(i) { 85 continue 86 } 87 err = types.DecimalDiv(&x[i], &y[i], &to, types.DivFracIncr) 88 if err == types.ErrDivByZero { 89 if err = handleDivisionByZeroError(b.ctx); err != nil { 90 return err 91 } 92 result.SetNull(i, true) 93 continue 94 } else if err == types.ErrTruncated { 95 if err = sc.HandleTruncate(errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", to)); err != nil { 96 return err 97 } 98 } else if err == nil { 99 _, frac = to.PrecisionAndFrac() 100 if frac < b.baseBuiltinFunc.tp.Decimal { 101 if err = to.Round(&to, b.baseBuiltinFunc.tp.Decimal, types.ModeHalfEven); err != nil { 102 return err 103 } 104 } 105 } else { 106 return err 107 } 108 x[i] = to 109 } 110 return nil 111 } 112 113 func (b *builtinArithmeticModIntSig) vectorized() bool { 114 return true 115 } 116 117 func (b *builtinArithmeticModIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 118 n := input.NumEvents() 119 lh, err := b.bufSlabPredictor.get(types.ETInt, n) 120 if err != nil { 121 return err 122 } 123 defer b.bufSlabPredictor.put(lh) 124 125 if err := b.args[0].VecEvalInt(b.ctx, input, lh); err != nil { 126 return err 127 } 128 // reuse result as rh to avoid buf allocate 129 if err := b.args[1].VecEvalInt(b.ctx, input, result); err != nil { 130 return err 131 } 132 133 isLHSUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 134 isRHSUnsigned := allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) 135 136 rh := result 137 switch { 138 case isLHSUnsigned && isRHSUnsigned: 139 err = b.modUU(lh, rh) 140 case isLHSUnsigned && !isRHSUnsigned: 141 err = b.modUS(lh, rh) 142 case !isLHSUnsigned && isRHSUnsigned: 143 err = b.modSU(lh, rh) 144 case !isLHSUnsigned && !isRHSUnsigned: 145 err = b.modSS(lh, rh) 146 } 147 return err 148 } 149 func (b *builtinArithmeticModIntSig) modUU(lh, rh *chunk.DeferredCauset) error { 150 lhi64s := lh.Int64s() 151 rhi64s := rh.Int64s() 152 153 for i := 0; i < len(lhi64s); i++ { 154 if rh.IsNull(i) { 155 continue 156 } 157 if rhi64s[i] == 0 { 158 if err := handleDivisionByZeroError(b.ctx); err != nil { 159 return err 160 } 161 rh.SetNull(i, true) 162 continue 163 } 164 if lh.IsNull(i) { 165 rh.SetNull(i, true) 166 continue 167 } 168 lhVar, rhVar := lhi64s[i], rhi64s[i] 169 rhi64s[i] = int64(uint64(lhVar) % uint64(rhVar)) 170 } 171 return nil 172 } 173 func (b *builtinArithmeticModIntSig) modUS(lh, rh *chunk.DeferredCauset) error { 174 lhi64s := lh.Int64s() 175 rhi64s := rh.Int64s() 176 177 for i := 0; i < len(lhi64s); i++ { 178 if rh.IsNull(i) { 179 continue 180 } 181 if rhi64s[i] == 0 { 182 if err := handleDivisionByZeroError(b.ctx); err != nil { 183 return err 184 } 185 rh.SetNull(i, true) 186 continue 187 } 188 if lh.IsNull(i) { 189 rh.SetNull(i, true) 190 continue 191 } 192 lhVar, rhVar := lhi64s[i], rhi64s[i] 193 if rhVar < 0 { 194 rhi64s[i] = int64(uint64(lhVar) % uint64(-rhVar)) 195 } else { 196 rhi64s[i] = int64(uint64(lhVar) % uint64(rhVar)) 197 } 198 } 199 return nil 200 } 201 func (b *builtinArithmeticModIntSig) modSU(lh, rh *chunk.DeferredCauset) error { 202 lhi64s := lh.Int64s() 203 rhi64s := rh.Int64s() 204 205 for i := 0; i < len(lhi64s); i++ { 206 if rh.IsNull(i) { 207 continue 208 } 209 if rhi64s[i] == 0 { 210 if err := handleDivisionByZeroError(b.ctx); err != nil { 211 return err 212 } 213 rh.SetNull(i, true) 214 continue 215 } 216 if lh.IsNull(i) { 217 rh.SetNull(i, true) 218 continue 219 } 220 lhVar, rhVar := lhi64s[i], rhi64s[i] 221 if lhVar < 0 { 222 rhi64s[i] = -int64(uint64(-lhVar) % uint64(rhVar)) 223 } else { 224 rhi64s[i] = int64(uint64(lhVar) % uint64(rhVar)) 225 } 226 } 227 return nil 228 } 229 func (b *builtinArithmeticModIntSig) modSS(lh, rh *chunk.DeferredCauset) error { 230 lhi64s := lh.Int64s() 231 rhi64s := rh.Int64s() 232 233 for i := 0; i < len(lhi64s); i++ { 234 if rh.IsNull(i) { 235 continue 236 } 237 if rhi64s[i] == 0 { 238 if err := handleDivisionByZeroError(b.ctx); err != nil { 239 return err 240 } 241 rh.SetNull(i, true) 242 continue 243 } 244 if lh.IsNull(i) { 245 rh.SetNull(i, true) 246 continue 247 } 248 lhVar, rhVar := lhi64s[i], rhi64s[i] 249 rhi64s[i] = lhVar % rhVar 250 } 251 return nil 252 } 253 254 func (b *builtinArithmeticMinusRealSig) vectorized() bool { 255 return true 256 } 257 258 func (b *builtinArithmeticMinusRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 259 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 260 return err 261 } 262 n := input.NumEvents() 263 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 264 if err != nil { 265 return err 266 } 267 defer b.bufSlabPredictor.put(buf) 268 if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil { 269 return err 270 } 271 272 result.MergeNulls(buf) 273 x := result.Float64s() 274 y := buf.Float64s() 275 for i := 0; i < n; i++ { 276 if result.IsNull(i) { 277 continue 278 } 279 if (x[i] > 0 && -y[i] > math.MaxFloat64-x[i]) || (x[i] < 0 && -y[i] < -math.MaxFloat64-x[i]) { 280 return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 281 } 282 x[i] = x[i] - y[i] 283 } 284 return nil 285 } 286 287 func (b *builtinArithmeticMinusDecimalSig) vectorized() bool { 288 return true 289 } 290 291 func (b *builtinArithmeticMinusDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 292 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 293 return err 294 } 295 n := input.NumEvents() 296 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 297 if err != nil { 298 return err 299 } 300 defer b.bufSlabPredictor.put(buf) 301 if err := b.args[1].VecEvalDecimal(b.ctx, input, buf); err != nil { 302 return err 303 } 304 305 result.MergeNulls(buf) 306 x := result.Decimals() 307 y := buf.Decimals() 308 var to types.MyDecimal 309 for i := 0; i < n; i++ { 310 if result.IsNull(i) { 311 continue 312 } 313 if err = types.DecimalSub(&x[i], &y[i], &to); err != nil { 314 return err 315 } 316 x[i] = to 317 } 318 return nil 319 } 320 321 func (b *builtinArithmeticMinusIntSig) vectorized() bool { 322 return true 323 } 324 325 func (b *builtinArithmeticMinusIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 326 n := input.NumEvents() 327 lh, err := b.bufSlabPredictor.get(types.ETInt, n) 328 if err != nil { 329 return err 330 } 331 defer b.bufSlabPredictor.put(lh) 332 333 if err := b.args[0].VecEvalInt(b.ctx, input, lh); err != nil { 334 return err 335 } 336 337 if err := b.args[1].VecEvalInt(b.ctx, input, result); err != nil { 338 return err 339 } 340 341 result.MergeNulls(lh) 342 343 rh := result 344 lhi64s := lh.Int64s() 345 rhi64s := rh.Int64s() 346 resulti64s := result.Int64s() 347 348 forceToSigned := b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoUnsignedSubtractionMode() 349 isLHSUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 350 isRHSUnsigned := allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) 351 352 switch { 353 case forceToSigned && isLHSUnsigned && isRHSUnsigned: 354 err = b.minusFUU(result, lhi64s, rhi64s, resulti64s) 355 case forceToSigned && isLHSUnsigned && !isRHSUnsigned: 356 err = b.minusFUS(result, lhi64s, rhi64s, resulti64s) 357 case forceToSigned && !isLHSUnsigned && isRHSUnsigned: 358 err = b.minusFSU(result, lhi64s, rhi64s, resulti64s) 359 case forceToSigned && !isLHSUnsigned && !isRHSUnsigned: 360 err = b.minusSS(result, lhi64s, rhi64s, resulti64s) 361 case !forceToSigned && isLHSUnsigned && isRHSUnsigned: 362 err = b.minusUU(result, lhi64s, rhi64s, resulti64s) 363 case !forceToSigned && isLHSUnsigned && !isRHSUnsigned: 364 err = b.minusUS(result, lhi64s, rhi64s, resulti64s) 365 case !forceToSigned && !isLHSUnsigned && isRHSUnsigned: 366 err = b.minusSU(result, lhi64s, rhi64s, resulti64s) 367 case !forceToSigned && !isLHSUnsigned && !isRHSUnsigned: 368 err = b.minusSS(result, lhi64s, rhi64s, resulti64s) 369 } 370 return err 371 } 372 func (b *builtinArithmeticMinusIntSig) minusFUU(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 373 for i := 0; i < len(lhi64s); i++ { 374 if result.IsNull(i) { 375 continue 376 } 377 lh, rh := lhi64s[i], rhi64s[i] 378 379 if lh < 0 { 380 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 381 } 382 383 if rh < 0 { 384 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 385 } 386 387 if (lh > 0 && -rh > math.MaxInt64-lh) || (lh < 0 && -rh < math.MinInt64-lh) { 388 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 389 } 390 391 resulti64s[i] = lh - rh 392 } 393 return nil 394 } 395 396 func (b *builtinArithmeticMinusIntSig) minusFUS(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 397 for i := 0; i < len(lhi64s); i++ { 398 if result.IsNull(i) { 399 continue 400 } 401 lh, rh := lhi64s[i], rhi64s[i] 402 403 if lh < 0 { 404 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 405 } 406 407 if (lh > 0 && -rh > math.MaxInt64-lh) || (lh < 0 && -rh < math.MinInt64-lh) { 408 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 409 } 410 411 resulti64s[i] = lh - rh 412 } 413 return nil 414 } 415 416 func (b *builtinArithmeticMinusIntSig) minusFSU(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 417 for i := 0; i < len(lhi64s); i++ { 418 if result.IsNull(i) { 419 continue 420 } 421 lh, rh := lhi64s[i], rhi64s[i] 422 423 if rh < 0 { 424 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 425 } 426 427 if (lh > 0 && -rh > math.MaxInt64-lh) || (lh < 0 && -rh < math.MinInt64-lh) { 428 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 429 } 430 431 resulti64s[i] = lh - rh 432 } 433 return nil 434 } 435 func (b *builtinArithmeticMinusIntSig) minusUU(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 436 for i := 0; i < len(lhi64s); i++ { 437 if result.IsNull(i) { 438 continue 439 } 440 lh, rh := lhi64s[i], rhi64s[i] 441 442 if uint64(lh) < uint64(rh) { 443 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 444 } 445 446 resulti64s[i] = lh - rh 447 } 448 return nil 449 } 450 451 func (b *builtinArithmeticMinusIntSig) minusUS(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 452 for i := 0; i < len(lhi64s); i++ { 453 if result.IsNull(i) { 454 continue 455 } 456 lh, rh := lhi64s[i], rhi64s[i] 457 458 if rh >= 0 && uint64(lh) < uint64(rh) { 459 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 460 } 461 if rh < 0 && uint64(lh) > math.MaxUint64-uint64(-rh) { 462 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 463 } 464 465 resulti64s[i] = lh - rh 466 } 467 return nil 468 } 469 470 func (b *builtinArithmeticMinusIntSig) minusSU(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 471 for i := 0; i < len(lhi64s); i++ { 472 if result.IsNull(i) { 473 continue 474 } 475 lh, rh := lhi64s[i], rhi64s[i] 476 477 if uint64(lh-math.MinInt64) < uint64(rh) { 478 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 479 } 480 481 resulti64s[i] = lh - rh 482 } 483 return nil 484 } 485 func (b *builtinArithmeticMinusIntSig) minusSS(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 486 for i := 0; i < len(lhi64s); i++ { 487 if result.IsNull(i) { 488 continue 489 } 490 lh, rh := lhi64s[i], rhi64s[i] 491 492 if (lh > 0 && -rh > math.MaxInt64-lh) || (lh < 0 && -rh < math.MinInt64-lh) { 493 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s - %s)", b.args[0].String(), b.args[1].String())) 494 } 495 496 resulti64s[i] = lh - rh 497 } 498 return nil 499 } 500 501 func (b *builtinArithmeticModRealSig) vectorized() bool { 502 return true 503 } 504 505 func (b *builtinArithmeticModRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 506 n := input.NumEvents() 507 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 508 if err != nil { 509 return err 510 } 511 defer b.bufSlabPredictor.put(buf) 512 if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil { 513 return err 514 } 515 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 516 return err 517 } 518 x := result.Float64s() 519 y := buf.Float64s() 520 for i := 0; i < n; i++ { 521 if buf.IsNull(i) { 522 result.SetNull(i, true) 523 continue 524 } 525 if y[i] == 0 { 526 if err := handleDivisionByZeroError(b.ctx); err != nil { 527 return err 528 } 529 result.SetNull(i, true) 530 continue 531 } 532 if result.IsNull(i) { 533 continue 534 } 535 536 x[i] = math.Mod(x[i], y[i]) 537 } 538 return nil 539 } 540 541 func (b *builtinArithmeticModDecimalSig) vectorized() bool { 542 return true 543 } 544 545 func (b *builtinArithmeticModDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 546 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 547 return err 548 } 549 n := input.NumEvents() 550 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 551 if err != nil { 552 return err 553 } 554 defer b.bufSlabPredictor.put(buf) 555 if err := b.args[1].VecEvalDecimal(b.ctx, input, buf); err != nil { 556 return err 557 } 558 559 result.MergeNulls(buf) 560 x := result.Decimals() 561 y := buf.Decimals() 562 var to types.MyDecimal 563 for i := 0; i < n; i++ { 564 if result.IsNull(i) { 565 continue 566 } 567 err = types.DecimalMod(&x[i], &y[i], &to) 568 if err == types.ErrDivByZero { 569 if err := handleDivisionByZeroError(b.ctx); err != nil { 570 return err 571 } 572 result.SetNull(i, true) 573 continue 574 } else if err != nil { 575 return err 576 } 577 578 x[i] = to 579 } 580 return nil 581 } 582 583 func (b *builtinArithmeticPlusRealSig) vectorized() bool { 584 return true 585 } 586 587 func (b *builtinArithmeticPlusRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 588 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 589 return err 590 } 591 n := input.NumEvents() 592 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 593 if err != nil { 594 return err 595 } 596 defer b.bufSlabPredictor.put(buf) 597 if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil { 598 return err 599 } 600 601 result.MergeNulls(buf) 602 x := result.Float64s() 603 y := buf.Float64s() 604 for i := 0; i < n; i++ { 605 if result.IsNull(i) { 606 continue 607 } 608 if (x[i] > 0 && y[i] > math.MaxFloat64-x[i]) || (x[i] < 0 && y[i] < -math.MaxFloat64-x[i]) { 609 return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 610 } 611 x[i] = x[i] + y[i] 612 } 613 return nil 614 } 615 616 func (b *builtinArithmeticMultiplyDecimalSig) vectorized() bool { 617 return true 618 } 619 620 func (b *builtinArithmeticMultiplyDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 621 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 622 return err 623 } 624 n := input.NumEvents() 625 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 626 if err != nil { 627 return err 628 } 629 defer b.bufSlabPredictor.put(buf) 630 if err := b.args[1].VecEvalDecimal(b.ctx, input, buf); err != nil { 631 return err 632 } 633 634 result.MergeNulls(buf) 635 x := result.Decimals() 636 y := buf.Decimals() 637 var to types.MyDecimal 638 for i := 0; i < n; i++ { 639 if result.IsNull(i) { 640 continue 641 } 642 err = types.DecimalMul(&x[i], &y[i], &to) 643 if err != nil && !terror.ErrorEqual(err, types.ErrTruncated) { 644 return err 645 } 646 x[i] = to 647 } 648 return nil 649 } 650 651 func (b *builtinArithmeticIntDivideDecimalSig) vectorized() bool { 652 return true 653 } 654 655 func (b *builtinArithmeticIntDivideDecimalSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 656 sc := b.ctx.GetStochastikVars().StmtCtx 657 n := input.NumEvents() 658 var err error 659 var buf [2]*chunk.DeferredCauset 660 var num [2][]types.MyDecimal 661 for i, arg := range b.args { 662 if buf[i], err = b.bufSlabPredictor.get(types.ETDecimal, n); err != nil { 663 return err 664 } 665 defer b.bufSlabPredictor.put(buf[i]) 666 667 err = arg.VecEvalDecimal(b.ctx, input, buf[i]) 668 if err != nil { 669 return err 670 } 671 672 num[i] = buf[i].Decimals() 673 } 674 675 isLHSUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 676 isRHSUnsigned := allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) 677 isUnsigned := isLHSUnsigned || isRHSUnsigned 678 679 result.ResizeInt64(n, false) 680 result.MergeNulls(buf[0], buf[1]) 681 i64s := result.Int64s() 682 for i := 0; i < n; i++ { 683 if result.IsNull(i) { 684 continue 685 } 686 687 c := &types.MyDecimal{} 688 err = types.DecimalDiv(&num[0][i], &num[1][i], c, types.DivFracIncr) 689 if err == types.ErrDivByZero { 690 if err = handleDivisionByZeroError(b.ctx); err != nil { 691 return err 692 } 693 result.SetNull(i, true) 694 continue 695 } 696 if err == types.ErrTruncated { 697 err = sc.HandleTruncate(errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c)) 698 } else if err == types.ErrOverflow { 699 newErr := errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c) 700 err = sc.HandleOverflow(newErr, newErr) 701 } 702 if err != nil { 703 return err 704 } 705 706 if isUnsigned { 707 val, err := c.ToUint() 708 // err returned by ToUint may be ErrTruncated or ErrOverflow, only handle ErrOverflow, ignore ErrTruncated. 709 if err == types.ErrOverflow { 710 v, err := c.ToInt() 711 // when the final result is at (-1, 0], it should be return 0 instead of the error 712 if v == 0 && err == types.ErrTruncated { 713 i64s[i] = int64(0) 714 continue 715 } 716 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s DIV %s)", num[0][i].String(), num[1][i].String())) 717 } 718 i64s[i] = int64(val) 719 } else { 720 i64s[i], err = c.ToInt() 721 // err returned by ToInt may be ErrTruncated or ErrOverflow, only handle ErrOverflow, ignore ErrTruncated. 722 if err == types.ErrOverflow { 723 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s DIV %s)", num[0][i].String(), num[1][i].String())) 724 } 725 } 726 } 727 return nil 728 } 729 730 func (b *builtinArithmeticMultiplyIntSig) vectorized() bool { 731 return true 732 } 733 734 func (b *builtinArithmeticMultiplyIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 735 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 736 return err 737 } 738 n := input.NumEvents() 739 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 740 if err != nil { 741 return err 742 } 743 defer b.bufSlabPredictor.put(buf) 744 745 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 746 return err 747 } 748 749 x := result.Int64s() 750 y := buf.Int64s() 751 result.MergeNulls(buf) 752 var tmp int64 753 for i := 0; i < n; i++ { 754 if result.IsNull(i) { 755 continue 756 } 757 758 tmp = x[i] * y[i] 759 if x[i] != 0 && tmp/x[i] != y[i] { 760 result.SetNull(i, true) 761 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s * %s)", b.args[0].String(), b.args[1].String())) 762 } 763 764 x[i] = tmp 765 } 766 767 return nil 768 } 769 770 func (b *builtinArithmeticDivideRealSig) vectorized() bool { 771 return true 772 } 773 774 func (b *builtinArithmeticDivideRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 775 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 776 return err 777 } 778 n := input.NumEvents() 779 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 780 if err != nil { 781 return err 782 } 783 defer b.bufSlabPredictor.put(buf) 784 if err := b.args[1].VecEvalReal(b.ctx, input, buf); err != nil { 785 return err 786 } 787 788 result.MergeNulls(buf) 789 x := result.Float64s() 790 y := buf.Float64s() 791 for i := 0; i < n; i++ { 792 if result.IsNull(i) { 793 continue 794 } 795 if y[i] == 0 { 796 if err := handleDivisionByZeroError(b.ctx); err != nil { 797 return err 798 } 799 result.SetNull(i, true) 800 continue 801 } 802 803 x[i] = x[i] / y[i] 804 if math.IsInf(x[i], 0) { 805 return types.ErrOverflow.GenWithStackByArgs("DOUBLE", fmt.Sprintf("(%s / %s)", b.args[0].String(), b.args[1].String())) 806 } 807 } 808 return nil 809 } 810 811 func (b *builtinArithmeticIntDivideIntSig) vectorized() bool { 812 return true 813 } 814 815 func (b *builtinArithmeticIntDivideIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 816 n := input.NumEvents() 817 lhsBuf, err := b.bufSlabPredictor.get(types.ETInt, n) 818 if err != nil { 819 return err 820 } 821 defer b.bufSlabPredictor.put(lhsBuf) 822 823 if err := b.args[0].VecEvalInt(b.ctx, input, lhsBuf); err != nil { 824 return err 825 } 826 827 // reuse result as rhsBuf to avoid buf allocate 828 if err := b.args[1].VecEvalInt(b.ctx, input, result); err != nil { 829 return err 830 } 831 832 result.MergeNulls(lhsBuf) 833 834 rhsBuf := result 835 lhsI64s := lhsBuf.Int64s() 836 rhsI64s := rhsBuf.Int64s() 837 resultI64s := result.Int64s() 838 839 isLHSUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 840 isRHSUnsigned := allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) 841 842 switch { 843 case isLHSUnsigned && isRHSUnsigned: 844 err = b.divideUU(result, lhsI64s, rhsI64s, resultI64s) 845 case isLHSUnsigned && !isRHSUnsigned: 846 err = b.divideUS(result, lhsI64s, rhsI64s, resultI64s) 847 case !isLHSUnsigned && isRHSUnsigned: 848 err = b.divideSU(result, lhsI64s, rhsI64s, resultI64s) 849 case !isLHSUnsigned && !isRHSUnsigned: 850 err = b.divideSS(result, lhsI64s, rhsI64s, resultI64s) 851 } 852 return err 853 } 854 855 func (b *builtinArithmeticIntDivideIntSig) divideUU(result *chunk.DeferredCauset, lhsI64s, rhsI64s, resultI64s []int64) error { 856 for i := 0; i < len(lhsI64s); i++ { 857 if result.IsNull(i) { 858 continue 859 } 860 lhs, rhs := lhsI64s[i], rhsI64s[i] 861 862 if rhs == 0 { 863 if err := handleDivisionByZeroError(b.ctx); err != nil { 864 return err 865 } 866 result.SetNull(i, true) 867 } else { 868 resultI64s[i] = int64(uint64(lhs) / uint64(rhs)) 869 } 870 871 } 872 return nil 873 } 874 875 func (b *builtinArithmeticIntDivideIntSig) divideUS(result *chunk.DeferredCauset, lhsI64s, rhsI64s, resultI64s []int64) error { 876 for i := 0; i < len(lhsI64s); i++ { 877 if result.IsNull(i) { 878 continue 879 } 880 lhs, rhs := lhsI64s[i], rhsI64s[i] 881 882 if rhs == 0 { 883 if err := handleDivisionByZeroError(b.ctx); err != nil { 884 return err 885 } 886 result.SetNull(i, true) 887 } else { 888 val, err := types.DivUintWithInt(uint64(lhs), rhs) 889 if err != nil { 890 return err 891 } 892 resultI64s[i] = int64(val) 893 } 894 } 895 return nil 896 } 897 898 func (b *builtinArithmeticIntDivideIntSig) divideSU(result *chunk.DeferredCauset, lhsI64s, rhsI64s, resultI64s []int64) error { 899 for i := 0; i < len(lhsI64s); i++ { 900 if result.IsNull(i) { 901 continue 902 } 903 lhs, rhs := lhsI64s[i], rhsI64s[i] 904 905 if rhs == 0 { 906 if err := handleDivisionByZeroError(b.ctx); err != nil { 907 return err 908 } 909 result.SetNull(i, true) 910 } else { 911 val, err := types.DivIntWithUint(lhs, uint64(rhs)) 912 if err != nil { 913 return err 914 } 915 resultI64s[i] = int64(val) 916 } 917 } 918 return nil 919 } 920 921 func (b *builtinArithmeticIntDivideIntSig) divideSS(result *chunk.DeferredCauset, lhsI64s, rhsI64s, resultI64s []int64) error { 922 for i := 0; i < len(lhsI64s); i++ { 923 if result.IsNull(i) { 924 continue 925 } 926 lhs, rhs := lhsI64s[i], rhsI64s[i] 927 928 if rhs == 0 { 929 if err := handleDivisionByZeroError(b.ctx); err != nil { 930 return err 931 } 932 result.SetNull(i, true) 933 } else { 934 val, err := types.DivInt64(lhs, rhs) 935 if err != nil { 936 return err 937 } 938 resultI64s[i] = val 939 } 940 } 941 return nil 942 } 943 944 func (b *builtinArithmeticPlusIntSig) vectorized() bool { 945 return true 946 } 947 948 func (b *builtinArithmeticPlusIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 949 n := input.NumEvents() 950 lh, err := b.bufSlabPredictor.get(types.ETInt, n) 951 if err != nil { 952 return err 953 } 954 defer b.bufSlabPredictor.put(lh) 955 956 if err := b.args[0].VecEvalInt(b.ctx, input, lh); err != nil { 957 return err 958 } 959 960 // reuse result as rh to avoid buf allocate 961 if err := b.args[1].VecEvalInt(b.ctx, input, result); err != nil { 962 return err 963 } 964 965 result.MergeNulls(lh) 966 967 rh := result 968 lhi64s := lh.Int64s() 969 rhi64s := rh.Int64s() 970 resulti64s := result.Int64s() 971 972 isLHSUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 973 isRHSUnsigned := allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag) 974 975 switch { 976 case isLHSUnsigned && isRHSUnsigned: 977 err = b.plusUU(result, lhi64s, rhi64s, resulti64s) 978 case isLHSUnsigned && !isRHSUnsigned: 979 err = b.plusUS(result, lhi64s, rhi64s, resulti64s) 980 case !isLHSUnsigned && isRHSUnsigned: 981 err = b.plusSU(result, lhi64s, rhi64s, resulti64s) 982 case !isLHSUnsigned && !isRHSUnsigned: 983 err = b.plusSS(result, lhi64s, rhi64s, resulti64s) 984 } 985 return err 986 } 987 func (b *builtinArithmeticPlusIntSig) plusUU(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 988 for i := 0; i < len(lhi64s); i++ { 989 if result.IsNull(i) { 990 continue 991 } 992 lh, rh := lhi64s[i], rhi64s[i] 993 994 if uint64(lh) > math.MaxUint64-uint64(rh) { 995 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 996 } 997 998 resulti64s[i] = lh + rh 999 } 1000 return nil 1001 } 1002 1003 func (b *builtinArithmeticPlusIntSig) plusUS(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 1004 for i := 0; i < len(lhi64s); i++ { 1005 if result.IsNull(i) { 1006 continue 1007 } 1008 lh, rh := lhi64s[i], rhi64s[i] 1009 1010 if rh < 0 && uint64(-rh) > uint64(lh) { 1011 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 1012 } 1013 if rh > 0 && uint64(lh) > math.MaxUint64-uint64(lh) { 1014 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 1015 } 1016 1017 resulti64s[i] = lh + rh 1018 } 1019 return nil 1020 } 1021 1022 func (b *builtinArithmeticPlusIntSig) plusSU(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 1023 for i := 0; i < len(lhi64s); i++ { 1024 if result.IsNull(i) { 1025 continue 1026 } 1027 lh, rh := lhi64s[i], rhi64s[i] 1028 1029 if lh < 0 && uint64(-lh) > uint64(rh) { 1030 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 1031 } 1032 if lh > 0 && uint64(rh) > math.MaxUint64-uint64(lh) { 1033 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 1034 } 1035 1036 resulti64s[i] = lh + rh 1037 } 1038 return nil 1039 } 1040 func (b *builtinArithmeticPlusIntSig) plusSS(result *chunk.DeferredCauset, lhi64s, rhi64s, resulti64s []int64) error { 1041 for i := 0; i < len(lhi64s); i++ { 1042 if result.IsNull(i) { 1043 continue 1044 } 1045 lh, rh := lhi64s[i], rhi64s[i] 1046 1047 if (lh > 0 && rh > math.MaxInt64-lh) || (lh < 0 && rh < math.MinInt64-lh) { 1048 return types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) 1049 } 1050 1051 resulti64s[i] = lh + rh 1052 } 1053 return nil 1054 } 1055 1056 func (b *builtinArithmeticPlusDecimalSig) vectorized() bool { 1057 return true 1058 } 1059 1060 func (b *builtinArithmeticPlusDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1061 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 1062 return err 1063 } 1064 n := input.NumEvents() 1065 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1066 if err != nil { 1067 return err 1068 } 1069 defer b.bufSlabPredictor.put(buf) 1070 if err := b.args[1].VecEvalDecimal(b.ctx, input, buf); err != nil { 1071 return err 1072 } 1073 1074 result.MergeNulls(buf) 1075 x := result.Decimals() 1076 y := buf.Decimals() 1077 to := new(types.MyDecimal) 1078 for i := 0; i < n; i++ { 1079 if result.IsNull(i) { 1080 continue 1081 } 1082 if err = types.DecimalAdd(&x[i], &y[i], to); err != nil { 1083 return err 1084 } 1085 x[i] = *to 1086 } 1087 return nil 1088 } 1089 1090 func (b *builtinArithmeticMultiplyIntUnsignedSig) vectorized() bool { 1091 return true 1092 } 1093 1094 func (b *builtinArithmeticMultiplyIntUnsignedSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1095 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 1096 return err 1097 } 1098 n := input.NumEvents() 1099 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 1100 if err != nil { 1101 return err 1102 } 1103 defer b.bufSlabPredictor.put(buf) 1104 1105 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 1106 return err 1107 } 1108 1109 x := result.Uint64s() 1110 y := buf.Uint64s() 1111 result.MergeNulls(buf) 1112 var res uint64 1113 for i := 0; i < n; i++ { 1114 if result.IsNull(i) { 1115 continue 1116 } 1117 1118 res = x[i] * y[i] 1119 if x[i] != 0 && res/x[i] != y[i] { 1120 return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s * %s)", b.args[0].String(), b.args[1].String())) 1121 } 1122 x[i] = res 1123 } 1124 return nil 1125 }