Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
rangetypes.h File Reference
#include "utils/typcache.h"
Include dependency graph for rangetypes.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RangeType
 
struct  RangeBound
 

Macros

#define RANGE_EMPTY_LITERAL   "empty"
 
#define RangeTypeGetOid(r)   ((r)->rangetypid)
 
#define RANGE_EMPTY   0x01 /* range is empty */
 
#define RANGE_LB_INC   0x02 /* lower bound is inclusive */
 
#define RANGE_UB_INC   0x04 /* upper bound is inclusive */
 
#define RANGE_LB_INF   0x08 /* lower bound is -infinity */
 
#define RANGE_UB_INF   0x10 /* upper bound is +infinity */
 
#define RANGE_LB_NULL   0x20 /* lower bound is null (NOT USED) */
 
#define RANGE_UB_NULL   0x40 /* upper bound is null (NOT USED) */
 
#define RANGE_CONTAIN_EMPTY
 
#define RANGE_HAS_LBOUND(flags)
 
#define RANGE_HAS_UBOUND(flags)
 
#define RangeIsEmpty(r)   ((range_get_flags(r) & RANGE_EMPTY) != 0)
 
#define RangeIsOrContainsEmpty(r)    ((range_get_flags(r) & (RANGE_EMPTY | RANGE_CONTAIN_EMPTY)) != 0)
 
#define PG_GETARG_RANGE_P(n)   DatumGetRangeTypeP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_RANGE_P_COPY(n)   DatumGetRangeTypePCopy(PG_GETARG_DATUM(n))
 
#define PG_RETURN_RANGE_P(x)   return RangeTypePGetDatum(x)
 
#define RANGESTRAT_BEFORE   RTLeftStrategyNumber
 
#define RANGESTRAT_OVERLEFT   RTOverLeftStrategyNumber
 
#define RANGESTRAT_OVERLAPS   RTOverlapStrategyNumber
 
#define RANGESTRAT_OVERRIGHT   RTOverRightStrategyNumber
 
#define RANGESTRAT_AFTER   RTRightStrategyNumber
 
#define RANGESTRAT_ADJACENT   RTSameStrategyNumber
 
#define RANGESTRAT_CONTAINS   RTContainsStrategyNumber
 
#define RANGESTRAT_CONTAINED_BY   RTContainedByStrategyNumber
 
#define RANGESTRAT_CONTAINS_ELEM   RTContainsElemStrategyNumber
 
#define RANGESTRAT_EQ   RTEqualStrategyNumber
 

Functions

static RangeTypeDatumGetRangeTypeP (Datum X)
 
static RangeTypeDatumGetRangeTypePCopy (Datum X)
 
static Datum RangeTypePGetDatum (const RangeType *X)
 
bool range_contains_elem_internal (TypeCacheEntry *typcache, const RangeType *r, Datum val)
 
bool range_eq_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_ne_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_contains_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_contained_by_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_before_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_after_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_adjacent_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_overlaps_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_overleft_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
bool range_overright_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
RangeTyperange_union_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2, bool strict)
 
RangeTyperange_minus_internal (TypeCacheEntry *typcache, RangeType *r1, RangeType *r2)
 
RangeTyperange_intersect_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
 
TypeCacheEntryrange_get_typcache (FunctionCallInfo fcinfo, Oid rngtypid)
 
RangeTyperange_serialize (TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty, struct Node *escontext)
 
void range_deserialize (TypeCacheEntry *typcache, const RangeType *range, RangeBound *lower, RangeBound *upper, bool *empty)
 
char range_get_flags (const RangeType *range)
 
void range_set_contain_empty (RangeType *range)
 
RangeTypemake_range (TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty, struct Node *escontext)
 
int range_cmp_bounds (TypeCacheEntry *typcache, const RangeBound *b1, const RangeBound *b2)
 
int range_cmp_bound_values (TypeCacheEntry *typcache, const RangeBound *b1, const RangeBound *b2)
 
int range_compare (const void *key1, const void *key2, void *arg)
 
bool bounds_adjacent (TypeCacheEntry *typcache, RangeBound boundA, RangeBound boundB)
 
RangeTypemake_empty_range (TypeCacheEntry *typcache)
 
bool range_split_internal (TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2, RangeType **output1, RangeType **output2)
 

Macro Definition Documentation

◆ PG_GETARG_RANGE_P

#define PG_GETARG_RANGE_P (   n)    DatumGetRangeTypeP(PG_GETARG_DATUM(n))

Definition at line 90 of file rangetypes.h.

◆ PG_GETARG_RANGE_P_COPY

#define PG_GETARG_RANGE_P_COPY (   n)    DatumGetRangeTypePCopy(PG_GETARG_DATUM(n))

Definition at line 91 of file rangetypes.h.

◆ PG_RETURN_RANGE_P

#define PG_RETURN_RANGE_P (   x)    return RangeTypePGetDatum(x)

Definition at line 92 of file rangetypes.h.

◆ RANGE_CONTAIN_EMPTY

#define RANGE_CONTAIN_EMPTY
Value:
0x80 /* marks a GiST internal-page entry whose
* subtree contains some empty ranges */

Definition at line 45 of file rangetypes.h.

◆ RANGE_EMPTY

#define RANGE_EMPTY   0x01 /* range is empty */

Definition at line 38 of file rangetypes.h.

◆ RANGE_EMPTY_LITERAL

#define RANGE_EMPTY_LITERAL   "empty"

Definition at line 32 of file rangetypes.h.

◆ RANGE_HAS_LBOUND

