4 -- directory paths and dlsuffix are passed to us in environment variables
5 \getenv abs_srcdir PG_ABS_SRCDIR
6 \getenv abs_builddir PG_ABS_BUILDDIR
7 \getenv libdir PG_LIBDIR
8 \getenv dlsuffix PG_DLSUFFIX
9 \set regresslib :libdir '/regress' :dlsuffix
10 CREATE FUNCTION overpaid(emp)
14 CREATE FUNCTION reverse_name(name)
22 SET unique1 = onek.unique1 + 1;
24 SET unique1 = onek.unique1 - 1;
29 -- SET unique1 = onek2.unique1 + 1;
31 -- SET unique1 = onek2.unique1 - 1;
33 -- BTREE shutting out non-functional updates
35 -- the following two tests seem to take a long time on some
36 -- systems. This non-func update stuff needs to be examined
37 -- more closely. - jolly (2/22/96)
39 SELECT two, stringu1, ten, string4
43 SET stringu1 = reverse_name(onek.stringu1)
45 WHERE onek.stringu1 = 'JBAAAA' and
46 onek.stringu1 = tmp.stringu1;
48 SET stringu1 = reverse_name(onek2.stringu1)
50 WHERE onek2.stringu1 = 'JCAAAA' and
51 onek2.stringu1 = tmp.stringu1;
57 -- WHERE name = 'linda';
61 \set filename :abs_builddir '/results/onek.data'
62 COPY onek TO :'filename';
63 CREATE TEMP TABLE onek_copy (LIKE onek);
64 COPY onek_copy FROM :'filename';
65 SELECT * FROM onek EXCEPT ALL SELECT * FROM onek_copy;
66 unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
67 ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
70 SELECT * FROM onek_copy EXCEPT ALL SELECT * FROM onek;
71 unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
72 ---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
75 \set filename :abs_builddir '/results/stud_emp.data'
76 COPY BINARY stud_emp TO :'filename';
77 CREATE TEMP TABLE stud_emp_copy (LIKE stud_emp);
78 COPY BINARY stud_emp_copy FROM :'filename';
79 SELECT * FROM stud_emp_copy;
80 name | age | location | salary | manager | gpa | percent
81 -------+-----+------------+--------+---------+-----+---------
82 jeff | 23 | (8,7.7) | 600 | sharon | 3.5 |
83 cim | 30 | (10.5,4.7) | 400 | | 3.4 |
84 linda | 19 | (0.9,6.1) | 100 | | 2.9 |
88 -- test data for postquel functions
90 CREATE TABLE hobbies_r (
94 CREATE TABLE equipment_r (
98 INSERT INTO hobbies_r (name, person)
99 SELECT 'posthacking', p.name
101 WHERE p.name = 'mike' or p.name = 'jeff';
102 INSERT INTO hobbies_r (name, person)
103 SELECT 'basketball', p.name
105 WHERE p.name = 'joe' or p.name = 'sally';
106 INSERT INTO hobbies_r (name) VALUES ('skywalking');
107 INSERT INTO equipment_r (name, hobby) VALUES ('advil', 'posthacking');
108 INSERT INTO equipment_r (name, hobby) VALUES ('peet''s coffee', 'posthacking');
109 INSERT INTO equipment_r (name, hobby) VALUES ('hightops', 'basketball');
110 INSERT INTO equipment_r (name, hobby) VALUES ('guts', 'skywalking');
112 -- postquel functions
114 CREATE FUNCTION hobbies(person)
115 RETURNS setof hobbies_r
116 AS 'select * from hobbies_r where person = $1.name'
118 CREATE FUNCTION hobby_construct(text, text)
120 AS 'select $1 as name, $2 as hobby'
122 CREATE FUNCTION hobby_construct_named(name text, hobby text)
124 AS 'select name, hobby'
126 CREATE FUNCTION hobbies_by_name(hobbies_r.name%TYPE)
127 RETURNS hobbies_r.person%TYPE
128 AS 'select person from hobbies_r where name = $1'
130 NOTICE: type reference hobbies_r.name%TYPE converted to text
131 NOTICE: type reference hobbies_r.person%TYPE converted to text
132 CREATE FUNCTION equipment(hobbies_r)
133 RETURNS setof equipment_r
134 AS 'select * from equipment_r where hobby = $1.name'
136 CREATE FUNCTION equipment_named(hobby hobbies_r)
137 RETURNS setof equipment_r
138 AS 'select * from equipment_r where equipment_r.hobby = equipment_named.hobby.name'
140 CREATE FUNCTION equipment_named_ambiguous_1a(hobby hobbies_r)
141 RETURNS setof equipment_r
142 AS 'select * from equipment_r where hobby = equipment_named_ambiguous_1a.hobby.name'
144 CREATE FUNCTION equipment_named_ambiguous_1b(hobby hobbies_r)
145 RETURNS setof equipment_r
146 AS 'select * from equipment_r where equipment_r.hobby = hobby.name'
148 CREATE FUNCTION equipment_named_ambiguous_1c(hobby hobbies_r)
149 RETURNS setof equipment_r
150 AS 'select * from equipment_r where hobby = hobby.name'
152 CREATE FUNCTION equipment_named_ambiguous_2a(hobby text)
153 RETURNS setof equipment_r
154 AS 'select * from equipment_r where hobby = equipment_named_ambiguous_2a.hobby'
156 CREATE FUNCTION equipment_named_ambiguous_2b(hobby text)
157 RETURNS setof equipment_r
158 AS 'select * from equipment_r where equipment_r.hobby = hobby'
161 -- mike does post_hacking,
162 -- joe and sally play basketball, and
163 -- everyone else does nothing.
165 SELECT p.name, name(p.hobbies) FROM ONLY person p;
167 -------+-------------
174 -- as above, but jeff also does post_hacking.
176 SELECT p.name, name(p.hobbies) FROM person* p;
178 -------+-------------
186 -- the next two queries demonstrate how functions generate bogus duplicates.
187 -- this is a "feature" ..
189 SELECT DISTINCT hobbies_r.name, name(hobbies_r.equipment) FROM hobbies_r
192 -------------+---------------
193 basketball | hightops
195 posthacking | peet's coffee
199 SELECT hobbies_r.name, (hobbies_r.equipment).name FROM hobbies_r;
201 -------------+---------------
203 posthacking | peet's coffee
205 posthacking | peet's coffee
206 basketball | hightops
207 basketball | hightops
212 -- mike needs advil and peet's coffee,
213 -- joe and sally need hightops, and
214 -- everyone else is fine.
216 SELECT p.name, name(p.hobbies), name(equipment(p.hobbies)) FROM ONLY person p;
218 -------+-------------+---------------
219 mike | posthacking | advil
220 mike | posthacking | peet's coffee
221 joe | basketball | hightops
222 sally | basketball | hightops
226 -- as above, but jeff needs advil and peet's coffee as well.
228 SELECT p.name, name(p.hobbies), name(equipment(p.hobbies)) FROM person* p;
230 -------+-------------+---------------
231 mike | posthacking | advil
232 mike | posthacking | peet's coffee
233 joe | basketball | hightops
234 sally | basketball | hightops
235 jeff | posthacking | advil
236 jeff | posthacking | peet's coffee
240 -- just like the last two, but make sure that the target list fixup and
241 -- unflattening is being done correctly.
243 SELECT name(equipment(p.hobbies)), p.name, name(p.hobbies) FROM ONLY person p;
245 ---------------+-------+-------------
246 advil | mike | posthacking
247 peet's coffee | mike | posthacking
248 hightops | joe | basketball
249 hightops | sally | basketball
252 SELECT (p.hobbies).equipment.name, p.name, name(p.hobbies) FROM person* p;
254 ---------------+-------+-------------
255 advil | mike | posthacking
256 peet's coffee | mike | posthacking
257 hightops | joe | basketball
258 hightops | sally | basketball
259 advil | jeff | posthacking
260 peet's coffee | jeff | posthacking
263 SELECT (p.hobbies).equipment.name, name(p.hobbies), p.name FROM ONLY person p;
265 ---------------+-------------+-------
266 advil | posthacking | mike
267 peet's coffee | posthacking | mike
268 hightops | basketball | joe
269 hightops | basketball | sally
272 SELECT name(equipment(p.hobbies)), name(p.hobbies), p.name FROM person* p;
274 ---------------+-------------+-------
275 advil | posthacking | mike
276 peet's coffee | posthacking | mike
277 hightops | basketball | joe
278 hightops | basketball | sally
279 advil | posthacking | jeff
280 peet's coffee | posthacking | jeff
283 SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer')));
289 SELECT name(equipment(hobby_construct_named(text 'skywalking', text 'mer')));
295 SELECT name(equipment_named(hobby_construct_named(text 'skywalking', text 'mer')));
301 SELECT name(equipment_named_ambiguous_1a(hobby_construct_named(text 'skywalking', text 'mer')));
307 SELECT name(equipment_named_ambiguous_1b(hobby_construct_named(text 'skywalking', text 'mer')));
313 SELECT name(equipment_named_ambiguous_1c(hobby_construct_named(text 'skywalking', text 'mer')));
319 SELECT name(equipment_named_ambiguous_2a(text 'skywalking'));
325 SELECT name(equipment_named_ambiguous_2b(text 'skywalking'));
334 SELECT hobbies_by_name('basketball');
340 SELECT name, overpaid(emp.*) FROM emp;
352 -- Try a few cases with SQL-spec row constructor expressions
354 SELECT * FROM equipment(ROW('skywalking', 'mer'));
360 SELECT name(equipment(ROW('skywalking', 'mer')));
366 SELECT *, name(equipment(h.*)) FROM hobbies_r h;
368 -------------+--------+---------------
369 posthacking | mike | advil
370 posthacking | mike | peet's coffee
371 posthacking | jeff | advil
372 posthacking | jeff | peet's coffee
373 basketball | joe | hightops
374 basketball | sally | hightops
378 SELECT *, (equipment(CAST((h.*) AS hobbies_r))).name FROM hobbies_r h;
380 -------------+--------+---------------
381 posthacking | mike | advil
382 posthacking | mike | peet's coffee
383 posthacking | jeff | advil
384 posthacking | jeff | peet's coffee
385 basketball | joe | hightops
386 basketball | sally | hightops