Skip to content

fix: read a model query's columns through a view instead of LIMIT 0 - #773

Open
Romano-arist wants to merge 4 commits into
ClickHouse:mainfrom
Romano-arist:fix/schema-probe-view
Open

Romano-arist wants to merge 4 commits into
ClickHouse:mainfrom
Romano-arist:fix/schema-probe-view

Conversation

@Romano-arist

@Romano-arist Romano-arist commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #480.

Contract enforcement and on_schema_change read a model query's columns with select * from {{ sql }} limit 0. ClickHouse does not push that LIMIT 0 into a subquery over a Distributed table, so the shards start executing the query. On models the check costs almost as much as the run itself, which makes contracts and on_schema_change expensive.

With this PR the columns come from a view (<model>__dbt_schema_probe): CREATE VIEW resolves the structure without executing the query.

Checklist


Note

Medium Risk
Touches schema introspection and contract/on_schema_change paths across several materializations; wrong column types or probe view lifecycle could cause false contract failures or extra DDL, but the change is localized and the old adapter fallback remains when target_columns is omitted.

Overview
Contract enforcement and on_schema_change no longer infer columns with select * from (query) limit 0, which could execute the full query on Distributed table shards. They now use a new clickhouse__get_column_schema_from_query macro that creates a temporary view \<model\>__dbt_schema_probe, reads columns with get_columns_in_relation, then drops the view. The view definition includes the model’s query settings so introspection still matches runtime types.

Callers (incremental, distributed incremental, materialized view, table materializations, distributed table DDL, and contract checks) pass those columns into check_incremental_schema_changes via a new optional target_columns argument on the adapter, avoiding the Python get_column_schema_from_query path when columns are already known.

Reviewed by Cursor Bugbot for commit 2ce8476. Bugbot is set up for automated code reviews on this repo. Configure here.

@koletzilla
koletzilla self-requested a review November 2, 2026 15:34
@koletzilla

Copy link
Copy Markdown
Contributor

Hi @Romano-arist , thanks for the PR.

I see the approach you're following looks good as it will stop that internal SQL execution. But there's an impactful tradeoff: Having to manage the liffecycle of a view that have already some impact on memory/cpu and that can be specially harmful in clusters with overloaded Keepers.

Can you, instead of using the view, use a DESCRIBE TABLE (sql) operation to get the types? That should behave in the correct way than the current LIMIT 0 approach. We are actually doing this same thing for V2 in https://github.com/dbt-labs/dbt/pull/16079/changes.

Thanks!

@Romano-arist

Copy link
Copy Markdown
Contributor Author

@koletzilla Thanks! Describe works for a plain select from a Distributed table, but it still materializes GLOBAL JOIN/IN and subqueries (cheaper than LIMIT 0, but still the full read). It is exactly our case and in the #480 issue. A view is the only way I see to get the columns without running anything (dropped in the same run). So, is the view ok here or do you want DESCRIBE anyway?

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.

Inefficient Schema change check

3 participants