LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - rangetypes.c (source / functions) Coverage Total Hit UNC LBC UBC GIC GNC CBC EUB ECB DUB DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 86.2 % 1072 924 2 146 9 915 2 9
Current Date: 2025-09-06 07:49:51 +0900 Functions: 90.7 % 86 78 1 7 7 71
Baseline: lcov-20250906-005545-baseline Branches: 71.9 % 800 575 2 2 221 4 6 565 18 10
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 8 8 8
(30,360] days: 93.8 % 32 30 2 1 29
(360..) days: 85.9 % 1032 886 146 886
Function coverage date bins:
(30,360] days: 100.0 % 2 2 1 1
(360..) days: 90.5 % 84 76 1 7 6 70
Branch coverage date bins:
(7,30] days: 75.0 % 8 6 2 6
(30,360] days: 92.9 % 14 13 1 13
(360..) days: 69.0 % 806 556 2 220 4 552 18 10

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * rangetypes.c
                                  4                 :                :  *    I/O functions, operators, and support functions for range types.
                                  5                 :                :  *
                                  6                 :                :  * The stored (serialized) format of a range value is:
                                  7                 :                :  *
                                  8                 :                :  *  4 bytes: varlena header
                                  9                 :                :  *  4 bytes: range type's OID
                                 10                 :                :  *  Lower boundary value, if any, aligned according to subtype's typalign
                                 11                 :                :  *  Upper boundary value, if any, aligned according to subtype's typalign
                                 12                 :                :  *  1 byte for flags
                                 13                 :                :  *
                                 14                 :                :  * This representation is chosen to avoid needing any padding before the
                                 15                 :                :  * lower boundary value, even when it requires double alignment.  We can
                                 16                 :                :  * expect that the varlena header is presented to us on a suitably aligned
                                 17                 :                :  * boundary (possibly after detoasting), and then the lower boundary is too.
                                 18                 :                :  * Note that this means we can't work with a packed (short varlena header)
                                 19                 :                :  * value; we must detoast it first.
                                 20                 :                :  *
                                 21                 :                :  *
                                 22                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 23                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 24                 :                :  *
                                 25                 :                :  *
                                 26                 :                :  * IDENTIFICATION
                                 27                 :                :  *    src/backend/utils/adt/rangetypes.c
                                 28                 :                :  *
                                 29                 :                :  *-------------------------------------------------------------------------
                                 30                 :                :  */
                                 31                 :                : #include "postgres.h"
                                 32                 :                : 
                                 33                 :                : #include "common/hashfn.h"
                                 34                 :                : #include "libpq/pqformat.h"
                                 35                 :                : #include "miscadmin.h"
                                 36                 :                : #include "nodes/makefuncs.h"
                                 37                 :                : #include "nodes/miscnodes.h"
                                 38                 :                : #include "nodes/supportnodes.h"
                                 39                 :                : #include "optimizer/clauses.h"
                                 40                 :                : #include "optimizer/cost.h"
                                 41                 :                : #include "optimizer/optimizer.h"
                                 42                 :                : #include "utils/builtins.h"
                                 43                 :                : #include "utils/date.h"
                                 44                 :                : #include "utils/lsyscache.h"
                                 45                 :                : #include "utils/rangetypes.h"
                                 46                 :                : #include "utils/sortsupport.h"
                                 47                 :                : #include "utils/timestamp.h"
                                 48                 :                : 
                                 49                 :                : 
                                 50                 :                : /* fn_extra cache entry for one of the range I/O functions */
                                 51                 :                : typedef struct RangeIOData
                                 52                 :                : {
                                 53                 :                :     TypeCacheEntry *typcache;   /* range type's typcache entry */
                                 54                 :                :     FmgrInfo    typioproc;      /* element type's I/O function */
                                 55                 :                :     Oid         typioparam;     /* element type's I/O parameter */
                                 56                 :                : } RangeIOData;
                                 57                 :                : 
                                 58                 :                : 
                                 59                 :                : static RangeIOData *get_range_io_data(FunctionCallInfo fcinfo, Oid rngtypid,
                                 60                 :                :                                       IOFuncSelector func);
                                 61                 :                : static int  range_fast_cmp(Datum a, Datum b, SortSupport ssup);
                                 62                 :                : static char range_parse_flags(const char *flags_str);
                                 63                 :                : static bool range_parse(const char *string, char *flags, char **lbound_str,
                                 64                 :                :                         char **ubound_str, Node *escontext);
                                 65                 :                : static const char *range_parse_bound(const char *string, const char *ptr,
                                 66                 :                :                                      char **bound_str, bool *infinite,
                                 67                 :                :                                      Node *escontext);
                                 68                 :                : static char *range_deparse(char flags, const char *lbound_str,
                                 69                 :                :                            const char *ubound_str);
                                 70                 :                : static char *range_bound_escape(const char *value);
                                 71                 :                : static Size datum_compute_size(Size data_length, Datum val, bool typbyval,
                                 72                 :                :                                char typalign, int16 typlen, char typstorage);
                                 73                 :                : static Pointer datum_write(Pointer ptr, Datum datum, bool typbyval,
                                 74                 :                :                            char typalign, int16 typlen, char typstorage);
                                 75                 :                : static Node *find_simplified_clause(PlannerInfo *root,
                                 76                 :                :                                     Expr *rangeExpr, Expr *elemExpr);
                                 77                 :                : static Expr *build_bound_expr(Expr *elemExpr, Datum val,
                                 78                 :                :                               bool isLowerBound, bool isInclusive,
                                 79                 :                :                               TypeCacheEntry *typeCache,
                                 80                 :                :                               Oid opfamily, Oid rng_collation);
                                 81                 :                : 
                                 82                 :                : 
                                 83                 :                : /*
                                 84                 :                :  *----------------------------------------------------------
                                 85                 :                :  * I/O FUNCTIONS
                                 86                 :                :  *----------------------------------------------------------
                                 87                 :                :  */
                                 88                 :                : 
                                 89                 :                : Datum
 5056 heikki.linnakangas@i       90                 :CBC        3538 : range_in(PG_FUNCTION_ARGS)
                                 91                 :                : {
 5045 bruce@momjian.us           92                 :           3538 :     char       *input_str = PG_GETARG_CSTRING(0);
                                 93                 :           3538 :     Oid         rngtypoid = PG_GETARG_OID(1);
                                 94                 :           3538 :     Oid         typmod = PG_GETARG_INT32(2);
  996 tgl@sss.pgh.pa.us          95                 :           3538 :     Node       *escontext = fcinfo->context;
                                 96                 :                :     RangeType  *range;
                                 97                 :                :     RangeIOData *cache;
                                 98                 :                :     char        flags;
                                 99                 :                :     char       *lbound_str;
                                100                 :                :     char       *ubound_str;
                                101                 :                :     RangeBound  lower;
                                102                 :                :     RangeBound  upper;
                                103                 :                : 
 3624 noah@leadboat.com         104                 :           3538 :     check_stack_depth();        /* recurses when subtype is a range type */
                                105                 :                : 
 5044 tgl@sss.pgh.pa.us         106                 :           3538 :     cache = get_range_io_data(fcinfo, rngtypoid, IOFunc_input);
                                107                 :                : 
                                108                 :                :     /* parse */
  996                           109         [ +  + ]:           3538 :     if (!range_parse(input_str, &flags, &lbound_str, &ubound_str, escontext))
                                110                 :              9 :         PG_RETURN_NULL();
                                111                 :                : 
                                112                 :                :     /* call element type's input function */
 5056 heikki.linnakangas@i      113         [ +  + ]:           3490 :     if (RANGE_HAS_LBOUND(flags))
  996 tgl@sss.pgh.pa.us         114         [ -  + ]:           3124 :         if (!InputFunctionCallSafe(&cache->typioproc, lbound_str,
                                115                 :                :                                    cache->typioparam, typmod,
                                116                 :                :                                    escontext, &lower.val))
  996 tgl@sss.pgh.pa.us         117                 :UBC           0 :             PG_RETURN_NULL();
 5056 heikki.linnakangas@i      118         [ +  + ]:CBC        3490 :     if (RANGE_HAS_UBOUND(flags))
  996 tgl@sss.pgh.pa.us         119         [ +  + ]:           3076 :         if (!InputFunctionCallSafe(&cache->typioproc, ubound_str,
                                120                 :                :                                    cache->typioparam, typmod,
                                121                 :                :                                    escontext, &upper.val))
                                122                 :             12 :             PG_RETURN_NULL();
                                123                 :                : 
 5044                           124                 :           3478 :     lower.infinite = (flags & RANGE_LB_INF) != 0;
                                125                 :           3478 :     lower.inclusive = (flags & RANGE_LB_INC) != 0;
                                126                 :           3478 :     lower.lower = true;
                                127                 :           3478 :     upper.infinite = (flags & RANGE_UB_INF) != 0;
                                128                 :           3478 :     upper.inclusive = (flags & RANGE_UB_INC) != 0;
                                129                 :           3478 :     upper.lower = false;
                                130                 :                : 
                                131                 :                :     /* serialize and canonicalize */
  996                           132                 :           3478 :     range = make_range(cache->typcache, &lower, &upper,
                                133                 :           3478 :                        flags & RANGE_EMPTY, escontext);
                                134                 :                : 
 2910                           135                 :           3469 :     PG_RETURN_RANGE_P(range);
                                136                 :                : }
                                137                 :                : 
                                138                 :                : Datum
 5056 heikki.linnakangas@i      139                 :          54112 : range_out(PG_FUNCTION_ARGS)
                                140                 :                : {
 2910 tgl@sss.pgh.pa.us         141                 :          54112 :     RangeType  *range = PG_GETARG_RANGE_P(0);
                                142                 :                :     char       *output_str;
                                143                 :                :     RangeIOData *cache;
                                144                 :                :     char        flags;
 5056 heikki.linnakangas@i      145                 :          54112 :     char       *lbound_str = NULL;
                                146                 :          54112 :     char       *ubound_str = NULL;
                                147                 :                :     RangeBound  lower;
                                148                 :                :     RangeBound  upper;
                                149                 :                :     bool        empty;
                                150                 :                : 
 3624 noah@leadboat.com         151                 :          54112 :     check_stack_depth();        /* recurses when subtype is a range type */
                                152                 :                : 
 5044 tgl@sss.pgh.pa.us         153                 :          54112 :     cache = get_range_io_data(fcinfo, RangeTypeGetOid(range), IOFunc_output);
                                154                 :                : 
                                155                 :                :     /* deserialize */
                                156                 :          54112 :     range_deserialize(cache->typcache, range, &lower, &upper, &empty);
                                157                 :          54112 :     flags = range_get_flags(range);
                                158                 :                : 
                                159                 :                :     /* call element type's output function */
 5056 heikki.linnakangas@i      160         [ +  + ]:          54112 :     if (RANGE_HAS_LBOUND(flags))
 2011 alvherre@alvh.no-ip.      161                 :          44402 :         lbound_str = OutputFunctionCall(&cache->typioproc, lower.val);
 5056 heikki.linnakangas@i      162         [ +  + ]:          54112 :     if (RANGE_HAS_UBOUND(flags))
 2011 alvherre@alvh.no-ip.      163                 :          44333 :         ubound_str = OutputFunctionCall(&cache->typioproc, upper.val);
                                164                 :                : 
                                165                 :                :     /* construct result string */
 5056 heikki.linnakangas@i      166                 :          54112 :     output_str = range_deparse(flags, lbound_str, ubound_str);
                                167                 :                : 
                                168                 :          54112 :     PG_RETURN_CSTRING(output_str);
                                169                 :                : }
                                170                 :                : 
                                171                 :                : /*
                                172                 :                :  * Binary representation: The first byte is the flags, then the lower bound
                                173                 :                :  * (if present), then the upper bound (if present).  Each bound is represented
                                174                 :                :  * by a 4-byte length header and the binary representation of that bound (as
                                175                 :                :  * returned by a call to the send function for the subtype).
                                176                 :                :  */
                                177                 :                : 
                                178                 :                : Datum
 5056 heikki.linnakangas@i      179                 :UBC           0 : range_recv(PG_FUNCTION_ARGS)
                                180                 :                : {
 5045 bruce@momjian.us          181                 :              0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
 5044 tgl@sss.pgh.pa.us         182                 :              0 :     Oid         rngtypoid = PG_GETARG_OID(1);
 5045 bruce@momjian.us          183                 :              0 :     int32       typmod = PG_GETARG_INT32(2);
                                184                 :                :     RangeType  *range;
                                185                 :                :     RangeIOData *cache;
                                186                 :                :     char        flags;
                                187                 :                :     RangeBound  lower;
                                188                 :                :     RangeBound  upper;
                                189                 :                : 
 3624 noah@leadboat.com         190                 :              0 :     check_stack_depth();        /* recurses when subtype is a range type */
                                191                 :                : 
 5044 tgl@sss.pgh.pa.us         192                 :              0 :     cache = get_range_io_data(fcinfo, rngtypoid, IOFunc_receive);
                                193                 :                : 
                                194                 :                :     /* receive the flags... */
                                195                 :              0 :     flags = (unsigned char) pq_getmsgbyte(buf);
                                196                 :                : 
                                197                 :                :     /*
                                198                 :                :      * Mask out any unsupported flags, particularly RANGE_xB_NULL which would
                                199                 :                :      * confuse following tests.  Note that range_serialize will take care of
                                200                 :                :      * cleaning up any inconsistencies in the remaining flags.
                                201                 :                :      */
                                202                 :              0 :     flags &= (RANGE_EMPTY |
                                203                 :                :               RANGE_LB_INC |
                                204                 :                :               RANGE_LB_INF |
                                205                 :                :               RANGE_UB_INC |
                                206                 :                :               RANGE_UB_INF);
                                207                 :                : 
                                208                 :                :     /* receive the bounds ... */
 5056 heikki.linnakangas@i      209         [ #  # ]:              0 :     if (RANGE_HAS_LBOUND(flags))
                                210                 :                :     {
 5045 bruce@momjian.us          211                 :              0 :         uint32      bound_len = pq_getmsgint(buf, 4);
                                212                 :              0 :         const char *bound_data = pq_getmsgbytes(buf, bound_len);
                                213                 :                :         StringInfoData bound_buf;
                                214                 :                : 
 5056 heikki.linnakangas@i      215                 :              0 :         initStringInfo(&bound_buf);
                                216                 :              0 :         appendBinaryStringInfo(&bound_buf, bound_data, bound_len);
                                217                 :                : 
 2011 alvherre@alvh.no-ip.      218                 :              0 :         lower.val = ReceiveFunctionCall(&cache->typioproc,
                                219                 :                :                                         &bound_buf,
                                220                 :                :                                         cache->typioparam,
                                221                 :                :                                         typmod);
 5056 heikki.linnakangas@i      222                 :              0 :         pfree(bound_buf.data);
                                223                 :                :     }
                                224                 :                :     else
                                225                 :              0 :         lower.val = (Datum) 0;
                                226                 :                : 
                                227         [ #  # ]:              0 :     if (RANGE_HAS_UBOUND(flags))
                                228                 :                :     {
 5045 bruce@momjian.us          229                 :              0 :         uint32      bound_len = pq_getmsgint(buf, 4);
                                230                 :              0 :         const char *bound_data = pq_getmsgbytes(buf, bound_len);
                                231                 :                :         StringInfoData bound_buf;
                                232                 :                : 
 5056 heikki.linnakangas@i      233                 :              0 :         initStringInfo(&bound_buf);
                                234                 :              0 :         appendBinaryStringInfo(&bound_buf, bound_data, bound_len);
                                235                 :                : 
 2011 alvherre@alvh.no-ip.      236                 :              0 :         upper.val = ReceiveFunctionCall(&cache->typioproc,
                                237                 :                :                                         &bound_buf,
                                238                 :                :                                         cache->typioparam,
                                239                 :                :                                         typmod);
 5056 heikki.linnakangas@i      240                 :              0 :         pfree(bound_buf.data);
                                241                 :                :     }
                                242                 :                :     else
                                243                 :              0 :         upper.val = (Datum) 0;
                                244                 :                : 
                                245                 :              0 :     pq_getmsgend(buf);
                                246                 :                : 
                                247                 :                :     /* finish constructing RangeBound representation */
 5045 bruce@momjian.us          248                 :              0 :     lower.infinite = (flags & RANGE_LB_INF) != 0;
 5056 heikki.linnakangas@i      249                 :              0 :     lower.inclusive = (flags & RANGE_LB_INC) != 0;
 5045 bruce@momjian.us          250                 :              0 :     lower.lower = true;
                                251                 :              0 :     upper.infinite = (flags & RANGE_UB_INF) != 0;
 5056 heikki.linnakangas@i      252                 :              0 :     upper.inclusive = (flags & RANGE_UB_INC) != 0;
 5045 bruce@momjian.us          253                 :              0 :     upper.lower = false;
                                254                 :                : 
                                255                 :                :     /* serialize and canonicalize */
  996 tgl@sss.pgh.pa.us         256                 :              0 :     range = make_range(cache->typcache, &lower, &upper,
                                257                 :              0 :                        flags & RANGE_EMPTY, NULL);
                                258                 :                : 
 2910                           259                 :              0 :     PG_RETURN_RANGE_P(range);
                                260                 :                : }
                                261                 :                : 
                                262                 :                : Datum
 5056 heikki.linnakangas@i      263                 :              0 : range_send(PG_FUNCTION_ARGS)
                                264                 :                : {
 2910 tgl@sss.pgh.pa.us         265                 :              0 :     RangeType  *range = PG_GETARG_RANGE_P(0);
 5045 bruce@momjian.us          266                 :              0 :     StringInfo  buf = makeStringInfo();
                                267                 :                :     RangeIOData *cache;
                                268                 :                :     char        flags;
                                269                 :                :     RangeBound  lower;
                                270                 :                :     RangeBound  upper;
                                271                 :                :     bool        empty;
                                272                 :                : 
 3624 noah@leadboat.com         273                 :              0 :     check_stack_depth();        /* recurses when subtype is a range type */
                                274                 :                : 
 5044 tgl@sss.pgh.pa.us         275                 :              0 :     cache = get_range_io_data(fcinfo, RangeTypeGetOid(range), IOFunc_send);
                                276                 :                : 
                                277                 :                :     /* deserialize */
                                278                 :              0 :     range_deserialize(cache->typcache, range, &lower, &upper, &empty);
                                279                 :              0 :     flags = range_get_flags(range);
                                280                 :                : 
                                281                 :                :     /* construct output */
 5056 heikki.linnakangas@i      282                 :              0 :     pq_begintypsend(buf);
                                283                 :                : 
                                284                 :              0 :     pq_sendbyte(buf, flags);
                                285                 :                : 
                                286         [ #  # ]:              0 :     if (RANGE_HAS_LBOUND(flags))
                                287                 :                :     {
   32 peter@eisentraut.org      288                 :UNC           0 :         bytea      *bound = SendFunctionCall(&cache->typioproc, lower.val);
 5045 bruce@momjian.us          289                 :UBC           0 :         uint32      bound_len = VARSIZE(bound) - VARHDRSZ;
                                290                 :              0 :         char       *bound_data = VARDATA(bound);
                                291                 :                : 
 2887 andres@anarazel.de        292                 :              0 :         pq_sendint32(buf, bound_len);
 5056 heikki.linnakangas@i      293                 :              0 :         pq_sendbytes(buf, bound_data, bound_len);
                                294                 :                :     }
                                295                 :                : 
                                296         [ #  # ]:              0 :     if (RANGE_HAS_UBOUND(flags))
                                297                 :                :     {
   32 peter@eisentraut.org      298                 :UNC           0 :         bytea      *bound = SendFunctionCall(&cache->typioproc, upper.val);
 5045 bruce@momjian.us          299                 :UBC           0 :         uint32      bound_len = VARSIZE(bound) - VARHDRSZ;
                                300                 :              0 :         char       *bound_data = VARDATA(bound);
                                301                 :                : 
 2887 andres@anarazel.de        302                 :              0 :         pq_sendint32(buf, bound_len);
 5056 heikki.linnakangas@i      303                 :              0 :         pq_sendbytes(buf, bound_data, bound_len);
                                304                 :                :     }
                                305                 :                : 
                                306                 :              0 :     PG_RETURN_BYTEA_P(pq_endtypsend(buf));
                                307                 :                : }
                                308                 :                : 
                                309                 :                : /*
                                310                 :                :  * get_range_io_data: get cached information needed for range type I/O
                                311                 :                :  *
                                312                 :                :  * The range I/O functions need a bit more cached info than other range
                                313                 :                :  * functions, so they store a RangeIOData struct in fn_extra, not just a
                                314                 :                :  * pointer to a type cache entry.
                                315                 :                :  */
                                316                 :                : static RangeIOData *
 5044 tgl@sss.pgh.pa.us         317                 :CBC       57650 : get_range_io_data(FunctionCallInfo fcinfo, Oid rngtypid, IOFuncSelector func)
                                318                 :                : {
                                319                 :          57650 :     RangeIOData *cache = (RangeIOData *) fcinfo->flinfo->fn_extra;
                                320                 :                : 
                                321   [ +  +  -  + ]:          57650 :     if (cache == NULL || cache->typcache->type_id != rngtypid)
                                322                 :                :     {
                                323                 :                :         int16       typlen;
                                324                 :                :         bool        typbyval;
                                325                 :                :         char        typalign;
                                326                 :                :         char        typdelim;
                                327                 :                :         Oid         typiofunc;
                                328                 :                : 
                                329                 :           5001 :         cache = (RangeIOData *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                330                 :                :                                                    sizeof(RangeIOData));
                                331                 :           5001 :         cache->typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
                                332         [ -  + ]:           5001 :         if (cache->typcache->rngelemtype == NULL)
 5044 tgl@sss.pgh.pa.us         333         [ #  # ]:UBC           0 :             elog(ERROR, "type %u is not a range type", rngtypid);
                                334                 :                : 
                                335                 :                :         /* get_type_io_data does more than we need, but is convenient */
 5044 tgl@sss.pgh.pa.us         336                 :CBC        5001 :         get_type_io_data(cache->typcache->rngelemtype->type_id,
                                337                 :                :                          func,
                                338                 :                :                          &typlen,
                                339                 :                :                          &typbyval,
                                340                 :                :                          &typalign,
                                341                 :                :                          &typdelim,
                                342                 :                :                          &cache->typioparam,
                                343                 :                :                          &typiofunc);
                                344                 :                : 
 2011 alvherre@alvh.no-ip.      345         [ -  + ]:           5001 :         if (!OidIsValid(typiofunc))
                                346                 :                :         {
                                347                 :                :             /* this could only happen for receive or send */
 5044 tgl@sss.pgh.pa.us         348         [ #  # ]:UBC           0 :             if (func == IOFunc_receive)
                                349         [ #  # ]:              0 :                 ereport(ERROR,
                                350                 :                :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                351                 :                :                          errmsg("no binary input function available for type %s",
                                352                 :                :                                 format_type_be(cache->typcache->rngelemtype->type_id))));
                                353                 :                :             else
                                354         [ #  # ]:              0 :                 ereport(ERROR,
                                355                 :                :                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                356                 :                :                          errmsg("no binary output function available for type %s",
                                357                 :                :                                 format_type_be(cache->typcache->rngelemtype->type_id))));
                                358                 :                :         }
 2011 alvherre@alvh.no-ip.      359                 :CBC        5001 :         fmgr_info_cxt(typiofunc, &cache->typioproc,
 5044 tgl@sss.pgh.pa.us         360                 :           5001 :                       fcinfo->flinfo->fn_mcxt);
                                361                 :                : 
  282 peter@eisentraut.org      362                 :           5001 :         fcinfo->flinfo->fn_extra = cache;
                                363                 :                :     }
                                364                 :                : 
 5044 tgl@sss.pgh.pa.us         365                 :          57650 :     return cache;
                                366                 :                : }
                                367                 :                : 
                                368                 :                : 
                                369                 :                : /*
                                370                 :                :  *----------------------------------------------------------
                                371                 :                :  * GENERIC FUNCTIONS
                                372                 :                :  *----------------------------------------------------------
                                373                 :                :  */
                                374                 :                : 
                                375                 :                : /* Construct standard-form range value from two arguments */
                                376                 :                : Datum
 5056 heikki.linnakangas@i      377                 :          54888 : range_constructor2(PG_FUNCTION_ARGS)
                                378                 :                : {
 5045 bruce@momjian.us          379                 :          54888 :     Datum       arg1 = PG_GETARG_DATUM(0);
                                380                 :          54888 :     Datum       arg2 = PG_GETARG_DATUM(1);
                                381                 :          54888 :     Oid         rngtypid = get_fn_expr_rettype(fcinfo->flinfo);
                                382                 :                :     RangeType  *range;
                                383                 :                :     TypeCacheEntry *typcache;
                                384                 :                :     RangeBound  lower;
                                385                 :                :     RangeBound  upper;
                                386                 :                : 
 5044 tgl@sss.pgh.pa.us         387                 :          54888 :     typcache = range_get_typcache(fcinfo, rngtypid);
                                388                 :                : 
 5045 bruce@momjian.us          389         [ +  + ]:          54888 :     lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
                                390                 :          54888 :     lower.infinite = PG_ARGISNULL(0);
 5044 tgl@sss.pgh.pa.us         391                 :          54888 :     lower.inclusive = true;
 5045 bruce@momjian.us          392                 :          54888 :     lower.lower = true;
                                393                 :                : 
                                394         [ +  + ]:          54888 :     upper.val = PG_ARGISNULL(1) ? (Datum) 0 : arg2;
                                395                 :          54888 :     upper.infinite = PG_ARGISNULL(1);
 5044 tgl@sss.pgh.pa.us         396                 :          54888 :     upper.inclusive = false;
 5045 bruce@momjian.us          397                 :          54888 :     upper.lower = false;
                                398                 :                : 
  996 tgl@sss.pgh.pa.us         399                 :          54888 :     range = make_range(typcache, &lower, &upper, false, NULL);
                                400                 :                : 
 2910                           401                 :          54870 :     PG_RETURN_RANGE_P(range);
                                402                 :                : }
                                403                 :                : 
                                404                 :                : /* Construct general range value from three arguments */
                                405                 :                : Datum
 5056 heikki.linnakangas@i      406                 :           2583 : range_constructor3(PG_FUNCTION_ARGS)
                                407                 :                : {
 5045 bruce@momjian.us          408                 :           2583 :     Datum       arg1 = PG_GETARG_DATUM(0);
                                409                 :           2583 :     Datum       arg2 = PG_GETARG_DATUM(1);
                                410                 :           2583 :     Oid         rngtypid = get_fn_expr_rettype(fcinfo->flinfo);
                                411                 :                :     RangeType  *range;
                                412                 :                :     TypeCacheEntry *typcache;
                                413                 :                :     RangeBound  lower;
                                414                 :                :     RangeBound  upper;
                                415                 :                :     char        flags;
                                416                 :                : 
 5044 tgl@sss.pgh.pa.us         417                 :           2583 :     typcache = range_get_typcache(fcinfo, rngtypid);
                                418                 :                : 
 5056 heikki.linnakangas@i      419         [ -  + ]:           2583 :     if (PG_ARGISNULL(2))
 5056 heikki.linnakangas@i      420         [ #  # ]:UBC           0 :         ereport(ERROR,
                                421                 :                :                 (errcode(ERRCODE_DATA_EXCEPTION),
                                422                 :                :                  errmsg("range constructor flags argument must not be null")));
                                423                 :                : 
 3100 noah@leadboat.com         424                 :CBC        2583 :     flags = range_parse_flags(text_to_cstring(PG_GETARG_TEXT_PP(2)));
                                425                 :                : 
 5045 bruce@momjian.us          426         [ +  + ]:           2583 :     lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
                                427                 :           2583 :     lower.infinite = PG_ARGISNULL(0);
 5044 tgl@sss.pgh.pa.us         428                 :           2583 :     lower.inclusive = (flags & RANGE_LB_INC) != 0;
 5045 bruce@momjian.us          429                 :           2583 :     lower.lower = true;
                                430                 :                : 
                                431         [ +  + ]:           2583 :     upper.val = PG_ARGISNULL(1) ? (Datum) 0 : arg2;
                                432                 :           2583 :     upper.infinite = PG_ARGISNULL(1);
 5044 tgl@sss.pgh.pa.us         433                 :           2583 :     upper.inclusive = (flags & RANGE_UB_INC) != 0;
 5045 bruce@momjian.us          434                 :           2583 :     upper.lower = false;
                                435                 :                : 
  996 tgl@sss.pgh.pa.us         436                 :           2583 :     range = make_range(typcache, &lower, &upper, false, NULL);
                                437                 :                : 
 2910                           438                 :           2583 :     PG_RETURN_RANGE_P(range);
                                439                 :                : }
                                440                 :                : 
                                441                 :                : 
                                442                 :                : /* range -> subtype functions */
                                443                 :                : 
                                444                 :                : /* extract lower bound value */
                                445                 :                : Datum
 5056 heikki.linnakangas@i      446                 :            129 : range_lower(PG_FUNCTION_ARGS)
                                447                 :                : {
 2910 tgl@sss.pgh.pa.us         448                 :            129 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                449                 :                :     TypeCacheEntry *typcache;
                                450                 :                :     RangeBound  lower;
                                451                 :                :     RangeBound  upper;
                                452                 :                :     bool        empty;
                                453                 :                : 
 5044                           454                 :            129 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                455                 :                : 
                                456                 :            129 :     range_deserialize(typcache, r1, &lower, &upper, &empty);
                                457                 :                : 
                                458                 :                :     /* Return NULL if there's no finite lower bound */
 5045                           459   [ +  +  +  + ]:            129 :     if (empty || lower.infinite)
                                460                 :             18 :         PG_RETURN_NULL();
                                461                 :                : 
 5056 heikki.linnakangas@i      462                 :            111 :     PG_RETURN_DATUM(lower.val);
                                463                 :                : }
                                464                 :                : 
                                465                 :                : /* extract upper bound value */
                                466                 :                : Datum
                                467                 :            114 : range_upper(PG_FUNCTION_ARGS)
                                468                 :                : {
 2910 tgl@sss.pgh.pa.us         469                 :            114 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                470                 :                :     TypeCacheEntry *typcache;
                                471                 :                :     RangeBound  lower;
                                472                 :                :     RangeBound  upper;
                                473                 :                :     bool        empty;
                                474                 :                : 
 5044                           475                 :            114 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                476                 :                : 
                                477                 :            114 :     range_deserialize(typcache, r1, &lower, &upper, &empty);
                                478                 :                : 
                                479                 :                :     /* Return NULL if there's no finite upper bound */
 5045                           480   [ +  +  +  + ]:            114 :     if (empty || upper.infinite)
                                481                 :             18 :         PG_RETURN_NULL();
                                482                 :                : 
 5056 heikki.linnakangas@i      483                 :             96 :     PG_RETURN_DATUM(upper.val);
                                484                 :                : }
                                485                 :                : 
                                486                 :                : 
                                487                 :                : /* range -> bool functions */
                                488                 :                : 
                                489                 :                : /* is range empty? */
                                490                 :                : Datum
                                491                 :           1098 : range_empty(PG_FUNCTION_ARGS)
                                492                 :                : {
 2910 tgl@sss.pgh.pa.us         493                 :           1098 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
 5044                           494                 :           1098 :     char        flags = range_get_flags(r1);
                                495                 :                : 
                                496                 :           1098 :     PG_RETURN_BOOL(flags & RANGE_EMPTY);
                                497                 :                : }
                                498                 :                : 
                                499                 :                : /* is lower bound inclusive? */
                                500                 :                : Datum
 5056 heikki.linnakangas@i      501                 :             36 : range_lower_inc(PG_FUNCTION_ARGS)
                                502                 :                : {
 2910 tgl@sss.pgh.pa.us         503                 :             36 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
 5044                           504                 :             36 :     char        flags = range_get_flags(r1);
                                505                 :                : 
                                506                 :             36 :     PG_RETURN_BOOL(flags & RANGE_LB_INC);
                                507                 :                : }
                                508                 :                : 
                                509                 :                : /* is upper bound inclusive? */
                                510                 :                : Datum
 5056 heikki.linnakangas@i      511                 :             36 : range_upper_inc(PG_FUNCTION_ARGS)
                                512                 :                : {
 2910 tgl@sss.pgh.pa.us         513                 :             36 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
 5044                           514                 :             36 :     char        flags = range_get_flags(r1);
                                515                 :                : 
                                516                 :             36 :     PG_RETURN_BOOL(flags & RANGE_UB_INC);
                                517                 :                : }
                                518                 :                : 
                                519                 :                : /* is lower bound infinite? */
                                520                 :                : Datum
 5056 heikki.linnakangas@i      521                 :             36 : range_lower_inf(PG_FUNCTION_ARGS)
                                522                 :                : {
 2910 tgl@sss.pgh.pa.us         523                 :             36 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
 5044                           524                 :             36 :     char        flags = range_get_flags(r1);
                                525                 :                : 
                                526                 :             36 :     PG_RETURN_BOOL(flags & RANGE_LB_INF);
                                527                 :                : }
                                528                 :                : 
                                529                 :                : /* is upper bound infinite? */
                                530                 :                : Datum
 5056 heikki.linnakangas@i      531                 :             36 : range_upper_inf(PG_FUNCTION_ARGS)
                                532                 :                : {
 2910 tgl@sss.pgh.pa.us         533                 :             36 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
 5044                           534                 :             36 :     char        flags = range_get_flags(r1);
                                535                 :                : 
                                536                 :             36 :     PG_RETURN_BOOL(flags & RANGE_UB_INF);
                                537                 :                : }
                                538                 :                : 
                                539                 :                : 
                                540                 :                : /* range, element -> bool functions */
                                541                 :                : 
                                542                 :                : /* contains? */
                                543                 :                : Datum
 5037                           544                 :          38100 : range_contains_elem(PG_FUNCTION_ARGS)
                                545                 :                : {
 2910                           546                 :          38100 :     RangeType  *r = PG_GETARG_RANGE_P(0);
 5037                           547                 :          38100 :     Datum       val = PG_GETARG_DATUM(1);
                                548                 :                :     TypeCacheEntry *typcache;
                                549                 :                : 
                                550                 :          38100 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                                551                 :                : 
                                552                 :          38100 :     PG_RETURN_BOOL(range_contains_elem_internal(typcache, r, val));
                                553                 :                : }
                                554                 :                : 
                                555                 :                : /* contained by? */
                                556                 :                : Datum
                                557                 :             42 : elem_contained_by_range(PG_FUNCTION_ARGS)
                                558                 :                : {
                                559                 :             42 :     Datum       val = PG_GETARG_DATUM(0);
 2910                           560                 :             42 :     RangeType  *r = PG_GETARG_RANGE_P(1);
                                561                 :                :     TypeCacheEntry *typcache;
                                562                 :                : 
 5037                           563                 :             42 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                                564                 :                : 
                                565                 :             42 :     PG_RETURN_BOOL(range_contains_elem_internal(typcache, r, val));
                                566                 :                : }
                                567                 :                : 
                                568                 :                : 
                                569                 :                : /* range, range -> bool functions */
                                570                 :                : 
                                571                 :                : /* equality (internal version) */
                                572                 :                : bool
 2137 peter@eisentraut.org      573                 :          79695 : range_eq_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                574                 :                : {
                                575                 :                :     RangeBound  lower1,
                                576                 :                :                 lower2;
                                577                 :                :     RangeBound  upper1,
                                578                 :                :                 upper2;
                                579                 :                :     bool        empty1,
                                580                 :                :                 empty2;
                                581                 :                : 
                                582                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         583         [ -  + ]:          79695 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      584         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                585                 :                : 
 5044 tgl@sss.pgh.pa.us         586                 :CBC       79695 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                587                 :          79695 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                588                 :                : 
 5056 heikki.linnakangas@i      589   [ +  +  +  + ]:          79695 :     if (empty1 && empty2)
 4798                           590                 :           3783 :         return true;
 5056                           591         [ +  + ]:          75912 :     if (empty1 != empty2)
 4798                           592                 :           6756 :         return false;
                                593                 :                : 
 5044 tgl@sss.pgh.pa.us         594         [ +  + ]:          69156 :     if (range_cmp_bounds(typcache, &lower1, &lower2) != 0)
 4798 heikki.linnakangas@i      595                 :          40705 :         return false;
                                596                 :                : 
 5044 tgl@sss.pgh.pa.us         597         [ +  + ]:          28451 :     if (range_cmp_bounds(typcache, &upper1, &upper2) != 0)
 4798 heikki.linnakangas@i      598                 :          16759 :         return false;
                                599                 :                : 
                                600                 :          11692 :     return true;
                                601                 :                : }
                                602                 :                : 
                                603                 :                : /* equality */
                                604                 :                : Datum
                                605                 :          39606 : range_eq(PG_FUNCTION_ARGS)
                                606                 :                : {
 2910 tgl@sss.pgh.pa.us         607                 :          39606 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                608                 :          39606 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                609                 :                :     TypeCacheEntry *typcache;
                                610                 :                : 
 4798 heikki.linnakangas@i      611                 :          39606 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                612                 :                : 
                                613                 :          39606 :     PG_RETURN_BOOL(range_eq_internal(typcache, r1, r2));
                                614                 :                : }
                                615                 :                : 
                                616                 :                : /* inequality (internal version) */
                                617                 :                : bool
 2137 peter@eisentraut.org      618                 :UBC           0 : range_ne_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                619                 :                : {
 4798 heikki.linnakangas@i      620                 :              0 :     return (!range_eq_internal(typcache, r1, r2));
                                621                 :                : }
                                622                 :                : 
                                623                 :                : /* inequality */
                                624                 :                : Datum
 5056                           625                 :              0 : range_ne(PG_FUNCTION_ARGS)
                                626                 :                : {
 2910 tgl@sss.pgh.pa.us         627                 :              0 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                628                 :              0 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                629                 :                :     TypeCacheEntry *typcache;
                                630                 :                : 
 4798 heikki.linnakangas@i      631                 :              0 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                632                 :                : 
                                633                 :              0 :     PG_RETURN_BOOL(range_ne_internal(typcache, r1, r2));
                                634                 :                : }
                                635                 :                : 
                                636                 :                : /* contains? */
                                637                 :                : Datum
 5056 heikki.linnakangas@i      638                 :CBC       77235 : range_contains(PG_FUNCTION_ARGS)
                                639                 :                : {
 2910 tgl@sss.pgh.pa.us         640                 :          77235 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                641                 :          77235 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                642                 :                :     TypeCacheEntry *typcache;
                                643                 :                : 
 5044                           644                 :          77235 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                645                 :                : 
                                646                 :          77235 :     PG_RETURN_BOOL(range_contains_internal(typcache, r1, r2));
                                647                 :                : }
                                648                 :                : 
                                649                 :                : /* contained by? */
                                650                 :                : Datum
 5056 heikki.linnakangas@i      651                 :          38466 : range_contained_by(PG_FUNCTION_ARGS)
                                652                 :                : {
 2910 tgl@sss.pgh.pa.us         653                 :          38466 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                654                 :          38466 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                655                 :                :     TypeCacheEntry *typcache;
                                656                 :                : 
 5044                           657                 :          38466 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                658                 :                : 
 4798 heikki.linnakangas@i      659                 :          38466 :     PG_RETURN_BOOL(range_contained_by_internal(typcache, r1, r2));
                                660                 :                : }
                                661                 :                : 
                                662                 :                : /* strictly left of? (internal version) */
                                663                 :                : bool
 2137 peter@eisentraut.org      664                 :          61861 : range_before_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                665                 :                : {
                                666                 :                :     RangeBound  lower1,
                                667                 :                :                 lower2;
                                668                 :                :     RangeBound  upper1,
                                669                 :                :                 upper2;
                                670                 :                :     bool        empty1,
                                671                 :                :                 empty2;
                                672                 :                : 
                                673                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         674         [ -  + ]:          61861 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      675         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                676                 :                : 
 5044 tgl@sss.pgh.pa.us         677                 :CBC       61861 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                678                 :          61861 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                679                 :                : 
                                680                 :                :     /* An empty range is neither before nor after any other range */
 5056 heikki.linnakangas@i      681   [ +  +  +  + ]:          61861 :     if (empty1 || empty2)
 4798                           682                 :           7455 :         return false;
                                683                 :                : 
                                684                 :          54406 :     return (range_cmp_bounds(typcache, &upper1, &lower2) < 0);
                                685                 :                : }
                                686                 :                : 
                                687                 :                : /* strictly left of? */
                                688                 :                : Datum
                                689                 :          39459 : range_before(PG_FUNCTION_ARGS)
                                690                 :                : {
 2910 tgl@sss.pgh.pa.us         691                 :          39459 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                692                 :          39459 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                693                 :                :     TypeCacheEntry *typcache;
                                694                 :                : 
 4798 heikki.linnakangas@i      695                 :          39459 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                696                 :                : 
                                697                 :          39459 :     PG_RETURN_BOOL(range_before_internal(typcache, r1, r2));
                                698                 :                : }
                                699                 :                : 
                                700                 :                : /* strictly right of? (internal version) */
                                701                 :                : bool
 2137 peter@eisentraut.org      702                 :          99574 : range_after_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                703                 :                : {
                                704                 :                :     RangeBound  lower1,
                                705                 :                :                 lower2;
                                706                 :                :     RangeBound  upper1,
                                707                 :                :                 upper2;
                                708                 :                :     bool        empty1,
                                709                 :                :                 empty2;
                                710                 :                : 
                                711                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         712         [ -  + ]:          99574 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      713         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                714                 :                : 
 5044 tgl@sss.pgh.pa.us         715                 :CBC       99574 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                716                 :          99574 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                717                 :                : 
                                718                 :                :     /* An empty range is neither before nor after any other range */
 5056 heikki.linnakangas@i      719   [ +  +  +  + ]:          99574 :     if (empty1 || empty2)
 4798                           720                 :           7155 :         return false;
                                721                 :                : 
                                722                 :          92419 :     return (range_cmp_bounds(typcache, &lower1, &upper2) > 0);
                                723                 :                : }
                                724                 :                : 
                                725                 :                : /* strictly right of? */
                                726                 :                : Datum
                                727                 :          39153 : range_after(PG_FUNCTION_ARGS)
                                728                 :                : {
 2910 tgl@sss.pgh.pa.us         729                 :          39153 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                730                 :          39153 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                731                 :                :     TypeCacheEntry *typcache;
                                732                 :                : 
 4798 heikki.linnakangas@i      733                 :          39153 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                734                 :                : 
                                735                 :          39153 :     PG_RETURN_BOOL(range_after_internal(typcache, r1, r2));
                                736                 :                : }
                                737                 :                : 
                                738                 :                : /*
                                739                 :                :  * Check if two bounds A and B are "adjacent", where A is an upper bound and B
                                740                 :                :  * is a lower bound. For the bounds to be adjacent, each subtype value must
                                741                 :                :  * satisfy strictly one of the bounds: there are no values which satisfy both
                                742                 :                :  * bounds (i.e. less than A and greater than B); and there are no values which
                                743                 :                :  * satisfy neither bound (i.e. greater than A and less than B).
                                744                 :                :  *
                                745                 :                :  * For discrete ranges, we rely on the canonicalization function to see if A..B
                                746                 :                :  * normalizes to empty. (If there is no canonicalization function, it's
                                747                 :                :  * impossible for such a range to normalize to empty, so we needn't bother to
                                748                 :                :  * try.)
                                749                 :                :  *
                                750                 :                :  * If A == B, the ranges are adjacent only if the bounds have different
                                751                 :                :  * inclusive flags (i.e., exactly one of the ranges includes the common
                                752                 :                :  * boundary point).
                                753                 :                :  *
                                754                 :                :  * And if A > B then the ranges are not adjacent in this order.
                                755                 :                :  */
                                756                 :                : bool
 4565                           757                 :         236059 : bounds_adjacent(TypeCacheEntry *typcache, RangeBound boundA, RangeBound boundB)
                                758                 :                : {
                                759                 :                :     int         cmp;
                                760                 :                : 
                                761   [ +  -  -  + ]:         236059 :     Assert(!boundA.lower && boundB.lower);
                                762                 :                : 
                                763                 :         236059 :     cmp = range_cmp_bound_values(typcache, &boundA, &boundB);
                                764         [ +  + ]:         236059 :     if (cmp < 0)
                                765                 :                :     {
                                766                 :                :         RangeType  *r;
                                767                 :                : 
                                768                 :                :         /*
                                769                 :                :          * Bounds do not overlap; see if there are points in between.
                                770                 :                :          */
                                771                 :                : 
                                772                 :                :         /* in a continuous subtype, there are assumed to be points between */
                                773         [ +  + ]:          72375 :         if (!OidIsValid(typcache->rng_canonical_finfo.fn_oid))
                                774                 :            408 :             return false;
                                775                 :                : 
                                776                 :                :         /*
                                777                 :                :          * The bounds are of a discrete range type; so make a range A..B and
                                778                 :                :          * see if it's empty.
                                779                 :                :          */
                                780                 :                : 
                                781                 :                :         /* flip the inclusion flags */
                                782                 :          71967 :         boundA.inclusive = !boundA.inclusive;
                                783                 :          71967 :         boundB.inclusive = !boundB.inclusive;
                                784                 :                :         /* change upper/lower labels to avoid Assert failures */
                                785                 :          71967 :         boundA.lower = true;
                                786                 :          71967 :         boundB.lower = false;
  996 tgl@sss.pgh.pa.us         787                 :          71967 :         r = make_range(typcache, &boundA, &boundB, false, NULL);
 4565 heikki.linnakangas@i      788                 :          71967 :         return RangeIsEmpty(r);
                                789                 :                :     }
                                790         [ +  + ]:         163684 :     else if (cmp == 0)
                                791                 :            946 :         return boundA.inclusive != boundB.inclusive;
                                792                 :                :     else
 4483 bruce@momjian.us          793                 :         162738 :         return false;           /* bounds overlap */
                                794                 :                : }
                                795                 :                : 
                                796                 :                : /* adjacent to (but not overlapping)? (internal version) */
                                797                 :                : bool
 2137 peter@eisentraut.org      798                 :          71399 : range_adjacent_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                799                 :                : {
                                800                 :                :     RangeBound  lower1,
                                801                 :                :                 lower2;
                                802                 :                :     RangeBound  upper1,
                                803                 :                :                 upper2;
                                804                 :                :     bool        empty1,
                                805                 :                :                 empty2;
                                806                 :                : 
                                807                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         808         [ -  + ]:          71399 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      809         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                810                 :                : 
 5044 tgl@sss.pgh.pa.us         811                 :CBC       71399 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                812                 :          71399 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                813                 :                : 
                                814                 :                :     /* An empty range is not adjacent to any other range */
 5056 heikki.linnakangas@i      815   [ +  +  -  + ]:          71399 :     if (empty1 || empty2)
 4798                           816                 :           6000 :         return false;
                                817                 :                : 
                                818                 :                :     /*
                                819                 :                :      * Given two ranges A..B and C..D, the ranges are adjacent if and only if
                                820                 :                :      * B is adjacent to C, or D is adjacent to A.
                                821                 :                :      */
 4565                           822   [ +  +  +  + ]:         130044 :     return (bounds_adjacent(typcache, upper1, lower2) ||
                                823                 :          64645 :             bounds_adjacent(typcache, upper2, lower1));
                                824                 :                : }
                                825                 :                : 
                                826                 :                : /* adjacent to (but not overlapping)? */
                                827                 :                : Datum
 4798                           828                 :          37218 : range_adjacent(PG_FUNCTION_ARGS)
                                829                 :                : {
 2910 tgl@sss.pgh.pa.us         830                 :          37218 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                831                 :          37218 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                832                 :                :     TypeCacheEntry *typcache;
                                833                 :                : 
 4798 heikki.linnakangas@i      834                 :          37218 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                835                 :                : 
                                836                 :          37218 :     PG_RETURN_BOOL(range_adjacent_internal(typcache, r1, r2));
                                837                 :                : }
                                838                 :                : 
                                839                 :                : /* overlaps? (internal version) */
                                840                 :                : bool
 2137 peter@eisentraut.org      841                 :          49149 : range_overlaps_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                842                 :                : {
                                843                 :                :     RangeBound  lower1,
                                844                 :                :                 lower2;
                                845                 :                :     RangeBound  upper1,
                                846                 :                :                 upper2;
                                847                 :                :     bool        empty1,
                                848                 :                :                 empty2;
                                849                 :                : 
                                850                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         851         [ -  + ]:          49149 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      852         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                853                 :                : 
 5044 tgl@sss.pgh.pa.us         854                 :CBC       49149 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                855                 :          49149 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                856                 :                : 
                                857                 :                :     /* An empty range does not overlap any other range */
 5056 heikki.linnakangas@i      858   [ +  +  +  + ]:          49149 :     if (empty1 || empty2)
 4798                           859                 :           7052 :         return false;
                                860                 :                : 
 5044 tgl@sss.pgh.pa.us         861   [ +  +  +  + ]:          80912 :     if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0 &&
                                862                 :          38815 :         range_cmp_bounds(typcache, &lower1, &upper2) <= 0)
 4798 heikki.linnakangas@i      863                 :           2494 :         return true;
                                864                 :                : 
 5044 tgl@sss.pgh.pa.us         865   [ +  +  +  + ]:          42885 :     if (range_cmp_bounds(typcache, &lower2, &lower1) >= 0 &&
                                866                 :           3282 :         range_cmp_bounds(typcache, &lower2, &upper1) <= 0)
 4798 heikki.linnakangas@i      867                 :           2997 :         return true;
                                868                 :                : 
                                869                 :          36606 :     return false;
                                870                 :                : }
                                871                 :                : 
                                872                 :                : /* overlaps? */
                                873                 :                : Datum
                                874                 :          38705 : range_overlaps(PG_FUNCTION_ARGS)
                                875                 :                : {
 2910 tgl@sss.pgh.pa.us         876                 :          38705 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                877                 :          38705 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                878                 :                :     TypeCacheEntry *typcache;
                                879                 :                : 
 4798 heikki.linnakangas@i      880                 :          38705 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                881                 :                : 
                                882                 :          38705 :     PG_RETURN_BOOL(range_overlaps_internal(typcache, r1, r2));
                                883                 :                : }
                                884                 :                : 
                                885                 :                : /* does not extend to right of? (internal version) */
                                886                 :                : bool
 2137 peter@eisentraut.org      887                 :          65663 : range_overleft_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                888                 :                : {
                                889                 :                :     RangeBound  lower1,
                                890                 :                :                 lower2;
                                891                 :                :     RangeBound  upper1,
                                892                 :                :                 upper2;
                                893                 :                :     bool        empty1,
                                894                 :                :                 empty2;
                                895                 :                : 
                                896                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         897         [ -  + ]:          65663 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      898         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                899                 :                : 
 5044 tgl@sss.pgh.pa.us         900                 :CBC       65663 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                901                 :          65663 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                902                 :                : 
                                903                 :                :     /* An empty range is neither before nor after any other range */
 5056 heikki.linnakangas@i      904   [ +  +  -  + ]:          65663 :     if (empty1 || empty2)
 4798                           905                 :           6573 :         return false;
                                906                 :                : 
 5044 tgl@sss.pgh.pa.us         907         [ +  + ]:          59090 :     if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
 4798 heikki.linnakangas@i      908                 :          20312 :         return true;
                                909                 :                : 
                                910                 :          38778 :     return false;
                                911                 :                : }
                                912                 :                : 
                                913                 :                : /* does not extend to right of? */
                                914                 :                : Datum
                                915                 :          38253 : range_overleft(PG_FUNCTION_ARGS)
                                916                 :                : {
 2910 tgl@sss.pgh.pa.us         917                 :          38253 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                918                 :          38253 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                919                 :                :     TypeCacheEntry *typcache;
                                920                 :                : 
 4798 heikki.linnakangas@i      921                 :          38253 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                922                 :                : 
                                923                 :          38253 :     PG_RETURN_BOOL(range_overleft_internal(typcache, r1, r2));
                                924                 :                : }
                                925                 :                : 
                                926                 :                : /* does not extend to left of? (internal version) */
                                927                 :                : bool
 2137 peter@eisentraut.org      928                 :         109009 : range_overright_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                                929                 :                : {
                                930                 :                :     RangeBound  lower1,
                                931                 :                :                 lower2;
                                932                 :                :     RangeBound  upper1,
                                933                 :                :                 upper2;
                                934                 :                :     bool        empty1,
                                935                 :                :                 empty2;
                                936                 :                : 
                                937                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us         938         [ -  + ]:         109009 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i      939         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                940                 :                : 
 5044 tgl@sss.pgh.pa.us         941                 :CBC      109009 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                                942                 :         109009 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                                943                 :                : 
                                944                 :                :     /* An empty range is neither before nor after any other range */
 5056 heikki.linnakangas@i      945   [ +  +  -  + ]:         109009 :     if (empty1 || empty2)
 4039 tgl@sss.pgh.pa.us         946                 :           6573 :         return false;
                                947                 :                : 
 5044                           948         [ +  + ]:         102436 :     if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
 4039                           949                 :          95474 :         return true;
                                950                 :                : 
                                951                 :           6962 :     return false;
                                952                 :                : }
                                953                 :                : 
                                954                 :                : /* does not extend to left of? */
                                955                 :                : Datum
 4798 heikki.linnakangas@i      956                 :          38250 : range_overright(PG_FUNCTION_ARGS)
                                957                 :                : {
 2910 tgl@sss.pgh.pa.us         958                 :          38250 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                959                 :          38250 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                960                 :                :     TypeCacheEntry *typcache;
                                961                 :                : 
 4798 heikki.linnakangas@i      962                 :          38250 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                963                 :                : 
                                964                 :          38250 :     PG_RETURN_BOOL(range_overright_internal(typcache, r1, r2));
                                965                 :                : }
                                966                 :                : 
                                967                 :                : 
                                968                 :                : /* range, range -> range functions */
                                969                 :                : 
                                970                 :                : /* set difference */
                                971                 :                : Datum
 5056                           972                 :             15 : range_minus(PG_FUNCTION_ARGS)
                                973                 :                : {
 2910 tgl@sss.pgh.pa.us         974                 :             15 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                                975                 :             15 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                                976                 :                :     RangeType  *ret;
                                977                 :                :     TypeCacheEntry *typcache;
                                978                 :                : 
                                979                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 1721 akorotkov@postgresql      980         [ -  + ]:             15 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 1721 akorotkov@postgresql      981         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                                982                 :                : 
 1721 akorotkov@postgresql      983                 :CBC          15 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                                984                 :                : 
                                985                 :             15 :     ret = range_minus_internal(typcache, r1, r2);
                                986         [ +  - ]:             15 :     if (ret)
                                987                 :             15 :         PG_RETURN_RANGE_P(ret);
                                988                 :                :     else
 1721 akorotkov@postgresql      989                 :UBC           0 :         PG_RETURN_NULL();
                                990                 :                : }
                                991                 :                : 
                                992                 :                : RangeType *
 1721 akorotkov@postgresql      993                 :CBC          48 : range_minus_internal(TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
                                994                 :                : {
                                995                 :                :     RangeBound  lower1,
                                996                 :                :                 lower2;
                                997                 :                :     RangeBound  upper1,
                                998                 :                :                 upper2;
                                999                 :                :     bool        empty1,
                               1000                 :                :                 empty2;
                               1001                 :                :     int         cmp_l1l2,
                               1002                 :                :                 cmp_l1u2,
                               1003                 :                :                 cmp_u1l2,
                               1004                 :                :                 cmp_u1u2;
                               1005                 :                : 
 5044 tgl@sss.pgh.pa.us        1006                 :             48 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               1007                 :             48 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               1008                 :                : 
                               1009                 :                :     /* if either is empty, r1 is the correct answer */
 5056 heikki.linnakangas@i     1010   [ +  -  -  + ]:             48 :     if (empty1 || empty2)
 1721 akorotkov@postgresql     1011                 :UBC           0 :         return r1;
                               1012                 :                : 
 5044 tgl@sss.pgh.pa.us        1013                 :CBC          48 :     cmp_l1l2 = range_cmp_bounds(typcache, &lower1, &lower2);
                               1014                 :             48 :     cmp_l1u2 = range_cmp_bounds(typcache, &lower1, &upper2);
                               1015                 :             48 :     cmp_u1l2 = range_cmp_bounds(typcache, &upper1, &lower2);
                               1016                 :             48 :     cmp_u1u2 = range_cmp_bounds(typcache, &upper1, &upper2);
                               1017                 :                : 
 5056 heikki.linnakangas@i     1018   [ +  +  -  + ]:             48 :     if (cmp_l1l2 < 0 && cmp_u1u2 > 0)
 5056 heikki.linnakangas@i     1019         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1020                 :                :                 (errcode(ERRCODE_DATA_EXCEPTION),
                               1021                 :                :                  errmsg("result of range difference would not be contiguous")));
                               1022                 :                : 
 5056 heikki.linnakangas@i     1023   [ +  -  +  + ]:CBC          48 :     if (cmp_l1u2 > 0 || cmp_u1l2 < 0)
 1721 akorotkov@postgresql     1024                 :              6 :         return r1;
                               1025                 :                : 
 5056 heikki.linnakangas@i     1026   [ +  +  +  + ]:             42 :     if (cmp_l1l2 >= 0 && cmp_u1u2 <= 0)
 1721 akorotkov@postgresql     1027                 :             21 :         return make_empty_range(typcache);
                               1028                 :                : 
 5056 heikki.linnakangas@i     1029   [ +  +  +  -  :             21 :     if (cmp_l1l2 <= 0 && cmp_u1l2 >= 0 && cmp_u1u2 <= 0)
                                              +  + ]
                               1030                 :                :     {
                               1031                 :             12 :         lower2.inclusive = !lower2.inclusive;
 5045 bruce@momjian.us         1032                 :             12 :         lower2.lower = false;   /* it will become the upper bound */
  996 tgl@sss.pgh.pa.us        1033                 :             12 :         return make_range(typcache, &lower1, &lower2, false, NULL);
                               1034                 :                :     }
                               1035                 :                : 
 5056 heikki.linnakangas@i     1036   [ +  -  +  -  :              9 :     if (cmp_l1l2 >= 0 && cmp_u1u2 >= 0 && cmp_l1u2 <= 0)
                                              +  - ]
                               1037                 :                :     {
                               1038                 :              9 :         upper2.inclusive = !upper2.inclusive;
 5045 bruce@momjian.us         1039                 :              9 :         upper2.lower = true;    /* it will become the lower bound */
  996 tgl@sss.pgh.pa.us        1040                 :              9 :         return make_range(typcache, &upper2, &upper1, false, NULL);
                               1041                 :                :     }
                               1042                 :                : 
 5045 tgl@sss.pgh.pa.us        1043         [ #  # ]:UBC           0 :     elog(ERROR, "unexpected case in range_minus");
                               1044                 :                :     return NULL;
                               1045                 :                : }
                               1046                 :                : 
                               1047                 :                : /*
                               1048                 :                :  * Set union.  If strict is true, it is an error that the two input ranges
                               1049                 :                :  * are not adjacent or overlapping.
                               1050                 :                :  */
                               1051                 :                : RangeType *
 3777 alvherre@alvh.no-ip.     1052                 :CBC         787 : range_union_internal(TypeCacheEntry *typcache, RangeType *r1, RangeType *r2,
                               1053                 :                :                      bool strict)
                               1054                 :                : {
                               1055                 :                :     RangeBound  lower1,
                               1056                 :                :                 lower2;
                               1057                 :                :     RangeBound  upper1,
                               1058                 :                :                 upper2;
                               1059                 :                :     bool        empty1,
                               1060                 :                :                 empty2;
                               1061                 :                :     RangeBound *result_lower;
                               1062                 :                :     RangeBound *result_upper;
                               1063                 :                : 
                               1064                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us        1065         [ -  + ]:            787 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5045 tgl@sss.pgh.pa.us        1066         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                               1067                 :                : 
 5044 tgl@sss.pgh.pa.us        1068                 :CBC         787 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               1069                 :            787 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               1070                 :                : 
                               1071                 :                :     /* if either is empty, the other is the correct answer */
 5056 heikki.linnakangas@i     1072         [ +  + ]:            787 :     if (empty1)
 3777 alvherre@alvh.no-ip.     1073                 :              3 :         return r2;
 5056 heikki.linnakangas@i     1074         [ -  + ]:            784 :     if (empty2)
 3777 alvherre@alvh.no-ip.     1075                 :UBC           0 :         return r1;
                               1076                 :                : 
 3777 alvherre@alvh.no-ip.     1077         [ +  + ]:CBC         784 :     if (strict &&
   29 peter@eisentraut.org     1078         [ +  + ]:GNC          69 :         !range_overlaps_internal(typcache, r1, r2) &&
                               1079         [ +  + ]:              6 :         !range_adjacent_internal(typcache, r1, r2))
 5056 heikki.linnakangas@i     1080         [ +  - ]:CBC           3 :         ereport(ERROR,
                               1081                 :                :                 (errcode(ERRCODE_DATA_EXCEPTION),
                               1082                 :                :                  errmsg("result of range union would not be contiguous")));
                               1083                 :                : 
 5044 tgl@sss.pgh.pa.us        1084         [ +  + ]:            781 :     if (range_cmp_bounds(typcache, &lower1, &lower2) < 0)
 5056 heikki.linnakangas@i     1085                 :            763 :         result_lower = &lower1;
                               1086                 :                :     else
                               1087                 :             18 :         result_lower = &lower2;
                               1088                 :                : 
 5044 tgl@sss.pgh.pa.us        1089         [ +  + ]:            781 :     if (range_cmp_bounds(typcache, &upper1, &upper2) > 0)
 5056 heikki.linnakangas@i     1090                 :             24 :         result_upper = &upper1;
                               1091                 :                :     else
                               1092                 :            757 :         result_upper = &upper2;
                               1093                 :                : 
  996 tgl@sss.pgh.pa.us        1094                 :            781 :     return make_range(typcache, result_lower, result_upper, false, NULL);
                               1095                 :                : }
                               1096                 :                : 
                               1097                 :                : Datum
 3777 alvherre@alvh.no-ip.     1098                 :              9 : range_union(PG_FUNCTION_ARGS)
                               1099                 :                : {
 2910 tgl@sss.pgh.pa.us        1100                 :              9 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                               1101                 :              9 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                               1102                 :                :     TypeCacheEntry *typcache;
                               1103                 :                : 
 3777 alvherre@alvh.no-ip.     1104                 :              9 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                               1105                 :                : 
 2910 tgl@sss.pgh.pa.us        1106                 :              9 :     PG_RETURN_RANGE_P(range_union_internal(typcache, r1, r2, true));
                               1107                 :                : }
                               1108                 :                : 
                               1109                 :                : /*
                               1110                 :                :  * range merge: like set union, except also allow and account for non-adjacent
                               1111                 :                :  * input ranges.
                               1112                 :                :  */
                               1113                 :                : Datum
 3777 alvherre@alvh.no-ip.     1114                 :             15 : range_merge(PG_FUNCTION_ARGS)
                               1115                 :                : {
 2910 tgl@sss.pgh.pa.us        1116                 :             15 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                               1117                 :             15 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                               1118                 :                :     TypeCacheEntry *typcache;
                               1119                 :                : 
 3777 alvherre@alvh.no-ip.     1120                 :             15 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                               1121                 :                : 
 2910 tgl@sss.pgh.pa.us        1122                 :             15 :     PG_RETURN_RANGE_P(range_union_internal(typcache, r1, r2, false));
                               1123                 :                : }
                               1124                 :                : 
                               1125                 :                : /* set intersection */
                               1126                 :                : Datum
 5056 heikki.linnakangas@i     1127                 :             62 : range_intersect(PG_FUNCTION_ARGS)
                               1128                 :                : {
 2910 tgl@sss.pgh.pa.us        1129                 :             62 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                               1130                 :             62 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                               1131                 :                :     TypeCacheEntry *typcache;
                               1132                 :                : 
                               1133                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 1721 akorotkov@postgresql     1134         [ -  + ]:             62 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 1721 akorotkov@postgresql     1135         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                               1136                 :                : 
 1721 akorotkov@postgresql     1137                 :CBC          62 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                               1138                 :                : 
                               1139                 :             62 :     PG_RETURN_RANGE_P(range_intersect_internal(typcache, r1, r2));
                               1140                 :                : }
                               1141                 :                : 
                               1142                 :                : RangeType *
                               1143                 :            212 : range_intersect_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                               1144                 :                : {
                               1145                 :                :     RangeBound  lower1,
                               1146                 :                :                 lower2;
                               1147                 :                :     RangeBound  upper1,
                               1148                 :                :                 upper2;
                               1149                 :                :     bool        empty1,
                               1150                 :                :                 empty2;
                               1151                 :                :     RangeBound *result_lower;
                               1152                 :                :     RangeBound *result_upper;
                               1153                 :                : 
 5044 tgl@sss.pgh.pa.us        1154                 :            212 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               1155                 :            212 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               1156                 :                : 
 1721 akorotkov@postgresql     1157   [ +  +  +  -  :            212 :     if (empty1 || empty2 || !range_overlaps_internal(typcache, r1, r2))
                                              +  + ]
                               1158                 :             15 :         return make_empty_range(typcache);
                               1159                 :                : 
 5044 tgl@sss.pgh.pa.us        1160         [ +  + ]:            197 :     if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
 5056 heikki.linnakangas@i     1161                 :            142 :         result_lower = &lower1;
                               1162                 :                :     else
                               1163                 :             55 :         result_lower = &lower2;
                               1164                 :                : 
 5044 tgl@sss.pgh.pa.us        1165         [ +  + ]:            197 :     if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
 5056 heikki.linnakangas@i     1166                 :            148 :         result_upper = &upper1;
                               1167                 :                :     else
                               1168                 :             49 :         result_upper = &upper2;
                               1169                 :                : 
  996 tgl@sss.pgh.pa.us        1170                 :            197 :     return make_range(typcache, result_lower, result_upper, false, NULL);
                               1171                 :                : }
                               1172                 :                : 
                               1173                 :                : /* range, range -> range, range functions */
                               1174                 :                : 
                               1175                 :                : /*
                               1176                 :                :  * range_split_internal - if r2 intersects the middle of r1, leaving non-empty
                               1177                 :                :  * ranges on both sides, then return true and set output1 and output2 to the
                               1178                 :                :  * results of r1 - r2 (in order). Otherwise return false and don't set output1
                               1179                 :                :  * or output2. Neither input range should be empty.
                               1180                 :                :  */
                               1181                 :                : bool
 1721 akorotkov@postgresql     1182                 :             66 : range_split_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2,
                               1183                 :                :                      RangeType **output1, RangeType **output2)
                               1184                 :                : {
                               1185                 :                :     RangeBound  lower1,
                               1186                 :                :                 lower2;
                               1187                 :                :     RangeBound  upper1,
                               1188                 :                :                 upper2;
                               1189                 :                :     bool        empty1,
                               1190                 :                :                 empty2;
                               1191                 :                : 
                               1192                 :             66 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               1193                 :             66 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               1194                 :                : 
                               1195   [ +  +  +  + ]:            105 :     if (range_cmp_bounds(typcache, &lower1, &lower2) < 0 &&
                               1196                 :             39 :         range_cmp_bounds(typcache, &upper1, &upper2) > 0)
                               1197                 :                :     {
                               1198                 :                :         /*
                               1199                 :                :          * Need to invert inclusive/exclusive for the lower2 and upper2
                               1200                 :                :          * points. They can't be infinite though. We're allowed to overwrite
                               1201                 :                :          * these RangeBounds since they only exist locally.
                               1202                 :                :          */
                               1203                 :              9 :         lower2.inclusive = !lower2.inclusive;
                               1204                 :              9 :         lower2.lower = false;
                               1205                 :              9 :         upper2.inclusive = !upper2.inclusive;
                               1206                 :              9 :         upper2.lower = true;
                               1207                 :                : 
  996 tgl@sss.pgh.pa.us        1208                 :              9 :         *output1 = make_range(typcache, &lower1, &lower2, false, NULL);
                               1209                 :              9 :         *output2 = make_range(typcache, &upper2, &upper1, false, NULL);
 1721 akorotkov@postgresql     1210                 :              9 :         return true;
                               1211                 :                :     }
                               1212                 :                : 
                               1213                 :             57 :     return false;
                               1214                 :                : }
                               1215                 :                : 
                               1216                 :                : /* range -> range aggregate functions */
                               1217                 :                : 
                               1218                 :                : Datum
                               1219                 :             21 : range_intersect_agg_transfn(PG_FUNCTION_ARGS)
                               1220                 :                : {
                               1221                 :                :     MemoryContext aggContext;
                               1222                 :                :     Oid         rngtypoid;
                               1223                 :                :     TypeCacheEntry *typcache;
                               1224                 :                :     RangeType  *result;
                               1225                 :                :     RangeType  *current;
                               1226                 :                : 
                               1227         [ -  + ]:             21 :     if (!AggCheckCallContext(fcinfo, &aggContext))
 1721 akorotkov@postgresql     1228         [ #  # ]:UBC           0 :         elog(ERROR, "range_intersect_agg_transfn called in non-aggregate context");
                               1229                 :                : 
 1721 akorotkov@postgresql     1230                 :CBC          21 :     rngtypoid = get_fn_expr_argtype(fcinfo->flinfo, 1);
                               1231         [ -  + ]:             21 :     if (!type_is_range(rngtypoid))
 1256 peter@eisentraut.org     1232         [ #  # ]:UBC           0 :         elog(ERROR, "range_intersect_agg must be called with a range");
                               1233                 :                : 
 1721 akorotkov@postgresql     1234                 :CBC          21 :     typcache = range_get_typcache(fcinfo, rngtypoid);
                               1235                 :                : 
                               1236                 :                :     /* strictness ensures these are non-null */
                               1237                 :             21 :     result = PG_GETARG_RANGE_P(0);
                               1238                 :             21 :     current = PG_GETARG_RANGE_P(1);
                               1239                 :                : 
                               1240                 :             21 :     result = range_intersect_internal(typcache, result, current);
                               1241                 :             21 :     PG_RETURN_RANGE_P(result);
                               1242                 :                : }
                               1243                 :                : 
                               1244                 :                : 
                               1245                 :                : /* Btree support */
                               1246                 :                : 
                               1247                 :                : /* btree comparator */
                               1248                 :                : Datum
 5056 heikki.linnakangas@i     1249                 :           9354 : range_cmp(PG_FUNCTION_ARGS)
                               1250                 :                : {
 2910 tgl@sss.pgh.pa.us        1251                 :           9354 :     RangeType  *r1 = PG_GETARG_RANGE_P(0);
                               1252                 :           9354 :     RangeType  *r2 = PG_GETARG_RANGE_P(1);
                               1253                 :                :     TypeCacheEntry *typcache;
                               1254                 :                :     RangeBound  lower1,
                               1255                 :                :                 lower2;
                               1256                 :                :     RangeBound  upper1,
                               1257                 :                :                 upper2;
                               1258                 :                :     bool        empty1,
                               1259                 :                :                 empty2;
                               1260                 :                :     int         cmp;
                               1261                 :                : 
 3624 noah@leadboat.com        1262                 :           9354 :     check_stack_depth();        /* recurses when subtype is a range type */
                               1263                 :                : 
                               1264                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 5044 tgl@sss.pgh.pa.us        1265         [ -  + ]:           9354 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 5056 heikki.linnakangas@i     1266         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                               1267                 :                : 
 5044 tgl@sss.pgh.pa.us        1268                 :CBC        9354 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
                               1269                 :                : 
                               1270                 :           9354 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               1271                 :           9354 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               1272                 :                : 
                               1273                 :                :     /* For b-tree use, empty ranges sort before all else */
 5056 heikki.linnakangas@i     1274   [ +  +  +  + ]:           9354 :     if (empty1 && empty2)
 4364                          1275                 :           1317 :         cmp = 0;
 5056                          1276         [ +  + ]:           8037 :     else if (empty1)
 4364                          1277                 :           1737 :         cmp = -1;
 5056                          1278         [ +  + ]:           6300 :     else if (empty2)
 4364                          1279                 :           1020 :         cmp = 1;
                               1280                 :                :     else
                               1281                 :                :     {
                               1282                 :           5280 :         cmp = range_cmp_bounds(typcache, &lower1, &lower2);
                               1283         [ +  + ]:           5280 :         if (cmp == 0)
                               1284                 :            270 :             cmp = range_cmp_bounds(typcache, &upper1, &upper2);
                               1285                 :                :     }
                               1286                 :                : 
                               1287         [ +  + ]:           9354 :     PG_FREE_IF_COPY(r1, 0);
                               1288         [ +  + ]:           9354 :     PG_FREE_IF_COPY(r2, 1);
                               1289                 :                : 
                               1290                 :           9354 :     PG_RETURN_INT32(cmp);
                               1291                 :                : }
                               1292                 :                : 
                               1293                 :                : /* Sort support strategy routine */
                               1294                 :                : Datum
  157                          1295                 :            842 : range_sortsupport(PG_FUNCTION_ARGS)
                               1296                 :                : {
                               1297                 :            842 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
                               1298                 :                : 
                               1299                 :            842 :     ssup->comparator = range_fast_cmp;
                               1300                 :            842 :     ssup->ssup_extra = NULL;
                               1301                 :                : 
                               1302                 :            842 :     PG_RETURN_VOID();
                               1303                 :                : }
                               1304                 :                : 
                               1305                 :                : /* like range_cmp, but uses the new sortsupport interface */
                               1306                 :                : static int
                               1307                 :         270356 : range_fast_cmp(Datum a, Datum b, SortSupport ssup)
                               1308                 :                : {
                               1309                 :         270356 :     RangeType  *range_a = DatumGetRangeTypeP(a);
                               1310                 :         270356 :     RangeType  *range_b = DatumGetRangeTypeP(b);
                               1311                 :                :     TypeCacheEntry *typcache;
                               1312                 :                :     RangeBound  lower1,
                               1313                 :                :                 lower2;
                               1314                 :                :     RangeBound  upper1,
                               1315                 :                :                 upper2;
                               1316                 :                :     bool        empty1,
                               1317                 :                :                 empty2;
                               1318                 :                :     int         cmp;
                               1319                 :                : 
                               1320                 :                :     /* cache the range info between calls */
                               1321         [ +  + ]:         270356 :     if (ssup->ssup_extra == NULL)
                               1322                 :                :     {
                               1323         [ -  + ]:            191 :         Assert(RangeTypeGetOid(range_a) == RangeTypeGetOid(range_b));
                               1324                 :            191 :         ssup->ssup_extra =
                               1325                 :            191 :             lookup_type_cache(RangeTypeGetOid(range_a), TYPECACHE_RANGE_INFO);
                               1326                 :                :     }
                               1327                 :         270356 :     typcache = ssup->ssup_extra;
                               1328                 :                : 
                               1329                 :         270356 :     range_deserialize(typcache, range_a, &lower1, &upper1, &empty1);
                               1330                 :         270356 :     range_deserialize(typcache, range_b, &lower2, &upper2, &empty2);
                               1331                 :                : 
                               1332                 :                :     /* For b-tree use, empty ranges sort before all else */
                               1333   [ +  +  +  + ]:         270356 :     if (empty1 && empty2)
                               1334                 :          38040 :         cmp = 0;
                               1335         [ +  + ]:         232316 :     else if (empty1)
                               1336                 :           9162 :         cmp = -1;
                               1337         [ +  + ]:         223154 :     else if (empty2)
                               1338                 :            732 :         cmp = 1;
                               1339                 :                :     else
                               1340                 :                :     {
                               1341                 :         222422 :         cmp = range_cmp_bounds(typcache, &lower1, &lower2);
                               1342         [ +  + ]:         222422 :         if (cmp == 0)
                               1343                 :          16155 :             cmp = range_cmp_bounds(typcache, &upper1, &upper2);
                               1344                 :                :     }
                               1345                 :                : 
   29 tgl@sss.pgh.pa.us        1346         [ +  - ]:GNC      270356 :     if ((Pointer) range_a != DatumGetPointer(a))
  157 heikki.linnakangas@i     1347                 :CBC      270356 :         pfree(range_a);
   29 tgl@sss.pgh.pa.us        1348         [ +  - ]:GNC      270356 :     if ((Pointer) range_b != DatumGetPointer(b))
  157 heikki.linnakangas@i     1349                 :CBC      270356 :         pfree(range_b);
                               1350                 :                : 
                               1351                 :         270356 :     return cmp;
                               1352                 :                : }
                               1353                 :                : 
                               1354                 :                : 
                               1355                 :                : /* inequality operators using the range_cmp function */
                               1356                 :                : Datum
 5056                          1357                 :            669 : range_lt(PG_FUNCTION_ARGS)
                               1358                 :                : {
   29 peter@eisentraut.org     1359                 :GNC         669 :     int         cmp = DatumGetInt32(range_cmp(fcinfo));
                               1360                 :                : 
 5056 heikki.linnakangas@i     1361                 :CBC         669 :     PG_RETURN_BOOL(cmp < 0);
                               1362                 :                : }
                               1363                 :                : 
                               1364                 :                : Datum
                               1365                 :           1506 : range_le(PG_FUNCTION_ARGS)
                               1366                 :                : {
   29 peter@eisentraut.org     1367                 :GNC        1506 :     int         cmp = DatumGetInt32(range_cmp(fcinfo));
                               1368                 :                : 
 5056 heikki.linnakangas@i     1369                 :CBC        1506 :     PG_RETURN_BOOL(cmp <= 0);
                               1370                 :                : }
                               1371                 :                : 
                               1372                 :                : Datum
                               1373                 :           1518 : range_ge(PG_FUNCTION_ARGS)
                               1374                 :                : {
   29 peter@eisentraut.org     1375                 :GNC        1518 :     int         cmp = DatumGetInt32(range_cmp(fcinfo));
                               1376                 :                : 
 5056 heikki.linnakangas@i     1377                 :CBC        1518 :     PG_RETURN_BOOL(cmp >= 0);
                               1378                 :                : }
                               1379                 :                : 
                               1380                 :                : Datum
                               1381                 :           1536 : range_gt(PG_FUNCTION_ARGS)
                               1382                 :                : {
   29 peter@eisentraut.org     1383                 :GNC        1536 :     int         cmp = DatumGetInt32(range_cmp(fcinfo));
                               1384                 :                : 
 5056 heikki.linnakangas@i     1385                 :CBC        1536 :     PG_RETURN_BOOL(cmp > 0);
                               1386                 :                : }
                               1387                 :                : 
                               1388                 :                : /* Hash support */
                               1389                 :                : 
                               1390                 :                : /* hash a range value */
                               1391                 :                : Datum
                               1392                 :            105 : hash_range(PG_FUNCTION_ARGS)
                               1393                 :                : {
 2910 tgl@sss.pgh.pa.us        1394                 :            105 :     RangeType  *r = PG_GETARG_RANGE_P(0);
                               1395                 :                :     uint32      result;
                               1396                 :                :     TypeCacheEntry *typcache;
                               1397                 :                :     TypeCacheEntry *scache;
                               1398                 :                :     RangeBound  lower;
                               1399                 :                :     RangeBound  upper;
                               1400                 :                :     bool        empty;
                               1401                 :                :     char        flags;
                               1402                 :                :     uint32      lower_hash;
                               1403                 :                :     uint32      upper_hash;
                               1404                 :                : 
 3624 noah@leadboat.com        1405                 :            105 :     check_stack_depth();        /* recurses when subtype is a range type */
                               1406                 :                : 
 5044 tgl@sss.pgh.pa.us        1407                 :            105 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                               1408                 :                : 
                               1409                 :                :     /* deserialize */
                               1410                 :            105 :     range_deserialize(typcache, r, &lower, &upper, &empty);
                               1411                 :            105 :     flags = range_get_flags(r);
                               1412                 :                : 
                               1413                 :                :     /*
                               1414                 :                :      * Look up the element type's hash function, if not done already.
                               1415                 :                :      */
                               1416                 :            105 :     scache = typcache->rngelemtype;
                               1417         [ +  + ]:            105 :     if (!OidIsValid(scache->hash_proc_finfo.fn_oid))
                               1418                 :                :     {
                               1419                 :              3 :         scache = lookup_type_cache(scache->type_id, TYPECACHE_HASH_PROC_FINFO);
                               1420         [ -  + ]:              3 :         if (!OidIsValid(scache->hash_proc_finfo.fn_oid))
 5056 heikki.linnakangas@i     1421         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1422                 :                :                     (errcode(ERRCODE_UNDEFINED_FUNCTION),
                               1423                 :                :                      errmsg("could not identify a hash function for type %s",
                               1424                 :                :                             format_type_be(scache->type_id))));
                               1425                 :                :     }
                               1426                 :                : 
                               1427                 :                :     /*
                               1428                 :                :      * Apply the hash function to each bound.
                               1429                 :                :      */
 5056 heikki.linnakangas@i     1430         [ +  + ]:CBC         105 :     if (RANGE_HAS_LBOUND(flags))
 5044 tgl@sss.pgh.pa.us        1431                 :             72 :         lower_hash = DatumGetUInt32(FunctionCall1Coll(&scache->hash_proc_finfo,
                               1432                 :                :                                                       typcache->rng_collation,
                               1433                 :                :                                                       lower.val));
                               1434                 :                :     else
                               1435                 :             33 :         lower_hash = 0;
                               1436                 :                : 
 5056 heikki.linnakangas@i     1437         [ +  + ]:            105 :     if (RANGE_HAS_UBOUND(flags))
 5044 tgl@sss.pgh.pa.us        1438                 :             78 :         upper_hash = DatumGetUInt32(FunctionCall1Coll(&scache->hash_proc_finfo,
                               1439                 :                :                                                       typcache->rng_collation,
                               1440                 :                :                                                       upper.val));
                               1441                 :                :     else
                               1442                 :             27 :         upper_hash = 0;
                               1443                 :                : 
                               1444                 :                :     /* Merge hashes of flags and bounds */
   32 peter@eisentraut.org     1445                 :GNC         105 :     result = hash_bytes_uint32((uint32) flags);
 5056 heikki.linnakangas@i     1446                 :CBC         105 :     result ^= lower_hash;
 1294 john.naylor@postgres     1447                 :            105 :     result = pg_rotate_left32(result, 1);
 5056 heikki.linnakangas@i     1448                 :            105 :     result ^= upper_hash;
                               1449                 :                : 
                               1450                 :            105 :     PG_RETURN_INT32(result);
                               1451                 :                : }
                               1452                 :                : 
                               1453                 :                : /*
                               1454                 :                :  * Returns 64-bit value by hashing a value to a 64-bit value, with a seed.
                               1455                 :                :  * Otherwise, similar to hash_range.
                               1456                 :                :  */
                               1457                 :                : Datum
 2928 rhaas@postgresql.org     1458                 :             30 : hash_range_extended(PG_FUNCTION_ARGS)
                               1459                 :                : {
 2910 tgl@sss.pgh.pa.us        1460                 :             30 :     RangeType  *r = PG_GETARG_RANGE_P(0);
 2927 rhaas@postgresql.org     1461                 :             30 :     Datum       seed = PG_GETARG_DATUM(1);
                               1462                 :                :     uint64      result;
                               1463                 :                :     TypeCacheEntry *typcache;
                               1464                 :                :     TypeCacheEntry *scache;
                               1465                 :                :     RangeBound  lower;
                               1466                 :                :     RangeBound  upper;
                               1467                 :                :     bool        empty;
                               1468                 :                :     char        flags;
                               1469                 :                :     uint64      lower_hash;
                               1470                 :                :     uint64      upper_hash;
                               1471                 :                : 
 2928                          1472                 :             30 :     check_stack_depth();
                               1473                 :                : 
                               1474                 :             30 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                               1475                 :                : 
                               1476                 :             30 :     range_deserialize(typcache, r, &lower, &upper, &empty);
                               1477                 :             30 :     flags = range_get_flags(r);
                               1478                 :                : 
                               1479                 :             30 :     scache = typcache->rngelemtype;
                               1480         [ -  + ]:             30 :     if (!OidIsValid(scache->hash_extended_proc_finfo.fn_oid))
                               1481                 :                :     {
 2928 rhaas@postgresql.org     1482                 :UBC           0 :         scache = lookup_type_cache(scache->type_id,
                               1483                 :                :                                    TYPECACHE_HASH_EXTENDED_PROC_FINFO);
                               1484         [ #  # ]:              0 :         if (!OidIsValid(scache->hash_extended_proc_finfo.fn_oid))
                               1485         [ #  # ]:              0 :             ereport(ERROR,
                               1486                 :                :                     (errcode(ERRCODE_UNDEFINED_FUNCTION),
                               1487                 :                :                      errmsg("could not identify a hash function for type %s",
                               1488                 :                :                             format_type_be(scache->type_id))));
                               1489                 :                :     }
                               1490                 :                : 
 2928 rhaas@postgresql.org     1491         [ +  - ]:CBC          30 :     if (RANGE_HAS_LBOUND(flags))
                               1492                 :             30 :         lower_hash = DatumGetUInt64(FunctionCall2Coll(&scache->hash_extended_proc_finfo,
                               1493                 :                :                                                       typcache->rng_collation,
                               1494                 :                :                                                       lower.val,
                               1495                 :                :                                                       seed));
                               1496                 :                :     else
 2928 rhaas@postgresql.org     1497                 :UBC           0 :         lower_hash = 0;
                               1498                 :                : 
 2928 rhaas@postgresql.org     1499         [ +  - ]:CBC          30 :     if (RANGE_HAS_UBOUND(flags))
                               1500                 :             30 :         upper_hash = DatumGetUInt64(FunctionCall2Coll(&scache->hash_extended_proc_finfo,
                               1501                 :                :                                                       typcache->rng_collation,
                               1502                 :                :                                                       upper.val,
                               1503                 :                :                                                       seed));
                               1504                 :                :     else
 2928 rhaas@postgresql.org     1505                 :UBC           0 :         upper_hash = 0;
                               1506                 :                : 
                               1507                 :                :     /* Merge hashes of flags and bounds */
 2927 rhaas@postgresql.org     1508                 :CBC          30 :     result = DatumGetUInt64(hash_uint32_extended((uint32) flags,
                               1509                 :             30 :                                                  DatumGetInt64(seed)));
 2928                          1510                 :             30 :     result ^= lower_hash;
                               1511                 :             30 :     result = ROTATE_HIGH_AND_LOW_32BITS(result);
                               1512                 :             30 :     result ^= upper_hash;
                               1513                 :                : 
                               1514                 :             30 :     PG_RETURN_UINT64(result);
                               1515                 :                : }
                               1516                 :                : 
                               1517                 :                : /*
                               1518                 :                :  *----------------------------------------------------------
                               1519                 :                :  * CANONICAL FUNCTIONS
                               1520                 :                :  *
                               1521                 :                :  *   Functions for specific built-in range types.
                               1522                 :                :  *----------------------------------------------------------
                               1523                 :                :  */
                               1524                 :                : 
                               1525                 :                : Datum
 5056 heikki.linnakangas@i     1526                 :         222039 : int4range_canonical(PG_FUNCTION_ARGS)
                               1527                 :                : {
 2910 tgl@sss.pgh.pa.us        1528                 :         222039 :     RangeType  *r = PG_GETARG_RANGE_P(0);
  996                          1529                 :         222039 :     Node       *escontext = fcinfo->context;
                               1530                 :                :     TypeCacheEntry *typcache;
                               1531                 :                :     RangeBound  lower;
                               1532                 :                :     RangeBound  upper;
                               1533                 :                :     bool        empty;
                               1534                 :                : 
 5044                          1535                 :         222039 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                               1536                 :                : 
                               1537                 :         222039 :     range_deserialize(typcache, r, &lower, &upper, &empty);
                               1538                 :                : 
 5056 heikki.linnakangas@i     1539         [ -  + ]:         222039 :     if (empty)
 2910 tgl@sss.pgh.pa.us        1540                 :UBC           0 :         PG_RETURN_RANGE_P(r);
                               1541                 :                : 
 5056 heikki.linnakangas@i     1542   [ +  +  +  + ]:CBC      222039 :     if (!lower.infinite && !lower.inclusive)
                               1543                 :                :     {
  996 tgl@sss.pgh.pa.us        1544                 :           1624 :         int32       bnd = DatumGetInt32(lower.val);
                               1545                 :                : 
                               1546                 :                :         /* Handle possible overflow manually */
                               1547         [ -  + ]:           1624 :         if (unlikely(bnd == PG_INT32_MAX))
  996 tgl@sss.pgh.pa.us        1548         [ #  # ]:UBC           0 :             ereturn(escontext, (Datum) 0,
                               1549                 :                :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1550                 :                :                      errmsg("integer out of range")));
  996 tgl@sss.pgh.pa.us        1551                 :CBC        1624 :         lower.val = Int32GetDatum(bnd + 1);
 5056 heikki.linnakangas@i     1552                 :           1624 :         lower.inclusive = true;
                               1553                 :                :     }
                               1554                 :                : 
                               1555   [ +  +  +  + ]:         222039 :     if (!upper.infinite && upper.inclusive)
                               1556                 :                :     {
  996 tgl@sss.pgh.pa.us        1557                 :           1621 :         int32       bnd = DatumGetInt32(upper.val);
                               1558                 :                : 
                               1559                 :                :         /* Handle possible overflow manually */
                               1560         [ +  + ]:           1621 :         if (unlikely(bnd == PG_INT32_MAX))
                               1561         [ +  + ]:              6 :             ereturn(escontext, (Datum) 0,
                               1562                 :                :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1563                 :                :                      errmsg("integer out of range")));
                               1564                 :           1615 :         upper.val = Int32GetDatum(bnd + 1);
 5056 heikki.linnakangas@i     1565                 :           1615 :         upper.inclusive = false;
                               1566                 :                :     }
                               1567                 :                : 
  996 tgl@sss.pgh.pa.us        1568                 :         222033 :     PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper,
                               1569                 :                :                                       false, escontext));
                               1570                 :                : }
                               1571                 :                : 
                               1572                 :                : Datum
 5056 heikki.linnakangas@i     1573                 :             49 : int8range_canonical(PG_FUNCTION_ARGS)
                               1574                 :                : {
 2910 tgl@sss.pgh.pa.us        1575                 :             49 :     RangeType  *r = PG_GETARG_RANGE_P(0);
  996                          1576                 :             49 :     Node       *escontext = fcinfo->context;
                               1577                 :                :     TypeCacheEntry *typcache;
                               1578                 :                :     RangeBound  lower;
                               1579                 :                :     RangeBound  upper;
                               1580                 :                :     bool        empty;
                               1581                 :                : 
 5044                          1582                 :             49 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                               1583                 :                : 
                               1584                 :             49 :     range_deserialize(typcache, r, &lower, &upper, &empty);
                               1585                 :                : 
 5056 heikki.linnakangas@i     1586         [ -  + ]:             49 :     if (empty)
 2910 tgl@sss.pgh.pa.us        1587                 :UBC           0 :         PG_RETURN_RANGE_P(r);
                               1588                 :                : 
 5056 heikki.linnakangas@i     1589   [ +  -  +  + ]:CBC          49 :     if (!lower.infinite && !lower.inclusive)
                               1590                 :                :     {
  996 tgl@sss.pgh.pa.us        1591                 :              9 :         int64       bnd = DatumGetInt64(lower.val);
                               1592                 :                : 
                               1593                 :                :         /* Handle possible overflow manually */
                               1594         [ -  + ]:              9 :         if (unlikely(bnd == PG_INT64_MAX))
  996 tgl@sss.pgh.pa.us        1595         [ #  # ]:UBC           0 :             ereturn(escontext, (Datum) 0,
                               1596                 :                :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1597                 :                :                      errmsg("bigint out of range")));
  996 tgl@sss.pgh.pa.us        1598                 :CBC           9 :         lower.val = Int64GetDatum(bnd + 1);
 5056 heikki.linnakangas@i     1599                 :              9 :         lower.inclusive = true;
                               1600                 :                :     }
                               1601                 :                : 
                               1602   [ +  -  +  + ]:             49 :     if (!upper.infinite && upper.inclusive)
                               1603                 :                :     {
  996 tgl@sss.pgh.pa.us        1604                 :             12 :         int64       bnd = DatumGetInt64(upper.val);
                               1605                 :                : 
                               1606                 :                :         /* Handle possible overflow manually */
                               1607         [ -  + ]:             12 :         if (unlikely(bnd == PG_INT64_MAX))
  996 tgl@sss.pgh.pa.us        1608         [ #  # ]:UBC           0 :             ereturn(escontext, (Datum) 0,
                               1609                 :                :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1610                 :                :                      errmsg("bigint out of range")));
  996 tgl@sss.pgh.pa.us        1611                 :CBC          12 :         upper.val = Int64GetDatum(bnd + 1);
 5056 heikki.linnakangas@i     1612                 :             12 :         upper.inclusive = false;
                               1613                 :                :     }
                               1614                 :                : 
  996 tgl@sss.pgh.pa.us        1615                 :             49 :     PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper,
                               1616                 :                :                                       false, escontext));
                               1617                 :                : }
                               1618                 :                : 
                               1619                 :                : Datum
 5056 heikki.linnakangas@i     1620                 :           1764 : daterange_canonical(PG_FUNCTION_ARGS)
                               1621                 :                : {
 2910 tgl@sss.pgh.pa.us        1622                 :           1764 :     RangeType  *r = PG_GETARG_RANGE_P(0);
  996                          1623                 :           1764 :     Node       *escontext = fcinfo->context;
                               1624                 :                :     TypeCacheEntry *typcache;
                               1625                 :                :     RangeBound  lower;
                               1626                 :                :     RangeBound  upper;
                               1627                 :                :     bool        empty;
                               1628                 :                : 
 5044                          1629                 :           1764 :     typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
                               1630                 :                : 
                               1631                 :           1764 :     range_deserialize(typcache, r, &lower, &upper, &empty);
                               1632                 :                : 
 5056 heikki.linnakangas@i     1633         [ -  + ]:           1764 :     if (empty)
 2910 tgl@sss.pgh.pa.us        1634                 :UBC           0 :         PG_RETURN_RANGE_P(r);
                               1635                 :                : 
 2242 jdavis@postgresql.or     1636   [ +  +  +  +  :CBC        1764 :     if (!lower.infinite && !DATE_NOT_FINITE(DatumGetDateADT(lower.val)) &&
                                              +  - ]
                               1637         [ +  + ]:           1740 :         !lower.inclusive)
                               1638                 :                :     {
  996 tgl@sss.pgh.pa.us        1639                 :             18 :         DateADT     bnd = DatumGetDateADT(lower.val);
                               1640                 :                : 
                               1641                 :                :         /* Check for overflow -- note we already eliminated PG_INT32_MAX */
                               1642                 :             18 :         bnd++;
                               1643   [ +  -  -  +  :             18 :         if (unlikely(!IS_VALID_DATE(bnd)))
                                              -  + ]
  996 tgl@sss.pgh.pa.us        1644         [ #  # ]:UBC           0 :             ereturn(escontext, (Datum) 0,
                               1645                 :                :                     (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                               1646                 :                :                      errmsg("date out of range")));
  996 tgl@sss.pgh.pa.us        1647                 :CBC          18 :         lower.val = DateADTGetDatum(bnd);
 5056 heikki.linnakangas@i     1648                 :             18 :         lower.inclusive = true;
                               1649                 :                :     }
                               1650                 :                : 
 2242 jdavis@postgresql.or     1651   [ +  +  +  -  :           1764 :     if (!upper.infinite && !DATE_NOT_FINITE(DatumGetDateADT(upper.val)) &&
                                              +  + ]
                               1652         [ +  + ]:           1677 :         upper.inclusive)
                               1653                 :                :     {
  996 tgl@sss.pgh.pa.us        1654                 :             18 :         DateADT     bnd = DatumGetDateADT(upper.val);
                               1655                 :                : 
                               1656                 :                :         /* Check for overflow -- note we already eliminated PG_INT32_MAX */
                               1657                 :             18 :         bnd++;
                               1658   [ +  -  +  +  :             18 :         if (unlikely(!IS_VALID_DATE(bnd)))
                                              +  + ]
                               1659         [ +  + ]:              6 :             ereturn(escontext, (Datum) 0,
                               1660                 :                :                     (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                               1661                 :                :                      errmsg("date out of range")));
                               1662                 :             12 :         upper.val = DateADTGetDatum(bnd);
 5056 heikki.linnakangas@i     1663                 :             12 :         upper.inclusive = false;
                               1664                 :                :     }
                               1665                 :                : 
  996 tgl@sss.pgh.pa.us        1666                 :           1758 :     PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper,
                               1667                 :                :                                       false, escontext));
                               1668                 :                : }
                               1669                 :                : 
                               1670                 :                : /*
                               1671                 :                :  *----------------------------------------------------------
                               1672                 :                :  * SUBTYPE_DIFF FUNCTIONS
                               1673                 :                :  *
                               1674                 :                :  * Functions for specific built-in range types.
                               1675                 :                :  *
                               1676                 :                :  * Note that subtype_diff does return the difference, not the absolute value
                               1677                 :                :  * of the difference, and it must take care to avoid overflow.
                               1678                 :                :  * (numrange_subdiff is at some risk there ...)
                               1679                 :                :  *----------------------------------------------------------
                               1680                 :                :  */
                               1681                 :                : 
                               1682                 :                : Datum
 5056 heikki.linnakangas@i     1683                 :         428031 : int4range_subdiff(PG_FUNCTION_ARGS)
                               1684                 :                : {
 5045 bruce@momjian.us         1685                 :         428031 :     int32       v1 = PG_GETARG_INT32(0);
                               1686                 :         428031 :     int32       v2 = PG_GETARG_INT32(1);
                               1687                 :                : 
      tgl@sss.pgh.pa.us        1688                 :         428031 :     PG_RETURN_FLOAT8((float8) v1 - (float8) v2);
                               1689                 :                : }
                               1690                 :                : 
                               1691                 :                : Datum
 5056 heikki.linnakangas@i     1692                 :UBC           0 : int8range_subdiff(PG_FUNCTION_ARGS)
                               1693                 :                : {
 5045 bruce@momjian.us         1694                 :              0 :     int64       v1 = PG_GETARG_INT64(0);
                               1695                 :              0 :     int64       v2 = PG_GETARG_INT64(1);
                               1696                 :                : 
      tgl@sss.pgh.pa.us        1697                 :              0 :     PG_RETURN_FLOAT8((float8) v1 - (float8) v2);
                               1698                 :                : }
                               1699                 :                : 
                               1700                 :                : Datum
 5056 heikki.linnakangas@i     1701                 :CBC         123 : numrange_subdiff(PG_FUNCTION_ARGS)
                               1702                 :                : {
                               1703                 :            123 :     Datum       v1 = PG_GETARG_DATUM(0);
                               1704                 :            123 :     Datum       v2 = PG_GETARG_DATUM(1);
                               1705                 :                :     Datum       numresult;
                               1706                 :                :     float8      floatresult;
                               1707                 :                : 
                               1708                 :            123 :     numresult = DirectFunctionCall2(numeric_sub, v1, v2);
                               1709                 :                : 
 5045 tgl@sss.pgh.pa.us        1710                 :            123 :     floatresult = DatumGetFloat8(DirectFunctionCall1(numeric_float8,
                               1711                 :                :                                                      numresult));
                               1712                 :                : 
 5056 heikki.linnakangas@i     1713                 :            123 :     PG_RETURN_FLOAT8(floatresult);
                               1714                 :                : }
                               1715                 :                : 
                               1716                 :                : Datum
 5045 tgl@sss.pgh.pa.us        1717                 :UBC           0 : daterange_subdiff(PG_FUNCTION_ARGS)
                               1718                 :                : {
                               1719                 :              0 :     int32       v1 = PG_GETARG_INT32(0);
                               1720                 :              0 :     int32       v2 = PG_GETARG_INT32(1);
                               1721                 :                : 
                               1722                 :              0 :     PG_RETURN_FLOAT8((float8) v1 - (float8) v2);
                               1723                 :                : }
                               1724                 :                : 
                               1725                 :                : Datum
 5056 heikki.linnakangas@i     1726                 :              0 : tsrange_subdiff(PG_FUNCTION_ARGS)
                               1727                 :                : {
                               1728                 :              0 :     Timestamp   v1 = PG_GETARG_TIMESTAMP(0);
                               1729                 :              0 :     Timestamp   v2 = PG_GETARG_TIMESTAMP(1);
                               1730                 :                :     float8      result;
                               1731                 :                : 
 5045 tgl@sss.pgh.pa.us        1732                 :              0 :     result = ((float8) v1 - (float8) v2) / USECS_PER_SEC;
 5056 heikki.linnakangas@i     1733                 :              0 :     PG_RETURN_FLOAT8(result);
                               1734                 :                : }
                               1735                 :                : 
                               1736                 :                : Datum
                               1737                 :              0 : tstzrange_subdiff(PG_FUNCTION_ARGS)
                               1738                 :                : {
                               1739                 :              0 :     Timestamp   v1 = PG_GETARG_TIMESTAMP(0);
                               1740                 :              0 :     Timestamp   v2 = PG_GETARG_TIMESTAMP(1);
                               1741                 :                :     float8      result;
                               1742                 :                : 
 5045 tgl@sss.pgh.pa.us        1743                 :              0 :     result = ((float8) v1 - (float8) v2) / USECS_PER_SEC;
 5056 heikki.linnakangas@i     1744                 :              0 :     PG_RETURN_FLOAT8(result);
                               1745                 :                : }
                               1746                 :                : 
                               1747                 :                : /*
                               1748                 :                :  *----------------------------------------------------------
                               1749                 :                :  * SUPPORT FUNCTIONS
                               1750                 :                :  *
                               1751                 :                :  *   These functions aren't in pg_proc, but are useful for
                               1752                 :                :  *   defining new generic range functions in C.
                               1753                 :                :  *----------------------------------------------------------
                               1754                 :                :  */
                               1755                 :                : 
                               1756                 :                : /*
                               1757                 :                :  * range_get_typcache: get cached information about a range type
                               1758                 :                :  *
                               1759                 :                :  * This is for use by range-related functions that follow the convention
                               1760                 :                :  * of using the fn_extra field as a pointer to the type cache entry for
                               1761                 :                :  * the range type.  Functions that need to cache more information than
                               1762                 :                :  * that must fend for themselves.
                               1763                 :                :  */
                               1764                 :                : TypeCacheEntry *
 5044 tgl@sss.pgh.pa.us        1765                 :CBC     2070237 : range_get_typcache(FunctionCallInfo fcinfo, Oid rngtypid)
                               1766                 :                : {
                               1767                 :        2070237 :     TypeCacheEntry *typcache = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
                               1768                 :                : 
                               1769         [ +  + ]:        2070237 :     if (typcache == NULL ||
                               1770         [ -  + ]:        2061215 :         typcache->type_id != rngtypid)
                               1771                 :                :     {
                               1772                 :           9022 :         typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
                               1773         [ -  + ]:           9022 :         if (typcache->rngelemtype == NULL)
 5044 tgl@sss.pgh.pa.us        1774         [ #  # ]:UBC           0 :             elog(ERROR, "type %u is not a range type", rngtypid);
  282 peter@eisentraut.org     1775                 :CBC        9022 :         fcinfo->flinfo->fn_extra = typcache;
                               1776                 :                :     }
                               1777                 :                : 
 5044 tgl@sss.pgh.pa.us        1778                 :        2070237 :     return typcache;
                               1779                 :                : }
                               1780                 :                : 
                               1781                 :                : /*
                               1782                 :                :  * range_serialize: construct a range value from bounds and empty-flag
                               1783                 :                :  *
                               1784                 :                :  * This does not force canonicalization of the range value.  In most cases,
                               1785                 :                :  * external callers should only be canonicalization functions.  Note that
                               1786                 :                :  * we perform some datatype-independent canonicalization checks anyway.
                               1787                 :                :  */
                               1788                 :                : RangeType *
                               1789                 :         453989 : range_serialize(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper,
                               1790                 :                :                 bool empty, struct Node *escontext)
                               1791                 :                : {
                               1792                 :                :     RangeType  *range;
                               1793                 :                :     int         cmp;
                               1794                 :                :     Size        msize;
                               1795                 :                :     Pointer     ptr;
                               1796                 :                :     int16       typlen;
                               1797                 :                :     bool        typbyval;
                               1798                 :                :     char        typalign;
                               1799                 :                :     char        typstorage;
 5045 bruce@momjian.us         1800                 :         453989 :     char        flags = 0;
                               1801                 :                : 
                               1802                 :                :     /*
                               1803                 :                :      * Verify range is not invalid on its face, and construct flags value,
                               1804                 :                :      * preventing any non-canonical combinations such as infinite+inclusive.
                               1805                 :                :      */
 5037 tgl@sss.pgh.pa.us        1806         [ -  + ]:         453989 :     Assert(lower->lower);
                               1807         [ -  + ]:         453989 :     Assert(!upper->lower);
                               1808                 :                : 
 5056 heikki.linnakangas@i     1809         [ +  + ]:         453989 :     if (empty)
                               1810                 :           1875 :         flags |= RANGE_EMPTY;
                               1811                 :                :     else
                               1812                 :                :     {
 5037 tgl@sss.pgh.pa.us        1813                 :         452114 :         cmp = range_cmp_bound_values(typcache, lower, upper);
                               1814                 :                : 
                               1815                 :                :         /* error check: if lower bound value is above upper, it's wrong */
                               1816         [ +  + ]:         452114 :         if (cmp > 0)
  996                          1817         [ +  + ]:             33 :             ereturn(escontext, NULL,
                               1818                 :                :                     (errcode(ERRCODE_DATA_EXCEPTION),
                               1819                 :                :                      errmsg("range lower bound must be less than or equal to range upper bound")));
                               1820                 :                : 
                               1821                 :                :         /* if bounds are equal, and not both inclusive, range is empty */
 5037                          1822   [ +  +  +  +  :         452081 :         if (cmp == 0 && !(lower->inclusive && upper->inclusive))
                                              +  + ]
                               1823                 :            192 :             flags |= RANGE_EMPTY;
                               1824                 :                :         else
                               1825                 :                :         {
                               1826                 :                :             /* infinite boundaries are never inclusive */
                               1827         [ +  + ]:         451889 :             if (lower->infinite)
                               1828                 :           5536 :                 flags |= RANGE_LB_INF;
                               1829         [ +  + ]:         446353 :             else if (lower->inclusive)
                               1830                 :         444537 :                 flags |= RANGE_LB_INC;
                               1831         [ +  + ]:         451889 :             if (upper->infinite)
                               1832                 :           3533 :                 flags |= RANGE_UB_INF;
                               1833         [ +  + ]:         448356 :             else if (upper->inclusive)
                               1834                 :           2063 :                 flags |= RANGE_UB_INC;
                               1835                 :                :         }
                               1836                 :                :     }
                               1837                 :                : 
                               1838                 :                :     /* Fetch information about range's element type */
                               1839                 :         453956 :     typlen = typcache->rngelemtype->typlen;
                               1840                 :         453956 :     typbyval = typcache->rngelemtype->typbyval;
                               1841                 :         453956 :     typalign = typcache->rngelemtype->typalign;
                               1842                 :         453956 :     typstorage = typcache->rngelemtype->typstorage;
                               1843                 :                : 
                               1844                 :                :     /* Count space for varlena header and range type's OID */
 5044                          1845                 :         453956 :     msize = sizeof(RangeType);
                               1846         [ -  + ]:         453956 :     Assert(msize == MAXALIGN(msize));
                               1847                 :                : 
                               1848                 :                :     /* Count space for bounds */
 5056 heikki.linnakangas@i     1849         [ +  + ]:         453956 :     if (RANGE_HAS_LBOUND(flags))
                               1850                 :                :     {
                               1851                 :                :         /*
                               1852                 :                :          * Make sure item to be inserted is not toasted.  It is essential that
                               1853                 :                :          * we not insert an out-of-line toast value pointer into a range
                               1854                 :                :          * object, for the same reasons that arrays and records can't contain
                               1855                 :                :          * them.  It would work to store a compressed-in-line value, but we
                               1856                 :                :          * prefer to decompress and then let compression be applied to the
                               1857                 :                :          * whole range object if necessary.  But, unlike arrays, we do allow
                               1858                 :                :          * short-header varlena objects to stay as-is.
                               1859                 :                :          */
 5045 tgl@sss.pgh.pa.us        1860         [ +  + ]:         446353 :         if (typlen == -1)
                               1861                 :           2380 :             lower->val = PointerGetDatum(PG_DETOAST_DATUM_PACKED(lower->val));
                               1862                 :                : 
 5056 heikki.linnakangas@i     1863                 :         446353 :         msize = datum_compute_size(msize, lower->val, typbyval, typalign,
                               1864                 :                :                                    typlen, typstorage);
                               1865                 :                :     }
                               1866                 :                : 
                               1867         [ +  + ]:         453956 :     if (RANGE_HAS_UBOUND(flags))
                               1868                 :                :     {
                               1869                 :                :         /* Make sure item to be inserted is not toasted */
 5045 tgl@sss.pgh.pa.us        1870         [ +  + ]:         448356 :         if (typlen == -1)
                               1871                 :           2362 :             upper->val = PointerGetDatum(PG_DETOAST_DATUM_PACKED(upper->val));
                               1872                 :                : 
 5056 heikki.linnakangas@i     1873                 :         448356 :         msize = datum_compute_size(msize, upper->val, typbyval, typalign,
                               1874                 :                :                                    typlen, typstorage);
                               1875                 :                :     }
                               1876                 :                : 
                               1877                 :                :     /* Add space for flag byte */
                               1878                 :         453956 :     msize += sizeof(char);
                               1879                 :                : 
                               1880                 :                :     /* Note: zero-fill is required here, just as in heap tuples */
 5044 tgl@sss.pgh.pa.us        1881                 :         453956 :     range = (RangeType *) palloc0(msize);
                               1882                 :         453956 :     SET_VARSIZE(range, msize);
                               1883                 :                : 
                               1884                 :                :     /* Now fill in the datum */
                               1885                 :         453956 :     range->rangetypid = typcache->type_id;
                               1886                 :                : 
                               1887                 :         453956 :     ptr = (char *) (range + 1);
                               1888                 :                : 
 5056 heikki.linnakangas@i     1889         [ +  + ]:         453956 :     if (RANGE_HAS_LBOUND(flags))
                               1890                 :                :     {
                               1891         [ -  + ]:         446353 :         Assert(lower->lower);
                               1892                 :         446353 :         ptr = datum_write(ptr, lower->val, typbyval, typalign, typlen,
                               1893                 :                :                           typstorage);
                               1894                 :                :     }
                               1895                 :                : 
                               1896         [ +  + ]:         453956 :     if (RANGE_HAS_UBOUND(flags))
                               1897                 :                :     {
                               1898         [ -  + ]:         448356 :         Assert(!upper->lower);
                               1899                 :         448356 :         ptr = datum_write(ptr, upper->val, typbyval, typalign, typlen,
                               1900                 :                :                           typstorage);
                               1901                 :                :     }
                               1902                 :                : 
 5044 tgl@sss.pgh.pa.us        1903                 :         453956 :     *((char *) ptr) = flags;
                               1904                 :                : 
                               1905                 :         453956 :     return range;
                               1906                 :                : }
                               1907                 :                : 
                               1908                 :                : /*
                               1909                 :                :  * range_deserialize: deconstruct a range value
                               1910                 :                :  *
                               1911                 :                :  * NB: the given range object must be fully detoasted; it cannot have a
                               1912                 :                :  * short varlena header.
                               1913                 :                :  *
                               1914                 :                :  * Note that if the element type is pass-by-reference, the datums in the
                               1915                 :                :  * RangeBound structs will be pointers into the given range object.
                               1916                 :                :  */
                               1917                 :                : void
 2137 peter@eisentraut.org     1918                 :        4900188 : range_deserialize(TypeCacheEntry *typcache, const RangeType *range,
                               1919                 :                :                   RangeBound *lower, RangeBound *upper, bool *empty)
                               1920                 :                : {
                               1921                 :                :     char        flags;
                               1922                 :                :     int16       typlen;
                               1923                 :                :     bool        typbyval;
                               1924                 :                :     char        typalign;
                               1925                 :                :     Pointer     ptr;
                               1926                 :                :     Datum       lbound;
                               1927                 :                :     Datum       ubound;
                               1928                 :                : 
                               1929                 :                :     /* assert caller passed the right typcache entry */
 5044 tgl@sss.pgh.pa.us        1930         [ -  + ]:        4900188 :     Assert(RangeTypeGetOid(range) == typcache->type_id);
                               1931                 :                : 
                               1932                 :                :     /* fetch the flag byte from datum's last byte */
 2137 peter@eisentraut.org     1933                 :        4900188 :     flags = *((const char *) range + VARSIZE(range) - 1);
                               1934                 :                : 
                               1935                 :                :     /* fetch information about range's element type */
 5044 tgl@sss.pgh.pa.us        1936                 :        4900188 :     typlen = typcache->rngelemtype->typlen;
                               1937                 :        4900188 :     typbyval = typcache->rngelemtype->typbyval;
                               1938                 :        4900188 :     typalign = typcache->rngelemtype->typalign;
                               1939                 :                : 
                               1940                 :                :     /* initialize data pointer just after the range OID */
                               1941                 :        4900188 :     ptr = (Pointer) (range + 1);
                               1942                 :                : 
                               1943                 :                :     /* fetch lower bound, if any */
 5056 heikki.linnakangas@i     1944         [ +  + ]:        4900188 :     if (RANGE_HAS_LBOUND(flags))
                               1945                 :                :     {
                               1946                 :                :         /* att_align_pointer cannot be necessary here */
                               1947                 :        4318433 :         lbound = fetch_att(ptr, typbyval, typlen);
 5045 tgl@sss.pgh.pa.us        1948   [ +  +  +  -  :        4318433 :         ptr = (Pointer) att_addlength_pointer(ptr, typlen, ptr);
                                     -  -  -  -  -  
                                     -  -  -  +  -  
                                              -  - ]
                               1949                 :                :     }
                               1950                 :                :     else
 5056 heikki.linnakangas@i     1951                 :         581755 :         lbound = (Datum) 0;
                               1952                 :                : 
                               1953                 :                :     /* fetch upper bound, if any */
                               1954         [ +  + ]:        4900188 :     if (RANGE_HAS_UBOUND(flags))
                               1955                 :                :     {
                               1956   [ +  +  +  +  :        4330962 :         ptr = (Pointer) att_align_pointer(ptr, typalign, typlen, ptr);
                                     +  +  +  -  +  
                                           -  -  - ]
                               1957                 :        4330962 :         ubound = fetch_att(ptr, typbyval, typlen);
                               1958                 :                :         /* no need for att_addlength_pointer */
                               1959                 :                :     }
                               1960                 :                :     else
                               1961                 :         569226 :         ubound = (Datum) 0;
                               1962                 :                : 
                               1963                 :                :     /* emit results */
                               1964                 :                : 
 5044 tgl@sss.pgh.pa.us        1965                 :        4900188 :     *empty = (flags & RANGE_EMPTY) != 0;
                               1966                 :                : 
 5045 bruce@momjian.us         1967                 :        4900188 :     lower->val = lbound;
      tgl@sss.pgh.pa.us        1968                 :        4900188 :     lower->infinite = (flags & RANGE_LB_INF) != 0;
 5044                          1969                 :        4900188 :     lower->inclusive = (flags & RANGE_LB_INC) != 0;
 5045 bruce@momjian.us         1970                 :        4900188 :     lower->lower = true;
                               1971                 :                : 
                               1972                 :        4900188 :     upper->val = ubound;
      tgl@sss.pgh.pa.us        1973                 :        4900188 :     upper->infinite = (flags & RANGE_UB_INF) != 0;
 5044                          1974                 :        4900188 :     upper->inclusive = (flags & RANGE_UB_INC) != 0;
 5045 bruce@momjian.us         1975                 :        4900188 :     upper->lower = false;
 5056 heikki.linnakangas@i     1976                 :        4900188 : }
                               1977                 :                : 
                               1978                 :                : /*
                               1979                 :                :  * range_get_flags: just get the flags from a RangeType value.
                               1980                 :                :  *
                               1981                 :                :  * This is frequently useful in places that only need the flags and not
                               1982                 :                :  * the full results of range_deserialize.
                               1983                 :                :  */
                               1984                 :                : char
 2137 peter@eisentraut.org     1985                 :        1473151 : range_get_flags(const RangeType *range)
                               1986                 :                : {
                               1987                 :                :     /* fetch the flag byte from datum's last byte */
 5044 tgl@sss.pgh.pa.us        1988                 :        1473151 :     return *((char *) range + VARSIZE(range) - 1);
                               1989                 :                : }
                               1990                 :                : 
                               1991                 :                : /*
                               1992                 :                :  * range_set_contain_empty: set the RANGE_CONTAIN_EMPTY bit in the value.
                               1993                 :                :  *
                               1994                 :                :  * This is only needed in GiST operations, so we don't include a provision
                               1995                 :                :  * for setting it in range_serialize; rather, this function must be applied
                               1996                 :                :  * afterwards.
                               1997                 :                :  */
                               1998                 :                : void
 5032                          1999                 :            309 : range_set_contain_empty(RangeType *range)
                               2000                 :                : {
                               2001                 :                :     char       *flagsp;
                               2002                 :                : 
                               2003                 :                :     /* flag byte is datum's last byte */
                               2004                 :            309 :     flagsp = (char *) range + VARSIZE(range) - 1;
                               2005                 :                : 
                               2006                 :            309 :     *flagsp |= RANGE_CONTAIN_EMPTY;
                               2007                 :            309 : }
                               2008                 :                : 
                               2009                 :                : /*
                               2010                 :                :  * This both serializes and canonicalizes (if applicable) the range.
                               2011                 :                :  * This should be used by most callers.
                               2012                 :                :  */
                               2013                 :                : RangeType *
 5044                          2014                 :         228563 : make_range(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper,
                               2015                 :                :            bool empty, struct Node *escontext)
                               2016                 :                : {
                               2017                 :                :     RangeType  *range;
                               2018                 :                : 
  996                          2019                 :         228563 :     range = range_serialize(typcache, lower, upper, empty, escontext);
                               2020                 :                : 
                               2021   [ +  +  +  -  :         228536 :     if (SOFT_ERROR_OCCURRED(escontext))
                                              +  + ]
                               2022                 :              6 :         return NULL;
                               2023                 :                : 
                               2024                 :                :     /* no need to call canonical on empty ranges ... */
 5037                          2025         [ +  + ]:         228530 :     if (OidIsValid(typcache->rng_canonical_finfo.fn_oid) &&
                               2026         [ +  + ]:         225712 :         !RangeIsEmpty(range))
                               2027                 :                :     {
                               2028                 :                :         /* Do this the hard way so that we can pass escontext */
  996                          2029                 :         223852 :         LOCAL_FCINFO(fcinfo, 1);
                               2030                 :                :         Datum       result;
                               2031                 :                : 
                               2032                 :         223852 :         InitFunctionCallInfoData(*fcinfo, &typcache->rng_canonical_finfo, 1,
                               2033                 :                :                                  InvalidOid, escontext, NULL);
                               2034                 :                : 
                               2035                 :         223852 :         fcinfo->args[0].value = RangeTypePGetDatum(range);
                               2036                 :         223852 :         fcinfo->args[0].isnull = false;
                               2037                 :                : 
                               2038                 :         223852 :         result = FunctionCallInvoke(fcinfo);
                               2039                 :                : 
                               2040   [ +  +  +  -  :         223852 :         if (SOFT_ERROR_OCCURRED(escontext))
                                              +  + ]
                               2041                 :             12 :             return NULL;
                               2042                 :                : 
                               2043                 :                :         /* Should not get a null result if there was no error */
                               2044         [ -  + ]:         223840 :         if (fcinfo->isnull)
  996 tgl@sss.pgh.pa.us        2045         [ #  # ]:UBC           0 :             elog(ERROR, "function %u returned NULL",
                               2046                 :                :                  typcache->rng_canonical_finfo.fn_oid);
                               2047                 :                : 
  996 tgl@sss.pgh.pa.us        2048                 :CBC      223840 :         range = DatumGetRangeTypeP(result);
                               2049                 :                :     }
                               2050                 :                : 
 5044                          2051                 :         228518 :     return range;
                               2052                 :                : }
                               2053                 :                : 
                               2054                 :                : /*
                               2055                 :                :  * Compare two range boundary points, returning <0, 0, or >0 according to
                               2056                 :                :  * whether b1 is less than, equal to, or greater than b2.
                               2057                 :                :  *
                               2058                 :                :  * The boundaries can be any combination of upper and lower; so it's useful
                               2059                 :                :  * for a variety of operators.
                               2060                 :                :  *
                               2061                 :                :  * The simple case is when b1 and b2 are both finite and inclusive, in which
                               2062                 :                :  * case the result is just a comparison of the values held in b1 and b2.
                               2063                 :                :  *
                               2064                 :                :  * If a bound is exclusive, then we need to know whether it's a lower bound,
                               2065                 :                :  * in which case we treat the boundary point as "just greater than" the held
                               2066                 :                :  * value; or an upper bound, in which case we treat the boundary point as
                               2067                 :                :  * "just less than" the held value.
                               2068                 :                :  *
                               2069                 :                :  * If a bound is infinite, it represents minus infinity (less than every other
                               2070                 :                :  * point) if it's a lower bound; or plus infinity (greater than every other
                               2071                 :                :  * point) if it's an upper bound.
                               2072                 :                :  *
                               2073                 :                :  * There is only one case where two boundaries compare equal but are not
                               2074                 :                :  * identical: when both bounds are inclusive and hold the same finite value,
                               2075                 :                :  * but one is an upper bound and the other a lower bound.
                               2076                 :                :  */
                               2077                 :                : int
 2137 peter@eisentraut.org     2078                 :        6283743 : range_cmp_bounds(TypeCacheEntry *typcache, const RangeBound *b1, const RangeBound *b2)
                               2079                 :                : {
                               2080                 :                :     int32       result;
                               2081                 :                : 
                               2082                 :                :     /*
                               2083                 :                :      * First, handle cases involving infinity, which don't require invoking
                               2084                 :                :      * the comparison proc.
                               2085                 :                :      */
 5056 heikki.linnakangas@i     2086   [ +  +  +  + ]:        6283743 :     if (b1->infinite && b2->infinite)
                               2087                 :                :     {
                               2088                 :                :         /*
                               2089                 :                :          * Both are infinity, so they are equal unless one is lower and the
                               2090                 :                :          * other not.
                               2091                 :                :          */
                               2092         [ +  + ]:          10611 :         if (b1->lower == b2->lower)
                               2093                 :          10566 :             return 0;
                               2094                 :                :         else
 5042 tgl@sss.pgh.pa.us        2095         [ +  + ]:             45 :             return b1->lower ? -1 : 1;
                               2096                 :                :     }
                               2097         [ +  + ]:        6273132 :     else if (b1->infinite)
                               2098         [ +  + ]:          49400 :         return b1->lower ? -1 : 1;
                               2099         [ +  + ]:        6223732 :     else if (b2->infinite)
                               2100         [ +  + ]:          15846 :         return b2->lower ? 1 : -1;
                               2101                 :                : 
                               2102                 :                :     /*
                               2103                 :                :      * Both boundaries are finite, so compare the held values.
                               2104                 :                :      */
 5044                          2105                 :        6207886 :     result = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                               2106                 :                :                                              typcache->rng_collation,
 5056 heikki.linnakangas@i     2107                 :        6207886 :                                              b1->val, b2->val));
                               2108                 :                : 
                               2109                 :                :     /*
                               2110                 :                :      * If the comparison is anything other than equal, we're done. If they
                               2111                 :                :      * compare equal though, we still have to consider whether the boundaries
                               2112                 :                :      * are inclusive or exclusive.
                               2113                 :                :      */
                               2114         [ +  + ]:        6207886 :     if (result == 0)
                               2115                 :                :     {
 5042 tgl@sss.pgh.pa.us        2116   [ +  +  +  + ]:         431105 :         if (!b1->inclusive && !b2->inclusive)
                               2117                 :                :         {
                               2118                 :                :             /* both are exclusive */
                               2119         [ +  + ]:         191244 :             if (b1->lower == b2->lower)
                               2120                 :         191241 :                 return 0;
                               2121                 :                :             else
                               2122         [ -  + ]:              3 :                 return b1->lower ? 1 : -1;
                               2123                 :                :         }
                               2124         [ +  + ]:         239861 :         else if (!b1->inclusive)
                               2125         [ -  + ]:            384 :             return b1->lower ? 1 : -1;
                               2126         [ +  + ]:         239477 :         else if (!b2->inclusive)
                               2127         [ +  + ]:            614 :             return b2->lower ? -1 : 1;
                               2128                 :                :         else
                               2129                 :                :         {
                               2130                 :                :             /*
                               2131                 :                :              * Both are inclusive and the values held are equal, so they are
                               2132                 :                :              * equal regardless of whether they are upper or lower boundaries,
                               2133                 :                :              * or a mix.
                               2134                 :                :              */
                               2135                 :         238863 :             return 0;
                               2136                 :                :         }
                               2137                 :                :     }
                               2138                 :                : 
 5056 heikki.linnakangas@i     2139                 :        5776781 :     return result;
                               2140                 :                : }
                               2141                 :                : 
                               2142                 :                : /*
                               2143                 :                :  * Compare two range boundary point values, returning <0, 0, or >0 according
                               2144                 :                :  * to whether b1 is less than, equal to, or greater than b2.
                               2145                 :                :  *
                               2146                 :                :  * This is similar to but simpler than range_cmp_bounds().  We just compare
                               2147                 :                :  * the values held in b1 and b2, ignoring inclusive/exclusive flags.  The
                               2148                 :                :  * lower/upper flags only matter for infinities, where they tell us if the
                               2149                 :                :  * infinity is plus or minus.
                               2150                 :                :  */
                               2151                 :                : int
 2137 peter@eisentraut.org     2152                 :         688173 : range_cmp_bound_values(TypeCacheEntry *typcache, const RangeBound *b1,
                               2153                 :                :                        const RangeBound *b2)
                               2154                 :                : {
                               2155                 :                :     /*
                               2156                 :                :      * First, handle cases involving infinity, which don't require invoking
                               2157                 :                :      * the comparison proc.
                               2158                 :                :      */
 5037 tgl@sss.pgh.pa.us        2159   [ +  +  +  + ]:         688173 :     if (b1->infinite && b2->infinite)
                               2160                 :                :     {
                               2161                 :                :         /*
                               2162                 :                :          * Both are infinity, so they are equal unless one is lower and the
                               2163                 :                :          * other not.
                               2164                 :                :          */
                               2165         [ -  + ]:            157 :         if (b1->lower == b2->lower)
 5037 tgl@sss.pgh.pa.us        2166                 :UBC           0 :             return 0;
                               2167                 :                :         else
 5037 tgl@sss.pgh.pa.us        2168         [ +  + ]:CBC         157 :             return b1->lower ? -1 : 1;
                               2169                 :                :     }
                               2170         [ +  + ]:         688016 :     else if (b1->infinite)
                               2171         [ +  + ]:           8704 :         return b1->lower ? -1 : 1;
                               2172         [ +  + ]:         679312 :     else if (b2->infinite)
                               2173         [ +  + ]:           7060 :         return b2->lower ? 1 : -1;
                               2174                 :                : 
                               2175                 :                :     /*
                               2176                 :                :      * Both boundaries are finite, so compare the held values.
                               2177                 :                :      */
                               2178                 :         672252 :     return DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                               2179                 :                :                                            typcache->rng_collation,
                               2180                 :         672252 :                                            b1->val, b2->val));
                               2181                 :                : }
                               2182                 :                : 
                               2183                 :                : /*
                               2184                 :                :  * qsort callback for sorting ranges.
                               2185                 :                :  *
                               2186                 :                :  * Two empty ranges compare equal; an empty range sorts to the left of any
                               2187                 :                :  * non-empty range.  Two non-empty ranges are sorted by lower bound first
                               2188                 :                :  * and by upper bound next.
                               2189                 :                :  */
                               2190                 :                : int
 1721 akorotkov@postgresql     2191                 :          13456 : range_compare(const void *key1, const void *key2, void *arg)
                               2192                 :                : {
                               2193                 :          13456 :     RangeType  *r1 = *(RangeType **) key1;
                               2194                 :          13456 :     RangeType  *r2 = *(RangeType **) key2;
                               2195                 :          13456 :     TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
                               2196                 :                :     RangeBound  lower1;
                               2197                 :                :     RangeBound  upper1;
                               2198                 :                :     RangeBound  lower2;
                               2199                 :                :     RangeBound  upper2;
                               2200                 :                :     bool        empty1;
                               2201                 :                :     bool        empty2;
                               2202                 :                :     int         cmp;
                               2203                 :                : 
                               2204                 :          13456 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               2205                 :          13456 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               2206                 :                : 
                               2207   [ +  +  +  + ]:          13456 :     if (empty1 && empty2)
                               2208                 :             24 :         cmp = 0;
                               2209         [ +  + ]:          13432 :     else if (empty1)
                               2210                 :             21 :         cmp = -1;
                               2211         [ +  + ]:          13411 :     else if (empty2)
                               2212                 :              6 :         cmp = 1;
                               2213                 :                :     else
                               2214                 :                :     {
                               2215                 :          13405 :         cmp = range_cmp_bounds(typcache, &lower1, &lower2);
                               2216         [ +  + ]:          13405 :         if (cmp == 0)
                               2217                 :             24 :             cmp = range_cmp_bounds(typcache, &upper1, &upper2);
                               2218                 :                :     }
                               2219                 :                : 
                               2220                 :          13456 :     return cmp;
                               2221                 :                : }
                               2222                 :                : 
                               2223                 :                : /*
                               2224                 :                :  * Build an empty range value of the type indicated by the typcache entry.
                               2225                 :                :  */
                               2226                 :                : RangeType *
 5044 tgl@sss.pgh.pa.us        2227                 :           1581 : make_empty_range(TypeCacheEntry *typcache)
                               2228                 :                : {
                               2229                 :                :     RangeBound  lower;
                               2230                 :                :     RangeBound  upper;
                               2231                 :                : 
                               2232                 :           1581 :     lower.val = (Datum) 0;
                               2233                 :           1581 :     lower.infinite = false;
                               2234                 :           1581 :     lower.inclusive = false;
 5056 heikki.linnakangas@i     2235                 :           1581 :     lower.lower = true;
                               2236                 :                : 
 5044 tgl@sss.pgh.pa.us        2237                 :           1581 :     upper.val = (Datum) 0;
                               2238                 :           1581 :     upper.infinite = false;
                               2239                 :           1581 :     upper.inclusive = false;
 5056 heikki.linnakangas@i     2240                 :           1581 :     upper.lower = false;
                               2241                 :                : 
  996 tgl@sss.pgh.pa.us        2242                 :           1581 :     return make_range(typcache, &lower, &upper, true, NULL);
                               2243                 :                : }
                               2244                 :                : 
                               2245                 :                : /*
                               2246                 :                :  * Planner support function for elem_contained_by_range (<@ operator).
                               2247                 :                :  */
                               2248                 :                : Datum
  595                          2249                 :             63 : elem_contained_by_range_support(PG_FUNCTION_ARGS)
                               2250                 :                : {
                               2251                 :             63 :     Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
                               2252                 :             63 :     Node       *ret = NULL;
                               2253                 :                : 
                               2254         [ +  + ]:             63 :     if (IsA(rawreq, SupportRequestSimplify))
                               2255                 :                :     {
                               2256                 :             48 :         SupportRequestSimplify *req = (SupportRequestSimplify *) rawreq;
                               2257                 :             48 :         FuncExpr   *fexpr = req->fcall;
                               2258                 :                :         Expr       *leftop,
                               2259                 :                :                    *rightop;
                               2260                 :                : 
                               2261         [ -  + ]:             48 :         Assert(list_length(fexpr->args) == 2);
                               2262                 :             48 :         leftop = linitial(fexpr->args);
                               2263                 :             48 :         rightop = lsecond(fexpr->args);
                               2264                 :                : 
                               2265                 :             48 :         ret = find_simplified_clause(req->root, rightop, leftop);
                               2266                 :                :     }
                               2267                 :                : 
                               2268                 :             63 :     PG_RETURN_POINTER(ret);
                               2269                 :                : }
                               2270                 :                : 
                               2271                 :                : /*
                               2272                 :                :  * Planner support function for range_contains_elem (@> operator).
                               2273                 :                :  */
                               2274                 :                : Datum
                               2275                 :            153 : range_contains_elem_support(PG_FUNCTION_ARGS)
                               2276                 :                : {
                               2277                 :            153 :     Node       *rawreq = (Node *) PG_GETARG_POINTER(0);
                               2278                 :            153 :     Node       *ret = NULL;
                               2279                 :                : 
                               2280         [ +  + ]:            153 :     if (IsA(rawreq, SupportRequestSimplify))
                               2281                 :                :     {
                               2282                 :             78 :         SupportRequestSimplify *req = (SupportRequestSimplify *) rawreq;
                               2283                 :             78 :         FuncExpr   *fexpr = req->fcall;
                               2284                 :                :         Expr       *leftop,
                               2285                 :                :                    *rightop;
                               2286                 :                : 
                               2287         [ -  + ]:             78 :         Assert(list_length(fexpr->args) == 2);
                               2288                 :             78 :         leftop = linitial(fexpr->args);
                               2289                 :             78 :         rightop = lsecond(fexpr->args);
                               2290                 :                : 
                               2291                 :             78 :         ret = find_simplified_clause(req->root, leftop, rightop);
                               2292                 :                :     }
                               2293                 :                : 
                               2294                 :            153 :     PG_RETURN_POINTER(ret);
                               2295                 :                : }
                               2296                 :                : 
                               2297                 :                : 
                               2298                 :                : /*
                               2299                 :                :  *----------------------------------------------------------
                               2300                 :                :  * STATIC FUNCTIONS
                               2301                 :                :  *----------------------------------------------------------
                               2302                 :                :  */
                               2303                 :                : 
                               2304                 :                : /*
                               2305                 :                :  * Given a string representing the flags for the range type, return the flags
                               2306                 :                :  * represented as a char.
                               2307                 :                :  */
                               2308                 :                : static char
 5045                          2309                 :           2583 : range_parse_flags(const char *flags_str)
                               2310                 :                : {
      bruce@momjian.us         2311                 :           2583 :     char        flags = 0;
                               2312                 :                : 
 5056 heikki.linnakangas@i     2313         [ +  - ]:           2583 :     if (flags_str[0] == '\0' ||
                               2314         [ +  - ]:           2583 :         flags_str[1] == '\0' ||
                               2315         [ -  + ]:           2583 :         flags_str[2] != '\0')
 5056 heikki.linnakangas@i     2316         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2317                 :                :                 (errcode(ERRCODE_SYNTAX_ERROR),
                               2318                 :                :                  errmsg("invalid range bound flags"),
                               2319                 :                :                  errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));
                               2320                 :                : 
 5056 heikki.linnakangas@i     2321      [ +  +  - ]:CBC        2583 :     switch (flags_str[0])
                               2322                 :                :     {
                               2323                 :            120 :         case '[':
                               2324                 :            120 :             flags |= RANGE_LB_INC;
                               2325                 :            120 :             break;
                               2326                 :           2463 :         case '(':
                               2327                 :           2463 :             break;
 5056 heikki.linnakangas@i     2328                 :UBC           0 :         default:
                               2329         [ #  # ]:              0 :             ereport(ERROR,
                               2330                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                               2331                 :                :                      errmsg("invalid range bound flags"),
                               2332                 :                :                      errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));
                               2333                 :                :     }
                               2334                 :                : 
 5056 heikki.linnakangas@i     2335      [ +  +  - ]:CBC        2583 :     switch (flags_str[1])
                               2336                 :                :     {
                               2337                 :           2526 :         case ']':
                               2338                 :           2526 :             flags |= RANGE_UB_INC;
                               2339                 :           2526 :             break;
                               2340                 :             57 :         case ')':
                               2341                 :             57 :             break;
 5056 heikki.linnakangas@i     2342                 :UBC           0 :         default:
                               2343         [ #  # ]:              0 :             ereport(ERROR,
                               2344                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                               2345                 :                :                      errmsg("invalid range bound flags"),
                               2346                 :                :                      errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));
                               2347                 :                :     }
                               2348                 :                : 
 5056 heikki.linnakangas@i     2349                 :CBC        2583 :     return flags;
                               2350                 :                : }
                               2351                 :                : 
                               2352                 :                : /*
                               2353                 :                :  * Parse range input.
                               2354                 :                :  *
                               2355                 :                :  * Input parameters:
                               2356                 :                :  *  string: input string to be parsed
                               2357                 :                :  * Output parameters:
                               2358                 :                :  *  *flags: receives flags bitmask
                               2359                 :                :  *  *lbound_str: receives palloc'd lower bound string, or NULL if none
                               2360                 :                :  *  *ubound_str: receives palloc'd upper bound string, or NULL if none
                               2361                 :                :  *
                               2362                 :                :  * This is modeled somewhat after record_in in rowtypes.c.
                               2363                 :                :  * The input syntax is:
                               2364                 :                :  *  <range>   := EMPTY
                               2365                 :                :  *             | <lb-inc> <string>, <string> <ub-inc>
                               2366                 :                :  *  <lb-inc>  := '[' | '('
                               2367                 :                :  *  <ub-inc>  := ']' | ')'
                               2368                 :                :  *
                               2369                 :                :  * Whitespace before or after <range> is ignored.  Whitespace within a <string>
                               2370                 :                :  * is taken literally and becomes part of the input string for that bound.
                               2371                 :                :  *
                               2372                 :                :  * A <string> of length zero is taken as "infinite" (i.e. no bound), unless it
                               2373                 :                :  * is surrounded by double-quotes, in which case it is the literal empty
                               2374                 :                :  * string.
                               2375                 :                :  *
                               2376                 :                :  * Within a <string>, special characters (such as comma, parenthesis, or
                               2377                 :                :  * brackets) can be enclosed in double-quotes or escaped with backslash. Within
                               2378                 :                :  * double-quotes, a double-quote can be escaped with double-quote or backslash.
                               2379                 :                :  *
                               2380                 :                :  * Returns true on success, false on failure (but failures will return only if
                               2381                 :                :  * escontext is an ErrorSaveContext).
                               2382                 :                :  */
                               2383                 :                : static bool
 5037 tgl@sss.pgh.pa.us        2384                 :           3538 : range_parse(const char *string, char *flags, char **lbound_str,
                               2385                 :                :             char **ubound_str, Node *escontext)
                               2386                 :                : {
                               2387                 :           3538 :     const char *ptr = string;
                               2388                 :                :     bool        infinite;
                               2389                 :                : 
 5056 heikki.linnakangas@i     2390                 :           3538 :     *flags = 0;
                               2391                 :                : 
                               2392                 :                :     /* consume whitespace */
 5044 tgl@sss.pgh.pa.us        2393   [ +  +  +  + ]:           3550 :     while (*ptr != '\0' && isspace((unsigned char) *ptr))
 5056 heikki.linnakangas@i     2394                 :             12 :         ptr++;
                               2395                 :                : 
                               2396                 :                :     /* check for empty range */
                               2397         [ +  + ]:           3538 :     if (pg_strncasecmp(ptr, RANGE_EMPTY_LITERAL,
                               2398                 :                :                        strlen(RANGE_EMPTY_LITERAL)) == 0)
                               2399                 :                :     {
                               2400                 :            294 :         *flags = RANGE_EMPTY;
 5037 tgl@sss.pgh.pa.us        2401                 :            294 :         *lbound_str = NULL;
                               2402                 :            294 :         *ubound_str = NULL;
                               2403                 :                : 
 5056 heikki.linnakangas@i     2404                 :            294 :         ptr += strlen(RANGE_EMPTY_LITERAL);
                               2405                 :                : 
                               2406                 :                :         /* the rest should be whitespace */
 5044 tgl@sss.pgh.pa.us        2407   [ +  +  +  - ]:            300 :         while (*ptr != '\0' && isspace((unsigned char) *ptr))
 5056 heikki.linnakangas@i     2408                 :              6 :             ptr++;
                               2409                 :                : 
                               2410                 :                :         /* should have consumed everything */
                               2411         [ -  + ]:            294 :         if (*ptr != '\0')
  996 tgl@sss.pgh.pa.us        2412         [ #  # ]:UBC           0 :             ereturn(escontext, false,
                               2413                 :                :                     (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2414                 :                :                      errmsg("malformed range literal: \"%s\"",
                               2415                 :                :                             string),
                               2416                 :                :                      errdetail("Junk after \"empty\" key word.")));
                               2417                 :                : 
  996 tgl@sss.pgh.pa.us        2418                 :CBC         294 :         return true;
                               2419                 :                :     }
                               2420                 :                : 
 5037                          2421         [ +  + ]:           3244 :     if (*ptr == '[')
                               2422                 :                :     {
                               2423                 :           2892 :         *flags |= RANGE_LB_INC;
 5056 heikki.linnakangas@i     2424                 :           2892 :         ptr++;
                               2425                 :                :     }
 5037 tgl@sss.pgh.pa.us        2426         [ +  + ]:            352 :     else if (*ptr == '(')
                               2427                 :            340 :         ptr++;
                               2428                 :                :     else
  996                          2429         [ +  + ]:             12 :         ereturn(escontext, false,
                               2430                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2431                 :                :                  errmsg("malformed range literal: \"%s\"",
                               2432                 :                :                         string),
                               2433                 :                :                  errdetail("Missing left parenthesis or bracket.")));
                               2434                 :                : 
                               2435                 :           3232 :     ptr = range_parse_bound(string, ptr, lbound_str, &infinite, escontext);
                               2436         [ -  + ]:           3229 :     if (ptr == NULL)
  996 tgl@sss.pgh.pa.us        2437                 :UBC           0 :         return false;
 5056 heikki.linnakangas@i     2438         [ +  + ]:CBC        3229 :     if (infinite)
                               2439                 :             90 :         *flags |= RANGE_LB_INF;
                               2440                 :                : 
 5037 tgl@sss.pgh.pa.us        2441         [ +  + ]:           3229 :     if (*ptr == ',')
                               2442                 :           3217 :         ptr++;
                               2443                 :                :     else
  996                          2444         [ +  - ]:             12 :         ereturn(escontext, false,
                               2445                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2446                 :                :                  errmsg("malformed range literal: \"%s\"",
                               2447                 :                :                         string),
                               2448                 :                :                  errdetail("Missing comma after lower bound.")));
                               2449                 :                : 
                               2450                 :           3217 :     ptr = range_parse_bound(string, ptr, ubound_str, &infinite, escontext);
                               2451         [ +  + ]:           3217 :     if (ptr == NULL)
                               2452                 :              6 :         return false;
 5037                          2453         [ +  + ]:           3211 :     if (infinite)
                               2454                 :            132 :         *flags |= RANGE_UB_INF;
                               2455                 :                : 
                               2456         [ +  + ]:           3211 :     if (*ptr == ']')
                               2457                 :                :     {
                               2458                 :            326 :         *flags |= RANGE_UB_INC;
                               2459                 :            326 :         ptr++;
                               2460                 :                :     }
                               2461         [ +  + ]:           2885 :     else if (*ptr == ')')
                               2462                 :           2879 :         ptr++;
                               2463                 :                :     else                        /* must be a comma */
  996                          2464         [ +  - ]:              6 :         ereturn(escontext, false,
                               2465                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2466                 :                :                  errmsg("malformed range literal: \"%s\"",
                               2467                 :                :                         string),
                               2468                 :                :                  errdetail("Too many commas.")));
                               2469                 :                : 
                               2470                 :                :     /* consume whitespace */
 5044                          2471   [ +  +  +  + ]:           3220 :     while (*ptr != '\0' && isspace((unsigned char) *ptr))
 5056 heikki.linnakangas@i     2472                 :             15 :         ptr++;
                               2473                 :                : 
                               2474         [ +  + ]:           3205 :     if (*ptr != '\0')
  996 tgl@sss.pgh.pa.us        2475         [ +  - ]:              9 :         ereturn(escontext, false,
                               2476                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2477                 :                :                  errmsg("malformed range literal: \"%s\"",
                               2478                 :                :                         string),
                               2479                 :                :                  errdetail("Junk after right parenthesis or bracket.")));
                               2480                 :                : 
                               2481                 :           3196 :     return true;
                               2482                 :                : }
                               2483                 :                : 
                               2484                 :                : /*
                               2485                 :                :  * Helper for range_parse: parse and de-quote one bound string.
                               2486                 :                :  *
                               2487                 :                :  * We scan until finding comma, right parenthesis, or right bracket.
                               2488                 :                :  *
                               2489                 :                :  * Input parameters:
                               2490                 :                :  *  string: entire input string (used only for error reports)
                               2491                 :                :  *  ptr: where to start parsing bound
                               2492                 :                :  * Output parameters:
                               2493                 :                :  *  *bound_str: receives palloc'd bound string, or NULL if none
                               2494                 :                :  *  *infinite: set true if no bound, else false
                               2495                 :                :  *
                               2496                 :                :  * The return value is the scan ptr, advanced past the bound string.
                               2497                 :                :  * However, if escontext is an ErrorSaveContext, we return NULL on failure.
                               2498                 :                :  */
                               2499                 :                : static const char *
 5037                          2500                 :           6449 : range_parse_bound(const char *string, const char *ptr,
                               2501                 :                :                   char **bound_str, bool *infinite, Node *escontext)
                               2502                 :                : {
                               2503                 :                :     StringInfoData buf;
                               2504                 :                : 
                               2505                 :                :     /* Check for null: completely empty input means null */
 5056 heikki.linnakangas@i     2506   [ +  +  +  +  :           6449 :     if (*ptr == ',' || *ptr == ')' || *ptr == ']')
                                              +  + ]
                               2507                 :                :     {
                               2508                 :            222 :         *bound_str = NULL;
 5045 bruce@momjian.us         2509                 :            222 :         *infinite = true;
                               2510                 :                :     }
                               2511                 :                :     else
                               2512                 :                :     {
                               2513                 :                :         /* Extract string for this bound */
 5056 heikki.linnakangas@i     2514                 :           6227 :         bool        inquote = false;
                               2515                 :                : 
                               2516                 :           6227 :         initStringInfo(&buf);
                               2517   [ +  +  +  +  :          20124 :         while (inquote || !(*ptr == ',' || *ptr == ')' || *ptr == ']'))
                                        +  +  +  + ]
                               2518                 :                :         {
                               2519                 :          13906 :             char        ch = *ptr++;
                               2520                 :                : 
                               2521         [ +  + ]:          13906 :             if (ch == '\0')
  996 tgl@sss.pgh.pa.us        2522         [ +  + ]:              9 :                 ereturn(escontext, NULL,
                               2523                 :                :                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2524                 :                :                          errmsg("malformed range literal: \"%s\"",
                               2525                 :                :                                 string),
                               2526                 :                :                          errdetail("Unexpected end of input.")));
 5056 heikki.linnakangas@i     2527         [ +  + ]:          13897 :             if (ch == '\\')
                               2528                 :                :             {
                               2529         [ -  + ]:             21 :                 if (*ptr == '\0')
  996 tgl@sss.pgh.pa.us        2530         [ #  # ]:UBC           0 :                     ereturn(escontext, NULL,
                               2531                 :                :                             (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                               2532                 :                :                              errmsg("malformed range literal: \"%s\"",
                               2533                 :                :                                     string),
                               2534                 :                :                              errdetail("Unexpected end of input.")));
 5056 heikki.linnakangas@i     2535                 :CBC          21 :                 appendStringInfoChar(&buf, *ptr++);
                               2536                 :                :             }
 3546 peter_e@gmx.net          2537         [ +  + ]:          13876 :             else if (ch == '"')
                               2538                 :                :             {
 5056 heikki.linnakangas@i     2539         [ +  + ]:            200 :                 if (!inquote)
                               2540                 :            100 :                     inquote = true;
 3546 peter_e@gmx.net          2541         [ +  + ]:            100 :                 else if (*ptr == '"')
                               2542                 :                :                 {
                               2543                 :                :                     /* doubled quote within quote sequence */
 5056 heikki.linnakangas@i     2544                 :              3 :                     appendStringInfoChar(&buf, *ptr++);
                               2545                 :                :                 }
                               2546                 :                :                 else
                               2547                 :             97 :                     inquote = false;
                               2548                 :                :             }
                               2549                 :                :             else
                               2550                 :          13676 :                 appendStringInfoChar(&buf, ch);
                               2551                 :                :         }
                               2552                 :                : 
                               2553                 :           6218 :         *bound_str = buf.data;
 5045 bruce@momjian.us         2554                 :           6218 :         *infinite = false;
                               2555                 :                :     }
                               2556                 :                : 
 5056 heikki.linnakangas@i     2557                 :           6440 :     return ptr;
                               2558                 :                : }
                               2559                 :                : 
                               2560                 :                : /*
                               2561                 :                :  * Convert a deserialized range value to text form
                               2562                 :                :  *
                               2563                 :                :  * Inputs are the flags byte, and the two bound values already converted to
                               2564                 :                :  * text (but not yet quoted).  If no bound value, pass NULL.
                               2565                 :                :  *
                               2566                 :                :  * Result is a palloc'd string
                               2567                 :                :  */
                               2568                 :                : static char *
 5037 tgl@sss.pgh.pa.us        2569                 :          54112 : range_deparse(char flags, const char *lbound_str, const char *ubound_str)
                               2570                 :                : {
                               2571                 :                :     StringInfoData buf;
                               2572                 :                : 
 5056 heikki.linnakangas@i     2573         [ +  + ]:          54112 :     if (flags & RANGE_EMPTY)
                               2574                 :           8452 :         return pstrdup(RANGE_EMPTY_LITERAL);
                               2575                 :                : 
 5044 tgl@sss.pgh.pa.us        2576                 :          45660 :     initStringInfo(&buf);
                               2577                 :                : 
 5037                          2578         [ +  + ]:          45660 :     appendStringInfoChar(&buf, (flags & RANGE_LB_INC) ? '[' : '(');
                               2579                 :                : 
 5056 heikki.linnakangas@i     2580         [ +  + ]:          45660 :     if (RANGE_HAS_LBOUND(flags))
                               2581                 :          44402 :         appendStringInfoString(&buf, range_bound_escape(lbound_str));
                               2582                 :                : 
 5037 tgl@sss.pgh.pa.us        2583                 :          45660 :     appendStringInfoChar(&buf, ',');
                               2584                 :                : 
 5056 heikki.linnakangas@i     2585         [ +  + ]:          45660 :     if (RANGE_HAS_UBOUND(flags))
                               2586                 :          44333 :         appendStringInfoString(&buf, range_bound_escape(ubound_str));
                               2587                 :                : 
 5037 tgl@sss.pgh.pa.us        2588         [ +  + ]:          45660 :     appendStringInfoChar(&buf, (flags & RANGE_UB_INC) ? ']' : ')');
                               2589                 :                : 
 5056 heikki.linnakangas@i     2590                 :          45660 :     return buf.data;
                               2591                 :                : }
                               2592                 :                : 
                               2593                 :                : /*
                               2594                 :                :  * Helper for range_deparse: quote a bound value as needed
                               2595                 :                :  *
                               2596                 :                :  * Result is a palloc'd string
                               2597                 :                :  */
                               2598                 :                : static char *
 5037 tgl@sss.pgh.pa.us        2599                 :          88735 : range_bound_escape(const char *value)
                               2600                 :                : {
                               2601                 :                :     bool        nq;
                               2602                 :                :     const char *ptr;
                               2603                 :                :     StringInfoData buf;
                               2604                 :                : 
 5056 heikki.linnakangas@i     2605                 :          88735 :     initStringInfo(&buf);
                               2606                 :                : 
                               2607                 :                :     /* Detect whether we need double quotes for this value */
                               2608                 :          88735 :     nq = (value[0] == '\0');    /* force quotes for empty string */
 5037 tgl@sss.pgh.pa.us        2609         [ +  + ]:         406649 :     for (ptr = value; *ptr; ptr++)
                               2610                 :                :     {
                               2611                 :         318169 :         char        ch = *ptr;
                               2612                 :                : 
 5056 heikki.linnakangas@i     2613   [ +  +  +  +  :         318169 :         if (ch == '"' || ch == '\\' ||
                                              +  + ]
                               2614   [ +  -  +  + ]:         318100 :             ch == '(' || ch == ')' ||
                               2615   [ +  -  +  + ]:         318088 :             ch == '[' || ch == ']' ||
                               2616                 :         318064 :             ch == ',' ||
                               2617         [ +  + ]:         318064 :             isspace((unsigned char) ch))
                               2618                 :                :         {
                               2619                 :            255 :             nq = true;
                               2620                 :            255 :             break;
                               2621                 :                :         }
                               2622                 :                :     }
                               2623                 :                : 
                               2624                 :                :     /* And emit the string */
                               2625         [ +  + ]:          88735 :     if (nq)
                               2626                 :            267 :         appendStringInfoChar(&buf, '"');
 5037 tgl@sss.pgh.pa.us        2627         [ +  + ]:         408746 :     for (ptr = value; *ptr; ptr++)
                               2628                 :                :     {
                               2629                 :         320011 :         char        ch = *ptr;
                               2630                 :                : 
 5056 heikki.linnakangas@i     2631   [ +  +  +  + ]:         320011 :         if (ch == '"' || ch == '\\')
                               2632                 :             60 :             appendStringInfoChar(&buf, ch);
                               2633                 :         320011 :         appendStringInfoChar(&buf, ch);
                               2634                 :                :     }
                               2635         [ +  + ]:          88735 :     if (nq)
                               2636                 :            267 :         appendStringInfoChar(&buf, '"');
                               2637                 :                : 
                               2638                 :          88735 :     return buf.data;
                               2639                 :                : }
                               2640                 :                : 
                               2641                 :                : /*
                               2642                 :                :  * Test whether range r1 contains range r2.
                               2643                 :                :  *
                               2644                 :                :  * Caller has already checked that they are the same range type, and looked up
                               2645                 :                :  * the necessary typcache entry.
                               2646                 :                :  */
                               2647                 :                : bool
 2137 peter@eisentraut.org     2648                 :         242297 : range_contains_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                               2649                 :                : {
                               2650                 :                :     RangeBound  lower1;
                               2651                 :                :     RangeBound  upper1;
                               2652                 :                :     bool        empty1;
                               2653                 :                :     RangeBound  lower2;
                               2654                 :                :     RangeBound  upper2;
                               2655                 :                :     bool        empty2;
                               2656                 :                : 
                               2657                 :                :     /* Different types should be prevented by ANYRANGE matching rules */
 4798 heikki.linnakangas@i     2658         [ -  + ]:         242297 :     if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
 4798 heikki.linnakangas@i     2659         [ #  # ]:UBC           0 :         elog(ERROR, "range types do not match");
                               2660                 :                : 
 5044 tgl@sss.pgh.pa.us        2661                 :CBC      242297 :     range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
                               2662                 :         242297 :     range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
                               2663                 :                : 
                               2664                 :                :     /* If either range is empty, the answer is easy */
 5056 heikki.linnakangas@i     2665         [ +  + ]:         242297 :     if (empty2)
                               2666                 :         157355 :         return true;
                               2667         [ +  + ]:          84942 :     else if (empty1)
                               2668                 :           6783 :         return false;
                               2669                 :                : 
                               2670                 :                :     /* Else we must have lower1 <= lower2 and upper1 >= upper2 */
 5044 tgl@sss.pgh.pa.us        2671         [ +  + ]:          78159 :     if (range_cmp_bounds(typcache, &lower1, &lower2) > 0)
 5056 heikki.linnakangas@i     2672                 :          37340 :         return false;
 5044 tgl@sss.pgh.pa.us        2673         [ +  + ]:          40819 :     if (range_cmp_bounds(typcache, &upper1, &upper2) < 0)
 5056 heikki.linnakangas@i     2674                 :          36688 :         return false;
                               2675                 :                : 
 5037 tgl@sss.pgh.pa.us        2676                 :           4131 :     return true;
                               2677                 :                : }
                               2678                 :                : 
                               2679                 :                : bool
 2137 peter@eisentraut.org     2680                 :          61390 : range_contained_by_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
                               2681                 :                : {
 4798 heikki.linnakangas@i     2682                 :          61390 :     return range_contains_internal(typcache, r2, r1);
                               2683                 :                : }
                               2684                 :                : 
                               2685                 :                : /*
                               2686                 :                :  * Test whether range r contains a specific element value.
                               2687                 :                :  */
                               2688                 :                : bool
 2137 peter@eisentraut.org     2689                 :          44774 : range_contains_elem_internal(TypeCacheEntry *typcache, const RangeType *r, Datum val)
                               2690                 :                : {
                               2691                 :                :     RangeBound  lower;
                               2692                 :                :     RangeBound  upper;
                               2693                 :                :     bool        empty;
                               2694                 :                :     int32       cmp;
                               2695                 :                : 
 5037 tgl@sss.pgh.pa.us        2696                 :          44774 :     range_deserialize(typcache, r, &lower, &upper, &empty);
                               2697                 :                : 
                               2698         [ +  + ]:          44774 :     if (empty)
                               2699                 :           6432 :         return false;
                               2700                 :                : 
                               2701         [ +  + ]:          38342 :     if (!lower.infinite)
                               2702                 :                :     {
                               2703                 :          36215 :         cmp = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                               2704                 :                :                                               typcache->rng_collation,
                               2705                 :                :                                               lower.val, val));
                               2706         [ +  + ]:          36215 :         if (cmp > 0)
                               2707                 :          35039 :             return false;
                               2708   [ +  +  -  + ]:           1176 :         if (cmp == 0 && !lower.inclusive)
 5037 tgl@sss.pgh.pa.us        2709                 :UBC           0 :             return false;
                               2710                 :                :     }
                               2711                 :                : 
 5037 tgl@sss.pgh.pa.us        2712         [ +  + ]:CBC        3303 :     if (!upper.infinite)
                               2713                 :                :     {
                               2714                 :           3266 :         cmp = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
                               2715                 :                :                                               typcache->rng_collation,
                               2716                 :                :                                               upper.val, val));
                               2717         [ +  + ]:           3266 :         if (cmp < 0)
                               2718                 :            240 :             return false;
                               2719   [ -  +  -  - ]:           3026 :         if (cmp == 0 && !upper.inclusive)
 5037 tgl@sss.pgh.pa.us        2720                 :UBC           0 :             return false;
                               2721                 :                :     }
                               2722                 :                : 
 5056 heikki.linnakangas@i     2723                 :CBC        3063 :     return true;
                               2724                 :                : }
                               2725                 :                : 
                               2726                 :                : 
                               2727                 :                : /*
                               2728                 :                :  * datum_compute_size() and datum_write() are used to insert the bound
                               2729                 :                :  * values into a range object.  They are modeled after heaptuple.c's
                               2730                 :                :  * heap_compute_data_size() and heap_fill_tuple(), but we need not handle
                               2731                 :                :  * null values here.  TYPE_IS_PACKABLE must test the same conditions as
                               2732                 :                :  * heaptuple.c's ATT_IS_PACKABLE macro.  See the comments there for more
                               2733                 :                :  * details.
                               2734                 :                :  */
                               2735                 :                : 
                               2736                 :                : /* Does datatype allow packing into the 1-byte-header varlena format? */
                               2737                 :                : #define TYPE_IS_PACKABLE(typlen, typstorage) \
                               2738                 :                :     ((typlen) == -1 && (typstorage) != TYPSTORAGE_PLAIN)
                               2739                 :                : 
                               2740                 :                : /*
                               2741                 :                :  * Increment data_length by the space needed by the datum, including any
                               2742                 :                :  * preceding alignment padding.
                               2743                 :                :  */
                               2744                 :                : static Size
                               2745                 :         894709 : datum_compute_size(Size data_length, Datum val, bool typbyval, char typalign,
                               2746                 :                :                    int16 typlen, char typstorage)
                               2747                 :                : {
                               2748   [ +  +  +  -  :         899451 :     if (TYPE_IS_PACKABLE(typlen, typstorage) &&
                                              +  + ]
                               2749   [ +  +  +  + ]:           4742 :         VARATT_CAN_MAKE_SHORT(DatumGetPointer(val)))
                               2750                 :                :     {
                               2751                 :                :         /*
                               2752                 :                :          * we're anticipating converting to a short varlena header, so adjust
                               2753                 :                :          * length and don't count any alignment
                               2754                 :                :          */
                               2755                 :           4283 :         data_length += VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val));
                               2756                 :                :     }
                               2757                 :                :     else
                               2758                 :                :     {
                               2759   [ +  +  +  +  :         890426 :         data_length = att_align_datum(data_length, typalign, typlen, val);
                                     +  +  +  -  +  
                                           -  -  - ]
                               2760   [ +  +  +  -  :         890426 :         data_length = att_addlength_datum(data_length, typlen, val);
                                     -  -  -  -  -  
                                     -  -  -  +  +  
                                              -  - ]
                               2761                 :                :     }
                               2762                 :                : 
                               2763                 :         894709 :     return data_length;
                               2764                 :                : }
                               2765                 :                : 
                               2766                 :                : /*
                               2767                 :                :  * Write the given datum beginning at ptr (after advancing to correct
                               2768                 :                :  * alignment, if needed).  Return the pointer incremented by space used.
                               2769                 :                :  */
                               2770                 :                : static Pointer
                               2771                 :         894709 : datum_write(Pointer ptr, Datum datum, bool typbyval, char typalign,
                               2772                 :                :             int16 typlen, char typstorage)
                               2773                 :                : {
                               2774                 :                :     Size        data_length;
                               2775                 :                : 
                               2776         [ +  + ]:         894709 :     if (typbyval)
                               2777                 :                :     {
                               2778                 :                :         /* pass-by-value */
                               2779   [ +  +  +  -  :         889967 :         ptr = (char *) att_align_nominal(ptr, typalign);
                                        +  -  -  - ]
                               2780                 :         889967 :         store_att_byval(ptr, datum, typlen);
                               2781                 :         889967 :         data_length = typlen;
                               2782                 :                :     }
                               2783         [ +  - ]:           4742 :     else if (typlen == -1)
                               2784                 :                :     {
                               2785                 :                :         /* varlena */
                               2786                 :           4742 :         Pointer     val = DatumGetPointer(datum);
                               2787                 :                : 
                               2788         [ -  + ]:           4742 :         if (VARATT_IS_EXTERNAL(val))
                               2789                 :                :         {
                               2790                 :                :             /*
                               2791                 :                :              * Throw error, because we must never put a toast pointer inside a
                               2792                 :                :              * range object.  Caller should have detoasted it.
                               2793                 :                :              */
 5045 tgl@sss.pgh.pa.us        2794         [ #  # ]:UBC           0 :             elog(ERROR, "cannot store a toast pointer inside a range");
                               2795                 :                :             data_length = 0;    /* keep compiler quiet */
                               2796                 :                :         }
 5056 heikki.linnakangas@i     2797         [ +  + ]:CBC        4742 :         else if (VARATT_IS_SHORT(val))
                               2798                 :                :         {
                               2799                 :                :             /* no alignment for short varlenas */
                               2800                 :            441 :             data_length = VARSIZE_SHORT(val);
                               2801                 :            441 :             memcpy(ptr, val, data_length);
                               2802                 :                :         }
                               2803   [ +  -  +  -  :           8602 :         else if (TYPE_IS_PACKABLE(typlen, typstorage) &&
                                              +  + ]
                               2804   [ +  -  +  + ]:           4301 :                  VARATT_CAN_MAKE_SHORT(val))
                               2805                 :                :         {
                               2806                 :                :             /* convert to short varlena -- no alignment */
                               2807                 :           4283 :             data_length = VARATT_CONVERTED_SHORT_SIZE(val);
                               2808                 :           4283 :             SET_VARSIZE_SHORT(ptr, data_length);
                               2809                 :           4283 :             memcpy(ptr + 1, VARDATA(val), data_length - 1);
                               2810                 :                :         }
                               2811                 :                :         else
                               2812                 :                :         {
                               2813                 :                :             /* full 4-byte header varlena */
                               2814   [ +  -  -  -  :             18 :             ptr = (char *) att_align_nominal(ptr, typalign);
                                        -  -  -  - ]
                               2815                 :             18 :             data_length = VARSIZE(val);
                               2816                 :             18 :             memcpy(ptr, val, data_length);
                               2817                 :                :         }
                               2818                 :                :     }
 5056 heikki.linnakangas@i     2819         [ #  # ]:UBC           0 :     else if (typlen == -2)
                               2820                 :                :     {
                               2821                 :                :         /* cstring ... never needs alignment */
 2012 tgl@sss.pgh.pa.us        2822         [ #  # ]:              0 :         Assert(typalign == TYPALIGN_CHAR);
 5056 heikki.linnakangas@i     2823                 :              0 :         data_length = strlen(DatumGetCString(datum)) + 1;
                               2824                 :              0 :         memcpy(ptr, DatumGetPointer(datum), data_length);
                               2825                 :                :     }
                               2826                 :                :     else
                               2827                 :                :     {
                               2828                 :                :         /* fixed-length pass-by-reference */
                               2829   [ #  #  #  #  :              0 :         ptr = (char *) att_align_nominal(ptr, typalign);
                                        #  #  #  # ]
                               2830         [ #  # ]:              0 :         Assert(typlen > 0);
                               2831                 :              0 :         data_length = typlen;
                               2832                 :              0 :         memcpy(ptr, DatumGetPointer(datum), data_length);
                               2833                 :                :     }
                               2834                 :                : 
 5056 heikki.linnakangas@i     2835                 :CBC      894709 :     ptr += data_length;
                               2836                 :                : 
                               2837                 :         894709 :     return ptr;
                               2838                 :                : }
                               2839                 :                : 
                               2840                 :                : /*
                               2841                 :                :  * Common code for the elem_contained_by_range and range_contains_elem
                               2842                 :                :  * support functions.  The caller has extracted the function argument
                               2843                 :                :  * expressions, and swapped them if necessary to pass the range first.
                               2844                 :                :  *
                               2845                 :                :  * Returns a simplified replacement expression, or NULL if we can't simplify.
                               2846                 :                :  */
                               2847                 :                : static Node *
  595 tgl@sss.pgh.pa.us        2848                 :            126 : find_simplified_clause(PlannerInfo *root, Expr *rangeExpr, Expr *elemExpr)
                               2849                 :                : {
                               2850                 :                :     RangeType  *range;
                               2851                 :                :     TypeCacheEntry *rangetypcache;
                               2852                 :                :     RangeBound  lower;
                               2853                 :                :     RangeBound  upper;
                               2854                 :                :     bool        empty;
                               2855                 :                : 
                               2856                 :                :     /* can't do anything unless the range is a non-null constant */
                               2857   [ +  +  -  + ]:            126 :     if (!IsA(rangeExpr, Const) || ((Const *) rangeExpr)->constisnull)
                               2858                 :             78 :         return NULL;
                               2859                 :             48 :     range = DatumGetRangeTypeP(((Const *) rangeExpr)->constvalue);
                               2860                 :                : 
                               2861                 :             48 :     rangetypcache = lookup_type_cache(RangeTypeGetOid(range),
                               2862                 :                :                                       TYPECACHE_RANGE_INFO);
                               2863         [ -  + ]:             48 :     if (rangetypcache->rngelemtype == NULL)
  595 tgl@sss.pgh.pa.us        2864         [ #  # ]:UBC           0 :         elog(ERROR, "type %u is not a range type", RangeTypeGetOid(range));
                               2865                 :                : 
  595 tgl@sss.pgh.pa.us        2866                 :CBC          48 :     range_deserialize(rangetypcache, range, &lower, &upper, &empty);
                               2867                 :                : 
                               2868         [ +  + ]:             48 :     if (empty)
                               2869                 :                :     {
                               2870                 :                :         /* if the range is empty, then there can be no matches */
                               2871                 :              3 :         return makeBoolConst(false, false);
                               2872                 :                :     }
                               2873   [ +  +  +  + ]:             45 :     else if (lower.infinite && upper.infinite)
                               2874                 :                :     {
                               2875                 :                :         /* the range has infinite bounds, so it matches everything */
                               2876                 :              3 :         return makeBoolConst(true, false);
                               2877                 :                :     }
                               2878                 :                :     else
                               2879                 :                :     {
                               2880                 :                :         /* at least one bound is available, we have something to work with */
                               2881                 :             42 :         TypeCacheEntry *elemTypcache = rangetypcache->rngelemtype;
                               2882                 :             42 :         Oid         opfamily = rangetypcache->rng_opfamily;
                               2883                 :             42 :         Oid         rng_collation = rangetypcache->rng_collation;
                               2884                 :             42 :         Expr       *lowerExpr = NULL;
                               2885                 :             42 :         Expr       *upperExpr = NULL;
                               2886                 :                : 
                               2887   [ +  +  +  + ]:             42 :         if (!lower.infinite && !upper.infinite)
                               2888                 :                :         {
                               2889                 :                :             /*
                               2890                 :                :              * When both bounds are present, we have a problem: the
                               2891                 :                :              * "simplified" clause would need to evaluate the elemExpr twice.
                               2892                 :                :              * That's definitely not okay if the elemExpr is volatile, and
                               2893                 :                :              * it's also unattractive if the elemExpr is expensive.
                               2894                 :                :              */
                               2895                 :                :             QualCost    eval_cost;
                               2896                 :                : 
                               2897         [ +  + ]:             33 :             if (contain_volatile_functions((Node *) elemExpr))
                               2898                 :              3 :                 return NULL;
                               2899                 :                : 
                               2900                 :                :             /*
                               2901                 :                :              * We define "expensive" as "contains any subplan or more than 10
                               2902                 :                :              * operators".  Note that the subplan search has to be done
                               2903                 :                :              * explicitly, since cost_qual_eval() will barf on unplanned
                               2904                 :                :              * subselects.
                               2905                 :                :              */
                               2906         [ -  + ]:             30 :             if (contain_subplans((Node *) elemExpr))
  595 tgl@sss.pgh.pa.us        2907                 :UBC           0 :                 return NULL;
  595 tgl@sss.pgh.pa.us        2908                 :CBC          30 :             cost_qual_eval_node(&eval_cost, (Node *) elemExpr, root);
                               2909                 :             30 :             if (eval_cost.startup + eval_cost.per_tuple >
                               2910         [ -  + ]:             30 :                 10 * cpu_operator_cost)
  595 tgl@sss.pgh.pa.us        2911                 :UBC           0 :                 return NULL;
                               2912                 :                :         }
                               2913                 :                : 
                               2914                 :                :         /* Okay, try to build boundary comparison expressions */
  595 tgl@sss.pgh.pa.us        2915         [ +  + ]:CBC          39 :         if (!lower.infinite)
                               2916                 :                :         {
                               2917                 :             36 :             lowerExpr = build_bound_expr(elemExpr,
                               2918                 :                :                                          lower.val,
                               2919                 :                :                                          true,
                               2920                 :             36 :                                          lower.inclusive,
                               2921                 :                :                                          elemTypcache,
                               2922                 :                :                                          opfamily,
                               2923                 :                :                                          rng_collation);
                               2924         [ -  + ]:             36 :             if (lowerExpr == NULL)
  595 tgl@sss.pgh.pa.us        2925                 :UBC           0 :                 return NULL;
                               2926                 :                :         }
                               2927                 :                : 
  595 tgl@sss.pgh.pa.us        2928         [ +  + ]:CBC          39 :         if (!upper.infinite)
                               2929                 :                :         {
                               2930                 :                :             /* Copy the elemExpr if we need two copies */
                               2931         [ +  + ]:             33 :             if (!lower.infinite)
                               2932                 :             30 :                 elemExpr = copyObject(elemExpr);
                               2933                 :             33 :             upperExpr = build_bound_expr(elemExpr,
                               2934                 :                :                                          upper.val,
                               2935                 :                :                                          false,
                               2936                 :             33 :                                          upper.inclusive,
                               2937                 :                :                                          elemTypcache,
                               2938                 :                :                                          opfamily,
                               2939                 :                :                                          rng_collation);
                               2940         [ -  + ]:             33 :             if (upperExpr == NULL)
  595 tgl@sss.pgh.pa.us        2941                 :UBC           0 :                 return NULL;
                               2942                 :                :         }
                               2943                 :                : 
  595 tgl@sss.pgh.pa.us        2944   [ +  +  +  + ]:CBC          39 :         if (lowerExpr != NULL && upperExpr != NULL)
                               2945                 :             30 :             return (Node *) make_andclause(list_make2(lowerExpr, upperExpr));
                               2946         [ +  + ]:              9 :         else if (lowerExpr != NULL)
                               2947                 :              6 :             return (Node *) lowerExpr;
                               2948         [ +  - ]:              3 :         else if (upperExpr != NULL)
                               2949                 :              3 :             return (Node *) upperExpr;
                               2950                 :                :         else
                               2951                 :                :         {
  595 tgl@sss.pgh.pa.us        2952                 :UBC           0 :             Assert(false);
                               2953                 :                :             return NULL;
                               2954                 :                :         }
                               2955                 :                :     }
                               2956                 :                : }
                               2957                 :                : 
                               2958                 :                : /*
                               2959                 :                :  * Helper function for find_simplified_clause().
                               2960                 :                :  *
                               2961                 :                :  * Build the expression (elemExpr Operator val), where the operator is
                               2962                 :                :  * the appropriate member of the given opfamily depending on
                               2963                 :                :  * isLowerBound and isInclusive.  typeCache is the typcache entry for
                               2964                 :                :  * the "val" value (presently, this will be the same type as elemExpr).
                               2965                 :                :  * rng_collation is the collation to use in the comparison.
                               2966                 :                :  *
                               2967                 :                :  * Return NULL on failure (if, for some reason, we can't find the operator).
                               2968                 :                :  */
                               2969                 :                : static Expr *
  595 tgl@sss.pgh.pa.us        2970                 :CBC          69 : build_bound_expr(Expr *elemExpr, Datum val,
                               2971                 :                :                  bool isLowerBound, bool isInclusive,
                               2972                 :                :                  TypeCacheEntry *typeCache,
                               2973                 :                :                  Oid opfamily, Oid rng_collation)
                               2974                 :                : {
                               2975                 :             69 :     Oid         elemType = typeCache->type_id;
                               2976                 :             69 :     int16       elemTypeLen = typeCache->typlen;
                               2977                 :             69 :     bool        elemByValue = typeCache->typbyval;
                               2978                 :             69 :     Oid         elemCollation = typeCache->typcollation;
                               2979                 :                :     int16       strategy;
                               2980                 :                :     Oid         oproid;
                               2981                 :                :     Expr       *constExpr;
                               2982                 :                : 
                               2983                 :                :     /* Identify the comparison operator to use */
                               2984         [ +  + ]:             69 :     if (isLowerBound)
                               2985         [ +  + ]:             36 :         strategy = isInclusive ? BTGreaterEqualStrategyNumber : BTGreaterStrategyNumber;
                               2986                 :                :     else
                               2987         [ +  + ]:             33 :         strategy = isInclusive ? BTLessEqualStrategyNumber : BTLessStrategyNumber;
                               2988                 :                : 
                               2989                 :                :     /*
                               2990                 :                :      * We could use exprType(elemExpr) here, if it ever becomes possible that
                               2991                 :                :      * elemExpr is not the exact same type as the range elements.
                               2992                 :                :      */
                               2993                 :             69 :     oproid = get_opfamily_member(opfamily, elemType, elemType, strategy);
                               2994                 :                : 
                               2995                 :                :     /* We don't really expect failure here, but just in case ... */
                               2996         [ -  + ]:             69 :     if (!OidIsValid(oproid))
  595 tgl@sss.pgh.pa.us        2997                 :UBC           0 :         return NULL;
                               2998                 :                : 
                               2999                 :                :     /* OK, convert "val" to a full-fledged Const node, and make the OpExpr */
  595 tgl@sss.pgh.pa.us        3000                 :CBC          69 :     constExpr = (Expr *) makeConst(elemType,
                               3001                 :                :                                    -1,
                               3002                 :                :                                    elemCollation,
                               3003                 :                :                                    elemTypeLen,
                               3004                 :                :                                    val,
                               3005                 :                :                                    false,
                               3006                 :                :                                    elemByValue);
                               3007                 :                : 
                               3008                 :             69 :     return make_opclause(oproid,
                               3009                 :                :                          BOOLOID,
                               3010                 :                :                          false,
                               3011                 :                :                          elemExpr,
                               3012                 :                :                          constExpr,
                               3013                 :                :                          InvalidOid,
                               3014                 :                :                          rng_collation);
                               3015                 :                : }
        

Generated by: LCOV version 2.4-beta