]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/enum.out
Remove useless whitespace at end of lines
[postgresql] / src / test / regress / expected / enum.out
1 --
2 -- Enum tests
3 --
4 CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');
5 --
6 -- Did it create the right number of rows?
7 --
8 SELECT COUNT(*) FROM pg_enum WHERE enumtypid = 'rainbow'::regtype;
9  count 
10 -------
11      6
12 (1 row)
13
14 --
15 -- I/O functions
16 --
17 SELECT 'red'::rainbow;
18  rainbow 
19 ---------
20  red
21 (1 row)
22
23 SELECT 'mauve'::rainbow;
24 ERROR:  invalid input value for enum rainbow: "mauve"
25 LINE 1: SELECT 'mauve'::rainbow;
26                ^
27 --
28 -- adding new values
29 --
30 CREATE TYPE planets AS ENUM ( 'venus', 'earth', 'mars' );
31 SELECT enumlabel, enumsortorder
32 FROM pg_enum
33 WHERE enumtypid = 'planets'::regtype
34 ORDER BY 2;
35  enumlabel | enumsortorder 
36 -----------+---------------
37  venus     |             1
38  earth     |             2
39  mars      |             3
40 (3 rows)
41
42 ALTER TYPE planets ADD VALUE 'uranus';
43 SELECT enumlabel, enumsortorder
44 FROM pg_enum
45 WHERE enumtypid = 'planets'::regtype
46 ORDER BY 2;
47  enumlabel | enumsortorder 
48 -----------+---------------
49  venus     |             1
50  earth     |             2
51  mars      |             3
52  uranus    |             4
53 (4 rows)
54
55 ALTER TYPE planets ADD VALUE 'mercury' BEFORE 'venus';
56 ALTER TYPE planets ADD VALUE 'saturn' BEFORE 'uranus';
57 ALTER TYPE planets ADD VALUE 'jupiter' AFTER 'mars';
58 ALTER TYPE planets ADD VALUE 'neptune' AFTER 'uranus';
59 SELECT enumlabel, enumsortorder
60 FROM pg_enum
61 WHERE enumtypid = 'planets'::regtype
62 ORDER BY 2;
63  enumlabel | enumsortorder 
64 -----------+---------------
65  mercury   |             0
66  venus     |             1
67  earth     |             2
68  mars      |             3
69  jupiter   |          3.25
70  saturn    |           3.5
71  uranus    |             4
72  neptune   |             5
73 (8 rows)
74
75 SELECT enumlabel, enumsortorder
76 FROM pg_enum
77 WHERE enumtypid = 'planets'::regtype
78 ORDER BY enumlabel::planets;
79  enumlabel | enumsortorder 
80 -----------+---------------
81  mercury   |             0
82  venus     |             1
83  earth     |             2
84  mars      |             3
85  jupiter   |          3.25
86  saturn    |           3.5
87  uranus    |             4
88  neptune   |             5
89 (8 rows)
90
91 -- errors for adding labels
92 ALTER TYPE planets ADD VALUE
93   'plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto';
94 ERROR:  invalid enum label "plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto"
95 DETAIL:  Labels must be 63 characters or less.
96 ALTER TYPE planets ADD VALUE 'pluto' AFTER 'zeus';
97 ERROR:  "zeus" is not an existing enum label
98 --
99 -- Test inserting so many values that we have to renumber
100 --
101 create type insenum as enum ('L1', 'L2');
102 alter type insenum add value 'i1' before 'L2';
103 alter type insenum add value 'i2' before 'L2';
104 alter type insenum add value 'i3' before 'L2';
105 alter type insenum add value 'i4' before 'L2';
106 alter type insenum add value 'i5' before 'L2';
107 alter type insenum add value 'i6' before 'L2';
108 alter type insenum add value 'i7' before 'L2';
109 alter type insenum add value 'i8' before 'L2';
110 alter type insenum add value 'i9' before 'L2';
111 alter type insenum add value 'i10' before 'L2';
112 alter type insenum add value 'i11' before 'L2';
113 alter type insenum add value 'i12' before 'L2';
114 alter type insenum add value 'i13' before 'L2';
115 alter type insenum add value 'i14' before 'L2';
116 alter type insenum add value 'i15' before 'L2';
117 alter type insenum add value 'i16' before 'L2';
118 alter type insenum add value 'i17' before 'L2';
119 alter type insenum add value 'i18' before 'L2';
120 alter type insenum add value 'i19' before 'L2';
121 alter type insenum add value 'i20' before 'L2';
122 alter type insenum add value 'i21' before 'L2';
123 alter type insenum add value 'i22' before 'L2';
124 alter type insenum add value 'i23' before 'L2';
125 alter type insenum add value 'i24' before 'L2';
126 alter type insenum add value 'i25' before 'L2';
127 alter type insenum add value 'i26' before 'L2';
128 alter type insenum add value 'i27' before 'L2';
129 alter type insenum add value 'i28' before 'L2';
130 alter type insenum add value 'i29' before 'L2';
131 alter type insenum add value 'i30' before 'L2';
132 -- The exact values of enumsortorder will now depend on the local properties
133 -- of float4, but in any reasonable implementation we should get at least
134 -- 20 splits before having to renumber; so only hide values > 20.
135 SELECT enumlabel,
136        case when enumsortorder > 20 then null else enumsortorder end as so
137 FROM pg_enum
138 WHERE enumtypid = 'insenum'::regtype
139 ORDER BY enumsortorder;
140  enumlabel | so 
141 -----------+----
142  L1        |  1
143  i1        |  2
144  i2        |  3
145  i3        |  4
146  i4        |  5
147  i5        |  6
148  i6        |  7
149  i7        |  8
150  i8        |  9
151  i9        | 10
152  i10       | 11
153  i11       | 12
154  i12       | 13
155  i13       | 14
156  i14       | 15
157  i15       | 16
158  i16       | 17
159  i17       | 18
160  i18       | 19
161  i19       | 20
162  i20       |   
163  i21       |   
164  i22       |   
165  i23       |   
166  i24       |   
167  i25       |   
168  i26       |   
169  i27       |   
170  i28       |   
171  i29       |   
172  i30       |   
173  L2        |   
174 (32 rows)
175
176 --
177 -- Basic table creation, row selection
178 --
179 CREATE TABLE enumtest (col rainbow);
180 INSERT INTO enumtest values ('red'), ('orange'), ('yellow'), ('green');
181 COPY enumtest FROM stdin;
182 SELECT * FROM enumtest;
183   col   
184 --------
185  red
186  orange
187  yellow
188  green
189  blue
190  purple
191 (6 rows)
192
193 --
194 -- Operators, no index
195 --
196 SELECT * FROM enumtest WHERE col = 'orange';
197   col   
198 --------
199  orange
200 (1 row)
201
202 SELECT * FROM enumtest WHERE col <> 'orange' ORDER BY col;
203   col   
204 --------
205  red
206  yellow
207  green
208  blue
209  purple
210 (5 rows)
211
212 SELECT * FROM enumtest WHERE col > 'yellow' ORDER BY col;
213   col   
214 --------
215  green
216  blue
217  purple
218 (3 rows)
219
220 SELECT * FROM enumtest WHERE col >= 'yellow' ORDER BY col;
221   col   
222 --------
223  yellow
224  green
225  blue
226  purple
227 (4 rows)
228
229 SELECT * FROM enumtest WHERE col < 'green' ORDER BY col;
230   col   
231 --------
232  red
233  orange
234  yellow
235 (3 rows)
236
237 SELECT * FROM enumtest WHERE col <= 'green' ORDER BY col;
238   col   
239 --------
240  red
241  orange
242  yellow
243  green
244 (4 rows)
245
246 --
247 -- Cast to/from text
248 --
249 SELECT 'red'::rainbow::text || 'hithere';
250   ?column?  
251 ------------
252  redhithere
253 (1 row)
254
255 SELECT 'red'::text::rainbow = 'red'::rainbow;
256  ?column? 
257 ----------
258  t
259 (1 row)
260
261 --
262 -- Aggregates
263 --
264 SELECT min(col) FROM enumtest;
265  min 
266 -----
267  red
268 (1 row)
269
270 SELECT max(col) FROM enumtest;
271   max   
272 --------
273  purple
274 (1 row)
275
276 SELECT max(col) FROM enumtest WHERE col < 'green';
277   max   
278 --------
279  yellow
280 (1 row)
281
282 --
283 -- Index tests, force use of index
284 --
285 SET enable_seqscan = off;
286 SET enable_bitmapscan = off;
287 --
288 -- Btree index / opclass with the various operators
289 --
290 CREATE UNIQUE INDEX enumtest_btree ON enumtest USING btree (col);
291 SELECT * FROM enumtest WHERE col = 'orange';
292   col   
293 --------
294  orange
295 (1 row)
296
297 SELECT * FROM enumtest WHERE col <> 'orange' ORDER BY col;
298   col   
299 --------
300  red
301  yellow
302  green
303  blue
304  purple
305 (5 rows)
306
307 SELECT * FROM enumtest WHERE col > 'yellow' ORDER BY col;
308   col   
309 --------
310  green
311  blue
312  purple
313 (3 rows)
314
315 SELECT * FROM enumtest WHERE col >= 'yellow' ORDER BY col;
316   col   
317 --------
318  yellow
319  green
320  blue
321  purple
322 (4 rows)
323
324 SELECT * FROM enumtest WHERE col < 'green' ORDER BY col;
325   col   
326 --------
327  red
328  orange
329  yellow
330 (3 rows)
331
332 SELECT * FROM enumtest WHERE col <= 'green' ORDER BY col;
333   col   
334 --------
335  red
336  orange
337  yellow
338  green
339 (4 rows)
340
341 SELECT min(col) FROM enumtest;
342  min 
343 -----
344  red
345 (1 row)
346
347 SELECT max(col) FROM enumtest;
348   max   
349 --------
350  purple
351 (1 row)
352
353 SELECT max(col) FROM enumtest WHERE col < 'green';
354   max   
355 --------
356  yellow
357 (1 row)
358
359 DROP INDEX enumtest_btree;
360 --
361 -- Hash index / opclass with the = operator
362 --
363 CREATE INDEX enumtest_hash ON enumtest USING hash (col);
364 SELECT * FROM enumtest WHERE col = 'orange';
365   col   
366 --------
367  orange
368 (1 row)
369
370 DROP INDEX enumtest_hash;
371 --
372 -- End index tests
373 --
374 RESET enable_seqscan;
375 RESET enable_bitmapscan;
376 --
377 -- Domains over enums
378 --
379 CREATE DOMAIN rgb AS rainbow CHECK (VALUE IN ('red', 'green', 'blue'));
380 SELECT 'red'::rgb;
381  rgb 
382 -----
383  red
384 (1 row)
385
386 SELECT 'purple'::rgb;
387 ERROR:  value for domain rgb violates check constraint "rgb_check"
388 SELECT 'purple'::rainbow::rgb;
389 ERROR:  value for domain rgb violates check constraint "rgb_check"
390 DROP DOMAIN rgb;
391 --
392 -- Arrays
393 --
394 SELECT '{red,green,blue}'::rainbow[];
395      rainbow      
396 ------------------
397  {red,green,blue}
398 (1 row)
399
400 SELECT ('{red,green,blue}'::rainbow[])[2];
401  rainbow 
402 ---------
403  green
404 (1 row)
405
406 SELECT 'red' = ANY ('{red,green,blue}'::rainbow[]);
407  ?column? 
408 ----------
409  t
410 (1 row)
411
412 SELECT 'yellow' = ANY ('{red,green,blue}'::rainbow[]);
413  ?column? 
414 ----------
415  f
416 (1 row)
417
418 SELECT 'red' = ALL ('{red,green,blue}'::rainbow[]);
419  ?column? 
420 ----------
421  f
422 (1 row)
423
424 SELECT 'red' = ALL ('{red,red}'::rainbow[]);
425  ?column? 
426 ----------
427  t
428 (1 row)
429
430 --
431 -- Support functions
432 --
433 SELECT enum_first(NULL::rainbow);
434  enum_first 
435 ------------
436  red
437 (1 row)
438
439 SELECT enum_last('green'::rainbow);
440  enum_last 
441 -----------
442  purple
443 (1 row)
444
445 SELECT enum_range(NULL::rainbow);
446               enum_range               
447 ---------------------------------------
448  {red,orange,yellow,green,blue,purple}
449 (1 row)
450
451 SELECT enum_range('orange'::rainbow, 'green'::rainbow);
452       enum_range       
453 -----------------------
454  {orange,yellow,green}
455 (1 row)
456
457 SELECT enum_range(NULL, 'green'::rainbow);
458         enum_range         
459 ---------------------------
460  {red,orange,yellow,green}
461 (1 row)
462
463 SELECT enum_range('orange'::rainbow, NULL);
464             enum_range             
465 -----------------------------------
466  {orange,yellow,green,blue,purple}
467 (1 row)
468
469 SELECT enum_range(NULL::rainbow, NULL);
470               enum_range               
471 ---------------------------------------
472  {red,orange,yellow,green,blue,purple}
473 (1 row)
474
475 --
476 -- User functions, can't test perl/python etc here since may not be compiled.
477 --
478 CREATE FUNCTION echo_me(anyenum) RETURNS text AS $$
479 BEGIN
480 RETURN $1::text || 'omg';
481 END
482 $$ LANGUAGE plpgsql;
483 SELECT echo_me('red'::rainbow);
484  echo_me 
485 ---------
486  redomg
487 (1 row)
488
489 --
490 -- Concrete function should override generic one
491 --
492 CREATE FUNCTION echo_me(rainbow) RETURNS text AS $$
493 BEGIN
494 RETURN $1::text || 'wtf';
495 END
496 $$ LANGUAGE plpgsql;
497 SELECT echo_me('red'::rainbow);
498  echo_me 
499 ---------
500  redwtf
501 (1 row)
502
503 --
504 -- If we drop the original generic one, we don't have to qualify the type
505 -- anymore, since there's only one match
506 --
507 DROP FUNCTION echo_me(anyenum);
508 SELECT echo_me('red');
509  echo_me 
510 ---------
511  redwtf
512 (1 row)
513
514 DROP FUNCTION echo_me(rainbow);
515 --
516 -- RI triggers on enum types
517 --
518 CREATE TABLE enumtest_parent (id rainbow PRIMARY KEY);
519 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "enumtest_parent_pkey" for table "enumtest_parent"
520 CREATE TABLE enumtest_child (parent rainbow REFERENCES enumtest_parent);
521 INSERT INTO enumtest_parent VALUES ('red');
522 INSERT INTO enumtest_child VALUES ('red');
523 INSERT INTO enumtest_child VALUES ('blue');  -- fail
524 ERROR:  insert or update on table "enumtest_child" violates foreign key constraint "enumtest_child_parent_fkey"
525 DETAIL:  Key (parent)=(blue) is not present in table "enumtest_parent".
526 DELETE FROM enumtest_parent;  -- fail
527 ERROR:  update or delete on table "enumtest_parent" violates foreign key constraint "enumtest_child_parent_fkey" on table "enumtest_child"
528 DETAIL:  Key (id)=(red) is still referenced from table "enumtest_child".
529 --
530 -- cross-type RI should fail
531 --
532 CREATE TYPE bogus AS ENUM('good', 'bad', 'ugly');
533 CREATE TABLE enumtest_bogus_child(parent bogus REFERENCES enumtest_parent);
534 ERROR:  foreign key constraint "enumtest_bogus_child_parent_fkey" cannot be implemented
535 DETAIL:  Key columns "parent" and "id" are of incompatible types: bogus and rainbow.
536 DROP TYPE bogus;
537 --
538 -- Cleanup
539 --
540 DROP TABLE enumtest_child;
541 DROP TABLE enumtest_parent;
542 DROP TABLE enumtest;
543 DROP TYPE rainbow;
544 --
545 -- Verify properly cleaned up
546 --
547 SELECT COUNT(*) FROM pg_type WHERE typname = 'rainbow';
548  count 
549 -------
550      0
551 (1 row)
552
553 SELECT * FROM pg_enum WHERE NOT EXISTS
554   (SELECT 1 FROM pg_type WHERE pg_type.oid = enumtypid);
555  enumtypid | enumsortorder | enumlabel 
556 -----------+---------------+-----------
557 (0 rows)
558