-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Extract dbt {% macro %} definitions from raw .sql models #575
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or requestparsing/qualityGraph extraction bugs, false positives, missing edgesGraph extraction bugs, false positives, missing edgespriority/normalStandard review queue; useful PR with ordinary maintainer urgency.Standard review queue; useful PR with ordinary maintainer urgency.
Milestone
Description
Activity
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestparsing/qualityGraph extraction bugs, false positives, missing edgesGraph extraction bugs, false positives, missing edgespriority/normalStandard review queue; useful PR with ordinary maintainer urgency.Standard review queue; useful PR with ordinary maintainer urgency.
What problem does this solve?
dbt
.sqlmodels are Jinja-templated ({{ ref() }},{{ source() }},{% macro %}), which the SQL grammar cannot parse, and dbt's manifest only exists afterdbt compile. So in a raw checkout, models and their dependencies are invisible: a model file is only a genericModule, and a referenced model likestg_usersis not even a node, soref()lineage cannot form.Public test bed:
dbt-labs/jaffle_shop(index the rawmodels/without compiling).Proposed solution
Run an additive
tree-sitter-jinja2pass on dbt-templated.sql(files containing{{/{%):{{ ref('m') }}/{{ source('s','t') }}becomeUSAGElineage edges..sqlfile with no macro defs) becomes aModelnode keyed by file stem, so cross-file{{ ref('that_model') }}resolves into model-to-model lineage.{% macro name(...) %}becomes aMacro.Zero schema change (freeform labels + existing
USAGEedge).Modelis emitted only on the.sqlpath, so a plain.jinja/.j2template is not treated as a model. These source-levelModelnodes coexist with the manifest path (#576) without conflict.Caveat: the vendored tree-sitter-jinja2 grammar models only {{ }} expressions (so ref() / source() are parsed from the AST). It has no rule for {% %} statements, so {% macro %} names are recovered with a small text scan until a statement-aware grammar is vendored.
Alternatives considered
<script>-in-HTML) re-parse path: rejected because dbt Jinja is interleaved throughout the file, not a delimited sub-region; a full-file second parse is the right shape.{% %}statements: larger and touches grammar vendoring; deferred.Confirmations