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

fix: star macro support on Snowflake (DNA-23644: DNA-25720) #90

Merged
merged 5 commits into from
Apr 25, 2024
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
6 changes: 3 additions & 3 deletions .pipelines/azure-pipelines-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ jobs:
source dbt-env/bin/activate
cd $(dbtProjectPath)
dbt build --profiles-dir $(Agent.BuildDirectory)/self/.pipelines --profile default -t databricks-ci \
--vars '{"schema_sources": "$(DBT_SCHEMA_SOURCES)", "DBT_DATABRICKS_CATALOG": "$(DBT_DATABRICKS_CATALOG)", "DBT_DATABRICKS_HOST": "$(DBT_DATABRICKS_HOST)", "DBT_DATABRICKS_HTTP_PATH": "$(DBT_DATABRICKS_HTTP_PATH)", "DBT_DATABRICKS_TOKEN": "$(DBT_DATABRICKS_TOKEN)", "DBT_SCHEMA": "$(DBT_SCHEMA)"}'
--vars '{"DBT_DATABRICKS_CATALOG": "$(DBT_DATABRICKS_CATALOG)", "DBT_DATABRICKS_HOST": "$(DBT_DATABRICKS_HOST)", "DBT_DATABRICKS_HTTP_PATH": "$(DBT_DATABRICKS_HTTP_PATH)", "DBT_DATABRICKS_TOKEN": "$(DBT_DATABRICKS_TOKEN)", "DBT_SCHEMA": "$(DBT_SCHEMA)"}'
displayName: Test (Databricks)

- bash: |
Expand All @@ -119,12 +119,12 @@ jobs:
source dbt-env-sqlserver/bin/activate
cd $(dbtProjectPathQuotes)
dbt build --profiles-dir $(Agent.BuildDirectory)/self/.pipelines --profile default -t sqlserver-ci \
--vars '{"schema_sources": "$(DBT_SCHEMA_SOURCES)", "DBT_SQL_SERVER_SERVER": "$(DBT_SQL_SERVER_SERVER)", "DBT_SQL_SERVER_USER": "$(DBT_SQL_SERVER_USER)", "DBT_SQL_SERVER_PASSWORD": "$(DBT_SQL_SERVER_PASSWORD)", "DBT_SQL_SERVER_DATABASE": "$(DBT_SQL_SERVER_DATABASE)", "DBT_SCHEMA": "$(DBT_SCHEMA)"}'
--vars '{"DBT_SQL_SERVER_SERVER": "$(DBT_SQL_SERVER_SERVER)", "DBT_SQL_SERVER_USER": "$(DBT_SQL_SERVER_USER)", "DBT_SQL_SERVER_PASSWORD": "$(DBT_SQL_SERVER_PASSWORD)", "DBT_SQL_SERVER_DATABASE": "$(DBT_SQL_SERVER_DATABASE)", "DBT_SCHEMA": "$(DBT_SCHEMA)"}'
displayName: Test (SQL Server)

- bash: |
source dbt-env/bin/activate
cd $(dbtProjectPathQuotes)
dbt build --profiles-dir $(Agent.BuildDirectory)/self/.pipelines --profile default -t snowflake-ci \
--vars '{"schema_sources": "$(DBT_SCHEMA_SOURCES)", "DBT_SNOWFLAKE_ACCOUNT": "$(DBT_SNOWFLAKE_ACCOUNT)", "DBT_SNOWFLAKE_USER": "$(DBT_SNOWFLAKE_USER)", "DBT_SNOWFLAKE_PASSWORD": "$(DBT_SNOWFLAKE_PASSWORD)", "DBT_SNOWFLAKE_ROLE": "$(DBT_SNOWFLAKE_ROLE)", "DBT_SNOWFLAKE_DATABASE": "$(DBT_SNOWFLAKE_DATABASE)", "DBT_SNOWFLAKE_WAREHOUSE": "$(DBT_SNOWFLAKE_WAREHOUSE)", "DBT_SCHEMA": "$(DBT_SCHEMA)"}'
--vars '{"DBT_SNOWFLAKE_ACCOUNT": "$(DBT_SNOWFLAKE_ACCOUNT)", "DBT_SNOWFLAKE_USER": "$(DBT_SNOWFLAKE_USER)", "DBT_SNOWFLAKE_PASSWORD": "$(DBT_SNOWFLAKE_PASSWORD)", "DBT_SNOWFLAKE_ROLE": "$(DBT_SNOWFLAKE_ROLE)", "DBT_SNOWFLAKE_DATABASE": "$(DBT_SNOWFLAKE_DATABASE)", "DBT_SNOWFLAKE_WAREHOUSE": "$(DBT_SNOWFLAKE_WAREHOUSE)", "DBT_SCHEMA": "$(DBT_SCHEMA)"}'
displayName: Test (Snowflake)
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ You can choose to exclude fields from the select statement, for example:
- When you apply logic to a field and don't want to keep the original.
- When you join tables and a field with the same name is available on multiple tables.

Make sure to put the relation also in the from clause. Otherwise, the table from which you select can't be found.

Usage:

Select all fields from model `Table_A`.
```
select
{{ pm_utils.star(ref('Table_A')) }}
from ref('Table_A')
from {{ ref('Table_A') }}
```

Select all fields from source `Table_A`.
Expand All @@ -157,23 +159,12 @@ select
from source('sources', 'Table_A')
```

Select all fields from `Table_A`, which is first created as CTE. You can write `from Table_A`, but the argument of the star macro requires the `ref()`.
```
with Table_A as (
select * from {{ ref('Table_A') }}
)

select
{{ pm_utils.star(ref('Table_A')) }}
from Table_A
```

Select all fields from `Table_A`, except for the field `Creation_date`. More fields can be added to the except list. Additional select statements can be written before and after the `star()` macro by separating the statements with a comma.
```
select
{{ pm_utils.star(ref('Table_A'), except=['Creation_date']) }},
{{ pm_utils.to_date('"Creation_date"') }} as "Creation_date"
from Table_A
from {{ ref('Table_A') }}
```

### Data type cast functions
Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'pm_utils'
version: '1.4.0'
version: '1.4.1'
config-version: 2

require-dbt-version: [">=1.0.0", "<2.0.0"]
11 changes: 11 additions & 0 deletions integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ version: '1.0.0'
config-version: 2

profile: 'integration_tests'

# All models are materialized as table.
models:
pm_utils_integration_tests:
materialized: table

# Quoting is enabled for databases, schemas, and identifiers.
quoting:
database: true
schema: true
identifier: true
10 changes: 7 additions & 3 deletions macros/SQL_generators/star.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
{%- for column in columns -%}
{%- if column.name not in except -%}
{%- if target.type == 'databricks' -%}
{%- set selects = selects.append(relation.identifier + '.`' + column.name + '`') -%}
{%- set selects = selects.append('`' + relation.identifier + '`.`' + column.name + '`') -%}
{%- elif target.type in ('sqlserver', 'snowflake') -%}
{%- set selects = selects.append(relation.identifier + '."' + column.name + '"') -%}
{%- set selects = selects.append('"' + relation.identifier + '"."' + column.name + '"') -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
Expand All @@ -23,7 +23,11 @@
{%- endif -%}
{% endfor %}
{% else %}
{{ relation.identifier }}.*
{%- if target.type == 'databricks' -%}
`{{ relation.identifier }}`.*
{%- elif target.type in ('sqlserver', 'snowflake') -%}
"{{ relation.identifier }}".*
{%- endif -%}
{% endif %}

{% endmacro %}
Loading