From 7b8aa2d19b90d43070306d9f023d2a52161c0b8c Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Tue, 24 Oct 2023 11:06:01 -0600 Subject: [PATCH 1/5] Handle unknown `type_code` for model contracts --- plugins/postgres/dbt/adapters/postgres/connections.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/postgres/dbt/adapters/postgres/connections.py b/plugins/postgres/dbt/adapters/postgres/connections.py index ac7df536bdb..50c609f54bb 100644 --- a/plugins/postgres/dbt/adapters/postgres/connections.py +++ b/plugins/postgres/dbt/adapters/postgres/connections.py @@ -204,4 +204,7 @@ def get_response(cls, cursor) -> AdapterResponse: @classmethod def data_type_code_to_name(cls, type_code: int) -> str: - return string_types[type_code].name + if type_code in string_types: + return string_types[type_code].name + else: + return f"unknown type_code {type_code}" From 2d6d923b5dc03cca8b0b8a58196acb1a614b6707 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Tue, 24 Oct 2023 11:27:20 -0600 Subject: [PATCH 2/5] Changelog entry --- .changes/unreleased/Fixes-20231024-110151.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20231024-110151.yaml diff --git a/.changes/unreleased/Fixes-20231024-110151.yaml b/.changes/unreleased/Fixes-20231024-110151.yaml new file mode 100644 index 00000000000..fbb6f390998 --- /dev/null +++ b/.changes/unreleased/Fixes-20231024-110151.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Handle unknown `type_code` for model contracts +time: 2023-10-24T11:01:51.980781-06:00 +custom: + Author: dbeatty10 + Issue: "8877" "8353" From a205d4ab28eff96e8f23d11cefdfeef80c9ce959 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Tue, 24 Oct 2023 18:38:56 -0600 Subject: [PATCH 3/5] Fix changelog entry --- .changes/unreleased/Fixes-20231024-110151.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/unreleased/Fixes-20231024-110151.yaml b/.changes/unreleased/Fixes-20231024-110151.yaml index fbb6f390998..711f431be8a 100644 --- a/.changes/unreleased/Fixes-20231024-110151.yaml +++ b/.changes/unreleased/Fixes-20231024-110151.yaml @@ -3,4 +3,4 @@ body: Handle unknown `type_code` for model contracts time: 2023-10-24T11:01:51.980781-06:00 custom: Author: dbeatty10 - Issue: "8877" "8353" + Issue: 8877 8353 From dc70ff3e44ed92268cfaef1bf77c244336655a25 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Tue, 24 Oct 2023 19:22:04 -0600 Subject: [PATCH 4/5] Functional test for a `type_code` that is not recognized by psycopg2 --- .../contracts/test_nonstandard_data_type.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/functional/contracts/test_nonstandard_data_type.py diff --git a/tests/functional/contracts/test_nonstandard_data_type.py b/tests/functional/contracts/test_nonstandard_data_type.py new file mode 100644 index 00000000000..869ddc17395 --- /dev/null +++ b/tests/functional/contracts/test_nonstandard_data_type.py @@ -0,0 +1,32 @@ +import pytest +from dbt.tests.util import run_dbt + + +my_money_model_sql = """ +select + cast('12.34' as money) as non_standard +""" + +model_schema_money_yml = """ +version: 2 +models: + - name: my_numeric_model + config: + contract: + enforced: true + columns: + - name: non_standard + data_type: money +""" + + +class TestModelContractNumericNoPrecision: + @pytest.fixture(scope="class") + def models(self): + return { + "my_numeric_model.sql": my_money_model_sql, + "schema.yml": model_schema_money_yml, + } + + def test_nonstandard_data_type(self, project): + run_dbt(["run"], expect_pass=True) From 4c8a3b09280d49a89b5fcba71fdbdc89e60c44c9 Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Tue, 24 Oct 2023 20:16:47 -0600 Subject: [PATCH 5/5] Functional tests for data type mismatches --- .../contracts/test_nonstandard_data_type.py | 58 ++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/tests/functional/contracts/test_nonstandard_data_type.py b/tests/functional/contracts/test_nonstandard_data_type.py index 869ddc17395..4ee559ff4e5 100644 --- a/tests/functional/contracts/test_nonstandard_data_type.py +++ b/tests/functional/contracts/test_nonstandard_data_type.py @@ -1,32 +1,76 @@ import pytest -from dbt.tests.util import run_dbt +from dbt.tests.util import run_dbt, run_dbt_and_capture +my_numeric_model_sql = """ +select + 12.34 as price +""" + my_money_model_sql = """ select - cast('12.34' as money) as non_standard + cast('12.34' as money) as price """ model_schema_money_yml = """ -version: 2 models: - - name: my_numeric_model + - name: my_model config: contract: enforced: true columns: - - name: non_standard + - name: price data_type: money """ +model_schema_numeric_yml = """ +models: + - name: my_model + config: + contract: + enforced: true + columns: + - name: price + data_type: numeric +""" + -class TestModelContractNumericNoPrecision: +class TestModelContractUnrecognizedTypeCode1: @pytest.fixture(scope="class") def models(self): return { - "my_numeric_model.sql": my_money_model_sql, + "my_model.sql": my_money_model_sql, "schema.yml": model_schema_money_yml, } def test_nonstandard_data_type(self, project): run_dbt(["run"], expect_pass=True) + + +class TestModelContractUnrecognizedTypeCodeActualMismatch: + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_money_model_sql, + "schema.yml": model_schema_numeric_yml, + } + + def test_nonstandard_data_type(self, project): + expected_msg = "unknown type_code 790 | DECIMAL | data type mismatch" + _, logs = run_dbt_and_capture(["run"], expect_pass=False) + assert expected_msg in logs + + +class TestModelContractUnrecognizedTypeCodeExpectedMismatch: + @pytest.fixture(scope="class") + def models(self): + return { + "my_model.sql": my_numeric_model_sql, + "schema.yml": model_schema_money_yml, + } + + def test_nonstandard_data_type(self, project): + expected_msg = "DECIMAL | unknown type_code 790 | data type mismatch" + _, logs = run_dbt_and_capture(["run"], expect_pass=False) + print(logs) + assert expected_msg in logs