1 /*------------------------------------------------------------------------- 2 * 3 * rangetypes.h 4 * Declarations for Postgres range types. 5 * 6 * 7 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group 8 * Portions Copyright (c) 1994, Regents of the University of California 9 * 10 * src/include/utils/rangetypes.h 11 * 12 *------------------------------------------------------------------------- 13 */ 14 #ifndef RANGETYPES_H 15 #define RANGETYPES_H 16 17 #include "utils/typcache.h" 18 19 20 /* 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! 24 */ 25 typedef struct 26 { 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 */ 30 } RangeType; 31 32 /* Use this macro in preference to fetching rangetypid field directly */ 33 #define RangeTypeGetOid(r) ((r)->rangetypid) 34 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 */ 45 46 #define RANGE_HAS_LBOUND(flags) (!((flags) & (RANGE_EMPTY | \ 47 RANGE_LB_NULL | \ 48 RANGE_LB_INF))) 49 50 #define RANGE_HAS_UBOUND(flags) (!((flags) & (RANGE_EMPTY | \ 51 RANGE_UB_NULL | \ 52 RANGE_UB_INF))) 53 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) 57 58 59 /* Internal representation of either bound of a range (not what's on disk) */ 60 typedef struct 61 { 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 */ 66 } RangeBound; 67 68 /* 69 * fmgr macros for range type objects 70 */ 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) 77 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 RTLeftStrategyNumber 81 #define RANGESTRAT_OVERLEFT RTOverLeftStrategyNumber 82 #define RANGESTRAT_OVERLAPS RTOverlapStrategyNumber 83 #define RANGESTRAT_OVERRIGHT RTOverRightStrategyNumber 84 #define RANGESTRAT_AFTER RTRightStrategyNumber 85 #define RANGESTRAT_ADJACENT RTSameStrategyNumber 86 #define RANGESTRAT_CONTAINS RTContainsStrategyNumber 87 #define RANGESTRAT_CONTAINED_BY RTContainedByStrategyNumber 88 #define RANGESTRAT_CONTAINS_ELEM RTContainsElemStrategyNumber 89 #define RANGESTRAT_EQ RTEqualStrategyNumber 90 91 /* 92 * prototypes for functions defined in rangetypes.c 93 */ 94 95 /* I/O */ 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); 100 101 /* constructors */ 102 extern Datum range_constructor2(PG_FUNCTION_ARGS); 103 extern Datum range_constructor3(PG_FUNCTION_ARGS); 104 105 /* range -> subtype */ 106 extern Datum range_lower(PG_FUNCTION_ARGS); 107 extern Datum range_upper(PG_FUNCTION_ARGS); 108 109 /* range -> bool */ 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); 115 116 /* range, element -> bool */ 117 extern Datum range_contains_elem(PG_FUNCTION_ARGS); 118 extern Datum elem_contained_by_range(PG_FUNCTION_ARGS); 119 120 extern bool range_contains_elem_internal(TypeCacheEntry *typcache, RangeType *r, Datum val); 121 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); 133 134 /* internal versions of the above */ 135 extern bool range_eq_internal(TypeCacheEntry *typcache, RangeType *r1, 136 RangeType *r2); 137 extern bool range_ne_internal(TypeCacheEntry *typcache, RangeType *r1, 138 RangeType *r2); 139 extern bool range_contains_internal(TypeCacheEntry *typcache, RangeType *r1, 140 RangeType *r2); 141 extern bool range_contained_by_internal(TypeCacheEntry *typcache, RangeType *r1, 142 RangeType *r2); 143 extern bool range_before_internal(TypeCacheEntry *typcache, RangeType *r1, 144 RangeType *r2); 145 extern bool range_after_internal(TypeCacheEntry *typcache, RangeType *r1, 146 RangeType *r2); 147 extern bool range_adjacent_internal(TypeCacheEntry *typcache, RangeType *r1, 148 RangeType *r2); 149 extern bool range_overlaps_internal(TypeCacheEntry *typcache, RangeType *r1, 150 RangeType *r2); 151 extern bool range_overleft_internal(TypeCacheEntry *typcache, RangeType *r1, 152 RangeType *r2); 153 extern bool range_overright_internal(TypeCacheEntry *typcache, RangeType *r1, 154 RangeType *r2); 155 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); 160 161 /* BTree support */ 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); 167 168 /* Hash support */ 169 extern Datum hash_range(PG_FUNCTION_ARGS); 170 171 /* ANALYZE support */ 172 extern Datum range_typanalyze(PG_FUNCTION_ARGS); 173 extern Datum rangesel(PG_FUNCTION_ARGS); 174 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); 179 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); 187 188 /* assorted support functions */ 189 extern TypeCacheEntry *range_get_typcache(FunctionCallInfo fcinfo, 190 Oid rngtypid); 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, 195 bool *empty); 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, 201 RangeBound *b2); 202 extern int range_cmp_bound_values(TypeCacheEntry *typcache, RangeBound *b1, 203 RangeBound *b2); 204 extern bool bounds_adjacent(TypeCacheEntry *typcache, RangeBound bound1, 205 RangeBound bound2); 206 extern RangeType *make_empty_range(TypeCacheEntry *typcache); 207 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_fetch(PG_FUNCTION_ARGS); 213 extern Datum range_gist_union(PG_FUNCTION_ARGS); 214 extern Datum range_merge(PG_FUNCTION_ARGS); 215 extern Datum range_gist_penalty(PG_FUNCTION_ARGS); 216 extern Datum range_gist_picksplit(PG_FUNCTION_ARGS); 217 extern Datum range_gist_same(PG_FUNCTION_ARGS); 218 219 #endif /* RANGETYPES_H */ 220