diff --git a/integration_tests/models/model_repeated.sql b/integration_tests/models/model_repeated.sql new file mode 100644 index 0000000..2c08a2f --- /dev/null +++ b/integration_tests/models/model_repeated.sql @@ -0,0 +1,13 @@ +{% if target.type == "bigquery" %} + + {#--- This exists to test the BigQuery-specific behavior requested in #190 -#} +select + [1, 2] AS repeated_int, + [ + STRUCT(1 as nested_int_field, [STRUCT("a" as string_field)] as nested_repeated_struct), + STRUCT(2 AS nested_int_field, [STRUCT("a" as string_field)] as nested_repeated_struct) + ] as repeated_struct + +{% else %} + select 1 as int_field +{% endif %} diff --git a/integration_tests/tests/test_generate_model_repeated_yaml.sql b/integration_tests/tests/test_generate_model_repeated_yaml.sql new file mode 100644 index 0000000..401cbe7 --- /dev/null +++ b/integration_tests/tests/test_generate_model_repeated_yaml.sql @@ -0,0 +1,56 @@ +{% set raw_schema = generate_schema_name('raw_data') %} + +{% set actual_source_yaml = codegen.generate_model_yaml( + model_names=['model_repeated'] + ) +%} + +{% if target.type == "bigquery" %} + +{% set expected_source_yaml %} +version: 2 + +models: + - name: model_repeated + description: "" + columns: + - name: repeated_int + data_type: array + description: "" + + - name: repeated_struct + data_type: array + description: "" + + - name: repeated_struct.nested_int_field + data_type: int64 + description: "" + + - name: repeated_struct.nested_repeated_struct + data_type: array + description: "" + + - name: repeated_struct.nested_repeated_struct.string_field + data_type: string + description: "" + +{% endset %} + +{% else %} + +{% set expected_source_yaml %} +version: 2 + +models: + - name: model_repeated + description: "" + columns: + - name: int_field + data_type: {{ integer_type_value() }} + description: "" + +{% endset %} + +{% endif %} + +{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }} diff --git a/integration_tests/tests/test_helper_get_models.sql b/integration_tests/tests/test_helper_get_models.sql index d9393a3..95c4d0c 100644 --- a/integration_tests/tests/test_helper_get_models.sql +++ b/integration_tests/tests/test_helper_get_models.sql @@ -7,6 +7,6 @@ {% set actual_list = codegen.get_models(prefix='model_')|sort %} {% endif %} -{% set expected_list = ['model_data_a', 'model_from_source', 'model_struct', 'model_without_any_ctes', 'model_without_import_ctes'] %} +{% set expected_list = ['model_data_a', 'model_from_source', 'model_repeated', 'model_struct', 'model_without_any_ctes', 'model_without_import_ctes'] %} {{ assert_equal (actual_list, expected_list) }} diff --git a/macros/vendored/dbt_core/format_column.sql b/macros/vendored/dbt_core/format_column.sql index a7a6669..2365638 100644 --- a/macros/vendored/dbt_core/format_column.sql +++ b/macros/vendored/dbt_core/format_column.sql @@ -1,5 +1,21 @@ {% macro format_column(column) -%} + {{ return(adapter.dispatch('format_column', 'codegen')(column)) }} +{%- endmacro %} + +{# Vendored from: https://github.com/dbt-labs/dbt-adapters/blob/c7b12aee533184bad391a657d1753539d1dd496a/dbt/include/global_project/macros/relations/column/columns_spec_ddl.sql#L85-L89 #} +{% macro default__format_column(column) -%} {% set data_type = column.dtype %} {% set formatted = column.column.lower() ~ " " ~ data_type %} {{ return({'name': column.name, 'data_type': data_type, 'formatted': formatted}) }} {%- endmacro -%} + +{# Vendored from: https://github.com/dbt-labs/dbt-bigquery/blob/4d255b2f854d21d5d8871bdaa8d7ab47e7e863a3/dbt/include/bigquery/macros/utils/get_columns_spec_ddl.sql#L1-L5 #} +{# But modified to handle https://github.com/dbt-labs/dbt-codegen/issues/190 #} +{% macro bigquery__format_column(column) -%} + {% set data_type = column.data_type %} + {% if column.mode.lower() == "repeated" and column.dtype.lower() == "record" %} + {% set data_type = "array" %} + {% endif %} + {% set formatted = column.column.lower() ~ " " ~ data_type %} + {{ return({'name': column.name, 'data_type': data_type, 'formatted': formatted}) }} +{%- endmacro -%}