4.2.12. Array Constructors

4.2.12. Array Constructors

4.2.12.数组构造函数

An array constructor is an expression that builds an array value using values for its member elements. A simple array constructor consists of the key word ARRAY, a left square bracket [, a list of expressions (separated by commas) for the array element values, and finally a right square bracket ]. For example:

数组构造函数是一个表达式,该表达式使用其成员元素的值构建数组值。一个简单的数组构造函数由关键字ARRAY,左方括号[,用于数组元素值的表达式列表(用逗号分隔)以及在最后的一个方括号]组成。例如:

SELECT ARRAY[1,2,3+4];

array

---------

{1,2,7}

(1 row)

By default, the array element type is the common type of the member expressions, determined using the same rules as for UNION or CASE constructs (see Section 10.5). You can override this by explicitly casting the array constructor to the desired type, for example:

默认情况下,数组元素的类型为组员表达式的通用类型,与UNION或CASE函数类似(参见10.5节)。可以通过显式的转换数组函数为需要的数据类型,例如:

SELECT ARRAY[1,2,22.7]::integer[];

array

----------

{1,2,23}

(1 row)

This has the same effect as casting each expression to the array element type individually. For more on casting, see Section 4.2.9.

这与将每个表达式分别转换为数组元素类型具有相同的效果。有关类型转换的更多信息,请参见第4.2.9节

Multidimensional array values can be built by nesting array constructors. In the inner constructors, the key word ARRAY can be omitted. For example, these produce the same result:

多维数组可以通过嵌套数组函数构建。嵌套里面的关键词ARRAY可以省略。例如,以下返回相同的结果:

SELECT ARRAY[ARRAY[1,2], ARRAY[3,4]];

array

---------------

{{1,2},{3,4}}

(1 row)

SELECT ARRAY[[1,2],[3,4]];

array

---------------

{{1,2},{3,4}}

(1 row)

Since multidimensional arrays must be rectangular, inner constructors at the same level must produce sub-arrays of identical dimensions. Any cast applied to the outer ARRAY constructor propagates automatically to all the inner constructors.

由于多维数组必须为矩形,因此处于同一级别的内部构造函数必须具有相同维度的子数组。应用于外部ARRAY构造函数的任何强制转换都会自动应用到所有内部构造函数。

Multidimensional array constructor elements can be anything yielding an array of the proper kind, not only a sub-ARRAY construct. For example:

多维数组构造函数元素可以是产生适当数组类型的任何东西,而不仅仅是子数组构造。 例如:

CREATE TABLE arr(f1 int[], f2 int[]);

INSERT INTO arr VALUES (ARRAY[[1,2],[3,4]], ARRAY[[5,6],[7,8]]);

SELECT ARRAY[f1, f2, '{{9,10},{11,12}}'::int[]] FROM arr;

array

------------------------------------------------

{{{1,2},{3,4}},{{5,6},{7,8}},{{9,10},{11,12}}}

(1 row)

You can construct an empty array, but since it's impossible to have an array with no type, you must explicitly cast your empty array to the desired type. For example:

可以构造一个空的数组,但因为数组不能没有类型,所以必须显式的将空数组转换为目标数据类型。例如:

SELECT ARRAY[]::integer[];

array

-------

{}

(1 row)

It is also possible to construct an array from the results of a subquery. In this form, the array constructor is written with the key word ARRAY followed by a parenthesized (not bracketed) subquery. For example:

也可以将子查询结果构造为数组。 在这种形式中,数组构造函数是用关键字ARRAY编写的,其后是带括号的(array未括在括号中)子查询。 例如:

SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');

array

-----------------------------------------------------------------------

{2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31,2412,2413}

(1 row)

SELECT ARRAY(SELECT ARRAY[i, i*2] FROM generate_series(1,5) AS

a(i));

array

----------------------------------

{{1,2},{2,4},{3,6},{4,8},{5,10}}

(1 row)

The subquery must return a single column. If the subquery's output column is of a non-array type, the resulting one-dimensional array will have an element for each row in the subquery result, with an element type matching that of the subquery's output column. If the subquery's output column is of an array type, the result will be an array of the same type but one higher dimension; in this case all the subquery rows must yield arrays of identical dimensionality, else the result would not be rectangular.

子查询必须仅返回一行。如果子查询的输出列是非数组类型,则生成的一维数组将为子查询结果中的每一行都包含一个元素,且该元素类型与子查询的输出列的类型匹配。 如果子查询的输出列是数组类型,则结果将是相同类型但维数较高的数组; 在这种情况下,所有子查询行必须为相同维度的数组,否则结果将不是矩形。

The subscripts of an array value built with ARRAY always begin with one. For more information about arrays, see Section 8.15.

用ARRAY构建的数组值的下标始终以1开头。 有关数组的更多信息,请参见第8.15节

发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104302868