Header And Logo

PostgreSQL
| The world's most advanced open source database.

rangetypes.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * rangetypes.h
00004  *    Declarations for Postgres range types.
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/utils/rangetypes.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef RANGETYPES_H
00015 #define RANGETYPES_H
00016 
00017 #include "utils/typcache.h"
00018 
00019 
00020 /*
00021  * Ranges are varlena objects, so must meet the varlena convention that
00022  * the first int32 of the object contains the total object size in bytes.
00023  * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though!
00024  */
00025 typedef struct
00026 {
00027     int32       vl_len_;        /* varlena header (do not touch directly!) */
00028     Oid         rangetypid;     /* range type's own OID */
00029     /* Following the OID are zero to two bound values, then a flags byte */
00030 } RangeType;
00031 
00032 /* Use this macro in preference to fetching rangetypid field directly */
00033 #define RangeTypeGetOid(r)  ((r)->rangetypid)
00034 
00035 /* A range's flags byte contains these bits: */
00036 #define RANGE_EMPTY         0x01    /* range is empty */
00037 #define RANGE_LB_INC        0x02    /* lower bound is inclusive */
00038 #define RANGE_UB_INC        0x04    /* upper bound is inclusive */
00039 #define RANGE_LB_INF        0x08    /* lower bound is -infinity */
00040 #define RANGE_UB_INF        0x10    /* upper bound is +infinity */
00041 #define RANGE_LB_NULL       0x20    /* lower bound is null (NOT USED) */
00042 #define RANGE_UB_NULL       0x40    /* upper bound is null (NOT USED) */
00043 #define RANGE_CONTAIN_EMPTY 0x80/* marks a GiST internal-page entry whose
00044                                  * subtree contains some empty ranges */
00045 
00046 #define RANGE_HAS_LBOUND(flags) (!((flags) & (RANGE_EMPTY | \
00047                                               RANGE_LB_NULL | \
00048                                               RANGE_LB_INF)))
00049 
00050 #define RANGE_HAS_UBOUND(flags) (!((flags) & (RANGE_EMPTY | \
00051                                               RANGE_UB_NULL | \
00052                                               RANGE_UB_INF)))
00053 
00054 #define RangeIsEmpty(r)  ((range_get_flags(r) & RANGE_EMPTY) != 0)
00055 #define RangeIsOrContainsEmpty(r)  \
00056     ((range_get_flags(r) & (RANGE_EMPTY | RANGE_CONTAIN_EMPTY)) != 0)
00057 
00058 
00059 /* Internal representation of either bound of a range (not what's on disk) */
00060 typedef struct
00061 {
00062     Datum       val;            /* the bound value, if any */
00063     bool        infinite;       /* bound is +/- infinity */
00064     bool        inclusive;      /* bound is inclusive (vs exclusive) */
00065     bool        lower;          /* this is the lower (vs upper) bound */
00066 } RangeBound;
00067 
00068 /*
00069  * fmgr macros for range type objects
00070  */
00071 #define DatumGetRangeType(X)        ((RangeType *) PG_DETOAST_DATUM(X))
00072 #define DatumGetRangeTypeCopy(X)    ((RangeType *) PG_DETOAST_DATUM_COPY(X))
00073 #define RangeTypeGetDatum(X)        PointerGetDatum(X)
00074 #define PG_GETARG_RANGE(n)          DatumGetRangeType(PG_GETARG_DATUM(n))
00075 #define PG_GETARG_RANGE_COPY(n)     DatumGetRangeTypeCopy(PG_GETARG_DATUM(n))
00076 #define PG_RETURN_RANGE(x)          return RangeTypeGetDatum(x)
00077 
00078 /* Operator strategy numbers used in the GiST and SP-GiST range opclasses */
00079 /* Numbers are chosen to match up operator names with existing usages */
00080 #define RANGESTRAT_BEFORE               1
00081 #define RANGESTRAT_OVERLEFT             2
00082 #define RANGESTRAT_OVERLAPS             3
00083 #define RANGESTRAT_OVERRIGHT            4
00084 #define RANGESTRAT_AFTER                5
00085 #define RANGESTRAT_ADJACENT             6
00086 #define RANGESTRAT_CONTAINS             7
00087 #define RANGESTRAT_CONTAINED_BY         8
00088 #define RANGESTRAT_CONTAINS_ELEM        16
00089 #define RANGESTRAT_EQ                   18
00090 
00091 /*
00092  * prototypes for functions defined in rangetypes.c
00093  */
00094 
00095 /* I/O */
00096 extern Datum range_in(PG_FUNCTION_ARGS);
00097 extern Datum range_out(PG_FUNCTION_ARGS);
00098 extern Datum range_recv(PG_FUNCTION_ARGS);
00099 extern Datum range_send(PG_FUNCTION_ARGS);
00100 
00101 /* constructors */
00102 extern Datum range_constructor2(PG_FUNCTION_ARGS);
00103 extern Datum range_constructor3(PG_FUNCTION_ARGS);
00104 
00105 /* range -> subtype */
00106 extern Datum range_lower(PG_FUNCTION_ARGS);
00107 extern Datum range_upper(PG_FUNCTION_ARGS);
00108 
00109 /* range -> bool */
00110 extern Datum range_empty(PG_FUNCTION_ARGS);
00111 extern Datum range_lower_inc(PG_FUNCTION_ARGS);
00112 extern Datum range_upper_inc(PG_FUNCTION_ARGS);
00113 extern Datum range_lower_inf(PG_FUNCTION_ARGS);
00114 extern Datum range_upper_inf(PG_FUNCTION_ARGS);
00115 
00116 /* range, element -> bool */
00117 extern Datum range_contains_elem(PG_FUNCTION_ARGS);
00118 extern Datum elem_contained_by_range(PG_FUNCTION_ARGS);
00119 
00120 extern bool range_contains_elem_internal(TypeCacheEntry *typcache, RangeType *r, Datum val);
00121 
00122 /* range, range -> bool */
00123 extern Datum range_eq(PG_FUNCTION_ARGS);
00124 extern Datum range_ne(PG_FUNCTION_ARGS);
00125 extern Datum range_contains(PG_FUNCTION_ARGS);
00126 extern Datum range_contained_by(PG_FUNCTION_ARGS);
00127 extern Datum range_before(PG_FUNCTION_ARGS);
00128 extern Datum range_after(PG_FUNCTION_ARGS);
00129 extern Datum range_adjacent(PG_FUNCTION_ARGS);
00130 extern Datum range_overlaps(PG_FUNCTION_ARGS);
00131 extern Datum range_overleft(PG_FUNCTION_ARGS);
00132 extern Datum range_overright(PG_FUNCTION_ARGS);
00133 
00134 /* internal versions of the above */
00135 extern bool range_eq_internal(TypeCacheEntry *typcache, RangeType *r1,
00136                   RangeType *r2);
00137 extern bool range_ne_internal(TypeCacheEntry *typcache, RangeType *r1,
00138                   RangeType *r2);
00139 extern bool range_contains_internal(TypeCacheEntry *typcache, RangeType *r1,
00140                         RangeType *r2);
00141 extern bool range_contained_by_internal(TypeCacheEntry *typcache, RangeType *r1,
00142                             RangeType *r2);
00143 extern bool range_before_internal(TypeCacheEntry *typcache, RangeType *r1,
00144                       RangeType *r2);
00145 extern bool range_after_internal(TypeCacheEntry *typcache, RangeType *r1,
00146                      RangeType *r2);
00147 extern bool range_adjacent_internal(TypeCacheEntry *typcache, RangeType *r1,
00148                         RangeType *r2);
00149 extern bool range_overlaps_internal(TypeCacheEntry *typcache, RangeType *r1,
00150                         RangeType *r2);
00151 extern bool range_overleft_internal(TypeCacheEntry *typcache, RangeType *r1,
00152                         RangeType *r2);
00153 extern bool range_overright_internal(TypeCacheEntry *typcache, RangeType *r1,
00154                          RangeType *r2);
00155 
00156 /* range, range -> range */
00157 extern Datum range_minus(PG_FUNCTION_ARGS);
00158 extern Datum range_union(PG_FUNCTION_ARGS);
00159 extern Datum range_intersect(PG_FUNCTION_ARGS);
00160 
00161 /* BTree support */
00162 extern Datum range_cmp(PG_FUNCTION_ARGS);
00163 extern Datum range_lt(PG_FUNCTION_ARGS);
00164 extern Datum range_le(PG_FUNCTION_ARGS);
00165 extern Datum range_ge(PG_FUNCTION_ARGS);
00166 extern Datum range_gt(PG_FUNCTION_ARGS);
00167 
00168 /* Hash support */
00169 extern Datum hash_range(PG_FUNCTION_ARGS);
00170 
00171 /* ANALYZE support */
00172 extern Datum range_typanalyze(PG_FUNCTION_ARGS);
00173 extern Datum rangesel(PG_FUNCTION_ARGS);
00174 
00175 /* Canonical functions */
00176 extern Datum int4range_canonical(PG_FUNCTION_ARGS);
00177 extern Datum int8range_canonical(PG_FUNCTION_ARGS);
00178 extern Datum daterange_canonical(PG_FUNCTION_ARGS);
00179 
00180 /* Subtype Difference functions */
00181 extern Datum int4range_subdiff(PG_FUNCTION_ARGS);
00182 extern Datum int8range_subdiff(PG_FUNCTION_ARGS);
00183 extern Datum numrange_subdiff(PG_FUNCTION_ARGS);
00184 extern Datum daterange_subdiff(PG_FUNCTION_ARGS);
00185 extern Datum tsrange_subdiff(PG_FUNCTION_ARGS);
00186 extern Datum tstzrange_subdiff(PG_FUNCTION_ARGS);
00187 
00188 /* assorted support functions */
00189 extern TypeCacheEntry *range_get_typcache(FunctionCallInfo fcinfo,
00190                    Oid rngtypid);
00191 extern RangeType *range_serialize(TypeCacheEntry *typcache, RangeBound *lower,
00192                 RangeBound *upper, bool empty);
00193 extern void range_deserialize(TypeCacheEntry *typcache, RangeType *range,
00194                   RangeBound *lower, RangeBound *upper,
00195                   bool *empty);
00196 extern char range_get_flags(RangeType *range);
00197 extern void range_set_contain_empty(RangeType *range);
00198 extern RangeType *make_range(TypeCacheEntry *typcache, RangeBound *lower,
00199            RangeBound *upper, bool empty);
00200 extern int range_cmp_bounds(TypeCacheEntry *typcache, RangeBound *b1,
00201                  RangeBound *b2);
00202 extern int range_cmp_bound_values(TypeCacheEntry *typcache, RangeBound *b1,
00203                        RangeBound *b2);
00204 extern bool bounds_adjacent(TypeCacheEntry *typcache, RangeBound bound1,
00205                 RangeBound bound2);
00206 extern RangeType *make_empty_range(TypeCacheEntry *typcache);
00207 
00208 /* GiST support (in rangetypes_gist.c) */
00209 extern Datum range_gist_consistent(PG_FUNCTION_ARGS);
00210 extern Datum range_gist_compress(PG_FUNCTION_ARGS);
00211 extern Datum range_gist_decompress(PG_FUNCTION_ARGS);
00212 extern Datum range_gist_union(PG_FUNCTION_ARGS);
00213 extern Datum range_gist_penalty(PG_FUNCTION_ARGS);
00214 extern Datum range_gist_picksplit(PG_FUNCTION_ARGS);
00215 extern Datum range_gist_same(PG_FUNCTION_ARGS);
00216 
00217 #endif   /* RANGETYPES_H */