LucidDbAppLib FLATTEN RECURSIVE HIERARCHY ALL LEVELS

From Eigenpedia

Jump to: navigation, search

Contents

Syntax

-- t is a table of 2 columns (parent and child)
SELECT * FROM TABLE(APPLIB.FLATTEN_RECURSIVE_HIERARCHY_ALL_LEVELS(CURSOR(SELECT * FROM t)));

Purpose

Flattens a two-column table specifying a graph and outputs to another table. Unlike the other variation (see link in Notes), this variation creates rows for non-leaf vertices as well.


Each row of the input table specifies an edge of a forest (first column contains parent nodes, second column contains child nodes). Each row of the output table represent a flattened path from a root to a leaf.

  • The first column contains integer values representing the actual number of vertices on the path.
  • The second column contains boolean values true if there exists multiple paths that end at the leaf node of the path.
  • The third column contains boolean values indicating whether the output row has a non-leaf starting node.
  • Fourth to last columns contain vertices along the path starting from the root ending at the leaf.

If the number of vertices is less than maxDepth, the path is right-padded with value of the leaf. If the number of vertices is greater than maxDepth, the path is truncated (actual number of vertices on the path before truncating is shown in first column).

Notes:

Parameters

Input table t must:

  • have exactly 2 columns as described above;
  • be acyclic
  • have non-null child node (parent node can be null)

Examples

CREATE TABLE tree1(parent VARCHAR(128), child VARCHAR(128));
INSERT INTO tree1 VALUES
   ('01','02'),
   ('01','03'),
   ('01','04'),
   ('02','05'),
   ('02','06'),
   ('07','08'),
   ('08','09'),
   ('08','10'),
   ('09','11'),
   ('11','12'),
   ('11','13');
SELECT *
FROM TABLE (APPLIB.FLATTEN_RECURSIVE_HIERARCHY(CURSOR(SELECT * FROM tree1)));

The result of the above set of queries is:

Vertices Multipath Non_leaf Level1Level2Level3Level4Level5Level6Level7Level8Level9Level10Level11Level12Level13Level14Level15
2falsetrue010202020202020202020202020202
1falsetrue010101010101010101010101010101
2falsefalse010303030303030303030303030303
2falsefalse010404040404040404040404040404
3falsefalse010205050505050505050505050505
3falsefalse010206060606060606060606060606
2falsetrue070808080808080808080808080808
1falsetrue070707070707070707070707070707
3falsetrue070809090909090909090909090909
3falsefalse070810101010101010101010101010
4falsetrue070809111111111111111111111111
5falsefalse070809111212121212121212121212
5falsefalse070809111313131313131313131313

Source Code

http://p4web.eigenbase.org/open/dev/luciddb/src/com/lucidera/luciddb/applib/cursor/FlattenRecursiveHierarchyUdx.java

Personal tools