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

Update star.sql to allow for non-quote wrapped column names #706

Merged
merged 10 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ models:
```

## Fixes
- Add star macro option to not encase column names in quotes. ([#706](https://github.com/dbt-labs/dbt-utils/pull/706))
- Explicitly stating the namespace for cross-db macros so that the dispatch logic works correctly by restoring the dbt. prefix for all migrated cross-db macros ([#701](https://github.com/dbt-labs/dbt-utils/pull/701))
- Better handling of whitespaces in the star macro ([#651](https://github.com/dbt-labs/dbt-utils/pull/651))
- Fix to correct behavior in `mutually_exclusive_ranges` test in certain situations when `zero_length_range_allowed: true` and multiple ranges in a partition have the same value for `lower_bound_column`. ([[#659](https://github.com/dbt-labs/dbt-utils/issues/659)], [#660](https://github.com/dbt-labs/dbt-utils/pull/660))
- Fix to utilize dbt Core version of `escape_single_quotes` instead of version from dbt Utils ([[#689](https://github.com/dbt-labs/dbt-utils/issues/689)], [#692](https://github.com/dbt-labs/dbt-utils/pull/692))

## Contributors:
- [@CR-Lough] (https://github.com/CR-Lough) (#706)
- [@SimonQuvang](https://github.com/SimonQuvang) (#701)
- [@christineberger](https://github.com/christineberger) (#624)
- [@epapineau](https://github.com/epapineau) (#634)
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ 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. When one or both are provided, they will be concatenated onto each field's alias
in the output (`prefix` ~ `field_name` ~ `suffix`). NB: This prevents the output from being used in any context other than a select statement.
This macro also has an optional `quote_identifiers` argument that will encase the selected columns and their aliases in double quotes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first time I read this, I thought it meant quoting the optional relation_alias. But re-reading it, I realize it means the alias it is assigned (if any).


**Args:**

Expand All @@ -941,6 +942,7 @@ in the output (`prefix` ~ `field_name` ~ `suffix`). NB: This prevents the output
- `relation_alias` (optional, default=`''`): will prefix all generated fields with an alias (`relation_alias`.`field_name`).
- `prefix` (optional, default=`''`): will prefix the output `field_name` (`field_name as prefix_field_name`).
- `suffix` (optional, default=`''`): will suffix the output `field_name` (`field_name as field_name_suffix`).
- `quote_identifiers` (optional, default=`True`): will encase selected columns and aliases in double quotes (`"field_name" as "field_name"`).

**Usage:**

Expand All @@ -951,6 +953,13 @@ from {{ ref('my_model') }}

```

```sql
select
{{ dbt_utils.star(from=ref('my_model'), quote_identifiers=False) }}
from {{ ref('my_model') }}

```

```sql
select
{{ dbt_utils.star(from=ref('my_model'), except=["exclude_field_1", "exclude_field_2"]) }}
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/data/sql/data_star_quote_identifiers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
column_one
a
6 changes: 6 additions & 0 deletions integration_tests/models/sql/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ models:
- dbt_utils.equality:
compare_model: ref('data_star_expected')

- name: test_star_quote_identifiers
tests:
- assert_equal:
actual: actual
expected: expected

- name: test_star_prefix_suffix
tests:
- dbt_utils.equality:
Expand Down
9 changes: 9 additions & 0 deletions integration_tests/models/sql/test_star_quote_identifiers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select
{{ dbt.string_literal(adapter.quote("column_one")) | lower }} as expected,
{{ dbt.string_literal(dbt_utils.star(from=ref('data_star_quote_identifiers'), quote_identifiers=True)) | trim | lower }} as actual

union all

select
{{ dbt.string_literal("column_one") | lower }} as expected,
{{ dbt.string_literal(dbt_utils.star(from=ref('data_star_quote_identifiers'), quote_identifiers=False)) | trim | lower }} as actual
54 changes: 30 additions & 24 deletions macros/sql/star.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
{% 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=[], prefix='', suffix='') -%}
{%- do dbt_utils._is_relation(from, 'star') -%}
{%- do dbt_utils._is_ephemeral(from, 'star') -%}

{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
{%- if not execute -%}
{{ return('*') }}
{%- endif -%}

{% set cols = dbt_utils.get_filtered_columns_in_relation(from, except) %}

{%- if cols|length <= 0 -%}
{{- return('*') -}}
{%- else -%}
{%- for col in cols %}
{%- 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 %}
{%- endfor -%}
{% endif %}
{%- endmacro %}
{% macro star(from, relation_alias=False, except=[], prefix='', suffix='', quote_identifiers=True) -%}
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except, prefix, suffix, quote_identifiers)) }}
{% endmacro %}

{% macro default__star(from, relation_alias=False, except=[], prefix='', suffix='', quote_identifiers=True) -%}
{%- do dbt_utils._is_relation(from, 'star') -%}
{%- do dbt_utils._is_ephemeral(from, 'star') -%}

{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
{%- if not execute -%}
{{ return('*') }}
{%- endif -%}

{% set cols = dbt_utils.get_filtered_columns_in_relation(from, except) %}

{%- if cols|length <= 0 -%}
{{- return('*') -}}
{%- else -%}
{%- for col in cols %}
{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}
{%- if quote_identifiers -%}
{{ adapter.quote(col)|trim }} {%- if prefix!='' or suffix!='' %} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }} {%- endif -%}
{%- else -%}
{{ col|trim }} {%- if prefix!='' or suffix!='' %} as {{ (prefix ~ col ~ suffix)|trim }} {%- endif -%}
{% endif %}
{%- if not loop.last %},{{ '\n ' }}{%- endif -%}
{%- endfor -%}
{% endif %}
{%- endmacro %}