Skip to content

Commit

Permalink
Enhance usability of star macro by only generating column aliases whe…
Browse files Browse the repository at this point in the history
…n prefix and/or suffix is specified (#468)

* The star macro should only produce column aliases when there is either a prefix or suffix specified.

* Enhanced the readme for the star macro.

* Add new integration test

Co-authored-by: Nick Perrott <nperrott@roiti.com>
Co-authored-by: Josh Elston-Green
Co-authored-by: Joel Labes <joel.labes@dbtlabs.com>
  • Loading branch information
3 people authored Feb 17, 2022
1 parent ae65d05 commit c863448
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
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

0 comments on commit c863448

Please sign in to comment.