@@ -66,79 +66,115 @@ public async Task Main()
6666
6767 var embedding = new Vector ( new float [ ] { 1 , 1 , 1 } ) ;
6868 var items = await ctx . Items . FromSql ( $ "SELECT * FROM efcore_items ORDER BY embedding <-> { embedding } LIMIT 5") . ToListAsync ( ) ;
69- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
70- Assert . Equal ( new float [ ] { 1 , 1 , 1 } , items [ 0 ] . Embedding ! . ToArray ( ) ) ;
71- Assert . Equal ( new Half [ ] { ( Half ) 1 , ( Half ) 1 , ( Half ) 1 } , items [ 0 ] . HalfEmbedding ! . ToArray ( ) ) ;
69+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
70+ Assert . Equal ( [ 1 , 1 , 1 ] , items [ 0 ] . Embedding ! . ToArray ( ) ) ;
71+ Assert . Equal ( [ ( Half ) 1 , ( Half ) 1 , ( Half ) 1 ] , items [ 0 ] . HalfEmbedding ! . ToArray ( ) ) ;
7272 Assert . Equal ( new BitArray ( new bool [ ] { false , false , false } ) , items [ 0 ] . BinaryEmbedding ! ) ;
73- Assert . Equal ( new float [ ] { 1 , 1 , 1 } , items [ 0 ] . SparseEmbedding ! . ToArray ( ) ) ;
73+ Assert . Equal ( [ 1 , 1 , 1 ] , items [ 0 ] . SparseEmbedding ! . ToArray ( ) ) ;
7474
7575 // vector distance functions
7676
7777 items = await ctx . Items . OrderBy ( x => x . Embedding ! . L2Distance ( embedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
78- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
79- Assert . Equal ( new float [ ] { 1 , 1 , 1 } , items [ 0 ] . Embedding ! . ToArray ( ) ) ;
78+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
79+ Assert . Equal ( [ 1 , 1 , 1 ] , items [ 0 ] . Embedding ! . ToArray ( ) ) ;
8080
8181 items = await ctx . Items . OrderBy ( x => x . Embedding ! . MaxInnerProduct ( embedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
82- Assert . Equal ( new int [ ] { 2 , 3 , 1 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
82+ Assert . Equal ( [ 2 , 3 , 1 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
8383
8484 items = await ctx . Items . OrderBy ( x => x . Embedding ! . CosineDistance ( embedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
8585 Assert . Equal ( 3 , items [ 2 ] . Id ) ;
8686
8787 items = await ctx . Items . OrderBy ( x => x . Embedding ! . L1Distance ( embedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
88- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
88+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
8989
9090 // halfvec distance functions
9191
9292 var halfEmbedding = new HalfVector ( new Half [ ] { ( Half ) 1 , ( Half ) 1 , ( Half ) 1 } ) ;
9393 items = await ctx . Items . OrderBy ( x => x . HalfEmbedding ! . L2Distance ( halfEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
94- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
94+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
9595
9696 items = await ctx . Items . OrderBy ( x => x . HalfEmbedding ! . MaxInnerProduct ( halfEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
97- Assert . Equal ( new int [ ] { 2 , 3 , 1 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
97+ Assert . Equal ( [ 2 , 3 , 1 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
9898
9999 items = await ctx . Items . OrderBy ( x => x . HalfEmbedding ! . CosineDistance ( halfEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
100100 Assert . Equal ( 3 , items [ 2 ] . Id ) ;
101101
102102 items = await ctx . Items . OrderBy ( x => x . HalfEmbedding ! . L1Distance ( halfEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
103- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
103+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
104104
105105 // sparsevec distance functions
106106
107107 var sparseEmbedding = new SparseVector ( new float [ ] { 1 , 1 , 1 } ) ;
108108 items = await ctx . Items . OrderBy ( x => x . SparseEmbedding ! . L2Distance ( sparseEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
109- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
109+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
110110
111111 items = await ctx . Items . OrderBy ( x => x . SparseEmbedding ! . MaxInnerProduct ( sparseEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
112- Assert . Equal ( new int [ ] { 2 , 3 , 1 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
112+ Assert . Equal ( [ 2 , 3 , 1 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
113113
114114 items = await ctx . Items . OrderBy ( x => x . SparseEmbedding ! . CosineDistance ( sparseEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
115115 Assert . Equal ( 3 , items [ 2 ] . Id ) ;
116116
117117 items = await ctx . Items . OrderBy ( x => x . SparseEmbedding ! . L1Distance ( sparseEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
118- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
118+ Assert . Equal ( [ 1 , 3 , 2 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
119119
120120 // bit distance functions
121121
122122 var binaryEmbedding = new BitArray ( new bool [ ] { true , false , true } ) ;
123123 items = await ctx . Items . OrderBy ( x => x . BinaryEmbedding ! . HammingDistance ( binaryEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
124- Assert . Equal ( new int [ ] { 2 , 3 , 1 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
124+ Assert . Equal ( [ 2 , 3 , 1 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
125125
126126 items = await ctx . Items . OrderBy ( x => x . BinaryEmbedding ! . JaccardDistance ( binaryEmbedding ) ) . Take ( 5 ) . ToListAsync ( ) ;
127- Assert . Equal ( new int [ ] { 2 , 3 , 1 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
127+ Assert . Equal ( [ 2 , 3 , 1 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
128128
129129 // additional
130130
131131 items = await ctx . Items
132132 . OrderBy ( x => x . Id )
133133 . Where ( x => x . Embedding ! . L2Distance ( embedding ) < 1.5 )
134134 . ToListAsync ( ) ;
135- Assert . Equal ( new int [ ] { 1 , 3 } , items . Select ( v => v . Id ) . ToArray ( ) ) ;
135+ Assert . Equal ( [ 1 , 3 ] , items . Select ( v => v . Id ) . ToArray ( ) ) ;
136136
137137 var neighbors = await ctx . Items
138138 . OrderBy ( x => x . Embedding ! . L2Distance ( embedding ) )
139139 . Select ( x => new { Entity = x , Distance = x . Embedding ! . L2Distance ( embedding ) } )
140140 . ToListAsync ( ) ;
141- Assert . Equal ( new int [ ] { 1 , 3 , 2 } , neighbors . Select ( v => v . Entity . Id ) . ToArray ( ) ) ;
142- Assert . Equal ( new double [ ] { 0 , 1 , Math . Sqrt ( 3 ) } , neighbors . Select ( v => v . Distance ) . ToArray ( ) ) ;
141+ Assert . Equal ( [ 1 , 3 , 2 ] , neighbors . Select ( v => v . Entity . Id ) . ToArray ( ) ) ;
142+ Assert . Equal ( [ 0 , 1 , Math . Sqrt ( 3 ) ] , neighbors . Select ( v => v . Distance ) . ToArray ( ) ) ;
143+ }
144+
145+ [ Theory ]
146+ [ InlineData ( typeof ( Vector ) , null , "vector" ) ]
147+ [ InlineData ( typeof ( Vector ) , 3 , "vector(3)" ) ]
148+ [ InlineData ( typeof ( HalfVector ) , null , "halfvec" ) ]
149+ [ InlineData ( typeof ( HalfVector ) , 3 , "halfvec(3)" ) ]
150+ [ InlineData ( typeof ( SparseVector ) , null , "sparsevec" ) ]
151+ [ InlineData ( typeof ( SparseVector ) , 3 , "sparsevec(3)" ) ]
152+ public void By_StoreType ( Type type , int ? size , string expectedStoreType )
153+ {
154+ using var ctx = new ItemContext ( ) ;
155+ var typeMappingSource = ctx . GetService < IRelationalTypeMappingSource > ( ) ;
156+
157+ var typeMapping = typeMappingSource . FindMapping ( type , storeTypeName : null , size : size ) ! ;
158+ Assert . Equal ( expectedStoreType , typeMapping . StoreType ) ;
159+ Assert . Same ( type , typeMapping . ClrType ) ;
160+ Assert . Equal ( size , typeMapping . Size ) ;
161+ }
162+
163+ [ Theory ]
164+ [ InlineData ( "vector" , typeof ( Vector ) , null ) ]
165+ [ InlineData ( "vector(3)" , typeof ( Vector ) , 3 ) ]
166+ [ InlineData ( "halfvec" , typeof ( HalfVector ) , null ) ]
167+ [ InlineData ( "halfvec(3)" , typeof ( HalfVector ) , 3 ) ]
168+ [ InlineData ( "sparsevec" , typeof ( SparseVector ) , null ) ]
169+ [ InlineData ( "sparsevec(3)" , typeof ( SparseVector ) , 3 ) ]
170+ public void By_ClrType ( string storeType , Type expectedType , int ? expectedSize )
171+ {
172+ using var ctx = new ItemContext ( ) ;
173+ var typeMappingSource = ctx . GetService < IRelationalTypeMappingSource > ( ) ;
174+
175+ var typeMapping = typeMappingSource . FindMapping ( storeType ) ! ;
176+ Assert . Equal ( storeType , typeMapping . StoreType ) ;
177+ Assert . Same ( expectedType , typeMapping . ClrType ) ;
178+ Assert . Equal ( expectedSize , typeMapping . Size ) ;
143179 }
144180}
0 commit comments