Skip to content

Commit c863448

Browse files
nickperrottNick Perrottjoellabes
authored
Enhance usability of star macro by only generating column aliases when 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>
1 parent ae65d05 commit c863448

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,9 @@ group by 1,2,3
729729
```
730730

731731
#### star ([source](macros/sql/star.sql))
732-
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`).
732+
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`).
733+
734+
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.
733735

734736
**Usage:**
735737
```sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
group_field_1,group_field_2,value_field
2+
a,b,1
3+
a,b,2
4+
c,d,3
5+
c,e,4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
group_field_1,group_field_2,value_field_sum
2+
a,b,3
3+
c,d,3
4+
c,e,4

integration_tests/models/sql/schema.yml

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ models:
111111
- dbt_utils.equality:
112112
compare_model: ref('data_star_prefix_suffix_expected')
113113

114+
- name: test_star_aggregate
115+
tests:
116+
- dbt_utils.equality:
117+
compare_model: ref('data_star_aggregate_expected')
118+
114119
- name: test_surrogate_key
115120
tests:
116121
- assert_equal:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{#-/*This test checks that column aliases aren't applied unless there's a prefix/suffix necessary, to ensure that GROUP BYs keep working*/-#}
2+
3+
{% set selected_columns = dbt_utils.star(from=ref('data_star_aggregate'), except=['value_field']) %}
4+
5+
with data as (
6+
7+
select
8+
{{ selected_columns }},
9+
sum(value_field) as value_field_sum
10+
11+
from {{ ref('data_star_aggregate') }}
12+
group by {{ selected_columns }}
13+
14+
)
15+
16+
select * from data

macros/sql/star.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
{%- for col in include_cols %}
2626

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

3030
{%- endfor -%}

0 commit comments

Comments
 (0)