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

Enhance usability of star macro by only generating column aliases when prefix and/or suffix is specified #468

Merged
merged 8 commits into from
Feb 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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,9 @@ group by 1,2,3
```

#### 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 (`relation_alias`.`field_name`). The macro also has optional `prefix` and `suffix` arguments, which will be appropriately concatenated to each field name in the output (`prefix` ~ `field_name` ~ `suffix`).
This macro generates a comma-separated 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 (`relation_alias`.`field_name`).

The macro also has optional `prefix` and `suffix` arguments. When one or both are provided, they will be concatenated onto each field's alias in the output (`prefix` ~ `field_name` ~ `suffix`). NB: This prevents the output from being used in any context other than a select statement.

**Usage:**
```sql
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/data/sql/data_star_aggregate.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
group_field_1,group_field_2,value_field
a,b,1
a,b,2
c,d,3
c,e,4
4 changes: 4 additions & 0 deletions integration_tests/data/sql/data_star_aggregate_expected.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
group_field_1,group_field_2,value_field_sum
a,b,3
c,d,3
c,e,4
5 changes: 5 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ models:
- dbt_utils.equality:
compare_model: ref('data_star_prefix_suffix_expected')

- name: test_star_aggregate
tests:
- dbt_utils.equality:
compare_model: ref('data_star_aggregate_expected')

- name: test_surrogate_key
tests:
- assert_equal:
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/models/sql/test_star_aggregate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{#-/*This test checks that column aliases aren't applied unless there's a prefix/suffix necessary, to ensure that GROUP BYs keep working*/-#}

{% set selected_columns = dbt_utils.star(from=ref('data_star_aggregate'), except=['value_field']) %}

with data as (

select
{{ selected_columns }},
sum(value_field) as value_field_sum

from {{ ref('data_star_aggregate') }}
group by {{ selected_columns }}

)

select * from data
2 changes: 1 addition & 1 deletion macros/sql/star.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

{%- for col in include_cols %}

{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }}
{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }} {%- if prefix!='' or suffix!='' -%} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }} {%- endif -%}
{%- if not loop.last %},{{ '\n ' }}{% endif %}

{%- endfor -%}
Expand Down