Skip to content

fix(fabric): drop leaked __dbt_tmp_vw helper view after CTAS#1011

Open
tderk wants to merge 1 commit into
elementary-data:masterfrom
tderk:fix/fabric-create-table-helper-view-cleanup
Open

fix(fabric): drop leaked __dbt_tmp_vw helper view after CTAS#1011
tderk wants to merge 1 commit into
elementary-data:masterfrom
tderk:fix/fabric-create-table-helper-view-cleanup

Conversation

@tderk
Copy link
Copy Markdown

@tderk tderk commented May 22, 2026

dbt-fabric's fabric__create_table_as implements CTAS via a helper view <table>__dbt_tmp_vw: it creates the view, then issues CREATE TABLE <table> AS SELECT * FROM <table>__dbt_tmp_vw. The macro does NOT drop the helper view at the end — it relies on the caller (dbt-fabric's own incremental / table materializations) to drop it via adapter.drop_relation after the CTAS.

Elementary's edr_create_table_as does not perform that post-drop, so the helper view leaks in the elementary schema every time edr_create_table_as is invoked on Fabric. Affects every artifact table built this way: dbt_columns, dbt_exposures, dbt_seeds, dbt_sources, dbt_tests, plus their _tmp intermediates from delete_and_insert. The leaked views accumulate one entry per run and per artifact, eventually polluting the schema.

Add a fabric__edr_get_create_table_as_sql dispatch implementation that calls dbt.get_create_table_as_sql and appends
EXEC('DROP VIEW IF EXISTS ...') to the emitted SQL, so the helper view is dropped in the same batch that consumed it. Uses IF EXISTS, so it is safe even if a future dbt-fabric release decides to clean up the helper view itself.

Upstream macro reference:
dbt-fabric/dbt/include/fabric/macros/materializations/models/table/create_table_as.sql

Summary by CodeRabbit

  • New Features
    • Improved CREATE TABLE AS operations in Fabric with automatic cleanup of temporary objects. The cleanup process is now idempotent, ensuring operations can be safely retried without errors.

Review Change Stack

dbt-fabric's fabric__create_table_as implements CTAS via a helper view
`<table>__dbt_tmp_vw`: it creates the view, then issues
`CREATE TABLE <table> AS SELECT * FROM <table>__dbt_tmp_vw`. The macro
does NOT drop the helper view at the end — it relies on the caller
(dbt-fabric's own incremental / table materializations) to drop it via
`adapter.drop_relation` after the CTAS.

Elementary's edr_create_table_as does not perform that post-drop, so the
helper view leaks in the elementary schema every time edr_create_table_as
is invoked on Fabric. Affects every artifact table built this way:
dbt_columns, dbt_exposures, dbt_seeds, dbt_sources, dbt_tests, plus
their __tmp_<ts> intermediates from delete_and_insert. The leaked views
accumulate one entry per run and per artifact, eventually polluting the
schema.

Add a fabric__edr_get_create_table_as_sql dispatch implementation that
calls dbt.get_create_table_as_sql and appends
`EXEC('DROP VIEW IF EXISTS ...')` to the emitted SQL, so the helper view
is dropped in the same batch that consumed it. Uses IF EXISTS, so it is
safe even if a future dbt-fabric release decides to clean up the helper
view itself.

Upstream macro reference:
dbt-fabric/dbt/include/fabric/macros/materializations/models/table/create_table_as.sql
@tderk tderk marked this pull request as ready for review May 22, 2026 09:58
@github-actions
Copy link
Copy Markdown
Contributor

👋 @tderk
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in the elementary repository.

@tderk tderk requested a deployment to elementary_test_env May 22, 2026 09:58 — with GitHub Actions Waiting
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e64c5765-ebfb-4418-aaca-32cdcd31d279

📥 Commits

Reviewing files that changed from the base of the PR and between ab1a10b and 1be8214.

📒 Files selected for processing (1)
  • macros/utils/table_operations/create_table_as.sql

📝 Walkthrough

Walkthrough

This PR adds a Fabric-specific CTAS macro implementation that extends dbt's standard get_create_table_as_sql to explicitly drop the temporary helper view (__dbt_tmp_vw) created during Fabric's CTAS process, appending a cleanup EXEC statement to the generated SQL.

Changes

Fabric CTAS with Helper View Cleanup

Layer / File(s) Summary
Fabric CTAS macro with helper view cleanup
macros/utils/table_operations/create_table_as.sql
The fabric__edr_get_create_table_as_sql macro wraps dbt's CTAS SQL generation and appends an EXEC('DROP VIEW IF EXISTS ...') statement to drop the Fabric CTAS helper view, ensuring intermediate views do not persist in the schema.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 A fabric thread once loose and wild,
Now cleaned up, tidy, reconciled,
Drop view commands with care are sewn,
Temporary helpers overthrown!
The schema gleams, no mess to find! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing a bug where helper views leaked after CTAS operations in dbt-fabric, directly addressing the core issue resolved in this PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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