diff --git a/integration_tests/models/sql/schema.yml b/integration_tests/models/sql/schema.yml index 0dd87bf3..fab8a20b 100644 --- a/integration_tests/models/sql/schema.yml +++ b/integration_tests/models/sql/schema.yml @@ -178,6 +178,12 @@ models: - dbt_utils.equality: compare_model: ref('data_star_expected') + - name: test_star_no_columns + columns: + - name: canary_column #If the no-columns state isn't hit, this table won't be queryable because there will be a missing comma + tests: + - not_null + - name: test_generate_surrogate_key tests: - assert_equal: diff --git a/integration_tests/models/sql/test_star_no_columns.sql b/integration_tests/models/sql/test_star_no_columns.sql new file mode 100644 index 00000000..ff5a5de9 --- /dev/null +++ b/integration_tests/models/sql/test_star_no_columns.sql @@ -0,0 +1,11 @@ +with data as ( + + select + {{ dbt_utils.star(from=ref('data_star'), except=['field_1', 'field_2', 'field_3']) }} + -- if star() returns `*` or a list of columns, this query will fail because there's no comma between the columns + 1 as canary_column + from {{ ref('data_star') }} + +) + +select * from data diff --git a/macros/sql/star.sql b/macros/sql/star.sql index 172e5c22..515b77b6 100644 --- a/macros/sql/star.sql +++ b/macros/sql/star.sql @@ -8,13 +8,23 @@ {#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #} {%- if not execute -%} - {{ return('*') }} + {% do return('*') %} {%- endif -%} {% set cols = dbt_utils.get_filtered_columns_in_relation(from, except) %} {%- if cols|length <= 0 -%} - {{- return('*') -}} + {% if flags.WHICH == 'compile' %} + {% set response %} +* +/* No columns were returned. Maybe the relation doesn't exist yet +or all columns were excluded. This star is only output during +dbt compile, and exists to keep SQLFluff happy. */ + {% endset %} + {% do return(response) %} + {% else %} + {% do return("/* no columns returned from star() macro */") %} + {% endif %} {%- else -%} {%- for col in cols %} {%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}