From 6b0706ac33ab5da815980c642a9cde9a4cd86b6b Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 20 Mar 2008 21:42:48 +0000 Subject: Arrange for an explicit cast applied to an ARRAY[] constructor to be applied directly to all the member expressions, instead of the previous implementation where the ARRAY[] constructor would infer a common element type and then we'd coerce the finished array after the fact. This has a number of benefits, one being that we can allow an empty ARRAY[] construct so long as its element type is specified by such a cast. Brendan Jurd, minor fixes by me. --- doc/src/sgml/syntax.sgml | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml index 8761b6e1da7..e677d80d5ce 100644 --- a/doc/src/sgml/syntax.sgml +++ b/doc/src/sgml/syntax.sgml @@ -1,4 +1,4 @@ - + SQL Syntax @@ -1305,7 +1305,7 @@ sqrt(2) where aggregate_name is a previously defined aggregate (possibly qualified with a schema name), and - expression is + expression is any value expression that does not itself contain an aggregate expression. @@ -1335,7 +1335,7 @@ sqrt(2) The predefined aggregate functions are described in . Other aggregate functions can be added - by the user. + by the user. @@ -1495,9 +1495,9 @@ SELECT name, (SELECT max(pop) FROM cities WHERE cities.state = states.name) An array constructor is an expression that builds an array value from values for its member elements. A simple array - constructor + constructor consists of the key word ARRAY, a left square bracket - [, one or more expressions (separated by commas) for the + [, a list of expressions (separated by commas) for the array element values, and finally a right square bracket ]. For example: @@ -1507,9 +1507,22 @@ SELECT ARRAY[1,2,3+4]; {1,2,7} (1 row) - The array element type is the common type of the member expressions, + 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 ). + CASE constructs (see ). + You can override this by explicitly casting the array constructor to the + desired type, for example: + +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 . @@ -1534,6 +1547,8 @@ SELECT ARRAY[[1,2],[3,4]]; 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. @@ -1553,6 +1568,19 @@ SELECT ARRAY[f1, f2, '{{9,10},{11,12}}'::int[]] FROM arr; + + 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 -- cgit v1.2.3