Repository navigation
fix: read a model query's columns through a view instead of LIMIT 0 - #773
Romano-arist wants to merge 4 commits into
Conversation
|
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 Thanks! |
|
@koletzilla Thanks! |
Summary
Closes #480.
Contract enforcement and
on_schema_changeread a model query's columns withselect * from {{ sql }} limit 0. ClickHouse does not push thatLIMIT 0into 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 andon_schema_changeexpensive.With this PR the columns come from a view (
<model>__dbt_schema_probe):CREATE VIEWresolves the structure without executing the query.Checklist
Note
Medium Risk
Touches schema introspection and contract/
on_schema_changepaths 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 whentarget_columnsis omitted.Overview
Contract enforcement and
on_schema_changeno longer infer columns withselect * from (query) limit 0, which could execute the full query on Distributed table shards. They now use a newclickhouse__get_column_schema_from_querymacro that creates a temporary view\<model\>__dbt_schema_probe, reads columns withget_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_changesvia a new optionaltarget_columnsargument on the adapter, avoiding the Pythonget_column_schema_from_querypath 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.