LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_date.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 100.0 % 90 90
Test Date: 2026-12-01 02:16:10 Functions: 100.0 % 28 28
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 100.0 % 22 22

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * contrib/btree_gist/btree_date.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/date.h"
      10                 :             : #include "utils/rel.h"
      11                 :             : #include "utils/sortsupport.h"
      12                 :             : 
      13                 :             : typedef struct
      14                 :             : {
      15                 :             :     DateADT     lower;
      16                 :             :     DateADT     upper;
      17                 :             : } dateKEY;
      18                 :             : 
      19                 :             : /* GiST support functions */
      20                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_compress);
      21                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_fetch);
      22                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_union);
      23                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_picksplit);
      24                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_consistent);
      25                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_distance);
      26                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_penalty);
      27                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_same);
      28                 :           4 : PG_FUNCTION_INFO_V1(gbt_date_sortsupport);
      29                 :             : 
      30                 :             : static bool
      31                 :        3445 : gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
      32                 :             : {
      33                 :        3445 :     return DatumGetBool(DirectFunctionCall2(date_gt,
      34                 :             :                                             DateADTGetDatum(*((const DateADT *) a)),
      35                 :             :                                             DateADTGetDatum(*((const DateADT *) b))));
      36                 :             : }
      37                 :             : 
      38                 :             : static bool
      39                 :        1179 : gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
      40                 :             : {
      41                 :        1179 :     return DatumGetBool(DirectFunctionCall2(date_ge,
      42                 :             :                                             DateADTGetDatum(*((const DateADT *) a)),
      43                 :             :                                             DateADTGetDatum(*((const DateADT *) b))));
      44                 :             : }
      45                 :             : 
      46                 :             : static bool
      47                 :        1467 : gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
      48                 :             : {
      49                 :        1467 :     return DatumGetBool(DirectFunctionCall2(date_eq,
      50                 :             :                                             DateADTGetDatum(*((const DateADT *) a)),
      51                 :             :                                             DateADTGetDatum(*((const DateADT *) b)))
      52                 :             :         );
      53                 :             : }
      54                 :             : 
      55                 :             : static bool
      56                 :        1950 : gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
      57                 :             : {
      58                 :        1950 :     return DatumGetBool(DirectFunctionCall2(date_le,
      59                 :             :                                             DateADTGetDatum(*((const DateADT *) a)),
      60                 :             :                                             DateADTGetDatum(*((const DateADT *) b))));
      61                 :             : }
      62                 :             : 
      63                 :             : static bool
      64                 :        3173 : gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
      65                 :             : {
      66                 :        3173 :     return DatumGetBool(DirectFunctionCall2(date_lt,
      67                 :             :                                             DateADTGetDatum(*((const DateADT *) a)),
      68                 :             :                                             DateADTGetDatum(*((const DateADT *) b))));
      69                 :             : }
      70                 :             : 
      71                 :             : 
      72                 :             : 
      73                 :             : static int
      74                 :        1764 : gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      75                 :             : {
      76                 :        1764 :     dateKEY    *ia = (dateKEY *) (((const Nsrt *) a)->t);
      77                 :        1764 :     dateKEY    *ib = (dateKEY *) (((const Nsrt *) b)->t);
      78                 :             :     int         res;
      79                 :             : 
      80                 :        1764 :     res = DatumGetInt32(DirectFunctionCall2(date_cmp,
      81                 :             :                                             DateADTGetDatum(ia->lower),
      82                 :             :                                             DateADTGetDatum(ib->lower)));
      83         [ +  + ]:        1764 :     if (res == 0)
      84                 :           5 :         return DatumGetInt32(DirectFunctionCall2(date_cmp,
      85                 :             :                                                  DateADTGetDatum(ia->upper),
      86                 :             :                                                  DateADTGetDatum(ib->upper)));
      87                 :             : 
      88                 :        1759 :     return res;
      89                 :             : }
      90                 :             : 
      91                 :             : static float8
      92                 :        1670 : gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
      93                 :             : {
      94                 :        1670 :     DateADT     da = *((const DateADT *) a);
      95                 :        1670 :     DateADT     db = *((const DateADT *) b);
      96                 :             : 
      97                 :             :     /*
      98                 :             :      * Treat same-sign infinities as equal, as the float8 opclass does; but
      99                 :             :      * any other case involving infinite inputs yields infinite distance.
     100                 :             :      */
     101   [ +  +  +  +  :        1670 :     if (DATE_NOT_FINITE(da) || DATE_NOT_FINITE(db))
             +  +  +  + ]
     102         [ +  + ]:         802 :         return (da == db) ? 0.0 : get_float8_infinity();
     103                 :             : 
     104                 :             :     /* the difference of two finite dates cannot overflow int32 */
     105                 :         868 :     return (float8) abs(da - db);
     106                 :             : }
     107                 :             : 
     108                 :             : 
     109                 :             : static const gbtree_ninfo tinfo =
     110                 :             : {
     111                 :             :     gbt_t_date,
     112                 :             :     sizeof(DateADT),
     113                 :             :     8,                          /* sizeof(gbtreekey8) */
     114                 :             :     gbt_dategt,
     115                 :             :     gbt_datege,
     116                 :             :     gbt_dateeq,
     117                 :             :     gbt_datele,
     118                 :             :     gbt_datelt,
     119                 :             :     gbt_datekey_cmp,
     120                 :             :     gdb_date_dist
     121                 :             : };
     122                 :             : 
     123                 :             : 
     124                 :           4 : PG_FUNCTION_INFO_V1(date_dist);
     125                 :             : Datum
     126                 :        3578 : date_dist(PG_FUNCTION_ARGS)
     127                 :             : {
     128                 :        3578 :     DateADT     a = PG_GETARG_DATEADT(0);
     129                 :        3578 :     DateADT     b = PG_GETARG_DATEADT(1);
     130                 :             : 
     131                 :             :     /*
     132                 :             :      * As in gdb_date_dist, same-sign infinities are equal.  For other cases
     133                 :             :      * involving infinite inputs, int4 has no infinity, so use PG_INT32_MAX.
     134                 :             :      * This is consistent with the ordering produced by gdb_date_dist, since
     135                 :             :      * no difference of two finite dates reaches PG_INT32_MAX.
     136                 :             :      */
     137   [ +  +  +  +  :        3578 :     if (DATE_NOT_FINITE(a) || DATE_NOT_FINITE(b))
             +  +  +  + ]
     138         [ +  + ]:        2028 :         PG_RETURN_INT32((a == b) ? 0 : PG_INT32_MAX);
     139                 :             : 
     140                 :             :     /* the difference of two finite dates cannot overflow int32 */
     141                 :        1550 :     PG_RETURN_INT32(abs(a - b));
     142                 :             : }
     143                 :             : 
     144                 :             : 
     145                 :             : /**************************************************
     146                 :             :  * GiST support functions
     147                 :             :  **************************************************/
     148                 :             : 
     149                 :             : Datum
     150                 :        2152 : gbt_date_compress(PG_FUNCTION_ARGS)
     151                 :             : {
     152                 :        2152 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     153                 :             : 
     154                 :        2152 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     155                 :             : }
     156                 :             : 
     157                 :             : Datum
     158                 :        1658 : gbt_date_fetch(PG_FUNCTION_ARGS)
     159                 :             : {
     160                 :        1658 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     161                 :             : 
     162                 :        1658 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     163                 :             : }
     164                 :             : 
     165                 :             : Datum
     166                 :        1914 : gbt_date_consistent(PG_FUNCTION_ARGS)
     167                 :             : {
     168                 :        1914 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     169                 :        1914 :     DateADT     query = PG_GETARG_DATEADT(1);
     170                 :        1914 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     171                 :             : #ifdef NOT_USED
     172                 :             :     Oid         subtype = PG_GETARG_OID(3);
     173                 :             : #endif
     174                 :        1914 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     175                 :        1914 :     dateKEY    *kkk = (dateKEY *) DatumGetPointer(entry->key);
     176                 :             :     GBT_NUMKEY_R key;
     177                 :             : 
     178                 :             :     /* All cases served by this function are exact */
     179                 :        1914 :     *recheck = false;
     180                 :             : 
     181                 :        1914 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     182                 :        1914 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     183                 :             : 
     184                 :        1914 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, strategy,
     185                 :             :                                       GIST_LEAF(entry), &tinfo,
     186                 :             :                                       fcinfo->flinfo));
     187                 :             : }
     188                 :             : 
     189                 :             : Datum
     190                 :        1672 : gbt_date_distance(PG_FUNCTION_ARGS)
     191                 :             : {
     192                 :        1672 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     193                 :        1672 :     DateADT     query = PG_GETARG_DATEADT(1);
     194                 :             : #ifdef NOT_USED
     195                 :             :     Oid         subtype = PG_GETARG_OID(3);
     196                 :             : #endif
     197                 :        1672 :     dateKEY    *kkk = (dateKEY *) DatumGetPointer(entry->key);
     198                 :             :     GBT_NUMKEY_R key;
     199                 :             : 
     200                 :        1672 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     201                 :        1672 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     202                 :             : 
     203                 :        1672 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     204                 :             :                                       &tinfo, fcinfo->flinfo));
     205                 :             : }
     206                 :             : 
     207                 :             : Datum
     208                 :         599 : gbt_date_union(PG_FUNCTION_ARGS)
     209                 :             : {
     210                 :         599 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     211                 :         599 :     void       *out = palloc_object(dateKEY);
     212                 :             : 
     213                 :         599 :     *(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
     214                 :         599 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     215                 :             : }
     216                 :             : 
     217                 :             : Datum
     218                 :        1780 : gbt_date_penalty(PG_FUNCTION_ARGS)
     219                 :             : {
     220                 :        1780 :     dateKEY    *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     221                 :        1780 :     dateKEY    *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     222                 :        1780 :     float      *result = (float *) PG_GETARG_POINTER(2);
     223                 :             : 
     224                 :             :     /*
     225                 :             :      * Don't use date_mi here: it rejects infinite dates.  Plain int32
     226                 :             :      * subtraction won't do either: it could overflow for cases involving
     227                 :             :      * infinite dates.  But we can use penalty_num: it computes in double,
     228                 :             :      * where every int32 and every difference of two int32s is exact.  The
     229                 :             :      * output for cases with unequal infinite dates will be finite but large,
     230                 :             :      * which seems fine for penalty calculations.
     231                 :             :      */
     232                 :        1780 :     penalty_num(result, origentry->lower, origentry->upper,
     233                 :             :                 newentry->lower, newentry->upper);
     234                 :             : 
     235                 :        1780 :     PG_RETURN_POINTER(result);
     236                 :             : }
     237                 :             : 
     238                 :             : Datum
     239                 :           4 : gbt_date_picksplit(PG_FUNCTION_ARGS)
     240                 :             : {
     241                 :           4 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     242                 :             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     243                 :             :                                         &tinfo, fcinfo->flinfo));
     244                 :             : }
     245                 :             : 
     246                 :             : Datum
     247                 :         598 : gbt_date_same(PG_FUNCTION_ARGS)
     248                 :             : {
     249                 :         598 :     dateKEY    *b1 = (dateKEY *) PG_GETARG_POINTER(0);
     250                 :         598 :     dateKEY    *b2 = (dateKEY *) PG_GETARG_POINTER(1);
     251                 :         598 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     252                 :             : 
     253                 :         598 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     254                 :         598 :     PG_RETURN_POINTER(result);
     255                 :             : }
     256                 :             : 
     257                 :             : static int
     258                 :        5364 : gbt_date_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     259                 :             : {
     260                 :        5364 :     dateKEY    *akey = (dateKEY *) DatumGetPointer(x);
     261                 :        5364 :     dateKEY    *bkey = (dateKEY *) DatumGetPointer(y);
     262                 :             : 
     263                 :             :     /* for leaf items we expect lower == upper, so only compare lower */
     264                 :        5364 :     return DatumGetInt32(DirectFunctionCall2(date_cmp,
     265                 :             :                                              DateADTGetDatum(akey->lower),
     266                 :             :                                              DateADTGetDatum(bkey->lower)));
     267                 :             : }
     268                 :             : 
     269                 :             : Datum
     270                 :           2 : gbt_date_sortsupport(PG_FUNCTION_ARGS)
     271                 :             : {
     272                 :           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     273                 :             : 
     274                 :           2 :     ssup->comparator = gbt_date_ssup_cmp;
     275                 :           2 :     ssup->ssup_extra = NULL;
     276                 :             : 
     277                 :           2 :     PG_RETURN_VOID();
     278                 :             : }
        

Generated by: LCOV version 2.0-1