~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/test/regress/expected/arrays.out

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--
 
2
-- ARRAYS
 
3
--
 
4
CREATE TABLE arrtest (
 
5
        a                       int2[],
 
6
        b                       int4[][][],
 
7
        c                       name[],
 
8
        d                       text[][], 
 
9
        e                       float8[],
 
10
        f                       char(5)[],
 
11
        g                       varchar(5)[]
 
12
);
 
13
--
 
14
-- only the 'e' array is 0-based, the others are 1-based.
 
15
--
 
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}     |                 |                 | 
 
35
(3 rows)
 
36
 
 
37
SELECT arrtest.a[1],
 
38
          arrtest.b[1][1][1],
 
39
          arrtest.c[1],
 
40
          arrtest.d[1][1], 
 
41
          arrtest.e[0]
 
42
   FROM arrtest;
 
43
 a  | b |   c    |  d   |  e  
 
44
----+---+--------+------+-----
 
45
  1 | 0 |        |      | 1.1
 
46
 11 |   | foobar | elt1 |    
 
47
    |   | foo    |      |    
 
48
(3 rows)
 
49
 
 
50
SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]
 
51
   FROM arrtest;
 
52
 a  | b |   c    |  d   |  e  
 
53
----+---+--------+------+-----
 
54
  1 | 0 |        |      | 1.1
 
55
 11 |   | foobar | elt1 |    
 
56
    |   | foo    |      |    
 
57
(3 rows)
 
58
 
 
59
SELECT a[1:3],
 
60
          b[1:1][1:2][1:2],
 
61
          c[1:2], 
 
62
          d[1:1][1:2]
 
63
   FROM arrtest;
 
64
     a      |        b        |     c     |       d       
 
65
------------+-----------------+-----------+---------------
 
66
 {1,2,3}    | {{{0,0},{1,2}}} |           | 
 
67
 {11,12,23} |                 | {foobar}  | {{elt1,elt2}}
 
68
            |                 | {foo,bar} | 
 
69
(3 rows)
 
70
 
 
71
SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
 
72
   FROM arrtest;
 
73
   a   |        b        |   c   
 
74
-------+-----------------+-------
 
75
 [1:5] | [1:1][1:2][1:2] | 
 
76
 [1:3] | [1:2][1:2]      | [1:1]
 
77
       | [1:2]           | [1:2]
 
78
(3 rows)
 
79
 
 
80
-- returns nothing 
 
81
SELECT *
 
82
   FROM arrtest
 
83
   WHERE a[1] < 5 and 
 
84
         c = '{"foobar"}'::_name;
 
85
 a | b | c | d | e | f | g 
 
86
---+---+---+---+---+---+---
 
87
(0 rows)
 
88
 
 
89
UPDATE arrtest
 
90
  SET a[1:2] = '{16,25}'
 
91
  WHERE NOT a = '{}'::_int2;
 
92
UPDATE arrtest
 
93
  SET b[1:1][1:1][1:2] = '{113, 117}',
 
94
      b[1:1][1:2][2:2] = '{142, 147}'
 
95
  WHERE array_dims(b) = '[1:1][1:2][1:2]';
 
96
UPDATE arrtest
 
97
  SET c[2:2] = '{"new_word"}'
 
98
  WHERE array_dims(c) is not null;
 
99
SELECT a,b,c FROM arrtest;
 
100
       a       |           b           |         c         
 
101
---------------+-----------------------+-------------------
 
102
 {16,25,3,4,5} | {{{113,142},{1,147}}} | {}
 
103
 {}            | {3,4}                 | {foo,new_word}
 
104
 {16,25,23}    | {{3,4},{4,5}}         | {foobar,new_word}
 
105
(3 rows)
 
106
 
 
107
SELECT a[1:3],
 
108
          b[1:1][1:2][1:2],
 
109
          c[1:2], 
 
110
          d[1:1][2:2]
 
111
   FROM arrtest;
 
112
     a      |           b           |         c         |    d     
 
113
------------+-----------------------+-------------------+----------
 
114
 {16,25,3}  | {{{113,142},{1,147}}} |                   | 
 
115
            |                       | {foo,new_word}    | 
 
116
 {16,25,23} |                       | {foobar,new_word} | {{elt2}}
 
117
(3 rows)
 
118
 
 
119
--
 
120
-- array expressions and operators
 
121
--
 
122
-- table creation and INSERTs
 
123
CREATE TEMP TABLE arrtest2 (i integer ARRAY[4], f float8[], n numeric[], t text[], d timestamp[]);
 
