Skip to content

Commit

Permalink
Output a warning when star finds no columns, not '*' (#732)
Browse files Browse the repository at this point in the history
* Change star() behaviour when no columns returned

* Code review: return a * in compile mode
  • Loading branch information
joellabes authored Nov 29, 2022
1 parent e2838c9 commit 9e58405
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions integration_tests/models/sql/test_star_no_columns.sql
Original file line number Diff line number Diff line change
@@ -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
14 changes: 12 additions & 2 deletions macros/sql/star.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down

0 comments on commit 9e58405

Please sign in to comment.