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

add prefix and suffix arguments to star macro #436

Merged
merged 3 commits into from
Nov 26, 2021
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ 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.
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`).

**Usage:**
```sql
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
prefix_field_1_suffix,prefix_field_2_suffix,prefix_field_3_suffix
a,b,c
d,e,f
g,h,i
5 changes: 5 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ models:
- dbt_utils.equality:
compare_model: ref('data_star_expected')

- name: test_star_prefix_suffix
tests:
- dbt_utils.equality:
compare_model: ref('data_star_prefix_suffix_expected')

- name: test_surrogate_key
tests:
- assert_equal:
Expand Down
13 changes: 13 additions & 0 deletions integration_tests/models/sql/test_star_prefix_suffix.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% set prefix_with = 'prefix_' if target.type != 'snowflake' else 'PREFIX_' %}
{% set suffix_with = '_suffix' if target.type != 'snowflake' else '_SUFFIX' %}

with data as (

select
{{ dbt_utils.star(from=ref('data_star'), prefix=prefix_with, suffix=suffix_with) }}

from {{ ref('data_star') }}

)

select * from data
8 changes: 4 additions & 4 deletions macros/sql/star.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% macro star(from, relation_alias=False, except=[]) -%}
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except)) }}
{% macro star(from, relation_alias=False, except=[], prefix='', suffix='') -%}
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except, prefix, suffix)) }}
{% endmacro %}

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

Expand All @@ -24,7 +24,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 }} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }}
{%- if not loop.last %},{{ '\n ' }}{% endif %}

{%- endfor -%}
Expand Down