Skip to content

fix(test): rewrite references in helper CTEs and order them after their dependencies - #1019

Merged
kvlonge merged 2 commits into
mainfrom
fix/helper-cte-references
Oct 8, 2026
Merged

kvlonge merged 2 commits into
mainfrom
fix/helper-cte-references

Conversation

@kvlonge

@kvlonge kvlonge commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Why

In a SQL unit test, a helper CTE that called __ref("model") was emitted with the call left unresolved and placed before the mock and model CTEs, so sqb compile passed but sqb test failed in the database. When only a helper referenced a model, the test was silently skipped (0 selected).

Changes

  • Only helpers the test reads (from __expected__/__assert__ CTEs, through helpers and mocks, respecting nested CTE scope) count; unread helpers behave as on main.
  • Native planner: read helpers that call references are resolved after the model chain like __assert__ CTEs (mocks, models, sources, seeds), with or without SQL analysis, and emitted after what they read. Output for tests whose helpers call no references is unchanged.
  • Unmocked __ref() targets of read helpers count as test targets (computed once in Python and passed to the planner), so sqb test --select <model> runs a test even when only a helper reads the model.
  • New located P013 error with exact-syntax help: a read helper's __ref() to an unknown model, a read cycle between test CTEs, and a mock or __table_fn__ fixture the test uses reading a helper that calls a reference.
  • Testing docs describe references in helpers and the P013 limits; the failure corpus gains a P013 case.

Verification

  • E2E DuckDB tests on both engines: the reported project, helper-only selection, helper→helper with a mock (also with sql_analysis = false, asserting inlined helpers), a mocked model read by a helper and an assertion, unread helpers and mocks ignored, nested CTE shadowing still P001, and each P013 case.
  • Rust tests for ordering, rewriting, inline expected rows, shared mock CTEs and unread helpers.
  • cargo test --workspace --all-features, clippy and fmt on 1.98.0, make compiler-differential, make compiler-differential-shipped, tests/e2e/scripts/compiler_differential, make check-ci, make test.

…ir dependencies

Helper CTEs in SQL tests now resolve __ref(), __source(), __seed() and the other
reference calls exactly like __assert__ CTEs, are emitted after the mocks and
models they read, and count their unmocked __ref() targets when selecting the
test. Helper references compile cannot resolve (unknown models, cycles, and
mocks or fixtures reading a referencing helper) fail at compile with P013.
Resolve, check and count references only in helpers reachable from the test's
expected rows and assertions, so unread helpers and mocks behave as before.
Inlined helpers now resolve in expected rows too, relations resolved helpers
read win over a step's own copy so assertions and helpers share one mock CTE,
nested CTEs shadowing a helper keep the existing redefinition error, and the
mock check only covers mocks and fixtures the test uses.
@kvlonge
kvlonge enabled auto-merge (squash) October 8, 2026 04:05
@kvlonge
kvlonge merged commit 620bca0 into main Oct 8, 2026
40 checks passed
@kvlonge
kvlonge deleted the fix/helper-cte-references branch October 8, 2026 04:35
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.

1 participant