Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functionality - Retrieve the description for identical column names from upstream models #61

Merged
merged 7 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
dbt --warn-error deps --target postgres
dbt --warn-error run-operation create_source_table --target postgres
dbt --warn-error seed --target postgres --full-refresh
dbt --warn-error run --target postgres
dbt --warn-error test --target postgres

- run:
Expand All @@ -55,6 +56,7 @@ jobs:
dbt --warn-error deps --target redshift
dbt --warn-error run-operation create_source_table --target redshift
dbt --warn-error seed --target redshift --full-refresh
dbt --warn-error run --target redshift
dbt --warn-error test --target redshift

- run:
Expand All @@ -66,6 +68,7 @@ jobs:
dbt --warn-error deps --target snowflake
dbt --warn-error run-operation create_source_table --target snowflake
dbt --warn-error seed --target snowflake --full-refresh
dbt --warn-error run --target snowflake
dbt --warn-error test --target snowflake

- run:
Expand All @@ -80,6 +83,7 @@ jobs:
dbt --warn-error deps --target bigquery
dbt --warn-error run-operation create_source_table --target bigquery
dbt --warn-error seed --target bigquery --full-refresh
dbt --warn-error run --target bigquery
dbt --warn-error test --target bigquery


Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

target/
dbt_modules/
dbt_packages/
logs/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased
## New features
- 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))

# dbt-codegen v0.6.0

This release creates breaking changes to the `generate_source.sql` macro.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ schema.yml file.

### Arguments:
* `model_name` (required): The model you wish to generate YAML for.
* `upstream_descriptions` (optional, default=False): Whether you want to include descriptions for identical column names from upstream models.

### Usage:
1. Create a model.
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/models/child_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select
*
from {{ ref('model_data_a') }}
3 changes: 3 additions & 0 deletions integration_tests/models/model_data_a.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select
*
from {{ ref('data__a_relation') }}
7 changes: 7 additions & 0 deletions integration_tests/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

models:
- name: model_data_a
columns:
- name: col_a
description: description column a
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% set actual_model_yaml = codegen.generate_model_yaml(
model_name='child_model',
upstream_descriptions=True
)
%}

{% set expected_model_yaml %}
version: 2

models:
- name: child_model
description: ""
columns:
- name: col_a
description: "description column a"

- name: col_b
description: ""

{% endset %}

{{ assert_equal (actual_model_yaml | trim, expected_model_yaml | trim) }}
5 changes: 3 additions & 2 deletions macros/generate_model_yaml.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% macro generate_model_yaml(model_name) %}
{% macro generate_model_yaml(model_name, upstream_descriptions=False) %}

{% set model_yaml=[] %}
{% set column_desc_dict = codegen.build_dict_column_descriptions(model_name) if upstream_descriptions else {} %}

{% do model_yaml.append('version: 2') %}
{% do model_yaml.append('') %}
Expand All @@ -14,7 +15,7 @@

{% for column in columns %}
{% do model_yaml.append(' - name: ' ~ column.name | lower ) %}
{% do model_yaml.append(' description: ""') %}
{% do model_yaml.append(' description: "' ~ column_desc_dict.get(column.name | lower,'') ~ '"') %}
{% do model_yaml.append('') %}
{% endfor %}

Expand Down
29 changes: 29 additions & 0 deletions macros/helpers/helpers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{# retrieve models directly upstream from a given model #}
{% macro get_model_dependencies(model_name) %}
{% for node in graph.nodes.values() | selectattr('name', "equalto", model_name) %}
{{ return(node.depends_on.nodes) }}
{% endfor %}
{% endmacro %}


{# add to an input dictionary entries containing all the column descriptions of a given model #}
{% macro add_model_column_descriptions_to_dict(model_name,dict_with_descriptions={}) %}
{% for node in graph.nodes.values() | selectattr('name', "equalto", model_name) %}
{% for col_name, col_values in node.columns.items() %}
{% do dict_with_descriptions.update( {col_name: col_values.description} ) %}
{% endfor %}
{% endfor %}
{{ return(dict_with_descriptions) }}
{% endmacro %}

{# build a global dictionary looping through all the direct parents models #}
{# if the same column name exists with different descriptions it is overwritten at each loop #}
{% macro build_dict_column_descriptions(model_name) %}
{% if execute %}
{% set glob_dict = {} %}
{% for full_model in codegen.get_model_dependencies(model_name) %}
{% do codegen.add_model_column_descriptions_to_dict(full_model.split('.')[-1],glob_dict) %}
{% endfor %}
{{ return(glob_dict) }}
{% endif %}
{% endmacro %}