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

Allow aliasing individual columns from star macro. #230

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,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: ">=0.15.1"
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?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it should, I'll PR that soon enough! :)


{% 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') -%}

Expand All @@ -19,7 +19,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