LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float4.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 81.8 % 88 72
Test Date: 2026-03-03 11:15:01 Functions: 92.9 % 28 26
Legend: Lines:     hit not hit

            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              : static bool
      30         1364 : gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
      31              : {
      32         1364 :     return (*((const float4 *) a) > *((const float4 *) b));
      33              : }
      34              : static bool
      35          522 : gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
      36              : {
      37          522 :     return (*((const float4 *) a) >= *((const float4 *) b));
      38              : }
      39              : static bool
      40          273 : gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
      41              : {
      42          273 :     return (*((const float4 *) a) == *((const float4 *) b));
      43              : }
      44              : static bool
      45          829 : gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
      46              : {
      47          829 :     return (*((const float4 *) a) <= *((const float4 *) b));
      48              : }
      49              : static bool
      50         1638 : gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
      51              : {
      52         1638 :     return (*((const float4 *) a) < *((const float4 *) b));
      53              : }
      54              : 
      55              : static int
      56          546 : gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      57              : {
      58          546 :     float4KEY  *ia = (float4KEY *) (((const Nsrt *) a)->t);
      59          546 :     float4KEY  *ib = (float4KEY *) (((const Nsrt *) b)->t);
      60              : 
      61          546 :     if (ia->lower == ib->lower)
      62              :     {
      63            0 :         if (ia->upper == ib->upper)
      64            0 :             return 0;
      65              : 
      66            0 :         return (ia->upper > ib->upper) ? 1 : -1;
      67              :     }
      68              : 
      69          546 :     return (ia->lower > ib->lower) ? 1 : -1;
      70              : }
      71              : 
      72              : static float8
      73          274 : gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
      74              : {
      75          274 :     return GET_FLOAT_DISTANCE(float4, a, b);
      76              : }
      77              : 
      78              : 
      79              : static const gbtree_ninfo tinfo =
      80              : {
      81              :     gbt_t_float4,
      82              :     sizeof(float4),
      83              :     8,                          /* sizeof(gbtreekey8) */
      84              :     gbt_float4gt,
      85              :     gbt_float4ge,
      86              :     gbt_float4eq,
      87              :     gbt_float4le,
      88              :     gbt_float4lt,
      89              :     gbt_float4key_cmp,
      90              :     gbt_float4_dist
      91              : };
      92              : 
      93              : 
      94            4 : PG_FUNCTION_INFO_V1(float4_dist);
      95              : Datum
      96          550 : float4_dist(PG_FUNCTION_ARGS)
      97              : {
      98          550 :     float4      a = PG_GETARG_FLOAT4(0);
      99          550 :     float4      b = PG_GETARG_FLOAT4(1);
     100              :     float4      r;
     101              : 
     102          550 :     r = a - b;
     103          550 :     if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
     104            0 :         float_overflow_error();
     105              : 
     106          550 :     PG_RETURN_FLOAT4(fabsf(r));
     107              : }
     108              : 
     109              : 
     110              : /**************************************************
     111              :  * GiST support functions
     112              :  **************************************************/
     113              : 
     114              : Datum
     115          549 : gbt_float4_compress(PG_FUNCTION_ARGS)
     116              : {
     117          549 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     118              : 
     119          549 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     120              : }
     121              : 
     122              : Datum
     123          273 : gbt_float4_fetch(PG_FUNCTION_ARGS)
     124              : {
     125          273 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     126              : 
     127          273 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     128              : }
     129              : 
     130              : Datum
     131         1923 : gbt_float4_consistent(PG_FUNCTION_ARGS)
     132              : {
     133         1923 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     134         1923 :     float4      query = PG_GETARG_FLOAT4(1);
     135         1923 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     136              : #ifdef NOT_USED
     137              :     Oid         subtype = PG_GETARG_OID(3);
     138              : #endif
     139         1923 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     140         1923 :     float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
     141              :     GBT_NUMKEY_R key;
     142              : 
     143              :     /* All cases served by this function are exact */
     144         1923 :     *recheck = false;
     145              : 
     146         1923 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     147         1923 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     148              : 
     149         1923 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     150              :                                       GIST_LEAF(entry), &tinfo,
     151              :                                       fcinfo->flinfo));
     152              : }
     153              : 
     154              : Datum
     155          275 : gbt_float4_distance(PG_FUNCTION_ARGS)
     156              : {
     157          275 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     158          275 :     float4      query = PG_GETARG_FLOAT4(1);
     159              : #ifdef NOT_USED
     160              :     Oid         subtype = PG_GETARG_OID(3);
     161              : #endif
     162          275 :     float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
     163              :     GBT_NUMKEY_R key;
     164              : 
     165          275 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     166          275 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     167              : 
     168          275 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     169              :                                       &tinfo, fcinfo->flinfo));
     170              : }
     171              : 
     172              : Datum
     173            1 : gbt_float4_union(PG_FUNCTION_ARGS)
     174              : {
     175            1 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     176            1 :     void       *out = palloc(sizeof(float4KEY));
     177              : 
     178            1 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
     179            1 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     180              : }
     181              : 
     182              : Datum
     183            0 : gbt_float4_penalty(PG_FUNCTION_ARGS)
     184              : {
     185            0 :     float4KEY  *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     186            0 :     float4KEY  *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     187            0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     188              : 
     189            0 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     190              : 
     191            0 :     PG_RETURN_POINTER(result);
     192              : }
     193              : 
     194              : Datum
     195            1 : gbt_float4_picksplit(PG_FUNCTION_ARGS)
     196              : {
     197            1 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     198              :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     199              :                                         &tinfo, fcinfo->flinfo));
     200              : }
     201              : 
     202              : Datum
     203            0 : gbt_float4_same(PG_FUNCTION_ARGS)
     204              : {
     205            0 :     float4KEY  *b1 = (float4KEY *) PG_GETARG_POINTER(0);
     206            0 :     float4KEY  *b2 = (float4KEY *) PG_GETARG_POINTER(1);
     207            0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     208              : 
     209            0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     210            0 :     PG_RETURN_POINTER(result);
     211              : }
     212              : 
     213              : static int
     214         5031 : gbt_float4_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     215              : {
     216         5031 :     float4KEY  *arg1 = (float4KEY *) DatumGetPointer(x);
     217         5031 :     float4KEY  *arg2 = (float4KEY *) DatumGetPointer(y);
     218              : 
     219              :     /* for leaf items we expect lower == upper, so only compare lower */
     220         5031 :     return float4_cmp_internal(arg1->lower, arg2->lower);
     221              : }
     222              : 
     223              : Datum
     224            1 : gbt_float4_sortsupport(PG_FUNCTION_ARGS)
     225              : {
     226            1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     227              : 
     228            1 :     ssup->comparator = gbt_float4_ssup_cmp;
     229            1 :     ssup->ssup_extra = NULL;
     230              : 
     231            1 :     PG_RETURN_VOID();
     232              : }
        

Generated by: LCOV version 2.0-1