LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float4.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 91.2 % 102 93
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: 35.7 % 28 10

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * contrib/btree_gist/btree_float4.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 float4key
      13                 :             : {
      14                 :             :     float4      lower;
      15                 :             :     float4      upper;
      16                 :             : } float4KEY;
      17                 :             : 
      18                 :             : /* GiST support functions */
      19                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_compress);
      20                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_fetch);
      21                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_union);
      22                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
      23                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_consistent);
      24                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_distance);
      25                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_penalty);
      26                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_same);
      27                 :           4 : PG_FUNCTION_INFO_V1(gbt_float4_sortsupport);
      28                 :             : 
      29                 :             : /*
      30                 :             :  * Use the NaN-aware comparators from utils/float.h, so that our results
      31                 :             :  * will agree with standard btree indexes.  Note that penalty and distance
      32                 :             :  * functions below must also cope with NaNs, in particular with the policy
      33                 :             :  * that all NaNs are equal.
      34                 :             :  */
      35                 :             : static bool
      36                 :        4389 : gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
      37                 :             : {
      38                 :        4389 :     return float4_gt(*((const float4 *) a), *((const float4 *) b));
      39                 :             : }
      40                 :             : static bool
      41                 :         802 : gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
      42                 :             : {
      43                 :         802 :     return float4_ge(*((const float4 *) a), *((const float4 *) b));
      44                 :             : }
      45                 :             : static bool
      46                 :         554 : gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
      47                 :             : {
      48                 :         554 :     return float4_eq(*((const float4 *) a), *((const float4 *) b));
      49                 :             : }
      50                 :             : static bool
      51                 :         560 : gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
      52                 :             : {
      53                 :         560 :     return float4_le(*((const float4 *) a), *((const float4 *) b));
      54                 :             : }
      55                 :             : static bool
      56                 :        4114 : gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
      57                 :             : {
      58                 :        4114 :     return float4_lt(*((const float4 *) a), *((const float4 *) b));
      59                 :             : }
      60                 :             : 
      61                 :             : static int
      62                 :        1098 : gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      63                 :             : {
      64                 :        1098 :     float4KEY  *ia = (float4KEY *) (((const Nsrt *) a)->t);
      65                 :        1098 :     float4KEY  *ib = (float4KEY *) (((const Nsrt *) b)->t);
      66                 :             :     int         res;
      67                 :             : 
      68                 :        1098 :     res = float4_cmp_internal(ia->lower, ib->lower);
      69         [ +  - ]:        1098 :     if (res != 0)
      70                 :        1098 :         return res;
      71                 :           0 :     return float4_cmp_internal(ia->upper, ib->upper);
      72                 :             : }
      73                 :             : 
      74                 :             : static float8
      75                 :         276 : gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
      76                 :             : {
      77                 :         276 :     float8      arg1 = *(const float4 *) a;
      78                 :         276 :     float8      arg2 = *(const float4 *) b;
      79                 :             :     float8      r;
      80                 :             : 
      81                 :         276 :     r = arg1 - arg2;
      82                 :             :     /* needn't consider isinf case here, must be due to input infinity */
      83         [ -  + ]:         276 :     if (unlikely(isnan(r)))
      84                 :             :     {
      85   [ #  #  #  # ]:           0 :         if (isnan(arg1) && isnan(arg2))
      86                 :           0 :             r = 0.0;            /* treat NaNs as equal */
      87   [ #  #  #  # ]:           0 :         else if (isnan(arg1) || isnan(arg2))
      88                 :           0 :             r = get_float8_infinity();  /* max dist for NaN vs non-NaN */
      89                 :             :         else
      90                 :           0 :             r = 0.0;            /* must be Inf - Inf case */
      91                 :             :     }
      92                 :         276 :     return fabs(r);
      93                 :             : }
      94                 :             : 
      95                 :             : 
      96                 :             : static const gbtree_ninfo tinfo =
      97                 :             : {
      98                 :             :     gbt_t_float4,
      99                 :             :     sizeof(float4),
     100                 :             :     8,                          /* sizeof(gbtreekey8) */
     101                 :             :     gbt_float4gt,
     102                 :             :     gbt_float4ge,
     103                 :             :     gbt_float4eq,
     104                 :             :     gbt_float4le,
     105                 :             :     gbt_float4lt,
     106                 :             :     gbt_float4key_cmp,
     107                 :             :     gbt_float4_dist
     108                 :             : };
     109                 :             : 
     110                 :             : 
     111                 :           4 : PG_FUNCTION_INFO_V1(float4_dist);
     112                 :             : Datum
     113                 :         553 : float4_dist(PG_FUNCTION_ARGS)
     114                 :             : {
     115                 :         553 :     float4      a = PG_GETARG_FLOAT4(0);
     116                 :         553 :     float4      b = PG_GETARG_FLOAT4(1);
     117                 :             :     float4      r;
     118                 :             : 
     119                 :         553 :     r = a - b;
     120   [ +  +  -  +  :         553 :     if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
                   -  - ]
     121                 :           0 :         float_overflow_error();
     122         [ +  + ]:         553 :     if (unlikely(isnan(r)))
     123                 :             :     {
     124   [ +  -  -  + ]:           1 :         if (isnan(a) && isnan(b))
     125                 :           0 :             r = 0.0;            /* treat NaNs as equal */
     126   [ -  +  -  - ]:           1 :         else if (isnan(a) || isnan(b))
     127                 :           1 :             r = get_float4_infinity();  /* max dist for NaN vs non-NaN */
     128                 :             :         else
     129                 :           0 :             r = 0.0;            /* must be Inf - Inf case */
     130                 :             :     }
     131                 :         553 :     PG_RETURN_FLOAT4(fabsf(r));
     132                 :             : }
     133                 :             : 
     134                 :             : 
     135                 :             : /**************************************************
     136                 :             :  * GiST support functions
     137                 :             :  **************************************************/
     138                 :             : 
     139                 :             : Datum
     140                 :        1658 : gbt_float4_compress(PG_FUNCTION_ARGS)
     141                 :             : {
     142                 :        1658 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     143                 :             : 
     144                 :        1658 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     145                 :             : }
     146                 :             : 
     147                 :             : Datum
     148                 :         275 : gbt_float4_fetch(PG_FUNCTION_ARGS)
     149                 :             : {
     150                 :         275 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     151                 :             : 
     152                 :         275 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     153                 :             : }
     154                 :             : 
     155                 :             : Datum
     156                 :        2215 : gbt_float4_consistent(PG_FUNCTION_ARGS)
     157                 :             : {
     158                 :        2215 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     159                 :        2215 :     float4      query = PG_GETARG_FLOAT4(1);
     160                 :        2215 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     161                 :             : #ifdef NOT_USED
     162                 :             :     Oid         subtype = PG_GETARG_OID(3);
     163                 :             : #endif
     164                 :        2215 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     165                 :        2215 :     float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
     166                 :             :     GBT_NUMKEY_R key;
     167                 :             : 
     168                 :             :     /* All cases served by this function are exact */
     169                 :        2215 :     *recheck = false;
     170                 :             : 
     171                 :        2215 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     172                 :        2215 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     173                 :             : 
     174                 :        2215 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy,
     175                 :             :                                       GIST_LEAF(entry), &tinfo,
     176                 :             :                                       fcinfo->flinfo));
     177                 :             : }
     178                 :             : 
     179                 :             : Datum
     180                 :         277 : gbt_float4_distance(PG_FUNCTION_ARGS)
     181                 :             : {
     182                 :         277 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     183                 :         277 :     float4      query = PG_GETARG_FLOAT4(1);
     184                 :             : #ifdef NOT_USED
     185                 :             :     Oid         subtype = PG_GETARG_OID(3);
     186                 :             : #endif
     187                 :         277 :     float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
     188                 :             :     GBT_NUMKEY_R key;
     189                 :             : 
     190                 :         277 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     191                 :         277 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     192                 :             : 
     193                 :         277 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     194                 :             :                                       &tinfo, fcinfo->flinfo));
     195                 :             : }
     196                 :             : 
     197                 :             : Datum
     198                 :           7 : gbt_float4_union(PG_FUNCTION_ARGS)
     199                 :             : {
     200                 :           7 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     201                 :           7 :     void       *out = palloc(sizeof(float4KEY));
     202                 :             : 
     203                 :           7 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
     204                 :           7 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     205                 :             : }
     206                 :             : 
     207                 :             : Datum
     208                 :         550 : gbt_float4_penalty(PG_FUNCTION_ARGS)
     209                 :             : {
     210                 :         550 :     float4KEY  *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     211                 :         550 :     float4KEY  *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     212                 :         550 :     float      *result = (float *) PG_GETARG_POINTER(2);
     213                 :             : 
     214                 :         550 :     float_penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     215                 :             : 
     216                 :         550 :     PG_RETURN_POINTER(result);
     217                 :             : }
     218                 :             : 
     219                 :             : Datum
     220                 :           2 : gbt_float4_picksplit(PG_FUNCTION_ARGS)
     221                 :             : {
     222                 :           2 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     223                 :             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     224                 :             :                                         &tinfo, fcinfo->flinfo));
     225                 :             : }
     226                 :             : 
     227                 :             : Datum
     228                 :           1 : gbt_float4_same(PG_FUNCTION_ARGS)
     229                 :             : {
     230                 :           1 :     float4KEY  *b1 = (float4KEY *) PG_GETARG_POINTER(0);
     231                 :           1 :     float4KEY  *b2 = (float4KEY *) PG_GETARG_POINTER(1);
     232                 :           1 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     233                 :             : 
     234                 :           1 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     235                 :           1 :     PG_RETURN_POINTER(result);
     236                 :             : }
     237                 :             : 
     238                 :             : static int
     239                 :       10276 : gbt_float4_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     240                 :             : {
     241                 :       10276 :     float4KEY  *arg1 = (float4KEY *) DatumGetPointer(x);
     242                 :       10276 :     float4KEY  *arg2 = (float4KEY *) DatumGetPointer(y);
     243                 :             : 
     244                 :             :     /* for leaf items we expect lower == upper, so only compare lower */
     245                 :       10276 :     return float4_cmp_internal(arg1->lower, arg2->lower);
     246                 :             : }
     247                 :             : 
     248                 :             : Datum
     249                 :           4 : gbt_float4_sortsupport(PG_FUNCTION_ARGS)
     250                 :             : {
     251                 :           4 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     252                 :             : 
     253                 :           4 :     ssup->comparator = gbt_float4_ssup_cmp;
     254                 :           4 :     ssup->ssup_extra = NULL;
     255                 :             : 
     256                 :           4 :     PG_RETURN_VOID();
     257                 :             : }
        

Generated by: LCOV version 2.0-1