1- <!-- $PostgreSQL: pgsql/doc/src/sgml/array.sgml,v 1.46 2005/11/04 23:13:59 petere Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/array.sgml,v 1.47 2005/11/17 22:14:50 tgl Exp $ -->
22
33<sect1 id="arrays">
44 <title>Arrays</title>
@@ -110,6 +110,13 @@ CREATE TABLE tictactoe (
110110 three subarrays of integers.
111111 </para>
112112
113+ <para>
114+ To set an element of an array constant to NULL, write <literal>NULL</>
115+ for the element value. (Any upper- or lower-case variant of
116+ <literal>NULL</> will do.) If you want an actual string value
117+ <quote>NULL</>, you must put double quotes around it.
118+ </para>
119+
113120 <para>
114121 (These kinds of array constants are actually only a special case of
115122 the generic type constants discussed in <xref
@@ -121,17 +128,6 @@ CREATE TABLE tictactoe (
121128 <para>
122129 Now we can show some <command>INSERT</command> statements.
123130
124- <programlisting>
125- INSERT INTO sal_emp
126- VALUES ('Bill',
127- '{10000, 10000, 10000, 10000}',
128- '{{"meeting", "lunch"}, {"meeting"}}');
129- ERROR: multidimensional arrays must have array expressions with matching dimensions
130- </programlisting>
131-
132- Note that multidimensional arrays must have matching extents for each
133- dimension. A mismatch causes an error report.
134-
135131<programlisting>
136132INSERT INTO sal_emp
137133 VALUES ('Bill',
@@ -145,15 +141,9 @@ INSERT INTO sal_emp
145141</programlisting>
146142 </para>
147143
148- <para>
149- A limitation of the present array implementation is that individual
150- elements of an array cannot be SQL null values. The entire array
151- can be set to null, but you can't have an array with some elements
152- null and some not. (This is likely to change in the future.)
153- </para>
154-
155144 <para>
156145 The result of the previous two inserts looks like this:
146+
157147<programlisting>
158148SELECT * FROM sal_emp;
159149 name | pay_by_quarter | schedule
@@ -183,6 +173,19 @@ INSERT INTO sal_emp
183173 constructor syntax is discussed in more detail in
184174 <xref linkend="sql-syntax-array-constructors">.
185175 </para>
176+
177+ <para>
178+ Multidimensional arrays must have matching extents for each
179+ dimension. A mismatch causes an error report, for example:
180+
181+ <programlisting>
182+ INSERT INTO sal_emp
183+ VALUES ('Bill',
184+ '{10000, 10000, 10000, 10000}',
185+ '{{"meeting", "lunch"}, {"meeting"}}');
186+ ERROR: multidimensional arrays must have array expressions with matching dimensions
187+ </programlisting>
188+ </para>
186189 </sect2>
187190
188191 <sect2>
@@ -262,14 +265,22 @@ SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';
262265 </para>
263266
264267 <para>
265- Fetching from outside the current bounds of an array yields a
266- SQL null value, not an error. For example, if <literal>schedule</>
268+ An array subscript expression will return null if either the array itself or
269+ any of the subscript expressions are null. Also, null is returned if a
270+ subscript is outside the array bounds (this case does not raise an error).
271+ For example, if <literal>schedule</>
267272 currently has the dimensions <literal>[1:3][1:2]</> then referencing
268273 <literal>schedule[3][3]</> yields NULL. Similarly, an array reference
269274 with the wrong number of subscripts yields a null rather than an error.
270- Fetching an array slice that
271- is completely outside the current bounds likewise yields a null array;
272- but if the requested slice partially overlaps the array bounds, then it
275+ </para>
276+
277+ <para>
278+ An array slice expression likewise yields null if the array itself or
279+ any of the subscript expressions are null. However, in other corner
280+ cases such as selecting an array slice that
281+ is completely outside the current array bounds, a slice expression
282+ yields an empty (zero-dimensional) array instead of null.
283+ If the requested slice partially overlaps the array bounds, then it
273284 is silently reduced to just the overlapping region.
274285 </para>
275286
@@ -349,7 +360,7 @@ UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
349360 </para>
350361
351362 <para>
352- Array slice assignment allows creation of arrays that do not use one-based
363+ Subscripted assignment allows creation of arrays that do not use one-based
353364 subscripts. For example one might assign to <literal>myarray[-2:7]</> to
354365 create an array with subscript values running from -2 to 7.
355366 </para>
@@ -442,7 +453,7 @@ SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
442453 arrays, but <function>array_cat</function> supports multidimensional arrays.
443454
444455 Note that the concatenation operator discussed above is preferred over
445- direct use of these functions. In fact, the functionsare primarily for use
456+ direct use of these functions. In fact, the functionsexist primarily for use
446457 in implementing the concatenation operator. However, they may be directly
447458 useful in the creation of user-defined aggregates. Some examples:
448459
@@ -544,8 +555,9 @@ SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);
544555
545556 <para>
546557 The array output routine will put double quotes around element values
547- if they are empty strings or contain curly braces, delimiter characters,
548- double quotes, backslashes, or white space. Double quotes and backslashes
558+ if they are empty strings, contain curly braces, delimiter characters,
559+ double quotes, backslashes, or white space, or match the word
560+ <literal>NULL</>. Double quotes and backslashes
549561 embedded in element values will be backslash-escaped. For numeric
550562 data types it is safe to assume that double quotes will never appear, but
551563 for textual data types one should be prepared to cope with either presence
@@ -555,35 +567,15 @@ SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);
555567
556568 <para>
557569 By default, the lower bound index value of an array's dimensions is
558- set to one.If any of an array's dimensions has a lowerbound index not
559- equal to one, an additional decoration that indicates the actual
560- arraydimensions will precede the array structure decoration .
570+ set to one. To represent arrays with other lowerbounds, the array
571+ subscript ranges can be specified explicitly before writing the
572+ arraycontents .
561573 This decoration consists of square brackets (<literal>[]</>)
562574 around each array dimension's lower and upper bounds, with
563575 a colon (<literal>:</>) delimiter character in between. The
564576 array dimension decoration is followed by an equal sign (<literal>=</>).
565577 For example:
566578<programlisting>
567- SELECT 1 || ARRAY[2,3] AS array;
568-
569- array
570- ---------------
571- [0:2]={1,2,3}
572- (1 row)
573-
574- SELECT ARRAY[1,2] || ARRAY[[3,4]] AS array;
575-
576- array
577- --------------------------
578- [0:1][1:2]={{1,2},{3,4}}
579- (1 row)
580- </programlisting>
581- </para>
582-
583- <para>
584- This syntax can also be used to specify non-default array subscripts
585- in an array literal. For example:
586- <programlisting>
587579SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
588580 FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1) AS ss;
589581
@@ -592,6 +584,18 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
592584 1 | 6
593585(1 row)
594586</programlisting>
587+ The array output routine will include explicit dimensions in its result
588+ only when there are one or more lower bounds different from one.
589+ </para>
590+
591+ <para>
592+ If the value written for an element is <literal>NULL</> (in any case
593+ variant), the element is taken to be NULL. The presence of any quotes
594+ or backslashes disables this and allows the literal string value
595+ <quote>NULL</> to be entered. Also, for backwards compatibility with
596+ pre-8.2 versions of <productname>PostgreSQL</>, the <xref
597+ linkend="guc-array-nulls"> configuration parameter may be turned
598+ <literal>off</> to suppress recognition of <literal>NULL</> as a NULL.
595599 </para>
596600
597601 <para>
@@ -600,7 +604,9 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
600604 if the element value would otherwise confuse the array-value parser.
601605 For example, elements containing curly braces, commas (or whatever the
602606 delimiter character is), double quotes, backslashes, or leading or trailing
603- whitespace must be double-quoted. To put a double quote or backslash in a
607+ whitespace must be double-quoted. Empty strings and strings matching the
608+ word <literal>NULL</> must be quoted, too. To put a double quote or
609+ backslash in a
604610 quoted array element value, precede it with a backslash. Alternatively, you
605611 can use backslash-escaping to protect all data characters that would
606612 otherwise be taken as array syntax.