Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * multirangetypes.h
4 : * Declarations for Postgres multirange types.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
8 : * Portions Copyright (c) 1994, Regents of the University of California
9 : *
10 : * src/include/utils/multirangetypes.h
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #ifndef MULTIRANGETYPES_H
15 : #define MULTIRANGETYPES_H
16 :
17 : #include "utils/rangetypes.h"
18 : #include "utils/typcache.h"
19 :
20 :
21 : /*
22 : * Multiranges are varlena objects, so must meet the varlena convention that
23 : * the first int32 of the object contains the total object size in bytes.
24 : * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though!
25 : */
26 : typedef struct
27 : {
28 : int32 vl_len_; /* varlena header (do not touch directly!) */
29 : Oid multirangetypid; /* multirange type's own OID */
30 : uint32 rangeCount; /* the number of ranges */
31 :
32 : /*
33 : * Following the count are the range objects themselves, as ShortRangeType
34 : * structs. Note that ranges are varlena too, depending on whether they
35 : * have lower/upper bounds and because even their base types can be
36 : * varlena. So we can't really index into this list.
37 : */
38 : } MultirangeType;
39 :
40 : /* Use these macros in preference to accessing these fields directly */
41 : #define MultirangeTypeGetOid(mr) ((mr)->multirangetypid)
42 : #define MultirangeIsEmpty(mr) ((mr)->rangeCount == 0)
43 :
44 : /*
45 : * fmgr functions for multirange type objects
46 : */
47 : static inline MultirangeType *
48 2429762 : DatumGetMultirangeTypeP(Datum X)
49 : {
50 2429762 : return (MultirangeType *) PG_DETOAST_DATUM(X);
51 : }
52 :
53 : static inline MultirangeType *
54 : DatumGetMultirangeTypePCopy(Datum X)
55 : {
56 : return (MultirangeType *) PG_DETOAST_DATUM_COPY(X);
57 : }
58 :
59 : static inline Datum
60 24606 : MultirangeTypePGetDatum(const MultirangeType *X)
61 : {
62 24606 : return PointerGetDatum(X);
63 : }
64 :
65 : #define PG_GETARG_MULTIRANGE_P(n) DatumGetMultirangeTypeP(PG_GETARG_DATUM(n))
66 : #define PG_GETARG_MULTIRANGE_P_COPY(n) DatumGetMultirangeTypePCopy(PG_GETARG_DATUM(n))
67 : #define PG_RETURN_MULTIRANGE_P(x) return MultirangeTypePGetDatum(x)
68 :
69 : /*
70 : * prototypes for functions defined in multirangetypes.c
71 : */
72 :
73 : /* internal versions of the above */
74 : extern bool multirange_eq_internal(TypeCacheEntry *rangetyp,
75 : const MultirangeType *mr1,
76 : const MultirangeType *mr2);
77 : extern bool multirange_ne_internal(TypeCacheEntry *rangetyp,
78 : const MultirangeType *mr1,
79 : const MultirangeType *mr2);
80 : extern bool multirange_contains_elem_internal(TypeCacheEntry *rangetyp,
81 : const MultirangeType *mr,
82 : Datum val);
83 : extern bool multirange_contains_range_internal(TypeCacheEntry *rangetyp,
84 : const MultirangeType *mr,
85 : const RangeType *r);
86 : extern bool range_contains_multirange_internal(TypeCacheEntry *rangetyp,
87 : const RangeType *r,
88 : const MultirangeType *mr);
89 : extern bool multirange_contains_multirange_internal(TypeCacheEntry *rangetyp,
90 : const MultirangeType *mr1,
91 : const MultirangeType *mr2);
92 : extern bool range_overlaps_multirange_internal(TypeCacheEntry *rangetyp,
93 : const RangeType *r,
94 : const MultirangeType *mr);
95 : extern bool multirange_overlaps_multirange_internal(TypeCacheEntry *rangetyp,
96 : const MultirangeType *mr1,
97 : const MultirangeType *mr2);
98 : extern bool range_overleft_multirange_internal(TypeCacheEntry *rangetyp,
99 : const RangeType *r,
100 : const MultirangeType *mr);
101 : extern bool range_overright_multirange_internal(TypeCacheEntry *rangetyp,
102 : const RangeType *r,
103 : const MultirangeType *mr);
104 : extern bool range_before_multirange_internal(TypeCacheEntry *rangetyp,
105 : const RangeType *r,
106 : const MultirangeType *mr);
107 : extern bool range_after_multirange_internal(TypeCacheEntry *rangetyp,
108 : const RangeType *r,
109 : const MultirangeType *mr);
110 : extern bool range_adjacent_multirange_internal(TypeCacheEntry *rangetyp,
111 : const RangeType *r,
112 : const MultirangeType *mr);
113 : extern bool multirange_before_multirange_internal(TypeCacheEntry *rangetyp,
114 : const MultirangeType *mr1,
115 : const MultirangeType *mr2);
116 : extern MultirangeType *multirange_minus_internal(Oid mltrngtypoid,
117 : TypeCacheEntry *rangetyp,
118 : int32 range_count1,
119 : RangeType **ranges1,
120 : int32 range_count2,
121 : RangeType **ranges2);
122 : extern MultirangeType *multirange_intersect_internal(Oid mltrngtypoid,
123 : TypeCacheEntry *rangetyp,
124 : int32 range_count1,
125 : RangeType **ranges1,
126 : int32 range_count2,
127 : RangeType **ranges2);
128 :
129 : /* assorted support functions */
130 : extern TypeCacheEntry *multirange_get_typcache(FunctionCallInfo fcinfo,
131 : Oid mltrngtypid);
132 : extern void multirange_deserialize(TypeCacheEntry *rangetyp,
133 : const MultirangeType *multirange,
134 : int32 *range_count,
135 : RangeType ***ranges);
136 : extern MultirangeType *make_multirange(Oid mltrngtypoid,
137 : TypeCacheEntry *rangetyp,
138 : int32 range_count, RangeType **ranges);
139 : extern MultirangeType *make_empty_multirange(Oid mltrngtypoid,
140 : TypeCacheEntry *rangetyp);
141 : extern void multirange_get_bounds(TypeCacheEntry *rangetyp,
142 : const MultirangeType *multirange,
143 : uint32 i,
144 : RangeBound *lower, RangeBound *upper);
145 : extern RangeType *multirange_get_range(TypeCacheEntry *rangetyp,
146 : const MultirangeType *multirange, int i);
147 : extern RangeType *multirange_get_union_range(TypeCacheEntry *rangetyp,
148 : const MultirangeType *mr);
149 :
150 : #endif /* MULTIRANGETYPES_H */
|