#define RANGE_HAS_LBOUND (   flags)
Value:
(!((flags) & (RANGE_EMPTY | \
#define RANGE_EMPTY
Definition: rangetypes.h:38
#define RANGE_LB_NULL
Definition: rangetypes.h:43
#define RANGE_LB_INF
Definition: rangetypes.h:41

Definition at line 47 of file rangetypes.h.

◆ RANGE_HAS_UBOUND

#define RANGE_HAS_UBOUND (   flags)
Value:
(!((flags) & (RANGE_EMPTY | \
#define RANGE_UB_INF
Definition: rangetypes.h:42
#define RANGE_UB_NULL
Definition: rangetypes.h:44

Definition at line 51 of file rangetypes.h.

◆ RANGE_LB_INC

#define RANGE_LB_INC   0x02 /* lower bound is inclusive */

Definition at line 39 of file rangetypes.h.

◆ RANGE_LB_INF

#define RANGE_LB_INF   0x08 /* lower bound is -infinity */

Definition at line 41 of file rangetypes.h.

◆ RANGE_LB_NULL

#define RANGE_LB_NULL   0x20 /* lower bound is null (NOT USED) */

Definition at line 43 of file rangetypes.h.

◆ RANGE_UB_INC

#define RANGE_UB_INC   0x04 /* upper bound is inclusive */

Definition at line 40 of file rangetypes.h.

◆ RANGE_UB_INF

#define RANGE_UB_INF   0x10 /* upper bound is +infinity */

Definition at line 42 of file rangetypes.h.

◆ RANGE_UB_NULL

#define RANGE_UB_NULL   0x40 /* upper bound is null (NOT USED) */

Definition at line 44 of file rangetypes.h.

◆ RangeIsEmpty

#define RangeIsEmpty (   r)    ((range_get_flags(r) & RANGE_EMPTY) != 0)

Definition at line 55 of file rangetypes.h.

◆ RangeIsOrContainsEmpty

#define RangeIsOrContainsEmpty (   r)     ((range_get_flags(r) & (RANGE_EMPTY | RANGE_CONTAIN_EMPTY)) != 0)

Definition at line 56 of file rangetypes.h.

◆ RANGESTRAT_ADJACENT

#define RANGESTRAT_ADJACENT   RTSameStrategyNumber

Definition at line 101 of file rangetypes.h.

◆ RANGESTRAT_AFTER

#define RANGESTRAT_AFTER   RTRightStrategyNumber

Definition at line 100 of file rangetypes.h.

◆ RANGESTRAT_BEFORE

#define RANGESTRAT_BEFORE   RTLeftStrategyNumber

Definition at line 96 of file rangetypes.h.

◆ RANGESTRAT_CONTAINED_BY

#define RANGESTRAT_CONTAINED_BY   RTContainedByStrategyNumber

Definition at line 103 of file rangetypes.h.

◆ RANGESTRAT_CONTAINS

#define RANGESTRAT_CONTAINS   RTContainsStrategyNumber

Definition at line 102 of file rangetypes.h.

◆ RANGESTRAT_CONTAINS_ELEM

#define RANGESTRAT_CONTAINS_ELEM   RTContainsElemStrategyNumber

Definition at line 104 of file rangetypes.h.

◆ RANGESTRAT_EQ

#define RANGESTRAT_EQ   RTEqualStrategyNumber

Definition at line 105 of file rangetypes.h.

◆ RANGESTRAT_OVERLAPS

#define RANGESTRAT_OVERLAPS   RTOverlapStrategyNumber

Definition at line 98 of file rangetypes.h.

◆ RANGESTRAT_OVERLEFT

#define RANGESTRAT_OVERLEFT   RTOverLeftStrategyNumber

Definition at line 97 of file rangetypes.h.

◆ RANGESTRAT_OVERRIGHT

#define RANGESTRAT_OVERRIGHT   RTOverRightStrategyNumber

Definition at line 99 of file rangetypes.h.

◆ RangeTypeGetOid

#define RangeTypeGetOid (   r)    ((r)->rangetypid)

Definition at line 35 of file rangetypes.h.

Function Documentation

◆ bounds_adjacent()

bool bounds_adjacent ( TypeCacheEntry typcache,
RangeBound  boundA,
RangeBound  boundB 
)

Definition at line 757 of file rangetypes.c.

758{
759 int cmp;
760
761 Assert(!boundA.lower && boundB.lower);
762
763 cmp = range_cmp_bound_values(typcache, &boundA, &boundB);
764 if (cmp < 0)
765 {
766 RangeType *r;
767
768 /*
769 * Bounds do not overlap; see if there are points in between.
770 */
771
772 /* in a continuous subtype, there are assumed to be points between */
773 if (!OidIsValid(typcache->rng_canonical_finfo.fn_oid))
774 return false;
775
776 /*
777 * The bounds are of a discrete range type; so make a range A..B and
778 * see if it's empty.
779 */
780
781 /* flip the inclusion flags */
782 boundA.inclusive = !boundA.inclusive;
783 boundB.inclusive = !boundB.inclusive;
784 /* change upper/lower labels to avoid Assert failures */
785 boundA.lower = true;
786 boundB.lower = false;
787 r = make_range(typcache, &boundA, &boundB, false, NULL);
788 return RangeIsEmpty(r);
789 }
790 else if (cmp == 0)
791 return boundA.inclusive != boundB.inclusive;
792 else
793 return false; /* bounds overlap */
794}
#define OidIsValid(objectId)
Definition: c.h:774
Assert(PointerIsAligned(start, uint64))
RangeType * make_range(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty, struct Node *escontext)
Definition: rangetypes.c:2014
int range_cmp_bound_values(TypeCacheEntry *typcache, const RangeBound *b1, const RangeBound *b2)
Definition: rangetypes.c:2152
#define RangeIsEmpty(r)
Definition: rangetypes.h:55
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
Oid fn_oid
Definition: fmgr.h:59
bool lower
Definition: rangetypes.h:66
bool inclusive
Definition: rangetypes.h:65
FmgrInfo rng_canonical_finfo
Definition: typcache.h:103

References Assert(), cmp(), FmgrInfo::fn_oid, RangeBound::inclusive, RangeBound::lower, make_range(), OidIsValid, range_cmp_bound_values(), RangeIsEmpty, and TypeCacheEntry::rng_canonical_finfo.

Referenced by adjacent_cmp_bounds(), multirange_adjacent_multirange(), range_adjacent_internal(), and range_adjacent_multirange_internal().

◆ DatumGetRangeTypeP()

◆ DatumGetRangeTypePCopy()

static RangeType * DatumGetRangeTypePCopy ( Datum  X)
inlinestatic

Definition at line 79 of file rangetypes.h.

81{
82 return (RangeType *) PG_DETOAST_DATUM_COPY(X);
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242

References PG_DETOAST_DATUM_COPY.

◆ make_empty_range()

RangeType * make_empty_range ( TypeCacheEntry typcache)

Definition at line 2227 of file rangetypes.c.

2228{
2231
2232 lower.val = (Datum) 0;
2233 lower.infinite = false;
2234 lower.inclusive = false;
2235 lower.lower = true;
2236
2237 upper.val = (Datum) 0;
2238 upper.infinite = false;
2239 upper.inclusive = false;
2240 upper.lower = false;
2241
2242 return make_range(typcache, &lower, &upper, true, NULL);
2243}
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
uint64_t Datum
Definition: postgres.h:70

References lower(), make_range(), and upper().

Referenced by multirange_agg_transfn(), multirange_get_union_range(), range_intersect_internal(), range_merge_from_multirange(), and range_minus_internal().

◆ make_range()

RangeType * make_range ( TypeCacheEntry typcache,
RangeBound lower,
RangeBound upper,
bool  empty,
struct Node escontext 
)

Definition at line 2014 of file rangetypes.c.

2016{
2018
2019 range = range_serialize(typcache, lower, upper, empty, escontext);
2020
2021 if (SOFT_ERROR_OCCURRED(escontext))
2022 return NULL;
2023
2024 /* no need to call canonical on empty ranges ... */
2025 if (OidIsValid(typcache->rng_canonical_finfo.fn_oid) &&
2027 {
2028 /* Do this the hard way so that we can pass escontext */
2029 LOCAL_FCINFO(fcinfo, 1);
2030 Datum result;
2031
2032 InitFunctionCallInfoData(*fcinfo, &typcache->rng_canonical_finfo, 1,
2033 InvalidOid, escontext, NULL);
2034
2035 fcinfo->args[0].value = RangeTypePGetDatum(range);
2036 fcinfo->args[0].isnull = false;
2037
2038 result = FunctionCallInvoke(fcinfo);
2039
2040 if (SOFT_ERROR_OCCURRED(escontext))
2041 return NULL;
2042
2043 /* Should not get a null result if there was no error */
2044 if (fcinfo->isnull)
2045 elog(ERROR, "function %u returned NULL",
2046 typcache->rng_canonical_finfo.fn_oid);
2047
2048 range = DatumGetRangeTypeP(result);
2049 }
2050
2051 return range;
2052}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
#define SOFT_ERROR_OCCURRED(escontext)
Definition: miscnodes.h:53
#define InvalidOid
Definition: postgres_ext.h:37
RangeType * range_serialize(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty, struct Node *escontext)
Definition: rangetypes.c:1789
static RangeType * DatumGetRangeTypeP(Datum X)
Definition: rangetypes.h:73
static Datum RangeTypePGetDatum(const RangeType *X)
Definition: rangetypes.h:85
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

References DatumGetRangeTypeP(), elog, ERROR, FmgrInfo::fn_oid, FunctionCallInvoke, InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, lower(), OidIsValid, range(), range_serialize(), RangeIsEmpty, RangeTypePGetDatum(), TypeCacheEntry::rng_canonical_finfo, SOFT_ERROR_OCCURRED, and upper().

Referenced by bounds_adjacent(), make_empty_range(), multirange_get_union_range(), range_constructor2(), range_constructor3(), range_in(), range_intersect_internal(), range_merge_from_multirange(), range_minus_internal(), range_recv(), range_split_internal(), range_super_union(), and range_union_internal().

◆ range_adjacent_internal()

bool range_adjacent_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 798 of file rangetypes.c.

799{
800 RangeBound lower1,
801 lower2;
802 RangeBound upper1,
803 upper2;
804 bool empty1,
805 empty2;
806
807 /* Different types should be prevented by ANYRANGE matching rules */
808 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
809 elog(ERROR, "range types do not match");
810
811 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
812 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
813
814 /* An empty range is not adjacent to any other range */
815 if (empty1 || empty2)
816 return false;
817
818 /*
819 * Given two ranges A..B and C..D, the ranges are adjacent if and only if
820 * B is adjacent to C, or D is adjacent to A.
821 */
822 return (bounds_adjacent(typcache, upper1, lower2) ||
823 bounds_adjacent(typcache, upper2, lower1));
824}
bool bounds_adjacent(TypeCacheEntry *typcache, RangeBound boundA, RangeBound boundB)
Definition: rangetypes.c:757
void range_deserialize(TypeCacheEntry *typcache, const RangeType *range, RangeBound *lower, RangeBound *upper, bool *empty)
Definition: rangetypes.c:1918
#define RangeTypeGetOid(r)
Definition: rangetypes.h:35

References bounds_adjacent(), elog, ERROR, range_deserialize(), and RangeTypeGetOid.

Referenced by multirange_canonicalize(), range_adjacent(), range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), range_union_internal(), and spg_range_quad_leaf_consistent().

◆ range_after_internal()

bool range_after_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 702 of file rangetypes.c.

703{
704 RangeBound lower1,
705 lower2;
706 RangeBound upper1,
707 upper2;
708 bool empty1,
709 empty2;
710
711 /* Different types should be prevented by ANYRANGE matching rules */
712 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
713 elog(ERROR, "range types do not match");
714
715 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
716 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
717
718 /* An empty range is neither before nor after any other range */
719 if (empty1 || empty2)
720 return false;
721
722 return (range_cmp_bounds(typcache, &lower1, &upper2) > 0);
723}
int range_cmp_bounds(TypeCacheEntry *typcache, const RangeBound *b1, const RangeBound *b2)
Definition: rangetypes.c:2078

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_after(), range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), and spg_range_quad_leaf_consistent().

◆ range_before_internal()

bool range_before_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 664 of file rangetypes.c.

665{
666 RangeBound lower1,
667 lower2;
668 RangeBound upper1,
669 upper2;
670 bool empty1,
671 empty2;
672
673 /* Different types should be prevented by ANYRANGE matching rules */
674 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
675 elog(ERROR, "range types do not match");
676
677 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
678 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
679
680 /* An empty range is neither before nor after any other range */
681 if (empty1 || empty2)
682 return false;
683
684 return (range_cmp_bounds(typcache, &upper1, &lower2) < 0);
685}

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by multirange_canonicalize(), multirange_intersect_internal(), multirange_minus_internal(), range_before(), range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), and spg_range_quad_leaf_consistent().

◆ range_cmp_bound_values()

int range_cmp_bound_values ( TypeCacheEntry typcache,
const RangeBound b1,
const RangeBound b2 
)

Definition at line 2152 of file rangetypes.c.

2154{
2155 /*
2156 * First, handle cases involving infinity, which don't require invoking
2157 * the comparison proc.
2158 */
2159 if (b1->infinite && b2->infinite)
2160 {
2161 /*
2162 * Both are infinity, so they are equal unless one is lower and the
2163 * other not.
2164 */
2165 if (b1->lower == b2->lower)
2166 return 0;
2167 else
2168 return b1->lower ? -1 : 1;
2169 }
2170 else if (b1->infinite)
2171 return b1->lower ? -1 : 1;
2172 else if (b2->infinite)
2173 return b2->lower ? 1 : -1;
2174
2175 /*
2176 * Both boundaries are finite, so compare the held values.
2177 */
2179 typcache->rng_collation,
2180 b1->val, b2->val));
2181}
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:212
bool infinite
Definition: rangetypes.h:64
Datum val
Definition: rangetypes.h:63
FmgrInfo rng_cmp_proc_finfo
Definition: typcache.h:102
Oid rng_collation
Definition: typcache.h:101

References DatumGetInt32(), FunctionCall2Coll(), RangeBound::infinite, RangeBound::lower, TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, and RangeBound::val.

Referenced by bounds_adjacent(), and range_serialize().

◆ range_cmp_bounds()

int range_cmp_bounds ( TypeCacheEntry typcache,
const RangeBound b1,
const RangeBound b2 
)

Definition at line 2078 of file rangetypes.c.

2079{
2080 int32 result;
2081
2082 /*
2083 * First, handle cases involving infinity, which don't require invoking
2084 * the comparison proc.
2085 */
2086 if (b1->infinite && b2->infinite)
2087 {
2088 /*
2089 * Both are infinity, so they are equal unless one is lower and the
2090 * other not.
2091 */
2092 if (b1->lower == b2->lower)
2093 return 0;
2094 else
2095 return b1->lower ? -1 : 1;
2096 }
2097 else if (b1->infinite)
2098 return b1->lower ? -1 : 1;
2099 else if (b2->infinite)
2100 return b2->lower ? 1 : -1;
2101
2102 /*
2103 * Both boundaries are finite, so compare the held values.
2104 */
2106 typcache->rng_collation,
2107 b1->val, b2->val));
2108
2109 /*
2110 * If the comparison is anything other than equal, we're done. If they
2111 * compare equal though, we still have to consider whether the boundaries
2112 * are inclusive or exclusive.
2113 */
2114 if (result == 0)
2115 {
2116 if (!b1->inclusive && !b2->inclusive)
2117 {
2118 /* both are exclusive */
2119 if (b1->lower == b2->lower)
2120 return 0;
2121 else
2122 return b1->lower ? 1 : -1;
2123 }
2124 else if (!b1->inclusive)
2125 return b1->lower ? 1 : -1;
2126 else if (!b2->inclusive)
2127 return b2->lower ? -1 : 1;
2128 else
2129 {
2130 /*
2131 * Both are inclusive and the values held are equal, so they are
2132 * equal regardless of whether they are upper or lower boundaries,
2133 * or a mix.
2134 */
2135 return 0;
2136 }
2137 }
2138
2139 return result;
2140}
int32_t int32
Definition: c.h:534

References DatumGetInt32(), FunctionCall2Coll(), RangeBound::inclusive, RangeBound::infinite, RangeBound::lower, TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, and RangeBound::val.

Referenced by adjacent_cmp_bounds(), adjacent_inner_consistent(), bound_cmp(), calc_hist_selectivity_contained(), getQuadrant(), interval_cmp_lower(), interval_cmp_upper(), multirange_before_multirange_internal(), multirange_cmp(), multirange_contains_multirange_internal(), multirange_eq_internal(), multirange_overlaps_multirange_internal(), multirange_overleft_multirange(), multirange_overleft_range(), multirange_overright_multirange(), multirange_overright_range(), multirange_range_contains_bsearch_comparison(), multirange_range_overlaps_bsearch_comparison(), multirange_union_range_equal(), range_after_internal(), range_after_multirange_internal(), range_before_internal(), range_before_multirange_internal(), range_bound_qsort_cmp(), range_bounds_contains(), range_bounds_overlaps(), range_cmp(), range_compare(), range_contains_internal(), range_eq_internal(), range_fast_cmp(), range_gist_double_sorting_split(), range_gist_penalty(), range_intersect_internal(), range_minus_internal(), range_overlaps_internal(), range_overleft_internal(), range_overleft_multirange_internal(), range_overright_internal(), range_overright_multirange_internal(), range_split_internal(), range_super_union(), range_union_internal(), rbound_bsearch(), single_bound_cmp(), and spg_range_quad_inner_consistent().

◆ range_compare()

int range_compare ( const void *  key1,
const void *  key2,
void *  arg 
)

Definition at line 2191 of file rangetypes.c.

2192{
2193 RangeType *r1 = *(RangeType **) key1;
2194 RangeType *r2 = *(RangeType **) key2;
2195 TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
2196 RangeBound lower1;
2197 RangeBound upper1;
2198 RangeBound lower2;
2199 RangeBound upper2;
2200 bool empty1;
2201 bool empty2;
2202 int cmp;
2203
2204 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
2205 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
2206
2207 if (empty1 && empty2)
2208 cmp = 0;
2209 else if (empty1)
2210 cmp = -1;
2211 else if (empty2)
2212 cmp = 1;
2213 else
2214 {
2215 cmp = range_cmp_bounds(typcache, &lower1, &lower2);
2216 if (cmp == 0)
2217 cmp = range_cmp_bounds(typcache, &upper1, &upper2);
2218 }
2219
2220 return cmp;
2221}
void * arg

References arg, cmp(), range_cmp_bounds(), and range_deserialize().

Referenced by multirange_canonicalize().

◆ range_contained_by_internal()

bool range_contained_by_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 2680 of file rangetypes.c.

2681{
2682 return range_contains_internal(typcache, r2, r1);
2683}
bool range_contains_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
Definition: rangetypes.c:2648

References range_contains_internal().

Referenced by range_contained_by(), range_gist_consistent_leaf_range(), and spg_range_quad_leaf_consistent().

◆ range_contains_elem_internal()

bool range_contains_elem_internal ( TypeCacheEntry typcache,
const RangeType r,
Datum  val 
)

Definition at line 2689 of file rangetypes.c.

2690{
2693 bool empty;
2694 int32 cmp;
2695
2696 range_deserialize(typcache, r, &lower, &upper, &empty);
2697
2698 if (empty)
2699 return false;
2700
2701 if (!lower.infinite)
2702 {
2704 typcache->rng_collation,
2705 lower.val, val));
2706 if (cmp > 0)
2707 return false;
2708 if (cmp == 0 && !lower.inclusive)
2709 return false;
2710 }
2711
2712 if (!upper.infinite)
2713 {
2715 typcache->rng_collation,
2716 upper.val, val));
2717 if (cmp < 0)
2718 return false;
2719 if (cmp == 0 && !upper.inclusive)
2720 return false;
2721 }
2722
2723 return true;
2724}
long val
Definition: informix.c:689

