Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6aa4482

Browse files
committed
Attached is an update to contrib/tablefunc. It introduces a new
function, connectby(), which can serve as a reference implementation for the changes made in the last few days -- namely the ability of a function to return an entire tuplestore, and the ability of a function to make use of the query provided "expected" tuple description. Description: connectby(text relname, text keyid_fld, text parent_keyid_fld, text start_with, int max_depth [, text branch_delim]) - returns keyid, parent_keyid, level, and an optional branch string - requires anonymous composite type syntax in the FROM clause. See the instructions in the documentation below. Joe Conway
1 parent 9fd842c commit 6aa4482

File tree

5 files changed

+602
-12
lines changed

5 files changed

+602
-12
lines changed

contrib/tablefunc/README.tablefunc

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ Installation:
6060
- requires anonymous composite type syntax in the FROM clause. See
6161
the instructions in the documentation below.
6262

63+
connectby(text relname, text keyid_fld, text parent_keyid_fld,
64+
text start_with, int max_depth [, text branch_delim])
65+
- returns keyid, parent_keyid, level, and an optional branch string
66+
- requires anonymous composite type syntax in the FROM clause. See
67+
the instructions in the documentation below.
68+
6369
Documentation
6470
==================================================================
6571
Name
@@ -324,6 +330,109 @@ AS ct(row_name text, category_1 text, category_2 text, category_3 text);
324330
test2 | val6 | val7 |
325331
(2 rows)
326332

333+
==================================================================
334+
Name
335+
336+
connectby(text, text, text, text, int[, text]) - returns a set
337+
representing a hierarchy (tree structure)
338+
339+
Synopsis
340+
341+
connectby(text relname, text keyid_fld, text parent_keyid_fld,
342+
text start_with, int max_depth [, text branch_delim])
343+
344+
Inputs
345+
346+
relname
347+
348+
Name of the source relation
349+
350+
keyid_fld
351+
352+
Name of the key field
353+
354+
parent_keyid_fld
355+
356+
Name of the key_parent field
357+
358+
start_with
359+
360+
root value of the tree input as a text value regardless of keyid_fld type
361+
362+
max_depth
363+
364+
zero (0) for unlimited depth, otherwise restrict level to this depth
365+
366+
branch_delim
367+
368+
if optional branch value is desired, this string is used as the delimiter
369+
370+
Outputs
371+
372+
Returns setof record, which must defined with a column definition
373+
in the FROM clause of the SELECT statement, e.g.:
374+
375+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
376+
AS t(keyid text, parent_keyid text, level int, branch text);
377+
378+
- or -
379+
380+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
381+
AS t(keyid text, parent_keyid text, level int);
382+
383+
Notes
384+
385+
1. keyid and parent_keyid must be the same data type
386+
387+
2. The column definition *must* include a third column of type INT4 for
388+
the level value output
389+
390+
3. If the branch field is not desired, omit both the branch_delim input
391+
parameter *and* the branch field in the query column definition
392+
393+
4. If the branch field is desired, it must be the forth column in the query
394+
column definition, and it must be type TEXT
395+
396+
Example usage
397+
398+
CREATE TABLE connectby_tree(keyid text, parent_keyid text);
399+
400+
INSERT INTO connectby_tree VALUES('row1',NULL);
401+
INSERT INTO connectby_tree VALUES('row2','row1');
402+
INSERT INTO connectby_tree VALUES('row3','row1');
403+
INSERT INTO connectby_tree VALUES('row4','row2');
404+
INSERT INTO connectby_tree VALUES('row5','row2');
405+
INSERT INTO connectby_tree VALUES('row6','row4');
406+
INSERT INTO connectby_tree VALUES('row7','row3');
407+
INSERT INTO connectby_tree VALUES('row8','row6');
408+
INSERT INTO connectby_tree VALUES('row9','row5');
409+
410+
-- with branch
411+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
412+
AS t(keyid text, parent_keyid text, level int, branch text);
413+
keyid | parent_keyid | level | branch
414+
-------+--------------+-------+---------------------
415+
row2 | | 0 | row2
416+
row4 | row2 | 1 | row2~row4
417+
row6 | row4 | 2 | row2~row4~row6
418+
row8 | row6 | 3 | row2~row4~row6~row8
419+
row5 | row2 | 1 | row2~row5
420+
row9 | row5 | 2 | row2~row5~row9
421+
(6 rows)
422+
423+
-- without branch
424+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
425+
AS t(keyid text, parent_keyid text, level int);
426+
keyid | parent_keyid | level
427+
-------+--------------+-------
428+
row2 | | 0
429+
row4 | row2 | 1
430+
row6 | row4 | 2
431+
row8 | row6 | 3
432+
row5 | row2 | 1
433+
row9 | row5 | 2
434+
(6 rows)
435+
327436
==================================================================
328437
-- Joe Conway
329438

