@@ -136,7 +136,7 @@ static void sortSimpleArray(SimpleArray *s, int32 direction);
136
136
static void uniqSimpleArray (SimpleArray * s , bool onlyDuplicate );
137
137
138
138
static int32 getNumOfIntersect (SimpleArray * sa , SimpleArray * sb );
139
- static float8 getSimilarity (SimpleArray * sa , SimpleArray * sb , int32 intersect );
139
+ static float8 getSimilarity (SimpleArray * sa , SimpleArray * sb , int32 intersection );
140
140
141
141
142
142
@@ -164,30 +164,26 @@ Datum
164
164
rum_extract_anyarray (PG_FUNCTION_ARGS )
165
165
{
166
166
/* Make copy of array input to ensure it doesn't disappear while in use */
167
- ArrayType * array = PG_GETARG_ARRAYTYPE_P_COPY (0 );
167
+ ArrayType * array = PG_GETARG_ARRAYTYPE_P_COPY (0 );
168
+ SimpleArray * sa ;
169
+ AnyArrayTypeInfo * info ;
168
170
169
- Datum * entries ;
170
- int32 * nentries = (int32 * ) PG_GETARG_POINTER (1 );
171
- bool * * entries_isnull = (bool * * ) PG_GETARG_POINTER (2 );
171
+ int32 * nentries = (int32 * ) PG_GETARG_POINTER (1 );
172
172
173
- Datum * * addInfo = (Datum * * ) PG_GETARG_POINTER (3 );
174
- bool * * addInfoIsNull = (bool * * ) PG_GETARG_POINTER (4 );
173
+ Datum * * addInfo = (Datum * * ) PG_GETARG_POINTER (3 );
174
+ bool * * addInfoIsNull = (bool * * ) PG_GETARG_POINTER (4 );
175
175
176
- int16 elmlen ;
177
- bool elmbyval ;
178
- char elmalign ;
179
- int i ;
176
+ int i ;
180
177
181
178
CHECKARRVALID (array );
182
179
183
- get_typlenbyvalalign (ARR_ELEMTYPE (array ),
184
- & elmlen , & elmbyval , & elmalign );
180
+ info = getAnyArrayTypeInfoCached (fcinfo , ARR_ELEMTYPE (array ));
185
181
186
- deconstruct_array (array ,
187
- ARR_ELEMTYPE (array ),
188
- elmlen , elmbyval , elmalign ,
189
- & entries , entries_isnull , nentries );
182
+ sa = Array2SimpleArray (info , array );
183
+ sortSimpleArray (sa , 1 );
184
+ uniqSimpleArray (sa , false);
190
185
186
+ * nentries = sa -> nelems ;
191
187
* addInfo = (Datum * ) palloc (* nentries * sizeof (Datum ));
192
188
* addInfoIsNull = (bool * ) palloc (* nentries * sizeof (bool ));
193
189
@@ -199,40 +195,32 @@ rum_extract_anyarray(PG_FUNCTION_ARGS)
199
195
}
200
196
201
197
/* we should not free array, entries[i] points into it */
202
- PG_RETURN_POINTER (entries );
198
+ PG_RETURN_POINTER (sa -> elems );
203
199
}
204
200
205
201
/* Enhanced version of ginqueryarrayextract() */
206
202
Datum
207
203
rum_extract_anyarray_query (PG_FUNCTION_ARGS )
208
204
{
209
205
/* Make copy of array input to ensure it doesn't disappear while in use */
210
- ArrayType * array = PG_GETARG_ARRAYTYPE_P_COPY (0 );
211
-
212
- Datum * entries ;
213
- int32 * nentries = (int32 * ) PG_GETARG_POINTER (1 );
214
- bool * * entries_isnull = (bool * * ) PG_GETARG_POINTER (5 );
215
-
216
- StrategyNumber strategy = PG_GETARG_UINT16 (2 );
217
-
218
- /* bool **pmatch = (bool **) PG_GETARG_POINTER(3); */
219
- /* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
206
+ ArrayType * array = PG_GETARG_ARRAYTYPE_P_COPY (0 );
207
+ SimpleArray * sa ;
208
+ AnyArrayTypeInfo * info ;
220
209
221
- int32 * searchMode = (int32 * ) PG_GETARG_POINTER (6 );
210
+ int32 * nentries = (int32 * ) PG_GETARG_POINTER (1 );
222
211
223
- int16 elmlen ;
224
- bool elmbyval ;
225
- char elmalign ;
212
+ StrategyNumber strategy = PG_GETARG_UINT16 (2 );
213
+ int32 * searchMode = (int32 * ) PG_GETARG_POINTER (6 );
226
214
227
215
CHECKARRVALID (array );
228
216
229
- get_typlenbyvalalign (ARR_ELEMTYPE (array ),
230
- & elmlen , & elmbyval , & elmalign );
217
+ info = getAnyArrayTypeInfoCached (fcinfo , ARR_ELEMTYPE (array ));
218
+
219
+ sa = Array2SimpleArray (info , array );
220
+ sortSimpleArray (sa , 1 );
221
+ uniqSimpleArray (sa , false);
231
222
232
- deconstruct_array (array ,
233
- ARR_ELEMTYPE (array ),
234
- elmlen , elmbyval , elmalign ,
235
- & entries , entries_isnull , nentries );
223
+ * nentries = sa -> nelems ;
236
224
237
225
switch (strategy )
238
226
{
@@ -268,7 +256,7 @@ rum_extract_anyarray_query(PG_FUNCTION_ARGS)
268
256
}
269
257
270
258
/* we should not free array, elems[i] points into it */
271
- PG_RETURN_POINTER (entries );
259
+ PG_RETURN_POINTER (sa -> elems );
272
260
}
273
261
274
262
@@ -373,11 +361,34 @@ rum_anyarray_consistent(PG_FUNCTION_ARGS)
373
361
}
374
362
break ;
375
363
case RUM_SIMILAR_STRATEGY :
376
- /* we will need recheck */
377
- * recheck = true ;
364
+ /* we won't need recheck */
365
+ * recheck = false ;
378
366
379
- /* can't do anything else useful here */
380
- res = true;
367
+ {
368
+ int32 intersection = 0 ,
369
+ nentries = -1 ;
370
+ SimpleArray sa , sb ;
371
+
372
+ for (i = 0 ; i < nkeys ; i ++ )
373
+ if (check [i ])
374
+ intersection ++ ;
375
+
376
+ for (i = 0 ; i < nkeys ; i ++ )
377
+ if (!addInfoIsNull [0 ])
378
+ {
379
+ nentries = DatumGetInt32 (addInfo [i ]);
380
+ break ;
381
+ }
382
+
383
+ if (nentries >= 0 )
384
+ {
385
+ InitDummySimpleArray (& sa , nentries );
386
+ InitDummySimpleArray (& sb , nkeys );
387
+ res = getSimilarity (& sa , & sb , intersection ) >= SmlLimit ;
388
+ }
389
+ else
390
+ res = false;
391
+ }
381
392
break ;
382
393
default :
383
394
elog (ERROR , "rum_anyarray_consistent: unknown strategy number: %d" ,
@@ -403,17 +414,17 @@ rum_anyarray_ordering(PG_FUNCTION_ARGS)
403
414
404
415
float8 dist ,
405
416
sml ;
406
- int32 intersect = 0 ,
417
+ int32 intersection = 0 ,
407
418
nentries = -1 ;
408
419
int i ;
409
420
410
421
SimpleArray sa , sb ;
411
422
412
423
for (i = 0 ; i < nkeys ; i ++ )
413
424
if (check [i ])
414
- intersect ++ ;
425
+ intersection ++ ;
415
426
416
- if (intersect == 0 )
427
+ if (intersection == 0 )
417
428
PG_RETURN_FLOAT8 (get_float8_infinity ());
418
429
419
430
for (i = 0 ; i < nkeys ; i ++ )
@@ -425,7 +436,7 @@ rum_anyarray_ordering(PG_FUNCTION_ARGS)
425
436
426
437
InitDummySimpleArray (& sa , nentries );
427
438
InitDummySimpleArray (& sb , nkeys );
428
- sml = getSimilarity (& sa , & sb , intersect );
439
+ sml = getSimilarity (& sa , & sb , intersection );
429
440
430
441
if (sml == 0.0 )
431
442
dist = get_float8_infinity ();
@@ -842,20 +853,24 @@ getNumOfIntersect(SimpleArray *sa, SimpleArray *sb)
842
853
}
843
854
844
855
static float8
845
- getSimilarity (SimpleArray * sa , SimpleArray * sb , int32 intersect )
856
+ getSimilarity (SimpleArray * sa , SimpleArray * sb , int32 intersection )
846
857
{
847
- float8 result = 0.0 ;
858
+ float8 result = 0.0 ;
848
859
849
860
switch (SmlType )
850
861
{
851
862
case AA_Cosine :
852
- result = ((float8 )intersect ) / sqrt (((float8 )sa -> nelems ) * ((float8 )sb -> nelems ));
863
+ result = ((float8 ) intersection ) /
864
+ sqrt (((float8 ) sa -> nelems ) * ((float8 ) sb -> nelems ));
853
865
break ;
854
866
case AA_Jaccard :
855
- result = ((float8 )intersect ) / (((float8 )sa -> nelems ) + ((float8 )sb -> nelems ) - ((double )intersect ));
867
+ result = ((float8 ) intersection ) /
868
+ (((float8 ) sa -> nelems ) +
869
+ ((float8 ) sb -> nelems ) -
870
+ ((float8 ) intersection ));
856
871
break ;
857
872
case AA_Overlap :
858
- result = intersect ;
873
+ result = intersection ;
859
874
break ;
860
875
default :
861
876
elog (ERROR , "unknown similarity type" );
0 commit comments