-
Notifications
You must be signed in to change notification settings - Fork 0
/
completely_covers.c
189 lines (157 loc) · 6.49 KB
/
completely_covers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* completely_covers.c -
* Provides an aggregate function
* that tells whether a bunch of input ranges competely cover a target range.
*/
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <postgres.h>
#include <fmgr.h>
#include <pg_config.h>
#include <miscadmin.h>
#include <utils/array.h>
#include <utils/guc.h>
#include <utils/acl.h>
#include <utils/lsyscache.h>
#include <utils/builtins.h>
#include <utils/rangetypes.h>
#include <utils/timestamp.h>
#include <catalog/pg_type.h>
#include <catalog/catalog.h>
#include <catalog/pg_tablespace.h>
#include <commands/tablespace.h>
#include "completely_covers.h"
typedef struct completely_covers_state {
TimestampTz covered_to;
TimestampTz target_start;
TimestampTz target_end;
bool target_start_unbounded;
bool target_end_unbounded;
bool answer_is_null;
bool finished; // Used to avoid further processing if we have already succeeded/failed.
bool completely_covered;
} completely_covers_state;
Datum completely_covers_transfn(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(completely_covers_transfn);
Datum completely_covers_transfn(PG_FUNCTION_ARGS)
{
MemoryContext aggContext;
completely_covers_state *state;
RangeType *current_range,
*target_range;
RangeBound current_start, current_end, target_start, target_end;
TypeCacheEntry *typcache;
bool current_empty, target_empty;
bool first_time;
if (!AggCheckCallContext(fcinfo, &aggContext)) {
elog(ERROR, "completely_covers called in non-aggregate context");
}
if (PG_ARGISNULL(0)) {
// Need to use MemoryContextAlloc with aggContext, not just palloc0,
// or the state will get cleared in between invocations:
state = (completely_covers_state *)MemoryContextAlloc(aggContext, sizeof(completely_covers_state));
state->finished = false;
state->completely_covered = false;
first_time = true;
// Need to find out the target range:
// TODO: Technically this will fail to detect an inconsistent target
// if only the first row is NULL:
if (PG_ARGISNULL(2)) {
// return NULL from the whole thing
state->answer_is_null = true;
state->finished = true;
PG_RETURN_POINTER(state);
}
state->answer_is_null = false;
target_range = PG_GETARG_RANGE(2);
typcache = range_get_typcache(fcinfo, RangeTypeGetOid(target_range));
range_deserialize(typcache, target_range, &target_start, &target_end, &target_empty);
state->target_start_unbounded = target_start.infinite;
state->target_end_unbounded = target_end.infinite;
state->target_start = DatumGetTimestampTz(target_start.val);
state->target_end = DatumGetTimestampTz(target_end.val);
// ereport(NOTICE, (errmsg("STARTING: state is [%ld, %ld) target is [%ld, %ld)", state->target_start, state->target_end, DatumGetTimestampTz(target_start.val), DatumGetTimestampTz(target_end.val))));
state->covered_to = 0;
} else {
// ereport(NOTICE, (errmsg("looking up state....")));
state = (completely_covers_state *)PG_GETARG_POINTER(0);
// TODO: Is there any better way to exit an aggregation early?
// Even https://pgxn.org/dist/first_last_agg/ hits all the input rows:
if (state->finished) PG_RETURN_POINTER(state);
first_time = false;
// Make sure the second arg is always the same:
if (PG_ARGISNULL(2)) {
ereport(ERROR, (errmsg("completely_covers second argument must be constant across the group")));
}
target_range = PG_GETARG_RANGE(2);
typcache = range_get_typcache(fcinfo, RangeTypeGetOid(target_range));
range_deserialize(typcache, target_range, &target_start, &target_end, &target_empty);
// ereport(NOTICE, (errmsg("state is [%ld, %ld) target is [%ld, %ld)", state->target_start, state->target_end, DatumGetTimestampTz(target_start.val), DatumGetTimestampTz(target_end.val))));
if (DatumGetTimestampTz(target_start.val) != state->target_start || DatumGetTimestampTz(target_end.val) != state->target_end) {
ereport(ERROR, (errmsg("completely_covers second argument must be constant across the group")));
}
}
if (PG_ARGISNULL(1)) PG_RETURN_POINTER(state);
current_range = PG_GETARG_RANGE(1);
typcache = range_get_typcache(fcinfo, RangeTypeGetOid(current_range));
range_deserialize(typcache, current_range, ¤t_start, ¤t_end, ¤t_empty);
// ereport(NOTICE, (errmsg("current is [%ld, %ld)", DatumGetTimestampTz(current_start.val), DatumGetTimestampTz(current_end.val))));
if (first_time) {
if (state->target_start_unbounded && !current_start.infinite) {
state->finished = true;
state->completely_covered = false;
PG_RETURN_POINTER(state);
}
if (DatumGetTimestampTz(current_start.val) > state->target_start) {
state->finished = true;
state->completely_covered = false;
PG_RETURN_POINTER(state);
}
} else {
// If there is a gap then fail:
if (DatumGetTimestampTz(current_start.val) > state->covered_to) {
// ereport(NOTICE, (errmsg("found a gap")));
state->finished = true;
state->completely_covered = false;
PG_RETURN_POINTER(state);
}
}
// This check is why we set covered_to to 0 above on the first pass:
// Note this check will not check unsorted inputs in some cases:
// - the inputs cover the target before we hit an out-of-order input.
if (DatumGetTimestampTz(current_start.val) < state->covered_to) {
// Right? Maybe this should be a warning....
ereport(ERROR, (errmsg("completely_covered first argument should be sorted")));
// ereport(ERROR, (errmsg("completely_covered first argument should be sorted but got %ld after covering up to %ld", DatumGetTimestampTz(current_start.val), state->covered_to)));
}
if (current_end.infinite) {
state->completely_covered = true;
state->finished = true;
} else {
state->covered_to = DatumGetTimestampTz(current_end.val);
if (!state->target_end_unbounded && state->covered_to >= state->target_end) {
state->completely_covered = true;
state->finished = true;
}
}
PG_RETURN_POINTER(state);
}
Datum completely_covers_finalfn(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(completely_covers_finalfn);
Datum completely_covers_finalfn(PG_FUNCTION_ARGS)
{
completely_covers_state *state;
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
state = (completely_covers_state *)PG_GETARG_POINTER(0);
if (state->answer_is_null) {
PG_RETURN_NULL();
} else {
PG_RETURN_BOOL(state->completely_covered);
}
}