-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add macro to get columns * star macro should use get_columns * add adapter. * swap adapter for dbt_utils Co-authored-by: Joel Labes <[email protected]> * update documentation * add output_lower arg * update name to get_filtered_columns_in_relation from get_columns * add tests * forgot args * too much whitespace removal ----------- Actual: ----------- --->"field_3"as "test_field_3"<--- ----------- Expected: ----------- --->"field_3" as "test_field_3"<--- * didnt mean to move a file that i did not create. moving things back. * remove lowercase logic * limit_zero Co-authored-by: Joel Labes <[email protected]>
- Loading branch information
1 parent
9e32d9c
commit d279542
Showing
9 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
integration_tests/data/sql/data_filtered_columns_in_relation.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
field_1,field_2,field_3 | ||
a,b,c | ||
d,e,f | ||
g,h,i |
2 changes: 2 additions & 0 deletions
2
integration_tests/data/sql/data_filtered_columns_in_relation_expected.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
field_2,field_3 | ||
h,i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{% macro assert_equal_values(actual_object, expected_object) %} | ||
{% if not execute %} | ||
|
||
{# pass #} | ||
|
||
{% elif actual_object != expected_object %} | ||
|
||
{% set msg %} | ||
Expected did not match actual | ||
|
||
----------- | ||
Actual: | ||
----------- | ||
--->{{ actual_object }}<--- | ||
|
||
----------- | ||
Expected: | ||
----------- | ||
--->{{ expected_object }}<--- | ||
|
||
{% endset %} | ||
|
||
{{ log(msg, info=True) }} | ||
|
||
select 'fail' | ||
|
||
{% else %} | ||
|
||
select 'ok' {{ limit_zero() }} | ||
|
||
{% endif %} | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
integration_tests/models/sql/test_get_filtered_columns_in_relation.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% set exclude_field = 'field_1' %} | ||
{% set column_names = dbt_utils.get_filtered_columns_in_relation(from= ref('data_filtered_columns_in_relation'), except=[exclude_field]) %} | ||
|
||
with data as ( | ||
|
||
select | ||
|
||
{% for column_name in column_names %} | ||
max({{ column_name }}) as {{ column_name }} {% if not loop.last %},{% endif %} | ||
{% endfor %} | ||
|
||
from {{ ref('data_filtered_columns_in_relation') }} | ||
|
||
) | ||
|
||
select * from data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% set exclude_field = 'FIELD_3' %} | ||
|
||
|
||
with data as ( | ||
|
||
select | ||
{{ dbt_utils.star(from=ref('data_star'), except=[exclude_field]) }} | ||
|
||
from {{ ref('data_star') }} | ||
|
||
) | ||
|
||
select * from data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{% macro get_filtered_columns_in_relation(from, except=[]) -%} | ||
{{ return(adapter.dispatch('get_filtered_columns_in_relation', 'dbt_utils')(from, except)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__get_filtered_columns_in_relation(from, except=[]) -%} | ||
{%- do dbt_utils._is_relation(from, 'get_filtered_columns_in_relation') -%} | ||
{%- do dbt_utils._is_ephemeral(from, 'get_filtered_columns_in_relation') -%} | ||
|
||
{# -- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #} | ||
{%- if not execute -%} | ||
{{ return('') }} | ||
{% endif %} | ||
|
||
{%- set include_cols = [] %} | ||
{%- set cols = adapter.get_columns_in_relation(from) -%} | ||
{%- set except = except | map("lower") | list %} | ||
{%- for col in cols -%} | ||
{%- if col.column|lower not in except -%} | ||
{% do include_cols.append(col.column) %} | ||
{%- endif %} | ||
{%- endfor %} | ||
|
||
{{ return(include_cols) }} | ||
|
||
{%- endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters