-
Notifications
You must be signed in to change notification settings - Fork 0
/
median.c
147 lines (117 loc) · 3.33 KB
/
median.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
#include <postgres.h>
#include <fmgr.h>
#include <catalog/pg_type.h>
#include <catalog/pg_collation.h>
#include <utils/tuplesort.h>
#include <utils/typcache.h>
#include <utils/builtins.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
struct median {
uint32 num_elems;
Tuplesortstate *tss;
};
static void initialize_state(MemoryContext agg_context, bytea **state)
{
Size len = VARHDRSZ + sizeof(struct median);
*state = MemoryContextAllocZero(agg_context, len);
if (!*state)
elog(ERROR, "could not initialize state");
SET_VARSIZE(*state, len);
}
static Tuplesortstate *initialize_tuplesort(FmgrInfo *finfo)
{
Tuplesortstate *tss;
TypeCacheEntry *typc;
Oid collation = InvalidOid,
oid = get_fn_expr_argtype(finfo, 1);
if (oid == InvalidOid)
return NULL;
typc = lookup_type_cache(oid, TYPECACHE_LT_OPR);
if (!typc)
elog(ERROR, "could not get type cache entry for type %u", oid);
if (typc->lt_opr == InvalidOid)
elog(ERROR, "could not get less-than operator for type %u", oid);
if (oid == TEXTOID)
collation = C_COLLATION_OID;
tss = tuplesort_begin_datum(
oid, /* datum type */
typc->lt_opr, /* less-than operator */
collation,
0,
5000,
0); /* no random access */
return tss;
}
PG_FUNCTION_INFO_V1(median_transfn);
/*
* Median state transfer function.
*
* This function is called for every value in the set that we are calculating
* the median for. On first call, the aggregate state, if any, needs to be
* initialized.
*/
Datum
median_transfn(PG_FUNCTION_ARGS)
{
Datum val;
struct median *ms;
MemoryContext agg_context;
bytea *state = (PG_ARGISNULL(0) ? NULL : PG_GETARG_BYTEA_P(0));
if (!AggCheckCallContext(fcinfo, &agg_context))
elog(ERROR, "median_transfn called in non-aggregate context");
if (PG_NARGS() < 2)
elog(ERROR, "too few arguments");
if (!state)
{
initialize_state(agg_context, &state);
ms = (struct median *) VARDATA(state);
ms->tss = initialize_tuplesort(fcinfo->flinfo);
if (!ms->tss)
elog(ERROR, "could not initialize tuplesort");
}
if (!PG_ARGISNULL(1)) /* skip null values */
{
val = PG_GETARG_DATUM(1);
ms = (struct median *) VARDATA(state);
tuplesort_putdatum(ms->tss, val, 0);
ms->num_elems++;
}
PG_RETURN_BYTEA_P(state);
}
PG_FUNCTION_INFO_V1(median_finalfn);
/*
* Median final function.
*
* This function is called after all values in the median set has been
* processed by the state transfer function. It should perform any necessary
* post processing and clean up any temporary state.
*/
Datum
median_finalfn(PG_FUNCTION_ARGS)
{
Datum val, ab;
bool is_null;
uint32 pos;
struct median *ms;
MemoryContext agg_context;
bytea *state = (PG_ARGISNULL(0) ? NULL : PG_GETARG_BYTEA_P(0));
if (!AggCheckCallContext(fcinfo, &agg_context))
elog(ERROR, "median_finalfn called in non-aggregate context");
if (PG_NARGS() < 2)
elog(ERROR, "too few arguments");
if (!state)
PG_RETURN_NULL();
ms = (struct median *) VARDATA(state);
tuplesort_performsort(ms->tss);
pos = ms->num_elems / 2;
if (!tuplesort_skiptuples(ms->tss, pos, 1))
elog(ERROR, "could not advance %u slots", pos);
if (!tuplesort_getdatum(ms->tss, 1, &val, &is_null, &ab))
elog(ERROR, "could not get element after advancing %u slots", pos);
// tuplesort_end(ms->tss);
if (is_null)
elog(ERROR, "element at position %u is null - this should not happen", pos);
PG_RETURN_DATUM(val);
}