Skip to content

closure

Package closure implements the transitive_closure virtual table: a parent-child graph walker that returns every descendant of a given node, with optional depth bounds.

CREATE VIRTUAL TABLE temp.tc USING transitive_closure(
tablename=org, idcolumn=id, parentcolumn=manager);
-- All reports (direct + indirect) of employee 42, depth ≤ 3:
SELECT id, depth FROM temp.tc
WHERE root = 42 AND depth <= 3;

For a typed Go handle over this vtab — Create / Descendants (with depth bounds and per-query retarget) / Reversed for ancestor walks — see Graph.

The vtab exposes:

id INTEGER -- the descendant rowid
depth INTEGER -- 0 for root, 1 for direct children, etc.
root HIDDEN -- the start node, required in every WHERE
tablename HIDDEN -- override the configured parent table at query time
idcolumn HIDDEN -- override the id column at query time
parentcolumn HIDDEN -- override the parent column at query time

The HIDDEN tablename/idcolumn/parentcolumn columns make it possible to point one closure vtab at many different graphs at query time; if you only ever traverse one graph, set them via the create arguments.

xFilter runs a single prepared SELECT idcolumn FROM tablename WHERE parentcolumn = ? against the configured table, then walks BFS from the root collecting every reachable id. A visited-set prevents infinite recursion on cyclic graphs.

Ported from ncruces/ext/closure, itself a Go port of the SQLite transitive_closure extension by D. Richard Hipp.


Full API: pkg.go.dev/gosqlite.org/ext/closure