LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_interval.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 96 100 96.0 %
Date: 2024-04-26 06:11:47 Functions: 29 29 100.0 %
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/builtins.h"
       9             : #include "utils/timestamp.h"
      10             : 
      11             : typedef struct
      12             : {
      13             :     Interval    lower,
      14             :                 upper;
      15             : } intvKEY;
      16             : 
      17             : 
      18             : /*
      19             : ** Interval ops
      20             : */
      21           4 : PG_FUNCTION_INFO_V1(gbt_intv_compress);
      22           4 : PG_FUNCTION_INFO_V1(gbt_intv_fetch);
      23           4 : PG_FUNCTION_INFO_V1(gbt_intv_decompress);
      24           4 : PG_FUNCTION_INFO_V1(gbt_intv_union);
      25           4 : PG_FUNCTION_INFO_V1(gbt_intv_picksplit);
      26           4 : PG_FUNCTION_INFO_V1(gbt_intv_consistent);
      27           4 : PG_FUNCTION_INFO_V1(gbt_intv_distance);
      28           4 : PG_FUNCTION_INFO_V1(gbt_intv_penalty);
      29           4 : PG_FUNCTION_INFO_V1(gbt_intv_same);
      30             : 
      31             : 
      32             : static bool
      33        3108 : gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
      34             : {
      35        3108 :     return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      36             : }
      37             : 
      38             : static bool
      39        1000 : gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
      40             : {
      41        1000 :     return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      42             : }
      43             : 
      44             : static bool
      45        2018 : gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
      46             : {
      47        2018 :     return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      48             : }
      49             : 
      50             : static bool
      51        1190 : gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
      52             : {
      53        1190 :     return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      54             : }
      55             : 
      56             : static bool
      57        2756 : gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
      58             : {
      59        2756 :     return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      60             : }
      61             : 
      62             : static int
      63        7954 : gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      64             : {
      65        7954 :     intvKEY    *ia = (intvKEY *) (((const Nsrt *) a)->t);
      66        7954 :     intvKEY    *ib = (intvKEY *) (((const Nsrt *) b)->t);
      67             :     int         res;
      68             : 
      69        7954 :     res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
      70        7954 :     if (res == 0)
      71           0 :         return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
      72             : 
      73        7954 :     return res;
      74             : }
      75             : 
      76             : 
      77             : static double
      78       10456 : intr2num(const Interval *i)
      79             : {
      80       10456 :     return INTERVAL_TO_SEC(i);
      81             : }
      82             : 
      83             : static float8
      84         588 : gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
      85             : {
      86         588 :     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 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           4 : 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             :  * interval ops
     141             :  **************************************************/
     142             : 
     143             : 
     144             : Datum
     145        1226 : gbt_intv_compress(PG_FUNCTION_ARGS)
     146             : {
     147        1226 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     148        1226 :     GISTENTRY  *retval = entry;
     149             : 
     150        1226 :     if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
     151             :     {
     152        1200 :         char       *r = (char *) palloc(2 * INTERVALSIZE);
     153             : 
     154        1200 :         retval = palloc(sizeof(GISTENTRY));
     155             : 
     156        1200 :         if (entry->leafkey)
     157             :         {
     158        1200 :             Interval   *key = DatumGetIntervalP(entry->key);
     159             : 
     160        1200 :             memcpy(r, key, INTERVALSIZE);
     161        1200 :             memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
     162             :         }
     163             :         else
     164             :         {
     165           0 :             intvKEY    *key = (intvKEY *) DatumGetPointer(entry->key);
     166             : 
     167           0 :             memcpy(r, &key->lower, INTERVALSIZE);
     168           0 :             memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
     169             :         }
     170        1200 :         gistentryinit(*retval, PointerGetDatum(r),
     171             :                       entry->rel, entry->page,
     172             :                       entry->offset, false);
     173             :     }
     174             : 
     175        1226 :     PG_RETURN_POINTER(retval);
     176             : }
     177             : 
     178             : Datum
     179         288 : gbt_intv_fetch(PG_FUNCTION_ARGS)
     180             : {
     181         288 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     182             : 
     183         288 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     184             : }
     185             : 
     186             : Datum
     187       10520 : gbt_intv_decompress(PG_FUNCTION_ARGS)
     188             : {
     189       10520 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     190       10520 :     GISTENTRY  *retval = entry;
     191             : 
     192             :     if (INTERVALSIZE != sizeof(Interval))
     193             :     {
     194             :         intvKEY    *r = palloc(sizeof(intvKEY));
     195             :         char       *key = DatumGetPointer(entry->key);
     196             : 
     197             :         retval = palloc(sizeof(GISTENTRY));
     198             :         memcpy(&r->lower, key, INTERVALSIZE);
     199             :         memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
     200             : 
     201             :         gistentryinit(*retval, PointerGetDatum(r),
     202             :                       entry->rel, entry->page,
     203             :                       entry->offset, false);
     204             :     }
     205       10520 :     PG_RETURN_POINTER(retval);
     206             : }
     207             : 
     208             : 
     209             : Datum
     210        3304 : gbt_intv_consistent(PG_FUNCTION_ARGS)
     211             : {
     212        3304 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     213        3304 :     Interval   *query = PG_GETARG_INTERVAL_P(1);
     214        3304 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     215             : 
     216             :     /* Oid      subtype = PG_GETARG_OID(3); */
     217        3304 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     218        3304 :     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
     219             :     GBT_NUMKEY_R key;
     220             : 
     221             :     /* All cases served by this function are exact */
     222        3304 :     *recheck = false;
     223             : 
     224        3304 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     225        3304 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     226             : 
     227        3304 :     PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) query, &strategy,
     228             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     229             : }
     230             : 
     231             : 
     232             : Datum
     233         592 : gbt_intv_distance(PG_FUNCTION_ARGS)
     234             : {
     235         592 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     236         592 :     Interval   *query = PG_GETARG_INTERVAL_P(1);
     237             : 
     238             :     /* Oid      subtype = PG_GETARG_OID(3); */
     239         592 :     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
     240             :     GBT_NUMKEY_R key;
     241             : 
     242         592 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     243         592 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     244             : 
     245         592 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) query, GIST_LEAF(entry),
     246             :                                       &tinfo, fcinfo->flinfo));
     247             : }
     248             : 
     249             : 
     250             : Datum
     251         872 : gbt_intv_union(PG_FUNCTION_ARGS)
     252             : {
     253         872 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     254         872 :     void       *out = palloc(sizeof(intvKEY));
     255             : 
     256         872 :     *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
     257         872 :     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
     258             : }
     259             : 
     260             : 
     261             : Datum
     262        2320 : gbt_intv_penalty(PG_FUNCTION_ARGS)
     263             : {
     264        2320 :     intvKEY    *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     265        2320 :     intvKEY    *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     266        2320 :     float      *result = (float *) PG_GETARG_POINTER(2);
     267             :     double      iorg[2],
     268             :                 inew[2];
     269             : 
     270        2320 :     iorg[0] = intr2num(&origentry->lower);
     271        2320 :     iorg[1] = intr2num(&origentry->upper);
     272        2320 :     inew[0] = intr2num(&newentry->lower);
     273        2320 :     inew[1] = intr2num(&newentry->upper);
     274             : 
     275        2320 :     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
     276             : 
     277        2320 :     PG_RETURN_POINTER(result);
     278             : }
     279             : 
     280             : Datum
     281           6 : gbt_intv_picksplit(PG_FUNCTION_ARGS)
     282             : {
     283           6 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     284             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     285             :                                         &tinfo, fcinfo->flinfo));
     286             : }
     287             : 
     288             : Datum
     289         870 : gbt_intv_same(PG_FUNCTION_ARGS)
     290             : {
     291         870 :     intvKEY    *b1 = (intvKEY *) PG_GETARG_POINTER(0);
     292         870 :     intvKEY    *b2 = (intvKEY *) PG_GETARG_POINTER(1);
     293         870 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     294             : 
     295         870 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     296         870 :     PG_RETURN_POINTER(result);
     297             : }

Generated by: LCOV version 1.14