Debian Codegraph @

contrib/tablefunc/sql/tablefunc.sql

raw
1 CREATE EXTENSION tablefunc;
2
3 --
4 -- normal_rand()
5 -- no easy way to do this for regression testing
6 --
7 SELECT avg(normal_rand)::int, count(*) FROM normal_rand(100, 250, 0.2);
8 -- negative number of tuples
9 SELECT avg(normal_rand)::int, count(*) FROM normal_rand(-1, 250, 0.2);
10
11 --
12 -- crosstab()
13 --
14 CREATE TABLE ct(id int, rowclass text, rowid text, attribute text, val text);
15 \copy ct from 'data/ct.data'
16
17 SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
18 SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
19 SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
20
21 SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
22 SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
23 SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
24
25 SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
26 SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
27 SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
28
29 SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;');
30 SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;');
31 SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;');
32
33 SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text);
34 SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text, att3 text);
35 SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text, att3 text, att4 text);
36
37 -- check it works with OUT parameters, too
38
39 CREATE FUNCTION crosstab_out(text,
40 OUT rowid text, OUT att1 text, OUT att2 text, OUT att3 text)
41 RETURNS setof record
42 AS '$libdir/tablefunc','crosstab'
43 LANGUAGE C STABLE STRICT;
44
45 SELECT * FROM crosstab_out('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
46
47 -- check error reporting
48 SELECT * FROM crosstab('SELECT rowid, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
49 AS ct(row_name text, category_1 text, category_2 text);
50 SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
51 AS ct(row_name text);
52 SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
53 AS ct(row_name int, category_1 text, category_2 text);
54 SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
55 AS ct(row_name text, category_1 text, category_2 int);
56
57 --
58 -- hash based crosstab
59 --
60 create table cth(id serial, rowid text, rowdt timestamp, attribute text, val text);
61 insert into cth values(DEFAULT,'test1','01 March 2003','temperature','42');
62 insert into cth values(DEFAULT,'test1','01 March 2003','test_result','PASS');
63 -- the next line is intentionally left commented and is therefore a "missing" attribute
64 -- insert into cth values(DEFAULT,'test1','01 March 2003','test_startdate','28 February 2003');
65 insert into cth values(DEFAULT,'test1','01 March 2003','volts','2.6987');
66 insert into cth values(DEFAULT,'test2','02 March 2003','temperature','53');
67 insert into cth values(DEFAULT,'test2','02 March 2003','test_result','FAIL');
68 insert into cth values(DEFAULT,'test2','02 March 2003','test_startdate','01 March 2003');
69 insert into cth values(DEFAULT,'test2','02 March 2003','volts','3.1234');
70 -- next group tests for NULL rowids
71 insert into cth values(DEFAULT,NULL,'25 October 2007','temperature','57');
72 insert into cth values(DEFAULT,NULL,'25 October 2007','test_result','PASS');
73 insert into cth values(DEFAULT,NULL,'25 October 2007','test_startdate','24 October 2007');
74 insert into cth values(DEFAULT,NULL,'25 October 2007','volts','1.41234');
75
76 -- return attributes as plain text
77 SELECT * FROM crosstab(
78 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
79 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
80 AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
81
82 -- this time without rowdt
83 SELECT * FROM crosstab(
84 'SELECT rowid, attribute, val FROM cth ORDER BY 1',
85 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
86 AS c(rowid text, temperature text, test_result text, test_startdate text, volts text);
87
88 -- convert attributes to specific datatypes
89 SELECT * FROM crosstab(
90 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
91 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
92 AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
93
94 -- source query and category query out of sync
95 SELECT * FROM crosstab(
96 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
97 'SELECT DISTINCT attribute FROM cth WHERE attribute IN (''temperature'',''test_result'',''test_startdate'') ORDER BY 1')
98 AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp);
99
100 -- if category query generates no rows, get expected error
101 SELECT * FROM crosstab(
102 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
103 'SELECT DISTINCT attribute FROM cth WHERE attribute = ''a'' ORDER BY 1')
104 AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
105
106 -- if category query generates more than one column, get expected error
107 SELECT * FROM crosstab(
108 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
109 'SELECT DISTINCT rowdt, attribute FROM cth ORDER BY 2')
110 AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
111
112 -- if category query generates a NULL value, get expected error
113 SELECT * FROM crosstab(
114 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
115 'SELECT NULL::text')
116 AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
117
118 -- if source query returns zero rows, get zero rows returned
119 SELECT * FROM crosstab(
120 'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1',
121 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
122 AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
123
124 -- if source query returns zero rows, get zero rows returned even if category query generates no rows
125 SELECT * FROM crosstab(
126 'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1',
127 'SELECT DISTINCT attribute FROM cth WHERE false ORDER BY 1')
128 AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
129
130 -- check errors with inappropriate input rowtype
131 SELECT * FROM crosstab(
132 'SELECT rowid, attribute FROM cth ORDER BY 1',
133 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
134 AS c(rowid text, temperature text, test_result text, test_startdate text, volts text);
135 SELECT * FROM crosstab(
136 'SELECT rowid, rowdt, rowdt, attribute, val FROM cth ORDER BY 1',
137 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
138 AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
139
140 -- check errors with inappropriate result rowtype
141 SELECT * FROM crosstab(
142 'SELECT rowid, attribute, val FROM cth ORDER BY 1',
143 'SELECT DISTINCT attribute FROM cth ORDER BY 1')
144 AS c(rowid text);
145
146 -- check it works with a named result rowtype
147
148 create type my_crosstab_result as (
149 rowid text, rowdt timestamp,
150 temperature int4, test_result text, test_startdate timestamp, volts float8);
151
152 CREATE FUNCTION crosstab_named(text, text)
153 RETURNS setof my_crosstab_result
154 AS '$libdir/tablefunc','crosstab_hash'
155 LANGUAGE C STABLE STRICT;
156
157 SELECT * FROM crosstab_named(
158 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
159 'SELECT DISTINCT attribute FROM cth ORDER BY 1');
160
161 -- check it works with OUT parameters
162
163 CREATE FUNCTION crosstab_out(text, text,
164 OUT rowid text, OUT rowdt timestamp,
165 OUT temperature int4, OUT test_result text,
166 OUT test_startdate timestamp, OUT volts float8)
167 RETURNS setof record
168 AS '$libdir/tablefunc','crosstab_hash'
169 LANGUAGE C STABLE STRICT;
170
171 SELECT * FROM crosstab_out(
172 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
173 'SELECT DISTINCT attribute FROM cth ORDER BY 1');
174
175 --
176 -- connectby
177 --
178
179 -- test connectby with text based hierarchy
180 CREATE TABLE connectby_text(keyid text, parent_keyid text, pos int);
181 \copy connectby_text from 'data/connectby_text.data'
182
183 -- with branch, without orderby
184 SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text);
185
186 -- without branch, without orderby
187 SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int);
188
189 -- with branch, with orderby
190 SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text, pos int) ORDER BY t.pos;
191
192 -- without branch, with orderby
193 SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0) AS t(keyid text, parent_keyid text, level int, pos int) ORDER BY t.pos;
194
195 -- test connectby with int based hierarchy
196 CREATE TABLE connectby_int(keyid int, parent_keyid int);
197 \copy connectby_int from 'data/connectby_int.data'
198
199 -- with branch
200 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text);
201
202 -- without branch
203 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
204
205 -- recursion detection
206 INSERT INTO connectby_int VALUES(10,9);
207 INSERT INTO connectby_int VALUES(11,10);
208 INSERT INTO connectby_int VALUES(9,11);
209
210 -- should fail due to infinite recursion
211 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text);
212
213 -- infinite recursion failure avoided by depth limit
214 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 4, '~') AS t(keyid int, parent_keyid int, level int, branch text);
215
216 -- should fail as first two columns must have the same type
217 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid text, parent_keyid int, level int, branch text);
218
219 -- should fail as key field datatype should match return datatype
220 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid float8, parent_keyid float8, level int, branch text);
221 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid float8, level int, branch text);
222
223 -- check other rowtype mismatch cases
224 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int, branch text);
225 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int);
226 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid text, level int);
227 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level float, branch float);
228 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch float);
229 SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text, pos text);
230 SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0) AS t(keyid text, parent_keyid text, level int, pos text);
231
232 -- tests for values using custom queries
233 -- query with one column - failed
234 SELECT * FROM connectby('connectby_int', '1; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
235 -- query with two columns first value as NULL
236 SELECT * FROM connectby('connectby_int', 'NULL::int, 1::int; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
237 -- query with two columns second value as NULL
238 SELECT * FROM connectby('connectby_int', '1::int, NULL::int; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
239 -- query with two columns, both values as NULL
240 SELECT * FROM connectby('connectby_int', 'NULL::int, NULL::int; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
241
242 -- test for falsely detected recursion
243 DROP TABLE connectby_int;
244 CREATE TABLE connectby_int(keyid int, parent_keyid int);
245 INSERT INTO connectby_int VALUES(11,NULL);
246 INSERT INTO connectby_int VALUES(10,11);
247 INSERT INTO connectby_int VALUES(111,11);
248 INSERT INTO connectby_int VALUES(1,111);
249 -- this should not fail due to recursion detection
250 SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '11', 0, '-') AS t(keyid int, parent_keyid int, level int, branch text);