SUM两个float4 []数组而不是int []数组(代码中的更改)

时间:2014-05-22 02:58:27

标签: c postgresql

我想知道,以下代码需要对两个float4[]数组而不是两个int[]数组求和进行哪些更改?

我尝试了一些事情,但没有成功

#include <postgres.h>
#include <fmgr.h>
#include <utils/array.h>

PG_MODULE_MAGIC;

Datum int4_array_add(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(int4_array_add);

/**
 * Returns the sum of two int arrays.
 * by Matt Solnit on Stack Overflow - http://stackoverflow.com/questions/16992339
 */
Datum
int4_array_add(PG_FUNCTION_ARGS)
{
  // The formal PostgreSQL array objects:
  ArrayType *array1, *array2;

  // The array element types (should always be INT4OID):
  Oid arrayElementType1, arrayElementType2;

  // The array element type widths (should always be 4):
  int16 arrayElementTypeWidth1, arrayElementTypeWidth2;

  // The array element type "is passed by value" flags (not used, should always be true):
  bool arrayElementTypeByValue1, arrayElementTypeByValue2;

  // The array element type alignment codes (not used):
  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;

  // 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 = 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);
}

我试过的是施放操作:

sumContent[i] = arrayContent1[i] + arrayContent2[i];

为:

sumContent[i] =  Float4GetDatum(arrayContent1[i]) +  Float4GetDatum(arrayContent2[i]);

和:

sumContent[i] =  DatumGetFloat4(arrayContent1[i]) +  DatumGetFloat4(arrayContent2[i]);

但它只给我0和奇怪的负数作为我的c函数的输出:

create or replace function sumar(float4[],float4[])
returns float4[]
as 'example.so', 'sumar'
Language C strict;

1 个答案:

答案 0 :(得分:0)

这一行

sumContent[i] =  Float4GetDatum(arrayContent1[i]) +  Float4GetDatum(arrayContent2[i]);

肯定是胡说八道 - 你不能通过这个陈述来总结两个基准,你应该使用反向宏:

sumContent[i] = Float4GetDatum( DatumGetFloat4(arrayContent1[i]) +  DatumGetFloat4(arrayContent2[i]));

下一个建议 - 使用elog(NOTICE,&#34; sometext&#34;)对函数开始确定,所以SQL真的使用了你的函数。