Skip to content

fix(tsql): drop test result helper tables on run end - #1111

Draft
tderk wants to merge 1 commit into
elementary-data:masterfrom
tderk:fix/tsql-test-result-relation-cleanup
Draft

tderk wants to merge 1 commit into
elementary-data:masterfrom
tderk:fix/tsql-test-result-relation-cleanup

Conversation

@tderk

@tderk tderk commented Dec 4, 2026 •

Copy link
Copy Markdown

Optional proposal. #1109 describes the problem, its root cause and a reproduction on its own. This PR is one possible fix, verified on a Fabric warehouse but not yet in CI (needs the fork approval). Happy to adapt it or close it if you prefer a different approach.

Closes #1109

Summary

On T-SQL the test materialization stores each test result in a regular table in the elementary schema, and the
failed row count copied it into another <alias>__tmp_<ts> table. Neither was ever dropped.

  • Register the test result relation in temp_test_table_relations_map on T-SQL, so the existing on-run-end
    cleanup drops it.
  • With store_failures, skip the helper table and point sql at dbt's audit relation after the
    materialization (use_stored_failures_as_test_sql). This keeps store_failures_as: view valid after cleanup.
  • Remove fabric__/sqlserver__get_failed_row_count_calc_query. At that point sql is already
    select * from <helper relation> on T-SQL, so the default implementation applies.

Non-T-SQL adapters are unaffected: registration is limited to is_tsql(), and the store_failures branch only
applies where the helper relation is used (is_tsql() or tests_use_temp_tables).

Verification

Fabric Data Warehouse, dbt-fabric 1.10.0post2, elementary schema in a different warehouse than
target.database. One model, six tests (unique, not_null, accepted_values with store_failures_as: table,
dbt_utils.unique_combination_of_columns, dbt_utils.expression_is_true, unique with store_failures_as: view).

master this PR (combined with the Fabric view fix)
Non-model objects in the elementary schema after one dbt test 8 0
failed_row_count for unique (sum(n_records)) 2 2
Samples, store_failures audit relations ok ok

Executed queries before / after:

-- master
select * into [..].[edr_t_unique__tmp_20261204130702845283] from ( select * from [..].[edr_t_unique] ) as __edr_inner
-- this PR
with results as ( select * from [..].[edr_t_unique] ) select sum(n_records) as count from results

Integration tests added in integration_tests/tests/test_tsql_test_result_relations.py (fabric, sqlserver).
I could not run them locally; they need the fork approval to run in CI.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved failed-row reporting for tests on Fabric and SQL Server, including tests that store failures as tables or views.
    • Test results now use stored failure data when available, helping ensure reported failure counts match the saved rows.
    • Tests that do not store failures no longer leave an unexpected result relation behind.
  • Tests
    • Added integration coverage for test-result relations and stored-failure row counts on Fabric and SQL Server.

On T-SQL the test materialization stores each test result in a regular table
in the elementary schema. It was never registered for cleanup, so one table
per test stayed behind. The failed row count additionally copied the result
into a timestamped table per failing test and run, also never dropped.

- register the test result relation in temp_test_table_relations_map
- with store_failures, reuse dbt's audit relation instead of a helper table,
  so store_failures_as=view keeps working after cleanup
- read the failed row count from the already flat test SQL

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The T-SQL test materialization now records result relations, avoids creating its own result relation when dbt stores failures, and can use dbt’s returned stored-failure relation as test SQL. Fabric and SQL Server use the default CTE-based failed-row-count query. Integration tests cover relation lookup and stored-failure row counts.

Changes: T-SQL test-result handling

Layer / File(s) Summary
Test-result relation lifecycle
macros/edr/materializations/test/test.sql, macros/edr/materializations/test/failed_row_count.sql
The materialization gates temporary result-table creation on the relation-use condition and whether dbt stores failures. It records T-SQL result relations and can use the first relation returned by dbt as test SQL when storing failures. The Fabric- and SQL Server-specific failed-row-count macros were removed, so those adapters use the default CTE-based query.
Relation lookup and integration tests
integration_tests/dbt_project/macros/tsql_relation_helpers.sql, integration_tests/tests/test_tsql_test_result_relations.py
New helpers list matching relations and count rows in a stored-failure relation. Fabric and SQL Server tests check failing test results, relation lookup, and stored-failure row counts.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant dbt_test_runner
  participant handle_dbt_test
  participant test_materialization
  participant temp_test_table_relations_map
  participant use_stored_failures_as_test_sql
  dbt_test_runner->>handle_dbt_test: Run test materialization
  handle_dbt_test->>test_materialization: Execute materialization
  test_materialization->>temp_test_table_relations_map: Record T-SQL result relation
  test_materialization-->>handle_dbt_test: Return materialization result
  handle_dbt_test->>use_stored_failures_as_test_sql: Pass materialization result
  use_stored_failures_as_test_sql-->>handle_dbt_test: Use first returned relation as test SQL when storing failures