References cmp(), DatumGetInt32(), FunctionCall2Coll(), lower(), range_deserialize(), TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, upper(), and val.

Referenced by elem_contained_by_range(), range_contains_elem(), range_gist_consistent_int_element(), range_gist_consistent_leaf_element(), and spg_range_quad_leaf_consistent().

◆ range_contains_internal()

bool range_contains_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 2648 of file rangetypes.c.

2649{
2650 RangeBound lower1;
2651 RangeBound upper1;
2652 bool empty1;
2653 RangeBound lower2;
2654 RangeBound upper2;
2655 bool empty2;
2656
2657 /* Different types should be prevented by ANYRANGE matching rules */
2658 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
2659 elog(ERROR, "range types do not match");
2660
2661 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
2662 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
2663
2664 /* If either range is empty, the answer is easy */
2665 if (empty2)
2666 return true;
2667 else if (empty1)
2668 return false;
2669
2670 /* Else we must have lower1 <= lower2 and upper1 >= upper2 */
2671 if (range_cmp_bounds(typcache, &lower1, &lower2) > 0)
2672 return false;
2673 if (range_cmp_bounds(typcache, &upper1, &upper2) < 0)
2674 return false;
2675
2676 return true;
2677}

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_contained_by_internal(), range_contains(), range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), and spg_range_quad_leaf_consistent().

