LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_cash.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 76 93 81.7 %
Date: 2025-08-31 01:17:28 Functions: 26 28 92.9 %
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           8 : PG_FUNCTION_INFO_V1(gbt_cash_compress);
      21           8 : PG_FUNCTION_INFO_V1(gbt_cash_fetch);
      22           8 : PG_FUNCTION_INFO_V1(gbt_cash_union);
      23           8 : PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
      24           8 : PG_FUNCTION_INFO_V1(gbt_cash_consistent);
      25           8 : PG_FUNCTION_INFO_V1(gbt_cash_distance);
      26           8 : PG_FUNCTION_INFO_V1(gbt_cash_penalty);
      27           8 : PG_FUNCTION_INFO_V1(gbt_cash_same);
      28           8 : PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
      29             : 
      30             : static bool
      31        3252 : gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
      32             : {
      33        3252 :     return (*((const Cash *) a) > *((const Cash *) b));
      34             : }
      35             : static bool
      36        1130 : gbt_cashge(const void *a, const void *b, FmgrInfo *flinfo)
      37             : {
      38        1130 :     return (*((const Cash *) a) >= *((const Cash *) b));
      39             : }
      40             : static bool
      41         544 : gbt_casheq(const void *a, const void *b, FmgrInfo *flinfo)
      42             : {
      43         544 :     return (*((const Cash *) a) == *((const Cash *) b));
      44             : }
      45             : static bool
      46        1108 : gbt_cashle(const void *a, const void *b, FmgrInfo *flinfo)
      47             : {
      48        1108 :     return (*((const Cash *) a) <= *((const Cash *) b));
      49             : }
      50             : static bool
      51        2710 : gbt_cashlt(const void *a, const void *b, FmgrInfo *flinfo)
      52             : {
      53        2710 :     return (*((const Cash *) a) < *((const Cash *) b));
      54             : }
      55             : 
      56             : static int
      57        1084 : gbt_cashkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      58             : {
      59        1084 :     cashKEY    *ia = (cashKEY *) (((const Nsrt *) a)->t);
      60        1084 :     cashKEY    *ib = (cashKEY *) (((const Nsrt *) b)->t);
      61             : 
      62        1084 :     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        1084 :     return (ia->lower > ib->lower) ? 1 : -1;
      71             : }
      72             : 
      73             : static float8
      74         546 : gbt_cash_dist(const void *a, const void *b, FmgrInfo *flinfo)
      75             : {
      76         546 :     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           8 : PG_FUNCTION_INFO_V1(cash_dist);
      96             : Datum
      97        1092 : cash_dist(PG_FUNCTION_ARGS)
      98             : {
      99        1092 :     Cash        a = PG_GETARG_CASH(0);
     100        1092 :     Cash        b = PG_GETARG_CASH(1);
     101             :     Cash        r;
     102             :     Cash        ra;
     103             : 
     104        1092 :     if (pg_sub_s64_overflow(a, b, &r) ||
     105        1092 :         r == PG_INT64_MIN)
     106           0 :         ereport(ERROR,
     107             :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     108             :                  errmsg("money out of range")));
     109             : 
     110        1092 :     ra = i64abs(r);
     111             : 
     112        1092 :     PG_RETURN_CASH(ra);
     113             : }
     114             : 
     115             : 
     116             : /**************************************************
     117             :  * GiST support functions
     118             :  **************************************************/
     119             : 
     120             : Datum
     121        1090 : gbt_cash_compress(PG_FUNCTION_ARGS)
     122             : {
     123        1090 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     124             : 
     125        1090 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     126             : }
     127             : 
     128             : Datum
     129         544 : gbt_cash_fetch(PG_FUNCTION_ARGS)
     130             : {
     131         544 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     132             : 
     133         544 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     134             : }
     135             : 
     136             : Datum
     137        3824 : gbt_cash_consistent(PG_FUNCTION_ARGS)
     138             : {
     139        3824 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     140        3824 :     Cash        query = PG_GETARG_CASH(1);
     141        3824 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     142             : 
     143             :     /* Oid      subtype = PG_GETARG_OID(3); */
     144        3824 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     145        3824 :     cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
     146             :     GBT_NUMKEY_R key;
     147             : 
     148             :     /* All cases served by this function are exact */
     149        3824 :     *recheck = false;
     150             : 
     151        3824 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     152        3824 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     153             : 
     154        3824 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     155             :                                       GIST_LEAF(entry), &tinfo,
     156             :                                       fcinfo->flinfo));
     157             : }
     158             : 
     159             : Datum
     160         548 : gbt_cash_distance(PG_FUNCTION_ARGS)
     161             : {
     162         548 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     163         548 :     Cash        query = PG_GETARG_CASH(1);
     164             : 
     165             :     /* Oid      subtype = PG_GETARG_OID(3); */
     166         548 :     cashKEY    *kkk = (cashKEY *) DatumGetPointer(entry->key);
     167             :     GBT_NUMKEY_R key;
     168             : 
     169         548 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     170         548 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     171             : 
     172         548 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     173             :                                       &tinfo, fcinfo->flinfo));
     174             : }
     175             : 
     176             : Datum
     177           2 : gbt_cash_union(PG_FUNCTION_ARGS)
     178             : {
     179           2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     180           2 :     void       *out = palloc(sizeof(cashKEY));
     181             : 
     182           2 :     *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
     183           2 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     184             : }
     185             : 
     186             : Datum
     187           0 : gbt_cash_penalty(PG_FUNCTION_ARGS)
     188             : {
     189           0 :     cashKEY    *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     190           0 :     cashKEY    *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     191           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     192             : 
     193           0 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     194             : 
     195           0 :     PG_RETURN_POINTER(result);
     196             : }
     197             : 
     198             : Datum
     199           2 : gbt_cash_picksplit(PG_FUNCTION_ARGS)
     200             : {
     201           2 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     202             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     203             :                                         &tinfo, fcinfo->flinfo));
     204             : }
     205             : 
     206             : Datum
     207           0 : gbt_cash_same(PG_FUNCTION_ARGS)
     208             : {
     209           0 :     cashKEY    *b1 = (cashKEY *) PG_GETARG_POINTER(0);
     210           0 :     cashKEY    *b2 = (cashKEY *) PG_GETARG_POINTER(1);
     211           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     212             : 
     213           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     214           0 :     PG_RETURN_POINTER(result);
     215             : }
     216             : 
     217             : static int
     218       10548 : gbt_cash_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     219             : {
     220       10548 :     cashKEY    *arg1 = (cashKEY *) DatumGetPointer(x);
     221       10548 :     cashKEY    *arg2 = (cashKEY *) DatumGetPointer(y);
     222             : 
     223             :     /* for leaf items we expect lower == upper, so only compare lower */
     224       10548 :     if (arg1->lower > arg2->lower)
     225        5852 :         return 1;
     226        4696 :     else if (arg1->lower < arg2->lower)
     227        4696 :         return -1;
     228             :     else
     229           0 :         return 0;
     230             : }
     231             : 
     232             : Datum
     233           2 : gbt_cash_sortsupport(PG_FUNCTION_ARGS)
     234             : {
     235           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     236             : 
     237           2 :     ssup->comparator = gbt_cash_ssup_cmp;
     238           2 :     ssup->ssup_extra = NULL;
     239             : 
     240           2 :     PG_RETURN_VOID();
     241             : }

Generated by: LCOV version 1.16