Skip to content

Commit

Permalink
Merge pull request #11 from fivetran/MagicBot/add-union-schema
Browse files Browse the repository at this point in the history
Feature: Union schema compatibility
  • Loading branch information
fivetran-catfritz authored Oct 12, 2023
2 parents 0835630 + 58ecc14 commit 79f522e
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export CI_SNOWFLAKE_DBT_USER=$(gcloud secrets versions access latest --secret="C
export CI_SNOWFLAKE_DBT_WAREHOUSE=$(gcloud secrets versions access latest --secret="CI_SNOWFLAKE_DBT_WAREHOUSE" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_HOST=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_HOST" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_HTTP_PATH=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_HTTP_PATH" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_TOKEN=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_TOKEN" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_TOKEN=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_TOKEN" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_CATALOG=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_CATALOG" --project="dbt-package-testing-363917")
1 change: 1 addition & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ steps:
- "CI_DATABRICKS_DBT_HOST"
- "CI_DATABRICKS_DBT_HTTP_PATH"
- "CI_DATABRICKS_DBT_TOKEN"
- "CI_DATABRICKS_DBT_CATALOG"
commands: |
bash .buildkite/scripts/run_models.sh databricks
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# dbt_amazon_ads v0.3.0
[PR #11](https://github.com/fivetran/dbt_amazon_ads/pull/11) includes the following updates:
## Feature update 🎉
- Unioning capability! This adds the ability to union source data from multiple amazon_ads connectors. Refer to the [Union Multiple Connectors README section](https://github.com/fivetran/dbt_amazon_ads/blob/main/README.md#union-multiple-connectors) for more details.

## Under the hood 🚘
- In the source package, updated tmp models to union source data using the `fivetran_utils.union_data` macro.
- To distinguish which source each field comes from, added `source_relation` column in each staging and downstream model and applied the `fivetran_utils.source_relation` macro.
- The `source_relation` column is included in all joins in the transform package.
- Updated tests to account for the new `source_relation` column.

# dbt_amazon_ads v0.2.0
[PR #6](https://github.com/fivetran/dbt_amazon_ads/pull/6) includes the following updates:
## 🚨 Breaking changes
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Include the following amazon_ads package version in your `packages.yml` file:
```yaml
packages:
- package: fivetran/amazon_ads
version: [">=0.2.0", "<0.3.0"] # we recommend using ranges to capture non-breaking changes automatically
version: [">=0.3.0", "<0.4.0"] # we recommend using ranges to capture non-breaking changes automatically
```

Do NOT include the `amazon_ads_source` package in this file. The transformation package itself has a dependency on it and will install the source package as well.
Expand All @@ -76,7 +76,17 @@ vars:
```

## (Optional) Step 5: Additional configurations
<details><summary>Expand for configurations</summary>
### Union multiple connectors
If you have multiple amazon_ads connectors in Fivetran and would like to use this package on all of them simultaneously, we have provided functionality to do so. The package will union all of the data together and pass the unioned table into the transformations. You will be able to see which source it came from in the `source_relation` column of each model. To use this functionality, you will need to set either the `amazon_ads_union_schemas` OR `amazon_ads_union_databases` variables (cannot do both) in your root `dbt_project.yml` file:

```yml
vars:
amazon_ads_union_schemas: ['amazon_ads_usa','amazon_ads_canada'] # use this if the data is in different schemas/datasets of the same database/project
amazon_ads_union_databases: ['amazon_ads_usa','amazon_ads_canada'] # use this if the data is in different databases/projects but uses the same schema name
```
Please be aware that the native `source.yml` connection set up in the package will not function when the union schema/database feature is utilized. Although the data will be correctly combined, you will not observe the sources linked to the package models in the Directed Acyclic Graph (DAG). This happens because the package includes only one defined `source.yml`.

To connect your multiple schema/database sources to the package models, follow the steps outlined in the [Union Data Defined Sources Configuration](https://github.com/fivetran/dbt_fivetran_utils/tree/releases/v0.4.latest#union_data-source) section of the Fivetran Utils documentation for the union_data macro. This will ensure a proper configuration and correct visualization of connections in the DAG.

### Passing Through Additional Metrics
By default, this package will select `clicks`, `impressions`, and `cost` from the source reporting tables to store into the staging models. If you would like to pass through additional metrics to the staging models, add the following configurations to your `dbt_project.yml` file. These variables allow the pass-through fields to be aliased (`alias`) if desired, but not required. Use the following format for declaring the respective pass-through variables:
Expand Down Expand Up @@ -122,8 +132,6 @@ If an individual source table has a different name than the package expects, add
vars:
amazon_ads_<default_source_table_name>_identifier: your_table_name
```
</details>

## (Optional) Step 6: Orchestrate your models with Fivetran Transformations for dbt Core™
<details><summary>Expand for more details</summary>

Expand All @@ -138,7 +146,7 @@ This dbt package is dependent on the following dbt packages. Be aware that these
```yml
packages:
- package: fivetran/amazon_ads_source
version: [">=0.2.0", "<0.3.0"]
version: [">=0.3.0", "<0.4.0"]
- package: fivetran/fivetran_utils
version: [">=0.4.0", "<0.5.0"]
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: 'amazon_ads'
version: '0.2.0'
version: '0.3.0'
config-version: 2
require-dbt-version: [">=1.3.0", "<2.0.0"]
models:
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/ci/sample.profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ integration_tests:
schema: amazon_ads_integration_tests_02
threads: 8
databricks:
catalog: null
catalog: "{{ env_var('CI_DATABRICKS_DBT_CATALOG') }}"
host: "{{ env_var('CI_DATABRICKS_DBT_HOST') }}"
http_path: "{{ env_var('CI_DATABRICKS_DBT_HTTP_PATH') }}"
schema: amazon_ads_integration_tests_02
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
config-version: 2

name: 'amazon_ads_integration_tests'
version: '0.2.0'
version: '0.3.0'

profile: 'integration_tests'

Expand Down
4 changes: 3 additions & 1 deletion integration_tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dbt-redshift>=1.3.0,<2.0.0
dbt-postgres>=1.3.0,<2.0.0
dbt-spark>=1.3.0,<2.0.0
dbt-spark[PyHive]>=1.3.0,<2.0.0
dbt-databricks>=1.3.0,<2.0.0
dbt-databricks>=1.3.0,<2.0.0

oscrypto @ git+https://github.com/wbond/oscrypto.git@d5f3437
21 changes: 21 additions & 0 deletions models/amazon_ads.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- profile_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: profile_id
Expand All @@ -35,9 +38,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- ad_group_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: account_id
Expand Down Expand Up @@ -82,9 +88,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- ad_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: account_id
Expand Down Expand Up @@ -137,9 +146,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- campaign_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: account_id
Expand Down Expand Up @@ -180,9 +192,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- portfolio_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: account_id
Expand Down Expand Up @@ -227,9 +242,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- keyword_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: account_id
Expand Down Expand Up @@ -280,10 +298,13 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- search_term
- keyword_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: "{{ doc('report_date') }}"
- name: account_id
Expand Down
5 changes: 4 additions & 1 deletion models/amazon_ads__account_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ campaigns as (

fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
Expand All @@ -36,11 +37,13 @@ fields as (

left join campaigns
on campaigns.campaign_id = report.campaign_id
and campaigns.source_relation = report.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation


{{ dbt_utils.group_by(5) }}
{{ dbt_utils.group_by(6) }}
)

select *
Expand Down
7 changes: 6 additions & 1 deletion models/amazon_ads__ad_group_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ad_groups as (

fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
Expand All @@ -55,14 +56,18 @@ fields as (

left join ad_groups
on ad_groups.ad_group_id = report.ad_group_id
and ad_groups.source_relation = report.source_relation
left join campaigns
on campaigns.campaign_id = ad_groups.campaign_id
and campaigns.source_relation = ad_groups.source_relation
left join portfolios
on portfolios.portfolio_id = campaigns.portfolio_id
and portfolios.source_relation = campaigns.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation

{{ dbt_utils.group_by(15) }}
{{ dbt_utils.group_by(16) }}
)

select *
Expand Down
12 changes: 9 additions & 3 deletions models/amazon_ads__ad_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ads as (

fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
Expand Down Expand Up @@ -65,16 +66,21 @@ fields as (

left join ads
on ads.ad_id = report.ad_id
and ads.source_relation = report.source_relation
left join ad_groups
on ad_groups.ad_group_id = report.ad_group_id
and ad_groups.source_relation = report.source_relation
left join campaigns
on campaigns.campaign_id = report.campaign_id
and campaigns.source_relation = report.source_relation
left join portfolios
on portfolios.portfolio_id = campaigns.portfolio_id
on portfolios.portfolio_id = campaigns.portfolio_id
and portfolios.source_relation = campaigns.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation

{{ dbt_utils.group_by(19) }}
{{ dbt_utils.group_by(20) }}
)

select *
Expand Down
10 changes: 7 additions & 3 deletions models/amazon_ads__campaign_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ campaigns as (

fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
Expand All @@ -47,12 +48,15 @@ fields as (

left join campaigns
on campaigns.campaign_id = report.campaign_id
and campaigns.source_relation = report.source_relation
left join portfolios
on portfolios.portfolio_id = campaigns.portfolio_id
on portfolios.portfolio_id = campaigns.portfolio_id
and portfolios.source_relation = campaigns.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation

{{ dbt_utils.group_by(13) }}
{{ dbt_utils.group_by(14) }}
)

select *
Expand Down
8 changes: 7 additions & 1 deletion models/amazon_ads__keyword_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ keywords as (

fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
Expand Down Expand Up @@ -64,16 +65,21 @@ fields as (

left join keywords
on keywords.keyword_id = report.keyword_id
and keywords.source_relation = report.source_relation
left join ad_groups
on ad_groups.ad_group_id = report.ad_group_id
and ad_groups.source_relation = report.source_relation
left join campaigns
on campaigns.campaign_id = report.campaign_id
and campaigns.source_relation = report.source_relation
left join portfolios
on portfolios.portfolio_id = campaigns.portfolio_id
and portfolios.source_relation = campaigns.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation

{{ dbt_utils.group_by(18) }}
{{ dbt_utils.group_by(19) }}
)

select *
Expand Down
6 changes: 5 additions & 1 deletion models/amazon_ads__portfolio_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ campaigns as (

fields as (
select
report.source_relation,
report.date_day,
account_info.account_name,
account_info.account_id,
Expand All @@ -52,12 +53,15 @@ fields as (

left join campaigns
on campaigns.portfolio_id = portfolios.portfolio_id
and campaigns.source_relation = portfolios.source_relation
left join account_info
on account_info.profile_id = campaigns.profile_id
and account_info.source_relation = campaigns.source_relation
left join report
on report.campaign_id = campaigns.campaign_id
and report.source_relation = campaigns.source_relation

{{ dbt_utils.group_by(15) }}
{{ dbt_utils.group_by(16) }}
)

select *
Expand Down
Loading

0 comments on commit 79f522e

Please sign in to comment.