summaryrefslogtreecommitdiff
path: root/src/test/regress/expected/enum.out
blob: 01159688e57cad99f2c4d44617661b30ffbd14a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
--
-- Enum tests
--
CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');
--
-- Did it create the right number of rows?
--
SELECT COUNT(*) FROM pg_enum WHERE enumtypid = 'rainbow'::regtype;
 count 
-------
     6
(1 row)

--
-- I/O functions
--
SELECT 'red'::rainbow;
 rainbow 
---------
 red
(1 row)

SELECT 'mauve'::rainbow;
ERROR:  invalid input value for enum rainbow: "mauve"
LINE 1: SELECT 'mauve'::rainbow;
               ^
-- Also try it with non-error-throwing API
SELECT pg_input_is_valid('red', 'rainbow');
 pg_input_is_valid 
-------------------
 t
(1 row)

SELECT pg_input_is_valid('mauve', 'rainbow');
 pg_input_is_valid 
-------------------
 f
(1 row)

SELECT * FROM pg_input_error_info('mauve', 'rainbow');
                    message                    | detail | hint | sql_error_code 
-----------------------------------------------+--------+------+----------------
 invalid input value for enum rainbow: "mauve" |        |      | 22P02
(1 row)

\x
SELECT * FROM pg_input_error_info(repeat('too_long', 32), 'rainbow');
-[ RECORD 1 ]--+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
message        | invalid input value for enum rainbow: "too_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_long"
detail         | 
hint           | 
sql_error_code | 22P02

\x
--
-- adding new values
--
CREATE TYPE planets AS ENUM ( 'venus', 'earth', 'mars' );
SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'planets'::regtype
ORDER BY 2;
 enumlabel | enumsortorder 
-----------+---------------
 venus     |             1
 earth     |             2
 mars      |             3
(3 rows)

ALTER TYPE planets ADD VALUE 'uranus';
SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'planets'::regtype
ORDER BY 2;
 enumlabel | enumsortorder 
-----------+---------------
 venus     |             1
 earth     |             2
 mars      |             3
 uranus    |             4
(4 rows)

ALTER TYPE planets ADD VALUE 'mercury' BEFORE 'venus';
ALTER TYPE planets ADD VALUE 'saturn' BEFORE 'uranus';
ALTER TYPE planets ADD VALUE 'jupiter' AFTER 'mars';
ALTER TYPE planets ADD VALUE 'neptune' AFTER 'uranus';
SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'planets'::regtype
ORDER BY 2;
 enumlabel | enumsortorder 
-----------+---------------
 mercury   |             0
 venus     |             1
 earth     |             2
 mars      |             3
 jupiter   |          3.25
 saturn    |           3.5
 uranus    |             4
 neptune   |             5
(8 rows)

SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'planets'::regtype
ORDER BY enumlabel::planets;
 enumlabel | enumsortorder 
-----------+---------------
 mercury   |             0
 venus     |             1
 earth     |             2
 mars      |             3
 jupiter   |          3.25
 saturn    |           3.5
 uranus    |             4
 neptune   |             5
(8 rows)

-- errors for adding labels
ALTER TYPE planets ADD VALUE
  'plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto';
ERROR:  invalid enum label "plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto"
DETAIL:  Labels must be 63 bytes or less.
ALTER TYPE planets ADD VALUE 'pluto' AFTER 'zeus';
ERROR:  "zeus" is not an existing enum label
-- if not exists tests
--  existing value gives error
ALTER TYPE planets ADD VALUE 'mercury';
ERROR:  enum label "mercury" already exists
-- unless IF NOT EXISTS is specified
ALTER TYPE planets ADD VALUE IF NOT EXISTS 'mercury';
NOTICE:  enum label "mercury" already exists, skipping
-- should be neptune, not mercury
SELECT enum_last(NULL::planets);
 enum_last 
-----------
 neptune
(1 row)

ALTER TYPE planets ADD VALUE IF NOT EXISTS 'pluto';
-- should be pluto, i.e. the new value
SELECT enum_last(NULL::planets);
 enum_last 
-----------
 pluto
(1 row)

