4141#define RUM_SIMILAR_STRATEGY 5
4242
4343
44- #define LINEAR_LIMIT 5
4544#define NDIM 1
4645
47-
4846#define ARR_NELEMS (x ) ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
49- #define ARR_ISVOID (x ) ((x) == NULL || ARR_NELEMS(x) == 0)
47+ #define ARR_ISVOID (x ) ( (x) == NULL || ARR_NELEMS(x) == 0 )
5048
5149#define CHECKARRVALID (x ) \
5250 do { \
7169 (s)->info = NULL; \
7270 } while (0)
7371
72+ #define DIST_FROM_SML (sml ) \
73+ ( (sml == 0.0) ? get_float8_infinity() : ((float8) 1) / ((float8) (sml)) )
74+
7475
7576typedef struct AnyArrayTypeInfo
7677{
@@ -128,7 +129,6 @@ static void freeAnyArrayTypeInfo(AnyArrayTypeInfo *info);
128129static void cmpFuncInit (AnyArrayTypeInfo * info );
129130
130131static SimpleArray * Array2SimpleArray (AnyArrayTypeInfo * info , ArrayType * a );
131- static ArrayType * SimpleArray2Array (SimpleArray * s );
132132static void freeSimpleArray (SimpleArray * s );
133133static int cmpAscArrayElem (const void * a , const void * b , void * arg );
134134static int cmpDescArrayElem (const void * a , const void * b , void * arg );
@@ -373,15 +373,19 @@ rum_anyarray_consistent(PG_FUNCTION_ARGS)
373373 if (check [i ])
374374 intersection ++ ;
375375
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 )
376+ if (intersection > 0 )
384377 {
378+ /* extract array's length from addInfo */
379+ for (i = 0 ; i < nkeys ; i ++ )
380+ if (!addInfoIsNull [i ])
381+ {
382+ nentries = DatumGetInt32 (addInfo [i ]);
383+ break ;
384+ }
385+
386+ /* there must be addInfo */
387+ Assert (nentries >= 0 );
388+
385389 InitDummySimpleArray (& sa , nentries );
386390 InitDummySimpleArray (& sb , nkeys );
387391 res = getSimilarity (& sa , & sb , intersection ) >= SmlLimit ;
@@ -412,8 +416,7 @@ rum_anyarray_ordering(PG_FUNCTION_ARGS)
412416 Datum * addInfo = (Datum * ) PG_GETARG_POINTER (8 );
413417 bool * addInfoIsNull = (bool * ) PG_GETARG_POINTER (9 );
414418
415- float8 dist ,
416- sml ;
419+ float8 sml ;
417420 int32 intersection = 0 ,
418421 nentries = -1 ;
419422 int i ;
@@ -424,26 +427,27 @@ rum_anyarray_ordering(PG_FUNCTION_ARGS)
424427 if (check [i ])
425428 intersection ++ ;
426429
427- if (intersection == 0 )
428- PG_RETURN_FLOAT8 (get_float8_infinity ());
430+ if (intersection > 0 )
431+ {
432+ /* extract array's length from addInfo */
433+ for (i = 0 ; i < nkeys ; i ++ )
434+ if (!addInfoIsNull [i ])
435+ {
436+ nentries = DatumGetInt32 (addInfo [i ]);
437+ break ;
438+ }
429439
430- for (i = 0 ; i < nkeys ; i ++ )
431- if (!addInfoIsNull [0 ])
432- {
433- nentries = DatumGetInt32 (addInfo [i ]);
434- break ;
435- }
440+ /* there must be addInfo */
441+ Assert (nentries >= 0 );
436442
437- InitDummySimpleArray (& sa , nentries );
438- InitDummySimpleArray (& sb , nkeys );
439- sml = getSimilarity (& sa , & sb , intersection );
443+ InitDummySimpleArray (& sa , nentries );
444+ InitDummySimpleArray (& sb , nkeys );
445+ sml = getSimilarity (& sa , & sb , intersection );
440446
441- if (sml == 0.0 )
442- dist = get_float8_infinity ();
443- else
444- dist = 1.0 / sml ;
447+ PG_RETURN_FLOAT8 (DIST_FROM_SML (sml ));
448+ }
445449
446- PG_RETURN_FLOAT8 (dist );
450+ PG_RETURN_FLOAT8 (DIST_FROM_SML ( 0.0 ) );
447451}
448452
449453Datum
@@ -494,7 +498,7 @@ rum_anyarray_distance(PG_FUNCTION_ARGS)
494498 AnyArrayTypeInfo * info ;
495499 SimpleArray * sa ,
496500 * sb ;
497- float8 result = 0.0 ;
501+ float8 sml = 0.0 ;
498502
499503 CHECKARRVALID (a );
500504 CHECKARRVALID (b );
@@ -515,19 +519,15 @@ rum_anyarray_distance(PG_FUNCTION_ARGS)
515519 sa = Array2SimpleArray (info , a );
516520 sb = Array2SimpleArray (info , b );
517521
518- result = getSimilarity (sa , sb , getNumOfIntersect (sa , sb ));
519- if (result == 0.0 )
520- result = get_float8_infinity ();
521- else
522- result = 1.0 / result ;
522+ sml = getSimilarity (sa , sb , getNumOfIntersect (sa , sb ));
523523
524524 freeSimpleArray (sb );
525525 freeSimpleArray (sa );
526526
527527 PG_FREE_IF_COPY (b , 1 );
528528 PG_FREE_IF_COPY (a , 0 );
529529
530- PG_RETURN_FLOAT8 (result );
530+ PG_RETURN_FLOAT8 (DIST_FROM_SML ( sml ) );
531531}
532532
533533
@@ -702,16 +702,6 @@ Array2SimpleArray(AnyArrayTypeInfo *info, ArrayType *a)
702702 return s ;
703703}
704704
705- static ArrayType *
706- SimpleArray2Array (SimpleArray * s )
707- {
708- return construct_array (s -> elems , s -> nelems ,
709- s -> info -> typid ,
710- s -> info -> typlen ,
711- s -> info -> typbyval ,
712- s -> info -> typalign );
713- }
714-
715705static void
716706freeSimpleArray (SimpleArray * s )
717707{
0 commit comments