domain.out 10.8 KB
Newer Older
1 2 3
-- Test Comment / Drop
create domain domaindroptest int4;
comment on domain domaindroptest is 'About to drop this..';
4
-- currently this will be disallowed
5
create domain basetypetest domaindroptest;
6
ERROR:  "domaindroptest" is not a valid base type for a domain
7
drop domain domaindroptest;
8 9
-- this should fail because already gone
drop domain domaindroptest cascade;
10
ERROR:  type "domaindroptest" does not exist
11 12 13 14 15
-- TEST Domains.
create domain domainvarchar varchar(5);
create domain domainnumeric numeric(8,2);
create domain domainint4 int4;
create domain domaintext text;
16 17 18 19 20 21 22 23
-- Test explicit coercions --- these should succeed (and truncate)
SELECT cast('123456' as domainvarchar);
 domainvarchar 
---------------
 12345
(1 row)

SELECT cast('12345' as domainvarchar);
24 25 26 27 28
 domainvarchar 
---------------
 12345
(1 row)

29 30 31 32 33 34 35 36 37 38 39
-- 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
40 41
-- Test copy
COPY basictest (testvarchar) FROM stdin; -- fail
42
ERROR:  value too long for type character varying(5)
43
CONTEXT:  COPY basictest, line 1: "notsoshorttext"
44
COPY basictest (testvarchar) FROM stdin;
45 46 47
select * from basictest;
 testint4 | testtext | testvarchar | testnumeric 
----------+----------+-------------+-------------
48 49 50
       88 | haha     | short       |      123.12
       88 | haha     | short       |      123.12
          |          | short       |            
51
(3 rows)
52

Tom Lane's avatar
Tom Lane committed
53
-- check that domains inherit operations from base types
54
select testtext || testvarchar as concat, testnumeric + 42 as sum
Tom Lane's avatar
Tom Lane committed
55 56 57 58 59
from basictest;
  concat   |  sum   
-----------+--------
 hahashort | 165.12
 hahashort | 165.12
60 61
           |       
(3 rows)
Tom Lane's avatar
Tom Lane committed
62

63 64 65 66
drop table basictest;
drop domain domainvarchar restrict;
drop domain domainnumeric restrict;
drop domain domainint4 restrict;
Tom Lane's avatar
Tom Lane committed
67
drop domain domaintext;
68 69 70 71 72 73 74
-- Array Test
create domain domainint4arr int4[1];
create domain domaintextarr text[2][3];
create table domarrtest
           ( testint4arr domainint4arr
           , testtextarr domaintextarr
            );
75 76 77 78 79
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"}}');
Tom Lane's avatar
Tom Lane committed
80 81 82
select * from domarrtest;
  testint4arr  |     testtextarr     
---------------+---------------------
83 84 85 86 87
 {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}}
Tom Lane's avatar
Tom Lane committed
88 89 90 91 92
(5 rows)

select testint4arr[1], testtextarr[2:2] from domarrtest;
 testint4arr | testtextarr 
-------------+-------------
93
           2 | {{c,d}}
94
             | {}
95
           2 | {{c,d}}
Tom Lane's avatar
Tom Lane committed
96
           2 | {{c}}
97
             | {{d,e,f}}
Tom Lane's avatar
Tom Lane committed
98 99
(5 rows)

100 101 102 103
drop table domarrtest;
drop domain domainint4arr restrict;
drop domain domaintextarr restrict;
create domain dnotnull varchar(15) NOT NULL;
104
create domain dnull    varchar(15);
105
create domain dcheck   varchar(15) NOT NULL CHECK (VALUE = 'a' OR VALUE = 'c' OR VALUE = 'd');
106 107 108 109 110
create table nulltest
           ( col1 dnotnull
           , col2 dnotnull NULL  -- NOT NULL in the domain cannot be overridden
           , col3 dnull    NOT NULL
           , col4 dnull
111
           , col5 dcheck CHECK (col5 IN ('c', 'd'))
112 113
           );
INSERT INTO nulltest DEFAULT VALUES;
114
ERROR:  domain dnotnull does not allow null values
115 116
INSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c');  -- Good
insert into nulltest values ('a', 'b', 'c', 'd', NULL);
117
ERROR:  domain dcheck does not allow null values
118
insert into nulltest values ('a', 'b', 'c', 'd', 'a');
119
ERROR:  new row for relation "nulltest" violates check constraint "nulltest_col5_check"
120
INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');
121
ERROR:  domain dnotnull does not allow null values
122
INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');
123
ERROR:  domain dnotnull does not allow null values
124
INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');
125
ERROR:  null value in column "col3" violates not-null constraint
126
INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good
127 128
-- Test copy
COPY nulltest FROM stdin; --fail
129
ERROR:  domain dcheck does not allow null values
130
CONTEXT:  COPY nulltest, line 1: "a	b	\N	d	\N"
131
-- Last row is bad
132
COPY nulltest FROM stdin;
133
ERROR:  new row for relation "nulltest" violates check constraint "nulltest_col5_check"
134
CONTEXT:  COPY nulltest, line 3: "a	b	c	\N	a"
135
select * from nulltest;
136 137 138 139 140
 col1 | col2 | col3 | col4 | col5 
