LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_interval.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 84.5 % 129 109
Test Date: 2026-12-01 02:16:10 Functions: 93.8 % 32 30
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 55.3 % 76 42

             Branch data     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 "common/int.h"
       9                 :             : #include "utils/fmgrprotos.h"
      10                 :             : #include "utils/rel.h"
      11                 :             : #include "utils/sortsupport.h"
      12                 :             : #include "utils/timestamp.h"
      13                 :             : 
      14                 :             : typedef struct
      15                 :             : {
      16                 :             :     Interval    lower,
      17                 :             :                 upper;
      18                 :             : } intvKEY;
      19                 :             : 
      20                 :             : /* GiST support functions */
      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                 :           4 : PG_FUNCTION_INFO_V1(gbt_intv_sortsupport);
      31                 :             : 
      32                 :             : 
      33                 :             : static bool
      34                 :        2093 : gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
      35                 :             : {
      36                 :        2093 :     return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      37                 :             : }
      38                 :             : 
      39                 :             : static bool
      40                 :         375 : gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
      41                 :             : {
      42                 :         375 :     return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      43                 :             : }
      44                 :             : 
      45                 :             : static bool
      46                 :         150 : gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
      47                 :             : {
      48                 :         150 :     return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      49                 :             : }
      50                 :             : 
      51                 :             : static bool
      52                 :         770 : gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
      53                 :             : {
      54                 :         770 :     return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      55                 :             : }
      56                 :             : 
      57                 :             : static bool
      58                 :        2243 : gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
      59                 :             : {
      60                 :        2243 :     return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
      61                 :             : }
      62                 :             : 
      63                 :             : static int
      64                 :        1197 : gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      65                 :             : {
      66                 :        1197 :     intvKEY    *ia = (intvKEY *) (((const Nsrt *) a)->t);
      67                 :        1197 :     intvKEY    *ib = (intvKEY *) (((const Nsrt *) b)->t);
      68                 :             :     int         res;
      69                 :             : 
      70                 :        1197 :     res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
      71         [ -  + ]:        1197 :     if (res == 0)
      72                 :           0 :         return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
      73                 :             : 
      74                 :        1197 :     return res;
      75                 :             : }
      76                 :             : 
      77                 :             : 
      78                 :             : static double
      79                 :         612 : intr2num(const Interval *i)
      80                 :             : {
      81                 :         612 :     return INTERVAL_TO_SEC(i);
      82                 :             : }
      83                 :             : 
      84                 :             : static float8
      85                 :         306 : gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
      86                 :             : {
      87                 :         306 :     return fabs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
      88                 :             : }
      89                 :             : 
      90                 :             : /*
      91                 :             :  * INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
      92                 :             :  * in pg_type.  This might be less than sizeof(Interval) if the compiler
      93                 :             :  * insists on adding alignment padding at the end of the struct.  (Note:
      94                 :             :  * this concern is obsolete with the current definition of Interval, but
      95                 :             :  * was real before a separate "day" field was added to it.)
      96                 :             :  */
      97                 :             : #define INTERVALSIZE 16
      98                 :             : 
      99                 :             : static const gbtree_ninfo tinfo =
     100                 :             : {
     101                 :             :     gbt_t_intv,
     102                 :             :     sizeof(Interval),
     103                 :             :     32,                         /* sizeof(gbtreekey32) */
     104                 :             :     gbt_intvgt,
     105                 :             :     gbt_intvge,
     106                 :             :     gbt_intveq,
     107                 :             :     gbt_intvle,
     108                 :             :     gbt_intvlt,
     109                 :             :     gbt_intvkey_cmp,
     110                 :             :     gbt_intv_dist
     111                 :             : };
     112                 :             : 
     113                 :             : 
     114                 :             : Interval *
     115                 :        1629 : abs_interval(Interval *a)
     116                 :             : {
     117                 :             :     static const Interval zero = {0, 0, 0};
     118                 :             : 
     119         [ +  + ]:        1629 :     if (DatumGetBool(DirectFunctionCall2(interval_lt,
     120                 :             :                                          IntervalPGetDatum(a),
     121                 :             :                                          IntervalPGetDatum(&zero))))
     122                 :         908 :         a = DatumGetIntervalP(DirectFunctionCall1(interval_um,
     123                 :             :                                                   IntervalPGetDatum(a)));
     124                 :             : 
     125                 :        1629 :     return a;
     126                 :             : }
     127                 :             : 
     128                 :           4 : PG_FUNCTION_INFO_V1(interval_dist);
     129                 :             : Datum
     130                 :         612 : interval_dist(PG_FUNCTION_ARGS)
     131                 :             : {
     132                 :         612 :     Interval   *a = PG_GETARG_INTERVAL_P(0);
     133                 :         612 :     Interval   *b = PG_GETARG_INTERVAL_P(1);
     134                 :         612 :     Interval   *result = palloc_object(Interval);
     135                 :             : 
     136                 :             :     /*
     137                 :             :      * Treat same-sign infinities as equal, as ts_dist does, while any other
     138                 :             :      * case involving an infinite input yields interval +Infinity.
     139                 :             :      */
     140   [ +  +  +  -  :         612 :     if (INTERVAL_NOT_FINITE(a) || INTERVAL_NOT_FINITE(b))
          -  +  +  +  +  
          -  -  +  -  +  
          -  -  -  -  +  
             +  +  -  +  
                      - ]
     141                 :             :     {
     142   [ +  +  +  -  :           4 :         if ((INTERVAL_IS_NOBEGIN(a) && INTERVAL_IS_NOBEGIN(b)) ||
          +  -  +  -  +  
                -  -  + ]
     143   [ +  +  +  -  :           3 :             (INTERVAL_IS_NOEND(a) && INTERVAL_IS_NOEND(b)))
          +  -  +  +  +  
                -  +  - ]
     144                 :             :         {
     145                 :           2 :             result->month = 0;
     146                 :           2 :             result->day = 0;
     147                 :           2 :             result->time = 0;
     148                 :             :         }
     149                 :             :         else
     150                 :           2 :             INTERVAL_NOEND(result);
     151                 :           4 :         PG_RETURN_INTERVAL_P(result);
     152                 :             :     }
     153                 :             : 
     154                 :             :     /*
     155                 :             :      * Instead of applying abs_interval(), we arrange to subtract the smaller
     156                 :             :      * input from the larger one.  Otherwise we'd need a non-overflowing
     157                 :             :      * version of abs_interval().
     158                 :             :      */
     159         [ +  + ]:         608 :     if (DatumGetBool(DirectFunctionCall2(interval_lt,
     160                 :             :                                          IntervalPGetDatum(a),
     161                 :             :                                          IntervalPGetDatum(b))))
     162                 :             :     {
     163                 :         332 :         Interval   *tmp = a;
     164                 :             : 
     165                 :         332 :         a = b;
     166                 :         332 :         b = tmp;
     167                 :             :     }
     168                 :             : 
     169                 :             :     /*
     170                 :             :      * Do what interval_mi() would do, except report interval +Infinity rather
     171                 :             :      * than throwing an out-of-range error.
     172                 :             :      */
     173   [ +  +  +  - ]:        1214 :     if (pg_sub_s32_overflow(a->month, b->month, &result->month) ||
     174         [ +  - ]:        1212 :         pg_sub_s32_overflow(a->day, b->day, &result->day) ||
     175                 :         606 :         pg_sub_s64_overflow(a->time, b->time, &result->time) ||
     176   [ -  +  -  -  :         606 :         INTERVAL_NOT_FINITE(result))
          -  -  -  +  -  
                -  -  - ]
     177                 :           2 :         INTERVAL_NOEND(result);
     178                 :             : 
     179                 :         608 :     PG_RETURN_INTERVAL_P(result);
     180                 :             : }
     181                 :             : 
     182                 :             : 
     183                 :             : /**************************************************
     184                 :             :  * GiST support functions
     185                 :             :  **************************************************/
     186                 :             : 
     187                 :             : Datum
     188                 :         604 : gbt_intv_compress(PG_FUNCTION_ARGS)
     189                 :             : {
     190                 :         604 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     191                 :         604 :     GISTENTRY  *retval = entry;
     192                 :             : 
     193         [ +  + ]:         604 :     if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
     194                 :             :     {
     195                 :         600 :         char       *r = (char *) palloc(2 * INTERVALSIZE);
     196                 :             : 
     197                 :         600 :         retval = palloc_object(GISTENTRY);
     198                 :             : 
     199         [ +  - ]:         600 :         if (entry->leafkey)
     200                 :             :         {
     201                 :         600 :             Interval   *key = DatumGetIntervalP(entry->key);
     202                 :             : 
     203                 :         600 :             memcpy(r, key, INTERVALSIZE);
     204                 :         600 :             memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
     205                 :             :         }
     206                 :             :         else
     207                 :             :         {
     208                 :           0 :             intvKEY    *key = (intvKEY *) DatumGetPointer(entry->key);
     209                 :             : 
     210                 :           0 :             memcpy(r, &key->lower, INTERVALSIZE);
     211                 :           0 :             memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
     212                 :             :         }
     213                 :         600 :         gistentryinit(*retval, PointerGetDatum(r),
     214                 :             :                       entry->rel, entry->page,
     215                 :             :                       entry->offset, false);
     216                 :             :     }
     217                 :             : 
     218                 :         604 :     PG_RETURN_POINTER(retval);
     219                 :             : }
     220                 :             : 
     221                 :             : Datum
     222                 :        1351 : gbt_intv_fetch(PG_FUNCTION_ARGS)
     223                 :             : {
     224                 :        1351 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     225                 :             : 
     226                 :        1351 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     227                 :             : }
     228                 :             : 
     229                 :             : Datum
     230                 :        4378 : gbt_intv_decompress(PG_FUNCTION_ARGS)
     231                 :             : {
     232                 :        4378 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     233                 :        4378 :     GISTENTRY  *retval = entry;
     234                 :             : 
     235                 :             :     if (INTERVALSIZE != sizeof(Interval))
     236                 :             :     {
     237                 :             :         intvKEY    *r = palloc_object(intvKEY);
     238                 :             :         char       *key = DatumGetPointer(entry->key);
     239                 :             : 
     240                 :             :         retval = palloc_object(GISTENTRY);
     241                 :             :         memcpy(&r->lower, key, INTERVALSIZE);
     242                 :             :         memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
     243                 :             : 
     244                 :             :         gistentryinit(*retval, PointerGetDatum(r),
     245                 :             :                       entry->rel, entry->page,
     246                 :             :                       entry->offset, false);
     247                 :             :     }
     248                 :        4378 :     PG_RETURN_POINTER(retval);
     249                 :             : }
     250                 :             : 
     251                 :             : 
     252                 :             : Datum
     253                 :        1670 : gbt_intv_consistent(PG_FUNCTION_ARGS)
     254                 :             : {
     255                 :        1670 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     256                 :        1670 :     Interval   *query = PG_GETARG_INTERVAL_P(1);
     257                 :        1670 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     258                 :             : #ifdef NOT_USED
     259                 :             :     Oid         subtype = PG_GETARG_OID(3);
     260                 :             : #endif
     261                 :        1670 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     262                 :        1670 :     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
     263                 :             :     GBT_NUMKEY_R key;
     264                 :             : 
     265                 :             :     /* All cases served by this function are exact */
     266                 :        1670 :     *recheck = false;
     267                 :             : 
     268                 :        1670 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     269                 :        1670 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     270                 :             : 
     271                 :        1670 :     PG_RETURN_BOOL(gbt_num_consistent(&key, query, strategy,
     272                 :             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     273                 :             : }
     274                 :             : 
     275                 :             : 
     276                 :             : Datum
     277                 :         308 : gbt_intv_distance(PG_FUNCTION_ARGS)
     278                 :             : {
     279                 :         308 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     280                 :         308 :     Interval   *query = PG_GETARG_INTERVAL_P(1);
     281                 :             : #ifdef NOT_USED
     282                 :             :     Oid         subtype = PG_GETARG_OID(3);
     283                 :             : #endif
     284                 :         308 :     intvKEY    *kkk = (intvKEY *) DatumGetPointer(entry->key);
     285                 :             :     GBT_NUMKEY_R key;
     286                 :             : 
     287                 :         308 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     288                 :         308 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     289                 :             : 
     290                 :         308 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry),
     291                 :             :                                       &tinfo, fcinfo->flinfo));
     292                 :             : }
     293                 :             : 
     294                 :             : 
     295                 :             : Datum
     296                 :           1 : gbt_intv_union(PG_FUNCTION_ARGS)
     297                 :             : {
     298                 :           1 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     299                 :           1 :     void       *out = palloc_object(intvKEY);
     300                 :             : 
     301                 :           1 :     *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
     302                 :           1 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     303                 :             : }
     304                 :             : 
     305                 :             : 
     306                 :             : Datum
     307                 :           0 : gbt_intv_penalty(PG_FUNCTION_ARGS)
     308                 :             : {
     309                 :           0 :     intvKEY    *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     310                 :           0 :     intvKEY    *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     311                 :           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     312                 :             :     double      iorg[2],
     313                 :             :                 inew[2];
     314                 :             : 
     315                 :           0 :     iorg[0] = intr2num(&origentry->lower);
     316                 :           0 :     iorg[1] = intr2num(&origentry->upper);
     317                 :           0 :     inew[0] = intr2num(&newentry->lower);
     318                 :           0 :     inew[1] = intr2num(&newentry->upper);
     319                 :             : 
     320                 :           0 :     penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
     321                 :             : 
     322                 :           0 :     PG_RETURN_POINTER(result);
     323                 :             : }
     324                 :             : 
     325                 :             : Datum
     326                 :           3 : gbt_intv_picksplit(PG_FUNCTION_ARGS)
     327                 :             : {
     328                 :           3 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     329                 :             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     330                 :             :                                         &tinfo, fcinfo->flinfo));
     331                 :             : }
     332                 :             : 
     333                 :             : Datum
     334                 :           0 : gbt_intv_same(PG_FUNCTION_ARGS)
     335                 :             : {
     336                 :           0 :     intvKEY    *b1 = (intvKEY *) PG_GETARG_POINTER(0);
     337                 :           0 :     intvKEY    *b2 = (intvKEY *) PG_GETARG_POINTER(1);
     338                 :           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     339                 :             : 
     340                 :           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     341                 :           0 :     PG_RETURN_POINTER(result);
     342                 :             : }
     343                 :             : 
     344                 :             : static int
     345                 :        5676 : gbt_intv_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     346                 :             : {
     347                 :        5676 :     intvKEY    *arg1 = (intvKEY *) DatumGetPointer(x);
     348                 :        5676 :     intvKEY    *arg2 = (intvKEY *) DatumGetPointer(y);
     349                 :             : 
     350                 :             :     /* for leaf items we expect lower == upper, so only compare lower */
     351                 :        5676 :     return DatumGetInt32(DirectFunctionCall2(interval_cmp,
     352                 :             :                                              IntervalPGetDatum(&arg1->lower),
     353                 :             :                                              IntervalPGetDatum(&arg2->lower)));
     354                 :             : }
     355                 :             : 
     356                 :             : Datum
     357                 :           1 : gbt_intv_sortsupport(PG_FUNCTION_ARGS)
     358                 :             : {
     359                 :           1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     360                 :             : 
     361                 :           1 :     ssup->comparator = gbt_intv_ssup_cmp;
     362                 :           1 :     ssup->ssup_extra = NULL;
     363                 :             : 
     364                 :           1 :     PG_RETURN_VOID();
     365                 :             : }
        

Generated by: LCOV version 2.0-1