--
-- Test inserting so many values that we have to renumber
--
create type insenum as enum ('L1', 'L2');
alter type insenum add value 'i1' before 'L2';
alter type insenum add value 'i2' before 'L2';
alter type insenum add value 'i3' before 'L2';
alter type insenum add value 'i4' before 'L2';
alter type insenum add value 'i5' before 'L2';
alter type insenum add value 'i6' before 'L2';
alter type insenum add value 'i7' before 'L2';
alter type insenum add value 'i8' before 'L2';
alter type insenum add value 'i9' before 'L2';
alter type insenum add value 'i10' before 'L2';
alter type insenum add value 'i11' before 'L2';
alter type insenum add value 'i12' before 'L2';
alter type insenum add value 'i13' before 'L2';
alter type insenum add value 'i14' before 'L2';
alter type insenum add value 'i15' before 'L2';
alter type insenum add value 'i16' before 'L2';
alter type insenum add value 'i17' before 'L2';
alter type insenum add value 'i18' before 'L2';
alter type insenum add value 'i19' before 'L2';
alter type insenum add value 'i20' before 'L2';
alter type insenum add value 'i21' before 'L2';
alter type insenum add value 'i22' before 'L2';
alter type insenum add value 'i23' before 'L2';
alter type insenum add value 'i24' before 'L2';
alter type insenum add value 'i25' before 'L2';
alter type insenum add value 'i26' before 'L2';
alter type insenum add value 'i27' before 'L2';
alter type insenum add value 'i28' before 'L2';
alter type insenum add value 'i29' before 'L2';
alter type insenum add value 'i30' before 'L2';
-- The exact values of enumsortorder will now depend on the local properties
-- of float4, but in any reasonable implementation we should get at least
-- 20 splits before having to renumber; so only hide values > 20.
SELECT enumlabel,
       case when enumsortorder > 20 then null else enumsortorder end as so
FROM pg_enum
WHERE enumtypid = 'insenum'::regtype
ORDER BY enumsortorder;
 enumlabel | so 
-----------+----
 L1        |  1
 i1        |  2
 i2        |  3
 i3        |  4
 i4        |  5
 i5        |  6
 i6        |  7
 i7        |  8
 i8        |  9
 i9        | 10
 i10       | 11
 i11       | 12
 i12       | 13
 i13       | 14
 i14       | 15
 i15       | 16
 i16       | 17
 i17       | 18
 i18       | 19
 i19       | 20
 i20       |   
 i21       |   
 i22       |   
 i23       |   
 i24       |   
 i25       |   
 i26       |   
 i27       |   
 i28       |   
 i29       |   
 i30       |   
 L2        |   
(32 rows)

--
-- Basic table creation, row selection
--
CREATE TABLE enumtest (col rainbow);
INSERT INTO enumtest values ('red'), ('orange'), ('yellow'), ('green');
COPY enumtest FROM stdin;
SELECT * FROM enumtest;
  col   
--------
 red
 orange
 yellow
 green
 blue
 purple
(6 rows)

--
-- Operators, no index
--
SELECT * FROM enumtest WHERE col = 'orange';
  col   
--------
 orange
(1 row)

SELECT * FROM enumtest WHERE col <> 'orange' ORDER BY col;
  col   
--------
 red
 yellow
 green
 blue
 purple
(5 rows)

SELECT * FROM enumtest WHERE col > 'yellow' ORDER BY col;
  col   
--------
 green
 blue
 purple
(3 rows)

SELECT * FROM enumtest WHERE col >= 'yellow' ORDER BY col;
  col   
--------
 yellow
 green
 blue
 purple
(4 rows)

SELECT * FROM enumtest WHERE col < 'green' ORDER BY col;
  col   
--------
 red
 orange
 yellow
(3 rows)

SELECT * FROM enumtest WHERE col <= 'green' ORDER BY col;
  col   
--------
 red
 orange
 yellow
 green
(4 rows)

--
-- Cast to/from text
--
SELECT 'red'::rainbow::text || 'hithere';
  ?column?  
------------
 redhithere
(1 row)

SELECT 'red'::text::rainbow = 'red'::rainbow;
 ?column? 
----------
 t
(1 row)

--
-- Aggregates
--
SELECT min(col) FROM enumtest;
 min 
-----
 red
(1 row)

SELECT max(col) FROM enumtest;
  max   
--------
 purple
(1 row)

SELECT max(col) FROM enumtest WHERE col < 'green';
  max   
--------
 yellow
(1 row)

--
-- Index tests, force use of index
--
SET enable_seqscan = off;
SET enable_bitmapscan = off;
--
-- Btree index / opclass with the various operators
--
CREATE UNIQUE INDEX enumtest_btree ON enumtest USING btree (col);
SELECT * FROM enumtest WHERE col = 'orange';
  col   
--------
 orange
(1 row)

SELECT * FROM enumtest WHERE col <> 'orange' ORDER BY col;
  col   
--------
 red
 yellow
 green
 blue
 purple
(5 rows)

SELECT * FROM enumtest WHERE col > 'yellow' ORDER BY col;
  col   
--------
 green
 blue
 purple
(3 rows)

SELECT * FROM enumtest WHERE col >= 'yellow' ORDER BY col;
  col   
--------
 yellow
 green
 blue
 purple
(4 rows)

SELECT * FROM enumtest WHERE col < 'green' ORDER BY col;
  col   
--------
 red
 orange
 yellow
(3 rows)

SELECT * FROM enumtest WHERE col <= 'green' ORDER BY col;
  col   
