github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/containers/vector_test.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 containers 16 17 import ( 18 "bytes" 19 "testing" 20 "time" 21 22 "github.com/RoaringBitmap/roaring" 23 "github.com/matrixorigin/matrixone/pkg/common/mpool" 24 "github.com/matrixorigin/matrixone/pkg/container/nulls" 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 movec "github.com/matrixorigin/matrixone/pkg/container/vector" 27 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 28 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestTnConst(t *testing.T) { 34 m := mpool.MustNewZero() 35 v := movec.NewConstNull(types.T_int32.ToType(), 20, m) 36 v.IsConstNull() 37 tnv := ToTNVector(v, m) 38 require.True(t, tnv.IsNull(2)) 39 } 40 41 func withAllocator(opt Options) Options { 42 opt.Allocator = mpool.MustNewZero() 43 return opt 44 } 45 46 func TestVectorShallowForeach(t *testing.T) { 47 defer testutils.AfterTest(t)() 48 for _, typ := range []types.Type{types.T_int32.ToType(), types.T_char.ToType()} { 49 vec := MakeVector(typ, common.DefaultAllocator) 50 for i := 0; i < 10; i++ { 51 if i%2 == 0 { 52 vec.Append(nil, true) 53 } else { 54 switch typ.Oid { 55 case types.T_int32: 56 vec.Append(int32(i), false) 57 case types.T_char: 58 vec.Append([]byte("test null"), false) 59 } 60 } 61 } 62 63 vec.Foreach(func(v any, isNull bool, row int) error { 64 if row%2 == 0 { 65 assert.True(t, isNull) 66 } 67 return nil 68 }, nil) 69 } 70 } 71 72 func TestVector1(t *testing.T) { 73 defer testutils.AfterTest(t)() 74 opt := withAllocator(Options{}) 75 vec := NewVector(types.T_int32.ToType(), opt) 76 vec.Append(int32(12), false) 77 vec.Append(int32(32), false) 78 vec.AppendMany([]any{int32(1), int32(100)}, []bool{false, false}) 79 assert.Equal(t, 4, vec.Length()) 80 assert.Equal(t, int32(12), vec.Get(0).(int32)) 81 assert.Equal(t, int32(32), vec.Get(1).(int32)) 82 assert.Equal(t, int32(1), vec.Get(2).(int32)) 83 assert.Equal(t, int32(100), vec.Get(3).(int32)) 84 vec2 := NewVector(types.T_int32.ToType()) 85 vec2.Extend(vec) 86 assert.Equal(t, 4, vec2.Length()) 87 assert.Equal(t, int32(12), vec2.Get(0).(int32)) 88 assert.Equal(t, int32(32), vec2.Get(1).(int32)) 89 assert.Equal(t, int32(1), vec2.Get(2).(int32)) 90 assert.Equal(t, int32(100), vec2.Get(3).(int32)) 91 vec.Close() 92 vec2.Close() 93 // XXX MPOOL 94 // alloc := vec.GetMPool() 95 // assert.Equal(t, 0, alloc.CurrNB()) 96 } 97 98 func TestVector2(t *testing.T) { 99 defer testutils.AfterTest(t)() 100 opt := withAllocator(Options{}) 101 vec := NewVector(types.T_int64.ToType(), opt) 102 t.Log(vec.String()) 103 now := time.Now() 104 for i := 10; i > 0; i-- { 105 vec.Append(int64(i), false) 106 } 107 t.Log(time.Since(now)) 108 assert.Equal(t, 10, vec.Length()) 109 assert.False(t, vec.HasNull()) 110 vec.Append(nil, true) 111 assert.Equal(t, 11, vec.Length()) 112 assert.True(t, vec.HasNull()) 113 assert.True(t, vec.IsNull(10)) 114 115 vec.Update(2, nil, true) 116 assert.Equal(t, 11, vec.Length()) 117 assert.True(t, vec.HasNull()) 118 assert.True(t, vec.IsNull(10)) 119 assert.True(t, vec.IsNull(2)) 120 121 vec.Update(2, int64(22), false) 122 assert.True(t, vec.HasNull()) 123 assert.True(t, vec.IsNull(10)) 124 assert.False(t, vec.IsNull(2)) 125 assert.Equal(t, any(int64(22)), vec.Get(2)) 126 127 vec.Update(10, int64(100), false) 128 assert.False(t, vec.HasNull()) 129 assert.False(t, vec.IsNull(10)) 130 assert.False(t, vec.IsNull(2)) 131 assert.Equal(t, any(int64(22)), vec.Get(2)) 132 assert.Equal(t, any(int64(100)), vec.Get(10)) 133 134 t.Log(vec.String()) 135 136 vec.Close() 137 assert.Zero(t, opt.Allocator.CurrNB()) 138 139 // vec2 := compute.MockVec(vec.GetType(), 0, 0) 140 // now = time.Now() 141 // for i := 1000000; i > 0; i-- { 142 // compute.AppendValue(vec2, int64(i)) 143 // } 144 // t.Log(time.Since(now)) 145 146 // vec3 := container.NewVector[int64](opt) 147 // now = time.Now() 148 // for i := 1000000; i > 0; i-- { 149 // vec3.Append(int64(i)) 150 // } 151 // t.Log(time.Since(now)) 152 } 153 154 func TestVector3(t *testing.T) { 155 defer testutils.AfterTest(t)() 156 opts := withAllocator(Options{}) 157 vec1 := NewVector(types.T_int32.ToType(), opts) 158 for i := 0; i < 100; i++ { 159 vec1.Append(int32(i), false) 160 } 161 162 w := new(bytes.Buffer) 163 _, err := vec1.WriteTo(w) 164 assert.NoError(t, err) 165 166 r := bytes.NewBuffer(w.Bytes()) 167 168 vec2 := NewVector(types.T_int32.ToType(), opts) 169 _, err = vec2.ReadFrom(r) 170 assert.NoError(t, err) 171 172 assert.True(t, vec1.Equals(vec2)) 173 174 // t.Log(vec1.String()) 175 // t.Log(vec2.String()) 176 vec1.Close() 177 vec2.Close() 178 assert.Zero(t, opts.Allocator.CurrNB()) 179 } 180 181 func TestVector5(t *testing.T) { 182 defer testutils.AfterTest(t)() 183 vecTypes := types.MockColTypes() 184 sels := nulls.NewWithSize(1) 185 sels.Add(uint64(2), uint64(6)) 186 for _, vecType := range vecTypes { 187 vec := MockVector(vecType, 10, false, nil) 188 rows := make([]int, 0) 189 op := func(v any, _ bool, row int) (err error) { 190 rows = append(rows, row) 191 assert.Equal(t, vec.Get(row), v) 192 return 193 } 194 _ = vec.Foreach(op, nil) 195 assert.Equal(t, 10, len(rows)) 196 for i, e := range rows { 197 assert.Equal(t, i, e) 198 } 199 200 rows = rows[:0] 201 _ = vec.Foreach(op, sels) 202 assert.Equal(t, 2, len(rows)) 203 assert.Equal(t, 2, rows[0]) 204 assert.Equal(t, 6, rows[1]) 205 206 rows = rows[:0] 207 _ = vec.ForeachWindow(2, 6, op, nil) 208 assert.Equal(t, []int{2, 3, 4, 5, 6, 7}, rows) 209 rows = rows[:0] 210 _ = vec.ForeachWindow(2, 6, op, sels) 211 assert.Equal(t, []int{2, 6}, rows) 212 rows = rows[:0] 213 _ = vec.ForeachWindow(3, 6, op, sels) 214 assert.Equal(t, []int{6}, rows) 215 rows = rows[:0] 216 _ = vec.ForeachWindow(3, 3, op, sels) 217 assert.Equal(t, []int{}, rows) 218 219 vec.Close() 220 } 221 } 222 223 func TestVector6(t *testing.T) { 224 defer testutils.AfterTest(t)() 225 vecTypes := types.MockColTypes() 226 sels := nulls.NewWithSize(1) 227 sels.Add(uint64(2), uint64(6)) 228 f := func(vecType types.Type, nullable bool) { 229 vec := MockVector(vecType, 10, false, nil) 230 if nullable { 231 vec.Update(4, nil, true) 232 } 233 bias := 0 234 win := vec.Window(bias, 8) 235 assert.Equal(t, 8, win.Length()) 236 rows := make([]int, 0) 237 op := func(v any, _ bool, row int) (err error) { 238 rows = append(rows, row) 239 assert.Equal(t, vec.Get(row+bias), v) 240 return 241 } 242 _ = win.Foreach(op, nil) 243 assert.Equal(t, 8, len(rows)) 244 assert.Equal(t, []int{0, 1, 2, 3, 4, 5, 6, 7}, rows) 245 rows = rows[:0] 246 _ = win.ForeachWindow(2, 3, op, nil) 247 assert.Equal(t, 3, len(rows)) 248 assert.Equal(t, []int{2, 3, 4}, rows) 249 250 rows = rows[:0] 251 _ = win.Foreach(op, sels) 252 assert.Equal(t, 2, len(rows)) 253 assert.Equal(t, 2, rows[0]) 254 assert.Equal(t, 6, rows[1]) 255 256 rows = rows[:0] 257 _ = win.ForeachWindow(2, 6, op, sels) 258 assert.Equal(t, []int{2, 6}, rows) 259 rows = rows[:0] 260 _ = win.ForeachWindow(3, 4, op, sels) 261 assert.Equal(t, []int{6}, rows) 262 rows = rows[:0] 263 _ = win.ForeachWindow(3, 3, op, sels) 264 assert.Equal(t, []int{}, rows) 265 266 bias = 1 267 win = vec.Window(bias, 8) 268 269 op2 := func(v any, _ bool, row int) (err error) { 270 rows = append(rows, row) 271 // t.Logf("row=%d,v=%v", row, v) 272 // t.Logf("row=%d, winv=%v", row, win.Get(row)) 273 // t.Logf("row+bias=%d, rawv=%v", row+bias, vec.Get(row+bias)) 274 assert.Equal(t, vec.Get(row+bias), v) 275 assert.Equal(t, win.Get(row), v) 276 return 277 } 278 rows = rows[:0] 279 _ = win.Foreach(op2, nil) 280 assert.Equal(t, 8, len(rows)) 281 assert.Equal(t, []int{0, 1, 2, 3, 4, 5, 6, 7}, rows) 282 rows = rows[:0] 283 _ = win.ForeachWindow(2, 3, op, nil) 284 assert.Equal(t, 3, len(rows)) 285 assert.Equal(t, []int{2, 3, 4}, rows) 286 rows = rows[:0] 287 _ = win.Foreach(op, sels) 288 assert.Equal(t, 2, len(rows)) 289 assert.Equal(t, 2, rows[0]) 290 assert.Equal(t, 6, rows[1]) 291 292 rows = rows[:0] 293 _ = win.ForeachWindow(2, 6, op, sels) 294 assert.Equal(t, []int{2, 6}, rows) 295 rows = rows[:0] 296 _ = win.ForeachWindow(3, 4, op, sels) 297 assert.Equal(t, []int{6}, rows) 298 rows = rows[:0] 299 _ = win.ForeachWindow(3, 3, op, sels) 300 assert.Equal(t, []int{}, rows) 301 302 vec.Close() 303 } 304 for _, vecType := range vecTypes { 305 f(vecType, false) 306 f(vecType, true) 307 } 308 } 309 310 func TestVector7(t *testing.T) { 311 defer testutils.AfterTest(t)() 312 vecTypes := types.MockColTypes() 313 testF := func(typ types.Type, nullable bool) { 314 vec := MockVector(typ, 10, false, nil) 315 if nullable { 316 vec.Append(nil, true) 317 } 318 vec2 := MockVector(typ, 10, false, nil) 319 vec3 := MakeVector(typ, common.DefaultAllocator) 320 vec3.Extend(vec) 321 assert.Equal(t, vec.Length(), vec3.Length()) 322 vec3.Extend(vec2) 323 assert.Equal(t, vec.Length()+vec2.Length(), vec3.Length()) 324 for i := 0; i < vec3.Length(); i++ { 325 if i >= vec.Length() { 326 assert.Equal(t, vec2.Get(i-vec.Length()), vec3.Get(i)) 327 } else { 328 if vec.IsNull(i) { 329 assert.Equal(t, true, vec3.IsNull(i)) 330 } else { 331 assert.Equal(t, vec.Get(i), vec3.Get(i)) 332 } 333 } 334 } 335 336 vec4 := MakeVector(typ, common.DefaultAllocator) 337 cnt := 5 338 if nullable { 339 cnt = 6 340 } 341 vec4.ExtendWithOffset(vec, 5, cnt) 342 assert.Equal(t, cnt, vec4.Length()) 343 // t.Log(vec4.String()) 344 // t.Log(vec.String()) 345 for i := 0; i < cnt; i++ { 346 assert.Equal(t, vec.Get(i+5), vec4.Get(i)) 347 } 348 349 vec.Close() 350 vec2.Close() 351 vec3.Close() 352 } 353 for _, typ := range vecTypes { 354 testF(typ, true) 355 testF(typ, false) 356 } 357 } 358 359 func TestVector8(t *testing.T) { 360 defer testutils.AfterTest(t)() 361 vec := MakeVector(types.T_int32.ToType(), common.DefaultAllocator) 362 defer vec.Close() 363 vec.Append(int32(0), false) 364 vec.Append(int32(1), false) 365 vec.Append(int32(2), false) 366 vec.Append(nil, true) 367 vec.Append(int32(4), false) 368 vec.Append(int32(5), false) 369 assert.True(t, vec.IsNull(3)) 370 vec.Delete(1) 371 assert.True(t, vec.IsNull(2)) 372 vec.Delete(3) 373 assert.True(t, vec.IsNull(2)) 374 vec.Update(1, nil, true) 375 assert.True(t, vec.IsNull(1)) 376 assert.True(t, vec.IsNull(2)) 377 vec.Append(nil, true) 378 assert.True(t, vec.IsNull(1)) 379 assert.True(t, vec.IsNull(2)) 380 assert.True(t, vec.IsNull(4)) 381 vec.Compact(roaring.BitmapOf(0, 2)) 382 assert.Equal(t, 3, vec.Length()) 383 assert.True(t, vec.IsNull(0)) 384 assert.True(t, vec.IsNull(2)) 385 t.Log(vec.String()) 386 } 387 388 func TestVector9(t *testing.T) { 389 defer testutils.AfterTest(t)() 390 opts := withAllocator(Options{}) 391 vec := NewVector(types.T_varchar.ToType(), opts) 392 vec.Append([]byte("h1"), false) 393 vec.Append([]byte("h22"), false) 394 vec.Append([]byte("h333"), false) 395 vec.Append(nil, true) 396 vec.Append([]byte("h4444"), false) 397 398 cloned := vec.CloneWindow(2, 2) 399 assert.Equal(t, 2, cloned.Length()) 400 assert.Equal(t, vec.Get(2), cloned.Get(0)) 401 assert.Equal(t, vec.Get(3), cloned.Get(1)) 402 cloned.Close() 403 vec.Close() 404 assert.Zero(t, opts.Allocator.CurrNB()) 405 } 406 407 func TestCompact(t *testing.T) { 408 defer testutils.AfterTest(t)() 409 opts := withAllocator(Options{}) 410 vec := NewVector(types.T_varchar.ToType(), opts) 411 412 vec.Append(nil, true) 413 t.Log(vec.String()) 414 deletes := roaring.BitmapOf(0) 415 //{null} 416 vec.Compact(deletes) 417 //{} 418 assert.Equal(t, 0, vec.Length()) 419 420 vec.Append(nil, true) 421 vec.Append(nil, true) 422 vec.Append(nil, true) 423 deletes = roaring.BitmapOf(0, 1) 424 //{n,n,n} 425 vec.Compact(deletes) 426 //{n} 427 assert.Equal(t, 1, vec.Length()) 428 assert.True(t, vec.IsNull(0)) 429 430 vec.Append([]byte("var"), false) 431 vec.Append(nil, true) 432 vec.Append([]byte("var"), false) 433 vec.Append(nil, true) 434 //{null,var,null,var,null} 435 deletes = roaring.BitmapOf(1, 3) 436 vec.Compact(deletes) 437 //{null,null,null} 438 assert.Equal(t, 3, vec.Length()) 439 assert.True(t, vec.IsNull(0)) 440 assert.True(t, vec.IsNull(1)) 441 assert.True(t, vec.IsNull(2)) 442 vec.Close() 443 } 444 445 func BenchmarkVectorExtend(t *testing.B) { 446 vec1 := MockVector(types.T_int32.ToType(), 0, true, nil) 447 vec2 := MockVector(types.T_int32.ToType(), 1, true, nil) 448 defer vec1.Close() 449 defer vec2.Close() 450 451 t.ResetTimer() 452 for i := 0; i < t.N; i++ { 453 vec1.Extend(vec2) 454 } 455 } 456 457 func TestForeachWindowFixed(t *testing.T) { 458 vec1 := MockVector(types.T_uint32.ToType(), 2, false, nil) 459 defer vec1.Close() 460 vec1.Append(nil, true) 461 462 cnt := 0 463 op := func(v uint32, isNull bool, row int) (err error) { 464 t.Logf("v=%v,null=%v,row=%d", v, isNull, row) 465 cnt++ 466 if cnt == vec1.Length() { 467 assert.True(t, isNull) 468 } else { 469 assert.Equal(t, vec1.Get(row).(uint32), v) 470 } 471 return 472 } 473 ForeachWindowFixed(vec1.GetDownstreamVector(), 0, vec1.Length(), op, nil, nil) 474 assert.Equal(t, vec1.Length(), cnt) 475 } 476 477 func TestForeachWindowBytes(t *testing.T) { 478 vec1 := MockVector(types.T_varchar.ToType(), 2, false, nil) 479 defer vec1.Close() 480 vec1.Append(nil, true) 481 482 cnt := 0 483 op := func(v []byte, isNull bool, row int) (err error) { 484 t.Logf("v=%v,null=%v,row=%d", v, isNull, row) 485 cnt++ 486 if cnt == vec1.Length() { 487 assert.True(t, isNull) 488 } else { 489 assert.Equal(t, 0, bytes.Compare(v, vec1.Get(row).([]byte))) 490 } 491 return 492 } 493 ForeachWindowVarlen(vec1.GetDownstreamVector(), 0, vec1.Length(), op, nil, nil) 494 assert.Equal(t, vec1.Length(), cnt) 495 } 496 497 func BenchmarkForeachVector(b *testing.B) { 498 rows := 1000 499 int64s := MockVector2(types.T_int64.ToType(), rows, 0) 500 defer int64s.Close() 501 b.Run("int64-old", func(b *testing.B) { 502 b.ResetTimer() 503 for i := 0; i < b.N; i++ { 504 int64s.Foreach(func(any, bool, int) error { 505 return nil 506 }, nil) 507 } 508 }) 509 b.Run("int64-new", func(b *testing.B) { 510 b.ResetTimer() 511 for i := 0; i < b.N; i++ { 512 ForeachVectorWindow(int64s, 0, rows, func(int64, bool, int) (err error) { 513 return 514 }, nil, nil) 515 } 516 }) 517 518 chars := MockVector2(types.T_varchar.ToType(), rows, 0) 519 defer chars.Close() 520 b.Run("chars-old", func(b *testing.B) { 521 b.ResetTimer() 522 for i := 0; i < b.N; i++ { 523 chars.Foreach(func(any, bool, int) error { 524 return nil 525 }, nil) 526 } 527 }) 528 b.Run("chars-new", func(b *testing.B) { 529 b.ResetTimer() 530 for i := 0; i < b.N; i++ { 531 ForeachVectorWindow(chars, 0, rows, func([]byte, bool, int) (err error) { 532 return 533 }, nil, nil) 534 } 535 }) 536 } 537 538 func BenchmarkForeachVectorBytes(b *testing.B) { 539 rows := 1000 540 vec := MockVector2(types.T_int64.ToType(), rows, 0) 541 defer vec.Close() 542 b.Run("int64-bytes", func(b *testing.B) { 543 b.ResetTimer() 544 for i := 0; i < b.N; i++ { 545 ForeachWindowBytes(vec.GetDownstreamVector(), 0, vec.Length(), func(v []byte, isNull bool, row int) (err error) { 546 return 547 }, nil) 548 } 549 }) 550 b.Run("int64-old", func(b *testing.B) { 551 b.ResetTimer() 552 for i := 0; i < b.N; i++ { 553 vec.Foreach(func(any, bool, int) error { 554 return nil 555 }, nil) 556 } 557 }) 558 } 559 560 func testFunc[T any](args ...any) func(T, bool, int) error { 561 return func(v T, isNull bool, row int) (err error) { 562 return 563 } 564 } 565 566 func BenchmarkFunctions(b *testing.B) { 567 var funcs = map[types.T]any{ 568 types.T_bool: testFunc[bool], 569 types.T_int32: testFunc[int32], 570 types.T_char: testFunc[[]byte], 571 } 572 vec := MockVector2(types.T_char.ToType(), 1000, 0) 573 defer vec.Close() 574 b.Run("func-new", func(b *testing.B) { 575 b.ResetTimer() 576 for i := 0; i < b.N; i++ { 577 ForeachVectorWindow(vec, 0, vec.Length(), MakeForeachVectorOp(vec.GetType().Oid, funcs), nil, nil) 578 } 579 }) 580 b.Run("func-old", func(b *testing.B) { 581 b.ResetTimer() 582 for i := 0; i < b.N; i++ { 583 vec.Foreach(func(any, bool, int) (err error) { 584 return 585 }, nil) 586 } 587 }) 588 } 589 590 func getOverload(typ types.T, t *testing.T, rows *roaring.Bitmap, vec Vector) any { 591 switch typ { 592 case types.T_bool: 593 return overLoadFactory[bool](t, rows, vec) 594 case types.T_bit: 595 return overLoadFactory[uint64](t, rows, vec) 596 case types.T_int8: 597 return overLoadFactory[int8](t, rows, vec) 598 case types.T_int16: 599 return overLoadFactory[int16](t, rows, vec) 600 case types.T_int32: 601 return overLoadFactory[int32](t, rows, vec) 602 case types.T_int64: 603 return overLoadFactory[int64](t, rows, vec) 604 case types.T_uint8: 605 return overLoadFactory[uint8](t, rows, vec) 606 case types.T_uint16: 607 return overLoadFactory[uint16](t, rows, vec) 608 case types.T_uint32: 609 return overLoadFactory[uint32](t, rows, vec) 610 case types.T_uint64: 611 return overLoadFactory[uint64](t, rows, vec) 612 case types.T_float32: 613 return overLoadFactory[float32](t, rows, vec) 614 case types.T_float64: 615 return overLoadFactory[float64](t, rows, vec) 616 case types.T_timestamp: 617 return overLoadFactory[types.Timestamp](t, rows, vec) 618 case types.T_date: 619 return overLoadFactory[types.Date](t, rows, vec) 620 case types.T_time: 621 return overLoadFactory[types.Time](t, rows, vec) 622 case types.T_datetime: 623 return overLoadFactory[types.Datetime](t, rows, vec) 624 case types.T_decimal64: 625 return overLoadFactory[types.Decimal64](t, rows, vec) 626 case types.T_decimal128: 627 return overLoadFactory[types.Decimal128](t, rows, vec) 628 case types.T_decimal256: 629 return overLoadFactory[types.Decimal256](t, rows, vec) 630 case types.T_TS: 631 return overLoadFactory[types.TS](t, rows, vec) 632 case types.T_Rowid: 633 return overLoadFactory[types.Rowid](t, rows, vec) 634 case types.T_Blockid: 635 return overLoadFactory[types.Blockid](t, rows, vec) 636 case types.T_uuid: 637 return overLoadFactory[types.Uuid](t, rows, vec) 638 case types.T_enum: 639 return overLoadFactory[types.Enum](t, rows, vec) 640 case types.T_char, types.T_varchar, types.T_blob, types.T_binary, types.T_varbinary, types.T_json, types.T_text, 641 types.T_array_float32, types.T_array_float64: 642 return overLoadFactory[[]byte](t, rows, vec) 643 default: 644 panic("unsupport") 645 } 646 } 647 648 func overLoadFactory[T any](t *testing.T, rows *roaring.Bitmap, vec Vector) func(v T, _ bool, row int) error { 649 return func(v T, _ bool, row int) (err error) { 650 rows.Add(uint32(row)) 651 assert.Equal(t, vec.Get(row), v) 652 return 653 } 654 } 655 656 func TestForeachSelectBitmap(t *testing.T) { 657 defer testutils.AfterTest(t)() 658 vecTypes := types.MockColTypes() 659 sels := nulls.NewWithSize(1) 660 sels.Add(uint64(2), uint64(6)) 661 f := func(vecType types.Type, nullable bool) { 662 vec := MockVector(vecType, 10, false, nil) 663 rows := roaring.New() 664 op := getOverload(vecType.Oid, t, rows, vec) 665 666 ForeachVectorWindow(vec, 0, vec.Length(), op, nil, sels) 667 assert.Equal(t, uint64(2), rows.GetCardinality()) 668 assert.True(t, rows.Contains(2)) 669 assert.True(t, rows.Contains(6)) 670 } 671 for _, vecType := range vecTypes { 672 f(vecType, false) 673 } 674 675 vec2 := MockVector(types.T_int32.ToType(), 10, true, nil) 676 defer vec2.Close() 677 678 _ = ForeachVectorWindow(vec2, 0, 5, nil, func(_ any, _ bool, _ int) (err error) { 679 return 680 }, nil) 681 } 682 683 func TestVectorPool(t *testing.T) { 684 cnt := 10 685 pool := NewVectorPool(t.Name(), cnt) 686 assert.Equal(t, cnt, len(pool.fixSizedPool)+len(pool.varlenPool)) 687 688 allTypes := []types.Type{ 689 types.T_bool.ToType(), 690 types.T_int32.ToType(), 691 types.T_uint8.ToType(), 692 types.T_float32.ToType(), 693 types.T_datetime.ToType(), 694 types.T_date.ToType(), 695 types.T_decimal64.ToType(), 696 types.T_char.ToType(), 697 types.T_char.ToType(), 698 types.T_varchar.ToType(), 699 types.T_varchar.ToType(), 700 types.T_json.ToType(), 701 } 702 vecs := make([]*vectorWrapper, 0, len(allTypes)) 703 for _, typ := range allTypes { 704 vec := pool.GetVector(&typ) 705 assert.Equal(t, *vec.GetType(), typ) 706 vecs = append(vecs, vec) 707 } 708 709 assert.Equal(t, 0, pool.Allocated()) 710 711 for _, vec := range vecs { 712 vec.PreExtend(100) 713 } 714 715 allocated := pool.Allocated() 716 717 if pool.ratio >= 0.6 && pool.ratio < 0.7 { 718 t.Log(pool.String()) 719 usedCnt, _ := pool.FixedSizeUsed(false) 720 assert.GreaterOrEqual(t, 6, usedCnt) 721 usedCnt, _ = pool.VarlenUsed(false) 722 assert.GreaterOrEqual(t, 4, usedCnt) 723 usedCnt, _ = pool.Used(false) 724 assert.GreaterOrEqual(t, 10, usedCnt) 725 } 726 727 for _, vec := range vecs { 728 vec.Close() 729 } 730 731 t.Log(pool.String()) 732 assert.Equal(t, allocated, pool.Allocated()) 733 } 734 735 func TestVectorPool2(t *testing.T) { 736 pool := NewVectorPool(t.Name(), 10, WithAllocationLimit(1024)) 737 typ := types.T_int32.ToType() 738 vec := pool.GetVector(&typ) 739 vec.PreExtend(300) 740 t.Log(pool.String()) 741 assert.Less(t, 1024, pool.Allocated()) 742 vec.Close() 743 assert.Equal(t, 0, pool.Allocated()) 744 } 745 746 func TestVectorPool3(t *testing.T) { 747 pool := NewVectorPool(t.Name(), 10) 748 typ := types.T_int32.ToType() 749 vec1 := NewVector(typ) 750 vec1.Append(int32(1), false) 751 vec1.Append(int32(2), false) 752 vec1.Append(int32(3), false) 753 754 vec2 := vec1.CloneWindowWithPool(0, 3, pool) 755 t.Log(vec2.PPString(0)) 756 vec1.Close() 757 vec2.Close() 758 } 759 760 func TestConstNullVector(t *testing.T) { 761 vec := NewConstNullVector(types.T_int32.ToType(), 10, common.DefaultAllocator) 762 defer vec.Close() 763 assert.Equal(t, 10, vec.Length()) 764 assert.True(t, vec.IsConstNull()) 765 766 for i := 0; i < vec.Length(); i++ { 767 assert.True(t, vec.IsNull(i)) 768 } 769 770 var w bytes.Buffer 771 _, err := vec.WriteTo(&w) 772 assert.NoError(t, err) 773 774 vec2 := MakeVector(types.T_int32.ToType(), common.DefaultAllocator) 775 defer vec2.Close() 776 _, err = vec2.ReadFrom(&w) 777 assert.NoError(t, err) 778 assert.Equal(t, 10, vec2.Length()) 779 assert.True(t, vec2.IsConstNull()) 780 for i := 0; i < vec2.Length(); i++ { 781 assert.True(t, vec2.IsNull(i)) 782 } 783 784 vecw := vec.Window(0, 5) 785 assert.Equal(t, 5, vecw.Length()) 786 assert.True(t, vecw.IsConstNull()) 787 for i := 0; i < vecw.Length(); i++ { 788 assert.True(t, vecw.IsNull(i)) 789 } 790 } 791 792 func TestConstVector(t *testing.T) { 793 vec := NewConstFixed[int32](types.T_int32.ToType(), int32(1), 10) 794 defer vec.Close() 795 796 assert.Equal(t, 10, vec.Length()) 797 assert.True(t, vec.IsConst()) 798 assert.False(t, vec.IsConstNull()) 799 800 for i := 0; i < vec.Length(); i++ { 801 v := vec.Get(i).(int32) 802 assert.Equal(t, int32(1), v) 803 } 804 805 vec2 := NewConstBytes(types.T_char.ToType(), []byte("abc"), 10) 806 defer vec2.Close() 807 assert.Equal(t, 10, vec2.Length()) 808 assert.True(t, vec2.IsConst()) 809 assert.False(t, vec2.IsConstNull()) 810 811 for i := 0; i < vec2.Length(); i++ { 812 assert.Equal(t, []byte("abc"), vec2.Get(i).([]byte)) 813 } 814 815 var w bytes.Buffer 816 _, err := vec.WriteTo(&w) 817 assert.NoError(t, err) 818 819 vec3 := MakeVector(types.T_int32.ToType(), common.DefaultAllocator) 820 defer vec3.Close() 821 _, err = vec3.ReadFrom(&w) 822 assert.NoError(t, err) 823 assert.True(t, vec3.IsConst()) 824 assert.False(t, vec3.IsConstNull()) 825 assert.Equal(t, 10, vec3.Length()) 826 for i := 0; i < vec3.Length(); i++ { 827 assert.Equal(t, int32(1), vec3.Get(i).(int32)) 828 } 829 830 w.Reset() 831 _, err = vec2.WriteTo(&w) 832 assert.NoError(t, err) 833 834 vec4 := MakeVector(types.T_char.ToType(), common.DefaultAllocator) 835 defer vec4.Close() 836 _, err = vec4.ReadFrom(&w) 837 assert.NoError(t, err) 838 assert.True(t, vec4.IsConst()) 839 assert.False(t, vec4.IsConstNull()) 840 assert.Equal(t, 10, vec4.Length()) 841 for i := 0; i < vec4.Length(); i++ { 842 assert.Equal(t, []byte("abc"), vec4.Get(i).([]byte)) 843 } 844 845 vecw := vec.Window(0, 5) 846 assert.Equal(t, 5, vecw.Length()) 847 assert.True(t, vecw.IsConst()) 848 assert.False(t, vecw.IsConstNull()) 849 for i := 0; i < vecw.Length(); i++ { 850 assert.Equal(t, int32(1), vecw.Get(i).(int32)) 851 } 852 853 vecw4 := vec4.Window(0, 5) 854 assert.Equal(t, 5, vecw4.Length()) 855 assert.True(t, vecw4.IsConst()) 856 assert.False(t, vecw4.IsConstNull()) 857 for i := 0; i < vecw4.Length(); i++ { 858 assert.Equal(t, []byte("abc"), vecw4.Get(i).([]byte)) 859 } 860 861 }