github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/func_unary.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package function 16 17 import ( 18 "context" 19 "crypto/md5" 20 "crypto/sha1" 21 "encoding/base64" 22 "encoding/hex" 23 "fmt" 24 "io" 25 "math" 26 "runtime" 27 "strconv" 28 "strings" 29 "time" 30 "unsafe" 31 32 "github.com/RoaringBitmap/roaring" 33 "golang.org/x/exp/constraints" 34 35 "github.com/matrixorigin/matrixone/pkg/common/moerr" 36 "github.com/matrixorigin/matrixone/pkg/common/mpool" 37 "github.com/matrixorigin/matrixone/pkg/common/system" 38 "github.com/matrixorigin/matrixone/pkg/container/types" 39 "github.com/matrixorigin/matrixone/pkg/container/vector" 40 "github.com/matrixorigin/matrixone/pkg/fileservice" 41 "github.com/matrixorigin/matrixone/pkg/logutil" 42 "github.com/matrixorigin/matrixone/pkg/sql/plan/function/functionUtil" 43 "github.com/matrixorigin/matrixone/pkg/util/fault" 44 "github.com/matrixorigin/matrixone/pkg/vectorize/lengthutf8" 45 "github.com/matrixorigin/matrixone/pkg/vectorize/moarray" 46 "github.com/matrixorigin/matrixone/pkg/vectorize/momath" 47 "github.com/matrixorigin/matrixone/pkg/version" 48 "github.com/matrixorigin/matrixone/pkg/vm/process" 49 ) 50 51 func AbsUInt64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 52 return opUnaryFixedToFixed[uint64, uint64](ivecs, result, proc, length, func(v uint64) uint64 { 53 return v 54 }) 55 } 56 57 func AbsInt64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 58 return opUnaryFixedToFixedWithErrorCheck[int64, int64](ivecs, result, proc, length, func(v int64) (int64, error) { 59 return momath.AbsSigned[int64](v) 60 }) 61 } 62 63 func AbsFloat64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 64 return opUnaryFixedToFixedWithErrorCheck[float64, float64](ivecs, result, proc, length, func(v float64) (float64, error) { 65 return momath.AbsSigned[float64](v) 66 }) 67 } 68 69 func absDecimal64(v types.Decimal64) types.Decimal64 { 70 if v.Sign() { 71 v = v.Minus() 72 } 73 return v 74 } 75 76 func AbsDecimal64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 77 return opUnaryFixedToFixed[types.Decimal64, types.Decimal64](ivecs, result, proc, length, func(v types.Decimal64) types.Decimal64 { 78 return absDecimal64(v) 79 }) 80 } 81 82 func absDecimal128(v types.Decimal128) types.Decimal128 { 83 if v.Sign() { 84 v = v.Minus() 85 } 86 return v 87 } 88 89 func AbsDecimal128(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 90 return opUnaryFixedToFixed[types.Decimal128, types.Decimal128](ivecs, result, proc, length, func(v types.Decimal128) types.Decimal128 { 91 return absDecimal128(v) 92 }) 93 } 94 95 func AbsArray[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 96 return opUnaryBytesToBytesWithErrorCheck(ivecs, result, proc, length, func(in []byte) ([]byte, error) { 97 _in := types.BytesToArray[T](in) 98 _out, err := moarray.Abs(_in) 99 if err != nil { 100 return nil, err 101 } 102 return types.ArrayToBytes[T](_out), nil 103 }) 104 } 105 106 func NormalizeL2Array[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 107 return opUnaryBytesToBytesWithErrorCheck(ivecs, result, proc, length, func(in []byte) ([]byte, error) { 108 _in := types.BytesToArray[T](in) 109 _out, err := moarray.NormalizeL2(_in) 110 if err != nil { 111 return nil, err 112 } 113 return types.ArrayToBytes[T](_out), nil 114 }) 115 } 116 117 func L1NormArray[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 118 return opUnaryBytesToFixedWithErrorCheck[float64](ivecs, result, proc, length, func(in []byte) (float64, error) { 119 _in := types.BytesToArray[T](in) 120 return moarray.L1Norm(_in) 121 }) 122 } 123 124 func L2NormArray[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 125 return opUnaryBytesToFixedWithErrorCheck[float64](ivecs, result, proc, length, func(in []byte) (out float64, err error) { 126 _in := types.BytesToArray[T](in) 127 return moarray.L2Norm(_in) 128 }) 129 } 130 131 func VectorDimsArray[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 132 return opUnaryBytesToFixed[int64](ivecs, result, proc, length, func(in []byte) (out int64) { 133 _in := types.BytesToArray[T](in) 134 return int64(len(_in)) 135 }) 136 } 137 138 func SummationArray[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 139 return opUnaryBytesToFixedWithErrorCheck[float64](ivecs, result, proc, length, func(in []byte) (out float64, err error) { 140 _in := types.BytesToArray[T](in) 141 142 return moarray.Summation[T](_in) 143 }) 144 } 145 146 func SubVectorWith2Args[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) (err error) { 147 rs := vector.MustFunctionResult[types.Varlena](result) 148 vs := vector.GenerateFunctionStrParameter(ivecs[0]) 149 starts := vector.GenerateFunctionFixedTypeParameter[int64](ivecs[1]) 150 151 for i := uint64(0); i < uint64(length); i++ { 152 v, null1 := vs.GetStrValue(i) 153 s, null2 := starts.GetValue(i) 154 155 if null1 || null2 { 156 if err = rs.AppendBytes(nil, true); err != nil { 157 return err 158 } 159 } else { 160 var r []T 161 if s > 0 { 162 r = moarray.SubArrayFromLeft[T](types.BytesToArray[T](v), s-1) 163 } else if s < 0 { 164 r = moarray.SubArrayFromRight[T](types.BytesToArray[T](v), -s) 165 } else { 166 r = []T{} 167 } 168 if err = rs.AppendBytes(types.ArrayToBytes[T](r), false); err != nil { 169 return err 170 } 171 } 172 } 173 return nil 174 } 175 176 func SubVectorWith3Args[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 177 rs := vector.MustFunctionResult[types.Varlena](result) 178 vs := vector.GenerateFunctionStrParameter(ivecs[0]) 179 starts := vector.GenerateFunctionFixedTypeParameter[int64](ivecs[1]) 180 lens := vector.GenerateFunctionFixedTypeParameter[int64](ivecs[2]) 181 182 for i := uint64(0); i < uint64(length); i++ { 183 in, null1 := vs.GetStrValue(i) 184 s, null2 := starts.GetValue(i) 185 l, null3 := lens.GetValue(i) 186 187 if null1 || null2 || null3 { 188 if err = rs.AppendBytes(nil, true); err != nil { 189 return err 190 } 191 } else { 192 var r []T 193 if s > 0 { 194 r = moarray.SubArrayFromLeftWithLength[T](types.BytesToArray[T](in), s-1, l) 195 } else if s < 0 { 196 r = moarray.SubArrayFromRightWithLength[T](types.BytesToArray[T](in), -s, l) 197 } else { 198 r = []T{} 199 } 200 if err = rs.AppendBytes(types.ArrayToBytes[T](r), false); err != nil { 201 return err 202 } 203 } 204 } 205 return nil 206 } 207 208 func StringSingle(val []byte) uint8 { 209 if len(val) == 0 { 210 return 0 211 } 212 return val[0] 213 } 214 215 func AsciiString(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 216 return opUnaryBytesToFixed[uint8](ivecs, result, proc, length, func(v []byte) uint8 { 217 return StringSingle(v) 218 }) 219 } 220 221 var ( 222 intStartMap = map[types.T]int{ 223 types.T_int8: 3, 224 types.T_uint8: 3, 225 types.T_int16: 2, 226 types.T_uint16: 2, 227 types.T_int32: 1, 228 types.T_uint32: 1, 229 types.T_int64: 0, 230 types.T_uint64: 0, 231 types.T_bit: 0, 232 } 233 ints = []int64{1e16, 1e8, 1e4, 1e2, 1e1} 234 uints = []uint64{1e16, 1e8, 1e4, 1e2, 1e1} 235 ) 236 237 func IntSingle[T types.Ints](val T, start int) uint8 { 238 if val < 0 { 239 return '-' 240 } 241 i64Val := int64(val) 242 for _, v := range ints[start:] { 243 if i64Val >= v { 244 i64Val /= v 245 } 246 } 247 return uint8(i64Val) + '0' 248 } 249 250 func AsciiInt[T types.Ints](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 251 start := intStartMap[ivecs[0].GetType().Oid] 252 253 return opUnaryFixedToFixed[T, uint8](ivecs, result, proc, length, func(v T) uint8 { 254 return IntSingle[T](v, start) 255 }) 256 } 257 258 func UintSingle[T types.UInts](val T, start int) uint8 { 259 u64Val := uint64(val) 260 for _, v := range uints[start:] { 261 if u64Val >= v { 262 u64Val /= v 263 } 264 } 265 return uint8(u64Val) + '0' 266 } 267 268 func AsciiUint[T types.UInts](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 269 start := intStartMap[ivecs[0].GetType().Oid] 270 271 return opUnaryFixedToFixed[T, uint8](ivecs, result, proc, length, func(v T) uint8 { 272 return UintSingle[T](v, start) 273 }) 274 } 275 276 func uintToBinary(x uint64) string { 277 if x == 0 { 278 return "0" 279 } 280 b, i := [64]byte{}, 63 281 for x > 0 { 282 if x&1 == 1 { 283 b[i] = '1' 284 } else { 285 b[i] = '0' 286 } 287 x >>= 1 288 i -= 1 289 } 290 291 return string(b[i+1:]) 292 } 293 294 func binInteger[T constraints.Unsigned | constraints.Signed](v T, proc *process.Process) (string, error) { 295 return uintToBinary(uint64(v)), nil 296 } 297 298 func binFloat[T constraints.Float](v T, proc *process.Process) (string, error) { 299 if err := overflowForNumericToNumeric[T, int64](proc.Ctx, []T{v}, nil); err != nil { 300 return "", err 301 } 302 return uintToBinary(uint64(int64(v))), nil 303 } 304 305 func Bin[T constraints.Unsigned | constraints.Signed](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 306 return opUnaryFixedToStrWithErrorCheck[T](ivecs, result, proc, length, func(v T) (string, error) { 307 val, err := binInteger[T](v, proc) 308 if err != nil { 309 return "", moerr.NewInvalidInput(proc.Ctx, "The input value is out of range") 310 } 311 return val, err 312 }) 313 } 314 315 func BinFloat[T constraints.Float](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 316 return opUnaryFixedToStrWithErrorCheck[T](ivecs, result, proc, length, func(v T) (string, error) { 317 val, err := binFloat[T](v, proc) 318 if err != nil { 319 return "", moerr.NewInvalidInput(proc.Ctx, "The input value is out of range") 320 } 321 return val, err 322 }) 323 } 324 325 func BitLengthFunc(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 326 return opUnaryStrToFixed[int64](ivecs, result, proc, length, func(v string) int64 { 327 return int64(len(v) * 8) 328 }) 329 } 330 331 func CurrentDate(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 332 var err error 333 334 loc := proc.SessionInfo.TimeZone 335 if loc == nil { 336 logutil.Warn("missing timezone in session info") 337 loc = time.Local 338 } 339 ts := types.UnixNanoToTimestamp(proc.UnixTime) 340 dateTimes := make([]types.Datetime, 1) 341 dateTimes, err = types.TimestampToDatetime(loc, []types.Timestamp{ts}, dateTimes) 342 if err != nil { 343 return err 344 } 345 r := dateTimes[0].ToDate() 346 347 return opNoneParamToFixed[types.Date](result, proc, length, func() types.Date { 348 return r 349 }) 350 } 351 352 func DateToDate(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 353 return opUnaryFixedToFixed[types.Date, types.Date](ivecs, result, proc, length, func(v types.Date) types.Date { 354 return v 355 }) 356 } 357 358 func DatetimeToDate(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 359 return opUnaryFixedToFixed[types.Datetime, types.Date](ivecs, result, proc, length, func(v types.Datetime) types.Date { 360 return v.ToDate() 361 }) 362 } 363 364 func TimeToDate(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 365 return opUnaryFixedToFixed[types.Time, types.Date](ivecs, result, proc, length, func(v types.Time) types.Date { 366 return v.ToDate() 367 }) 368 } 369 370 // DateStringToDate can still speed up if vec is const. but we will do the constant fold. so it does not matter. 371 func DateStringToDate(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 372 return opUnaryBytesToFixedWithErrorCheck[types.Date](ivecs, result, proc, length, func(v []byte) (types.Date, error) { 373 d, e := types.ParseDatetime(functionUtil.QuickBytesToStr(v), 6) 374 if e != nil { 375 return 0, moerr.NewOutOfRangeNoCtx("date", "'%s'", v) 376 } 377 return d.ToDate(), nil 378 }) 379 } 380 381 func DateToDay(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 382 return opUnaryFixedToFixed[types.Date, uint8](ivecs, result, proc, length, func(v types.Date) uint8 { 383 return v.Day() 384 }) 385 } 386 387 func DatetimeToDay(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 388 return opUnaryFixedToFixed[types.Datetime, uint8](ivecs, result, proc, length, func(v types.Datetime) uint8 { 389 return v.Day() 390 }) 391 } 392 393 func DayOfYear(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 394 return opUnaryFixedToFixed[types.Date, uint16](ivecs, result, proc, length, func(v types.Date) uint16 { 395 return v.DayOfYear() 396 }) 397 } 398 399 func Empty(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 400 return opUnaryBytesToFixed[bool](ivecs, result, proc, length, func(v []byte) bool { 401 return len(v) == 0 402 }) 403 } 404 405 func JsonQuote(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 406 single := func(str string) ([]byte, error) { 407 bj, err := types.ParseStringToByteJson(strconv.Quote(str)) 408 if err != nil { 409 return nil, err 410 } 411 return bj.Marshal() 412 } 413 414 return opUnaryStrToBytesWithErrorCheck(ivecs, result, proc, length, single) 415 } 416 417 func JsonUnquote(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 418 jsonSingle := func(v []byte) (string, error) { 419 bj := types.DecodeJson(v) 420 return bj.Unquote() 421 } 422 423 stringSingle := func(v []byte) (string, error) { 424 bj, err := types.ParseSliceToByteJson(v) 425 if err != nil { 426 return "", err 427 } 428 return bj.Unquote() 429 } 430 431 fSingle := jsonSingle 432 if ivecs[0].GetType().Oid.IsMySQLString() { 433 fSingle = stringSingle 434 } 435 436 return opUnaryBytesToStrWithErrorCheck(ivecs, result, proc, length, fSingle) 437 } 438 439 func ReadFromFile(Filepath string, fs fileservice.FileService) (io.ReadCloser, error) { 440 fs, readPath, err := fileservice.GetForETL(context.TODO(), fs, Filepath) 441 if fs == nil || err != nil { 442 return nil, err 443 } 444 var r io.ReadCloser 445 ctx := context.TODO() 446 vec := fileservice.IOVector{ 447 FilePath: readPath, 448 Entries: []fileservice.IOEntry{ 449 0: { 450 Offset: 0, 451 Size: -1, 452 ReadCloserForRead: &r, 453 }, 454 }, 455 } 456 err = fs.Read(ctx, &vec) 457 if err != nil { 458 return nil, err 459 } 460 return r, nil 461 } 462 463 // Too confused. 464 func LoadFile(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 465 rs := vector.MustFunctionResult[types.Varlena](result) 466 ivec := vector.GenerateFunctionStrParameter(ivecs[0]) 467 Filepath, null := ivec.GetStrValue(0) 468 if null { 469 if err := rs.AppendBytes(nil, true); err != nil { 470 return err 471 } 472 return nil 473 } 474 fs := proc.FileService 475 r, err := ReadFromFile(string(Filepath), fs) 476 if err != nil { 477 return err 478 } 479 defer r.Close() 480 ctx, err := io.ReadAll(r) 481 if err != nil { 482 return err 483 } 484 if len(ctx) > 65536 /*blob size*/ { 485 return moerr.NewInternalError(proc.Ctx, "Data too long for blob") 486 } 487 if len(ctx) == 0 { 488 if err = rs.AppendBytes(nil, true); err != nil { 489 return err 490 } 491 return nil 492 } 493 494 if err = rs.AppendBytes(ctx, false); err != nil { 495 return err 496 } 497 return nil 498 } 499 500 func MoMemUsage(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 501 if len(ivecs) != 1 { 502 return moerr.NewInvalidInput(proc.Ctx, "no mpool name") 503 } 504 if !ivecs[0].IsConst() { 505 return moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input") 506 } 507 508 return opUnaryStrToBytesWithErrorCheck(ivecs, result, proc, length, func(v string) ([]byte, error) { 509 memUsage := mpool.ReportMemUsage(v) 510 return functionUtil.QuickStrToBytes(memUsage), nil 511 }) 512 } 513 514 func moMemUsageCmd(cmd string, ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 515 if len(ivecs) != 1 { 516 return moerr.NewInvalidInput(proc.Ctx, "no mpool name") 517 } 518 if !ivecs[0].IsConst() { 519 return moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input") 520 } 521 522 return opUnaryStrToBytesWithErrorCheck(ivecs, result, proc, length, func(v string) ([]byte, error) { 523 ok := mpool.MPoolControl(v, cmd) 524 return functionUtil.QuickStrToBytes(ok), nil 525 }) 526 } 527 528 func MoEnableMemUsageDetail(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 529 return moMemUsageCmd("enable_detail", ivecs, result, proc, length) 530 } 531 532 func MoDisableMemUsageDetail(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 533 return moMemUsageCmd("disable_detail", ivecs, result, proc, length) 534 } 535 536 func MoMemory(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 537 if len(ivecs) != 1 { 538 return moerr.NewInvalidInput(proc.Ctx, "no memory command name") 539 } 540 if !ivecs[0].IsConst() { 541 return moerr.NewInvalidInput(proc.Ctx, "mo memory can only take scalar input") 542 } 543 return opUnaryStrToFixedWithErrorCheck(ivecs, result, proc, length, func(v string) (int64, error) { 544 switch v { 545 case "go": 546 return int64(system.MemoryGolang()), nil 547 case "total": 548 return int64(system.MemoryTotal()), nil 549 case "used": 550 return int64(system.MemoryUsed()), nil 551 case "available": 552 return int64(system.MemoryAvailable()), nil 553 default: 554 return -1, moerr.NewInvalidInput(proc.Ctx, "unsupported memory command: %s", v) 555 } 556 }) 557 } 558 559 func MoCPU(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 560 if len(ivecs) != 1 { 561 return moerr.NewInvalidInput(proc.Ctx, "no cpu command name") 562 } 563 if !ivecs[0].IsConst() { 564 return moerr.NewInvalidInput(proc.Ctx, "mo cpu can only take scalar input") 565 } 566 return opUnaryStrToFixedWithErrorCheck(ivecs, result, proc, length, func(v string) (int64, error) { 567 switch v { 568 case "goroutine": 569 return int64(system.GoRoutines()), nil 570 case "total": 571 return int64(system.NumCPU()), nil 572 case "available": 573 return int64(system.CPUAvailable()), nil 574 default: 575 return -1, moerr.NewInvalidInput(proc.Ctx, "no cpu command name") 576 } 577 }) 578 } 579 580 const ( 581 DefaultStackSize = 10 << 20 // 10MB 582 ) 583 584 func MoCPUDump(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 585 if len(ivecs) != 1 { 586 return moerr.NewInvalidInput(proc.Ctx, "no cpu dump command name") 587 } 588 if !ivecs[0].IsConst() { 589 return moerr.NewInvalidInput(proc.Ctx, "mo cpu dump can only take scalar input") 590 } 591 return opUnaryStrToBytesWithErrorCheck(ivecs, result, proc, length, func(v string) ([]byte, error) { 592 switch v { 593 case "goroutine": 594 buf := make([]byte, DefaultStackSize) 595 n := runtime.Stack(buf, true) 596 return buf[:n], nil 597 default: 598 return nil, moerr.NewInvalidInput(proc.Ctx, "no cpu dump command name") 599 } 600 }) 601 } 602 603 const ( 604 MaxAllowedValue = 8000 605 ) 606 607 func FillSpaceNumber[T types.BuiltinNumber](v T) (string, error) { 608 var ilen int 609 if v < 0 { 610 ilen = 0 611 } else { 612 ilen = int(v) 613 if ilen > MaxAllowedValue || ilen < 0 { 614 return "", moerr.NewInvalidInputNoCtx("the space count is greater than max allowed value %d", MaxAllowedValue) 615 } 616 } 617 return strings.Repeat(" ", ilen), nil 618 } 619 620 func SpaceNumber[T types.BuiltinNumber](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 621 return opUnaryFixedToStrWithErrorCheck[T](ivecs, result, proc, length, FillSpaceNumber[T]) 622 } 623 624 func TimeToTime(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 625 return opUnaryFixedToFixed[types.Time, types.Time](ivecs, result, proc, length, func(v types.Time) types.Time { 626 return v 627 }) 628 } 629 630 func DateToTime(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 631 return opUnaryFixedToFixed[types.Date, types.Time](ivecs, result, proc, length, func(v types.Date) types.Time { 632 return v.ToTime() 633 }) 634 } 635 636 func DatetimeToTime(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 637 scale := ivecs[0].GetType().Scale 638 return opUnaryFixedToFixed[types.Datetime, types.Time](ivecs, result, proc, length, func(v types.Datetime) types.Time { 639 return v.ToTime(scale) 640 }) 641 } 642 643 func Int64ToTime(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 644 return opUnaryFixedToFixedWithErrorCheck[int64, types.Time](ivecs, result, proc, length, func(v int64) (types.Time, error) { 645 t, e := types.ParseInt64ToTime(v, 0) 646 if e != nil { 647 return 0, moerr.NewOutOfRangeNoCtx("time", "'%d'", v) 648 } 649 return t, nil 650 }) 651 } 652 653 func DateStringToTime(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 654 return opUnaryBytesToFixedWithErrorCheck[types.Time](ivecs, result, proc, length, func(v []byte) (types.Time, error) { 655 t, e := types.ParseTime(string(v), 6) 656 if e != nil { 657 return 0, moerr.NewOutOfRangeNoCtx("time", "'%s'", string(v)) 658 } 659 return t, nil 660 }) 661 } 662 663 func Decimal128ToTime(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 664 scale := ivecs[0].GetType().Scale 665 return opUnaryFixedToFixedWithErrorCheck[types.Decimal128, types.Time](ivecs, result, proc, length, func(v types.Decimal128) (types.Time, error) { 666 t, e := types.ParseDecimal128ToTime(v, scale, 6) 667 if e != nil { 668 return 0, moerr.NewOutOfRangeNoCtx("time", "'%s'", v.Format(0)) 669 } 670 return t, nil 671 }) 672 } 673 674 func DateToTimestamp(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 675 return opUnaryFixedToFixed[types.Date, types.Timestamp](ivecs, result, proc, length, func(v types.Date) types.Timestamp { 676 return v.ToTimestamp(proc.SessionInfo.TimeZone) 677 }) 678 } 679 680 func DatetimeToTimestamp(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 681 return opUnaryFixedToFixed[types.Datetime, types.Timestamp](ivecs, result, proc, length, func(v types.Datetime) types.Timestamp { 682 return v.ToTimestamp(proc.SessionInfo.TimeZone) 683 }) 684 } 685 686 func TimestampToTimestamp(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 687 return opUnaryFixedToFixed[types.Timestamp, types.Timestamp](ivecs, result, proc, length, func(v types.Timestamp) types.Timestamp { 688 return v 689 }) 690 } 691 692 func DateStringToTimestamp(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 693 return opUnaryStrToFixedWithErrorCheck[types.Timestamp](ivecs, result, proc, length, func(v string) (types.Timestamp, error) { 694 val, err := types.ParseTimestamp(proc.SessionInfo.TimeZone, v, 6) 695 if err != nil { 696 return 0, err 697 } 698 return val, nil 699 }) 700 } 701 702 func Values(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 703 fromVec := parameters[0] 704 toVec := result.GetResultVector() 705 toVec.Reset(*toVec.GetType()) 706 707 sels := make([]int32, fromVec.Length()) 708 for j := 0; j < len(sels); j++ { 709 sels[j] = int32(j) 710 } 711 712 err := toVec.Union(fromVec, sels, proc.GetMPool()) 713 return err 714 } 715 716 func TimestampToHour(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 717 return opUnaryFixedToFixed[types.Timestamp, uint8](ivecs, result, proc, length, func(v types.Timestamp) uint8 { 718 return uint8(v.ToDatetime(proc.SessionInfo.TimeZone).Hour()) 719 }) 720 } 721 722 func DatetimeToHour(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 723 return opUnaryFixedToFixed[types.Datetime, uint8](ivecs, result, proc, length, func(v types.Datetime) uint8 { 724 return uint8(v.Hour()) 725 }) 726 } 727 728 func TimestampToMinute(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 729 return opUnaryFixedToFixed[types.Timestamp, uint8](ivecs, result, proc, length, func(v types.Timestamp) uint8 { 730 return uint8(v.ToDatetime(proc.SessionInfo.TimeZone).Minute()) 731 }) 732 } 733 734 func DatetimeToMinute(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 735 return opUnaryFixedToFixed[types.Datetime, uint8](ivecs, result, proc, length, func(v types.Datetime) uint8 { 736 return uint8(v.Minute()) 737 }) 738 } 739 740 func TimestampToSecond(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 741 return opUnaryFixedToFixed[types.Timestamp, uint8](ivecs, result, proc, length, func(v types.Timestamp) uint8 { 742 return uint8(v.ToDatetime(proc.SessionInfo.TimeZone).Sec()) 743 }) 744 } 745 746 func DatetimeToSecond(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 747 return opUnaryFixedToFixed[types.Datetime, uint8](ivecs, result, proc, length, func(v types.Datetime) uint8 { 748 return uint8(v.Sec()) 749 }) 750 } 751 752 func doBinary(orig []byte) []byte { 753 if len(orig) > types.MaxBinaryLen { 754 return orig[:types.MaxBinaryLen] 755 } else { 756 return orig 757 } 758 } 759 760 func Binary(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 761 return opUnaryBytesToBytes(ivecs, result, proc, length, doBinary) 762 } 763 764 func Charset(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 765 r := proc.SessionInfo.GetCharset() 766 return opNoneParamToBytes(result, proc, length, func() []byte { 767 return functionUtil.QuickStrToBytes(r) 768 }) 769 } 770 771 func Collation(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 772 r := proc.SessionInfo.GetCollation() 773 return opNoneParamToBytes(result, proc, length, func() []byte { 774 return functionUtil.QuickStrToBytes(r) 775 }) 776 } 777 778 func ConnectionID(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 779 r := proc.SessionInfo.ConnectionID 780 return opNoneParamToFixed[uint64](result, proc, length, func() uint64 { 781 return r 782 }) 783 } 784 785 func HexString(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 786 return opUnaryBytesToStr(ivecs, result, proc, length, hexEncodeString) 787 } 788 789 func HexInt64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 790 return opUnaryFixedToStr[int64](ivecs, result, proc, length, hexEncodeInt64) 791 } 792 793 func HexUint64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 794 return opUnaryFixedToStr[uint64](ivecs, result, proc, length, hexEncodeUint64) 795 } 796 797 func HexArray(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 798 return opUnaryBytesToBytesWithErrorCheck(ivecs, result, proc, length, func(data []byte) ([]byte, error) { 799 buf := make([]byte, hex.EncodedLen(len(functionUtil.QuickBytesToStr(data)))) 800 hex.Encode(buf, data) 801 return buf, nil 802 }) 803 } 804 805 func hexEncodeString(xs []byte) string { 806 return hex.EncodeToString(xs) 807 } 808 809 func hexEncodeInt64(xs int64) string { 810 return fmt.Sprintf("%X", uint64(xs)) 811 } 812 813 func hexEncodeUint64(xs uint64) string { 814 return fmt.Sprintf("%X", xs) 815 } 816 817 func unhexToBytes(data []byte, null bool, rs *vector.FunctionResult[types.Varlena]) error { 818 if null { 819 return rs.AppendMustNullForBytesResult() 820 } 821 822 buf := make([]byte, hex.DecodedLen(len(data))) 823 _, err := hex.Decode(buf, data) 824 if err != nil { 825 return rs.AppendMustNullForBytesResult() 826 } 827 return rs.AppendMustBytesValue(buf) 828 } 829 830 func Unhex(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 831 source := vector.GenerateFunctionStrParameter(parameters[0]) 832 rs := vector.MustFunctionResult[types.Varlena](result) 833 834 rowCount := uint64(length) 835 for i := uint64(0); i < rowCount; i++ { 836 data, null := source.GetStrValue(i) 837 if err := unhexToBytes(data, null, rs); err != nil { 838 return err 839 } 840 } 841 842 return nil 843 } 844 845 func Md5(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 846 return opUnaryBytesToBytes(parameters, result, proc, length, func(data []byte) []byte { 847 sum := md5.Sum(data) 848 return []byte(hex.EncodeToString(sum[:])) 849 }) 850 851 } 852 853 func ToBase64(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 854 return opUnaryBytesToBytesWithErrorCheck(ivecs, result, proc, length, func(data []byte) ([]byte, error) { 855 buf := make([]byte, base64.StdEncoding.EncodedLen(len(functionUtil.QuickBytesToStr(data)))) 856 base64.StdEncoding.Encode(buf, data) 857 return buf, nil 858 }) 859 } 860 861 func FromBase64(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 862 source := vector.GenerateFunctionStrParameter(parameters[0]) 863 rs := vector.MustFunctionResult[types.Varlena](result) 864 865 rowCount := uint64(length) 866 for i := uint64(0); i < rowCount; i++ { 867 data, null := source.GetStrValue(i) 868 if null { 869 return rs.AppendMustNullForBytesResult() 870 } 871 872 buf := make([]byte, base64.StdEncoding.DecodedLen(len(functionUtil.QuickBytesToStr(data)))) 873 _, err := base64.StdEncoding.Decode(buf, data) 874 if err != nil { 875 return rs.AppendMustNullForBytesResult() 876 } 877 _ = rs.AppendMustBytesValue(buf) 878 } 879 880 return nil 881 } 882 883 func Length(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 884 return opUnaryStrToFixed[int64](ivecs, result, proc, length, strLength) 885 } 886 887 func strLength(xs string) int64 { 888 return int64(len(xs)) 889 } 890 891 func LengthUTF8(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 892 return opUnaryBytesToFixed[uint64](ivecs, result, proc, length, strLengthUTF8) 893 } 894 895 func strLengthUTF8(xs []byte) uint64 { 896 return lengthutf8.CountUTF8CodePoints(xs) 897 } 898 899 func Ltrim(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 900 return opUnaryStrToStr(ivecs, result, proc, length, ltrim) 901 } 902 903 func ltrim(xs string) string { 904 return strings.TrimLeft(xs, " ") 905 } 906 907 func Rtrim(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 908 return opUnaryStrToStr(ivecs, result, proc, length, rtrim) 909 } 910 911 func rtrim(xs string) string { 912 return strings.TrimRight(xs, " ") 913 } 914 915 func Reverse(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 916 return opUnaryStrToStr(ivecs, result, proc, length, reverse) 917 } 918 919 func reverse(str string) string { 920 runes := []rune(str) 921 for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { 922 runes[i], runes[j] = runes[j], runes[i] 923 } 924 return string(runes) 925 } 926 927 func Oct[T constraints.Unsigned | constraints.Signed](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 928 return opUnaryFixedToFixedWithErrorCheck[T, types.Decimal128](ivecs, result, proc, length, oct[T]) 929 } 930 931 func oct[T constraints.Unsigned | constraints.Signed](val T) (types.Decimal128, error) { 932 _val := uint64(val) 933 return types.ParseDecimal128(fmt.Sprintf("%o", _val), 38, 0) 934 } 935 936 func OctFloat[T constraints.Float](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 937 return opUnaryFixedToFixedWithErrorCheck[T, types.Decimal128](ivecs, result, proc, length, octFloat[T]) 938 } 939 940 func octFloat[T constraints.Float](xs T) (types.Decimal128, error) { 941 var res types.Decimal128 942 943 if xs < 0 { 944 val, err := strconv.ParseInt(fmt.Sprintf("%1.0f", xs), 10, 64) 945 if err != nil { 946 return res, moerr.NewInternalErrorNoCtx("the input value is out of integer range") 947 } 948 res, err = oct(uint64(val)) 949 if err != nil { 950 return res, err 951 } 952 } else { 953 val, err := strconv.ParseUint(fmt.Sprintf("%1.0f", xs), 10, 64) 954 if err != nil { 955 return res, moerr.NewInternalErrorNoCtx("the input value is out of integer range") 956 } 957 res, err = oct(val) 958 if err != nil { 959 return res, err 960 } 961 } 962 return res, nil 963 } 964 965 func DateToMonth(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 966 return opUnaryFixedToFixed[types.Date, uint8](ivecs, result, proc, length, func(v types.Date) uint8 { 967 return v.Month() 968 }) 969 } 970 971 func DatetimeToMonth(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 972 return opUnaryFixedToFixed[types.Datetime, uint8](ivecs, result, proc, length, func(v types.Datetime) uint8 { 973 return v.Month() 974 }) 975 } 976 977 // TODO: I will support template soon. 978 func DateStringToMonth(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 979 //return opUnaryStrToFixedWithErrorCheck[uint8](ivecs, result, proc, length, func(v string) (uint8, error) { 980 // d, e := types.ParseDateCast(v) 981 // if e != nil { 982 // return 0, e 983 // } 984 // return d.Month(), nil 985 //}) 986 987 ivec := vector.GenerateFunctionStrParameter(ivecs[0]) 988 rs := vector.MustFunctionResult[uint8](result) 989 for i := uint64(0); i < uint64(length); i++ { 990 v, null := ivec.GetStrValue(i) 991 if null { 992 if err := rs.Append(0, true); err != nil { 993 return err 994 } 995 } else { 996 d, e := types.ParseDateCast(functionUtil.QuickBytesToStr(v)) 997 if e != nil { 998 if err := rs.Append(0, true); err != nil { 999 return err 1000 } 1001 } else { 1002 if err := rs.Append(d.Month(), false); err != nil { 1003 return err 1004 } 1005 } 1006 } 1007 } 1008 return nil 1009 } 1010 1011 func DateToYear(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1012 return opUnaryFixedToFixed[types.Date, int64](ivecs, result, proc, length, func(v types.Date) int64 { 1013 return int64(v.Year()) 1014 }) 1015 } 1016 1017 func DatetimeToYear(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1018 return opUnaryFixedToFixed[types.Datetime, int64](ivecs, result, proc, length, func(v types.Datetime) int64 { 1019 return int64(v.Year()) 1020 }) 1021 } 1022 1023 func DateStringToYear(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1024 return opUnaryStrToFixedWithErrorCheck[int64](ivecs, result, proc, length, func(v string) (int64, error) { 1025 d, e := types.ParseDateCast(v) 1026 if e != nil { 1027 return 0, e 1028 } 1029 return int64(d.Year()), nil 1030 }) 1031 } 1032 1033 func DateToWeek(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1034 return opUnaryFixedToFixed[types.Date, uint8](ivecs, result, proc, length, func(v types.Date) uint8 { 1035 return v.WeekOfYear2() 1036 }) 1037 } 1038 1039 func DatetimeToWeek(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1040 return opUnaryFixedToFixed[types.Datetime, uint8](ivecs, result, proc, length, func(v types.Datetime) uint8 { 1041 return v.ToDate().WeekOfYear2() 1042 }) 1043 } 1044 1045 func DateToWeekday(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1046 return opUnaryFixedToFixed[types.Date, int64](ivecs, result, proc, length, func(v types.Date) int64 { 1047 return int64(v.DayOfWeek2()) 1048 }) 1049 } 1050 1051 func DatetimeToWeekday(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1052 return opUnaryFixedToFixed[types.Datetime, int64](ivecs, result, proc, length, func(v types.Datetime) int64 { 1053 return int64(v.ToDate().DayOfWeek2()) 1054 }) 1055 } 1056 1057 func FoundRows(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1058 return opNoneParamToFixed[uint64](result, proc, length, func() uint64 { 1059 return 0 1060 }) 1061 } 1062 1063 func ICULIBVersion(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1064 return opNoneParamToBytes(result, proc, length, func() []byte { 1065 return functionUtil.QuickStrToBytes("") 1066 }) 1067 } 1068 1069 func LastInsertID(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1070 return opNoneParamToFixed[uint64](result, proc, length, func() uint64 { 1071 return proc.SessionInfo.LastInsertID 1072 }) 1073 } 1074 1075 // TODO: may support soon. 1076 func LastQueryIDWithoutParam(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 1077 rs := vector.MustFunctionResult[types.Varlena](result) 1078 1079 for i := uint64(0); i < uint64(length); i++ { 1080 cnt := int64(len(proc.SessionInfo.QueryId)) 1081 if cnt == 0 { 1082 if err = rs.AppendBytes(nil, true); err != nil { 1083 return err 1084 } 1085 continue 1086 } 1087 var idx int 1088 idx, err = makeQueryIdIdx(-1, cnt, proc) 1089 if err != nil { 1090 return err 1091 } 1092 1093 if err = rs.AppendBytes(functionUtil.QuickStrToBytes(proc.SessionInfo.QueryId[idx]), false); err != nil { 1094 return err 1095 } 1096 } 1097 return nil 1098 } 1099 1100 func makeQueryIdIdx(loc, cnt int64, proc *process.Process) (int, error) { 1101 // https://docs.snowflake.com/en/sql-reference/functions/last_query_id.html 1102 var idx int 1103 if loc < 0 { 1104 if loc < -cnt { 1105 return 0, moerr.NewInvalidInput(proc.Ctx, "index out of range: %d", loc) 1106 } 1107 idx = int(loc + cnt) 1108 } else { 1109 if loc > cnt { 1110 return 0, moerr.NewInvalidInput(proc.Ctx, "index out of range: %d", loc) 1111 } 1112 idx = int(loc) 1113 } 1114 return idx, nil 1115 } 1116 1117 func LastQueryID(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 1118 rs := vector.MustFunctionResult[types.Varlena](result) 1119 ivec := vector.GenerateFunctionFixedTypeParameter[int64](ivecs[0]) 1120 1121 //TODO: Not at all sure about this. Should we do null check 1122 // Validate: https://github.com/m-schen/matrixone/blob/9e8ef37e2a6f34873ceeb3c101ec9bb14a82a8a7/pkg/sql/plan/function/builtin/unary/infomation_function.go#L245 1123 loc, _ := ivec.GetValue(0) 1124 for i := uint64(0); i < uint64(length); i++ { 1125 cnt := int64(len(proc.SessionInfo.QueryId)) 1126 if cnt == 0 { 1127 if err = rs.AppendBytes(nil, true); err != nil { 1128 return err 1129 } 1130 } 1131 var idx int 1132 idx, err = makeQueryIdIdx(loc, cnt, proc) 1133 if err != nil { 1134 return err 1135 } 1136 1137 if err = rs.AppendBytes(functionUtil.QuickStrToBytes(proc.SessionInfo.QueryId[idx]), false); err != nil { 1138 return err 1139 } 1140 } 1141 return nil 1142 } 1143 1144 func RolesGraphml(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1145 return opNoneParamToBytes(result, proc, length, func() []byte { 1146 return functionUtil.QuickStrToBytes("") 1147 }) 1148 } 1149 1150 func RowCount(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1151 return opNoneParamToFixed[uint64](result, proc, length, func() uint64 { 1152 return 0 1153 }) 1154 } 1155 1156 func User(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1157 return opNoneParamToBytes(result, proc, length, func() []byte { 1158 return functionUtil.QuickStrToBytes(proc.SessionInfo.GetUserHost()) 1159 }) 1160 } 1161 1162 func Pi(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1163 r := math.Pi 1164 1165 return opNoneParamToFixed[float64](result, proc, length, func() float64 { 1166 return r 1167 }) 1168 } 1169 1170 func DisableFaultInjection(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1171 fault.Disable() 1172 1173 return opNoneParamToFixed[bool](result, proc, length, func() bool { 1174 return true 1175 }) 1176 } 1177 1178 func EnableFaultInjection(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1179 fault.Enable() 1180 1181 return opNoneParamToFixed[bool](result, proc, length, func() bool { 1182 return true 1183 }) 1184 } 1185 1186 func RemoveFaultPoint(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 1187 if !ivecs[0].IsConst() || ivecs[0].IsConstNull() { 1188 return moerr.NewInvalidArg(proc.Ctx, "RemoveFaultPoint", "not scalar") 1189 } 1190 1191 return opUnaryStrToFixedWithErrorCheck[bool](ivecs, result, proc, length, func(v string) (bool, error) { 1192 err = fault.RemoveFaultPoint(proc.Ctx, v) 1193 return true, err 1194 }) 1195 } 1196 1197 func TriggerFaultPoint(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) (err error) { 1198 if !ivecs[0].IsConst() || ivecs[0].IsConstNull() { 1199 return moerr.NewInvalidArg(proc.Ctx, "TriggerFaultPoint", "not scalar") 1200 } 1201 1202 ivec := vector.GenerateFunctionStrParameter(ivecs[0]) 1203 rs := vector.MustFunctionResult[int64](result) 1204 1205 for i := uint64(0); i < uint64(length); i++ { 1206 v, null := ivec.GetStrValue(i) 1207 if null { 1208 if err = rs.Append(0, true); err != nil { 1209 return err 1210 } 1211 } else { 1212 iv, _, ok := fault.TriggerFault(functionUtil.QuickBytesToStr(v)) 1213 if !ok { 1214 if err = rs.Append(0, true); err != nil { 1215 return err 1216 } 1217 } else { 1218 if err = rs.Append(iv, false); err != nil { 1219 return err 1220 } 1221 } 1222 } 1223 } 1224 return nil 1225 } 1226 1227 func UTCTimestamp(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1228 return opNoneParamToFixed[types.Datetime](result, proc, length, func() types.Datetime { 1229 return types.UTC() 1230 }) 1231 } 1232 1233 func sleepSeconds(proc *process.Process, sec float64) (uint8, error) { 1234 if sec < 0 { 1235 return 0, moerr.NewInvalidArg(proc.Ctx, "sleep", "input contains negative") 1236 } 1237 1238 sleepNano := time.Nanosecond * time.Duration(sec*1e9) 1239 select { 1240 case <-time.After(sleepNano): 1241 return 0, nil 1242 case <-proc.Ctx.Done(): //query aborted 1243 return 1, nil 1244 } 1245 } 1246 1247 func Sleep[T uint64 | float64](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1248 rs := vector.MustFunctionResult[uint8](result) 1249 ivec := vector.GenerateFunctionFixedTypeParameter[T](ivecs[0]) 1250 for i := uint64(0); i < uint64(length); i++ { 1251 v, null := ivec.GetValue(i) 1252 if null { 1253 return moerr.NewInvalidArg(proc.Ctx, "sleep", "input contains null") 1254 } else { 1255 res, err := sleepSeconds(proc, float64(v)) 1256 if err == nil { 1257 err = rs.Append(res, false) 1258 } 1259 if err != nil { 1260 return err 1261 } 1262 } 1263 } 1264 return nil 1265 } 1266 1267 func Version(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1268 versionStr := proc.SessionInfo.GetVersion() 1269 1270 return opNoneParamToBytes(result, proc, length, func() []byte { 1271 return functionUtil.QuickStrToBytes(versionStr) 1272 }) 1273 } 1274 1275 func GitVersion(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1276 s := "unknown" 1277 if version.CommitID != "" { 1278 s = version.CommitID 1279 } 1280 1281 return opNoneParamToBytes(result, proc, length, func() []byte { 1282 return functionUtil.QuickStrToBytes(s) 1283 }) 1284 } 1285 1286 func BuildVersion(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1287 t, err := strconv.ParseInt(version.BuildTime, 10, 64) 1288 if err != nil { 1289 return err 1290 } 1291 buildT := types.UnixToTimestamp(t) 1292 1293 return opNoneParamToFixed[types.Timestamp](result, proc, length, func() types.Timestamp { 1294 return buildT 1295 }) 1296 } 1297 1298 func bitCastBinaryToFixed[T types.FixedSizeTExceptStrType]( 1299 ctx context.Context, 1300 from vector.FunctionParameterWrapper[types.Varlena], 1301 to *vector.FunctionResult[T], 1302 byteLen int, 1303 length int, 1304 ) error { 1305 var i uint64 1306 var l = uint64(length) 1307 var result, emptyT T 1308 resultBytes := unsafe.Slice((*byte)(unsafe.Pointer(&result)), byteLen) 1309 1310 for i = 0; i < l; i++ { 1311 v, null := from.GetStrValue(i) 1312 if null { 1313 if err := to.Append(result, true); err != nil { 1314 return err 1315 } 1316 } else { 1317 if len(v) > byteLen { 1318 return moerr.NewOutOfRange(ctx, fmt.Sprintf("%d-byte fixed-length type", byteLen), "binary value '0x%s'", hex.EncodeToString(v)) 1319 } 1320 1321 if len(v) < byteLen { 1322 result = emptyT 1323 } 1324 copy(resultBytes, v) 1325 if err := to.Append(result, false); err != nil { 1326 return err 1327 } 1328 } 1329 } 1330 1331 return nil 1332 } 1333 1334 func BitCast( 1335 parameters []*vector.Vector, 1336 result vector.FunctionResultWrapper, 1337 proc *process.Process, 1338 length int, 1339 ) error { 1340 source := vector.GenerateFunctionStrParameter(parameters[0]) 1341 toType := parameters[1].GetType() 1342 ctx := proc.Ctx 1343 1344 switch toType.Oid { 1345 case types.T_bit: 1346 rs := vector.MustFunctionResult[uint64](result) 1347 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1348 case types.T_int8: 1349 rs := vector.MustFunctionResult[int8](result) 1350 return bitCastBinaryToFixed(ctx, source, rs, 1, length) 1351 case types.T_int16: 1352 rs := vector.MustFunctionResult[int16](result) 1353 return bitCastBinaryToFixed(ctx, source, rs, 2, length) 1354 case types.T_int32: 1355 rs := vector.MustFunctionResult[int32](result) 1356 return bitCastBinaryToFixed(ctx, source, rs, 4, length) 1357 case types.T_int64: 1358 rs := vector.MustFunctionResult[int64](result) 1359 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1360 case types.T_uint8: 1361 rs := vector.MustFunctionResult[uint8](result) 1362 return bitCastBinaryToFixed(ctx, source, rs, 1, length) 1363 case types.T_uint16: 1364 rs := vector.MustFunctionResult[uint16](result) 1365 return bitCastBinaryToFixed(ctx, source, rs, 2, length) 1366 case types.T_uint32: 1367 rs := vector.MustFunctionResult[uint32](result) 1368 return bitCastBinaryToFixed(ctx, source, rs, 4, length) 1369 case types.T_uint64: 1370 rs := vector.MustFunctionResult[uint64](result) 1371 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1372 case types.T_float32: 1373 rs := vector.MustFunctionResult[float32](result) 1374 return bitCastBinaryToFixed(ctx, source, rs, 4, length) 1375 case types.T_float64: 1376 rs := vector.MustFunctionResult[float64](result) 1377 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1378 case types.T_decimal64: 1379 rs := vector.MustFunctionResult[types.Decimal64](result) 1380 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1381 case types.T_decimal128: 1382 rs := vector.MustFunctionResult[types.Decimal128](result) 1383 return bitCastBinaryToFixed(ctx, source, rs, 16, length) 1384 case types.T_bool: 1385 rs := vector.MustFunctionResult[bool](result) 1386 return bitCastBinaryToFixed(ctx, source, rs, 1, length) 1387 case types.T_uuid: 1388 rs := vector.MustFunctionResult[types.Uuid](result) 1389 return bitCastBinaryToFixed(ctx, source, rs, 16, length) 1390 case types.T_date: 1391 rs := vector.MustFunctionResult[types.Date](result) 1392 return bitCastBinaryToFixed(ctx, source, rs, 4, length) 1393 case types.T_datetime: 1394 rs := vector.MustFunctionResult[types.Datetime](result) 1395 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1396 case types.T_time: 1397 rs := vector.MustFunctionResult[types.Time](result) 1398 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1399 case types.T_timestamp: 1400 rs := vector.MustFunctionResult[types.Timestamp](result) 1401 return bitCastBinaryToFixed(ctx, source, rs, 8, length) 1402 } 1403 1404 return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from %s to %s", source.GetType(), toType)) 1405 } 1406 1407 func BitmapBitPosition(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1408 return opUnaryFixedToFixed[uint64, uint64](parameters, result, proc, length, func(v uint64) uint64 { 1409 // low 15 bits 1410 return v & 0x7fff 1411 }) 1412 } 1413 1414 func BitmapBucketNumber(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1415 return opUnaryFixedToFixed[uint64, uint64](parameters, result, proc, length, func(v uint64) uint64 { 1416 return v >> 15 1417 }) 1418 } 1419 1420 func BitmapCount(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 1421 return opUnaryBytesToFixed[uint64](parameters, result, proc, length, func(v []byte) (cnt uint64) { 1422 bmp := roaring.New() 1423 if err := bmp.UnmarshalBinary(v); err != nil { 1424 return 0 1425 } 1426 return bmp.GetCardinality() 1427 }) 1428 } 1429 1430 func SHA1Func( 1431 parameters []*vector.Vector, 1432 result vector.FunctionResultWrapper, 1433 proc *process.Process, 1434 length int, 1435 ) error { 1436 return opUnaryBytesToBytes(parameters, result, proc, length, func(v []byte) []byte { 1437 sum := sha1.Sum(v) 1438 return []byte(hex.EncodeToString(sum[:])) 1439 }) 1440 }