Skip to content

Commit e982a11

Browse files
bodschutdbeatty10
andauthored
Updated generate_model_yaml macro to correctly handle nested bigquery… (#54)
* Updated generate_model_yaml macro to correctly handle nested bigquery fields * Updated changelog.md * Update macros/generate_model_yaml.sql * Integration tests Co-authored-by: Bob De Schutter <bob.deschutter@digitalswat.be> Co-authored-by: Doug Beatty <44704949+dbeatty10@users.noreply.github.com> Co-authored-by: Doug Beatty <doug.beatty@dbtlabs.com>
1 parent 046eb72 commit e982a11

10 files changed

+114
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Unreleased
2+
23
## New features
34
- Add support for importing descriptions from columns with the same names in upstream models. It is available by setting the parameter `upstream_descriptions` to `True` in `generate_model_yaml` ([#61](https://github.com/dbt-labs/dbt-codegen/pull/61))
45
- Add support for including description placeholders for the source and table, which changes the behavior of `generate_source` when `include_descriptions` is set to `True`. Previous logic only created description placeholders for the columns.
56
- Add optional `name` arg to `generate_source`
67
- Add optional `table_names` arg to `generate_source` (#50 @rahulj51)
78

9+
## Fixes
10+
- generate_model_yaml now correctly handles nested bigquery fields (#27)
11+
812
# dbt-codegen v0.6.0
913

1014
This release creates breaking changes to the `generate_source.sql` macro.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% if target.type == "bigquery" %}
2+
3+
{#--- This exists to test the BigQuery-specific behavior reqeusted in #27 -#}
4+
select
5+
STRUCT(
6+
source,
7+
medium,
8+
source_medium
9+
) as analytics,
10+
col_x
11+
from {{ ref('data__campaign_analytics') }}
12+
13+
{% else %}
14+
15+
{#--- This enables mimicking the BigQuery behavior for other adapters -#}
16+
select
17+
analytics,
18+
source,
19+
medium,
20+
source_medium,
21+
col_x
22+
from {{ ref('data__campaign_analytics') }}
23+
24+
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source,medium,source_medium,analytics,col_x
2+
source_1,medium_a,1a,,x
3+
source_2,medium_b,2b,,x
4+
source_3,medium_c,3c,,x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{% set raw_schema = generate_schema_name('raw_data') %}
2+
3+
-- test all args
4+
{% set actual_source_yaml = codegen.generate_source(
5+
database_name=target.database,
6+
schema_name='codegen_integration_tests__data_source_schema',
7+
table_names=['codegen_integration_tests__data_source_table_nested_array'],
8+
generate_columns=True,
9+
include_descriptions=True
10+
) %}
11+
12+
{% set actual_source_yaml = codegen.generate_model_yaml(
13+
model_name='model_struct'
14+
)
15+
%}
16+
17+
18+
{% set expected_source_yaml %}
19+
version: 2
20+
21+
models:
22+
- name: model_struct
23+
description: ""
24+
columns:
25+
- name: analytics
26+
description: ""
27+
28+
- name: source
29+
description: ""
30+
31+
- name: medium
32+
description: ""
33+
34+
- name: source_medium
35+
description: ""
36+
37+
- name: col_x
38+
description: ""
39+
40+
{% endset %}
41+
42+
{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }}

integration_tests/tests/test_generate_source.sql

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sources:
1212
tables:
1313
- name: data__a_relation
1414
- name: data__b_relation
15+
- name: data__campaign_analytics
1516
{% endset %}
1617

1718

integration_tests/tests/test_generate_source_all_args.sql

+14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ sources:
3636
- name: col_b
3737
description: ""
3838

39+
- name: data__campaign_analytics
40+
description: ""
41+
columns:
42+
- name: source
43+
description: ""
44+
- name: medium
45+
description: ""
46+
- name: source_medium
47+
description: ""
48+
- name: analytics
49+
description: ""
50+
- name: col_x
51+
description: ""
52+
3953
{% endset %}
4054

4155
{{ assert_equal (actual_source_yaml | trim, expected_source_yaml | trim) }}

integration_tests/tests/test_generate_source_exclude.sql

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ sources:
1111
- name: {{ raw_schema | trim | lower}}
1212
tables:
1313
- name: data__b_relation
14+
- name: data__campaign_analytics
1415
{% endset %}
1516

1617

integration_tests/tests/test_generate_source_table_descriptions.sql

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ sources:
1515
description: ""
1616
- name: data__b_relation
1717
description: ""
18+
- name: data__campaign_analytics
19+
description: ""
1820
{% endset %}
1921

2022

integration_tests/tests/test_generate_source_table_name.sql

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ sources:
1313
tables:
1414
- name: data__a_relation
1515
- name: data__b_relation
16+
- name: data__campaign_analytics
1617

1718
{% endset %}
1819

macros/generate_model_yaml.sql

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
{% macro generate_column_yaml(column, model_yaml, column_desc_dict, parent_column_name="") %}
2+
{% if parent_column_name %}
3+
{% set column_name = parent_column_name ~ "." ~ column.name %}
4+
{% else %}
5+
{% set column_name = column.name %}
6+
{% endif %}
7+
8+
{% do model_yaml.append(' - name: ' ~ column.name | lower ) %}
9+
{% do model_yaml.append(' description: "' ~ column_desc_dict.get(column.name | lower,'') ~ '"') %}
10+
{% do model_yaml.append('') %}
11+
12+
{% if column.fields|length > 0 %}
13+
{% for child_column in column.fields %}
14+
{% set model_yaml = codegen.generate_column_yaml(child_column, model_yaml, column_desc_dict, parent_column_name=column_name) %}
15+
{% endfor %}
16+
{% endif %}
17+
{% do return(model_yaml) %}
18+
{% endmacro %}
19+
120
{% macro generate_model_yaml(model_name, upstream_descriptions=False) %}
221

322
{% set model_yaml=[] %}
@@ -14,9 +33,7 @@
1433
{%- set columns = adapter.get_columns_in_relation(relation) -%}
1534

1635
{% for column in columns %}
17-
{% do model_yaml.append(' - name: ' ~ column.name | lower ) %}
18-
{% do model_yaml.append(' description: "' ~ column_desc_dict.get(column.name | lower,'') ~ '"') %}
19-
{% do model_yaml.append('') %}
36+
{% set model_yaml = codegen.generate_column_yaml(column, model_yaml, column_desc_dict) %}
2037
{% endfor %}
2138

2239
{% if execute %}
@@ -27,4 +44,4 @@
2744

2845
{% endif %}
2946

30-
{% endmacro %}
47+
{% endmacro %}

0 commit comments

Comments
 (0)