------+------+------+------+------
 a    | b    | c    | d    | c
 a    | b    | c    |      | d
(2 rows)
141

142 143 144 145 146 147 148 149
-- Test out coerced (casted) constraints
SELECT cast('1' as dnotnull);
 dnotnull 
----------
 1
(1 row)

SELECT cast(NULL as dnotnull); -- fail
150
ERROR:  domain dnotnull does not allow null values
151
SELECT cast(cast(NULL as dnull) as dnotnull); -- fail
152
ERROR:  domain dnotnull does not allow null values
153
SELECT cast(col4 as dnotnull) from nulltest; -- fail
154
ERROR:  domain dnotnull does not allow null values
155
-- cleanup
156 157 158
drop table nulltest;
drop domain dnotnull restrict;
drop domain dnull restrict;
159
drop domain dcheck restrict;
160 161 162 163 164
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;
165
create domain ddef4 int4 DEFAULT nextval('ddef4_seq');
166 167 168 169 170
create domain ddef5 numeric(8,2) NOT NULL DEFAULT '12.12';
create table defaulttest
            ( col1 ddef1
            , col2 ddef2
            , col3 ddef3
171
            , col4 ddef4 PRIMARY KEY
172 173 174
            , col5 ddef1 NOT NULL DEFAULT NULL
            , col6 ddef2 DEFAULT '88'
            , col7 ddef4 DEFAULT 8000
175
            , col8 ddef5
176
            );
177
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "defaulttest_pkey" for table "defaulttest"
178 179 180
insert into defaulttest default values;
insert into defaulttest default values;
insert into defaulttest default values;
181 182
-- Test defaults with copy
COPY defaulttest(col5) FROM stdin;
183 184 185
select * from defaulttest;
 col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8  
------+------+------+------+------+------+------+-------
186 187 188 189
    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
190
(4 rows)
191

192 193 194 195 196 197 198 199 200
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
201
ERROR:  column "col1" of table "domnotnull" contains null values
202 203
update domnotnull set col1 = 5;
alter domain dnotnulltest set not null; -- fails
204
ERROR:  column "col2" of table "domnotnull" contains null values
205 206 207
update domnotnull set col2 = 6;
alter domain dnotnulltest set not null;
update domnotnull set col1 = null; -- fails
208
ERROR:  domain dnotnulltest does not allow null values
209 210 211
alter domain dnotnulltest drop not null;
update domnotnull set col1 = null;
drop domain dnotnulltest cascade;
212 213
NOTICE:  drop cascades to table domnotnull column col2
NOTICE:  drop cascades to table domnotnull column col1
214 215 216 217 218 219
-- Test ALTER DOMAIN .. DEFAULT ..
create table domdeftest (col1 ddef1);
insert into domdeftest default values;
select * from domdeftest;
 col1 
------
220
    3
221 222 223 224 225 226 227
(1 row)

alter domain ddef1 set default '42';
insert into domdeftest default values;
select * from domdeftest;
 col1 
------
228 229
    3
   42
230 231 232 233 234 235 236
(2 rows)

alter domain ddef1 drop default;
insert into domdeftest default values;
select * from domdeftest;
 col1 
------
237 238 239
    3
   42
     
240 241 242 243 244 245 246 247 248
(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
249
ERROR:  column "col1" of table "domcontest" contains values that violate the new constraint
250 251 252
alter domain con add constraint t check (VALUE < 34);
alter domain con add check (VALUE > 0);
insert into domcontest values (-5); -- fails
253
ERROR:  value for domain con violates check constraint "con_check"
254
insert into domcontest values (42); -- fails
255
ERROR:  value for domain con violates check constraint "t"
256 257 258
insert into domcontest values (5);
alter domain con drop constraint t;
insert into domcontest values (-5); --fails
259
ERROR:  value for domain con violates check constraint "con_check"
260
insert into domcontest values (42);
261 262 263 264 265 266 267 268 269
-- 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 
------
270 271
     
    5
272 273 274 275
(2 rows)

alter domain dom set not null;
select * from domview; -- fail
276
ERROR:  domain dom does not allow null values
277 278 279 280
alter domain dom drop not null;
select * from domview;
 col1 
------
281 282
     
    5
283 284 285 286
(2 rows)

alter domain dom add constraint domchkgt6 check(value > 6);
select * from domview; --fail
287
ERROR:  value for domain dom violates check constraint "domchkgt6"
288 289 290 291
alter domain dom drop constraint domchkgt6 restrict;
select * from domview;
 col1 
------
292 293
     
    5
294 295
(2 rows)

296
-- cleanup
297 298 299 300 301
drop domain ddef1 restrict;
drop domain ddef2 restrict;
drop domain ddef3 restrict;
drop domain ddef4 restrict;
drop domain ddef5 restrict;
302
drop sequence ddef4_seq;
303 304 305 306 307 308 309 310 311 312 313 314 315 316
-- 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"
317 318 319 320 321 322 323 324 325 326 327 328 329 330
-- 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