14 -- only the 'e' array is 0-based, the others are 1-based.
16 INSERT INTO arrtest (a[1:5], b[1:1][1:2][1:2], c, d, f, g)
17 VALUES ('{1,2,3,4,5}', '{{{0,0},{1,2}}}', '{}', '{}', '{}', '{}');
18 UPDATE arrtest SET e[0] = '1.1';
19 UPDATE arrtest SET e[1] = '2.2';
20 INSERT INTO arrtest (f)
21 VALUES ('{"too long"}');
22 ERROR: value too long for type character(5)
23 INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
24 VALUES ('{11,12,23}', '{{3,4},{4,5}}', '{"foobar"}',
25 '{{"elt1", "elt2"}}', '{"3.4", "6.7"}',
26 '{"abc","abcde"}', '{"abc","abcde"}');
27 INSERT INTO arrtest (a, b[1:2], c, d[1:2])
28 VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
29 SELECT * FROM arrtest;
30 a | b | c | d | e | f | g
31 -------------+-----------------+-----------+---------------+-----------------+-----------------+-------------
32 {1,2,3,4,5} | {{{0,0},{1,2}}} | {} | {} | [0:1]={1.1,2.2} | {} | {}
33 {11,12,23} | {{3,4},{4,5}} | {foobar} | {{elt1,elt2}} | {3.4,6.7} | {"abc ",abcde} | {abc,abcde}
34 {} | {3,4} | {foo,bar} | {bar,foo} | | |
44 ----+---+--------+------+-----
46 11 | | foobar | elt1 |
50 SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]
53 ----+---+--------+------+-----
55 11 | | foobar | elt1 |
65 ------------+-----------------+-----------+---------------
66 {1,2,3} | {{{0,0},{1,2}}} | {} | {}
67 {11,12,23} | {} | {foobar} | {{elt1,elt2}}
68 {} | {} | {foo,bar} | {}
71 SELECT array_ndims(a) AS a,array_ndims(b) AS b,array_ndims(c) AS c
80 SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
83 -------+-----------------+-------
84 [1:5] | [1:1][1:2][1:2] |
85 [1:3] | [1:2][1:2] | [1:1]
93 c = '{"foobar"}'::_name;
94 a | b | c | d | e | f | g
95 ---+---+---+---+---+---+---
99 SET a[1:2] = '{16,25}'
100 WHERE NOT a = '{}'::_int2;
102 SET b[1:1][1:1][1:2] = '{113, 117}',
103 b[1:1][1:2][2:2] = '{142, 147}'
104 WHERE array_dims(b) = '[1:1][1:2][1:2]';
106 SET c[2:2] = '{"new_word"}'
107 WHERE array_dims(c) is not null;
108 SELECT a,b,c FROM arrtest;
110 ---------------+-----------------------+-------------------
111 {16,25,3,4,5} | {{{113,142},{1,147}}} | {}
112 {} | {3,4} | {foo,new_word}
113 {16,25,23} | {{3,4},{4,5}} | {foobar,new_word}
122 ------------+-----------------------+-------------------+----------
123 {16,25,3} | {{{113,142},{1,147}}} | {} | {}
124 {} | {} | {foo,new_word} | {}
125 {16,25,23} | {} | {foobar,new_word} | {{elt2}}
128 INSERT INTO arrtest(a) VALUES('{1,null,3}');
129 SELECT a FROM arrtest;
138 UPDATE arrtest SET a[4] = NULL WHERE a[2] IS NULL;
139 SELECT a FROM arrtest WHERE a[2] IS NULL;
146 DELETE FROM arrtest WHERE a[2] IS NULL AND b IS NULL;
147 SELECT a,b,c FROM arrtest;
149 ---------------+-----------------------+-------------------
150 {16,25,3,4,5} | {{{113,142},{1,147}}} | {}
151 {16,25,23} | {{3,4},{4,5}} | {foobar,new_word}
152 [4:4]={NULL} | {3,4} | {foo,new_word}
156 -- test array extension
158 CREATE TEMP TABLE arrtest1 (i int[], t text[]);
159 insert into arrtest1 values(array[1,2,null,4], array['one','two',null,'four']);
160 select * from arrtest1;
162 --------------+---------------------
163 {1,2,NULL,4} | {one,two,NULL,four}
166 update arrtest1 set i[2] = 22, t[2] = 'twenty-two';
167 select * from arrtest1;
169 ---------------+----------------------------
170 {1,22,NULL,4} | {one,twenty-two,NULL,four}
173 update arrtest1 set i[5] = 5, t[5] = 'five';
174 select * from arrtest1;
176 -----------------+---------------------------------
177 {1,22,NULL,4,5} | {one,twenty-two,NULL,four,five}
180 update arrtest1 set i[8] = 8, t[8] = 'eight';
181 select * from arrtest1;
183 -----------------------------+-------------------------------------------------
184 {1,22,NULL,4,5,NULL,NULL,8} | {one,twenty-two,NULL,four,five,NULL,NULL,eight}
187 update arrtest1 set i[0] = 0, t[0] = 'zero';
188 select * from arrtest1;
190 -------------------------------------+------------------------------------------------------------
191 [0:8]={0,1,22,NULL,4,5,NULL,NULL,8} | [0:8]={zero,one,twenty-two,NULL,four,five,NULL,NULL,eight}
194 update arrtest1 set i[-3] = -3, t[-3] = 'minus-three';
195 select * from arrtest1;
197 ---------------------------------------------------+-----------------------------------------------------------------------------------
198 [-3:8]={-3,NULL,NULL,0,1,22,NULL,4,5,NULL,NULL,8} | [-3:8]={minus-three,NULL,NULL,zero,one,twenty-two,NULL,four,five,NULL,NULL,eight}
201 update arrtest1 set i[0:2] = array[10,11,12], t[0:2] = array['ten','eleven','twelve'];
202 select * from arrtest1;
204 -----------------------------------------------------+---------------------------------------------------------------------------------
205 [-3:8]={-3,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,8} | [-3:8]={minus-three,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,eight}
208 update arrtest1 set i[8:10] = array[18,null,20], t[8:10] = array['p18',null,'p20'];
209 select * from arrtest1;
211 ---------------------------------------------------------------+-----------------------------------------------------------------------------------------
212 [-3:10]={-3,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,18,NULL,20} | [-3:10]={minus-three,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,p18,NULL,p20}
215 update arrtest1 set i[11:12] = array[null,22], t[11:12] = array[null,'p22'];
216 select * from arrtest1;
218 -----------------------------------------------------------------------+--------------------------------------------------------------------------------------------------
219 [-3:12]={-3,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,18,NULL,20,NULL,22} | [-3:12]={minus-three,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,p18,NULL,p20,NULL,p22}
222 update arrtest1 set i[15:16] = array[null,26], t[15:16] = array[null,'p26'];
223 select * from arrtest1;
225 -----------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------
226 [-3:16]={-3,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,18,NULL,20,NULL,22,NULL,NULL,NULL,26} | [-3:16]={minus-three,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,p18,NULL,p20,NULL,p22,NULL,NULL,NULL,p26}
229 update arrtest1 set i[-5:-3] = array[-15,-14,-13], t[-5:-3] = array['m15','m14','m13'];
230 select * from arrtest1;
232 --------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------
233 [-5:16]={-15,-14,-13,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,18,NULL,20,NULL,22,NULL,NULL,NULL,26} | [-5:16]={m15,m14,m13,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,p18,NULL,p20,NULL,p22,NULL,NULL,NULL,p26}
236 update arrtest1 set i[-7:-6] = array[-17,null], t[-7:-6] = array['m17',null];
237 select * from arrtest1;
239 -----------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------
240 [-7:16]={-17,NULL,-15,-14,-13,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,18,NULL,20,NULL,22,NULL,NULL,NULL,26} | [-7:16]={m17,NULL,m15,m14,m13,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,p18,NULL,p20,NULL,p22,NULL,NULL,NULL,p26}
243 update arrtest1 set i[-12:-10] = array[-22,null,-20], t[-12:-10] = array['m22',null,'m20'];
244 select * from arrtest1;
246 -----------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------
247 [-12:16]={-22,NULL,-20,NULL,NULL,-17,NULL,-15,-14,-13,NULL,NULL,10,11,12,NULL,4,5,NULL,NULL,18,NULL,20,NULL,22,NULL,NULL,NULL,26} | [-12:16]={m22,NULL,m20,NULL,NULL,m17,NULL,m15,m14,m13,NULL,NULL,ten,eleven,twelve,NULL,four,five,NULL,NULL,p18,NULL,p20,NULL,p22,NULL,NULL,NULL,p26}
250 delete from arrtest1;
251 insert into arrtest1 values(array[1,2,null,4], array['one','two',null,'four']);
252 select * from arrtest1;
254 --------------+---------------------
255 {1,2,NULL,4} | {one,two,NULL,four}
258 update arrtest1 set i[0:5] = array[0,1,2,null,4,5], t[0:5] = array['z','p1','p2',null,'p4','p5'];
259 select * from arrtest1;
261 ------------------------+----------------------------
262 [0:5]={0,1,2,NULL,4,5} | [0:5]={z,p1,p2,NULL,p4,p5}
266 -- array expressions and operators
268 -- table creation and INSERTs
269 CREATE TEMP TABLE arrtest2 (i integer ARRAY[4], f float8[], n numeric[], t text[], d timestamp[]);
270 INSERT INTO arrtest2 VALUES(
271 ARRAY[[[113,142],[1,147]]],
272 ARRAY[1.1,1.2,1.3]::float8[],
274 ARRAY[[['aaa','aab'],['aba','abb'],['aca','acb']],[['baa','bab'],['bba','bbb'],['bca','bcb']]],
275 ARRAY['19620326','19931223','19970117']::timestamp[]
277 -- some more test data
278 CREATE TEMP TABLE arrtest_f (f0 int, f1 text, f2 float8);
279 insert into arrtest_f values(1,'cat1',1.21);
280 insert into arrtest_f values(2,'cat1',1.24);
281 insert into arrtest_f values(3,'cat1',1.18);
282 insert into arrtest_f values(4,'cat1',1.26);
283 insert into arrtest_f values(5,'cat1',1.15);
284 insert into arrtest_f values(6,'cat2',1.15);
285 insert into arrtest_f values(7,'cat2',1.26);
286 insert into arrtest_f values(8,'cat2',1.32);
287 insert into arrtest_f values(9,'cat2',1.30);
288 CREATE TEMP TABLE arrtest_i (f0 int, f1 text, f2 int);
289 insert into arrtest_i values(1,'cat1',21);
290 insert into arrtest_i values(2,'cat1',24);
291 insert into arrtest_i values(3,'cat1',18);
292 insert into arrtest_i values(4,'cat1',26);
293 insert into arrtest_i values(5,'cat1',15);
294 insert into arrtest_i values(6,'cat2',15);
295 insert into arrtest_i values(7,'cat2',26);
296 insert into arrtest_i values(8,'cat2',32);
297 insert into arrtest_i values(9,'cat2',30);
299 SELECT t.f[1][3][1] AS "131", t.f[2][2][1] AS "221" FROM (
300 SELECT ARRAY[[[111,112],[121,122],[131,132]],[[211,212],[221,122],[231,232]]] AS f
307 SELECT ARRAY[[[[[['hello'],['world']]]]]];
309 ---------------------------
310 {{{{{{hello},{world}}}}}}
313 SELECT ARRAY[ARRAY['hello'],ARRAY['world']];
319 SELECT ARRAY(select f2 from arrtest_f order by f2) AS "ARRAY";
321 -----------------------------------------------
322 {1.15,1.15,1.18,1.21,1.24,1.26,1.26,1.3,1.32}
326 SELECT '{1,null,3}'::int[];
332 SELECT ARRAY[1,NULL,3];
339 SELECT array_append(array[42], 6) AS "{42,6}";
345 SELECT array_prepend(6, array[42]) AS "{6,42}";
351 SELECT array_cat(ARRAY[1,2], ARRAY[3,4]) AS "{1,2,3,4}";
357 SELECT array_cat(ARRAY[1,2], ARRAY[[3,4],[5,6]]) AS "{{1,2},{3,4},{5,6}}";
359 ---------------------
363 SELECT array_cat(ARRAY[[3,4],[5,6]], ARRAY[1,2]) AS "{{3,4},{5,6},{1,2}}";
365 ---------------------
370 SELECT a FROM arrtest WHERE b = ARRAY[[[113,142],[1,147]]];
376 SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";
382 SELECT ARRAY[1,2] || 3 AS "{1,2,3}";
388 SELECT 0 || ARRAY[1,2] AS "{0,1,2}";
394 SELECT ARRAY[1,2] || ARRAY[3,4] AS "{1,2,3,4}";
400 SELECT ARRAY[[['hello','world']]] || ARRAY[[['happy','birthday']]] AS "ARRAY";
402 --------------------------------------
403 {{{hello,world}},{{happy,birthday}}}
406 SELECT ARRAY[[1,2],[3,4]] || ARRAY[5,6] AS "{{1,2},{3,4},{5,6}}";
408 ---------------------
412 SELECT ARRAY[0,0] || ARRAY[1,1] || ARRAY[2,2] AS "{0,0,1,1,2,2}";
418 SELECT 0 || ARRAY[1,2] || 3 AS "{0,1,2,3}";
424 SELECT * FROM array_op_test WHERE i @> '{32}' ORDER BY seqno;
426 -------+---------------------------------+------------------------------------------------------------------------------------------------------------------------------------
427 6 | {39,35,5,94,17,92,60,32} | {AAAAAAAAAAAAAAA35875,AAAAAAAAAAAAAAAA23657}
428 74 | {32} | {AAAAAAAAAAAAAAAA1729,AAAAAAAAAAAAA22860,AAAAAA99807,AAAAA17383,AAAAAAAAAAAAAAA67062,AAAAAAAAAAA15165,AAAAAAAAAAA50956}
429 77 | {97,15,32,17,55,59,18,37,50,39} | {AAAAAAAAAAAA67946,AAAAAA54032,AAAAAAAA81587,55847,AAAAAAAAAAAAAA28620,AAAAAAAAAAAAAAAAA43052,AAAAAA75463,AAAA49534,AAAAAAAA44066}
430 89 | {40,32,17,6,30,88} | {AA44673,AAAAAAAAAAA6119,AAAAAAAAAAAAAAAA23657,AAAAAAAAAAAAAAAAAA47955,AAAAAAAAAAAAAAAA33598,AAAAAAAAAAA33576,AA44673}
431 98 | {38,34,32,89} | {AAAAAAAAAAAAAAAAAA71621,AAAA8857,AAAAAAAAAAAAAAAAAAA65037,AAAAAAAAAAAAAAAA31334,AAAAAAAAAA48845}
432 100 | {85,32,57,39,49,84,32,3,30} | {AAAAAAA80240,AAAAAAAAAAAAAAAA1729,AAAAA60038,AAAAAAAAAAA92631,AAAAAAAA9523}
435 SELECT * FROM array_op_test WHERE i && '{32}' ORDER BY seqno;
437 -------+---------------------------------+------------------------------------------------------------------------------------------------------------------------------------
438 6 | {39,35,5,94,17,92,60,32} | {AAAAAAAAAAAAAAA35875,AAAAAAAAAAAAAAAA23657}
439 74 | {32} | {AAAAAAAAAAAAAAAA1729,AAAAAAAAAAAAA22860,AAAAAA99807,AAAAA17383,AAAAAAAAAAAAAAA67062,AAAAAAAAAAA15165,AAAAAAAAAAA50956}
440 77 | {97,15,32,17,55,59,18,37,50,39} | {AAAAAAAAAAAA67946,AAAAAA54032,AAAAAAAA81587,55847,AAAAAAAAAAAAAA28620,AAAAAAAAAAAAAAAAA43052,AAAAAA75463,AAAA49534,AAAAAAAA44066}
441 89 | {40,32,17,6,30,88} | {AA44673,AAAAAAAAAAA6119,AAAAAAAAAAAAAAAA23657,AAAAAAAAAAAAAAAAAA47955,AAAAAAAAAAAAAAAA33598,AAAAAAAAAAA33576,AA44673}
442 98 | {38,34,32,89} | {AAAAAAAAAAAAAAAAAA71621,AAAA8857,AAAAAAAAAAAAAAAAAAA65037,AAAAAAAAAAAAAAAA31334,AAAAAAAAAA48845}
443 100 | {85,32,57,39,49,84,32,3,30} | {AAAAAAA80240,AAAAAAAAAAAAAAAA1729,AAAAA60038,AAAAAAAAAAA92631,AAAAAAAA9523}
446 SELECT * FROM array_op_test WHERE i @> '{17}' ORDER BY seqno;
448 -------+---------------------------------+------------------------------------------------------------------------------------------------------------------------------------
449 6 | {39,35,5,94,17,92,60,32} | {AAAAAAAAAAAAAAA35875,AAAAAAAAAAAAAAAA23657}
450 12 | {17,99,18,52,91,72,0,43,96,23} | {AAAAA33250,AAAAAAAAAAAAAAAAAAA85420,AAAAAAAAAAA33576}
451 15 | {17,14,16,63,67} | {AA6416,AAAAAAAAAA646,AAAAA95309}
452 19 | {52,82,17,74,23,46,69,51,75} | {AAAAAAAAAAAAA73084,AAAAA75968,AAAAAAAAAAAAAAAA14047,AAAAAAA80240,AAAAAAAAAAAAAAAAAAA1205,A68938}
453 53 | {38,17} | {AAAAAAAAAAA21658}
454 65 | {61,5,76,59,17} | {AAAAAA99807,AAAAA64741,AAAAAAAAAAA53908,AA21643,AAAAAAAAA10012}
455 77 | {97,15,32,17,55,59,18,37,50,39} | {AAAAAAAAAAAA67946,AAAAAA54032,AAAAAAAA81587,55847,AAAAAAAAAAAAAA28620,AAAAAAAAAAAAAAAAA43052,AAAAAA75463,AAAA49534,AAAAAAAA44066}
456 89 | {40,32,17,6,30,88} | {AA44673,AAAAAAAAAAA6119,AAAAAAAAAAAAAAAA23657,AAAAAAAAAAAAAAAAAA47955,AAAAAAAAAAAAAAAA33598,AAAAAAAAAAA33576,AA44673}
459 SELECT * FROM array_op_test WHERE i && '{17}' ORDER BY seqno;
461 -------+---------------------------------+------------------------------------------------------------------------------------------------------------------------------------
462 6 | {39,35,5,94,17,92,60,32} | {AAAAAAAAAAAAAAA35875,AAAAAAAAAAAAAAAA23657}
463 12 | {17,99,18,52,91,72,0,43,96,23} | {AAAAA33250,AAAAAAAAAAAAAAAAAAA85420,AAAAAAAAAAA33576}
464 15 | {17,14,16,63,67} | {AA6416,AAAAAAAAAA646,AAAAA95309}
465 19 | {52,82,17,74,23,46,69,51,75} | {AAAAAAAAAAAAA73084,AAAAA75968,AAAAAAAAAAAAAAAA14047,AAAAAAA80240,AAAAAAAAAAAAAAAAAAA1205,A68938}
466 53 | {38,17} | {AAAAAAAAAAA21658}
467 65 | {61,5,76,59,17} | {AAAAAA99807,AAAAA64741,AAAAAAAAAAA53908,AA21643,AAAAAAAAA10012}
468 77 | {97,15,32,17,55,59,18,37,50,39} | {AAAAAAAAAAAA67946,AAAAAA54032,AAAAAAAA81587,55847,AAAAAAAAAAAAAA28620,AAAAAAAAAAAAAAAAA43052,AAAAAA75463,AAAA49534,AAAAAAAA44066}
469 89 | {40,32,17,6,30,88} | {AA44673,AAAAAAAAAAA6119,AAAAAAAAAAAAAAAA23657,AAAAAAAAAAAAAAAAAA47955,AAAAAAAAAAAAAAAA33598,AAAAAAAAAAA33576,AA44673}
472 SELECT * FROM array_op_test WHERE i @> '{32,17}' ORDER BY seqno;
474 -------+---------------------------------+------------------------------------------------------------------------------------------------------------------------------------
475 6 | {39,35,5,94,17,92,60,32} | {AAAAAAAAAAAAAAA35875,AAAAAAAAAAAAAAAA23657}
476 77 | {97,15,32,17,55,59,18,37,50,39} | {AAAAAAAAAAAA67946,AAAAAA54032,AAAAAAAA81587,55847,AAAAAAAAAAAAAA28620,AAAAAAAAAAAAAAAAA43052,AAAAAA75463,AAAA49534,AAAAAAAA44066}
477 89 | {40,32,17,6,30,88} | {AA44673,AAAAAAAAAAA6119,AAAAAAAAAAAAAAAA23657,AAAAAAAAAAAAAAAAAA47955,AAAAAAAAAAAAAAAA33598,AAAAAAAAAAA33576,AA44673}
480 SELECT * FROM array_op_test WHERE i && '{32,17}' ORDER BY seqno;
482 -------+---------------------------------+------------------------------------------------------------------------------------------------------------------------------------
483 6 | {39,35,5,94,17,92,60,32} | {AAAAAAAAAAAAAAA35875,AAAAAAAAAAAAAAAA23657}
484 12 | {17,99,18,52,91,72,0,43,96,23} | {AAAAA33250,AAAAAAAAAAAAAAAAAAA85420,AAAAAAAAAAA33576}
485 15 | {17,14,16,63,67} | {AA6416,AAAAAAAAAA646,AAAAA95309}
486 19 | {52,82,17,74,23,46,69,51,75} | {AAAAAAAAAAAAA73084,AAAAA75968,AAAAAAAAAAAAAAAA14047,AAAAAAA80240,AAAAAAAAAAAAAAAAAAA1205,A68938}
487 53 | {38,17} | {AAAAAAAAAAA21658}
488 65 | {61,5,76,59,17} | {AAAAAA99807,AAAAA64741,AAAAAAAAAAA53908,AA21643,AAAAAAAAA10012}
489 74 | {32} | {AAAAAAAAAAAAAAAA1729,AAAAAAAAAAAAA22860,AAAAAA99807,AAAAA17383,AAAAAAAAAAAAAAA67062,AAAAAAAAAAA15165,AAAAAAAAAAA50956}
490 77 | {97,15,32,17,55,59,18,37,50,39} | {AAAAAAAAAAAA67946,AAAAAA54032,AAAAAAAA81587,55847,AAAAAAAAAAAAAA28620,AAAAAAAAAAAAAAAAA43052,AAAAAA75463,AAAA49534,AAAAAAAA44066}
491 89 | {40,32,17,6,30,88} | {AA44673,AAAAAAAAAAA6119,AAAAAAAAAAAAAAAA23657,AAAAAAAAAAAAAAAAAA47955,AAAAAAAAAAAAAAAA33598,AAAAAAAAAAA33576,AA44673}
492 98 | {38,34,32,89} | {AAAAAAAAAAAAAAAAAA71621,AAAA8857,AAAAAAAAAAAAAAAAAAA65037,AAAAAAAAAAAAAAAA31334,AAAAAAAAAA48845}
493 100 | {85,32,57,39,49,84,32,3,30} | {AAAAAAA80240,AAAAAAAAAAAAAAAA1729,AAAAA60038,AAAAAAAAAAA92631,AAAAAAAA9523}
496 SELECT * FROM array_op_test WHERE i <@ '{38,34,32,89}' ORDER BY seqno;
498 -------+---------------+----------------------------------------------------------------------------------------------------------------------------
499 40 | {34} | {AAAAAAAAAAAAAA10611,AAAAAAAAAAAAAAAAAAA1205,AAAAAAAAAAA50956,AAAAAAAAAAAAAAAA31334,AAAAA70466,AAAAAAAA81587,AAAAAAA74623}
500 74 | {32} | {AAAAAAAAAAAAAAAA1729,AAAAAAAAAAAAA22860,AAAAAA99807,AAAAA17383,AAAAAAAAAAAAAAA67062,AAAAAAAAAAA15165,AAAAAAAAAAA50956}
501 98 | {38,34,32,89} | {AAAAAAAAAAAAAAAAAA71621,AAAA8857,AAAAAAAAAAAAAAAAAAA65037,AAAAAAAAAAAAAAAA31334,AAAAAAAAAA48845}
504 SELECT * FROM array_op_test WHERE t @> '{AAAAAAAA72908}' ORDER BY seqno;
506 -------+-----------------------+--------------------------------------------------------------------------------------------------------------------------------------------
507 22 | {11,6,56,62,53,30} | {AAAAAAAA72908}
508 45 | {99,45} | {AAAAAAAA72908,AAAAAAAAAAAAAAAAAAA17075,AA88409,AAAAAAAAAAAAAAAAAA36842,AAAAAAA48038,AAAAAAAAAAAAAA10611}
509 72 | {22,1,16,78,20,91,83} | {47735,AAAAAAA56483,AAAAAAAAAAAAA93788,AA42406,AAAAAAAAAAAAA73084,AAAAAAAA72908,AAAAAAAAAAAAAAAAAA61286,AAAAA66674,AAAAAAAAAAAAAAAAA50407}
510 79 | {45} | {AAAAAAAAAA646,AAAAAAAAAAAAAAAAAAA70415,AAAAAA43678,AAAAAAAA72908}
513 SELECT * FROM array_op_test WHERE t && '{AAAAAAAA72908}' ORDER BY seqno;
515 -------+-----------------------+--------------------------------------------------------------------------------------------------------------------------------------------
516 22 | {11,6,56,62,53,30} | {AAAAAAAA72908}
517 45 | {99,45} | {AAAAAAAA72908,AAAAAAAAAAAAAAAAAAA17075,AA88409,AAAAAAAAAAAAAAAAAA36842,AAAAAAA48038,AAAAAAAAAAAAAA10611}
518 72 | {22,1,16,78,20,91,83} | {47735,AAAAAAA56483,AAAAAAAAAAAAA93788,AA42406,AAAAAAAAAAAAA73084,AAAAAAAA72908,AAAAAAAAAAAAAAAAAA61286,AAAAA66674,AAAAAAAAAAAAAAAAA50407}
519 79 | {45} | {AAAAAAAAAA646,AAAAAAAAAAAAAAAAAAA70415,AAAAAA43678,AAAAAAAA72908}
522 SELECT * FROM array_op_test WHERE t @> '{AAAAAAAAAA646}' ORDER BY seqno;
524 -------+------------------+--------------------------------------------------------------------
525 15 | {17,14,16,63,67} | {AA6416,AAAAAAAAAA646,AAAAA95309}
526 79 | {45} | {AAAAAAAAAA646,AAAAAAAAAAAAAAAAAAA70415,AAAAAA43678,AAAAAAAA72908}
527 96 | {23,97,43} | {AAAAAAAAAA646,A87088}
530 SELECT * FROM array_op_test WHERE t && '{AAAAAAAAAA646}' ORDER BY seqno;
532 -------+------------------+--------------------------------------------------------------------
533 15 | {17,14,16,63,67} | {AA6416,AAAAAAAAAA646,AAAAA95309}
534 79 | {45} | {AAAAAAAAAA646,AAAAAAAAAAAAAAAAAAA70415,AAAAAA43678,AAAAAAAA72908}
535 96 | {23,97,43} | {AAAAAAAAAA646,A87088}
538 SELECT * FROM array_op_test WHERE t @> '{AAAAAAAA72908,AAAAAAAAAA646}' ORDER BY seqno;
540 -------+------+--------------------------------------------------------------------
541 79 | {45} | {AAAAAAAAAA646,AAAAAAAAAAAAAAAAAAA70415,AAAAAA43678,AAAAAAAA72908}
544 SELECT * FROM array_op_test WHERE t && '{AAAAAAAA72908,AAAAAAAAAA646}' ORDER BY seqno;
546 -------+-----------------------+--------------------------------------------------------------------------------------------------------------------------------------------
547 15 | {17,14,16,63,67} | {AA6416,AAAAAAAAAA646,AAAAA95309}
548 22 | {11,6,56,62,53,30} | {AAAAAAAA72908}
549 45 | {99,45} | {AAAAAAAA72908,AAAAAAAAAAAAAAAAAAA17075,AA88409,AAAAAAAAAAAAAAAAAA36842,AAAAAAA48038,AAAAAAAAAAAAAA10611}
550 72 | {22,1,16,78,20,91,83} | {47735,AAAAAAA56483,AAAAAAAAAAAAA93788,AA42406,AAAAAAAAAAAAA73084,AAAAAAAA72908,AAAAAAAAAAAAAAAAAA61286,AAAAA66674,AAAAAAAAAAAAAAAAA50407}
551 79 | {45} | {AAAAAAAAAA646,AAAAAAAAAAAAAAAAAAA70415,AAAAAA43678,AAAAAAAA72908}
552 96 | {23,97,43} | {AAAAAAAAAA646,A87088}
555 SELECT * FROM array_op_test WHERE t <@ '{AAAAAAAA72908,AAAAAAAAAAAAAAAAAAA17075,AA88409,AAAAAAAAAAAAAAAAAA36842,AAAAAAA48038,AAAAAAAAAAAAAA10611}' ORDER BY seqno;
557 -------+--------------------+-----------------------------------------------------------------------------------------------------------
558 22 | {11,6,56,62,53,30} | {AAAAAAAA72908}
559 45 | {99,45} | {AAAAAAAA72908,AAAAAAAAAAAAAAAAAAA17075,AA88409,AAAAAAAAAAAAAAAAAA36842,AAAAAAA48038,AAAAAAAAAAAAAA10611}
563 SELECT ARRAY[1,2,3]::text[]::int[]::float8[] AS "{1,2,3}";
569 SELECT ARRAY[1,2,3]::text[]::int[]::float8[] is of (float8[]) as "TRUE";
575 SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] AS "{{a,bc},{def,hijk}}";
577 ---------------------
581 SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] is of (varchar[]) as "TRUE";
587 SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}";
589 ----------------------
593 -- scalar op any/all (array)
594 select 33 = any ('{1,2,3}');
600 select 33 = any ('{1,2,33}');
606 select 33 = all ('{1,2,33}');
612 select 33 >= all ('{1,2,33}');
619 select null::int >= all ('{1,2,33}');
625 select null::int >= all ('{}');
631 select null::int >= any ('{}');
638 select 33.4 = any (array[1,2,3]);
644 select 33.4 > all (array[1,2,3]);
651 select 33 * any ('{1,2,3}');
652 ERROR: op ANY/ALL (array) requires operator to yield boolean
653 LINE 1: select 33 * any ('{1,2,3}');
655 select 33 * any (44);
656 ERROR: op ANY/ALL (array) requires array on right side
657 LINE 1: select 33 * any (44);
660 select 33 = any (null::int[]);
666 select null::int = any ('{1,2,3}');
672 select 33 = any ('{1,null,3}');
678 select 33 = any ('{1,null,33}');
684 select 33 = all (null::int[]);
690 select null::int = all ('{1,2,3}');
696 select 33 = all ('{1,null,3}');
702 select 33 = all ('{33,null,33}');
708 -- test indexes on arrays
709 create temp table arr_tbl (f1 int[] unique);
710 NOTICE: CREATE TABLE / UNIQUE will create implicit index "arr_tbl_f1_key" for table "arr_tbl"
711 insert into arr_tbl values ('{1,2,3}');
712 insert into arr_tbl values ('{1,2}');
714 insert into arr_tbl values ('{1,2,3}');
715 ERROR: duplicate key value violates unique constraint "arr_tbl_f1_key"
716 insert into arr_tbl values ('{2,3,4}');
717 insert into arr_tbl values ('{1,5,3}');
718 insert into arr_tbl values ('{1,2,10}');
719 set enable_seqscan to off;
720 set enable_bitmapscan to off;
721 select * from arr_tbl where f1 > '{1,2,3}' and f1 <= '{1,5,3}';
728 select * from arr_tbl where f1 >= '{1,2,3}' and f1 < '{1,5,3}';
735 -- note: if above selects don't produce the expected tuple order,
736 -- then you didn't get an indexscan plan, and something is busted.
737 reset enable_seqscan;
738 reset enable_bitmapscan;
739 -- test [not] (like|ilike) (any|all) (...)
740 select 'foo' like any (array['%a', '%o']); -- t
746 select 'foo' like any (array['%a', '%b']); -- f
752 select 'foo' like all (array['f%', '%o']); -- t
758 select 'foo' like all (array['f%', '%b']); -- f
764 select 'foo' not like any (array['%a', '%b']); -- t
770 select 'foo' not like all (array['%a', '%o']); -- f
776 select 'foo' ilike any (array['%A', '%O']); -- t
782 select 'foo' ilike all (array['F%', '%O']); -- t
789 -- General array parser tests
791 -- none of the following should be accepted
792 select '{{1,{2}},{2,3}}'::text[];
793 ERROR: malformed array literal: "{{1,{2}},{2,3}}"
794 LINE 1: select '{{1,{2}},{2,3}}'::text[];
796 select '{{},{}}'::text[];
797 ERROR: malformed array literal: "{{},{}}"
798 LINE 1: select '{{},{}}'::text[];
800 select E'{{1,2},\\{2,3}}'::text[];
801 ERROR: malformed array literal: "{{1,2},\{2,3}}"
802 LINE 1: select E'{{1,2},\\{2,3}}'::text[];
804 select '{{"1 2" x},{3}}'::text[];
805 ERROR: malformed array literal: "{{"1 2" x},{3}}"
806 LINE 1: select '{{"1 2" x},{3}}'::text[];
808 select '{}}'::text[];
809 ERROR: malformed array literal: "{}}"
810 LINE 1: select '{}}'::text[];
812 select '{ }}'::text[];
813 ERROR: malformed array literal: "{ }}"
814 LINE 1: select '{ }}'::text[];
817 ERROR: cannot determine type of empty array
818 LINE 1: select array[];
820 HINT: Explicitly cast to the desired type, for example ARRAY[]::integer[].
821 -- none of the above should be accepted
822 -- all of the following should be accepted
829 select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
831 -----------------------------------------------
832 {{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}
835 select '{0 second ,0 second}'::interval[];
841 select '{ { "," } , { 3 } }'::text[];
847 select ' { { " 0 second " , 0 second } }'::text[];
849 -------------------------------
850 {{" 0 second ","0 second"}}
855 @ 1 hour @ 42 minutes @ 20 seconds
858 ------------------------------------
859 {"@ 0","@ 1 hour 42 mins 20 secs"}
862 select array[]::text[];
868 select '[0:1]={1.1,2.2}'::float8[];
874 -- all of the above should be accepted
875 -- tests for array aggregates
876 CREATE TEMP TABLE arraggtest ( f1 INT[], f2 TEXT[][], f3 FLOAT[]);
877 INSERT INTO arraggtest (f1, f2, f3) VALUES
878 ('{1,2,3,4}','{{grey,red},{blue,blue}}','{1.6, 0.0}');
879 INSERT INTO arraggtest (f1, f2, f3) VALUES
880 ('{1,2,3}','{{grey,red},{grey,blue}}','{1.6}');
881 SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
882 max | min | max | min | max | min
883 -----------+---------+--------------------------+--------------------------+---------+-------
884 {1,2,3,4} | {1,2,3} | {{grey,red},{grey,blue}} | {{grey,red},{blue,blue}} | {1.6,0} | {1.6}
887 INSERT INTO arraggtest (f1, f2, f3) VALUES
888 ('{3,3,2,4,5,6}','{{white,yellow},{pink,orange}}','{2.1,3.3,1.8,1.7,1.6}');
889 SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
890 max | min | max | min | max | min
891 ---------------+---------+--------------------------------+--------------------------+-----------------------+-------
892 {3,3,2,4,5,6} | {1,2,3} | {{white,yellow},{pink,orange}} | {{grey,red},{blue,blue}} | {2.1,3.3,1.8,1.7,1.6} | {1.6}
895 INSERT INTO arraggtest (f1, f2, f3) VALUES
896 ('{2}','{{black,red},{green,orange}}','{1.6,2.2,2.6,0.4}');
897 SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
898 max | min | max | min | max | min
899 ---------------+---------+--------------------------------+------------------------------+-----------------------+-------
900 {3,3,2,4,5,6} | {1,2,3} | {{white,yellow},{pink,orange}} | {{black,red},{green,orange}} | {2.1,3.3,1.8,1.7,1.6} | {1.6}
903 INSERT INTO arraggtest (f1, f2, f3) VALUES
904 ('{4,2,6,7,8,1}','{{red},{black},{purple},{blue},{blue}}',NULL);
905 SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
906 max | min | max | min | max | min
907 ---------------+---------+--------------------------------+------------------------------+-----------------------+-------
908 {4,2,6,7,8,1} | {1,2,3} | {{white,yellow},{pink,orange}} | {{black,red},{green,orange}} | {2.1,3.3,1.8,1.7,1.6} | {1.6}
911 INSERT INTO arraggtest (f1, f2, f3) VALUES
912 ('{}','{{pink,white,blue,red,grey,orange}}','{2.1,1.87,1.4,2.2}');
913 SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
914 max | min | max | min | max | min
915 ---------------+-----+--------------------------------+------------------------------+-----------------------+-------
916 {4,2,6,7,8,1} | {} | {{white,yellow},{pink,orange}} | {{black,red},{green,orange}} | {2.1,3.3,1.8,1.7,1.6} | {1.6}
919 -- A few simple tests for arrays of composite types
920 create type comptype as (f1 int, f2 text);
921 create table comptable (c1 comptype, c2 comptype[]);
922 -- XXX would like to not have to specify row() construct types here ...
923 insert into comptable
924 values (row(1,'foo'), array[row(2,'bar')::comptype, row(3,'baz')::comptype]);
925 -- check that implicitly named array type _comptype isn't a problem
926 create type _comptype as enum('fooey');
927 select * from comptable;
929 ---------+-----------------------
930 (1,foo) | {"(2,bar)","(3,baz)"}
933 select c2[2].f2 from comptable;
940 drop table comptable;
942 create or replace function unnest1(anyarray)
943 returns setof anyelement as $$
944 select $1[s] from generate_subscripts($1,1) g(s);
945 $$ language sql immutable;
946 create or replace function unnest2(anyarray)
947 returns setof anyelement as $$
948 select $1[s1][s2] from generate_subscripts($1,1) g1(s1),
949 generate_subscripts($1,2) g2(s2);
950 $$ language sql immutable;
951 select * from unnest1(array[1,2,3]);
959 select * from unnest2(array[[1,2,3],[4,5,6]]);
970 drop function unnest1(anyarray);
971 drop function unnest2(anyarray);
972 select array_fill(null::integer, array[3,3],array[2,2]);
974 -----------------------------------------------------------------
975 [2:4][2:4]={{NULL,NULL,NULL},{NULL,NULL,NULL},{NULL,NULL,NULL}}
978 select array_fill(null::integer, array[3,3]);
980 ------------------------------------------------------
981 {{NULL,NULL,NULL},{NULL,NULL,NULL},{NULL,NULL,NULL}}
984 select array_fill(null::text, array[3,3],array[2,2]);
986 -----------------------------------------------------------------
987 [2:4][2:4]={{NULL,NULL,NULL},{NULL,NULL,NULL},{NULL,NULL,NULL}}
990 select array_fill(null::text, array[3,3]);
992 ------------------------------------------------------
993 {{NULL,NULL,NULL},{NULL,NULL,NULL},{NULL,NULL,NULL}}
996 select array_fill(7, array[3,3],array[2,2]);
998 --------------------------------------
999 [2:4][2:4]={{7,7,7},{7,7,7},{7,7,7}}
1002 select array_fill(7, array[3,3]);
1004 ---------------------------
1005 {{7,7,7},{7,7,7},{7,7,7}}
1008 select array_fill('juhu'::text, array[3,3],array[2,2]);
1010 -----------------------------------------------------------------
1011 [2:4][2:4]={{juhu,juhu,juhu},{juhu,juhu,juhu},{juhu,juhu,juhu}}
1014 select array_fill('juhu'::text, array[3,3]);
1016 ------------------------------------------------------
1017 {{juhu,juhu,juhu},{juhu,juhu,juhu},{juhu,juhu,juhu}}
1021 select array_fill(1, null, array[2,2]);
1022 ERROR: dimension array or low bound array cannot be NULL
1023 select array_fill(1, array[2,2], null);
1024 ERROR: dimension array or low bound array cannot be NULL
1025 select array_fill(1, array[3,3], array[1,1,1]);
1026 ERROR: wrong number of array subscripts
1027 DETAIL: Low bound array has different size than dimensions array.
1028 select array_fill(1, array[1,2,null]);
1029 ERROR: dimension values cannot be null
1030 select string_to_array('1|2|3', '|');
1036 select string_to_array('1|2|3|', '|');
1042 select string_to_array('1||2|3||', '||');
1048 select string_to_array('1|2|3', '');
1054 select string_to_array('', '|');
1060 select string_to_array('1|2|3', NULL);
1066 select string_to_array(NULL, '|');
1072 select array_to_string(string_to_array('1|2|3', '|'), '|');
1078 select array_length(array[1,2,3], 1);
1084 select array_length(array[[1,2,3], [4,5,6]], 0);
1090 select array_length(array[[1,2,3], [4,5,6]], 1);
1096 select array_length(array[[1,2,3], [4,5,6]], 2);
1102 select array_length(array[[1,2,3], [4,5,6]], 3);
1108 select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 order by unique1) ss;
1110 --------------------------------------
1111 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}
1114 select array_agg(ten) from (select ten from tenk1 where unique1 < 15 order by unique1) ss;
1116 ---------------------------------
1117 {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4}
1120 select array_agg(nullif(ten, 4)) from (select ten from tenk1 where unique1 < 15 order by unique1) ss;
1122 ---------------------------------------
1123 {0,1,2,3,NULL,5,6,7,8,9,0,1,2,3,NULL}
1126 select array_agg(unique1) from tenk1 where unique1 < -15;
1132 select unnest(array[1,2,3]);
1140 select * from unnest(array[1,2,3]);
1148 select unnest(array[1,2,3,4.5]::float8[]);
1157 select unnest(array[1,2,3,4.5]::numeric[]);
1166 select unnest(array[1,2,3,null,4,null,null,5,6]);
1180 select unnest(array[1,2,3,null,4,null,null,5,6]::text[]);