github.com/matrixorigin/matrixone@v1.2.0/pkg/testutil/util_new.go (about) 1 // Copyright 2021 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 testutil 16 17 import ( 18 "context" 19 "encoding/binary" 20 "math/rand" 21 "strconv" 22 "time" 23 "unsafe" 24 25 "github.com/matrixorigin/matrixone/pkg/common/moerr" 26 "github.com/matrixorigin/matrixone/pkg/common/mpool" 27 "github.com/matrixorigin/matrixone/pkg/common/runtime" 28 "github.com/matrixorigin/matrixone/pkg/container/batch" 29 "github.com/matrixorigin/matrixone/pkg/container/types" 30 "github.com/matrixorigin/matrixone/pkg/container/vector" 31 "github.com/matrixorigin/matrixone/pkg/defines" 32 "github.com/matrixorigin/matrixone/pkg/fileservice" 33 "github.com/matrixorigin/matrixone/pkg/incrservice" 34 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 35 "github.com/matrixorigin/matrixone/pkg/vm/process" 36 ) 37 38 func NewProcess() *process.Process { 39 mp := mpool.MustNewZeroNoFixed() 40 return NewProcessWithMPool(mp) 41 } 42 43 func SetupAutoIncrService() { 44 rt := runtime.ProcessLevelRuntime() 45 if rt == nil { 46 rt = runtime.DefaultRuntime() 47 runtime.SetupProcessLevelRuntime(rt) 48 } 49 rt.SetGlobalVariables( 50 runtime.AutoIncrementService, 51 incrservice.NewIncrService( 52 "", 53 incrservice.NewMemStore(), 54 incrservice.Config{})) 55 } 56 57 func NewProcessWithMPool(mp *mpool.MPool) *process.Process { 58 SetupAutoIncrService() 59 proc := process.New( 60 context.Background(), 61 mp, 62 nil, // no txn client can be set 63 nil, // no txn operator can be set 64 NewFS(), 65 nil, 66 nil, 67 nil, 68 nil, 69 nil, 70 ) 71 proc.Lim.Size = 1 << 20 72 proc.Lim.BatchRows = 1 << 20 73 proc.Lim.BatchSize = 1 << 20 74 proc.Lim.ReaderSize = 1 << 20 75 proc.SessionInfo.TimeZone = time.Local 76 return proc 77 } 78 79 var NewProc = NewProcess 80 81 func NewFS() *fileservice.FileServices { 82 local, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil) 83 if err != nil { 84 panic(err) 85 } 86 s3, err := fileservice.NewMemoryFS(defines.SharedFileServiceName, fileservice.DisabledCacheConfig, nil) 87 if err != nil { 88 panic(err) 89 } 90 etl, err := fileservice.NewMemoryFS(defines.ETLFileServiceName, fileservice.DisabledCacheConfig, nil) 91 if err != nil { 92 panic(err) 93 } 94 fs, err := fileservice.NewFileServices( 95 "", 96 local, 97 s3, 98 etl, 99 ) 100 if err != nil { 101 panic(err) 102 } 103 return fs 104 } 105 106 func NewSharedFS() fileservice.FileService { 107 fs, err := fileservice.NewMemoryFS(defines.SharedFileServiceName, fileservice.DisabledCacheConfig, nil) 108 if err != nil { 109 panic(err) 110 } 111 return fs 112 } 113 114 func NewETLFS() fileservice.FileService { 115 fs, err := fileservice.NewMemoryFS(defines.ETLFileServiceName, fileservice.DisabledCacheConfig, nil) 116 if err != nil { 117 panic(err) 118 } 119 return fs 120 } 121 122 func NewBatch(ts []types.Type, random bool, n int, m *mpool.MPool) *batch.Batch { 123 bat := batch.NewWithSize(len(ts)) 124 bat.SetRowCount(n) 125 for i := range bat.Vecs { 126 bat.Vecs[i] = NewVector(n, ts[i], m, random, nil) 127 // XXX do we need to init nulls here? can we be lazy? 128 bat.Vecs[i].GetNulls().InitWithSize(n) 129 } 130 return bat 131 } 132 133 func NewBatchWithNulls(ts []types.Type, random bool, n int, m *mpool.MPool) *batch.Batch { 134 bat := batch.NewWithSize(len(ts)) 135 bat.SetRowCount(n) 136 for i := range bat.Vecs { 137 bat.Vecs[i] = NewVector(n, ts[i], m, random, nil) 138 bat.Vecs[i].GetNulls().InitWithSize(n) 139 nsp := bat.Vecs[i].GetNulls() 140 for j := 0; j < n; j++ { 141 if j%2 == 0 { 142 nsp.Set(uint64(j)) 143 } 144 } 145 } 146 return bat 147 } 148 149 func NewBatchWithVectors(vs []*vector.Vector, zs []int64) *batch.Batch { 150 bat := batch.NewWithSize(len(vs)) 151 if len(vs) > 0 { 152 bat.SetRowCount(vs[0].Length()) 153 bat.Vecs = vs 154 } 155 return bat 156 } 157 158 func NewVector(n int, typ types.Type, m *mpool.MPool, random bool, Values interface{}) *vector.Vector { 159 switch typ.Oid { 160 case types.T_bool: 161 if vs, ok := Values.([]bool); ok { 162 return NewBoolVector(n, typ, m, random, vs) 163 } 164 return NewBoolVector(n, typ, m, random, nil) 165 case types.T_bit: 166 if vs, ok := Values.([]uint64); ok { 167 return NewUInt64Vector(n, typ, m, random, vs) 168 } 169 return NewUInt64Vector(n, typ, m, random, nil) 170 case types.T_int8: 171 if vs, ok := Values.([]int8); ok { 172 return NewInt8Vector(n, typ, m, random, vs) 173 } 174 return NewInt8Vector(n, typ, m, random, nil) 175 case types.T_int16: 176 if vs, ok := Values.([]int16); ok { 177 return NewInt16Vector(n, typ, m, random, vs) 178 } 179 return NewInt16Vector(n, typ, m, random, nil) 180 case types.T_int32: 181 if vs, ok := Values.([]int32); ok { 182 return NewInt32Vector(n, typ, m, random, vs) 183 } 184 return NewInt32Vector(n, typ, m, random, nil) 185 case types.T_int64: 186 if vs, ok := Values.([]int64); ok { 187 return NewInt64Vector(n, typ, m, random, vs) 188 } 189 return NewInt64Vector(n, typ, m, random, nil) 190 case types.T_uint8: 191 if vs, ok := Values.([]uint8); ok { 192 return NewUInt8Vector(n, typ, m, random, vs) 193 } 194 return NewUInt8Vector(n, typ, m, random, nil) 195 case types.T_uint16: 196 if vs, ok := Values.([]uint16); ok { 197 return NewUInt16Vector(n, typ, m, random, vs) 198 } 199 return NewUInt16Vector(n, typ, m, random, nil) 200 case types.T_uint32: 201 if vs, ok := Values.([]uint32); ok { 202 return NewUInt32Vector(n, typ, m, random, vs) 203 } 204 return NewUInt32Vector(n, typ, m, random, nil) 205 case types.T_uint64: 206 if vs, ok := Values.([]uint64); ok { 207 return NewUInt64Vector(n, typ, m, random, vs) 208 } 209 return NewUInt64Vector(n, typ, m, random, nil) 210 case types.T_float32: 211 if vs, ok := Values.([]float32); ok { 212 return NewFloat32Vector(n, typ, m, random, vs) 213 } 214 return NewFloat32Vector(n, typ, m, random, nil) 215 case types.T_float64: 216 if vs, ok := Values.([]float64); ok { 217 return NewFloat64Vector(n, typ, m, random, vs) 218 } 219 return NewFloat64Vector(n, typ, m, random, nil) 220 case types.T_date: 221 if vs, ok := Values.([]string); ok { 222 return NewDateVector(n, typ, m, random, vs) 223 } 224 return NewDateVector(n, typ, m, random, nil) 225 case types.T_time: 226 if vs, ok := Values.([]string); ok { 227 return NewTimeVector(n, typ, m, random, vs) 228 } 229 return NewTimeVector(n, typ, m, random, nil) 230 case types.T_datetime: 231 if vs, ok := Values.([]string); ok { 232 return NewDatetimeVector(n, typ, m, random, vs) 233 } 234 return NewDatetimeVector(n, typ, m, random, nil) 235 case types.T_timestamp: 236 if vs, ok := Values.([]string); ok { 237 return NewTimestampVector(n, typ, m, random, vs) 238 } 239 return NewTimestampVector(n, typ, m, random, nil) 240 case types.T_decimal64: 241 if vs, ok := Values.([]types.Decimal64); ok { 242 return NewDecimal64Vector(n, typ, m, random, vs) 243 } 244 return NewDecimal64Vector(n, typ, m, random, nil) 245 case types.T_decimal128: 246 if vs, ok := Values.([]types.Decimal128); ok { 247 return NewDecimal128Vector(n, typ, m, random, vs) 248 } 249 return NewDecimal128Vector(n, typ, m, random, nil) 250 case types.T_char, types.T_varchar, 251 types.T_binary, types.T_varbinary, types.T_blob, types.T_text: 252 if vs, ok := Values.([]string); ok { 253 return NewStringVector(n, typ, m, random, vs) 254 } 255 return NewStringVector(n, typ, m, random, nil) 256 case types.T_array_float32: 257 if vs, ok := Values.([][]float32); ok { 258 return NewArrayVector[float32](n, typ, m, random, vs) 259 } 260 return NewArrayVector[float32](n, typ, m, random, nil) 261 case types.T_array_float64: 262 if vs, ok := Values.([][]float64); ok { 263 return NewArrayVector[float64](n, typ, m, random, vs) 264 } 265 return NewArrayVector[float64](n, typ, m, random, nil) 266 case types.T_json: 267 if vs, ok := Values.([]string); ok { 268 return NewJsonVector(n, typ, m, random, vs) 269 } 270 return NewJsonVector(n, typ, m, random, nil) 271 case types.T_TS: 272 if vs, ok := Values.([]types.TS); ok { 273 return NewTsVector(n, typ, m, random, vs) 274 } 275 return NewTsVector(n, typ, m, random, nil) 276 case types.T_Rowid: 277 if vs, ok := Values.([]types.Rowid); ok { 278 return NewRowidVector(n, typ, m, random, vs) 279 } 280 return NewRowidVector(n, typ, m, random, nil) 281 case types.T_Blockid: 282 if vs, ok := Values.([]types.Blockid); ok { 283 return NewBlockidVector(n, typ, m, random, vs) 284 } 285 return NewBlockidVector(n, typ, m, random, nil) 286 case types.T_enum: 287 if vs, ok := Values.([]uint16); ok { 288 return NewUInt16Vector(n, typ, m, random, vs) 289 } 290 return NewUInt16Vector(n, typ, m, random, nil) 291 default: 292 panic(moerr.NewInternalErrorNoCtx("unsupport vector's type '%v", typ)) 293 } 294 } 295 296 func NewTsVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []types.TS) *vector.Vector { 297 vec := vector.NewVec(typ) 298 if vs != nil { 299 for i := range vs { 300 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 301 vec.Free(m) 302 return nil 303 } 304 } 305 return vec 306 } 307 for i := 0; i < n; i++ { 308 var t timestamp.Timestamp 309 310 t.PhysicalTime = int64(i) 311 if err := vector.AppendFixed(vec, types.TimestampToTS(t), false, m); err != nil { 312 vec.Free(m) 313 return nil 314 } 315 } 316 return vec 317 } 318 319 func NewRowidVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []types.Rowid) *vector.Vector { 320 vec := vector.NewVec(typ) 321 if vs != nil { 322 for i := range vs { 323 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 324 vec.Free(m) 325 return nil 326 } 327 } 328 return vec 329 } 330 for i := 0; i < n; i++ { 331 var rowId types.Rowid 332 binary.LittleEndian.PutUint64( 333 unsafe.Slice(&rowId[types.RowidSize/2], 8), 334 uint64(i), 335 ) 336 if err := vector.AppendFixed(vec, rowId, false, m); err != nil { 337 vec.Free(m) 338 return nil 339 } 340 } 341 return vec 342 } 343 344 func NewBlockidVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []types.Blockid) *vector.Vector { 345 vec := vector.NewVec(typ) 346 if vs != nil { 347 for i := range vs { 348 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 349 vec.Free(m) 350 return nil 351 } 352 } 353 return vec 354 } 355 for i := 0; i < n; i++ { 356 var blockId types.Blockid 357 binary.LittleEndian.PutUint64( 358 unsafe.Slice(&blockId[types.BlockidSize/2], 8), 359 uint64(i), 360 ) 361 if err := vector.AppendFixed(vec, blockId, false, m); err != nil { 362 vec.Free(m) 363 return nil 364 } 365 } 366 return vec 367 } 368 369 func NewJsonVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []string) *vector.Vector { 370 vec := vector.NewVec(typ) 371 if vs != nil { 372 for _, v := range vs { 373 json, err := types.ParseStringToByteJson(v) 374 if err != nil { 375 vec.Free(m) 376 return nil 377 } 378 jbytes, err := json.Marshal() 379 if err != nil { 380 vec.Free(m) 381 return nil 382 } 383 if err := vector.AppendFixed(vec, jbytes, false, m); err != nil { 384 vec.Free(m) 385 return nil 386 } 387 } 388 return vec 389 } 390 for i := 0; i < n; i++ { 391 json, _ := types.ParseStringToByteJson(`{"a":1}`) 392 jbytes, _ := json.Marshal() 393 if err := vector.AppendBytes(vec, jbytes, false, m); err != nil { 394 vec.Free(m) 395 return nil 396 } 397 } 398 return vec 399 } 400 401 func NewBoolVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []bool) *vector.Vector { 402 vec := vector.NewVec(typ) 403 if vs != nil { 404 for i := range vs { 405 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 406 vec.Free(m) 407 return nil 408 } 409 } 410 return vec 411 } 412 for i := 0; i < n; i++ { 413 if err := vector.AppendFixed(vec, bool(i%2 == 0), false, m); err != nil { 414 vec.Free(m) 415 return nil 416 } 417 } 418 return vec 419 } 420 421 func NewInt8Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int8) *vector.Vector { 422 vec := vector.NewVec(typ) 423 if vs != nil { 424 for i := range vs { 425 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 426 vec.Free(m) 427 return nil 428 } 429 } 430 return vec 431 } 432 for i := 0; i < n; i++ { 433 v := i 434 if random { 435 v = rand.Int() 436 } 437 if err := vector.AppendFixed(vec, int8(v), false, m); err != nil { 438 vec.Free(m) 439 return nil 440 } 441 } 442 return vec 443 } 444 445 func NewInt16Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int16) *vector.Vector { 446 vec := vector.NewVec(typ) 447 if vs != nil { 448 for i := range vs { 449 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 450 vec.Free(m) 451 return nil 452 } 453 } 454 return vec 455 } 456 for i := 0; i < n; i++ { 457 v := i 458 if random { 459 v = rand.Int() 460 } 461 if err := vector.AppendFixed(vec, int16(v), false, m); err != nil { 462 vec.Free(m) 463 return nil 464 } 465 } 466 return vec 467 } 468 469 func NewInt32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int32) *vector.Vector { 470 vec := vector.NewVec(typ) 471 if vs != nil { 472 for i := range vs { 473 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 474 vec.Free(m) 475 return nil 476 } 477 } 478 return vec 479 } 480 for i := 0; i < n; i++ { 481 v := i 482 if random { 483 v = rand.Int() 484 } 485 if err := vector.AppendFixed(vec, int32(v), false, m); err != nil { 486 vec.Free(m) 487 return nil 488 } 489 } 490 return vec 491 } 492 493 func NewInt64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int64) *vector.Vector { 494 vec := vector.NewVec(typ) 495 if vs != nil { 496 for i := range vs { 497 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 498 vec.Free(m) 499 return nil 500 } 501 } 502 return vec 503 } 504 for i := 0; i < n; i++ { 505 v := i 506 if random { 507 v = rand.Int() 508 } 509 if err := vector.AppendFixed(vec, int64(v), false, m); err != nil { 510 vec.Free(m) 511 return nil 512 } 513 } 514 return vec 515 } 516 517 func NewUInt8Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint8) *vector.Vector { 518 vec := vector.NewVec(typ) 519 if vs != nil { 520 for i := range vs { 521 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 522 vec.Free(m) 523 return nil 524 } 525 } 526 return vec 527 } 528 for i := 0; i < n; i++ { 529 v := i 530 if random { 531 v = rand.Int() 532 } 533 if err := vector.AppendFixed(vec, uint8(v), false, m); err != nil { 534 vec.Free(m) 535 return nil 536 } 537 } 538 return vec 539 } 540 541 func NewUInt16Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint16) *vector.Vector { 542 vec := vector.NewVec(typ) 543 if vs != nil { 544 for i := range vs { 545 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 546 vec.Free(m) 547 return nil 548 } 549 } 550 return vec 551 } 552 for i := 0; i < n; i++ { 553 v := i 554 if random { 555 v = rand.Int() 556 } 557 if err := vector.AppendFixed(vec, uint16(v), false, m); err != nil { 558 vec.Free(m) 559 return nil 560 } 561 } 562 return vec 563 } 564 565 func NewUInt32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint32) *vector.Vector { 566 vec := vector.NewVec(typ) 567 if vs != nil { 568 for i := range vs { 569 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 570 vec.Free(m) 571 return nil 572 } 573 } 574 return vec 575 } 576 for i := 0; i < n; i++ { 577 v := i 578 if random { 579 v = rand.Int() 580 } 581 if err := vector.AppendFixed(vec, uint32(v), false, m); err != nil { 582 vec.Free(m) 583 return nil 584 } 585 } 586 return vec 587 } 588 589 func NewUInt64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint64) *vector.Vector { 590 vec := vector.NewVec(typ) 591 if vs != nil { 592 for i := range vs { 593 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 594 vec.Free(m) 595 return nil 596 } 597 } 598 return vec 599 } 600 for i := 0; i < n; i++ { 601 v := i 602 if random { 603 v = rand.Int() 604 } 605 if err := vector.AppendFixed(vec, uint64(v), false, m); err != nil { 606 vec.Free(m) 607 return nil 608 } 609 } 610 return vec 611 } 612 613 func NewFloat32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []float32) *vector.Vector { 614 vec := vector.NewVec(typ) 615 if vs != nil { 616 for i := range vs { 617 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 618 vec.Free(m) 619 return nil 620 } 621 } 622 return vec 623 } 624 for i := 0; i < n; i++ { 625 v := float32(i) 626 if random { 627 v = rand.Float32() 628 } 629 if err := vector.AppendFixed(vec, float32(v), false, m); err != nil { 630 vec.Free(m) 631 return nil 632 } 633 } 634 return vec 635 } 636 637 func NewFloat64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []float64) *vector.Vector { 638 vec := vector.NewVec(typ) 639 if vs != nil { 640 for i := range vs { 641 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 642 vec.Free(m) 643 return nil 644 } 645 } 646 return vec 647 } 648 for i := 0; i < n; i++ { 649 v := float64(i) 650 if random { 651 v = rand.Float64() 652 } 653 if err := vector.AppendFixed(vec, float64(v), false, m); err != nil { 654 vec.Free(m) 655 return nil 656 } 657 } 658 return vec 659 } 660 661 func NewDecimal64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []types.Decimal64) *vector.Vector { 662 vec := vector.NewVec(typ) 663 if vs != nil { 664 for i := range vs { 665 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 666 vec.Free(m) 667 return nil 668 } 669 } 670 return vec 671 } 672 for i := 0; i < n; i++ { 673 v := i 674 if random { 675 v = rand.Int() 676 } 677 d := types.Decimal64(v) 678 if err := vector.AppendFixed(vec, d, false, m); err != nil { 679 680 vec.Free(m) 681 return nil 682 } 683 } 684 return vec 685 } 686 687 func NewDecimal128Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []types.Decimal128) *vector.Vector { 688 vec := vector.NewVec(typ) 689 if vs != nil { 690 for i := range vs { 691 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 692 vec.Free(m) 693 return nil 694 } 695 } 696 return vec 697 } 698 for i := 0; i < n; i++ { 699 v := i 700 if random { 701 v = rand.Int() 702 } 703 d := types.Decimal128{B0_63: uint64(v), B64_127: 0} 704 if err := vector.AppendFixed(vec, d, false, m); err != nil { 705 vec.Free(m) 706 return nil 707 } 708 } 709 return vec 710 } 711 712 func NewDateVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 713 vec := vector.NewVec(typ) 714 if vs != nil { 715 for i := range vs { 716 d, err := types.ParseDateCast(vs[i]) 717 if err != nil { 718 return nil 719 } 720 if err := vector.AppendFixed(vec, d, false, m); err != nil { 721 vec.Free(m) 722 return nil 723 } 724 } 725 return vec 726 } 727 for i := 0; i < n; i++ { 728 v := i 729 if random { 730 v = rand.Int() 731 } 732 if err := vector.AppendFixed(vec, types.Date(v), false, m); err != nil { 733 vec.Free(m) 734 return nil 735 } 736 } 737 return vec 738 } 739 740 func NewTimeVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 741 vec := vector.NewVec(typ) 742 if vs != nil { 743 for i := range vs { 744 d, err := types.ParseTime(vs[i], 6) 745 if err != nil { 746 return nil 747 } 748 if err := vector.AppendFixed(vec, d, false, m); err != nil { 749 vec.Free(m) 750 return nil 751 } 752 } 753 return vec 754 } 755 for i := 0; i < n; i++ { 756 v := i 757 if random { 758 v = rand.Int() 759 } 760 if err := vector.AppendFixed(vec, types.Time(v), false, m); err != nil { 761 vec.Free(m) 762 return nil 763 } 764 } 765 return vec 766 } 767 768 func NewDatetimeVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 769 vec := vector.NewVec(typ) 770 if vs != nil { 771 for i := range vs { 772 d, err := types.ParseDatetime(vs[i], 6) 773 if err != nil { 774 return nil 775 } 776 if err := vector.AppendFixed(vec, d, false, m); err != nil { 777 vec.Free(m) 778 return nil 779 } 780 } 781 return vec 782 } 783 for i := 0; i < n; i++ { 784 v := i 785 if random { 786 v = rand.Int() 787 } 788 if err := vector.AppendFixed(vec, types.Datetime(v), false, m); err != nil { 789 vec.Free(m) 790 return nil 791 } 792 } 793 return vec 794 } 795 796 func NewTimestampVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 797 vec := vector.NewVec(typ) 798 if vs != nil { 799 for i := range vs { 800 d, err := types.ParseTimestamp(time.Local, vs[i], 6) 801 if err != nil { 802 return nil 803 } 804 if err := vector.AppendFixed(vec, d, false, m); err != nil { 805 vec.Free(m) 806 return nil 807 } 808 } 809 return vec 810 } 811 for i := 0; i < n; i++ { 812 v := i 813 if random { 814 v = rand.Int() 815 } 816 if err := vector.AppendFixed(vec, types.Timestamp(v), false, m); err != nil { 817 vec.Free(m) 818 return nil 819 } 820 } 821 return vec 822 } 823 824 func NewStringVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 825 vec := vector.NewVec(typ) 826 if vs != nil { 827 for i := range vs { 828 if err := vector.AppendBytes(vec, []byte(vs[i]), false, m); err != nil { 829 vec.Free(m) 830 return nil 831 } 832 } 833 return vec 834 } 835 for i := 0; i < n; i++ { 836 v := i 837 if random { 838 v = rand.Int() 839 } 840 if err := vector.AppendBytes(vec, []byte(strconv.Itoa(v)), false, m); err != nil { 841 vec.Free(m) 842 return nil 843 } 844 } 845 return vec 846 } 847 848 func NewArrayVector[T types.RealNumbers](n int, typ types.Type, m *mpool.MPool, random bool, vs [][]T) *vector.Vector { 849 vec := vector.NewVec(typ) 850 if vs != nil { 851 for i := range vs { 852 if err := vector.AppendArray[T](vec, vs[i], false, m); err != nil { 853 vec.Free(m) 854 return nil 855 } 856 } 857 return vec 858 } 859 for i := 0; i < n; i++ { 860 v := i 861 if random { 862 v = rand.Int() 863 } 864 if err := vector.AppendArray[T](vec, []T{T(v), T(v + 1)}, false, m); err != nil { 865 vec.Free(m) 866 return nil 867 } 868 } 869 return vec 870 }