◆ range_deserialize()

void range_deserialize ( TypeCacheEntry typcache,
const RangeType range,
RangeBound lower,
RangeBound upper,
bool *  empty 
)

Definition at line 1918 of file rangetypes.c.

1920{
1921 char flags;
1922 int16 typlen;
1923 bool typbyval;
1924 char typalign;
1925 Pointer ptr;
1926 Datum lbound;
1927 Datum ubound;
1928
1929 /* assert caller passed the right typcache entry */
1930 Assert(RangeTypeGetOid(range) == typcache->type_id);
1931
1932 /* fetch the flag byte from datum's last byte */
1933 flags = *((const char *) range + VARSIZE(range) - 1);
1934
1935 /* fetch information about range's element type */
1936 typlen = typcache->rngelemtype->typlen;
1937 typbyval = typcache->rngelemtype->typbyval;
1938 typalign = typcache->rngelemtype->typalign;
1939
1940 /* initialize data pointer just after the range OID */
1941 ptr = (Pointer) (range + 1);
1942
1943 /* fetch lower bound, if any */
1944 if (RANGE_HAS_LBOUND(flags))
1945 {
1946 /* att_align_pointer cannot be necessary here */
1947 lbound = fetch_att(ptr, typbyval, typlen);
1948 ptr = (Pointer) att_addlength_pointer(ptr, typlen, ptr);
1949 }
1950 else
1951 lbound = (Datum) 0;
1952
1953 /* fetch upper bound, if any */
1954 if (RANGE_HAS_UBOUND(flags))
1955 {
1956 ptr = (Pointer) att_align_pointer(ptr, typalign, typlen, ptr);
1957 ubound = fetch_att(ptr, typbyval, typlen);
1958 /* no need for att_addlength_pointer */
1959 }
1960 else
1961 ubound = (Datum) 0;
1962
1963 /* emit results */
1964
1965 *empty = (flags & RANGE_EMPTY) != 0;
1966
1967 lower->val = lbound;
1968 lower->infinite = (flags & RANGE_LB_INF) != 0;
1969 lower->inclusive = (flags & RANGE_LB_INC) != 0;
1970 lower->lower = true;
1971
1972 upper->val = ubound;
1973 upper->infinite = (flags & RANGE_UB_INF) != 0;
1974 upper->inclusive = (flags & RANGE_UB_INC) != 0;
1975 upper->lower = false;
1976}
char * Pointer
Definition: c.h:529
int16_t int16
Definition: c.h:533
char typalign
Definition: pg_type.h:176
#define RANGE_HAS_UBOUND(flags)
Definition: rangetypes.h:51
#define RANGE_UB_INC
Definition: rangetypes.h:40
#define RANGE_LB_INC
Definition: rangetypes.h:39
#define RANGE_HAS_LBOUND(flags)
Definition: rangetypes.h:47
char typalign
Definition: typcache.h:41
struct TypeCacheEntry * rngelemtype
Definition: typcache.h:99
bool typbyval
Definition: typcache.h:40
int16 typlen
Definition: typcache.h:39
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:113
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:180
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition: tupmacs.h:50
static Size VARSIZE(const void *PTR)
Definition: varatt.h:298

