@@ -30,10 +30,10 @@ PG_FUNCTION_INFO_V1(rum_tsquery_timestamp_consistent);
3030PG_FUNCTION_INFO_V1 (rum_tsquery_distance );
3131PG_FUNCTION_INFO_V1 (rum_ts_distance );
3232
33- static float calc_rank_pos_and (float * w ,Datum * addInfo , bool * addInfoIsNull ,
34- int size );
35- static float calc_rank_pos_or (float * w ,Datum * addInfo , bool * addInfoIsNull ,
36- int size );
33+ static float calc_rank_pos_and (float * w ,bool * check ,
34+ Datum * addInfo , bool * addInfoIsNull , int size );
35+ static float calc_rank_pos_or (float * w ,bool * check ,
36+ Datum * addInfo , bool * addInfoIsNull , int size );
3737
3838static int count_pos (char * ptr ,int len );
3939static char * decompress_pos (char * ptr ,uint16 * pos );
@@ -330,7 +330,7 @@ count_pos(char *ptr, int len)
330330}
331331
332332static float
333- calc_rank_pos_and (float * w ,Datum * addInfo ,bool * addInfoIsNull ,int size )
333+ calc_rank_pos_and (float * w ,bool * check , Datum * addInfo ,bool * addInfoIsNull ,int size )
334334{
335335int i ,
336336k ,
@@ -345,13 +345,13 @@ calc_rank_pos_and(float *w, Datum *addInfo, bool *addInfoIsNull, int size)
345345char * ptrt ,* ptrc ;
346346
347347if (size < 2 )
348- {
349- return calc_rank_pos_or (w ,addInfo ,addInfoIsNull ,size );
350- }
351- WEP_SETPOS (POSNULL .pos [0 ],MAXENTRYPOS - 1 );
348+ return calc_rank_pos_or (w ,check ,addInfo ,addInfoIsNull ,size );
352349
353350for (i = 0 ;i < size ;i ++ )
354351{
352+ if (!check [i ])
353+ continue ;
354+
355355if (!addInfoIsNull [i ])
356356{
357357dimt = count_pos (VARDATA_ANY (addInfo [i ]),VARSIZE_ANY_EXHDR (addInfo [i ]));
@@ -371,15 +371,21 @@ calc_rank_pos_and(float *w, Datum *addInfo, bool *addInfoIsNull, int size)
371371post = 0 ;
372372for (l = 0 ;l < dimt ;l ++ )
373373{
374- ptrt = decompress_pos (ptrt ,& post );
374+ if (ptrt == (char * )POSNULL .pos )
375+ post = POSNULL .pos [0 ];
376+ else
377+ ptrt = decompress_pos (ptrt ,& post );
375378ct = 0 ;
376379if (!addInfoIsNull [k ])
377380ptrc = (char * )VARDATA_ANY (addInfo [k ]);
378381else
379382ptrc = (char * )POSNULL .pos ;
380383for (p = 0 ;p < lenct ;p ++ )
381384{
382- ptrc = decompress_pos (ptrc ,& ct );
385+ if (ptrc == (char * )POSNULL .pos )
386+ ct = POSNULL .pos [0 ];
387+ else
388+ ptrc = decompress_pos (ptrc ,& ct );
383389dist = Abs ((int )WEP_GETPOS (post )- (int )WEP_GETPOS (ct ));
384390if (dist || (dist == 0 && (ptrt == (char * )POSNULL .pos || ptrc == (char * )POSNULL .pos )))
385391{
@@ -399,7 +405,7 @@ calc_rank_pos_and(float *w, Datum *addInfo, bool *addInfoIsNull, int size)
399405}
400406
401407static float
402- calc_rank_pos_or (float * w ,Datum * addInfo ,bool * addInfoIsNull ,int size )
408+ calc_rank_pos_or (float * w ,bool * check , Datum * addInfo ,bool * addInfoIsNull ,int size )
403409{
404410WordEntryPos post ;
405411int32 dimt ,
@@ -414,6 +420,9 @@ calc_rank_pos_or(float *w, Datum *addInfo, bool *addInfoIsNull, int size)
414420wjm ;
415421int32 jm ;
416422
423+ if (!check [i ])
424+ continue ;
425+
417426if (!addInfoIsNull [i ])
418427{
419428dimt = count_pos (VARDATA_ANY (addInfo [i ]),VARSIZE_ANY_EXHDR (addInfo [i ]));
@@ -431,7 +440,10 @@ calc_rank_pos_or(float *w, Datum *addInfo, bool *addInfoIsNull, int size)
431440post = 0 ;
432441for (j = 0 ;j < dimt ;j ++ )
433442{
434- ptrt = decompress_pos (ptrt ,& post );
443+ if (ptrt == (char * )POSNULL .pos )
444+ post = POSNULL .pos [0 ];
445+ else
446+ ptrt = decompress_pos (ptrt ,& post );
435447resj = resj + wpos (post ) / ((j + 1 )* (j + 1 ));
436448if (wpos (post )> wjm )
437449{
@@ -455,19 +467,21 @@ calc_rank_pos_or(float *w, Datum *addInfo, bool *addInfoIsNull, int size)
455467}
456468
457469static float
458- calc_rank_pos (float * w ,TSQuery q ,Datum * addInfo ,bool * addInfoIsNull ,int size )
470+ calc_rank_pos (float * w ,bool * check , TSQuery q ,Datum * addInfo ,bool * addInfoIsNull ,int size )
459471{
460472QueryItem * item = GETQUERY (q );
461473float res = 0.0 ;
462474
463475if (!size || !q -> size )
464476return 0.0 ;
465477
478+ WEP_SETPOS (POSNULL .pos [0 ],MAXENTRYPOS - 1 );
479+
466480/* XXX: What about NOT? */
467481res = (item -> type == QI_OPR && (item -> qoperator .oper == OP_AND ||
468482item -> qoperator .oper == OP_PHRASE )) ?
469- calc_rank_pos_and (w ,addInfo ,addInfoIsNull ,size ) :
470- calc_rank_pos_or (w ,addInfo ,addInfoIsNull ,size );
483+ calc_rank_pos_and (w ,check , addInfo ,addInfoIsNull ,size ) :
484+ calc_rank_pos_or (w ,check , addInfo ,addInfoIsNull ,size );
471485
472486if (res < 0 )
473487res = 1e-20f ;
@@ -691,18 +705,16 @@ rum_extract_tsquery(PG_FUNCTION_ARGS)
691705Datum
692706rum_tsquery_distance (PG_FUNCTION_ARGS )
693707{
694- /* bool *check = (bool *) PG_GETARG_POINTER(0); */
695-
708+ bool * check = (bool * )PG_GETARG_POINTER (0 );
696709/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
697710TSQuery query = PG_GETARG_TSQUERY (2 );
698-
699711int32 nkeys = PG_GETARG_INT32 (3 );
700- /* Pointer*extra_data = (Pointer *) PG_GETARG_POINTER(4); */
712+ Pointer * extra_data = (Pointer * )PG_GETARG_POINTER (4 );
701713Datum * addInfo = (Datum * )PG_GETARG_POINTER (8 );
702714bool * addInfoIsNull = (bool * )PG_GETARG_POINTER (9 );
703715float8 res ;
704716
705- res = 1.0 / (float8 )calc_rank_pos (weights ,query ,
717+ res = 1.0 / (float8 )calc_rank_pos (weights ,check , query ,
706718addInfo ,addInfoIsNull ,nkeys );
707719
708720PG_RETURN_FLOAT8 (res );
@@ -711,10 +723,15 @@ rum_tsquery_distance(PG_FUNCTION_ARGS)
711723Datum
712724rum_ts_distance (PG_FUNCTION_ARGS )
713725{
714- return DirectFunctionCall2Coll (ts_rank_tt ,
715- PG_GET_COLLATION (),
716- PG_GETARG_DATUM (0 ),
717- PG_GETARG_DATUM (1 ));
726+ float4 r = DatumGetFloat4 (DirectFunctionCall2Coll (ts_rank_tt ,
727+ PG_GET_COLLATION (),
728+ PG_GETARG_DATUM (0 ),
729+ PG_GETARG_DATUM (1 )));
730+
731+ if (r == 0 )
732+ PG_RETURN_FLOAT4 (get_float4_infinity ());
733+ else
734+ PG_RETURN_FLOAT4 (1.0 /r );
718735}
719736
720737Datum