From 616de0f525e2927eda648b9a4c75e502d3624129 Mon Sep 17 00:00:00 2001 From: carolinabtt Date: Tue, 15 Oct 2024 19:34:48 +0200 Subject: [PATCH 1/3] Fix dynamic materializations with contract enabled --- dbt/include/bigquery/macros/adapters/columns.sql | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dbt/include/bigquery/macros/adapters/columns.sql b/dbt/include/bigquery/macros/adapters/columns.sql index c540f1e2d..928aa5bc6 100644 --- a/dbt/include/bigquery/macros/adapters/columns.sql +++ b/dbt/include/bigquery/macros/adapters/columns.sql @@ -2,6 +2,13 @@ {%- if select_sql_header is not none -%} {{ select_sql_header }} {%- endif -%} + {%- set contract_config = config.get('contract') -%} + {%- if contract_config.enforced and '_dbt_max_partition' in compiled_code -%} + declare _dbt_max_partition {{ partition_by.data_type_for_partition() }} default ( + select max({{ partition_by.field }}) from {{ relation }} + where {{ partition_by.field }} is not null + ); + {%- endif -%} select * from ( {{ select_sql }} ) as __dbt_sbq From a2c62b30f0a942abeea5917e2b98f056223b59fb Mon Sep 17 00:00:00 2001 From: carolinabtt Date: Tue, 15 Oct 2024 19:39:02 +0200 Subject: [PATCH 2/3] Partition_by config parameter --- dbt/include/bigquery/macros/adapters/columns.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dbt/include/bigquery/macros/adapters/columns.sql b/dbt/include/bigquery/macros/adapters/columns.sql index 928aa5bc6..ec7c7258a 100644 --- a/dbt/include/bigquery/macros/adapters/columns.sql +++ b/dbt/include/bigquery/macros/adapters/columns.sql @@ -4,10 +4,12 @@ {%- endif -%} {%- set contract_config = config.get('contract') -%} {%- if contract_config.enforced and '_dbt_max_partition' in compiled_code -%} + {%- set raw_partition_by = config.get('partition_by', none) -%} + {%- set partition_by = adapter.parse_partition_by(raw_partition_by) -%} + declare _dbt_max_partition {{ partition_by.data_type_for_partition() }} default ( select max({{ partition_by.field }}) from {{ relation }} - where {{ partition_by.field }} is not null - ); + where {{ partition_by.field }} is not null ); {%- endif -%} select * from ( {{ select_sql }} From f09ef48433ac12017e681473403c021f89bece52 Mon Sep 17 00:00:00 2001 From: carolinabtt Date: Tue, 15 Oct 2024 19:41:50 +0200 Subject: [PATCH 3/3] This parameter --- dbt/include/bigquery/macros/adapters/columns.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/include/bigquery/macros/adapters/columns.sql b/dbt/include/bigquery/macros/adapters/columns.sql index ec7c7258a..ab8c42945 100644 --- a/dbt/include/bigquery/macros/adapters/columns.sql +++ b/dbt/include/bigquery/macros/adapters/columns.sql @@ -8,7 +8,7 @@ {%- set partition_by = adapter.parse_partition_by(raw_partition_by) -%} declare _dbt_max_partition {{ partition_by.data_type_for_partition() }} default ( - select max({{ partition_by.field }}) from {{ relation }} + select max({{ partition_by.field }}) from {{ this }} where {{ partition_by.field }} is not null ); {%- endif -%} select * from (