References Assert(), att_addlength_pointer, att_align_pointer, fetch_att(), lower(), range(), RANGE_EMPTY, RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RANGE_LB_INC, RANGE_LB_INF, RANGE_UB_INC, RANGE_UB_INF, RangeTypeGetOid, TypeCacheEntry::rngelemtype, typalign, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::type_id, TypeCacheEntry::typlen, upper(), and VARSIZE().

Referenced by calc_hist_selectivity(), compute_range_stats(), daterange_canonical(), find_simplified_clause(), getQuadrant(), hash_range(), hash_range_extended(), int4range_canonical(), int8range_canonical(), multirange_contains_range_internal(), multirange_overleft_range(), multirange_overright_range(), multirange_union_range_equal(), range_adjacent_internal(), range_adjacent_multirange_internal(), range_after_internal(), range_after_multirange_internal(), range_before_internal(), range_before_multirange_internal(), range_cmp(), range_compare(), range_contains_elem_internal(), range_contains_internal(), range_contains_multirange_internal(), range_eq_internal(), range_fast_cmp(), range_gist_double_sorting_split(), range_gist_penalty(), range_gist_single_sorting_split(), range_intersect_internal(), range_lower(), range_minus_internal(), range_out(), range_overlaps_internal(), range_overlaps_multirange_internal(), range_overleft_internal(), range_overleft_multirange_internal(), range_overright_internal(), range_overright_multirange_internal(), range_send(), range_split_internal(), range_super_union(), range_union_internal(), range_upper(), spg_range_quad_inner_consistent(), and spg_range_quad_picksplit().

◆ range_eq_internal()

bool range_eq_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 573 of file rangetypes.c.

