-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_max.c
275 lines (250 loc) · 8.77 KB
/
array_max.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
/*
array_max.c
https://stackoverflow.com/questions/16992339/why-is-postgresql-array-access-so-much-faster-in-c-than-in-pl-pgsql/16996606#16996606
#### 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_max.c
gcc -shared -o /home/jian/Desktop/regress_pgsql/array_max.so /home/jian/Desktop/regress_pgsql/array_max.o
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "utils/numeric.h"
#include "utils/datetime.h"
#include "utils/date.h"
#include "utils/timestamp.h"
#include "utils/timeout.h"
#include "utils/pg_lsn.h"
#include "funcapi.h"
#include "utils/lsyscache.h"
#include "utils/fmgrprotos.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(array_max);
Datum
array_max(PG_FUNCTION_ARGS)
{
ArrayType *arr;
Datum *type_oids;
Oid basetype;
int nelements;
int16 typlen;
bool typbyval;
char typalign;
bool *typnullflag;
int i;
int nullcnt = 0; /* init value for later doing arithmetic */
int non_nullcnt;
Datum temp;
arr = PG_GETARG_ARRAYTYPE_P(0);
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
// This requirement could probably be lifted pretty easily:
if (ARR_NDIM(arr) != 1)
ereport(ERROR, (errmsg("One-dimesional arrays are required")));
//actual data pointer.
type_oids = (Datum *) ARR_DATA_PTR(arr);
basetype = ARR_ELEMTYPE(arr);
if (basetype != INT2OID && basetype != INT4OID &&
basetype != INT8OID && basetype != FLOAT4OID &&
basetype != FLOAT8OID && basetype != NUMERICOID &&
basetype != DATEOID && basetype != TIMEOID &&
basetype != TIMESTAMPOID && basetype != TIMESTAMPTZOID &&
basetype != INTERVALOID && basetype != TIMETZOID &&
basetype != PG_LSNOID
)
ereport(ERROR,
(errmsg("allowed data type int2, int4,int8,float4"
"float8,numeric,date,time,timestamp"
"timestamptz,interval,timetz,pg_lsn;")));
get_typlenbyvalalign(basetype, &typlen, &typbyval, &typalign);
nelements = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
if (nelements == 0)
PG_RETURN_NULL();
// Extract the array contents (as Datum objects).
deconstruct_array(arr,basetype,typlen,typbyval,typalign,&type_oids,&typnullflag,&nelements);
temp = type_oids[0];
if (typnullflag[0])
nullcnt++;
switch (basetype)
{
case INT2OID:
case INT4OID:
case INT8OID:
for (i = 1; i < nelements; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if((bool) DirectFunctionCall2(int8gt,Int64GetDatum(type_oids[i]),temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case FLOAT4OID:
for (i = 0; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(float4gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case FLOAT8OID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(float8gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case NUMERICOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(numeric_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case DATEOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(date_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case TIMEOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(time_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case TIMESTAMPOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(timestamp_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case TIMESTAMPTZOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if ((bool) DirectFunctionCall2(timestamp_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case INTERVALOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(interval_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case TIMETZOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(timetz_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
case PG_LSNOID:
for (i = 1; i < nelements ; i++)
{
if (typnullflag[i])
{
nullcnt++;
continue;
}
if( (bool) DirectFunctionCall2(pg_lsn_gt,type_oids[i],temp))
temp = type_oids[i];
}
non_nullcnt = nelements - nullcnt;
if (non_nullcnt == 0)
PG_RETURN_NULL();
else
PG_RETURN_DATUM(temp);
default:
ereport(ERROR, (errmsg("allowed data type int2, int4, int8, float4, float8, numeric")));
}
}