forked from pjungwir/aggs_for_vecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hist_md.c
250 lines (216 loc) · 10 KB
/
hist_md.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
247
248
249
250
// We store bookkeping stuff in the vectmpvalues of our state variable,
// so that we don't have to figure everything out every row.
// Here are some macros to make sure we access what we want
// the same way every time:
#define HIST_MD_DIMENSIONS(s) (s)->vectmpvalues[0].i32
#define HIST_MD_VAL_LENGTH(s) (s)->vectmpvalues[1].i32
#define HIST_MD_VAL_INDEX(s, dim) (s)->vectmpvalues[2 + 4 * (dim) + 0].i32
#define HIST_MD_BUCKET_START(s, dim) (s)->vectmpvalues[2 + 4 * (dim) + 1]
#define HIST_MD_BUCKET_WIDTH(s, dim) (s)->vectmpvalues[2 + 4 * (dim) + 2]
#define HIST_MD_BUCKET_COUNT(s, dim) (s)->vectmpvalues[2 + 4 * (dim) + 3].i32
Datum hist_md_transfn(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(hist_md_transfn);
/**
* Returns a 2-dimensional histogram as a 2-d array.
*
* by Paul A. Jungwirth
*/
Datum
hist_md_transfn(PG_FUNCTION_ARGS)
{
Oid elemTypeId;
int16 elemTypeWidth, intTypeWidth;
bool elemTypeByValue, intTypeByValue;
char elemTypeAlignmentCode, intTypeAlignmentCode;
MemoryContext aggContext;
VecArrayBuildState *state = NULL;
int dimensions;
ArrayType *valsArray, *indexesArray, *bucketStartsArray, *bucketWidthsArray, *bucketCountsArray;
Datum *valVals, *indexVals, *bucketStartVals, *bucketWidthVals, *bucketCountVals;
bool *valNulls, *indexNulls, *bucketStartNulls, *bucketWidthNulls, *bucketCountNulls;
int valLength, indexLength, bucketStartLength, bucketWidthLength, bucketCountLength;
int resultLength;
int i;
int resultPos, idx, b, skip;
if (!AggCheckCallContext(fcinfo, &aggContext)) {
elog(ERROR, "hist_md_transfn called in non-aggregate context");
}
if (PG_ARGISNULL(2)) elog(ERROR, "hist_md val_indexes can't be null");
if (PG_ARGISNULL(3)) elog(ERROR, "hist_md bucket_starts can't be null");
if (PG_ARGISNULL(4)) elog(ERROR, "hist_md bucket_widths can't be null");
if (PG_ARGISNULL(5)) elog(ERROR, "hist_md bucket_counts can't be null");
if (PG_ARGISNULL(0)) {
indexesArray = PG_GETARG_ARRAYTYPE_P(2);
bucketStartsArray = PG_GETARG_ARRAYTYPE_P(3);
bucketWidthsArray = PG_GETARG_ARRAYTYPE_P(4);
bucketCountsArray = PG_GETARG_ARRAYTYPE_P(5);
if (ARR_NDIM(indexesArray) != 1) {
elog(ERROR, "hist_md val_indexes must have 1 dimension");
}
if (ARR_NDIM(bucketStartsArray) != 1) {
elog(ERROR, "hist_md bucket_starts must have 1 dimension");
}
if (ARR_NDIM(bucketWidthsArray) != 1) {
elog(ERROR, "hist_md bucket_widths must have 1 dimension");
}
if (ARR_NDIM(bucketCountsArray) != 1) {
elog(ERROR, "hist_md bucket_counts must have 1 dimension");
}
// Figure out the result dimensions:
get_typlenbyvalalign(INT4OID, &intTypeWidth, &intTypeByValue, &intTypeAlignmentCode);
deconstruct_array(indexesArray, INT4OID, intTypeWidth, intTypeByValue, intTypeAlignmentCode,
&indexVals, &indexNulls, &indexLength);
dimensions = indexLength;
if (ARR_ELEMTYPE(bucketCountsArray) != INT4OID) {
elog(ERROR, "hist_md bucket_counts must be array of INTEGER");
}
deconstruct_array(bucketCountsArray, INT4OID, intTypeWidth, intTypeByValue, intTypeAlignmentCode,
&bucketCountVals, &bucketCountNulls, &bucketCountLength);
if (bucketCountLength != dimensions) elog(ERROR, "hist_md bucket_counts must have one entry for each dimension");
resultLength = 1;
for (i = 0; i < dimensions; i++) {
if (bucketCountNulls[i]) elog(ERROR, "bucket_counts can't contain NULLs");
resultLength *= DatumGetInt32(bucketCountVals[i]);
}
elemTypeId = ARR_ELEMTYPE(bucketStartsArray);
if (elemTypeId != INT2OID &&
elemTypeId != INT4OID &&
elemTypeId != INT8OID &&
elemTypeId != FLOAT4OID &&
elemTypeId != FLOAT8OID) {
elog(ERROR, "hist_md vals must be array of SMALLINT, INTEGER, BIGINT, REAL, or DOUBLE PRECISION");
}
// make sure we have enough tmp space
// for our bookkeeping:
state = initVecArrayResultWithNulls(elemTypeId, INT4OID, aggContext, Max(resultLength, 1 + dimensions * 4));
memset(state->state.dnulls, 0, state->state.alen * sizeof(bool));
state->state.nelems = resultLength;
HIST_MD_DIMENSIONS(state) = dimensions;
// Can't initialize this until we get our first non-NULL row:
HIST_MD_VAL_LENGTH(state) = -1;
// Now put all the params into `state`
// for easy access on future iterations:
get_typlenbyvalalign(elemTypeId, &elemTypeWidth, &elemTypeByValue, &elemTypeAlignmentCode);
deconstruct_array(bucketStartsArray, elemTypeId, elemTypeWidth, elemTypeByValue, elemTypeAlignmentCode,
&bucketStartVals, &bucketStartNulls, &bucketStartLength);
deconstruct_array(bucketWidthsArray, elemTypeId, elemTypeWidth, elemTypeByValue, elemTypeAlignmentCode,
&bucketWidthVals, &bucketWidthNulls, &bucketWidthLength);
// if (indexLength != dimensions) elog(ERROR, "hist_md val_indexes must have one entry for each dimension");
if (bucketStartLength != dimensions) elog(ERROR, "hist_md bucket_starts must have one entry for each dimension");
if (bucketWidthLength != dimensions) elog(ERROR, "hist_md bucket_widths must have one entry for each dimension");
for (i = 0; i < dimensions; i++) {
if (indexNulls[i]) elog(ERROR, "val_indexes can't contain NULLs");
if (bucketStartNulls[i]) elog(ERROR, "bucket_starts can't contain NULLs");
if (bucketWidthNulls[i]) elog(ERROR, "bucket_widths can't contain NULLs");
// Postgres arrays are 1-indexed for users,
// so we should behave the same way:
HIST_MD_VAL_INDEX(state, i) = DatumGetInt32(indexVals[i]) - 1;
HIST_MD_BUCKET_COUNT(state, i) = DatumGetInt32(bucketCountVals[i]);
if (HIST_MD_VAL_INDEX(state, i) < 0) {
elog(ERROR, "val_indexes must all be greater than 0");
}
switch (elemTypeId) {
case INT2OID:
HIST_MD_BUCKET_START(state, i).i16 = DatumGetInt16(bucketStartVals[i]);
HIST_MD_BUCKET_WIDTH(state, i).i16 = DatumGetInt16(bucketWidthVals[i]);
break;
case INT4OID:
HIST_MD_BUCKET_START(state, i).i32 = DatumGetInt32(bucketStartVals[i]);
HIST_MD_BUCKET_WIDTH(state, i).i32 = DatumGetInt32(bucketWidthVals[i]);
break;
case INT8OID:
HIST_MD_BUCKET_START(state, i).i64 = DatumGetInt64(bucketStartVals[i]);
HIST_MD_BUCKET_WIDTH(state, i).i64 = DatumGetInt64(bucketWidthVals[i]);
break;
case FLOAT4OID:
HIST_MD_BUCKET_START(state, i).f4 = DatumGetFloat4(bucketStartVals[i]);
HIST_MD_BUCKET_WIDTH(state, i).f4 = DatumGetFloat4(bucketWidthVals[i]);
break;
case FLOAT8OID:
HIST_MD_BUCKET_START(state, i).f8 = DatumGetFloat8(bucketStartVals[i]);
HIST_MD_BUCKET_WIDTH(state, i).f8 = DatumGetFloat8(bucketWidthVals[i]);
break;
}
}
} else {
state = (VecArrayBuildState *)PG_GETARG_POINTER(0);
elemTypeId = state->inputElementType;
get_typlenbyvalalign(elemTypeId, &elemTypeWidth, &elemTypeByValue, &elemTypeAlignmentCode);
}
// Nothing to change:
// Note this has to come after we initialize `state`,
// so that we can return an array of all zeros if we get a single NULL row.
if (PG_ARGISNULL(1)) PG_RETURN_POINTER(state);
valsArray = PG_GETARG_ARRAYTYPE_P(1);
if (ARR_NDIM(valsArray) != 1) {
elog(ERROR, "hist_md vals must have 1 dimension");
}
deconstruct_array(valsArray, elemTypeId, elemTypeWidth, elemTypeByValue, elemTypeAlignmentCode,
&valVals, &valNulls, &valLength);
if (HIST_MD_VAL_LENGTH(state) == -1) {
HIST_MD_VAL_LENGTH(state) = valLength;
for (i = 0; i < HIST_MD_DIMENSIONS(state); i++) {
if (HIST_MD_VAL_INDEX(state, i) >= valLength) {
elog(ERROR, "val_indexes must all be less than or equal to %d", valLength);
}
}
} else if (HIST_MD_VAL_LENGTH(state) != valLength) {
elog(ERROR, "hist_md val arrays must all be the same length");
}
resultPos = 0;
skip = 1;
for (i = 0; i < HIST_MD_DIMENSIONS(state); i++) {
idx = HIST_MD_VAL_INDEX(state, i);
// One of the values is null so we can't plot this row:
if (valNulls[idx]) PG_RETURN_POINTER(state);
switch (elemTypeId) {
case INT2OID:
b = (DatumGetInt16(valVals[idx]) - HIST_MD_BUCKET_START(state, i).i16) / HIST_MD_BUCKET_WIDTH(state, i).i16;
break;
case INT4OID:
b = (DatumGetInt32(valVals[idx]) - HIST_MD_BUCKET_START(state, i).i32) / HIST_MD_BUCKET_WIDTH(state, i).i32;
break;
case INT8OID:
b = (DatumGetInt64(valVals[idx]) - HIST_MD_BUCKET_START(state, i).i64) / HIST_MD_BUCKET_WIDTH(state, i).i64;
break;
case FLOAT4OID:
b = (DatumGetFloat4(valVals[idx]) - HIST_MD_BUCKET_START(state, i).f4) / HIST_MD_BUCKET_WIDTH(state, i).f4;
break;
case FLOAT8OID:
b = (DatumGetFloat8(valVals[idx]) - HIST_MD_BUCKET_START(state, i).f8) / HIST_MD_BUCKET_WIDTH(state, i).f8;
break;
}
resultPos += b * skip;
skip *= HIST_MD_BUCKET_COUNT(state, i);
}
state->veccounts[resultPos] += 1;
PG_RETURN_POINTER(state);
}
Datum hist_md_finalfn(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(hist_md_finalfn);
Datum
hist_md_finalfn(PG_FUNCTION_ARGS)
{
Datum result;
VecArrayBuildState *state;
MemoryContext aggContext;
int *dims;
int *lbs; // Lower Bounds of each dimension
int i;
if (!AggCheckCallContext(fcinfo, &aggContext)) {
elog(ERROR, "hist_md_transfn called in non-aggregate context");
}
state = PG_ARGISNULL(0) ? NULL : (VecArrayBuildState *)PG_GETARG_POINTER(0);
if (state == NULL) PG_RETURN_NULL();
for (i = 0; i < state->state.nelems; i++) {
state->state.dvalues[i] = Int32GetDatum((int32)state->veccounts[i]);
}
dims = MemoryContextAlloc(aggContext, HIST_MD_DIMENSIONS(state) * sizeof(int));
lbs = MemoryContextAlloc(aggContext, HIST_MD_DIMENSIONS(state) * sizeof(int));
for (i = 0; i < HIST_MD_DIMENSIONS(state); i++) {
dims[i] = HIST_MD_BUCKET_COUNT(state, i);
lbs[i] = 1;
}
result = makeMdArrayResult(&state->state, HIST_MD_DIMENSIONS(state), dims, lbs, CurrentMemoryContext, false);
PG_RETURN_DATUM(result);
}