--------
 red
 orange
 yellow
 green
(4 rows)

SELECT min(col) FROM enumtest;
 min 
-----
 red
(1 row)

SELECT max(col) FROM enumtest;
  max   
--------
 purple
(1 row)

SELECT max(col) FROM enumtest WHERE col < 'green';
  max   
--------
 yellow
(1 row)

DROP INDEX enumtest_btree;
--
-- Hash index / opclass with the = operator
--
CREATE INDEX enumtest_hash ON enumtest USING hash (col);
SELECT * FROM enumtest WHERE col = 'orange';
  col   
--------
 orange
(1 row)

DROP INDEX enumtest_hash;
--
-- End index tests
--
RESET enable_seqscan;
RESET enable_bitmapscan;
--
-- Domains over enums
--
CREATE DOMAIN rgb AS rainbow CHECK (VALUE IN ('red', 'green', 'blue'));
SELECT 'red'::rgb;
 rgb 
-----
 red
(1 row)

SELECT 'purple'::rgb;
ERROR:  value for domain rgb violates check constraint "rgb_check"
SELECT 'purple'::rainbow::rgb;
ERROR:  value for domain rgb violates check constraint "rgb_check"
DROP DOMAIN rgb;
--
-- Arrays
--
SELECT '{red,green,blue}'::rainbow[];
     rainbow      
------------------
 {red,green,blue}
(1 row)

SELECT ('{red,green,blue}'::rainbow[])[2];
 rainbow 
---------
 green
(1 row)

SELECT 'red' = ANY ('{red,green,blue}'::rainbow[]);
 ?column? 
----------
 t
(1 row)

SELECT 'yellow' = ANY ('{red,green,blue}'::rainbow[]);
 ?column? 
----------
 f
(1 row)

SELECT 'red' = ALL ('{red,green,blue}'::rainbow[]);
 ?column? 
----------
 f
(1 row)

SELECT 'red' = ALL ('{red,red}'::rainbow[]);
 ?column? 
----------
 t
(1 row)

--
-- Support functions
--
SELECT enum_first(NULL::rainbow);
 enum_first 
------------
 red
(1 row)

SELECT enum_last('green'::rainbow);
 enum_last 
-----------
 purple
(1 row)

SELECT enum_range(NULL::rainbow);
              enum_range               
---------------------------------------
 {red,orange,yellow,green,blue,purple}
(1 row)

SELECT enum_range('orange'::rainbow, 'green'::rainbow);
      enum_range       
-----------------------
 {orange,yellow,green}
(1 row)

SELECT enum_range(NULL, 'green'::rainbow);
        enum_range         
---------------------------
 {red,orange,yellow,green}
(1 row)

SELECT enum_range('orange'::rainbow, NULL);
            enum_range             
-----------------------------------
 {orange,yellow,green,blue,purple}
(1 row)

SELECT enum_range(NULL::rainbow, NULL);
              enum_range               
---------------------------------------
 {red,orange,yellow,green,blue,purple}
(1 row)

--
-- User functions, can't test perl/python etc here since may not be compiled.
--
CREATE FUNCTION echo_me(anyenum) RETURNS text AS $$
BEGIN
RETURN $1::text || 'omg';
END
$$ LANGUAGE plpgsql;
SELECT echo_me('red'::rainbow);
 echo_me 
---------
 redomg
(1 row)

--
-- Concrete function should override generic one
--
CREATE FUNCTION echo_me(rainbow) RETURNS text AS $$
BEGIN
RETURN $1::text || 'wtf';
END
$$ LANGUAGE plpgsql;
SELECT echo_me('red'::rainbow);
 echo_me 
---------
 redwtf
(1 row)

--
-- If we drop the original generic one, we don't have to qualify the type
-- anymore, since there's only one match
--
DROP FUNCTION echo_me(anyenum);
SELECT echo_me('red');
 echo_me 
---------
 redwtf
(1 row)

DROP FUNCTION echo_me(rainbow);
--
-- RI triggers on enum types
--
CREATE TABLE enumtest_parent (id rainbow PRIMARY KEY);
CREATE TABLE enumtest_child (parent rainbow REFERENCES enumtest_parent);
INSERT INTO enumtest_parent VALUES ('red');
INSERT INTO enumtest_child VALUES ('red');
INSERT INTO enumtest_child VALUES ('blue');  -- fail
ERROR:  insert or update on table "enumtest_child" violates foreign key constraint "enumtest_child_parent_fkey"
DETAIL:  Key (parent)=(blue) is not present in table "enumtest_parent".
DELETE FROM enumtest_parent;  -- fail
ERROR:  update or delete on table "enumtest_parent" violates foreign key constraint "enumtest_child_parent_fkey" on table "enumtest_child"
DETAIL:  Key (id)=(red) is still referenced from table "enumtest_child".
--
-- cross-type RI should fail
--
CREATE TYPE bogus AS ENUM('good', 'bad', 'ugly');
CREATE TABLE enumtest_bogus_child(parent bogus REFERENCES enumtest_parent);
ERROR:  foreign key constraint "enumtest_bogus_child_parent_fkey" cannot be implemented
DETAIL:  Key columns "parent" and "id" are of incompatible types: bogus and rainbow.
DROP TYPE bogus;
-- check renaming a value
ALTER TYPE rainbow RENAME VALUE 'red' TO 'crimson';
SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'rainbow'::regtype
ORDER BY 2;
 enumlabel | enumsortorder 
