|
41 | 41 | #include "storage/spin.h"
|
42 | 42 | #include "utils/builtins.h"
|
43 | 43 | #include "utils/geo_decls.h"
|
| 44 | +#include "utils/lsyscache.h" |
44 | 45 | #include "utils/memutils.h"
|
45 | 46 | #include "utils/rel.h"
|
46 | 47 | #include "utils/typcache.h"
|
@@ -1216,3 +1217,47 @@ binary_coercible(PG_FUNCTION_ARGS)
|
1216 | 1217 |
|
1217 | 1218 | PG_RETURN_BOOL(IsBinaryCoercible(srctype, targettype));
|
1218 | 1219 | }
|
| 1220 | + |
| 1221 | +/* |
| 1222 | + * Return the column offset of the last data in the given array of |
| 1223 | + * data types. The input data types must be fixed-length data types. |
| 1224 | + */ |
| 1225 | +PG_FUNCTION_INFO_V1(get_column_offset); |
| 1226 | +Datum |
| 1227 | +get_column_offset(PG_FUNCTION_ARGS) |
| 1228 | +{ |
| 1229 | + ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0); |
| 1230 | + Oid *type_oids; |
| 1231 | + int ntypes; |
| 1232 | + int column_offset = 0; |
| 1233 | + |
| 1234 | + if (ARR_HASNULL(ta) && array_contains_nulls(ta)) |
| 1235 | + elog(ERROR, "argument must not contain nulls"); |
| 1236 | + |
| 1237 | + if (ARR_NDIM(ta) > 1) |
| 1238 | + elog(ERROR, "argument must be empty or one-dimensional array"); |
| 1239 | + |
| 1240 | + type_oids = (Oid *) ARR_DATA_PTR(ta); |
| 1241 | + ntypes = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta)); |
| 1242 | + for (int i = 0; i < ntypes; i++) |
| 1243 | + { |
| 1244 | + Oid typeoid = type_oids[i]; |
| 1245 | + int16 typlen; |
| 1246 | + bool typbyval; |
| 1247 | + char typalign; |
| 1248 | + |
| 1249 | + get_typlenbyvalalign(typeoid, &typlen, &typbyval, &typalign); |
| 1250 | + |
| 1251 | + /* the data type must be fixed-length */ |
| 1252 | + if (!(typbyval || (typlen > 0))) |
| 1253 | + elog(ERROR, "type %u is not fixed-length data type", typeoid); |
| 1254 | + |
| 1255 | + column_offset = att_align_nominal(column_offset, typalign); |
| 1256 | + |
| 1257 | + /* not include the last type size */ |
| 1258 | + if (i != (ntypes - 1)) |
| 1259 | + column_offset += typlen; |
| 1260 | + } |
| 1261 | + |
| 1262 | + PG_RETURN_INT32(column_offset); |
| 1263 | +} |
0 commit comments