574{
575 RangeBound lower1,
576 lower2;
577 RangeBound upper1,
578 upper2;
579 bool empty1,
580 empty2;
581
582 /* Different types should be prevented by ANYRANGE matching rules */
583 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
584 elog(ERROR, "range types do not match");
585
586 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
587 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
588
589 if (empty1 && empty2)
590 return true;
591 if (empty1 != empty2)
592 return false;
593
594 if (range_cmp_bounds(typcache, &lower1, &lower2) != 0)
595 return false;
596
597 if (range_cmp_bounds(typcache, &upper1, &upper2) != 0)
598 return false;
599
600 return true;
601}

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_eq(), range_gist_consistent_leaf_range(), range_gist_same(), range_ne_internal(), and spg_range_quad_leaf_consistent().

◆ range_get_flags()

char range_get_flags ( const RangeType range)

Definition at line 1985 of file rangetypes.c.

1986{
1987 /* fetch the flag byte from datum's last byte */
1988 return *((char *) range + VARSIZE(range) - 1);
1989}

References range(), and VARSIZE().

Referenced by get_gist_range_class(), hash_range(), hash_range_extended(), range_empty(), range_gist_same(), range_lower_inc(), range_lower_inf(), range_out(), range_send(), range_super_union(), range_upper_inc(), and range_upper_inf().

◆ range_get_typcache()

TypeCacheEntry * range_get_typcache ( FunctionCallInfo  fcinfo,
Oid  rngtypid 
)

Definition at line 1765 of file rangetypes.c.

1766{
1767 TypeCacheEntry *typcache = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
1768
1769 if (typcache == NULL ||
1770 typcache->type_id != rngtypid)
1771 {
1772 typcache = lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO);
1773 if (typcache->rngelemtype == NULL)
1774 elog(ERROR, "type %u is not a range type", rngtypid);
1775 fcinfo->flinfo->fn_extra = typcache;
1776 }
1777
1778 return typcache;
1779}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * fn_extra
Definition: fmgr.h:64
FmgrInfo * flinfo
Definition: fmgr.h:87
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386
#define TYPECACHE_RANGE_INFO
Definition: typcache.h:149

References elog, ERROR, FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, if(), lookup_type_cache(), TypeCacheEntry::rngelemtype, TypeCacheEntry::type_id, and TYPECACHE_RANGE_INFO.

Referenced by daterange_canonical(), elem_contained_by_range(), hash_range(), hash_range_extended(), int4range_canonical(), int8range_canonical(), multirange_gist_consistent(), range_adjacent(), range_after(), range_before(), range_cmp(), range_constructor2(), range_constructor3(), range_contained_by(), range_contains(), range_contains_elem(), range_eq(), range_gist_consistent(), range_gist_penalty(), range_gist_picksplit(), range_gist_same(), range_gist_union(), range_intersect(), range_intersect_agg_transfn(), range_lower(), range_merge(), range_minus(), range_ne(), range_overlaps(), range_overleft(), range_overright(), range_typanalyze(), range_union(), range_upper(), rangesel(), spg_range_quad_choose(), spg_range_quad_inner_consistent(), spg_range_quad_leaf_consistent(), and spg_range_quad_picksplit().

◆ range_intersect_internal()

RangeType * range_intersect_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 1143 of file rangetypes.c.

1144{
1145 RangeBound lower1,
1146 lower2;
1147 RangeBound upper1,
1148 upper2;
1149 bool empty1,
1150 empty2;
1151 RangeBound *result_lower;
1152 RangeBound *result_upper;
1153
1154 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
1155 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
1156
1157 if (empty1 || empty2 || !range_overlaps_internal(typcache, r1, r2))
1158 return make_empty_range(typcache);
1159
1160 if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
1161 result_lower = &lower1;
1162 else
1163 result_lower = &lower2;
1164
1165 if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
1166 result_upper = &upper1;
1167 else
1168 result_upper = &upper2;
1169
1170 return make_range(typcache, result_lower, result_upper, false, NULL);
1171}
bool range_overlaps_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
Definition: rangetypes.c:841
RangeType * make_empty_range(TypeCacheEntry *typcache)
Definition: rangetypes.c:2227

References make_empty_range(), make_range(), range_cmp_bounds(), range_deserialize(), and range_overlaps_internal().

Referenced by multirange_intersect_internal(), range_intersect(), and range_intersect_agg_transfn().

◆ range_minus_internal()

RangeType * range_minus_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2 
)

Definition at line 993 of file rangetypes.c.

994{
995 RangeBound lower1,
996 lower2;
997 RangeBound upper1,
998 upper2;
999 bool empty1,
1000 empty2;
1001 int cmp_l1l2,
1002 cmp_l1u2,
1003 cmp_u1l2,
1004 cmp_u1u2;
1005
1006 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
1007 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
1008
1009 /* if either is empty, r1 is the correct answer */
1010 if (empty1 || empty2)
1011 return r1;
1012
1013 cmp_l1l2 = range_cmp_bounds(typcache, &lower1, &lower2);
1014 cmp_l1u2 = range_cmp_bounds(typcache, &lower1, &upper2);
1015 cmp_u1l2 = range_cmp_bounds(typcache, &upper1, &lower2);
1016 cmp_u1u2 = range_cmp_bounds(typcache, &upper1, &upper2);
1017
1018 if (cmp_l1l2 < 0 && cmp_u1u2 > 0)
1019 ereport(ERROR,
1020 (errcode(ERRCODE_DATA_EXCEPTION),
1021 errmsg("result of range difference would not be contiguous")));
1022
1023 if (cmp_l1u2 > 0 || cmp_u1l2 < 0)
1024 return r1;
1025
1026 if (cmp_l1l2 >= 0 && cmp_u1u2 <= 0)
1027 return make_empty_range(typcache);
1028
1029 if (cmp_l1l2 <= 0 && cmp_u1l2 >= 0 && cmp_u1u2 <= 0)
1030 {
1031 lower2.inclusive = !lower2.inclusive;
1032 lower2.lower = false; /* it will become the upper bound */
1033 return make_range(typcache, &lower1, &lower2, false, NULL);
1034 }
1035
1036 if (cmp_l1l2 >= 0 && cmp_u1u2 >= 0 && cmp_l1u2 <= 0)
1037 {
1038 upper2.inclusive = !upper2.inclusive;
1039 upper2.lower = true; /* it will become the lower bound */
1040 return make_range(typcache, &upper2, &upper1, false, NULL);
1041 }
1042
1043 elog(ERROR, "unexpected case in range_minus");
1044 return NULL;
1045}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ereport(elevel,...)
Definition: elog.h:150

References elog, ereport, errcode(), errmsg(), ERROR, RangeBound::inclusive, RangeBound::lower, make_empty_range(), make_range(), range_cmp_bounds(), and range_deserialize().

