1 /*-------------------------------------------------------------------------
4 * Declarations for Postgres range types.
7 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/utils/rangetypes.h
12 *-------------------------------------------------------------------------
17 #include "utils/typcache.h"
21 * Ranges are varlena objects, so must meet the varlena convention that
22 * the first int32 of the object contains the total object size in bytes.
23 * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though!
27 int32 vl_len_; /* varlena header (do not touch directly!) */
28 Oid rangetypid; /* range type's own OID */
29 /* Following the OID are zero to two bound values, then a flags byte */
32 /* Use this macro in preference to fetching rangetypid field directly */
33 #define RangeTypeGetOid(r) ((r)->rangetypid)
35 /* A range's flags byte contains these bits: */
36 #define RANGE_EMPTY 0x01 /* range is empty */
37 #define RANGE_LB_INC 0x02 /* lower bound is inclusive */
38 #define RANGE_UB_INC 0x04 /* upper bound is inclusive */
39 #define RANGE_LB_INF 0x08 /* lower bound is -infinity */
40 #define RANGE_UB_INF 0x10 /* upper bound is +infinity */
41 #define RANGE_LB_NULL 0x20 /* lower bound is null (NOT USED) */
42 #define RANGE_UB_NULL 0x40 /* upper bound is null (NOT USED) */
43 #define RANGE_CONTAIN_EMPTY 0x80/* marks a GiST internal-page entry whose
44 * subtree contains some empty ranges */
46 #define RANGE_HAS_LBOUND(flags) (!((flags) & (RANGE_EMPTY | \
50 #define RANGE_HAS_UBOUND(flags) (!((flags) & (RANGE_EMPTY | \
54 #define RangeIsEmpty(r) ((range_get_flags(r) & RANGE_EMPTY) != 0)
55 #define RangeIsOrContainsEmpty(r) \
56 ((range_get_flags(r) & (RANGE_EMPTY | RANGE_CONTAIN_EMPTY)) != 0)
59 /* Internal representation of either bound of a range (not what's on disk) */
62 Datum val; /* the bound value, if any */
63 bool infinite; /* bound is +/- infinity */
64 bool inclusive; /* bound is inclusive (vs exclusive) */
65 bool lower; /* this is the lower (vs upper) bound */
69 * fmgr macros for range type objects
71 #define DatumGetRangeType(X) ((RangeType *) PG_DETOAST_DATUM(X))
72 #define DatumGetRangeTypeCopy(X) ((RangeType *) PG_DETOAST_DATUM_COPY(X))
73 #define RangeTypeGetDatum(X) PointerGetDatum(X)
74 #define PG_GETARG_RANGE(n) DatumGetRangeType(PG_GETARG_DATUM(n))
75 #define PG_GETARG_RANGE_COPY(n) DatumGetRangeTypeCopy(PG_GETARG_DATUM(n))
76 #define PG_RETURN_RANGE(x) return RangeTypeGetDatum(x)
78 /* Operator strategy numbers used in the GiST and SP-GiST range opclasses */
79 /* Numbers are chosen to match up operator names with existing usages */
80 #define RANGESTRAT_BEFORE 1
81 #define RANGESTRAT_OVERLEFT 2
82 #define RANGESTRAT_OVERLAPS 3
83 #define RANGESTRAT_OVERRIGHT 4
84 #define RANGESTRAT_AFTER 5
85 #define RANGESTRAT_ADJACENT 6
86 #define RANGESTRAT_CONTAINS 7
87 #define RANGESTRAT_CONTAINED_BY 8
88 #define RANGESTRAT_CONTAINS_ELEM 16
89 #define RANGESTRAT_EQ 18
92 * prototypes for functions defined in rangetypes.c
96 extern Datum range_in(PG_FUNCTION_ARGS);
97 extern Datum range_out(PG_FUNCTION_ARGS);
98 extern Datum range_recv(PG_FUNCTION_ARGS);
99 extern Datum range_send(PG_FUNCTION_ARGS);
102 extern Datum range_constructor2(PG_FUNCTION_ARGS);
103 extern Datum range_constructor3(PG_FUNCTION_ARGS);
105 /* range -> subtype */
106 extern Datum range_lower(PG_FUNCTION_ARGS);
107 extern Datum range_upper(PG_FUNCTION_ARGS);
110 extern Datum range_empty(PG_FUNCTION_ARGS);
111 extern Datum range_lower_inc(PG_FUNCTION_ARGS);
112 extern Datum range_upper_inc(PG_FUNCTION_ARGS);
113 extern Datum range_lower_inf(PG_FUNCTION_ARGS);
114 extern Datum range_upper_inf(PG_FUNCTION_ARGS);
116 /* range, element -> bool */
117 extern Datum range_contains_elem(PG_FUNCTION_ARGS);
118 extern Datum elem_contained_by_range(PG_FUNCTION_ARGS);
120 extern bool range_contains_elem_internal(TypeCacheEntry *typcache, RangeType *r, Datum val);
122 /* range, range -> bool */
123 extern Datum range_eq(PG_FUNCTION_ARGS);
124 extern Datum range_ne(PG_FUNCTION_ARGS);
125 extern Datum range_contains(PG_FUNCTION_ARGS);
126 extern Datum range_contained_by(PG_FUNCTION_ARGS);
127 extern Datum range_before(PG_FUNCTION_ARGS);
128 extern Datum range_after(PG_FUNCTION_ARGS);
129 extern Datum range_adjacent(PG_FUNCTION_ARGS);
130 extern Datum range_overlaps(PG_FUNCTION_ARGS);
131 extern Datum range_overleft(PG_FUNCTION_ARGS);
132 extern Datum range_overright(PG_FUNCTION_ARGS);
134 /* internal versions of the above */
135 extern bool range_eq_internal(TypeCacheEntry *typcache, RangeType *r1,
137 extern bool range_ne_internal(TypeCacheEntry *typcache, RangeType *r1,
139 extern bool range_contains_internal(TypeCacheEntry *typcache, RangeType *r1,
141 extern bool range_contained_by_internal(TypeCacheEntry *typcache, RangeType *r1,
143 extern bool range_before_internal(TypeCacheEntry *typcache, RangeType *r1,
145 extern bool range_after_internal(TypeCacheEntry *typcache, RangeType *r1,
147 extern bool range_adjacent_internal(TypeCacheEntry *typcache, RangeType *r1,
149 extern bool range_overlaps_internal(TypeCacheEntry *typcache, RangeType *r1,
151 extern bool range_overleft_internal(TypeCacheEntry *typcache, RangeType *r1,
153 extern bool range_overright_internal(TypeCacheEntry *typcache, RangeType *r1,
156 /* range, range -> range */
157 extern Datum range_minus(PG_FUNCTION_ARGS);
158 extern Datum range_union(PG_FUNCTION_ARGS);
159 extern Datum range_intersect(PG_FUNCTION_ARGS);
162 extern Datum range_cmp(PG_FUNCTION_ARGS);
163 extern Datum range_lt(PG_FUNCTION_ARGS);
164 extern Datum range_le(PG_FUNCTION_ARGS);
165 extern Datum range_ge(PG_FUNCTION_ARGS);
166 extern Datum range_gt(PG_FUNCTION_ARGS);
169 extern Datum hash_range(PG_FUNCTION_ARGS);
171 /* ANALYZE support */
172 extern Datum range_typanalyze(PG_FUNCTION_ARGS);
173 extern Datum rangesel(PG_FUNCTION_ARGS);
175 /* Canonical functions */
176 extern Datum int4range_canonical(PG_FUNCTION_ARGS);
177 extern Datum int8range_canonical(PG_FUNCTION_ARGS);
178 extern Datum daterange_canonical(PG_FUNCTION_ARGS);
180 /* Subtype Difference functions */
181 extern Datum int4range_subdiff(PG_FUNCTION_ARGS);
182 extern Datum int8range_subdiff(PG_FUNCTION_ARGS);
183 extern Datum numrange_subdiff(PG_FUNCTION_ARGS);
184 extern Datum daterange_subdiff(PG_FUNCTION_ARGS);
185 extern Datum tsrange_subdiff(PG_FUNCTION_ARGS);
186 extern Datum tstzrange_subdiff(PG_FUNCTION_ARGS);
188 /* assorted support functions */
189 extern TypeCacheEntry *range_get_typcache(FunctionCallInfo fcinfo,
191 extern RangeType *range_serialize(TypeCacheEntry *typcache, RangeBound *lower,
192 RangeBound *upper, bool empty);
193 extern void range_deserialize(TypeCacheEntry *typcache, RangeType *range,
194 RangeBound *lower, RangeBound *upper,
196 extern char range_get_flags(RangeType *range);
197 extern void range_set_contain_empty(RangeType *range);
198 extern RangeType *make_range(TypeCacheEntry *typcache, RangeBound *lower,
199 RangeBound *upper, bool empty);
200 extern int range_cmp_bounds(TypeCacheEntry *typcache, RangeBound *b1,
202 extern int range_cmp_bound_values(TypeCacheEntry *typcache, RangeBound *b1,
204 extern bool bounds_adjacent(TypeCacheEntry *typcache, RangeBound bound1,
206 extern RangeType *make_empty_range(TypeCacheEntry *typcache);
208 /* GiST support (in rangetypes_gist.c) */
209 extern Datum range_gist_consistent(PG_FUNCTION_ARGS);
210 extern Datum range_gist_compress(PG_FUNCTION_ARGS);
211 extern Datum range_gist_decompress(PG_FUNCTION_ARGS);
212 extern Datum range_gist_union(PG_FUNCTION_ARGS);
213 extern Datum range_gist_penalty(PG_FUNCTION_ARGS);
214 extern Datum range_gist_picksplit(PG_FUNCTION_ARGS);
215 extern Datum range_gist_same(PG_FUNCTION_ARGS);
217 #endif /* RANGETYPES_H */