perf: reuse columns from process_schema_changes in incremental materialization - #1412
perf: reuse columns from process_schema_changes in incremental materialization#1412moomindani wants to merge 2 commits into
Conversation
b466c9c to
f6ffdd7
Compare
53ade57 to
d7a15e6
Compare
d7a15e6 to
70a2f94
Compare
…alization Incremental materialization previously discarded the return value of `process_schema_changes`, causing each strategy macro (`merge`, `append`, `delete+insert`) to issue a second `DESCRIBE TABLE EXTENDED` on the target relation even though `check_for_schema_changes` had just DESCRIBEd it. This change: - captures the columns returned by `process_schema_changes` in both V1 and V2 paths - falls back to a single `adapter.get_columns_in_relation(existing_relation)` when `on_schema_change == 'ignore'` - threads the result through `strategy_arg_dict['dest_columns']` - teaches `databricks__get_merge_sql`, `get_delete_insert_sql`, and `get_insert_into_sql` to honor a pre-supplied `dest_columns` and skip their own `DESCRIBE` when provided Net effect: one fewer `DESCRIBE TABLE EXTENDED … AS JSON` round-trip per incremental model, per run. Verified on a project with 9 incremental stg models (V1 path, `on_schema_change: 'fail'`): target DESCRIBE count drops from 2 to 1 per model across merge, append, and delete+insert strategies. Resolves databricks#1411 Co-authored-by: Isaac
70a2f94 to
d9f669d
Compare
…able `process_schema_changes` returns the source columns, so the reused `dest_columns` can omit a column the target still has under `on_schema_change: append_new_columns`. That subset made the matching-sets branch of `insert_into_sql_impl` always fire, which on DBR < 12.2 degrades to a positional `select *` instead of the explicit column list. Describe the target there instead of reusing the columns. Co-authored-by: Isaac <no-reply@databricks.com>
|
Hi @sd-db @tejassp-db — rebased onto latest I also pushed a follow-up fix (
Three macro tests pin the generated SQL for all three cases. CI has not run on this PR either — the workflows are at Open since April with review requested, so any feedback is welcome. |
|
One public reference I should have included with the previous comment: the column-matching failure the follow-up commit prevents is the same class as #1289 — a positional
On scope: this PR only reuses columns the materialization has already fetched. It issues no new metadata call, and the number of calls is reduced or unchanged on every path — including |
Summary
Closes #1411.
The incremental materialization currently discards the return value of
process_schema_changes, so each downstream strategy macro (merge,append,delete+insert) re-issues anotherDESCRIBE TABLE EXTENDEDon the target relation even though
check_for_schema_changeshas justDESCRIBEd it. This PR reuses those columns, eliminating one metadata
round-trip per incremental model, per run.
Changes
dbt/include/databricks/macros/materializations/incremental/incremental.sqlprocess_schema_changesin both V1 and V2 paths.on_schema_change == 'ignore'(returns{}), fall back to asingle
adapter.get_columns_in_relation(existing_relation).strategy_arg_dict['dest_columns'](previously hard-coded to
none).get_build_sqlwith adest_columns=noneparameter so theV2 path can pass through.
dbt/include/databricks/macros/materializations/incremental/strategies.sqldatabricks__get_merge_sql: only DESCRIBE the target whendest_columns is none.get_delete_insert_sql: honorarg_dict['dest_columns']when set.get_insert_into_sql: accept adest_columns=noneparameter andhonor it;
databricks__get_incremental_append_sqlnow passesarg_dict['dest_columns']through.CHANGELOG.md: new entry under## dbt-databricks next→Under the Hood.Behavior
on_schema_changeis'fail','sync_all_columns', or'append_new_columns':process_schema_changesalready DESCRIBEdboth relations, so we reuse its result — one fewer DESCRIBE.
on_schema_change == 'ignore': we issue exactly one DESCRIBE onthe existing relation, matching today's total count for that path.
get_build_sqlgainsan optional keyword argument that defaults to
none.Test plan
Manually verified on a live Databricks SQL Warehouse with a project of
9 incremental stg models (
on_schema_change: 'fail', 7merge+ 2appendstrategies).Target
DESCRIBE TABLE EXTENDED … AS JSONcount per incremental model:use_materialization_v2: false)use_materialization_v2: true)Wall-clock impact on a full
dbt runis within measurement noise atthis scale (9 small models, 16 threads); the saved round-trips get
absorbed by parallelism. The win here is fewer metadata round-trips
(lower warehouse load, less API traffic), not a dramatic wall-clock
speedup.