Beginners guide for CDS Hierarchies – Part 2
This blog post is continuation to Beginners guide for CDS Hierarchies – Part 1
Part 2- In this blog post, I will be going to provide more information on types of hierarchies available with ABAP CDS views.
- Multiple parent-child hierarchy
- CDS hierarchies with parameters
- Generate spantree with CDS hierarchy
- Load Clause in transversal configurations
Source table for multiple parent child hierarchy
In a multiple parent child hierarchy, a child can have multiple parents. employee E1 has multiple parents E3 and E4. Employee E2 has multiple parents E5 and E6. If no start where condition is specified, then hierarchy table consists of only child node E12. In case of absent start where condition, the child node without parent is considered as root node. All other nodes which are not linked in hierarchy to root node are ignored as orphans (default condition orphans ignore).
The optional addition ORPHANS define the way orphan nodes are handled. The following categories of orphan nodes exist:Orphans
- Hierarchy nodes that could have parent nodes (as specified by the parent-child relationship) but the parent nodes are not in the hierarchy (known as true orphans).
- Hierarchy nodes that cannot be reached from the root node set using hierarchy edges.
- Hierarchy nodes that are part of a node cycle and cannot be reached from the root node set using hierarchy nodes (known as island orphans).
The additions work as follows:
This is the default setting (any orphan nodes are not inserted in the hierarchy).
Any orphan nodes detected raise an exception.
Orphan nodes are inserted in the hierarchy as follows:
- True orphans are included in the root node set as root nodes and flagged as orphan nodes in the hierarchy attribute HIERARCHY_IS_ORPHAN.
- Descendant nodes of true orphans are handled like descendant nodes of parent nodes from the root node set but are also flagged as orphan nodes in the hierarchy attribute HIERARCHY_IS_ORPHAN.
- For the hierarchy nodes of island orphans, a parent node in the root node set is generated for the child node where the cycle occurs. In the generated root node, all columns of the source hierarchy source contain the null value. In the hierarchy attributes, the additional root node is flagged as an orphan node and PARENT_ID also contains the null value.
The addition CYCLES define how node cycles are defined. The additions work as follows:
This is the default setting (an exception is raised when a node cycle is detected).
The traversal of descendant nodes is broken at the child node where the node cycle occurs and the hierarchy attribute HIERARCHY_IS_CYCLE is set to the value 1.
If the addition BREAKUP is specified, MULTIPLE PARENTS ALLOWED must also be specified.
CDS Hierarchies with parameters
- This feature is used when multiple hierarchies are present in a same source table/view
- Directory works as an additional filter (where clause) based on the fields of an association mentioned in the directory clause
- Multiple directories/departments in same table view
Employees E1 to E 10 belong to one department F1 and employees E11 to E13 belong to another department F2
The optional addition DIRECTORY in a CDS hierarchy defines a filter condition for the rows of the source of the hierarchy specified after BASE_CDS_VIEW. The hierarchy is generated only from those rows in the source that meet the filter condition. Comparisons for equality can be specified for conditional expressions using =:
- The operator on the left side of a comparison must be an element of the current hierarchy (dir_key1, dir_key2) whose name occurs as an operand in the BASE_CDS_VIEW on the left side of an ON condition of an association _DirAssoc.
- The operator on the right side of the comparison can be a simple literal or a type-friendly parameter from the parameter list of the hierarchy.
The same rules apply to the comparable types as to CDS views.
If start where = ‘E11’ is not included, then the information of directory with company F2 and country US is not shown. The first root node in hierarchy employee E1 is considered as default root. This results in child nodes found for root node which fit the cds view parameters.
- A filter condition removes all hierarchy nodes and their descendant nodes from the results set that do not match the condition specified in association.
- The content of the target data source of the association _directory_assoc is ignored when the filter condition is evaluated.
- The restriction of the operands on the left side of a comparison to the operands of an association of the source is supported by certain programming models. Any frameworks based on these models read data from the target data source of this association and pass it to input parameters of the hierarchy that evaluates them in the filter condition.
Generate spantree with CDS hierarchy
- Generates a hierarchy on recursive parent-child source containing only shortest paths between start and result nodes.
- If a graph that is not a well-formed tree, a spanning tree/forest will be computed, starting from the root nodes and traversing the source graph in breadth-first order.
- The value of the HIERARCHY_LEVEL attribute is equal to the distance from the root node +1, it corresponds to the shortest path in the source graph from a root to the node.
Graph(not well-formed tree) Spanning tree
When the start (E12) and result nodes (E11) in a tree are not connected through shortest path and the tree is not well-formed, it is recommended to generate a spanning tree. Spantree generates a hierarchy between parent-child using only the shortest paths between start and result nodes.
Considerations for spantree generation
- multiparent”, “cycles” and “orphan” specifications are not supported with SPANTREE function
- Technical fields returned by this function is the same as standard hierarchy function
Spantree generation in CDS hierarchy
Next Blog Post :
Part 3 – ABAP SQL in CDS hierarchies, ADT tools for hierarchies, Temporal hierachies and tips
Images: self made