-----------+---------------
 crimson   |             1
 orange    |             2
 yellow    |             3
 green     |             4
 blue      |             5
 purple    |             6
(6 rows)

-- check that renaming a non-existent value fails
ALTER TYPE rainbow RENAME VALUE 'red' TO 'crimson';
ERROR:  "red" is not an existing enum label
-- check that renaming to an existent value fails
ALTER TYPE rainbow RENAME VALUE 'blue' TO 'green';
ERROR:  enum label "green" already exists
--
-- check transactional behaviour of ALTER TYPE ... ADD VALUE
--
CREATE TYPE bogus AS ENUM('good');
-- check that we can add new values to existing enums in a transaction
-- but we can't use them
BEGIN;
ALTER TYPE bogus ADD VALUE 'new';
SAVEPOINT x;
SELECT 'new'::bogus;  -- unsafe
ERROR:  unsafe use of new value "new" of enum type bogus
LINE 1: SELECT 'new'::bogus;
               ^
HINT:  New enum values must be committed before they can be used.
ROLLBACK TO x;
SELECT enum_first(null::bogus);  -- safe
 enum_first 
------------
 good
(1 row)

SELECT enum_last(null::bogus);  -- unsafe
ERROR:  unsafe use of new value "new" of enum type bogus
HINT:  New enum values must be committed before they can be used.
ROLLBACK TO x;
SELECT enum_range(null::bogus);  -- unsafe
ERROR:  unsafe use of new value "new" of enum type bogus
HINT:  New enum values must be committed before they can be used.
ROLLBACK TO x;
COMMIT;
SELECT 'new'::bogus;  -- now safe
 bogus 
-------
 new
(1 row)

SELECT enumlabel, enumsortorder
FROM pg_enum
WHERE enumtypid = 'bogus'::regtype
ORDER BY 2;
 enumlabel | enumsortorder 
-----------+---------------
 good      |             1
 new       |             2
(2 rows)

-- check that we recognize the case where the enum already existed but was
-- modified in the current txn; this should not be considered safe
BEGIN;
ALTER TYPE bogus RENAME TO bogon;
ALTER TYPE bogon ADD VALUE 'bad';
SELECT 'bad'::bogon;
ERROR:  unsafe use of new value "bad" of enum type bogon
LINE 1: SELECT 'bad'::bogon;
               ^
HINT:  New enum values must be committed before they can be used.
ROLLBACK;
-- but a renamed value is safe to use later in same transaction
BEGIN;
ALTER TYPE bogus RENAME VALUE 'good' to 'bad';
SELECT 'bad'::bogus;
 bogus 
-------
 bad
(1 row)

ROLLBACK;
DROP TYPE bogus;
-- check that values created during CREATE TYPE can be used in any case
BEGIN;
CREATE TYPE bogus AS ENUM('good','bad','ugly');
ALTER TYPE bogus RENAME TO bogon;
select enum_range(null::bogon);
   enum_range    
-----------------
 {good,bad,ugly}
(1 row)

ROLLBACK;
-- ideally, we'd allow this usage; but it requires keeping track of whether
-- the enum type was created in the current transaction, which is expensive
BEGIN;
CREATE TYPE bogus AS ENUM('good');
ALTER TYPE bogus RENAME TO bogon;
ALTER TYPE bogon ADD VALUE 'bad';
ALTER TYPE bogon ADD VALUE 'ugly';
select enum_range(null::bogon);  -- fails
ERROR:  unsafe use of new value "bad" of enum type bogon
HINT:  New enum values must be committed before they can be used.
ROLLBACK;
--
-- Cleanup
--
DROP TABLE enumtest_child;
DROP TABLE enumtest_parent;
DROP TABLE enumtest;
DROP TYPE rainbow;
--
-- Verify properly cleaned up
--
SELECT COUNT(*) FROM pg_type WHERE typname = 'rainbow';
 count 
-------
     0
(1 row)

SELECT * FROM pg_enum WHERE NOT EXISTS
  (SELECT 1 FROM pg_type WHERE pg_type.oid = enumtypid);
 oid | enumtypid | enumsortorder | enumlabel 
-----+-----------+---------------+-----------
(0 rows)