LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_interval.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 90 110 81.8 %
Date: 2025-08-19 14:18:13 Functions: 30 32 93.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/btree_gist/btree_interval.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include "btree_gist.h"
       7             : #include "btree_utils_num.h"
       8             : #include "utils/fmgrprotos.h"
       9             : #include "utils/rel.h"
      10             : #include "utils/sortsupport.h"
      11             : #include "utils/timestamp.h"
      12             : 
      13             : typedef struct
      14             : {
      15             :     Interval    lower,
      16             :                 upper;
      17             : } intvKEY;
      18             : 
      19             : /* GiST support functions */
      20           8 : PG_FUNCTION_INFO_V1(gbt_intv_compress);
      21           8 : PG_FUNCTION_INFO_V1(gbt_intv_fetch);
      22           8 : PG_FUNCTION_INFO_V1(gbt_intv_decompress);
      23           8 : PG_FUNCTION_INFO_V1(gbt_intv_union);
      24           8 : PG_FUNCTION_INFO_V1(gbt_intv_picksplit);
      25           8 : PG_FUNCTION_INFO_V1(gbt_intv_consistent);
      26           8 : PG_FUNCTION_INFO_V1(gbt_intv_distance);
      27           8 : PG_FUNCTION_INFO_V1(gbt_intv_penalty);
      28           8 : PG_FUNCTION_INFO_V1(gbt_intv_same);
      29           8 : PG_FUNCTION_INFO_V1(gbt_intv_sortsupport);
      30             : 
      31             : 
      32             : static bool
      33        4486 : gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
      34             : {
      35        4486 :     return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      36             : }
      37             : 
      38             : static bool
      39        1044 : gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
      40             : {
      41        1044 :     return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      42             : }
      43             : 
      44             : static bool
      45         300 : gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
      46             : {
      47         300 :     return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      48             : }
      49             : 
      50             : static bool
      51        1246 : gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
      52             : {
      53        1246 :     return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      54             : }
      55             : 
      56             : static bool
      57        4186 : gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
      58             : {
      59        4186 :     return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      60             : }
      61             : 
      62             : static int
      63        2394 : gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      64             : {
      65        2394 :     intvKEY    *ia = (intvKEY *) (((const Nsrt *) a)->t);
      66        2394 :     intvKEY    *ib = (intvKEY *) (((const Nsrt *) b)->t);
      67             :     int         res;
      68             : 
      69        2394 :     res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
      70        2394 :     if (res == 0)
      71           0 :         return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
      72             : 
      73        2394 :     return res;
      74             : }
      75             : 
      76             : 
      77             : static double
      78        1224 : intr2num(const Interval *i)
      79             : {
      80        1224 :     return INTERVAL_TO_SEC(i);
      81             : }
      82             : 
      83             : static float8
      84         612 : gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
      85             : {
      86         612 :     return fabs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
      87             : }
      88             : 
      89             : /*
      90             :  * INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
      91             :  * in pg_type.  This might be less than sizeof(Interval) if the compiler
      92             :  * insists on adding alignment padding at the end of the struct.  (Note:
      93             :  * this concern is obsolete with the current definition of Interval, but
      94             :  * was real before a separate "day" field was added to it.)
      95             :  */
      96             : #define INTERVALSIZE 16
      97             : 
      98             : static const gbtree_ninfo tinfo =
      99             : {
     100             :     gbt_t_intv,
     101             :     sizeof(Interval),
     102             :     32,                         /* sizeof(gbtreekey32) */
     103             :     gbt_intvgt,
     104             :     gbt_intvge,
     105             :     gbt_intveq,
     106             :     gbt_intvle,
     107             :     gbt_intvlt,
     108             :     gbt_intvkey_cmp,
     109             :     gbt_intv_dist
     110             : };
     111             : 
     112             : 
     113             : Interval *
     114        4468 : abs_interval(Interval *a)
     115             : {
     116             :     static const Interval zero = {0, 0, 0};
     117             : 
     118        4468 :     if (DatumGetBool(DirectFunctionCall2(interval_lt,
     119             :                                          IntervalPGetDatum(a),
     120             :                                          IntervalPGetDatum(&zero))))
     121        2478 :         a = DatumGetIntervalP(DirectFunctionCall1(interval_um,
     122             :                                                   IntervalPGetDatum(a)));
     123             : 
     124        4468 :     return a;
     125             : }
     126             : 
     127           8 : PG_FUNCTION_INFO_V1(interval_dist);
     128             : Datum
     129        1212 : interval_dist(PG_FUNCTION_ARGS)
     130             : {
     131        1212 :     Datum       diff = DirectFunctionCall2(interval_mi,
     132             :                                            PG_GETARG_DATUM(0),
     133             :                                            PG_GETARG_DATUM(1));
     134             : 
     135        1212 :     PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
     136             : }
     137             : 
     138             : 
     139             : /**************************************************
     140             :  * GiST support functions
     141             :  **************************************************/
     142             : 
     143             : Datum
     144        1208 : gbt_intv_compress(PG_FUNCTION_ARGS)
     145             : {
     146        1208 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     147        1208 :     GISTENTRY  *retval = entry;
     148             : 
     149        1208 :     if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
     150             :     {
     151        1200 :         char       *r = (char *) palloc(2 * INTERVALSIZE);
     152             : 
     153        1200 :         retval = palloc(sizeof(GISTENTRY));
     154             : 
     155        1200 :         if (entry->leafkey)
     156             :         {
     157        1200 :             Interval   *key = DatumGetIntervalP(entry->key);
     158             : 
     159        1200 :             memcpy(r, key, INTERVALSIZE);
     160        1200 :             memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
     161             :         }
     162             :         else
     163             :         {
     164           0 :             intvKEY    *key = (intvKEY *) DatumGetPointer(entry->key);
     165             : 
     166           0 :             memcpy(r, &key->lower, INTERVALSIZE);
     167           0 :             memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
     168             :         }
     169        1200 :         gistentryinit(*retval, PointerGetDatum(r),
     170             :                       entry->rel, entry->page,
     171             :                       entry->offset, false);
     172             :     }
     173             : 
     174        1208 :     PG_RETURN_POINTER(retval);
     175             : }
     176             : 
     177             : Datum
     178         300 : gbt_intv_fetch(PG_FUNCTION_ARGS)
     179             : {
     180         300 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     181             : 
     182         300 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     183             : }
     184             : 
     185             : Datum
     186        8756 : gbt_intv_decompress(PG_FUNCTION_ARGS)
     187             : {
     188        8756 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     189        8756 :     GISTENTRY  *retval = entry;
     190             : 
     191             :     if (INTERVALSIZE != sizeof(Interval))
     192             :     {
     193             :         intvKEY    *r = palloc(sizeof(intvKEY));
     194             :         char       *key = DatumGetPointer(entry->key);
     195             : 
     196             :         retval = palloc(sizeof(GISTENTRY));
     197             :         memcpy(&r->lower, key, INTERVALSIZE);
     198             :         memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
     199             : 
     200             :         gistentryinit(*retval, PointerGetDatum(r),
     201             :                       entry->rel, entry->page,
     202             :                       entry->offset, false);
     203             :     }
     204        8756 :     PG_RETURN_POINTER(retval);
     205             : }
     206             : 
     207             : 
     208             : Datum
     209        3340 : gbt_intv_consistent(PG_FUNCTION_ARGS)
     210             : {
     211        3340 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     212        3340 :     Interval   *query = PG_GETARG_INTERVAL_P(1);
     213        3340 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     214             : 
     215             :     /* Oid      subtype = PG_GETARG_OID(3); */
     216        3340 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     217        3340 :     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
     218             :     GBT_NUMKEY_R key;
     219             : 
     220             :     /* All cases served by this function are exact */
     221        3340 :     *recheck = false;
     222             : 
     223        3340 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     224        3340 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     225             : 
     226        3340 :     PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
     227             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     228             : }
     229             : 
     230             : 
     231             : Datum
     232         616 : gbt_intv_distance(PG_FUNCTION_ARGS)
     233             : {
     234         616 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     235         616 :     Interval   *query = PG_GETARG_INTERVAL_P(1);
     236             : 
     237             :     /* Oid      subtype = PG_GETARG_OID(3); */
     238         616 :     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
     239             :     GBT_NUMKEY_R key;
     240             : 
     241         616 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     242         616 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     243             : 
     244         616 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry),
     245             :                                       &tinfo, fcinfo->flinfo));
     246             : }
     247             : 
     248             : 
     249             : Datum
     250           2 : gbt_intv_union(PG_FUNCTION_ARGS)
     251             : {
     252           2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     253           2 :     void       *out = palloc(sizeof(intvKEY));
     254             : 
     255           2 :     *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
     256           2 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     257             : }
     258             : 
     259             : 
     260             : Datum
     261           0 : gbt_intv_penalty(PG_FUNCTION_ARGS)
     262             : {
     263           0 :     intvKEY    *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     264           0 :     intvKEY    *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     265           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     266             :     double      iorg[2],
     267             :                 inew[2];
     268             : 
     269           0 :     iorg[0] = intr2num(&origentry->lower);
     270           0 :     iorg[1] = intr2num(&origentry->upper);
     271           0 :     inew[0] = intr2num(&newentry->lower);
     272           0 :     inew[1] = intr2num(&newentry->upper);
     273             : 
     274           0 :     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
     275             : 
     276           0 :     PG_RETURN_POINTER(result);
     277             : }
     278             : 
     279             : Datum
     280           6 : gbt_intv_picksplit(PG_FUNCTION_ARGS)
     281             : {
     282           6 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     283             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     284             :                                         &tinfo, fcinfo->flinfo));
     285             : }
     286             : 
     287             : Datum
     288           0 : gbt_intv_same(PG_FUNCTION_ARGS)
     289             : {
     290           0 :     intvKEY    *b1 = (intvKEY *) PG_GETARG_POINTER(0);
     291           0 :     intvKEY    *b2 = (intvKEY *) PG_GETARG_POINTER(1);
     292           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     293             : 
     294           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     295           0 :     PG_RETURN_POINTER(result);
     296             : }
     297             : 
     298             : static int
     299       11352 : gbt_intv_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     300             : {
     301       11352 :     intvKEY    *arg1 = (intvKEY *) DatumGetPointer(x);
     302       11352 :     intvKEY    *arg2 = (intvKEY *) DatumGetPointer(y);
     303             : 
     304             :     /* for leaf items we expect lower == upper, so only compare lower */
     305       11352 :     return DatumGetInt32(DirectFunctionCall2(interval_cmp,
     306             :                                              IntervalPGetDatum(&arg1->lower),
     307             :                                              IntervalPGetDatum(&arg2->lower)));
     308             : }
     309             : 
     310             : Datum
     311           2 : gbt_intv_sortsupport(PG_FUNCTION_ARGS)
     312             : {
     313           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     314             : 
     315           2 :     ssup->comparator = gbt_intv_ssup_cmp;
     316           2 :     ssup->ssup_extra = NULL;
     317             : 
     318           2 :     PG_RETURN_VOID();
     319             : }

Generated by: LCOV version 1.16