feat: add overwrite_src_cols parameter to derived_columns#474
feat: add overwrite_src_cols parameter to derived_columns#474pkumar-data wants to merge 4 commits into
Conversation
66403e7 to
ca5a1e8
Compare
There was a problem hiding this comment.
Pull request overview
Adds support for a new per-derived-column boolean parameter overwrite_src_cols that suppresses specified source columns (from src_cols_required) after the derived_columns CTE, enabling true “rename/replace” behavior and preventing duplicated original+alias columns downstream.
Changes:
- Introduces
extract_overwrite_columns()helper macro to collectsrc_cols_requiredentries for derived columns marked withoverwrite_src_cols: true. - Updates adapter-specific
stage.sqlmacros to (a) extract overwrite columns before datatype detection and (b) remove those source columns fromfinal_columns_to_selectin thederived_columnsCTE.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| macros/staging/stage_processing_macros.sql | Adds extract_overwrite_columns() helper macro used by staging to compute which source columns to drop. |
| macros/staging/bigquery/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/databricks/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/exasol/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/fabric/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/oracle/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/postgres/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/redshift/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/snowflake/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/sqlserver/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/synapse/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
| macros/staging/trino/stage.sql | Extracts overwrite columns and excludes them from derived_columns CTE output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Adds a new per-derived-column boolean parameter overwrite_src_cols (default: false). When true, the columns listed in src_cols_required are suppressed from the derived_columns CTE SELECT onward, so the source column is replaced by the alias rather than duplicated alongside it. Fixes the case where renaming a column with special characters (e.g. German umlauts on Databricks) resulted in both the original and the alias appearing in all downstream CTEs. Implemented via a new extract_overwrite_columns() helper macro in stage_processing_macros.sql. The helper is called before derived_columns_datatypes() to avoid the in-place dict mutation that strips custom keys from the columns dict.
…exclusion Databricks normalizes final_columns_to_select to lowercase in the first process_columns_to_select call, so overwrite_src_columns must also be lowercased to ensure case-insensitive matching works correctly.
Initialize overwrite_src_columns = [] before the if block rather than in the else clause. This eliminates the redundant else block, makes the intent clearer (variable is always needed), and follows DRY principle. Applies to all staging macros: bigquery, snowflake, postgres, redshift, oracle, exasol, trino, databricks, sqlserver, synapse, fabric.
78ede82 to
c429a44
Compare
tkiehn
left a comment
There was a problem hiding this comment.
Looks good in general, nice and easy implementation!
Lastly please also add documentation for the new parameter in the top-level macro macros/staging/stage.sql and the docs-markdowns.
Documents the new per-column boolean parameter in macros/staging/stage.sql (derived_columns_description) and in the derived-columns markdown guide, including a umlaut-rename example and an OVERWRITE SOURCE COLUMNS section.
ac2bfa3 to
212f037
Compare
|
|
||
| The optional boolean parameter `overwrite_src_cols` can be set per derived column. When set to `true`, the source columns listed under `src_cols_required` are **excluded from the staging CTE output**, so only the derived alias column appears — preventing duplicate columns. | ||
|
|
||
| This is particularly useful when **renaming columns whose original name cannot be referenced downstream**, for example columns containing special characters (e.g. German umlauts like `ä`, `ö`, `ü`) on platforms such as Databricks. Without `overwrite_src_cols: true`, both the original and the renamed column would appear side by side in the output. |
There was a problem hiding this comment.
| This is particularly useful when **renaming columns whose original name cannot be referenced downstream**, for example columns containing special characters (e.g. German umlauts like `ä`, `ö`, `ü`) on platforms such as Databricks. Without `overwrite_src_cols: true`, both the original and the renamed column would appear side by side in the output. |
|
|
||
| ## OVERWRITE SOURCE COLUMNS | ||
|
|
||
| The optional boolean parameter `overwrite_src_cols` can be set per derived column. When set to `true`, the source columns listed under `src_cols_required` are **excluded from the staging CTE output**, so only the derived alias column appears — preventing duplicate columns. |
There was a problem hiding this comment.
| The optional boolean parameter `overwrite_src_cols` can be set per derived column. When set to `true`, the source columns listed under `src_cols_required` are **excluded from the staging CTE output**, so only the derived alias column appears — preventing duplicate columns. | |
| The optional boolean parameter `overwrite_src_cols` can be set per derived column. When set to `true`, the source columns listed under `src_cols_required` are **excluded from the staging CTE output**, so only the derived alias column appears. The parameter defaults to `false` when omitted. |
| account_name_clean: | ||
| value: Kontonäme | ||
| datatype: STRING | ||
| src_cols_required: Kontonäme | ||
| overwrite_src_cols: true |
There was a problem hiding this comment.
| account_name_clean: | |
| value: Kontonäme | |
| datatype: STRING | |
| src_cols_required: Kontonäme | |
| overwrite_src_cols: true | |
| domain: | |
| value: Domäne | |
| datatype: STRING | |
| src_cols_required: Domäne | |
| overwrite_src_cols: true |
| overwrite_src_cols: true | ||
| ``` | ||
|
|
||
| In this example, `Kontonäme` is excluded from the staging CTE and only `account_name_clean` appears in the output. The parameter defaults to `false` when omitted. |
There was a problem hiding this comment.
| In this example, `Kontonäme` is excluded from the staging CTE and only `account_name_clean` appears in the output. The parameter defaults to `false` when omitted. | |
| In this example, `Domäne` is excluded from the staging CTE and only `domain` appears in the output. |
| {'account_name_clean': {'value': 'Kontonäme', Renames a source column whose name contains a special character. Setting | ||
| 'datatype': 'STRING', 'overwrite_src_cols: true' ensures the original column is dropped from the | ||
| 'src_cols_required': 'Kontonäme', CTE output, so only 'account_name_clean' appears and the special-character |
There was a problem hiding this comment.
| {'account_name_clean': {'value': 'Kontonäme', Renames a source column whose name contains a special character. Setting | |
| 'datatype': 'STRING', 'overwrite_src_cols: true' ensures the original column is dropped from the | |
| 'src_cols_required': 'Kontonäme', CTE output, so only 'account_name_clean' appears and the special-character | |
| {'domain': {'value': 'Domäne', Renames a source column whose name contains a special character. Setting | |
| 'datatype': 'STRING', 'overwrite_src_cols: true' ensures the original column is dropped from the | |
| 'src_cols_required': 'Domäne', CTE output, so only 'domain' appears and the special-character |
Adds a new per-derived-column boolean parameter
overwrite_src_cols(default:false). Whentrue, the columns listed insrc_cols_requiredare suppressed from thederived_columnsCTE SELECT onward, so the source column is replaced by the alias rather than duplicated alongside it. Fixes the case where renaming a column with special characters (e.g. German umlauts on Databricks) resulted in both the original and the alias appearing in all downstream CTEs.Implemented via a new
extract_overwrite_columns()helper macro instage_processing_macros.sql. The helper is called beforederived_columns_datatypes()to avoid the in-place dict mutation that strips custom keys from the columns dict.Fixes ScalefreeCOM/product_development_milestones#328
Type of change
How Has This Been Tested?
Tested on BigQuery using a stage model with a source column renamed via
overwrite_src_cols: true:dbt compile— verified thederived_columnsCTE no longer includes the original source column alongside the alias.dbt run— view created successfully with the correct output schema (original column absent, alias present).Test Configuration:
Checklist: