Skip to content

Commit 31c503d

Browse files
Doctaconcrlough
and
crlough
authored
Update star.sql to allow for non-quote wrapped column names (#706)
* Update star.sql * Update star.sql * feat: add testing to star macro column encased in quotes functionality * chore: update schema.yml * Update star.sql * chore: update star.sql and schema.yml * chore: update star.sql to trim blank space * Update README.md * Update README.md adds example usage of star macro's quote_identifiers argument Co-authored-by: crlough <connor.lough@pitchbook.com>
1 parent 56cf179 commit 31c503d

File tree

6 files changed

+58
-24
lines changed

6 files changed

+58
-24
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ models:
5151
```
5252
5353
## Fixes
54+
- Add star macro option to not encase column names in quotes. ([#706](https://github.com/dbt-labs/dbt-utils/pull/706))
5455
- 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))
5556
- Better handling of whitespaces in the star macro ([#651](https://github.com/dbt-labs/dbt-utils/pull/651))
5657
- 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))
5758
- 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))
5859

5960
## Contributors:
61+
- [@CR-Lough] (https://github.com/CR-Lough) (#706)
6062
- [@SimonQuvang](https://github.com/SimonQuvang) (#701)
6163
- [@christineberger](https://github.com/christineberger) (#624)
6264
- [@epapineau](https://github.com/epapineau) (#634)

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ the star macro.
933933
This macro also has an optional `relation_alias` argument that will prefix all generated fields with an alias (`relation_alias`.`field_name`).
934934
The macro also has optional `prefix` and `suffix` arguments. When one or both are provided, they will be concatenated onto each field's alias
935935
in the output (`prefix` ~ `field_name` ~ `suffix`). NB: This prevents the output from being used in any context other than a select statement.
936+
This macro also has an optional `quote_identifiers` argument that will encase the selected columns and their aliases in double quotes.
936937

937938
**Args:**
938939

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

945947
**Usage:**
946948

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

952954
```
953955

956+
```sql
957+
select
958+
{{ dbt_utils.star(from=ref('my_model'), quote_identifiers=False) }}
959+
from {{ ref('my_model') }}
960+
961+
```
962+
954963
```sql
955964
select
956965
{{ dbt_utils.star(from=ref('my_model'), except=["exclude_field_1", "exclude_field_2"]) }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
column_one
2+
a

integration_tests/models/sql/schema.yml

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ models:
127127
- dbt_utils.equality:
128128
compare_model: ref('data_star_expected')
129129

130+
- name: test_star_quote_identifiers
131+
tests:
132+
- assert_equal:
133+
actual: actual
134+
expected: expected
135+
130136
- name: test_star_prefix_suffix
131137
tests:
132138
- dbt_utils.equality:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
select
2+
{{ dbt.string_literal(adapter.quote("column_one")) | lower }} as expected,
3+
{{ dbt.string_literal(dbt_utils.star(from=ref('data_star_quote_identifiers'), quote_identifiers=True)) | trim | lower }} as actual
4+
5+
union all
6+
7+
select
8+
{{ dbt.string_literal("column_one") | lower }} as expected,
9+
{{ dbt.string_literal(dbt_utils.star(from=ref('data_star_quote_identifiers'), quote_identifiers=False)) | trim | lower }} as actual

macros/sql/star.sql

+30-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
{% macro star(from, relation_alias=False, except=[], prefix='', suffix='') -%}
2-
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except, prefix, suffix)) }}
3-
{% endmacro %}
4-
5-
{% macro default__star(from, relation_alias=False, except=[], prefix='', suffix='') -%}
6-
{%- do dbt_utils._is_relation(from, 'star') -%}
7-
{%- do dbt_utils._is_ephemeral(from, 'star') -%}
8-
9-
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
10-
{%- if not execute -%}
11-
{{ return('*') }}
12-
{%- endif -%}
13-
14-
{% set cols = dbt_utils.get_filtered_columns_in_relation(from, except) %}
15-
16-
{%- if cols|length <= 0 -%}
17-
{{- return('*') -}}
18-
{%- else -%}
19-
{%- for col in cols %}
20-
{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }} {%- if prefix!='' or suffix!='' %} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }} {%- endif -%}
21-
{%- if not loop.last %},{{ '\n ' }}{% endif %}
22-
{%- endfor -%}
23-
{% endif %}
24-
{%- endmacro %}
1+
{% macro star(from, relation_alias=False, except=[], prefix='', suffix='', quote_identifiers=True) -%}
2+
{{ return(adapter.dispatch('star', 'dbt_utils')(from, relation_alias, except, prefix, suffix, quote_identifiers)) }}
3+
{% endmacro %}
4+
5+
{% macro default__star(from, relation_alias=False, except=[], prefix='', suffix='', quote_identifiers=True) -%}
6+
{%- do dbt_utils._is_relation(from, 'star') -%}
7+
{%- do dbt_utils._is_ephemeral(from, 'star') -%}
8+
9+
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
10+
{%- if not execute -%}
11+
{{ return('*') }}
12+
{%- endif -%}
13+
14+
{% set cols = dbt_utils.get_filtered_columns_in_relation(from, except) %}
15+
16+
{%- if cols|length <= 0 -%}
17+
{{- return('*') -}}
18+
{%- else -%}
19+
{%- for col in cols %}
20+
{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}
21+
{%- if quote_identifiers -%}
22+
{{ adapter.quote(col)|trim }} {%- if prefix!='' or suffix!='' %} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }} {%- endif -%}
23+
{%- else -%}
24+
{{ col|trim }} {%- if prefix!='' or suffix!='' %} as {{ (prefix ~ col ~ suffix)|trim }} {%- endif -%}
25+
{% endif %}
26+
{%- if not loop.last %},{{ '\n ' }}{%- endif -%}
27+
{%- endfor -%}
28+
{% endif %}
29+
{%- endmacro %}
30+

0 commit comments

Comments
 (0)