124
INSERT INTO arrtest2 VALUES(
 
125
  ARRAY[[[113,142],[1,147]]],
 
126
  ARRAY[1.1,1.2,1.3]::float8[],
 
127
  ARRAY[1.1,1.2,1.3],
 
128
  ARRAY[[['aaa','aab'],['aba','abb'],['aca','acb']],[['baa','bab'],['bba','bbb'],['bca','bcb']]],
 
129
  ARRAY['19620326','19931223','19970117']::timestamp[]
 
130
);
 
131
-- some more test data
 
132
CREATE TEMP TABLE arrtest_f (f0 int, f1 text, f2 float8);
 
133
insert into arrtest_f values(1,'cat1',1.21);
 
134
insert into arrtest_f values(2,'cat1',1.24);
 
135
insert into arrtest_f values(3,'cat1',1.18);
 
136
insert into arrtest_f values(4,'cat1',1.26);
 
137
insert into arrtest_f values(5,'cat1',1.15);
 
138
insert into arrtest_f values(6,'cat2',1.15);
 
139
insert into arrtest_f values(7,'cat2',1.26);
 
140
insert into arrtest_f values(8,'cat2',1.32);
 
141
insert into arrtest_f values(9,'cat2',1.30);
 
142
CREATE TEMP TABLE arrtest_i (f0 int, f1 text, f2 int);
 
143
insert into arrtest_i values(1,'cat1',21);
 
144
insert into arrtest_i values(2,'cat1',24);
 
145
insert into arrtest_i values(3,'cat1',18);
 
146
insert into arrtest_i values(4,'cat1',26);
 
147
insert into arrtest_i values(5,'cat1',15);
 
148
insert into arrtest_i values(6,'cat2',15);
 
149
insert into arrtest_i values(7,'cat2',26);
 
150
insert into arrtest_i values(8,'cat2',32);
 
151
insert into arrtest_i values(9,'cat2',30);
 
152
-- expressions
 
153
SELECT t.f[1][3][1] AS "131", t.f[2][2][1] AS "221" FROM (
 
154
  SELECT ARRAY[[[111,112],[121,122],[131,132]],[[211,212],[221,122],[231,232]]] AS f
 
155
) AS t;
 
156
 131 | 221 
 
157
-----+-----
 
158
 131 | 221
 
159
(1 row)
 
160
 
 
161
SELECT ARRAY[[[[[['hello'],['world']]]]]];
 
162
           array           
 
163
---------------------------
 
164
 {{{{{{hello},{world}}}}}}
 
165
(1 row)
 
166
 
 
167
SELECT ARRAY[ARRAY['hello'],ARRAY['world']];
 
168
       array       
 
169
-------------------
 
170
 {{hello},{world}}
 
171
(1 row)
 
172
 
 
173
SELECT ARRAY(select f2 from arrtest_f order by f2) AS "ARRAY";
 
174
                     ARRAY                     
 
175
-----------------------------------------------
 
176
 {1.15,1.15,1.18,1.21,1.24,1.26,1.26,1.3,1.32}
 
177
(1 row)
 
178
 
 
179
-- functions
 
180
SELECT array_append(array[42], 6) AS "{42,6}";
 
181
 {42,6} 
 
182
--------
 
183
 {42,6}
 
184
(1 row)
 
185
 
 
186
SELECT array_prepend(6, array[42]) AS "{6,42}";
 
187
    {6,42}    
 
188
--------------
 
189
 [0:1]={6,42}
 
190
(1 row)
 
191
 
 
192
SELECT array_cat(ARRAY[1,2], ARRAY[3,4]) AS "{1,2,3,4}";
 
193
 {1,2,3,4} 
 
194
-----------
 
195
 {1,2,3,4}
 
196
(1 row)
 
197
 
 
198
SELECT array_cat(ARRAY[1,2], ARRAY[[3,4],[5,6]]) AS "{{1,2},{3,4},{5,6}}";
 
199
      {{1,2},{3,4},{5,6}}       
 
200
--------------------------------
 
201
 [0:2][1:2]={{1,2},{3,4},{5,6}}
 
202
(1 row)
 
203
 
 
204
SELECT array_cat(ARRAY[[3,4],[5,6]], ARRAY[1,2]) AS "{{3,4},{5,6},{1,2}}";
 
205
 {{3,4},{5,6},{1,2}} 
 
206
---------------------
 
207
 {{3,4},{5,6},{1,2}}
 
208
(1 row)
 
209
 
 
210
-- operators
 
211
SELECT a FROM arrtest WHERE b = ARRAY[[[113,142],[1,147]]];
 
212
       a       
 
213
---------------
 
214
 {16,25,3,4,5}
 
215
(1 row)
 
216
 
 
217
SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";
 
218
 FALSE 
 
219
-------
 
220
 f
 
221
(1 row)
 
