github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/lockop/fetch_test.go (about) 1 // Copyright 2023 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 lockop 16 17 import ( 18 "bytes" 19 "math" 20 "testing" 21 22 "github.com/matrixorigin/matrixone/pkg/common/mpool" 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 "github.com/matrixorigin/matrixone/pkg/container/vector" 25 "github.com/matrixorigin/matrixone/pkg/pb/lock" 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestFetchBoolRows(t *testing.T) { 31 values := []bool{false, true} 32 runFetchRowsTest( 33 t, 34 types.New(types.T_bool, 0, 0), 35 values, 36 lock.Granularity_Range, 37 values, 38 values, 39 values, 40 func(packer *types.Packer, v bool) { 41 packer.EncodeBool(v) 42 }, 43 nil, 44 nil, 45 false, 46 ) 47 } 48 49 func TestFetchBitRows(t *testing.T) { 50 values := []uint64{1, 0} 51 expectRangeValues := []uint64{0, 1} 52 runFetchRowsTest( 53 t, 54 types.New(types.T_bit, 0, 0), 55 values, 56 lock.Granularity_Row, 57 values, 58 expectRangeValues, 59 []uint64{0, math.MaxUint64}, 60 func(packer *types.Packer, v uint64) { 61 packer.EncodeUint64(v) 62 }, 63 nil, 64 nil, 65 false, 66 ) 67 } 68 69 func TestFetchBitRowsWithFilter(t *testing.T) { 70 values := []uint64{1, 0, 2} 71 expectRangeValues := []uint64{0, 1} 72 runFetchRowsTest( 73 t, 74 types.New(types.T_bit, 0, 0), 75 values, 76 lock.Granularity_Row, 77 values[:2], 78 expectRangeValues, 79 []uint64{0, math.MaxUint64}, 80 func(packer *types.Packer, v uint64) { 81 packer.EncodeUint64(v) 82 }, 83 getRowsFilter(1, []uint64{1, 2}), 84 []int32{0, 0, 1}, 85 false, 86 ) 87 } 88 89 func TestFetchBitRowsWithFilterAll(t *testing.T) { 90 values := []uint64{1, 0, 2} 91 expectRangeValues := []uint64{0, 1} 92 runFetchRowsTest( 93 t, 94 types.New(types.T_bit, 0, 0), 95 values, 96 lock.Granularity_Row, 97 values[:2], 98 expectRangeValues, 99 []uint64{0, math.MaxUint64}, 100 func(packer *types.Packer, v uint64) { 101 packer.EncodeUint64(v) 102 }, 103 getRowsFilter(1, []uint64{1, 2}), 104 []int32{1, 1, 1}, 105 true, 106 ) 107 } 108 109 func TestFetchInt8Rows(t *testing.T) { 110 values := []int8{1, 0} 111 expectRangeValues := []int8{0, 1} 112 runFetchRowsTest( 113 t, 114 types.New(types.T_int8, 0, 0), 115 values, 116 lock.Granularity_Row, 117 values, 118 expectRangeValues, 119 []int8{math.MinInt8, math.MaxInt8}, 120 func(packer *types.Packer, v int8) { 121 packer.EncodeInt8(v) 122 }, 123 nil, 124 nil, 125 false, 126 ) 127 } 128 129 func TestFetchInt8RowsWithFilter(t *testing.T) { 130 values := []int8{1, 0, 2} 131 expectRangeValues := []int8{0, 1} 132 runFetchRowsTest( 133 t, 134 types.New(types.T_int8, 0, 0), 135 values, 136 lock.Granularity_Row, 137 values[:2], 138 expectRangeValues, 139 []int8{math.MinInt8, math.MaxInt8}, 140 func(packer *types.Packer, v int8) { 141 packer.EncodeInt8(v) 142 }, 143 getRowsFilter(1, []uint64{1, 2}), 144 []int32{0, 0, 1}, 145 false, 146 ) 147 } 148 149 func TestFetchInt8RowsWithFilterAll(t *testing.T) { 150 values := []int8{1, 0, 2} 151 expectRangeValues := []int8{0, 1} 152 runFetchRowsTest( 153 t, 154 types.New(types.T_int8, 0, 0), 155 values, 156 lock.Granularity_Row, 157 values[:2], 158 expectRangeValues, 159 []int8{math.MinInt8, math.MaxInt8}, 160 func(packer *types.Packer, v int8) { 161 packer.EncodeInt8(v) 162 }, 163 getRowsFilter(1, []uint64{1, 2}), 164 []int32{1, 1, 1}, 165 true, 166 ) 167 } 168 169 func TestFetchInt16Rows(t *testing.T) { 170 values := []int16{1, 0} 171 expectRangeValues := []int16{0, 1} 172 runFetchRowsTest( 173 t, 174 types.New(types.T_int16, 0, 0), 175 values, 176 lock.Granularity_Row, 177 values, 178 expectRangeValues, 179 []int16{math.MinInt16, math.MaxInt16}, 180 func(packer *types.Packer, v int16) { 181 packer.EncodeInt16(v) 182 }, 183 nil, 184 nil, 185 false, 186 ) 187 } 188 189 func TestFetchInt16RowsWithFilter(t *testing.T) { 190 values := []int16{1, 0, 2} 191 expectRangeValues := []int16{0, 1} 192 runFetchRowsTest( 193 t, 194 types.New(types.T_int16, 0, 0), 195 values, 196 lock.Granularity_Row, 197 values[:2], 198 expectRangeValues, 199 []int16{math.MinInt16, math.MaxInt16}, 200 func(packer *types.Packer, v int16) { 201 packer.EncodeInt16(v) 202 }, 203 getRowsFilter(1, []uint64{1, 2}), 204 []int32{0, 0, 1}, 205 false, 206 ) 207 } 208 209 func TestFetchInt16RowsWithFilterAll(t *testing.T) { 210 values := []int16{1, 0, 2} 211 expectRangeValues := []int16{0, 1} 212 runFetchRowsTest( 213 t, 214 types.New(types.T_int16, 0, 0), 215 values, 216 lock.Granularity_Row, 217 values[:2], 218 expectRangeValues, 219 []int16{math.MinInt16, math.MaxInt16}, 220 func(packer *types.Packer, v int16) { 221 packer.EncodeInt16(v) 222 }, 223 getRowsFilter(1, []uint64{1, 2}), 224 []int32{1, 1, 1}, 225 true, 226 ) 227 } 228 229 func TestFetchInt32Rows(t *testing.T) { 230 values := []int32{1, 0} 231 expectRangeValues := []int32{0, 1} 232 runFetchRowsTest( 233 t, 234 types.New(types.T_int32, 0, 0), 235 values, 236 lock.Granularity_Row, 237 values, 238 expectRangeValues, 239 []int32{math.MinInt32, math.MaxInt32}, 240 func(packer *types.Packer, v int32) { 241 packer.EncodeInt32(v) 242 }, 243 nil, 244 nil, 245 false, 246 ) 247 } 248 249 func TestFetchInt32RowsWithFilter(t *testing.T) { 250 values := []int32{1, 0, 2} 251 expectRangeValues := []int32{0, 1} 252 runFetchRowsTest( 253 t, 254 types.New(types.T_int32, 0, 0), 255 values, 256 lock.Granularity_Row, 257 values[:2], 258 expectRangeValues, 259 []int32{math.MinInt32, math.MaxInt32}, 260 func(packer *types.Packer, v int32) { 261 packer.EncodeInt32(v) 262 }, 263 getRowsFilter(1, []uint64{1, 2}), 264 []int32{0, 0, 1}, 265 false, 266 ) 267 } 268 269 func TestFetchInt32RowsWithFilterAll(t *testing.T) { 270 values := []int32{1, 0, 2} 271 expectRangeValues := []int32{0, 1} 272 runFetchRowsTest( 273 t, 274 types.New(types.T_int32, 0, 0), 275 values, 276 lock.Granularity_Row, 277 values[:2], 278 expectRangeValues, 279 []int32{math.MinInt32, math.MaxInt32}, 280 func(packer *types.Packer, v int32) { 281 packer.EncodeInt32(v) 282 }, 283 getRowsFilter(1, []uint64{1, 2}), 284 []int32{1, 1, 1}, 285 true, 286 ) 287 } 288 289 func TestFetchInt64Rows(t *testing.T) { 290 values := []int64{1, 0} 291 expectRangeValues := []int64{0, 1} 292 runFetchRowsTest( 293 t, 294 types.New(types.T_int64, 0, 0), 295 values, 296 lock.Granularity_Row, 297 values, 298 expectRangeValues, 299 []int64{math.MinInt64, math.MaxInt64}, 300 func(packer *types.Packer, v int64) { 301 packer.EncodeInt64(v) 302 }, 303 nil, 304 nil, 305 false, 306 ) 307 } 308 309 func TestFetchInt64RowsWithFilter(t *testing.T) { 310 values := []int64{1, 0, 2} 311 expectRangeValues := []int64{0, 1} 312 runFetchRowsTest( 313 t, 314 types.New(types.T_int64, 0, 0), 315 values, 316 lock.Granularity_Row, 317 values[:2], 318 expectRangeValues, 319 []int64{math.MinInt64, math.MaxInt64}, 320 func(packer *types.Packer, v int64) { 321 packer.EncodeInt64(v) 322 }, 323 getRowsFilter(1, []uint64{1, 2}), 324 []int32{0, 0, 1}, 325 false, 326 ) 327 } 328 329 func TestFetchInt64RowsWithFilterAll(t *testing.T) { 330 values := []int64{1, 0, 2} 331 expectRangeValues := []int64{0, 1} 332 runFetchRowsTest( 333 t, 334 types.New(types.T_int64, 0, 0), 335 values, 336 lock.Granularity_Row, 337 values[:2], 338 expectRangeValues, 339 []int64{math.MinInt64, math.MaxInt64}, 340 func(packer *types.Packer, v int64) { 341 packer.EncodeInt64(v) 342 }, 343 getRowsFilter(1, []uint64{1, 2}), 344 []int32{1, 1, 1}, 345 true, 346 ) 347 } 348 349 func TestFetchUint8Rows(t *testing.T) { 350 values := []uint8{1, 0} 351 expectRangeValues := []uint8{0, 1} 352 runFetchRowsTest( 353 t, 354 types.New(types.T_uint8, 0, 0), 355 values, 356 lock.Granularity_Row, 357 values, 358 expectRangeValues, 359 []uint8{0, math.MaxUint8}, 360 func(packer *types.Packer, v uint8) { 361 packer.EncodeUint8(v) 362 }, 363 nil, 364 nil, 365 false, 366 ) 367 } 368 369 func TestFetchUint8RowsWithFilter(t *testing.T) { 370 values := []uint8{1, 0, 2} 371 expectRangeValues := []uint8{0, 1} 372 runFetchRowsTest( 373 t, 374 types.New(types.T_uint8, 0, 0), 375 values, 376 lock.Granularity_Row, 377 values[:2], 378 expectRangeValues, 379 []uint8{0, math.MaxUint8}, 380 func(packer *types.Packer, v uint8) { 381 packer.EncodeUint8(v) 382 }, 383 getRowsFilter(1, []uint64{1, 2}), 384 []int32{0, 0, 1}, 385 false, 386 ) 387 } 388 389 func TestFetchUint8RowsWithFilterAll(t *testing.T) { 390 values := []uint8{1, 0, 2} 391 expectRangeValues := []uint8{0, 1} 392 runFetchRowsTest( 393 t, 394 types.New(types.T_uint8, 0, 0), 395 values, 396 lock.Granularity_Row, 397 values[:2], 398 expectRangeValues, 399 []uint8{0, math.MaxUint8}, 400 func(packer *types.Packer, v uint8) { 401 packer.EncodeUint8(v) 402 }, 403 getRowsFilter(1, []uint64{1, 2}), 404 []int32{1, 1, 1}, 405 true, 406 ) 407 } 408 409 func TestFetchUint16Rows(t *testing.T) { 410 values := []uint16{1, 0} 411 expectRangeValues := []uint16{0, 1} 412 runFetchRowsTest( 413 t, 414 types.New(types.T_uint16, 0, 0), 415 values, 416 lock.Granularity_Row, 417 values, 418 expectRangeValues, 419 []uint16{0, math.MaxUint16}, 420 func(packer *types.Packer, v uint16) { 421 packer.EncodeUint16(v) 422 }, 423 nil, 424 nil, 425 false, 426 ) 427 } 428 429 func TestFetchUint16RowsWithFilter(t *testing.T) { 430 values := []uint16{1, 0, 2} 431 expectRangeValues := []uint16{0, 1} 432 runFetchRowsTest( 433 t, 434 types.New(types.T_uint16, 0, 0), 435 values, 436 lock.Granularity_Row, 437 values[:2], 438 expectRangeValues, 439 []uint16{0, math.MaxUint16}, 440 func(packer *types.Packer, v uint16) { 441 packer.EncodeUint16(v) 442 }, 443 getRowsFilter(1, []uint64{1, 2}), 444 []int32{0, 0, 1}, 445 false, 446 ) 447 } 448 449 func TestFetchUint16RowsWithFilterAll(t *testing.T) { 450 values := []uint16{1, 0, 2} 451 expectRangeValues := []uint16{0, 1} 452 runFetchRowsTest( 453 t, 454 types.New(types.T_uint16, 0, 0), 455 values, 456 lock.Granularity_Row, 457 values[:2], 458 expectRangeValues, 459 []uint16{0, math.MaxUint16}, 460 func(packer *types.Packer, v uint16) { 461 packer.EncodeUint16(v) 462 }, 463 getRowsFilter(1, []uint64{1, 2}), 464 []int32{1, 1, 1}, 465 true, 466 ) 467 } 468 469 func TestFetchUint32Rows(t *testing.T) { 470 values := []uint32{1, 0} 471 expectRangeValues := []uint32{0, 1} 472 runFetchRowsTest( 473 t, 474 types.New(types.T_uint32, 0, 0), 475 values, 476 lock.Granularity_Row, 477 values, 478 expectRangeValues, 479 []uint32{0, math.MaxUint32}, 480 func(packer *types.Packer, v uint32) { 481 packer.EncodeUint32(v) 482 }, 483 nil, 484 nil, 485 false, 486 ) 487 } 488 489 func TestFetchUint32RowsWithFilter(t *testing.T) { 490 values := []uint32{1, 0, 2} 491 expectRangeValues := []uint32{0, 1} 492 runFetchRowsTest( 493 t, 494 types.New(types.T_uint32, 0, 0), 495 values, 496 lock.Granularity_Row, 497 values[:2], 498 expectRangeValues, 499 []uint32{0, math.MaxUint32}, 500 func(packer *types.Packer, v uint32) { 501 packer.EncodeUint32(v) 502 }, 503 getRowsFilter(1, []uint64{1, 2}), 504 []int32{0, 0, 1}, 505 false, 506 ) 507 } 508 509 func TestFetchUint32RowsWithFilterAll(t *testing.T) { 510 values := []uint32{1, 0, 2} 511 expectRangeValues := []uint32{0, 1} 512 runFetchRowsTest( 513 t, 514 types.New(types.T_uint32, 0, 0), 515 values, 516 lock.Granularity_Row, 517 values[:2], 518 expectRangeValues, 519 []uint32{0, math.MaxUint32}, 520 func(packer *types.Packer, v uint32) { 521 packer.EncodeUint32(v) 522 }, 523 getRowsFilter(1, []uint64{1, 2}), 524 []int32{1, 1, 1}, 525 true, 526 ) 527 } 528 529 func TestFetchUint64Rows(t *testing.T) { 530 values := []uint64{1, 0} 531 expectRangeValues := []uint64{0, 1} 532 runFetchRowsTest( 533 t, 534 types.New(types.T_uint64, 0, 0), 535 values, 536 lock.Granularity_Row, 537 values, 538 expectRangeValues, 539 []uint64{0, math.MaxUint64}, 540 func(packer *types.Packer, v uint64) { 541 packer.EncodeUint64(v) 542 }, 543 nil, 544 nil, 545 false, 546 ) 547 } 548 549 func TestFetchUint64RowsWithFilter(t *testing.T) { 550 values := []uint64{1, 0, 2} 551 expectRangeValues := []uint64{0, 1} 552 runFetchRowsTest( 553 t, 554 types.New(types.T_uint64, 0, 0), 555 values, 556 lock.Granularity_Row, 557 values[:2], 558 expectRangeValues, 559 []uint64{0, math.MaxUint64}, 560 func(packer *types.Packer, v uint64) { 561 packer.EncodeUint64(v) 562 }, 563 getRowsFilter(1, []uint64{1, 2}), 564 []int32{0, 0, 1}, 565 false, 566 ) 567 } 568 569 func TestFetchUint64RowsWithFilterAll(t *testing.T) { 570 values := []uint64{1, 0, 2} 571 expectRangeValues := []uint64{0, 1} 572 runFetchRowsTest( 573 t, 574 types.New(types.T_uint64, 0, 0), 575 values, 576 lock.Granularity_Row, 577 values[:2], 578 expectRangeValues, 579 []uint64{0, math.MaxUint64}, 580 func(packer *types.Packer, v uint64) { 581 packer.EncodeUint64(v) 582 }, 583 getRowsFilter(1, []uint64{1, 2}), 584 []int32{1, 1, 1}, 585 true, 586 ) 587 } 588 589 func TestFetchFloat32Rows(t *testing.T) { 590 values := []float32{0.1, 0.0} 591 expectRangeValues := []float32{0.0, 0.1} 592 runFetchRowsTest( 593 t, 594 types.New(types.T_float32, 0, 0), 595 values, 596 lock.Granularity_Row, 597 values, 598 expectRangeValues, 599 []float32{math.SmallestNonzeroFloat32, math.MaxFloat32}, 600 func(packer *types.Packer, v float32) { 601 packer.EncodeFloat32(v) 602 }, 603 nil, 604 nil, 605 false, 606 ) 607 } 608 609 func TestFetchFloat32RowsWithFilter(t *testing.T) { 610 values := []float32{0.1, 0.0, 0.2} 611 expectRangeValues := []float32{0.0, 0.1} 612 runFetchRowsTest( 613 t, 614 types.New(types.T_float32, 0, 0), 615 values, 616 lock.Granularity_Row, 617 values[:2], 618 expectRangeValues, 619 []float32{math.SmallestNonzeroFloat32, math.MaxFloat32}, 620 func(packer *types.Packer, v float32) { 621 packer.EncodeFloat32(v) 622 }, 623 getRowsFilter(1, []uint64{1, 2}), 624 []int32{0, 0, 1}, 625 false, 626 ) 627 } 628 629 func TestFetchFloat32RowsWithFilterAll(t *testing.T) { 630 values := []float32{0.1, 0.0, 0.2} 631 expectRangeValues := []float32{0.0, 0.1} 632 runFetchRowsTest( 633 t, 634 types.New(types.T_float32, 0, 0), 635 values, 636 lock.Granularity_Row, 637 values[:2], 638 expectRangeValues, 639 []float32{math.SmallestNonzeroFloat32, math.MaxFloat32}, 640 func(packer *types.Packer, v float32) { 641 packer.EncodeFloat32(v) 642 }, 643 getRowsFilter(1, []uint64{1, 2}), 644 []int32{1, 1, 1}, 645 true, 646 ) 647 } 648 649 func TestFetchFloat64Rows(t *testing.T) { 650 values := []float64{0.1, 0.0} 651 expectRangeValues := []float64{0.0, 0.1} 652 runFetchRowsTest( 653 t, 654 types.New(types.T_float64, 0, 0), 655 values, 656 lock.Granularity_Row, 657 values, 658 expectRangeValues, 659 []float64{math.SmallestNonzeroFloat64, math.MaxFloat64}, 660 func(packer *types.Packer, v float64) { 661 packer.EncodeFloat64(v) 662 }, 663 nil, 664 nil, 665 false, 666 ) 667 } 668 669 func TestFetchFloat64RowsWithFilter(t *testing.T) { 670 values := []float64{0.1, 0.0, 0.2} 671 expectRangeValues := []float64{0.0, 0.1} 672 runFetchRowsTest( 673 t, 674 types.New(types.T_float64, 0, 0), 675 values, 676 lock.Granularity_Row, 677 values[:2], 678 expectRangeValues, 679 []float64{math.SmallestNonzeroFloat64, math.MaxFloat64}, 680 func(packer *types.Packer, v float64) { 681 packer.EncodeFloat64(v) 682 }, 683 getRowsFilter(1, []uint64{1, 2}), 684 []int32{0, 0, 1}, 685 false, 686 ) 687 } 688 689 func TestFetchFloat64RowsWithFilterAll(t *testing.T) { 690 values := []float64{0.1, 0.0, 0.2} 691 expectRangeValues := []float64{0.0, 0.1} 692 runFetchRowsTest( 693 t, 694 types.New(types.T_float64, 0, 0), 695 values, 696 lock.Granularity_Row, 697 values[:2], 698 expectRangeValues, 699 []float64{math.SmallestNonzeroFloat64, math.MaxFloat64}, 700 func(packer *types.Packer, v float64) { 701 packer.EncodeFloat64(v) 702 }, 703 getRowsFilter(1, []uint64{1, 2}), 704 []int32{1, 1, 1}, 705 true, 706 ) 707 } 708 709 func TestFetchDateRows(t *testing.T) { 710 values := []types.Date{1, 0} 711 expectRangeValues := []types.Date{0, 1} 712 runFetchRowsTest( 713 t, 714 types.New(types.T_date, 0, 0), 715 values, 716 lock.Granularity_Row, 717 values, 718 expectRangeValues, 719 []types.Date{math.MinInt32, math.MaxInt32}, 720 func(packer *types.Packer, v types.Date) { 721 packer.EncodeDate(v) 722 }, 723 nil, 724 nil, 725 false, 726 ) 727 } 728 729 func TestFetchDateRowsWithFilter(t *testing.T) { 730 values := []types.Date{1, 0, 2} 731 expectRangeValues := []types.Date{0, 1} 732 runFetchRowsTest( 733 t, 734 types.New(types.T_date, 0, 0), 735 values, 736 lock.Granularity_Row, 737 values[:2], 738 expectRangeValues, 739 []types.Date{math.MinInt32, math.MaxInt32}, 740 func(packer *types.Packer, v types.Date) { 741 packer.EncodeDate(v) 742 }, 743 getRowsFilter(1, []uint64{1, 2}), 744 []int32{0, 0, 1}, 745 false, 746 ) 747 } 748 749 func TestFetchDateRowsWithFilterAll(t *testing.T) { 750 values := []types.Date{1, 0, 2} 751 expectRangeValues := []types.Date{0, 1} 752 runFetchRowsTest( 753 t, 754 types.New(types.T_date, 0, 0), 755 values, 756 lock.Granularity_Row, 757 values[:2], 758 expectRangeValues, 759 []types.Date{math.MinInt32, math.MaxInt32}, 760 func(packer *types.Packer, v types.Date) { 761 packer.EncodeDate(v) 762 }, 763 getRowsFilter(1, []uint64{1, 2}), 764 []int32{1, 1, 1}, 765 true, 766 ) 767 } 768 769 func TestFetchTimeRows(t *testing.T) { 770 values := []types.Time{1, 0} 771 expectRangeValues := []types.Time{0, 1} 772 runFetchRowsTest( 773 t, 774 types.New(types.T_time, 0, 0), 775 values, 776 lock.Granularity_Row, 777 values, 778 expectRangeValues, 779 []types.Time{math.MinInt64, math.MaxInt64}, 780 func(packer *types.Packer, v types.Time) { 781 packer.EncodeTime(v) 782 }, 783 nil, 784 nil, 785 false, 786 ) 787 } 788 789 func TestFetchTimeRowsWithFilter(t *testing.T) { 790 values := []types.Time{1, 0, 2} 791 expectRangeValues := []types.Time{0, 1} 792 runFetchRowsTest( 793 t, 794 types.New(types.T_time, 0, 0), 795 values, 796 lock.Granularity_Row, 797 values[:2], 798 expectRangeValues, 799 []types.Time{math.MinInt64, math.MaxInt64}, 800 func(packer *types.Packer, v types.Time) { 801 packer.EncodeTime(v) 802 }, 803 getRowsFilter(1, []uint64{1, 2}), 804 []int32{0, 0, 1}, 805 false, 806 ) 807 } 808 809 func TestFetchTimeRowsWithFilterAll(t *testing.T) { 810 values := []types.Time{1, 0, 2} 811 expectRangeValues := []types.Time{0, 1} 812 runFetchRowsTest( 813 t, 814 types.New(types.T_time, 0, 0), 815 values, 816 lock.Granularity_Row, 817 values[:2], 818 expectRangeValues, 819 []types.Time{math.MinInt64, math.MaxInt64}, 820 func(packer *types.Packer, v types.Time) { 821 packer.EncodeTime(v) 822 }, 823 getRowsFilter(1, []uint64{1, 2}), 824 []int32{1, 1, 1}, 825 true, 826 ) 827 } 828 829 func TestFetchDateTimeRows(t *testing.T) { 830 values := []types.Datetime{1, 0} 831 expectRangeValues := []types.Datetime{0, 1} 832 runFetchRowsTest( 833 t, 834 types.New(types.T_datetime, 0, 0), 835 values, 836 lock.Granularity_Row, 837 values, 838 expectRangeValues, 839 []types.Datetime{math.MinInt64, math.MaxInt64}, 840 func(packer *types.Packer, v types.Datetime) { 841 packer.EncodeDatetime(v) 842 }, 843 nil, 844 nil, 845 false, 846 ) 847 } 848 849 func TestFetchDateTimeRowsWithFilter(t *testing.T) { 850 values := []types.Datetime{1, 0, 2} 851 expectRangeValues := []types.Datetime{0, 1} 852 runFetchRowsTest( 853 t, 854 types.New(types.T_datetime, 0, 0), 855 values, 856 lock.Granularity_Row, 857 values[:2], 858 expectRangeValues, 859 []types.Datetime{math.MinInt64, math.MaxInt64}, 860 func(packer *types.Packer, v types.Datetime) { 861 packer.EncodeDatetime(v) 862 }, 863 getRowsFilter(1, []uint64{1, 2}), 864 []int32{0, 0, 1}, 865 false, 866 ) 867 } 868 869 func TestFetchDateTimeRowsWithFilterAll(t *testing.T) { 870 values := []types.Datetime{1, 0, 2} 871 expectRangeValues := []types.Datetime{0, 1} 872 runFetchRowsTest( 873 t, 874 types.New(types.T_datetime, 0, 0), 875 values, 876 lock.Granularity_Row, 877 values[:2], 878 expectRangeValues, 879 []types.Datetime{math.MinInt64, math.MaxInt64}, 880 func(packer *types.Packer, v types.Datetime) { 881 packer.EncodeDatetime(v) 882 }, 883 getRowsFilter(1, []uint64{1, 2}), 884 []int32{1, 1, 1}, 885 true, 886 ) 887 } 888 889 func TestFetchTimestampRows(t *testing.T) { 890 values := []types.Timestamp{1, 0} 891 expectRangeValues := []types.Timestamp{0, 1} 892 runFetchRowsTest( 893 t, 894 types.New(types.T_timestamp, 0, 0), 895 values, 896 lock.Granularity_Row, 897 values, 898 expectRangeValues, 899 []types.Timestamp{math.MinInt64, math.MaxInt64}, 900 func(packer *types.Packer, v types.Timestamp) { 901 packer.EncodeTimestamp(v) 902 }, 903 nil, 904 nil, 905 false, 906 ) 907 } 908 909 func TestFetchTimestampRowsWithFilter(t *testing.T) { 910 values := []types.Timestamp{1, 0, 2} 911 expectRangeValues := []types.Timestamp{0, 1} 912 runFetchRowsTest( 913 t, 914 types.New(types.T_timestamp, 0, 0), 915 values, 916 lock.Granularity_Row, 917 values[:2], 918 expectRangeValues, 919 []types.Timestamp{math.MinInt64, math.MaxInt64}, 920 func(packer *types.Packer, v types.Timestamp) { 921 packer.EncodeTimestamp(v) 922 }, 923 getRowsFilter(1, []uint64{1, 2}), 924 []int32{0, 0, 1}, 925 false, 926 ) 927 } 928 929 func TestFetchTimestampRowsWithFilterAll(t *testing.T) { 930 values := []types.Timestamp{1, 0, 2} 931 expectRangeValues := []types.Timestamp{0, 1} 932 runFetchRowsTest( 933 t, 934 types.New(types.T_timestamp, 0, 0), 935 values, 936 lock.Granularity_Row, 937 values[:2], 938 expectRangeValues, 939 []types.Timestamp{math.MinInt64, math.MaxInt64}, 940 func(packer *types.Packer, v types.Timestamp) { 941 packer.EncodeTimestamp(v) 942 }, 943 getRowsFilter(1, []uint64{1, 2}), 944 []int32{1, 1, 1}, 945 true, 946 ) 947 } 948 949 func TestFetchDecimal64Rows(t *testing.T) { 950 max := types.Decimal64(999999999999999999) 951 min := max.Minus() 952 values := []types.Decimal64{1, 0} 953 expectRangeValues := []types.Decimal64{0, 1} 954 runFetchRowsTest( 955 t, 956 types.New(types.T_decimal64, 0, 0), 957 values, 958 lock.Granularity_Row, 959 values, 960 expectRangeValues, 961 []types.Decimal64{min, max}, 962 func(packer *types.Packer, v types.Decimal64) { 963 packer.EncodeDecimal64(v) 964 }, 965 nil, 966 nil, 967 false, 968 ) 969 } 970 971 func TestFetchDecimal64RowsWithFilter(t *testing.T) { 972 max := types.Decimal64(999999999999999999) 973 min := max.Minus() 974 values := []types.Decimal64{1, 0, 2} 975 expectRangeValues := []types.Decimal64{0, 1} 976 runFetchRowsTest( 977 t, 978 types.New(types.T_decimal64, 0, 0), 979 values, 980 lock.Granularity_Row, 981 values[:2], 982 expectRangeValues, 983 []types.Decimal64{min, max}, 984 func(packer *types.Packer, v types.Decimal64) { 985 packer.EncodeDecimal64(v) 986 }, 987 getRowsFilter(1, []uint64{1, 2}), 988 []int32{0, 0, 1}, 989 false, 990 ) 991 } 992 993 func TestFetchDecimal64RowsWithFilterAll(t *testing.T) { 994 max := types.Decimal64(999999999999999999) 995 min := max.Minus() 996 values := []types.Decimal64{1, 0, 2} 997 expectRangeValues := []types.Decimal64{0, 1} 998 runFetchRowsTest( 999 t, 1000 types.New(types.T_decimal64, 0, 0), 1001 values, 1002 lock.Granularity_Row, 1003 values[:2], 1004 expectRangeValues, 1005 []types.Decimal64{min, max}, 1006 func(packer *types.Packer, v types.Decimal64) { 1007 packer.EncodeDecimal64(v) 1008 }, 1009 getRowsFilter(1, []uint64{1, 2}), 1010 []int32{1, 1, 1}, 1011 true, 1012 ) 1013 } 1014 1015 func TestFetchDecimal128Rows(t *testing.T) { 1016 max, _, _ := types.Parse128("99999999999999999999999999999999999999") 1017 min := max.Minus() 1018 values := []types.Decimal128{{B0_63: 1, B64_127: 1}, {B0_63: 0, B64_127: 0}} 1019 expectRangeValues := []types.Decimal128{{B0_63: 0, B64_127: 0}, {B0_63: 1, B64_127: 1}} 1020 runFetchRowsTest( 1021 t, 1022 types.New(types.T_decimal128, 0, 0), 1023 values, 1024 lock.Granularity_Row, 1025 values, 1026 expectRangeValues, 1027 []types.Decimal128{min, max}, 1028 func(packer *types.Packer, v types.Decimal128) { 1029 packer.EncodeDecimal128(v) 1030 }, 1031 nil, 1032 nil, 1033 false, 1034 ) 1035 } 1036 1037 func TestFetchDecimal128RowsWithFilter(t *testing.T) { 1038 max, _, _ := types.Parse128("99999999999999999999999999999999999999") 1039 min := max.Minus() 1040 values := []types.Decimal128{{B0_63: 1, B64_127: 1}, {B0_63: 0, B64_127: 0}, {B0_63: 2, B64_127: 2}} 1041 expectRangeValues := []types.Decimal128{{B0_63: 0, B64_127: 0}, {B0_63: 1, B64_127: 1}} 1042 runFetchRowsTest( 1043 t, 1044 types.New(types.T_decimal128, 0, 0), 1045 values, 1046 lock.Granularity_Row, 1047 values[:2], 1048 expectRangeValues, 1049 []types.Decimal128{min, max}, 1050 func(packer *types.Packer, v types.Decimal128) { 1051 packer.EncodeDecimal128(v) 1052 }, 1053 getRowsFilter(1, []uint64{1, 2}), 1054 []int32{0, 0, 1}, 1055 false, 1056 ) 1057 } 1058 1059 func TestFetchDecimal128RowsWithFilterAll(t *testing.T) { 1060 max, _, _ := types.Parse128("99999999999999999999999999999999999999") 1061 min := max.Minus() 1062 values := []types.Decimal128{{B0_63: 1, B64_127: 1}, {B0_63: 0, B64_127: 0}, {B0_63: 2, B64_127: 2}} 1063 expectRangeValues := []types.Decimal128{{B0_63: 0, B64_127: 0}, {B0_63: 1, B64_127: 1}} 1064 runFetchRowsTest( 1065 t, 1066 types.New(types.T_decimal128, 0, 0), 1067 values, 1068 lock.Granularity_Row, 1069 values[:2], 1070 expectRangeValues, 1071 []types.Decimal128{min, max}, 1072 func(packer *types.Packer, v types.Decimal128) { 1073 packer.EncodeDecimal128(v) 1074 }, 1075 getRowsFilter(1, []uint64{1, 2}), 1076 []int32{1, 1, 1}, 1077 true, 1078 ) 1079 } 1080 1081 func TestFetchUUIDRows(t *testing.T) { 1082 values := []types.Uuid{[16]byte{1}, [16]byte{}} 1083 expectRangeValues := []types.Uuid{[16]byte{}, [16]byte{1}} 1084 runFetchRowsTest( 1085 t, 1086 types.New(types.T_uuid, 0, 0), 1087 values, 1088 lock.Granularity_Row, 1089 values, 1090 expectRangeValues, 1091 []types.Uuid{minUUID, maxUUID}, 1092 func(packer *types.Packer, v types.Uuid) { 1093 packer.EncodeStringType(v[:]) 1094 }, 1095 nil, 1096 nil, 1097 false, 1098 ) 1099 } 1100 1101 func TestFetchUUIDRowsWithFilter(t *testing.T) { 1102 values := []types.Uuid{[16]byte{1}, [16]byte{}, [16]byte{2}} 1103 expectRangeValues := []types.Uuid{[16]byte{}, [16]byte{1}} 1104 runFetchRowsTest( 1105 t, 1106 types.New(types.T_uuid, 0, 0), 1107 values, 1108 lock.Granularity_Row, 1109 values[:2], 1110 expectRangeValues, 1111 []types.Uuid{minUUID, maxUUID}, 1112 func(packer *types.Packer, v types.Uuid) { 1113 packer.EncodeStringType(v[:]) 1114 }, 1115 getRowsFilter(1, []uint64{1, 2}), 1116 []int32{0, 0, 1}, 1117 false, 1118 ) 1119 } 1120 1121 func TestFetchUUIDRowsWithFilterAll(t *testing.T) { 1122 values := []types.Uuid{[16]byte{1}, [16]byte{}, [16]byte{2}} 1123 expectRangeValues := []types.Uuid{[16]byte{}, [16]byte{1}} 1124 runFetchRowsTest( 1125 t, 1126 types.New(types.T_uuid, 0, 0), 1127 values, 1128 lock.Granularity_Row, 1129 values[:2], 1130 expectRangeValues, 1131 []types.Uuid{minUUID, maxUUID}, 1132 func(packer *types.Packer, v types.Uuid) { 1133 packer.EncodeStringType(v[:]) 1134 }, 1135 getRowsFilter(1, []uint64{1, 2}), 1136 []int32{1, 1, 1}, 1137 true, 1138 ) 1139 } 1140 1141 func TestFetchCharRows(t *testing.T) { 1142 values := [][]byte{{1}, {0}} 1143 expectRangeValues := [][]byte{{0}, {1}} 1144 runFetchBytesRowsTest( 1145 t, 1146 types.New(types.T_char, 2, 0), 1147 values, 1148 lock.Granularity_Row, 1149 values, 1150 expectRangeValues, 1151 [][]byte{{0}, {math.MaxUint8, math.MaxUint8}}, 1152 func(packer *types.Packer, v []byte) { 1153 packer.EncodeStringType(v) 1154 }, 1155 nil, 1156 nil, 1157 ) 1158 } 1159 1160 func TestFetchCharRowsWithFilter(t *testing.T) { 1161 values := [][]byte{{1}, {0}, {2}} 1162 expectRangeValues := [][]byte{{0}, {1}} 1163 runFetchBytesRowsTest( 1164 t, 1165 types.New(types.T_char, 2, 0), 1166 values, 1167 lock.Granularity_Row, 1168 values[:2], 1169 expectRangeValues, 1170 [][]byte{{0}, {math.MaxUint8, math.MaxUint8}}, 1171 func(packer *types.Packer, v []byte) { 1172 packer.EncodeStringType(v) 1173 }, 1174 getRowsFilter(1, []uint64{1, 2}), 1175 []int32{0, 0, 1}, 1176 ) 1177 } 1178 1179 func TestFetchVarcharRows(t *testing.T) { 1180 values := [][]byte{{1}, {0}} 1181 expectRangeValues := [][]byte{{0}, {1}} 1182 runFetchBytesRowsTest( 1183 t, 1184 types.New(types.T_varchar, 2, 0), 1185 values, 1186 lock.Granularity_Row, 1187 values, 1188 expectRangeValues, 1189 [][]byte{{0}, {math.MaxUint8, math.MaxUint8}}, 1190 func(packer *types.Packer, v []byte) { 1191 packer.EncodeStringType(v) 1192 }, 1193 nil, 1194 nil, 1195 ) 1196 } 1197 1198 func TestFetchVarcharRowsWithFilter(t *testing.T) { 1199 values := [][]byte{{1}, {0}, {2}} 1200 expectRangeValues := [][]byte{{0}, {1}} 1201 runFetchBytesRowsTest( 1202 t, 1203 types.New(types.T_varchar, 2, 0), 1204 values, 1205 lock.Granularity_Row, 1206 values[:2], 1207 expectRangeValues, 1208 [][]byte{{0}, {math.MaxUint8, math.MaxUint8}}, 1209 func(packer *types.Packer, v []byte) { 1210 packer.EncodeStringType(v) 1211 }, 1212 getRowsFilter(1, []uint64{1, 2}), 1213 []int32{0, 0, 1}, 1214 ) 1215 } 1216 1217 func TestFetchRangeWithSameMinAndMax(t *testing.T) { 1218 values := []int16{1, 1} 1219 runFetchRowsTest( 1220 t, 1221 types.New(types.T_int16, 0, 0), 1222 values, 1223 lock.Granularity_Row, 1224 values, 1225 values[:1], 1226 []int16{math.MinInt16, math.MaxInt16}, 1227 func(packer *types.Packer, v int16) { 1228 packer.EncodeInt16(v) 1229 }, 1230 nil, 1231 nil, 1232 false, 1233 ) 1234 } 1235 1236 func runFetchRowsTest[T any]( 1237 t *testing.T, 1238 tp types.Type, 1239 values []T, 1240 expectG lock.Granularity, 1241 expectValues []T, 1242 expectRangeValues []T, 1243 expectLockTableValues []T, 1244 fn func(*types.Packer, T), 1245 filter RowsFilter, 1246 filterCols []int32, 1247 filterAll bool) { 1248 runFetchRowsTestWithAppendFunc( 1249 t, 1250 tp, 1251 values, 1252 expectG, 1253 expectValues, 1254 expectRangeValues, 1255 expectLockTableValues, 1256 fn, 1257 func(vec *vector.Vector, mp *mpool.MPool) { 1258 vector.AppendFixedList(vec, values, nil, mp) 1259 }, 1260 filter, 1261 filterCols, 1262 filterAll, 1263 ) 1264 } 1265 1266 func runFetchBytesRowsTest( 1267 t *testing.T, 1268 tp types.Type, 1269 values [][]byte, 1270 expectG lock.Granularity, 1271 expectValues [][]byte, 1272 expectRangeValues [][]byte, 1273 expectLockTableValues [][]byte, 1274 fn func(*types.Packer, []byte), 1275 filter RowsFilter, 1276 filterCols []int32) { 1277 runFetchRowsTestWithAppendFunc( 1278 t, 1279 tp, 1280 values, 1281 expectG, 1282 expectValues, 1283 expectRangeValues, 1284 expectLockTableValues, 1285 fn, 1286 func(vec *vector.Vector, mp *mpool.MPool) { 1287 for _, v := range values { 1288 vector.AppendBytes(vec, v[:], false, mp) 1289 } 1290 }, 1291 filter, 1292 filterCols, 1293 false, 1294 ) 1295 } 1296 1297 func runFetchRowsTestWithAppendFunc[T any]( 1298 t *testing.T, 1299 tp types.Type, 1300 values []T, 1301 expectG lock.Granularity, 1302 expectValues []T, 1303 expectRangeValues []T, 1304 expectLockTableValues []T, 1305 fn func(*types.Packer, T), 1306 appendFunc func(vec *vector.Vector, mp *mpool.MPool), 1307 filter RowsFilter, 1308 filterCols []int32, 1309 filterAll bool) { 1310 mp := mpool.MustNew("test") 1311 vec := vector.NewVec(tp) 1312 appendFunc(vec, mp) 1313 1314 packer := types.NewPacker(mpool.MustNew("test")) 1315 fetcher := GetFetchRowsFunc(tp) 1316 assertFN := func(values []T, rows [][]byte) { 1317 for idx, v := range values { 1318 packer.Reset() 1319 fn(packer, v) 1320 assert.Equal(t, packer.Bytes(), rows[idx]) 1321 } 1322 } 1323 1324 // many rows 1325 ok, rows, g := fetcher(vec, packer, tp, len(values), false, filter, filterCols) 1326 if !ok { 1327 require.Equal(t, filterAll, !ok) 1328 return 1329 } else { 1330 assert.Equal(t, expectG, g) 1331 assert.Equal(t, len(expectValues), len(rows)) 1332 assertFN(expectValues, rows) 1333 } 1334 1335 // many rows => range row 1336 ok, rows, g = fetcher(vec, packer, tp, len(values)-1, false, filter, filterCols) 1337 if !ok { 1338 require.Equal(t, filterAll, !ok) 1339 return 1340 } else { 1341 if len(expectRangeValues) > 1 { 1342 assert.Equal(t, lock.Granularity_Range, g) 1343 assert.Equal(t, 2, len(rows)) 1344 assertFN(expectRangeValues, rows) 1345 } else { 1346 assert.Equal(t, lock.Granularity_Row, g) 1347 assert.Equal(t, 1, len(rows)) 1348 assertFN(expectRangeValues, rows) 1349 } 1350 } 1351 1352 // lock table 1353 ok, rows, g = fetcher(vec, packer, tp, len(values), true, filter, filterCols) 1354 require.True(t, ok) 1355 assert.Equal(t, lock.Granularity_Range, g) 1356 assert.Equal(t, 2, len(rows)) 1357 assertFN(expectLockTableValues, rows) 1358 } 1359 1360 func TestDecimal128(t *testing.T) { 1361 packer := types.NewPacker(mpool.MustNew("test")) 1362 decimal128Fn := func(v types.Decimal128) []byte { 1363 packer.Reset() 1364 packer.EncodeDecimal128(v) 1365 return packer.Bytes() 1366 } 1367 max128, _, _ := types.Parse128("99999999999999999999999999999999999999") 1368 minDecimal128 := decimal128Fn(max128.Minus()) 1369 maxDecimal128 := decimal128Fn(max128) 1370 assert.True(t, bytes.Compare(minDecimal128, maxDecimal128) < 0) 1371 }