2
-- first, define the functions. Turn off echoing so that expected file
3
-- does not depend on contents of tablefunc.sql.
11
-- no easy way to do this for regression testing
13
SELECT avg(normal_rand)::int FROM normal_rand(100, 250, 0.2);
18
CREATE TABLE ct(id int, rowclass text, rowid text, attribute text, val text);
19
\copy ct from 'data/ct.data'
21
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
22
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
23
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
25
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
26
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
27
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
29
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
30
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
31
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
33
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;');
34
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;');
35
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;');
37
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;', 2) AS c(rowid text, att1 text, att2 text);
38
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;', 3) AS c(rowid text, att1 text, att2 text, att3 text);
39
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;', 4) AS c(rowid text, att1 text, att2 text, att3 text, att4 text);
42
-- hash based crosstab
44
create table cth(id serial, rowid text, rowdt timestamp, attribute text, val text);
45
insert into cth values(DEFAULT,'test1','01 March 2003','temperature','42');
46
insert into cth values(DEFAULT,'test1','01 March 2003','test_result','PASS');
47
-- the next line is intentionally left commented and is therefore a "missing" attribute
48
-- insert into cth values(DEFAULT,'test1','01 March 2003','test_startdate','28 February 2003');
49
insert into cth values(DEFAULT,'test1','01 March 2003','volts','2.6987');
50
insert into cth values(DEFAULT,'test2','02 March 2003','temperature','53');
51
insert into cth values(DEFAULT,'test2','02 March 2003','test_result','FAIL');
52
insert into cth values(DEFAULT,'test2','02 March 2003','test_startdate','01 March 2003');
53
insert into cth values(DEFAULT,'test2','02 March 2003','volts','3.1234');
55
-- return attributes as plain text
56
SELECT * FROM crosstab(
57
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
58
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
59
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
61
-- this time without rowdt
62
SELECT * FROM crosstab(
63
'SELECT rowid, attribute, val FROM cth ORDER BY 1',
64
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
65
AS c(rowid text, temperature text, test_result text, test_startdate text, volts text);
67
-- convert attributes to specific datatypes
68
SELECT * FROM crosstab(
69
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
70
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
71
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
73
-- source query and category query out of sync
74
SELECT * FROM crosstab(
75
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
76
'SELECT DISTINCT attribute FROM cth WHERE attribute IN (''temperature'',''test_result'',''test_startdate'') ORDER BY 1')
77
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp);
79
-- if category query generates no rows, get expected error
80
SELECT * FROM crosstab(
81
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
82
'SELECT DISTINCT attribute FROM cth WHERE attribute = ''a'' ORDER BY 1')
83
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
85
-- if category query generates more than one column, get expected error
86
SELECT * FROM crosstab(
87
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
88
'SELECT DISTINCT rowdt, attribute FROM cth ORDER BY 2')
89
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
91
-- if source query returns zero rows, get zero rows returned
92
SELECT * FROM crosstab(
93
'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1',
94
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
95
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
97
-- if source query returns zero rows, get zero rows returned even if category query generates no rows
98
SELECT * FROM crosstab(
99
'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1',
100
'SELECT DISTINCT attribute FROM cth WHERE false ORDER BY 1')
101
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
107
-- test connectby with text based hierarchy
108
CREATE TABLE connectby_text(keyid text, parent_keyid text, pos int);
109
\copy connectby_text from 'data/connectby_text.data'
111
-- with branch, without orderby
112
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text);
114
-- without branch, without orderby
115
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int);
117
-- with branch, with orderby
118
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;
120
-- without branch, with orderby
121
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;
123
-- test connectby with int based hierarchy
124
CREATE TABLE connectby_int(keyid int, parent_keyid int);
125
\copy connectby_int from 'data/connectby_int.data'
128
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text);
131
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
133
-- recursion detection
134
INSERT INTO connectby_int VALUES(10,9);
135
INSERT INTO connectby_int VALUES(11,10);
136
INSERT INTO connectby_int VALUES(9,11);
138
-- should fail due to infinite recursion
139
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text);
141
-- infinite recursion failure avoided by depth limit
142
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 4, '~') AS t(keyid int, parent_keyid int, level int, branch text);
144
-- test for falsely detected recursion
145
DROP TABLE connectby_int;
146
CREATE TABLE connectby_int(keyid int, parent_keyid int);
147
INSERT INTO connectby_int VALUES(11,NULL);
148
INSERT INTO connectby_int VALUES(10,11);
149
INSERT INTO connectby_int VALUES(111,11);
150
INSERT INTO connectby_int VALUES(1,111);
151
-- this should not fail due to recursion detection
152
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '11', 0, '-') AS t(keyid int, parent_keyid int, level int, branch text);