LCOV - code coverage report
Current view: top level - src/backend/utils/adt - rangetypes.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 86.9 % 1128 980
Test Date: 2026-11-19 13:16:08 Functions: 93.2 % 88 82
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 74.5 % 780 581

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

Generated by: LCOV version 2.0-1