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 4 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
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/
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 directly upstream models 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 dictionnary entries containing all the column descriptions of a given model #}
b-per marked this conversation as resolved.
Show resolved Hide resolved
{% 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 dictionnary looping through all the direct parents models #}
b-per marked this conversation as resolved.
Show resolved Hide resolved
{# 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 %}