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 $ -->
2
2
3
3
<sect1 id="arrays">
4
4
<title>Arrays</title>
@@ -110,6 +110,13 @@ CREATE TABLE tictactoe (
110
110
three subarrays of integers.
111
111
</para>
112
112
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
+
113
120
<para>
114
121
(These kinds of array constants are actually only a special case of
115
122
the generic type constants discussed in <xref
@@ -121,17 +128,6 @@ CREATE TABLE tictactoe (
121
128
<para>
122
129
Now we can show some <command>INSERT</command> statements.
123
130
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
-
135
131
<programlisting>
136
132
INSERT INTO sal_emp
137
133
VALUES ('Bill',
@@ -145,15 +141,9 @@ INSERT INTO sal_emp
145
141
</programlisting>
146
142
</para>
147
143
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
-
155
144
<para>
156
145
The result of the previous two inserts looks like this:
146
+
157
147
<programlisting>
158
148
SELECT * FROM sal_emp;
159
149
name | pay_by_quarter | schedule
@@ -183,6 +173,19 @@ INSERT INTO sal_emp
183
173
constructor syntax is discussed in more detail in
184
174
<xref linkend="sql-syntax-array-constructors">.
185
175
</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>
186
189
</sect2>
187
190
188
191
<sect2>
@@ -262,14 +265,22 @@ SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';
262
265
</para>
263
266
264
267
<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</>
267
272
currently has the dimensions <literal>[1:3][1:2]</> then referencing
268
273
<literal>schedule[3][3]</> yields NULL. Similarly, an array reference
269
274
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
273
284
is silently reduced to just the overlapping region.
274
285
</para>
275
286
@@ -349,7 +360,7 @@ UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
349
360
</para>
350
361
351
362
<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
353
364
subscripts. For example one might assign to <literal>myarray[-2:7]</> to
354
365
create an array with subscript values running from -2 to 7.
355
366
</para>
@@ -442,7 +453,7 @@ SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
442
453
arrays, but <function>array_cat</function> supports multidimensional arrays.
443
454
444
455
Note that the concatenation operator discussed above is preferred over
445
- direct use of these functions. In fact, the functions are primarily for use
456
+ direct use of these functions. In fact, the functions exist primarily for use
446
457
in implementing the concatenation operator. However, they may be directly
447
458
useful in the creation of user-defined aggregates. Some examples:
448
459
@@ -544,8 +555,9 @@ SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);
544
555
545
556
<para>
546
557
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
549
561
embedded in element values will be backslash-escaped. For numeric
550
562
data types it is safe to assume that double quotes will never appear, but
551
563
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);
555
567
556
568
<para>
557
569
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 lower bound index not
559
- equal to one, an additional decoration that indicates the actual
560
- array dimensions will precede the array structure decoration .
570
+ set to one. To represent arrays with other lower bounds, the array
571
+ subscript ranges can be specified explicitly before writing the
572
+ array contents .
561
573
This decoration consists of square brackets (<literal>[]</>)
562
574
around each array dimension's lower and upper bounds, with
563
575
a colon (<literal>:</>) delimiter character in between. The
564
576
array dimension decoration is followed by an equal sign (<literal>=</>).
565
577
For example:
566
578
<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>
587
579
SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
588
580
FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1) AS ss;
589
581
@@ -592,6 +584,18 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
592
584
1 | 6
593
585
(1 row)
594
586
</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.
595
599
</para>
596
600
597
601
<para>
@@ -600,7 +604,9 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
600
604
if the element value would otherwise confuse the array-value parser.
601
605
For example, elements containing curly braces, commas (or whatever the
602
606
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
604
610
quoted array element value, precede it with a backslash. Alternatively, you
605
611
can use backslash-escaping to protect all data characters that would
606
612
otherwise be taken as array syntax.
0 commit comments