github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_string_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 "encoding/base64" 18 "encoding/hex" 19 "fmt" 20 "math" 21 "strconv" 22 "strings" 23 "unicode/utf8" 24 25 "github.com/whtcorpsinc/BerolinaSQL/ast" 26 "github.com/whtcorpsinc/BerolinaSQL/charset" 27 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 28 "github.com/whtcorpsinc/milevadb/stochastikctx" 29 "github.com/whtcorpsinc/milevadb/types" 30 "github.com/whtcorpsinc/milevadb/soliton/chunk" 31 "github.com/whtcorpsinc/milevadb/soliton/defCauslate" 32 "golang.org/x/text/transform" 33 ) 34 35 func (b *builtinLowerSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 36 if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil { 37 return err 38 } 39 if types.IsBinaryStr(b.args[0].GetType()) { 40 return nil 41 } 42 43 for i := 0; i < input.NumEvents(); i++ { 44 result.SetRaw(i, []byte(strings.ToLower(result.GetString(i)))) 45 } 46 return nil 47 } 48 49 func (b *builtinLowerSig) vectorized() bool { 50 return true 51 } 52 53 func (b *builtinRepeatSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 54 n := input.NumEvents() 55 buf, err := b.bufSlabPredictor.get(types.ETString, n) 56 if err != nil { 57 return err 58 } 59 defer b.bufSlabPredictor.put(buf) 60 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 61 return err 62 } 63 64 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 65 if err != nil { 66 return err 67 } 68 defer b.bufSlabPredictor.put(buf2) 69 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 70 return err 71 } 72 73 result.ReserveString(n) 74 nums := buf2.Int64s() 75 for i := 0; i < n; i++ { 76 if buf.IsNull(i) || buf2.IsNull(i) { 77 result.AppendNull() 78 continue 79 } 80 num := nums[i] 81 if num < 1 { 82 result.AppendString("") 83 continue 84 } 85 if num > math.MaxInt32 { 86 // to avoid overflow when calculating uint64(byteLength)*uint64(num) later 87 num = math.MaxInt32 88 } 89 90 str := buf.GetString(i) 91 byteLength := len(str) 92 if uint64(byteLength)*uint64(num) > b.maxAllowedPacket { 93 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("repeat", b.maxAllowedPacket)) 94 result.AppendNull() 95 continue 96 } 97 if int64(byteLength) > int64(b.tp.Flen)/num { 98 result.AppendNull() 99 continue 100 } 101 result.AppendString(strings.Repeat(str, int(num))) 102 } 103 return nil 104 } 105 106 func (b *builtinRepeatSig) vectorized() bool { 107 return true 108 } 109 110 func (b *builtinStringIsNullSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 111 n := input.NumEvents() 112 buf, err := b.bufSlabPredictor.get(types.ETString, n) 113 if err != nil { 114 return err 115 } 116 defer b.bufSlabPredictor.put(buf) 117 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 118 return err 119 } 120 121 result.ResizeInt64(n, false) 122 i64s := result.Int64s() 123 for i := 0; i < n; i++ { 124 if buf.IsNull(i) { 125 i64s[i] = 1 126 } else { 127 i64s[i] = 0 128 } 129 } 130 return nil 131 } 132 133 func (b *builtinStringIsNullSig) vectorized() bool { 134 return true 135 } 136 137 func (b *builtinUpperUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 138 if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil { 139 return err 140 } 141 142 for i := 0; i < input.NumEvents(); i++ { 143 result.SetRaw(i, []byte(strings.ToUpper(result.GetString(i)))) 144 } 145 return nil 146 } 147 148 func (b *builtinUpperUTF8Sig) vectorized() bool { 149 return true 150 } 151 152 func (b *builtinUpperSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 153 return b.args[0].VecEvalString(b.ctx, input, result) 154 } 155 156 func (b *builtinUpperSig) vectorized() bool { 157 return true 158 } 159 160 func (b *builtinLeftUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 161 n := input.NumEvents() 162 buf, err := b.bufSlabPredictor.get(types.ETString, n) 163 if err != nil { 164 return err 165 } 166 defer b.bufSlabPredictor.put(buf) 167 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 168 return err 169 } 170 171 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 172 if err != nil { 173 return err 174 } 175 defer b.bufSlabPredictor.put(buf2) 176 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 177 return err 178 } 179 180 result.ReserveString(n) 181 nums := buf2.Int64s() 182 for i := 0; i < n; i++ { 183 if buf.IsNull(i) || buf2.IsNull(i) { 184 result.AppendNull() 185 continue 186 } 187 188 str := buf.GetString(i) 189 runes, leftLength := []rune(str), int(nums[i]) 190 if runeLength := len(runes); leftLength > runeLength { 191 leftLength = runeLength 192 } else if leftLength < 0 { 193 leftLength = 0 194 } 195 196 result.AppendString(string(runes[:leftLength])) 197 } 198 return nil 199 } 200 201 func (b *builtinLeftUTF8Sig) vectorized() bool { 202 return true 203 } 204 205 func (b *builtinRightUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 206 n := input.NumEvents() 207 buf, err := b.bufSlabPredictor.get(types.ETString, n) 208 if err != nil { 209 return err 210 } 211 defer b.bufSlabPredictor.put(buf) 212 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 213 return err 214 } 215 216 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 217 if err != nil { 218 return err 219 } 220 defer b.bufSlabPredictor.put(buf2) 221 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 222 return err 223 } 224 225 result.ReserveString(n) 226 nums := buf2.Int64s() 227 for i := 0; i < n; i++ { 228 if buf.IsNull(i) || buf2.IsNull(i) { 229 result.AppendNull() 230 continue 231 } 232 233 str := buf.GetString(i) 234 runes := []rune(str) 235 strLength, rightLength := len(runes), int(nums[i]) 236 if rightLength > strLength { 237 rightLength = strLength 238 } else if rightLength < 0 { 239 rightLength = 0 240 } 241 242 result.AppendString(string(runes[strLength-rightLength:])) 243 } 244 return nil 245 } 246 247 func (b *builtinRightUTF8Sig) vectorized() bool { 248 return true 249 } 250 251 // vecEvalString evals a builtinSpaceSig. 252 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_space 253 func (b *builtinSpaceSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 254 n := input.NumEvents() 255 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 256 if err != nil { 257 return err 258 } 259 defer b.bufSlabPredictor.put(buf) 260 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 261 return err 262 } 263 264 result.ReserveString(n) 265 nums := buf.Int64s() 266 for i := 0; i < n; i++ { 267 if buf.IsNull(i) { 268 result.AppendNull() 269 continue 270 } 271 num := nums[i] 272 if num < 0 { 273 num = 0 274 } 275 if uint64(num) > b.maxAllowedPacket { 276 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("space", b.maxAllowedPacket)) 277 result.AppendNull() 278 continue 279 } 280 if num > allegrosql.MaxBlobWidth { 281 result.AppendNull() 282 continue 283 } 284 result.AppendString(strings.Repeat(" ", int(num))) 285 } 286 return nil 287 } 288 289 func (b *builtinSpaceSig) vectorized() bool { 290 return true 291 } 292 293 // vecEvalString evals a REVERSE(str). 294 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_reverse 295 func (b *builtinReverseUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 296 if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil { 297 return err 298 } 299 for i := 0; i < input.NumEvents(); i++ { 300 if result.IsNull(i) { 301 continue 302 } 303 str := result.GetString(i) 304 reversed := reverseRunes([]rune(str)) 305 result.SetRaw(i, []byte(string(reversed))) 306 } 307 return nil 308 } 309 310 func (b *builtinReverseUTF8Sig) vectorized() bool { 311 return true 312 } 313 314 func (b *builtinConcatSig) vectorized() bool { 315 return true 316 } 317 318 // vecEvalString evals a CONCAT(str1,str2,...) 319 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_concat 320 func (b *builtinConcatSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 321 n := input.NumEvents() 322 buf, err := b.bufSlabPredictor.get(types.ETString, n) 323 if err != nil { 324 return err 325 } 326 defer b.bufSlabPredictor.put(buf) 327 328 strs := make([][]byte, n) 329 isNulls := make([]bool, n) 330 result.ReserveString(n) 331 var byteBuf []byte 332 for j := 0; j < len(b.args); j++ { 333 if err := b.args[j].VecEvalString(b.ctx, input, buf); err != nil { 334 return err 335 } 336 for i := 0; i < n; i++ { 337 if isNulls[i] { 338 continue 339 } 340 if buf.IsNull(i) { 341 isNulls[i] = true 342 continue 343 } 344 byteBuf = buf.GetBytes(i) 345 if uint64(len(strs[i])+len(byteBuf)) > b.maxAllowedPacket { 346 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("concat", b.maxAllowedPacket)) 347 isNulls[i] = true 348 continue 349 } 350 strs[i] = append(strs[i], byteBuf...) 351 } 352 } 353 for i := 0; i < n; i++ { 354 if isNulls[i] { 355 result.AppendNull() 356 } else { 357 result.AppendBytes(strs[i]) 358 } 359 } 360 return nil 361 } 362 363 func (b *builtinLocate3ArgsUTF8Sig) vectorized() bool { 364 return true 365 } 366 367 // vecEvalInt evals LOCATE(substr,str,pos). 368 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_locate 369 func (b *builtinLocate3ArgsUTF8Sig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 370 n := input.NumEvents() 371 buf, err := b.bufSlabPredictor.get(types.ETString, n) 372 if err != nil { 373 return err 374 } 375 defer b.bufSlabPredictor.put(buf) 376 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 377 return err 378 } 379 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 380 if err != nil { 381 return err 382 } 383 defer b.bufSlabPredictor.put(buf1) 384 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 385 return err 386 } 387 // causetstore positions in result 388 if err := b.args[2].VecEvalInt(b.ctx, input, result); err != nil { 389 return err 390 } 391 392 result.MergeNulls(buf, buf1) 393 i64s := result.Int64s() 394 ci := defCauslate.IsCIDefCauslation(b.defCauslation) 395 for i := 0; i < n; i++ { 396 if result.IsNull(i) { 397 continue 398 } 399 subStr := buf.GetString(i) 400 str := buf1.GetString(i) 401 pos := i64s[i] 402 403 // Transfer the argument which starts from 1 to real index which starts from 0. 404 pos-- 405 strLen := int64(len([]rune(str))) 406 subStrLen := int64(len([]rune(subStr))) 407 if pos < 0 || pos > strLen-subStrLen { 408 i64s[i] = 0 409 continue 410 } else if subStrLen == 0 { 411 i64s[i] = pos + 1 412 continue 413 } 414 slice := string([]rune(str)[pos:]) 415 if ci { 416 subStr = strings.ToLower(subStr) 417 slice = strings.ToLower(slice) 418 } 419 idx := strings.Index(slice, subStr) 420 if idx != -1 { 421 i64s[i] = pos + int64(utf8.RuneCountInString(slice[:idx])) + 1 422 continue 423 } 424 i64s[i] = 0 425 } 426 return nil 427 } 428 429 func (b *builtinHexStrArgSig) vectorized() bool { 430 return true 431 } 432 433 // vecEvalString evals a builtinHexStrArgSig, corresponding to hex(str) 434 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_hex 435 func (b *builtinHexStrArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 436 n := input.NumEvents() 437 buf0, err := b.bufSlabPredictor.get(types.ETString, n) 438 if err != nil { 439 return err 440 } 441 defer b.bufSlabPredictor.put(buf0) 442 if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil { 443 return err 444 } 445 result.ReserveString(n) 446 for i := 0; i < n; i++ { 447 if buf0.IsNull(i) { 448 result.AppendNull() 449 continue 450 } 451 result.AppendString(strings.ToUpper(hex.EncodeToString(buf0.GetBytes(i)))) 452 } 453 return nil 454 } 455 456 func (b *builtinLTrimSig) vectorized() bool { 457 return true 458 } 459 460 // vecEvalString evals a builtinLTrimSig 461 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_ltrim 462 func (b *builtinLTrimSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 463 n := input.NumEvents() 464 buf, err := b.bufSlabPredictor.get(types.ETString, n) 465 if err != nil { 466 return err 467 } 468 defer b.bufSlabPredictor.put(buf) 469 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 470 return err 471 } 472 473 result.ReserveString(n) 474 for i := 0; i < n; i++ { 475 if buf.IsNull(i) { 476 result.AppendNull() 477 continue 478 } 479 480 str := buf.GetString(i) 481 result.AppendString(strings.TrimLeft(str, spaceChars)) 482 } 483 484 return nil 485 } 486 487 func (b *builtinQuoteSig) vectorized() bool { 488 return true 489 } 490 491 func (b *builtinQuoteSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 492 n := input.NumEvents() 493 buf, err := b.bufSlabPredictor.get(types.ETString, n) 494 if err != nil { 495 return err 496 } 497 defer b.bufSlabPredictor.put(buf) 498 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 499 return err 500 } 501 502 result.ReserveString(n) 503 for i := 0; i < n; i++ { 504 if buf.IsNull(i) { 505 result.AppendString("NULL") 506 continue 507 } 508 str := buf.GetString(i) 509 result.AppendString(Quote(str)) 510 } 511 return nil 512 } 513 514 func (b *builtinInsertSig) vectorized() bool { 515 return true 516 } 517 518 func (b *builtinInsertSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 519 n := input.NumEvents() 520 str, err := b.bufSlabPredictor.get(types.ETString, n) 521 if err != nil { 522 return err 523 } 524 defer b.bufSlabPredictor.put(str) 525 if err := b.args[0].VecEvalString(b.ctx, input, str); err != nil { 526 return err 527 } 528 pos, err := b.bufSlabPredictor.get(types.ETInt, n) 529 if err != nil { 530 return err 531 } 532 defer b.bufSlabPredictor.put(pos) 533 if err := b.args[1].VecEvalInt(b.ctx, input, pos); err != nil { 534 return err 535 } 536 length, err := b.bufSlabPredictor.get(types.ETInt, n) 537 if err != nil { 538 return err 539 } 540 defer b.bufSlabPredictor.put(length) 541 if err := b.args[2].VecEvalInt(b.ctx, input, length); err != nil { 542 return err 543 } 544 newstr, err := b.bufSlabPredictor.get(types.ETString, n) 545 if err != nil { 546 return err 547 } 548 defer b.bufSlabPredictor.put(newstr) 549 if err := b.args[3].VecEvalString(b.ctx, input, newstr); err != nil { 550 return err 551 } 552 posIs := pos.Int64s() 553 lengthIs := length.Int64s() 554 result.ReserveString(n) 555 for i := 0; i < n; i++ { 556 if str.IsNull(i) || pos.IsNull(i) || length.IsNull(i) || newstr.IsNull(i) { 557 result.AppendNull() 558 continue 559 } 560 strI := str.GetString(i) 561 strLength := int64(len(strI)) 562 posI := posIs[i] 563 if posI < 1 || posI > strLength { 564 result.AppendString(strI) 565 continue 566 } 567 lengthI := lengthIs[i] 568 if lengthI > strLength-posI+1 || lengthI < 0 { 569 lengthI = strLength - posI + 1 570 } 571 newstrI := newstr.GetString(i) 572 if uint64(strLength-lengthI+int64(len(newstrI))) > b.maxAllowedPacket { 573 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("insert", b.maxAllowedPacket)) 574 result.AppendNull() 575 continue 576 } 577 result.AppendString(strI[0:posI-1] + newstrI + strI[posI+lengthI-1:]) 578 } 579 return nil 580 } 581 582 func (b *builtinConcatWSSig) vectorized() bool { 583 return true 584 } 585 586 // vecEvalString evals a CONCAT_WS(separator,str1,str2,...). 587 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_concat-ws 588 func (b *builtinConcatWSSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 589 n := input.NumEvents() 590 argsLen := len(b.args) 591 592 bufs := make([]*chunk.DeferredCauset, argsLen) 593 var err error 594 for i := 0; i < argsLen; i++ { 595 bufs[i], err = b.bufSlabPredictor.get(types.ETString, n) 596 if err != nil { 597 return err 598 } 599 defer b.bufSlabPredictor.put(bufs[i]) 600 if err := b.args[i].VecEvalString(b.ctx, input, bufs[i]); err != nil { 601 return err 602 } 603 } 604 605 isNulls := make([]bool, n) 606 seps := make([]string, n) 607 strs := make([][]string, n) 608 for i := 0; i < n; i++ { 609 if bufs[0].IsNull(i) { 610 // If the separator is NULL, the result is NULL. 611 isNulls[i] = true 612 continue 613 } 614 isNulls[i] = false 615 seps[i] = bufs[0].GetString(i) 616 strs[i] = make([]string, 0, argsLen-1) 617 } 618 619 var strBuf string 620 targetLengths := make([]int, n) 621 for j := 1; j < argsLen; j++ { 622 for i := 0; i < n; i++ { 623 if isNulls[i] || bufs[j].IsNull(i) { 624 // CONCAT_WS() does not skip empty strings. However, 625 // it does skip any NULL values after the separator argument. 626 continue 627 } 628 strBuf = bufs[j].GetString(i) 629 targetLengths[i] += len(strBuf) 630 if i > 1 { 631 targetLengths[i] += len(seps[i]) 632 } 633 if uint64(targetLengths[i]) > b.maxAllowedPacket { 634 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("concat_ws", b.maxAllowedPacket)) 635 isNulls[i] = true 636 continue 637 } 638 strs[i] = append(strs[i], strBuf) 639 } 640 } 641 result.ReserveString(n) 642 for i := 0; i < n; i++ { 643 if isNulls[i] { 644 result.AppendNull() 645 continue 646 } 647 str := strings.Join(strs[i], seps[i]) 648 // todo check whether the length of result is larger than Flen 649 //if b.tp.Flen != types.UnspecifiedLength && len(str) > b.tp.Flen { 650 // result.AppendNull() 651 // continue 652 //} 653 result.AppendString(str) 654 } 655 return nil 656 } 657 658 func (b *builtinConvertSig) vectorized() bool { 659 return true 660 } 661 662 func (b *builtinConvertSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 663 n := input.NumEvents() 664 expr, err := b.bufSlabPredictor.get(types.ETString, n) 665 if err != nil { 666 return err 667 } 668 defer b.bufSlabPredictor.put(expr) 669 if err := b.args[0].VecEvalString(b.ctx, input, expr); err != nil { 670 return err 671 } 672 // Since charset is already validated and set from getFunction(), there's no 673 // need to get charset from args again. 674 encoding, _ := charset.Lookup(b.tp.Charset) 675 // However, if `b.tp.Charset` is abnormally set to a wrong charset, we still 676 // return with error. 677 if encoding == nil { 678 return errUnknownCharacterSet.GenWithStackByArgs(b.tp.Charset) 679 } 680 result.ReserveString(n) 681 for i := 0; i < n; i++ { 682 if expr.IsNull(i) { 683 result.AppendNull() 684 continue 685 } 686 exprI := expr.GetString(i) 687 target, _, err := transform.String(encoding.NewCausetDecoder(), exprI) 688 if err != nil { 689 return err 690 } 691 result.AppendString(target) 692 } 693 return nil 694 } 695 696 func (b *builtinSubstringIndexSig) vectorized() bool { 697 return true 698 } 699 700 // vecEvalString evals a builtinSubstringIndexSig. 701 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_substring-index 702 func (b *builtinSubstringIndexSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 703 n := input.NumEvents() 704 buf, err := b.bufSlabPredictor.get(types.ETString, n) 705 if err != nil { 706 return err 707 } 708 defer b.bufSlabPredictor.put(buf) 709 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 710 return err 711 } 712 713 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 714 if err != nil { 715 return err 716 } 717 defer b.bufSlabPredictor.put(buf1) 718 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 719 return err 720 } 721 722 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 723 if err != nil { 724 return err 725 } 726 defer b.bufSlabPredictor.put(buf2) 727 if err := b.args[2].VecEvalInt(b.ctx, input, buf2); err != nil { 728 return err 729 } 730 731 result.ReserveString(n) 732 counts := buf2.Int64s() 733 for i := 0; i < n; i++ { 734 if buf.IsNull(i) || buf1.IsNull(i) || buf2.IsNull(i) { 735 result.AppendNull() 736 continue 737 } 738 739 str := buf.GetString(i) 740 delim := buf1.GetString(i) 741 count := counts[i] 742 743 if len(delim) == 0 { 744 result.AppendString("") 745 continue 746 } 747 748 strs := strings.Split(str, delim) 749 start, end := int64(0), int64(len(strs)) 750 if count > 0 { 751 // If count is positive, everything to the left of the final delimiter (counting from the left) is returned. 752 if count < end { 753 end = count 754 } 755 } else { 756 // If count is negative, everything to the right of the final delimiter (counting from the right) is returned. 757 count = -count 758 if count < 0 { 759 // -count overflows max int64, returns an empty string. 760 result.AppendString("") 761 continue 762 } 763 764 if count < end { 765 start = end - count 766 } 767 } 768 substrs := strs[start:end] 769 result.AppendString(strings.Join(substrs, delim)) 770 } 771 772 return nil 773 } 774 775 func (b *builtinUnHexSig) vectorized() bool { 776 return true 777 } 778 779 func (b *builtinUnHexSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 780 n := input.NumEvents() 781 buf, err := b.bufSlabPredictor.get(types.ETString, n) 782 if err != nil { 783 return err 784 } 785 defer b.bufSlabPredictor.put(buf) 786 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 787 return err 788 } 789 790 result.ReserveString(n) 791 for i := 0; i < n; i++ { 792 if buf.IsNull(i) { 793 result.AppendNull() 794 continue 795 } 796 str := buf.GetString(i) 797 if len(str)%2 != 0 { 798 str = "0" + str 799 } 800 bs, e := hex.DecodeString(str) 801 if e != nil { 802 result.AppendNull() 803 continue 804 } 805 result.AppendString(string(bs)) 806 } 807 return nil 808 } 809 810 func (b *builtinExportSet3ArgSig) vectorized() bool { 811 return true 812 } 813 814 // vecEvalString evals EXPORT_SET(bits,on,off). 815 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_export-set 816 func (b *builtinExportSet3ArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 817 n := input.NumEvents() 818 bits, err := b.bufSlabPredictor.get(types.ETInt, n) 819 if err != nil { 820 return err 821 } 822 defer b.bufSlabPredictor.put(bits) 823 if err := b.args[0].VecEvalInt(b.ctx, input, bits); err != nil { 824 return err 825 } 826 on, err := b.bufSlabPredictor.get(types.ETString, n) 827 if err != nil { 828 return err 829 } 830 defer b.bufSlabPredictor.put(on) 831 if err := b.args[1].VecEvalString(b.ctx, input, on); err != nil { 832 return err 833 } 834 off, err := b.bufSlabPredictor.get(types.ETString, n) 835 if err != nil { 836 return err 837 } 838 defer b.bufSlabPredictor.put(off) 839 if err := b.args[2].VecEvalString(b.ctx, input, off); err != nil { 840 return err 841 } 842 result.ReserveString(n) 843 i64s := bits.Int64s() 844 for i := 0; i < n; i++ { 845 if bits.IsNull(i) || on.IsNull(i) || off.IsNull(i) { 846 result.AppendNull() 847 continue 848 } 849 result.AppendString(exportSet(i64s[i], on.GetString(i), off.GetString(i), 850 ",", 64)) 851 } 852 return nil 853 } 854 855 func (b *builtinASCIISig) vectorized() bool { 856 return true 857 } 858 859 // vecEvalInt evals a builtinASCIISig. 860 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_ascii 861 func (b *builtinASCIISig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 862 n := input.NumEvents() 863 buf, err := b.bufSlabPredictor.get(types.ETString, n) 864 if err != nil { 865 return err 866 } 867 defer b.bufSlabPredictor.put(buf) 868 if err = b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 869 return err 870 } 871 872 result.ResizeInt64(n, false) 873 result.MergeNulls(buf) 874 i64s := result.Int64s() 875 for i := 0; i < n; i++ { 876 if result.IsNull(i) { 877 continue 878 } 879 str := buf.GetString(i) 880 if len(str) == 0 { 881 i64s[i] = 0 882 continue 883 } 884 i64s[i] = int64(str[0]) 885 } 886 return nil 887 } 888 889 func (b *builtinLpadSig) vectorized() bool { 890 return true 891 } 892 893 // vecEvalString evals LPAD(str,len,padstr). 894 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_lpad 895 func (b *builtinLpadSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 896 n := input.NumEvents() 897 strBuf, err := b.bufSlabPredictor.get(types.ETString, n) 898 if err != nil { 899 return err 900 } 901 defer b.bufSlabPredictor.put(strBuf) 902 if err := b.args[0].VecEvalString(b.ctx, input, strBuf); err != nil { 903 return err 904 } 905 lenBuf, err := b.bufSlabPredictor.get(types.ETInt, n) 906 if err != nil { 907 return err 908 } 909 defer b.bufSlabPredictor.put(lenBuf) 910 if err := b.args[1].VecEvalInt(b.ctx, input, lenBuf); err != nil { 911 return err 912 } 913 padBuf, err := b.bufSlabPredictor.get(types.ETString, n) 914 if err != nil { 915 return err 916 } 917 defer b.bufSlabPredictor.put(padBuf) 918 if err := b.args[2].VecEvalString(b.ctx, input, padBuf); err != nil { 919 return err 920 } 921 922 result.ReserveString(n) 923 i64s := lenBuf.Int64s() 924 lenBuf.MergeNulls(strBuf) 925 for i := 0; i < n; i++ { 926 if lenBuf.IsNull(i) { 927 result.AppendNull() 928 continue 929 } 930 targetLength := int(i64s[i]) 931 if uint64(targetLength) > b.maxAllowedPacket { 932 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("lpad", b.maxAllowedPacket)) 933 result.AppendNull() 934 continue 935 } 936 937 if padBuf.IsNull(i) { 938 result.AppendNull() 939 continue 940 } 941 str := strBuf.GetString(i) 942 strLength := len(str) 943 padStr := padBuf.GetString(i) 944 padLength := len(padStr) 945 if targetLength < 0 || targetLength > b.tp.Flen || (strLength < targetLength && padLength == 0) { 946 result.AppendNull() 947 continue 948 } 949 if tailLen := targetLength - strLength; tailLen > 0 { 950 repeatCount := tailLen/padLength + 1 951 str = strings.Repeat(padStr, repeatCount)[:tailLen] + str 952 } 953 result.AppendString(str[:targetLength]) 954 } 955 return nil 956 } 957 958 func (b *builtinLpadUTF8Sig) vectorized() bool { 959 return true 960 } 961 962 // vecEvalString evals LPAD(str,len,padstr). 963 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_lpad 964 func (b *builtinLpadUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 965 n := input.NumEvents() 966 buf, err := b.bufSlabPredictor.get(types.ETString, n) 967 if err != nil { 968 return err 969 } 970 defer b.bufSlabPredictor.put(buf) 971 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 972 return err 973 } 974 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 975 if err != nil { 976 return err 977 } 978 defer b.bufSlabPredictor.put(buf1) 979 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 980 return err 981 } 982 buf2, err := b.bufSlabPredictor.get(types.ETString, n) 983 if err != nil { 984 return err 985 } 986 defer b.bufSlabPredictor.put(buf2) 987 if err := b.args[2].VecEvalString(b.ctx, input, buf2); err != nil { 988 return err 989 } 990 991 result.ReserveString(n) 992 i64s := buf1.Int64s() 993 for i := 0; i < n; i++ { 994 if buf.IsNull(i) || buf1.IsNull(i) { 995 result.AppendNull() 996 continue 997 } 998 targetLength := int(i64s[i]) 999 if uint64(targetLength)*uint64(allegrosql.MaxBytesOfCharacter) > b.maxAllowedPacket { 1000 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("lpad", b.maxAllowedPacket)) 1001 result.AppendNull() 1002 continue 1003 } 1004 if buf2.IsNull(i) { 1005 result.AppendNull() 1006 continue 1007 } 1008 str := buf.GetString(i) 1009 padStr := buf2.GetString(i) 1010 runeLength := len([]rune(str)) 1011 padLength := len([]rune(padStr)) 1012 1013 if targetLength < 0 || targetLength*4 > b.tp.Flen || (runeLength < targetLength && padLength == 0) { 1014 result.AppendNull() 1015 continue 1016 } 1017 if tailLen := targetLength - runeLength; tailLen > 0 { 1018 repeatCount := tailLen/padLength + 1 1019 str = string([]rune(strings.Repeat(padStr, repeatCount))[:tailLen]) + str 1020 } 1021 result.AppendString(string([]rune(str)[:targetLength])) 1022 } 1023 return nil 1024 } 1025 1026 func (b *builtinFindInSetSig) vectorized() bool { 1027 return true 1028 } 1029 1030 func (b *builtinFindInSetSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1031 n := input.NumEvents() 1032 str, err := b.bufSlabPredictor.get(types.ETString, n) 1033 if err != nil { 1034 return err 1035 } 1036 defer b.bufSlabPredictor.put(str) 1037 if err := b.args[0].VecEvalString(b.ctx, input, str); err != nil { 1038 return err 1039 } 1040 strlist, err := b.bufSlabPredictor.get(types.ETString, n) 1041 if err != nil { 1042 return err 1043 } 1044 defer b.bufSlabPredictor.put(strlist) 1045 if err := b.args[1].VecEvalString(b.ctx, input, strlist); err != nil { 1046 return err 1047 } 1048 result.ResizeInt64(n, false) 1049 result.MergeNulls(str, strlist) 1050 res := result.Int64s() 1051 for i := 0; i < n; i++ { 1052 if result.IsNull(i) { 1053 continue 1054 } 1055 strlistI := strlist.GetString(i) 1056 if len(strlistI) == 0 { 1057 res[i] = 0 1058 continue 1059 } 1060 for j, strInSet := range strings.Split(strlistI, ",") { 1061 if b.ctor.Compare(str.GetString(i), strInSet) == 0 { 1062 res[i] = int64(j + 1) 1063 } 1064 } 1065 } 1066 return nil 1067 } 1068 1069 func (b *builtinLeftSig) vectorized() bool { 1070 return true 1071 } 1072 1073 func (b *builtinLeftSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1074 n := input.NumEvents() 1075 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1076 if err != nil { 1077 return err 1078 } 1079 defer b.bufSlabPredictor.put(buf) 1080 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1081 return err 1082 } 1083 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1084 if err != nil { 1085 return err 1086 } 1087 defer b.bufSlabPredictor.put(buf2) 1088 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 1089 return err 1090 } 1091 left := buf2.Int64s() 1092 result.ReserveString(n) 1093 for i := 0; i < n; i++ { 1094 if buf.IsNull(i) || buf2.IsNull(i) { 1095 result.AppendNull() 1096 continue 1097 } 1098 leftLength, str := int(left[i]), buf.GetString(i) 1099 if strLength := len(str); leftLength > strLength { 1100 leftLength = strLength 1101 } else if leftLength < 0 { 1102 leftLength = 0 1103 } 1104 result.AppendString(str[:leftLength]) 1105 } 1106 return nil 1107 } 1108 1109 func (b *builtinReverseSig) vectorized() bool { 1110 return true 1111 } 1112 1113 func (b *builtinReverseSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1114 if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil { 1115 return err 1116 } 1117 for i := 0; i < input.NumEvents(); i++ { 1118 if result.IsNull(i) { 1119 continue 1120 } 1121 reversed := reverseBytes(result.GetBytes(i)) 1122 result.SetRaw(i, reversed) 1123 } 1124 return nil 1125 } 1126 1127 func (b *builtinRTrimSig) vectorized() bool { 1128 return true 1129 } 1130 1131 // vecEvalString evals a builtinRTrimSig 1132 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_rtrim 1133 func (b *builtinRTrimSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1134 n := input.NumEvents() 1135 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1136 if err != nil { 1137 return err 1138 } 1139 defer b.bufSlabPredictor.put(buf) 1140 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1141 return err 1142 } 1143 1144 result.ReserveString(n) 1145 for i := 0; i < n; i++ { 1146 if buf.IsNull(i) { 1147 result.AppendNull() 1148 continue 1149 } 1150 1151 str := buf.GetString(i) 1152 result.AppendString(strings.TrimRight(str, spaceChars)) 1153 } 1154 1155 return nil 1156 } 1157 1158 func (b *builtinStrcmpSig) vectorized() bool { 1159 return true 1160 } 1161 1162 func (b *builtinStrcmpSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1163 n := input.NumEvents() 1164 leftBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1165 if err != nil { 1166 return err 1167 } 1168 defer b.bufSlabPredictor.put(leftBuf) 1169 if err := b.args[0].VecEvalString(b.ctx, input, leftBuf); err != nil { 1170 return err 1171 } 1172 rightBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1173 if err != nil { 1174 return err 1175 } 1176 defer b.bufSlabPredictor.put(rightBuf) 1177 if err := b.args[1].VecEvalString(b.ctx, input, rightBuf); err != nil { 1178 return err 1179 } 1180 result.ResizeInt64(n, false) 1181 result.MergeNulls(leftBuf, rightBuf) 1182 i64s := result.Int64s() 1183 for i := 0; i < n; i++ { 1184 // if left or right is null, then set to null and return 0(which is the default value) 1185 if result.IsNull(i) { 1186 continue 1187 } 1188 i64s[i] = int64(types.CompareString(leftBuf.GetString(i), rightBuf.GetString(i), b.defCauslation)) 1189 } 1190 return nil 1191 } 1192 1193 func (b *builtinLocate2ArgsSig) vectorized() bool { 1194 return true 1195 } 1196 1197 func (b *builtinLocate2ArgsSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1198 n := input.NumEvents() 1199 buf0, err := b.bufSlabPredictor.get(types.ETString, n) 1200 if err != nil { 1201 return err 1202 } 1203 defer b.bufSlabPredictor.put(buf0) 1204 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 1205 if err != nil { 1206 return err 1207 } 1208 defer b.bufSlabPredictor.put(buf1) 1209 if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil { 1210 return err 1211 } 1212 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 1213 return err 1214 } 1215 result.ResizeInt64(n, false) 1216 result.MergeNulls(buf0, buf1) 1217 i64s := result.Int64s() 1218 for i := 0; i < n; i++ { 1219 if result.IsNull(i) { 1220 continue 1221 } 1222 subStr := buf0.GetString(i) 1223 if len(subStr) == 0 { 1224 i64s[i] = 1 1225 continue 1226 } 1227 i64s[i] = int64(strings.Index(buf1.GetString(i), subStr) + 1) 1228 } 1229 return nil 1230 } 1231 1232 func (b *builtinLocate3ArgsSig) vectorized() bool { 1233 return true 1234 } 1235 1236 // vecEvalInt evals LOCATE(substr,str,pos), case-sensitive. 1237 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_locate 1238 func (b *builtinLocate3ArgsSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1239 n := input.NumEvents() 1240 buf0, err := b.bufSlabPredictor.get(types.ETString, n) 1241 if err != nil { 1242 return err 1243 } 1244 defer b.bufSlabPredictor.put(buf0) 1245 if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil { 1246 return err 1247 } 1248 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 1249 if err != nil { 1250 return err 1251 } 1252 defer b.bufSlabPredictor.put(buf1) 1253 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 1254 return err 1255 } 1256 // causetstore positions in result 1257 if err := b.args[2].VecEvalInt(b.ctx, input, result); err != nil { 1258 return err 1259 } 1260 1261 result.MergeNulls(buf0, buf1) 1262 i64s := result.Int64s() 1263 for i := 0; i < n; i++ { 1264 if result.IsNull(i) { 1265 continue 1266 } 1267 pos := i64s[i] 1268 // Transfer the argument which starts from 1 to real index which starts from 0. 1269 pos-- 1270 subStr := buf0.GetString(i) 1271 str := buf1.GetString(i) 1272 subStrLen := len(subStr) 1273 if pos < 0 || pos > int64(len(str)-subStrLen) { 1274 i64s[i] = 0 1275 continue 1276 } else if subStrLen == 0 { 1277 i64s[i] = pos + 1 1278 continue 1279 } 1280 slice := str[pos:] 1281 idx := strings.Index(slice, subStr) 1282 if idx != -1 { 1283 i64s[i] = pos + int64(idx) + 1 1284 continue 1285 } 1286 i64s[i] = 0 1287 } 1288 return nil 1289 } 1290 1291 func (b *builtinExportSet4ArgSig) vectorized() bool { 1292 return true 1293 } 1294 1295 // vecEvalString evals EXPORT_SET(bits,on,off,separator). 1296 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_export-set 1297 func (b *builtinExportSet4ArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1298 n := input.NumEvents() 1299 bits, err := b.bufSlabPredictor.get(types.ETInt, n) 1300 if err != nil { 1301 return err 1302 } 1303 defer b.bufSlabPredictor.put(bits) 1304 if err := b.args[0].VecEvalInt(b.ctx, input, bits); err != nil { 1305 return err 1306 } 1307 on, err := b.bufSlabPredictor.get(types.ETString, n) 1308 if err != nil { 1309 return err 1310 } 1311 defer b.bufSlabPredictor.put(on) 1312 if err := b.args[1].VecEvalString(b.ctx, input, on); err != nil { 1313 return err 1314 } 1315 off, err := b.bufSlabPredictor.get(types.ETString, n) 1316 if err != nil { 1317 return err 1318 } 1319 defer b.bufSlabPredictor.put(off) 1320 if err := b.args[2].VecEvalString(b.ctx, input, off); err != nil { 1321 return err 1322 } 1323 separator, err := b.bufSlabPredictor.get(types.ETString, n) 1324 if err != nil { 1325 return err 1326 } 1327 defer b.bufSlabPredictor.put(separator) 1328 if err := b.args[3].VecEvalString(b.ctx, input, separator); err != nil { 1329 return err 1330 } 1331 result.ReserveString(n) 1332 i64s := bits.Int64s() 1333 for i := 0; i < n; i++ { 1334 if bits.IsNull(i) || on.IsNull(i) || off.IsNull(i) || separator.IsNull(i) { 1335 result.AppendNull() 1336 continue 1337 } 1338 result.AppendString(exportSet(i64s[i], on.GetString(i), off.GetString(i), 1339 separator.GetString(i), 64)) 1340 } 1341 return nil 1342 } 1343 1344 func (b *builtinRpadSig) vectorized() bool { 1345 return true 1346 } 1347 1348 // vecEvalString evals RPAD(str,len,padstr). 1349 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_rpad 1350 func (b *builtinRpadSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1351 n := input.NumEvents() 1352 strBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1353 if err != nil { 1354 return err 1355 } 1356 defer b.bufSlabPredictor.put(strBuf) 1357 if err := b.args[0].VecEvalString(b.ctx, input, strBuf); err != nil { 1358 return err 1359 } 1360 lenBuf, err := b.bufSlabPredictor.get(types.ETInt, n) 1361 if err != nil { 1362 return err 1363 } 1364 defer b.bufSlabPredictor.put(lenBuf) 1365 if err := b.args[1].VecEvalInt(b.ctx, input, lenBuf); err != nil { 1366 return err 1367 } 1368 padBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1369 if err != nil { 1370 return err 1371 } 1372 defer b.bufSlabPredictor.put(padBuf) 1373 if err := b.args[2].VecEvalString(b.ctx, input, padBuf); err != nil { 1374 return err 1375 } 1376 1377 result.ReserveString(n) 1378 i64s := lenBuf.Int64s() 1379 lenBuf.MergeNulls(strBuf) 1380 for i := 0; i < n; i++ { 1381 if lenBuf.IsNull(i) { 1382 result.AppendNull() 1383 continue 1384 } 1385 targetLength := int(i64s[i]) 1386 if uint64(targetLength) > b.maxAllowedPacket { 1387 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("rpad", b.maxAllowedPacket)) 1388 result.AppendNull() 1389 continue 1390 } 1391 1392 if padBuf.IsNull(i) { 1393 result.AppendNull() 1394 continue 1395 } 1396 str := strBuf.GetString(i) 1397 strLength := len(str) 1398 padStr := padBuf.GetString(i) 1399 padLength := len(padStr) 1400 if targetLength < 0 || targetLength > b.tp.Flen || (strLength < targetLength && padLength == 0) { 1401 result.AppendNull() 1402 continue 1403 } 1404 if tailLen := targetLength - strLength; tailLen > 0 { 1405 repeatCount := tailLen/padLength + 1 1406 str = str + strings.Repeat(padStr, repeatCount) 1407 } 1408 result.AppendString(str[:targetLength]) 1409 } 1410 return nil 1411 } 1412 1413 func (b *builtinFormatWithLocaleSig) vectorized() bool { 1414 return true 1415 } 1416 1417 func (b *builtinFormatWithLocaleSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1418 n := input.NumEvents() 1419 1420 dBuf, err := b.bufSlabPredictor.get(types.ETInt, n) 1421 if err != nil { 1422 return err 1423 } 1424 defer b.bufSlabPredictor.put(dBuf) 1425 if err := b.args[1].VecEvalInt(b.ctx, input, dBuf); err != nil { 1426 return err 1427 } 1428 dInt64s := dBuf.Int64s() 1429 1430 localeBuf, err := b.bufSlabPredictor.get(types.ETString, n) 1431 if err != nil { 1432 return err 1433 } 1434 defer b.bufSlabPredictor.put(localeBuf) 1435 if err := b.args[2].VecEvalString(b.ctx, input, localeBuf); err != nil { 1436 return err 1437 } 1438 1439 // decimal x 1440 if b.args[0].GetType().EvalType() == types.ETDecimal { 1441 xBuf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 1442 if err != nil { 1443 return err 1444 } 1445 defer b.bufSlabPredictor.put(xBuf) 1446 if err := b.args[0].VecEvalDecimal(b.ctx, input, xBuf); err != nil { 1447 return err 1448 } 1449 1450 result.ReserveString(n) 1451 xBuf.MergeNulls(dBuf) 1452 return formatDecimal(b.ctx, xBuf, dInt64s, result, localeBuf) 1453 } 1454 1455 // real x 1456 xBuf, err := b.bufSlabPredictor.get(types.ETReal, n) 1457 if err != nil { 1458 return err 1459 } 1460 defer b.bufSlabPredictor.put(xBuf) 1461 if err := b.args[0].VecEvalReal(b.ctx, input, xBuf); err != nil { 1462 return err 1463 } 1464 1465 result.ReserveString(n) 1466 xBuf.MergeNulls(dBuf) 1467 return formatReal(b.ctx, xBuf, dInt64s, result, localeBuf) 1468 } 1469 1470 func (b *builtinSubstring2ArgsSig) vectorized() bool { 1471 return true 1472 } 1473 1474 func (b *builtinSubstring2ArgsSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1475 n := input.NumEvents() 1476 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1477 if err != nil { 1478 return err 1479 } 1480 defer b.bufSlabPredictor.put(buf) 1481 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1482 return err 1483 } 1484 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1485 if err != nil { 1486 return err 1487 } 1488 defer b.bufSlabPredictor.put(buf2) 1489 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 1490 return err 1491 } 1492 1493 result.ReserveString(n) 1494 nums := buf2.Int64s() 1495 for i := 0; i < n; i++ { 1496 if buf.IsNull(i) || buf2.IsNull(i) { 1497 result.AppendNull() 1498 continue 1499 } 1500 1501 str := buf.GetString(i) 1502 pos := nums[i] 1503 length := int64(len(str)) 1504 if pos < 0 { 1505 pos += length 1506 } else { 1507 pos-- 1508 } 1509 if pos > length || pos < 0 { 1510 pos = length 1511 } 1512 result.AppendString(str[pos:]) 1513 } 1514 return nil 1515 } 1516 1517 func (b *builtinSubstring2ArgsUTF8Sig) vectorized() bool { 1518 return true 1519 } 1520 1521 // vecEvalString evals SUBSTR(str,pos), SUBSTR(str FROM pos), SUBSTR() is a synonym for SUBSTRING(). 1522 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_substr 1523 func (b *builtinSubstring2ArgsUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1524 n := input.NumEvents() 1525 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1526 if err != nil { 1527 return err 1528 } 1529 defer b.bufSlabPredictor.put(buf) 1530 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1531 return err 1532 } 1533 1534 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1535 if err != nil { 1536 return err 1537 } 1538 defer b.bufSlabPredictor.put(buf2) 1539 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 1540 return err 1541 } 1542 1543 result.ReserveString(n) 1544 nums := buf2.Int64s() 1545 for i := 0; i < n; i++ { 1546 if buf.IsNull(i) || buf2.IsNull(i) { 1547 result.AppendNull() 1548 continue 1549 } 1550 1551 str := buf.GetString(i) 1552 pos := nums[i] 1553 1554 runes := []rune(str) 1555 length := int64(len(runes)) 1556 if pos < 0 { 1557 pos += length 1558 } else { 1559 pos-- 1560 } 1561 if pos > length || pos < 0 { 1562 pos = length 1563 } 1564 result.AppendString(string(runes[pos:])) 1565 } 1566 1567 return nil 1568 } 1569 1570 func (b *builtinTrim2ArgsSig) vectorized() bool { 1571 return true 1572 } 1573 1574 // vecEvalString evals a builtinTrim2ArgsSig, corresponding to trim(str, remstr) 1575 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_trim 1576 func (b *builtinTrim2ArgsSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1577 n := input.NumEvents() 1578 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1579 if err != nil { 1580 return err 1581 } 1582 defer b.bufSlabPredictor.put(buf) 1583 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1584 return err 1585 } 1586 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 1587 if err != nil { 1588 return err 1589 } 1590 defer b.bufSlabPredictor.put(buf1) 1591 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 1592 return err 1593 } 1594 1595 result.ReserveString(n) 1596 for i := 0; i < n; i++ { 1597 if buf.IsNull(i) || buf1.IsNull(i) { 1598 result.AppendNull() 1599 continue 1600 } 1601 1602 str := buf.GetString(i) 1603 remstr := buf1.GetString(i) 1604 result.AppendString(trimRight(trimLeft(str, remstr), remstr)) 1605 } 1606 1607 return nil 1608 } 1609 1610 func (b *builtinInstrUTF8Sig) vectorized() bool { 1611 return true 1612 } 1613 1614 func (b *builtinInstrUTF8Sig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1615 n := input.NumEvents() 1616 str, err := b.bufSlabPredictor.get(types.ETString, n) 1617 if err != nil { 1618 return err 1619 } 1620 defer b.bufSlabPredictor.put(str) 1621 if err := b.args[0].VecEvalString(b.ctx, input, str); err != nil { 1622 return err 1623 } 1624 substr, err := b.bufSlabPredictor.get(types.ETString, n) 1625 if err != nil { 1626 return err 1627 } 1628 defer b.bufSlabPredictor.put(substr) 1629 if err := b.args[1].VecEvalString(b.ctx, input, substr); err != nil { 1630 return err 1631 } 1632 result.ResizeInt64(n, false) 1633 result.MergeNulls(str, substr) 1634 res := result.Int64s() 1635 ci := defCauslate.IsCIDefCauslation(b.defCauslation) 1636 var strI string 1637 var substrI string 1638 for i := 0; i < n; i++ { 1639 if result.IsNull(i) { 1640 continue 1641 } 1642 if ci { 1643 strI = strings.ToLower(str.GetString(i)) 1644 substrI = strings.ToLower(substr.GetString(i)) 1645 } else { 1646 strI = str.GetString(i) 1647 substrI = substr.GetString(i) 1648 } 1649 idx := strings.Index(strI, substrI) 1650 if idx == -1 { 1651 res[i] = 0 1652 continue 1653 } 1654 res[i] = int64(utf8.RuneCountInString(strI[:idx]) + 1) 1655 } 1656 return nil 1657 } 1658 1659 func (b *builtinOctStringSig) vectorized() bool { 1660 return true 1661 } 1662 1663 func (b *builtinOctStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1664 n := input.NumEvents() 1665 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1666 if err != nil { 1667 return err 1668 } 1669 defer b.bufSlabPredictor.put(buf) 1670 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1671 return err 1672 } 1673 1674 result.ReserveString(n) 1675 for i := 0; i < n; i++ { 1676 if buf.IsNull(i) { 1677 result.AppendNull() 1678 continue 1679 } 1680 negative, overflow := false, false 1681 str := buf.GetString(i) 1682 str = getValidPrefix(strings.TrimSpace(str), 10) 1683 if len(str) == 0 { 1684 result.AppendString("0") 1685 continue 1686 } 1687 if str[0] == '-' { 1688 negative, str = true, str[1:] 1689 } 1690 numVal, err := strconv.ParseUint(str, 10, 64) 1691 if err != nil { 1692 numError, ok := err.(*strconv.NumError) 1693 if !ok || numError.Err != strconv.ErrRange { 1694 return err 1695 } 1696 overflow = true 1697 } 1698 if negative && !overflow { 1699 numVal = -numVal 1700 } 1701 result.AppendString(strconv.FormatUint(numVal, 8)) 1702 } 1703 return nil 1704 } 1705 1706 func (b *builtinEltSig) vectorized() bool { 1707 return true 1708 } 1709 1710 // vecEvalString evals a ELT(N,str1,str2,str3,...). 1711 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_elt 1712 func (b *builtinEltSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1713 n := input.NumEvents() 1714 buf0, err := b.bufSlabPredictor.get(types.ETInt, n) 1715 if err != nil { 1716 return err 1717 } 1718 defer b.bufSlabPredictor.put(buf0) 1719 if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil { 1720 return err 1721 } 1722 1723 result.ReserveString(n) 1724 i64s := buf0.Int64s() 1725 argLen := len(b.args) 1726 bufs := make([]*chunk.DeferredCauset, argLen) 1727 for i := 0; i < n; i++ { 1728 if buf0.IsNull(i) { 1729 result.AppendNull() 1730 continue 1731 } 1732 j := i64s[i] 1733 if j < 1 || j >= int64(argLen) { 1734 result.AppendNull() 1735 continue 1736 } 1737 if bufs[j] == nil { 1738 bufs[j], err = b.bufSlabPredictor.get(types.ETString, n) 1739 if err != nil { 1740 return err 1741 } 1742 defer b.bufSlabPredictor.put(bufs[j]) 1743 if err := b.args[j].VecEvalString(b.ctx, input, bufs[j]); err != nil { 1744 return err 1745 } 1746 } 1747 if bufs[j].IsNull(i) { 1748 result.AppendNull() 1749 continue 1750 } 1751 result.AppendString(bufs[j].GetString(i)) 1752 } 1753 return nil 1754 } 1755 1756 func (b *builtinInsertUTF8Sig) vectorized() bool { 1757 return true 1758 } 1759 1760 // vecEvalString evals INSERT(str,pos,len,newstr). 1761 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_insert 1762 func (b *builtinInsertUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1763 n := input.NumEvents() 1764 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1765 if err != nil { 1766 return err 1767 } 1768 defer b.bufSlabPredictor.put(buf) 1769 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1770 return err 1771 } 1772 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 1773 if err != nil { 1774 return err 1775 } 1776 defer b.bufSlabPredictor.put(buf1) 1777 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 1778 return err 1779 } 1780 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1781 if err != nil { 1782 return err 1783 } 1784 defer b.bufSlabPredictor.put(buf2) 1785 if err := b.args[2].VecEvalInt(b.ctx, input, buf2); err != nil { 1786 return err 1787 } 1788 buf3, err := b.bufSlabPredictor.get(types.ETString, n) 1789 if err != nil { 1790 return err 1791 } 1792 defer b.bufSlabPredictor.put(buf3) 1793 if err := b.args[3].VecEvalString(b.ctx, input, buf3); err != nil { 1794 return err 1795 } 1796 1797 result.ReserveString(n) 1798 i64s1 := buf1.Int64s() 1799 i64s2 := buf2.Int64s() 1800 buf1.MergeNulls(buf2) 1801 for i := 0; i < n; i++ { 1802 if buf.IsNull(i) || buf1.IsNull(i) || buf3.IsNull(i) { 1803 result.AppendNull() 1804 continue 1805 } 1806 str := buf.GetString(i) 1807 pos := i64s1[i] 1808 length := i64s2[i] 1809 newstr := buf3.GetString(i) 1810 1811 runes := []rune(str) 1812 runeLength := int64(len(runes)) 1813 if pos < 1 || pos > runeLength { 1814 result.AppendString(str) 1815 continue 1816 } 1817 if length > runeLength-pos+1 || length < 0 { 1818 length = runeLength - pos + 1 1819 } 1820 1821 strHead := string(runes[0 : pos-1]) 1822 strTail := string(runes[pos+length-1:]) 1823 if uint64(len(strHead)+len(newstr)+len(strTail)) > b.maxAllowedPacket { 1824 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("insert", b.maxAllowedPacket)) 1825 result.AppendNull() 1826 continue 1827 } 1828 result.AppendString(strHead + newstr + strTail) 1829 } 1830 return nil 1831 } 1832 1833 func (b *builtinExportSet5ArgSig) vectorized() bool { 1834 return true 1835 } 1836 1837 // vecEvalString evals EXPORT_SET(bits,on,off,separator,number_of_bits). 1838 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_export-set 1839 func (b *builtinExportSet5ArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1840 n := input.NumEvents() 1841 bits, err := b.bufSlabPredictor.get(types.ETInt, n) 1842 if err != nil { 1843 return err 1844 } 1845 defer b.bufSlabPredictor.put(bits) 1846 if err := b.args[0].VecEvalInt(b.ctx, input, bits); err != nil { 1847 return err 1848 } 1849 on, err := b.bufSlabPredictor.get(types.ETString, n) 1850 if err != nil { 1851 return err 1852 } 1853 defer b.bufSlabPredictor.put(on) 1854 if err := b.args[1].VecEvalString(b.ctx, input, on); err != nil { 1855 return err 1856 } 1857 off, err := b.bufSlabPredictor.get(types.ETString, n) 1858 if err != nil { 1859 return err 1860 } 1861 defer b.bufSlabPredictor.put(off) 1862 if err := b.args[2].VecEvalString(b.ctx, input, off); err != nil { 1863 return err 1864 } 1865 separator, err := b.bufSlabPredictor.get(types.ETString, n) 1866 if err != nil { 1867 return err 1868 } 1869 defer b.bufSlabPredictor.put(separator) 1870 if err := b.args[3].VecEvalString(b.ctx, input, separator); err != nil { 1871 return err 1872 } 1873 numberOfBits, err := b.bufSlabPredictor.get(types.ETInt, n) 1874 if err != nil { 1875 return err 1876 } 1877 defer b.bufSlabPredictor.put(numberOfBits) 1878 if err := b.args[4].VecEvalInt(b.ctx, input, numberOfBits); err != nil { 1879 return err 1880 } 1881 result.ReserveString(n) 1882 bits.MergeNulls(numberOfBits) 1883 i64s := bits.Int64s() 1884 i64s2 := numberOfBits.Int64s() 1885 for i := 0; i < n; i++ { 1886 if bits.IsNull(i) || on.IsNull(i) || off.IsNull(i) || separator.IsNull(i) { 1887 result.AppendNull() 1888 continue 1889 } 1890 if i64s2[i] < 0 || i64s2[i] > 64 { 1891 i64s2[i] = 64 1892 } 1893 result.AppendString(exportSet(i64s[i], on.GetString(i), off.GetString(i), 1894 separator.GetString(i), i64s2[i])) 1895 } 1896 return nil 1897 } 1898 1899 func (b *builtinSubstring3ArgsUTF8Sig) vectorized() bool { 1900 return true 1901 } 1902 1903 // vecEvalString evals SUBSTR(str,pos,len), SUBSTR(str FROM pos FOR len), SUBSTR() is a synonym for SUBSTRING(). 1904 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_substr 1905 func (b *builtinSubstring3ArgsUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1906 n := input.NumEvents() 1907 buf, err := b.bufSlabPredictor.get(types.ETString, n) 1908 if err != nil { 1909 return err 1910 } 1911 defer b.bufSlabPredictor.put(buf) 1912 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 1913 return err 1914 } 1915 1916 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 1917 if err != nil { 1918 return err 1919 } 1920 defer b.bufSlabPredictor.put(buf1) 1921 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 1922 return err 1923 } 1924 1925 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1926 if err != nil { 1927 return err 1928 } 1929 defer b.bufSlabPredictor.put(buf2) 1930 if err := b.args[2].VecEvalInt(b.ctx, input, buf2); err != nil { 1931 return err 1932 } 1933 1934 result.ReserveString(n) 1935 positions := buf1.Int64s() 1936 lengths := buf2.Int64s() 1937 for i := 0; i < n; i++ { 1938 if buf.IsNull(i) || buf1.IsNull(i) || buf2.IsNull(i) { 1939 result.AppendNull() 1940 continue 1941 } 1942 1943 str := buf.GetString(i) 1944 pos := positions[i] 1945 length := lengths[i] 1946 runes := []rune(str) 1947 numRunes := int64(len(runes)) 1948 if pos < 0 { 1949 pos += numRunes 1950 } else { 1951 pos-- 1952 } 1953 if pos > numRunes || pos < 0 { 1954 pos = numRunes 1955 } 1956 end := pos + length 1957 if end < pos { 1958 result.AppendString("") 1959 continue 1960 } else if end < numRunes { 1961 result.AppendString(string(runes[pos:end])) 1962 continue 1963 } 1964 result.AppendString(string(runes[pos:])) 1965 } 1966 1967 return nil 1968 } 1969 1970 func (b *builtinTrim3ArgsSig) vectorized() bool { 1971 return true 1972 } 1973 1974 func (b *builtinTrim3ArgsSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 1975 n := input.NumEvents() 1976 buf0, err := b.bufSlabPredictor.get(types.ETString, n) 1977 if err != nil { 1978 return err 1979 } 1980 defer b.bufSlabPredictor.put(buf0) 1981 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 1982 if err != nil { 1983 return err 1984 } 1985 defer b.bufSlabPredictor.put(buf1) 1986 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 1987 if err != nil { 1988 return err 1989 } 1990 defer b.bufSlabPredictor.put(buf2) 1991 if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil { 1992 return err 1993 } 1994 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 1995 return err 1996 } 1997 if err := b.args[2].VecEvalInt(b.ctx, input, buf2); err != nil { 1998 return err 1999 } 2000 result.ReserveString(n) 2001 for i := 0; i < n; i++ { 2002 if buf0.IsNull(i) || buf2.IsNull(i) { 2003 result.AppendNull() 2004 continue 2005 } 2006 useDefaultRemStr := buf1.IsNull(i) 2007 direction := ast.TrimDirectionType(buf2.GetInt64(i)) 2008 baseStr := buf0.GetString(i) 2009 remStr := buf1.GetString(i) 2010 if direction == ast.TrimLeading { 2011 if useDefaultRemStr { 2012 result.AppendString(strings.TrimLeft(baseStr, spaceChars)) 2013 } else { 2014 result.AppendString(trimLeft(baseStr, remStr)) 2015 } 2016 } else if direction == ast.TrimTrailing { 2017 if useDefaultRemStr { 2018 result.AppendString(strings.TrimRight(baseStr, spaceChars)) 2019 } else { 2020 result.AppendString(trimRight(baseStr, remStr)) 2021 } 2022 } else { 2023 if useDefaultRemStr { 2024 result.AppendString(strings.Trim(baseStr, spaceChars)) 2025 } else { 2026 tmpStr := trimLeft(baseStr, remStr) 2027 result.AppendString(trimRight(tmpStr, remStr)) 2028 } 2029 } 2030 } 2031 return nil 2032 } 2033 2034 func (b *builtinOrdSig) vectorized() bool { 2035 return true 2036 } 2037 2038 func (b *builtinOrdSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2039 n := input.NumEvents() 2040 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2041 if err != nil { 2042 return err 2043 } 2044 defer b.bufSlabPredictor.put(buf) 2045 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2046 return err 2047 } 2048 2049 ord, err := chooseOrdFunc(b.args[0].GetType().Charset) 2050 if err != nil { 2051 return err 2052 } 2053 2054 result.ResizeInt64(n, false) 2055 result.MergeNulls(buf) 2056 i64s := result.Int64s() 2057 for i := 0; i < n; i++ { 2058 if result.IsNull(i) { 2059 continue 2060 } 2061 str := buf.GetString(i) 2062 i64s[i] = ord(str) 2063 } 2064 return nil 2065 } 2066 2067 func (b *builtinInstrSig) vectorized() bool { 2068 return true 2069 } 2070 2071 func (b *builtinInstrSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2072 n := input.NumEvents() 2073 str, err := b.bufSlabPredictor.get(types.ETString, n) 2074 if err != nil { 2075 return err 2076 } 2077 defer b.bufSlabPredictor.put(str) 2078 if err := b.args[0].VecEvalString(b.ctx, input, str); err != nil { 2079 return err 2080 } 2081 substr, err := b.bufSlabPredictor.get(types.ETString, n) 2082 if err != nil { 2083 return err 2084 } 2085 defer b.bufSlabPredictor.put(substr) 2086 if err := b.args[1].VecEvalString(b.ctx, input, substr); err != nil { 2087 return err 2088 } 2089 result.ResizeInt64(n, false) 2090 result.MergeNulls(str, substr) 2091 res := result.Int64s() 2092 for i := 0; i < n; i++ { 2093 if result.IsNull(i) { 2094 continue 2095 } 2096 strI := str.GetString(i) 2097 substrI := substr.GetString(i) 2098 idx := strings.Index(strI, substrI) 2099 res[i] = int64(idx + 1) 2100 } 2101 return nil 2102 } 2103 2104 func (b *builtinLengthSig) vectorized() bool { 2105 return true 2106 } 2107 2108 // vecEvalInt evaluates a builtinLengthSig. 2109 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html 2110 func (b *builtinLengthSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2111 n := input.NumEvents() 2112 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2113 if err != nil { 2114 return err 2115 } 2116 defer b.bufSlabPredictor.put(buf) 2117 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2118 return err 2119 } 2120 2121 result.ResizeInt64(n, false) 2122 result.MergeNulls(buf) 2123 i64s := result.Int64s() 2124 for i := 0; i < n; i++ { 2125 if result.IsNull(i) { 2126 continue 2127 } 2128 str := buf.GetBytes(i) 2129 i64s[i] = int64(len(str)) 2130 } 2131 return nil 2132 } 2133 2134 func (b *builtinLocate2ArgsUTF8Sig) vectorized() bool { 2135 return true 2136 } 2137 2138 // vecEvalInt evals LOCATE(substr,str). 2139 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_locate 2140 func (b *builtinLocate2ArgsUTF8Sig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2141 n := input.NumEvents() 2142 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2143 if err != nil { 2144 return err 2145 } 2146 defer b.bufSlabPredictor.put(buf) 2147 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2148 return err 2149 } 2150 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 2151 if err != nil { 2152 return err 2153 } 2154 defer b.bufSlabPredictor.put(buf1) 2155 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 2156 return err 2157 } 2158 2159 result.ResizeInt64(n, false) 2160 result.MergeNulls(buf, buf1) 2161 i64s := result.Int64s() 2162 ci := defCauslate.IsCIDefCauslation(b.defCauslation) 2163 for i := 0; i < n; i++ { 2164 if result.IsNull(i) { 2165 continue 2166 } 2167 subStr := buf.GetString(i) 2168 str := buf1.GetString(i) 2169 subStrLen := int64(len([]rune(subStr))) 2170 if subStrLen == 0 { 2171 i64s[i] = 1 2172 continue 2173 } 2174 slice := str 2175 if ci { 2176 slice = strings.ToLower(slice) 2177 subStr = strings.ToLower(subStr) 2178 } 2179 idx := strings.Index(slice, subStr) 2180 if idx != -1 { 2181 i64s[i] = int64(utf8.RuneCountInString(slice[:idx])) + 1 2182 continue 2183 } 2184 i64s[i] = 0 2185 } 2186 return nil 2187 } 2188 2189 func (b *builtinBitLengthSig) vectorized() bool { 2190 return true 2191 } 2192 2193 func (b *builtinBitLengthSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2194 n := input.NumEvents() 2195 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2196 if err != nil { 2197 return err 2198 } 2199 defer b.bufSlabPredictor.put(buf) 2200 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2201 return err 2202 } 2203 2204 result.ResizeInt64(n, false) 2205 result.MergeNulls(buf) 2206 i64s := result.Int64s() 2207 for i := 0; i < n; i++ { 2208 if result.IsNull(i) { 2209 continue 2210 } 2211 str := buf.GetBytes(i) 2212 i64s[i] = int64(len(str) * 8) 2213 } 2214 return nil 2215 } 2216 2217 func (b *builtinCharSig) vectorized() bool { 2218 return true 2219 } 2220 2221 func (b *builtinCharSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2222 n := input.NumEvents() 2223 l := len(b.args) 2224 buf := make([]*chunk.DeferredCauset, l-1) 2225 for i := 0; i < len(b.args)-1; i++ { 2226 te, err := b.bufSlabPredictor.get(types.ETInt, n) 2227 if err != nil { 2228 return err 2229 } 2230 buf[i] = te 2231 defer b.bufSlabPredictor.put(buf[i]) 2232 if err := b.args[i].VecEvalInt(b.ctx, input, buf[i]); err != nil { 2233 return err 2234 } 2235 } 2236 bufstr, err := b.bufSlabPredictor.get(types.ETString, n) 2237 if err != nil { 2238 return err 2239 } 2240 defer b.bufSlabPredictor.put(bufstr) 2241 bigints := make([]int64, 0, l-1) 2242 result.ReserveString(n) 2243 bufint := make([]([]int64), l-1) 2244 for i := 0; i < l-1; i++ { 2245 bufint[i] = buf[i].Int64s() 2246 } 2247 for i := 0; i < n; i++ { 2248 bigints = bigints[0:0] 2249 for j := 0; j < l-1; j++ { 2250 if buf[j].IsNull(i) { 2251 continue 2252 } 2253 bigints = append(bigints, bufint[j][i]) 2254 } 2255 tempString := string(b.convertToBytes(bigints)) 2256 result.AppendString(tempString) 2257 } 2258 return nil 2259 } 2260 2261 func (b *builtinReplaceSig) vectorized() bool { 2262 return true 2263 } 2264 2265 // vecEvalString evals a builtinReplaceSig. 2266 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_replace 2267 func (b *builtinReplaceSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2268 n := input.NumEvents() 2269 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2270 if err != nil { 2271 return err 2272 } 2273 defer b.bufSlabPredictor.put(buf) 2274 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2275 return err 2276 } 2277 buf1, err := b.bufSlabPredictor.get(types.ETString, n) 2278 if err != nil { 2279 return err 2280 } 2281 defer b.bufSlabPredictor.put(buf1) 2282 if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil { 2283 return err 2284 } 2285 buf2, err := b.bufSlabPredictor.get(types.ETString, n) 2286 if err != nil { 2287 return err 2288 } 2289 defer b.bufSlabPredictor.put(buf2) 2290 if err := b.args[2].VecEvalString(b.ctx, input, buf2); err != nil { 2291 return err 2292 } 2293 2294 result.ReserveString(n) 2295 for i := 0; i < n; i++ { 2296 if buf.IsNull(i) || buf1.IsNull(i) || buf2.IsNull(i) { 2297 result.AppendNull() 2298 continue 2299 } 2300 str := buf.GetString(i) 2301 oldStr := buf1.GetString(i) 2302 newStr := buf2.GetString(i) 2303 if oldStr == "" { 2304 result.AppendString(str) 2305 continue 2306 } 2307 str = strings.Replace(str, oldStr, newStr, -1) 2308 result.AppendString(str) 2309 } 2310 return nil 2311 } 2312 2313 func (b *builtinMakeSetSig) vectorized() bool { 2314 return true 2315 } 2316 2317 // vecEvalString evals MAKE_SET(bits,str1,str2,...). 2318 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_make-set 2319 func (b *builtinMakeSetSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2320 nr := input.NumEvents() 2321 bitsBuf, err := b.bufSlabPredictor.get(types.ETInt, nr) 2322 if err != nil { 2323 return err 2324 } 2325 defer b.bufSlabPredictor.put(bitsBuf) 2326 if err := b.args[0].VecEvalInt(b.ctx, input, bitsBuf); err != nil { 2327 return err 2328 } 2329 2330 strBuf := make([]*chunk.DeferredCauset, len(b.args)-1) 2331 for i := 1; i < len(b.args); i++ { 2332 strBuf[i-1], err = b.bufSlabPredictor.get(types.ETString, nr) 2333 if err != nil { 2334 return err 2335 } 2336 defer b.bufSlabPredictor.put(strBuf[i-1]) 2337 if err := b.args[i].VecEvalString(b.ctx, input, strBuf[i-1]); err != nil { 2338 return err 2339 } 2340 } 2341 2342 bits := bitsBuf.Int64s() 2343 result.ReserveString(nr) 2344 sets := make([]string, 0, len(b.args)-1) 2345 for i := 0; i < nr; i++ { 2346 if bitsBuf.IsNull(i) { 2347 result.AppendNull() 2348 continue 2349 } 2350 sets = sets[:0] 2351 for j := 0; j < len(b.args)-1; j++ { 2352 if strBuf[j].IsNull(i) || (bits[i]&(1<<uint(j))) == 0 { 2353 continue 2354 } 2355 sets = append(sets, strBuf[j].GetString(i)) 2356 } 2357 result.AppendString(strings.Join(sets, ",")) 2358 } 2359 2360 return nil 2361 } 2362 2363 func (b *builtinOctIntSig) vectorized() bool { 2364 return true 2365 } 2366 2367 func (b *builtinOctIntSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2368 n := input.NumEvents() 2369 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 2370 if err != nil { 2371 return err 2372 } 2373 defer b.bufSlabPredictor.put(buf) 2374 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 2375 return err 2376 } 2377 2378 result.ReserveString(n) 2379 nums := buf.Int64s() 2380 for i := 0; i < n; i++ { 2381 if buf.IsNull(i) { 2382 result.AppendNull() 2383 continue 2384 } 2385 result.AppendString(strconv.FormatUint(uint64(nums[i]), 8)) 2386 } 2387 return nil 2388 } 2389 2390 func (b *builtinToBase64Sig) vectorized() bool { 2391 return true 2392 } 2393 2394 // vecEvalString evals a builtinToBase64Sig. 2395 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_to-base64 2396 func (b *builtinToBase64Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2397 n := input.NumEvents() 2398 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2399 if err != nil { 2400 return err 2401 } 2402 defer b.bufSlabPredictor.put(buf) 2403 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2404 return err 2405 } 2406 2407 result.ReserveString(n) 2408 for i := 0; i < n; i++ { 2409 if buf.IsNull(i) { 2410 result.AppendNull() 2411 continue 2412 } 2413 str := buf.GetString(i) 2414 needEncodeLen := base64NeededEncodedLength(len(str)) 2415 if needEncodeLen == -1 { 2416 result.AppendNull() 2417 continue 2418 } else if needEncodeLen > int(b.maxAllowedPacket) { 2419 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("to_base64", b.maxAllowedPacket)) 2420 result.AppendNull() 2421 continue 2422 } else if b.tp.Flen == -1 || b.tp.Flen > allegrosql.MaxBlobWidth { 2423 result.AppendNull() 2424 continue 2425 } 2426 2427 newStr := base64.StdEncoding.EncodeToString([]byte(str)) 2428 //A newline is added after each 76 characters of encoded output to divide long output into multiple lines. 2429 count := len(newStr) 2430 if count > 76 { 2431 newStr = strings.Join(splitToSubN(newStr, 76), "\n") 2432 } 2433 result.AppendString(newStr) 2434 } 2435 return nil 2436 } 2437 2438 func (b *builtinTrim1ArgSig) vectorized() bool { 2439 return true 2440 } 2441 2442 func (b *builtinTrim1ArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2443 n := input.NumEvents() 2444 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2445 if err != nil { 2446 return err 2447 } 2448 defer b.bufSlabPredictor.put(buf) 2449 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2450 return err 2451 } 2452 2453 result.ReserveString(n) 2454 for i := 0; i < n; i++ { 2455 if buf.IsNull(i) { 2456 result.AppendNull() 2457 continue 2458 } 2459 2460 str := buf.GetString(i) 2461 result.AppendString(strings.Trim(str, spaceChars)) 2462 } 2463 2464 return nil 2465 } 2466 2467 func (b *builtinRpadUTF8Sig) vectorized() bool { 2468 return true 2469 } 2470 2471 // vecEvalString evals RPAD(str,len,padstr). 2472 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_rpad 2473 func (b *builtinRpadUTF8Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2474 n := input.NumEvents() 2475 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2476 if err != nil { 2477 return err 2478 } 2479 defer b.bufSlabPredictor.put(buf) 2480 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2481 return err 2482 } 2483 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 2484 if err != nil { 2485 return err 2486 } 2487 defer b.bufSlabPredictor.put(buf1) 2488 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 2489 return err 2490 } 2491 buf2, err := b.bufSlabPredictor.get(types.ETString, n) 2492 if err != nil { 2493 return err 2494 } 2495 defer b.bufSlabPredictor.put(buf2) 2496 if err := b.args[2].VecEvalString(b.ctx, input, buf2); err != nil { 2497 return err 2498 } 2499 2500 result.ReserveString(n) 2501 i64s := buf1.Int64s() 2502 for i := 0; i < n; i++ { 2503 if buf.IsNull(i) || buf1.IsNull(i) { 2504 result.AppendNull() 2505 continue 2506 } 2507 targetLength := int(i64s[i]) 2508 if uint64(targetLength)*uint64(allegrosql.MaxBytesOfCharacter) > b.maxAllowedPacket { 2509 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("rpad", b.maxAllowedPacket)) 2510 result.AppendNull() 2511 continue 2512 } 2513 if buf2.IsNull(i) { 2514 result.AppendNull() 2515 continue 2516 } 2517 str := buf.GetString(i) 2518 padStr := buf2.GetString(i) 2519 runeLength := len([]rune(str)) 2520 padLength := len([]rune(padStr)) 2521 2522 if targetLength < 0 || targetLength*4 > b.tp.Flen || (runeLength < targetLength && padLength == 0) { 2523 result.AppendNull() 2524 continue 2525 } 2526 if tailLen := targetLength - runeLength; tailLen > 0 { 2527 repeatCount := tailLen/padLength + 1 2528 str = str + strings.Repeat(padStr, repeatCount) 2529 } 2530 result.AppendString(string([]rune(str)[:targetLength])) 2531 } 2532 return nil 2533 } 2534 2535 func (b *builtinCharLengthBinarySig) vectorized() bool { 2536 return true 2537 } 2538 2539 func (b *builtinCharLengthBinarySig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2540 n := input.NumEvents() 2541 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2542 if err != nil { 2543 return err 2544 } 2545 defer b.bufSlabPredictor.put(buf) 2546 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2547 return err 2548 } 2549 result.ResizeInt64(n, false) 2550 result.MergeNulls(buf) 2551 res := result.Int64s() 2552 for i := 0; i < n; i++ { 2553 if result.IsNull(i) { 2554 continue 2555 } 2556 str := buf.GetString(i) 2557 res[i] = int64(len(str)) 2558 } 2559 return nil 2560 } 2561 2562 func (b *builtinBinSig) vectorized() bool { 2563 return true 2564 } 2565 2566 func (b *builtinBinSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2567 n := input.NumEvents() 2568 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 2569 if err != nil { 2570 return err 2571 } 2572 defer b.bufSlabPredictor.put(buf) 2573 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 2574 return err 2575 } 2576 2577 result.ReserveString(n) 2578 nums := buf.Int64s() 2579 for i := 0; i < n; i++ { 2580 if buf.IsNull(i) { 2581 result.AppendNull() 2582 continue 2583 } 2584 result.AppendString(fmt.Sprintf("%b", uint64(nums[i]))) 2585 } 2586 return nil 2587 } 2588 2589 func (b *builtinFormatSig) vectorized() bool { 2590 return true 2591 } 2592 2593 // vecEvalString evals FORMAT(X,D). 2594 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_format 2595 func (b *builtinFormatSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2596 n := input.NumEvents() 2597 2598 dBuf, err := b.bufSlabPredictor.get(types.ETInt, n) 2599 if err != nil { 2600 return err 2601 } 2602 defer b.bufSlabPredictor.put(dBuf) 2603 if err := b.args[1].VecEvalInt(b.ctx, input, dBuf); err != nil { 2604 return err 2605 } 2606 dInt64s := dBuf.Int64s() 2607 2608 // decimal x 2609 if b.args[0].GetType().EvalType() == types.ETDecimal { 2610 xBuf, err := b.bufSlabPredictor.get(types.ETDecimal, n) 2611 if err != nil { 2612 return err 2613 } 2614 defer b.bufSlabPredictor.put(xBuf) 2615 if err := b.args[0].VecEvalDecimal(b.ctx, input, xBuf); err != nil { 2616 return err 2617 } 2618 2619 result.ReserveString(n) 2620 xBuf.MergeNulls(dBuf) 2621 return formatDecimal(b.ctx, xBuf, dInt64s, result, nil) 2622 } 2623 2624 // real x 2625 xBuf, err := b.bufSlabPredictor.get(types.ETReal, n) 2626 if err != nil { 2627 return err 2628 } 2629 defer b.bufSlabPredictor.put(xBuf) 2630 if err := b.args[0].VecEvalReal(b.ctx, input, xBuf); err != nil { 2631 return err 2632 } 2633 2634 result.ReserveString(n) 2635 xBuf.MergeNulls(dBuf) 2636 return formatReal(b.ctx, xBuf, dInt64s, result, nil) 2637 } 2638 2639 func (b *builtinRightSig) vectorized() bool { 2640 return true 2641 } 2642 2643 func (b *builtinRightSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2644 n := input.NumEvents() 2645 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2646 if err != nil { 2647 return err 2648 } 2649 defer b.bufSlabPredictor.put(buf) 2650 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2651 return err 2652 } 2653 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 2654 if err != nil { 2655 return err 2656 } 2657 defer b.bufSlabPredictor.put(buf2) 2658 if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil { 2659 return err 2660 } 2661 right := buf2.Int64s() 2662 result.ReserveString(n) 2663 for i := 0; i < n; i++ { 2664 if buf.IsNull(i) || buf2.IsNull(i) { 2665 result.AppendNull() 2666 continue 2667 } 2668 str, rightLength := buf.GetString(i), int(right[i]) 2669 strLength := len(str) 2670 if rightLength > strLength { 2671 rightLength = strLength 2672 } else if rightLength < 0 { 2673 rightLength = 0 2674 } 2675 result.AppendString(str[strLength-rightLength:]) 2676 } 2677 return nil 2678 } 2679 2680 func (b *builtinSubstring3ArgsSig) vectorized() bool { 2681 return true 2682 } 2683 2684 // vecEvalString evals SUBSTR(str,pos,len), SUBSTR(str FROM pos FOR len), SUBSTR() is a synonym for SUBSTRING(). 2685 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_substr 2686 func (b *builtinSubstring3ArgsSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2687 n := input.NumEvents() 2688 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2689 if err != nil { 2690 return err 2691 } 2692 defer b.bufSlabPredictor.put(buf) 2693 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2694 return err 2695 } 2696 2697 buf1, err := b.bufSlabPredictor.get(types.ETInt, n) 2698 if err != nil { 2699 return err 2700 } 2701 defer b.bufSlabPredictor.put(buf1) 2702 if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil { 2703 return err 2704 } 2705 2706 buf2, err := b.bufSlabPredictor.get(types.ETInt, n) 2707 if err != nil { 2708 return err 2709 } 2710 defer b.bufSlabPredictor.put(buf2) 2711 if err := b.args[2].VecEvalInt(b.ctx, input, buf2); err != nil { 2712 return err 2713 } 2714 2715 result.ReserveString(n) 2716 positions := buf1.Int64s() 2717 lengths := buf2.Int64s() 2718 for i := 0; i < n; i++ { 2719 if buf.IsNull(i) || buf1.IsNull(i) || buf2.IsNull(i) { 2720 result.AppendNull() 2721 continue 2722 } 2723 2724 str := buf.GetString(i) 2725 pos := positions[i] 2726 length := lengths[i] 2727 2728 byteLen := int64(len(str)) 2729 if pos < 0 { 2730 pos += byteLen 2731 } else { 2732 pos-- 2733 } 2734 if pos > byteLen || pos < 0 { 2735 pos = byteLen 2736 } 2737 end := pos + length 2738 if end < pos { 2739 result.AppendString("") 2740 continue 2741 } else if end < byteLen { 2742 result.AppendString(str[pos:end]) 2743 continue 2744 } 2745 result.AppendString(str[pos:]) 2746 } 2747 2748 return nil 2749 } 2750 2751 func (b *builtinHexIntArgSig) vectorized() bool { 2752 return true 2753 } 2754 2755 func (b *builtinHexIntArgSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2756 n := input.NumEvents() 2757 buf, err := b.bufSlabPredictor.get(types.ETInt, n) 2758 if err != nil { 2759 return err 2760 } 2761 defer b.bufSlabPredictor.put(buf) 2762 if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { 2763 return err 2764 } 2765 result.ReserveString(n) 2766 i64s := buf.Int64s() 2767 for i := 0; i < n; i++ { 2768 if buf.IsNull(i) { 2769 result.AppendNull() 2770 continue 2771 } 2772 result.AppendString(strings.ToUpper(fmt.Sprintf("%x", uint64(i64s[i])))) 2773 } 2774 return nil 2775 } 2776 2777 func (b *builtinFromBase64Sig) vectorized() bool { 2778 return true 2779 } 2780 2781 // vecEvalString evals FROM_BASE64(str). 2782 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_from-base64 2783 func (b *builtinFromBase64Sig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2784 n := input.NumEvents() 2785 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2786 if err != nil { 2787 return err 2788 } 2789 defer b.bufSlabPredictor.put(buf) 2790 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2791 return err 2792 } 2793 2794 result.ReserveString(n) 2795 for i := 0; i < n; i++ { 2796 if buf.IsNull(i) { 2797 result.AppendNull() 2798 continue 2799 } 2800 str := buf.GetString(i) 2801 needDecodeLen := base64NeededDecodedLength(len(str)) 2802 if needDecodeLen == -1 { 2803 result.AppendNull() 2804 continue 2805 } else if needDecodeLen > int(b.maxAllowedPacket) { 2806 b.ctx.GetStochastikVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("from_base64", b.maxAllowedPacket)) 2807 result.AppendNull() 2808 continue 2809 } 2810 2811 str = strings.Replace(str, "\t", "", -1) 2812 str = strings.Replace(str, " ", "", -1) 2813 newStr, err := base64.StdEncoding.DecodeString(str) 2814 if err != nil { 2815 // When error happens, take `from_base64("asc")` as an example, we should return NULL. 2816 result.AppendNull() 2817 continue 2818 } 2819 result.AppendString(string(newStr)) 2820 } 2821 return nil 2822 } 2823 2824 func (b *builtinCharLengthUTF8Sig) vectorized() bool { 2825 return true 2826 } 2827 2828 func (b *builtinCharLengthUTF8Sig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 2829 n := input.NumEvents() 2830 buf, err := b.bufSlabPredictor.get(types.ETString, n) 2831 if err != nil { 2832 return err 2833 } 2834 defer b.bufSlabPredictor.put(buf) 2835 if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil { 2836 return err 2837 } 2838 2839 result.ResizeInt64(n, false) 2840 result.MergeNulls(buf) 2841 i64s := result.Int64s() 2842 for i := 0; i < n; i++ { 2843 if result.IsNull(i) { 2844 continue 2845 } 2846 str := buf.GetString(i) 2847 i64s[i] = int64(len([]rune(str))) 2848 } 2849 return nil 2850 } 2851 2852 func formatDecimal(sctx stochastikctx.Context, xBuf *chunk.DeferredCauset, dInt64s []int64, result *chunk.DeferredCauset, localeBuf *chunk.DeferredCauset) error { 2853 xDecimals := xBuf.Decimals() 2854 for i := range xDecimals { 2855 if xBuf.IsNull(i) { 2856 result.AppendNull() 2857 continue 2858 } 2859 2860 x, d := xDecimals[i], dInt64s[i] 2861 2862 if d < 0 { 2863 d = 0 2864 } else if d > formatMaxDecimals { 2865 d = formatMaxDecimals 2866 } 2867 2868 var locale string 2869 if localeBuf == nil { 2870 // FORMAT(x, d) 2871 locale = "en_US" 2872 } else if localeBuf.IsNull(i) { 2873 // FORMAT(x, d, NULL) 2874 sctx.GetStochastikVars().StmtCtx.AppendWarning(errUnknownLocale.GenWithStackByArgs("NULL")) 2875 locale = "en_US" 2876 } else { 2877 locale = localeBuf.GetString(i) 2878 } 2879 2880 xStr := roundFormatArgs(x.String(), int(d)) 2881 dStr := strconv.FormatInt(d, 10) 2882 localeFormatFunction := allegrosql.GetLocaleFormatFunction(locale) 2883 2884 formatString, err := localeFormatFunction(xStr, dStr) 2885 if err != nil { 2886 return err 2887 } 2888 result.AppendString(formatString) 2889 } 2890 return nil 2891 } 2892 2893 func formatReal(sctx stochastikctx.Context, xBuf *chunk.DeferredCauset, dInt64s []int64, result *chunk.DeferredCauset, localeBuf *chunk.DeferredCauset) error { 2894 xFloat64s := xBuf.Float64s() 2895 for i := range xFloat64s { 2896 if xBuf.IsNull(i) { 2897 result.AppendNull() 2898 continue 2899 } 2900 2901 x, d := xFloat64s[i], dInt64s[i] 2902 2903 if d < 0 { 2904 d = 0 2905 } else if d > formatMaxDecimals { 2906 d = formatMaxDecimals 2907 } 2908 2909 var locale string 2910 if localeBuf == nil { 2911 // FORMAT(x, d) 2912 locale = "en_US" 2913 } else if localeBuf.IsNull(i) { 2914 // FORMAT(x, d, NULL) 2915 sctx.GetStochastikVars().StmtCtx.AppendWarning(errUnknownLocale.GenWithStackByArgs("NULL")) 2916 locale = "en_US" 2917 } else { 2918 locale = localeBuf.GetString(i) 2919 } 2920 2921 xStr := roundFormatArgs(strconv.FormatFloat(x, 'f', -1, 64), int(d)) 2922 dStr := strconv.FormatInt(d, 10) 2923 localeFormatFunction := allegrosql.GetLocaleFormatFunction(locale) 2924 2925 formatString, err := localeFormatFunction(xStr, dStr) 2926 if err != nil { 2927 return err 2928 } 2929 result.AppendString(formatString) 2930 } 2931 return nil 2932 }