-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathenums
1326 lines (1087 loc) · 33.7 KB
/
enums
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# LogicTest: !3node-tenant(49854)
statement ok
CREATE TYPE t AS ENUM ()
statement error pq: relation "t" does not exist
SELECT * FROM t
statement error pq: type "t" already exists
CREATE TABLE t (x INT)
statement error pq: type "t" already exists
CREATE TYPE t AS ENUM ()
statement ok
CREATE TABLE torename (x INT)
statement error pq: type "t" already exists
ALTER TABLE torename RENAME TO t
statement ok
CREATE DATABASE db2;
CREATE TYPE db2.t AS ENUM ()
statement error pq: relation "db2.t" does not exist
SELECT * FROM db2.t
statement error pq: type "t" already exists
CREATE TYPE db2.t AS ENUM ()
# Regression for #48537. Dropping a table with a type name caused a panic.
statement error pq: relation "t" does not exist
DROP TABLE t
statement error pq: enum definition contains duplicate value "dup"
CREATE TYPE bad AS ENUM ('dup', 'dup')
# Duplicates with different casing count as different.
statement ok
CREATE TYPE notbad AS ENUM ('dup', 'DUP')
# Test that we can create types that shadow builtin type names,
# but in different schemas.
statement ok
CREATE TYPE int AS ENUM ('Z', 'S of int')
statement error pq: could not parse "Z" as type int
SELECT 'Z'::int
query T
SELECT 'Z'::public.int
----
Z
statement ok
CREATE TYPE greeting AS ENUM ('hello', 'howdy', 'hi')
# Test that we can only reference greeting with the right qualification.
statement error pq: type "pg_catalog.greeting" does not exist
SELECT 'hello'::pg_catalog.greeting
query T
SELECT 'hello'::public.greeting
----
hello
# Test some expression evaluation on enums.
# These test should live in TestEval, but it is difficult to adjust the
# test to handle creation of user defined types.
query TTT
SELECT 'hello'::greeting, 'howdy'::greeting, 'hi'::greeting
----
hello howdy hi
# Test type annotations.
query TTT
SELECT 'hello':::greeting, 'howdy':::greeting, 'hi':::greeting
----
hello howdy hi
statement error pq: invalid input value for enum greeting: "goodbye"
SELECT 'goodbye'::greeting
query T
SELECT 'hello'::greeting::string
----
hello
query BBBBBBBBBBB
SELECT 'hello'::greeting < 'howdy'::greeting,
'howdy'::greeting < 'hi',
'hi' > 'hello'::greeting,
'howdy'::greeting < 'hello'::greeting,
'hi'::greeting <= 'hi',
NULL < 'hello'::greeting,
'hi'::greeting < NULL,
'hello'::greeting = 'hello'::greeting,
'hello' != 'hi'::greeting,
'howdy'::greeting IS NOT DISTINCT FROM NULL,
'hello'::greeting IN ('hi'::greeting, 'howdy'::greeting, 'hello'::greeting)
----
true true true false true NULL NULL true true false true
statement ok
CREATE TYPE farewell AS ENUM ('bye', 'seeya')
statement error pq: invalid comparison between different enum types: <greeting> = <farewell>
SELECT 'hello'::greeting = 'bye'::farewell
statement error pq: invalid comparison between different enum types: <greeting> < <farewell>
SELECT 'hello'::greeting < 'bye'::farewell
statement error pq: invalid comparison between different enum types: <greeting> <= <farewell>
SELECT 'hello'::greeting <= 'bye'::farewell
query T
SELECT 'hello'::greeting::greeting
----
hello
statement ok
CREATE TYPE greeting2 AS ENUM ('hello')
statement error pq: invalid cast: greeting -> greeting2
SELECT 'hello'::greeting::greeting2
# Ensure that we can perform a limited form of implicit casts for
# the case of ENUM binary operations with strings.
query BB
SELECT 'hello'::greeting != 'howdy', 'hi' > 'hello'::greeting
----
true true
# Check that the implicit cast gives an appropriate error message
# when firing but unable to complete the type check.
statement error pq: invalid input value for enum greeting: "notagreeting"
SELECT 'hello'::greeting = 'notagreeting'
# Tests for enum builtins.
statement ok
CREATE TYPE dbs AS ENUM ('postgres', 'mysql', 'spanner', 'cockroach')
query TT
SELECT enum_first('mysql'::dbs), enum_last('spanner'::dbs)
----
postgres cockroach
query T
SELECT enum_range('cockroach'::dbs)
----
{postgres,mysql,spanner,cockroach}
query TT
SELECT enum_range(NULL, 'mysql'::dbs), enum_range('spanner'::dbs, NULL)
----
{postgres,mysql} {spanner,cockroach}
query TT
SELECT enum_range('postgres'::dbs, 'spanner'::dbs), enum_range('spanner'::dbs, 'cockroach'::dbs)
----
{postgres,mysql,spanner} {spanner,cockroach}
query T
SELECT enum_range('cockroach'::dbs, 'cockroach'::dbs)
----
{cockroach}
query T
SELECT enum_range('cockroach'::dbs, 'spanner'::dbs)
----
{}
query error pq: enum_range\(\): both arguments cannot be NULL
SELECT enum_range(NULL::dbs, NULL::dbs)
query error pq: enum_range\(\): mismatched types
SELECT enum_range('cockroach'::dbs, 'hello'::greeting)
# Test inserting and reading enum data from tables.
statement ok
CREATE TABLE greeting_table (x1 greeting, x2 greeting)
statement error pq: invalid input value for enum greeting: "bye"
INSERT INTO greeting_table VALUES ('bye', 'hi')
statement ok
INSERT INTO greeting_table VALUES ('hi', 'hello')
query TT
SELECT * FROM greeting_table
----
hi hello
query TT
SELECT 'hello'::greeting, x1 FROM greeting_table
----
hello hi
query TB
SELECT x1, x1 < 'hello' FROM greeting_table
----
hi false
query TT
SELECT x1, enum_first(x1) FROM greeting_table
----
hi hello
statement ok
CREATE TABLE t1 (x greeting, INDEX i (x));
CREATE TABLE t2 (x greeting, INDEX i (x));
INSERT INTO t1 VALUES ('hello');
INSERT INTO t2 VALUES ('hello')
query TT
SELECT * FROM t1 INNER LOOKUP JOIN t2 ON t1.x = t2.x
----
hello hello
query TT
SELECT * FROM t1 INNER HASH JOIN t2 ON t1.x = t2.x
----
hello hello
query TT
SELECT * FROM t1 INNER MERGE JOIN t2 ON t1.x = t2.x
----
hello hello
statement ok
INSERT INTO t2 VALUES ('hello'), ('hello'), ('howdy'), ('hi')
query T rowsort
SELECT DISTINCT x FROM t2
----
hello
howdy
hi
query T
SELECT DISTINCT x FROM t2 ORDER BY x DESC
----
hi
howdy
hello
# Test out some subqueries.
query T rowsort
SELECT x FROM t2 WHERE x > (SELECT x FROM t1 ORDER BY x LIMIT 1)
----
hi
howdy
# Test ordinality.
query TI
SELECT * FROM t2 WITH ORDINALITY ORDER BY x
----
hello 1
hello 2
hello 3
howdy 4
hi 5
# Test ordering with and without limits.
statement ok
INSERT INTO t1 VALUES ('hi'), ('hello'), ('howdy'), ('howdy'), ('howdy'), ('hello')
query T
SELECT x FROM t1 ORDER BY x DESC
----
hi
howdy
howdy
howdy
hello
hello
hello
query T
SELECT x FROM t1 ORDER BY x ASC
----
hello
hello
hello
howdy
howdy
howdy
hi
query T
SELECT x FROM t1 ORDER BY x ASC LIMIT 3
----
hello
hello
hello
query T
SELECT x FROM t1 ORDER BY x DESC LIMIT 3
----
hi
howdy
howdy
# Test we can group on enums.
query T rowsort
(SELECT * FROM t1) UNION (SELECT * FROM t2)
----
hello
howdy
hi
statement ok
CREATE TABLE enum_agg (x greeting, y INT);
INSERT INTO enum_agg VALUES
('hello', 1),
('hello', 3),
('howdy', 5),
('howdy', 0),
('howdy', 1),
('hi', 10)
query TIRI rowsort
SELECT x, max(y), sum(y), min(y) FROM enum_agg GROUP BY x
----
hello 3 4 1
howdy 5 6 0
hi 10 10 10
# Test aggregations on ENUM columns.
query TT
SELECT max(x), min(x) FROM enum_agg
----
hi hello
# Test that enums without any members can still get an aggregate
# resolved when distributing a flow.
statement ok
CREATE TYPE empty AS ENUM ();
CREATE TABLE empty_enum (x empty)
query TT
SELECT max(x), min(x) FROM empty_enum
----
NULL NULL
# Regression to ensure that statistics jobs can be run on tables
# with user defined types.
statement ok
CREATE TABLE greeting_stats (x greeting PRIMARY KEY);
INSERT INTO greeting_stats VALUES ('hi');
CREATE STATISTICS s FROM greeting_stats
query T
SELECT x FROM greeting_stats
----
hi
# Test that we can cast from bytes to enum.
# Use a singleton enum so that the bytes encoding is simple.
statement ok
CREATE TYPE as_bytes AS ENUM ('bytes')
query TT
SELECT b'\x80'::as_bytes, b'\x80':::as_bytes
----
bytes bytes
query error pq: could not find \[255\] in enum "public.as_bytes" representation
SELECT b'\xFF'::as_bytes
# Regression for #49300. Ensure that virtual tables have access to hydrated
# type descriptors.
query TT
SHOW CREATE t1
----
t1 CREATE TABLE public.t1 (
x public.greeting NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
INDEX i (x ASC),
FAMILY "primary" (x, rowid)
)
# SHOW CREATE uses a virtual index, so also check the code path where a
# descriptor scan is used.
query T
SELECT create_statement FROM crdb_internal.create_statements WHERE descriptor_name = 't1'
----
CREATE TABLE public.t1 (
x public.greeting NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
INDEX i (x ASC),
FAMILY "primary" (x, rowid)
)
# Test that the implicit array type has been created, and that we can use it.
query TT
SELECT ARRAY['hello']::_greeting, ARRAY['hello'::greeting]
----
{hello} {hello}
# Test that we can't mix enums in an array.
query error pq: expected 'cockroach'::public.dbs to be of type greeting, found type dbs
SELECT ARRAY['hello'::greeting, 'cockroach'::dbs]
statement ok
CREATE TABLE enum_array (x _greeting, y greeting[]);
INSERT INTO enum_array VALUES (ARRAY['hello'], ARRAY['hello']), (ARRAY['howdy'], ARRAY['howdy'])
query TT rowsort
SELECT * FROM enum_array
----
{hello} {hello}
{howdy} {howdy}
query TTT
SELECT pg_typeof(x), pg_typeof(x[1]), pg_typeof(ARRAY['hello']::_greeting) FROM enum_array LIMIT 1
----
greeting[] greeting greeting[]
# Ensure that the implicitly created array type will tolerate collisions.
# _collision will create __collision as its implicit array type, so the
# creation of collision will have to retry twice before it finds the open
# spot of ___collision for its implicit array type.
statement ok
CREATE TYPE _collision AS ENUM ();
CREATE TYPE collision AS ENUM ();
# _collision and __collision typelem and typarray should point back at each
# other, and vice versa for collision and ___collision.
query TOOO rowsort
SELECT
typname, oid, typelem, typarray
FROM
pg_type
WHERE
typname IN ('collision', '_collision', '__collision', '___collision')
----
_collision 100085 0 100086
__collision 100086 100085 0
collision 100087 0 100088
___collision 100088 100087 0
# Regression for #49756.
query TT
SELECT
column_name, column_type
FROM
crdb_internal.table_columns
WHERE
descriptor_name = 'enum_array' AND column_name = 'x'
----
x family:ArrayFamily width:0 precision:0 locale:"" visible_type:0 oid:100067 array_contents:<InternalType:<family:EnumFamily width:0 precision:0 locale:"" visible_type:0 oid:100066 time_precision_is_set:false udt_metadata:<array_type_oid:100067 > > TypeMeta:<Version:0 > > time_precision_is_set:false
# Test tables using enums in DEFAULT expressions.
statement ok
CREATE TABLE enum_default (
x INT,
y greeting DEFAULT 'hello',
z BOOL DEFAULT ('hello':::greeting IS OF (greeting, greeting)),
FAMILY (x, y, z)
);
INSERT INTO enum_default VALUES (1), (2)
query ITB rowsort
SELECT * FROM enum_default
----
1 hello true
2 hello true
query T
SELECT
pg_get_expr(d.adbin, d.adrelid)
FROM
pg_attribute AS a
LEFT JOIN pg_attrdef AS d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type AS t ON a.atttypid = t.oid
LEFT JOIN pg_collation AS c ON
a.attcollation = c.oid AND a.attcollation != t.typcollation
WHERE
a.attrelid = 'enum_default'::REGCLASS
AND NOT a.attisdropped
AND attname = 'y'
----
'hello'::public.greeting
# Test that enum default values are formatted in human readable ways.
query TT
SHOW CREATE enum_default
----
enum_default CREATE TABLE public.enum_default (
x INT8 NULL,
y public.greeting NULL DEFAULT 'hello':::public.greeting,
z BOOL NULL DEFAULT 'hello':::public.greeting IS OF (public.greeting, public.greeting),
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
FAMILY fam_0_x_y_z_rowid (x, y, z, rowid)
)
# Test crdb_internal.table_columns.
query TT
SELECT
column_name, default_expr
FROM
crdb_internal.table_columns
WHERE
descriptor_name='enum_default' AND (column_name = 'y' OR column_name = 'z')
ORDER BY
column_name
----
y 'hello':::public.greeting
z 'hello':::public.greeting IS OF (public.greeting, public.greeting)
# Test information_schema.columns.
query TT
SELECT
column_name, column_default
FROM
information_schema.columns
WHERE
table_name='enum_default' AND (column_name = 'y' OR column_name = 'z')
ORDER BY
column_name
----
y 'hello':::public.greeting
z 'hello':::public.greeting IS OF (public.greeting, public.greeting)
# Test computed columns with enum values.
statement ok
CREATE TABLE enum_computed (
x INT,
y greeting AS ('hello') STORED,
z BOOL AS (w = 'howdy') STORED,
w greeting,
FAMILY (x, y, z)
);
INSERT INTO enum_computed (x, w) VALUES (1, 'hello'), (2, 'hello')
query ITBT rowsort
SELECT * FROM enum_computed
----
1 hello false hello
2 hello false hello
query TT
SHOW CREATE enum_computed
----
enum_computed CREATE TABLE public.enum_computed (
x INT8 NULL,
y public.greeting NULL AS ('hello':::public.greeting) STORED,
z BOOL NULL AS (w = 'howdy':::public.greeting) STORED,
w public.greeting NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
FAMILY fam_0_x_y_z_w_rowid (x, y, z, w, rowid)
)
# Test information_schema.columns. generation_expression should not be
# formatted with type annotations.
query TT
SELECT
column_name, generation_expression
FROM
information_schema.columns
WHERE
table_name='enum_computed' AND (column_name = 'y' OR column_name = 'z')
ORDER BY
column_name
----
y 'hello'
z w = 'howdy'
# Test check constraints with enum values.
statement ok
CREATE TABLE enum_checks (
x greeting,
CHECK (x = 'hello'::greeting),
CHECK ('hello':::greeting = 'hello':::greeting)
);
INSERT INTO enum_checks VALUES ('hello')
query TT
SHOW CREATE enum_checks
----
enum_checks CREATE TABLE public.enum_checks (
x public.greeting NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
FAMILY "primary" (x, rowid),
CONSTRAINT check_x CHECK (x = 'hello':::public.greeting),
CONSTRAINT "check" CHECK ('hello':::public.greeting = 'hello':::public.greeting)
)
# Ensure that we can add check constraints to tables with enums.
statement ok
DROP TABLE enum_checks;
CREATE TABLE enum_checks (x greeting);
INSERT INTO enum_checks VALUES ('hi'), ('howdy');
ALTER TABLE enum_checks ADD CHECK (x > 'hello')
# Ensure that checks are validated on insert.
statement error pq: failed to satisfy CHECK constraint \(x > 'hello':::public.greeting\)
INSERT INTO enum_checks VALUES ('hello')
# Try adding a check that fails validation.
statement error pq: validation of CHECK "x = 'hello':::greeting" failed
ALTER TABLE enum_checks ADD CHECK (x = 'hello')
# Check the above cases, but in a transaction.
statement ok
DROP TABLE enum_checks;
BEGIN;
CREATE TABLE enum_checks (x greeting);
INSERT INTO enum_checks VALUES ('hi'), ('howdy');
ALTER TABLE enum_checks ADD CHECK (x > 'hello')
statement error pq: failed to satisfy CHECK constraint \(x > 'hello':::public.greeting\)
INSERT INTO enum_checks VALUES ('hello')
statement ok
ROLLBACK
statement ok
BEGIN;
CREATE TABLE enum_checks (x greeting);
INSERT INTO enum_checks VALUES ('hi'), ('howdy');
# Try adding a check that fails validation.
statement error pq: validation of CHECK "x = 'hello':::public.greeting" failed
ALTER TABLE enum_checks ADD CHECK (x = 'hello')
statement ok
ROLLBACK
# Test that cross database type references are disallowed.
statement ok
CREATE DATABASE other;
CREATE TYPE other.t AS ENUM ('other')
# We can still reference other databases types when creating objects
# within those databases.
statement ok
CREATE TABLE other.tt (x other.t)
# Referencing other databases in this database's objects will error.
statement error pq: cross database type references are not supported: other.public.t
CREATE TABLE cross_error (x other.t)
# Test that we can't hide cross database references in expressions.
statement error pq: cross database type references are not supported: other.public.t
CREATE TABLE cross_error (x BOOL DEFAULT ('other':::other.t = 'other':::other.t))
statement error pq: cross database type references are not supported: other.public.t
CREATE TABLE cross_error (x BOOL AS ('other':::other.t = 'other':::other.t) STORED)
statement error pq: cross database type references are not supported: other.public.t
CREATE TABLE cross_error (x INT, CHECK ('other':::other.t = 'other':::other.t))
# Test that we can't add columns or checks that use these either.
statement ok
CREATE TABLE cross_error (x INT)
statement error pq: cross database type references are not supported: other.public.t
ALTER TABLE cross_error ADD COLUMN y other.t
statement error pq: cross database type references are not supported: other.public.t
ALTER TABLE cross_error ADD COLUMN y BOOL DEFAULT ('other':::other.t = 'other':::other.t)
statement error pq: cross database type references are not supported: other.public.t
ALTER TABLE cross_error ADD COLUMN y BOOL AS ('other':::other.t = 'other':::other.t) STORED
statement error pq: cross database type references are not supported: other.public.t
ALTER TABLE cross_error ADD CHECK ('other':::other.t = 'other':::other.t)
subtest schema_changes
# Ensure that we can drop and create indexes on user defined type columns,
# as well as on other columns when the table has a user defined type column.
statement ok
CREATE TABLE sc (x greeting NOT NULL, y int NOT NULL);
INSERT INTO sc VALUES ('hello', 0), ('howdy', 1), ('hi', 2);
statement ok
CREATE INDEX i1 ON sc (x);
CREATE INDEX i2 ON sc (y);
CREATE INDEX i3 ON sc (x, y)
query T rowsort
SELECT x FROM sc@i1
----
hello
howdy
hi
query TI rowsort
SELECT x, y FROM sc@i3
----
hello 0
howdy 1
hi 2
statement ok
DROP INDEX sc@i1;
DROP INDEX sc@i2;
DROP INDEX sc@i3
# Test the above, but exercise the schema change in txn code path.
statement ok
DROP TABLE sc
statement ok
BEGIN;
CREATE TABLE sc (x greeting NOT NULL, y int NOT NULL);
INSERT INTO sc VALUES ('hello', 0), ('howdy', 1), ('hi', 2);
CREATE INDEX i1 ON sc (x);
CREATE INDEX i2 ON sc (y);
CREATE INDEX i3 ON sc (x, y)
query T rowsort
SELECT x FROM sc@i1
----
hello
howdy
hi
query TI rowsort
SELECT x, y FROM sc@i3
----
hello 0
howdy 1
hi 2
statement ok
DROP INDEX sc@i1;
DROP INDEX sc@i2;
DROP INDEX sc@i3;
COMMIT
# Ensure that we can create an index on a table and type created in
# the same transaction.
statement ok
BEGIN;
CREATE TYPE in_txn AS ENUM ('in', 'txn');
CREATE TABLE tbl_in_txn (x in_txn);
INSERT INTO tbl_in_txn VALUES ('txn');
CREATE INDEX i ON tbl_in_txn (x)
query T
SELECT * FROM tbl_in_txn@i
----
txn
statement ok
ROLLBACK
# Ensure that index drops that can't use range deletions succeed.
statement ok
CREATE TABLE enum_parent (x greeting PRIMARY KEY);
CREATE TABLE enum_child (x greeting, y greeting, PRIMARY KEY (x, y)) INTERLEAVE IN PARENT enum_parent (x);
INSERT INTO enum_parent VALUES ('hello');
INSERT INTO enum_child VALUES ('hello', 'hello');
CREATE INDEX i ON enum_child (x, y) INTERLEAVE IN PARENT enum_parent (x);
DROP INDEX enum_child@i
# Test the above case, but in a transaction.
statement ok
DROP TABLE enum_child;
DROP TABLE enum_parent;
BEGIN;
CREATE TABLE enum_parent (x greeting PRIMARY KEY);
CREATE TABLE enum_child (x greeting, y greeting, PRIMARY KEY (x, y)) INTERLEAVE IN PARENT enum_parent (x);
INSERT INTO enum_parent VALUES ('hello');
INSERT INTO enum_child VALUES ('hello', 'hello');
CREATE INDEX i ON enum_child (x, y) INTERLEAVE IN PARENT enum_parent (x);
DROP INDEX enum_child@i;
ROLLBACK
# Test primary key change to an ENUM column.
statement ok
CREATE TABLE enum_not_pk (x INT PRIMARY KEY, y greeting NOT NULL);
INSERT INTO enum_not_pk VALUES (1, 'howdy');
ALTER TABLE enum_not_pk ALTER PRIMARY KEY USING COLUMNS (y);
DROP TABLE enum_not_pk
# Test primary key change away from an ENUM column.
statement ok
CREATE TABLE enum_pk (x GREETING PRIMARY KEY, y INT NOT NULL);
INSERT INTO enum_pk VALUES ('howdy', 1);
ALTER TABLE enum_pk ALTER PRIMARY KEY USING COLUMNS (y);
DROP TABLE enum_pk
# Repeat the above tests, but in a transaction.
statement ok
BEGIN;
CREATE TABLE enum_not_pk (x INT PRIMARY KEY, y greeting NOT NULL);
INSERT INTO enum_not_pk VALUES (1, 'howdy');
ALTER TABLE enum_not_pk ALTER PRIMARY KEY USING COLUMNS (y);
ROLLBACK
statement ok
BEGIN;
CREATE TABLE enum_pk (x GREETING PRIMARY KEY, y INT NOT NULL);
INSERT INTO enum_pk VALUES ('howdy', 1);
ALTER TABLE enum_pk ALTER PRIMARY KEY USING COLUMNS (y);
ROLLBACK
# Test basic CTAS.
statement ok
CREATE TABLE enum_ctas_base (x greeting, y greeting, z _greeting);
INSERT INTO enum_ctas_base VALUES ('hi', 'howdy', ARRAY['hello']);
CREATE TABLE enum_ctas AS TABLE enum_ctas_base
query TTT
SELECT * from enum_ctas
----
hi howdy {hello}
statement ok
DROP TABLE enum_ctas
# Test basic CTAS in a transaction.
statement ok
BEGIN;
CREATE TABLE enum_ctas AS TABLE enum_ctas_base
query TTT
SELECT * from enum_ctas
----
hi howdy {hello}
statement ok
ROLLBACK
# Test CTAS with a SELECT query.
statement ok
CREATE TABLE enum_ctas AS (SELECT x, enum_first(y), z, enum_range(x) FROM enum_ctas_base)
query TTTT
SELECT * FROM enum_ctas
----
hi hello {hello} {hello,howdy,hi}
# Test CTAS with a SELECT query in a transaction.
statement ok
DROP TABLE enum_ctas;
BEGIN;
CREATE TABLE enum_ctas AS (SELECT x, enum_first(y), z, enum_range(x) FROM enum_ctas_base)
query TTTT
SELECT * FROM enum_ctas
----
hi hello {hello} {hello,howdy,hi}
statement ok
ROLLBACK
# Test CTAS from a VALUES clause.
statement ok
CREATE TABLE enum_ctas AS VALUES ('howdy'::greeting, ('how' || 'dy')::greeting, 'cockroach'::dbs)
query TTT
SELECT * FROM enum_ctas
----
howdy howdy cockroach
statement ok
DROP TABLE enum_ctas;
# Test CTAS from a VALUES clause in a transaction.
statement ok
BEGIN;
CREATE TABLE enum_ctas AS VALUES ('howdy'::greeting, 'cockroach'::dbs)
query TT
SELECT * FROM enum_ctas
----
howdy cockroach
statement ok
ROLLBACK
# Tests for column additions with enum tables.
statement ok
CREATE TABLE column_add (x greeting);
INSERT INTO column_add VALUES ('hello')
# Test that we can backfill a column when an enum column is in the table.
statement ok
ALTER TABLE column_add ADD COLUMN y INT DEFAULT 1
query TI
SELECT * FROM column_add
----
hello 1
# Test that we can add an enum column with a default value.
statement ok
ALTER TABLE column_add ADD COLUMN z greeting DEFAULT 'howdy'
query TIT
SELECT * FROM column_add
----
hello 1 howdy
# Test that we can add an enum computed column.
statement ok
ALTER TABLE column_add ADD COLUMN w greeting AS ('hi') STORED
query TITT
SELECT * FROM column_add
----
hello 1 howdy hi
# Test that we can add a computed column that uses an enum.
statement ok
ALTER TABLE column_add ADD COLUMN v BOOL AS (z < 'hi' AND x >= 'hello') STORED
query TITTB
SELECT * FROM column_add
----
hello 1 howdy hi true
# Repeat the above process on a new table in a transaction to exercise
# the schema change in txn codepath.
statement ok
DROP TABLE column_add
statement ok
BEGIN;
CREATE TABLE column_add (x greeting);
INSERT INTO column_add VALUES ('hello');
ALTER TABLE column_add ADD COLUMN y INT DEFAULT 1;
ALTER TABLE column_add ADD COLUMN z greeting DEFAULT 'howdy';
ALTER TABLE column_add ADD COLUMN w greeting AS ('hi') STORED;
ALTER TABLE column_add ADD COLUMN v BOOL AS (z < 'hi' AND x >= 'hello') STORED
query TITTB
SELECT * FROM column_add
----
hello 1 howdy hi true
statement ok
COMMIT
# Lastly, ensure that we can create a column using a type created in the
# same transaction.
statement ok
BEGIN;
CREATE TYPE in_txn AS ENUM ('in', 'txn');
CREATE TABLE tbl_in_txn (x INT);
INSERT INTO tbl_in_txn VALUES (1);
ALTER TABLE tbl_in_txn ADD COLUMN y in_txn DEFAULT 'txn';
query IT
SELECT * FROM tbl_in_txn
----
1 txn
statement ok
ROLLBACK
# Test that we can add foreign keys using enum columns.
statement ok
CREATE TABLE enum_origin (x greeting PRIMARY KEY);
CREATE TABLE enum_referenced (x greeting PRIMARY KEY);
INSERT INTO enum_origin VALUES ('hello');
INSERT INTO enum_referenced VALUES ('hello');
ALTER TABLE enum_origin ADD FOREIGN KEY (x) REFERENCES enum_referenced (x)
# Try a foreign key insert that fails validation.
statement error pq: insert on table "enum_origin" violates foreign key constraint "fk_x_ref_enum_referenced"
INSERT INTO enum_origin VALUES ('howdy')
# Try the above, but in a transaction.
statement ok
DROP TABLE enum_referenced, enum_origin;
BEGIN;
CREATE TABLE enum_origin (x greeting PRIMARY KEY);
CREATE TABLE enum_referenced (x greeting PRIMARY KEY);
INSERT INTO enum_origin VALUES ('hello');
INSERT INTO enum_referenced VALUES ('hello');
ALTER TABLE enum_origin ADD FOREIGN KEY (x) REFERENCES enum_referenced (x)
statement error pq: insert on table "enum_origin" violates foreign key constraint "fk_x_ref_enum_referenced"
INSERT INTO enum_origin VALUES ('howdy')
statement ok
ROLLBACK
# Try adding a foreign key that fails validation when building.
# Foreign keys added in the same transaction as a new table are not validated,
# so we can't test the below case in a transaction as well.
statement ok
CREATE TABLE enum_origin (x greeting PRIMARY KEY);
CREATE TABLE enum_referenced (x greeting PRIMARY KEY);
INSERT INTO enum_origin VALUES ('hello');
INSERT INTO enum_referenced VALUES ('howdy')
statement error pq: foreign key violation: "enum_origin" row x='hello' has no match in "enum_referenced"