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 macro to get columns #516

Merged
merged 13 commits into from
Mar 28, 2022
Prev Previous commit
Next Next commit
star macro should use get_columns
  • Loading branch information
patkearns10 committed Mar 22, 2022
commit 1999e2bdca756b0302b1cc86c16dcb2513f4df5e
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,21 +587,15 @@ This macro returns the unique values for a column in a given [relation](https://
```

#### get_columns ([source](macros/sql/get_columns.sql))
This macro returns the unique columns for a given [relation](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation) as a comma-separated list.
This macro returns an iterable Jinja list of columns for a given [relation](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation)

**Args:**
- `from` (required): a [Relation](https://docs.getdbt.com/reference/dbt-classes#relation) (a `ref` or `source`) that contains the list of columns you wish to select from
- `except` (optional, default=`[]`): The name of the columns you wish to exclude
patkearns10 marked this conversation as resolved.
Show resolved Hide resolved

{% set column_names = get_columns(from=ref('fct_rate_rapid_onboarding'), except=["rate_rapid_onboarding__survey_question_response_id", "rate_rapid_onboarding__survey_response_id"]) %}

{% for column_name in column_names %}
max({{ column_name }}) ... as '{{ column_name }}'_test,
{% endfor %}

**Usage:**
```sql
-- Returns a list of the columns from a relation, then iterate in a for loop
-- Returns a list of the columns from a relation, so you can then iterate in a for loop
{% set column_names = get_columns(from=ref('your_model'), except=["field_1", "field_2"]) %}
...
{% for column_name in column_names %}
Expand Down
6 changes: 6 additions & 0 deletions macros/sql/get_columns.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{% macro get_columns(from, except=[]) -%}
patkearns10 marked this conversation as resolved.
Show resolved Hide resolved
{{ return(adapter.dispatch('get_columns', 'dbt_utils')(from, except)) }}
{% endmacro %}

{% macro default__get_columns(from, except=[]) -%}
{%- do dbt_utils._is_relation(from, 'get_columns') -%}
{%- do dbt_utils._is_ephemeral(from, 'get_columns') -%}

{# -- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
{%- if not execute -%}
Expand Down
13 changes: 1 addition & 12 deletions macros/sql/star.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@
{{ return('') }}
{% endif %}

{%- set include_cols = [] %}
{%- set cols = adapter.get_columns_in_relation(from) -%}
{%- set except = except | map("lower") | list %}
{%- for col in cols -%}

{%- if col.column|lower not in except -%}
{% do include_cols.append(col.column) %}

{%- endif %}
{%- endfor %}

{%- for col in include_cols %}
{%- for col in get_columns(from, except) %}

{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }} {%- if prefix!='' or suffix!='' -%} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }} {%- endif -%}
{%- if not loop.last %},{{ '\n ' }}{% endif %}
Expand Down