github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_cast_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 "math" 18 "strconv" 19 "strings" 20 21 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 22 "github.com/whtcorpsinc/milevadb/types" 23 "github.com/whtcorpsinc/milevadb/types/json" 24 "github.com/whtcorpsinc/milevadb/soliton/chunk" 25 ) 26 27 func (b *builtinCastIntAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 28 n := input.NumRows() 29 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 30 if err != nil { 31 return err 32 } 33 defer b.bufSlabPredictor.put(buf) 34 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 35 return err 36 } 37 38 result.ResizeGoDuration(n, false) 39 result.MergeNulls(buf) 40 i64s := buf.Int64s() 41 ds := result.GoDurations() 42 for i := 0; i < n; i++ { 43 if result.IsNull(i) { 44 continue 45 } 46 dur, err := types.NumberToDuration(i64s[i], int8(b.tp.Decimal)) 47 if err != nil { 48 if types.ErrOverflow.Equal(err) { 49 err = b.ctx.GetStochastikVars().StmtCtx.HandleOverflow(err, err) 50 } 51 if err != nil { 52 return err 53 } 54 result.SetNull(i, true) 55 continue 56 } 57 ds[i] = dur.Duration 58 } 59 return nil 60 } 61 62 func (b *builtinCastIntAsDurationSig) vectorized() bool { 63 return true 64 } 65 66 func (b *builtinCastIntAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 67 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 68 return err 69 } 70 if b.inUnion && allegrosql.HasUnsignedFlag(b.tp.Flag) { 71 i64s := result.Int64s() 72 // the null array of result is set by its child args[0], 73 // so we can skip it here to make this loop simpler to improve its performance. 74 for i := range i64s { 75 if i64s[i] < 0 { 76 i64s[i] = 0 77 } 78 } 79 } 80 return nil 81 } 82 83 func (b *builtinCastIntAsIntSig) vectorized() bool { 84 return true 85 } 86 87 func (b *builtinCastIntAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 88 n := input.NumRows() 89 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 90 if err != nil { 91 return err 92 } 93 defer b.bufSlabPredictor.put(buf) 94 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 95 return err 96 } 97 98 result.ResizeFloat64(n, false) 99 result.MergeNulls(buf) 100 101 i64s := buf.Int64s() 102 rs := result.Float64s() 103 104 hasUnsignedFlag0 := allegrosql.HasUnsignedFlag(b.tp.Flag) 105 hasUnsignedFlag1 := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 106 107 for i := 0; i < n; i++ { 108 if result.IsNull(i) { 109 continue 110 } 111 if !hasUnsignedFlag0 && !hasUnsignedFlag1 { 112 rs[i] = float64(i64s[i]) 113 } else if b.inUnion && !hasUnsignedFlag1 && i64s[i] < 0 { 114 // Round up to 0 if the value is negative but the memex eval type is unsigned in `UNION` memex 115 // NOTE: the following memexs are equal (so choose the more efficient one): 116 // `b.inUnion && hasUnsignedFlag0 && !hasUnsignedFlag1 && i64s[i] < 0` 117 // `b.inUnion && !hasUnsignedFlag1 && i64s[i] < 0` 118 rs[i] = 0 119 } else { 120 // recall that, int to float is different from uint to float 121 rs[i] = float64(uint64(i64s[i])) 122 } 123 } 124 return nil 125 } 126 127 func (b *builtinCastIntAsRealSig) vectorized() bool { 128 return true 129 } 130 131 func (b *builtinCastRealAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 132 if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil { 133 return err 134 } 135 n := input.NumRows() 136 f64s := result.Float64s() 137 conditionUnionAndUnsigned := b.inUnion && allegrosql.HasUnsignedFlag(b.tp.Flag) 138 if !conditionUnionAndUnsigned { 139 return nil 140 } 141 for i := 0; i < n; i++ { 142 if result.IsNull(i) { 143 continue 144 } 145 if f64s[i] < 0 { 146 f64s[i] = 0 147 } 148 } 149 return nil 150 } 151 152 func (b *builtinCastRealAsRealSig) vectorized() bool { 153 return true 154 } 155 156 func (b *builtinCastTimeAsJSONSig) vectorized() bool { 157 return true 158 } 159 160 func (b *builtinCastTimeAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 161 n := input.NumRows() 162 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 163 if err != nil { 164 return err 165 } 166 defer b.bufSlabPredictor.put(buf) 167 if err = b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 168 return err 169 } 170 171 result.ReserveJSON(n) 172 tms := buf.Times() 173 for i := 0; i < n; i++ { 174 if buf.IsNull(i) { 175 result.AppendNull() 176 continue 177 } 178 179 tp := tms[i].Type() 180 if tp == allegrosql.TypeDatetime || tp == allegrosql.TypeTimestamp { 181 tms[i].SetFsp(types.MaxFsp) 182 } 183 result.AppendJSON(json.CreateBinary(tms[i].String())) 184 } 185 return nil 186 } 187 188 func (b *builtinCastRealAsStringSig) vectorized() bool { 189 return true 190 } 191 192 func (b *builtinCastRealAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 193 n := input.NumRows() 194 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 195 if err != nil { 196 return err 197 } 198 defer b.bufSlabPredictor.put(buf) 199 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 200 return err 201 } 202 203 bits := 64 204 if b.args[0].GetType().Tp == allegrosql.TypeFloat { 205 // b.args[0].EvalReal() casts the value from float32 to float64, for example: 206 // float32(208.867) is cast to float64(208.86700439) 207 // If we strconv.FormatFloat the value with 64bits, the result is incorrect! 208 bits = 32 209 } 210 211 var isNull bool 212 var res string 213 f64s := buf.Float64s() 214 result.ReserveString(n) 215 sc := b.ctx.GetStochastikVars().StmtCtx 216 for i, v := range f64s { 217 if buf.IsNull(i) { 218 result.AppendNull() 219 continue 220 } 221 res, err = types.ProduceStrWithSpecifiedTp(strconv.FormatFloat(v, 'f', -1, bits), b.tp, sc, false) 222 if err != nil { 223 return err 224 } 225 res, isNull, err = padZeroForBinaryType(res, b.tp, b.ctx) 226 if err != nil { 227 return err 228 } 229 if isNull { 230 result.AppendNull() 231 continue 232 } 233 result.AppendString(res) 234 } 235 return nil 236 } 237 238 func (b *builtinCastDecimalAsStringSig) vectorized() bool { 239 return true 240 } 241 242 func (b *builtinCastDecimalAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 243 n := input.NumRows() 244 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 245 if err != nil { 246 return err 247 } 248 defer b.bufSlabPredictor.put(buf) 249 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 250 return err 251 } 252 253 sc := b.ctx.GetStochastikVars().StmtCtx 254 vas := buf.Decimals() 255 result.ReserveString(n) 256 for i, v := range vas { 257 if buf.IsNull(i) { 258 result.AppendNull() 259 continue 260 } 261 res, e := types.ProduceStrWithSpecifiedTp(string(v.ToString()), b.tp, sc, false) 262 if e != nil { 263 return e 264 } 265 str, b, e1 := padZeroForBinaryType(res, b.tp, b.ctx) 266 if e1 != nil { 267 return e1 268 } 269 if b { 270 result.AppendNull() 271 continue 272 } 273 result.AppendString(str) 274 } 275 return nil 276 } 277 278 func (b *builtinCastTimeAsDecimalSig) vectorized() bool { 279 return true 280 } 281 282 func (b *builtinCastTimeAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 283 n := input.NumRows() 284 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 285 if err != nil { 286 return err 287 } 288 defer b.bufSlabPredictor.put(buf) 289 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 290 return err 291 } 292 293 result.ResizeDecimal(n, false) 294 result.MergeNulls(buf) 295 times := buf.Times() 296 decs := result.Decimals() 297 sc := b.ctx.GetStochastikVars().StmtCtx 298 dec := new(types.MyDecimal) 299 for i := 0; i < n; i++ { 300 if result.IsNull(i) { 301 continue 302 } 303 *dec = types.MyDecimal{} 304 times[i].FillNumber(dec) 305 dec, err = types.ProduceDecWithSpecifiedTp(dec, b.tp, sc) 306 if err != nil { 307 return err 308 } 309 decs[i] = *dec 310 } 311 return nil 312 } 313 314 func (b *builtinCastDurationAsIntSig) vectorized() bool { 315 return true 316 } 317 318 func (b *builtinCastDurationAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 319 n := input.NumRows() 320 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 321 if err != nil { 322 return err 323 } 324 defer b.bufSlabPredictor.put(buf) 325 if err := b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 326 return err 327 } 328 329 result.ResizeInt64(n, false) 330 result.MergeNulls(buf) 331 i64s := result.Int64s() 332 var duration types.Duration 333 ds := buf.GoDurations() 334 fsp := int8(b.args[0].GetType().Decimal) 335 for i := 0; i < n; i++ { 336 if result.IsNull(i) { 337 continue 338 } 339 340 duration.Duration = ds[i] 341 duration.Fsp = fsp 342 dur, err := duration.RoundFrac(types.DefaultFsp) 343 if err != nil { 344 return err 345 } 346 i64s[i], err = dur.ToNumber().ToInt() 347 if err != nil { 348 return err 349 } 350 } 351 return nil 352 353 } 354 355 func (b *builtinCastIntAsTimeSig) vectorized() bool { 356 return true 357 } 358 359 func (b *builtinCastIntAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 360 n := input.NumRows() 361 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 362 if err != nil { 363 return err 364 } 365 defer b.bufSlabPredictor.put(buf) 366 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 367 return err 368 } 369 370 result.ResizeTime(n, false) 371 result.MergeNulls(buf) 372 times := result.Times() 373 i64s := buf.Int64s() 374 stmt := b.ctx.GetStochastikVars().StmtCtx 375 fsp := int8(b.tp.Decimal) 376 for i := 0; i < n; i++ { 377 if buf.IsNull(i) { 378 continue 379 } 380 tm, err := types.ParseTimeFromNum(stmt, i64s[i], b.tp.Tp, fsp) 381 if err != nil { 382 if err = handleInvalidTimeError(b.ctx, err); err != nil { 383 return err 384 } 385 result.SetNull(i, true) 386 continue 387 } 388 times[i] = tm 389 if b.tp.Tp == allegrosql.TypeDate { 390 // Truncate hh:mm:ss part if the type is Date. 391 times[i].SetCoreTime(types.FromDate(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0)) 392 } 393 } 394 return nil 395 } 396 397 func (b *builtinCastRealAsJSONSig) vectorized() bool { 398 return true 399 } 400 401 func (b *builtinCastRealAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 402 n := input.NumRows() 403 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 404 if err != nil { 405 return err 406 } 407 defer b.bufSlabPredictor.put(buf) 408 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 409 return err 410 } 411 f64s := buf.Float64s() 412 result.ReserveJSON(n) 413 for i := 0; i < n; i++ { 414 // FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.``` 415 if buf.IsNull(i) { 416 result.AppendNull() 417 } else { 418 result.AppendJSON(json.CreateBinary(f64s[i])) 419 } 420 } 421 return nil 422 } 423 424 func (b *builtinCastJSONAsRealSig) vectorized() bool { 425 return true 426 } 427 428 func (b *builtinCastJSONAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 429 n := input.NumRows() 430 buf, err := b.bufSlabPredictor.get(types.ETJson, n) 431 if err != nil { 432 return err 433 } 434 defer b.bufSlabPredictor.put(buf) 435 if err := b.args[0].VecEvalJSON(b.ctx, input, buf); err != nil { 436 return err 437 } 438 439 result.ResizeFloat64(n, false) 440 result.MergeNulls(buf) 441 f64s := result.Float64s() 442 sc := b.ctx.GetStochastikVars().StmtCtx 443 for i := 0; i < n; i++ { 444 if result.IsNull(i) { 445 continue 446 } 447 f64s[i], err = types.ConvertJSONToFloat(sc, buf.GetJSON(i)) 448 if err != nil { 449 return err 450 } 451 } 452 return nil 453 } 454 455 func (b *builtinCastJSONAsTimeSig) vectorized() bool { 456 return true 457 } 458 459 func (b *builtinCastJSONAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 460 n := input.NumRows() 461 buf, err := b.bufSlabPredictor.get(types.ETJson, n) 462 if err != nil { 463 return err 464 } 465 defer b.bufSlabPredictor.put(buf) 466 if err := b.args[0].VecEvalJSON(b.ctx, input, buf); err != nil { 467 return err 468 } 469 470 result.ResizeTime(n, false) 471 result.MergeNulls(buf) 472 times := result.Times() 473 stmtCtx := b.ctx.GetStochastikVars().StmtCtx 474 fsp := int8(b.tp.Decimal) 475 for i := 0; i < n; i++ { 476 if result.IsNull(i) { 477 continue 478 } 479 s, err := buf.GetJSON(i).Unquote() 480 if err != nil { 481 return err 482 } 483 tm, err := types.ParseTime(stmtCtx, s, b.tp.Tp, fsp) 484 if err != nil { 485 if err = handleInvalidTimeError(b.ctx, err); err != nil { 486 return err 487 } 488 result.SetNull(i, true) 489 continue 490 } 491 times[i] = tm 492 if b.tp.Tp == allegrosql.TypeDate { 493 // Truncate hh:mm:ss part if the type is Date. 494 times[i].SetCoreTime(types.FromDate(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0)) 495 } 496 } 497 return nil 498 } 499 500 func (b *builtinCastRealAsTimeSig) vectorized() bool { 501 return true 502 } 503 504 func (b *builtinCastRealAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 505 n := input.NumRows() 506 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 507 if err != nil { 508 return err 509 } 510 defer b.bufSlabPredictor.put(buf) 511 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 512 return err 513 } 514 515 result.ResizeTime(n, false) 516 result.MergeNulls(buf) 517 times := result.Times() 518 f64s := buf.Float64s() 519 stmt := b.ctx.GetStochastikVars().StmtCtx 520 fsp := int8(b.tp.Decimal) 521 for i := 0; i < n; i++ { 522 if buf.IsNull(i) { 523 continue 524 } 525 fv := strconv.FormatFloat(f64s[i], 'f', -1, 64) 526 if fv == "0" { 527 times[i] = types.ZeroTime 528 continue 529 } 530 tm, err := types.ParseTime(stmt, fv, b.tp.Tp, fsp) 531 if err != nil { 532 if err = handleInvalidTimeError(b.ctx, err); err != nil { 533 return err 534 } 535 result.SetNull(i, true) 536 continue 537 } 538 times[i] = tm 539 if b.tp.Tp == allegrosql.TypeDate { 540 // Truncate hh:mm:ss part if the type is Date. 541 times[i].SetCoreTime(types.FromDate(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0)) 542 } 543 } 544 return nil 545 } 546 547 func (b *builtinCastDecimalAsDecimalSig) vectorized() bool { 548 return true 549 } 550 551 func (b *builtinCastDecimalAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 552 if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil { 553 return err 554 } 555 556 n := input.NumRows() 557 decs := result.Decimals() 558 sc := b.ctx.GetStochastikVars().StmtCtx 559 conditionUnionAndUnsigned := b.inUnion && allegrosql.HasUnsignedFlag(b.tp.Flag) 560 dec := new(types.MyDecimal) 561 for i := 0; i < n; i++ { 562 if result.IsNull(i) { 563 continue 564 } 565 *dec = types.MyDecimal{} 566 if !(conditionUnionAndUnsigned && decs[i].IsNegative()) { 567 *dec = decs[i] 568 } 569 dec, err := types.ProduceDecWithSpecifiedTp(dec, b.tp, sc) 570 if err != nil { 571 return err 572 } 573 decs[i] = *dec 574 } 575 return nil 576 } 577 578 func (b *builtinCastDurationAsTimeSig) vectorized() bool { 579 return true 580 } 581 582 func (b *builtinCastDurationAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 583 n := input.NumRows() 584 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 585 if err != nil { 586 return err 587 } 588 defer b.bufSlabPredictor.put(buf) 589 if err := b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 590 return err 591 } 592 593 result.ResizeTime(n, false) 594 result.MergeNulls(buf) 595 var duration types.Duration 596 ds := buf.GoDurations() 597 times := result.Times() 598 stmtCtx := b.ctx.GetStochastikVars().StmtCtx 599 fsp := int8(b.tp.Decimal) 600 for i := 0; i < n; i++ { 601 if result.IsNull(i) { 602 continue 603 } 604 605 duration.Duration = ds[i] 606 duration.Fsp = fsp 607 tm, err := duration.ConvertToTime(stmtCtx, b.tp.Tp) 608 if err != nil { 609 if err = handleInvalidTimeError(b.ctx, err); err != nil { 610 return err 611 } 612 result.SetNull(i, true) 613 continue 614 } 615 tm, err = tm.RoundFrac(stmtCtx, fsp) 616 if err != nil { 617 return err 618 } 619 times[i] = tm 620 } 621 return nil 622 } 623 624 func (b *builtinCastIntAsStringSig) vectorized() bool { 625 return true 626 } 627 628 func (b *builtinCastIntAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 629 n := input.NumRows() 630 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 631 if err != nil { 632 return err 633 } 634 defer b.bufSlabPredictor.put(buf) 635 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 636 return err 637 } 638 639 isUnsigned := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 640 result.ReserveString(n) 641 i64s := buf.Int64s() 642 for i := 0; i < n; i++ { 643 var str string 644 if buf.IsNull(i) { 645 result.AppendNull() 646 continue 647 } 648 if !isUnsigned { 649 str = strconv.FormatInt(i64s[i], 10) 650 } else { 651 str = strconv.FormatUint(uint64(i64s[i]), 10) 652 } 653 str, err = types.ProduceStrWithSpecifiedTp(str, b.tp, b.ctx.GetStochastikVars().StmtCtx, false) 654 if err != nil { 655 return err 656 } 657 var d bool 658 str, d, err = padZeroForBinaryType(str, b.tp, b.ctx) 659 if err != nil { 660 return err 661 } 662 if d { 663 result.AppendNull() 664 } else { 665 result.AppendString(str) 666 } 667 } 668 return nil 669 } 670 671 func (b *builtinCastRealAsIntSig) vectorized() bool { 672 return true 673 } 674 675 func (b *builtinCastRealAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 676 n := input.NumRows() 677 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 678 if err != nil { 679 return err 680 } 681 defer b.bufSlabPredictor.put(buf) 682 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 683 return err 684 } 685 686 result.ResizeInt64(n, false) 687 result.MergeNulls(buf) 688 i64s := result.Int64s() 689 f64s := buf.Float64s() 690 unsigned := allegrosql.HasUnsignedFlag(b.tp.Flag) 691 for i := 0; i < n; i++ { 692 if result.IsNull(i) { 693 continue 694 } 695 696 if !unsigned { 697 i64s[i], err = types.ConvertFloatToInt(f64s[i], types.IntergerSignedLowerBound(allegrosql.TypeLonglong), types.IntergerSignedUpperBound(allegrosql.TypeLonglong), allegrosql.TypeLonglong) 698 } else if b.inUnion && f64s[i] < 0 { 699 i64s[i] = 0 700 } else { 701 var uintVal uint64 702 sc := b.ctx.GetStochastikVars().StmtCtx 703 uintVal, err = types.ConvertFloatToUint(sc, f64s[i], types.IntergerUnsignedUpperBound(allegrosql.TypeLonglong), allegrosql.TypeLonglong) 704 i64s[i] = int64(uintVal) 705 } 706 if types.ErrOverflow.Equal(err) { 707 err = b.ctx.GetStochastikVars().StmtCtx.HandleOverflow(err, err) 708 } 709 if err != nil { 710 return err 711 } 712 } 713 return nil 714 } 715 716 func (b *builtinCastTimeAsRealSig) vectorized() bool { 717 return true 718 } 719 720 func (b *builtinCastTimeAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 721 n := input.NumRows() 722 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 723 if err != nil { 724 return err 725 } 726 defer b.bufSlabPredictor.put(buf) 727 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 728 return err 729 } 730 result.ResizeFloat64(n, false) 731 result.MergeNulls(buf) 732 times := buf.Times() 733 f64s := result.Float64s() 734 for i := 0; i < n; i++ { 735 if result.IsNull(i) { 736 continue 737 } 738 f64, err := times[i].ToNumber().ToFloat64() 739 if err != nil { 740 if types.ErrOverflow.Equal(err) { 741 err = b.ctx.GetStochastikVars().StmtCtx.HandleOverflow(err, err) 742 } 743 if err != nil { 744 return err 745 } 746 result.SetNull(i, true) 747 continue 748 } 749 f64s[i] = f64 750 } 751 return nil 752 } 753 754 func (b *builtinCastStringAsJSONSig) vectorized() bool { 755 return true 756 } 757 758 func (b *builtinCastStringAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 759 n := input.NumRows() 760 buf, err := b.bufSlabPredictor.get(types.ETString, n) 761 if err != nil { 762 return err 763 } 764 defer b.bufSlabPredictor.put(buf) 765 if err = b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 766 return err 767 } 768 769 result.ReserveJSON(n) 770 hasParse := allegrosql.HasParseToJSONFlag(b.tp.Flag) 771 if hasParse { 772 var res json.BinaryJSON 773 for i := 0; i < n; i++ { 774 if buf.IsNull(i) { 775 result.AppendNull() 776 continue 777 } 778 res, err = json.ParseBinaryFromString(buf.GetString(i)) 779 if err != nil { 780 return err 781 } 782 result.AppendJSON(res) 783 } 784 } else { 785 for i := 0; i < n; i++ { 786 if buf.IsNull(i) { 787 result.AppendNull() 788 continue 789 } 790 result.AppendJSON(json.CreateBinary(buf.GetString(i))) 791 } 792 } 793 return nil 794 } 795 796 func (b *builtinCastRealAsDecimalSig) vectorized() bool { 797 return true 798 } 799 800 func (b *builtinCastRealAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 801 n := input.NumRows() 802 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 803 if err != nil { 804 return err 805 } 806 defer b.bufSlabPredictor.put(buf) 807 if err = b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 808 return err 809 } 810 result.ResizeDecimal(n, false) 811 result.MergeNulls(buf) 812 bufreal := buf.Float64s() 813 resdecimal := result.Decimals() 814 for i := 0; i < n; i++ { 815 if result.IsNull(i) { 816 continue 817 } 818 if !b.inUnion || bufreal[i] >= 0 { 819 if err = resdecimal[i].FromFloat64(bufreal[i]); err != nil { 820 if types.ErrOverflow.Equal(err) { 821 warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", b.args[0]) 822 err = b.ctx.GetStochastikVars().StmtCtx.HandleOverflow(err, warnErr) 823 } else if types.ErrTruncated.Equal(err) { 824 // This behavior is consistent with MyALLEGROSQL. 825 err = nil 826 } 827 if err != nil { 828 return err 829 } 830 } 831 } 832 dec, err := types.ProduceDecWithSpecifiedTp(&resdecimal[i], b.tp, b.ctx.GetStochastikVars().StmtCtx) 833 if err != nil { 834 return err 835 } 836 resdecimal[i] = *dec 837 } 838 return nil 839 } 840 841 func (b *builtinCastStringAsIntSig) vectorized() bool { 842 return true 843 } 844 845 func (b *builtinCastStringAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 846 n := input.NumRows() 847 if b.args[0].GetType().Hybrid() || IsBinaryLiteral(b.args[0]) { 848 return b.args[0].VecEvalInt(b.ctx, input, result) 849 } 850 result.ResizeInt64(n, false) 851 buf, err := b.bufSlabPredictor.get(types.ETString, n) 852 if err != nil { 853 return err 854 } 855 defer b.bufSlabPredictor.put(buf) 856 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 857 return err 858 } 859 result.MergeNulls(buf) 860 sc := b.ctx.GetStochastikVars().StmtCtx 861 i64s := result.Int64s() 862 isUnsigned := allegrosql.HasUnsignedFlag(b.tp.Flag) 863 unionUnsigned := isUnsigned && b.inUnion 864 for i := 0; i < n; i++ { 865 if result.IsNull(i) { 866 continue 867 } 868 var ( 869 res int64 870 ures uint64 871 ) 872 val := strings.TrimSpace(buf.GetString(i)) 873 isNegative := len(val) > 1 && val[0] == '-' 874 if !isNegative { 875 ures, err = types.StrToUint(sc, val, true) 876 if !isUnsigned && err == nil && ures > uint64(math.MaxInt64) { 877 sc.AppendWarning(types.ErrCastAsSignedOverflow) 878 } 879 res = int64(ures) 880 } else if unionUnsigned { 881 res = 0 882 } else { 883 res, err = types.StrToInt(sc, val, true) 884 if err == nil && isUnsigned { 885 // If overflow, don't append this warnings 886 sc.AppendWarning(types.ErrCastNegIntAsUnsigned) 887 } 888 } 889 res, err = b.handleOverflow(res, val, err, isNegative) 890 if err != nil { 891 return err 892 } 893 i64s[i] = res 894 } 895 return nil 896 } 897 898 func (b *builtinCastStringAsDurationSig) vectorized() bool { 899 return true 900 } 901 902 func (b *builtinCastStringAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 903 n := input.NumRows() 904 buf, err := b.bufSlabPredictor.get(types.ETString, n) 905 if err != nil { 906 return err 907 } 908 defer b.bufSlabPredictor.put(buf) 909 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 910 return err 911 } 912 result.ResizeGoDuration(n, false) 913 result.MergeNulls(buf) 914 ds := result.GoDurations() 915 for i := 0; i < n; i++ { 916 if result.IsNull(i) { 917 continue 918 } 919 dur, err := types.ParseDuration(b.ctx.GetStochastikVars().StmtCtx, buf.GetString(i), int8(b.tp.Decimal)) 920 if err != nil { 921 if types.ErrTruncatedWrongVal.Equal(err) { 922 err = b.ctx.GetStochastikVars().StmtCtx.HandleTruncate(err) 923 } 924 if err != nil { 925 return err 926 } 927 if dur == types.ZeroDuration { 928 result.SetNull(i, true) 929 continue 930 } 931 } 932 ds[i] = dur.Duration 933 } 934 return nil 935 } 936 937 func (b *builtinCastDurationAsDecimalSig) vectorized() bool { 938 return true 939 } 940 941 func (b *builtinCastDurationAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 942 n := input.NumRows() 943 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 944 if err != nil { 945 return err 946 } 947 defer b.bufSlabPredictor.put(buf) 948 if err := b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 949 return err 950 } 951 result.ResizeDecimal(n, false) 952 result.MergeNulls(buf) 953 d64s := result.Decimals() 954 var duration types.Duration 955 ds := buf.GoDurations() 956 sc := b.ctx.GetStochastikVars().StmtCtx 957 fsp := int8(b.args[0].GetType().Decimal) 958 if fsp, err = types.CheckFsp(int(fsp)); err != nil { 959 return err 960 } 961 for i := 0; i < n; i++ { 962 if result.IsNull(i) { 963 continue 964 } 965 duration.Duration = ds[i] 966 duration.Fsp = fsp 967 res, err := types.ProduceDecWithSpecifiedTp(duration.ToNumber(), b.tp, sc) 968 if err != nil { 969 return err 970 } 971 d64s[i] = *res 972 } 973 return nil 974 } 975 976 func (b *builtinCastIntAsDecimalSig) vectorized() bool { 977 return true 978 } 979 980 func (b *builtinCastIntAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 981 n := input.NumRows() 982 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 983 if err != nil { 984 return err 985 } 986 defer b.bufSlabPredictor.put(buf) 987 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 988 return err 989 } 990 991 isUnsignedTp := allegrosql.HasUnsignedFlag(b.tp.Flag) 992 isUnsignedArgs0 := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 993 nums := buf.Int64s() 994 result.ResizeDecimal(n, false) 995 result.MergeNulls(buf) 996 decs := result.Decimals() 997 sc := b.ctx.GetStochastikVars().StmtCtx 998 dec := new(types.MyDecimal) 999 for i := 0; i < n; i++ { 1000 if result.IsNull(i) { 1001 continue 1002 } 1003 1004 *dec = types.MyDecimal{} 1005 if !isUnsignedTp && !isUnsignedArgs0 { 1006 dec.FromInt(nums[i]) 1007 } else if b.inUnion && !isUnsignedArgs0 && nums[i] < 0 { 1008 dec.FromUint(0) 1009 } else { 1010 dec.FromUint(uint64(nums[i])) 1011 } 1012 1013 dec, err = types.ProduceDecWithSpecifiedTp(dec, b.tp, sc) 1014 if err != nil { 1015 return err 1016 } 1017 decs[i] = *dec 1018 } 1019 return nil 1020 } 1021 1022 func (b *builtinCastIntAsJSONSig) vectorized() bool { 1023 return true 1024 } 1025 1026 func (b *builtinCastIntAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1027 n := input.NumRows() 1028 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 1029 if err != nil { 1030 return err 1031 } 1032 defer b.bufSlabPredictor.put(buf) 1033 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 1034 return err 1035 } 1036 nums := buf.Int64s() 1037 result.ReserveJSON(n) 1038 if allegrosql.HasIsBooleanFlag(b.args[0].GetType().Flag) { 1039 for i := 0; i < n; i++ { 1040 if buf.IsNull(i) { 1041 result.AppendNull() 1042 } else { 1043 result.AppendJSON(json.CreateBinary(nums[i] != 0)) 1044 } 1045 } 1046 } else if allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) { 1047 for i := 0; i < n; i++ { 1048 if buf.IsNull(i) { 1049 result.AppendNull() 1050 } else { 1051 result.AppendJSON(json.CreateBinary(uint64(nums[i]))) 1052 } 1053 } 1054 } else { 1055 for i := 0; i < n; i++ { 1056 if buf.IsNull(i) { 1057 result.AppendNull() 1058 } else { 1059 result.AppendJSON(json.CreateBinary(nums[i])) 1060 } 1061 } 1062 } 1063 1064 return nil 1065 } 1066 1067 func (b *builtinCastJSONAsJSONSig) vectorized() bool { 1068 return true 1069 } 1070 1071 func (b *builtinCastJSONAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1072 return b.args[0].VecEvalJSON(b.ctx, input, result) 1073 } 1074 1075 func (b *builtinCastJSONAsStringSig) vectorized() bool { 1076 return true 1077 } 1078 1079 func (b *builtinCastJSONAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1080 n := input.NumRows() 1081 buf, err := b.bufSlabPredictor.get(types.ETJson, n) 1082 if err != nil { 1083 return err 1084 } 1085 defer b.bufSlabPredictor.put(buf) 1086 if err := b.args[0].VecEvalJSON(b.ctx, input, buf); err != nil { 1087 return err 1088 } 1089 1090 result.ReserveString(n) 1091 for i := 0; i < n; i++ { 1092 if buf.IsNull(i) { 1093 result.AppendNull() 1094 continue 1095 } 1096 result.AppendString(buf.GetJSON(i).String()) 1097 } 1098 return nil 1099 } 1100 1101 func (b *builtinCastDurationAsRealSig) vectorized() bool { 1102 return true 1103 } 1104 1105 func (b *builtinCastDurationAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1106 n := input.NumRows() 1107 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1108 if err != nil { 1109 return err 1110 } 1111 defer b.bufSlabPredictor.put(buf) 1112 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1113 return err 1114 } 1115 1116 result.ResizeFloat64(n, false) 1117 result.MergeNulls(buf) 1118 f64s := result.Float64s() 1119 1120 var duration types.Duration 1121 fsp := int8(b.args[0].GetType().Decimal) 1122 if fsp, err = types.CheckFsp(int(fsp)); err != nil { 1123 return err 1124 } 1125 ds := buf.GoDurations() 1126 for i := 0; i < n; i++ { 1127 if result.IsNull(i) { 1128 continue 1129 } 1130 1131 duration.Duration = ds[i] 1132 duration.Fsp = fsp 1133 if f64s[i], err = duration.ToNumber().ToFloat64(); err != nil { 1134 return err 1135 } 1136 } 1137 return nil 1138 } 1139 1140 func (b *builtinCastJSONAsIntSig) vectorized() bool { 1141 return true 1142 } 1143 1144 func (b *builtinCastJSONAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1145 n := input.NumRows() 1146 buf, err := b.bufSlabPredictor.get(types.ETJson, n) 1147 if err != nil { 1148 return err 1149 } 1150 defer b.bufSlabPredictor.put(buf) 1151 if err := b.args[0].VecEvalJSON(b.ctx, input, buf); err != nil { 1152 return err 1153 } 1154 1155 result.ResizeInt64(n, false) 1156 result.MergeNulls(buf) 1157 i64s := result.Int64s() 1158 sc := b.ctx.GetStochastikVars().StmtCtx 1159 for i := 0; i < n; i++ { 1160 if result.IsNull(i) { 1161 continue 1162 } 1163 i64s[i], err = types.ConvertJSONToInt(sc, buf.GetJSON(i), allegrosql.HasUnsignedFlag(b.tp.Flag)) 1164 if err != nil { 1165 return err 1166 } 1167 } 1168 return nil 1169 } 1170 1171 func (b *builtinCastRealAsDurationSig) vectorized() bool { 1172 return true 1173 } 1174 1175 func (b *builtinCastRealAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1176 n := input.NumRows() 1177 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 1178 if err != nil { 1179 return err 1180 } 1181 defer b.bufSlabPredictor.put(buf) 1182 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 1183 return err 1184 } 1185 result.ResizeGoDuration(n, false) 1186 result.MergeNulls(buf) 1187 f64s := buf.Float64s() 1188 ds := result.GoDurations() 1189 for i := 0; i < n; i++ { 1190 if result.IsNull(i) { 1191 continue 1192 } 1193 dur, err := types.ParseDuration(b.ctx.GetStochastikVars().StmtCtx, strconv.FormatFloat(f64s[i], 'f', -1, 64), int8(b.tp.Decimal)) 1194 if err != nil { 1195 return err 1196 } 1197 ds[i] = dur.Duration 1198 } 1199 return nil 1200 } 1201 1202 func (b *builtinCastTimeAsDurationSig) vectorized() bool { 1203 return true 1204 } 1205 1206 func (b *builtinCastTimeAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1207 n := input.NumRows() 1208 arg0, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1209 if err != nil { 1210 return err 1211 } 1212 defer b.bufSlabPredictor.put(arg0) 1213 if err := b.args[0].VecEvalTime(b.ctx, input, arg0); err != nil { 1214 return err 1215 } 1216 arg0s := arg0.Times() 1217 result.ResizeGoDuration(n, false) 1218 result.MergeNulls(arg0) 1219 ds := result.GoDurations() 1220 for i, t := range arg0s { 1221 if result.IsNull(i) { 1222 continue 1223 } 1224 d, err := t.ConvertToDuration() 1225 if err != nil { 1226 return err 1227 } 1228 d, err = d.RoundFrac(int8(b.tp.Decimal)) 1229 if err != nil { 1230 return err 1231 } 1232 ds[i] = d.Duration 1233 } 1234 return nil 1235 } 1236 1237 func (b *builtinCastDurationAsDurationSig) vectorized() bool { 1238 return true 1239 } 1240 1241 func (b *builtinCastDurationAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1242 var err error 1243 if err = b.args[0].VecEvalDuration(b.ctx, input, result); err != nil { 1244 return err 1245 } 1246 1247 res := result.GoDurations() 1248 dur := &types.Duration{ 1249 Fsp: types.UnspecifiedFsp, 1250 } 1251 var rd types.Duration 1252 for i, v := range res { 1253 if result.IsNull(i) { 1254 continue 1255 } 1256 dur.Duration = v 1257 rd, err = dur.RoundFrac(int8(b.tp.Decimal)) 1258 if err != nil { 1259 return err 1260 } 1261 res[i] = rd.Duration 1262 } 1263 return nil 1264 } 1265 1266 func (b *builtinCastDurationAsStringSig) vectorized() bool { 1267 return true 1268 } 1269 1270 func (b *builtinCastDurationAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1271 n := input.NumRows() 1272 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1273 if err != nil { 1274 return err 1275 } 1276 defer b.bufSlabPredictor.put(buf) 1277 if err := b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1278 return err 1279 } 1280 1281 var res string 1282 var isNull bool 1283 sc := b.ctx.GetStochastikVars().StmtCtx 1284 result.ReserveString(n) 1285 for i := 0; i < n; i++ { 1286 if buf.IsNull(i) { 1287 result.AppendNull() 1288 continue 1289 } 1290 res, err = types.ProduceStrWithSpecifiedTp(buf.GetDuration(i, 0).String(), b.tp, sc, false) 1291 if err != nil { 1292 return err 1293 } 1294 res, isNull, err = padZeroForBinaryType(res, b.tp, b.ctx) 1295 if err != nil { 1296 return err 1297 } 1298 if isNull { 1299 result.AppendNull() 1300 continue 1301 } 1302 result.AppendString(res) 1303 } 1304 return nil 1305 } 1306 1307 func (b *builtinCastDecimalAsRealSig) vectorized() bool { 1308 return true 1309 } 1310 1311 func (b *builtinCastDecimalAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1312 n := input.NumRows() 1313 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1314 if err != nil { 1315 return err 1316 } 1317 defer b.bufSlabPredictor.put(buf) 1318 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1319 return err 1320 } 1321 1322 result.ResizeFloat64(n, false) 1323 result.MergeNulls(buf) 1324 1325 d := buf.Decimals() 1326 rs := result.Float64s() 1327 1328 inUnionAndUnsigned := b.inUnion && allegrosql.HasUnsignedFlag(b.tp.Flag) 1329 for i := 0; i < n; i++ { 1330 if result.IsNull(i) { 1331 continue 1332 } 1333 if inUnionAndUnsigned && d[i].IsNegative() { 1334 rs[i] = 0 1335 continue 1336 } 1337 res, err := d[i].ToFloat64() 1338 if err != nil { 1339 if types.ErrOverflow.Equal(err) { 1340 err = b.ctx.GetStochastikVars().StmtCtx.HandleOverflow(err, err) 1341 } 1342 if err != nil { 1343 return err 1344 } 1345 result.SetNull(i, true) 1346 continue 1347 } 1348 rs[i] = res 1349 } 1350 return nil 1351 } 1352 1353 func (b *builtinCastDecimalAsTimeSig) vectorized() bool { 1354 return true 1355 } 1356 1357 func (b *builtinCastDecimalAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1358 n := input.NumRows() 1359 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1360 if err != nil { 1361 return err 1362 } 1363 defer b.bufSlabPredictor.put(buf) 1364 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1365 return err 1366 } 1367 1368 result.ResizeTime(n, false) 1369 result.MergeNulls(buf) 1370 times := result.Times() 1371 decimals := buf.Decimals() 1372 stmt := b.ctx.GetStochastikVars().StmtCtx 1373 fsp := int8(b.tp.Decimal) 1374 for i := 0; i < n; i++ { 1375 if buf.IsNull(i) { 1376 continue 1377 } 1378 tm, err := types.ParseTimeFromFloatString(stmt, string(decimals[i].ToString()), b.tp.Tp, fsp) 1379 if err != nil { 1380 if err = handleInvalidTimeError(b.ctx, err); err != nil { 1381 return err 1382 } 1383 result.SetNull(i, true) 1384 continue 1385 } 1386 times[i] = tm 1387 if b.tp.Tp == allegrosql.TypeDate { 1388 // Truncate hh:mm:ss part if the type is Date. 1389 times[i].SetCoreTime(types.FromDate(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0)) 1390 } 1391 } 1392 return nil 1393 } 1394 1395 func (b *builtinCastTimeAsIntSig) vectorized() bool { 1396 return true 1397 } 1398 1399 func (b *builtinCastTimeAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1400 n := input.NumRows() 1401 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1402 if err != nil { 1403 return err 1404 } 1405 defer b.bufSlabPredictor.put(buf) 1406 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 1407 return err 1408 } 1409 1410 result.ResizeInt64(n, false) 1411 result.MergeNulls(buf) 1412 times := buf.Times() 1413 i64s := result.Int64s() 1414 sc := b.ctx.GetStochastikVars().StmtCtx 1415 for i := 0; i < n; i++ { 1416 if result.IsNull(i) { 1417 continue 1418 } 1419 t, err := times[i].RoundFrac(sc, types.DefaultFsp) 1420 if err != nil { 1421 return err 1422 } 1423 i64s[i], err = t.ToNumber().ToInt() 1424 if err != nil { 1425 return err 1426 } 1427 } 1428 return nil 1429 } 1430 1431 func (b *builtinCastTimeAsTimeSig) vectorized() bool { 1432 return true 1433 } 1434 1435 func (b *builtinCastTimeAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1436 n := input.NumRows() 1437 if err := b.args[0].VecEvalTime(b.ctx, input, result); err != nil { 1438 return err 1439 } 1440 1441 times := result.Times() 1442 stmt := b.ctx.GetStochastikVars().StmtCtx 1443 fsp := int8(b.tp.Decimal) 1444 for i := 0; i < n; i++ { 1445 if result.IsNull(i) { 1446 continue 1447 } 1448 res, err := times[i].Convert(stmt, b.tp.Tp) 1449 if err != nil { 1450 if err = handleInvalidTimeError(b.ctx, err); err != nil { 1451 return err 1452 } 1453 result.SetNull(i, true) 1454 continue 1455 } 1456 tm, err := res.RoundFrac(stmt, fsp) 1457 if err != nil { 1458 return err 1459 } 1460 times[i] = tm 1461 if b.tp.Tp == allegrosql.TypeDate { 1462 // Truncate hh:mm:ss part if the type is Date. 1463 times[i].SetCoreTime(types.FromDate(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0)) 1464 times[i].SetType(b.tp.Tp) 1465 } 1466 } 1467 return nil 1468 } 1469 1470 func (b *builtinCastTimeAsStringSig) vectorized() bool { 1471 return true 1472 } 1473 1474 func (b *builtinCastTimeAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1475 n := input.NumRows() 1476 buf, err := b.bufSlabPredictor.get(types.ETTimestamp, n) 1477 if err != nil { 1478 return err 1479 } 1480 defer b.bufSlabPredictor.put(buf) 1481 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 1482 return err 1483 } 1484 1485 var res string 1486 var isNull bool 1487 sc := b.ctx.GetStochastikVars().StmtCtx 1488 vas := buf.Times() 1489 result.ReserveString(n) 1490 for i, v := range vas { 1491 if buf.IsNull(i) { 1492 result.AppendNull() 1493 continue 1494 } 1495 res, err = types.ProduceStrWithSpecifiedTp(v.String(), b.tp, sc, false) 1496 if err != nil { 1497 return err 1498 } 1499 res, isNull, err = padZeroForBinaryType(res, b.tp, b.ctx) 1500 if err != nil { 1501 return err 1502 } 1503 if isNull { 1504 result.AppendNull() 1505 continue 1506 } 1507 result.AppendString(res) 1508 } 1509 return nil 1510 } 1511 1512 func (b *builtinCastJSONAsDecimalSig) vectorized() bool { 1513 return true 1514 } 1515 1516 func (b *builtinCastJSONAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1517 n := input.NumRows() 1518 buf, err := b.bufSlabPredictor.get(types.ETJson, n) 1519 if err != nil { 1520 return err 1521 } 1522 defer b.bufSlabPredictor.put(buf) 1523 if err = b.args[0].VecEvalJSON(b.ctx, input, buf); err != nil { 1524 return err 1525 } 1526 sc := b.ctx.GetStochastikVars().StmtCtx 1527 result.ResizeDecimal(n, false) 1528 result.MergeNulls(buf) 1529 res := result.Decimals() 1530 for i := 0; i < n; i++ { 1531 if result.IsNull(i) { 1532 continue 1533 } 1534 tempres, err := types.ConvertJSONToDecimal(sc, buf.GetJSON(i)) 1535 if err != nil { 1536 return err 1537 } 1538 tempres, err = types.ProduceDecWithSpecifiedTp(tempres, b.tp, sc) 1539 if err != nil { 1540 return err 1541 } 1542 res[i] = *tempres 1543 } 1544 return nil 1545 } 1546 1547 func (b *builtinCastStringAsRealSig) vectorized() bool { 1548 return true 1549 } 1550 1551 func (b *builtinCastStringAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1552 if IsBinaryLiteral(b.args[0]) { 1553 return b.args[0].VecEvalReal(b.ctx, input, result) 1554 } 1555 n := input.NumRows() 1556 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1557 if err != nil { 1558 return err 1559 } 1560 defer b.bufSlabPredictor.put(buf) 1561 if err = b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1562 return err 1563 } 1564 1565 result.ResizeFloat64(n, false) 1566 result.MergeNulls(buf) 1567 ret := result.Float64s() 1568 sc := b.ctx.GetStochastikVars().StmtCtx 1569 1570 for i := 0; i < n; i++ { 1571 if result.IsNull(i) { 1572 continue 1573 } 1574 res, err := types.StrToFloat(sc, buf.GetString(i), true) 1575 if err != nil { 1576 return err 1577 } 1578 if b.inUnion && allegrosql.HasUnsignedFlag(b.tp.Flag) && res < 0 { 1579 res = 0 1580 } 1581 res, err = types.ProduceFloatWithSpecifiedTp(res, b.tp, sc) 1582 if err != nil { 1583 return err 1584 } 1585 ret[i] = res 1586 } 1587 return nil 1588 } 1589 1590 func (b *builtinCastStringAsDecimalSig) vectorized() bool { 1591 return true 1592 } 1593 1594 func (b *builtinCastStringAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1595 if IsBinaryLiteral(b.args[0]) { 1596 return b.args[0].VecEvalDecimal(b.ctx, input, result) 1597 } 1598 n := input.NumRows() 1599 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1600 if err != nil { 1601 return err 1602 } 1603 defer b.bufSlabPredictor.put(buf) 1604 if err = b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1605 return err 1606 } 1607 result.ResizeDecimal(n, false) 1608 result.MergeNulls(buf) 1609 res := result.Decimals() 1610 stmtCtx := b.ctx.GetStochastikVars().StmtCtx 1611 for i := 0; i < n; i++ { 1612 if result.IsNull(i) { 1613 continue 1614 } 1615 val := strings.TrimSpace(buf.GetString(i)) 1616 isNegative := len(val) > 0 && val[0] == '-' 1617 dec := new(types.MyDecimal) 1618 if !(b.inUnion && allegrosql.HasUnsignedFlag(b.tp.Flag) && isNegative) { 1619 if err := stmtCtx.HandleTruncate(dec.FromString([]byte(val))); err != nil { 1620 return err 1621 } 1622 dec, err := types.ProduceDecWithSpecifiedTp(dec, b.tp, stmtCtx) 1623 if err != nil { 1624 return err 1625 } 1626 res[i] = *dec 1627 } 1628 } 1629 return nil 1630 } 1631 1632 func (b *builtinCastStringAsTimeSig) vectorized() bool { 1633 return true 1634 } 1635 1636 func (b *builtinCastStringAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1637 n := input.NumRows() 1638 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1639 if err != nil { 1640 return err 1641 } 1642 defer b.bufSlabPredictor.put(buf) 1643 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1644 return err 1645 } 1646 1647 result.ResizeTime(n, false) 1648 result.MergeNulls(buf) 1649 times := result.Times() 1650 stmtCtx := b.ctx.GetStochastikVars().StmtCtx 1651 fsp := int8(b.tp.Decimal) 1652 for i := 0; i < n; i++ { 1653 if result.IsNull(i) { 1654 continue 1655 } 1656 tm, err := types.ParseTime(stmtCtx, buf.GetString(i), b.tp.Tp, fsp) 1657 if err != nil { 1658 if err = handleInvalidTimeError(b.ctx, err); err != nil { 1659 return err 1660 } 1661 result.SetNull(i, true) 1662 continue 1663 } 1664 times[i] = tm 1665 if b.tp.Tp == allegrosql.TypeDate { 1666 // Truncate hh:mm:ss part if the type is Date. 1667 times[i].SetCoreTime(types.FromDate(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0)) 1668 } 1669 } 1670 return nil 1671 } 1672 1673 func (b *builtinCastDecimalAsIntSig) vectorized() bool { 1674 return true 1675 } 1676 1677 func (b *builtinCastDecimalAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1678 n := input.NumRows() 1679 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1680 if err != nil { 1681 return err 1682 } 1683 defer b.bufSlabPredictor.put(buf) 1684 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1685 return err 1686 } 1687 1688 result.ResizeInt64(n, false) 1689 result.MergeNulls(buf) 1690 i64s := result.Int64s() 1691 d64s := buf.Decimals() 1692 for i := 0; i < n; i++ { 1693 if result.IsNull(i) { 1694 continue 1695 } 1696 1697 // Round is needed for both unsigned and signed. 1698 to := d64s[i] 1699 err = d64s[i].Round(&to, 0, types.ModeHalfEven) 1700 if err != nil { 1701 return err 1702 } 1703 1704 if !allegrosql.HasUnsignedFlag(b.tp.Flag) { 1705 i64s[i], err = to.ToInt() 1706 } else if b.inUnion && to.IsNegative() { 1707 i64s[i] = 0 1708 } else { 1709 var uintRes uint64 1710 uintRes, err = to.ToUint() 1711 i64s[i] = int64(uintRes) 1712 } 1713 1714 if types.ErrOverflow.Equal(err) { 1715 warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", d64s[i]) 1716 err = b.ctx.GetStochastikVars().StmtCtx.HandleOverflow(err, warnErr) 1717 } 1718 1719 if err != nil { 1720 return err 1721 } 1722 } 1723 return nil 1724 } 1725 1726 func (b *builtinCastDecimalAsDurationSig) vectorized() bool { 1727 return true 1728 } 1729 1730 func (b *builtinCastDecimalAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1731 n := input.NumRows() 1732 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1733 if err != nil { 1734 return err 1735 } 1736 defer b.bufSlabPredictor.put(buf) 1737 if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1738 return err 1739 } 1740 1741 result.ResizeGoDuration(n, false) 1742 result.MergeNulls(buf) 1743 args := buf.Decimals() 1744 ds := result.GoDurations() 1745 for i := 0; i < n; i++ { 1746 if result.IsNull(i) { 1747 continue 1748 } 1749 dur, err := types.ParseDuration(b.ctx.GetStochastikVars().StmtCtx, string(args[i].ToString()), int8(b.tp.Decimal)) 1750 if err != nil { 1751 if types.ErrTruncatedWrongVal.Equal(err) { 1752 err = b.ctx.GetStochastikVars().StmtCtx.HandleTruncate(err) 1753 } 1754 if err != nil { 1755 return err 1756 } 1757 if dur == types.ZeroDuration { 1758 result.SetNull(i, true) 1759 continue 1760 } 1761 } 1762 ds[i] = dur.Duration 1763 } 1764 return nil 1765 } 1766 1767 func (b *builtinCastStringAsStringSig) vectorized() bool { 1768 return true 1769 } 1770 1771 func (b *builtinCastStringAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1772 n := input.NumRows() 1773 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1774 if err != nil { 1775 return err 1776 } 1777 defer b.bufSlabPredictor.put(buf) 1778 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1779 return err 1780 } 1781 1782 var res string 1783 var isNull bool 1784 sc := b.ctx.GetStochastikVars().StmtCtx 1785 result.ReserveString(n) 1786 for i := 0; i < n; i++ { 1787 if buf.IsNull(i) { 1788 result.AppendNull() 1789 continue 1790 } 1791 res, err = types.ProduceStrWithSpecifiedTp(buf.GetString(i), b.tp, sc, false) 1792 if err != nil { 1793 return err 1794 } 1795 res, isNull, err = padZeroForBinaryType(res, b.tp, b.ctx) 1796 if err != nil { 1797 return err 1798 } 1799 if isNull { 1800 result.AppendNull() 1801 continue 1802 } 1803 result.AppendString(res) 1804 } 1805 return nil 1806 } 1807 1808 func (b *builtinCastJSONAsDurationSig) vectorized() bool { 1809 return true 1810 } 1811 1812 func (b *builtinCastJSONAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1813 n := input.NumRows() 1814 buf, err := b.bufSlabPredictor.get(types.ETJson, n) 1815 if err != nil { 1816 return err 1817 } 1818 defer b.bufSlabPredictor.put(buf) 1819 if err := b.args[0].VecEvalJSON(b.ctx, input, buf); err != nil { 1820 return err 1821 } 1822 1823 ctx := b.ctx.GetStochastikVars().StmtCtx 1824 result.ResizeGoDuration(n, false) 1825 result.MergeNulls(buf) 1826 var dur types.Duration 1827 ds := result.GoDurations() 1828 for i := 0; i < n; i++ { 1829 if result.IsNull(i) { 1830 continue 1831 } 1832 s, err := buf.GetJSON(i).Unquote() 1833 if err != nil { 1834 return nil 1835 } 1836 dur, err = types.ParseDuration(ctx, s, int8(b.tp.Decimal)) 1837 if types.ErrTruncatedWrongVal.Equal(err) { 1838 err = ctx.HandleTruncate(err) 1839 } 1840 if err != nil { 1841 return err 1842 } 1843 ds[i] = dur.Duration 1844 } 1845 return nil 1846 } 1847 1848 func (b *builtinCastDecimalAsJSONSig) vectorized() bool { 1849 return true 1850 } 1851 1852 func (b *builtinCastDecimalAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1853 n := input.NumRows() 1854 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1855 if err != nil { 1856 return err 1857 } 1858 defer b.bufSlabPredictor.put(buf) 1859 if err = b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 1860 return err 1861 } 1862 1863 result.ReserveJSON(n) 1864 f64s := buf.Decimals() 1865 var f float64 1866 for i := 0; i < n; i++ { 1867 if buf.IsNull(i) { 1868 result.AppendNull() 1869 continue 1870 } 1871 // FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now. 1872 f, err = f64s[i].ToFloat64() 1873 if err != nil { 1874 return err 1875 } 1876 result.AppendJSON(json.CreateBinary(f)) 1877 } 1878 return nil 1879 } 1880 1881 func (b *builtinCastDurationAsJSONSig) vectorized() bool { 1882 return true 1883 } 1884 1885 func (b *builtinCastDurationAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1886 n := input.NumRows() 1887 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1888 if err != nil { 1889 return err 1890 } 1891 defer b.bufSlabPredictor.put(buf) 1892 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1893 return err 1894 } 1895 1896 result.ReserveJSON(n) 1897 var dur types.Duration 1898 dur.Fsp = types.MaxFsp 1899 ds := buf.GoDurations() 1900 for i := 0; i < n; i++ { 1901 if buf.IsNull(i) { 1902 result.AppendNull() 1903 continue 1904 } 1905 dur.Duration = ds[i] 1906 result.AppendJSON(json.CreateBinary(dur.String())) 1907 } 1908 return nil 1909 }