Class ConnectByExpander

java.lang.Object
dev.frostlake.executor.ConnectByExpander

public final class ConnectByExpander extends Object
Snowflake hierarchical queries — [START WITH <pred>] CONNECT BY [PRIOR] c = [PRIOR] c.

The expansion walks the FROM clause's rows as a forest: the rows matching START WITH are the roots (without START WITH every row is its own root, live-verified), and a row becomes a child of the row above it whenever the CONNECT BY predicate holds with PRIOR bound to the parent and every other reference bound to the candidate child. Each emitted row carries its own values plus two pseudo-column groups, both hidden from SELECT * exactly as Snowflake hides them:

  • LEVEL — 1 for a root, +1 per step down;
  • CONNECT_BY_ROOT$<col> — the root row's value of every source column, which is what ConnectByRootExpression reads.

Snowflake applies WHERE to the EXPANDED rows (live-verified: filtering out a parent still keeps its children), so this runs before the WHERE stage and everything downstream sees an ordinary relation.

Snowflake performs no cycle detection — a cyclic hierarchy simply runs until the warehouse times out (live-verified). An in-memory engine cannot hang, so the walk is bounded by MAX_DEPTH, the same guard value the recursive-CTE fixpoint loop uses, and reports the cycle instead.

  • Field Details

    • MAX_DEPTH

      public static final int MAX_DEPTH
      Maximum hierarchy depth before the walk is reported as cyclic.
      See Also:
    • LEVEL_COLUMN

      public static final String LEVEL_COLUMN
      The LEVEL pseudo-column's name.
      See Also:
    • SHADOWED_LEVEL_COLUMN

      public static final String SHADOWED_LEVEL_COLUMN
      The out-of-the-way name a source column called LEVEL takes in the expanded relation, so the name LEVEL resolves to the depth pseudo-column.
      See Also:
  • Constructor Details

  • Method Details

    • expandedTable

      public Table expandedTable(Table baseTable)
      The relation the expansion produces: the source columns followed by the LEVEL depth pseudo-column and the hidden CONNECT_BY_ROOT$<col> pseudo-columns. The pseudo-column SHADOWS a source column of the same name — live, both the bare level and the QUALIFIED t.level answer the depth inside a CONNECT BY query, even over a table whose own LEVEL column holds other values — so a clashing source column is renamed out of resolution here. Without a clash the depth column stays hidden from SELECT *, exactly as before; with one it takes the source column's star slot.
    • expand

      public List<Row> expand(List<Row> baseRows, Table baseTable, Table expandedTable, FrostlakeParser.ConnectByClauseContext clause, Map<String,Table> aliasToTable, List<Table> allTables)
      Expand baseRows into the hierarchy described by the clause, in depth-first pre-order (parent immediately before its subtree). The result's rows match expandedTable(dev.frostlake.metastore.model.Table)'s shape.