Repository navigation
Conversation
…ew model dbt injects the CTEs of ephemeral models into the model SQL only once: before the first `--<name>:begin` marker, or into the `with` clause of the first view. The marker extraction then gave views without the CTEs, so a view selecting from an ephemeral model failed with `Unknown table expression identifier '__dbt__cte__...'`. Each extracted view that does not contain the injected CTEs now gets them, merged into its own `with` clause when it has one. Fixes ClickHouse#521
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit d498c50. Configure here.
|
A materialized view model with several |

Summary
Fixes #521.
A materialized view model with several
--<name>:begin/--<name>:endviews fails when a view selects from an ephemeral model:dbt injects the CTEs of ephemeral models into the model SQL only once (
inject_ctes_into_sqlin dbt-core). Depending on how the first view starts, they end up either before the first marker (with __dbt__cte__x as (...) --mv1:begin select ...) or inside thewithclause of the first view.clickhouse__extract_mv_viewsonly keeps the text between the markers, so the views were created without the CTEs. A model without markers was not affected, because its single view is the whole SQL.clickhouse__extract_mv_viewsnow reads the injected CTEs frommodel.extra_ctesand, for each marked view that does not already contain them, adds them: merged into the view's ownwithclause if it has one (after leading comments, and afterrecursive), otherwise as a newwithclause before the query. Views of models that use no ephemeral models, and models without markers, are unchanged. The target table is still created from the whole model SQL, which already had the CTEs.Tests
tests/unit/test_extract_mv_views.py): CTEs injected before the first marker, CTEs injected into the first view'swithclause, a later view with its ownWITHclause after a comment, several ephemeral models, and the unchanged cases. The four new cases that expect added CTEs fail onmain.TestMultipleMVFromEphemeralintest_multiple_materialized_views.py): a two-view model selecting from an ephemeral model, with and without awithclause in the first view. It runs--full-refreshand then a normal run (view query update), and checks that new source rows reach the target table through both views. Both cases fail onmainwithUNKNOWN_TABLEand pass with the fix.Commands, against a local
clickhouse/clickhouse-server26.9.17.8:pytest tests/unit: 102 passedDBT_CH_TEST_PORT=18123 pytest tests/integration/adapter/materialized_view tests/integration/adapter/basic: 80 passed, 1 skippedmake lint: passesChecklist
Note
Low Risk
Localized change to MV view splitting macros with broad unit/integration coverage; no auth or data-migration behavior.
Overview
Fixes multi-view materialized_view models that
ref()an ephemeral model, which previously failed at run time withUnknown table expression identifier '__dbt__cte__...'.dbt injects ephemeral CTEs into compiled SQL once (before the first
--name:beginmarker or inside the first view’sWITH).clickhouse__extract_mv_viewsonly kept text between markers, so later views were deployed without those CTEs. The macro now readsmodel.extra_ctes, and for any marked view that does not already contain that SQL,clickhouse__add_ctes_to_view_sqlprepends or merges the CTEs into the view’sWITH(including after leading comments). Single-view models and multi-view models without ephemerals are unchanged; the target table still uses the full model SQL.Unit and integration tests cover CTE placement edge cases and end-to-end
dbt run/ view query updates.Reviewed by Cursor Bugbot for commit a1b9862. Bugbot is set up for automated code reviews on this repo. Configure here.