AppLib FLATTEN RECURSIVE HIERARCHY

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(CURSOR(SELECT * FROM t)));

Purpose

Flattens a two-column table specifying a graph and output to another table.


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.
  • Third 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 Level1Level2Level3Level4Level5Level6Level7Level8Level9Level10Level11Level12Level13Level14Level15
2false010303030303030303030303030303
2false010404040404040404040404040404
3false010205050505050505050505050505
3false010206060606060606060606060606
3false070810101010101010101010101010
5false070809111212121212121212121212
5false070809111313131313131313131313

Source Code

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

Personal tools