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

Generated by: LCOV version 1.13