LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float8.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 76 93 81.7 %
Date: 2025-08-31 01:17:28 Functions: 26 28 92.9 %
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           8 : PG_FUNCTION_INFO_V1(gbt_float8_compress);
      20           8 : PG_FUNCTION_INFO_V1(gbt_float8_fetch);
      21           8 : PG_FUNCTION_INFO_V1(gbt_float8_union);
      22           8 : PG_FUNCTION_INFO_V1(gbt_float8_picksplit);
      23           8 : PG_FUNCTION_INFO_V1(gbt_float8_consistent);
      24           8 : PG_FUNCTION_INFO_V1(gbt_float8_distance);
      25           8 : PG_FUNCTION_INFO_V1(gbt_float8_penalty);
      26           8 : PG_FUNCTION_INFO_V1(gbt_float8_same);
      27           8 : PG_FUNCTION_INFO_V1(gbt_float8_sortsupport);
      28             : 
      29             : 
      30             : static bool
      31        2714 : gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
      32             : {
      33        2714 :     return (*((const float8 *) a) > *((const float8 *) b));
      34             : }
      35             : static bool
      36        1028 : gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
      37             : {
      38        1028 :     return (*((const float8 *) a) >= *((const float8 *) b));
      39             : }
      40             : static bool
      41         544 : gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
      42             : {
      43         544 :     return (*((const float8 *) a) == *((const float8 *) b));
      44             : }
      45             : static bool
      46        1650 : gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
      47             : {
      48        1650 :     return (*((const float8 *) a) <= *((const float8 *) b));
      49             : }
      50             : static bool
      51        3258 : gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
      52             : {
      53        3258 :     return (*((const float8 *) a) < *((const float8 *) b));
      54             : }
      55             : 
      56             : static int
      57        1086 : gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      58             : {
      59        1086 :     float8KEY  *ia = (float8KEY *) (((const Nsrt *) a)->t);
      60        1086 :     float8KEY  *ib = (float8KEY *) (((const Nsrt *) b)->t);
      61             : 
      62        1086 :     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        1086 :     return (ia->lower > ib->lower) ? 1 : -1;
      71             : }
      72             : 
      73             : static float8
      74         546 : gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
      75             : {
      76         546 :     float8      arg1 = *(const float8 *) a;
      77         546 :     float8      arg2 = *(const float8 *) b;
      78             :     float8      r;
      79             : 
      80         546 :     r = arg1 - arg2;
      81         546 :     if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
      82           0 :         float_overflow_error();
      83         546 :     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           8 : PG_FUNCTION_INFO_V1(float8_dist);
     103             : Datum
     104        1094 : float8_dist(PG_FUNCTION_ARGS)
     105             : {
     106        1094 :     float8      a = PG_GETARG_FLOAT8(0);
     107        1094 :     float8      b = PG_GETARG_FLOAT8(1);
     108             :     float8      r;
     109             : 
     110        1094 :     r = a - b;
     111        1094 :     if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
     112           0 :         float_overflow_error();
     113             : 
     114        1094 :     PG_RETURN_FLOAT8(fabs(r));
     115             : }
     116             : 
     117             : 
     118             : /**************************************************
     119             :  * GiST support functions
     120             :  **************************************************/
     121             : 
     122             : Datum
     123        1092 : gbt_float8_compress(PG_FUNCTION_ARGS)
     124             : {
     125        1092 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     126             : 
     127        1092 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     128             : }
     129             : 
     130             : Datum
     131         544 : gbt_float8_fetch(PG_FUNCTION_ARGS)
     132             : {
     133         544 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     134             : 
     135         544 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     136             : }
     137             : 
     138             : Datum
     139        3828 : gbt_float8_consistent(PG_FUNCTION_ARGS)
     140             : {
     141        3828 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     142        3828 :     float8      query = PG_GETARG_FLOAT8(1);
     143        3828 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     144             : 
     145             :     /* Oid      subtype = PG_GETARG_OID(3); */
     146        3828 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     147        3828 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     148             :     GBT_NUMKEY_R key;
     149             : 
     150             :     /* All cases served by this function are exact */
     151        3828 :     *recheck = false;
     152             : 
     153        3828 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     154        3828 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     155             : 
     156        3828 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     157             :                                       GIST_LEAF(entry), &tinfo,
     158             :                                       fcinfo->flinfo));
     159             : }
     160             : 
     161             : Datum
     162         548 : gbt_float8_distance(PG_FUNCTION_ARGS)
     163             : {
     164         548 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     165         548 :     float8      query = PG_GETARG_FLOAT8(1);
     166             : 
     167             :     /* Oid      subtype = PG_GETARG_OID(3); */
     168         548 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     169             :     GBT_NUMKEY_R key;
     170             : 
     171         548 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     172         548 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     173             : 
     174         548 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     175             :                                       &tinfo, fcinfo->flinfo));
     176             : }
     177             : 
     178             : Datum
     179           2 : gbt_float8_union(PG_FUNCTION_ARGS)
     180             : {
     181           2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     182           2 :     void       *out = palloc(sizeof(float8KEY));
     183             : 
     184           2 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
     185           2 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     186             : }
     187             : 
     188             : Datum
     189           0 : gbt_float8_penalty(PG_FUNCTION_ARGS)
     190             : {
     191           0 :     float8KEY  *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     192           0 :     float8KEY  *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     193           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     194             : 
     195           0 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     196             : 
     197           0 :     PG_RETURN_POINTER(result);
     198             : }
     199             : 
     200             : Datum
     201           2 : gbt_float8_picksplit(PG_FUNCTION_ARGS)
     202             : {
     203           2 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     204             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     205             :                                         &tinfo, fcinfo->flinfo));
     206             : }
     207             : 
     208             : Datum
     209           0 : gbt_float8_same(PG_FUNCTION_ARGS)
     210             : {
     211           0 :     float8KEY  *b1 = (float8KEY *) PG_GETARG_POINTER(0);
     212           0 :     float8KEY  *b2 = (float8KEY *) PG_GETARG_POINTER(1);
     213           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     214             : 
     215           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     216           0 :     PG_RETURN_POINTER(result);
     217             : }
     218             : 
     219             : static int
     220       10554 : gbt_float8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     221             : {
     222       10554 :     float8KEY  *arg1 = (float8KEY *) DatumGetPointer(x);
     223       10554 :     float8KEY  *arg2 = (float8KEY *) DatumGetPointer(y);
     224             : 
     225             :     /* for leaf items we expect lower == upper, so only compare lower */
     226       10554 :     return float8_cmp_internal(arg1->lower, arg2->lower);
     227             : }
     228             : 
     229             : Datum
     230           2 : gbt_float8_sortsupport(PG_FUNCTION_ARGS)
     231             : {
     232           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     233             : 
     234           2 :     ssup->comparator = gbt_float8_ssup_cmp;
     235           2 :     ssup->ssup_extra = NULL;
     236             : 
     237           2 :     PG_RETURN_VOID();
     238             : }

Generated by: LCOV version 1.16