Loading

Suggested reviewers: elazarlachkar, haritamar, guyeshdat

Merge Risk: Minimal · up to 33c55

This change drops T-SQL test-result helper tables at run end and uses dbt's stored-failure relation for store_failures. No concrete merge-blocking risk was found. CI should confirm the new integration tests.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(tsql): drop test result helper tables on run end" matches the main change. The PR registers the test-result relation for on-run-end cleanup on T-SQL, stops creating the failed-row-count…
Linked Issues check Passed [#1109] The directly linked issue has three coding requirements. (1) Register the test-result relation in temp_test_table_relations_map on T-SQL so on-run-end cleanup drops it. Implemented in create_t…
Out of Scope Changes check Passed All changed areas connect to #1109. The test.sql changes implement the relation registration, the store_failures audit-relation reuse, and the helper macros should_use_test_result_relation and use_sto…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "fix(tsql): drop test result helper tables on run end" matches the main change. The PR registers the test-result relation for on-run-end cleanup on T-SQL, stops creating the failed-row-count helper table, and uses dbt's audit relation for store_failures. The title names the T-SQL scope and the cleanup behavior. It is concise and uses a conventional commit prefix. It does not mention the store_failures change, but that is a secondary detail.

Full details: Linked Issues check

Explanation

[#1109] The directly linked issue has three coding requirements. (1) Register the test-result relation in temp_test_table_relations_map on T-SQL so on-run-end cleanup drops it. Implemented in create_test_result_temp_table (macros/edr/materializations/test/test.sql at head): when is_tsql() is true, the relation from create_temp_table is stored under model.unique_id in temp_test_table_relations_map. (2) With store_failures, skip the helper table and point sql at dbt's audit relation after materialization. Implemented: materialize_test skips create_test_result_temp_table when dbt.should_store_failures() is true, and handle_dbt_test calls use_stored_failures_as_test_sql(result), which sets sql to select * from the first relation returned by dbt. (3) Remove fabric__ and sqlserver__get_failed_row_count_calc_query. Confirmed at head: failed_row_count.sql now contains only the default__ implementation, which wraps sql in a results CTE. The integration tests in integration_tests/tests/test_tsql_test_result_relations.py cover the store_failures and failed-row-count paths. The author reports a manual Fabric run with 0 leaked objects versus 8 on master. The author states the tests were not run in CI. The alias-based drop-if-exists healing behavior depends on the existing create_temp_table, which was not re-read in this review. That behavior is a rationale in the issue, not a separate requirement, so it does not change the verdict.

Full details: Out of Scope Changes check

Explanation

All changed areas connect to #1109. The test.sql changes implement the relation registration, the store_failures audit-relation reuse, and the helper macros should_use_test_result_relation and use_stored_failures_as_test_sql. The failed_row_count.sql change removes the Fabric and SQL Server overrides that create the _tmp helper table. The integration_tests macros and test file exist only to verify the new relation behavior. The tests_use_temp_tables condition in should_use_test_result_relation extends the store_failures path to non-T-SQL adapters when that flag is set. The PR description states this explicitly, and it is a design choice tied to the same helper relation, not an unrelated change. No unrelated change was found in the inspected files.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6adcc342-aa16-4c04-8c05-63a29e80ff07
Commits

Reviewing files that changed from the base of the PR and between 09516ff and 33c5520.

Files selected for processing: 4

  • integration_tests/dbt_project/macros/tsql_relation_helpers.sql
  • integration_tests/tests/test_tsql_test_result_relations.py
  • macros/edr/materializations/test/failed_row_count.sql
  • macros/edr/materializations/test/test.sql
Files with no reviewable changes: 1

  • macros/edr/materializations/test/failed_row_count.sql

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@jonlevine7

jonlevine7 commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

This branch has not been deployed

No deployments
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.

T-SQL: test result helper tables are never dropped (one per test, plus one per failing test and run)

2 participants