-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from GeneralMills/add_macros
Add macros
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @pinakidas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,67 @@ | ||
# gmi_common_dbt_utils | ||
# gmi_common_dbt_utils | ||
|
||
This dbt package contains macros which can reused across General Mills dbt projects | ||
|
||
## Installation Instructions | ||
|
||
To add this package into your dbt project you need to make an entry in the packages.yml file if its not already present | ||
|
||
```yml | ||
- git: "https://github.com/GeneralMills/gmi_common_dbt_utils.git" | ||
revision: main # use a branch or a tag name | ||
``` | ||
## Contents | ||
### Macros | ||
- [generate_schema_name](#generate_schema_name) [(source)](./macros/generate_schema_name.sql) | ||
- [smart_source](#smart_source) [(source)](./macros/smart_source.sql) | ||
### Usage | ||
#### generate_schema_name | ||
This overwrites the default implementation of generate_schema_name from the core package | ||
```text | ||
{{generate_schema_name('input')}} | ||
``` | ||
|
||
#### smart_source | ||
This macro helps use keep a check on the bigquery costs and at the same time validate sql queries end to end. | ||
You can use the codegen package to have the script generated and then replace `source` with `smart_source`. | ||
[Link](https://github.com/dbt-labs/dbt-codegen#usage-1) on usage of codegen for generating SQL for a base model | ||
|
||
Snippets to generate base mode code for reference | ||
|
||
Executing using scratchpad/statement tab in dbtCloud IDE | ||
``` | ||
{{ codegen.generate_base_model( | ||
source_name='<source_name>', | ||
table_name='<table_name>' | ||
) }} | ||
``` | ||
|
||
Executing the macro as an operation | ||
``` | ||
dbt run-operation generate_base_model --args '{"source_name": "<source_name>", "table_name": "<table_name>"}' | ||
``` | ||
|
||
On the generated sql, replace `source` with `smart_source` | ||
|
||
```sql | ||
with source as ( | ||
select * from {{ smart_source('<source_name>', '<table_name>') }} | ||
), | ||
|
||
renamed as ( | ||
select | ||
. | ||
. | ||
. | ||
from source | ||
), | ||
|
||
select * from renamed | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: 'gmi_common_dbt_utils' | ||
version: '0.1.0' | ||
|
||
require-dbt-version: [">=1.0.0", "<2.0.0"] | ||
|
||
config-version: 2 | ||
|
||
target-path: "target" | ||
clean-targets: ["target", "dbt_modules", "dbt_packages"] | ||
macro-paths: ["macros"] | ||
log-path: "logs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% macro generate_schema_name(custom_schema_name, node) -%} | ||
{%- set developer_dataset_number = env_var('DBT_DEVELOPER_DATASET_NUMBER') -%} | ||
{%- set use_zdev = env_var('DBT_USE_ZDEV_DATASET') -%} | ||
{%- set default_schema = target.schema -%} | ||
{%- if env_var('DBT_USE_ZDEV_DATASET') | upper == 'TRUE' and custom_schema_name is not none -%} | ||
{{ default_schema }}{{ developer_dataset_number }}_{{ custom_schema_name | trim }} | ||
{%- else -%} | ||
{{ custom_schema_name | trim }} | ||
{%- endif -%} | ||
{%- endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% macro smart_source(source_name, table_name) %} | ||
{%- if env_var('DBT_RUN_ENV') == 'DEV' -%} | ||
(select * from {{source(source_name,table_name) }} where 1 = 0) as __dbt_source_{{ table_name }} | ||
{% else %} | ||
{{source(source_name, table_name)}} | ||
{% endif %} | ||
{% endmacro %} |