Skip to content

fix(macros): resolve_template fails in audits when this_model is a subquery - #5931

Open
sravankumarkunadi wants to merge 1 commit into
SQLMesh:mainfrom
sravankumarkunadi:fix-resolve-template-audit-subquery
Open

fix(macros): resolve_template fails in audits when this_model is a subquery#5931
sravankumarkunadi wants to merge 1 commit into
SQLMesh:mainfrom
sravankumarkunadi:fix-resolve-template-audit-subquery

Conversation

@sravankumarkunadi

@sravankumarkunadi sravankumarkunadi commented Jul 30, 2026

Copy link
Copy Markdown

Description

@resolve_template raises AttributeError: 'Subquery' object has no attribute 'catalog' in an audit attached to a model with a time_column (so, any INCREMENTAL_BY_TIME_RANGE model).

The macro assumes @this_model is a table and calls .catalog / .db / .name on it. But Model.render_audit_query only passes a table when there's no time filter — otherwise it passes a subquery restricting the physical table to the audited range:

"this_model": exp.select("*").from_(quoted_model_name).where(where).subquery()
if where is not None
else quoted_model_name,

exp.to_table() returns non-Table expressions untouched, so the Subquery reaches .catalog and raises. #3991 fixed the where is None branch; the time-column branch still produces the subquery.

This unwraps the subquery to the table it selects from before resolving the template. @this_model is unchanged — audits still get the time-filtered subquery — only the catalog/schema/table lookup reaches through it, which is what those placeholders mean. That makes mode := 'table' usable in audits that need the full materialized table rather than the current batch.

Closes #5927

Test Plan

  • test_resolve_template_subquery (test_macros.py) — mode := 'table' with a subquery this_model; fails on main with the reported AttributeError.
  • test_resolve_template (test_audit.py) — end to end through render_audit_query on an incremental model, both modes; the literal case also asserts @this_model still renders as the time-filtered subquery.
  • pytest tests/core/test_macros.py tests/core/test_audit.py tests/core/test_model.py -m "not slow and not docker" → 487 passed vs 485 on main (the 2 new tests). Both runs hit the same 27 errors, all ConfigError: Materialization strategy ... was not found — test fixture entry points missing from my venv, none in the two files touched here.
  • make style clean on the changed files.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) — ran the three files above rather than the full suite, see the pre-existing local errors noted there
  • My commits are signed off (git commit -s) per the DCO

…bquery

Signed-off-by: sravankumarkunadi <sravankumarkunadi@users.noreply.github.com>
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.

Bug: @resolve_template fails in audits on INCREMENTAL_BY_TIME_RANGE models (this_model is a Subquery)

1 participant