diff --git a/dbt/include/bigquery/macros/utils/safe_cast.sql b/dbt/include/bigquery/macros/utils/safe_cast.sql index 57eaf7b1eb..be9f0c6207 100644 --- a/dbt/include/bigquery/macros/utils/safe_cast.sql +++ b/dbt/include/bigquery/macros/utils/safe_cast.sql @@ -3,6 +3,8 @@ (select array_agg(safe_cast(i as {{type.lower()[6:-1]}})) from unnest({{field}}) i) {%- elif type.lower() == 'json' and field is mapping -%} safe_cast(json {{ dbt.string_literal(tojson(field)) }} as json) +{%- elif type.lower().startswith('struct') and field is string -%} + safe_cast({{field.strip('"').strip("'")}} as {{type}}) {%- else -%} safe_cast({{field}} as {{type}}) {%- endif -%} diff --git a/tests/functional/adapter/unit_testing/test_unit_testing.py b/tests/functional/adapter/unit_testing/test_unit_testing.py index 9b1497b25c..9181df67ab 100644 --- a/tests/functional/adapter/unit_testing/test_unit_testing.py +++ b/tests/functional/adapter/unit_testing/test_unit_testing.py @@ -34,6 +34,11 @@ def data_types(self): """{"name": "Cooper", "forname": "Alice"}""", ], ["""json '{"name": "Cooper", "forname": "Alice"}'""", "{}"], + # structs + ["STRUCT('Isha' as name, 22 as age)", """'STRUCT("Isha" as name, 22 as age)'"""][ + "STRUCT('Kipketer' AS name, [23.2, 26.1, 27.3, 29.4] AS laps)", + """'STRUCT("Kipketer" AS name, [23.2, 26.1, 27.3, 29.4] AS laps)'""", + ], ]