222
 
 
223
SELECT ARRAY[1,2] || 3 AS "{1,2,3}";
 
224
 {1,2,3} 
 
225
---------
 
226
 {1,2,3}
 
227
(1 row)
 
228
 
 
229
SELECT 0 || ARRAY[1,2] AS "{0,1,2}";
 
230
    {0,1,2}    
 
231
---------------
 
232
 [0:2]={0,1,2}
 
233
(1 row)
 
234
 
 
235
SELECT ARRAY[1,2] || ARRAY[3,4] AS "{1,2,3,4}";
 
236
 {1,2,3,4} 
 
237
-----------
 
238
 {1,2,3,4}
 
239
(1 row)
 
240
 
 
241
SELECT ARRAY[[['hello','world']]] || ARRAY[[['happy','birthday']]] AS "ARRAY";
 
242
                ARRAY                 
 
243
--------------------------------------
 
244
 {{{hello,world}},{{happy,birthday}}}
 
245
(1 row)
 
246
 
 
247
SELECT ARRAY[[1,2],[3,4]] || ARRAY[5,6] AS "{{1,2},{3,4},{5,6}}";
 
248
 {{1,2},{3,4},{5,6}} 
 
249
---------------------
 
250
 {{1,2},{3,4},{5,6}}
 
251
(1 row)
 
252
 
 
253
SELECT ARRAY[0,0] || ARRAY[1,1] || ARRAY[2,2] AS "{0,0,1,1,2,2}";
 
254
 {0,0,1,1,2,2} 
 
255
---------------
 
256
 {0,0,1,1,2,2}
 
257
(1 row)
 
258
 
 
259
SELECT 0 || ARRAY[1,2] || 3 AS "{0,1,2,3}";
 
260
    {0,1,2,3}    
 
261
-----------------
 
262
 [0:3]={0,1,2,3}
 
263
(1 row)
 
264
 
 
265
-- array casts
 
266
SELECT ARRAY[1,2,3]::text[]::int[]::float8[] AS "{1,2,3}";
 
267
 {1,2,3} 
 
268
---------
 
269
 {1,2,3}
 
270
(1 row)
 
271
 
 
272
SELECT ARRAY[1,2,3]::text[]::int[]::float8[] is of (float8[]) as "TRUE";
 
273
 TRUE 
 
274
------
 
275
 t
 
276
(1 row)
 
277
 
 
278
SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] AS "{{a,bc},{def,hijk}}";
 
279
 {{a,bc},{def,hijk}} 
 
280
---------------------
 
281
 {{a,bc},{def,hijk}}
 
282
(1 row)
 
283
 
 
284
SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] is of (varchar[]) as "TRUE";
 
285
 TRUE 
 
286
------
 
287
 t
 
288
(1 row)
 
289
 
 
290
SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}";
 
291
 {{{{{{a,bb,ccc}}}}}} 
 
292
----------------------
 
293
 {{{{{{a,bb,ccc}}}}}}
 
294
(1 row)
 
295
 
 
296
-- scalar op any/all (array)
 
297
select 33 = any ('{1,2,3}');
 
298
 ?column? 
 
299
----------
 
300
 f
 
301
(1 row)
 
302
 
 
303
select 33 = any ('{1,2,33}');
 
304
 ?column? 
 
305
----------
 
306
 t
 
307
(1 row)
 
308
 
 
309
select 33 = all ('{1,2,33}');
 
310
 ?column? 
 
311
----------
 
312
 f
 
313
(1 row)
 
314
 
 
315
select 33 >= all ('{1,2,33}');
 
316
 ?column? 
 
317
----------
 
318
 t
 
319
(1 row)
 
320
 
 
321
-- boundary cases
 
322
select null::int >= all ('{1,2,33}');
 
323
 ?column? 
 
324
----------
 
325
 
 
326
(1 row)
 
327
 
 
328
select null::int >= all ('{}');
 
329
 ?column? 
 
330
----------
 
331
 t
 
332
(1 row)
 
333
 
 
334
select null::int >= any ('{}');
 
335
 ?column? 
 
336
----------
 
337
 f
 
338
(1 row)
 
339
 
 
340
-- cross-datatype
 
341
select 33.4 = any (array[1,2,3]);
 
342
 ?column? 
 
343
----------
 
344
 f
 
345
(1 row)
 
346
 
 
347
select 33.4 > all (array[1,2,3]);
 
348
 ?column? 
 
349
----------
 
350
 t
 
351
(1 row)
 
352
 
 
353
-- errors
 
354
select 33 * any ('{1,2,3}');
 
355
ERROR:  op ANY/ALL (array) requires operator to yield boolean
 
356
select 33 * any (44);
 
357
ERROR:  op ANY/ALL (array) requires array on right side
 
