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

Generated by: LCOV version 1.14