github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/cast.go (about) 1 // Copyright 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 operator 16 17 import ( 18 "context" 19 "fmt" 20 "math" 21 "strings" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/sql/plan/function/builtin/binary" 25 "github.com/matrixorigin/matrixone/pkg/vectorize/timestamp" 26 27 "github.com/matrixorigin/matrixone/pkg/common/moerr" 28 29 "github.com/matrixorigin/matrixone/pkg/container/nulls" 30 "github.com/matrixorigin/matrixone/pkg/container/types" 31 "github.com/matrixorigin/matrixone/pkg/container/vector" 32 "github.com/matrixorigin/matrixone/pkg/vm/process" 33 "golang.org/x/exp/constraints" 34 ) 35 36 func Cast(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 37 vec, err := doCast(vs, proc) 38 return vec, err 39 } 40 41 // shorten the string to the one with no more than 101 characters. 42 func shortenValueString(valueStr string) string { 43 utf8Str := []rune(valueStr) 44 l := len(utf8Str) 45 if l > 100 { 46 return string(utf8Str[:100]) + "..." 47 } 48 return valueStr 49 } 50 51 func formatCastError(ctx context.Context, vec *vector.Vector, typ types.Type, extraInfo string) error { 52 var errStr string 53 if vec.IsScalar() { 54 if vec.ConstVectorIsNull() { 55 errStr = fmt.Sprintf("Can't cast 'NULL' as %v type.", typ) 56 } else { 57 valueStr := strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", vec), "["), "]") 58 shortenValueStr := shortenValueString(valueStr) 59 errStr = fmt.Sprintf("Can't cast '%s' from %v type to %v type.", shortenValueStr, vec.Typ, typ) 60 } 61 } else { 62 errStr = fmt.Sprintf("Can't cast column from %v type to %v type because of one or more values in that column.", vec.Typ, typ) 63 } 64 return moerr.NewInternalError(ctx, errStr+" "+extraInfo) 65 } 66 67 func doCast(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 68 lv := vs[0] 69 rv := vs[1] 70 if rv.IsScalarNull() { 71 return nil, formatCastError(proc.Ctx, lv, rv.Typ, "the target type of cast function cannot be null") 72 } 73 if lv.IsScalarNull() { 74 return proc.AllocScalarNullVector(rv.Typ), nil 75 } 76 77 if lv.Typ.Oid == rv.Typ.Oid && lv.Typ.IsFixedLen() { 78 switch lv.Typ.Oid { 79 case types.T_int8: 80 return CastSameType[int8](lv, rv, proc) 81 case types.T_int16: 82 return CastSameType[int16](lv, rv, proc) 83 case types.T_int32: 84 return CastSameType[int32](lv, rv, proc) 85 case types.T_int64: 86 return CastSameType[int64](lv, rv, proc) 87 case types.T_uint8: 88 return CastSameType[uint8](lv, rv, proc) 89 case types.T_uint16: 90 return CastSameType[uint16](lv, rv, proc) 91 case types.T_uint32: 92 return CastSameType[uint32](lv, rv, proc) 93 case types.T_uint64: 94 return CastSameType[uint64](lv, rv, proc) 95 case types.T_float32: 96 return CastSameType[float32](lv, rv, proc) 97 case types.T_float64: 98 return CastSameType[float64](lv, rv, proc) 99 case types.T_date: 100 return CastSameType[types.Date](lv, rv, proc) 101 case types.T_time: 102 return CastSameType[types.Time](lv, rv, proc) 103 case types.T_datetime: 104 return CastSameType[types.Datetime](lv, rv, proc) 105 case types.T_timestamp: 106 return CastSameType[types.Timestamp](lv, rv, proc) 107 case types.T_decimal64: 108 return CastSameType[types.Decimal64](lv, rv, proc) 109 case types.T_decimal128: 110 return CastSameType[types.Decimal128](lv, rv, proc) 111 case types.T_TS: 112 return CastSameType[types.TS](lv, rv, proc) 113 case types.T_Rowid: 114 return CastSameType[types.Rowid](lv, rv, proc) 115 default: 116 panic("unknow type in case same type of fixed size.") 117 } 118 } 119 120 if lv.Typ.Oid != rv.Typ.Oid && IsNumeric(lv.Typ.Oid) && IsNumeric(rv.Typ.Oid) { 121 switch lv.Typ.Oid { 122 case types.T_int8: 123 switch rv.Typ.Oid { 124 case types.T_int16: 125 return CastLeftToRight[int8, int16](lv, rv, proc) 126 case types.T_int32: 127 return CastLeftToRight[int8, int32](lv, rv, proc) 128 case types.T_int64: 129 return CastLeftToRight[int8, int64](lv, rv, proc) 130 case types.T_uint8: 131 return CastLeftToRight[int8, uint8](lv, rv, proc) 132 case types.T_uint16: 133 return CastLeftToRight[int8, uint16](lv, rv, proc) 134 case types.T_uint32: 135 return CastLeftToRight[int8, uint32](lv, rv, proc) 136 case types.T_uint64: 137 return CastLeftToRight[int8, uint64](lv, rv, proc) 138 case types.T_float32: 139 return CastLeftToRight[int8, float32](lv, rv, proc) 140 case types.T_float64: 141 return CastLeftToRight[int8, float64](lv, rv, proc) 142 } 143 case types.T_int16: 144 switch rv.Typ.Oid { 145 case types.T_int8: 146 return CastLeftToRight[int16, int8](lv, rv, proc) 147 case types.T_int32: 148 return CastLeftToRight[int16, int32](lv, rv, proc) 149 case types.T_int64: 150 return CastLeftToRight[int16, int64](lv, rv, proc) 151 case types.T_uint8: 152 return CastLeftToRight[int16, uint8](lv, rv, proc) 153 case types.T_uint16: 154 return CastLeftToRight[int16, uint16](lv, rv, proc) 155 case types.T_uint32: 156 return CastLeftToRight[int16, uint32](lv, rv, proc) 157 case types.T_uint64: 158 return CastLeftToRight[int16, uint64](lv, rv, proc) 159 case types.T_float32: 160 return CastLeftToRight[int16, float32](lv, rv, proc) 161 case types.T_float64: 162 return CastLeftToRight[int16, float64](lv, rv, proc) 163 } 164 case types.T_int32: 165 switch rv.Typ.Oid { 166 case types.T_int8: 167 return CastLeftToRight[int32, int8](lv, rv, proc) 168 case types.T_int16: 169 return CastLeftToRight[int32, int16](lv, rv, proc) 170 case types.T_int64: 171 return CastLeftToRight[int32, int64](lv, rv, proc) 172 case types.T_uint8: 173 return CastLeftToRight[int32, uint8](lv, rv, proc) 174 case types.T_uint16: 175 return CastLeftToRight[int32, uint16](lv, rv, proc) 176 case types.T_uint32: 177 return CastLeftToRight[int32, uint32](lv, rv, proc) 178 case types.T_uint64: 179 return CastLeftToRight[int32, uint64](lv, rv, proc) 180 case types.T_float32: 181 return CastLeftToRight[int32, float32](lv, rv, proc) 182 case types.T_float64: 183 return CastLeftToRight[int32, float64](lv, rv, proc) 184 } 185 case types.T_int64: 186 switch rv.Typ.Oid { 187 case types.T_int8: 188 return CastLeftToRight[int64, int8](lv, rv, proc) 189 case types.T_int16: 190 return CastLeftToRight[int64, int16](lv, rv, proc) 191 case types.T_int32: 192 return CastLeftToRight[int64, int32](lv, rv, proc) 193 case types.T_uint8: 194 return CastLeftToRight[int64, uint8](lv, rv, proc) 195 case types.T_uint16: 196 return CastLeftToRight[int64, uint16](lv, rv, proc) 197 case types.T_uint32: 198 return CastLeftToRight[int64, uint32](lv, rv, proc) 199 case types.T_uint64: 200 return CastInt64ToUint64(lv, rv, proc) 201 case types.T_float32: 202 return CastLeftToRight[int64, float32](lv, rv, proc) 203 case types.T_float64: 204 return CastLeftToRight[int64, float64](lv, rv, proc) 205 } 206 case types.T_uint8: 207 switch rv.Typ.Oid { 208 case types.T_int8: 209 return CastLeftToRight[uint8, int8](lv, rv, proc) 210 case types.T_int16: 211 return CastLeftToRight[uint8, int16](lv, rv, proc) 212 case types.T_int32: 213 return CastLeftToRight[uint8, int32](lv, rv, proc) 214 case types.T_int64: 215 return CastLeftToRight[uint8, int64](lv, rv, proc) 216 case types.T_uint16: 217 return CastLeftToRight[uint8, uint16](lv, rv, proc) 218 case types.T_uint32: 219 return CastLeftToRight[uint8, uint32](lv, rv, proc) 220 case types.T_uint64: 221 return CastLeftToRight[uint8, uint64](lv, rv, proc) 222 case types.T_float32: 223 return CastLeftToRight[uint8, float32](lv, rv, proc) 224 case types.T_float64: 225 return CastLeftToRight[uint8, float64](lv, rv, proc) 226 } 227 case types.T_uint16: 228 switch rv.Typ.Oid { 229 case types.T_int8: 230 return CastLeftToRight[uint16, int8](lv, rv, proc) 231 case types.T_int16: 232 return CastLeftToRight[uint16, int16](lv, rv, proc) 233 case types.T_int32: 234 return CastLeftToRight[uint16, int32](lv, rv, proc) 235 case types.T_int64: 236 return CastLeftToRight[uint16, int64](lv, rv, proc) 237 case types.T_uint8: 238 return CastLeftToRight[uint16, uint8](lv, rv, proc) 239 case types.T_uint32: 240 return CastLeftToRight[uint16, uint32](lv, rv, proc) 241 case types.T_uint64: 242 return CastLeftToRight[uint16, uint64](lv, rv, proc) 243 case types.T_float32: 244 return CastLeftToRight[uint16, float32](lv, rv, proc) 245 case types.T_float64: 246 return CastLeftToRight[uint16, float64](lv, rv, proc) 247 } 248 case types.T_uint32: 249 switch rv.Typ.Oid { 250 case types.T_int8: 251 return CastLeftToRight[uint32, int8](lv, rv, proc) 252 case types.T_int16: 253 return CastLeftToRight[uint32, int16](lv, rv, proc) 254 case types.T_int32: 255 return CastLeftToRight[uint32, int32](lv, rv, proc) 256 case types.T_int64: 257 return CastLeftToRight[uint32, int64](lv, rv, proc) 258 case types.T_uint8: 259 return CastLeftToRight[uint32, uint8](lv, rv, proc) 260 case types.T_uint16: 261 return CastLeftToRight[uint32, uint16](lv, rv, proc) 262 case types.T_uint64: 263 return CastLeftToRight[uint32, uint64](lv, rv, proc) 264 case types.T_float32: 265 return CastLeftToRight[uint32, float32](lv, rv, proc) 266 case types.T_float64: 267 return CastLeftToRight[uint32, float64](lv, rv, proc) 268 } 269 case types.T_uint64: 270 switch rv.Typ.Oid { 271 case types.T_int8: 272 return CastLeftToRight[uint64, int8](lv, rv, proc) 273 case types.T_int16: 274 return CastLeftToRight[uint64, int16](lv, rv, proc) 275 case types.T_int32: 276 return CastLeftToRight[uint64, int32](lv, rv, proc) 277 case types.T_int64: 278 return CastUint64ToInt64(lv, rv, proc) 279 case types.T_uint8: 280 return CastLeftToRight[uint64, uint8](lv, rv, proc) 281 case types.T_uint16: 282 return CastLeftToRight[uint64, uint16](lv, rv, proc) 283 case types.T_uint32: 284 return CastLeftToRight[uint64, uint32](lv, rv, proc) 285 case types.T_float32: 286 return CastLeftToRight[uint64, float32](lv, rv, proc) 287 case types.T_float64: 288 return CastLeftToRight[uint64, float64](lv, rv, proc) 289 } 290 case types.T_float32: 291 switch rv.Typ.Oid { 292 case types.T_int8: 293 return CastFloatToInt[float32, int8](lv, rv, proc) 294 case types.T_int16: 295 return CastFloatToInt[float32, int16](lv, rv, proc) 296 case types.T_int32: 297 return CastFloatToInt[float32, int32](lv, rv, proc) 298 case types.T_int64: 299 return CastFloatToInt[float32, int64](lv, rv, proc) 300 case types.T_uint8: 301 return CastFloatToInt[float32, uint8](lv, rv, proc) 302 case types.T_uint16: 303 return CastFloatToInt[float32, uint16](lv, rv, proc) 304 case types.T_uint32: 305 return CastFloatToInt[float32, uint32](lv, rv, proc) 306 case types.T_uint64: 307 return CastFloatToInt[float32, uint64](lv, rv, proc) 308 case types.T_float64: 309 return CastLeftToRight[float32, float64](lv, rv, proc) 310 } 311 case types.T_float64: 312 switch rv.Typ.Oid { 313 case types.T_int8: 314 return CastFloatToInt[float64, int8](lv, rv, proc) 315 case types.T_int16: 316 return CastFloatToInt[float64, int16](lv, rv, proc) 317 case types.T_int32: 318 return CastFloatToInt[float64, int32](lv, rv, proc) 319 case types.T_int64: 320 return CastFloat64ToInt64(lv, rv, proc) 321 case types.T_uint8: 322 return CastFloatToInt[float64, uint8](lv, rv, proc) 323 case types.T_uint16: 324 return CastFloatToInt[float64, uint16](lv, rv, proc) 325 case types.T_uint32: 326 return CastFloatToInt[float64, uint32](lv, rv, proc) 327 case types.T_uint64: 328 return CastFloatToInt[float64, uint64](lv, rv, proc) 329 case types.T_float32: 330 return CastLeftToRight[float64, float32](lv, rv, proc) 331 } 332 } 333 } 334 335 if isString(lv.Typ.Oid) && IsInteger(rv.Typ.Oid) { 336 switch rv.Typ.Oid { 337 case types.T_int8: 338 return CastSpecials1Int[int8](lv, rv, proc) 339 case types.T_int16: 340 return CastSpecials1Int[int16](lv, rv, proc) 341 case types.T_int32: 342 return CastSpecials1Int[int32](lv, rv, proc) 343 case types.T_int64: 344 return CastSpecials1Int[int64](lv, rv, proc) 345 case types.T_uint8: 346 return CastSpecials1Uint[uint8](lv, rv, proc) 347 case types.T_uint16: 348 return CastSpecials1Uint[uint16](lv, rv, proc) 349 case types.T_uint32: 350 return CastSpecials1Uint[uint32](lv, rv, proc) 351 case types.T_uint64: 352 return CastSpecials1Uint[uint64](lv, rv, proc) 353 } 354 } 355 356 if isString(lv.Typ.Oid) && IsFloat(rv.Typ.Oid) { 357 switch rv.Typ.Oid { 358 case types.T_float32: 359 return CastSpecials1Float[float32](lv, rv, proc) 360 case types.T_float64: 361 return CastSpecials1Float[float64](lv, rv, proc) 362 } 363 } 364 365 if isString(lv.Typ.Oid) && IsDecimal(rv.Typ.Oid) { 366 switch rv.Typ.Oid { 367 case types.T_decimal64: 368 return CastStringAsDecimal64(lv, rv, proc) 369 case types.T_decimal128: 370 return CastStringAsDecimal128(lv, rv, proc) 371 } 372 } 373 374 if IsInteger(lv.Typ.Oid) && isString(rv.Typ.Oid) { 375 switch lv.Typ.Oid { 376 case types.T_int8: 377 return CastSpecials2Int[int8](lv, rv, proc) 378 case types.T_int16: 379 return CastSpecials2Int[int16](lv, rv, proc) 380 case types.T_int32: 381 return CastSpecials2Int[int32](lv, rv, proc) 382 case types.T_int64: 383 return CastSpecials2Int[int64](lv, rv, proc) 384 case types.T_uint8: 385 return CastSpecials2Uint[uint8](lv, rv, proc) 386 case types.T_uint16: 387 return CastSpecials2Uint[uint16](lv, rv, proc) 388 case types.T_uint32: 389 return CastSpecials2Uint[uint32](lv, rv, proc) 390 case types.T_uint64: 391 return CastSpecials2Uint[uint64](lv, rv, proc) 392 } 393 } 394 395 if IsFloat(lv.Typ.Oid) && isString(rv.Typ.Oid) { 396 switch lv.Typ.Oid { 397 case types.T_float32: 398 return CastSpecials2Float[float32](lv, rv, proc) 399 case types.T_float64: 400 return CastSpecials2Float[float64](lv, rv, proc) 401 } 402 } 403 if IsDecimal(lv.Typ.Oid) && isString(rv.Typ.Oid) { 404 switch lv.Typ.Oid { 405 case types.T_decimal64: 406 return CastDecimal64ToString(lv, rv, proc) 407 case types.T_decimal128: 408 return CastDecimal128ToString(lv, rv, proc) 409 } 410 } 411 412 if isString(lv.Typ.Oid) && isString(rv.Typ.Oid) { 413 return CastSpecials3(lv, rv, proc) 414 } 415 416 if isSignedInteger(lv.Typ.Oid) && rv.Typ.Oid == types.T_decimal128 { 417 switch lv.Typ.Oid { 418 case types.T_int8: 419 return CastSpecials4[int8](lv, rv, proc) 420 case types.T_int16: 421 return CastSpecials4[int16](lv, rv, proc) 422 case types.T_int32: 423 return CastSpecials4[int32](lv, rv, proc) 424 case types.T_int64: 425 return CastSpecials4[int64](lv, rv, proc) 426 } 427 } 428 429 //The Big Number will be processed by string, it's ok 430 if isSignedInteger(lv.Typ.Oid) && (rv.Typ.Oid == types.T_decimal64) { 431 switch lv.Typ.Oid { 432 case types.T_int8: 433 return CastSpecials4_64[int8](lv, rv, proc) 434 case types.T_int16: 435 return CastSpecials4_64[int16](lv, rv, proc) 436 case types.T_int32: 437 return CastSpecials4_64[int32](lv, rv, proc) 438 case types.T_int64: 439 return CastSpecials4_64[int64](lv, rv, proc) 440 } 441 } 442 443 if isUnsignedInteger(lv.Typ.Oid) && rv.Typ.Oid == types.T_decimal128 { 444 switch lv.Typ.Oid { 445 case types.T_uint8: 446 return CastSpecialu4[uint8](lv, rv, proc) 447 case types.T_uint16: 448 return CastSpecialu4[uint16](lv, rv, proc) 449 case types.T_uint32: 450 return CastSpecialu4[uint32](lv, rv, proc) 451 case types.T_uint64: 452 return CastSpecialu4[uint64](lv, rv, proc) 453 } 454 } 455 456 if IsFloat(lv.Typ.Oid) && rv.Typ.Oid == types.T_decimal128 { 457 switch lv.Typ.Oid { 458 case types.T_float32: 459 return CastFloatAsDecimal128[float32](lv, rv, proc) 460 case types.T_float64: 461 return CastFloatAsDecimal128[float64](lv, rv, proc) 462 } 463 } 464 465 if IsFloat(lv.Typ.Oid) && rv.Typ.Oid == types.T_decimal64 { 466 switch lv.Typ.Oid { 467 case types.T_float32: 468 return CastFloatAsDecimal64[float32](lv, rv, proc) 469 case types.T_float64: 470 return CastFloatAsDecimal64[float64](lv, rv, proc) 471 } 472 } 473 474 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_date { 475 return CastVarcharAsDate(lv, rv, proc) 476 } 477 478 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_time { 479 return CastVarcharAsTime(lv, rv, proc) 480 } 481 482 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_datetime { 483 return CastVarcharAsDatetime(lv, rv, proc) 484 } 485 486 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_timestamp { 487 return CastVarcharAsTimestamp(lv, rv, proc) 488 } 489 490 if lv.Typ.Oid == types.T_decimal64 && rv.Typ.Oid == types.T_decimal128 { 491 return CastDecimal64AsDecimal128(lv, rv, proc) 492 } 493 494 if lv.Typ.Oid == types.T_timestamp && rv.Typ.Oid == types.T_datetime { 495 return castTimestampAsDatetime(lv, rv, proc) 496 } 497 498 if lv.Typ.Oid == types.T_datetime && rv.Typ.Oid == types.T_timestamp { 499 return CastDatetimeAsTimestamp(lv, rv, proc) 500 } 501 502 if lv.Typ.Oid == types.T_date && rv.Typ.Oid == types.T_timestamp { 503 return CastDateAsTimestamp(lv, rv, proc) 504 } 505 506 if lv.Typ.Oid == types.T_date && rv.Typ.Oid == types.T_time { 507 return CastDateAsTime(lv, rv, proc) 508 } 509 510 if lv.Typ.Oid == types.T_time && rv.Typ.Oid == types.T_date { 511 return CastTimeAsDate(lv, rv, proc) 512 } 513 514 if lv.Typ.Oid == types.T_timestamp && isString(rv.Typ.Oid) { 515 return castTimestampAsVarchar(lv, rv, proc) 516 } 517 518 if lv.Typ.Oid == types.T_bool && isString(rv.Typ.Oid) { 519 return CastBoolToString(lv, rv, proc) 520 } 521 522 if lv.Typ.Oid == types.T_date && rv.Typ.Oid == types.T_datetime { 523 return CastDateAsDatetime(lv, rv, proc) 524 } 525 526 if lv.Typ.Oid == types.T_datetime && rv.Typ.Oid == types.T_date { 527 return CastDatetimeAsDate(lv, rv, proc) 528 } 529 530 if lv.Typ.Oid == types.T_datetime && rv.Typ.Oid == types.T_time { 531 return CastDatetimeAsTime(lv, rv, proc) 532 } 533 534 if lv.Typ.Oid == types.T_time && rv.Typ.Oid == types.T_datetime { 535 return CastTimeAsDatetime(lv, rv, proc) 536 } 537 538 if lv.Typ.Oid == types.T_date && isString(rv.Typ.Oid) { 539 return CastDateAsString(lv, rv, proc) 540 } 541 542 if lv.Typ.Oid == types.T_time && isString(rv.Typ.Oid) { 543 return CastTimeAsString(lv, rv, proc) 544 } 545 546 if lv.Typ.Oid == types.T_datetime && isString(rv.Typ.Oid) { 547 return CastDatetimeAsString(lv, rv, proc) 548 } 549 550 if IsInteger(lv.Typ.Oid) && rv.Typ.Oid == types.T_timestamp { 551 switch lv.Typ.Oid { 552 case types.T_int8: 553 return CastIntAsTimestamp[int8](lv, rv, proc) 554 case types.T_int16: 555 return CastIntAsTimestamp[int16](lv, rv, proc) 556 case types.T_int32: 557 return CastIntAsTimestamp[int32](lv, rv, proc) 558 case types.T_int64: 559 return CastIntAsTimestamp[int64](lv, rv, proc) 560 case types.T_uint8: 561 return CastUIntAsTimestamp[uint8](lv, rv, proc) 562 case types.T_uint16: 563 return CastUIntAsTimestamp[uint16](lv, rv, proc) 564 case types.T_uint32: 565 return CastUIntAsTimestamp[uint32](lv, rv, proc) 566 case types.T_uint64: 567 return CastUIntAsTimestamp[uint64](lv, rv, proc) 568 } 569 } 570 571 if IsInteger(lv.Typ.Oid) && rv.Typ.Oid == types.T_time { 572 switch lv.Typ.Oid { 573 case types.T_int8: 574 return CastIntAsTime[int8](lv, rv, proc) 575 case types.T_int16: 576 return CastIntAsTime[int16](lv, rv, proc) 577 case types.T_int32: 578 return CastIntAsTime[int32](lv, rv, proc) 579 case types.T_int64: 580 return CastIntAsTime[int64](lv, rv, proc) 581 case types.T_uint8: 582 return CastUIntAsTime[uint8](lv, rv, proc) 583 case types.T_uint16: 584 return CastUIntAsTime[uint16](lv, rv, proc) 585 case types.T_uint32: 586 return CastUIntAsTime[uint32](lv, rv, proc) 587 case types.T_uint64: 588 return CastUIntAsTime[uint64](lv, rv, proc) 589 } 590 } 591 592 if lv.Typ.Oid == types.T_time && IsInteger(rv.Typ.Oid) { 593 switch rv.Typ.Oid { 594 case types.T_int8: 595 return CastTimeAsNumeric[int8](lv, rv, proc) 596 case types.T_int16: 597 return CastTimeAsNumeric[int16](lv, rv, proc) 598 case types.T_int32: 599 return CastTimeAsNumeric[int32](lv, rv, proc) 600 case types.T_int64: 601 return CastTimeAsNumeric[int64](lv, rv, proc) 602 case types.T_uint8: 603 return CastTimeAsNumeric[uint8](lv, rv, proc) 604 case types.T_uint16: 605 return CastTimeAsNumeric[uint16](lv, rv, proc) 606 case types.T_uint32: 607 return CastTimeAsNumeric[uint32](lv, rv, proc) 608 case types.T_uint64: 609 return CastTimeAsNumeric[uint64](lv, rv, proc) 610 } 611 } 612 613 if lv.Typ.Oid == types.T_date && rv.Typ.Oid == types.T_int32 { 614 return castDateAsInt32(lv, rv, proc) 615 } 616 617 if lv.Typ.Oid == types.T_date && rv.Typ.Oid == types.T_int64 { 618 return castDateAsInt64(lv, rv, proc) 619 } 620 621 if lv.Typ.Oid == types.T_datetime && rv.Typ.Oid == types.T_int32 { 622 return castDatetimeAsInt32(lv, rv, proc) 623 } 624 625 if lv.Typ.Oid == types.T_datetime && rv.Typ.Oid == types.T_int64 { 626 return castDatetimeAsInt64(lv, rv, proc) 627 } 628 629 if lv.Typ.Oid == types.T_timestamp && rv.Typ.Oid == types.T_int32 { 630 return castTimestampAsInt32(lv, rv, proc) 631 } 632 633 if lv.Typ.Oid == types.T_timestamp && rv.Typ.Oid == types.T_int64 { 634 return castTimestampAsInt64(lv, rv, proc) 635 } 636 637 if IsDecimal(lv.Typ.Oid) && rv.Typ.Oid == types.T_timestamp { 638 switch lv.Typ.Oid { 639 case types.T_decimal64: 640 return CastDecimal64AsTimestamp(lv, rv, proc) 641 case types.T_decimal128: 642 return CastDecimal128AsTimestamp(lv, rv, proc) 643 } 644 } 645 646 if IsDecimal(lv.Typ.Oid) && rv.Typ.Oid == types.T_time { 647 switch lv.Typ.Oid { 648 case types.T_decimal64: 649 return CastDecimal64AsTime(lv, rv, proc) 650 case types.T_decimal128: 651 return CastDecimal128AsTime(lv, rv, proc) 652 } 653 } 654 655 if lv.Typ.Oid == types.T_time && IsDecimal(rv.Typ.Oid) { 656 switch rv.Typ.Oid { 657 case types.T_decimal64: 658 return CastTimeAsDecimal64(lv, rv, proc) 659 case types.T_decimal128: 660 return CastTimeAsDecimal128(lv, rv, proc) 661 } 662 } 663 664 if lv.Typ.Oid == types.T_timestamp && rv.Typ.Oid == types.T_date { 665 return CastTimestampAsDate(lv, rv, proc) 666 } 667 668 if lv.Typ.Oid == types.T_decimal64 && rv.Typ.Oid == types.T_float32 { 669 return CastDecimal64ToFloat32(lv, rv, proc) 670 } 671 if lv.Typ.Oid == types.T_decimal128 && rv.Typ.Oid == types.T_float32 { 672 return CastDecimal128ToFloat32(lv, rv, proc) 673 } 674 675 if lv.Typ.Oid == types.T_decimal64 && rv.Typ.Oid == types.T_float64 { 676 return CastDecimal64ToFloat64(lv, rv, proc) 677 } 678 if lv.Typ.Oid == types.T_decimal128 && rv.Typ.Oid == types.T_float64 { 679 return CastDecimal128ToFloat64(lv, rv, proc) 680 } 681 if lv.Typ.Oid == types.T_decimal64 && rv.Typ.Oid == types.T_int64 { 682 return CastDecimal64ToInt64(lv, rv, proc) 683 } 684 if lv.Typ.Oid == types.T_decimal128 && rv.Typ.Oid == types.T_int32 { 685 return CastDecimal128ToInt32(lv, rv, proc) 686 } 687 if lv.Typ.Oid == types.T_decimal128 && rv.Typ.Oid == types.T_int64 { 688 return CastDecimal128ToInt64(lv, rv, proc) 689 } 690 if lv.Typ.Oid == types.T_decimal64 && rv.Typ.Oid == types.T_uint64 { 691 return CastDecimal64ToUint64(lv, rv, proc) 692 } 693 if lv.Typ.Oid == types.T_decimal128 && rv.Typ.Oid == types.T_uint64 { 694 return CastDecimal128ToUint64(lv, rv, proc) 695 } 696 if lv.Typ.Oid == types.T_decimal128 && rv.Typ.Oid == types.T_decimal64 { 697 return CastDecimal128ToDecimal64(lv, rv, proc) 698 } 699 // if lv.Typ.Oid == types.T_timestamp && rv.Typ.Oid == types.T_time { 700 // return CastTimestampAsTime(lv, rv, proc) 701 // } 702 703 if IsNumeric(lv.Typ.Oid) && rv.Typ.Oid == types.T_bool { 704 switch lv.Typ.Oid { 705 case types.T_int8: 706 return CastNumValToBool[int8](lv, rv, proc) 707 case types.T_int16: 708 return CastNumValToBool[int16](lv, rv, proc) 709 case types.T_int32: 710 return CastNumValToBool[int32](lv, rv, proc) 711 case types.T_int64: 712 return CastNumValToBool[int64](lv, rv, proc) 713 case types.T_uint8: 714 return CastNumValToBool[uint8](lv, rv, proc) 715 case types.T_uint16: 716 return CastNumValToBool[uint16](lv, rv, proc) 717 case types.T_uint32: 718 return CastNumValToBool[uint32](lv, rv, proc) 719 case types.T_uint64: 720 return CastNumValToBool[uint64](lv, rv, proc) 721 case types.T_float32: 722 return CastNumValToBool[float32](lv, rv, proc) 723 case types.T_float64: 724 return CastNumValToBool[float64](lv, rv, proc) 725 } 726 } 727 728 if lv.Typ.Oid == types.T_bool && IsNumeric(rv.Typ.Oid) { 729 switch rv.Typ.Oid { 730 case types.T_int8: 731 return CastBoolToNumeric[int8](lv, rv, proc) 732 case types.T_int16: 733 return CastBoolToNumeric[int16](lv, rv, proc) 734 case types.T_int32: 735 return CastBoolToNumeric[int32](lv, rv, proc) 736 case types.T_int64: 737 return CastBoolToNumeric[int64](lv, rv, proc) 738 case types.T_uint8: 739 return CastBoolToNumeric[uint8](lv, rv, proc) 740 case types.T_uint16: 741 return CastBoolToNumeric[uint16](lv, rv, proc) 742 case types.T_uint32: 743 return CastBoolToNumeric[uint32](lv, rv, proc) 744 case types.T_uint64: 745 return CastBoolToNumeric[uint64](lv, rv, proc) 746 } 747 } 748 749 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_bool { 750 return CastStringToBool(lv, rv, proc) 751 } 752 753 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_json { 754 return CastStringToJson(lv, rv, proc) 755 } 756 if lv.Typ.Oid == types.T_json && isString(rv.Typ.Oid) { 757 return CastJsonToString(lv, rv, proc) 758 } 759 760 if isString(lv.Typ.Oid) && rv.Typ.Oid == types.T_uuid { 761 return CastStringToUuid(lv, rv, proc) 762 } 763 764 if lv.Typ.Oid == types.T_uuid && isString(rv.Typ.Oid) { 765 return CastUuidToString(lv, rv, proc) 766 } 767 768 return nil, formatCastError(proc.Ctx, lv, rv.Typ, "") 769 } 770 771 func CastTimestampAsDate(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 772 var t *time.Location 773 if proc == nil { 774 t = time.Local 775 } else { 776 t = proc.SessionInfo.TimeZone 777 } 778 lvs := vector.MustTCols[types.Timestamp](lv) 779 if lv.IsScalar() { 780 rs := make([]types.Datetime, 1) 781 if _, err := binary.TimestampToDatetime(proc.Ctx, t, lvs, rs); err != nil { 782 return nil, err 783 } 784 rs2 := make([]types.Date, 1) 785 rs2[0] = rs[0].ToDate() 786 vec := vector.NewConstFixed(rv.Typ, 1, rs2[0], proc.Mp()) 787 nulls.Set(vec.Nsp, lv.Nsp) 788 return vec, nil 789 } 790 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 791 if err != nil { 792 return nil, err 793 } 794 rs := vector.MustTCols[types.Date](vec) 795 rs2 := make([]types.Datetime, len(lvs), cap(lvs)) 796 if _, err := binary.TimestampToDatetime(proc.Ctx, t, lvs, rs2); err != nil { 797 return nil, err 798 } 799 for i := 0; i < len(rs2); i++ { 800 rs[i] = rs2[i].ToDate() 801 } 802 return vec, nil 803 } 804 805 func CastDecimal64ToString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 806 var err error 807 if lv.IsScalar() { 808 if lv.IsScalarNull() { 809 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 810 } else { 811 lvs := vector.MustTCols[types.Decimal64](lv) 812 col := make([]string, 1) 813 if col, err = binary.Decimal64ToBytes(lvs, col, lv.Typ.Scale); err != nil { 814 return nil, err 815 } 816 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 817 } 818 } else { 819 lvs := vector.MustTCols[types.Decimal64](lv) 820 col := make([]string, len(lvs)) 821 if col, err = binary.Decimal64ToBytes(lvs, col, lv.Typ.Scale); err != nil { 822 return nil, err 823 } 824 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 825 } 826 } 827 828 func CastDecimal128ToString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 829 var err error 830 if lv.IsScalar() { 831 if lv.IsScalarNull() { 832 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 833 } else { 834 lvs := vector.MustTCols[types.Decimal128](lv) 835 col := make([]string, 1) 836 if col, err = binary.Decimal128ToBytes(lvs, col, lv.Typ.Scale); err != nil { 837 return nil, err 838 } 839 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 840 } 841 } else { 842 lvs := vector.MustTCols[types.Decimal128](lv) 843 col := make([]string, len(lvs)) 844 if col, err = binary.Decimal128ToBytes(lvs, col, lv.Typ.Scale); err != nil { 845 return nil, err 846 } 847 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 848 } 849 } 850 851 // CastSameType : Cast handles the same data type and is numeric , Contains the following: 852 // int8 -> int8, 853 // int16 -> int16, 854 // int32 -> int32, 855 // int64 -> int64, 856 // uint8 -> uint8, 857 // uint16 -> uint16, 858 // uint32 -> uint32, 859 // uint64 -> uint64, 860 // float32 -> float32, 861 // float64 -> float64, 862 func CastSameType[T types.FixedSizeT](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 863 lvs := vector.MustTCols[T](lv) 864 865 if lv.IsScalar() { 866 vec := proc.AllocScalarVector(rv.Typ) 867 rs := make([]T, 1) 868 copy(rs, lvs) 869 nulls.Set(vec.Nsp, lv.Nsp) 870 vector.SetCol(vec, rs) 871 return vec, nil 872 } 873 874 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 875 if err != nil { 876 return nil, err 877 } 878 rs := vector.MustTCols[T](vec) 879 copy(rs, lvs) 880 return vec, nil 881 } 882 883 // CastLeftToRight : Cast handles conversions in the form of cast (left as right), where left and right are different types, 884 // 885 // and both left and right are numeric types, Contains the following: 886 // 887 // int8 -> (int16/int32/int64/uint8/uint16/uint32/uint64/float32/float64) 888 // int16 -> (int8/int32/int64/uint8/uint16/uint32/uint64/float32/float64) 889 // int32 -> (int8/int16/int64/uint8/uint16/uint32/uint64/float32/float64) 890 // int64 -> (int8/int16/int32/uint8/uint16/uint32/uint64/float32/float64) 891 // uint8 -> (int8/int16/int32/int64/uint16/uint32/uint64/float32/float64) 892 // uint16 -> (int8/int16/int32/int64/uint8/uint32/uint64/float32/float64) 893 // uint32 -> (int8/int16/int32/int64/uint8/uint16/uint64/float32/float64) 894 // uint64 -> (int8/int16/int32/int64/uint8/uint16/uint32/float32/float64) 895 func CastLeftToRight[T1, T2 constraints.Integer | constraints.Float](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 896 lvs := vector.MustTCols[T1](lv) 897 898 if lv.IsScalar() { 899 vec := proc.AllocScalarVector(rv.Typ) 900 rs := make([]T2, 1) 901 if _, err := binary.NumericToNumeric(proc.Ctx, lvs, rs); err != nil { 902 return nil, err 903 } 904 nulls.Set(vec.Nsp, lv.Nsp) 905 vector.SetCol(vec, rs) 906 return vec, nil 907 } 908 909 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 910 if err != nil { 911 return nil, err 912 } 913 rs := vector.MustTCols[T2](vec) 914 if _, err := binary.NumericToNumeric(proc.Ctx, lvs, rs); err != nil { 915 return nil, err 916 } 917 return vec, nil 918 } 919 920 func CastFloatToInt[T1 constraints.Float, T2 constraints.Integer](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 921 lvs := vector.MustTCols[T1](lv) 922 923 if lv.IsScalar() { 924 vec := proc.AllocScalarVector(rv.Typ) 925 rs := make([]T2, 1) 926 if _, err := binary.FloatToIntWithoutError(proc.Ctx, lvs, rs); err != nil { 927 return nil, err 928 } 929 nulls.Set(vec.Nsp, lv.Nsp) 930 vector.SetCol(vec, rs) 931 return vec, nil 932 } 933 934 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 935 if err != nil { 936 return nil, err 937 } 938 rs := vector.MustTCols[T2](vec) 939 if _, err := binary.FloatToIntWithoutError(proc.Ctx, lvs, rs); err != nil { 940 return nil, err 941 } 942 return vec, nil 943 } 944 945 // XXX can someone document why this one does not use the templated code? 946 // CastFloat64ToInt64 : cast float64 to int64 947 func CastFloat64ToInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 948 lvs := vector.MustTCols[float64](lv) 949 950 if lv.IsScalar() { 951 vec := proc.AllocScalarVector(rv.Typ) 952 rs := make([]int64, 1) 953 if _, err := binary.Float64ToInt64(proc.Ctx, lvs, rs); err != nil { 954 return nil, err 955 } 956 nulls.Set(vec.Nsp, lv.Nsp) 957 vector.SetCol(vec, rs) 958 return vec, nil 959 } 960 961 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 962 if err != nil { 963 return nil, err 964 } 965 rs := vector.MustTCols[int64](vec) 966 if _, err := binary.Float64ToInt64(proc.Ctx, lvs, rs); err != nil { 967 return nil, err 968 } 969 return vec, nil 970 } 971 972 // CastUint64ToInt64 : cast uint64 to int64 973 func CastUint64ToInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 974 lvs := vector.MustTCols[uint64](lv) 975 976 if lv.IsScalar() { 977 vec := proc.AllocScalarVector(rv.Typ) 978 rs := make([]int64, 1) 979 if _, err := binary.Uint64ToInt64(proc.Ctx, lvs, rs); err != nil { 980 return nil, err 981 } 982 nulls.Set(vec.Nsp, lv.Nsp) 983 vector.SetCol(vec, rs) 984 return vec, nil 985 } 986 987 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 988 if err != nil { 989 return nil, err 990 } 991 rs := vector.MustTCols[int64](vec) 992 if _, err := binary.Uint64ToInt64(proc.Ctx, lvs, rs); err != nil { 993 return nil, err 994 } 995 return vec, nil 996 } 997 998 // CastInt64ToUint64 : cast int64 to uint64 999 func CastInt64ToUint64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1000 lvs := vector.MustTCols[int64](lv) 1001 1002 if lv.IsScalar() { 1003 vec := proc.AllocScalarVector(rv.Typ) 1004 rs := make([]uint64, 1) 1005 if _, err := binary.Int64ToUint64(proc.Ctx, lvs, rs); err != nil { 1006 return nil, err 1007 } 1008 nulls.Set(vec.Nsp, lv.Nsp) 1009 vector.SetCol(vec, rs) 1010 return vec, nil 1011 } 1012 1013 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1014 if err != nil { 1015 return nil, err 1016 } 1017 rs := vector.MustTCols[uint64](vec) 1018 if _, err := binary.Int64ToUint64(proc.Ctx, lvs, rs); err != nil { 1019 return nil, err 1020 } 1021 return vec, nil 1022 } 1023 1024 // CastSpecials1Int : Cast converts string to integer,Contains the following: 1025 // (char / varhcar / text) -> (int8 / int16 / int32/ int64 / uint8 / uint16 / uint32 / uint64) 1026 func CastSpecials1Int[T constraints.Signed](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1027 col := vector.MustStrCols(lv) 1028 var err error 1029 if lv.IsScalarNull() { 1030 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1031 } else if lv.IsScalar() { 1032 rs := make([]T, 1) 1033 if _, err = binary.BytesToInt(proc.Ctx, col, rs, lv.GetIsBin()); err != nil { 1034 return nil, err 1035 } 1036 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 1037 } else { 1038 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(col)), lv.Nsp) 1039 if err != nil { 1040 return nil, err 1041 } 1042 rs := vector.MustTCols[T](vec) 1043 if _, err = binary.BytesToInt(proc.Ctx, col, rs); err != nil { 1044 return nil, err 1045 } 1046 return vector.NewWithFixed(rv.Typ, rs, lv.Nsp, proc.Mp()), nil 1047 } 1048 } 1049 1050 func CastSpecials1Uint[T constraints.Unsigned](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1051 col := vector.MustStrCols(lv) 1052 var err error 1053 if lv.IsScalarNull() { 1054 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1055 } else if lv.IsScalar() { 1056 rs := make([]T, 1) 1057 if _, err = binary.BytesToUint(proc.Ctx, col, rs, lv.GetIsBin()); err != nil { 1058 return nil, err 1059 } 1060 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 1061 } else { 1062 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(col)), lv.Nsp) 1063 if err != nil { 1064 return nil, err 1065 } 1066 rs := vector.MustTCols[T](vec) 1067 if _, err = binary.BytesToUint(proc.Ctx, col, rs); err != nil { 1068 return nil, err 1069 } 1070 return vector.NewWithFixed(rv.Typ, rs, lv.Nsp, proc.Mp()), nil 1071 } 1072 } 1073 1074 // CastSpecials1Float : Cast converts string to floating point number,Contains the following: 1075 // (char / varhcar / text) -> (float32 / float64) 1076 func CastSpecials1Float[T constraints.Float](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1077 col := vector.MustStrCols(lv) 1078 var err error 1079 if lv.IsScalarNull() { 1080 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1081 } else if lv.IsScalar() { 1082 rs := make([]T, 1) 1083 if _, err = binary.BytesToFloat(proc.Ctx, col, rs, lv.GetIsBin()); err != nil { 1084 return nil, err 1085 } 1086 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 1087 } else { 1088 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(col)), lv.Nsp) 1089 if err != nil { 1090 return nil, err 1091 } 1092 rs := vector.MustTCols[T](vec) 1093 if _, err = binary.BytesToFloat(proc.Ctx, col, rs, lv.GetIsBin()); err != nil { 1094 return nil, err 1095 } 1096 return vector.NewWithFixed(rv.Typ, rs, lv.Nsp, proc.Mp()), nil 1097 } 1098 } 1099 1100 // CastSpecials2Int : Cast converts integer to string,Contains the following: 1101 // (int8 /int16/int32/int64) -> (char / varhcar / text) 1102 func CastSpecials2Int[T constraints.Signed](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1103 var err error 1104 if lv.IsScalarNull() { 1105 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1106 } 1107 lvs := vector.MustTCols[T](lv) 1108 col := make([]string, len(lvs)) 1109 if col, err = binary.IntToBytes(lvs, col); err != nil { 1110 return nil, err 1111 } 1112 1113 if lv.IsScalar() { 1114 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 1115 } 1116 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 1117 } 1118 1119 func CastSpecials2Uint[T constraints.Unsigned](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1120 var err error 1121 if lv.IsScalarNull() { 1122 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1123 } 1124 lvs := vector.MustTCols[T](lv) 1125 col := make([]string, len(lvs)) 1126 if col, err = binary.UintToBytes(lvs, col); err != nil { 1127 return nil, err 1128 } 1129 1130 if lv.IsScalar() { 1131 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 1132 } 1133 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 1134 } 1135 1136 // CastSpecials2Float : Cast converts floating point number to string ,Contains the following: 1137 // (float32/float64) -> (char / varhcar) 1138 func CastSpecials2Float[T constraints.Float](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1139 var err error 1140 if lv.IsScalarNull() { 1141 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1142 } 1143 lvs := vector.MustTCols[T](lv) 1144 col := make([]string, len(lvs)) 1145 if col, err = binary.FloatToBytes(lvs, col); err != nil { 1146 return nil, err 1147 } 1148 1149 if lv.IsScalar() { 1150 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 1151 } 1152 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 1153 } 1154 1155 // func CastSpecials2Decimal[T constraints.decimal](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1156 // var err error 1157 // lvs := lv.Col.([]T) 1158 // col := &types.Bytes{ 1159 // Data: make([]byte, 0, len(lvs)), 1160 // Offsets: make([]uint32, 0, len(lvs)), 1161 // Lengths: make([]uint32, 0, len(lvs)), 1162 // } 1163 // if col, err = typecast.FloatToBytes(lvs, col); err != nil { 1164 // return nil, err 1165 // } 1166 // if err = proc.Mp.Gm.Alloc(int64(cap(col.Data))); err != nil { 1167 // return nil, err 1168 // } 1169 // vec := vector.New(rv.Typ) 1170 // if lv.IsScalar() { 1171 // vec.IsConst = true 1172 // } 1173 // vec.Data = col.Data 1174 // nulls.Set(vec.Nsp, lv.Nsp) 1175 // vector.SetCol(vec, col) 1176 // return vec, nil 1177 // } 1178 1179 // CastSpecials3 : Cast converts string to string ,Contains the following: 1180 // char -> char 1181 // char -> varhcar 1182 // char -> blob 1183 // varchar -> char 1184 // varchar -> varhcar 1185 // varchar -> blob 1186 // blob -> char 1187 // blob -> varchar 1188 // blob -> blob 1189 // we need to consider the visiblity of 0xXXXX, the rule is a little complex, 1190 // please do that in the future 1191 // the rule is, if src string len is larger than the dest string len, report an error 1192 // for example: select cast('aaaaaaa' as char(1)); will report an error here. 1193 // insert into col(varchar(1) values 'aaaaa', report an error 1194 // for other cases, where col(varchar(1))='aaaaa', do not report error, just return empty result. maybe we can optimize this to false? 1195 // sometimes, the dest len is 0, then do not report error here. maybe a bug and need to fix in the future? 1196 func CastSpecials3(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1197 source := vector.MustStrCols(lv) 1198 if lv.IsScalar() { 1199 if lv.IsScalarNull() { 1200 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1201 } 1202 if rv.Typ.Oid != types.T_text && int(rv.Typ.Width) != 0 && len(source[0]) > int(rv.Typ.Width) { 1203 errInfo := fmt.Sprintf(" Src length %v is larger than Dest length %v", len(source[0]), rv.Typ.Width) 1204 return nil, formatCastError(proc.Ctx, lv, rv.Typ, errInfo) 1205 } 1206 return vector.NewConstString(rv.Typ, lv.Length(), source[0], proc.Mp()), nil 1207 } 1208 destLen := int(rv.Typ.Width) 1209 if rv.Typ.Oid != types.T_text && destLen != 0 { 1210 for i, str := range source { 1211 if nulls.Contains(lv.Nsp, uint64(i)) { 1212 continue 1213 } 1214 if len(str) > destLen { 1215 errInfo := fmt.Sprintf(" Src length %v is larger than Dest length %v", len(str), destLen) 1216 return nil, formatCastError(proc.Ctx, lv, rv.Typ, errInfo) 1217 } 1218 } 1219 } 1220 return vector.NewWithStrings(rv.Typ, source, lv.Nsp, proc.Mp()), nil 1221 } 1222 1223 func CastSpecialIntToDecimal[T constraints.Integer]( 1224 lv, rv *vector.Vector, 1225 i2d func(ctx context.Context, xs []T, rs []types.Decimal128, width, scale int32) ([]types.Decimal128, error), 1226 proc *process.Process) (*vector.Vector, error) { 1227 resultScale := int32(0) 1228 resultTyp := types.T_decimal128.ToType() 1229 resultTyp.Scale = resultScale 1230 lvs := vector.MustTCols[T](lv) 1231 1232 if lv.IsScalarNull() { 1233 return proc.AllocConstNullVector(resultTyp, lv.Length()), nil 1234 } else if lv.IsScalar() { 1235 rs := make([]types.Decimal128, 1) 1236 if _, err := i2d(proc.Ctx, lvs, rs, rv.Typ.Width, rv.Typ.Scale); err != nil { 1237 return nil, err 1238 } 1239 return vector.NewConstFixed(resultTyp, lv.Length(), rs[0], proc.Mp()), nil 1240 } 1241 1242 vec, err := proc.AllocVectorOfRows(resultTyp, int64(len(lvs)), lv.Nsp) 1243 if err != nil { 1244 return nil, err 1245 } 1246 rs := vector.MustTCols[types.Decimal128](vec) 1247 if _, err := i2d(proc.Ctx, lvs, rs, rv.Typ.Width, rv.Typ.Scale); err != nil { 1248 return nil, err 1249 } 1250 return vec, nil 1251 } 1252 1253 func CastSpecialIntToDecimal64[T constraints.Integer]( 1254 lv, rv *vector.Vector, 1255 i2d func(ctx context.Context, xs []T, rs []types.Decimal64, width, scale int32) ([]types.Decimal64, error), 1256 proc *process.Process) (*vector.Vector, error) { 1257 1258 resultScale := int32(0) 1259 resultTyp := types.T_decimal64.ToType() 1260 resultTyp.Scale = resultScale 1261 lvs := vector.MustTCols[T](lv) 1262 1263 if lv.IsScalarNull() { 1264 return proc.AllocConstNullVector(resultTyp, lv.Length()), nil 1265 } else if lv.IsScalar() { 1266 rs := make([]types.Decimal64, 1) 1267 if _, err := i2d(proc.Ctx, lvs, rs, rv.Typ.Width, rv.Typ.Scale); err != nil { 1268 return nil, err 1269 } 1270 return vector.NewConstFixed(resultTyp, lv.Length(), rs[0], proc.Mp()), nil 1271 } 1272 1273 vec, err := proc.AllocVectorOfRows(resultTyp, int64(len(lvs)), lv.Nsp) 1274 if err != nil { 1275 return nil, err 1276 } 1277 rs := vector.MustTCols[types.Decimal64](vec) 1278 if _, err := i2d(proc.Ctx, lvs, rs, rv.Typ.Width, rv.Typ.Scale); err != nil { 1279 return nil, err 1280 } 1281 return vec, nil 1282 } 1283 1284 // CastSpecials4 : Cast converts signed integer to decimal128 ,Contains the following: 1285 // (int8/int16/int32/int64) to decimal128 1286 func CastSpecials4[T constraints.Signed](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1287 return CastSpecialIntToDecimal(lv, rv, binary.IntToDecimal128[T], proc) 1288 } 1289 1290 func CastSpecials4_64[T constraints.Signed](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1291 return CastSpecialIntToDecimal64(lv, rv, binary.IntToDecimal64[T], proc) 1292 } 1293 1294 // CastSpecialu4 : Cast converts unsigned integer to decimal128 ,Contains the following: 1295 // (uint8/uint16/uint32/uint64) to decimal128 1296 func CastSpecialu4[T constraints.Unsigned](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1297 return CastSpecialIntToDecimal(lv, rv, binary.UintToDecimal128[T], proc) 1298 } 1299 1300 // XXX This is a super slow function that we need to vectorwise. 1301 func CastFloatAsDecimal128[T constraints.Float](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1302 resultType := rv.Typ 1303 vs := vector.MustTCols[T](lv) 1304 if lv.IsScalar() { 1305 srcStr := fmt.Sprintf("%f", vs[0]) 1306 vec := proc.AllocScalarVector(resultType) 1307 rs := make([]types.Decimal128, 1) 1308 decimal128, err := types.ParseStringToDecimal128(srcStr, resultType.Width, resultType.Scale, lv.GetIsBin()) 1309 if err != nil { 1310 return nil, err 1311 } 1312 rs[0] = decimal128 1313 nulls.Reset(vec.Nsp) 1314 vector.SetCol(vec, rs) 1315 return vec, nil 1316 } 1317 vec, err := proc.AllocVectorOfRows(resultType, int64(len(vs)), lv.Nsp) 1318 if err != nil { 1319 return nil, err 1320 } 1321 rs := vector.MustTCols[types.Decimal128](vec) 1322 for i := 0; i < len(vs); i++ { 1323 if nulls.Contains(lv.Nsp, uint64(i)) { 1324 continue 1325 } 1326 strValue := fmt.Sprintf("%f", vs[i]) 1327 decimal128, err2 := types.ParseStringToDecimal128(strValue, resultType.Width, resultType.Scale, lv.GetIsBin()) 1328 if err2 != nil { 1329 return nil, err2 1330 } 1331 rs[i] = decimal128 1332 } 1333 return vec, nil 1334 } 1335 1336 // XXX Super slow. 1337 func CastFloatAsDecimal64[T constraints.Float](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1338 resultType := rv.Typ 1339 vs := vector.MustTCols[T](lv) 1340 var err error 1341 if lv.IsScalar() { 1342 vec := proc.AllocScalarVector(resultType) 1343 rs := make([]types.Decimal64, 1) 1344 rs[0], err = types.Decimal64_FromFloat64(float64(vs[0]), resultType.Width, resultType.Scale) 1345 if err != nil { 1346 return nil, err 1347 } 1348 nulls.Reset(vec.Nsp) 1349 vector.SetCol(vec, rs) 1350 return vec, nil 1351 } 1352 1353 vec, err := proc.AllocVectorOfRows(resultType, int64(len(vs)), lv.Nsp) 1354 if err != nil { 1355 return nil, err 1356 } 1357 rs := vector.MustTCols[types.Decimal64](vec) 1358 for i := 0; i < len(vs); i++ { 1359 if nulls.Contains(lv.Nsp, uint64(i)) { 1360 continue 1361 } 1362 rs[i], err = types.Decimal64_FromFloat64(float64(vs[i]), resultType.Width, resultType.Scale) 1363 if err != nil { 1364 return nil, err 1365 } 1366 } 1367 return vec, nil 1368 } 1369 1370 // CastVarcharAsDate : Cast converts varchar to date type 1371 func CastVarcharAsDate(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1372 vs := vector.MustStrCols(lv) 1373 1374 if lv.IsScalar() { 1375 if lv.IsScalarNull() || len(vs[0]) == 0 { 1376 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1377 } 1378 data, err2 := types.ParseDateCast(vs[0]) 1379 if err2 != nil { 1380 return nil, err2 1381 } 1382 return vector.NewConstFixed(rv.Typ, lv.Length(), data, proc.Mp()), nil 1383 } 1384 1385 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 1386 if err != nil { 1387 return nil, err 1388 } 1389 nulls.Set(vec.Nsp, lv.Nsp) 1390 rs := vector.MustTCols[types.Date](vec) 1391 for i, str := range vs { 1392 if nulls.Contains(lv.Nsp, uint64(i)) { 1393 continue 1394 } 1395 if len(str) == 0 { 1396 nulls.Add(vec.Nsp, uint64(i)) 1397 continue 1398 } 1399 data, err2 := types.ParseDateCast(str) 1400 if err2 != nil { 1401 return nil, err2 1402 } 1403 rs[i] = data 1404 } 1405 return vec, nil 1406 } 1407 1408 func CastVarcharAsTime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1409 vs := vector.MustStrCols(lv) 1410 if lv.IsScalar() { 1411 if lv.IsScalarNull() || len(vs[0]) == 0 { 1412 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1413 } 1414 data, err2 := types.ParseTime(vs[0], rv.Typ.Precision) 1415 if err2 != nil { 1416 return nil, err2 1417 } 1418 return vector.NewConstFixed(rv.Typ, lv.Length(), data, proc.Mp()), nil 1419 } 1420 1421 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 1422 if err != nil { 1423 return nil, err 1424 } 1425 nulls.Set(vec.Nsp, lv.Nsp) 1426 rs := vector.MustTCols[types.Time](vec) 1427 for i, str := range vs { 1428 if nulls.Contains(lv.Nsp, uint64(i)) { 1429 continue 1430 } 1431 if len(str) == 0 { 1432 nulls.Add(vec.Nsp, uint64(i)) 1433 continue 1434 } 1435 data, err2 := types.ParseTime(str, rv.Typ.Precision) 1436 if err2 != nil { 1437 return nil, err2 1438 } 1439 rs[i] = data 1440 } 1441 return vec, nil 1442 } 1443 1444 // CastVarcharAsDatetime : Cast converts varchar to datetime type 1445 func CastVarcharAsDatetime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1446 vs := vector.MustStrCols(lv) 1447 1448 if lv.IsScalar() { 1449 if lv.IsScalarNull() || len(vs[0]) == 0 { 1450 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1451 } 1452 data, err2 := types.ParseDatetime(vs[0], rv.Typ.Precision) 1453 if err2 != nil { 1454 return nil, err2 1455 } 1456 return vector.NewConstFixed(rv.Typ, lv.Length(), data, proc.Mp()), nil 1457 } 1458 1459 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 1460 if err != nil { 1461 return nil, err 1462 } 1463 nulls.Set(vec.Nsp, lv.Nsp) 1464 rs := vector.MustTCols[types.Datetime](vec) 1465 for i, str := range vs { 1466 if nulls.Contains(lv.Nsp, uint64(i)) { 1467 continue 1468 } 1469 if len(str) == 0 { 1470 nulls.Add(vec.Nsp, uint64(i)) 1471 continue 1472 } 1473 data, err2 := types.ParseDatetime(str, rv.Typ.Precision) 1474 if err2 != nil { 1475 return nil, err2 1476 } 1477 rs[i] = data 1478 } 1479 return vec, nil 1480 } 1481 1482 // CastVarcharAsTimestamp : Cast converts varchar to timestamp type 1483 func CastVarcharAsTimestamp(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1484 var t *time.Location 1485 if proc == nil { 1486 t = time.Local 1487 } else { 1488 t = proc.SessionInfo.TimeZone 1489 } 1490 vs := vector.MustStrCols(lv) 1491 1492 if lv.IsScalar() { 1493 if lv.IsScalarNull() || len(vs[0]) == 0 { 1494 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1495 } 1496 data, err := types.ParseTimestamp(t, vs[0], rv.Typ.Precision) 1497 if err != nil { 1498 return nil, err 1499 } 1500 return vector.NewConstFixed(rv.Typ, lv.Length(), data, proc.Mp()), nil 1501 } 1502 1503 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 1504 if err != nil { 1505 return nil, err 1506 } 1507 nulls.Set(vec.Nsp, lv.Nsp) 1508 rs := vector.MustTCols[types.Timestamp](vec) 1509 for i, str := range vs { 1510 if nulls.Contains(lv.Nsp, uint64(i)) { 1511 continue 1512 } 1513 if len(str) == 0 { 1514 nulls.Add(vec.Nsp, uint64(i)) 1515 continue 1516 } 1517 data, err := types.ParseTimestamp(t, str, rv.Typ.Precision) 1518 if err != nil { 1519 return nil, err 1520 } 1521 rs[i] = data 1522 } 1523 return vec, nil 1524 } 1525 1526 // CastDecimal64AsDecimal128 : Cast converts decimal64 to decimal128 1527 func CastDecimal64AsDecimal128(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1528 lvScale := lv.Typ.Scale 1529 resultTyp := types.T_decimal128.ToType() 1530 resultTyp.Scale = lvScale 1531 lvs := vector.MustTCols[types.Decimal64](lv) 1532 1533 if lv.IsScalar() { 1534 vec := proc.AllocScalarVector(resultTyp) 1535 rs := make([]types.Decimal128, 1) 1536 if _, err := binary.Decimal64ToDecimal128(proc.Ctx, lvs, rs, rv.Typ.Width, rv.Typ.Scale); err != nil { 1537 return nil, err 1538 } 1539 nulls.Set(vec.Nsp, lv.Nsp) 1540 vector.SetCol(vec, rs) 1541 return vec, nil 1542 } 1543 1544 vec, err := proc.AllocVectorOfRows(resultTyp, int64(len(lvs)), lv.Nsp) 1545 if err != nil { 1546 return nil, err 1547 } 1548 rs := vector.MustTCols[types.Decimal128](vec) 1549 if _, err := binary.Decimal64ToDecimal128(proc.Ctx, lvs, rs, rv.Typ.Width, rv.Typ.Scale); err != nil { 1550 return nil, err 1551 } 1552 return vec, nil 1553 } 1554 1555 // castDateAsInt32 : Converts date to int32 1556 func castDateAsInt32(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1557 lvs := vector.MustTCols[types.Date](lv) 1558 if lv.IsScalar() { 1559 vec := proc.AllocScalarVector(rv.Typ) 1560 rs := vector.MustTCols[int32](vec) 1561 rs[0] = lvs[0].DaysSinceUnixEpoch() 1562 nulls.Set(vec.Nsp, lv.Nsp) 1563 return vec, nil 1564 } 1565 1566 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1567 if err != nil { 1568 return nil, err 1569 } 1570 rs := vector.MustTCols[int32](vec) 1571 for i := range lvs { 1572 rs[i] = lvs[i].DaysSinceUnixEpoch() 1573 } 1574 return vec, nil 1575 } 1576 1577 // castDateAsInt64 : Converts date to int64 1578 func castDateAsInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1579 lvs := vector.MustTCols[types.Date](lv) 1580 if lv.IsScalar() { 1581 vec := proc.AllocScalarVector(rv.Typ) 1582 rs := vector.MustTCols[int64](vec) 1583 rs[0] = int64(lvs[0].DaysSinceUnixEpoch()) 1584 nulls.Set(vec.Nsp, lv.Nsp) 1585 return vec, nil 1586 } 1587 1588 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1589 if err != nil { 1590 return nil, err 1591 } 1592 rs := vector.MustTCols[int64](vec) 1593 for i := range lvs { 1594 rs[i] = int64(lvs[i].DaysSinceUnixEpoch()) 1595 } 1596 return vec, nil 1597 } 1598 1599 // castDatetimeAsInt32 : Converts datetime to int32 1600 func castDatetimeAsInt32(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1601 lvs := vector.MustTCols[types.Datetime](lv) 1602 if lv.IsScalar() { 1603 vec := proc.AllocScalarVector(rv.Typ) 1604 val := lvs[0].SecsSinceUnixEpoch() 1605 if val < math.MinInt32 || val > math.MaxInt32 { 1606 return nil, moerr.NewOutOfRangeNoCtx("datetime", "value '%v'", val) 1607 } 1608 rs := vector.MustTCols[int32](vec) 1609 rs[0] = int32(val) 1610 nulls.Set(vec.Nsp, lv.Nsp) 1611 return vec, nil 1612 } 1613 1614 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1615 if err != nil { 1616 return nil, err 1617 } 1618 rs := vector.MustTCols[int64](vec) 1619 for i := range lvs { 1620 val := lvs[i].SecsSinceUnixEpoch() 1621 if val < math.MinInt32 || val > math.MaxInt32 { 1622 return nil, moerr.NewOutOfRangeNoCtx("datetime", "value '%v'", val) 1623 } 1624 rs[i] = val 1625 } 1626 return vec, nil 1627 } 1628 1629 // castDatetimeAsInt64 : Converts datetime to int64 1630 func castDatetimeAsInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1631 lvs := vector.MustTCols[types.Datetime](lv) 1632 if lv.IsScalar() { 1633 vec := proc.AllocScalarVector(rv.Typ) 1634 rs := vector.MustTCols[int64](vec) 1635 rs[0] = lvs[0].SecsSinceUnixEpoch() 1636 nulls.Set(vec.Nsp, lv.Nsp) 1637 return vec, nil 1638 } 1639 1640 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1641 if err != nil { 1642 return nil, err 1643 } 1644 rs := vector.MustTCols[int64](vec) 1645 for i := range lvs { 1646 rs[i] = lvs[i].SecsSinceUnixEpoch() 1647 } 1648 return vec, nil 1649 } 1650 1651 // castTimestampAsInt32 : Converts timestamp to int32 1652 func castTimestampAsInt32(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1653 lvs := vector.MustTCols[types.Timestamp](lv) 1654 if lv.IsScalar() { 1655 vec := proc.AllocScalarVector(rv.Typ) 1656 rs := vector.MustTCols[int64](vec) 1657 val := lvs[0].Unix() 1658 if val < math.MinInt32 || val > math.MaxInt32 { 1659 return nil, moerr.NewOutOfRangeNoCtx("timestamp", "value '%v'", val) 1660 } 1661 rs[0] = val 1662 nulls.Set(vec.Nsp, lv.Nsp) 1663 return vec, nil 1664 } 1665 1666 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1667 if err != nil { 1668 return nil, err 1669 } 1670 rs := vector.MustTCols[int64](vec) 1671 for i := range lvs { 1672 val := lvs[i].Unix() 1673 if val < math.MinInt32 || val > math.MaxInt32 { 1674 return nil, moerr.NewOutOfRangeNoCtx("timestamp", "value '%v'", val) 1675 } 1676 rs[i] = val 1677 } 1678 return vec, nil 1679 } 1680 1681 // castTimestampAsInt64 : Converts timestamp to int64 1682 func castTimestampAsInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1683 lvs := vector.MustTCols[types.Timestamp](lv) 1684 if lv.IsScalar() { 1685 vec := proc.AllocScalarVector(rv.Typ) 1686 rs := vector.MustTCols[int64](vec) 1687 rs[0] = lvs[0].Unix() 1688 nulls.Set(vec.Nsp, lv.Nsp) 1689 return vec, nil 1690 } 1691 1692 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1693 if err != nil { 1694 return nil, err 1695 } 1696 rs := vector.MustTCols[int64](vec) 1697 for i := range lvs { 1698 rs[i] = lvs[i].Unix() 1699 } 1700 return vec, nil 1701 } 1702 1703 // castTimestampAsDatetime : Cast converts timestamp to datetime decimal128 1704 func castTimestampAsDatetime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1705 var t *time.Location 1706 if proc == nil { 1707 t = time.Local 1708 } else { 1709 t = proc.SessionInfo.TimeZone 1710 } 1711 lvs := vector.MustTCols[types.Timestamp](lv) 1712 if lv.IsScalar() { 1713 vec := proc.AllocScalarVector(rv.Typ) 1714 rs := make([]types.Datetime, 1) 1715 if _, err := binary.TimestampToDatetime(proc.Ctx, t, lvs, rs); err != nil { 1716 return nil, err 1717 } 1718 nulls.Set(vec.Nsp, lv.Nsp) 1719 vector.SetCol(vec, rs) 1720 return vec, nil 1721 } 1722 1723 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1724 if err != nil { 1725 return nil, err 1726 } 1727 rs := vector.MustTCols[types.Datetime](vec) 1728 if _, err := binary.TimestampToDatetime(proc.Ctx, t, lvs, rs); err != nil { 1729 return nil, err 1730 } 1731 return vec, nil 1732 } 1733 1734 // castTimestampAsVarchar : Cast converts timestamp to varchar 1735 func castTimestampAsVarchar(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1736 var t *time.Location 1737 if proc == nil { 1738 t = time.Local 1739 } else { 1740 t = proc.SessionInfo.TimeZone 1741 } 1742 lvs := vector.MustTCols[types.Timestamp](lv) 1743 resultType := rv.Typ 1744 precision := lv.Typ.Precision 1745 if lv.IsScalar() { 1746 if lv.IsScalarNull() { 1747 return proc.AllocConstNullVector(resultType, lv.Length()), nil 1748 } 1749 rs := make([]string, 1) 1750 if _, err := binary.TimestampToVarchar(proc.Ctx, t, lvs, rs, precision); err != nil { 1751 return nil, err 1752 } 1753 return vector.NewConstString(resultType, lv.Length(), rs[0], proc.Mp()), nil 1754 } 1755 1756 rs := make([]string, len(lvs)) 1757 if _, err := binary.TimestampToVarchar(proc.Ctx, t, lvs, rs, precision); err != nil { 1758 return nil, err 1759 } 1760 return vector.NewWithStrings(resultType, rs, lv.Nsp, proc.Mp()), nil 1761 } 1762 1763 // CastStringAsDecimal64 : onverts char/varchar/text as decimal64 1764 func CastStringAsDecimal64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1765 vs := vector.MustStrCols(lv) 1766 if lv.IsScalar() { 1767 if lv.IsScalarNull() { 1768 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1769 } 1770 decimal64, err := types.ParseStringToDecimal64(vs[0], rv.Typ.Width, rv.Typ.Scale, lv.GetIsBin()) 1771 if err != nil { 1772 return nil, err 1773 } 1774 return vector.NewConstFixed(rv.Typ, lv.Length(), decimal64, proc.Mp()), nil 1775 } 1776 1777 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 1778 if err != nil { 1779 return nil, err 1780 } 1781 rs := vector.MustTCols[types.Decimal64](vec) 1782 for i, str := range vs { 1783 if nulls.Contains(lv.Nsp, uint64(i)) { 1784 continue 1785 } 1786 decimal64, err2 := types.ParseStringToDecimal64(str, rv.Typ.Width, rv.Typ.Scale, lv.GetIsBin()) 1787 if err2 != nil { 1788 return nil, err2 1789 } 1790 rs[i] = decimal64 1791 } 1792 return vec, nil 1793 } 1794 1795 func CastBoolToString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1796 var err error 1797 if lv.IsScalarNull() { 1798 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1799 } 1800 1801 lvs := vector.MustTCols[bool](lv) 1802 col := make([]string, len(lvs)) 1803 if lv.IsScalar() { 1804 binary.BoolToBytes(proc.Ctx, lvs, col) 1805 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 1806 } 1807 1808 if _, err = binary.BoolToBytes(proc.Ctx, lvs, col); err != nil { 1809 return nil, err 1810 } 1811 1812 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 1813 } 1814 1815 func CastDateAsString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1816 var err error 1817 1818 if lv.IsScalarNull() { 1819 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1820 } 1821 1822 lvs := vector.MustTCols[types.Date](lv) 1823 col := make([]string, len(lvs)) 1824 if lv.IsScalar() { 1825 binary.DateToBytes(proc.Ctx, lvs, col) 1826 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 1827 } 1828 1829 // XXX All these binary functions should take null.Nulls as input 1830 if col, err = binary.DateToBytes(proc.Ctx, lvs, col); err != nil { 1831 return nil, err 1832 } 1833 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 1834 } 1835 1836 func CastDateAsDatetime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1837 lvs := vector.MustTCols[types.Date](lv) 1838 if lv.IsScalar() { 1839 if lv.IsScalarNull() { 1840 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1841 } 1842 rs := make([]types.Datetime, 1) 1843 if _, err := binary.DateToDatetime(proc.Ctx, lvs, rs); err != nil { 1844 return nil, err 1845 } 1846 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 1847 } 1848 1849 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1850 if err != nil { 1851 return nil, err 1852 } 1853 rs := vector.MustTCols[types.Datetime](vec) 1854 if _, err := binary.DateToDatetime(proc.Ctx, lvs, rs); err != nil { 1855 return nil, err 1856 } 1857 return vec, nil 1858 } 1859 1860 // XXX I felt I have written this ten times, what is going on? 1861 // CastStringAsDecimal128 : onverts char/varchar as decimal128 1862 func CastStringAsDecimal128(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1863 vs := vector.MustStrCols(lv) 1864 if lv.IsScalar() { 1865 if lv.IsScalarNull() { 1866 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1867 } 1868 decimal128, err := types.ParseStringToDecimal128(vs[0], rv.Typ.Width, rv.Typ.Scale, lv.GetIsBin()) 1869 if err != nil { 1870 return nil, err 1871 } 1872 return vector.NewConstFixed(rv.Typ, lv.Length(), decimal128, proc.Mp()), nil 1873 } 1874 1875 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 1876 if err != nil { 1877 return nil, err 1878 } 1879 rs := vector.MustTCols[types.Decimal128](vec) 1880 for i := range vs { 1881 if nulls.Contains(lv.Nsp, uint64(i)) { 1882 continue 1883 } 1884 decimal128, err2 := types.ParseStringToDecimal128(vs[i], rv.Typ.Width, rv.Typ.Scale, lv.GetIsBin()) 1885 if err2 != nil { 1886 return nil, err2 1887 } 1888 rs[i] = decimal128 1889 } 1890 return vec, nil 1891 } 1892 1893 // CastDatetimeAsTimestamp : Cast converts datetime to timestamp 1894 func CastDatetimeAsTimestamp(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1895 var t *time.Location 1896 if proc == nil { 1897 t = time.Local 1898 } else { 1899 t = proc.SessionInfo.TimeZone 1900 } 1901 lvs := vector.MustTCols[types.Datetime](lv) 1902 if lv.IsScalar() { 1903 if lv.IsScalarNull() { 1904 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1905 } 1906 rs := make([]types.Timestamp, 1) 1907 timestamp.DatetimeToTimestamp(t, lvs, lv.Nsp, rs) 1908 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 1909 } 1910 1911 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1912 if err != nil { 1913 return nil, err 1914 } 1915 rs := vector.MustTCols[types.Timestamp](vec) 1916 timestamp.DatetimeToTimestamp(t, lvs, lv.Nsp, rs) 1917 return vec, nil 1918 } 1919 1920 // CastDateAsTimestamp : Cast converts date to timestamp 1921 func CastDateAsTimestamp(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1922 var t *time.Location 1923 if proc == nil { 1924 t = time.Local 1925 } else { 1926 t = proc.SessionInfo.TimeZone 1927 } 1928 lvs := vector.MustTCols[types.Date](lv) 1929 if lv.IsScalar() { 1930 if lv.IsScalarNull() { 1931 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1932 } 1933 rs := make([]types.Timestamp, 1) 1934 timestamp.DateToTimestamp(t, lvs, lv.Nsp, rs) 1935 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 1936 } 1937 1938 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1939 if err != nil { 1940 return nil, err 1941 } 1942 rs := vector.MustTCols[types.Timestamp](vec) 1943 timestamp.DateToTimestamp(t, lvs, lv.Nsp, rs) 1944 return vec, nil 1945 } 1946 1947 func CastDatetimeAsString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1948 var err error 1949 1950 lvs := vector.MustTCols[types.Datetime](lv) 1951 col := make([]string, len(lvs)) 1952 if lv.IsScalar() { 1953 if lv.IsScalarNull() { 1954 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 1955 } 1956 if col, err = binary.DatetimeToBytes(proc.Ctx, lvs, col, lv.Typ.Precision); err != nil { 1957 return nil, err 1958 } 1959 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 1960 } 1961 1962 if col, err = binary.DatetimeToBytes(proc.Ctx, lvs, col, lv.Typ.Precision); err != nil { 1963 return nil, err 1964 } 1965 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 1966 } 1967 1968 // CastDatetimeAsDate : convert datetime to date 1969 // DateTime : high 44 bits stands for the seconds passed by, low 20 bits stands for the microseconds passed by 1970 func CastDatetimeAsDate(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1971 lvs := vector.MustTCols[types.Datetime](lv) 1972 if lv.IsScalar() { 1973 vec := proc.AllocScalarVector(rv.Typ) 1974 rs := make([]types.Date, 1) 1975 if _, err := binary.DatetimeToDate(proc.Ctx, lvs, rs); err != nil { 1976 return nil, err 1977 } 1978 nulls.Set(vec.Nsp, lv.Nsp) 1979 vector.SetCol(vec, rs) 1980 return vec, nil 1981 } 1982 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 1983 if err != nil { 1984 return nil, err 1985 } 1986 rs := vector.MustTCols[types.Date](vec) 1987 if _, err := binary.DatetimeToDate(proc.Ctx, lvs, rs); err != nil { 1988 return nil, err 1989 } 1990 return vec, nil 1991 } 1992 1993 func CastIntAsTimestamp[T constraints.Signed](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 1994 lvs := vector.MustTCols[T](lv) 1995 if lv.IsScalar() { 1996 vec := proc.AllocScalarVector(rv.Typ) 1997 rs := make([]types.Timestamp, 1) 1998 if lvs[0] < 0 || int64(lvs[0]) > 32536771199 { 1999 nulls.Add(lv.Nsp, 0) 2000 } 2001 if _, err := binary.NumericToTimestamp(lvs, rs); err != nil { 2002 return nil, err 2003 } 2004 nulls.Set(vec.Nsp, lv.Nsp) 2005 vector.SetCol(vec, rs) 2006 return vec, nil 2007 } 2008 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2009 if err != nil { 2010 return nil, err 2011 } 2012 rs := vector.MustTCols[types.Timestamp](vec) 2013 // XXX This is simply WRONG. 2014 for i := 0; i < len(lvs); i++ { 2015 if lvs[i] < 0 || int64(lvs[i]) > 32536771199 { 2016 nulls.Add(vec.Nsp, uint64(i)) 2017 } 2018 } 2019 if _, err := binary.NumericToTimestamp(lvs, rs); err != nil { 2020 return nil, err 2021 } 2022 return vec, nil 2023 } 2024 2025 func CastUIntAsTimestamp[T constraints.Unsigned](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2026 lvs := vector.MustTCols[T](lv) 2027 if lv.IsScalar() { 2028 vec := proc.AllocScalarVector(rv.Typ) 2029 rs := make([]types.Timestamp, 1) 2030 if lvs[0] < 0 || uint64(lvs[0]) > 32536771199 { 2031 nulls.Add(lv.Nsp, 0) 2032 } 2033 if _, err := binary.NumericToTimestamp(lvs, rs); err != nil { 2034 return nil, err 2035 } 2036 nulls.Set(vec.Nsp, lv.Nsp) 2037 vector.SetCol(vec, rs) 2038 return vec, nil 2039 } 2040 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2041 if err != nil { 2042 return nil, err 2043 } 2044 rs := vector.MustTCols[types.Timestamp](vec) 2045 // XXX Again, simply WRONG. 2046 for i := 0; i < len(lvs); i++ { 2047 if lvs[i] < 0 || uint64(lvs[i]) > 32536771199 { 2048 nulls.Add(vec.Nsp, uint64(i)) 2049 } 2050 } 2051 if _, err := binary.NumericToTimestamp(lvs, rs); err != nil { 2052 return nil, err 2053 } 2054 return vec, nil 2055 } 2056 2057 func CastDecimal64AsTimestamp(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2058 lvs := vector.MustTCols[types.Decimal64](lv) 2059 if lv.IsScalar() { 2060 vec := proc.AllocScalarVector(rv.Typ) 2061 rs := make([]types.Timestamp, 1) 2062 if _, err := binary.Decimal64ToTimestamp(lvs, lv.Typ.Precision, lv.Typ.Scale, rs); err != nil { 2063 return nil, err 2064 } 2065 nulls.Set(vec.Nsp, lv.Nsp) 2066 vector.SetCol(vec, rs) 2067 return vec, nil 2068 } 2069 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2070 if err != nil { 2071 return nil, err 2072 } 2073 rs := vector.MustTCols[types.Timestamp](vec) 2074 if _, err := binary.Decimal64ToTimestamp(lvs, lv.Typ.Precision, lv.Typ.Scale, rs); err != nil { 2075 return nil, err 2076 } 2077 return vec, nil 2078 } 2079 2080 func CastDecimal128AsTimestamp(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2081 lvs := vector.MustTCols[types.Decimal128](lv) 2082 if lv.IsScalar() { 2083 vec := proc.AllocScalarVector(rv.Typ) 2084 rs := make([]types.Timestamp, 1) 2085 if _, err := binary.Decimal128ToTimestamp(lvs, lv.Typ.Precision, lv.Typ.Scale, rs); err != nil { 2086 return nil, err 2087 } 2088 nulls.Set(vec.Nsp, lv.Nsp) 2089 vector.SetCol(vec, rs) 2090 return vec, nil 2091 } 2092 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2093 if err != nil { 2094 return nil, err 2095 } 2096 rs := vector.MustTCols[types.Timestamp](vec) 2097 if _, err := binary.Decimal128ToTimestamp(lvs, lv.Typ.Precision, lv.Typ.Scale, rs); err != nil { 2098 return nil, err 2099 } 2100 return vec, nil 2101 } 2102 2103 func CastTimeAsString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2104 var err error 2105 2106 lvs := vector.MustTCols[types.Time](lv) 2107 col := make([]string, len(lvs)) 2108 if lv.IsScalar() { 2109 if lv.IsScalarNull() { 2110 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2111 } 2112 if col, err = binary.TimeToBytes(proc.Ctx, lvs, col, lv.Typ.Precision); err != nil { 2113 return nil, err 2114 } 2115 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 2116 } 2117 2118 if col, err = binary.TimeToBytes(proc.Ctx, lvs, col, lv.Typ.Precision); err != nil { 2119 return nil, err 2120 } 2121 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 2122 } 2123 2124 func CastTimeAsDate(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2125 lvs := vector.MustTCols[types.Time](lv) 2126 if lv.IsScalar() { 2127 if lv.IsScalarNull() { 2128 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2129 } 2130 rs := make([]types.Date, 1) 2131 if _, err := binary.TimeToDate(proc.Ctx, lvs, rs); err != nil { 2132 return nil, err 2133 } 2134 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 2135 } 2136 2137 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2138 if err != nil { 2139 return nil, err 2140 } 2141 rs := vector.MustTCols[types.Date](vec) 2142 if _, err := binary.TimeToDate(proc.Ctx, lvs, rs); err != nil { 2143 return nil, err 2144 } 2145 return vec, nil 2146 } 2147 2148 func CastDateAsTime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2149 lvs := vector.MustTCols[types.Date](lv) 2150 if lv.IsScalar() { 2151 if lv.IsScalarNull() { 2152 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2153 } 2154 rs := make([]types.Time, 1) 2155 if _, err := binary.DateToTime(proc.Ctx, lvs, rs); err != nil { 2156 return nil, err 2157 } 2158 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 2159 } 2160 2161 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2162 if err != nil { 2163 return nil, err 2164 } 2165 rs := vector.MustTCols[types.Time](vec) 2166 if _, err := binary.DateToTime(proc.Ctx, lvs, rs); err != nil { 2167 return nil, err 2168 } 2169 return vec, nil 2170 } 2171 2172 func CastTimeAsDatetime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2173 lvs := vector.MustTCols[types.Time](lv) 2174 if lv.IsScalar() { 2175 if lv.IsScalarNull() { 2176 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2177 } 2178 rs := make([]types.Datetime, 1) 2179 if _, err := binary.TimeToDatetime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2180 return nil, err 2181 } 2182 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 2183 } 2184 2185 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2186 if err != nil { 2187 return nil, err 2188 } 2189 rs := vector.MustTCols[types.Datetime](vec) 2190 if _, err := binary.TimeToDatetime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2191 return nil, err 2192 } 2193 return vec, nil 2194 } 2195 2196 func CastDatetimeAsTime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2197 lvs := vector.MustTCols[types.Datetime](lv) 2198 precision := rv.Typ.Precision 2199 if lv.IsScalar() { 2200 if lv.IsScalarNull() { 2201 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2202 } 2203 rs := make([]types.Time, 1) 2204 if _, err := binary.DatetimeToTime(proc.Ctx, lvs, rs, precision); err != nil { 2205 return nil, err 2206 } 2207 return vector.NewConstFixed(rv.Typ, lv.Length(), rs[0], proc.Mp()), nil 2208 } 2209 2210 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2211 if err != nil { 2212 return nil, err 2213 } 2214 rs := vector.MustTCols[types.Time](vec) 2215 if _, err := binary.DatetimeToTime(proc.Ctx, lvs, rs, precision); err != nil { 2216 return nil, err 2217 } 2218 return vec, nil 2219 } 2220 2221 func CastIntAsTime[T constraints.Signed](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2222 lvs := vector.MustTCols[T](lv) 2223 if lv.IsScalar() { 2224 vec := proc.AllocScalarVector(rv.Typ) 2225 rs := make([]types.Time, 1) 2226 if int64(lvs[0]) < types.MinInputIntTime || int64(lvs[0]) > types.MaxInputIntTime { 2227 nulls.Add(lv.Nsp, 0) 2228 } 2229 if _, err := binary.NumericToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2230 return nil, err 2231 } 2232 nulls.Set(vec.Nsp, lv.Nsp) 2233 vector.SetCol(vec, rs) 2234 return vec, nil 2235 } 2236 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2237 if err != nil { 2238 return nil, err 2239 } 2240 rs := vector.MustTCols[types.Time](vec) 2241 for i := 0; i < len(lvs); i++ { 2242 if int64(lvs[0]) < types.MinInputIntTime || int64(lvs[0]) > types.MaxInputIntTime { 2243 nulls.Add(vec.Nsp, uint64(i)) 2244 } 2245 } 2246 if _, err := binary.NumericToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2247 return nil, err 2248 } 2249 return vec, nil 2250 } 2251 2252 func CastUIntAsTime[T constraints.Unsigned](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2253 lvs := vector.MustTCols[T](lv) 2254 if lv.IsScalar() { 2255 vec := proc.AllocScalarVector(rv.Typ) 2256 rs := make([]types.Time, 1) 2257 if uint64(lvs[0]) > types.MaxInputIntTime { 2258 nulls.Add(lv.Nsp, 0) 2259 } 2260 if _, err := binary.NumericToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2261 return nil, err 2262 } 2263 nulls.Set(vec.Nsp, lv.Nsp) 2264 vector.SetCol(vec, rs) 2265 return vec, nil 2266 } 2267 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2268 if err != nil { 2269 return nil, err 2270 } 2271 rs := vector.MustTCols[types.Time](vec) 2272 for i := 0; i < len(lvs); i++ { 2273 if uint64(lvs[0]) > types.MaxInputIntTime { 2274 nulls.Add(vec.Nsp, uint64(i)) 2275 } 2276 } 2277 if _, err := binary.NumericToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2278 return nil, err 2279 } 2280 return vec, nil 2281 } 2282 2283 func CastDecimal64AsTime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2284 lvs := vector.MustTCols[types.Decimal64](lv) 2285 if lv.IsScalar() { 2286 vec := proc.AllocScalarVector(rv.Typ) 2287 rs := make([]types.Time, 1) 2288 if _, err := binary.Decimal64ToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2289 return nil, err 2290 } 2291 nulls.Set(vec.Nsp, lv.Nsp) 2292 vector.SetCol(vec, rs) 2293 return vec, nil 2294 } 2295 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2296 if err != nil { 2297 return nil, err 2298 } 2299 rs := vector.MustTCols[types.Time](vec) 2300 if _, err := binary.Decimal64ToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2301 return nil, err 2302 } 2303 return vec, nil 2304 } 2305 2306 func CastDecimal128AsTime(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2307 lvs := vector.MustTCols[types.Decimal128](lv) 2308 if lv.IsScalar() { 2309 vec := proc.AllocScalarVector(rv.Typ) 2310 rs := make([]types.Time, 1) 2311 if _, err := binary.Decimal128ToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2312 return nil, err 2313 } 2314 nulls.Set(vec.Nsp, lv.Nsp) 2315 vector.SetCol(vec, rs) 2316 return vec, nil 2317 } 2318 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2319 if err != nil { 2320 return nil, err 2321 } 2322 rs := vector.MustTCols[types.Time](vec) 2323 if _, err := binary.Decimal128ToTime(proc.Ctx, lvs, rs, rv.Typ.Precision); err != nil { 2324 return nil, err 2325 } 2326 return vec, nil 2327 } 2328 2329 func CastTimeAsNumeric[T constraints.Integer](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2330 lvs := vector.MustTCols[types.Time](lv) 2331 if lv.IsScalar() { 2332 vec := proc.AllocScalarVector(rv.Typ) 2333 rs := make([]T, 1) 2334 if _, err := binary.TimeToNumeric(proc.Ctx, lvs, rs); err != nil { 2335 return nil, err 2336 } 2337 nulls.Set(vec.Nsp, lv.Nsp) 2338 vector.SetCol(vec, rs) 2339 return vec, nil 2340 } 2341 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2342 if err != nil { 2343 return nil, err 2344 } 2345 rs := vector.MustTCols[T](vec) 2346 if _, err := binary.TimeToNumeric(proc.Ctx, lvs, rs); err != nil { 2347 return nil, err 2348 } 2349 return vec, nil 2350 } 2351 2352 func CastTimeAsDecimal64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2353 lvs := vector.MustTCols[types.Time](lv) 2354 if lv.IsScalar() { 2355 vec := proc.AllocScalarVector(rv.Typ) 2356 rs := make([]types.Decimal64, 1) 2357 if _, err := binary.TimeToDecimal64(proc.Ctx, lvs, rs, rv.Typ.Width, lv.Typ.Precision); err != nil { 2358 return nil, err 2359 } 2360 nulls.Set(vec.Nsp, lv.Nsp) 2361 vector.SetCol(vec, rs) 2362 return vec, nil 2363 } 2364 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2365 if err != nil { 2366 return nil, err 2367 } 2368 rs := vector.MustTCols[types.Decimal64](vec) 2369 if _, err := binary.TimeToDecimal64(proc.Ctx, lvs, rs, rv.Typ.Width, lv.Typ.Precision); err != nil { 2370 return nil, err 2371 } 2372 return vec, nil 2373 } 2374 2375 func CastTimeAsDecimal128(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2376 lvs := vector.MustTCols[types.Time](lv) 2377 if lv.IsScalar() { 2378 vec := proc.AllocScalarVector(rv.Typ) 2379 rs := make([]types.Decimal128, 1) 2380 if _, err := binary.TimeToDecimal128(proc.Ctx, lvs, rs, rv.Typ.Width, lv.Typ.Precision); err != nil { 2381 return nil, err 2382 } 2383 nulls.Set(vec.Nsp, lv.Nsp) 2384 vector.SetCol(vec, rs) 2385 return vec, nil 2386 } 2387 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2388 if err != nil { 2389 return nil, err 2390 } 2391 rs := vector.MustTCols[types.Decimal128](vec) 2392 if _, err := binary.TimeToDecimal128(proc.Ctx, lvs, rs, rv.Typ.Width, lv.Typ.Precision); err != nil { 2393 return nil, err 2394 } 2395 return vec, nil 2396 } 2397 2398 func CastDecimal64ToFloat32(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2399 lvs := lv.Col.([]types.Decimal64) 2400 if lv.IsScalar() { 2401 vec := proc.AllocScalarVector(rv.Typ) 2402 rs := make([]float32, 1) 2403 if _, err := binary.Decimal64ToFloat32(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2404 return nil, err 2405 } 2406 nulls.Set(vec.Nsp, lv.Nsp) 2407 vector.SetCol(vec, rs) 2408 return vec, nil 2409 } 2410 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2411 if err != nil { 2412 return nil, err 2413 } 2414 rs := vector.MustTCols[float32](vec) 2415 if _, err := binary.Decimal64ToFloat32(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2416 return nil, err 2417 } 2418 return vec, nil 2419 } 2420 2421 func CastDecimal128ToFloat32(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2422 lvs := lv.Col.([]types.Decimal128) 2423 if lv.IsScalar() { 2424 vec := proc.AllocScalarVector(rv.Typ) 2425 rs := make([]float32, 1) 2426 if _, err := binary.Decimal128ToFloat32(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2427 return nil, err 2428 } 2429 nulls.Set(vec.Nsp, lv.Nsp) 2430 vector.SetCol(vec, rs) 2431 return vec, nil 2432 } 2433 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2434 if err != nil { 2435 return nil, err 2436 } 2437 rs := vector.MustTCols[float32](vec) 2438 if _, err := binary.Decimal128ToFloat32(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2439 return nil, err 2440 } 2441 return vec, nil 2442 } 2443 2444 func CastDecimal64ToFloat64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2445 lvs := lv.Col.([]types.Decimal64) 2446 if lv.IsScalar() { 2447 vec := proc.AllocScalarVector(rv.Typ) 2448 rs := make([]float64, 1) 2449 if _, err := binary.Decimal64ToFloat64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2450 return nil, err 2451 } 2452 nulls.Set(vec.Nsp, lv.Nsp) 2453 vector.SetCol(vec, rs) 2454 return vec, nil 2455 } 2456 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2457 if err != nil { 2458 return nil, err 2459 } 2460 rs := vector.MustTCols[float64](vec) 2461 if _, err := binary.Decimal64ToFloat64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2462 return nil, err 2463 } 2464 return vec, nil 2465 } 2466 2467 func CastDecimal128ToFloat64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2468 lvs := lv.Col.([]types.Decimal128) 2469 if lv.IsScalar() { 2470 vec := proc.AllocScalarVector(rv.Typ) 2471 rs := make([]float64, 1) 2472 if _, err := binary.Decimal128ToFloat64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2473 return nil, err 2474 } 2475 nulls.Set(vec.Nsp, lv.Nsp) 2476 vector.SetCol(vec, rs) 2477 return vec, nil 2478 } 2479 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2480 if err != nil { 2481 return nil, err 2482 } 2483 rs := vector.MustTCols[float64](vec) 2484 if _, err := binary.Decimal128ToFloat64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2485 return nil, err 2486 } 2487 return vec, nil 2488 } 2489 2490 func CastDecimal64ToUint64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2491 lvs := lv.Col.([]types.Decimal64) 2492 if lv.IsScalar() { 2493 vec := proc.AllocScalarVector(rv.Typ) 2494 rs := make([]uint64, 1) 2495 if _, err := binary.Decimal64ToUint64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2496 return nil, err 2497 } 2498 nulls.Set(vec.Nsp, lv.Nsp) 2499 vector.SetCol(vec, rs) 2500 return vec, nil 2501 } 2502 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2503 if err != nil { 2504 return nil, err 2505 } 2506 rs := vector.MustTCols[uint64](vec) 2507 if _, err := binary.Decimal64ToUint64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2508 return nil, err 2509 } 2510 return vec, nil 2511 } 2512 2513 func CastDecimal128ToUint64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2514 lvs := lv.Col.([]types.Decimal128) 2515 if lv.IsScalar() { 2516 vec := proc.AllocScalarVector(rv.Typ) 2517 rs := make([]uint64, 1) 2518 if _, err := binary.Decimal128ToUint64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2519 return nil, err 2520 } 2521 nulls.Set(vec.Nsp, lv.Nsp) 2522 vector.SetCol(vec, rs) 2523 return vec, nil 2524 } 2525 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2526 if err != nil { 2527 return nil, err 2528 } 2529 rs := vector.MustTCols[uint64](vec) 2530 if _, err := binary.Decimal128ToUint64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2531 return nil, err 2532 } 2533 return vec, nil 2534 } 2535 2536 // this cast function is too slow, and therefore only temporary, rewrite needed 2537 func CastDecimal128ToDecimal64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2538 if lv.Typ.Scale > 18 { 2539 return nil, formatCastError(proc.Ctx, lv, rv.Typ, "") 2540 } 2541 lvs := lv.Col.([]types.Decimal128) 2542 if lv.IsScalar() { 2543 vec := proc.AllocScalarVector(rv.Typ) 2544 rs := make([]types.Decimal64, 1) 2545 if _, err := binary.Decimal128ToDecimal64(proc.Ctx, lvs, rv.Typ.Width, rv.Typ.Scale, rs); err != nil { 2546 return nil, err 2547 } 2548 nulls.Set(vec.Nsp, lv.Nsp) 2549 vector.SetCol(vec, rs) 2550 return vec, nil 2551 } 2552 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2553 if err != nil { 2554 return nil, err 2555 } 2556 rs := vector.MustTCols[types.Decimal64](vec) 2557 if _, err := binary.Decimal128ToDecimal64(proc.Ctx, lvs, rv.Typ.Width, rv.Typ.Scale, rs); err != nil { 2558 return nil, err 2559 } 2560 return vec, nil 2561 } 2562 2563 func CastDecimal64ToInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2564 lvs := lv.Col.([]types.Decimal64) 2565 if lv.IsScalar() { 2566 vec := proc.AllocScalarVector(rv.Typ) 2567 rs := make([]int64, 1) 2568 if _, err := binary.Decimal64ToInt64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2569 return nil, err 2570 } 2571 nulls.Set(vec.Nsp, lv.Nsp) 2572 vector.SetCol(vec, rs) 2573 return vec, nil 2574 } 2575 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2576 if err != nil { 2577 return nil, err 2578 } 2579 rs := vector.MustTCols[int64](vec) 2580 if _, err := binary.Decimal64ToInt64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2581 return nil, err 2582 } 2583 return vec, nil 2584 } 2585 2586 func CastDecimal128ToInt64(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2587 lvs := lv.Col.([]types.Decimal128) 2588 if lv.IsScalar() { 2589 vec := proc.AllocScalarVector(rv.Typ) 2590 rs := make([]int64, 1) 2591 if _, err := binary.Decimal128ToInt64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2592 return nil, err 2593 } 2594 nulls.Set(vec.Nsp, lv.Nsp) 2595 vector.SetCol(vec, rs) 2596 return vec, nil 2597 } 2598 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2599 if err != nil { 2600 return nil, err 2601 } 2602 rs := vector.MustTCols[int64](vec) 2603 if _, err := binary.Decimal128ToInt64(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2604 return nil, err 2605 } 2606 return vec, nil 2607 } 2608 2609 func CastDecimal128ToInt32(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2610 lvs := lv.Col.([]types.Decimal128) 2611 if lv.IsScalar() { 2612 vec := proc.AllocScalarVector(rv.Typ) 2613 rs := make([]int32, 1) 2614 if _, err := binary.Decimal128ToInt32(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2615 return nil, err 2616 } 2617 nulls.Set(vec.Nsp, lv.Nsp) 2618 vector.SetCol(vec, rs) 2619 return vec, nil 2620 } 2621 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2622 if err != nil { 2623 return nil, err 2624 } 2625 rs := vector.MustTCols[int32](vec) 2626 if _, err := binary.Decimal128ToInt32(proc.Ctx, lvs, lv.Typ.Scale, rs); err != nil { 2627 return nil, err 2628 } 2629 return vec, nil 2630 } 2631 2632 func CastNumValToBool[T constraints.Integer | constraints.Float](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2633 lvs := vector.MustTCols[T](lv) 2634 if lv.IsScalar() { 2635 vec := proc.AllocScalarVector(rv.Typ) 2636 rs := make([]bool, 1) 2637 if _, err := binary.NumericToBool(lvs, rs); err != nil { 2638 return nil, err 2639 } 2640 nulls.Set(vec.Nsp, lv.Nsp) 2641 vector.SetCol(vec, rs) 2642 return vec, nil 2643 } 2644 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2645 if err != nil { 2646 return nil, err 2647 } 2648 rs := vector.MustTCols[bool](vec) 2649 if _, err := binary.NumericToBool(lvs, rs); err != nil { 2650 return nil, err 2651 } 2652 return vec, nil 2653 } 2654 2655 func CastBoolToNumeric[T constraints.Integer](lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2656 lvs := vector.MustTCols[bool](lv) 2657 if lv.IsScalar() { 2658 vec := proc.AllocScalarVector(rv.Typ) 2659 rs := make([]T, 1) 2660 if _, err := binary.BoolToNumeric(lvs, rs); err != nil { 2661 return nil, err 2662 } 2663 nulls.Set(vec.Nsp, lv.Nsp) 2664 vector.SetCol(vec, rs) 2665 return vec, nil 2666 } 2667 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(lvs)), lv.Nsp) 2668 if err != nil { 2669 return nil, err 2670 } 2671 rs := vector.MustTCols[T](vec) 2672 if _, err := binary.BoolToNumeric(lvs, rs); err != nil { 2673 return nil, err 2674 } 2675 return vec, nil 2676 } 2677 2678 func CastStringToJson(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2679 vs := vector.MustStrCols(lv) 2680 if lv.IsScalar() { 2681 if lv.IsScalarNull() { 2682 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2683 } 2684 2685 json, err := types.ParseStringToByteJson(vs[0]) 2686 if err != nil { 2687 return nil, err 2688 } 2689 val, err := types.EncodeJson(json) 2690 if err != nil { 2691 return nil, err 2692 } 2693 return vector.NewConstBytes(rv.Typ, lv.Length(), val, proc.Mp()), nil 2694 } 2695 2696 col := make([][]byte, len(vs)) 2697 for i, str := range vs { 2698 if nulls.Contains(lv.Nsp, uint64(i)) { 2699 continue 2700 } 2701 2702 json, err := types.ParseStringToByteJson(str) 2703 if err != nil { 2704 return nil, err 2705 } 2706 val, err := types.EncodeJson(json) 2707 if err != nil { 2708 return nil, err 2709 } 2710 col[i] = val 2711 } 2712 return vector.NewWithBytes(rv.Typ, col, lv.Nsp, proc.Mp()), nil 2713 } 2714 2715 func CastJsonToString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2716 vs := vector.MustBytesCols(lv) 2717 if lv.IsScalar() { 2718 if lv.IsScalarNull() { 2719 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2720 } 2721 2722 bj := types.DecodeJson(vs[0]) 2723 val, err := bj.MarshalJSON() 2724 if err != nil { 2725 return nil, err 2726 } 2727 return vector.NewConstBytes(rv.Typ, lv.Length(), val, proc.Mp()), nil 2728 } 2729 2730 col := make([][]byte, len(vs)) 2731 for i, v := range vs { 2732 if nulls.Contains(lv.Nsp, uint64(i)) { 2733 continue 2734 } 2735 2736 bj := types.DecodeJson(v) 2737 val, err := bj.MarshalJSON() 2738 if err != nil { 2739 return nil, err 2740 } 2741 col[i] = val 2742 } 2743 return vector.NewWithBytes(rv.Typ, col, lv.Nsp, proc.Mp()), nil 2744 } 2745 2746 func CastStringToBool(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2747 vs := vector.MustStrCols(lv) 2748 if lv.IsScalar() { 2749 if lv.IsScalarNull() { 2750 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2751 } 2752 2753 val, err := types.ParseBool(vs[0]) 2754 if err != nil { 2755 return nil, err 2756 } 2757 return vector.NewConstFixed(rv.Typ, lv.Length(), val, proc.Mp()), nil 2758 } 2759 2760 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 2761 if err != nil { 2762 return nil, err 2763 } 2764 rs := vector.MustTCols[bool](vec) 2765 for i, str := range vs { 2766 if nulls.Contains(lv.Nsp, uint64(i)) { 2767 continue 2768 } 2769 val, err := types.ParseBool(str) 2770 if err != nil { 2771 return nil, err 2772 } 2773 rs[i] = val 2774 } 2775 return vec, nil 2776 } 2777 2778 // ---------------------------------------------uuid cast--------------------------------------------------------------- 2779 func CastStringToUuid(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2780 vs := vector.MustStrCols(lv) 2781 if lv.IsScalar() { 2782 if lv.IsScalarNull() { 2783 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2784 } 2785 val, err := types.ParseUuid(vs[0]) 2786 if err != nil { 2787 return nil, err 2788 } 2789 return vector.NewConstFixed(rv.Typ, lv.Length(), val, proc.Mp()), nil 2790 } 2791 vec, err := proc.AllocVectorOfRows(rv.Typ, int64(len(vs)), lv.Nsp) 2792 if err != nil { 2793 return nil, err 2794 } 2795 rs := vector.MustTCols[types.Uuid](vec) 2796 for i := range vs { 2797 if nulls.Contains(lv.Nsp, uint64(i)) { 2798 continue 2799 } 2800 val, err2 := types.ParseUuid(vs[i]) 2801 if err2 != nil { 2802 return nil, err2 2803 } 2804 rs[i] = val 2805 } 2806 return vec, nil 2807 } 2808 2809 func CastUuidToString(lv, rv *vector.Vector, proc *process.Process) (*vector.Vector, error) { 2810 var err error 2811 if lv.IsScalar() { 2812 if lv.IsScalarNull() { 2813 return proc.AllocConstNullVector(rv.Typ, lv.Length()), nil 2814 } else { 2815 lvs := vector.MustTCols[types.Uuid](lv) 2816 col := make([]string, 1) 2817 if col, err = binary.UuidToBytes(lvs, col); err != nil { 2818 return nil, err 2819 } 2820 return vector.NewConstString(rv.Typ, lv.Length(), col[0], proc.Mp()), nil 2821 } 2822 } else { 2823 lvs := vector.MustTCols[types.Uuid](lv) 2824 col := make([]string, len(lvs)) 2825 if col, err = binary.UuidToBytes(lvs, col); err != nil { 2826 return nil, err 2827 } 2828 return vector.NewWithStrings(rv.Typ, col, lv.Nsp, proc.Mp()), nil 2829 } 2830 } 2831 2832 // ---------------------------------------------------------------------------------------------------------------------- 2833 // IsInteger return true if the types.T is integer type 2834 func IsInteger(t types.T) bool { 2835 if t == types.T_int8 || t == types.T_int16 || t == types.T_int32 || t == types.T_int64 || 2836 t == types.T_uint8 || t == types.T_uint16 || t == types.T_uint32 || t == types.T_uint64 { 2837 return true 2838 } 2839 return false 2840 } 2841 2842 // isSignedInteger: return true if the types.T is Signed integer type 2843 func isSignedInteger(t types.T) bool { 2844 if t == types.T_int8 || t == types.T_int16 || t == types.T_int32 || t == types.T_int64 { 2845 return true 2846 } 2847 return false 2848 } 2849 2850 // isUnsignedInteger: return true if the types.T is UnSigned integer type 2851 func isUnsignedInteger(t types.T) bool { 2852 if t == types.T_uint8 || t == types.T_uint16 || t == types.T_uint32 || t == types.T_uint64 { 2853 return true 2854 } 2855 return false 2856 } 2857 2858 // IsFloat: return true if the types.T is floating Point Types 2859 func IsFloat(t types.T) bool { 2860 if t == types.T_float32 || t == types.T_float64 { 2861 return true 2862 } 2863 return false 2864 } 2865 2866 // IsNumeric: return true if the types.T is numbric type 2867 func IsNumeric(t types.T) bool { 2868 if IsInteger(t) || IsFloat(t) { 2869 return true 2870 } 2871 return false 2872 } 2873 2874 // isString: return true if the types.T is string type 2875 func isString(t types.T) bool { 2876 if t == types.T_char || t == types.T_varchar || t == types.T_blob || t == types.T_text { 2877 return true 2878 } 2879 return false 2880 } 2881 2882 // IsDecimal: return true if the types.T is decimal64 or decimal128 2883 func IsDecimal(t types.T) bool { 2884 if t == types.T_decimal64 || t == types.T_decimal128 { 2885 return true 2886 } 2887 return false 2888 }