-
Notifications
You must be signed in to change notification settings - Fork 7
/
array_to_min_max.c
186 lines (161 loc) · 5.92 KB
/
array_to_min_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
Datum array_to_min_max(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(array_to_min_max);
/**
* Returns a min and max from an (unsorted) array of numbers.
* by Paul A. Jungwirth
*/
Datum
array_to_min_max(PG_FUNCTION_ARGS)
{
// Our arguments:
ArrayType *vals;
// The array element type:
Oid valsType;
// The array element type widths for our input array:
int16 valsTypeWidth;
int16 retTypeWidth;
// The array element type "is passed by value" flags:
bool valsTypeByValue;
bool retTypeByValue;
// The array element type alignment codes:
char valsTypeAlignmentCode;
char retTypeAlignmentCode;
// The array contents, as PostgreSQL "Datum" objects:
Datum *valsContent;
Datum retContent[2];
bool retNulls[2] = {true, true};
// List of "is null" flags for the array contents:
bool *valsNullFlags;
// The size of the input array:
int valsLength;
// The output array:
ArrayType* retArray;
bool resultIsNull = true;
int i;
pgnum minV, maxV;
int dims[1];
int lbs[1]; // Lower Bounds of each dimension
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
vals = PG_GETARG_ARRAYTYPE_P(0);
if (ARR_NDIM(vals) == 0) {
PG_RETURN_NULL();
}
if (ARR_NDIM(vals) > 1) {
ereport(ERROR, (errmsg("One-dimesional arrays are required")));
}
// Determine the array element types.
valsType = ARR_ELEMTYPE(vals);
if (valsType != INT2OID &&
valsType != INT4OID &&
valsType != INT8OID &&
valsType != FLOAT4OID &&
valsType != FLOAT8OID) {
ereport(ERROR, (errmsg("Minmax subject must be SMALLINT, INTEGER, BIGINT, REAL, or DOUBLE PRECISION values")));
}
valsLength = (ARR_DIMS(vals))[0];
get_typlenbyvalalign(valsType, &valsTypeWidth, &valsTypeByValue, &valsTypeAlignmentCode);
// Extract the array contents (as Datum objects).
deconstruct_array(vals, valsType, valsTypeWidth, valsTypeByValue, valsTypeAlignmentCode,
&valsContent, &valsNullFlags, &valsLength);
if (valsLength == 0) PG_RETURN_NULL();
// Compute the max.
switch (valsType) {
case INT2OID:
for (i = 0; i < valsLength; i++) {
if (valsNullFlags[i]) {
continue;
} else if (resultIsNull) {
minV.i16 = DatumGetInt16(valsContent[i]);
maxV.i16 = DatumGetInt16(valsContent[i]);
resultIsNull = false;
} else {
if (DatumGetInt16(valsContent[i]) < minV.i16) minV.i16 = DatumGetInt16(valsContent[i]);
if (DatumGetInt16(valsContent[i]) > maxV.i16) maxV.i16 = DatumGetInt16(valsContent[i]);
}
}
retContent[0] = Int16GetDatum(minV.i16);
retContent[1] = Int16GetDatum(maxV.i16);
get_typlenbyvalalign(INT2OID, &retTypeWidth, &retTypeByValue, &retTypeAlignmentCode);
break;
case INT4OID:
for (i = 0; i < valsLength; i++) {
if (valsNullFlags[i]) {
continue;
} else if (resultIsNull) {
minV.i32 = DatumGetInt32(valsContent[i]);
maxV.i32 = DatumGetInt32(valsContent[i]);
resultIsNull = false;
} else {
if (DatumGetInt32(valsContent[i]) < minV.i32) minV.i32 = DatumGetInt32(valsContent[i]);
if (DatumGetInt32(valsContent[i]) > maxV.i32) maxV.i32 = DatumGetInt32(valsContent[i]);
}
}
retContent[0] = Int32GetDatum(minV.i32);
retContent[1] = Int32GetDatum(maxV.i32);
get_typlenbyvalalign(INT4OID, &retTypeWidth, &retTypeByValue, &retTypeAlignmentCode);
break;
case INT8OID:
for (i = 0; i < valsLength; i++) {
if (valsNullFlags[i]) {
continue;
} else if (resultIsNull) {
minV.i64 = DatumGetInt64(valsContent[i]);
maxV.i64 = DatumGetInt64(valsContent[i]);
resultIsNull = false;
} else {
if (DatumGetInt64(valsContent[i]) < minV.i64) minV.i64 = DatumGetInt64(valsContent[i]);
if (DatumGetInt64(valsContent[i]) > maxV.i64) maxV.i64 = DatumGetInt64(valsContent[i]);
}
}
retContent[0] = Int64GetDatum(minV.i64);
retContent[1] = Int64GetDatum(maxV.i64);
get_typlenbyvalalign(INT8OID, &retTypeWidth, &retTypeByValue, &retTypeAlignmentCode);
break;
case FLOAT4OID:
for (i = 0; i < valsLength; i++) {
if (valsNullFlags[i]) {
continue;
} else if (resultIsNull) {
minV.f4 = DatumGetFloat4(valsContent[i]);
maxV.f4 = DatumGetFloat4(valsContent[i]);
resultIsNull = false;
} else {
if (DatumGetFloat4(valsContent[i]) < minV.f4) minV.f4 = DatumGetFloat4(valsContent[i]);
if (DatumGetFloat4(valsContent[i]) > maxV.f4) maxV.f4 = DatumGetFloat4(valsContent[i]);
}
}
retContent[0] = Float4GetDatum(minV.f4);
retContent[1] = Float4GetDatum(maxV.f4);
get_typlenbyvalalign(FLOAT4OID, &retTypeWidth, &retTypeByValue, &retTypeAlignmentCode);
break;
case FLOAT8OID:
for (i = 0; i < valsLength; i++) {
if (valsNullFlags[i]) {
continue;
} else if (resultIsNull) {
minV.f8 = DatumGetFloat8(valsContent[i]);
maxV.f8 = DatumGetFloat8(valsContent[i]);
resultIsNull = false;
} else {
if (DatumGetFloat8(valsContent[i]) < minV.f8) minV.f8 = DatumGetFloat8(valsContent[i]);
if (DatumGetFloat8(valsContent[i]) > maxV.f8) maxV.f8 = DatumGetFloat8(valsContent[i]);
}
}
retContent[0] = Float8GetDatum(minV.f8);
retContent[1] = Float8GetDatum(maxV.f8);
get_typlenbyvalalign(FLOAT8OID, &retTypeWidth, &retTypeByValue, &retTypeAlignmentCode);
break;
default:
ereport(ERROR, (errmsg("Minmax subject must be SMALLINT, INTEGER, BIGINT, REAL, or DOUBLE PRECISION values")));
}
lbs[0] = 1;
dims[0] = 2;
if (!resultIsNull) {
retNulls[0] = false;
retNulls[1] = false;
}
retArray = construct_md_array(retContent, retNulls, 1, dims, lbs, valsType, retTypeWidth, retTypeByValue, retTypeAlignmentCode);
PG_RETURN_ARRAYTYPE_P(retArray);
}