LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float8.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 90.4 % 104 94
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 28 28
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 41.2 % 34 14

             Branch data     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                 :             : /*
      31                 :             :  * Use the NaN-aware comparators from utils/float.h, so that our results
      32                 :             :  * will agree with standard btree indexes.  Note that penalty and distance
      33                 :             :  * functions below must also cope with NaNs, in particular with the policy
      34                 :             :  * that all NaNs are equal.
      35                 :             :  */
      36                 :             : static bool
      37                 :        5994 : gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
      38                 :             : {
      39                 :        5994 :     return float8_gt(*((const float8 *) a), *((const float8 *) b));
      40                 :             : }
      41                 :             : static bool
      42                 :         792 : gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
      43                 :             : {
      44                 :         792 :     return float8_ge(*((const float8 *) a), *((const float8 *) b));
      45                 :             : }
      46                 :             : static bool
      47                 :         416 : gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
      48                 :             : {
      49                 :         416 :     return float8_eq(*((const float8 *) a), *((const float8 *) b));
      50                 :             : }
      51                 :             : static bool
      52                 :         558 : gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
      53                 :             : {
      54                 :         558 :     return float8_le(*((const float8 *) a), *((const float8 *) b));
      55                 :             : }
      56                 :             : static bool
      57                 :        5720 : gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
      58                 :             : {
      59                 :        5720 :     return float8_lt(*((const float8 *) a), *((const float8 *) b));
      60                 :             : }
      61                 :             : 
      62                 :             : static int
      63                 :        1637 : gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      64                 :             : {
      65                 :        1637 :     float8KEY  *ia = (float8KEY *) (((const Nsrt *) a)->t);
      66                 :        1637 :     float8KEY  *ib = (float8KEY *) (((const Nsrt *) b)->t);
      67                 :             :     int         res;
      68                 :             : 
      69                 :        1637 :     res = float8_cmp_internal(ia->lower, ib->lower);
      70         [ +  - ]:        1637 :     if (res != 0)
      71                 :        1637 :         return res;
      72                 :           0 :     return float8_cmp_internal(ia->upper, ib->upper);
      73                 :             : }
      74                 :             : 
      75                 :             : static float8
      76                 :         274 : gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
      77                 :             : {
      78                 :         274 :     float8      arg1 = *(const float8 *) a;
      79                 :         274 :     float8      arg2 = *(const float8 *) b;
      80                 :             :     float8      r;
      81                 :             : 
      82                 :         274 :     r = arg1 - arg2;
      83   [ +  +  +  -  :         274 :     if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
                   -  + ]
      84                 :           0 :         float_overflow_error();
      85         [ -  + ]:         274 :     if (unlikely(isnan(r)))
      86                 :             :     {
      87   [ #  #  #  # ]:           0 :         if (isnan(arg1) && isnan(arg2))
      88                 :           0 :             r = 0.0;            /* treat NaNs as equal */
      89   [ #  #  #  # ]:           0 :         else if (isnan(arg1) || isnan(arg2))
      90                 :           0 :             r = get_float8_infinity();  /* max dist for NaN vs non-NaN */
      91                 :             :         else
      92                 :           0 :             r = 0.0;            /* must be Inf - Inf case */
      93                 :             :     }
      94                 :         274 :     return fabs(r);
      95                 :             : }
      96                 :             : 
      97                 :             : 
      98                 :             : static const gbtree_ninfo tinfo =
      99                 :             : {
     100                 :             :     gbt_t_float8,
     101                 :             :     sizeof(float8),
     102                 :             :     16,                         /* sizeof(gbtreekey16) */
     103                 :             :     gbt_float8gt,
     104                 :             :     gbt_float8ge,
     105                 :             :     gbt_float8eq,
     106                 :             :     gbt_float8le,
     107                 :             :     gbt_float8lt,
     108                 :             :     gbt_float8key_cmp,
     109                 :             :     gbt_float8_dist
     110                 :             : };
     111                 :             : 
     112                 :             : 
     113                 :           4 : PG_FUNCTION_INFO_V1(float8_dist);
     114                 :             : Datum
     115                 :         550 : float8_dist(PG_FUNCTION_ARGS)
     116                 :             : {
     117                 :         550 :     float8      a = PG_GETARG_FLOAT8(0);
     118                 :         550 :     float8      b = PG_GETARG_FLOAT8(1);
     119                 :             :     float8      r;
     120                 :             : 
     121                 :         550 :     r = a - b;
     122   [ +  +  -  +  :         550 :     if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
                   -  - ]
     123                 :           0 :         float_overflow_error();
     124         [ +  + ]:         550 :     if (unlikely(isnan(r)))
     125                 :             :     {
     126   [ +  -  -  + ]:           1 :         if (isnan(a) && isnan(b))
     127                 :           0 :             r = 0.0;            /* treat NaNs as equal */
     128   [ -  +  -  - ]:           1 :         else if (isnan(a) || isnan(b))
     129                 :           1 :             r = get_float8_infinity();  /* max dist for NaN vs non-NaN */
     130                 :             :         else
     131                 :           0 :             r = 0.0;            /* must be Inf - Inf case */
     132                 :             :     }
     133                 :         550 :     PG_RETURN_FLOAT8(fabs(r));
     134                 :             : }
     135                 :             : 
     136                 :             : 
     137                 :             : /**************************************************
     138                 :             :  * GiST support functions
     139                 :             :  **************************************************/
     140                 :             : 
     141                 :             : Datum
     142                 :        1653 : gbt_float8_compress(PG_FUNCTION_ARGS)
     143                 :             : {
     144                 :        1653 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     145                 :             : 
     146                 :        1653 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     147                 :             : }
     148                 :             : 
     149                 :             : Datum
     150                 :         273 : gbt_float8_fetch(PG_FUNCTION_ARGS)
     151                 :             : {
     152                 :         273 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     153                 :             : 
     154                 :         273 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     155                 :             : }
     156                 :             : 
     157                 :             : Datum
     158                 :        2067 : gbt_float8_consistent(PG_FUNCTION_ARGS)
     159                 :             : {
     160                 :        2067 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     161                 :        2067 :     float8      query = PG_GETARG_FLOAT8(1);
     162                 :        2067 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     163                 :             : #ifdef NOT_USED
     164                 :             :     Oid         subtype = PG_GETARG_OID(3);
     165                 :             : #endif
     166                 :        2067 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     167                 :        2067 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     168                 :             :     GBT_NUMKEY_R key;
     169                 :             : 
     170                 :             :     /* All cases served by this function are exact */
     171                 :        2067 :     *recheck = false;
     172                 :             : 
     173                 :        2067 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     174                 :        2067 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     175                 :             : 
     176                 :        2067 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy,
     177                 :             :                                       GIST_LEAF(entry), &tinfo,
     178                 :             :                                       fcinfo->flinfo));
     179                 :             : }
     180                 :             : 
     181                 :             : Datum
     182                 :         275 : gbt_float8_distance(PG_FUNCTION_ARGS)
     183                 :             : {
     184                 :         275 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     185                 :         275 :     float8      query = PG_GETARG_FLOAT8(1);
     186                 :             : #ifdef NOT_USED
     187                 :             :     Oid         subtype = PG_GETARG_OID(3);
     188                 :             : #endif
     189                 :         275 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     190                 :             :     GBT_NUMKEY_R key;
     191                 :             : 
     192                 :         275 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     193                 :         275 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     194                 :             : 
     195                 :         275 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     196                 :             :                                       &tinfo, fcinfo->flinfo));
     197                 :             : }
     198                 :             : 
     199                 :             : Datum
     200                 :          15 : gbt_float8_union(PG_FUNCTION_ARGS)
     201                 :             : {
     202                 :          15 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     203                 :          15 :     void       *out = palloc(sizeof(float8KEY));
     204                 :             : 
     205                 :          15 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
     206                 :          15 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     207                 :             : }
     208                 :             : 
     209                 :             : Datum
     210                 :        1094 : gbt_float8_penalty(PG_FUNCTION_ARGS)
     211                 :             : {
     212                 :        1094 :     float8KEY  *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     213                 :        1094 :     float8KEY  *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     214                 :        1094 :     float      *result = (float *) PG_GETARG_POINTER(2);
     215                 :             : 
     216                 :        1094 :     float_penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     217                 :             : 
     218                 :        1094 :     PG_RETURN_POINTER(result);
     219                 :             : }
     220                 :             : 
     221                 :             : Datum
     222                 :           4 : gbt_float8_picksplit(PG_FUNCTION_ARGS)
     223                 :             : {
     224                 :           4 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     225                 :             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     226                 :             :                                         &tinfo, fcinfo->flinfo));
     227                 :             : }
     228                 :             : 
     229                 :             : Datum
     230                 :           3 : gbt_float8_same(PG_FUNCTION_ARGS)
     231                 :             : {
     232                 :           3 :     float8KEY  *b1 = (float8KEY *) PG_GETARG_POINTER(0);
     233                 :           3 :     float8KEY  *b2 = (float8KEY *) PG_GETARG_POINTER(1);
     234                 :           3 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     235                 :             : 
     236                 :           3 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     237                 :           3 :     PG_RETURN_POINTER(result);
     238                 :             : }
     239                 :             : 
     240                 :             : static int
     241                 :       10432 : gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     242                 :             : {
     243                 :       10432 :     float8KEY  *arg1 = (float8KEY *) DatumGetPointer(x);
     244                 :       10432 :     float8KEY  *arg2 = (float8KEY *) DatumGetPointer(y);
     245                 :             : 
     246                 :             :     /* for leaf items we expect lower == upper, so only compare lower */
     247                 :       10432 :     return float8_cmp_internal(arg1->lower, arg2->lower);
     248                 :             : }
     249                 :             : 
     250                 :             : Datum
     251                 :           4 : gbt_float8_sortsupport(PG_FUNCTION_ARGS)
     252                 :             : {
     253                 :           4 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     254                 :             : 
     255                 :           4 :     ssup->comparator = gbt_float8_ssup_cmp;
     256                 :           4 :     ssup->ssup_extra = NULL;
     257                 :             : 
     258                 :           4 :     PG_RETURN_VOID();
     259                 :             : }
        

Generated by: LCOV version 2.0-1