Skip to content

Commit

Permalink
Merge pull request #20 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 committed Oct 12, 2023
2 parents fcda5ca + 62fcf76 commit 8c96517
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 26 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")
3 changes: 2 additions & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ steps:
commands: |
bash .buildkite/scripts/run_models.sh redshift
- label: ":bricks: Run Tests - Databricks"
- label: ":databricks: Run Tests - Databricks"
key: "run_dbt_databricks"
plugins:
- docker#v3.13.0:
Expand All @@ -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
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# dbt_apple_search_ads v0.3.0
[PR #20](https://github.com/fivetran/dbt_apple_search_ads/pull/20) includes the following updates:
## Feature update 🎉
- Unioning capability! This adds the ability to union source data from multiple apple_search_ads connectors. Refer to the [Union Multiple Connectors README section](https://github.com/fivetran/dbt_apple_search_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_apple_search_ads v0.2.2
## Bugfix:
- Updated the dbt_utils.unique_combination_of_columns test for the `apple_search_ads__search_term_report` to include the following fields. ([PR #18](https://github.com/fivetran/dbt_apple_search_ads/pull/18)):
Expand Down Expand Up @@ -66,4 +77,4 @@ Accidental Release
# dbt_apple_search_ads v0.1.0

## Initial Release
- This is the initial release of this package. For more information refer to the [README](/README.md).
- This is the initial release of this package. For more information refer to the [README](/README.md).
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Include the following apple_search_ads package version in your `packages.yml` fi
```yaml
packages:
- package: fivetran/apple_search_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 `apple_search_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 @@ -68,7 +68,17 @@ vars:

## (Optional) Step 4: Additional configurations

<details><summary>Expand for configurations</summary>
### Union multiple connectors
If you have multiple apple_search_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 `apple_search_ads_union_schemas` OR `apple_search_ads_union_databases` variables (cannot do both) in your root `dbt_project.yml` file:

```yml
vars:
apple_search_ads_union_schemas: ['apple_search_ads_usa','apple_search_ads_canada'] # use this if the data is in different schemas/datasets of the same database/project
apple_search_ads_union_databases: ['apple_search_ads_usa','apple_search_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.

### Adding passthrough 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 below configurations to your `dbt_project.yml` file. These variables allow for the pass-through fields to be aliased (`alias`) if desired, but not required. Use the below format for declaring the respective pass-through variables:
Expand Down Expand Up @@ -129,8 +139,6 @@ vars:
apple_search_ads_<default_source_table_name>_identifier: your_table_name
```

</details>

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

Expand All @@ -145,7 +153,7 @@ This dbt package is dependent on the following dbt packages. Please be aware tha
```yml
packages:
- package: fivetran/apple_search_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: 'apple_search_ads'
version: '0.2.2'
version: '0.3.0'
config-version: 2
require-dbt-version: [">=1.3.0", "<2.0.0"]
vars:
Expand Down
2 changes: 1 addition & 1 deletion docs/catalog.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/manifest.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/run_results.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions integration_tests/ci/sample.profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ integration_tests:
schema: apple_search_ads_integrations_tests_1
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: apple_search_ads_integrations_tests_1
threads: 2
threads: 8
token: "{{ env_var('CI_DATABRICKS_DBT_TOKEN') }}"
type: databricks
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: 'apple_search_ads_integration_tests'
version: '0.2.2'
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
18 changes: 18 additions & 0 deletions models/apple_search_ads.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- organization_id
- campaign_id
- ad_group_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with this record.
- name: organization_name
Expand Down Expand Up @@ -55,12 +58,15 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- organization_id
- campaign_id
- ad_group_id
- ad_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with this record.
- name: organization_name
Expand Down Expand Up @@ -105,10 +111,13 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- organization_id
- campaign_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with this record.
- name: organization_name
Expand Down Expand Up @@ -149,12 +158,15 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- organization_id
- campaign_id
- ad_group_id
- keyword_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with this record.
- name: organization_name
Expand Down Expand Up @@ -201,9 +213,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- organization_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with this record.
tests:
Expand Down Expand Up @@ -234,6 +249,7 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- search_term_text
- match_type
- date_day
Expand All @@ -242,6 +258,8 @@ models:
- campaign_id
- organization_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with this record.
- name: organization_name
Expand Down
6 changes: 5 additions & 1 deletion models/apple_search_ads__ad_group_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ organization as (
joined as (

select
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
Expand All @@ -51,11 +52,14 @@ joined as (
from report
join ad_group
on report.ad_group_id = ad_group.ad_group_id
and report.source_relation = ad_group.source_relation
join campaign
on ad_group.campaign_id = campaign.campaign_id
and ad_group.source_relation = campaign.source_relation
join organization
on ad_group.organization_id = organization.organization_id
{{ dbt_utils.group_by(11) }}
and ad_group.source_relation = organization.source_relation
{{ dbt_utils.group_by(12) }}
)

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

select
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
Expand All @@ -58,13 +59,17 @@ joined as (
from report
join ad
on report.ad_id = ad.ad_id
and report.source_relation = ad.source_relation
join ad_group
on report.ad_group_id = ad_group.ad_group_id
and report.source_relation = ad_group.source_relation
join campaign
on report.campaign_id = campaign.campaign_id
and report.source_relation = campaign.source_relation
join organization
on ad.organization_id = organization.organization_id
{{ dbt_utils.group_by(11) }}
and ad.source_relation = organization.source_relation
{{ dbt_utils.group_by(12) }}
)

select *
Expand Down
5 changes: 4 additions & 1 deletion models/apple_search_ads__campaign_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ organization as (
joined as (

select
report.source_relation,
report.date_day,
campaign.organization_id,
organization.organization_name,
Expand All @@ -44,9 +45,11 @@ joined as (
from report
join campaign
on report.campaign_id = campaign.campaign_id
and report.source_relation = campaign.source_relation
join organization
on campaign.organization_id = organization.organization_id
{{ dbt_utils.group_by(9) }}
and campaign.source_relation = organization.source_relation
{{ dbt_utils.group_by(10) }}
)

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

select
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
Expand All @@ -59,13 +60,17 @@ joined as (
from report
join keyword
on report.keyword_id = keyword.keyword_id
and report.source_relation = keyword.source_relation
join ad_group
on keyword.ad_group_id = ad_group.ad_group_id
and keyword.source_relation = ad_group.source_relation
join campaign
on ad_group.campaign_id = campaign.campaign_id
and ad_group.source_relation = campaign.source_relation
join organization
on ad_group.organization_id = organization.organization_id
{{ dbt_utils.group_by(12) }}
and ad_group.source_relation = organization.source_relation
{{ dbt_utils.group_by(13) }}
)

select *
Expand Down
5 changes: 4 additions & 1 deletion models/apple_search_ads__organization_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ organization as (
joined as (

select
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
Expand All @@ -37,9 +38,11 @@ joined as (
from report
join campaign
on report.campaign_id = campaign.campaign_id
and report.source_relation = campaign.source_relation
join organization
on campaign.organization_id = organization.organization_id
{{ dbt_utils.group_by(4) }}
and campaign.source_relation = organization.source_relation
{{ dbt_utils.group_by(5) }}
)

select *
Expand Down
5 changes: 4 additions & 1 deletion models/apple_search_ads__search_term_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ organization as (
joined as (

select
report.source_relation,
report.date_day,
organization.organization_id,
organization.organization_name,
Expand All @@ -45,10 +46,12 @@ joined as (
from report
join campaign
on report.campaign_id = campaign.campaign_id
and report.source_relation = campaign.source_relation
join organization
on campaign.organization_id = organization.organization_id
and campaign.source_relation = organization.source_relation
where report.search_term_text is not null
{{ dbt_utils.group_by(12) }}
{{ dbt_utils.group_by(13) }}
)

select *
Expand Down
2 changes: 1 addition & 1 deletion packages.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- package: fivetran/apple_search_ads_source
version: [">=0.2.0", "<0.3.0"]
version: [">=0.3.0", "<0.4.0"]

0 comments on commit 8c96517

Please sign in to comment.