LCOV - code coverage report
Current view: top level - src/backend/utils/adt - rangetypes.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 814 952 85.5 %
Date: 2023-12-02 14:10:25 Functions: 72 80 90.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14