LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_ts.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 151 151 100.0 %
Date: 2024-11-21 08:14:44 Functions: 34 34 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/btree_gist/btree_ts.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include <limits.h>
       7             : 
       8             : #include "btree_gist.h"
       9             : #include "btree_utils_num.h"
      10             : #include "utils/fmgrprotos.h"
      11             : #include "utils/timestamp.h"
      12             : #include "utils/float.h"
      13             : 
      14             : typedef struct
      15             : {
      16             :     Timestamp   lower;
      17             :     Timestamp   upper;
      18             : } tsKEY;
      19             : 
      20             : /*
      21             : ** timestamp ops
      22             : */
      23           6 : PG_FUNCTION_INFO_V1(gbt_ts_compress);
      24           4 : PG_FUNCTION_INFO_V1(gbt_tstz_compress);
      25           8 : PG_FUNCTION_INFO_V1(gbt_ts_fetch);
      26           8 : PG_FUNCTION_INFO_V1(gbt_ts_union);
      27           8 : PG_FUNCTION_INFO_V1(gbt_ts_picksplit);
      28           6 : PG_FUNCTION_INFO_V1(gbt_ts_consistent);
      29           6 : PG_FUNCTION_INFO_V1(gbt_ts_distance);
      30           4 : PG_FUNCTION_INFO_V1(gbt_tstz_consistent);
      31           4 : PG_FUNCTION_INFO_V1(gbt_tstz_distance);
      32           8 : PG_FUNCTION_INFO_V1(gbt_ts_penalty);
      33           8 : PG_FUNCTION_INFO_V1(gbt_ts_same);
      34             : 
      35             : 
      36             : #ifdef USE_FLOAT8_BYVAL
      37             : #define TimestampGetDatumFast(X) TimestampGetDatum(X)
      38             : #else
      39             : #define TimestampGetDatumFast(X) PointerGetDatum(&(X))
      40             : #endif
      41             : 
      42             : 
      43             : static bool
      44       25020 : gbt_tsgt(const void *a, const void *b, FmgrInfo *flinfo)
      45             : {
      46       25020 :     const Timestamp *aa = (const Timestamp *) a;
      47       25020 :     const Timestamp *bb = (const Timestamp *) b;
      48             : 
      49       25020 :     return DatumGetBool(DirectFunctionCall2(timestamp_gt,
      50             :                                             TimestampGetDatumFast(*aa),
      51             :                                             TimestampGetDatumFast(*bb)));
      52             : }
      53             : 
      54             : static bool
      55        3716 : gbt_tsge(const void *a, const void *b, FmgrInfo *flinfo)
      56             : {
      57        3716 :     const Timestamp *aa = (const Timestamp *) a;
      58        3716 :     const Timestamp *bb = (const Timestamp *) b;
      59             : 
      60        3716 :     return DatumGetBool(DirectFunctionCall2(timestamp_ge,
      61             :                                             TimestampGetDatumFast(*aa),
      62             :                                             TimestampGetDatumFast(*bb)));
      63             : }
      64             : 
      65             : static bool
      66       12124 : gbt_tseq(const void *a, const void *b, FmgrInfo *flinfo)
      67             : {
      68       12124 :     const Timestamp *aa = (const Timestamp *) a;
      69       12124 :     const Timestamp *bb = (const Timestamp *) b;
      70             : 
      71       12124 :     return DatumGetBool(DirectFunctionCall2(timestamp_eq,
      72             :                                             TimestampGetDatumFast(*aa),
      73             :                                             TimestampGetDatumFast(*bb)));
      74             : }
      75             : 
      76             : static bool
      77        3924 : gbt_tsle(const void *a, const void *b, FmgrInfo *flinfo)
      78             : {
      79        3924 :     const Timestamp *aa = (const Timestamp *) a;
      80        3924 :     const Timestamp *bb = (const Timestamp *) b;
      81             : 
      82        3924 :     return DatumGetBool(DirectFunctionCall2(timestamp_le,
      83             :                                             TimestampGetDatumFast(*aa),
      84             :                                             TimestampGetDatumFast(*bb)));
      85             : }
      86             : 
      87             : static bool
      88       24692 : gbt_tslt(const void *a, const void *b, FmgrInfo *flinfo)
      89             : {
      90       24692 :     const Timestamp *aa = (const Timestamp *) a;
      91       24692 :     const Timestamp *bb = (const Timestamp *) b;
      92             : 
      93       24692 :     return DatumGetBool(DirectFunctionCall2(timestamp_lt,
      94             :                                             TimestampGetDatumFast(*aa),
      95             :                                             TimestampGetDatumFast(*bb)));
      96             : }
      97             : 
      98             : 
      99             : static int
     100       25646 : gbt_tskey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
     101             : {
     102       25646 :     tsKEY      *ia = (tsKEY *) (((const Nsrt *) a)->t);
     103       25646 :     tsKEY      *ib = (tsKEY *) (((const Nsrt *) b)->t);
     104             :     int         res;
     105             : 
     106       25646 :     res = DatumGetInt32(DirectFunctionCall2(timestamp_cmp, TimestampGetDatumFast(ia->lower), TimestampGetDatumFast(ib->lower)));
     107       25646 :     if (res == 0)
     108        7502 :         return DatumGetInt32(DirectFunctionCall2(timestamp_cmp, TimestampGetDatumFast(ia->upper), TimestampGetDatumFast(ib->upper)));
     109             : 
     110       18144 :     return res;
     111             : }
     112             : 
     113             : static float8
     114        1070 : gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo)
     115             : {
     116        1070 :     const Timestamp *aa = (const Timestamp *) a;
     117        1070 :     const Timestamp *bb = (const Timestamp *) b;
     118             :     Interval   *i;
     119             : 
     120        1070 :     if (TIMESTAMP_NOT_FINITE(*aa) || TIMESTAMP_NOT_FINITE(*bb))
     121          84 :         return get_float8_infinity();
     122             : 
     123         986 :     i = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
     124             :                                               TimestampGetDatumFast(*aa),
     125             :                                               TimestampGetDatumFast(*bb)));
     126         986 :     return fabs(INTERVAL_TO_SEC(i));
     127             : }
     128             : 
     129             : 
     130             : static const gbtree_ninfo tinfo =
     131             : {
     132             :     gbt_t_ts,
     133             :     sizeof(Timestamp),
     134             :     16,                         /* sizeof(gbtreekey16) */
     135             :     gbt_tsgt,
     136             :     gbt_tsge,
     137             :     gbt_tseq,
     138             :     gbt_tsle,
     139             :     gbt_tslt,
     140             :     gbt_tskey_cmp,
     141             :     gbt_ts_dist
     142             : };
     143             : 
     144             : 
     145           4 : PG_FUNCTION_INFO_V1(ts_dist);
     146             : Datum
     147        1142 : ts_dist(PG_FUNCTION_ARGS)
     148             : {
     149        1142 :     Timestamp   a = PG_GETARG_TIMESTAMP(0);
     150        1142 :     Timestamp   b = PG_GETARG_TIMESTAMP(1);
     151             :     Interval   *r;
     152             : 
     153        1142 :     if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
     154             :     {
     155          48 :         Interval   *p = palloc(sizeof(Interval));
     156             : 
     157          48 :         p->day = INT_MAX;
     158          48 :         p->month = INT_MAX;
     159          48 :         p->time = PG_INT64_MAX;
     160          48 :         PG_RETURN_INTERVAL_P(p);
     161             :     }
     162             :     else
     163        1094 :         r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
     164             :                                                   PG_GETARG_DATUM(0),
     165             :                                                   PG_GETARG_DATUM(1)));
     166        1094 :     PG_RETURN_INTERVAL_P(abs_interval(r));
     167             : }
     168             : 
     169           4 : PG_FUNCTION_INFO_V1(tstz_dist);
     170             : Datum
     171        1104 : tstz_dist(PG_FUNCTION_ARGS)
     172             : {
     173        1104 :     TimestampTz a = PG_GETARG_TIMESTAMPTZ(0);
     174        1104 :     TimestampTz b = PG_GETARG_TIMESTAMPTZ(1);
     175             :     Interval   *r;
     176             : 
     177        1104 :     if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
     178             :     {
     179          36 :         Interval   *p = palloc(sizeof(Interval));
     180             : 
     181          36 :         p->day = INT_MAX;
     182          36 :         p->month = INT_MAX;
     183          36 :         p->time = PG_INT64_MAX;
     184          36 :         PG_RETURN_INTERVAL_P(p);
     185             :     }
     186             : 
     187        1068 :     r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
     188             :                                               PG_GETARG_DATUM(0),
     189             :                                               PG_GETARG_DATUM(1)));
     190        1068 :     PG_RETURN_INTERVAL_P(abs_interval(r));
     191             : }
     192             : 
     193             : 
     194             : /**************************************************
     195             :  * timestamp ops
     196             :  **************************************************/
     197             : 
     198             : 
     199             : static inline Timestamp
     200       11454 : tstz_to_ts_gmt(TimestampTz ts)
     201             : {
     202             :     /* No timezone correction is needed, since GMT is offset 0 by definition */
     203       11454 :     return (Timestamp) ts;
     204             : }
     205             : 
     206             : 
     207             : Datum
     208        5238 : gbt_ts_compress(PG_FUNCTION_ARGS)
     209             : {
     210        5238 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     211             : 
     212        5238 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     213             : }
     214             : 
     215             : 
     216             : Datum
     217        1112 : gbt_tstz_compress(PG_FUNCTION_ARGS)
     218             : {
     219        1112 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     220             :     GISTENTRY  *retval;
     221             : 
     222        1112 :     if (entry->leafkey)
     223             :     {
     224        1098 :         tsKEY      *r = (tsKEY *) palloc(sizeof(tsKEY));
     225        1098 :         TimestampTz ts = DatumGetTimestampTz(entry->key);
     226             :         Timestamp   gmt;
     227             : 
     228        1098 :         gmt = tstz_to_ts_gmt(ts);
     229             : 
     230        1098 :         retval = palloc(sizeof(GISTENTRY));
     231        1098 :         r->lower = r->upper = gmt;
     232        1098 :         gistentryinit(*retval, PointerGetDatum(r),
     233             :                       entry->rel, entry->page,
     234             :                       entry->offset, false);
     235             :     }
     236             :     else
     237          14 :         retval = entry;
     238             : 
     239        1112 :     PG_RETURN_POINTER(retval);
     240             : }
     241             : 
     242             : Datum
     243        1062 : gbt_ts_fetch(PG_FUNCTION_ARGS)
     244             : {
     245        1062 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     246             : 
     247        1062 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     248             : }
     249             : 
     250             : Datum
     251        3956 : gbt_ts_consistent(PG_FUNCTION_ARGS)
     252             : {
     253        3956 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     254        3956 :     Timestamp   query = PG_GETARG_TIMESTAMP(1);
     255        3956 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     256             : 
     257             :     /* Oid      subtype = PG_GETARG_OID(3); */
     258        3956 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     259        3956 :     tsKEY      *kkk = (tsKEY *) DatumGetPointer(entry->key);
     260             :     GBT_NUMKEY_R key;
     261             : 
     262             :     /* All cases served by this function are exact */
     263        3956 :     *recheck = false;
     264             : 
     265        3956 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     266        3956 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     267             : 
     268        3956 :     PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
     269             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     270             : }
     271             : 
     272             : Datum
     273         420 : gbt_ts_distance(PG_FUNCTION_ARGS)
     274             : {
     275         420 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     276         420 :     Timestamp   query = PG_GETARG_TIMESTAMP(1);
     277             : 
     278             :     /* Oid      subtype = PG_GETARG_OID(3); */
     279         420 :     tsKEY      *kkk = (tsKEY *) DatumGetPointer(entry->key);
     280             :     GBT_NUMKEY_R key;
     281             : 
     282         420 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     283         420 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     284             : 
     285         420 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
     286             :                                       &tinfo, fcinfo->flinfo));
     287             : }
     288             : 
     289             : Datum
     290        9702 : gbt_tstz_consistent(PG_FUNCTION_ARGS)
     291             : {
     292        9702 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     293        9702 :     TimestampTz query = PG_GETARG_TIMESTAMPTZ(1);
     294        9702 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     295             : 
     296             :     /* Oid      subtype = PG_GETARG_OID(3); */
     297        9702 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     298        9702 :     char       *kkk = (char *) DatumGetPointer(entry->key);
     299             :     GBT_NUMKEY_R key;
     300             :     Timestamp   qqq;
     301             : 
     302             :     /* All cases served by this function are exact */
     303        9702 :     *recheck = false;
     304             : 
     305        9702 :     key.lower = (GBT_NUMKEY *) &kkk[0];
     306        9702 :     key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
     307        9702 :     qqq = tstz_to_ts_gmt(query);
     308             : 
     309        9702 :     PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &qqq, &strategy,
     310             :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     311             : }
     312             : 
     313             : Datum
     314         654 : gbt_tstz_distance(PG_FUNCTION_ARGS)
     315             : {
     316         654 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     317         654 :     TimestampTz query = PG_GETARG_TIMESTAMPTZ(1);
     318             : 
     319             :     /* Oid      subtype = PG_GETARG_OID(3); */
     320         654 :     char       *kkk = (char *) DatumGetPointer(entry->key);
     321             :     GBT_NUMKEY_R key;
     322             :     Timestamp   qqq;
     323             : 
     324         654 :     key.lower = (GBT_NUMKEY *) &kkk[0];
     325         654 :     key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
     326         654 :     qqq = tstz_to_ts_gmt(query);
     327             : 
     328         654 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &qqq, GIST_LEAF(entry),
     329             :                                       &tinfo, fcinfo->flinfo));
     330             : }
     331             : 
     332             : 
     333             : Datum
     334        4994 : gbt_ts_union(PG_FUNCTION_ARGS)
     335             : {
     336        4994 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     337        4994 :     void       *out = palloc(sizeof(tsKEY));
     338             : 
     339        4994 :     *(int *) PG_GETARG_POINTER(1) = sizeof(tsKEY);
     340        4994 :     PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
     341             : }
     342             : 
     343             : 
     344             : #define penalty_check_max_float(val) \
     345             :     do { \
     346             :         if ( val > FLT_MAX ) \
     347             :                 val = FLT_MAX; \
     348             :         if ( val < -FLT_MAX ) \
     349             :                 val = -FLT_MAX; \
     350             :     } while (0)
     351             : 
     352             : 
     353             : Datum
     354       10414 : gbt_ts_penalty(PG_FUNCTION_ARGS)
     355             : {
     356       10414 :     tsKEY      *origentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     357       10414 :     tsKEY      *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     358       10414 :     float      *result = (float *) PG_GETARG_POINTER(2);
     359             : 
     360             :     double      orgdbl[2],
     361             :                 newdbl[2];
     362             : 
     363             :     /*
     364             :      * We are always using "double" timestamps here. Precision should be good
     365             :      * enough.
     366             :      */
     367       10414 :     orgdbl[0] = ((double) origentry->lower);
     368       10414 :     orgdbl[1] = ((double) origentry->upper);
     369       10414 :     newdbl[0] = ((double) newentry->lower);
     370       10414 :     newdbl[1] = ((double) newentry->upper);
     371             : 
     372       10414 :     penalty_check_max_float(orgdbl[0]);
     373       10414 :     penalty_check_max_float(orgdbl[1]);
     374       10414 :     penalty_check_max_float(newdbl[0]);
     375       10414 :     penalty_check_max_float(newdbl[1]);
     376             : 
     377       10414 :     penalty_num(result, orgdbl[0], orgdbl[1], newdbl[0], newdbl[1]);
     378             : 
     379       10414 :     PG_RETURN_POINTER(result);
     380             : }
     381             : 
     382             : 
     383             : Datum
     384          46 : gbt_ts_picksplit(PG_FUNCTION_ARGS)
     385             : {
     386          46 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     387             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     388             :                                         &tinfo, fcinfo->flinfo));
     389             : }
     390             : 
     391             : Datum
     392        4948 : gbt_ts_same(PG_FUNCTION_ARGS)
     393             : {
     394        4948 :     tsKEY      *b1 = (tsKEY *) PG_GETARG_POINTER(0);
     395        4948 :     tsKEY      *b2 = (tsKEY *) PG_GETARG_POINTER(1);
     396        4948 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     397             : 
     398        4948 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     399        4948 :     PG_RETURN_POINTER(result);
     400             : }

Generated by: LCOV version 1.14