-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggtrans.c
192 lines (163 loc) · 4.52 KB
/
aggtrans.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
#include "postgres.h"
#include "utils/array.h"
#include "utils/fmgrprotos.h"
#include "utils/numeric.h"
#include "nodes/execnodes.h"
#include "aggtrans.h"
#define INIT_AGGSTATE(aggstate) \
{ \
Node *node = (Node *) &aggstate; \
memset(&aggstate, 0, sizeof(AggState)); \
node->type = T_AggState; \
}
static Datum CallAggfunction1(FmgrInfo *flinfo, Datum arg1, fmNodePtr *context)
{
LOCAL_FCINFO(fcinfo, 1);
InitFunctionCallInfoData(*fcinfo, flinfo, 1, InvalidOid, (Node *)context, NULL);
fcinfo->args[0].value = arg1;
fcinfo->args[0].isnull = false;
return FunctionCallInvoke(fcinfo);
}
/* KITE */
/* int */
static Datum agg_p_int128(int64 count, int128 sum) {
PolyNumAggState *state;
AggState aggstate;
FmgrInfo flinfo;
state = makePolyNumAggStateCurrentContext(false);
state->N = count;
#ifdef HAVE_INT128
state->sumX = sum;
#else
// error out
elog(ERROR, "system not support int128");
#endif
memset(&flinfo, 0, sizeof(FmgrInfo));
fmgr_info_cxt(fmgr_internal_function("int8_avg_serialize"), &flinfo, CurrentMemoryContext);
INIT_AGGSTATE(aggstate);
return CallAggfunction1(&flinfo, (Datum)state, (fmNodePtr *)&aggstate);
}
/* Int8TransTypeData. expected 2-element int8 array (numeric.c:5799) */
Datum avg_p_int64(PG_FUNCTION_ARGS) {
int64 count = PG_GETARG_INT64(0);
int64 sum = PG_GETARG_INT64(1);
int ndim = 1;
int nelem = 2;
int sz = ARR_OVERHEAD_NONULLS(ndim);
sz += nelem * sizeof(int64_t);
ArrayType *arr = (ArrayType *) palloc(sz);
SET_VARSIZE(arr, sz);
arr->ndim = ndim;
arr->dataoffset = 0;
arr->elemtype = INT8OID;
int *dims = ARR_DIMS(arr);
int *lbs = ARR_LBOUND(arr);
dims[0] = nelem;
lbs[0] = 1;
int64_t *p = (int64_t *) ARR_DATA_PTR(arr);
p[0] = count;
p[1] = sum;
PG_RETURN_ARRAYTYPE_P(arr);
}
Datum avg_p_int128(PG_FUNCTION_ARGS) {
int64 count = PG_GETARG_INT64(0);
int128 sum = *((int128 *) PG_GETARG_POINTER(1));
return agg_p_int128(count, sum);
}
Datum sum_p_int64(PG_FUNCTION_ARGS) {
int64 count = 1;
int64 sum = PG_GETARG_INT64(0);
return agg_p_int128(count, sum);
}
Datum sum_p_int128(PG_FUNCTION_ARGS) {
int64 count = 1;
int128 sum = *((int128 *) PG_GETARG_POINTER(0));
return agg_p_int128(count, sum);
}
/* numeric */
static Datum agg_p_numeric(int64 count, Numeric sum) {
NumericAggState *state;
AggState aggstate;
FmgrInfo flinfo;
state = makeNumericAggStateCurrentContext(false);
state->N = count;
do_numeric_accum(state, sum);
state->N--;
memset(&flinfo, 0, sizeof(FmgrInfo));
fmgr_info_cxt(fmgr_internal_function("numeric_avg_serialize"), &flinfo, CurrentMemoryContext);
INIT_AGGSTATE(aggstate);
return CallAggfunction1(&flinfo, (Datum)state, (fmNodePtr *)&aggstate);
}
Datum avg_p_numeric(PG_FUNCTION_ARGS) {
int64 count = (int64) PG_GETARG_INT64(0);
Numeric sum = PG_GETARG_NUMERIC(1);
return agg_p_numeric(count, sum);
}
Datum sum_p_numeric(PG_FUNCTION_ARGS) {
int64 count = 1;
Numeric sum = PG_GETARG_NUMERIC(0);
return agg_p_numeric(count, sum);
}
Datum sum_p_float4(PG_FUNCTION_ARGS) {
float8 sum = PG_GETARG_FLOAT8(0);
float sumf = (float) sum;
return Float4GetDatum(sumf);
}
/* float8 */
Datum avg_p_float8(PG_FUNCTION_ARGS) {
int64 count = PG_GETARG_INT64(0);
float8 sum = PG_GETARG_FLOAT8(1);
float8 N = count;
float8 Sxx = 0;
Datum transdatums[3];
ArrayType *result;
transdatums[0] = Float8GetDatumFast(N);
transdatums[1] = Float8GetDatumFast(sum);
transdatums[2] = Float8GetDatumFast(Sxx);
result = construct_array(transdatums, 3,
FLOAT8OID,
sizeof(float8), FLOAT8PASSBYVAL, 'd');
PG_RETURN_ARRAYTYPE_P(result);
}
PGFunction GetTranscodingFnFromOid(Oid aggfnoid) {
PGFunction refnaddr = NULL;
if (aggfnoid == InvalidOid)
{
return NULL;
}
switch (aggfnoid)
{
case 2100: // avg bigint
/* - 2100 pg_catalog.avg int8|bigint */
refnaddr = avg_p_int128;
break;
case 2101: // avg integer
case 2102: // avg smallint
refnaddr = avg_p_int64;
break;
case 2107:
/* - 2107 pg_catalog.sum int8|bigint */
refnaddr = sum_p_int128;
break;
case 2110:
/* - 2110 pg_catalog.sum float */
refnaddr = sum_p_float4;
break;
case 2103:
/* - 2103 pg_catalog.avg numeric */
refnaddr = avg_p_numeric;
break;
case 2114:
/* - 2114 pg_catalog.sum numeric */
refnaddr = sum_p_numeric;
break;
case 2104:
case 2105:
/* - 2105 pg_catalog.avg float8 */
refnaddr = avg_p_float8;
break;
default:
break;
}
return refnaddr;
}