Skip to content

fix(lineage): unify dbt model producer and consumer nodes (#32) - #42

Merged
melonamin merged 8 commits into
masterfrom
fix/issue-32-dbt-multi-model-lineage
Apr 23, 2026
Merged

melonamin merged 8 commits into
masterfrom
fix/issue-32-dbt-multi-model-lineage

Conversation

@melonamin

Copy link
Copy Markdown
Member

Summary

  • Fixes dbt model is single #32 — dbt multi-model chains now render as connected lineage instead of disconnected per-file fragments.
  • Materializes a dbt model's bare SELECT as the canonical Table node (sharing its id with consumers) instead of a per-statement Output node, so the flattener unifies producer and consumer by canonical name.
  • Passes the sink id through to analyze_query as the target, so table-level DataFlow edges are emitted between models (matching the shape CTAS already produces).

Before: supplies, stg_supplies, int_supplies show up as disconnected boxes in mermaid and fct_supplies is missing entirely.
After: supplies -> stg_supplies -> int_supplies -> fct_supplies renders as one connected chain.

Test plan

  • New regression test dbt_chained_models_unify_producer_and_consumer_nodes covering a 3-model A -> B -> C chain (asserts node unification, absence of stray Output nodes, cross-statement edges, and table-level DataFlow arrows).
  • Three existing dbt tests updated to assert the corrected Table sink shape instead of the old Output shape.
  • cargo test --workspace passes.
  • cargo clippy --workspace --all-targets -- -D warnings clean.
  • cargo fmt --check clean.
  • CLI mermaid repro on a 3-file dbt chain shows the full raw.supplies -> stg -> int -> fct lineage.
  • Reviewer: visual check in Vite dev server (just build-wasm && just build-ts && just dev) with a dbt-style 3-model repro, plus diamond and unreferenced-leaf cases.

In dbt mode, a bare SELECT previously emitted a per-statement Output
node while downstream consumers referencing the same model via
`{{ ref(...) }}` emitted separate Table nodes. With matching canonical
names but different node types, the flattener could not merge them,
leaving multi-hop chains (A -> B -> C) as disconnected fragments and
omitting leaf models from the mermaid table view.

Materialize the dbt sink as the canonical relation node and pass its
id to analyze_query as the target, so producer and consumer collapse
into one node and table-level DataFlow edges connect the chain - the
same shape CTAS already produces.
Preserve dbt view materialization, keep producer occurrence metadata on merged model nodes, and treat dbt relation sinks as writes in script dependency views.
Scope `materialized` kwarg parsing to the body of the first `config(...)` call via a paren-depth scanner that respects string literals, eliminating false positives from comments or unrelated SQL. Extend `DbtMaterialization` to cover table/incremental/snapshot/ephemeral/materialized_view, with ephemeral and materialized_view correctly mapped to `NodeType::View`. Pre-declare view-materialized dbt models during DDL pre-collection so consumers resolved before the producer still merge onto the canonical view sink, and rework `getCreatedRelationNodeIds` to identify statement-local projection columns via edge-shape instead of `qualifiedName`, which can be inherited across producer/consumer merges.
Bundle analyze_statement source params into a StatementSource struct
so the signature drops under clippy's 7-arg limit, rename the sink
node id field to match its broadened semantics (Output node or
canonical relation sink), and avoid a String round-trip on the dbt
sink id by keeping it as Option<Arc<str>>.

Extend dbt materialization parsing to honor multiple config(...)
calls (last materialized= wins, matching dbt's override behavior)
and predeclare model producers so forward refs resolve without
leaving placeholder nodes behind. Extract isCreatedProjectionColumn
and isRelationNode helpers in lineageHelpers.ts so the projection
detection rule reads as a named predicate rather than an inline
boolean soup.

Add tests covering unknown adapter materializations, dynamic Jinja
materializations, later-config-wins override, and forward-ref
resolution.
…tion

- Detect duplicate dbt model names across files and emit SCHEMA_CONFLICT
  instead of silently letting the later definition win.
- Warn when `config(materialized=...)` is present but the value can't be
  resolved (dynamic Jinja, unknown adapter), so users know we fell back
  to table.
- Bound the `config(...)` body scanner with iteration and paren-depth
  limits, and reuse a single lowercased buffer across calls.
- Reject inverted byte ranges when deriving the dbt producer span and
  fall back through the templated range before giving up.
- Split precollection so it only declares a relation's type (via new
  `declare_table` / existing `declare_view` / `declare_ephemeral`);
  producer indices stay owned by the main analysis pass. `is_declared`
  on the tracker keeps forward `ref(...)` consumers from misfiring
  UNRESOLVED_REFERENCE.
- Clarify docs on `getCreatedRelationNodeIds` and
  `isCreatedProjectionColumn` to reflect written-relation semantics and
  the per-case heuristic. Add escaped-quote coverage and drop a
  redundant assertion in the forward-ref test.
Addresses multi-model review feedback by documenting the declare-vs-produce
split on CrossStatementTracker, the dual shape of StatementContext sink ids,
and the projection-column classification rule with worked examples. Also
rewrites the duplicate-dbt-model warning to reflect actual behavior (first
definition wins for identity; later producer still claims the producer index).
…n occurrences

Mark dbt model sinks with a `dbtModelSink` metadata flag so `hide_ctes`
keeps ephemeral model sinks in the global graph, and treat them as
written relations in script-level graph building and matrix dependency
extraction. Track definition occurrences separately so statement-scoped
views surface the producer span before any consumer refs.
…ache

Apply two fixes from code review: clarify the duplicate dbt-model warning
so the split between "first definition wins materialization" and "last
producer wins lineage" is unambiguous, and make `record_view_produced`
remove the canonical name from `declared_tables` to uphold the
declared-set disjointness invariant documented on `CrossStatementTracker`.

Also bump the analysis cache sentinel to `v5`. The flatten refactor on
master moved `nodes`/`edges` to the top level of `AnalyzeResult` without
bumping `HASH_VERSION`, so pre-flatten v4 cache entries rehydrated into
a slot where `result.nodes` was undefined and crashed
`useSearchSuggestions`. The bump also covers this branch's analyzer
changes (dbtModelSink, definition occurrences, ephemeral model sinks).
@melonamin
melonamin merged commit 1095968 into master Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbt model is single

1 participant