LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float8.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 81.7 % 93 76
Test Date: 2026-02-17 17:20:33 Functions: 92.9 % 28 26
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*
       2              :  * contrib/btree_gist/btree_float8.c
       3              :  */
       4              : #include "postgres.h"
       5              : 
       6              : #include "btree_gist.h"
       7              : #include "btree_utils_num.h"
       8              : #include "utils/float.h"
       9              : #include "utils/rel.h"
      10              : #include "utils/sortsupport.h"
      11              : 
      12              : typedef struct float8key
      13              : {
      14              :     float8      lower;
      15              :     float8      upper;
      16              : } float8KEY;
      17              : 
      18              : /* GiST support functions */
      19            4 : PG_FUNCTION_INFO_V1(gbt_float8_compress);
      20            4 : PG_FUNCTION_INFO_V1(gbt_float8_fetch);
      21            4 : PG_FUNCTION_INFO_V1(gbt_float8_union);
      22            4 : PG_FUNCTION_INFO_V1(gbt_float8_picksplit);
      23            4 : PG_FUNCTION_INFO_V1(gbt_float8_consistent);
      24            4 : PG_FUNCTION_INFO_V1(gbt_float8_distance);
      25            4 : PG_FUNCTION_INFO_V1(gbt_float8_penalty);
      26            4 : PG_FUNCTION_INFO_V1(gbt_float8_same);
      27            4 : PG_FUNCTION_INFO_V1(gbt_float8_sortsupport);
      28              : 
      29              : 
      30              : static bool
      31         1357 : gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
      32              : {
      33         1357 :     return (*((const float8 *) a) > *((const float8 *) b));
      34              : }
      35              : static bool
      36          514 : gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
      37              : {
      38          514 :     return (*((const float8 *) a) >= *((const float8 *) b));
      39              : }
      40              : static bool
      41          272 : gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
      42              : {
      43          272 :     return (*((const float8 *) a) == *((const float8 *) b));
      44              : }
      45              : static bool
      46          825 : gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
      47              : {
      48          825 :     return (*((const float8 *) a) <= *((const float8 *) b));
      49              : }
      50              : static bool
      51         1629 : gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
      52              : {
      53         1629 :     return (*((const float8 *) a) < *((const float8 *) b));
      54              : }
      55              : 
      56              : static int
      57          543 : gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      58              : {
      59          543 :     float8KEY  *ia = (float8KEY *) (((const Nsrt *) a)->t);
      60          543 :     float8KEY  *ib = (float8KEY *) (((const Nsrt *) b)->t);
      61              : 
      62          543 :     if (ia->lower == ib->lower)
      63              :     {
      64            0 :         if (ia->upper == ib->upper)
      65            0 :             return 0;
      66              : 
      67            0 :         return (ia->upper > ib->upper) ? 1 : -1;
      68              :     }
      69              : 
      70          543 :     return (ia->lower > ib->lower) ? 1 : -1;
      71              : }
      72              : 
      73              : static float8
      74          273 : gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
      75              : {
      76          273 :     float8      arg1 = *(const float8 *) a;
      77          273 :     float8      arg2 = *(const float8 *) b;
      78              :     float8      r;
      79              : 
      80          273 :     r = arg1 - arg2;
      81          273 :     if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
      82            0 :         float_overflow_error();
      83          273 :     return fabs(r);
      84              : }
      85              : 
      86              : 
      87              : static const gbtree_ninfo tinfo =
      88              : {
      89              :     gbt_t_float8,
      90              :     sizeof(float8),
      91              :     16,                         /* sizeof(gbtreekey16) */
      92              :     gbt_float8gt,
      93              :     gbt_float8ge,
      94              :     gbt_float8eq,
      95              :     gbt_float8le,
      96              :     gbt_float8lt,
      97              :     gbt_float8key_cmp,
      98              :     gbt_float8_dist
      99              : };
     100              : 
     101              : 
     102            4 : PG_FUNCTION_INFO_V1(float8_dist);
     103              : Datum
     104          547 : float8_dist(PG_FUNCTION_ARGS)
     105              : {
     106          547 :     float8      a = PG_GETARG_FLOAT8(0);
     107          547 :     float8      b = PG_GETARG_FLOAT8(1);
     108              :     float8      r;
     109              : 
     110          547 :     r = a - b;
     111          547 :     if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
     112            0 :         float_overflow_error();
     113              : 
     114          547 :     PG_RETURN_FLOAT8(fabs(r));
     115              : }
     116              : 
     117              : 
     118              : /**************************************************
     119              :  * GiST support functions
     120              :  **************************************************/
     121              : 
     122              : Datum
     123          546 : gbt_float8_compress(PG_FUNCTION_ARGS)
     124              : {
     125          546 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     126              : 
     127          546 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     128              : }
     129              : 
     130              : Datum
     131          272 : gbt_float8_fetch(PG_FUNCTION_ARGS)
     132              : {
     133          272 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     134              : 
     135          272 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     136              : }
     137              : 
     138              : Datum
     139         1914 : gbt_float8_consistent(PG_FUNCTION_ARGS)
     140              : {
     141         1914 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     142         1914 :     float8      query = PG_GETARG_FLOAT8(1);
     143         1914 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     144              : #ifdef NOT_USED
     145              :     Oid         subtype = PG_GETARG_OID(3);
     146              : #endif
     147         1914 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     148         1914 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     149              :     GBT_NUMKEY_R key;
     150              : 
     151              :     /* All cases served by this function are exact */
     152         1914 :     *recheck = false;
     153              : 
     154         1914 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     155         1914 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     156              : 
     157         1914 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     158              :                                       GIST_LEAF(entry), &tinfo,
     159              :                                       fcinfo->flinfo));
     160              : }
     161              : 
     162              : Datum
     163          274 : gbt_float8_distance(PG_FUNCTION_ARGS)
     164              : {
     165          274 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     166          274 :     float8      query = PG_GETARG_FLOAT8(1);
     167              : #ifdef NOT_USED
     168              :     Oid         subtype = PG_GETARG_OID(3);
     169              : #endif
     170          274 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     171              :     GBT_NUMKEY_R key;
     172              : 
     173          274 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     174          274 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     175              : 
     176          274 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     177              :                                       &tinfo, fcinfo->flinfo));
     178              : }
     179              : 
     180              : Datum
     181            1 : gbt_float8_union(PG_FUNCTION_ARGS)
     182              : {
     183            1 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     184            1 :     void       *out = palloc(sizeof(float8KEY));
     185              : 
     186            1 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
     187            1 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     188              : }
     189              : 
     190              : Datum
     191            0 : gbt_float8_penalty(PG_FUNCTION_ARGS)
     192              : {
     193            0 :     float8KEY  *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     194            0 :     float8KEY  *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     195            0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     196              : 
     197            0 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     198              : 
     199            0 :     PG_RETURN_POINTER(result);
     200              : }
     201              : 
     202              : Datum
     203            1 : gbt_float8_picksplit(PG_FUNCTION_ARGS)
     204              : {
     205            1 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     206              :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     207              :                                         &tinfo, fcinfo->flinfo));
     208              : }
     209              : 
     210              : Datum
     211            0 : gbt_float8_same(PG_FUNCTION_ARGS)
     212              : {
     213            0 :     float8KEY  *b1 = (float8KEY *) PG_GETARG_POINTER(0);
     214            0 :     float8KEY  *b2 = (float8KEY *) PG_GETARG_POINTER(1);
     215            0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     216              : 
     217            0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     218            0 :     PG_RETURN_POINTER(result);
     219              : }
     220              : 
     221              : static int
     222         5277 : gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     223              : {
     224         5277 :     float8KEY  *arg1 = (float8KEY *) DatumGetPointer(x);
     225         5277 :     float8KEY  *arg2 = (float8KEY *) DatumGetPointer(y);
     226              : 
     227              :     /* for leaf items we expect lower == upper, so only compare lower */
     228         5277 :     return float8_cmp_internal(arg1->lower, arg2->lower);
     229              : }
     230              : 
     231              : Datum
     232            1 : gbt_float8_sortsupport(PG_FUNCTION_ARGS)
     233              : {
     234            1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     235              : 
     236            1 :     ssup->comparator = gbt_float8_ssup_cmp;
     237            1 :     ssup->ssup_extra = NULL;
     238              : 
     239            1 :     PG_RETURN_VOID();
     240              : }
        

Generated by: LCOV version 2.0-1