contrib/tablefunc/tablefunc-test.sql

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
--
2-
-- show_all_settings()
3-
--
4-
SELECT * FROM show_all_settings();
5-
61
--
72
-- normal_rand()
83
--
@@ -47,3 +42,44 @@ select * from crosstab4('select rowid, attribute, value from ct where rowclass =
4742
select * from crosstab('select rowid, attribute, value from ct where rowclass = ''group1'' order by 1,2;', 2) as c(rowid text, att1 text, att2 text);
4843
select * from crosstab('select rowid, attribute, value from ct where rowclass = ''group1'' order by 1,2;', 3) as c(rowid text, att1 text, att2 text, att3 text);
4944
select * from crosstab('select rowid, attribute, value from ct where rowclass = ''group1'' order by 1,2;', 4) as c(rowid text, att1 text, att2 text, att3 text, att4 text);
45+
46+
-- test connectby with text based hierarchy
47+
DROP TABLE connectby_tree;
48+
CREATE TABLE connectby_tree(keyid text, parent_keyid text);
49+
50+
INSERT INTO connectby_tree VALUES('row1',NULL);
51+
INSERT INTO connectby_tree VALUES('row2','row1');
52+
INSERT INTO connectby_tree VALUES('row3','row1');
53+
INSERT INTO connectby_tree VALUES('row4','row2');
54+
INSERT INTO connectby_tree VALUES('row5','row2');
55+
INSERT INTO connectby_tree VALUES('row6','row4');
56+
INSERT INTO connectby_tree VALUES('row7','row3');
57+
INSERT INTO connectby_tree VALUES('row8','row6');
58+
INSERT INTO connectby_tree VALUES('row9','row5');
59+
60+
-- with branch
61+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~') AS t(keyid text, parent_keyid text, level int, branch text);
62+
63+
-- without branch
64+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0) AS t(keyid text, parent_keyid text, level int);
65+
66+
-- test connectby with int based hierarchy
67+
DROP TABLE connectby_tree;
68+
CREATE TABLE connectby_tree(keyid int, parent_keyid int);
69+
70+
INSERT INTO connectby_tree VALUES(1,NULL);
71+
INSERT INTO connectby_tree VALUES(2,1);
72+
INSERT INTO connectby_tree VALUES(3,1);
73+
INSERT INTO connectby_tree VALUES(4,2);
74+
INSERT INTO connectby_tree VALUES(5,2);
75+
INSERT INTO connectby_tree VALUES(6,4);
76+
INSERT INTO connectby_tree VALUES(7,3);
77+
INSERT INTO connectby_tree VALUES(8,6);
78+
INSERT INTO connectby_tree VALUES(9,5);
79+
80+
-- with branch
81+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', '2', 0, '~') AS t(keyid int, parent_keyid int, level int, branch text);
82+
83+
-- without branch
84+
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', '2', 0) AS t(keyid int, parent_keyid int, level int);
85+

0 commit comments

Comments
 (0)