358
-- test indexes on arrays
 
359
create temp table arr_tbl (f1 int[] unique);
 
360
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "arr_tbl_f1_key" for table "arr_tbl"
 
361
insert into arr_tbl values ('{1,2,3}');
 
362
insert into arr_tbl values ('{1,2}');
 
363
-- failure expected:
 
364
insert into arr_tbl values ('{1,2,3}');
 
365
ERROR:  duplicate key violates unique constraint "arr_tbl_f1_key"
 
366
insert into arr_tbl values ('{2,3,4}');
 
367
insert into arr_tbl values ('{1,5,3}');
 
368
insert into arr_tbl values ('{1,2,10}');
 
369
set enable_seqscan to off;
 
370
select * from arr_tbl where f1 > '{1,2,3}' and f1 <= '{1,5,3}';
 
371
    f1    
 
372
----------
 
373
 {1,2,10}
 
374
 {1,5,3}
 
375
(2 rows)
 
376
 
 
377
-- note: if above select doesn't produce the expected tuple order,
 
378
-- then you didn't get an indexscan plan, and something is busted.
 
379
-- test [not] (like|ilike) (any|all) (...)
 
380
select 'foo' like any (array['%a', '%o']); -- t
 
381
 ?column? 
 
382
----------
 
383
 t
 
384
(1 row)
 
385
 
 
386
select 'foo' like any (array['%a', '%b']); -- f
 
387
 ?column? 
 
388
----------
 
389
 f
 
390
(1 row)
 
391
 
 
392
select 'foo' like all (array['f%', '%o']); -- t
 
393
 ?column? 
 
394
----------
 
395
 t
 
396
(1 row)
 
397
 
 
398
select 'foo' like all (array['f%', '%b']); -- f
 
399
 ?column? 
 
400
----------
 
401
 f
 
402
(1 row)
 
403
 
 
404
select 'foo' not like any (array['%a', '%b']); -- t
 
405
 ?column? 
 
406
----------
 
407
 t
 
408
(1 row)
 
409
 
 
410
select 'foo' not like all (array['%a', '%o']); -- f
 
411
 ?column? 
 
412
----------
 
413
 f
 
414
(1 row)
 
415
 
 
416
select 'foo' ilike any (array['%A', '%O']); -- t
 
417
 ?column? 
 
418
----------
 
419
 t
 
420
(1 row)
 
421
 
 
422
select 'foo' ilike all (array['F%', '%O']); -- t
 
423
 ?column? 
 
424
----------
 
425
 t
 
426
(1 row)
 
427
 
 
428
--
 
429
-- General array parser tests
 
430
--
 
431
-- none of the following should be accepted
 
432
select '{{1,{2}},{2,3}}'::text[];
 
433
ERROR:  malformed array literal: "{{1,{2}},{2,3}}"
 
434
select '{{},{}}'::text[];
 
435
ERROR:  malformed array literal: "{{},{}}"
 
436
select '{{1,2},\\{2,3}}'::text[];
 
437
ERROR:  malformed array literal: "{{1,2},\{2,3}}"
 
438
select '{{"1 2" x},{3}}'::text[];
 
439
ERROR:  malformed array literal: "{{"1 2" x},{3}}"
 
440
select '{}}'::text[];
 
441
ERROR:  malformed array literal: "{}}"
 
442
select '{ }}'::text[];
 
443
ERROR:  malformed array literal: "{ }}"
 
444
-- none of the above should be accepted
 
445
-- all of the following should be accepted
 
446
select '{}'::text[];
 
447
 text 
 
448
------
 
449
 {}
 
450
(1 row)
 
451
 
 
452
select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
 
453
                     text                      
 
454
-----------------------------------------------
 
455
 {{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}
 
456
(1 row)
 
457
 
 
458
select '{0 second  ,0 second}'::interval[];
 
459
   interval    
 
460
---------------
 
461
 {"@ 0","@ 0"}
 
462
(1 row)
 
463
 
 
464
select '{ { "," } , { 3 } }'::text[];
 
465
    text     
 
466
-------------
 
467
 {{","},{3}}
 
468
(1 row)
 
469
 
 
470
select '  {   {  "  0 second  "   ,  0 second  }   }'::text[];
 
471
             text              
 
472
-------------------------------
 
473
 {{"  0 second  ","0 second"}}
 
474
(1 row)
 
475
 
 
476
select '{
 
477
           0 second,
 
478
           @ 1 hour @ 42 minutes @ 20 seconds
 
479
         }'::interval[];
 
480
              interval              
 
481
------------------------------------
 
482
 {"@ 0","@ 1 hour 42 mins 20 secs"}
 
483
(1 row)
 
484
 
 
485
-- all of the above should be accepted