| 1 |
CREATE EXTENSION tablefunc; |
| 2 |
-- |
| 3 |
-- normal_rand() |
| 4 |
-- no easy way to do this for regression testing |
| 5 |
-- |
| 6 |
SELECT avg(normal_rand)::int, count(*) FROM normal_rand(100, 250, 0.2); |
| 7 |
avg | count |
| 8 |
-----+------- |
| 9 |
250 | 100 |
| 10 |
(1 row) |
| 11 |
|
| 12 |
-- negative number of tuples |
| 13 |
SELECT avg(normal_rand)::int, count(*) FROM normal_rand(-1, 250, 0.2); |
| 14 |
ERROR: number of rows cannot be negative |
| 15 |
-- |
| 16 |
-- crosstab() |
| 17 |
-- |
| 18 |
CREATE TABLE ct(id int, rowclass text, rowid text, attribute text, val text); |
| 19 |
\copy ct from 'data/ct.data' |
| 20 |
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;'); |
| 21 |
row_name | category_1 | category_2 |
| 22 |
----------+------------+------------ |
| 23 |
test1 | val2 | val3 |
| 24 |
test2 | val6 | val7 |
| 25 |
| val10 | val11 |
| 26 |
(3 rows) |
| 27 |
|
| 28 |
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;'); |
| 29 |
row_name | category_1 | category_2 | category_3 |
| 30 |
----------+------------+------------+------------ |
| 31 |
test1 | val2 | val3 | |
| 32 |
test2 | val6 | val7 | |
| 33 |
| val10 | val11 | |
| 34 |
(3 rows) |
| 35 |
|
| 36 |
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;'); |
| 37 |
row_name | category_1 | category_2 | category_3 | category_4 |
| 38 |
----------+------------+------------+------------+------------ |
| 39 |
test1 | val2 | val3 | | |
| 40 |
test2 | val6 | val7 | | |
| 41 |
| val10 | val11 | | |
| 42 |
(3 rows) |
| 43 |
|
| 44 |
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;'); |
| 45 |
row_name | category_1 | category_2 |
| 46 |
----------+------------+------------ |
| 47 |
test1 | val1 | val2 |
| 48 |
test2 | val5 | val6 |
| 49 |
| val9 | val10 |
| 50 |
(3 rows) |
| 51 |
|
| 52 |
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;'); |
| 53 |
row_name | category_1 | category_2 | category_3 |
| 54 |
----------+------------+------------+------------ |
| 55 |
test1 | val1 | val2 | val3 |
| 56 |
test2 | val5 | val6 | val7 |
| 57 |
| val9 | val10 | val11 |
| 58 |
(3 rows) |
| 59 |
|
| 60 |
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;'); |
| 61 |
row_name | category_1 | category_2 | category_3 | category_4 |
| 62 |
----------+------------+------------+------------+------------ |
| 63 |
test1 | val1 | val2 | val3 | val4 |
| 64 |
test2 | val5 | val6 | val7 | val8 |
| 65 |
| val9 | val10 | val11 | val12 |
| 66 |
(3 rows) |
| 67 |
|
| 68 |
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;'); |
| 69 |
row_name | category_1 | category_2 |
| 70 |
----------+------------+------------ |
| 71 |
test3 | val1 | val2 |
| 72 |
test4 | val4 | val5 |
| 73 |
(2 rows) |
| 74 |
|
| 75 |
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;'); |
| 76 |
row_name | category_1 | category_2 | category_3 |
| 77 |
----------+------------+------------+------------ |
| 78 |
test3 | val1 | val2 | |
| 79 |
test4 | val4 | val5 | |
| 80 |
(2 rows) |
| 81 |
|
| 82 |
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;'); |
| 83 |
row_name | category_1 | category_2 | category_3 | category_4 |
| 84 |
----------+------------+------------+------------+------------ |
| 85 |
test3 | val1 | val2 | | |
| 86 |
test4 | val4 | val5 | | |
| 87 |
(2 rows) |
| 88 |
|
| 89 |
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;'); |
| 90 |
row_name | category_1 | category_2 |
| 91 |
----------+------------+------------ |
| 92 |
test3 | val1 | val2 |
| 93 |
test4 | val4 | val5 |
| 94 |
(2 rows) |
| 95 |
|
| 96 |
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;'); |
| 97 |
row_name | category_1 | category_2 | category_3 |
| 98 |
----------+------------+------------+------------ |
| 99 |
test3 | val1 | val2 | val3 |
| 100 |
test4 | val4 | val5 | val6 |
| 101 |
(2 rows) |
| 102 |
|
| 103 |
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' ORDER BY 1,2;'); |
| 104 |
row_name | category_1 | category_2 | category_3 | category_4 |
| 105 |
----------+------------+------------+------------+------------ |
| 106 |
test3 | val1 | val2 | val3 | |
| 107 |
test4 | val4 | val5 | val6 | |
| 108 |
(2 rows) |
| 109 |
|
| 110 |
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text); |
| 111 |
rowid | att1 | att2 |
| 112 |
-------+------+------- |
| 113 |
test1 | val1 | val2 |
| 114 |
test2 | val5 | val6 |
| 115 |
| val9 | val10 |
| 116 |
(3 rows) |
| 117 |
|
| 118 |
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); |
| 119 |
rowid | att1 | att2 | att3 |
| 120 |
-------+------+-------+------- |
| 121 |
test1 | val1 | val2 | val3 |
| 122 |
test2 | val5 | val6 | val7 |
| 123 |
| val9 | val10 | val11 |
| 124 |
(3 rows) |
| 125 |
|
| 126 |
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); |
| 127 |
rowid | att1 | att2 | att3 | att4 |
| 128 |
-------+------+-------+-------+------- |
| 129 |
test1 | val1 | val2 | val3 | val4 |
| 130 |
test2 | val5 | val6 | val7 | val8 |
| 131 |
| val9 | val10 | val11 | val12 |
| 132 |
(3 rows) |
| 133 |
|
| 134 |
-- check it works with OUT parameters, too |
| 135 |
CREATE FUNCTION crosstab_out(text, |
| 136 |
OUT rowid text, OUT att1 text, OUT att2 text, OUT att3 text) |
| 137 |
RETURNS setof record |
| 138 |
AS '$libdir/tablefunc','crosstab' |
| 139 |
LANGUAGE C STABLE STRICT; |
| 140 |
SELECT * FROM crosstab_out('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;'); |
| 141 |
rowid | att1 | att2 | att3 |
| 142 |
-------+------+-------+------- |
| 143 |
test1 | val1 | val2 | val3 |
| 144 |
test2 | val5 | val6 | val7 |
| 145 |
| val9 | val10 | val11 |
| 146 |
(3 rows) |
| 147 |
|
| 148 |
-- check error reporting |
| 149 |
SELECT * FROM crosstab('SELECT rowid, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;') |
| 150 |
AS ct(row_name text, category_1 text, category_2 text); |
| 151 |
ERROR: invalid crosstab source data query |
| 152 |
DETAIL: The query must return 3 columns: row_name, category, and value. |
| 153 |
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;') |
| 154 |
AS ct(row_name text); |
| 155 |
ERROR: invalid crosstab return type |
| 156 |
DETAIL: Return row must have at least two columns. |
| 157 |
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;') |
| 158 |
AS ct(row_name int, category_1 text, category_2 text); |
| 159 |
ERROR: invalid crosstab return type |
| 160 |
DETAIL: Source row_name datatype text does not match return row_name datatype integer. |
| 161 |
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;') |
| 162 |
AS ct(row_name text, category_1 text, category_2 int); |
| 163 |
ERROR: invalid crosstab return type |
| 164 |
DETAIL: Source value datatype text does not match return value datatype integer in column 3. |
| 165 |
-- |
| 166 |
-- hash based crosstab |
| 167 |
-- |
| 168 |
create table cth(id serial, rowid text, rowdt timestamp, attribute text, val text); |
| 169 |
insert into cth values(DEFAULT,'test1','01 March 2003','temperature','42'); |
| 170 |
insert into cth values(DEFAULT,'test1','01 March 2003','test_result','PASS'); |
| 171 |
-- the next line is intentionally left commented and is therefore a "missing" attribute |
| 172 |
-- insert into cth values(DEFAULT,'test1','01 March 2003','test_startdate','28 February 2003'); |
| 173 |
insert into cth values(DEFAULT,'test1','01 March 2003','volts','2.6987'); |
| 174 |
insert into cth values(DEFAULT,'test2','02 March 2003','temperature','53'); |
| 175 |
insert into cth values(DEFAULT,'test2','02 March 2003','test_result','FAIL'); |
| 176 |
insert into cth values(DEFAULT,'test2','02 March 2003','test_startdate','01 March 2003'); |
| 177 |
insert into cth values(DEFAULT,'test2','02 March 2003','volts','3.1234'); |
| 178 |
-- next group tests for NULL rowids |
| 179 |
insert into cth values(DEFAULT,NULL,'25 October 2007','temperature','57'); |
| 180 |
insert into cth values(DEFAULT,NULL,'25 October 2007','test_result','PASS'); |
| 181 |
insert into cth values(DEFAULT,NULL,'25 October 2007','test_startdate','24 October 2007'); |
| 182 |
insert into cth values(DEFAULT,NULL,'25 October 2007','volts','1.41234'); |
| 183 |
-- return attributes as plain text |
| 184 |
SELECT * FROM crosstab( |
| 185 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 186 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 187 |
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text); |
| 188 |
rowid | rowdt | temperature | test_result | test_startdate | volts |
| 189 |
-------+--------------------------+-------------+-------------+-----------------+--------- |
| 190 |
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987 |
| 191 |
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | 01 March 2003 | 3.1234 |
| 192 |
| Thu Oct 25 00:00:00 2007 | 57 | PASS | 24 October 2007 | 1.41234 |
| 193 |
(3 rows) |
| 194 |
|
| 195 |
-- this time without rowdt |
| 196 |
SELECT * FROM crosstab( |
| 197 |
'SELECT rowid, attribute, val FROM cth ORDER BY 1', |
| 198 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 199 |
AS c(rowid text, temperature text, test_result text, test_startdate text, volts text); |
| 200 |
rowid | temperature | test_result | test_startdate | volts |
| 201 |
-------+-------------+-------------+-----------------+--------- |
| 202 |
test1 | 42 | PASS | | 2.6987 |
| 203 |
test2 | 53 | FAIL | 01 March 2003 | 3.1234 |
| 204 |
| 57 | PASS | 24 October 2007 | 1.41234 |
| 205 |
(3 rows) |
| 206 |
|
| 207 |
-- convert attributes to specific datatypes |
| 208 |
SELECT * FROM crosstab( |
| 209 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 210 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 211 |
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8); |
| 212 |
rowid | rowdt | temperature | test_result | test_startdate | volts |
| 213 |
-------+--------------------------+-------------+-------------+--------------------------+--------- |
| 214 |
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987 |
| 215 |
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234 |
| 216 |
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 | 1.41234 |
| 217 |
(3 rows) |
| 218 |
|
| 219 |
-- source query and category query out of sync |
| 220 |
SELECT * FROM crosstab( |
| 221 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 222 |
'SELECT DISTINCT attribute FROM cth WHERE attribute IN (''temperature'',''test_result'',''test_startdate'') ORDER BY 1') |
| 223 |
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp); |
| 224 |
rowid | rowdt | temperature | test_result | test_startdate |
| 225 |
-------+--------------------------+-------------+-------------+-------------------------- |
| 226 |
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | |
| 227 |
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 |
| 228 |
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 |
| 229 |
(3 rows) |
| 230 |
|
| 231 |
-- if category query generates no rows, get expected error |
| 232 |
SELECT * FROM crosstab( |
| 233 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 234 |
'SELECT DISTINCT attribute FROM cth WHERE attribute = ''a'' ORDER BY 1') |
| 235 |
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8); |
| 236 |
ERROR: crosstab categories query must return at least one row |
| 237 |
-- if category query generates more than one column, get expected error |
| 238 |
SELECT * FROM crosstab( |
| 239 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 240 |
'SELECT DISTINCT rowdt, attribute FROM cth ORDER BY 2') |
| 241 |
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8); |
| 242 |
ERROR: invalid crosstab categories query |
| 243 |
DETAIL: The query must return one column. |
| 244 |
-- if category query generates a NULL value, get expected error |
| 245 |
SELECT * FROM crosstab( |
| 246 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 247 |
'SELECT NULL::text') |
| 248 |
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8); |
| 249 |
ERROR: crosstab category value must not be null |
| 250 |
-- if source query returns zero rows, get zero rows returned |
| 251 |
SELECT * FROM crosstab( |
| 252 |
'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1', |
| 253 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 254 |
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text); |
| 255 |
rowid | rowdt | temperature | test_result | test_startdate | volts |
| 256 |
-------+-------+-------------+-------------+----------------+------- |
| 257 |
(0 rows) |
| 258 |
|
| 259 |
-- if source query returns zero rows, get zero rows returned even if category query generates no rows |
| 260 |
SELECT * FROM crosstab( |
| 261 |
'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1', |
| 262 |
'SELECT DISTINCT attribute FROM cth WHERE false ORDER BY 1') |
| 263 |
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text); |
| 264 |
rowid | rowdt | temperature | test_result | test_startdate | volts |
| 265 |
-------+-------+-------------+-------------+----------------+------- |
| 266 |
(0 rows) |
| 267 |
|
| 268 |
-- check errors with inappropriate input rowtype |
| 269 |
SELECT * FROM crosstab( |
| 270 |
'SELECT rowid, attribute FROM cth ORDER BY 1', |
| 271 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 272 |
AS c(rowid text, temperature text, test_result text, test_startdate text, volts text); |
| 273 |
ERROR: invalid crosstab source data query |
| 274 |
DETAIL: The query must return at least 3 columns: row_name, category, and value. |
| 275 |
SELECT * FROM crosstab( |
| 276 |
'SELECT rowid, rowdt, rowdt, attribute, val FROM cth ORDER BY 1', |
| 277 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 278 |
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8); |
| 279 |
ERROR: invalid crosstab return type |
| 280 |
DETAIL: Return row must have 7 columns, not 6. |
| 281 |
-- check errors with inappropriate result rowtype |
| 282 |
SELECT * FROM crosstab( |
| 283 |
'SELECT rowid, attribute, val FROM cth ORDER BY 1', |
| 284 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1') |
| 285 |
AS c(rowid text); |
| 286 |
ERROR: invalid crosstab return type |
| 287 |
DETAIL: Return row must have at least two columns. |
| 288 |
-- check it works with a named result rowtype |
| 289 |
create type my_crosstab_result as ( |
| 290 |
rowid text, rowdt timestamp, |
| 291 |
temperature int4, test_result text, test_startdate timestamp, volts float8); |
| 292 |
CREATE FUNCTION crosstab_named(text, text) |
| 293 |
RETURNS setof my_crosstab_result |
| 294 |
AS '$libdir/tablefunc','crosstab_hash' |
| 295 |
LANGUAGE C STABLE STRICT; |
| 296 |
SELECT * FROM crosstab_named( |
| 297 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 298 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1'); |
| 299 |
rowid | rowdt | temperature | test_result | test_startdate | volts |
| 300 |
-------+--------------------------+-------------+-------------+--------------------------+--------- |
| 301 |
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987 |
| 302 |
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234 |
| 303 |
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 | 1.41234 |
| 304 |
(3 rows) |
| 305 |
|
| 306 |
-- check it works with OUT parameters |
| 307 |
CREATE FUNCTION crosstab_out(text, text, |
| 308 |
OUT rowid text, OUT rowdt timestamp, |
| 309 |
OUT temperature int4, OUT test_result text, |
| 310 |
OUT test_startdate timestamp, OUT volts float8) |
| 311 |
RETURNS setof record |
| 312 |
AS '$libdir/tablefunc','crosstab_hash' |
| 313 |
LANGUAGE C STABLE STRICT; |
| 314 |
SELECT * FROM crosstab_out( |
| 315 |
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1', |
| 316 |
'SELECT DISTINCT attribute FROM cth ORDER BY 1'); |
| 317 |
rowid | rowdt | temperature | test_result | test_startdate | volts |
| 318 |
-------+--------------------------+-------------+-------------+--------------------------+--------- |
| 319 |
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987 |
| 320 |
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234 |
| 321 |
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 | 1.41234 |
| 322 |
(3 rows) |
| 323 |
|
| 324 |
-- |
| 325 |
-- connectby |
| 326 |
-- |
| 327 |
-- test connectby with text based hierarchy |
| 328 |
CREATE TABLE connectby_text(keyid text, parent_keyid text, pos int); |
| 329 |
\copy connectby_text from 'data/connectby_text.data' |
| 330 |
-- with branch, without orderby |
| 331 |
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text); |
| 332 |
keyid | parent_keyid | level | branch |
| 333 |
-------+--------------+-------+--------------------- |
| 334 |
row2 | | 0 | row2 |
| 335 |
row4 | row2 | 1 | row2~row4 |
| 336 |
row6 | row4 | 2 | row2~row4~row6 |
| 337 |
row8 | row6 | 3 | row2~row4~row6~row8 |
| 338 |
row5 | row2 | 1 | row2~row5 |
| 339 |
row9 | row5 | 2 | row2~row5~row9 |
| 340 |
(6 rows) |
| 341 |
|
| 342 |
-- without branch, without orderby |
| 343 |
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int); |
| 344 |
keyid | parent_keyid | level |
| 345 |
-------+--------------+------- |
| 346 |
row2 | | 0 |
| 347 |
row4 | row2 | 1 |
| 348 |
row6 | row4 | 2 |
| 349 |
row8 | row6 | 3 |
| 350 |
row5 | row2 | 1 |
| 351 |
row9 | row5 | 2 |
| 352 |
(6 rows) |
| 353 |
|
| 354 |
-- with branch, with orderby |
| 355 |
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; |
| 356 |
keyid | parent_keyid | level | branch | pos |
| 357 |
-------+--------------+-------+---------------------+----- |
| 358 |
row2 | | 0 | row2 | 1 |
| 359 |
row5 | row2 | 1 | row2~row5 | 2 |
| 360 |
row9 | row5 | 2 | row2~row5~row9 | 3 |
| 361 |
row4 | row2 | 1 | row2~row4 | 4 |
| 362 |
row6 | row4 | 2 | row2~row4~row6 | 5 |
| 363 |
row8 | row6 | 3 | row2~row4~row6~row8 | 6 |
| 364 |
(6 rows) |
| 365 |
|
| 366 |
-- without branch, with orderby |
| 367 |
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; |
| 368 |
keyid | parent_keyid | level | pos |
| 369 |
-------+--------------+-------+----- |
| 370 |
row2 | | 0 | 1 |
| 371 |
row5 | row2 | 1 | 2 |
| 372 |
row9 | row5 | 2 | 3 |
| 373 |
row4 | row2 | 1 | 4 |
| 374 |
row6 | row4 | 2 | 5 |
| 375 |
row8 | row6 | 3 | 6 |
| 376 |
(6 rows) |
| 377 |
|
| 378 |
-- test connectby with int based hierarchy |
| 379 |
CREATE TABLE connectby_int(keyid int, parent_keyid int); |
| 380 |
\copy connectby_int from 'data/connectby_int.data' |
| 381 |
-- with branch |
| 382 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text); |
| 383 |
keyid | parent_keyid | level | branch |
| 384 |
-------+--------------+-------+--------- |
| 385 |
2 | | 0 | 2 |
| 386 |
4 | 2 | 1 | 2~4 |
| 387 |
6 | 4 | 2 | 2~4~6 |
| 388 |
8 | 6 | 3 | 2~4~6~8 |
| 389 |
5 | 2 | 1 | 2~5 |
| 390 |
9 | 5 | 2 | 2~5~9 |
| 391 |
(6 rows) |
| 392 |
|
| 393 |
-- without branch |
| 394 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int); |
| 395 |
keyid | parent_keyid | level |
| 396 |
-------+--------------+------- |
| 397 |
2 | | 0 |
| 398 |
4 | 2 | 1 |
| 399 |
6 | 4 | 2 |
| 400 |
8 | 6 | 3 |
| 401 |
5 | 2 | 1 |
| 402 |
9 | 5 | 2 |
| 403 |
(6 rows) |
| 404 |
|
| 405 |
-- recursion detection |
| 406 |
INSERT INTO connectby_int VALUES(10,9); |
| 407 |
INSERT INTO connectby_int VALUES(11,10); |
| 408 |
INSERT INTO connectby_int VALUES(9,11); |
| 409 |
-- should fail due to infinite recursion |
| 410 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text); |
| 411 |
ERROR: infinite recursion detected |
| 412 |
-- infinite recursion failure avoided by depth limit |
| 413 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 4, '~') AS t(keyid int, parent_keyid int, level int, branch text); |
| 414 |
keyid | parent_keyid | level | branch |
| 415 |
-------+--------------+-------+------------- |
| 416 |
2 | | 0 | 2 |
| 417 |
4 | 2 | 1 | 2~4 |
| 418 |
6 | 4 | 2 | 2~4~6 |
| 419 |
8 | 6 | 3 | 2~4~6~8 |
| 420 |
5 | 2 | 1 | 2~5 |
| 421 |
9 | 5 | 2 | 2~5~9 |
| 422 |
10 | 9 | 3 | 2~5~9~10 |
| 423 |
11 | 10 | 4 | 2~5~9~10~11 |
| 424 |
(8 rows) |
| 425 |
|
| 426 |
-- should fail as first two columns must have the same type |
| 427 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid text, parent_keyid int, level int, branch text); |
| 428 |
ERROR: invalid connectby return type |
| 429 |
DETAIL: Source key type integer does not match return key type text. |
| 430 |
-- should fail as key field datatype should match return datatype |
| 431 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid float8, parent_keyid float8, level int, branch text); |
| 432 |
ERROR: invalid connectby return type |
| 433 |
DETAIL: Source key type integer does not match return key type double precision. |
| 434 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid float8, level int, branch text); |
| 435 |
ERROR: invalid connectby return type |
| 436 |
DETAIL: Source parent key type integer does not match return parent key type double precision. |
| 437 |
-- check other rowtype mismatch cases |
| 438 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int, branch text); |
| 439 |
ERROR: invalid connectby return type |
| 440 |
DETAIL: Return row must have 3 columns, not 4. |
| 441 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int); |
| 442 |
ERROR: invalid connectby return type |
| 443 |
DETAIL: Return row must have 4 columns, not 3. |
| 444 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid text, level int); |
| 445 |
ERROR: invalid connectby return type |
| 446 |
DETAIL: Source parent key type integer does not match return parent key type text. |
| 447 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level float, branch float); |
| 448 |
ERROR: invalid connectby return type |
| 449 |
DETAIL: Third return column (depth) must be type integer. |
| 450 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch float); |
| 451 |
ERROR: invalid connectby return type |
| 452 |
DETAIL: Fourth return column (branch) must be type text. |
| 453 |
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text, pos text); |
| 454 |
ERROR: invalid connectby return type |
| 455 |
DETAIL: Fifth return column (serial) must be type integer. |
| 456 |
SELECT * FROM connectby('connectby_text', 'keyid', 'parent_keyid', 'pos', 'row2', 0) AS t(keyid text, parent_keyid text, level int, pos text); |
| 457 |
ERROR: invalid connectby return type |
| 458 |
DETAIL: Fourth return column (serial) must be type integer. |
| 459 |
-- tests for values using custom queries |
| 460 |
-- query with one column - failed |
| 461 |
SELECT * FROM connectby('connectby_int', '1; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int); |
| 462 |
ERROR: invalid connectby source data query |
| 463 |
DETAIL: The query must return at least two columns. |
| 464 |
-- query with two columns first value as NULL |
| 465 |
SELECT * FROM connectby('connectby_int', 'NULL::int, 1::int; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int); |
| 466 |
keyid | parent_keyid | level |
| 467 |
-------+--------------+------- |
| 468 |
2 | | 0 |
| 469 |
| 1 | 1 |
| 470 |
(2 rows) |
| 471 |
|
| 472 |
-- query with two columns second value as NULL |
| 473 |
SELECT * FROM connectby('connectby_int', '1::int, NULL::int; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int); |
| 474 |
ERROR: infinite recursion detected |
| 475 |
-- query with two columns, both values as NULL |
| 476 |
SELECT * FROM connectby('connectby_int', 'NULL::int, NULL::int; --', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int); |
| 477 |
keyid | parent_keyid | level |
| 478 |
-------+--------------+------- |
| 479 |
2 | | 0 |
| 480 |
| | 1 |
| 481 |
(2 rows) |
| 482 |
|
| 483 |
-- test for falsely detected recursion |
| 484 |
DROP TABLE connectby_int; |
| 485 |
CREATE TABLE connectby_int(keyid int, parent_keyid int); |
| 486 |
INSERT INTO connectby_int VALUES(11,NULL); |
| 487 |
INSERT INTO connectby_int VALUES(10,11); |
| 488 |
INSERT INTO connectby_int VALUES(111,11); |
| 489 |
INSERT INTO connectby_int VALUES(1,111); |
| 490 |
-- this should not fail due to recursion detection |
| 491 |
SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '11', 0, '-') AS t(keyid int, parent_keyid int, level int, branch text); |
| 492 |
keyid | parent_keyid | level | branch |
| 493 |
-------+--------------+-------+---------- |
| 494 |
11 | | 0 | 11 |
| 495 |
10 | 11 | 1 | 11-10 |
| 496 |
111 | 11 | 1 | 11-111 |
| 497 |
1 | 111 | 2 | 11-111-1 |
| 498 |
(4 rows) |
| 499 |
|