Referenced by multirange_minus_internal(), and range_minus().

◆ range_ne_internal()

bool range_ne_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 618 of file rangetypes.c.

619{
620 return (!range_eq_internal(typcache, r1, r2));
621}
bool range_eq_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
Definition: rangetypes.c:573

References range_eq_internal().

Referenced by range_ne().

◆ range_overlaps_internal()

bool range_overlaps_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 841 of file rangetypes.c.

842{
843 RangeBound lower1,
844 lower2;
845 RangeBound upper1,
846 upper2;
847 bool empty1,
848 empty2;
849
850 /* Different types should be prevented by ANYRANGE matching rules */
851 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
852 elog(ERROR, "range types do not match");
853
854 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
855 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
856
857 /* An empty range does not overlap any other range */
858 if (empty1 || empty2)
859 return false;
860
861 if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0 &&
862 range_cmp_bounds(typcache, &lower1, &upper2) <= 0)
863 return true;
864
865 if (range_cmp_bounds(typcache, &lower2, &lower1) >= 0 &&
866 range_cmp_bounds(typcache, &lower2, &upper1) <= 0)
867 return true;
868
869 return false;
870}

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by multirange_intersect_internal(), multirange_minus_internal(), range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), range_intersect_internal(), range_overlaps(), range_union_internal(), and spg_range_quad_leaf_consistent().

◆ range_overleft_internal()

bool range_overleft_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 887 of file rangetypes.c.

888{
889 RangeBound lower1,
890 lower2;
891 RangeBound upper1,
892 upper2;
893 bool empty1,
894 empty2;
895
896 /* Different types should be prevented by ANYRANGE matching rules */
897 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
898 elog(ERROR, "range types do not match");
899
900 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
901 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
902
903 /* An empty range is neither before nor after any other range */
904 if (empty1 || empty2)
905 return false;
906
907 if (range_cmp_bounds(typcache, &upper1, &upper2) <= 0)
908 return true;
909
910 return false;
911}

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by multirange_intersect_internal(), range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), range_overleft(), and spg_range_quad_leaf_consistent().

◆ range_overright_internal()

bool range_overright_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2 
)

Definition at line 928 of file rangetypes.c.

929{
930 RangeBound lower1,
931 lower2;
932 RangeBound upper1,
933 upper2;
934 bool empty1,
935 empty2;
936
937 /* Different types should be prevented by ANYRANGE matching rules */
938 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
939 elog(ERROR, "range types do not match");
940
941 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
942 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
943
944 /* An empty range is neither before nor after any other range */
945 if (empty1 || empty2)
946 return false;
947
948 if (range_cmp_bounds(typcache, &lower1, &lower2) >= 0)
949 return true;
950
951 return false;
952}

References elog, ERROR, range_cmp_bounds(), range_deserialize(), and RangeTypeGetOid.

Referenced by range_gist_consistent_int_range(), range_gist_consistent_leaf_range(), range_overright(), and spg_range_quad_leaf_consistent().

◆ range_serialize()

RangeType * range_serialize ( TypeCacheEntry typcache,
RangeBound lower,
RangeBound upper,
bool  empty,
struct Node escontext 
)

Definition at line 1789 of file rangetypes.c.

1791{
1793 int cmp;
1794 Size msize;
1795 Pointer ptr;
1796 int16 typlen;
1797 bool typbyval;
1798 char typalign;
1799 char typstorage;
1800 char flags = 0;
1801
1802 /*
1803 * Verify range is not invalid on its face, and construct flags value,
1804 * preventing any non-canonical combinations such as infinite+inclusive.
1805 */
1806 Assert(lower->lower);
1807 Assert(!upper->lower);
1808
1809 if (empty)
1810 flags |= RANGE_EMPTY;
1811 else
1812 {
1813 cmp = range_cmp_bound_values(typcache, lower, upper);
1814
1815 /* error check: if lower bound value is above upper, it's wrong */
1816 if (cmp > 0)
1817 ereturn(escontext, NULL,
1818 (errcode(ERRCODE_DATA_EXCEPTION),
1819 errmsg("range lower bound must be less than or equal to range upper bound")));
1820
1821 /* if bounds are equal, and not both inclusive, range is empty */
1822 if (cmp == 0 && !(lower->inclusive && upper->inclusive))
1823 flags |= RANGE_EMPTY;
1824 else
1825 {
1826 /* infinite boundaries are never inclusive */
1827 if (lower->infinite)
1828 flags |= RANGE_LB_INF;
1829 else if (lower->inclusive)
1830 flags |= RANGE_LB_INC;
1831 if (upper->infinite)
1832 flags |= RANGE_UB_INF;
1833 else if (upper->inclusive)
1834 flags |= RANGE_UB_INC;
1835 }
1836 }
1837
1838 /* Fetch information about range's element type */
1839 typlen = typcache->rngelemtype->typlen;
1840 typbyval = typcache->rngelemtype->typbyval;
1841 typalign = typcache->rngelemtype->typalign;
1842 typstorage = typcache->rngelemtype->typstorage;
1843
1844 /* Count space for varlena header and range type's OID */
1845 msize = sizeof(RangeType);
1846 Assert(msize == MAXALIGN(msize));
1847
1848 /* Count space for bounds */
1849 if (RANGE_HAS_LBOUND(flags))
1850 {
1851 /*
1852 * Make sure item to be inserted is not toasted. It is essential that
1853 * we not insert an out-of-line toast value pointer into a range
1854 * object, for the same reasons that arrays and records can't contain
1855 * them. It would work to store a compressed-in-line value, but we
1856 * prefer to decompress and then let compression be applied to the
1857 * whole range object if necessary. But, unlike arrays, we do allow
1858 * short-header varlena objects to stay as-is.
1859 */
1860 if (typlen == -1)
1862
1863 msize = datum_compute_size(msize, lower->val, typbyval, typalign,
1864 typlen, typstorage);
1865 }
1866
1867 if (RANGE_HAS_UBOUND(flags))
1868 {
1869 /* Make sure item to be inserted is not toasted */
1870 if (typlen == -1)
1872
1873 msize = datum_compute_size(msize, upper->val, typbyval, typalign,
1874 typlen, typstorage);
1875 }
1876
1877 /* Add space for flag byte */
1878 msize += sizeof(char);
1879
1880 /* Note: zero-fill is required here, just as in heap tuples */
1881 range = (RangeType *) palloc0(msize);
1882 SET_VARSIZE(range, msize);
1883
1884 /* Now fill in the datum */
1885 range->rangetypid = typcache->type_id;
1886
1887 ptr = (char *) (range + 1);
1888
1889 if (RANGE_HAS_LBOUND(flags))
1890 {
1891 Assert(lower->lower);
1892 ptr = datum_write(ptr, lower->val, typbyval, typalign, typlen,
1893 typstorage);
1894 }
1895
1896 if (RANGE_HAS_UBOUND(flags))
1897 {
1898 Assert(!upper->lower);
1899 ptr = datum_write(ptr, upper->val, typbyval, typalign, typlen,
1900 typstorage);
1901 }
1902
1903 *((char *) ptr) = flags;
1904
1905 return range;
1906}
#define MAXALIGN(LEN)
Definition: c.h:810
size_t Size
Definition: c.h:610
#define ereturn(context, dummy_value,...)
Definition: elog.h:278
#define PG_DETOAST_DATUM_PACKED(datum)
Definition: fmgr.h:248
void * palloc0(Size size)
Definition: mcxt.c:1395
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
static Pointer datum_write(Pointer ptr, Datum datum, bool typbyval, char typalign, int16 typlen, char typstorage)
Definition: rangetypes.c:2771
static Size datum_compute_size(Size data_length, Datum val, bool typbyval, char typalign, int16 typlen, char typstorage)
Definition: rangetypes.c:2745
char typstorage
Definition: typcache.h:42
static void SET_VARSIZE(void *PTR, Size len)
Definition: varatt.h:432

