Skip to content

Commit

Permalink
Allow aliasing individual columns from star macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliott O'Hara authored and clrcrl committed Dec 23, 2020
1 parent 9490488 commit 171970e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@ Usage:

#### star ([source](macros/sql/star.sql))
This macro generates a list of all fields that exist in the `from` relation, excluding any fields listed in the `except` argument. The construction is identical to `select * from {{ref('my_model')}}`, replacing star (`*`) with the star macro. This macro also has an optional `relation_alias` argument that will prefix all generated fields with an alias.

It also has an optional arg that allows aliasing of individual columns.
Usage:
```
select
{{ dbt_utils.star(from=ref('my_model'), except=["exclude_field_1", "exclude_field_2"]) }}
{{ dbt_utils.star(from=ref('my_model'), except=["exclude_field_1", "exclude_field_2"], aliases={"field_x":"pretty_name", "field_y":"another_pretty_name"}) }}
from {{ref('my_model')}}
```

Expand Down
4 changes: 4 additions & 0 deletions integration_tests/data/sql/data_star_aliases_expected.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
field_1,field_2,aliased
a,b,c
d,e,f
g,h,i
1 change: 1 addition & 0 deletions integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name: 'dbt_utils_integration_tests'
version: '1.0'


profile: 'integration_tests'

# require-dbt-version: inherit this from dbt-utils
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ models:
- dbt_utils.equality:
compare_model: ref('data_star_expected')

- name: test_star_aliases
tests:
- dbt_utils.equality:
compare_model: ref('data_star_aliases_expected')

- name: test_surrogate_key
tests:
- assert_equal:
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/models/sql/test_star_aliases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

-- TODO : Should the star macro use a case-insensitive comparison for the `except` field on Snowflake?

{% set aliases = {'FIELD_3':'aliased'} if target.type == 'snowflake' else {'field_3':'aliased'} %}


with data as (

select
{{ dbt_utils.star(from=ref('data_star'), aliases=aliases) }}

from {{ ref('data_star') }}

)

select * from data
4 changes: 2 additions & 2 deletions macros/sql/star.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% macro star(from, relation_alias=False, except=[]) -%}
{% macro star(from, relation_alias=False, except=[], aliases={}) -%}

{%- do dbt_utils._is_relation(from, 'star') -%}
{%- do dbt_utils._is_ephemeral(from, 'star') -%}
Expand All @@ -21,7 +21,7 @@

{%- for col in include_cols %}

{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }}
{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }}{%- if col in aliases %} as {{ adapter.quote(aliases[col]) | trim }}{%- endif -%}
{%- if not loop.last %},{{ '\n ' }}{% endif %}

{%- endfor -%}
Expand Down

0 comments on commit 171970e

Please sign in to comment.