dbt

Junior

Why does my dbt macro compile to something completely different than what I expected?

A macro is a template, not the SQL that runs. Debugging the template instead of what it actually produced is debugging the wrong layer entirely.

Read in:
Debug the macro Debug compiled SQL
Where the actual answer lives - not a measured breakdown.

The macro reads correctly. The compiled SQL doesn't look anything like what the macro reads - and that gap is almost always one of three specific Jinja behaviors, not a dbt bug.

Read the compiled SQL, not the macro

dbt compile writes the fully rendered SQL for every model to target/compiled/ - that file, not the Jinja template, is the real source of truth for what a macro actually produced. Staring at the macro's source and reasoning about what it should output is debugging a different layer than the one that actually runs.

# the rendered SQL, not the template, is the ground truth dbt compile --select my_model cat target/compiled/my_project/models/my_model.sql

Whitespace control changes what Jinja even considers code

{%- ... -%} trims the whitespace and newlines immediately surrounding a tag. Leaving the dashes off inside a loop or conditional inside a macro is the single most common reason compiled SQL comes out with stray blank lines, or worse, a trailing comma left behind from a loop iteration that didn't trim the way it looked like it would.

Variable scoping inside a macro isn't what it looks like

A variable set with {% set x = ... %} inside a {% for %} loop doesn't persist outside that loop's scope the way a plain variable would in most languages - this is a routine Jinja behavior, not a dbt-specific quirk, and the usual fix when a macro needs to accumulate a value across loop iterations is a namespace() object instead of a bare set.

See what this looks like on your own pipeline.

Upload a real Spark event log, dbt run_results.json, or Flink metrics export and get concrete, approve-before-apply recommendations back - not another rule of thumb.