LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float8.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 78 83 94.0 %
Date: 2024-04-26 14:11:25 Functions: 25 25 100.0 %
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             : 
      10             : typedef struct float8key
      11             : {
      12             :     float8      lower;
      13             :     float8      upper;
      14             : } float8KEY;
      15             : 
      16             : /*
      17             : ** float8 ops
      18             : */
      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             : 
      28             : 
      29             : static bool
      30        3194 : gbt_float8gt(const void *a, const void *b, FmgrInfo *flinfo)
      31             : {
      32        3194 :     return (*((const float8 *) a) > *((const float8 *) b));
      33             : }
      34             : static bool
      35         764 : gbt_float8ge(const void *a, const void *b, FmgrInfo *flinfo)
      36             : {
      37         764 :     return (*((const float8 *) a) >= *((const float8 *) b));
      38             : }
      39             : static bool
      40        1468 : gbt_float8eq(const void *a, const void *b, FmgrInfo *flinfo)
      41             : {
      42        1468 :     return (*((const float8 *) a) == *((const float8 *) b));
      43             : }
      44             : static bool
      45        1116 : gbt_float8le(const void *a, const void *b, FmgrInfo *flinfo)
      46             : {
      47        1116 :     return (*((const float8 *) a) <= *((const float8 *) b));
      48             : }
      49             : static bool
      50        3456 : gbt_float8lt(const void *a, const void *b, FmgrInfo *flinfo)
      51             : {
      52        3456 :     return (*((const float8 *) a) < *((const float8 *) b));
      53             : }
      54             : 
      55             : static int
      56       13610 : gbt_float8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      57             : {
      58       13610 :     float8KEY  *ia = (float8KEY *) (((const Nsrt *) a)->t);
      59       13610 :     float8KEY  *ib = (float8KEY *) (((const Nsrt *) b)->t);
      60             : 
      61       13610 :     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       13610 :     return (ia->lower > ib->lower) ? 1 : -1;
      70             : }
      71             : 
      72             : static float8
      73         276 : gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
      74             : {
      75         276 :     float8      arg1 = *(const float8 *) a;
      76         276 :     float8      arg2 = *(const float8 *) b;
      77             :     float8      r;
      78             : 
      79         276 :     r = arg1 - arg2;
      80         276 :     if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
      81           0 :         float_overflow_error();
      82         276 :     return fabs(r);
      83             : }
      84             : 
      85             : 
      86             : static const gbtree_ninfo tinfo =
      87             : {
      88             :     gbt_t_float8,
      89             :     sizeof(float8),
      90             :     16,                         /* sizeof(gbtreekey16) */
      91             :     gbt_float8gt,
      92             :     gbt_float8ge,
      93             :     gbt_float8eq,
      94             :     gbt_float8le,
      95             :     gbt_float8lt,
      96             :     gbt_float8key_cmp,
      97             :     gbt_float8_dist
      98             : };
      99             : 
     100             : 
     101           4 : PG_FUNCTION_INFO_V1(float8_dist);
     102             : Datum
     103        1094 : float8_dist(PG_FUNCTION_ARGS)
     104             : {
     105        1094 :     float8      a = PG_GETARG_FLOAT8(0);
     106        1094 :     float8      b = PG_GETARG_FLOAT8(1);
     107             :     float8      r;
     108             : 
     109        1094 :     r = a - b;
     110        1094 :     if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
     111           0 :         float_overflow_error();
     112             : 
     113        1094 :     PG_RETURN_FLOAT8(fabs(r));
     114             : }
     115             : 
     116             : /**************************************************
     117             :  * float8 ops
     118             :  **************************************************/
     119             : 
     120             : 
     121             : Datum
     122        1104 : gbt_float8_compress(PG_FUNCTION_ARGS)
     123             : {
     124        1104 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     125             : 
     126        1104 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     127             : }
     128             : 
     129             : Datum
     130         270 : gbt_float8_fetch(PG_FUNCTION_ARGS)
     131             : {
     132         270 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     133             : 
     134         270 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     135             : }
     136             : 
     137             : Datum
     138        3026 : gbt_float8_consistent(PG_FUNCTION_ARGS)
     139             : {
     140        3026 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     141        3026 :     float8      query = PG_GETARG_FLOAT8(1);
     142        3026 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     143             : 
     144             :     /* Oid      subtype = PG_GETARG_OID(3); */
     145        3026 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     146        3026 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     147             :     GBT_NUMKEY_R key;
     148             : 
     149             :     /* All cases served by this function are exact */
     150        3026 :     *recheck = false;
     151             : 
     152        3026 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     153        3026 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     154             : 
     155        3026 :     PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
     156             :                                       GIST_LEAF(entry), &tinfo,
     157             :                                       fcinfo->flinfo));
     158             : }
     159             : 
     160             : 
     161             : Datum
     162         278 : gbt_float8_distance(PG_FUNCTION_ARGS)
     163             : {
     164         278 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     165         278 :     float8      query = PG_GETARG_FLOAT8(1);
     166             : 
     167             :     /* Oid      subtype = PG_GETARG_OID(3); */
     168         278 :     float8KEY  *kkk = (float8KEY *) DatumGetPointer(entry->key);
     169             :     GBT_NUMKEY_R key;
     170             : 
     171         278 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     172         278 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     173             : 
     174         278 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
     175             :                                       &tinfo, fcinfo->flinfo));
     176             : }
     177             : 
     178             : 
     179             : Datum
     180         602 : gbt_float8_union(PG_FUNCTION_ARGS)
     181             : {
     182         602 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     183         602 :     void       *out = palloc(sizeof(float8KEY));
     184             : 
     185         602 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
     186         602 :     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
     187             : }
     188             : 
     189             : 
     190             : Datum
     191        1088 : gbt_float8_penalty(PG_FUNCTION_ARGS)
     192             : {
     193        1088 :     float8KEY  *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     194        1088 :     float8KEY  *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     195        1088 :     float      *result = (float *) PG_GETARG_POINTER(2);
     196             : 
     197        1088 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     198             : 
     199        1088 :     PG_RETURN_POINTER(result);
     200             : }
     201             : 
     202             : Datum
     203           6 : gbt_float8_picksplit(PG_FUNCTION_ARGS)
     204             : {
     205           6 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     206             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     207             :                                         &tinfo, fcinfo->flinfo));
     208             : }
     209             : 
     210             : Datum
     211         600 : gbt_float8_same(PG_FUNCTION_ARGS)
     212             : {
     213         600 :     float8KEY  *b1 = (float8KEY *) PG_GETARG_POINTER(0);
     214         600 :     float8KEY  *b2 = (float8KEY *) PG_GETARG_POINTER(1);
     215         600 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     216             : 
     217         600 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     218         600 :     PG_RETURN_POINTER(result);
     219             : }

Generated by: LCOV version 1.14