aboutsummaryrefslogtreecommitdiff
path: root/src/test/regress/expected/domain.out
blob: 8e432e1cb846b8710959eb8a74c6258377ac591a (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
--
-- Test domains.
--
-- Test Comment / Drop
create domain domaindroptest int4;
comment on domain domaindroptest is 'About to drop this..';
create domain dependenttypetest domaindroptest;
-- fail because of dependent type
drop domain domaindroptest;
NOTICE:  type dependenttypetest depends on type domaindroptest
ERROR:  cannot drop type domaindroptest because other objects depend on it
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
drop domain domaindroptest cascade;
NOTICE:  drop cascades to type dependenttypetest
-- this should fail because already gone
drop domain domaindroptest cascade;
ERROR:  type "domaindroptest" does not exist
-- Test domain input.
-- Note: the point of checking both INSERT and COPY FROM is that INSERT
-- exercises CoerceToDomain while COPY exercises domain_in.
create domain domainvarchar varchar(5);
create domain domainnumeric numeric(8,2);
create domain domainint4 int4;
create domain domaintext text;
-- Test explicit coercions --- these should succeed (and truncate)
SELECT cast('123456' as domainvarchar);
 domainvarchar 
---------------
 12345
(1 row)

SELECT cast('12345' as domainvarchar);
 domainvarchar 
---------------
 12345
(1 row)

-- Test tables using domains
create table basictest
           ( testint4 domainint4
           , testtext domaintext
           , testvarchar domainvarchar
           , testnumeric domainnumeric
           );
INSERT INTO basictest values ('88', 'haha', 'short', '123.12');      -- Good
INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar
ERROR:  value too long for type character varying(5)
INSERT INTO basictest values ('88', 'haha', 'short', '123.1212');    -- Truncate numeric
-- Test copy
COPY basictest (testvarchar) FROM stdin; -- fail
ERROR:  value too long for type character varying(5)
CONTEXT:  COPY basictest, line 1, column testvarchar: "notsoshorttext"
COPY basictest (testvarchar) FROM stdin;
select * from basictest;
 testint4 | testtext | testvarchar | testnumeric 
----------+----------+-------------+-------------
       88 | haha     | short       |      123.12
       88 | haha     | short       |      123.12
          |          | short       |            
(3 rows)

-- check that domains inherit operations from base types
select testtext || testvarchar as concat, testnumeric + 42 as sum
from basictest;
  concat   |  sum   
-----------+--------
 hahashort | 165.12
 hahashort | 165.12
           |       
(3 rows)

drop table basictest;
drop domain domainvarchar restrict;
drop domain domainnumeric restrict;
drop domain domainint4 restrict;
drop domain domaintext;
-- Test domains over array types
create domain domainint4arr int4[1];
create domain domainchar4arr varchar(4)[2][3];
create table domarrtest
           ( testint4arr domainint4arr
           , testchar4arr domainchar4arr
            );
INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"}}');
INSERT INTO domarrtest values ('{{2,2},{2,2}}', '{{"a","b"}}');
INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"},{"e","f"}}');
INSERT INTO domarrtest values ('{2,2}', '{{"a"},{"c"}}');
INSERT INTO domarrtest values (NULL, '{{"a","b","c"},{"d","e","f"}}');
INSERT INTO domarrtest values (NULL, '{{"toolong","b","c"},{"d","e","f"}}');
ERROR:  value too long for type character varying(4)
select * from domarrtest;
  testint4arr  |    testchar4arr     
---------------+---------------------
 {2,2}         | {{a,b},{c,d}}
 {{2,2},{2,2}} | {{a,b}}
 {2,2}         | {{a,b},{c,d},{e,f}}
 {2,2}         | {{a},{c}}
               | {{a,b,c},{d,e,f}}
(5 rows)

select testint4arr[1], testchar4arr[2:2] from domarrtest;
 testint4arr | testchar4arr 
-------------+--------------
           2 | {{c,d}}
             | {}
           2 | {{c,d}}
           2 | {{c}}
             | {{d,e,f}}
(5 rows)

COPY domarrtest FROM stdin;
COPY domarrtest FROM stdin;	-- fail
ERROR:  value too long for type character varying(4)
CONTEXT:  COPY domarrtest, line 1, column testchar4arr: "{qwerty,w,e}"
select * from domarrtest;
  testint4arr  |    testchar4arr     
---------------+---------------------
 {2,2}         | {{a,b},{c,d}}
 {{2,2},{2,2}} | {{a,b}}
 {2,2}         | {{a,b},{c,d},{e,f}}
 {2,2}         | {{a},{c}}
               | {{a,b,c},{d,e,f}}
 {3,4}         | {q,w,e}
               | 
(7 rows)

drop table domarrtest;
drop domain domainint4arr restrict;
drop domain domainchar4arr restrict;
create domain dnotnull varchar(15) NOT NULL;
create domain dnull    varchar(15);
create domain dcheck   varchar(15) NOT NULL CHECK (VALUE = 'a' OR VALUE = 'c' OR VALUE = 'd');
create table nulltest
           ( col1 dnotnull
           , col2 dnotnull NULL  -- NOT NULL in the domain cannot be overridden
           , col3 dnull    NOT NULL
           , col4 dnull
           , col5 dcheck CHECK (col5 IN ('c', 'd'))
           );
INSERT INTO nulltest DEFAULT VALUES;
ERROR:  domain dnotnull does not allow null values
INSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c');  -- Good
insert into nulltest values ('a', 'b', 'c', 'd', NULL);
ERROR:  domain dcheck does not allow null values
insert into nulltest values ('a', 'b', 'c', 'd', 'a');
ERROR:  new row for relation "nulltest" violates check constraint "nulltest_col5_check"
INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');
ERROR:  domain dnotnull does not allow null values
INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
ERROR:  domain dnotnull does not allow null values
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
ERROR:  null value in column "col3" violates not-null constraint
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
-- Test copy
COPY nulltest FROM stdin; --fail
ERROR:  null value in column "col3" violates not-null constraint
CONTEXT:  COPY nulltest, line 1: "a	b	\N	d	d"
COPY nulltest FROM stdin; --fail
ERROR:  domain dcheck does not allow null values
CONTEXT:  COPY nulltest, line 1, column col5: null input
-- Last row is bad
COPY nulltest FROM stdin;
ERROR:  new row for relation "nulltest" violates check constraint "nulltest_col5_check"
CONTEXT:  COPY nulltest, line 3: "a	b	c	\N	a"
select * from nulltest;
 col1 | col2 | col3 | col4 | col5 
------+------+------+------+------
 a    | b    | c    | d    | c
 a    | b    | c    |      | d
(2 rows)

-- Test out coerced (casted) constraints
SELECT cast('1' as dnotnull);
 dnotnull 
----------
 1
(1 row)

SELECT cast(NULL as dnotnull); -- fail
ERROR:  domain dnotnull does not allow null values
SELECT cast(cast(NULL as dnull) as dnotnull); -- fail
ERROR:  domain dnotnull does not allow null values
SELECT cast(col4 as dnotnull) from nulltest; -- fail
ERROR:  domain dnotnull does not allow null values
-- cleanup
drop table nulltest;
drop domain dnotnull restrict;
drop domain dnull restrict;
drop domain dcheck restrict;
create domain ddef1 int4 DEFAULT 3;
create domain ddef2 oid DEFAULT '12';
-- Type mixing, function returns int8
create domain ddef3 text DEFAULT 5;
create sequence ddef4_seq;
create domain ddef4 int4 DEFAULT nextval('ddef4_seq');
create domain ddef5 numeric(8,2) NOT NULL DEFAULT '12.12';
create table defaulttest
            ( col1 ddef1
            , col2 ddef2
            , col3 ddef3
            , col4 ddef4 PRIMARY KEY
            , col5 ddef1 NOT NULL DEFAULT NULL
            , col6 ddef2 DEFAULT '88'
            , col7 ddef4 DEFAULT 8000
            , col8 ddef5
            );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "defaulttest_pkey" for table "defaulttest"
insert into defaulttest default values;
insert into defaulttest default values;
insert into defaulttest default values;
-- Test defaults with copy
COPY defaulttest(col5) FROM stdin;
select * from defaulttest;
 col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8  
------+------+------+------+------+------+------+-------
    3 |   12 | 5    |    1 |    3 |   88 | 8000 | 12.12
    3 |   12 | 5    |    2 |    3 |   88 | 8000 | 12.12
    3 |   12 | 5    |    3 |    3 |   88 | 8000 | 12.12
    3 |   12 | 5    |    4 |   42 |   88 | 8000 | 12.12
(4 rows)

drop table defaulttest cascade;
-- Test ALTER DOMAIN .. NOT NULL
create domain dnotnulltest integer;
create table domnotnull
( col1 dnotnulltest
, col2 dnotnulltest
);
insert into domnotnull default values;
alter domain dnotnulltest set not null; -- fails
ERROR:  column "col1" of table "domnotnull" contains null values
update domnotnull set col1 = 5;
alter domain dnotnulltest set not null; -- fails
ERROR:  column "col2" of table "domnotnull" contains null values
update domnotnull set col2 = 6;
alter domain dnotnulltest set not null;
update domnotnull set col1 = null; -- fails
ERROR:  domain dnotnulltest does not allow null values
alter domain dnotnulltest drop not null;
update domnotnull set col1 = null;
drop domain dnotnulltest cascade;
NOTICE:  drop cascades to table domnotnull column col2
NOTICE:  drop cascades to table domnotnull column col1
-- Test ALTER DOMAIN .. DEFAULT ..
create table domdeftest (col1 ddef1);
insert into domdeftest default values;
select * from domdeftest;
 col1 
------
    3
(1 row)

alter domain ddef1 set default '42';
insert into domdeftest default values;
select * from domdeftest;
 col1 
------
    3
   42
(2 rows)

alter domain ddef1 drop default;
insert into domdeftest default values;
select * from domdeftest;
 col1 
------
    3
   42
     
(3 rows)

drop table domdeftest;
-- Test ALTER DOMAIN .. CONSTRAINT ..
create domain con as integer;
create table domcontest (col1 con);
insert into domcontest values (1);
insert into domcontest values (2);
alter domain con add constraint t check (VALUE < 1); -- fails
ERROR:  column "col1" of table "domcontest" contains values that violate the new constraint
alter domain con add constraint t check (VALUE < 34);
alter domain con add check (VALUE > 0);
insert into domcontest values (-5); -- fails
ERROR:  value for domain con violates check constraint "con_check"
insert into domcontest values (42); -- fails
ERROR:  value for domain con violates check constraint "t"
insert into domcontest values (5);
alter domain con drop constraint t;
insert into domcontest values (-5); --fails
ERROR:  value for domain con violates check constraint "con_check"
insert into domcontest values (42);
-- Confirm ALTER DOMAIN with RULES.
create table domtab (col1 integer);
create domain dom as integer;
create view domview as select cast(col1 as dom) from domtab;
insert into domtab (col1) values (null);
insert into domtab (col1) values (5);
select * from domview;
 col1 
------
     
    5
(2 rows)

alter domain dom set not null;
select * from domview; -- fail
ERROR:  domain dom does not allow null values
alter domain dom drop not null;
select * from domview;
 col1 
------
     
    5
(2 rows)

alter domain dom add constraint domchkgt6 check(value > 6);
select * from domview; --fail
ERROR:  value for domain dom violates check constraint "domchkgt6"
alter domain dom drop constraint domchkgt6 restrict;
select * from domview;
 col1 
------
     
    5
(2 rows)

-- cleanup
drop domain ddef1 restrict;
drop domain ddef2 restrict;
drop domain ddef3 restrict;
drop domain ddef4 restrict;
drop domain ddef5 restrict;
drop sequence ddef4_seq;
-- Test domains over domains
create domain vchar4 varchar(4);
create domain dinter vchar4 check (substring(VALUE, 1, 1) = 'x');
create domain dtop dinter check (substring(VALUE, 2, 1) = '1');
select 'x123'::dtop;
 dtop 
------
 x123
(1 row)

select 'x1234'::dtop; -- explicit coercion should truncate
 dtop 
------
 x123
(1 row)

select 'y1234'::dtop; -- fail
ERROR:  value for domain dtop violates check constraint "dinter_check"
select 'y123'::dtop; -- fail
ERROR:  value for domain dtop violates check constraint "dinter_check"
select 'yz23'::dtop; -- fail
ERROR:  value for domain dtop violates check constraint "dinter_check"
select 'xz23'::dtop; -- fail
ERROR:  value for domain dtop violates check constraint "dtop_check"
create temp table dtest(f1 dtop);
insert into dtest values('x123');
insert into dtest values('x1234'); -- fail, implicit coercion
ERROR:  value too long for type character varying(4)
insert into dtest values('y1234'); -- fail, implicit coercion
ERROR:  value too long for type character varying(4)
insert into dtest values('y123'); -- fail
ERROR:  value for domain dtop violates check constraint "dinter_check"
insert into dtest values('yz23'); -- fail
ERROR:  value for domain dtop violates check constraint "dinter_check"
insert into dtest values('xz23'); -- fail
ERROR:  value for domain dtop violates check constraint "dtop_check"
drop table dtest;
drop domain vchar4 cascade;
NOTICE:  drop cascades to type dinter
NOTICE:  drop cascades to type dtop
-- Make sure that constraints of newly-added domain columns are
-- enforced correctly, even if there's no default value for the new
-- column. Per bug #1433
create domain str_domain as text not null;
create table domain_test (a int, b int);
insert into domain_test values (1, 2);
insert into domain_test values (1, 2);
-- should fail
alter table domain_test add column c str_domain;
ERROR:  domain str_domain does not allow null values
create domain str_domain2 as text check (value <> 'foo') default 'foo';
-- should fail
alter table domain_test add column d str_domain2;
ERROR:  value for domain str_domain2 violates check constraint "str_domain2_check"
-- Check that domain constraints on prepared statement parameters of
-- unknown type are enforced correctly.
create domain pos_int as int4 check (value > 0) not null;
prepare s1 as select $1::pos_int = 10 as "is_ten";
execute s1(10);
 is_ten 
--------
 t
(1 row)

execute s1(0); -- should fail
ERROR:  value for domain pos_int violates check constraint "pos_int_check"
execute s1(NULL); -- should fail
ERROR:  domain pos_int does not allow null values
-- Check that domain constraints on plpgsql function parameters, results,
-- and local variables are enforced correctly.
create function doubledecrement(p1 pos_int) returns pos_int as $$
declare v pos_int;
begin
    return p1;
end$$ language plpgsql;
select doubledecrement(3); -- fail because of implicit null assignment
ERROR:  domain pos_int does not allow null values
CONTEXT:  PL/pgSQL function "doubledecrement" line 2 at block variables initialization
create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
declare v pos_int := 0;
begin
    return p1;
end$$ language plpgsql;
select doubledecrement(3); -- fail at initialization assignment
ERROR:  value for domain pos_int violates check constraint "pos_int_check"
CONTEXT:  PL/pgSQL function "doubledecrement" line 2 at block variables initialization
create or replace function doubledecrement(p1 pos_int) returns pos_int as $$
declare v pos_int := 1;
begin
    v := p1 - 1;
    return v - 1;
end$$ language plpgsql;
select doubledecrement(null); -- fail before call
ERROR:  domain pos_int does not allow null values
select doubledecrement(0); -- fail before call
ERROR:  value for domain pos_int violates check constraint "pos_int_check"
select doubledecrement(1); -- fail at assignment to v
ERROR:  value for domain pos_int violates check constraint "pos_int_check"
CONTEXT:  PL/pgSQL function "doubledecrement" line 3 at assignment
select doubledecrement(2); -- fail at return
ERROR:  value for domain pos_int violates check constraint "pos_int_check"
CONTEXT:  PL/pgSQL function "doubledecrement" while casting return value to function's return type
select doubledecrement(3); -- good
 doubledecrement 
-----------------
               1
(1 row)