Skip to content

Commit

Permalink
Merge pull request #34 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 2a78589 + a331f80 commit 3af5d05
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 28 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
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# dbt_facebook_ads v0.UPDATE.UPDATE
# dbt_facebook_ads v0.7.0
[PR #34](https://github.com/fivetran/dbt_facebook_ads/pull/34) includes the following updates:
## Feature update 🎉
- Unioning capability! This adds the ability to union source data from multiple facebook_ads connectors. Refer to the [Union Multiple Connectors README section](https://github.com/fivetran/dbt_facebook_ads/blob/main/README.md#union-multiple-connectors) for more details.

## Under the Hood:
## 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 and window function partition clauses in the transform package.
- Updated tests to account for the new `source_relation` column.

[PR #27](https://github.com/fivetran/dbt_facebook_ads/pull/27) includes the following updates:
- Incorporated the new `fivetran_utils.drop_schemas_automation` macro into the end of each Buildkite integration test job.
- Updated the pull request [templates](/.github).
# dbt_facebook_ads v0.6.0
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Include the following facebook_ads package version in your `packages.yml` file:
```yaml
packages:
- package: fivetran/facebook_ads
version: [">=0.6.0", "<0.7.0"] # we recommend using ranges to capture non-breaking changes automatically
version: [">=0.7.0", "<0.8.0"] # we recommend using ranges to capture non-breaking changes automatically
```

Do NOT include the `facebook_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 @@ -79,7 +79,18 @@ vars:
```

## (Optional) Step 4: Additional configurations
<details><summary>Expand for configurations</summary>

### Union multiple connectors
If you have multiple facebook_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 `facebook_ads_union_schemas` OR `facebook_ads_union_databases` variables (cannot do both) in your root `dbt_project.yml` file:

```yml
vars:
facebook_ads_union_schemas: ['facebook_ads_usa','facebook_ads_canada'] # use this if the data is in different schemas/datasets of the same database/project
facebook_ads_union_databases: ['facebook_ads_usa','facebook_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 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 @@ -114,8 +125,6 @@ vars:
facebook_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 @@ -130,7 +139,7 @@ This dbt package is dependent on the following dbt packages. Please be aware tha
```yml
packages:
- package: fivetran/facebook_ads_source
version: [">=0.6.0", "<0.7.0"]
version: [">=0.7.0", "<0.8.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: 'facebook_ads'
version: '0.6.0'
version: '0.7.0'
config-version: 2
require-dbt-version: [">=1.3.0", "<2.0.0"]
models:
Expand Down
2 changes: 1 addition & 1 deletion docs/catalog.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 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: facebook_ads_integration_tests
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: facebook_ads_integration_tests
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,5 +1,5 @@
name: 'facebook_ads_integration_tests'
version: '0.6.0'
version: '0.7.0'
profile: 'integration_tests'
config-version: 2

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
12 changes: 12 additions & 0 deletions macros/get_url_tag_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
cleaned_json as (

select
source_relation,
_fivetran_id,
creative_id,
json_extract_array(replace(trim(url_tags, '"'),'\\','')) as cleaned_url_tags
Expand All @@ -16,6 +17,7 @@
unnested as (

select
source_relation,
_fivetran_id,
creative_id,
url_tag_element
Expand All @@ -27,6 +29,7 @@
fields as (

select
source_relation,
_fivetran_id,
creative_id,
json_extract_scalar(url_tag_element, '$.key') as key,
Expand All @@ -42,6 +45,7 @@
cleaned_json as (

select
source_relation,
_fivetran_id,
creative_id,
replace(trim(url_tags::text, '"'),'\\','')::json as cleaned_url_tags
Expand All @@ -51,6 +55,7 @@
unnested as (

select
source_relation,
_fivetran_id,
creative_id,
url_tag_element
Expand All @@ -62,6 +67,7 @@
fields as (

select
source_relation,
_fivetran_id,
creative_id,
url_tag_element->>'key' as key,
Expand All @@ -83,6 +89,7 @@
flattened_url_tags as (

select
source_relation,
_fivetran_id,
creative_id,
json_extract_array_element_text(required_fields.url_tags, numbers.generated_number::int - 1, true) as element
Expand All @@ -94,6 +101,7 @@
fields as (

select
source_relation,
_fivetran_id,
creative_id,
json_extract_path_text(element,'key') as key,
Expand All @@ -109,6 +117,7 @@
cleaned_fields as (

select
source_relation,
_fivetran_id,
creative_id,
parse_json(url_tags) as url_tags
Expand All @@ -119,6 +128,7 @@
fields as (

select
source_relation,
_fivetran_id,
creative_id,
url_tags.value:key::string as key,
Expand All @@ -135,6 +145,7 @@
cleaned_fields as (

select
source_relation,
_fivetran_id,
creative_id,
explode(from_json(url_tags, 'array<struct<key:STRING, value:STRING, type:STRING>>')) as url_tags
Expand All @@ -145,6 +156,7 @@
fields as (

select
source_relation,
_fivetran_id,
creative_id,
url_tags.key as key,
Expand Down
18 changes: 18 additions & 0 deletions models/facebook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- account_id
- campaign_id
Expand All @@ -15,6 +16,8 @@ models:
# Below is so we don't run this test if the user has allowed urls to have null entries
enabled: "{{ var('ad_reporting__url_report__using_null_filter', True) }}"
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: The date of the performance.
- name: account_id
Expand Down Expand Up @@ -70,12 +73,15 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- account_id
- campaign_id
- ad_set_id
- ad_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: The date of the performance.
- name: account_id
Expand Down Expand Up @@ -108,11 +114,14 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- account_id
- campaign_id
- ad_set_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: The date of the performance.
- name: account_id
Expand Down Expand Up @@ -141,10 +150,13 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- account_id
- campaign_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: The date of the performance.
- name: account_id
Expand All @@ -169,9 +181,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_day
- account_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: The date of the performance.
- name: account_id
Expand All @@ -192,10 +207,13 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- _fivetran_id
- key
- type
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_id
description: The unique fivetran ID for this record.
tests:
Expand Down
4 changes: 3 additions & 1 deletion models/facebook_ads__account_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ accounts as (
joined as (

select
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
Expand All @@ -34,7 +35,8 @@ joined as (
from report
left join accounts
on report.account_id = accounts.account_id
{{ dbt_utils.group_by(8) }}
and report.source_relation = accounts.source_relation
{{ dbt_utils.group_by(9) }}
)

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

select
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
Expand All @@ -59,13 +60,17 @@ joined as (
from report
left join accounts
on report.account_id = accounts.account_id
and report.source_relation = accounts.source_relation
left join ads
on report.ad_id = ads.ad_id
and report.source_relation = ads.source_relation
left join campaigns
on ads.campaign_id = campaigns.campaign_id
and ads.source_relation = campaigns.source_relation
left join ad_sets
on ads.ad_set_id = ad_sets.ad_set_id
{{ dbt_utils.group_by(9) }}
and ads.source_relation = ad_sets.source_relation
{{ dbt_utils.group_by(10) }}
)

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

select
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
Expand All @@ -62,13 +63,17 @@ joined as (
from report
left join accounts
on report.account_id = accounts.account_id
and report.source_relation = accounts.source_relation
left join ads
on report.ad_id = ads.ad_id
and report.source_relation = ads.source_relation
left join campaigns
on ads.campaign_id = campaigns.campaign_id
and ads.source_relation = campaigns.source_relation
left join ad_sets
on ads.ad_set_id = ad_sets.ad_set_id
{{ dbt_utils.group_by(12) }}
and ads.source_relation = ad_sets.source_relation
{{ dbt_utils.group_by(13) }}
)

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

select
report.source_relation,
report.date_day,
accounts.account_id,
accounts.account_name,
Expand All @@ -53,11 +54,14 @@ joined as (
from report
left join accounts
on report.account_id = accounts.account_id
and report.source_relation = accounts.source_relation
left join ads
on report.ad_id = ads.ad_id
and report.source_relation = ads.source_relation
left join campaigns
on ads.campaign_id = campaigns.campaign_id
{{ dbt_utils.group_by(11) }}
and ads.source_relation = campaigns.source_relation
{{ dbt_utils.group_by(12) }}
)

select *
Expand Down
Loading

0 comments on commit 3af5d05

Please sign in to comment.