LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_float4.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 74 78 94.9 %
Date: 2024-12-12 16:15:21 Functions: 25 25 100.0 %
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             : 
      10             : typedef struct float4key
      11             : {
      12             :     float4      lower;
      13             :     float4      upper;
      14             : } float4KEY;
      15             : 
      16             : /*
      17             : ** float4 ops
      18             : */
      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             : 
      28             : static bool
      29        2352 : gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
      30             : {
      31        2352 :     return (*((const float4 *) a) > *((const float4 *) b));
      32             : }
      33             : static bool
      34        1028 : gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
      35             : {
      36        1028 :     return (*((const float4 *) a) >= *((const float4 *) b));
      37             : }
      38             : static bool
      39        1376 : gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
      40             : {
      41        1376 :     return (*((const float4 *) a) == *((const float4 *) b));
      42             : }
      43             : static bool
      44        1642 : gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
      45             : {
      46        1642 :     return (*((const float4 *) a) <= *((const float4 *) b));
      47             : }
      48             : static bool
      49        2916 : gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
      50             : {
      51        2916 :     return (*((const float4 *) a) < *((const float4 *) b));
      52             : }
      53             : 
      54             : static int
      55        6494 : gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      56             : {
      57        6494 :     float4KEY  *ia = (float4KEY *) (((const Nsrt *) a)->t);
      58        6494 :     float4KEY  *ib = (float4KEY *) (((const Nsrt *) b)->t);
      59             : 
      60        6494 :     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        6494 :     return (ia->lower > ib->lower) ? 1 : -1;
      69             : }
      70             : 
      71             : static float8
      72         532 : gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
      73             : {
      74         532 :     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             :  * float4 ops
     111             :  **************************************************/
     112             : 
     113             : 
     114             : Datum
     115        1102 : gbt_float4_compress(PG_FUNCTION_ARGS)
     116             : {
     117        1102 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     118             : 
     119        1102 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     120             : }
     121             : 
     122             : Datum
     123         530 : gbt_float4_fetch(PG_FUNCTION_ARGS)
     124             : {
     125         530 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     126             : 
     127         530 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     128             : }
     129             : 
     130             : Datum
     131        3798 : gbt_float4_consistent(PG_FUNCTION_ARGS)
     132             : {
     133        3798 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     134        3798 :     float4      query = PG_GETARG_FLOAT4(1);
     135        3798 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     136             : 
     137             :     /* Oid      subtype = PG_GETARG_OID(3); */
     138        3798 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     139        3798 :     float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
     140             :     GBT_NUMKEY_R key;
     141             : 
     142             :     /* All cases served by this function are exact */
     143        3798 :     *recheck = false;
     144             : 
     145        3798 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     146        3798 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     147             : 
     148        3798 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     149             :                                       GIST_LEAF(entry), &tinfo,
     150             :                                       fcinfo->flinfo));
     151             : }
     152             : 
     153             : 
     154             : Datum
     155         534 : gbt_float4_distance(PG_FUNCTION_ARGS)
     156             : {
     157         534 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     158         534 :     float4      query = PG_GETARG_FLOAT4(1);
     159             : 
     160             :     /* Oid      subtype = PG_GETARG_OID(3); */
     161         534 :     float4KEY  *kkk = (float4KEY *) DatumGetPointer(entry->key);
     162             :     GBT_NUMKEY_R key;
     163             : 
     164         534 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     165         534 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     166             : 
     167         534 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     168             :                                       &tinfo, fcinfo->flinfo));
     169             : }
     170             : 
     171             : 
     172             : Datum
     173         426 : gbt_float4_union(PG_FUNCTION_ARGS)
     174             : {
     175         426 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     176         426 :     void       *out = palloc(sizeof(float4KEY));
     177             : 
     178         426 :     *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
     179         426 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     180             : }
     181             : 
     182             : 
     183             : Datum
     184         704 : gbt_float4_penalty(PG_FUNCTION_ARGS)
     185             : {
     186         704 :     float4KEY  *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     187         704 :     float4KEY  *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     188         704 :     float      *result = (float *) PG_GETARG_POINTER(2);
     189             : 
     190         704 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     191             : 
     192         704 :     PG_RETURN_POINTER(result);
     193             : }
     194             : 
     195             : Datum
     196           2 : gbt_float4_picksplit(PG_FUNCTION_ARGS)
     197             : {
     198           2 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     199             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     200             :                                         &tinfo, fcinfo->flinfo));
     201             : }
     202             : 
     203             : Datum
     204         424 : gbt_float4_same(PG_FUNCTION_ARGS)
     205             : {
     206         424 :     float4KEY  *b1 = (float4KEY *) PG_GETARG_POINTER(0);
     207         424 :     float4KEY  *b2 = (float4KEY *) PG_GETARG_POINTER(1);
     208         424 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     209             : 
     210         424 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     211         424 :     PG_RETURN_POINTER(result);
     212             : }

Generated by: LCOV version 1.14