Skip to content

Commit cffe8d5

Browse files
add prefix and suffix arguments to star macro (#436)
* add prefix and suffix to star macro * add tests * snowflake casing?
1 parent 6ed3130 commit cffe8d5

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ group by 1,2,3
726726
```
727727

728728
#### star ([source](macros/sql/star.sql))
729-
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.
729+
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`).
730730

731731
**Usage:**
732732
```sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
prefix_field_1_suffix,prefix_field_2_suffix,prefix_field_3_suffix
2+
a,b,c
3+
d,e,f
4+
g,h,i

integration_tests/models/sql/schema.yml

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ models:
106106
- dbt_utils.equality:
107107
compare_model: ref('data_star_expected')
108108

109+
- name: test_star_prefix_suffix
110+
tests:
111+
- dbt_utils.equality:
112+
compare_model: ref('data_star_prefix_suffix_expected')
113+
109114
- name: test_surrogate_key
110115
tests:
111116
- assert_equal:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% set prefix_with = 'prefix_' if target.type != 'snowflake' else 'PREFIX_' %}
2+
{% set suffix_with = '_suffix' if target.type != 'snowflake' else '_SUFFIX' %}
3+
4+
with data as (
5+
6+
select
7+
{{ dbt_utils.star(from=ref('data_star'), prefix=prefix_with, suffix=suffix_with) }}
8+
9+
from {{ ref('data_star') }}
10+
11+
)
12+
13+
select * from data

macros/sql/star.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro star(from, relation_alias=False, except=[]) -%}
2-
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except)) }}
1+
{% macro star(from, relation_alias=False, except=[], prefix='', suffix='') -%}
2+
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except, prefix, suffix)) }}
33
{% endmacro %}
44

5-
{% macro default__star(from, relation_alias=False, except=[]) -%}
5+
{% macro default__star(from, relation_alias=False, except=[], prefix='', suffix='') -%}
66
{%- do dbt_utils._is_relation(from, 'star') -%}
77
{%- do dbt_utils._is_ephemeral(from, 'star') -%}
88

@@ -24,7 +24,7 @@
2424

2525
{%- for col in include_cols %}
2626

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

3030
{%- endfor -%}

0 commit comments

Comments
 (0)