-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_avg.c
171 lines (141 loc) · 5.21 KB
/
array_avg.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
/*
array_avg.c
#### replace to your local include dir, c file, so file, o file.
gcc -I/home/jian/postgres/2023_05_25_beta5421/include/server -fPIC -c /home/jian/Desktop/regress_pgsql/array_avg.c
gcc -shared -o /home/jian/Desktop/regress_pgsql/array_avg.so /home/jian/Desktop/regress_pgsql/array_avg.o
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "utils/numeric.h"
#include "funcapi.h"
#include "utils/lsyscache.h"
#include "utils/fmgrprotos.h"
#include "utils/timestamp.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(array_avg);
Datum
array_avg(PG_FUNCTION_ARGS)
{
ArrayType *arr;
Datum *type_oids;
Oid basetype;
int nelements;
int16 typlen;
bool typbyval;
char typalign;
bool *typnullflag;
int i;
bool resisnull = true;
int nullcnt = 0;
int non_nullcnt = 0;
int ndim;
Datum countd,
sumd;
arr = PG_GETARG_ARRAYTYPE_P(0);
if (PG_ARGISNULL(0))
ereport(ERROR, (errmsg("only for non null arrays.")));
//This requirement could probably be lifted pretty easily:
ndim = ARR_NDIM(arr);
if (ndim == 0)
ereport(ERROR, (errmsg("empty array not allowed")));
else if (ndim > 1)
ereport(ERROR, (errmsg("One-dimesional arrays are required")));
//actual data pointer.
type_oids = (Datum *) ARR_DATA_PTR(arr);
basetype = ARR_ELEMTYPE(arr);
get_typlenbyvalalign(basetype, &typlen, &typbyval, &typalign);
nelements = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
//Extract the array contents(as Datum objects).
deconstruct_array(arr, basetype, typlen, typbyval, typalign, &type_oids, &typnullflag, &nelements);
switch (basetype)
{
case INT8OID:
sumd = DirectFunctionCall1(int8_numeric, 0);
for (i = 0; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(numeric_add, DirectFunctionCall1(int8_numeric, type_oids[i]), sumd);
}
non_nullcnt = nelements - nullcnt;
countd = NumericGetDatum(int64_to_numeric(non_nullcnt));
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(DirectFunctionCall2(numeric_div,sumd, countd));
case INT4OID:
sumd = DirectFunctionCall1(int8_numeric, 0);
for (i = 0; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(numeric_add, DirectFunctionCall1(int4_numeric, type_oids[i]), sumd);
}
non_nullcnt = nelements - nullcnt;
countd = NumericGetDatum(int64_to_numeric(non_nullcnt));
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(DirectFunctionCall2(numeric_div,sumd, countd));
case INT2OID:
sumd = DirectFunctionCall1(int8_numeric, 0);
for (i = 0; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(numeric_add, DirectFunctionCall1(int2_numeric, type_oids[i]), sumd);
}
non_nullcnt = nelements - nullcnt;
countd = NumericGetDatum(int64_to_numeric( non_nullcnt));
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(DirectFunctionCall2(numeric_div,sumd, countd));
case NUMERICOID:
sumd = DirectFunctionCall1(int8_numeric,0);
for (i = 0; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(numeric_add, type_oids[i], sumd);
}
non_nullcnt = nelements - nullcnt;
countd = NumericGetDatum(int64_to_numeric( non_nullcnt));
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(DirectFunctionCall2(numeric_div,sumd, countd));
case INTERVALOID:
sumd = DirectFunctionCall6(make_interval,0,0,0,0,0,0);
for (i = 0; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(interval_pl, type_oids[i],sumd);
}
non_nullcnt = nelements - nullcnt;
countd = Float8GetDatum(non_nullcnt);
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(DirectFunctionCall2(interval_div,sumd, countd));
default:
ereport(ERROR, (errmsg("allowed data type int2, int4, int8, numeric, interval")));
}
}