在功能初始阵列聚集多维数组 [英] Initial array in function to aggregate multi-dimensional array

查看:159
本文介绍了在功能初始阵列聚集多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表整数数组。

我想创建一个聚合函数,将与所有行返回一个2维数组在一起。然后,它被传递到 PLR 做一些关于它的数学。

I want to create an aggregate function that will return a 2-dimensional array with all the rows together. It then gets passed to plr to do some maths on it.

我有:

CREATE OR REPLACE
FUNCTION arrayappend(left int[][], right int[]) 
RETURNS int[] AS 
$BODY$
   SELECT $1 || $2 ;
$BODY$
LANGUAGE SQL;

CREATE AGGREGATE array_sum2 (int[])  (
    SFUNC     = arrayappend,
    STYPE     = int[][],
    INITCOND  = '{}'
);

但是,返回类型是 INT [] ,而不是 INT [] []

如何初始化骨料与整数空二维数组?

How can I initialize the aggregate with an empty two dimensional array of integer?

推荐答案

的Postgres 9.5 船舶聚集函数的附加变量 ARRAY_AGG()可聚合阵列成一个高维的阵列的阵列。参见:

Postgres 9.5 ships an additional variant of the aggregate function array_agg() that can aggregate arrays into an array of array of one higher dimension. See:

  • How to sort two dimensional int array in PostgreSQL?

随着<一个href=\"http://www.postgresql.org/docs/current/interactive/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC\"相对=nofollow>态类型 anyarray的 它适用于各种阵列,而不仅仅是整数

CREATE AGGREGATE array_agg_mult (anyarray)  (
    SFUNC     = array_cat
   ,STYPE     = anyarray
   ,INITCOND  = '{}'
);

由于@Lukas规定,自定义函数 arrayappend()是没有必要的。在 内置array_cat( ) 做这项工作。然而,这并不能解释的为什么的你的榜样失败,而一个在@Lukas的回答工作。有关不同的是,@Lukas嵌套阵列到另一个阵列层数组[D.A]

As @Lukas provided, the custom function arrayappend() is not needed. The built in array_cat() does the job. However, that doesn't explain why your example fails, while the one in @Lukas' answer works. The relevant difference is that @Lukas nested the array into another array layer with array[d.a].

您在不正确的假设,你可以声明一个类旅行 INT [] [] 。但是你不能: INT [] [] 同类型 INT [] PostgreSQL的类型系统。该在手动对数组类型本章解释:

You trip over the incorrect assumption that you could declare a type int[][]. But you cannot: int[][] is the same type as int[] for the PostgreSQL type system. The chapter on array types in the manual explains:

目前的实现并不强制的申报数量
  尺寸无论是。特定元素类型的数组都
  认为是无论大小或数量的相同的类型,
  尺寸。因此,在宣布维数组大小和数目
  CREATE TABLE只是简单的文档;它不影响运行时
  行为。

The current implementation does not enforce the declared number of dimensions either. Arrays of a particular element type are all considered to be of the same type, regardless of size or number of dimensions. So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; it does not affect run-time behavior.

这是 N 维整型数组实际上是 N-1 PostgreSQL中的整数维数组的数组。你不能告诉,从只定义了基本元素的类型。你要问 array_dims() 得到具体细节。

An n-dimensional integer array effectively is an array of n-1-dimensional arrays of integer in PostgreSQL. You can't tell that from the type which only defines the base element. You have to ask array_dims() to get the specifics.

要证明:

SELECT array_agg_mult(arr)               AS arr1  --> 1-dimensional array
      ,array_agg_mult(ARRAY[arr])        AS arr2  --> 2-dimensional array
      ,array_agg_mult(ARRAY[ARRAY[arr]]) AS arr3  --> 3-dimensional array
    -- etc.
FROM  (
   VALUES
      ('{1,2,3}'::int[]) -- 1-dimensional array
     ,('{4,5,6}')
     ,('{7,8,9}'))
   ) x(arr);

或者

SELECT array_agg_mult(arr) AS arr1      --> 2-dimensional array
FROM  (
   VALUES
      ('{{1,2,3}}'::int[]) -- 2-dimensional array
     ,('{{4,5,6}}')
     ,('{{7,8,9}}')
   ) x(arr);

所有的结果列是相同的键入的的: INT []

All resulting columns are of the same type: int[].

这篇关于在功能初始阵列聚集多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