c - AGGREGATE 中的最终函数不会在 PostgreSQL 中运行

标签 c postgresql aggregate-functions

我正在使用 C 语言创建 PostgreSQL 的扩展。

我实现了一个聚合函数,以返回一个有序的时间戳数组。

time_to_array 函数负责将表列的所有时间戳元素添加到数组中。

time_to_array_final 函数接收时间戳数组并排序。

以下是sql中的定义:

CREATE OR REPLACE FUNCTION time_to_array(timestamp[], timestamp)
    RETURNS timestamp[]
    AS 'MODULE_PATHNAME','time_to_array'
    LANGUAGE C IMMUTABLE;

CREATE OR REPLACE FUNCTION time_to_array_final(timestamp[])
    RETURNS timestamp[]
    AS 'MODULE_PATHNAME', 'time_to_array_final'
    LANGUAGE C IMMUTABLE;

CREATE AGGREGATE array_time_agg(timestamp)
(
  SFUNC = time_to_array,
  STYPE = timestamp[],
  FINALFUNC = time_to_array_final
);

SELECT array_time_agg(column) FROM table;

elog() : 注意:time_to_array CALL(5 次)

结果: array_time_agg | {"2016-06-01 00:00:00","2016-06-02 00:00:00","2016-06-05 00:00:00","2016-06-03 00:00: 00","2016-07-03 00:00:00"}

我目前正在使用 elog 来分析代码中发生的事情。

在执行select的时候可以看到每次都正确调用了time_to_array函数。

但是 time_to_array_final 函数没有。 elog()在函数的开头,说明没有调用same:

PG_FUNCTION_INFO_V1(time_to_array_final);

Datum
time_to_array_final(PG_FUNCTION_ARGS)
{
  elog(NOTICE, "time_to_array_final");

  ArrayType *array_time;
  ArrayType *array_time_result;

  /* variables for "deconstructed" array*/
  Datum *datums_time;
  bool *nulls_time;
  int count_time;
  int16 typlen;
  bool typbyval;
  char typalign;

  array_time = PG_GETARG_ARRAYTYPE_P(0);

  /*values found in table pg_type*/
  get_typlenbyvalalign(TIMESTAMPOID, &typlen, &typbyval, &typalign);

  deconstruct_array(array_time, TIMESTAMPOID, typlen, typbyval, typalign , &datums_time, &nulls_time, &count_time);

  quick_sort(datums_time, 0, count_time - 1);

  array_time_result = construct_array(datums_time, 1, TIMESTAMPOID, typlen, typbyval, typalign);

  PG_RETURN_ARRAYTYPE_P(array_time_result);
}

我是否传递了一些错误的参数?

我还需要其他任何东西来创建聚合函数吗?我读到 initcond 不是必需的,并且该函数假定它为 null。

提前致谢

最佳答案

你的 DDL 语句是正确的,你的 C 函数中一定有东西。视频:

create or replace function time_to_array(timestamp[], timestamp)
returns timestamp[] language sql as $$
    select $1 || $2
$$;

create or replace function time_to_array_final(timestamp[])
returns timestamp[] language sql as $$
    select array_agg(elem order by elem)
    from unnest($1) as elem;
$$;

create aggregate array_time_agg(timestamp)
(
  sfunc = time_to_array,
  stype = timestamp[],
  finalfunc = time_to_array_final
);

with my_table(tstamp) as (
values
    ('2017-08-20'::timestamp),
    ('2017-08-19'),
    ('2017-08-22'),
    ('2017-08-21')
)
select array_time_agg(tstamp) 
from my_table;

                                      array_time_agg                                       
-------------------------------------------------------------------------------------------
 {"2017-08-19 00:00:00","2017-08-20 00:00:00","2017-08-21 00:00:00","2017-08-22 00:00:00"}
(1 row) 

我编译了你的函数(没有 quicksort()),它似乎有一个小错误:

array_time_result = construct_array(datums_time, count_time, TIMESTAMPOID, typlen, typbyval, typalign);
//                                               | was 1

无论如何,它在我的聚合中作为最终函数运行良好(虽然没有排序)。

关于c - AGGREGATE 中的最终函数不会在 PostgreSQL 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868532/

相关文章:

C编译器优化(gcc) : Automatically Inline a non-static function vs static function

c - MPI_Sendrecv_replace 函数非常耗时。

database - 我可以对 PostgreSQL HStore 值使用聚合函数吗?

sql - 与行上的滞后/超前合并?

postgresql - 根据列中的值将行分为两种类型

CS50 PSET4 指针

c - 将 .csv 文件读入 C 中的数组

ruby-on-rails - 在 Rails 4 应用程序中使用 MySQL 和 Postgres

sql - 如何从按索引存储数组元素的规范化表中获取数组?

sql - 根据存在条件计算行数