My question is about C functions in PostgreSQL, where I'm trying to receive 2 float4[]
arrays as parameters and return a float4[]
array as the sum of array1 + array2 (columns in tables are set as float4[] as well), but can't achieve it:
To create the function:
CREATE OR REPLACE FUNCTION sum(float4[], float4[]) RETURNS float4[]
AS 'example.so', 'sum'
LANGUAGE C STRICT;
-- 'example.so' is already on /usr/lib/postgresql/9.1/lib
And the C code:
Datum sumar(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(sumar);
Datum sumar(PG_FUNCTION_ARGS)
{
ArrayType *array1, *array2;
// The array element types:
Oid arrayElementType1, arrayElementType2;
// The array element type widths:
int16 arrayElementTypeWidth1, arrayElementTypeWidth2;
// The array element type:
bool arrayElementTypeByValue1, arrayElementTypeByValue2;
// The array element type alignment codes:
char arrayElementTypeAlignmentCode1, arrayElementTypeAlignmentCode2;
// The array contents, as PostgreSQL "datum" objects:
Datum *arrayContent1, *arrayContent2;
// List of "is null" flags for the array contents:
bool *arrayNullFlags1, *arrayNullFlags2;
// The size of each array:
int arrayLength1, arrayLength2;
Datum *sumContent;
int i;
ArrayType* resultArray;
// Extract the PostgreSQL arrays from the parameters passed to this function call.
array1 = PG_GETARG_ARRAYTYPE_P(0);
array2 = PG_GETARG_ARRAYTYPE_P(1);
// Determine the array element types.
arrayElementType1 = ARR_ELEMTYPE(array1);
get_typlenbyvalalign(arrayElementType1, &arrayElementTypeWidth1, &arrayElementTypeByValue1, &arrayElementTypeAlignmentCode1);
arrayElementType2 = ARR_ELEMTYPE(array2);
get_typlenbyvalalign(arrayElementType2, &arrayElementTypeWidth2, &arrayElementTypeByValue2, &arrayElementTypeAlignmentCode2);
// Extract the array contents (as Datum objects).
deconstruct_array(array1, arrayElementType1, arrayElementTypeWidth1, arrayElementTypeByValue1, arrayElementTypeAlignmentCode1, &arrayContent1, &arrayNullFlags1, &arrayLength1);
deconstruct_array(array2,arrayElementType2, arrayElementTypeWidth2, arrayElementTypeByValue2, arrayElementTypeAlignmentCode2, &arrayContent2, &arrayNullFlags2, &arrayLength2);
//Create a new array of sum results (as Datum objects).
sumContent = (Datum *) palloc(sizeof(Datum) * arrayLength1);
// Generate the sums.
for (i = 0; i < arrayLength1; i++)
{
sumContent[i] = arrayContent1[i] + arrayContent2[i];
}
// Wrap the sums in a new PostgreSQL array object.
resultArray = construct_array(sumContent, arrayLength1, arrayElementType1, arrayElementTypeWidth1, arrayElementTypeByValue1, arrayElementTypeAlignmentCode1);
// Return the final PostgreSQL array object.
PG_RETURN_ARRAYTYPE_P(resultArray);
}
When it returns the array at the ends, it shows me just negative values (the sum is wrong), but when I pass 2 int arrays it works fine. Also I have tried casting the values before the sum, just like:
sumContent[i] = Float4GetDatum(arrayContent1[i]) + Float4GetDatum(arrayContent2[i];)
and
sumContent[i] = DatumGetFloat4arrayContent1[i]) + DatumGetFloat4(arrayContent2[i]);
but it keeps giving me strange values.