-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_sum.c
221 lines (188 loc) · 7.16 KB
/
array_sum.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
/*
/home/jian/Desktop/regress_pgsql/array_sum.c
https://stackoverflow.com/questions/16992339/why-is-postgresql-array-access-so-much-faster-in-c-than-in-pl-pgsql/16996606#16996606
gcc -I/home/jian/postgres/2023_05_25_beta5421/include/server -fPIC -c /home/jian/Desktop/regress_pgsql/array_sum.c
gcc -shared -o /home/jian/Desktop/regress_pgsql/array_sum.so /home/jian/Desktop/regress_pgsql/array_sum.o
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
#include "funcapi.h"
#include "utils/lsyscache.h"
#include "utils/fmgrprotos.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(array_sum);
Datum
array_sum(PG_FUNCTION_ARGS)
{
ArrayType *arr;
Datum *type_oids;
Oid basetype;
int nelements;
int16 typlen; /* needed info about element datatype */
bool typbyval; /* needed info about element datatype */
char typalign; /* needed info about element datatype */
bool *typnullflag; /* null bit map */
int i;
int ndim;
int nullcnt = 0; /* init value for later doing arithmetic */
int non_nullcnt;
bool resisnull = true;
Datum sumd;
if (PG_ARGISNULL(0))
ereport(ERROR, (errmsg("only for non null arrays.")));
arr = PG_GETARG_ARRAYTYPE_P(0);
//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);
/* get array container base type */
basetype = ARR_ELEMTYPE(arr);
if (basetype != INT2OID && basetype != INT4OID &&
basetype != INT8OID && basetype != FLOAT4OID &&
basetype != FLOAT8OID && basetype != NUMERICOID &&
basetype != INTERVALOID)
ereport(ERROR,
(errmsg("allowed data type int2, int4,int8,"
"float4,float8,numeric, interval")));
get_typlenbyvalalign(basetype, &typlen, &typbyval, &typalign);
nelements = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
if (nelements == 0)
PG_RETURN_NULL();
deconstruct_array(arr,basetype,typlen,typbyval
,typalign,&type_oids,&typnullflag,&nelements);
switch (basetype)
{
case INT8OID:
/* for properly datatype datum value initialize */
sumd = DirectFunctionCall1(int8_numeric, 0);
for (i = 0; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
// elog(NOTICE,"nullcnt:%d", nullcnt);
continue;
}
sumd = DirectFunctionCall2(
numeric_add
,DirectFunctionCall1(int8_numeric, type_oids[i]), sumd);
}
non_nullcnt = nelements - nullcnt;
// elog(NOTICE," nelements:%d nullcnt:%d ,non_nullcnt:%d",nelements,nullcnt,non_nullcnt);
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
case INT2OID:
/* for properly datatype datum value initialize */
sumd = Int64GetDatum(0);
for (i = 0; i < nelements ; i++)
{
if(typnullflag[i])
{
nullcnt++;
// elog(NOTICE,"nullcnt:%d", nullcnt);
continue;
}
sumd = DirectFunctionCall2(int8pl,Int64GetDatum(type_oids[i]),sumd);
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
case INT4OID:
/* for properly datatype datum value initialize */
sumd = Int64GetDatum(0);
for (i = 0; i < nelements ; i++)
{
if(typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(int8pl,Int64GetDatum(type_oids[i]),sumd);
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
case FLOAT4OID:
/* for properly datatype datum value initialize */
sumd = Float4GetDatum(0.0f);
for (i = 0; i < nelements ; i++)
{
if(typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(float4pl,type_oids[i],sumd);
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
case FLOAT8OID:
/* for properly datatype datum value initialize */
sumd = Float8GetDatum(0.0f);
for (i = 0; i < nelements ; i++)
{
if(typnullflag[i])
{
nullcnt++;
continue;
}
sumd = DirectFunctionCall2(float8pl,type_oids[i],sumd);
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
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;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
case NUMERICOID:
/* for properly datatype datum value initialize */
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;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(sumd);
default:
ereport(ERROR, (errmsg("allowed data type int2, int4, int8, float4, float8, numeric")));
}
}