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

Generated by: LCOV version 1.14