github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_time_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 "strconv" 20 "strings" 21 "time" 22 23 "github.com/whtcorpsinc/errors" 24 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 25 "github.com/whtcorpsinc/BerolinaSQL/terror" 26 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 27 "github.com/whtcorpsinc/milevadb/causetstore/einsteindb/oracle" 28 "github.com/whtcorpsinc/milevadb/types" 29 "github.com/whtcorpsinc/milevadb/soliton/chunk" 30 ) 31 32 func (b *builtinMonthSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 33 n := input.NumEvents() 34 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 35 if err != nil { 36 return err 37 } 38 defer b.bufSlabPredictor.put(buf) 39 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 40 return err 41 } 42 43 result.ResizeInt64(n, false) 44 result.MergeNulls(buf) 45 i64s := result.Int64s() 46 ds := buf.Times() 47 for i := 0; i < input.NumEvents(); i++ { 48 if result.IsNull(i) { 49 continue 50 } 51 if ds[i].IsZero() { 52 if b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode() { 53 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 54 return err 55 } 56 result.SetNull(i, true) 57 continue 58 } 59 i64s[i] = 0 60 continue 61 } 62 i64s[i] = int64(ds[i].Month()) 63 } 64 return nil 65 } 66 67 func (b *builtinMonthSig) vectorized() bool { 68 return true 69 } 70 71 func (b *builtinYearSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 72 n := input.NumEvents() 73 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 74 if err != nil { 75 return err 76 } 77 defer b.bufSlabPredictor.put(buf) 78 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 79 return err 80 } 81 82 result.ResizeInt64(n, false) 83 result.MergeNulls(buf) 84 i64s := result.Int64s() 85 ds := buf.Times() 86 for i := 0; i < input.NumEvents(); i++ { 87 if result.IsNull(i) { 88 continue 89 } 90 if ds[i].IsZero() { 91 if b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode() { 92 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 93 return err 94 } 95 result.SetNull(i, true) 96 continue 97 } 98 i64s[i] = 0 99 continue 100 } 101 i64s[i] = int64(ds[i].Year()) 102 } 103 return nil 104 } 105 106 func (b *builtinYearSig) vectorized() bool { 107 return true 108 } 109 110 func (b *builtinDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 111 if err := b.args[0].VecEvalTime(b.ctx, input, result); err != nil { 112 return err 113 } 114 times := result.Times() 115 for i := 0; i < len(times); i++ { 116 if result.IsNull(i) { 117 continue 118 } 119 if times[i].IsZero() { 120 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, times[i].String())); err != nil { 121 return err 122 } 123 result.SetNull(i, true) 124 } else { 125 times[i].SetCoreTime(types.FromDate(times[i].Year(), times[i].Month(), times[i].Day(), 0, 0, 0, 0)) 126 times[i].SetType(allegrosql.TypeDate) 127 } 128 } 129 return nil 130 } 131 132 func (b *builtinDateSig) vectorized() bool { 133 return true 134 } 135 136 func (b *builtinFromUnixTime2ArgSig) vectorized() bool { 137 return true 138 } 139 140 func (b *builtinFromUnixTime2ArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 141 n := input.NumEvents() 142 buf1, err := b.bufSlabPredictor.get(types.ETDecimal, n) 143 if err != nil { 144 return err 145 } 146 defer b.bufSlabPredictor.put(buf1) 147 if err = b.args[0].VecEvalDecimal(b.ctx, input, buf1); err != nil { 148 return err 149 } 150 151 buf2, err := b.bufSlabPredictor.get(types.ETString, n) 152 if err != nil { 153 return err 154 } 155 defer b.bufSlabPredictor.put(buf2) 156 if err = b.args[1].VecEvalString(b.ctx, input, buf2); err != nil { 157 return err 158 } 159 160 result.ReserveString(n) 161 ds := buf1.Decimals() 162 fsp := int8(b.tp.Decimal) 163 for i := 0; i < n; i++ { 164 if buf1.IsNull(i) || buf2.IsNull(i) { 165 result.AppendNull() 166 continue 167 } 168 t, isNull, err := evalFromUnixTime(b.ctx, fsp, &ds[i]) 169 if err != nil { 170 return err 171 } 172 if isNull { 173 result.AppendNull() 174 continue 175 } 176 res, err := t.DateFormat(buf2.GetString(i)) 177 if err != nil { 178 return err 179 } 180 result.AppendString(res) 181 } 182 return nil 183 } 184 185 func (b *builtinSysDateWithoutFspSig) vectorized() bool { 186 return true 187 } 188 189 func (b *builtinSysDateWithoutFspSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 190 n := input.NumEvents() 191 loc := b.ctx.GetStochastikVars().Location() 192 now := time.Now().In(loc) 193 194 result.ResizeTime(n, false) 195 times := result.Times() 196 t, err := convertTimeToMysqlTime(now, 0, types.ModeHalfEven) 197 if err != nil { 198 return err 199 } 200 for i := 0; i < n; i++ { 201 times[i] = t 202 } 203 return nil 204 } 205 206 func (b *builtinExtractDatetimeSig) vectorized() bool { 207 return true 208 } 209 210 func (b *builtinExtractDatetimeSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 211 n := input.NumEvents() 212 buf, err := b.bufSlabPredictor.get(types.ETString, n) 213 if err != nil { 214 return err 215 } 216 defer b.bufSlabPredictor.put(buf) 217 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 218 return err 219 } 220 221 buf1, err := b.bufSlabPredictor.get(types.ETDatetime, n) 222 if err != nil { 223 return err 224 } 225 defer b.bufSlabPredictor.put(buf1) 226 if err := b.args[1].VecEvalTime(b.ctx, input, buf1); err != nil { 227 return err 228 } 229 230 result.ResizeInt64(n, false) 231 i64s := result.Int64s() 232 ds := buf1.Times() 233 result.MergeNulls(buf, buf1) 234 for i := 0; i < n; i++ { 235 if result.IsNull(i) { 236 continue 237 } 238 res, err := types.ExtractDatetimeNum(&ds[i], buf.GetString(i)) 239 if err != nil { 240 return err 241 } 242 i64s[i] = res 243 } 244 return nil 245 } 246 247 func (b *builtinDayNameSig) vectorized() bool { 248 return true 249 } 250 251 func (b *builtinDayNameSig) vecEvalIndex(input *chunk.Chunk, apply func(i, res int), applyNull func(i int)) error { 252 n := input.NumEvents() 253 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 254 if err != nil { 255 return err 256 } 257 defer b.bufSlabPredictor.put(buf) 258 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 259 return err 260 } 261 262 ds := buf.Times() 263 for i := 0; i < n; i++ { 264 if buf.IsNull(i) { 265 applyNull(i) 266 continue 267 } 268 if ds[i].InvalidZero() { 269 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 270 return err 271 } 272 applyNull(i) 273 continue 274 } 275 // Monday is 0, ... Sunday = 6 in MyALLEGROSQL 276 // but in go, Sunday is 0, ... Saturday is 6 277 // we will do a conversion. 278 res := (int(ds[i].Weekday()) + 6) % 7 279 apply(i, res) 280 } 281 return nil 282 } 283 284 // vecEvalString evals a builtinDayNameSig. 285 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_dayname 286 func (b *builtinDayNameSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 287 n := input.NumEvents() 288 result.ReserveString(n) 289 290 return b.vecEvalIndex(input, 291 func(i, res int) { 292 result.AppendString(types.WeekdayNames[res]) 293 }, 294 func(i int) { 295 result.AppendNull() 296 }, 297 ) 298 } 299 300 func (b *builtinDayNameSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 301 n := input.NumEvents() 302 result.ResizeFloat64(n, false) 303 f64s := result.Float64s() 304 305 return b.vecEvalIndex(input, 306 func(i, res int) { 307 f64s[i] = float64(res) 308 }, 309 func(i int) { 310 result.SetNull(i, true) 311 }, 312 ) 313 } 314 315 func (b *builtinDayNameSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 316 n := input.NumEvents() 317 result.ResizeInt64(n, false) 318 i64s := result.Int64s() 319 320 return b.vecEvalIndex(input, 321 func(i, res int) { 322 i64s[i] = int64(res) 323 }, 324 func(i int) { 325 result.SetNull(i, true) 326 }, 327 ) 328 } 329 330 func (b *builtinWeekDaySig) vectorized() bool { 331 return true 332 } 333 334 func (b *builtinWeekDaySig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 335 n := input.NumEvents() 336 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 337 if err != nil { 338 return err 339 } 340 defer b.bufSlabPredictor.put(buf) 341 if err = b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 342 return err 343 } 344 345 result.ResizeInt64(n, false) 346 result.MergeNulls(buf) 347 i64s := result.Int64s() 348 ds := buf.Times() 349 for i := 0; i < input.NumEvents(); i++ { 350 if result.IsNull(i) { 351 continue 352 } 353 if ds[i].IsZero() { 354 if err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 355 return err 356 } 357 result.SetNull(i, true) 358 continue 359 } 360 i64s[i] = int64((ds[i].Weekday() + 6) % 7) 361 } 362 return nil 363 } 364 365 func (b *builtinTimeFormatSig) vectorized() bool { 366 return true 367 } 368 369 func (b *builtinTimeFormatSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 370 n := input.NumEvents() 371 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 372 if err != nil { 373 return err 374 } 375 defer b.bufSlabPredictor.put(buf) 376 if err := b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 377 // If err != nil, then dur is ZeroDuration, outputs 00:00:00 378 // in this case which follows the behavior of allegrosql. 379 // Use the non-vectorized method to handle this HoTT of error. 380 return vecEvalStringByEvents(b, input, result) 381 } 382 383 buf1, err1 := b.bufSlabPredictor.get(types.ETString, n) 384 if err1 != nil { 385 return err1 386 } 387 defer b.bufSlabPredictor.put(buf1) 388 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 389 return err 390 } 391 392 result.ReserveString(n) 393 for i := 0; i < n; i++ { 394 if buf.IsNull(i) || buf1.IsNull(i) { 395 result.AppendNull() 396 continue 397 } 398 res, err := b.formatTime(b.ctx, buf.GetDuration(i, 0), buf1.GetString(i)) 399 if err != nil { 400 return err 401 } 402 result.AppendString(res) 403 } 404 return nil 405 } 406 407 func (b *builtinUTCTimeWithArgSig) vectorized() bool { 408 return true 409 } 410 411 // vecEvalDuration evals a builtinUTCTimeWithArgSig. 412 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-time 413 func (b *builtinUTCTimeWithArgSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 414 n := input.NumEvents() 415 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 416 if err != nil { 417 return err 418 } 419 defer b.bufSlabPredictor.put(buf) 420 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 421 return err 422 } 423 nowTs, err := getStmtTimestamp(b.ctx) 424 if err != nil { 425 return err 426 } 427 utc := nowTs.UTC().Format(types.TimeFSPFormat) 428 stmtCtx := b.ctx.GetStochastikVars().StmtCtx 429 result.ResizeGoDuration(n, false) 430 d64s := result.GoDurations() 431 i64s := buf.Int64s() 432 result.MergeNulls(buf) 433 for i := 0; i < n; i++ { 434 if result.IsNull(i) { 435 continue 436 } 437 fsp := i64s[i] 438 if fsp > int64(types.MaxFsp) { 439 return errors.Errorf("Too-big precision %v specified for 'utc_time'. Maximum is %v.", fsp, types.MaxFsp) 440 } 441 if fsp < int64(types.MinFsp) { 442 return errors.Errorf("Invalid negative %d specified, must in [0, 6].", fsp) 443 } 444 res, err := types.ParseDuration(stmtCtx, utc, int8(fsp)) 445 if err != nil { 446 return err 447 } 448 d64s[i] = res.Duration 449 } 450 return nil 451 } 452 453 func (b *builtinUnixTimestampCurrentSig) vectorized() bool { 454 return true 455 } 456 457 func (b *builtinUnixTimestampCurrentSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 458 nowTs, err := getStmtTimestamp(b.ctx) 459 if err != nil { 460 return err 461 } 462 dec, err := goTimeToMysqlUnixTimestamp(nowTs, 1) 463 if err != nil { 464 return err 465 } 466 intVal, err := dec.ToInt() 467 if !terror.ErrorEqual(err, types.ErrTruncated) { 468 terror.Log(err) 469 } 470 n := input.NumEvents() 471 result.ResizeInt64(n, false) 472 intRes := result.Int64s() 473 for i := 0; i < n; i++ { 474 intRes[i] = intVal 475 } 476 return nil 477 } 478 479 func (b *builtinYearWeekWithoutModeSig) vectorized() bool { 480 return true 481 } 482 483 // vecEvalInt evals YEARWEEK(date). 484 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_yearweek 485 func (b *builtinYearWeekWithoutModeSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 486 n := input.NumEvents() 487 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 488 if err != nil { 489 return err 490 } 491 defer b.bufSlabPredictor.put(buf) 492 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 493 return err 494 } 495 496 result.ResizeInt64(n, false) 497 result.MergeNulls(buf) 498 i64s := result.Int64s() 499 ds := buf.Times() 500 for i := 0; i < n; i++ { 501 if result.IsNull(i) { 502 continue 503 } 504 date := ds[i] 505 if date.InvalidZero() { 506 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String())); err != nil { 507 return err 508 } 509 result.SetNull(i, true) 510 continue 511 } 512 year, week := date.YearWeek(0) 513 i64s[i] = int64(week + year*100) 514 if i64s[i] < 0 { 515 i64s[i] = int64(math.MaxUint32) 516 } 517 } 518 return nil 519 } 520 521 func (b *builtinPeriodDiffSig) vectorized() bool { 522 return true 523 } 524 525 // vecEvalInt evals PERIOD_DIFF(P1,P2). 526 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_period-diff 527 func (b *builtinPeriodDiffSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 528 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 529 return err 530 } 531 532 n := input.NumEvents() 533 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 534 if err != nil { 535 return err 536 } 537 defer b.bufSlabPredictor.put(buf) 538 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 539 return err 540 } 541 542 i64s := result.Int64s() 543 periods := buf.Int64s() 544 result.MergeNulls(buf) 545 for i := 0; i < n; i++ { 546 if result.IsNull(i) { 547 continue 548 } 549 if !validPeriod(i64s[i]) || !validPeriod(periods[i]) { 550 return errIncorrectArgs.GenWithStackByArgs("period_diff") 551 } 552 i64s[i] = int64(period2Month(uint64(i64s[i])) - period2Month(uint64(periods[i]))) 553 } 554 return nil 555 } 556 557 func (b *builtinNowWithArgSig) vectorized() bool { 558 return true 559 } 560 561 func (b *builtinNowWithArgSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 562 n := input.NumEvents() 563 bufFsp, err := b.bufSlabPredictor.get(types.ETInt, n) 564 if err != nil { 565 return err 566 } 567 defer b.bufSlabPredictor.put(bufFsp) 568 if err = b.args[0].VecEvalInt(b.ctx, input, bufFsp); err != nil { 569 return err 570 } 571 572 result.ResizeTime(n, false) 573 times := result.Times() 574 fsps := bufFsp.Int64s() 575 576 for i := 0; i < n; i++ { 577 fsp := int8(0) 578 if !bufFsp.IsNull(i) { 579 if fsps[i] > int64(types.MaxFsp) { 580 return errors.Errorf("Too-big precision %v specified for 'now'. Maximum is %v.", fsps[i], types.MaxFsp) 581 } 582 if fsps[i] < int64(types.MinFsp) { 583 return errors.Errorf("Invalid negative %d specified, must in [0, 6].", fsps[i]) 584 } 585 fsp = int8(fsps[i]) 586 } 587 588 t, isNull, err := evalNowWithFsp(b.ctx, fsp) 589 if err != nil { 590 return err 591 } 592 if isNull { 593 result.SetNull(i, true) 594 continue 595 } 596 597 times[i] = t 598 } 599 return nil 600 } 601 602 func (b *builtinGetFormatSig) vectorized() bool { 603 return true 604 } 605 606 // vecEvalString evals a builtinGetFormatSig. 607 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_get-format 608 func (b *builtinGetFormatSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 609 n := input.NumEvents() 610 buf0, err := b.bufSlabPredictor.get(types.ETString, n) 611 if err != nil { 612 return err 613 } 614 defer b.bufSlabPredictor.put(buf0) 615 if err = b.args[0].VecEvalString(b.ctx, input, buf0); err != nil { 616 return err 617 } 618 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 619 if err != nil { 620 return err 621 } 622 defer b.bufSlabPredictor.put(buf1) 623 if err = b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 624 return err 625 } 626 627 result.ReserveString(n) 628 for i := 0; i < n; i++ { 629 if buf0.IsNull(i) || buf1.IsNull(i) { 630 result.AppendNull() 631 continue 632 } 633 format := buf0.GetString(i) 634 location := buf1.GetString(i) 635 res := b.getFormat(format, location) 636 result.AppendString(res) 637 } 638 return nil 639 } 640 641 func (b *builtinGetFormatSig) getFormat(format, location string) string { 642 res := "" 643 switch format { 644 case dateFormat: 645 switch location { 646 case usaLocation: 647 res = "%m.%d.%Y" 648 case jisLocation: 649 res = "%Y-%m-%d" 650 case isoLocation: 651 res = "%Y-%m-%d" 652 case eurLocation: 653 res = "%d.%m.%Y" 654 case internalLocation: 655 res = "%Y%m%d" 656 } 657 case datetimeFormat, timestampFormat: 658 switch location { 659 case usaLocation: 660 res = "%Y-%m-%d %H.%i.%s" 661 case jisLocation: 662 res = "%Y-%m-%d %H:%i:%s" 663 case isoLocation: 664 res = "%Y-%m-%d %H:%i:%s" 665 case eurLocation: 666 res = "%Y-%m-%d %H.%i.%s" 667 case internalLocation: 668 res = "%Y%m%d%H%i%s" 669 } 670 case timeFormat: 671 switch location { 672 case usaLocation: 673 res = "%h:%i:%s %p" 674 case jisLocation: 675 res = "%H:%i:%s" 676 case isoLocation: 677 res = "%H:%i:%s" 678 case eurLocation: 679 res = "%H.%i.%s" 680 case internalLocation: 681 res = "%H%i%s" 682 } 683 } 684 return res 685 } 686 687 func (b *builtinLastDaySig) vectorized() bool { 688 return true 689 } 690 691 func (b *builtinLastDaySig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 692 n := input.NumEvents() 693 if err := b.args[0].VecEvalTime(b.ctx, input, result); err != nil { 694 return err 695 } 696 times := result.Times() 697 for i := 0; i < n; i++ { 698 if result.IsNull(i) { 699 continue 700 } 701 if times[i].InvalidZero() { 702 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, times[i].String())); err != nil { 703 return err 704 } 705 result.SetNull(i, true) 706 continue 707 } 708 tm := times[i] 709 year, month := tm.Year(), tm.Month() 710 lastDay := types.GetLastDay(year, month) 711 times[i] = types.NewTime(types.FromDate(year, month, lastDay, 0, 0, 0, 0), allegrosql.TypeDate, types.DefaultFsp) 712 } 713 return nil 714 } 715 716 func (b *builtinStrToDateDateSig) vectorized() bool { 717 return true 718 } 719 720 func (b *builtinStrToDateDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 721 n := input.NumEvents() 722 bufStrings, err := b.bufSlabPredictor.get(types.ETString, n) 723 if err != nil { 724 return err 725 } 726 defer b.bufSlabPredictor.put(bufStrings) 727 if err := b.args[0].VecEvalString(b.ctx, input, bufStrings); err != nil { 728 return err 729 } 730 731 bufFormats, err := b.bufSlabPredictor.get(types.ETString, n) 732 if err != nil { 733 return err 734 } 735 defer b.bufSlabPredictor.put(bufFormats) 736 if err := b.args[1].VecEvalString(b.ctx, input, bufFormats); err != nil { 737 return err 738 } 739 740 result.ResizeTime(n, false) 741 result.MergeNulls(bufStrings, bufFormats) 742 times := result.Times() 743 sc := b.ctx.GetStochastikVars().StmtCtx 744 for i := 0; i < n; i++ { 745 if result.IsNull(i) { 746 continue 747 } 748 var t types.Time 749 succ := t.StrToDate(sc, bufStrings.GetString(i), bufFormats.GetString(i)) 750 if !succ { 751 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); err != nil { 752 return err 753 } 754 result.SetNull(i, true) 755 continue 756 } 757 if b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode() && (t.Year() == 0 || t.Month() == 0 || t.Day() == 0) { 758 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); err != nil { 759 return err 760 } 761 result.SetNull(i, true) 762 continue 763 } 764 t.SetType(allegrosql.TypeDate) 765 t.SetFsp(types.MinFsp) 766 times[i] = t 767 } 768 return nil 769 } 770 771 func (b *builtinSysDateWithFspSig) vectorized() bool { 772 return true 773 } 774 775 func (b *builtinSysDateWithFspSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 776 n := input.NumEvents() 777 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 778 if err != nil { 779 return err 780 } 781 defer b.bufSlabPredictor.put(buf) 782 if err = b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 783 return err 784 } 785 786 loc := b.ctx.GetStochastikVars().Location() 787 now := time.Now().In(loc) 788 789 result.ResizeTime(n, false) 790 result.MergeNulls(buf) 791 times := result.Times() 792 ds := buf.Int64s() 793 794 for i := 0; i < n; i++ { 795 if result.IsNull(i) { 796 continue 797 } 798 t, err := convertTimeToMysqlTime(now, int8(ds[i]), types.ModeHalfEven) 799 if err != nil { 800 return err 801 } 802 times[i] = t 803 } 804 return nil 805 } 806 807 func (b *builtinMilevaDBParseTsoSig) vectorized() bool { 808 return true 809 } 810 811 func (b *builtinMilevaDBParseTsoSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 812 n := input.NumEvents() 813 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 814 if err != nil { 815 return err 816 } 817 defer b.bufSlabPredictor.put(buf) 818 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 819 return err 820 } 821 args := buf.Int64s() 822 result.ResizeTime(n, false) 823 result.MergeNulls(buf) 824 times := result.Times() 825 for i := 0; i < n; i++ { 826 if result.IsNull(i) { 827 continue 828 } 829 if args[i] <= 0 { 830 result.SetNull(i, true) 831 continue 832 } 833 t := oracle.GetTimeFromTS(uint64(args[i])) 834 r := types.NewTime(types.FromGoTime(t), allegrosql.TypeDatetime, types.MaxFsp) 835 if err := r.ConvertTimeZone(time.Local, b.ctx.GetStochastikVars().Location()); err != nil { 836 return err 837 } 838 times[i] = r 839 } 840 return nil 841 } 842 843 func (b *builtinFromDaysSig) vectorized() bool { 844 return true 845 } 846 847 func (b *builtinFromDaysSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 848 n := input.NumEvents() 849 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 850 if err != nil { 851 return err 852 } 853 defer b.bufSlabPredictor.put(buf) 854 if err = b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 855 return err 856 } 857 858 result.ResizeTime(n, false) 859 result.MergeNulls(buf) 860 ts := result.Times() 861 i64s := buf.Int64s() 862 for i := 0; i < n; i++ { 863 if result.IsNull(i) { 864 continue 865 } 866 ts[i] = types.TimeFromDays(i64s[i]) 867 } 868 return nil 869 } 870 871 func (b *builtinMicroSecondSig) vectorized() bool { 872 return true 873 } 874 875 func (b *builtinMicroSecondSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 876 n := input.NumEvents() 877 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 878 if err != nil { 879 return err 880 } 881 defer b.bufSlabPredictor.put(buf) 882 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 883 return vecEvalIntByEvents(b, input, result) 884 } 885 886 result.ResizeInt64(n, false) 887 result.MergeNulls(buf) 888 i64s := result.Int64s() 889 ds := buf.GoDurations() 890 for i := 0; i < n; i++ { 891 if result.IsNull(i) { 892 continue 893 } 894 i64s[i] = int64((ds[i] % time.Second) / time.Microsecond) 895 } 896 return nil 897 } 898 899 func (b *builtinQuarterSig) vectorized() bool { 900 return true 901 } 902 903 // vecEvalInt evals QUARTER(date). 904 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_quarter 905 func (b *builtinQuarterSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 906 n := input.NumEvents() 907 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 908 if err != nil { 909 return err 910 } 911 defer b.bufSlabPredictor.put(buf) 912 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 913 return err 914 } 915 916 result.ResizeInt64(n, false) 917 result.MergeNulls(buf) 918 i64s := result.Int64s() 919 ds := buf.Times() 920 for i := 0; i < n; i++ { 921 if result.IsNull(i) { 922 continue 923 } 924 date := ds[i] 925 if date.IsZero() { 926 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String())); err != nil { 927 return err 928 } 929 result.SetNull(i, true) 930 continue 931 } 932 i64s[i] = int64((date.Month() + 2) / 3) 933 } 934 return nil 935 } 936 937 func (b *builtinWeekWithModeSig) vectorized() bool { 938 return true 939 } 940 941 func (b *builtinWeekWithModeSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 942 n := input.NumEvents() 943 buf1, err := b.bufSlabPredictor.get(types.ETDatetime, n) 944 if err != nil { 945 return err 946 } 947 if err := b.args[0].VecEvalTime(b.ctx, input, buf1); err != nil { 948 return err 949 } 950 defer b.bufSlabPredictor.put(buf1) 951 952 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 953 if err != nil { 954 return err 955 } 956 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 957 return err 958 } 959 defer b.bufSlabPredictor.put(buf2) 960 961 result.ResizeInt64(n, false) 962 i64s := result.Int64s() 963 ds := buf1.Times() 964 ms := buf2.Int64s() 965 for i := 0; i < n; i++ { 966 if buf1.IsNull(i) { 967 result.SetNull(i, true) 968 continue 969 } 970 date := ds[i] 971 if date.IsZero() { 972 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String())); err != nil { 973 return err 974 } 975 result.SetNull(i, true) 976 continue 977 } 978 if buf2.IsNull(i) { 979 result.SetNull(i, true) 980 continue 981 } 982 mode := int(ms[i]) 983 week := date.Week(mode) 984 i64s[i] = int64(week) 985 } 986 return nil 987 } 988 989 func (b *builtinExtractDurationSig) vectorized() bool { 990 return true 991 } 992 993 func (b *builtinExtractDurationSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 994 n := input.NumEvents() 995 unit, err := b.bufSlabPredictor.get(types.ETString, n) 996 if err != nil { 997 return err 998 } 999 defer b.bufSlabPredictor.put(unit) 1000 if err := b.args[0].VecEvalString(b.ctx, input, unit); err != nil { 1001 return err 1002 } 1003 dur, err := b.bufSlabPredictor.get(types.ETDuration, n) 1004 if err != nil { 1005 return err 1006 } 1007 defer b.bufSlabPredictor.put(dur) 1008 if err = b.args[1].VecEvalDuration(b.ctx, input, dur); err != nil { 1009 return err 1010 } 1011 result.ResizeInt64(n, false) 1012 result.MergeNulls(unit, dur) 1013 i64s := result.Int64s() 1014 durIs := dur.GoDurations() 1015 var duration types.Duration 1016 duration.Fsp = int8(b.args[1].GetType().Decimal) 1017 for i := 0; i < n; i++ { 1018 if result.IsNull(i) { 1019 continue 1020 } 1021 unitI := unit.GetString(i) 1022 duration.Duration = durIs[i] 1023 i64s[i], err = types.ExtractDurationNum(&duration, unitI) 1024 if err != nil { 1025 return err 1026 } 1027 } 1028 return nil 1029 } 1030 1031 func (b *builtinStrToDateDurationSig) vectorized() bool { 1032 return true 1033 } 1034 1035 func (b *builtinStrToDateDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1036 n := input.NumEvents() 1037 bufStrings, err := b.bufSlabPredictor.get(types.ETString, n) 1038 if err != nil { 1039 return err 1040 } 1041 defer b.bufSlabPredictor.put(bufStrings) 1042 if err := b.args[0].VecEvalString(b.ctx, input, bufStrings); err != nil { 1043 return err 1044 } 1045 1046 bufFormats, err := b.bufSlabPredictor.get(types.ETString, n) 1047 if err != nil { 1048 return err 1049 } 1050 defer b.bufSlabPredictor.put(bufFormats) 1051 if err := b.args[1].VecEvalString(b.ctx, input, bufFormats); err != nil { 1052 return err 1053 } 1054 1055 result.ResizeGoDuration(n, false) 1056 result.MergeNulls(bufStrings, bufFormats) 1057 d64s := result.GoDurations() 1058 sc := b.ctx.GetStochastikVars().StmtCtx 1059 hasNoZeroDateMode := b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode() 1060 for i := 0; i < n; i++ { 1061 if result.IsNull(i) { 1062 continue 1063 } 1064 var t types.Time 1065 succ := t.StrToDate(sc, bufStrings.GetString(i), bufFormats.GetString(i)) 1066 if !succ { 1067 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); err != nil { 1068 return err 1069 } 1070 result.SetNull(i, true) 1071 continue 1072 } 1073 if hasNoZeroDateMode && (t.Year() == 0 || t.Month() == 0 || t.Day() == 0) { 1074 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); err != nil { 1075 return err 1076 } 1077 result.SetNull(i, true) 1078 continue 1079 } 1080 t.SetFsp(int8(b.tp.Decimal)) 1081 dur, err := t.ConvertToDuration() 1082 if err != nil { 1083 return err 1084 } 1085 d64s[i] = dur.Duration 1086 } 1087 return nil 1088 } 1089 1090 func (b *builtinToSecondsSig) vectorized() bool { 1091 return true 1092 } 1093 1094 // vecEvalInt evals a builtinToSecondsSig. 1095 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_to-seconds 1096 func (b *builtinToSecondsSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1097 n := input.NumEvents() 1098 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1099 if err != nil { 1100 return err 1101 } 1102 defer b.bufSlabPredictor.put(buf) 1103 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 1104 return err 1105 } 1106 1107 result.ResizeInt64(n, false) 1108 result.MergeNulls(buf) 1109 i64s := result.Int64s() 1110 ds := buf.Times() 1111 for i := 0; i < n; i++ { 1112 if result.IsNull(i) { 1113 continue 1114 } 1115 arg := ds[i] 1116 ret := types.TimestamFIDeliff("SECOND", types.ZeroDate, arg) 1117 if ret == 0 { 1118 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, arg.String())); err != nil { 1119 return err 1120 } 1121 result.SetNull(i, true) 1122 continue 1123 } 1124 i64s[i] = ret 1125 } 1126 return nil 1127 } 1128 1129 func (b *builtinMinuteSig) vectorized() bool { 1130 return true 1131 } 1132 1133 func (b *builtinMinuteSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1134 n := input.NumEvents() 1135 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1136 if err != nil { 1137 return err 1138 } 1139 defer b.bufSlabPredictor.put(buf) 1140 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1141 return vecEvalIntByEvents(b, input, result) 1142 } 1143 1144 result.ResizeInt64(n, false) 1145 result.MergeNulls(buf) 1146 i64s := result.Int64s() 1147 for i := 0; i < n; i++ { 1148 if result.IsNull(i) { 1149 continue 1150 } 1151 i64s[i] = int64(buf.GetDuration(i, int(types.UnspecifiedFsp)).Minute()) 1152 } 1153 return nil 1154 } 1155 1156 func (b *builtinSecondSig) vectorized() bool { 1157 return true 1158 } 1159 1160 func (b *builtinSecondSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1161 n := input.NumEvents() 1162 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1163 if err != nil { 1164 return err 1165 } 1166 defer b.bufSlabPredictor.put(buf) 1167 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1168 return vecEvalIntByEvents(b, input, result) 1169 } 1170 1171 result.ResizeInt64(n, false) 1172 result.MergeNulls(buf) 1173 i64s := result.Int64s() 1174 for i := 0; i < n; i++ { 1175 if result.IsNull(i) { 1176 continue 1177 } 1178 i64s[i] = int64(buf.GetDuration(i, int(types.UnspecifiedFsp)).Second()) 1179 } 1180 return nil 1181 } 1182 1183 func (b *builtinNowWithoutArgSig) vectorized() bool { 1184 return true 1185 } 1186 1187 func (b *builtinNowWithoutArgSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1188 n := input.NumEvents() 1189 nowTs, isNull, err := evalNowWithFsp(b.ctx, int8(0)) 1190 if err != nil { 1191 return err 1192 } 1193 if isNull { 1194 result.ResizeTime(n, true) 1195 return nil 1196 } 1197 result.ResizeTime(n, false) 1198 times := result.Times() 1199 for i := 0; i < n; i++ { 1200 times[i] = nowTs 1201 } 1202 return nil 1203 } 1204 1205 func (b *builtinTimestampLiteralSig) vectorized() bool { 1206 return true 1207 } 1208 1209 func (b *builtinTimestampLiteralSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1210 n := input.NumEvents() 1211 result.ResizeTime(n, false) 1212 times := result.Times() 1213 for i := range times { 1214 times[i] = b.tm 1215 } 1216 return nil 1217 } 1218 1219 func (b *builtinMakeDateSig) vectorized() bool { 1220 return true 1221 } 1222 1223 func (b *builtinMakeDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1224 n := input.NumEvents() 1225 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 1226 if err != nil { 1227 return err 1228 } 1229 defer b.bufSlabPredictor.put(buf1) 1230 if err := b.args[0].VecEvalInt(b.ctx, input, buf1); err != nil { 1231 return err 1232 } 1233 1234 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1235 if err != nil { 1236 return err 1237 } 1238 defer b.bufSlabPredictor.put(buf2) 1239 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 1240 return err 1241 } 1242 1243 result.ResizeTime(n, false) 1244 result.MergeNulls(buf1, buf2) 1245 1246 times := result.Times() 1247 years := buf1.Int64s() 1248 days := buf2.Int64s() 1249 1250 for i := 0; i < n; i++ { 1251 if result.IsNull(i) { 1252 continue 1253 } 1254 if days[i] <= 0 || years[i] < 0 || years[i] > 9999 { 1255 result.SetNull(i, true) 1256 continue 1257 } 1258 if years[i] < 70 { 1259 years[i] += 2000 1260 } else if years[i] < 100 { 1261 years[i] += 1900 1262 } 1263 startTime := types.NewTime(types.FromDate(int(years[i]), 1, 1, 0, 0, 0, 0), allegrosql.TypeDate, 0) 1264 retTimestamp := types.TimestamFIDeliff("DAY", types.ZeroDate, startTime) 1265 if retTimestamp == 0 { 1266 if err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, startTime.String())); err != nil { 1267 return err 1268 } 1269 result.SetNull(i, true) 1270 continue 1271 } 1272 ret := types.TimeFromDays(retTimestamp + days[i] - 1) 1273 if ret.IsZero() || ret.Year() > 9999 { 1274 result.SetNull(i, true) 1275 continue 1276 } 1277 times[i] = ret 1278 } 1279 return nil 1280 } 1281 1282 func (b *builtinWeekOfYearSig) vectorized() bool { 1283 return true 1284 } 1285 1286 func (b *builtinWeekOfYearSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1287 n := input.NumEvents() 1288 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1289 if err != nil { 1290 return err 1291 } 1292 defer b.bufSlabPredictor.put(buf) 1293 if err = b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 1294 return err 1295 } 1296 result.ResizeInt64(n, false) 1297 result.MergeNulls(buf) 1298 i64s := result.Int64s() 1299 ds := buf.Times() 1300 for i := 0; i < n; i++ { 1301 if result.IsNull(i) { 1302 continue 1303 } 1304 if ds[i].IsZero() { 1305 if err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 1306 return err 1307 } 1308 result.SetNull(i, true) 1309 continue 1310 } 1311 week := ds[i].Week(3) 1312 i64s[i] = int64(week) 1313 } 1314 return nil 1315 } 1316 1317 func (b *builtinUTCTimestampWithArgSig) vectorized() bool { 1318 return true 1319 } 1320 1321 // vecEvalTime evals UTC_TIMESTAMP(fsp). 1322 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-timestamp 1323 func (b *builtinUTCTimestampWithArgSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1324 n := input.NumEvents() 1325 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 1326 if err != nil { 1327 return err 1328 } 1329 defer b.bufSlabPredictor.put(buf) 1330 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 1331 return err 1332 } 1333 result.ResizeTime(n, false) 1334 t64s := result.Times() 1335 i64s := buf.Int64s() 1336 result.MergeNulls(buf) 1337 for i := 0; i < n; i++ { 1338 if result.IsNull(i) { 1339 continue 1340 } 1341 fsp := i64s[i] 1342 if fsp > int64(types.MaxFsp) { 1343 return errors.Errorf("Too-big precision %v specified for 'utc_timestamp'. Maximum is %v.", fsp, types.MaxFsp) 1344 } 1345 if fsp < int64(types.MinFsp) { 1346 return errors.Errorf("Invalid negative %d specified, must in [0, 6].", fsp) 1347 } 1348 res, isNull, err := evalUTCTimestampWithFsp(b.ctx, int8(fsp)) 1349 if err != nil { 1350 return err 1351 } 1352 if isNull { 1353 result.SetNull(i, true) 1354 continue 1355 } 1356 t64s[i] = res 1357 } 1358 return nil 1359 } 1360 1361 func (b *builtinTimeToSecSig) vectorized() bool { 1362 return true 1363 } 1364 1365 // vecEvalInt evals TIME_TO_SEC(time). 1366 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_time-to-sec 1367 func (b *builtinTimeToSecSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1368 n := input.NumEvents() 1369 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1370 if err != nil { 1371 return err 1372 } 1373 defer b.bufSlabPredictor.put(buf) 1374 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1375 return err 1376 } 1377 1378 result.ResizeInt64(n, false) 1379 result.MergeNulls(buf) 1380 i64s := result.Int64s() 1381 fsp := b.args[0].GetType().Decimal 1382 for i := 0; i < n; i++ { 1383 if result.IsNull(i) { 1384 continue 1385 } 1386 var sign int 1387 duration := buf.GetDuration(i, fsp) 1388 if duration.Duration >= 0 { 1389 sign = 1 1390 } else { 1391 sign = -1 1392 } 1393 i64s[i] = int64(sign * (duration.Hour()*3600 + duration.Minute()*60 + duration.Second())) 1394 } 1395 return nil 1396 } 1397 1398 func (b *builtinStrToDateDatetimeSig) vectorized() bool { 1399 return true 1400 } 1401 1402 func (b *builtinStrToDateDatetimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1403 n := input.NumEvents() 1404 dateBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1405 if err != nil { 1406 return err 1407 } 1408 defer b.bufSlabPredictor.put(dateBuf) 1409 if err = b.args[0].VecEvalString(b.ctx, input, dateBuf); err != nil { 1410 return err 1411 } 1412 1413 formatBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1414 if err != nil { 1415 return err 1416 } 1417 defer b.bufSlabPredictor.put(formatBuf) 1418 if err = b.args[1].VecEvalString(b.ctx, input, formatBuf); err != nil { 1419 return err 1420 } 1421 1422 result.ResizeTime(n, false) 1423 result.MergeNulls(dateBuf, formatBuf) 1424 times := result.Times() 1425 sc := b.ctx.GetStochastikVars().StmtCtx 1426 hasNoZeroDateMode := b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode() 1427 fsp := int8(b.tp.Decimal) 1428 1429 for i := 0; i < n; i++ { 1430 if result.IsNull(i) { 1431 continue 1432 } 1433 var t types.Time 1434 succ := t.StrToDate(sc, dateBuf.GetString(i), formatBuf.GetString(i)) 1435 if !succ { 1436 if err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); err != nil { 1437 return err 1438 } 1439 result.SetNull(i, true) 1440 continue 1441 } 1442 if hasNoZeroDateMode && (t.Year() == 0 || t.Month() == 0 || t.Day() == 0) { 1443 if err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); err != nil { 1444 return err 1445 } 1446 result.SetNull(i, true) 1447 continue 1448 } 1449 t.SetType(allegrosql.TypeDatetime) 1450 t.SetFsp(fsp) 1451 times[i] = t 1452 } 1453 return nil 1454 } 1455 1456 func (b *builtinUTCDateSig) vectorized() bool { 1457 return true 1458 } 1459 1460 func (b *builtinUTCDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1461 nowTs, err := getStmtTimestamp(b.ctx) 1462 if err != nil { 1463 return err 1464 } 1465 year, month, day := nowTs.UTC().Date() 1466 utcDate := types.NewTime(types.FromGoTime(time.Date(year, month, day, 0, 0, 0, 0, time.UTC)), allegrosql.TypeDate, types.UnspecifiedFsp) 1467 1468 n := input.NumEvents() 1469 result.ResizeTime(n, false) 1470 times := result.Times() 1471 for i := 0; i < n; i++ { 1472 times[i] = utcDate 1473 } 1474 return nil 1475 } 1476 1477 func (b *builtinWeekWithoutModeSig) vectorized() bool { 1478 return true 1479 } 1480 1481 func (b *builtinWeekWithoutModeSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1482 n := input.NumEvents() 1483 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1484 if err != nil { 1485 return err 1486 } 1487 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 1488 return err 1489 } 1490 defer b.bufSlabPredictor.put(buf) 1491 1492 result.ResizeInt64(n, false) 1493 result.MergeNulls(buf) 1494 i64s := result.Int64s() 1495 ds := buf.Times() 1496 1497 mode := 0 1498 modeStr, ok := b.ctx.GetStochastikVars().GetSystemVar(variable.DefaultWeekFormat) 1499 if ok && modeStr != "" { 1500 mode, err = strconv.Atoi(modeStr) 1501 if err != nil { 1502 return handleInvalidTimeError(b.ctx, types.ErrInvalidWeekModeFormat.GenWithStackByArgs(modeStr)) 1503 } 1504 } 1505 for i := 0; i < n; i++ { 1506 if result.IsNull(i) { 1507 continue 1508 } 1509 date := ds[i] 1510 if date.IsZero() { 1511 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String())); err != nil { 1512 return err 1513 } 1514 result.SetNull(i, true) 1515 continue 1516 } 1517 1518 week := date.Week(mode) 1519 i64s[i] = int64(week) 1520 } 1521 return nil 1522 } 1523 1524 func (b *builtinUnixTimestamFIDelecSig) vectorized() bool { 1525 return true 1526 } 1527 1528 func (b *builtinUnixTimestamFIDelecSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1529 n := input.NumEvents() 1530 result.ResizeDecimal(n, false) 1531 ts := result.Decimals() 1532 timeBuf, err := b.bufSlabPredictor.get(types.ETTimestamp, n) 1533 if err != nil { 1534 return err 1535 } 1536 defer b.bufSlabPredictor.put(timeBuf) 1537 if err := b.args[0].VecEvalTime(b.ctx, input, timeBuf); err != nil { 1538 for i := 0; i < n; i++ { 1539 temp, isNull, err := b.evalDecimal(input.GetEvent(i)) 1540 if err != nil { 1541 return err 1542 } 1543 ts[i] = *temp 1544 if isNull { 1545 result.SetNull(i, true) 1546 } 1547 } 1548 } else { 1549 result.MergeNulls(timeBuf) 1550 for i := 0; i < n; i++ { 1551 if result.IsNull(i) { 1552 continue 1553 } 1554 t, err := timeBuf.GetTime(i).GoTime(getTimeZone(b.ctx)) 1555 if err != nil { 1556 ts[i] = *new(types.MyDecimal) 1557 continue 1558 } 1559 tmp, err := goTimeToMysqlUnixTimestamp(t, b.tp.Decimal) 1560 if err != nil { 1561 return err 1562 } 1563 ts[i] = *tmp 1564 } 1565 } 1566 return nil 1567 } 1568 1569 func (b *builtinPeriodAddSig) vectorized() bool { 1570 return true 1571 } 1572 1573 // vecEvalInt evals PERIOD_ADD(P,N). 1574 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_period-add 1575 func (b *builtinPeriodAddSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1576 if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil { 1577 return err 1578 } 1579 1580 n := input.NumEvents() 1581 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 1582 if err != nil { 1583 return err 1584 } 1585 defer b.bufSlabPredictor.put(buf) 1586 if err := b.args[1].VecEvalInt(b.ctx, input, buf); err != nil { 1587 return err 1588 } 1589 i64s := result.Int64s() 1590 result.MergeNulls(buf) 1591 ns := buf.Int64s() 1592 for i := 0; i < n; i++ { 1593 if result.IsNull(i) { 1594 continue 1595 } 1596 1597 // in MyALLEGROSQL, if p is invalid but n is NULL, the result is NULL, so we have to check if n is NULL first. 1598 if !validPeriod(i64s[i]) { 1599 return errIncorrectArgs.GenWithStackByArgs("period_add") 1600 } 1601 sumMonth := int64(period2Month(uint64(i64s[i]))) + ns[i] 1602 i64s[i] = int64(month2Period(uint64(sumMonth))) 1603 } 1604 return nil 1605 } 1606 1607 func (b *builtinTimestampAddSig) vectorized() bool { 1608 return true 1609 } 1610 1611 // vecEvalString evals a builtinTimestampAddSig. 1612 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampadd 1613 func (b *builtinTimestampAddSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1614 n := input.NumEvents() 1615 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1616 if err != nil { 1617 return err 1618 } 1619 defer b.bufSlabPredictor.put(buf) 1620 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1621 return err 1622 } 1623 1624 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 1625 if err != nil { 1626 return err 1627 } 1628 defer b.bufSlabPredictor.put(buf1) 1629 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 1630 return err 1631 } 1632 1633 buf2, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1634 if err != nil { 1635 return err 1636 } 1637 defer b.bufSlabPredictor.put(buf2) 1638 if err := b.args[2].VecEvalTime(b.ctx, input, buf2); err != nil { 1639 return err 1640 } 1641 1642 result.ReserveString(n) 1643 nums := buf1.Int64s() 1644 ds := buf2.Times() 1645 for i := 0; i < n; i++ { 1646 if buf.IsNull(i) || buf1.IsNull(i) || buf2.IsNull(i) { 1647 result.AppendNull() 1648 continue 1649 } 1650 1651 unit := buf.GetString(i) 1652 v := nums[i] 1653 arg := ds[i] 1654 1655 tm1, err := arg.GoTime(time.Local) 1656 if err != nil { 1657 return err 1658 } 1659 var tb time.Time 1660 fsp := types.DefaultFsp 1661 switch unit { 1662 case "MICROSECOND": 1663 tb = tm1.Add(time.Duration(v) * time.Microsecond) 1664 fsp = types.MaxFsp 1665 case "SECOND": 1666 tb = tm1.Add(time.Duration(v) * time.Second) 1667 case "MINUTE": 1668 tb = tm1.Add(time.Duration(v) * time.Minute) 1669 case "HOUR": 1670 tb = tm1.Add(time.Duration(v) * time.Hour) 1671 case "DAY": 1672 tb = tm1.AddDate(0, 0, int(v)) 1673 case "WEEK": 1674 tb = tm1.AddDate(0, 0, 7*int(v)) 1675 case "MONTH": 1676 tb = tm1.AddDate(0, int(v), 0) 1677 case "QUARTER": 1678 tb = tm1.AddDate(0, 3*int(v), 0) 1679 case "YEAR": 1680 tb = tm1.AddDate(int(v), 0, 0) 1681 default: 1682 return types.ErrWrongValue.GenWithStackByArgs(types.TimeStr, unit) 1683 } 1684 r := types.NewTime(types.FromGoTime(tb), b.resolveType(arg.Type(), unit), fsp) 1685 if err = r.Check(b.ctx.GetStochastikVars().StmtCtx); err != nil { 1686 if err = handleInvalidTimeError(b.ctx, err); err != nil { 1687 return err 1688 } 1689 result.AppendNull() 1690 continue 1691 } 1692 result.AppendString(r.String()) 1693 } 1694 return nil 1695 } 1696 1697 func (b *builtinToDaysSig) vectorized() bool { 1698 return true 1699 } 1700 1701 // vecEvalInt evals a builtinToDaysSig. 1702 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_to-days 1703 func (b *builtinToDaysSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1704 n := input.NumEvents() 1705 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1706 if err != nil { 1707 return err 1708 } 1709 defer b.bufSlabPredictor.put(buf) 1710 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 1711 return err 1712 } 1713 1714 result.ResizeInt64(n, false) 1715 result.MergeNulls(buf) 1716 i64s := result.Int64s() 1717 ds := buf.Times() 1718 for i := 0; i < n; i++ { 1719 if result.IsNull(i) { 1720 continue 1721 } 1722 arg := ds[i] 1723 ret := types.TimestamFIDeliff("DAY", types.ZeroDate, arg) 1724 if ret == 0 { 1725 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, arg.String())); err != nil { 1726 return err 1727 } 1728 result.SetNull(i, true) 1729 continue 1730 } 1731 i64s[i] = ret 1732 } 1733 return nil 1734 } 1735 1736 func (b *builtinDateFormatSig) vectorized() bool { 1737 return true 1738 } 1739 1740 func (b *builtinDateFormatSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1741 n := input.NumEvents() 1742 1743 dateBuf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1744 if err != nil { 1745 return err 1746 } 1747 defer b.bufSlabPredictor.put(dateBuf) 1748 if err := b.args[0].VecEvalTime(b.ctx, input, dateBuf); err != nil { 1749 return err 1750 } 1751 times := dateBuf.Times() 1752 1753 formatBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1754 if err != nil { 1755 return err 1756 } 1757 defer b.bufSlabPredictor.put(formatBuf) 1758 if err := b.args[1].VecEvalString(b.ctx, input, formatBuf); err != nil { 1759 return err 1760 } 1761 1762 result.ReserveString(n) 1763 1764 for i := range times { 1765 t := times[i] 1766 if dateBuf.IsNull(i) || formatBuf.IsNull(i) { 1767 result.AppendNull() 1768 continue 1769 } 1770 1771 formatMask := formatBuf.GetString(i) 1772 // MyALLEGROSQL compatibility, #11203 1773 // If format mask is 0 then return 0 without warnings 1774 if formatMask == "0" { 1775 result.AppendString("0") 1776 continue 1777 } 1778 1779 if t.InvalidZero() { 1780 // MyALLEGROSQL compatibility, #11203 1781 // 0 | 0.0 should be converted to null without warnings 1782 n, err := t.ToNumber().ToInt() 1783 isOriginalIntOrDecimalZero := err == nil && n == 0 1784 // Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6 1785 isOriginalStringZero := t.Fsp() > 0 1786 1787 result.AppendNull() 1788 if isOriginalIntOrDecimalZero && !isOriginalStringZero { 1789 continue 1790 } 1791 if errHandled := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String())); errHandled != nil { 1792 return errHandled 1793 } 1794 continue 1795 } 1796 res, err := t.DateFormat(formatMask) 1797 if err != nil { 1798 return err 1799 } 1800 result.AppendString(res) 1801 } 1802 return nil 1803 } 1804 1805 func (b *builtinHourSig) vectorized() bool { 1806 return true 1807 } 1808 1809 func (b *builtinHourSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1810 n := input.NumEvents() 1811 buf, err := b.bufSlabPredictor.get(types.ETDuration, n) 1812 if err != nil { 1813 return err 1814 } 1815 defer b.bufSlabPredictor.put(buf) 1816 if err = b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil { 1817 return vecEvalIntByEvents(b, input, result) 1818 } 1819 1820 result.ResizeInt64(n, false) 1821 result.MergeNulls(buf) 1822 i64s := result.Int64s() 1823 ds := buf.GoDurations() 1824 for i := 0; i < n; i++ { 1825 if result.IsNull(i) { 1826 continue 1827 } 1828 i64s[i] = int64(ds[i].Hours()) 1829 } 1830 return nil 1831 } 1832 1833 func (b *builtinSecToTimeSig) vectorized() bool { 1834 return true 1835 } 1836 1837 func (b *builtinSecToTimeSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1838 n := input.NumEvents() 1839 buf, err := b.bufSlabPredictor.get(types.ETReal, n) 1840 if err != nil { 1841 return err 1842 } 1843 defer b.bufSlabPredictor.put(buf) 1844 if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { 1845 return err 1846 } 1847 args := buf.Float64s() 1848 result.ResizeGoDuration(n, false) 1849 result.MergeNulls(buf) 1850 durations := result.GoDurations() 1851 for i := 0; i < n; i++ { 1852 if result.IsNull(i) { 1853 continue 1854 } 1855 secondsFloat := args[i] 1856 negative := "" 1857 if secondsFloat < 0 { 1858 negative = "-" 1859 secondsFloat = math.Abs(secondsFloat) 1860 } 1861 seconds := int64(secondsFloat) 1862 demical := secondsFloat - float64(seconds) 1863 var minute, second int64 1864 hour := seconds / 3600 1865 if hour > 838 { 1866 hour = 838 1867 minute = 59 1868 second = 59 1869 } else { 1870 minute = seconds % 3600 / 60 1871 second = seconds % 60 1872 } 1873 secondDemical := float64(second) + demical 1874 duration, err := types.ParseDuration(b.ctx.GetStochastikVars().StmtCtx, fmt.Sprintf("%s%02d:%02d:%s", negative, hour, minute, strconv.FormatFloat(secondDemical, 'f', -1, 64)), int8(b.tp.Decimal)) 1875 if err != nil { 1876 return err 1877 } 1878 durations[i] = duration.Duration 1879 } 1880 return nil 1881 } 1882 1883 func (b *builtinUTCTimeWithoutArgSig) vectorized() bool { 1884 return true 1885 } 1886 1887 // vecEvalDuration evals a builtinUTCTimeWithoutArgSig. 1888 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-time 1889 func (b *builtinUTCTimeWithoutArgSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1890 n := input.NumEvents() 1891 nowTs, err := getStmtTimestamp(b.ctx) 1892 if err != nil { 1893 return err 1894 } 1895 res, err := types.ParseDuration(b.ctx.GetStochastikVars().StmtCtx, nowTs.UTC().Format(types.TimeFormat), types.DefaultFsp) 1896 if err != nil { 1897 return err 1898 } 1899 result.ResizeGoDuration(n, false) 1900 d64s := result.GoDurations() 1901 for i := 0; i < n; i++ { 1902 d64s[i] = res.Duration 1903 } 1904 return nil 1905 } 1906 1907 func (b *builtinDateDiffSig) vectorized() bool { 1908 return true 1909 } 1910 1911 func (b *builtinDateDiffSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1912 n := input.NumEvents() 1913 buf0, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1914 if err != nil { 1915 return err 1916 } 1917 defer b.bufSlabPredictor.put(buf0) 1918 if err = b.args[0].VecEvalTime(b.ctx, input, buf0); err != nil { 1919 return err 1920 } 1921 buf1, err := b.bufSlabPredictor.get(types.ETDatetime, n) 1922 if err != nil { 1923 return err 1924 } 1925 defer b.bufSlabPredictor.put(buf1) 1926 if err = b.args[1].VecEvalTime(b.ctx, input, buf1); err != nil { 1927 return err 1928 } 1929 result.ResizeInt64(n, false) 1930 result.MergeNulls(buf0, buf1) 1931 args0 := buf0.Times() 1932 args1 := buf1.Times() 1933 i64s := result.Int64s() 1934 for i := 0; i < n; i++ { 1935 if result.IsNull(i) { 1936 continue 1937 } 1938 if invalidArg0, invalidArg1 := args0[i].InvalidZero(), args1[i].InvalidZero(); invalidArg0 || invalidArg1 { 1939 if invalidArg0 { 1940 err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, args0[i].String())) 1941 } 1942 if invalidArg1 { 1943 err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, args1[i].String())) 1944 } 1945 if err != nil { 1946 return err 1947 } 1948 result.SetNull(i, true) 1949 continue 1950 } 1951 i64s[i] = int64(types.DateDiff(args0[i].CoreTime(), args1[i].CoreTime())) 1952 } 1953 return nil 1954 } 1955 1956 func (b *builtinCurrentDateSig) vectorized() bool { 1957 return true 1958 } 1959 1960 func (b *builtinCurrentDateSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1961 nowTs, err := getStmtTimestamp(b.ctx) 1962 if err != nil { 1963 return err 1964 } 1965 1966 tz := b.ctx.GetStochastikVars().Location() 1967 year, month, day := nowTs.In(tz).Date() 1968 timeValue := types.NewTime(types.FromDate(year, int(month), day, 0, 0, 0, 0), allegrosql.TypeDate, 0) 1969 1970 n := input.NumEvents() 1971 result.ResizeTime(n, false) 1972 times := result.Times() 1973 for i := 0; i < n; i++ { 1974 times[i] = timeValue 1975 } 1976 return nil 1977 } 1978 1979 func (b *builtinMakeTimeSig) vectorized() bool { 1980 return true 1981 } 1982 1983 func (b *builtinMakeTimeSig) getVecIntParam(arg Expression, input *chunk.Chunk, defCaus *chunk.DeferredCauset) (err error) { 1984 if arg.GetType().EvalType() == types.ETReal { 1985 err = arg.VecEvalReal(b.ctx, input, defCaus) 1986 if err != nil { 1987 return err 1988 } 1989 f64s := defCaus.Float64s() 1990 i64s := defCaus.Int64s() 1991 n := input.NumEvents() 1992 for i := 0; i < n; i++ { 1993 i64s[i] = int64(f64s[i]) 1994 } 1995 return nil 1996 } 1997 err = arg.VecEvalInt(b.ctx, input, defCaus) 1998 return err 1999 } 2000 2001 func (b *builtinMakeTimeSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2002 n := input.NumEvents() 2003 result.ResizeInt64(n, false) 2004 hoursBuf := result 2005 var err error 2006 if err = b.getVecIntParam(b.args[0], input, hoursBuf); err != nil { 2007 return err 2008 } 2009 minutesBuf, err := b.bufSlabPredictor.get(types.ETInt, n) 2010 if err != nil { 2011 return err 2012 } 2013 defer b.bufSlabPredictor.put(minutesBuf) 2014 if err = b.getVecIntParam(b.args[1], input, minutesBuf); err != nil { 2015 return err 2016 } 2017 secondsBuf, err := b.bufSlabPredictor.get(types.ETReal, n) 2018 if err != nil { 2019 return err 2020 } 2021 defer b.bufSlabPredictor.put(secondsBuf) 2022 if err = b.args[2].VecEvalReal(b.ctx, input, secondsBuf); err != nil { 2023 return err 2024 } 2025 hours := hoursBuf.Int64s() 2026 minutes := minutesBuf.Int64s() 2027 seconds := secondsBuf.Float64s() 2028 durs := result.GoDurations() 2029 result.MergeNulls(minutesBuf, secondsBuf) 2030 hourUnsignedFlag := allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) 2031 for i := 0; i < n; i++ { 2032 if result.IsNull(i) { 2033 continue 2034 } 2035 if minutes[i] < 0 || minutes[i] >= 60 || seconds[i] < 0 || seconds[i] >= 60 { 2036 result.SetNull(i, true) 2037 continue 2038 } 2039 dur, err := b.makeTime(hours[i], minutes[i], seconds[i], hourUnsignedFlag) 2040 if err != nil { 2041 return err 2042 } 2043 durs[i] = dur.Duration 2044 } 2045 return nil 2046 } 2047 2048 func (b *builtinDayOfYearSig) vectorized() bool { 2049 return true 2050 } 2051 2052 func (b *builtinDayOfYearSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2053 n := input.NumEvents() 2054 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2055 if err != nil { 2056 return err 2057 } 2058 defer b.bufSlabPredictor.put(buf) 2059 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 2060 return err 2061 } 2062 result.ResizeInt64(n, false) 2063 result.MergeNulls(buf) 2064 i64s := result.Int64s() 2065 ds := buf.Times() 2066 for i := 0; i < n; i++ { 2067 if result.IsNull(i) { 2068 continue 2069 } 2070 if ds[i].InvalidZero() { 2071 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 2072 return err 2073 } 2074 result.SetNull(i, true) 2075 continue 2076 } 2077 i64s[i] = int64(ds[i].YearDay()) 2078 } 2079 return nil 2080 } 2081 2082 func (b *builtinFromUnixTime1ArgSig) vectorized() bool { 2083 return true 2084 } 2085 2086 func (b *builtinFromUnixTime1ArgSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2087 n := input.NumEvents() 2088 buf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 2089 if err != nil { 2090 return err 2091 } 2092 defer b.bufSlabPredictor.put(buf) 2093 if err = b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { 2094 return err 2095 } 2096 result.ResizeTime(n, false) 2097 result.MergeNulls(buf) 2098 ts := result.Times() 2099 ds := buf.Decimals() 2100 fsp := int8(b.tp.Decimal) 2101 for i := 0; i < n; i++ { 2102 if result.IsNull(i) { 2103 continue 2104 } 2105 t, isNull, err := evalFromUnixTime(b.ctx, fsp, &ds[i]) 2106 if err != nil { 2107 return err 2108 } 2109 if isNull { 2110 result.SetNull(i, true) 2111 continue 2112 } 2113 ts[i] = t 2114 } 2115 return nil 2116 } 2117 2118 func (b *builtinYearWeekWithModeSig) vectorized() bool { 2119 return true 2120 } 2121 2122 // vecEvalInt evals YEARWEEK(date,mode). 2123 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_yearweek 2124 func (b *builtinYearWeekWithModeSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2125 n := input.NumEvents() 2126 buf1, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2127 if err != nil { 2128 return err 2129 } 2130 if err := b.args[0].VecEvalTime(b.ctx, input, buf1); err != nil { 2131 return err 2132 } 2133 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 2134 if err != nil { 2135 return err 2136 } 2137 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 2138 return err 2139 } 2140 2141 result.ResizeInt64(n, false) 2142 result.MergeNulls(buf1) 2143 i64s := result.Int64s() 2144 ds := buf1.Times() 2145 ms := buf2.Int64s() 2146 for i := 0; i < n; i++ { 2147 if result.IsNull(i) { 2148 continue 2149 } 2150 date := ds[i] 2151 if date.IsZero() { 2152 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String())); err != nil { 2153 return err 2154 } 2155 result.SetNull(i, true) 2156 continue 2157 } 2158 mode := int(ms[i]) 2159 if buf2.IsNull(i) { 2160 mode = 0 2161 } 2162 year, week := date.YearWeek(mode) 2163 i64s[i] = int64(week + year*100) 2164 if i64s[i] < 0 { 2165 i64s[i] = int64(math.MaxUint32) 2166 } 2167 } 2168 return nil 2169 } 2170 2171 func (b *builtinTimestamFIDeliffSig) vectorized() bool { 2172 return true 2173 } 2174 2175 // vecEvalInt evals a builtinTimestamFIDeliffSig. 2176 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestamFIDeliff 2177 func (b *builtinTimestamFIDeliffSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2178 n := input.NumEvents() 2179 unitBuf, err := b.bufSlabPredictor.get(types.ETString, n) 2180 if err != nil { 2181 return err 2182 } 2183 defer b.bufSlabPredictor.put(unitBuf) 2184 if err := b.args[0].VecEvalString(b.ctx, input, unitBuf); err != nil { 2185 return err 2186 } 2187 lhsBuf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2188 if err != nil { 2189 return err 2190 } 2191 defer b.bufSlabPredictor.put(lhsBuf) 2192 if err := b.args[1].VecEvalTime(b.ctx, input, lhsBuf); err != nil { 2193 return err 2194 } 2195 rhsBuf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2196 if err != nil { 2197 return err 2198 } 2199 defer b.bufSlabPredictor.put(rhsBuf) 2200 if err := b.args[2].VecEvalTime(b.ctx, input, rhsBuf); err != nil { 2201 return err 2202 } 2203 2204 result.ResizeInt64(n, false) 2205 result.MergeNulls(unitBuf, lhsBuf, rhsBuf) 2206 i64s := result.Int64s() 2207 lhs := lhsBuf.Times() 2208 rhs := rhsBuf.Times() 2209 for i := 0; i < n; i++ { 2210 if result.IsNull(i) { 2211 continue 2212 } 2213 if invalidLHS, invalidRHS := lhs[i].InvalidZero(), rhs[i].InvalidZero(); invalidLHS || invalidRHS { 2214 if invalidLHS { 2215 err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, lhs[i].String())) 2216 } 2217 if invalidRHS { 2218 err = handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, rhs[i].String())) 2219 } 2220 if err != nil { 2221 return err 2222 } 2223 result.SetNull(i, true) 2224 continue 2225 } 2226 i64s[i] = types.TimestamFIDeliff(unitBuf.GetString(i), lhs[i], rhs[i]) 2227 } 2228 return nil 2229 } 2230 2231 func (b *builtinUnixTimestampIntSig) vectorized() bool { 2232 return true 2233 } 2234 2235 // vecEvalInt evals a UNIX_TIMESTAMP(time). 2236 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_unix-timestamp 2237 func (b *builtinUnixTimestampIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2238 n := input.NumEvents() 2239 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2240 if err != nil { 2241 return err 2242 } 2243 defer b.bufSlabPredictor.put(buf) 2244 2245 result.ResizeInt64(n, false) 2246 i64s := result.Int64s() 2247 2248 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 2249 var isNull bool 2250 for i := 0; i < n; i++ { 2251 i64s[i], isNull, err = b.evalInt(input.GetEvent(i)) 2252 if err != nil { 2253 return err 2254 } 2255 if isNull { 2256 result.SetNull(i, true) 2257 } 2258 } 2259 } else { 2260 result.MergeNulls(buf) 2261 for i := 0; i < n; i++ { 2262 if result.IsNull(i) { 2263 continue 2264 } 2265 2266 t, err := buf.GetTime(i).GoTime(getTimeZone(b.ctx)) 2267 if err != nil { 2268 i64s[i] = 0 2269 continue 2270 } 2271 dec, err := goTimeToMysqlUnixTimestamp(t, 1) 2272 if err != nil { 2273 return err 2274 } 2275 intVal, err := dec.ToInt() 2276 if !terror.ErrorEqual(err, types.ErrTruncated) { 2277 terror.Log(err) 2278 } 2279 i64s[i] = intVal 2280 } 2281 } 2282 2283 return nil 2284 } 2285 2286 func (b *builtinCurrentTime0ArgSig) vectorized() bool { 2287 return true 2288 } 2289 2290 func (b *builtinCurrentTime0ArgSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2291 n := input.NumEvents() 2292 nowTs, err := getStmtTimestamp(b.ctx) 2293 if err != nil { 2294 return err 2295 } 2296 tz := b.ctx.GetStochastikVars().Location() 2297 dur := nowTs.In(tz).Format(types.TimeFormat) 2298 res, err := types.ParseDuration(b.ctx.GetStochastikVars().StmtCtx, dur, types.MinFsp) 2299 if err != nil { 2300 return err 2301 } 2302 result.ResizeGoDuration(n, false) 2303 durations := result.GoDurations() 2304 for i := 0; i < n; i++ { 2305 durations[i] = res.Duration 2306 } 2307 return nil 2308 } 2309 2310 func (b *builtinTimeSig) vectorized() bool { 2311 return true 2312 } 2313 2314 func (b *builtinTimeSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2315 n := input.NumEvents() 2316 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2317 if err != nil { 2318 return err 2319 } 2320 defer b.bufSlabPredictor.put(buf) 2321 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2322 return err 2323 } 2324 2325 result.ResizeGoDuration(n, false) 2326 result.MergeNulls(buf) 2327 ds := result.GoDurations() 2328 sc := b.ctx.GetStochastikVars().StmtCtx 2329 for i := 0; i < n; i++ { 2330 if result.IsNull(i) { 2331 continue 2332 } 2333 2334 fsp := 0 2335 expr := buf.GetString(i) 2336 if idx := strings.Index(expr, "."); idx != -1 { 2337 fsp = len(expr) - idx - 1 2338 } 2339 2340 var tmpFsp int8 2341 if tmpFsp, err = types.CheckFsp(fsp); err != nil { 2342 return err 2343 } 2344 fsp = int(tmpFsp) 2345 2346 res, err := types.ParseDuration(sc, expr, int8(fsp)) 2347 if types.ErrTruncatedWrongVal.Equal(err) { 2348 err = sc.HandleTruncate(err) 2349 } 2350 if err != nil { 2351 return err 2352 } 2353 ds[i] = res.Duration 2354 } 2355 return nil 2356 } 2357 2358 func (b *builtinDateLiteralSig) vectorized() bool { 2359 return true 2360 } 2361 2362 func (b *builtinDateLiteralSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2363 n := input.NumEvents() 2364 mode := b.ctx.GetStochastikVars().ALLEGROSQLMode 2365 if mode.HasNoZeroDateMode() && b.literal.IsZero() { 2366 return types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, b.literal.String()) 2367 } 2368 if mode.HasNoZeroInDateMode() && (b.literal.InvalidZero() && !b.literal.IsZero()) { 2369 return types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, b.literal.String()) 2370 } 2371 2372 result.ResizeTime(n, false) 2373 times := result.Times() 2374 for i := range times { 2375 times[i] = b.literal 2376 } 2377 return nil 2378 } 2379 2380 func (b *builtinTimeLiteralSig) vectorized() bool { 2381 return true 2382 } 2383 2384 func (b *builtinTimeLiteralSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2385 n := input.NumEvents() 2386 result.ResizeGoDuration(n, false) 2387 d64s := result.GoDurations() 2388 for i := 0; i < n; i++ { 2389 d64s[i] = b.duration.Duration 2390 } 2391 return nil 2392 } 2393 2394 func (b *builtinMonthNameSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2395 n := input.NumEvents() 2396 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2397 if err != nil { 2398 return err 2399 } 2400 defer b.bufSlabPredictor.put(buf) 2401 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 2402 return err 2403 } 2404 2405 result.ReserveString(n) 2406 ds := buf.Times() 2407 for i := 0; i < n; i++ { 2408 if buf.IsNull(i) { 2409 result.AppendNull() 2410 continue 2411 } 2412 mon := ds[i].Month() 2413 if (ds[i].IsZero() && b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode()) || mon < 0 || mon > len(types.MonthNames) { 2414 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 2415 return err 2416 } 2417 result.AppendNull() 2418 continue 2419 } else if mon == 0 || ds[i].IsZero() { 2420 result.AppendNull() 2421 continue 2422 } 2423 result.AppendString(types.MonthNames[mon-1]) 2424 } 2425 return nil 2426 } 2427 2428 func (b *builtinMonthNameSig) vectorized() bool { 2429 return true 2430 } 2431 2432 func (b *builtinDayOfWeekSig) vectorized() bool { 2433 return true 2434 } 2435 2436 func (b *builtinDayOfWeekSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2437 n := input.NumEvents() 2438 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2439 if err != nil { 2440 return err 2441 } 2442 defer b.bufSlabPredictor.put(buf) 2443 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 2444 return err 2445 } 2446 result.ResizeInt64(n, false) 2447 result.MergeNulls(buf) 2448 i64s := result.Int64s() 2449 ds := buf.Times() 2450 for i := 0; i < n; i++ { 2451 if result.IsNull(i) { 2452 continue 2453 } 2454 if ds[i].InvalidZero() { 2455 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 2456 return err 2457 } 2458 result.SetNull(i, true) 2459 continue 2460 } 2461 i64s[i] = int64(ds[i].Weekday() + 1) 2462 } 2463 return nil 2464 } 2465 2466 func (b *builtinCurrentTime1ArgSig) vectorized() bool { 2467 return true 2468 } 2469 2470 func (b *builtinCurrentTime1ArgSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2471 n := input.NumEvents() 2472 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 2473 if err != nil { 2474 return err 2475 } 2476 defer b.bufSlabPredictor.put(buf) 2477 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 2478 return err 2479 } 2480 2481 nowTs, err := getStmtTimestamp(b.ctx) 2482 if err != nil { 2483 return err 2484 } 2485 tz := b.ctx.GetStochastikVars().Location() 2486 dur := nowTs.In(tz).Format(types.TimeFSPFormat) 2487 stmtCtx := b.ctx.GetStochastikVars().StmtCtx 2488 i64s := buf.Int64s() 2489 result.ResizeGoDuration(n, false) 2490 durations := result.GoDurations() 2491 for i := 0; i < n; i++ { 2492 res, err := types.ParseDuration(stmtCtx, dur, int8(i64s[i])) 2493 if err != nil { 2494 return err 2495 } 2496 durations[i] = res.Duration 2497 } 2498 return nil 2499 } 2500 2501 func (b *builtinUTCTimestampWithoutArgSig) vectorized() bool { 2502 return true 2503 } 2504 2505 // vecEvalTime evals UTC_TIMESTAMP(). 2506 // See https://dev.allegrosql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-timestamp 2507 func (b *builtinUTCTimestampWithoutArgSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2508 n := input.NumEvents() 2509 res, isNull, err := evalUTCTimestampWithFsp(b.ctx, types.DefaultFsp) 2510 if err != nil { 2511 return err 2512 } 2513 if isNull { 2514 result.ResizeTime(n, true) 2515 return nil 2516 } 2517 result.ResizeTime(n, false) 2518 t64s := result.Times() 2519 for i := 0; i < n; i++ { 2520 t64s[i] = res 2521 } 2522 return nil 2523 } 2524 2525 func (b *builtinConvertTzSig) vectorized() bool { 2526 return true 2527 } 2528 2529 func (b *builtinConvertTzSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2530 n := input.NumEvents() 2531 if err := b.args[0].VecEvalTime(b.ctx, input, result); err != nil { 2532 return err 2533 } 2534 2535 fromTzBuf, err := b.bufSlabPredictor.get(types.ETString, n) 2536 if err != nil { 2537 return err 2538 } 2539 defer b.bufSlabPredictor.put(fromTzBuf) 2540 if err := b.args[1].VecEvalString(b.ctx, input, fromTzBuf); err != nil { 2541 return err 2542 } 2543 2544 toTzBuf, err := b.bufSlabPredictor.get(types.ETString, n) 2545 if err != nil { 2546 return err 2547 } 2548 defer b.bufSlabPredictor.put(toTzBuf) 2549 if err := b.args[2].VecEvalString(b.ctx, input, toTzBuf); err != nil { 2550 return err 2551 } 2552 2553 result.MergeNulls(fromTzBuf, toTzBuf) 2554 ts := result.Times() 2555 var isNull bool 2556 for i := 0; i < n; i++ { 2557 if result.IsNull(i) { 2558 continue 2559 } 2560 2561 ts[i], isNull, err = b.convertTz(ts[i], fromTzBuf.GetString(i), toTzBuf.GetString(i)) 2562 if err != nil { 2563 return err 2564 } 2565 if isNull { 2566 result.SetNull(i, true) 2567 } 2568 } 2569 return nil 2570 } 2571 2572 func (b *builtinTimestamp1ArgSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2573 n := input.NumEvents() 2574 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2575 if err != nil { 2576 return err 2577 } 2578 defer b.bufSlabPredictor.put(buf) 2579 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2580 return err 2581 } 2582 2583 result.ResizeTime(n, false) 2584 result.MergeNulls(buf) 2585 times := result.Times() 2586 sc := b.ctx.GetStochastikVars().StmtCtx 2587 var tm types.Time 2588 for i := 0; i < n; i++ { 2589 if result.IsNull(i) { 2590 continue 2591 } 2592 s := buf.GetString(i) 2593 2594 if b.isFloat { 2595 tm, err = types.ParseTimeFromFloatString(sc, s, allegrosql.TypeDatetime, types.GetFsp(s)) 2596 } else { 2597 tm, err = types.ParseTime(sc, s, allegrosql.TypeDatetime, types.GetFsp(s)) 2598 } 2599 if err != nil { 2600 if err = handleInvalidTimeError(b.ctx, err); err != nil { 2601 return err 2602 } 2603 result.SetNull(i, true) 2604 continue 2605 } 2606 times[i] = tm 2607 } 2608 return nil 2609 } 2610 2611 func (b *builtinTimestamp1ArgSig) vectorized() bool { 2612 return true 2613 } 2614 2615 func (b *builtinTimestamp2ArgsSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2616 n := input.NumEvents() 2617 buf0, err := b.bufSlabPredictor.get(types.ETString, n) 2618 if err != nil { 2619 return err 2620 } 2621 defer b.bufSlabPredictor.put(buf0) 2622 if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil { 2623 return err 2624 } 2625 2626 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 2627 if err != nil { 2628 return err 2629 } 2630 defer b.bufSlabPredictor.put(buf1) 2631 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 2632 return err 2633 } 2634 2635 result.ResizeTime(n, false) 2636 result.MergeNulls(buf0, buf1) 2637 times := result.Times() 2638 sc := b.ctx.GetStochastikVars().StmtCtx 2639 var tm types.Time 2640 for i := 0; i < n; i++ { 2641 if result.IsNull(i) { 2642 continue 2643 } 2644 arg0 := buf0.GetString(i) 2645 arg1 := buf1.GetString(i) 2646 2647 if b.isFloat { 2648 tm, err = types.ParseTimeFromFloatString(sc, arg0, allegrosql.TypeDatetime, types.GetFsp(arg0)) 2649 } else { 2650 tm, err = types.ParseTime(sc, arg0, allegrosql.TypeDatetime, types.GetFsp(arg0)) 2651 } 2652 if err != nil { 2653 if err = handleInvalidTimeError(b.ctx, err); err != nil { 2654 return err 2655 } 2656 result.SetNull(i, true) 2657 continue 2658 } 2659 2660 if !isDuration(arg1) { 2661 result.SetNull(i, true) 2662 continue 2663 } 2664 2665 duration, err := types.ParseDuration(sc, arg1, types.GetFsp(arg1)) 2666 if err != nil { 2667 if err = handleInvalidTimeError(b.ctx, err); err != nil { 2668 return err 2669 } 2670 result.SetNull(i, true) 2671 continue 2672 } 2673 tmp, err := tm.Add(sc, duration) 2674 if err != nil { 2675 return err 2676 } 2677 times[i] = tmp 2678 } 2679 return nil 2680 } 2681 2682 func (b *builtinTimestamp2ArgsSig) vectorized() bool { 2683 return true 2684 } 2685 2686 func (b *builtinDayOfMonthSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2687 n := input.NumEvents() 2688 buf, err := b.bufSlabPredictor.get(types.ETDatetime, n) 2689 if err != nil { 2690 return err 2691 } 2692 defer b.bufSlabPredictor.put(buf) 2693 if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { 2694 return err 2695 } 2696 result.ResizeInt64(n, false) 2697 result.MergeNulls(buf) 2698 i64s := result.Int64s() 2699 ds := buf.Times() 2700 for i := 0; i < n; i++ { 2701 if result.IsNull(i) { 2702 continue 2703 } 2704 if ds[i].IsZero() { 2705 if b.ctx.GetStochastikVars().ALLEGROSQLMode.HasNoZeroDateMode() { 2706 if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil { 2707 return err 2708 } 2709 result.SetNull(i, true) 2710 continue 2711 } 2712 i64s[i] = 0 2713 continue 2714 } 2715 i64s[i] = int64(ds[i].Day()) 2716 } 2717 return nil 2718 } 2719 2720 func (b *builtinDayOfMonthSig) vectorized() bool { 2721 return true 2722 }