forked from bashtanov/argm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargm.c
295 lines (249 loc) · 7.4 KB
/
argm.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "postgres.h"
#include "fmgr.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/typcache.h"
PG_MODULE_MAGIC;
/*
*******************************************************************************
* Types
*******************************************************************************
*/
typedef struct ArgmDatumWithType {
Oid type;
/* info about datatype */
int16 typlen;
bool typbyval;
char typalign;
RegProcedure cmp_proc;
/* data itself */
bool is_null;
Datum value;
} ArgmDatumWithType;
typedef struct ArgmState {
/*
* element 0 of this array is payload value
* elements 1, 2, ... are keys to be sorted by
*/
ArgmDatumWithType *keys;
int key_count;
} ArgmState;
/*
*******************************************************************************
* Supplementary function headers
*******************************************************************************
*/
static void
argm_copy_datum(bool is_null, Datum src, ArgmDatumWithType *dest, bool free);
static Datum
argm_transfn_universal(PG_FUNCTION_ARGS, int32 compareFunctionResultToAdvance);
/*
*******************************************************************************
* Headers for functions available in the DB
*******************************************************************************
*/
PG_FUNCTION_INFO_V1(argmax_transfn);
PG_FUNCTION_INFO_V1(argmin_transfn);
PG_FUNCTION_INFO_V1(argm_finalfn);
PG_FUNCTION_INFO_V1(anyold_transfn);
PG_FUNCTION_INFO_V1(anyold_finalfn);
/*
*******************************************************************************
* Supplementary function bodies
*******************************************************************************
*/
static void
argm_copy_datum(bool is_null, Datum src, ArgmDatumWithType *dest, bool free)
{
if (free && !dest->typbyval && !dest->is_null)
pfree(DatumGetPointer(dest->value));
if (is_null)
dest->is_null = true;
else {
dest->is_null = false;
if (dest->typlen == -1)
dest->value = PointerGetDatum(PG_DETOAST_DATUM_COPY(src));
else
dest->value = datumCopy(src, dest->typbyval, dest->typlen);
}
}
/*
* The function args are the following:
* 0 -- state (internal type)
* 1 -- payload
* 2, 3, ... -- keys
*/
static Datum
argm_transfn_universal(PG_FUNCTION_ARGS, int32 compareFunctionResultToAdvance)
{
Oid type;
ArgmState *state;
MemoryContext aggcontext,
oldcontext;
int i;
int32 comparison_result;
if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "argm**_transfn called in non-aggregate context");
}
if (PG_ARGISNULL(0))
{
/* No state, first time through --- initialize */
/* Make a temporary context to hold all the junk */
oldcontext = MemoryContextSwitchTo(aggcontext);
/* Initialize the state variable*/
state = palloc(sizeof(ArgmState));
state->key_count = PG_NARGS() - 1;
state->keys = palloc(sizeof(ArgmDatumWithType) * (state->key_count));
/*
* store the info about payload and keys (arguments 1 and 2, 3, ... respectively)
* into state->keys ([0] and [1, 2, ...] respectively)
*/
for (i = 0; i < state->key_count; i++)
{
type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
if (type == InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine input data type")));
state->keys[i].type = type;
get_typlenbyvalalign(type,
&state->keys[i].typlen,
&state->keys[i].typbyval,
&state->keys[i].typalign);
/* For keys, but not for payload, determine the sorting proc */
if (i != 0)
state->keys[i].cmp_proc =
lookup_type_cache(state->keys[i].type,
TYPECACHE_CMP_PROC)->cmp_proc;
/* Copy initial values */
argm_copy_datum(PG_ARGISNULL(i + 1),
PG_GETARG_DATUM(i + 1),
&(state->keys[i]), false);
}
}
else
{
state = (ArgmState *) PG_GETARG_POINTER(0);
oldcontext = MemoryContextSwitchTo(aggcontext);
/*
* compare the stored keys (but not payload; that is, elements 1, 2, ... )
* and the input keys (but not payload; that is, elements 2, 3, ... )
* lexicographically
*/
for (i = 1; i < state->key_count; i++)
{
if (PG_ARGISNULL(i + 1) && !state->keys[i].is_null)
/* nulls last */
break;
if (!PG_ARGISNULL(i + 1) && state->keys[i].is_null) {
/* nulls last */
comparison_result = 1;
} else {
comparison_result = DatumGetInt32(OidFunctionCall2Coll(
state->keys[i].cmp_proc,
PG_GET_COLLATION(),
PG_GETARG_DATUM(i + 1),
state->keys[i].value
)) * compareFunctionResultToAdvance;
if (comparison_result < 0)
break;
}
if (comparison_result > 0)
{
/*
* if we are here it means that the input data was considered
* to be "better" (i.e. more for argmax or less for argmin)
* than the stored ones
*
* thus copy the new payloads and the keys to the state
*/
for (i = 0; i < state->key_count; i++)
argm_copy_datum(PG_ARGISNULL(i + 1),
PG_GETARG_DATUM(i + 1),
&(state->keys[i]), true);
break;
}
}
}
MemoryContextSwitchTo(oldcontext);
PG_RETURN_POINTER(state);
}
/*
*******************************************************************************
* Bodies for functions available in the DB
*******************************************************************************
*/
Datum
argmax_transfn(PG_FUNCTION_ARGS)
{
return argm_transfn_universal(fcinfo, 1);
}
Datum
argmin_transfn(PG_FUNCTION_ARGS)
{
return argm_transfn_universal(fcinfo, -1);
}
Datum argm_finalfn(PG_FUNCTION_ARGS)
{
ArgmState *state;
/* cannot be called directly because of internal-type argument */
Assert(AggCheckCallContext(fcinfo, NULL));
state = (ArgmState *) PG_GETARG_POINTER(0);
if (state->keys[0].is_null)
PG_RETURN_NULL();
PG_RETURN_DATUM(state->keys[0].value);
}
Datum anyold_transfn(PG_FUNCTION_ARGS)
{
Oid type;
Datum state;
MemoryContext aggcontext,
oldcontext;
int16 typlen;
bool typbyval;
char typalign;
if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "anyold_transfn called in non-aggregate context");
}
if (PG_ARGISNULL(0))
{
if (PG_ARGISNULL(1))
PG_RETURN_NULL();
/* First non-null value --- initialize */
oldcontext = MemoryContextSwitchTo(aggcontext);
type = get_fn_expr_argtype(fcinfo->flinfo, 1);
if (type == InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine input data type")));
get_typlenbyvalalign(type,
&typlen,
&typbyval,
&typalign);
/* Copy initial value */
if (typlen == -1)
state = PointerGetDatum(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
else
state = datumCopy(PG_GETARG_DATUM(1), typbyval, typlen);
MemoryContextSwitchTo(oldcontext);
}
else
{
state = PG_GETARG_DATUM(0);
}
PG_RETURN_DATUM(state);
}
Datum anyold_finalfn(PG_FUNCTION_ARGS)
{
/* cannot be called directly because of internal-type argument */
Assert(AggCheckCallContext(fcinfo, NULL));
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
}