Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * rangetypes.c
4 : : * I/O functions, operators, and support functions for range types.
5 : : *
6 : : * The stored (serialized) format of a range value is:
7 : : *
8 : : * 4 bytes: varlena header
9 : : * 4 bytes: range type's OID
10 : : * Lower boundary value, if any, aligned according to subtype's typalign
11 : : * Upper boundary value, if any, aligned according to subtype's typalign
12 : : * 1 byte for flags
13 : : *
14 : : * This representation is chosen to avoid needing any padding before the
15 : : * lower boundary value, even when it requires double alignment. We can
16 : : * expect that the varlena header is presented to us on a suitably aligned
17 : : * boundary (possibly after detoasting), and then the lower boundary is too.
18 : : * Note that this means we can't work with a packed (short varlena header)
19 : : * value; we must detoast it first.
20 : : *
21 : : *
22 : : * Portions Copyright (c) 1996-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
5504 heikki.linnakangas@i 96 :CBC 5333 : range_in(PG_FUNCTION_ARGS)
97 : : {
5493 bruce@momjian.us 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);
1444 tgl@sss.pgh.pa.us 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 : :
4072 noah@leadboat.com 110 : 5333 : check_stack_depth(); /* recurses when subtype is a range type */
111 : :
5492 tgl@sss.pgh.pa.us 112 : 5333 : cache = get_range_io_data(fcinfo, rngtypoid, IOFunc_input);
113 : :
114 : : /* parse */
1444 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 */
5504 heikki.linnakangas@i 119 [ + + ]: 5261 : if (RANGE_HAS_LBOUND(flags))
1444 tgl@sss.pgh.pa.us 120 [ - + ]: 4760 : if (!InputFunctionCallSafe(&cache->typioproc, lbound_str,
121 : : cache->typioparam, typmod,
122 : : escontext, &lower.val))
1444 tgl@sss.pgh.pa.us 123 :UBC 0 : PG_RETURN_NULL();
5504 heikki.linnakangas@i 124 [ + + ]:CBC 5257 : if (RANGE_HAS_UBOUND(flags))
1444 tgl@sss.pgh.pa.us 125 [ + + ]: 4688 : if (!InputFunctionCallSafe(&cache->typioproc, ubound_str,
126 : : cache->typioparam, typmod,
127 : : escontext, &upper.val))
128 : 16 : PG_RETURN_NULL();
129 : :
5492 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 */
1444 138 : 5237 : range = make_range(cache->typcache, &lower, &upper,
139 : 5237 : flags & RANGE_EMPTY, escontext);
140 : :
3358 141 : 5225 : PG_RETURN_RANGE_P(range);
142 : : }
143 : :
144 : : Datum
5504 heikki.linnakangas@i 145 : 55833 : range_out(PG_FUNCTION_ARGS)
146 : : {
3358 tgl@sss.pgh.pa.us 147 : 55833 : RangeType *range = PG_GETARG_RANGE_P(0);
148 : : char *output_str;
149 : : RangeIOData *cache;
150 : : char flags;
5504 heikki.linnakangas@i 151 : 55833 : char *lbound_str = NULL;
152 : 55833 : char *ubound_str = NULL;
153 : : RangeBound lower;
154 : : RangeBound upper;
155 : : bool empty;
156 : :
4072 noah@leadboat.com 157 : 55833 : check_stack_depth(); /* recurses when subtype is a range type */
158 : :
5492 tgl@sss.pgh.pa.us 159 : 55833 : cache = get_range_io_data(fcinfo, RangeTypeGetOid(range), IOFunc_output);
160 : :
161 : : /* deserialize */
162 : 55833 : range_deserialize(cache->typcache, range, &lower, &upper, &empty);
163 : 55833 : flags = range_get_flags(range);
164 : :
165 : : /* call element type's output function */
5504 heikki.linnakangas@i 166 [ + + ]: 55833 : if (RANGE_HAS_LBOUND(flags))
2459 alvherre@alvh.no-ip. 167 : 45942 : lbound_str = OutputFunctionCall(&cache->typioproc, lower.val);
5504 heikki.linnakangas@i 168 [ + + ]: 55813 : if (RANGE_HAS_UBOUND(flags))
2459 alvherre@alvh.no-ip. 169 : 45830 : ubound_str = OutputFunctionCall(&cache->typioproc, upper.val);
170 : :
171 : : /* construct result string */
5504 heikki.linnakangas@i 172 : 55813 : output_str = range_deparse(flags, lbound_str, ubound_str);
173 : :
174 : 55813 : 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
5504 heikki.linnakangas@i 185 :UBC 0 : range_recv(PG_FUNCTION_ARGS)
186 : : {
5493 bruce@momjian.us 187 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
5492 tgl@sss.pgh.pa.us 188 : 0 : Oid rngtypoid = PG_GETARG_OID(1);
5493 bruce@momjian.us 189 : 0 : int32 typmod = PG_GETARG_INT32(2);
190 : : RangeType *range;
191 : : RangeIOData *cache;
192 : : char flags;
193 : : RangeBound lower;
194 : : RangeBound upper;
195 : :
4072 noah@leadboat.com 196 : 0 : check_stack_depth(); /* recurses when subtype is a range type */
197 : :
5492 tgl@sss.pgh.pa.us 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 ... */
5504 heikki.linnakangas@i 215 [ # # ]: 0 : if (RANGE_HAS_LBOUND(flags))
216 : : {
5493 bruce@momjian.us 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 : :
5504 heikki.linnakangas@i 221 : 0 : initStringInfo(&bound_buf);
222 : 0 : appendBinaryStringInfo(&bound_buf, bound_data, bound_len);
223 : :
2459 alvherre@alvh.no-ip. 224 : 0 : lower.val = ReceiveFunctionCall(&cache->typioproc,
225 : : &bound_buf,
226 : : cache->typioparam,
227 : : typmod);
5504 heikki.linnakangas@i 228 : 0 : pfree(bound_buf.data);
229 : : }
230 : : else
231 : 0 : lower.val = (Datum) 0;
232 : :
233 [ # # ]: 0 : if (RANGE_HAS_UBOUND(flags))
234 : : {
5493 bruce@momjian.us 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 : :
5504 heikki.linnakangas@i 239 : 0 : initStringInfo(&bound_buf);
240 : 0 : appendBinaryStringInfo(&bound_buf, bound_data, bound_len);
241 : :
2459 alvherre@alvh.no-ip. 242 : 0 : upper.val = ReceiveFunctionCall(&cache->typioproc,
243 : : &bound_buf,
244 : : cache->typioparam,
245 : : typmod);
5504 heikki.linnakangas@i 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 */
5493 bruce@momjian.us 254 : 0 : lower.infinite = (flags & RANGE_LB_INF) != 0;
5504 heikki.linnakangas@i 255 : 0 : lower.inclusive = (flags & RANGE_LB_INC) != 0;
5493 bruce@momjian.us 256 : 0 : lower.lower = true;
257 : 0 : upper.infinite = (flags & RANGE_UB_INF) != 0;
5504 heikki.linnakangas@i 258 : 0 : upper.inclusive = (flags & RANGE_UB_INC) != 0;
5493 bruce@momjian.us 259 : 0 : upper.lower = false;
260 : :
261 : : /* serialize and canonicalize */
1444 tgl@sss.pgh.pa.us 262 : 0 : range = make_range(cache->typcache, &lower, &upper,
263 : 0 : flags & RANGE_EMPTY, NULL);
264 : :
3358 265 : 0 : PG_RETURN_RANGE_P(range);
266 : : }
267 : :
268 : : Datum
5504 heikki.linnakangas@i 269 : 0 : range_send(PG_FUNCTION_ARGS)
270 : : {
3358 tgl@sss.pgh.pa.us 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 : :
4072 noah@leadboat.com 279 : 0 : check_stack_depth(); /* recurses when subtype is a range type */
280 : :
387 drowley@postgresql.o 281 : 0 : initStringInfo(&buf);
282 : :
5492 tgl@sss.pgh.pa.us 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 */
387 drowley@postgresql.o 290 : 0 : pq_begintypsend(&buf);
291 : :
292 : 0 : pq_sendbyte(&buf, flags);
293 : :
5504 heikki.linnakangas@i 294 [ # # ]: 0 : if (RANGE_HAS_LBOUND(flags))
295 : : {
480 peter@eisentraut.org 296 : 0 : bytea *bound = SendFunctionCall(&cache->typioproc, lower.val);
5493 bruce@momjian.us 297 : 0 : uint32 bound_len = VARSIZE(bound) - VARHDRSZ;
298 : 0 : char *bound_data = VARDATA(bound);
299 : :
387 drowley@postgresql.o 300 : 0 : pq_sendint32(&buf, bound_len);
301 : 0 : pq_sendbytes(&buf, bound_data, bound_len);
302 : : }
303 : :
5504 heikki.linnakangas@i 304 [ # # ]: 0 : if (RANGE_HAS_UBOUND(flags))
305 : : {
480 peter@eisentraut.org 306 : 0 : bytea *bound = SendFunctionCall(&cache->typioproc, upper.val);
5493 bruce@momjian.us 307 : 0 : uint32 bound_len = VARSIZE(bound) - VARHDRSZ;
308 : 0 : char *bound_data = VARDATA(bound);
309 : :
387 drowley@postgresql.o 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 *
5492 tgl@sss.pgh.pa.us 325 :CBC 61166 : get_range_io_data(FunctionCallInfo fcinfo, Oid rngtypid, IOFuncSelector func)
326 : : {
327 : 61166 : RangeIOData *cache = (RangeIOData *) fcinfo->flinfo->fn_extra;
328 : :
329 [ + + - + ]: 61166 : 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 : 7276 : cache = (RangeIOData *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
338 : : sizeof(RangeIOData));
339 : 7276 : cache->typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
340 [ - + ]: 7276 : if (cache->typcache->rngelemtype == NULL)
5492 tgl@sss.pgh.pa.us 341 [ # # ]:UBC 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 */
5492 tgl@sss.pgh.pa.us 344 :CBC 7276 : 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 : :
2459 alvherre@alvh.no-ip. 353 [ - + ]: 7276 : if (!OidIsValid(typiofunc))
354 : : {
355 : : /* this could only happen for receive or send */
5492 tgl@sss.pgh.pa.us 356 [ # # ]:UBC 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 : : }
2459 alvherre@alvh.no-ip. 367 :CBC 7276 : fmgr_info_cxt(typiofunc, &cache->typioproc,
5492 tgl@sss.pgh.pa.us 368 : 7276 : fcinfo->flinfo->fn_mcxt);
369 : :
730 peter@eisentraut.org 370 : 7276 : fcinfo->flinfo->fn_extra = cache;
371 : : }
372 : :
5492 tgl@sss.pgh.pa.us 373 : 61166 : return cache;
374 : : }
375 : :
376 : :
377 : : /*
378 : : *----------------------------------------------------------
379 : : * GENERIC FUNCTIONS
380 : : *----------------------------------------------------------
381 : : */
382 : :
383 : : /* Construct standard-form range value from two arguments */
384 : : Datum
5504 heikki.linnakangas@i 385 : 74895 : range_constructor2(PG_FUNCTION_ARGS)
386 : : {
5493 bruce@momjian.us 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 : :
5492 tgl@sss.pgh.pa.us 395 : 74895 : typcache = range_get_typcache(fcinfo, rngtypid);
396 : :
5493 bruce@momjian.us 397 [ + + ]: 74895 : lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
398 : 74895 : lower.infinite = PG_ARGISNULL(0);
5492 tgl@sss.pgh.pa.us 399 : 74895 : lower.inclusive = true;
5493 bruce@momjian.us 400 : 74895 : lower.lower = true;
401 : :
402 [ + + ]: 74895 : upper.val = PG_ARGISNULL(1) ? (Datum) 0 : arg2;
403 : 74895 : upper.infinite = PG_ARGISNULL(1);
5492 tgl@sss.pgh.pa.us 404 : 74895 : upper.inclusive = false;
5493 bruce@momjian.us 405 : 74895 : upper.lower = false;
406 : :
1444 tgl@sss.pgh.pa.us 407 : 74895 : range = make_range(typcache, &lower, &upper, false, NULL);
408 : :
3358 409 : 74871 : PG_RETURN_RANGE_P(range);
410 : : }
411 : :
412 : : /* Construct general range value from three arguments */
413 : : Datum
5504 heikki.linnakangas@i 414 : 3540 : range_constructor3(PG_FUNCTION_ARGS)
415 : : {
5493 bruce@momjian.us 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 : :
5492 tgl@sss.pgh.pa.us 425 : 3540 : typcache = range_get_typcache(fcinfo, rngtypid);
426 : :
5504 heikki.linnakangas@i 427 [ - + ]: 3540 : if (PG_ARGISNULL(2))
5504 heikki.linnakangas@i 428 [ # # ]:UBC 0 : ereport(ERROR,
429 : : (errcode(ERRCODE_DATA_EXCEPTION),
430 : : errmsg("range constructor flags argument must not be null")));
431 : :
3548 noah@leadboat.com 432 :CBC 3540 : flags = range_parse_flags(text_to_cstring(PG_GETARG_TEXT_PP(2)));
433 : :
5493 bruce@momjian.us 434 [ + + ]: 3540 : lower.val = PG_ARGISNULL(0) ? (Datum) 0 : arg1;
435 : 3540 : lower.infinite = PG_ARGISNULL(0);
5492 tgl@sss.pgh.pa.us 436 : 3540 : lower.inclusive = (flags & RANGE_LB_INC) != 0;
5493 bruce@momjian.us 437 : 3540 : lower.lower = true;
438 : :
439 [ + + ]: 3540 : upper.val = PG_ARGISNULL(1) ? (Datum) 0 : arg2;
440 : 3540 : upper.infinite = PG_ARGISNULL(1);
5492 tgl@sss.pgh.pa.us 441 : 3540 : upper.inclusive = (flags & RANGE_UB_INC) != 0;
5493 bruce@momjian.us 442 : 3540 : upper.lower = false;
443 : :
1444 tgl@sss.pgh.pa.us 444 : 3540 : range = make_range(typcache, &lower, &upper, false, NULL);
445 : :
3358 446 : 3540 : PG_RETURN_RANGE_P(range);
447 : : }
448 : :
449 : :
450 : : /* range -> subtype functions */
451 : :
452 : : /* extract lower bound value */
453 : : Datum
5504 heikki.linnakangas@i 454 : 292 : range_lower(PG_FUNCTION_ARGS)
455 : : {
3358 tgl@sss.pgh.pa.us 456 : 292 : RangeType *r1 = PG_GETARG_RANGE_P(0);
457 : : TypeCacheEntry *typcache;
458 : : RangeBound lower;
459 : : RangeBound upper;
460 : : bool empty;
461 : :
5492 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 */
5493 467 [ + + + + ]: 292 : if (empty || lower.infinite)
468 : 24 : PG_RETURN_NULL();
469 : :
5504 heikki.linnakangas@i 470 : 268 : PG_RETURN_DATUM(lower.val);
471 : : }
472 : :
473 : : /* extract upper bound value */
474 : : Datum
475 : 172 : range_upper(PG_FUNCTION_ARGS)
476 : : {
3358 tgl@sss.pgh.pa.us 477 : 172 : RangeType *r1 = PG_GETARG_RANGE_P(0);
478 : : TypeCacheEntry *typcache;
479 : : RangeBound lower;
480 : : RangeBound upper;
481 : : bool empty;
482 : :
5492 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 */
5493 488 [ + + + + ]: 172 : if (empty || upper.infinite)
489 : 24 : PG_RETURN_NULL();
490 : :
5504 heikki.linnakangas@i 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 : : {
3358 tgl@sss.pgh.pa.us 501 : 1464 : RangeType *r1 = PG_GETARG_RANGE_P(0);
5492 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
5504 heikki.linnakangas@i 509 : 48 : range_lower_inc(PG_FUNCTION_ARGS)
510 : : {
3358 tgl@sss.pgh.pa.us 511 : 48 : RangeType *r1 = PG_GETARG_RANGE_P(0);
5492 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
5504 heikki.linnakangas@i 519 : 48 : range_upper_inc(PG_FUNCTION_ARGS)
520 : : {
3358 tgl@sss.pgh.pa.us 521 : 48 : RangeType *r1 = PG_GETARG_RANGE_P(0);
5492 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
5504 heikki.linnakangas@i 529 : 48 : range_lower_inf(PG_FUNCTION_ARGS)
530 : : {
3358 tgl@sss.pgh.pa.us 531 : 48 : RangeType *r1 = PG_GETARG_RANGE_P(0);
5492 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
5504 heikki.linnakangas@i 539 : 48 : range_upper_inf(PG_FUNCTION_ARGS)
540 : : {
3358 tgl@sss.pgh.pa.us 541 : 48 : RangeType *r1 = PG_GETARG_RANGE_P(0);
5492 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
5485 552 : 50805 : range_contains_elem(PG_FUNCTION_ARGS)
553 : : {
3358 554 : 50805 : RangeType *r = PG_GETARG_RANGE_P(0);
5485 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);
3358 568 : 58 : RangeType *r = PG_GETARG_RANGE_P(1);
569 : : TypeCacheEntry *typcache;
570 : :
5485 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
2585 peter@eisentraut.org 581 : 104878 : 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 */
5492 tgl@sss.pgh.pa.us 591 [ - + ]: 104878 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 592 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
593 : :
5492 tgl@sss.pgh.pa.us 594 :CBC 104878 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
595 : 104878 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
596 : :
5504 heikki.linnakangas@i 597 [ + + + + ]: 104878 : if (empty1 && empty2)
5246 598 : 5044 : return true;
5504 599 [ + + ]: 99834 : if (empty1 != empty2)
5246 600 : 9008 : return false;
601 : :
5492 tgl@sss.pgh.pa.us 602 [ + + ]: 90826 : if (range_cmp_bounds(typcache, &lower1, &lower2) != 0)
5246 heikki.linnakangas@i 603 : 53166 : return false;
604 : :
5492 tgl@sss.pgh.pa.us 605 [ + + ]: 37660 : if (range_cmp_bounds(typcache, &upper1, &upper2) != 0)
5246 heikki.linnakangas@i 606 : 22343 : return false;
607 : :
608 : 15317 : return true;
609 : : }
610 : :
611 : : /* equality */
612 : : Datum
613 : 52816 : range_eq(PG_FUNCTION_ARGS)
614 : : {
3358 tgl@sss.pgh.pa.us 615 : 52816 : RangeType *r1 = PG_GETARG_RANGE_P(0);
616 : 52816 : RangeType *r2 = PG_GETARG_RANGE_P(1);
617 : : TypeCacheEntry *typcache;
618 : :
5246 heikki.linnakangas@i 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
2585 peter@eisentraut.org 626 : 20 : range_ne_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
627 : : {
5246 heikki.linnakangas@i 628 : 20 : return (!range_eq_internal(typcache, r1, r2));
629 : : }
630 : :
631 : : /* inequality */
632 : : Datum
5504 633 : 20 : range_ne(PG_FUNCTION_ARGS)
634 : : {
3358 tgl@sss.pgh.pa.us 635 : 20 : RangeType *r1 = PG_GETARG_RANGE_P(0);
636 : 20 : RangeType *r2 = PG_GETARG_RANGE_P(1);
637 : : TypeCacheEntry *typcache;
638 : :
5246 heikki.linnakangas@i 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
5504 646 : 102980 : range_contains(PG_FUNCTION_ARGS)
647 : : {
3358 tgl@sss.pgh.pa.us 648 : 102980 : RangeType *r1 = PG_GETARG_RANGE_P(0);
649 : 102980 : RangeType *r2 = PG_GETARG_RANGE_P(1);
650 : : TypeCacheEntry *typcache;
651 : :
5492 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
5504 heikki.linnakangas@i 659 : 51290 : range_contained_by(PG_FUNCTION_ARGS)
660 : : {
3358 tgl@sss.pgh.pa.us 661 : 51290 : RangeType *r1 = PG_GETARG_RANGE_P(0);
662 : 51290 : RangeType *r2 = PG_GETARG_RANGE_P(1);
663 : : TypeCacheEntry *typcache;
664 : :
5492 665 : 51290 : typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
666 : :
5246 heikki.linnakangas@i 667 : 51290 : PG_RETURN_BOOL(range_contained_by_internal(typcache, r1, r2));
668 : : }
669 : :
670 : : /* strictly left of? (internal version) */
671 : : bool
2585 peter@eisentraut.org 672 : 82471 : 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 */
5492 tgl@sss.pgh.pa.us 682 [ - + ]: 82471 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 683 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
684 : :
5492 tgl@sss.pgh.pa.us 685 :CBC 82471 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
686 : 82471 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
687 : :
688 : : /* An empty range is neither before nor after any other range */
5504 heikki.linnakangas@i 689 [ + + + + ]: 82471 : if (empty1 || empty2)
5246 690 : 9940 : return false;
691 : :
692 : 72531 : return (range_cmp_bounds(typcache, &upper1, &lower2) < 0);
693 : : }
694 : :
695 : : /* strictly left of? */
696 : : Datum
697 : 52615 : range_before(PG_FUNCTION_ARGS)
698 : : {
3358 tgl@sss.pgh.pa.us 699 : 52615 : RangeType *r1 = PG_GETARG_RANGE_P(0);
700 : 52615 : RangeType *r2 = PG_GETARG_RANGE_P(1);
701 : : TypeCacheEntry *typcache;
702 : :
5246 heikki.linnakangas@i 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
2585 peter@eisentraut.org 710 : 132535 : 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 */
5492 tgl@sss.pgh.pa.us 720 [ - + ]: 132535 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 721 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
722 : :
5492 tgl@sss.pgh.pa.us 723 :CBC 132535 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
724 : 132535 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
725 : :
726 : : /* An empty range is neither before nor after any other range */
5504 heikki.linnakangas@i 727 [ + + + + ]: 132535 : if (empty1 || empty2)
5246 728 : 9540 : return false;
729 : :
730 : 122995 : return (range_cmp_bounds(typcache, &lower1, &upper2) > 0);
731 : : }
732 : :
733 : : /* strictly right of? */
734 : : Datum
735 : 52205 : range_after(PG_FUNCTION_ARGS)
736 : : {
3358 tgl@sss.pgh.pa.us 737 : 52205 : RangeType *r1 = PG_GETARG_RANGE_P(0);
738 : 52205 : RangeType *r2 = PG_GETARG_RANGE_P(1);
739 : : TypeCacheEntry *typcache;
740 : :
5246 heikki.linnakangas@i 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
5013 765 : 315976 : bounds_adjacent(TypeCacheEntry *typcache, RangeBound boundA, RangeBound boundB)
766 : : {
767 : : int cmp;
768 : :
769 [ + - - + ]: 315976 : Assert(!boundA.lower && boundB.lower);
770 : :
771 : 315976 : cmp = range_cmp_bound_values(typcache, &boundA, &boundB);
772 [ + + ]: 315976 : 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 [ + + ]: 97096 : 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 : 96383 : boundA.inclusive = !boundA.inclusive;
791 : 96383 : boundB.inclusive = !boundB.inclusive;
792 : : /* change upper/lower labels to avoid Assert failures */
793 : 96383 : boundA.lower = true;
794 : 96383 : boundB.lower = false;
1444 tgl@sss.pgh.pa.us 795 : 96383 : r = make_range(typcache, &boundA, &boundB, false, NULL);
5013 heikki.linnakangas@i 796 : 96383 : return RangeIsEmpty(r);
797 : : }
798 [ + + ]: 218880 : else if (cmp == 0)
799 : 1276 : return boundA.inclusive != boundB.inclusive;
800 : : else
4931 bruce@momjian.us 801 : 217604 : return false; /* bounds overlap */
802 : : }
803 : :
804 : : /* adjacent to (but not overlapping)? (internal version) */
805 : : bool
2585 peter@eisentraut.org 806 : 95740 : 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 */
5492 tgl@sss.pgh.pa.us 816 [ - + ]: 95740 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 817 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
818 : :
5492 tgl@sss.pgh.pa.us 819 :CBC 95740 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
820 : 95740 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
821 : :
822 : : /* An empty range is not adjacent to any other range */
5504 heikki.linnakangas@i 823 [ + + - + ]: 95740 : if (empty1 || empty2)
5246 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 : : */
5013 830 [ + + + + ]: 174467 : return (bounds_adjacent(typcache, upper1, lower2) ||
831 : 86727 : bounds_adjacent(typcache, upper2, lower1));
832 : : }
833 : :
834 : : /* adjacent to (but not overlapping)? */
835 : : Datum
5246 836 : 49630 : range_adjacent(PG_FUNCTION_ARGS)
837 : : {
3358 tgl@sss.pgh.pa.us 838 : 49630 : RangeType *r1 = PG_GETARG_RANGE_P(0);
839 : 49630 : RangeType *r2 = PG_GETARG_RANGE_P(1);
840 : : TypeCacheEntry *typcache;
841 : :
5246 heikki.linnakangas@i 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
2585 peter@eisentraut.org 849 : 66665 : 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 */
5492 tgl@sss.pgh.pa.us 859 [ - + ]: 66665 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 860 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
861 : :
5492 tgl@sss.pgh.pa.us 862 :CBC 66665 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
863 : 66665 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
864 : :
865 : : /* An empty range does not overlap any other range */
5504 heikki.linnakangas@i 866 [ + + + + ]: 66665 : if (empty1 || empty2)
5246 867 : 9560 : return false;
868 : :
5492 tgl@sss.pgh.pa.us 869 [ + + + + ]: 109090 : if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0 &&
870 : 51985 : range_cmp_bounds(typcache, &lower1, &upper2) <= 0)
5246 heikki.linnakangas@i 871 : 4097 : return true;
872 : :
5492 tgl@sss.pgh.pa.us 873 [ + + + + ]: 58128 : if (range_cmp_bounds(typcache, &lower2, &lower1) >= 0 &&
874 : 5120 : range_cmp_bounds(typcache, &lower2, &upper1) <= 0)
5246 heikki.linnakangas@i 875 : 4190 : return true;
876 : :
877 : 48818 : return false;
878 : : }
879 : :
880 : : /* overlaps? */
881 : : Datum
882 : 52016 : range_overlaps(PG_FUNCTION_ARGS)
883 : : {
3358 tgl@sss.pgh.pa.us 884 : 52016 : RangeType *r1 = PG_GETARG_RANGE_P(0);
885 : 52016 : RangeType *r2 = PG_GETARG_RANGE_P(1);
886 : : TypeCacheEntry *typcache;
887 : :
5246 heikki.linnakangas@i 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
2585 peter@eisentraut.org 895 : 87717 : 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 */
5492 tgl@sss.pgh.pa.us 905 [ - + ]: 87717 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 906 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
907 : :
5492 tgl@sss.pgh.pa.us 908 :CBC 87717 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
909 : 87717 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
910 : :
911 : : /* An empty range is neither before nor after any other range */
5504 heikki.linnakangas@i 912 [ + + - + ]: 87717 : if (empty1 || empty2)
5246 913 : 8764 : return false;
914 : :
5492 tgl@sss.pgh.pa.us 915 [ + + ]: 78953 : if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
5246 heikki.linnakangas@i 916 : 27093 : return true;
917 : :
918 : 51860 : return false;
919 : : }
920 : :
921 : : /* does not extend to right of? */
922 : : Datum
923 : 51005 : range_overleft(PG_FUNCTION_ARGS)
924 : : {
3358 tgl@sss.pgh.pa.us 925 : 51005 : RangeType *r1 = PG_GETARG_RANGE_P(0);
926 : 51005 : RangeType *r2 = PG_GETARG_RANGE_P(1);
927 : : TypeCacheEntry *typcache;
928 : :
5246 heikki.linnakangas@i 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
2585 peter@eisentraut.org 936 : 145361 : 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 */
5492 tgl@sss.pgh.pa.us 946 [ - + ]: 145361 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 947 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
948 : :
5492 tgl@sss.pgh.pa.us 949 :CBC 145361 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
950 : 145361 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
951 : :
952 : : /* An empty range is neither before nor after any other range */
5504 heikki.linnakangas@i 953 [ + + - + ]: 145361 : if (empty1 || empty2)
4487 tgl@sss.pgh.pa.us 954 : 8764 : return false;
955 : :
5492 956 [ + + ]: 136597 : if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
4487 957 : 127302 : return true;
958 : :
959 : 9295 : return false;
960 : : }
961 : :
962 : : /* does not extend to left of? */
963 : : Datum
5246 heikki.linnakangas@i 964 : 51000 : range_overright(PG_FUNCTION_ARGS)
965 : : {
3358 tgl@sss.pgh.pa.us 966 : 51000 : RangeType *r1 = PG_GETARG_RANGE_P(0);
967 : 51000 : RangeType *r2 = PG_GETARG_RANGE_P(1);
968 : : TypeCacheEntry *typcache;
969 : :
5246 heikki.linnakangas@i 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
5504 980 : 25 : range_minus(PG_FUNCTION_ARGS)
981 : : {
3358 tgl@sss.pgh.pa.us 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 */
2169 akorotkov@postgresql 988 [ - + ]: 25 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
2169 akorotkov@postgresql 989 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
990 : :
2169 akorotkov@postgresql 991 :CBC 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
2169 akorotkov@postgresql 997 :UBC 0 : PG_RETURN_NULL();
998 : : }
999 : :
1000 : : RangeType *
2169 akorotkov@postgresql 1001 :CBC 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 : :
5492 tgl@sss.pgh.pa.us 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 */
5504 heikki.linnakangas@i 1018 [ + - - + ]: 124 : if (empty1 || empty2)
2169 akorotkov@postgresql 1019 :UBC 0 : return r1;
1020 : :
5492 tgl@sss.pgh.pa.us 1021 :CBC 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 : :
5504 heikki.linnakangas@i 1026 [ + + - + ]: 124 : if (cmp_l1l2 < 0 && cmp_u1u2 > 0)
5504 heikki.linnakangas@i 1027 [ # # ]:UBC 0 : ereport(ERROR,
1028 : : (errcode(ERRCODE_DATA_EXCEPTION),
1029 : : errmsg("result of range difference would not be contiguous")));
1030 : :
5504 heikki.linnakangas@i 1031 [ + - + + ]:CBC 124 : if (cmp_l1u2 > 0 || cmp_u1l2 < 0)
2169 akorotkov@postgresql 1032 : 10 : return r1;
1033 : :
5504 heikki.linnakangas@i 1034 [ + + + + ]: 114 : if (cmp_l1l2 >= 0 && cmp_u1u2 <= 0)
2169 akorotkov@postgresql 1035 : 59 : return make_empty_range(typcache);
1036 : :
5504 heikki.linnakangas@i 1037 [ + + + - : 55 : if (cmp_l1l2 <= 0 && cmp_u1l2 >= 0 && cmp_u1u2 <= 0)
+ + ]
1038 : : {
1039 : 28 : lower2.inclusive = !lower2.inclusive;
5493 bruce@momjian.us 1040 : 28 : lower2.lower = false; /* it will become the upper bound */
1444 tgl@sss.pgh.pa.us 1041 : 28 : return make_range(typcache, &lower1, &lower2, false, NULL);
1042 : : }
1043 : :
5504 heikki.linnakangas@i 1044 [ + - + - : 27 : if (cmp_l1l2 >= 0 && cmp_u1u2 >= 0 && cmp_l1u2 <= 0)
+ - ]
1045 : : {
1046 : 27 : upper2.inclusive = !upper2.inclusive;
5493 bruce@momjian.us 1047 : 27 : upper2.lower = true; /* it will become the lower bound */
1444 tgl@sss.pgh.pa.us 1048 : 27 : return make_range(typcache, &upper2, &upper1, false, NULL);
1049 : : }
1050 : :
5493 tgl@sss.pgh.pa.us 1051 [ # # ]:UBC 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 *
4225 alvherre@alvh.no-ip. 1060 :CBC 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 */
5492 tgl@sss.pgh.pa.us 1073 [ - + ]: 1090 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5493 tgl@sss.pgh.pa.us 1074 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
1075 : :
5492 tgl@sss.pgh.pa.us 1076 :CBC 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 */
5504 heikki.linnakangas@i 1080 [ + + ]: 1090 : if (empty1)
4225 alvherre@alvh.no-ip. 1081 : 4 : return r2;
5504 heikki.linnakangas@i 1082 [ - + ]: 1086 : if (empty2)
4225 alvherre@alvh.no-ip. 1083 :UBC 0 : return r1;
1084 : :
4225 alvherre@alvh.no-ip. 1085 [ + + ]:CBC 1086 : if (strict &&
477 peter@eisentraut.org 1086 [ + + ]: 126 : !range_overlaps_internal(typcache, r1, r2) &&
1087 [ + + ]: 9 : !range_adjacent_internal(typcache, r1, r2))
5504 heikki.linnakangas@i 1088 [ + - ]: 4 : ereport(ERROR,
1089 : : (errcode(ERRCODE_DATA_EXCEPTION),
1090 : : errmsg("result of range union would not be contiguous")));
1091 : :
5492 tgl@sss.pgh.pa.us 1092 [ + + ]: 1082 : if (range_cmp_bounds(typcache, &lower1, &lower2) < 0)
5504 heikki.linnakangas@i 1093 : 1056 : result_lower = &lower1;
1094 : : else
1095 : 26 : result_lower = &lower2;
1096 : :
5492 tgl@sss.pgh.pa.us 1097 [ + + ]: 1082 : if (range_cmp_bounds(typcache, &upper1, &upper2) > 0)
5504 heikki.linnakangas@i 1098 : 38 : result_upper = &upper1;
1099 : : else
1100 : 1044 : result_upper = &upper2;
1101 : :
1444 tgl@sss.pgh.pa.us 1102 : 1082 : return make_range(typcache, result_lower, result_upper, false, NULL);
1103 : : }
1104 : :
1105 : : Datum
4225 alvherre@alvh.no-ip. 1106 : 38 : range_union(PG_FUNCTION_ARGS)
1107 : : {
3358 tgl@sss.pgh.pa.us 1108 : 38 : RangeType *r1 = PG_GETARG_RANGE_P(0);
1109 : 38 : RangeType *r2 = PG_GETARG_RANGE_P(1);
1110 : : TypeCacheEntry *typcache;
1111 : :
4225 alvherre@alvh.no-ip. 1112 : 38 : typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
1113 : :
3358 tgl@sss.pgh.pa.us 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
4225 alvherre@alvh.no-ip. 1122 : 23 : range_merge(PG_FUNCTION_ARGS)
1123 : : {
3358 tgl@sss.pgh.pa.us 1124 : 23 : RangeType *r1 = PG_GETARG_RANGE_P(0);
1125 : 23 : RangeType *r2 = PG_GETARG_RANGE_P(1);
1126 : : TypeCacheEntry *typcache;
1127 : :
4225 alvherre@alvh.no-ip. 1128 : 23 : typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r1));
1129 : :
3358 tgl@sss.pgh.pa.us 1130 : 23 : PG_RETURN_RANGE_P(range_union_internal(typcache, r1, r2, false));
1131 : : }
1132 : :
1133 : : /* set intersection */
1134 : : Datum
5504 heikki.linnakangas@i 1135 : 1294 : range_intersect(PG_FUNCTION_ARGS)
1136 : : {
3358 tgl@sss.pgh.pa.us 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 */
2169 akorotkov@postgresql 1142 [ - + ]: 1294 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
2169 akorotkov@postgresql 1143 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
1144 : :
2169 akorotkov@postgresql 1145 :CBC 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 : :
5492 tgl@sss.pgh.pa.us 1162 : 1514 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
1163 : 1514 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
1164 : :
2169 akorotkov@postgresql 1165 [ + + + - : 1514 : if (empty1 || empty2 || !range_overlaps_internal(typcache, r1, r2))
+ + ]
1166 : 502 : return make_empty_range(typcache);
1167 : :
5492 tgl@sss.pgh.pa.us 1168 [ + + ]: 1012 : if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
5504 heikki.linnakangas@i 1169 : 820 : result_lower = &lower1;
1170 : : else
1171 : 192 : result_lower = &lower2;
1172 : :
5492 tgl@sss.pgh.pa.us 1173 [ + + ]: 1012 : if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
5504 heikki.linnakangas@i 1174 : 939 : result_upper = &upper1;
1175 : : else
1176 : 73 : result_upper = &upper2;
1177 : :
1444 tgl@sss.pgh.pa.us 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
2169 akorotkov@postgresql 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 : :
1444 tgl@sss.pgh.pa.us 1216 : 27 : *output1 = make_range(typcache, &lower1, &lower2, false, NULL);
1217 : 27 : *output2 = make_range(typcache, &upper2, &upper1, false, NULL);
2169 akorotkov@postgresql 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
371 peter@eisentraut.org 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))
371 peter@eisentraut.org 1262 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
1263 : :
1264 : : /* allocate memory for user context */
353 michael@paquier.xyz 1265 :CBC 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 : : */
371 peter@eisentraut.org 1271 : 36 : rngtypid = RangeTypeGetOid(r1);
1272 : 36 : typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
1273 [ - + ]: 36 : if (typcache->rngelemtype == NULL)
371 peter@eisentraut.org 1274 [ # # ]:UBC 0 : elog(ERROR, "type %u is not a range type", rngtypid);
371 peter@eisentraut.org 1275 :CBC 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 : : }
371 peter@eisentraut.org 1377 [ # # # # :UBC 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
2169 akorotkov@postgresql 1393 :CBC 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))
2169 akorotkov@postgresql 1402 [ # # ]:UBC 0 : elog(ERROR, "range_intersect_agg_transfn called in non-aggregate context");
1403 : :
2169 akorotkov@postgresql 1404 :CBC 28 : rngtypoid = get_fn_expr_argtype(fcinfo->flinfo, 1);
1405 [ - + ]: 28 : if (!type_is_range(rngtypoid))
1704 peter@eisentraut.org 1406 [ # # ]:UBC 0 : elog(ERROR, "range_intersect_agg must be called with a range");
1407 : :
2169 akorotkov@postgresql 1408 :CBC 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
5504 heikki.linnakangas@i 1423 : 12574 : range_cmp(PG_FUNCTION_ARGS)
1424 : : {
3358 tgl@sss.pgh.pa.us 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 : :
4072 noah@leadboat.com 1436 : 12574 : check_stack_depth(); /* recurses when subtype is a range type */
1437 : :
1438 : : /* Different types should be prevented by ANYRANGE matching rules */
5492 tgl@sss.pgh.pa.us 1439 [ - + ]: 12574 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5504 heikki.linnakangas@i 1440 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
1441 : :
5492 tgl@sss.pgh.pa.us 1442 :CBC 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 */
5504 heikki.linnakangas@i 1448 [ + + + + ]: 12574 : if (empty1 && empty2)
4812 1449 : 1756 : cmp = 0;
5504 1450 [ + + ]: 10818 : else if (empty1)
4812 1451 : 2316 : cmp = -1;
5504 1452 [ + + ]: 8502 : else if (empty2)
4812 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
605 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 : 374464 : range_fast_cmp(Datum a, Datum b, SortSupport ssup)
1482 : : {
1483 : 374464 : RangeType *range_a = DatumGetRangeTypeP(a);
1484 : 374464 : 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 [ + + ]: 374464 : if (ssup->ssup_extra == NULL)
1496 : : {
1497 [ - + ]: 288 : 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 : 374464 : typcache = ssup->ssup_extra;
1502 : :
1503 : 374464 : range_deserialize(typcache, range_a, &lower1, &upper1, &empty1);
1504 : 374464 : range_deserialize(typcache, range_b, &lower2, &upper2, &empty2);
1505 : :
1506 : : /* For b-tree use, empty ranges sort before all else */
1507 [ + + + + ]: 374464 : if (empty1 && empty2)
1508 : 53000 : cmp = 0;
1509 [ + + ]: 321464 : else if (empty1)
1510 : 13568 : cmp = -1;
1511 [ + + ]: 307896 : else if (empty2)
1512 : 1884 : cmp = 1;
1513 : : else
1514 : : {
1515 : 306012 : cmp = range_cmp_bounds(typcache, &lower1, &lower2);
1516 [ + + ]: 306012 : if (cmp == 0)
1517 : 22708 : cmp = range_cmp_bounds(typcache, &upper1, &upper2);
1518 : : }
1519 : :
359 peter@eisentraut.org 1520 [ + + ]: 374464 : if (range_a != DatumGetPointer(a))
605 heikki.linnakangas@i 1521 : 360464 : pfree(range_a);
359 peter@eisentraut.org 1522 [ + + ]: 374464 : if (range_b != DatumGetPointer(b))
605 heikki.linnakangas@i 1523 : 360464 : pfree(range_b);
1524 : :
1525 : 374464 : return cmp;
1526 : : }
1527 : :
1528 : :
1529 : : /* inequality operators using the range_cmp function */
1530 : : Datum
5504 1531 : 894 : range_lt(PG_FUNCTION_ARGS)
1532 : : {
477 peter@eisentraut.org 1533 : 894 : int cmp = DatumGetInt32(range_cmp(fcinfo));
1534 : :
5504 heikki.linnakangas@i 1535 : 894 : PG_RETURN_BOOL(cmp < 0);
1536 : : }
1537 : :
1538 : : Datum
1539 : 2008 : range_le(PG_FUNCTION_ARGS)
1540 : : {
477 peter@eisentraut.org 1541 : 2008 : int cmp = DatumGetInt32(range_cmp(fcinfo));
1542 : :
5504 heikki.linnakangas@i 1543 : 2008 : PG_RETURN_BOOL(cmp <= 0);
1544 : : }
1545 : :
1546 : : Datum
1547 : 2024 : range_ge(PG_FUNCTION_ARGS)
1548 : : {
477 peter@eisentraut.org 1549 : 2024 : int cmp = DatumGetInt32(range_cmp(fcinfo));
1550 : :
5504 heikki.linnakangas@i 1551 : 2024 : PG_RETURN_BOOL(cmp >= 0);
1552 : : }
1553 : :
1554 : : Datum
1555 : 2048 : range_gt(PG_FUNCTION_ARGS)
1556 : : {
477 peter@eisentraut.org 1557 : 2048 : int cmp = DatumGetInt32(range_cmp(fcinfo));
1558 : :
5504 heikki.linnakangas@i 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 : : {
3358 tgl@sss.pgh.pa.us 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 : :
4072 noah@leadboat.com 1579 : 140 : check_stack_depth(); /* recurses when subtype is a range type */
1580 : :
5492 tgl@sss.pgh.pa.us 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))
5504 heikki.linnakangas@i 1595 [ # # ]:UBC 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 : : */
5504 heikki.linnakangas@i 1604 [ + + ]:CBC 140 : if (RANGE_HAS_LBOUND(flags))
5492 tgl@sss.pgh.pa.us 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 : :
5504 heikki.linnakangas@i 1611 [ + + ]: 140 : if (RANGE_HAS_UBOUND(flags))
5492 tgl@sss.pgh.pa.us 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 */
480 peter@eisentraut.org 1619 : 140 : result = hash_bytes_uint32((uint32) flags);
5504 heikki.linnakangas@i 1620 : 140 : result ^= lower_hash;
1742 john.naylor@postgres 1621 : 140 : result = pg_rotate_left32(result, 1);
5504 heikki.linnakangas@i 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
3376 rhaas@postgresql.org 1632 : 40 : hash_range_extended(PG_FUNCTION_ARGS)
1633 : : {
3358 tgl@sss.pgh.pa.us 1634 : 40 : RangeType *r = PG_GETARG_RANGE_P(0);
3375 rhaas@postgresql.org 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 : :
3376 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 : : {
3376 rhaas@postgresql.org 1656 :UBC 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 : :
3376 rhaas@postgresql.org 1665 [ + - ]:CBC 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
3376 rhaas@postgresql.org 1671 :UBC 0 : lower_hash = 0;
1672 : :
3376 rhaas@postgresql.org 1673 [ + - ]:CBC 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
3376 rhaas@postgresql.org 1679 :UBC 0 : upper_hash = 0;
1680 : :
1681 : : /* Merge hashes of flags and bounds */
3375 rhaas@postgresql.org 1682 :CBC 40 : result = DatumGetUInt64(hash_uint32_extended((uint32) flags,
1683 : 40 : DatumGetInt64(seed)));
3376 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
5504 heikki.linnakangas@i 1700 : 298766 : int4range_canonical(PG_FUNCTION_ARGS)
1701 : : {
3358 tgl@sss.pgh.pa.us 1702 : 298766 : RangeType *r = PG_GETARG_RANGE_P(0);
1444 1703 : 298766 : Node *escontext = fcinfo->context;
1704 : : TypeCacheEntry *typcache;
1705 : : RangeBound lower;
1706 : : RangeBound upper;
1707 : : bool empty;
1708 : :
5492 1709 : 298766 : typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
1710 : :
1711 : 298766 : range_deserialize(typcache, r, &lower, &upper, &empty);
1712 : :
5504 heikki.linnakangas@i 1713 [ - + ]: 298766 : if (empty)
3358 tgl@sss.pgh.pa.us 1714 :UBC 0 : PG_RETURN_RANGE_P(r);
1715 : :
5504 heikki.linnakangas@i 1716 [ + + + + ]:CBC 298766 : if (!lower.infinite && !lower.inclusive)
1717 : : {
1444 tgl@sss.pgh.pa.us 1718 : 2168 : int32 bnd = DatumGetInt32(lower.val);
1719 : :
1720 : : /* Handle possible overflow manually */
1721 [ - + ]: 2168 : if (unlikely(bnd == PG_INT32_MAX))
1444 tgl@sss.pgh.pa.us 1722 [ # # ]:UBC 0 : ereturn(escontext, (Datum) 0,
1723 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1724 : : errmsg("integer out of range")));
1444 tgl@sss.pgh.pa.us 1725 :CBC 2168 : lower.val = Int32GetDatum(bnd + 1);
5504 heikki.linnakangas@i 1726 : 2168 : lower.inclusive = true;
1727 : : }
1728 : :
1729 [ + + + + ]: 298766 : if (!upper.infinite && upper.inclusive)
1730 : : {
1444 tgl@sss.pgh.pa.us 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);
5504 heikki.linnakangas@i 1739 : 2167 : upper.inclusive = false;
1740 : : }
1741 : :
1444 tgl@sss.pgh.pa.us 1742 : 298758 : PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper,
1743 : : false, escontext));
1744 : : }
1745 : :
1746 : : Datum
5504 heikki.linnakangas@i 1747 : 63 : int8range_canonical(PG_FUNCTION_ARGS)
1748 : : {
3358 tgl@sss.pgh.pa.us 1749 : 63 : RangeType *r = PG_GETARG_RANGE_P(0);
1444 1750 : 63 : Node *escontext = fcinfo->context;
1751 : : TypeCacheEntry *typcache;
1752 : : RangeBound lower;
1753 : : RangeBound upper;
1754 : : bool empty;
1755 : :
5492 1756 : 63 : typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
1757 : :
1758 : 63 : range_deserialize(typcache, r, &lower, &upper, &empty);
1759 : :
5504 heikki.linnakangas@i 1760 [ - + ]: 63 : if (empty)
3358 tgl@sss.pgh.pa.us 1761 :UBC 0 : PG_RETURN_RANGE_P(r);
1762 : :
5504 heikki.linnakangas@i 1763 [ + - + + ]:CBC 63 : if (!lower.infinite && !lower.inclusive)
1764 : : {
1444 tgl@sss.pgh.pa.us 1765 : 13 : int64 bnd = DatumGetInt64(lower.val);
1766 : :
1767 : : /* Handle possible overflow manually */
1768 [ - + ]: 13 : if (unlikely(bnd == PG_INT64_MAX))
1444 tgl@sss.pgh.pa.us 1769 [ # # ]:UBC 0 : ereturn(escontext, (Datum) 0,
1770 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1771 : : errmsg("bigint out of range")));
1444 tgl@sss.pgh.pa.us 1772 :CBC 13 : lower.val = Int64GetDatum(bnd + 1);
5504 heikki.linnakangas@i 1773 : 13 : lower.inclusive = true;
1774 : : }
1775 : :
1776 [ + - + + ]: 63 : if (!upper.infinite && upper.inclusive)
1777 : : {
1444 tgl@sss.pgh.pa.us 1778 : 14 : int64 bnd = DatumGetInt64(upper.val);
1779 : :
1780 : : /* Handle possible overflow manually */
1781 [ - + ]: 14 : if (unlikely(bnd == PG_INT64_MAX))
1444 tgl@sss.pgh.pa.us 1782 [ # # ]:UBC 0 : ereturn(escontext, (Datum) 0,
1783 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1784 : : errmsg("bigint out of range")));
1444 tgl@sss.pgh.pa.us 1785 :CBC 14 : upper.val = Int64GetDatum(bnd + 1);
5504 heikki.linnakangas@i 1786 : 14 : upper.inclusive = false;
1787 : : }
1788 : :
1444 tgl@sss.pgh.pa.us 1789 : 63 : PG_RETURN_RANGE_P(range_serialize(typcache, &lower, &upper,
1790 : : false, escontext));
1791 : : }
1792 : :
1793 : : Datum
5504 heikki.linnakangas@i 1794 : 2678 : daterange_canonical(PG_FUNCTION_ARGS)
1795 : : {
3358 tgl@sss.pgh.pa.us 1796 : 2678 : RangeType *r = PG_GETARG_RANGE_P(0);
1444 1797 : 2678 : Node *escontext = fcinfo->context;
1798 : : TypeCacheEntry *typcache;
1799 : : RangeBound lower;
1800 : : RangeBound upper;
1801 : : bool empty;
1802 : :
5492 1803 : 2678 : typcache = range_get_typcache(fcinfo, RangeTypeGetOid(r));
1804 : :
1805 : 2678 : range_deserialize(typcache, r, &lower, &upper, &empty);
1806 : :
5504 heikki.linnakangas@i 1807 [ - + ]: 2678 : if (empty)
3358 tgl@sss.pgh.pa.us 1808 :UBC 0 : PG_RETURN_RANGE_P(r);
1809 : :
2690 jdavis@postgresql.or 1810 [ + + + + :CBC 2678 : if (!lower.infinite && !DATE_NOT_FINITE(DatumGetDateADT(lower.val)) &&
+ - ]
1811 [ + + ]: 2638 : !lower.inclusive)
1812 : : {
1444 tgl@sss.pgh.pa.us 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)))
- + ]
1444 tgl@sss.pgh.pa.us 1818 [ # # ]:UBC 0 : ereturn(escontext, (Datum) 0,
1819 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1820 : : errmsg("date out of range")));
1444 tgl@sss.pgh.pa.us 1821 :CBC 28 : lower.val = DateADTGetDatum(bnd);
5504 heikki.linnakangas@i 1822 : 28 : lower.inclusive = true;
1823 : : }
1824 : :
2690 jdavis@postgresql.or 1825 [ + + + - : 2678 : if (!upper.infinite && !DATE_NOT_FINITE(DatumGetDateADT(upper.val)) &&
+ + ]
1826 [ + + ]: 2544 : upper.inclusive)
1827 : : {
1444 tgl@sss.pgh.pa.us 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);
5504 heikki.linnakangas@i 1837 : 20 : upper.inclusive = false;
1838 : : }
1839 : :
1444 tgl@sss.pgh.pa.us 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
5504 heikki.linnakangas@i 1856 : 574080 : int4range_subdiff(PG_FUNCTION_ARGS)
1857 : : {
5493 bruce@momjian.us 1858 : 574080 : int32 v1 = PG_GETARG_INT32(0);
1859 : 574080 : int32 v2 = PG_GETARG_INT32(1);
1860 : :
tgl@sss.pgh.pa.us 1861 : 574080 : PG_RETURN_FLOAT8((float8) v1 - (float8) v2);
1862 : : }
1863 : :
1864 : : Datum
5504 heikki.linnakangas@i 1865 :UBC 0 : int8range_subdiff(PG_FUNCTION_ARGS)
1866 : : {
5493 bruce@momjian.us 1867 : 0 : int64 v1 = PG_GETARG_INT64(0);
1868 : 0 : int64 v2 = PG_GETARG_INT64(1);
1869 : :
tgl@sss.pgh.pa.us 1870 : 0 : PG_RETURN_FLOAT8((float8) v1 - (float8) v2);
1871 : : }
1872 : :
1873 : : Datum
5504 heikki.linnakangas@i 1874 :CBC 208 : numrange_subdiff(PG_FUNCTION_ARGS)
1875 : : {
23 tomas.vondra@postgre 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);
16 tgl@sss.pgh.pa.us 1888 [ + + ]: 208 : if (escontext.error_occurred)
1889 : : {
1890 : : /* The values v1 and v2 may be in arbitrary order. */
23 tomas.vondra@postgre 1891 [ - + ]: 4 : if (DatumGetBool(DirectFunctionCall2(numeric_lt, NumericGetDatum(v1),
1892 : : NumericGetDatum(v2))))
23 tomas.vondra@postgre 1893 :UBC 0 : floatresult = -get_float8_infinity();
1894 : : else
23 tomas.vondra@postgre 1895 :CBC 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 : :
5504 heikki.linnakangas@i 1908 : 208 : PG_RETURN_FLOAT8(floatresult);
1909 : : }
1910 : :
1911 : : Datum
5493 tgl@sss.pgh.pa.us 1912 :UBC 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
5504 heikki.linnakangas@i 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 : :
5493 tgl@sss.pgh.pa.us 1927 : 0 : result = ((float8) v1 - (float8) v2) / USECS_PER_SEC;
5504 heikki.linnakangas@i 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 : :
5493 tgl@sss.pgh.pa.us 1938 : 0 : result = ((float8) v1 - (float8) v2) / USECS_PER_SEC;
5504 heikki.linnakangas@i 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 *
5492 tgl@sss.pgh.pa.us 1960 :CBC 2758344 : range_get_typcache(FunctionCallInfo fcinfo, Oid rngtypid)
1961 : : {
1962 : 2758344 : TypeCacheEntry *typcache = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
1963 : :
1964 [ + + ]: 2758344 : if (typcache == NULL ||
1965 [ - + ]: 2744133 : typcache->type_id != rngtypid)
1966 : : {
1967 : 14211 : typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
1968 [ - + ]: 14211 : if (typcache->rngelemtype == NULL)
5492 tgl@sss.pgh.pa.us 1969 [ # # ]:UBC 0 : elog(ERROR, "type %u is not a range type", rngtypid);
730 peter@eisentraut.org 1970 :CBC 14211 : fcinfo->flinfo->fn_extra = typcache;
1971 : : }
1972 : :
5492 tgl@sss.pgh.pa.us 1973 : 2758344 : 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 : 613727 : 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;
5493 bruce@momjian.us 1995 : 613727 : 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 : : */
5485 tgl@sss.pgh.pa.us 2001 [ - + ]: 613727 : Assert(lower->lower);
2002 [ - + ]: 613727 : Assert(!upper->lower);
2003 : :
5504 heikki.linnakangas@i 2004 [ + + ]: 613727 : if (empty)
2005 : 3030 : flags |= RANGE_EMPTY;
2006 : : else
2007 : : {
5485 tgl@sss.pgh.pa.us 2008 : 610697 : cmp = range_cmp_bound_values(typcache, lower, upper);
2009 : :
2010 : : /* error check: if lower bound value is above upper, it's wrong */
2011 [ + + ]: 610697 : if (cmp > 0)
1444 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 */
5485 2017 [ + + + + : 610653 : if (cmp == 0 && !(lower->inclusive && upper->inclusive))
+ + ]
2018 : 258 : flags |= RANGE_EMPTY;
2019 : : else
2020 : : {
2021 : : /* infinite boundaries are never inclusive */
2022 [ + + ]: 610395 : if (lower->infinite)
2023 : 7355 : flags |= RANGE_LB_INF;
2024 [ + + ]: 603040 : else if (lower->inclusive)
2025 : 600577 : flags |= RANGE_LB_INC;
2026 [ + + ]: 610395 : if (upper->infinite)
2027 : 4795 : flags |= RANGE_UB_INF;
2028 [ + + ]: 605600 : else if (upper->inclusive)
2029 : 2889 : flags |= RANGE_UB_INC;
2030 : : }
2031 : : }
2032 : :
2033 : : /* Fetch information about range's element type */
2034 : 613683 : typlen = typcache->rngelemtype->typlen;
2035 : 613683 : typbyval = typcache->rngelemtype->typbyval;
2036 : 613683 : typalign = typcache->rngelemtype->typalign;
2037 : 613683 : typstorage = typcache->rngelemtype->typstorage;
2038 : :
2039 : : /* Count space for varlena header and range type's OID */
5492 2040 : 613683 : msize = sizeof(RangeType);
2041 [ - + ]: 613683 : Assert(msize == MAXALIGN(msize));
2042 : :
2043 : : /* Count space for bounds */
5504 heikki.linnakangas@i 2044 [ + + ]: 613683 : 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 : : */
5493 tgl@sss.pgh.pa.us 2055 [ + + ]: 603040 : if (typlen == -1)
2056 : 4147 : lower->val = PointerGetDatum(PG_DETOAST_DATUM_PACKED(lower->val));
2057 : :
5504 heikki.linnakangas@i 2058 : 603040 : msize = datum_compute_size(msize, lower->val, typbyval, typalign,
2059 : : typlen, typstorage);
2060 : : }
2061 : :
2062 [ + + ]: 613683 : if (RANGE_HAS_UBOUND(flags))
2063 : : {
2064 : : /* Make sure item to be inserted is not toasted */
5493 tgl@sss.pgh.pa.us 2065 [ + + ]: 605600 : if (typlen == -1)
2066 : 4115 : upper->val = PointerGetDatum(PG_DETOAST_DATUM_PACKED(upper->val));
2067 : :
5504 heikki.linnakangas@i 2068 : 605600 : msize = datum_compute_size(msize, upper->val, typbyval, typalign,
2069 : : typlen, typstorage);
2070 : : }
2071 : :
2072 : : /* Add space for flag byte */
2073 : 613683 : msize += sizeof(char);
2074 : :
2075 : : /* Note: zero-fill is required here, just as in heap tuples */
5492 tgl@sss.pgh.pa.us 2076 : 613683 : range = (RangeType *) palloc0(msize);
2077 : 613683 : SET_VARSIZE(range, msize);
2078 : :
2079 : : /* Now fill in the datum */
2080 : 613683 : range->rangetypid = typcache->type_id;
2081 : :
2082 : 613683 : ptr = (char *) (range + 1);
2083 : :
5504 heikki.linnakangas@i 2084 [ + + ]: 613683 : if (RANGE_HAS_LBOUND(flags))
2085 : : {
2086 [ - + ]: 603040 : Assert(lower->lower);
2087 : 603040 : ptr = datum_write(ptr, lower->val, typbyval, typalign, typlen,
2088 : : typstorage);
2089 : : }
2090 : :
2091 [ + + ]: 613683 : if (RANGE_HAS_UBOUND(flags))
2092 : : {
2093 [ - + ]: 605600 : Assert(!upper->lower);
2094 : 605600 : ptr = datum_write(ptr, upper->val, typbyval, typalign, typlen,
2095 : : typstorage);
2096 : : }
2097 : :
5492 tgl@sss.pgh.pa.us 2098 : 613683 : *((char *) ptr) = flags;
2099 : :
2100 : 613683 : 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
2585 peter@eisentraut.org 2113 : 6550464 : 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 */
5492 tgl@sss.pgh.pa.us 2125 [ - + ]: 6550464 : Assert(RangeTypeGetOid(range) == typcache->type_id);
2126 : :
2127 : : /* fetch the flag byte from datum's last byte */
2585 peter@eisentraut.org 2128 : 6550464 : flags = *((const char *) range + VARSIZE(range) - 1);
2129 : :
2130 : : /* fetch information about range's element type */
5492 tgl@sss.pgh.pa.us 2131 : 6550464 : typlen = typcache->rngelemtype->typlen;
2132 : 6550464 : typbyval = typcache->rngelemtype->typbyval;
2133 : 6550464 : typalign = typcache->rngelemtype->typalign;
2134 : :
2135 : : /* initialize data pointer just after the range OID */
306 peter@eisentraut.org 2136 : 6550464 : ptr = (const char *) (range + 1);
2137 : :
2138 : : /* fetch lower bound, if any */
5504 heikki.linnakangas@i 2139 [ + + ]: 6550464 : if (RANGE_HAS_LBOUND(flags))
2140 : : {
2141 : : /* att_align_pointer cannot be necessary here */
2142 : 5773792 : lbound = fetch_att(ptr, typbyval, typlen);
102 peter@eisentraut.org 2143 [ + + + - :GNC 5773792 : ptr = att_addlength_pointer(ptr, typlen, ptr);
- - ]
2144 : : }
2145 : : else
5504 heikki.linnakangas@i 2146 :CBC 776672 : lbound = (Datum) 0;
2147 : :
2148 : : /* fetch upper bound, if any */
2149 [ + + ]: 6550464 : if (RANGE_HAS_UBOUND(flags))
2150 : : {
360 peter@eisentraut.org 2151 [ + + + + ]: 5789323 : ptr = (char *) att_align_pointer(ptr, typalign, typlen, ptr);
5504 heikki.linnakangas@i 2152 : 5789323 : ubound = fetch_att(ptr, typbyval, typlen);
2153 : : /* no need for att_addlength_pointer */
2154 : : }
2155 : : else
2156 : 761141 : ubound = (Datum) 0;
2157 : :
2158 : : /* emit results */
2159 : :
5492 tgl@sss.pgh.pa.us 2160 : 6550464 : *empty = (flags & RANGE_EMPTY) != 0;
2161 : :
5493 bruce@momjian.us 2162 : 6550464 : lower->val = lbound;
tgl@sss.pgh.pa.us 2163 : 6550464 : lower->infinite = (flags & RANGE_LB_INF) != 0;
5492 2164 : 6550464 : lower->inclusive = (flags & RANGE_LB_INC) != 0;
5493 bruce@momjian.us 2165 : 6550464 : lower->lower = true;
2166 : :
2167 : 6550464 : upper->val = ubound;
tgl@sss.pgh.pa.us 2168 : 6550464 : upper->infinite = (flags & RANGE_UB_INF) != 0;
5492 2169 : 6550464 : upper->inclusive = (flags & RANGE_UB_INC) != 0;
5493 bruce@momjian.us 2170 : 6550464 : upper->lower = false;
5504 heikki.linnakangas@i 2171 : 6550464 : }
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
2585 peter@eisentraut.org 2180 : 1952754 : range_get_flags(const RangeType *range)
2181 : : {
2182 : : /* fetch the flag byte from datum's last byte */
306 2183 : 1952754 : 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
5480 tgl@sss.pgh.pa.us 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 *
5492 2209 : 309356 : make_range(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper,
2210 : : bool empty, struct Node *escontext)
2211 : : {
2212 : : RangeType *range;
2213 : :
1444 2214 : 309356 : range = range_serialize(typcache, lower, upper, empty, escontext);
2215 : :
2216 [ + + + - : 309320 : if (SOFT_ERROR_OCCURRED(escontext))
+ + ]
2217 : 8 : return NULL;
2218 : :
2219 : : /* no need to call canonical on empty ranges ... */
5485 2220 [ + + ]: 309312 : if (OidIsValid(typcache->rng_canonical_finfo.fn_oid) &&
2221 [ + + ]: 304475 : !RangeIsEmpty(range))
2222 : : {
2223 : : /* Do this the hard way so that we can pass escontext */
1444 2224 : 301507 : LOCAL_FCINFO(fcinfo, 1);
2225 : : Datum result;
2226 : :
2227 : 301507 : InitFunctionCallInfoData(*fcinfo, &typcache->rng_canonical_finfo, 1,
2228 : : InvalidOid, escontext, NULL);
2229 : :
2230 : 301507 : fcinfo->args[0].value = RangeTypePGetDatum(range);
2231 : 301507 : fcinfo->args[0].isnull = false;
2232 : :
2233 : 301507 : result = FunctionCallInvoke(fcinfo);
2234 : :
2235 [ + + + - : 301507 : if (SOFT_ERROR_OCCURRED(escontext))
+ + ]
2236 : 16 : return NULL;
2237 : :
2238 : : /* Should not get a null result if there was no error */
2239 [ - + ]: 301491 : if (fcinfo->isnull)
1444 tgl@sss.pgh.pa.us 2240 [ # # ]:UBC 0 : elog(ERROR, "function %u returned NULL",
2241 : : typcache->rng_canonical_finfo.fn_oid);
2242 : :
1444 tgl@sss.pgh.pa.us 2243 :CBC 301491 : range = DatumGetRangeTypeP(result);
2244 : : }
2245 : :
5492 2246 : 309296 : 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
2585 peter@eisentraut.org 2273 : 8402619 : 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 : : */
5504 heikki.linnakangas@i 2281 [ + + + + ]: 8402619 : 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 [ + + ]: 14035 : if (b1->lower == b2->lower)
2288 : 13971 : return 0;
2289 : : else
5490 tgl@sss.pgh.pa.us 2290 [ + + ]: 64 : return b1->lower ? -1 : 1;
2291 : : }
2292 [ + + ]: 8388584 : else if (b1->infinite)
2293 [ + + ]: 65942 : return b1->lower ? -1 : 1;
2294 [ + + ]: 8322642 : else if (b2->infinite)
2295 [ + + ]: 21102 : return b2->lower ? 1 : -1;
2296 : :
2297 : : /*
2298 : : * Both boundaries are finite, so compare the held values.
2299 : : */
5492 2300 : 8301540 : result = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
2301 : : typcache->rng_collation,
5504 heikki.linnakangas@i 2302 : 8301540 : 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 [ + + ]: 8301540 : if (result == 0)
2310 : : {
5490 tgl@sss.pgh.pa.us 2311 [ + + + + ]: 577955 : if (!b1->inclusive && !b2->inclusive)
2312 : : {
2313 : : /* both are exclusive */
2314 [ + + ]: 256061 : if (b1->lower == b2->lower)
2315 : 256056 : return 0;
2316 : : else
2317 [ - + ]: 5 : return b1->lower ? 1 : -1;
2318 : : }
2319 [ + + ]: 321894 : else if (!b1->inclusive)
2320 [ - + ]: 525 : return b1->lower ? 1 : -1;
2321 [ + + ]: 321369 : else if (!b2->inclusive)
2322 [ + + ]: 845 : 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 : 320524 : return 0;
2331 : : }
2332 : : }
2333 : :
5504 heikki.linnakangas@i 2334 : 7723585 : 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
2585 peter@eisentraut.org 2347 : 926673 : 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 : : */
5485 tgl@sss.pgh.pa.us 2354 [ + + + + ]: 926673 : 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)
5485 tgl@sss.pgh.pa.us 2361 :UBC 0 : return 0;
2362 : : else
5485 tgl@sss.pgh.pa.us 2363 [ + + ]:CBC 237 : return b1->lower ? -1 : 1;
2364 : : }
2365 [ + + ]: 926436 : else if (b1->infinite)
2366 [ + + ]: 11558 : return b1->lower ? -1 : 1;
2367 [ + + ]: 914878 : else if (b2->infinite)
2368 [ + + ]: 9470 : return b2->lower ? 1 : -1;
2369 : :
2370 : : /*
2371 : : * Both boundaries are finite, so compare the held values.
2372 : : */
2373 : 905408 : return DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
2374 : : typcache->rng_collation,
2375 : 905408 : 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
2169 akorotkov@postgresql 2386 : 18431 : range_compare(const void *key1, const void *key2, void *arg)
2387 : : {
306 peter@eisentraut.org 2388 : 18431 : RangeType *r1 = *(RangeType *const *) key1;
2389 : 18431 : RangeType *r2 = *(RangeType *const *) key2;
2169 akorotkov@postgresql 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 *
5492 tgl@sss.pgh.pa.us 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;
5504 heikki.linnakangas@i 2430 : 2622 : lower.lower = true;
2431 : :
5492 tgl@sss.pgh.pa.us 2432 : 2622 : upper.val = (Datum) 0;
2433 : 2622 : upper.infinite = false;
2434 : 2622 : upper.inclusive = false;
5504 heikki.linnakangas@i 2435 : 2622 : upper.lower = false;
2436 : :
1444 tgl@sss.pgh.pa.us 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
1043 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 [ - + ]: 80 : 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 [ - + ]: 130 : 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
5493 2504 : 3540 : range_parse_flags(const char *flags_str)
2505 : : {
bruce@momjian.us 2506 : 3540 : char flags = 0;
2507 : :
5504 heikki.linnakangas@i 2508 [ + - ]: 3540 : if (flags_str[0] == '\0' ||
2509 [ + - ]: 3540 : flags_str[1] == '\0' ||
2510 [ - + ]: 3540 : flags_str[2] != '\0')
5504 heikki.linnakangas@i 2511 [ # # ]:UBC 0 : ereport(ERROR,
2512 : : (errcode(ERRCODE_SYNTAX_ERROR),
2513 : : errmsg("invalid range bound flags"),
2514 : : errhint("Valid values are \"[]\", \"[)\", \"(]\", and \"()\".")));
2515 : :
5504 heikki.linnakangas@i 2516 [ + + - ]:CBC 3540 : switch (flags_str[0])
2517 : : {
2518 : 220 : case '[':
2519 : 220 : flags |= RANGE_LB_INC;
2520 : 220 : break;
2521 : 3320 : case '(':
2522 : 3320 : break;
5504 heikki.linnakangas@i 2523 :UBC 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 : :
5504 heikki.linnakangas@i 2530 [ + + - ]:CBC 3540 : switch (flags_str[1])
2531 : : {
2532 : 3445 : case ']':
2533 : 3445 : flags |= RANGE_UB_INC;
2534 : 3445 : break;
2535 : 95 : case ')':
2536 : 95 : break;
5504 heikki.linnakangas@i 2537 :UBC 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 : :
5504 heikki.linnakangas@i 2544 :CBC 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
5485 tgl@sss.pgh.pa.us 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 : :
5504 heikki.linnakangas@i 2585 : 5333 : *flags = 0;
2586 : :
2587 : : /* consume whitespace */
5492 tgl@sss.pgh.pa.us 2588 [ + + + + ]: 5349 : while (*ptr != '\0' && isspace((unsigned char) *ptr))
5504 heikki.linnakangas@i 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;
5485 tgl@sss.pgh.pa.us 2596 : 408 : *lbound_str = NULL;
2597 : 408 : *ubound_str = NULL;
2598 : :
5504 heikki.linnakangas@i 2599 : 408 : ptr += strlen(RANGE_EMPTY_LITERAL);
2600 : :
2601 : : /* the rest should be whitespace */
5492 tgl@sss.pgh.pa.us 2602 [ + + + - ]: 416 : while (*ptr != '\0' && isspace((unsigned char) *ptr))
5504 heikki.linnakangas@i 2603 : 8 : ptr++;
2604 : :
2605 : : /* should have consumed everything */
2606 [ - + ]: 408 : if (*ptr != '\0')
1444 tgl@sss.pgh.pa.us 2607 [ # # ]:UBC 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 : :
1444 tgl@sss.pgh.pa.us 2613 :CBC 408 : return true;
2614 : : }
2615 : :
5485 2616 [ + + ]: 4925 : if (*ptr == '[')
2617 : : {
2618 : 4451 : *flags |= RANGE_LB_INC;
5504 heikki.linnakangas@i 2619 : 4451 : ptr++;
2620 : : }
5485 tgl@sss.pgh.pa.us 2621 [ + + ]: 474 : else if (*ptr == '(')
2622 : 450 : ptr++;
2623 : : else
1444 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)
1444 tgl@sss.pgh.pa.us 2632 :UBC 0 : return false;
5504 heikki.linnakangas@i 2633 [ + + ]:CBC 4897 : if (infinite)
2634 : 117 : *flags |= RANGE_LB_INF;
2635 : :
5485 tgl@sss.pgh.pa.us 2636 [ + + ]: 4897 : if (*ptr == ',')
2637 : 4881 : ptr++;
2638 : : else
1444 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;
5485 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 */
1444 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 */
5492 2666 [ + + + + ]: 4885 : while (*ptr != '\0' && isspace((unsigned char) *ptr))
5504 heikki.linnakangas@i 2667 : 20 : ptr++;
2668 : :
2669 [ + + ]: 4865 : if (*ptr != '\0')
1444 tgl@sss.pgh.pa.us 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 *
5485 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 */
5504 heikki.linnakangas@i 2701 [ + + + + : 9782 : if (*ptr == ',' || *ptr == ')' || *ptr == ']')
+ + ]
2702 : : {
2703 : 294 : *bound_str = NULL;
5493 bruce@momjian.us 2704 : 294 : *infinite = true;
2705 : : }
2706 : : else
2707 : : {
2708 : : /* Extract string for this bound */
5504 heikki.linnakangas@i 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')
1444 tgl@sss.pgh.pa.us 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.")));
5504 heikki.linnakangas@i 2722 [ + + ]: 20832 : if (ch == '\\')
2723 : : {
2724 [ - + ]: 28 : if (*ptr == '\0')
1444 tgl@sss.pgh.pa.us 2725 [ # # ]:UBC 0 : ereturn(escontext, NULL,
2726 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2727 : : errmsg("malformed range literal: \"%s\"",
2728 : : string),
2729 : : errdetail("Unexpected end of input.")));
5504 heikki.linnakangas@i 2730 :CBC 28 : appendStringInfoChar(&buf, *ptr++);
2731 : : }
3994 peter_e@gmx.net 2732 [ + + ]: 20804 : else if (ch == '"')
2733 : : {
5504 heikki.linnakangas@i 2734 [ + + ]: 258 : if (!inquote)
2735 : 129 : inquote = true;
3994 peter_e@gmx.net 2736 [ + + ]: 129 : else if (*ptr == '"')
2737 : : {
2738 : : /* doubled quote within quote sequence */
5504 heikki.linnakangas@i 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;
5493 bruce@momjian.us 2749 : 9476 : *infinite = false;
2750 : : }
2751 : :
5504 heikki.linnakangas@i 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 *
5485 tgl@sss.pgh.pa.us 2764 : 55813 : range_deparse(char flags, const char *lbound_str, const char *ubound_str)
2765 : : {
2766 : : StringInfoData buf;
2767 : :
5504 heikki.linnakangas@i 2768 [ + + ]: 55813 : if (flags & RANGE_EMPTY)
2769 : 8496 : return pstrdup(RANGE_EMPTY_LITERAL);
2770 : :
5492 tgl@sss.pgh.pa.us 2771 : 47317 : initStringInfo(&buf);
2772 : :
5485 2773 [ + + ]: 47317 : appendStringInfoChar(&buf, (flags & RANGE_LB_INC) ? '[' : '(');
2774 : :
5504 heikki.linnakangas@i 2775 [ + + ]: 47317 : if (RANGE_HAS_LBOUND(flags))
2776 : 45922 : appendStringInfoString(&buf, range_bound_escape(lbound_str));
2777 : :
5485 tgl@sss.pgh.pa.us 2778 : 47317 : appendStringInfoChar(&buf, ',');
2779 : :
5504 heikki.linnakangas@i 2780 [ + + ]: 47317 : if (RANGE_HAS_UBOUND(flags))
2781 : 45830 : appendStringInfoString(&buf, range_bound_escape(ubound_str));
2782 : :
5485 tgl@sss.pgh.pa.us 2783 [ + + ]: 47317 : appendStringInfoChar(&buf, (flags & RANGE_UB_INC) ? ']' : ')');
2784 : :
5504 heikki.linnakangas@i 2785 : 47317 : 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 *
5485 tgl@sss.pgh.pa.us 2794 : 91752 : range_bound_escape(const char *value)
2795 : : {
2796 : : bool nq;
2797 : : const char *ptr;
2798 : : StringInfoData buf;
2799 : :
5504 heikki.linnakangas@i 2800 : 91752 : initStringInfo(&buf);
2801 : :
2802 : : /* Detect whether we need double quotes for this value */
2803 : 91752 : nq = (value[0] == '\0'); /* force quotes for empty string */
5485 tgl@sss.pgh.pa.us 2804 [ + + ]: 419040 : for (ptr = value; *ptr; ptr++)
2805 : : {
2806 : 327608 : char ch = *ptr;
2807 : :
5504 heikki.linnakangas@i 2808 [ + + + + : 327608 : if (ch == '"' || ch == '\\' ||
+ + ]
2809 [ + - + + ]: 327508 : ch == '(' || ch == ')' ||
2810 [ + - + + ]: 327492 : ch == '[' || ch == ']' ||
2811 : 327460 : ch == ',' ||
2812 [ + + ]: 327460 : isspace((unsigned char) ch))
2813 : : {
2814 : 320 : nq = true;
2815 : 320 : break;
2816 : : }
2817 : : }
2818 : :
2819 : : /* And emit the string */
2820 [ + + ]: 91752 : if (nq)
2821 : 336 : appendStringInfoChar(&buf, '"');
5485 tgl@sss.pgh.pa.us 2822 [ + + ]: 421708 : for (ptr = value; *ptr; ptr++)
2823 : : {
2824 : 329956 : char ch = *ptr;
2825 : :
5504 heikki.linnakangas@i 2826 [ + + + + ]: 329956 : if (ch == '"' || ch == '\\')
2827 : 112 : appendStringInfoChar(&buf, ch);
2828 : 329956 : appendStringInfoChar(&buf, ch);
2829 : : }
2830 [ + + ]: 91752 : if (nq)
2831 : 336 : appendStringInfoChar(&buf, '"');
2832 : :
2833 : 91752 : 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
2585 peter@eisentraut.org 2843 : 322241 : 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 */
5246 heikki.linnakangas@i 2853 [ - + ]: 322241 : if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
5246 heikki.linnakangas@i 2854 [ # # ]:UBC 0 : elog(ERROR, "range types do not match");
2855 : :
5492 tgl@sss.pgh.pa.us 2856 :CBC 322241 : range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
2857 : 322241 : range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
2858 : :
2859 : : /* If either range is empty, the answer is easy */
5504 heikki.linnakangas@i 2860 [ + + ]: 322241 : if (empty2)
2861 : 209806 : return true;
2862 [ + + ]: 112435 : else if (empty1)
2863 : 9044 : return false;
2864 : :
2865 : : /* Else we must have lower1 <= lower2 and upper1 >= upper2 */
5492 tgl@sss.pgh.pa.us 2866 [ + + ]: 103391 : if (range_cmp_bounds(typcache, &lower1, &lower2) > 0)
5504 heikki.linnakangas@i 2867 : 49558 : return false;
5492 tgl@sss.pgh.pa.us 2868 [ + + ]: 53833 : if (range_cmp_bounds(typcache, &upper1, &upper2) < 0)
5504 heikki.linnakangas@i 2869 : 48326 : return false;
2870 : :
5485 tgl@sss.pgh.pa.us 2871 : 5507 : return true;
2872 : : }
2873 : :
2874 : : bool
2585 peter@eisentraut.org 2875 : 81264 : range_contained_by_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
2876 : : {
5246 heikki.linnakangas@i 2877 : 81264 : return range_contains_internal(typcache, r2, r1);
2878 : : }
2879 : :
2880 : : /*
2881 : : * Test whether range r contains a specific element value.
2882 : : */
2883 : : bool
2585 peter@eisentraut.org 2884 : 59458 : 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 : :
5485 tgl@sss.pgh.pa.us 2891 : 59458 : range_deserialize(typcache, r, &lower, &upper, &empty);
2892 : :
2893 [ + + ]: 59458 : if (empty)
2894 : 8576 : return false;
2895 : :
2896 [ + + ]: 50882 : if (!lower.infinite)
2897 : : {
2898 : 48046 : cmp = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
2899 : : typcache->rng_collation,
2900 : : lower.val, val));
2901 [ + + ]: 48046 : if (cmp > 0)
2902 : 46487 : return false;
2903 [ + + - + ]: 1559 : if (cmp == 0 && !lower.inclusive)
5485 tgl@sss.pgh.pa.us 2904 :UBC 0 : return false;
2905 : : }
2906 : :
5485 tgl@sss.pgh.pa.us 2907 [ + + ]:CBC 4395 : if (!upper.infinite)
2908 : : {
2909 : 4344 : cmp = DatumGetInt32(FunctionCall2Coll(&typcache->rng_cmp_proc_finfo,
2910 : : typcache->rng_collation,
2911 : : upper.val, val));
2912 [ + + ]: 4344 : if (cmp < 0)
2913 : 321 : return false;
2914 [ - + - - ]: 4023 : if (cmp == 0 && !upper.inclusive)
5485 tgl@sss.pgh.pa.us 2915 :UBC 0 : return false;
2916 : : }
2917 : :
5504 heikki.linnakangas@i 2918 :CBC 4074 : 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 : 1208640 : datum_compute_size(Size data_length, Datum val, bool typbyval, char typalign,
2941 : : int16 typlen, char typstorage)
2942 : : {
2943 [ + + + - : 1216902 : 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 [ + + + + ]: 1201235 : data_length = att_align_datum(data_length, typalign, typlen, val);
2955 [ + + + - : 1201235 : data_length = att_addlength_datum(data_length, typlen, val);
- - ]
2956 : : }
2957 : :
2958 : 1208640 : 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 *
360 peter@eisentraut.org 2966 : 1208640 : datum_write(char *ptr, Datum datum, bool typbyval, char typalign,
2967 : : int16 typlen, char typstorage)
2968 : : {
2969 : : Size data_length;
2970 : :
5504 heikki.linnakangas@i 2971 [ + + ]: 1208640 : if (typbyval)
2972 : : {
2973 : : /* pass-by-value */
2974 : 1200378 : ptr = (char *) att_align_nominal(ptr, typalign);
2975 : 1200378 : store_att_byval(ptr, datum, typlen);
2976 : 1200378 : 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 : : */
5493 tgl@sss.pgh.pa.us 2989 [ # # ]:UBC 0 : elog(ERROR, "cannot store a toast pointer inside a range");
2990 : : data_length = 0; /* keep compiler quiet */
2991 : : }
5504 heikki.linnakangas@i 2992 [ + + ]:CBC 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 : : }
5504 heikki.linnakangas@i 3014 [ # # ]:UBC 0 : else if (typlen == -2)
3015 : : {
3016 : : /* cstring ... never needs alignment */
2460 tgl@sss.pgh.pa.us 3017 [ # # ]: 0 : Assert(typalign == TYPALIGN_CHAR);
5504 heikki.linnakangas@i 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 [ # # ]: 0 : Assert(typlen > 0);
3026 : 0 : data_length = typlen;
3027 : 0 : memcpy(ptr, DatumGetPointer(datum), data_length);
3028 : : }
3029 : :
5504 heikki.linnakangas@i 3030 :CBC 1208640 : ptr += data_length;
3031 : :
3032 : 1208640 : 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 *
1043 tgl@sss.pgh.pa.us 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)
1043 tgl@sss.pgh.pa.us 3059 [ # # ]:UBC 0 : elog(ERROR, "type %u is not a range type", RangeTypeGetOid(range));
3060 : :
1043 tgl@sss.pgh.pa.us 3061 :CBC 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))
1043 tgl@sss.pgh.pa.us 3102 :UBC 0 : return NULL;
1043 tgl@sss.pgh.pa.us 3103 :CBC 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)
1043 tgl@sss.pgh.pa.us 3106 :UBC 0 : return NULL;
3107 : : }
3108 : :
3109 : : /* Okay, try to build boundary comparison expressions */
1043 tgl@sss.pgh.pa.us 3110 [ + + ]:CBC 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)
1043 tgl@sss.pgh.pa.us 3120 :UBC 0 : return NULL;
3121 : : }
3122 : :
1043 tgl@sss.pgh.pa.us 3123 [ + + ]:CBC 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)
1043 tgl@sss.pgh.pa.us 3136 :UBC 0 : return NULL;
3137 : : }
3138 : :
1043 tgl@sss.pgh.pa.us 3139 [ + + + + ]:CBC 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 : : {
1043 tgl@sss.pgh.pa.us 3147 :UBC 0 : Assert(false);
3148 : : 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 *
1043 tgl@sss.pgh.pa.us 3165 :CBC 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))
1043 tgl@sss.pgh.pa.us 3192 :UBC 0 : return NULL;
3193 : :
3194 : : /* OK, convert "val" to a full-fledged Const node, and make the OpExpr */
1043 tgl@sss.pgh.pa.us 3195 :CBC 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 : : }
|