github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/utils.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 package objectio 15 16 import ( 17 "encoding/binary" 18 "io" 19 "math/rand" 20 "strconv" 21 "time" 22 "unsafe" 23 24 "github.com/matrixorigin/matrixone/pkg/common/moerr" 25 "github.com/matrixorigin/matrixone/pkg/common/mpool" 26 "github.com/matrixorigin/matrixone/pkg/container/batch" 27 "github.com/matrixorigin/matrixone/pkg/container/types" 28 "github.com/matrixorigin/matrixone/pkg/container/vector" 29 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 30 ) 31 32 var ( 33 RowidType types.Type 34 ) 35 36 func init() { 37 RowidType = types.T_Rowid.ToType() 38 } 39 40 type CreateObjOpt struct { 41 Id *types.Objectid 42 } 43 44 func (o *CreateObjOpt) WithId(id *types.Objectid) *CreateObjOpt { 45 o.Id = id 46 return o 47 } 48 49 type CreateBlockOpt struct { 50 Loc *struct { 51 Metaloc Location 52 Deltaloc Location 53 } 54 55 Id *struct { 56 Filen uint16 57 Blkn uint16 58 } 59 } 60 61 func (o *CreateBlockOpt) WithMetaloc(s Location) *CreateBlockOpt { 62 if o.Loc != nil { 63 o.Loc.Metaloc = s 64 } else { 65 o.Loc = &struct { 66 Metaloc Location 67 Deltaloc Location 68 }{Metaloc: s} 69 } 70 return o 71 } 72 73 func (o *CreateBlockOpt) WithDetaloc(s Location) *CreateBlockOpt { 74 if o.Loc != nil { 75 o.Loc.Deltaloc = s 76 } else { 77 o.Loc = &struct { 78 Metaloc Location 79 Deltaloc Location 80 }{Deltaloc: s} 81 } 82 return o 83 } 84 85 func (o *CreateBlockOpt) WithFileIdx(s uint16) *CreateBlockOpt { 86 if o.Id != nil { 87 o.Id.Filen = s 88 } else { 89 o.Id = &struct { 90 Filen uint16 91 Blkn uint16 92 }{Filen: s} 93 } 94 return o 95 } 96 97 func (o *CreateBlockOpt) WithBlkIdx(s uint16) *CreateBlockOpt { 98 if o.Id != nil { 99 o.Id.Blkn = s 100 } else { 101 o.Id = &struct { 102 Filen uint16 103 Blkn uint16 104 }{Blkn: s} 105 } 106 return o 107 } 108 109 func WriteString(str string, w io.Writer) (n int64, err error) { 110 buf := []byte(str) 111 size := uint32(len(buf)) 112 if _, err = w.Write(types.EncodeUint32(&size)); err != nil { 113 return 114 } 115 wn, err := w.Write(buf) 116 return int64(wn + 4), err 117 } 118 119 func WriteBytes(b []byte, w io.Writer) (n int64, err error) { 120 size := uint32(len(b)) 121 if _, err = w.Write(types.EncodeUint32(&size)); err != nil { 122 return 123 } 124 wn, err := w.Write(b) 125 return int64(wn + 4), err 126 } 127 128 func ReadString(r io.Reader) (str string, n int64, err error) { 129 strLen := uint32(0) 130 if _, err = r.Read(types.EncodeUint32(&strLen)); err != nil { 131 return 132 } 133 buf := make([]byte, strLen) 134 if _, err = r.Read(buf); err != nil { 135 return 136 } 137 str = string(buf) 138 n = 4 + int64(strLen) 139 return 140 } 141 142 func ReadBytes(r io.Reader) (buf []byte, n int64, err error) { 143 strLen := uint32(0) 144 if _, err = r.Read(types.EncodeUint32(&strLen)); err != nil { 145 return 146 } 147 buf = make([]byte, strLen) 148 if _, err = r.Read(buf); err != nil { 149 return 150 } 151 n = 4 + int64(strLen) 152 return 153 } 154 155 type Seqnums struct { 156 Seqs []uint16 157 MaxSeq uint16 // do not consider special column like rowid and committs 158 MetaColCnt uint16 // include special columns 159 } 160 161 func NewSeqnums(seqs []uint16) *Seqnums { 162 s := &Seqnums{ 163 Seqs: make([]uint16, 0, len(seqs)), 164 } 165 if len(seqs) == 0 { 166 return s 167 } 168 169 for _, v := range seqs { 170 if v < SEQNUM_UPPER && v > s.MaxSeq { 171 s.MaxSeq = v 172 } 173 } 174 175 maxseq := s.MaxSeq 176 for _, v := range seqs { 177 if v >= SEQNUM_UPPER { 178 s.Seqs = append(s.Seqs, maxseq+1) 179 maxseq += 1 180 } else { 181 s.Seqs = append(s.Seqs, v) 182 } 183 } 184 s.MetaColCnt = maxseq + 1 185 return s 186 } 187 188 func (s *Seqnums) InitWithColCnt(colcnt int) { 189 for i := 0; i < colcnt; i++ { 190 s.Seqs = append(s.Seqs, uint16(i)) 191 } 192 s.MaxSeq = uint16(colcnt) - 1 193 s.MetaColCnt = uint16(colcnt) 194 } 195 196 func ConstructRowidColumn( 197 id *Blockid, start, length uint32, mp *mpool.MPool, 198 ) (vec *vector.Vector, err error) { 199 vec = vector.NewVec(RowidType) 200 if err = ConstructRowidColumnTo(vec, id, start, length, mp); err != nil { 201 vec = nil 202 } 203 return 204 } 205 206 func ConstructRowidColumnTo( 207 vec *vector.Vector, 208 id *Blockid, start, length uint32, mp *mpool.MPool, 209 ) (err error) { 210 if err = vec.PreExtend(int(length), mp); err != nil { 211 return 212 } 213 for i := uint32(0); i < length; i++ { 214 rid := NewRowid(id, start+i) 215 if err = vector.AppendFixed(vec, *rid, false, mp); err != nil { 216 break 217 } 218 } 219 if err != nil { 220 vec.Free(mp) 221 } 222 return 223 } 224 225 func ConstructRowidColumnToWithSels( 226 vec *vector.Vector, 227 id *Blockid, 228 sels []int32, 229 mp *mpool.MPool, 230 ) (err error) { 231 if err = vec.PreExtend(len(sels), mp); err != nil { 232 return 233 } 234 for _, row := range sels { 235 rid := NewRowid(id, uint32(row)) 236 if err = vector.AppendFixed(vec, *rid, false, mp); err != nil { 237 break 238 } 239 } 240 if err != nil { 241 vec.Free(mp) 242 } 243 return 244 } 245 246 //============================================= 247 // add following functions to solve the cycle reference problem. 248 // because,I add hakeeper client interface to the Process for supporting 249 // the table function mo_configurations to get info from the hakeeper. 250 // 251 // `Hakeeper logservice.CNHAKeeperClient` 252 // 253 // I get following cycle reference error. 254 // error: 255 //package github.com/matrixorigin/matrixone/pkg/objectio 256 // imports github.com/matrixorigin/matrixone/pkg/catalog 257 // imports github.com/matrixorigin/matrixone/pkg/hakeeper/operator 258 // imports github.com/matrixorigin/matrixone/pkg/hakeeper/checkers 259 // imports github.com/matrixorigin/matrixone/pkg/logservice 260 // imports github.com/matrixorigin/matrixone/pkg/vm/process 261 // imports github.com/matrixorigin/matrixone/pkg/testutil: import cycle not allowed in test 262 // 263 // To fix the error. I move some necessary functions here. 264 //============================================= 265 266 func NewBatch(ts []types.Type, random bool, n int, m *mpool.MPool) *batch.Batch { 267 bat := batch.NewWithSize(len(ts)) 268 bat.SetRowCount(n) 269 for i := range bat.Vecs { 270 bat.Vecs[i] = NewVector(n, ts[i], m, random, nil) 271 // XXX do we need to init nulls here? can we be lazy? 272 bat.Vecs[i].GetNulls().InitWithSize(n) 273 } 274 return bat 275 } 276 277 func NewVector(n int, typ types.Type, m *mpool.MPool, random bool, Values interface{}) *vector.Vector { 278 switch typ.Oid { 279 case types.T_bool: 280 if vs, ok := Values.([]bool); ok { 281 return NewBoolVector(n, typ, m, random, vs) 282 } 283 return NewBoolVector(n, typ, m, random, nil) 284 case types.T_bit: 285 if vs, ok := Values.([]uint64); ok { 286 return NewUInt64Vector(n, typ, m, random, vs) 287 } 288 return NewUInt64Vector(n, typ, m, random, nil) 289 case types.T_int8: 290 if vs, ok := Values.([]int8); ok { 291 return NewInt8Vector(n, typ, m, random, vs) 292 } 293 return NewInt8Vector(n, typ, m, random, nil) 294 case types.T_int16: 295 if vs, ok := Values.([]int16); ok { 296 return NewInt16Vector(n, typ, m, random, vs) 297 } 298 return NewInt16Vector(n, typ, m, random, nil) 299 case types.T_int32: 300 if vs, ok := Values.([]int32); ok { 301 return NewInt32Vector(n, typ, m, random, vs) 302 } 303 return NewInt32Vector(n, typ, m, random, nil) 304 case types.T_int64: 305 if vs, ok := Values.([]int64); ok { 306 return NewInt64Vector(n, typ, m, random, vs) 307 } 308 return NewInt64Vector(n, typ, m, random, nil) 309 case types.T_uint8: 310 if vs, ok := Values.([]uint8); ok { 311 return NewUInt8Vector(n, typ, m, random, vs) 312 } 313 return NewUInt8Vector(n, typ, m, random, nil) 314 case types.T_uint16: 315 if vs, ok := Values.([]uint16); ok { 316 return NewUInt16Vector(n, typ, m, random, vs) 317 } 318 return NewUInt16Vector(n, typ, m, random, nil) 319 case types.T_uint32: 320 if vs, ok := Values.([]uint32); ok { 321 return NewUInt32Vector(n, typ, m, random, vs) 322 } 323 return NewUInt32Vector(n, typ, m, random, nil) 324 case types.T_uint64: 325 if vs, ok := Values.([]uint64); ok { 326 return NewUInt64Vector(n, typ, m, random, vs) 327 } 328 return NewUInt64Vector(n, typ, m, random, nil) 329 case types.T_float32: 330 if vs, ok := Values.([]float32); ok { 331 return NewFloat32Vector(n, typ, m, random, vs) 332 } 333 return NewFloat32Vector(n, typ, m, random, nil) 334 case types.T_float64: 335 if vs, ok := Values.([]float64); ok { 336 return NewFloat64Vector(n, typ, m, random, vs) 337 } 338 return NewFloat64Vector(n, typ, m, random, nil) 339 case types.T_date: 340 if vs, ok := Values.([]string); ok { 341 return NewDateVector(n, typ, m, random, vs) 342 } 343 return NewDateVector(n, typ, m, random, nil) 344 case types.T_time: 345 if vs, ok := Values.([]string); ok { 346 return NewTimeVector(n, typ, m, random, vs) 347 } 348 return NewTimeVector(n, typ, m, random, nil) 349 case types.T_datetime: 350 if vs, ok := Values.([]string); ok { 351 return NewDatetimeVector(n, typ, m, random, vs) 352 } 353 return NewDatetimeVector(n, typ, m, random, nil) 354 case types.T_timestamp: 355 if vs, ok := Values.([]string); ok { 356 return NewTimestampVector(n, typ, m, random, vs) 357 } 358 return NewTimestampVector(n, typ, m, random, nil) 359 case types.T_decimal64: 360 if vs, ok := Values.([]types.Decimal64); ok { 361 return NewDecimal64Vector(n, typ, m, random, vs) 362 } 363 return NewDecimal64Vector(n, typ, m, random, nil) 364 case types.T_decimal128: 365 if vs, ok := Values.([]types.Decimal128); ok { 366 return NewDecimal128Vector(n, typ, m, random, vs) 367 } 368 return NewDecimal128Vector(n, typ, m, random, nil) 369 case types.T_char, types.T_varchar, 370 types.T_binary, types.T_varbinary, types.T_blob, types.T_text: 371 if vs, ok := Values.([]string); ok { 372 return NewStringVector(n, typ, m, random, vs) 373 } 374 return NewStringVector(n, typ, m, random, nil) 375 case types.T_array_float32: 376 if vs, ok := Values.([][]float32); ok { 377 return NewArrayVector[float32](n, typ, m, random, vs) 378 } 379 return NewArrayVector[float32](n, typ, m, random, nil) 380 case types.T_array_float64: 381 if vs, ok := Values.([][]float64); ok { 382 return NewArrayVector[float64](n, typ, m, random, vs) 383 } 384 return NewArrayVector[float64](n, typ, m, random, nil) 385 case types.T_json: 386 if vs, ok := Values.([]string); ok { 387 return NewJsonVector(n, typ, m, random, vs) 388 } 389 return NewJsonVector(n, typ, m, random, nil) 390 case types.T_TS: 391 if vs, ok := Values.([]types.TS); ok { 392 return NewTsVector(n, typ, m, random, vs) 393 } 394 return NewTsVector(n, typ, m, random, nil) 395 case types.T_Rowid: 396 if vs, ok := Values.([]types.Rowid); ok { 397 return NewRowidVector(n, typ, m, random, vs) 398 } 399 return NewRowidVector(n, typ, m, random, nil) 400 case types.T_Blockid: 401 if vs, ok := Values.([]types.Blockid); ok { 402 return NewBlockidVector(n, typ, m, random, vs) 403 } 404 return NewBlockidVector(n, typ, m, random, nil) 405 case types.T_enum: 406 if vs, ok := Values.([]uint16); ok { 407 return NewUInt16Vector(n, typ, m, random, vs) 408 } 409 return NewUInt16Vector(n, typ, m, random, nil) 410 default: 411 panic(moerr.NewInternalErrorNoCtx("unsupport vector's type '%v", typ)) 412 } 413 } 414 415 func NewTsVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []types.TS) *vector.Vector { 416 vec := vector.NewVec(typ) 417 if vs != nil { 418 for i := range vs { 419 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 420 vec.Free(m) 421 return nil 422 } 423 } 424 return vec 425 } 426 for i := 0; i < n; i++ { 427 var t timestamp.Timestamp 428 429 t.PhysicalTime = int64(i) 430 if err := vector.AppendFixed(vec, types.TimestampToTS(t), false, m); err != nil { 431 vec.Free(m) 432 return nil 433 } 434 } 435 return vec 436 } 437 438 func NewRowidVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []types.Rowid) *vector.Vector { 439 vec := vector.NewVec(typ) 440 if vs != nil { 441 for i := range vs { 442 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 443 vec.Free(m) 444 return nil 445 } 446 } 447 return vec 448 } 449 for i := 0; i < n; i++ { 450 var rowId types.Rowid 451 binary.LittleEndian.PutUint64( 452 unsafe.Slice(&rowId[types.RowidSize/2], 8), 453 uint64(i), 454 ) 455 if err := vector.AppendFixed(vec, rowId, false, m); err != nil { 456 vec.Free(m) 457 return nil 458 } 459 } 460 return vec 461 } 462 463 func NewBlockidVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []types.Blockid) *vector.Vector { 464 vec := vector.NewVec(typ) 465 if vs != nil { 466 for i := range vs { 467 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 468 vec.Free(m) 469 return nil 470 } 471 } 472 return vec 473 } 474 for i := 0; i < n; i++ { 475 var blockId types.Blockid 476 binary.LittleEndian.PutUint64( 477 unsafe.Slice(&blockId[types.BlockidSize/2], 8), 478 uint64(i), 479 ) 480 if err := vector.AppendFixed(vec, blockId, false, m); err != nil { 481 vec.Free(m) 482 return nil 483 } 484 } 485 return vec 486 } 487 488 func NewJsonVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []string) *vector.Vector { 489 vec := vector.NewVec(typ) 490 if vs != nil { 491 for _, v := range vs { 492 json, err := types.ParseStringToByteJson(v) 493 if err != nil { 494 vec.Free(m) 495 return nil 496 } 497 jbytes, err := json.Marshal() 498 if err != nil { 499 vec.Free(m) 500 return nil 501 } 502 if err := vector.AppendFixed(vec, jbytes, false, m); err != nil { 503 vec.Free(m) 504 return nil 505 } 506 } 507 return vec 508 } 509 for i := 0; i < n; i++ { 510 json, _ := types.ParseStringToByteJson(`{"a":1}`) 511 jbytes, _ := json.Marshal() 512 if err := vector.AppendBytes(vec, jbytes, false, m); err != nil { 513 vec.Free(m) 514 return nil 515 } 516 } 517 return vec 518 } 519 520 func NewBoolVector(n int, typ types.Type, m *mpool.MPool, _ bool, vs []bool) *vector.Vector { 521 vec := vector.NewVec(typ) 522 if vs != nil { 523 for i := range vs { 524 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 525 vec.Free(m) 526 return nil 527 } 528 } 529 return vec 530 } 531 for i := 0; i < n; i++ { 532 if err := vector.AppendFixed(vec, bool(i%2 == 0), false, m); err != nil { 533 vec.Free(m) 534 return nil 535 } 536 } 537 return vec 538 } 539 540 func NewInt8Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int8) *vector.Vector { 541 vec := vector.NewVec(typ) 542 if vs != nil { 543 for i := range vs { 544 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 545 vec.Free(m) 546 return nil 547 } 548 } 549 return vec 550 } 551 for i := 0; i < n; i++ { 552 v := i 553 if random { 554 v = rand.Int() 555 } 556 if err := vector.AppendFixed(vec, int8(v), false, m); err != nil { 557 vec.Free(m) 558 return nil 559 } 560 } 561 return vec 562 } 563 564 func NewInt16Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int16) *vector.Vector { 565 vec := vector.NewVec(typ) 566 if vs != nil { 567 for i := range vs { 568 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 569 vec.Free(m) 570 return nil 571 } 572 } 573 return vec 574 } 575 for i := 0; i < n; i++ { 576 v := i 577 if random { 578 v = rand.Int() 579 } 580 if err := vector.AppendFixed(vec, int16(v), false, m); err != nil { 581 vec.Free(m) 582 return nil 583 } 584 } 585 return vec 586 } 587 588 func NewInt32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int32) *vector.Vector { 589 vec := vector.NewVec(typ) 590 if vs != nil { 591 for i := range vs { 592 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 593 vec.Free(m) 594 return nil 595 } 596 } 597 return vec 598 } 599 for i := 0; i < n; i++ { 600 v := i 601 if random { 602 v = rand.Int() 603 } 604 if err := vector.AppendFixed(vec, int32(v), false, m); err != nil { 605 vec.Free(m) 606 return nil 607 } 608 } 609 return vec 610 } 611 612 func NewInt64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int64) *vector.Vector { 613 vec := vector.NewVec(typ) 614 if vs != nil { 615 for i := range vs { 616 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 617 vec.Free(m) 618 return nil 619 } 620 } 621 return vec 622 } 623 for i := 0; i < n; i++ { 624 v := i 625 if random { 626 v = rand.Int() 627 } 628 if err := vector.AppendFixed(vec, int64(v), false, m); err != nil { 629 vec.Free(m) 630 return nil 631 } 632 } 633 return vec 634 } 635 636 func NewUInt8Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint8) *vector.Vector { 637 vec := vector.NewVec(typ) 638 if vs != nil { 639 for i := range vs { 640 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 641 vec.Free(m) 642 return nil 643 } 644 } 645 return vec 646 } 647 for i := 0; i < n; i++ { 648 v := i 649 if random { 650 v = rand.Int() 651 } 652 if err := vector.AppendFixed(vec, uint8(v), false, m); err != nil { 653 vec.Free(m) 654 return nil 655 } 656 } 657 return vec 658 } 659 660 func NewUInt16Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint16) *vector.Vector { 661 vec := vector.NewVec(typ) 662 if vs != nil { 663 for i := range vs { 664 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 665 vec.Free(m) 666 return nil 667 } 668 } 669 return vec 670 } 671 for i := 0; i < n; i++ { 672 v := i 673 if random { 674 v = rand.Int() 675 } 676 if err := vector.AppendFixed(vec, uint16(v), false, m); err != nil { 677 vec.Free(m) 678 return nil 679 } 680 } 681 return vec 682 } 683 684 func NewUInt32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint32) *vector.Vector { 685 vec := vector.NewVec(typ) 686 if vs != nil { 687 for i := range vs { 688 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 689 vec.Free(m) 690 return nil 691 } 692 } 693 return vec 694 } 695 for i := 0; i < n; i++ { 696 v := i 697 if random { 698 v = rand.Int() 699 } 700 if err := vector.AppendFixed(vec, uint32(v), false, m); err != nil { 701 vec.Free(m) 702 return nil 703 } 704 } 705 return vec 706 } 707 708 func NewUInt64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint64) *vector.Vector { 709 vec := vector.NewVec(typ) 710 if vs != nil { 711 for i := range vs { 712 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 713 vec.Free(m) 714 return nil 715 } 716 } 717 return vec 718 } 719 for i := 0; i < n; i++ { 720 v := i 721 if random { 722 v = rand.Int() 723 } 724 if err := vector.AppendFixed(vec, uint64(v), false, m); err != nil { 725 vec.Free(m) 726 return nil 727 } 728 } 729 return vec 730 } 731 732 func NewFloat32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []float32) *vector.Vector { 733 vec := vector.NewVec(typ) 734 if vs != nil { 735 for i := range vs { 736 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 737 vec.Free(m) 738 return nil 739 } 740 } 741 return vec 742 } 743 for i := 0; i < n; i++ { 744 v := float32(i) 745 if random { 746 v = rand.Float32() 747 } 748 if err := vector.AppendFixed(vec, float32(v), false, m); err != nil { 749 vec.Free(m) 750 return nil 751 } 752 } 753 return vec 754 } 755 756 func NewFloat64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []float64) *vector.Vector { 757 vec := vector.NewVec(typ) 758 if vs != nil { 759 for i := range vs { 760 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 761 vec.Free(m) 762 return nil 763 } 764 } 765 return vec 766 } 767 for i := 0; i < n; i++ { 768 v := float64(i) 769 if random { 770 v = rand.Float64() 771 } 772 if err := vector.AppendFixed(vec, float64(v), false, m); err != nil { 773 vec.Free(m) 774 return nil 775 } 776 } 777 return vec 778 } 779 780 func NewDecimal64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []types.Decimal64) *vector.Vector { 781 vec := vector.NewVec(typ) 782 if vs != nil { 783 for i := range vs { 784 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 785 vec.Free(m) 786 return nil 787 } 788 } 789 return vec 790 } 791 for i := 0; i < n; i++ { 792 v := i 793 if random { 794 v = rand.Int() 795 } 796 d := types.Decimal64(v) 797 if err := vector.AppendFixed(vec, d, false, m); err != nil { 798 799 vec.Free(m) 800 return nil 801 } 802 } 803 return vec 804 } 805 806 func NewDecimal128Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []types.Decimal128) *vector.Vector { 807 vec := vector.NewVec(typ) 808 if vs != nil { 809 for i := range vs { 810 if err := vector.AppendFixed(vec, vs[i], false, m); err != nil { 811 vec.Free(m) 812 return nil 813 } 814 } 815 return vec 816 } 817 for i := 0; i < n; i++ { 818 v := i 819 if random { 820 v = rand.Int() 821 } 822 d := types.Decimal128{B0_63: uint64(v), B64_127: 0} 823 if err := vector.AppendFixed(vec, d, false, m); err != nil { 824 vec.Free(m) 825 return nil 826 } 827 } 828 return vec 829 } 830 831 func NewDateVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 832 vec := vector.NewVec(typ) 833 if vs != nil { 834 for i := range vs { 835 d, err := types.ParseDateCast(vs[i]) 836 if err != nil { 837 return nil 838 } 839 if err := vector.AppendFixed(vec, d, false, m); err != nil { 840 vec.Free(m) 841 return nil 842 } 843 } 844 return vec 845 } 846 for i := 0; i < n; i++ { 847 v := i 848 if random { 849 v = rand.Int() 850 } 851 if err := vector.AppendFixed(vec, types.Date(v), false, m); err != nil { 852 vec.Free(m) 853 return nil 854 } 855 } 856 return vec 857 } 858 859 func NewTimeVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 860 vec := vector.NewVec(typ) 861 if vs != nil { 862 for i := range vs { 863 d, err := types.ParseTime(vs[i], 6) 864 if err != nil { 865 return nil 866 } 867 if err := vector.AppendFixed(vec, d, false, m); err != nil { 868 vec.Free(m) 869 return nil 870 } 871 } 872 return vec 873 } 874 for i := 0; i < n; i++ { 875 v := i 876 if random { 877 v = rand.Int() 878 } 879 if err := vector.AppendFixed(vec, types.Time(v), false, m); err != nil { 880 vec.Free(m) 881 return nil 882 } 883 } 884 return vec 885 } 886 887 func NewDatetimeVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 888 vec := vector.NewVec(typ) 889 if vs != nil { 890 for i := range vs { 891 d, err := types.ParseDatetime(vs[i], 6) 892 if err != nil { 893 return nil 894 } 895 if err := vector.AppendFixed(vec, d, false, m); err != nil { 896 vec.Free(m) 897 return nil 898 } 899 } 900 return vec 901 } 902 for i := 0; i < n; i++ { 903 v := i 904 if random { 905 v = rand.Int() 906 } 907 if err := vector.AppendFixed(vec, types.Datetime(v), false, m); err != nil { 908 vec.Free(m) 909 return nil 910 } 911 } 912 return vec 913 } 914 915 func NewTimestampVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 916 vec := vector.NewVec(typ) 917 if vs != nil { 918 for i := range vs { 919 d, err := types.ParseTimestamp(time.Local, vs[i], 6) 920 if err != nil { 921 return nil 922 } 923 if err := vector.AppendFixed(vec, d, false, m); err != nil { 924 vec.Free(m) 925 return nil 926 } 927 } 928 return vec 929 } 930 for i := 0; i < n; i++ { 931 v := i 932 if random { 933 v = rand.Int() 934 } 935 if err := vector.AppendFixed(vec, types.Timestamp(v), false, m); err != nil { 936 vec.Free(m) 937 return nil 938 } 939 } 940 return vec 941 } 942 943 func NewStringVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector { 944 vec := vector.NewVec(typ) 945 if vs != nil { 946 for i := range vs { 947 if err := vector.AppendBytes(vec, []byte(vs[i]), false, m); err != nil { 948 vec.Free(m) 949 return nil 950 } 951 } 952 return vec 953 } 954 for i := 0; i < n; i++ { 955 v := i 956 if random { 957 v = rand.Int() 958 } 959 if err := vector.AppendBytes(vec, []byte(strconv.Itoa(v)), false, m); err != nil { 960 vec.Free(m) 961 return nil 962 } 963 } 964 return vec 965 } 966 967 func NewArrayVector[T types.RealNumbers](n int, typ types.Type, m *mpool.MPool, random bool, vs [][]T) *vector.Vector { 968 vec := vector.NewVec(typ) 969 if vs != nil { 970 for i := range vs { 971 if err := vector.AppendArray[T](vec, vs[i], false, m); err != nil { 972 vec.Free(m) 973 return nil 974 } 975 } 976 return vec 977 } 978 for i := 0; i < n; i++ { 979 v := i 980 if random { 981 v = rand.Int() 982 } 983 if err := vector.AppendArray[T](vec, []T{T(v), T(v + 1)}, false, m); err != nil { 984 vec.Free(m) 985 return nil 986 } 987 } 988 return vec 989 }