-
Notifications
You must be signed in to change notification settings - Fork 848
/
histogram.c
246 lines (202 loc) · 6.92 KB
/
histogram.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <catalog/pg_type.h>
#include <utils/builtins.h>
#include <utils/array.h>
#include <nodes/makefuncs.h>
#include <utils/lsyscache.h>
#include <netinet/in.h>
#include <libpq/pqformat.h>
#include "compat/compat.h"
#include "utils.h"
/* aggregate histogram:
* histogram(state, val, min, max, nbuckets) returns the histogram array with nbuckets
*
* Usage:
* SELECT grouping_element, histogram(field, min, max, nbuckets) FROM table GROUP BY
*grouping_element.
*
* Description:
* Histogram generates a histogram array based off of a specified range passed into the function.
* Values falling outside of this range are bucketed into the 0 or nbucket+1 buckets depending on
* if they are below or above the range, respectively. The resultant histogram therefore contains
* nbucket+2 buckets accounting for buckets outside the range.
*/
TS_FUNCTION_INFO_V1(ts_hist_sfunc);
TS_FUNCTION_INFO_V1(ts_hist_combinefunc);
TS_FUNCTION_INFO_V1(ts_hist_serializefunc);
TS_FUNCTION_INFO_V1(ts_hist_deserializefunc);
TS_FUNCTION_INFO_V1(ts_hist_finalfunc);
#define HISTOGRAM_SIZE(state, nbuckets) (sizeof(*(state)) + (nbuckets) * sizeof(*(state)->buckets))
typedef struct Histogram
{
int32 nbuckets;
Datum buckets[FLEXIBLE_ARRAY_MEMBER];
} Histogram;
/* histogram(state, val, min, max, nbuckets) */
Datum
ts_hist_sfunc(PG_FUNCTION_ARGS)
{
MemoryContext aggcontext;
Histogram *state = (Histogram *) (PG_ARGISNULL(0) ? NULL : PG_GETARG_POINTER(0));
Datum val_datum = PG_GETARG_DATUM(1);
Datum min_datum = PG_GETARG_DATUM(2);
Datum max_datum = PG_GETARG_DATUM(3);
double min = DatumGetFloat8(min_datum);
double max = DatumGetFloat8(max_datum);
int nbuckets;
if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "ts_hist_sfunc called in non-aggregate context");
}
if (min > max)
{
/* cannot generate a histogram with incompatible bounds */
elog(ERROR, "lower bound cannot exceed upper bound");
}
if (state == NULL)
{
nbuckets = PG_GETARG_INT32(4) + 2;
/* Allocate memory to a new histogram state array */
state = MemoryContextAllocZero(aggcontext, HISTOGRAM_SIZE(state, nbuckets));
state->nbuckets = nbuckets;
}
/* Since the number of buckets is an argument to the calls it might differ
* from the number we initialized with so we need to make sure we check
* against what we actually have.
*/
nbuckets = state->nbuckets - 2;
if (nbuckets != PG_GETARG_INT32(4))
elog(ERROR, "number of buckets must not change between calls");
int32 bucket = DatumGetInt32(DirectFunctionCall4(width_bucket_float8,
val_datum,
min_datum,
max_datum,
Int32GetDatum(nbuckets)));
/* Increment the proper histogram bucket */
Assert(bucket < state->nbuckets);
if (DatumGetInt32(state->buckets[bucket]) >= PG_INT32_MAX - 1)
elog(ERROR, "overflow in histogram");
state->buckets[bucket] = Int32GetDatum(DatumGetInt32(state->buckets[bucket]) + 1);
PG_RETURN_POINTER(state);
}
/* Make a copy of the histogram state */
static inline Histogram *
copy_state(MemoryContext aggcontext, Histogram *state)
{
Histogram *copy;
Size bucket_bytes = state->nbuckets * sizeof(*copy->buckets);
copy = MemoryContextAlloc(aggcontext, sizeof(*copy) + bucket_bytes);
copy->nbuckets = state->nbuckets;
memcpy(copy->buckets, state->buckets, bucket_bytes);
return copy;
}
/* ts_hist_combinefunc(internal, internal) => internal */
Datum
ts_hist_combinefunc(PG_FUNCTION_ARGS)
{
MemoryContext aggcontext;
Histogram *state1 = (Histogram *) (PG_ARGISNULL(0) ? NULL : PG_GETARG_POINTER(0));
Histogram *state2 = (Histogram *) (PG_ARGISNULL(1) ? NULL : PG_GETARG_POINTER(1));
Histogram *result;
if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "ts_hist_combinefunc called in non-aggregate context");
}
if (state1 == NULL && state2 == NULL)
{
PG_RETURN_NULL();
}
else if (state2 == NULL)
{
result = copy_state(aggcontext, state1);
}
else if (state1 == NULL)
{
result = copy_state(aggcontext, state2);
}
else
{
/* Since number of buckets is part of the aggregation call the initialization
* might be different in the partials so we error out if they are not identical. */
if (state1->nbuckets != state2->nbuckets)
elog(ERROR, "number of buckets must not change between calls");
result = copy_state(aggcontext, state1);
/* Combine values from state1 and state2 when both states are non-null */
for (int32 i = 0; i < state1->nbuckets; i++)
{
/* Perform addition using int64 to check for overflow */
int64 val = (int64) DatumGetInt32(result->buckets[i]);
int64 other = (int64) DatumGetInt32(state2->buckets[i]);
if (val + other >= PG_INT32_MAX)
elog(ERROR, "overflow in histogram combine");
result->buckets[i] = Int32GetDatum((int32) (val + other));
}
}
PG_RETURN_POINTER(result);
}
/* ts_hist_serializefunc(internal) => bytea */
Datum
ts_hist_serializefunc(PG_FUNCTION_ARGS)
{
Histogram *state;
StringInfoData buf;
Assert(!PG_ARGISNULL(0));
state = (Histogram *) PG_GETARG_POINTER(0);
pq_begintypsend(&buf);
pq_sendint32(&buf, state->nbuckets);
for (int32 i = 0; i < state->nbuckets; i++)
pq_sendint32(&buf, DatumGetInt32(state->buckets[i]));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* ts_hist_deserializefunc(bytea *, internal) => internal */
Datum
ts_hist_deserializefunc(PG_FUNCTION_ARGS)
{
MemoryContext aggcontext;
bytea *serialized;
int32 nbuckets;
int32 i;
StringInfoData buf;
Histogram *state;
if (!AggCheckCallContext(fcinfo, &aggcontext))
elog(ERROR, "ts_hist_deserializefunc called in non-aggregate context");
Assert(!PG_ARGISNULL(0));
serialized = PG_GETARG_BYTEA_P(0);
buf.data = VARDATA(serialized);
buf.len = VARSIZE(serialized) - VARHDRSZ;
buf.maxlen = VARSIZE(serialized) - VARHDRSZ;
buf.cursor = 0; /* used by pq_getmsgint*/
nbuckets = pq_getmsgint(&buf, 4);
state = MemoryContextAllocZero(aggcontext, HISTOGRAM_SIZE(state, nbuckets));
state->nbuckets = nbuckets;
for (i = 0; i < state->nbuckets; i++)
state->buckets[i] = Int32GetDatum(pq_getmsgint(&buf, 4));
PG_RETURN_POINTER(state);
}
/* hist_finalfunc(internal, val REAL, MIN REAL, MAX REAL, nbuckets INTEGER) => INTEGER[] */
Datum
ts_hist_finalfunc(PG_FUNCTION_ARGS)
{
Histogram *state;
int dims[1];
int lbs[1];
if (!AggCheckCallContext(fcinfo, NULL))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "ts_hist_finalfunc called in non-aggregate context");
}
state = (Histogram *) (PG_ARGISNULL(0) ? NULL : PG_GETARG_POINTER(0));
if (state == NULL)
PG_RETURN_NULL();
dims[0] = state->nbuckets;
lbs[0] = 1;
PG_RETURN_ARRAYTYPE_P(
construct_md_array(state->buckets, NULL, 1, dims, lbs, INT4OID, 4, true, 'i'));
}