diff --git a/README.md b/README.md index 72861e16..ca46d193 100644 --- a/README.md +++ b/README.md @@ -726,7 +726,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 diff --git a/integration_tests/data/sql/data_star_prefix_suffix_expected.csv b/integration_tests/data/sql/data_star_prefix_suffix_expected.csv new file mode 100644 index 00000000..fd34199b --- /dev/null +++ b/integration_tests/data/sql/data_star_prefix_suffix_expected.csv @@ -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 diff --git a/integration_tests/models/sql/schema.yml b/integration_tests/models/sql/schema.yml index 0e8b2497..5b341457 100644 --- a/integration_tests/models/sql/schema.yml +++ b/integration_tests/models/sql/schema.yml @@ -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: diff --git a/integration_tests/models/sql/test_star_prefix_suffix.sql b/integration_tests/models/sql/test_star_prefix_suffix.sql new file mode 100644 index 00000000..ae1c64fe --- /dev/null +++ b/integration_tests/models/sql/test_star_prefix_suffix.sql @@ -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 \ No newline at end of file diff --git a/macros/sql/star.sql b/macros/sql/star.sql index d659e7cb..ffe48946 100644 --- a/macros/sql/star.sql +++ b/macros/sql/star.sql @@ -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') -%} @@ -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 -%}