From 1214e37a05e92d96c16e4b4b0c4da7caa1332b07 Mon Sep 17 00:00:00 2001 From: Damian Owsianny Date: Fri, 11 Oct 2024 14:11:36 +0200 Subject: [PATCH] Get 'properties' config inside 'properties' function. Add tests for table properties --- dbt/include/trino/macros/adapters.sql | 15 +++--- .../materializations/materialized_view.sql | 3 +- .../adapter/test_table_properties.py | 49 +++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 tests/functional/adapter/test_table_properties.py diff --git a/dbt/include/trino/macros/adapters.sql b/dbt/include/trino/macros/adapters.sql index faf72b2d..3ac4a725 100644 --- a/dbt/include/trino/macros/adapters.sql +++ b/dbt/include/trino/macros/adapters.sql @@ -58,7 +58,6 @@ {% macro trino__create_csv_table(model, agate_table) %} {%- set column_override = model['config'].get('column_types', {}) -%} {%- set quote_seed_column = model['config'].get('quote_columns', None) -%} - {%- set _properties = config.get('properties') -%} {% set sql %} create table {{ this.render() }} ( @@ -68,7 +67,7 @@ {%- set column_name = (col_name | string) -%} {{ adapter.quote_seed_column(column_name, quote_seed_column) }} {{ type }} {%- if not loop.last -%}, {%- endif -%} {%- endfor -%} - ) {{ properties(_properties) }} + ) {{ properties() }} {% endset %} {% call statement('_') -%} @@ -78,10 +77,11 @@ {{ return(sql) }} {% endmacro %} -{% macro properties(properties) %} - {%- if properties is not none -%} +{% macro properties() %} + {%- set _properties = config.get('properties') -%} + {%- if _properties is not none -%} WITH ( - {%- for key, value in properties.items() -%} + {%- for key, value in _properties.items() -%} {{ key }} = {{ value }} {%- if not loop.last -%}{{ ',\n ' }}{%- endif -%} {%- endfor -%} @@ -100,7 +100,6 @@ {%- endmacro -%} {% macro trino__create_table_as(temporary, relation, sql, replace=False) -%} - {%- set _properties = config.get('properties') -%} {%- if replace -%} {%- set or_replace = ' or replace' -%} @@ -117,7 +116,7 @@ {{ get_assert_columns_equivalent(sql) }} {%- set sql = get_select_subquery(sql) %} {{ comment(model.get('description')) }} - {{ properties(_properties) }} + {{ properties() }} ; insert into {{ relation }} @@ -130,7 +129,7 @@ create{{ or_replace }} table {{ relation }} {{ comment(model.get('description')) }} - {{ properties(_properties) }} + {{ properties() }} as ( {{ sql }} ); diff --git a/dbt/include/trino/macros/materializations/materialized_view.sql b/dbt/include/trino/macros/materializations/materialized_view.sql index 6bee70bb..d85aabdb 100644 --- a/dbt/include/trino/macros/materializations/materialized_view.sql +++ b/dbt/include/trino/macros/materializations/materialized_view.sql @@ -1,7 +1,6 @@ {%- macro trino__get_create_materialized_view_as_sql(target_relation, sql) -%} - {%- set _properties = config.get('properties') -%} create materialized view {{ target_relation }} - {{ properties(_properties) }} + {{ properties() }} as {{ sql }} ; diff --git a/tests/functional/adapter/test_table_properties.py b/tests/functional/adapter/test_table_properties.py new file mode 100644 index 00000000..b2ff85c9 --- /dev/null +++ b/tests/functional/adapter/test_table_properties.py @@ -0,0 +1,49 @@ +import pytest +from dbt.tests.util import run_dbt, run_dbt_and_capture + +from tests.functional.adapter.materialization.fixtures import model_sql, seed_csv + + +class BaseTableProperties: + # Everything that goes in the "seeds" directory + @pytest.fixture(scope="class") + def seeds(self): + return { + "seed.csv": seed_csv, + } + + # Everything that goes in the "models" directory + @pytest.fixture(scope="class") + def models(self): + return { + "model.sql": model_sql, + } + + +@pytest.mark.iceberg +class TestTableProperties(BaseTableProperties): + # Configuration in dbt_project.yml + @pytest.fixture(scope="class") + def project_config_update(self): + return { + "name": "properties_test", + "models": { + "+materialized": "table", + "+properties": { + "format": "'PARQUET'", + "format_version": "2", + }, + }, + } + + def test_table_properties(self, project): + # Seed seed + results = run_dbt(["seed"], expect_pass=True) + assert len(results) == 1 + + # Create model with properties + results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True) + assert len(results) == 1 + assert "WITH (" in logs + assert "format = 'PARQUET'" in logs + assert "format_version = 2" in logs