4 -- Make both a standalone composite type and a table rowtype
5 create type complex as (r float8, i float8);
6 create temp table fullname (first text, last text);
8 create type quad as (c1 complex, c2 complex);
9 -- Some simple tests of I/O conversions and row construction
10 select (1.1,2.2)::complex, row((3.3,4.4),(5.5,null))::quad;
12 -----------+------------------------
13 (1.1,2.2) | ("(3.3,4.4)","(5.5,)")
16 select row('Joe', 'Blow')::fullname, '(Joe,Blow)'::fullname;
18 ------------+------------
19 (Joe,Blow) | (Joe,Blow)
22 select '(Joe,von Blow)'::fullname, '(Joe,d''Blow)'::fullname;
24 ------------------+--------------
25 (Joe,"von Blow") | (Joe,d'Blow)
28 select '(Joe,"von""Blow")'::fullname, E'(Joe,d\\\\Blow)'::fullname;
30 -------------------+-----------------
31 (Joe,"von""Blow") | (Joe,"d\\Blow")
34 select '(Joe,"Blow,Jr")'::fullname;
40 select '(Joe,)'::fullname; -- ok, null 2nd column
46 select '(Joe)'::fullname; -- bad
47 ERROR: malformed record literal: "(Joe)"
48 LINE 1: select '(Joe)'::fullname;
50 DETAIL: Too few columns.
51 select '(Joe,,)'::fullname; -- bad
52 ERROR: malformed record literal: "(Joe,,)"
53 LINE 1: select '(Joe,,)'::fullname;
55 DETAIL: Too many columns.
56 create temp table quadtable(f1 int, q quad);
57 insert into quadtable values (1, ((3.3,4.4),(5.5,6.6)));
58 insert into quadtable values (2, ((null,4.4),(5.5,6.6)));
59 select * from quadtable;
61 ----+---------------------------
62 1 | ("(3.3,4.4)","(5.5,6.6)")
63 2 | ("(,4.4)","(5.5,6.6)")
67 set local add_missing_from = false;
68 select f1, q.c1 from quadtable; -- fails, q is a table reference
69 ERROR: missing FROM-clause entry for table "q"
70 LINE 1: select f1, q.c1 from quadtable;
73 select f1, (q).c1, (qq.q).c1.i from quadtable qq;
75 ----+-----------+-----
80 create temp table people (fn fullname, bd date);
81 insert into people values ('(Joe,Blow)', '1984-01-10');
84 ------------+------------
85 (Joe,Blow) | 01-10-1984
88 -- at the moment this will not work due to ALTER TABLE inadequacy:
89 alter table fullname add column suffix text default '';
90 ERROR: cannot alter table "fullname" because column "people"."fn" uses its rowtype
91 -- but this should work:
92 alter table fullname add column suffix text default null;
95 -------------+------------
96 (Joe,Blow,) | 01-10-1984
99 -- test insertion/updating of subfields
100 update people set fn.suffix = 'Jr';
101 select * from people;
103 ---------------+------------
104 (Joe,Blow,Jr) | 01-10-1984
107 insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
108 select * from quadtable;
110 ----+---------------------------
111 1 | ("(3.3,4.4)","(5.5,6.6)")
112 2 | ("(,4.4)","(5.5,6.6)")
113 44 | ("(55,)","(,66)")
116 -- The object here is to ensure that toasted references inside
117 -- composite values don't cause problems. The large f1 value will
118 -- be toasted inside pp, it must still work after being copied to people.
119 create temp table pp (f1 text);
120 insert into pp values (repeat('abcdefghijkl', 100000));
121 insert into people select ('Jim', f1, null)::fullname, current_date from pp;
122 select (fn).first, substr((fn).last, 1, 20), length((fn).last) from people;
123 first | substr | length
124 -------+----------------------+---------
126 Jim | abcdefghijklabcdefgh | 1200000
129 -- Test row comparison semantics. Prior to PG 8.2 we did this in a totally
130 -- non-spec-compliant way.
131 select ROW(1,2) < ROW(1,3) as true;
137 select ROW(1,2) < ROW(1,1) as false;
143 select ROW(1,2) < ROW(1,NULL) as null;
149 select ROW(1,2,3) < ROW(1,3,NULL) as true; -- the NULL is not examined
155 select ROW(11,'ABC') < ROW(11,'DEF') as true;
161 select ROW(11,'ABC') > ROW(11,'DEF') as false;
167 select ROW(12,'ABC') > ROW(11,'DEF') as true;
173 -- = and <> have different NULL-behavior than < etc
174 select ROW(1,2,3) < ROW(1,NULL,4) as null;
180 select ROW(1,2,3) = ROW(1,NULL,4) as false;
186 select ROW(1,2,3) <> ROW(1,NULL,4) as true;
192 -- We allow operators beyond the six standard ones, if they have btree
194 select ROW('ABC','DEF') ~<=~ ROW('DEF','ABC') as true;
200 select ROW('ABC','DEF') ~>=~ ROW('DEF','ABC') as false;
206 select ROW('ABC','DEF') ~~ ROW('DEF','ABC') as fail;
207 ERROR: could not determine interpretation of row comparison operator ~~
208 LINE 1: select ROW('ABC','DEF') ~~ ROW('DEF','ABC') as fail;
210 HINT: Row comparison operators must be associated with btree operator families.
211 -- Check row comparison with a subselect
212 select unique1, unique2 from tenk1
213 where (unique1, unique2) < any (select ten, ten from tenk1 where hundred < 3)
222 -- Also check row comparison with an indexable condition
223 select thousand, tenthous from tenk1
224 where (thousand, tenthous) >= (997, 5000)
225 order by thousand, tenthous;
227 ----------+----------
255 -- Check some corner cases involving empty rowtypes
262 select ROW() IS NULL;
268 select ROW() = ROW();
269 ERROR: cannot compare rows of zero length
270 LINE 1: select ROW() = ROW();
272 -- Check ability to create arrays of anonymous rowtypes
273 select array[ row(1,2), row(3,4), row(5,6) ];
275 ---------------------------
276 {"(1,2)","(3,4)","(5,6)"}
279 -- Check ability to compare an anonymous row to elements of an array
280 select row(1,1.1) = any (array[ row(7,7.7), row(1,1.1), row(0,0.0) ]);
286 select row(1,1.1) = any (array[ row(7,7.7), row(1,1.0), row(0,0.0) ]);