From 9e584055e3a59464ab9de71587f07d042dc41fa0 Mon Sep 17 00:00:00 2001 From: Joel Labes Date: Tue, 29 Nov 2022 15:30:49 +1300 Subject: [PATCH] Output a warning when star finds no columns, not '*' (#732) * Change star() behaviour when no columns returned * Code review: return a * in compile mode --- integration_tests/models/sql/schema.yml | 6 ++++++ .../models/sql/test_star_no_columns.sql | 11 +++++++++++ macros/sql/star.sql | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 integration_tests/models/sql/test_star_no_columns.sql 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 -%}