LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_cash.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 81.7 % 93 76
Test Date: 2026-02-17 17:20:33 Functions: 92.9 % 28 26
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*
       2              :  * contrib/btree_gist/btree_cash.c
       3              :  */
       4              : #include "postgres.h"
       5              : 
       6              : #include "btree_gist.h"
       7              : #include "btree_utils_num.h"
       8              : #include "common/int.h"
       9              : #include "utils/cash.h"
      10              : #include "utils/rel.h"
      11              : #include "utils/sortsupport.h"
      12              : 
      13              : typedef struct
      14              : {
      15              :     Cash        lower;
      16              :     Cash        upper;
      17              : } cashKEY;
      18              : 
      19              : /* GiST support functions */
      20            4 : PG_FUNCTION_INFO_V1(gbt_cash_compress);
      21            4 : PG_FUNCTION_INFO_V1(gbt_cash_fetch);
      22            4 : PG_FUNCTION_INFO_V1(gbt_cash_union);
      23            4 : PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
      24            4 : PG_FUNCTION_INFO_V1(gbt_cash_consistent);
      25            4 : PG_FUNCTION_INFO_V1(gbt_cash_distance);
      26            4 : PG_FUNCTION_INFO_V1(gbt_cash_penalty);
      27            4 : PG_FUNCTION_INFO_V1(gbt_cash_same);
      28            4 : PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
      29              : 
      30              : static bool
      31         1626 : gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
      32              : {
      33         1626 :     return (*((const Cash *) a) > *((const Cash *) b));
      34              : }
      35              : static bool
      36          565 : gbt_cashge(const void *a, const void *b, FmgrInfo *flinfo)
      37              : {
      38          565 :     return (*((const Cash *) a) >= *((const Cash *) b));
      39              : }
      40              : static bool
      41          272 : gbt_casheq(const void *a, const void *b, FmgrInfo *flinfo)
      42              : {
      43          272 :     return (*((const Cash *) a) == *((const Cash *) b));
      44              : }
      45              : static bool
      46          554 : gbt_cashle(const void *a, const void *b, FmgrInfo *flinfo)
      47              : {
      48          554 :     return (*((const Cash *) a) <= *((const Cash *) b));
      49              : }
      50              : static bool
      51         1355 : gbt_cashlt(const void *a, const void *b, FmgrInfo *flinfo)
      52              : {
      53         1355 :     return (*((const Cash *) a) < *((const Cash *) b));
      54              : }
      55              : 
      56              : static int
      57          542 : gbt_cashkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      58              : {
      59          542 :     cashKEY    *ia = (cashKEY *) (((const Nsrt *) a)->t);
      60          542 :     cashKEY    *ib = (cashKEY *) (((const Nsrt *) b)->t);
      61              : 
      62          542 :     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          542 :     return (ia->lower > ib->lower) ? 1 : -1;
      71              : }
      72              : 
      73              : static float8
      74          273 : gbt_cash_dist(const void *a, const void *b, FmgrInfo *flinfo)
      75              : {
      76          273 :     return GET_FLOAT_DISTANCE(Cash, a, b);
      77              : }
      78              : 
      79              : 
      80              : static const gbtree_ninfo tinfo =
      81              : {
      82              :     gbt_t_cash,
      83              :     sizeof(Cash),
      84              :     16,                         /* sizeof(gbtreekey16) */
      85              :     gbt_cashgt,
      86              :     gbt_cashge,
      87              :     gbt_casheq,
      88              :     gbt_cashle,
      89              :     gbt_cashlt,
      90              :     gbt_cashkey_cmp,
      91              :     gbt_cash_dist
      92              : };
      93              : 
      94              : 
      95            4 : PG_FUNCTION_INFO_V1(cash_dist);
      96              : Datum
      97          546 : cash_dist(PG_FUNCTION_ARGS)
      98              : {
      99          546 :     Cash        a = PG_GETARG_CASH(0);
     100          546 :     Cash        b = PG_GETARG_CASH(1);
     101              :     Cash        r;
     102              :     Cash        ra;
     103              : 
     104          546 :     if (pg_sub_s64_overflow(a, b, &r) ||
     105          546 :         r == PG_INT64_MIN)
     106            0 :         ereport(ERROR,
     107              :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     108              :                  errmsg("money out of range")));
     109              : 
     110          546 :     ra = i64abs(r);
     111              : 
     112          546 :     PG_RETURN_CASH(ra);
     113              : }
     114              : 
     115              : 
     116              : /**************************************************
     117              :  * GiST support functions
     118              :  **************************************************/
     119              : 
     120              : Datum
     121          545 : gbt_cash_compress(PG_FUNCTION_ARGS)
     122              : {
     123          545 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     124              : 
     125          545 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     126              : }
     127              : 
     128              : Datum
     129          272 : gbt_cash_fetch(PG_FUNCTION_ARGS)
     130              : {
     131          272 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     132              : 
     133          272 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     134              : }
     135              : 
     136              : Datum
     137         1912 : gbt_cash_consistent(PG_FUNCTION_ARGS)
     138              : {
     139         1912 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     140         1912 :     Cash        query = PG_GETARG_CASH(1);
     141         1912 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     142              : #ifdef NOT_USED
     143              :     Oid         subtype = PG_GETARG_OID(3);
     144              : #endif
     145         1912 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     146         1912 :     cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
     147              :     GBT_NUMKEY_R key;
     148              : 
     149              :     /* All cases served by this function are exact */
     150         1912 :     *recheck = false;
     151              : 
     152         1912 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     153         1912 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     154              : 
     155         1912 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     156              :                                       GIST_LEAF(entry), &tinfo,
     157              :                                       fcinfo->flinfo));
     158              : }
     159              : 
     160              : Datum
     161          274 : gbt_cash_distance(PG_FUNCTION_ARGS)
     162              : {
     163          274 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     164          274 :     Cash        query = PG_GETARG_CASH(1);
     165              : #ifdef NOT_USED
     166              :     Oid         subtype = PG_GETARG_OID(3);
     167              : #endif
     168          274 :     cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
     169              :     GBT_NUMKEY_R key;
     170              : 
     171          274 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     172          274 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     173              : 
     174          274 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     175              :                                       &tinfo, fcinfo->flinfo));
     176              : }
     177              : 
     178              : Datum
     179            1 : gbt_cash_union(PG_FUNCTION_ARGS)
     180              : {
     181            1 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     182            1 :     void       *out = palloc(sizeof(cashKEY));
     183              : 
     184            1 :     *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
     185            1 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     186              : }
     187              : 
     188              : Datum
     189            0 : gbt_cash_penalty(PG_FUNCTION_ARGS)
     190              : {
     191            0 :     cashKEY    *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     192            0 :     cashKEY    *newentry = (cashKEY *) 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            1 : gbt_cash_picksplit(PG_FUNCTION_ARGS)
     202              : {
     203            1 :     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_cash_same(PG_FUNCTION_ARGS)
     210              : {
     211            0 :     cashKEY    *b1 = (cashKEY *) PG_GETARG_POINTER(0);
     212            0 :     cashKEY    *b2 = (cashKEY *) 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         5274 : gbt_cash_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     221              : {
     222         5274 :     cashKEY    *arg1 = (cashKEY *) DatumGetPointer(x);
     223         5274 :     cashKEY    *arg2 = (cashKEY *) DatumGetPointer(y);
     224              : 
     225              :     /* for leaf items we expect lower == upper, so only compare lower */
     226         5274 :     if (arg1->lower > arg2->lower)
     227         2926 :         return 1;
     228         2348 :     else if (arg1->lower < arg2->lower)
     229         2348 :         return -1;
     230              :     else
     231            0 :         return 0;
     232              : }
     233              : 
     234              : Datum
     235            1 : gbt_cash_sortsupport(PG_FUNCTION_ARGS)
     236              : {
     237            1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     238              : 
     239            1 :     ssup->comparator = gbt_cash_ssup_cmp;
     240            1 :     ssup->ssup_extra = NULL;
     241              : 
     242            1 :     PG_RETURN_VOID();
     243              : }
        

Generated by: LCOV version 2.0-1