@@ -36,6 +36,7 @@ import (
3636 "github.com/matrixorigin/matrixone/pkg/sql/plan/function"
3737 "github.com/matrixorigin/matrixone/pkg/testutil"
3838 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
39+ "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
3940 "github.com/stretchr/testify/require"
4041)
4142
@@ -1249,8 +1250,9 @@ func TestConstructBlockPKFilter(t *testing.T) {
12491250 basePKFilter .String (),
12501251 common .MoVectorToString (inputVec , 100 ))
12511252
1252- sel1 := blkPKFilter .SortedSearchFunc (inputVec )
1253- sel2 := blkPKFilter .UnSortedSearchFunc (inputVec )
1253+ cacheVectors := containers.Vectors {* inputVec }
1254+ sel1 := blkPKFilter .SortedSearchFunc (cacheVectors )
1255+ sel2 := blkPKFilter .UnSortedSearchFunc (cacheVectors )
12541256
12551257 require .Equal (t , sel1 , sel2 , msg )
12561258 }
@@ -1657,7 +1659,9 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
16571659 require .NotNil (t , readFilter .UnSortedSearchFunc )
16581660
16591661 // Test the search function
1660- result := readFilter .UnSortedSearchFunc (inputVec )
1662+ // For non-composite PK without optimization, use single vector
1663+ cacheVectors := containers.Vectors {* inputVec }
1664+ result := readFilter .UnSortedSearchFunc (cacheVectors )
16611665 // Should return indices of values that exist in BF: [10, 20, 30] -> indices [1, 3, 5]
16621666 require .Equal (t , []int64 {1 , 3 , 5 }, result )
16631667 })
@@ -1695,8 +1699,16 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
16951699 require .True (t , readFilter .Valid )
16961700 require .NotNil (t , readFilter .UnSortedSearchFunc )
16971701
1698- // Test the search function
1699- result := readFilter .UnSortedSearchFunc (compositePKVec )
1702+ // Test the search function with optimization (len(cacheVectors) >= 2)
1703+ // Create last column vector for optimization
1704+ lastColVec := vector .NewVec (types .T_int64 .ToType ())
1705+ for _ , tuple := range tuples {
1706+ lastVal := tuple [len (tuple )- 1 ].(int64 )
1707+ vector .AppendFixed (lastColVec , lastVal , false , mp )
1708+ }
1709+ defer lastColVec .Free (mp )
1710+ cacheVectors := containers.Vectors {* compositePKVec , * lastColVec }
1711+ result := readFilter .UnSortedSearchFunc (cacheVectors )
17001712 // Should return all indices [0, 1, 2] since all last columns match
17011713 require .Equal (t , []int64 {0 , 1 , 2 }, result )
17021714 })
@@ -1731,7 +1743,15 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
17311743 )
17321744 require .NoError (t , err )
17331745
1734- result := readFilter .UnSortedSearchFunc (compositePKVec )
1746+ // Create last column vector for optimization
1747+ lastColVec := vector .NewVec (types .T_int64 .ToType ())
1748+ for _ , tuple := range tuples {
1749+ lastVal := tuple [len (tuple )- 1 ].(int64 )
1750+ vector .AppendFixed (lastColVec , lastVal , false , mp )
1751+ }
1752+ defer lastColVec .Free (mp )
1753+ cacheVectors := containers.Vectors {* compositePKVec , * lastColVec }
1754+ result := readFilter .UnSortedSearchFunc (cacheVectors )
17351755 // Should return indices containing [0, 2] for values 100 and 300
17361756 // Note: BloomFilter may have false positives, so we check that it contains the expected indices
17371757 require .Contains (t , result , int64 (0 ), "Should contain index 0 (value 100)" )
@@ -1766,7 +1786,15 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
17661786 )
17671787 require .NoError (t , err )
17681788
1769- result := readFilter .UnSortedSearchFunc (compositePKVec )
1789+ // Create last column vector for optimization
1790+ lastColVec := vector .NewVec (types .T_int64 .ToType ())
1791+ for _ , tuple := range tuples {
1792+ lastVal := tuple [len (tuple )- 1 ].(int64 )
1793+ vector .AppendFixed (lastColVec , lastVal , false , mp )
1794+ }
1795+ defer lastColVec .Free (mp )
1796+ cacheVectors := containers.Vectors {* compositePKVec , * lastColVec }
1797+ result := readFilter .UnSortedSearchFunc (cacheVectors )
17701798 // Should return empty slice since no matches (not nil, but empty slice)
17711799 require .Empty (t , result )
17721800 })
@@ -1812,7 +1840,15 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
18121840 require .True (t , readFilter .Valid )
18131841
18141842 // With only BloomFilter, should return indices 0, 1, 2 (values 100, 200, 300)
1815- result := readFilter .UnSortedSearchFunc (compositePKVec )
1843+ // Create last column vector for optimization
1844+ lastColVec := vector .NewVec (types .T_int64 .ToType ())
1845+ for _ , tuple := range tuples {
1846+ lastVal := tuple [len (tuple )- 1 ].(int64 )
1847+ vector .AppendFixed (lastColVec , lastVal , false , mp )
1848+ }
1849+ defer lastColVec .Free (mp )
1850+ cacheVectors := containers.Vectors {* compositePKVec , * lastColVec }
1851+ result := readFilter .UnSortedSearchFunc (cacheVectors )
18161852 require .NotNil (t , result )
18171853 require .Contains (t , result , int64 (0 ), "Should contain index 0 (value 100)" )
18181854 require .Contains (t , result , int64 (1 ), "Should contain index 1 (value 200)" )
@@ -1847,7 +1883,15 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
18471883 )
18481884 require .NoError (t , err )
18491885
1850- result := readFilter .UnSortedSearchFunc (compositePKVec )
1886+ // Create last column vector for optimization
1887+ lastColVec := vector .NewVec (types .T_int32 .ToType ())
1888+ for _ , tuple := range tuples {
1889+ lastVal := tuple [len (tuple )- 1 ].(int32 )
1890+ vector .AppendFixed (lastColVec , lastVal , false , mp )
1891+ }
1892+ defer lastColVec .Free (mp )
1893+ cacheVectors := containers.Vectors {* compositePKVec , * lastColVec }
1894+ result := readFilter .UnSortedSearchFunc (cacheVectors )
18511895 require .Equal (t , []int64 {0 , 1 }, result )
18521896 })
18531897
@@ -1872,7 +1916,11 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
18721916 )
18731917 require .NoError (t , err )
18741918
1875- result := readFilter .UnSortedSearchFunc (compositePKVec )
1919+ // For empty vector, optimization path should return nil
1920+ emptyLastColVec := vector .NewVec (types .T_int64 .ToType ())
1921+ defer emptyLastColVec .Free (mp )
1922+ cacheVectors := containers.Vectors {* compositePKVec , * emptyLastColVec }
1923+ result := readFilter .UnSortedSearchFunc (cacheVectors )
18761924 require .Nil (t , result )
18771925 })
18781926
@@ -1901,7 +1949,11 @@ func TestConstructBlockPKFilterWithBloomFilter(t *testing.T) {
19011949 require .NoError (t , err )
19021950
19031951 // Should handle invalid data gracefully
1904- result := readFilter .UnSortedSearchFunc (vec )
1952+ // For invalid data, optimization path should return nil
1953+ invalidLastColVec := vector .NewVec (types .T_int64 .ToType ())
1954+ defer invalidLastColVec .Free (mp )
1955+ cacheVectors := containers.Vectors {* vec , * invalidLastColVec }
1956+ result := readFilter .UnSortedSearchFunc (cacheVectors )
19051957 // Should return nil or empty since extraction fails
19061958 require .Nil (t , result )
19071959 })
0 commit comments