References Assert(), cmp(), datum_compute_size(), datum_write(), ereturn, errcode(), errmsg(), lower(), MAXALIGN, palloc0(), PG_DETOAST_DATUM_PACKED, PointerGetDatum(), range(), range_cmp_bound_values(), RANGE_EMPTY, RANGE_HAS_LBOUND, RANGE_HAS_UBOUND, RANGE_LB_INC, RANGE_LB_INF, RANGE_UB_INC, RANGE_UB_INF, TypeCacheEntry::rngelemtype, SET_VARSIZE(), typalign, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::type_id, TypeCacheEntry::typlen, TypeCacheEntry::typstorage, and upper().

Referenced by compute_range_stats(), daterange_canonical(), int4range_canonical(), int8range_canonical(), make_range(), multirangesel(), rangesel(), and spg_range_quad_picksplit().

◆ range_set_contain_empty()

void range_set_contain_empty ( RangeType range)

Definition at line 1999 of file rangetypes.c.

2000{
2001 char *flagsp;
2002
2003 /* flag byte is datum's last byte */
2004 flagsp = (char *) range + VARSIZE(range) - 1;
2005
2006 *flagsp |= RANGE_CONTAIN_EMPTY;
2007}
#define RANGE_CONTAIN_EMPTY
Definition: rangetypes.h:45

References range(), RANGE_CONTAIN_EMPTY, and VARSIZE().

Referenced by range_super_union().

◆ range_split_internal()

bool range_split_internal ( TypeCacheEntry typcache,
const RangeType r1,
const RangeType r2,
RangeType **  output1,
RangeType **  output2 
)

Definition at line 1182 of file rangetypes.c.

1184{
1185 RangeBound lower1,
1186 lower2;
1187 RangeBound upper1,
1188 upper2;
1189 bool empty1,
1190 empty2;
1191
1192 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
1193 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
1194
1195 if (range_cmp_bounds(typcache, &lower1, &lower2) < 0 &&
1196 range_cmp_bounds(typcache, &upper1, &upper2) > 0)
1197 {
1198 /*
1199 * Need to invert inclusive/exclusive for the lower2 and upper2
1200 * points. They can't be infinite though. We're allowed to overwrite
1201 * these RangeBounds since they only exist locally.
1202 */
1203 lower2.inclusive = !lower2.inclusive;
1204 lower2.lower = false;
1205 upper2.inclusive = !upper2.inclusive;
1206 upper2.lower = true;
1207
1208 *output1 = make_range(typcache, &lower1, &lower2, false, NULL);
1209 *output2 = make_range(typcache, &upper2, &upper1, false, NULL);
1210 return true;
1211 }
1212
1213 return false;
1214}

References RangeBound::inclusive, RangeBound::lower, make_range(), range_cmp_bounds(), and range_deserialize().

Referenced by multirange_minus_internal().

◆ range_union_internal()

RangeType * range_union_internal ( TypeCacheEntry typcache,
RangeType r1,
RangeType r2,
bool  strict 
)

Definition at line 1052 of file rangetypes.c.

1054{
1055 RangeBound lower1,
1056 lower2;
1057 RangeBound upper1,
1058 upper2;
1059 bool empty1,
1060 empty2;
1061 RangeBound *result_lower;
1062 RangeBound *result_upper;
1063
1064 /* Different types should be prevented by ANYRANGE matching rules */
1065 if (RangeTypeGetOid(r1) != RangeTypeGetOid(r2))
1066 elog(ERROR, "range types do not match");
1067
1068 range_deserialize(typcache, r1, &lower1, &upper1, &empty1);
1069 range_deserialize(typcache, r2, &lower2, &upper2, &empty2);
1070
1071 /* if either is empty, the other is the correct answer */
1072 if (empty1)
1073 return r2;
1074 if (empty2)
1075 return r1;
1076
1077 if (strict &&
1078 !range_overlaps_internal(typcache, r1, r2) &&
1079 !range_adjacent_internal(typcache, r1, r2))
1080 ereport(ERROR,
1081 (errcode(ERRCODE_DATA_EXCEPTION),
1082 errmsg("result of range union would not be contiguous")));
1083
1084 if (range_cmp_bounds(typcache, &lower1, &lower2) < 0)
1085 result_lower = &lower1;
1086 else
1087 result_lower = &lower2;
1088
1089 if (range_cmp_bounds(typcache, &upper1, &upper2) > 0)
1090 result_upper = &upper1;
1091 else
1092 result_upper = &upper2;
1093
1094 return make_range(typcache, result_lower, result_upper, false, NULL);
1095}
bool range_adjacent_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2)
Definition: rangetypes.c:798

References elog, ereport, errcode(), errmsg(), ERROR, make_range(), range_adjacent_internal(), range_cmp_bounds(), range_deserialize(), range_overlaps_internal(), and RangeTypeGetOid.

Referenced by multirange_canonicalize(), range_merge(), and range_union().

◆ RangeTypePGetDatum()