Hierarchical tables
A hierarchical table shows a table's rows as a structure that you expand and collapse, so you can start from a summary and drill down only into the parts you care about. Deephaven has two kinds: rollup tables, which aggregate rows into levels of groups, and tree tables, which arrange rows by a parent-child relationship stored in the data. Both are built from an ordinary source table, and both keep updating as that source ticks, so a live feed of millions of rows becomes a few summary rows that stay current. This guide explains what hierarchical tables are, how the two kinds differ, and how they behave with ticking data and in the UI.
What is a hierarchical table?
A hierarchical table organizes the rows of a source table into nodes that you expand and collapse. The terms it uses are defined in Shared vocabulary.
A hierarchical table is not a Table. The rollup method returns a RollupTable, and the tree method returns a TreeTable. Neither one supports table operations such as where, update, or natural_join. Run those operations on the source table first, and then create the hierarchical table from the result. A hierarchical table has its own, smaller set of methods, described in Work with a hierarchical table. Methods that change it return a new hierarchical table, and the original is unchanged.
Shared vocabulary
Both kinds of hierarchical table use the same terms:
- Node: A set of rows that share a parent. In a rollup table, a node holds the rows below one aggregated row. In a tree table, a node holds the rows that name one row as their parent.
- Root: The top node. Every other node hangs below a row in the node above it.
- Children: The rows in the node directly below a row. When you expand a row in the UI, Deephaven shows its children. When you collapse it, they're hidden again.
- Leaf: A row with no children.
- Depth: How far a row is below the root. The UI indents each row by its depth.
- Expansion: Which rows are showing their children. Expansion is part of what the UI asks the server for, not part of the hierarchical table itself, so two users can view the same table with different rows expanded.
- Constituents: In a rollup table created with
include_constituents=True, the rows from the source table below each leaf group. Constituent rows have the source table's columns and types. - Orphan: In a tree table, a row whose parent ID is not null but matches no row's ID.
Rollup tables and tree tables
The two kinds of hierarchical table get their structure from different places. A rollup table creates its hierarchy by aggregating: you name the group-by columns, and Deephaven computes a level of aggregated rows for each one. A tree table finds its hierarchy in the data: every row has an ID, and every row names its parent's ID.
| Rollup table | Tree table | |
|---|---|---|
| Created with | rollup | tree |
| Where the hierarchy comes from | The group-by columns. Each level groups by one more column, from left to right. | An ID column and a parent column. Each row's parent column holds the ID of its parent row. |
| What each row is | An aggregated row for one group, or, if you include constituents, a row from the source table | A row from the source table |
| The root | A single row that aggregates every row in the source table | Not a row. The root node holds every row whose parent is null, so a tree can have many top-level rows. |
| Leaves | The rows grouped by every group-by column, or, with constituents included, the source rows below them | Rows that no other row names as its parent |
| Requirements | Some aggregations aren't supported. See the rollup how-to guide. | IDs must be unique, and the ID and parent columns must have the same type. |
| Rows left out | None | Orphans and their descendants, unless you promote them with promote_orphans=True |
The following example builds both kinds from the same table of employees. rollup_result totals the salaries by department and then by team. tree_result arranges the employees by who manages whom:
In rollup_result, the top row is the company total. Expanding it shows one row per department, and expanding a department shows its teams. A rollup table keeps every group-by column at every level, so each level has the same columns, and a level that doesn't group by a column shows null: the total row shows null in both Dept and Team, and the department rows show null in Team. In tree_result, Ana is the only top-level row, because she is the only employee with a null ManagerID.
Work with a hierarchical table
Most of a hierarchical table's methods fall into two groups: filters, and operations that apply to each node.
Filter
Both kinds of hierarchical table can be filtered, but they filter in different ways:
RollupTable.with_filtersfilters the rows before they're aggregated and then recomputes every level, so the totals in the parent rows count only the rows that pass the filter. A filter can use group-by columns and other source columns, but not a column that an aggregation summarizes, such asSalaryinagg.sum_("Salary").TreeTable.with_filterskeeps the rows that match and every ancestor of each match, so a matching row is still reachable from the root. It doesn't add a matching row's descendants, so a row below a match stays only if it also matches or is an ancestor of another match.
Neither accepts a filter that reads other rows, such as one that uses a virtual row variable like i or a column array. Apply such a filter to the source table first.
The following example filters both tables from the previous example. In rollup_filtered, the total row counts only the two employees on the Core team, so it shows a salary of 320 and a headcount of 2. In tree_filtered, Dee matches, and her manager Ben and his manager Ana stay so that Dee can still be reached from the root:
Apply operations to each node
A node operations recorder holds operations that Deephaven applies to every node: sorting, formatting, and either adding columns (rollup tables) or filtering (tree tables). To use one:
- Get a recorder from
RollupTable.node_operation_recorderorTreeTable.node_operation_recorder. - Record operations on it.
- Pass it to
RollupTable.with_node_operationsorTreeTable.with_node_operations, which returns a new hierarchical table.
Operations from a recorder apply to each node separately. A sort, for example, orders the children of each row among themselves. It doesn't move a row out from under its parent.
- A
RollupNodeOperationsRecordercan sort and format each node, and add columns with itsupdate_viewmethod. A rollup table's aggregated rows and its constituent rows take separate recorders, chosen withNodeType. - A
TreeNodeOperationsRecordercan sort and format each node, and filter it with itswheremethod. UnlikeTreeTable.with_filters, it filters each node on its own and doesn't keep the ancestors of matching rows.
A rollup table also has with_update_view, a shortcut that adds columns to the aggregated rows.
Hierarchical tables and ticking data
A hierarchical table updates as its source table ticks. Deephaven recomputes only the parts that change: a new source row updates the aggregates on its path to the root of a rollup table, or adds a row to one node of a tree table. A hierarchical table built from a live data feed therefore stays current without being rebuilt.
Some sources don't work with hierarchical tables:
-
Blink tables keep only the rows from the current update cycle, so they can't supply the history that some parts of a hierarchy need:
treeraises an error.rollupraises an error withinclude_constituents=True, or withagg.first,agg.last,agg.sorted_first,agg.sorted_last,agg.group, oragg.formula.- Other aggregations, such as a sum or a count, work and summarize every row seen since the rollup started.
To use any of these, first convert the blink table with
blink_to_append_only. -
Duplicate IDs in a tree: Each ID in a tree table must be unique. If the source table already has a duplicate ID,
treeraises an error. If a duplicate ID arrives on a later update, Deephaven reports an error and the tree table can no longer update correctly.
View and navigate in the UI
The web UI shows a hierarchical table as a grid with an arrow next to each row that has children. Click the arrow to expand or collapse that row. Each level is indented below its parent.
When you open a rollup table, its root row is expanded one level, so you see the grand total and the first level of groups. When you open a tree table, its top-level rows are showing, all collapsed.
When a client such as the web UI displays a ticking hierarchical table, the server sends a fresh copy of every row the client can see whenever the source table changes, not only the rows that changed. It sends these no more often than the subscription's update interval. The JavaScript API requests an interval of one second unless the client asks for a different one. The server's default interval — also one second — applies only to a subscription that doesn't request one. See What is Barrage? for how the update interval works.
From the JavaScript API, a client works with both kinds through dh.TreeTable, which can expand, collapse, sort, and filter rows on the server.
Related documentation
- Aggregations in Deephaven
- Incremental update model
- Table types
- What is Barrage?
- Create a hierarchical rollup table
- Create a hierarchical tree table
rolluptreewhereupdatenatural_joinblink_to_append_onlyagg.sum_agg.firstagg.lastagg.sorted_firstagg.sorted_lastagg.groupagg.formulaTableRollupTableTreeTableRollupNodeOperationsRecorderTreeNodeOperationsRecorderNodeTypedh.TreeTable