From 5df41fd3a142aa9b03bbfe5502ed2b14899e75ae Mon Sep 17 00:00:00 2001 From: James McNeill Date: Sun, 18 Sep 2022 21:38:14 +0100 Subject: [PATCH 1/2] implement type_boolean macro --- core/dbt/adapters/base/column.py | 1 + .../macros/utils/data_types.sql | 14 ++++++++++- .../utils/data_types/test_type_boolean.py | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/adapter/dbt/tests/adapter/utils/data_types/test_type_boolean.py diff --git a/core/dbt/adapters/base/column.py b/core/dbt/adapters/base/column.py index dba172dad85..b47aac64062 100644 --- a/core/dbt/adapters/base/column.py +++ b/core/dbt/adapters/base/column.py @@ -12,6 +12,7 @@ class Column: "TIMESTAMP": "TIMESTAMP", "FLOAT": "FLOAT", "INTEGER": "INT", + "BOOLEAN": "BOOLEAN", } column: str dtype: str diff --git a/core/dbt/include/global_project/macros/utils/data_types.sql b/core/dbt/include/global_project/macros/utils/data_types.sql index 49914e64c12..c4508ff3066 100644 --- a/core/dbt/include/global_project/macros/utils/data_types.sql +++ b/core/dbt/include/global_project/macros/utils/data_types.sql @@ -59,7 +59,7 @@ The TIMESTAMP_* variation associated with TIMESTAMP is specified by the TIMESTAM {{ return(api.Column.translate_type("float")) }} {% endmacro %} -{# numeric ------------------------------------------------ #} +{# numeric ------------------------------------------------- #} {%- macro type_numeric() -%} {{ return(adapter.dispatch('type_numeric', 'dbt')()) }} @@ -115,3 +115,15 @@ the precision and scale explicitly.) -- returns 'int' everywhere, except BigQuery, where it returns 'int64' -- (but BigQuery also now accepts 'int' as a valid alias for 'int64') + +{# bool ------------------------------------------------- #} + +{%- macro type_boolean() -%} + {{ return(adapter.dispatch('type_boolean', 'dbt')()) }} +{%- endmacro -%} + +{%- macro default__type_boolean() -%} + {{ return(api.Column.translate_type("boolean")) }} +{%- endmacro -%} + +-- returns 'boolean' everywhere. BigQuery accepts 'boolean' as a valid alias for 'bool' diff --git a/tests/adapter/dbt/tests/adapter/utils/data_types/test_type_boolean.py b/tests/adapter/dbt/tests/adapter/utils/data_types/test_type_boolean.py new file mode 100644 index 00000000000..3efd7874236 --- /dev/null +++ b/tests/adapter/dbt/tests/adapter/utils/data_types/test_type_boolean.py @@ -0,0 +1,24 @@ +import pytest +from dbt.tests.adapter.utils.data_types.base_data_type_macro import BaseDataTypeMacro + +seeds__expected_csv = """boolean_col +True +""".lstrip() + +models__actual_sql = """ +select cast('True' as {{ type_boolean() }}) as boolean_col +""" + + +class BaseTypeBoolean(BaseDataTypeMacro): + @pytest.fixture(scope="class") + def seeds(self): + return {"expected.csv": seeds__expected_csv} + + @pytest.fixture(scope="class") + def models(self): + return {"actual.sql": self.interpolate_macro_namespace(models__actual_sql, "type_boolean")} + + +class TestTypeBoolean(BaseTypeBoolean): + pass From 2bb86b5eb018726935f61aa9666a5ff00e2cb19e Mon Sep 17 00:00:00 2001 From: James McNeill Date: Mon, 19 Sep 2022 23:14:26 +0100 Subject: [PATCH 2/2] changie result --- .changes/unreleased/Features-20220919-231414.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changes/unreleased/Features-20220919-231414.yaml diff --git a/.changes/unreleased/Features-20220919-231414.yaml b/.changes/unreleased/Features-20220919-231414.yaml new file mode 100644 index 00000000000..1db12449d31 --- /dev/null +++ b/.changes/unreleased/Features-20220919-231414.yaml @@ -0,0 +1,7 @@ +kind: Features +body: add type_boolean as a data type macro +time: 2022-09-19T23:14:14.9871+01:00 +custom: + Author: jpmmcneill + Issue: "5739" + PR: "5875"