From eb7f6974e99705f6346776f6c07a0b8a0f6e45a6 Mon Sep 17 00:00:00 2001 From: Teghan Nightengale Date: Tue, 18 Apr 2023 21:40:32 -0400 Subject: [PATCH] Multiple Appended `nth_ever` Relationships (#38) Fixes #37 --- README.md | 2 +- dbt_project.yml | 2 +- .../models/nth_ever/dataset__nth_ever_1.sql | 27 +++++++++++++++++++ .../models/nth_ever/nth_ever.yml | 9 +++++++ .../seeds/nth_ever/input__nth_ever.csv | 17 ++++++++++++ .../nth_ever/output/output__nth_ever_1.csv | 3 +++ macros/dataset.sql | 4 +-- .../aliasing/alias_appended_activity.sql | 9 ++++++- pyproject.toml | 2 +- 9 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 integration_tests/models/nth_ever/dataset__nth_ever_1.sql create mode 100644 integration_tests/models/nth_ever/nth_ever.yml create mode 100644 integration_tests/seeds/nth_ever/input__nth_ever.csv create mode 100644 integration_tests/seeds/nth_ever/output/output__nth_ever_1.csv diff --git a/README.md b/README.md index 6be84ba..30f136e 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Include in `packages.yml`: ```yaml packages: - package: tnightengale/dbt_activity_schema - version: 0.4.0 + version: 0.4.1 ``` For latest release, see https://github.com/tnightengale/dbt-activity-schema/releases. diff --git a/dbt_project.yml b/dbt_project.yml index 3817edb..006b0ea 100644 --- a/dbt_project.yml +++ b/dbt_project.yml @@ -1,7 +1,7 @@ # Project name. name: 'dbt_activity_schema' -version: '0.4.0' +version: '0.4.1' config-version: 2 require-dbt-version: [">=1.3.0", "<2.0.0"] diff --git a/integration_tests/models/nth_ever/dataset__nth_ever_1.sql b/integration_tests/models/nth_ever/dataset__nth_ever_1.sql new file mode 100644 index 0000000..71237ee --- /dev/null +++ b/integration_tests/models/nth_ever/dataset__nth_ever_1.sql @@ -0,0 +1,27 @@ +{{ + dbt_activity_schema.dataset( + ref("input__nth_ever"), + dbt_activity_schema.activity( + dbt_activity_schema.nth_ever(2), + "visit page", + ["activity_occurrence"] + ), + [ + dbt_activity_schema.activity( + dbt_activity_schema.nth_ever(3), + "visit page", + ["activity_occurrence"] + ), + dbt_activity_schema.activity( + dbt_activity_schema.nth_ever(4), + "visit page", + ["activity_occurrence"] + ), + dbt_activity_schema.activity( + dbt_activity_schema.nth_ever(5), + "visit page", + ["activity_occurrence"] + ), + ] + ) +}} diff --git a/integration_tests/models/nth_ever/nth_ever.yml b/integration_tests/models/nth_ever/nth_ever.yml new file mode 100644 index 0000000..d944132 --- /dev/null +++ b/integration_tests/models/nth_ever/nth_ever.yml @@ -0,0 +1,9 @@ +version: 2 + +models: + - name: dataset__nth_ever_1 + + description: A dataset model used to test basic functionality of the `nth_ever` relationship. + tests: + - dbt_utils.equality: + compare_model: ref("output__nth_ever_1") diff --git a/integration_tests/seeds/nth_ever/input__nth_ever.csv b/integration_tests/seeds/nth_ever/input__nth_ever.csv new file mode 100644 index 0000000..eb43133 --- /dev/null +++ b/integration_tests/seeds/nth_ever/input__nth_ever.csv @@ -0,0 +1,17 @@ +activity_id,ts,entity_uuid,activity,anonymous_entity_uuid,feature_json,revenue_impact,link,activity_occurrence,activity_repeated_at +1,2022-01-01 22:10:11,1,visit page,,"{""type"": 1}",0,,1,2022-01-03 22:10:11 +2,2022-01-02 22:10:11,1,signed up,,"{""type"": 1}",0,,1, +3,2022-01-03 22:10:11,1,visit page,,"{""type"": 2}",0,,2,2022-01-04 22:10:14 +4,2022-01-04 22:10:12,1,added to cart,,"{""type"": 4}",0,,1,2022-01-04 22:10:13 +5,2022-01-04 22:10:13,1,added to cart,,"{""type"": 4}",0,,2, +6,2022-01-04 22:10:14,1,visit page,,"{""type"": 2}",0,,3,2022-01-06 22:10:16 +7,2022-01-05 22:10:15,1,bought something,,"{""type"": 1}",100,,1, +8,2022-01-06 22:10:16,1,visit page,,"{""type"": 1}",0,,4, +9,2022-01-07 22:10:11,7,visit page,,"{""type"": 1}",0,,1,2022-01-09 22:10:11 +10,2022-01-08 22:10:11,7,signed up,,"{""type"": 1}",0,,1, +11,2022-01-09 22:10:11,7,visit page,,"{""type"": 2}",0,,2,2022-01-10 22:10:14 +12,2022-01-10 22:10:12,7,added to cart,,"{""type"": 4}",0,,1,2022-01-10 22:10:13 +13,2022-01-10 22:10:13,7,added to cart,,"{""type"": 4}",0,,2, +14,2022-01-10 22:10:14,7,visit page,,"{""type"": 2}",0,,3,2022-01-12 22:10:16 +15,2022-01-11 22:10:15,7,bought something,,"{""type"": 1}",100,,1, +16,2022-01-12 22:10:16,7,visit page,,"{""type"": 1}",0,,4, diff --git a/integration_tests/seeds/nth_ever/output/output__nth_ever_1.csv b/integration_tests/seeds/nth_ever/output/output__nth_ever_1.csv new file mode 100644 index 0000000..e6a04c6 --- /dev/null +++ b/integration_tests/seeds/nth_ever/output/output__nth_ever_1.csv @@ -0,0 +1,3 @@ +activity_occurrence,nth_ever_3_visit_page_activity_occurrence,nth_ever_4_visit_page_activity_occurrence,nth_ever_5_visit_page_activity_occurrence +2,3.000000,4.000000, +2,3.000000,4.000000, diff --git a/macros/dataset.sql b/macros/dataset.sql index c920e30..1393b63 100644 --- a/macros/dataset.sql +++ b/macros/dataset.sql @@ -57,7 +57,7 @@ filter_activity_stream_using_primary_activity as ( select -- Primary Activity Columns - {% for col in primary_activity.included_columns %} + {% for col in primary_activity.included_columns + primary_activity.required_columns %} {{ primary() }}.{{- col }}, {% endfor %} @@ -94,7 +94,7 @@ filter_activity_stream_using_primary_activity as ( ) group by - {% for col in primary_activity.included_columns %} + {% for col in primary_activity.included_columns + primary_activity.required_columns %} {{ primary() }}.{{ col }}{%- if not loop.last -%},{%- endif %} {% endfor %} ), diff --git a/macros/utils/aliasing/alias_appended_activity.sql b/macros/utils/aliasing/alias_appended_activity.sql index 9a5d2a3..c004f2c 100644 --- a/macros/utils/aliasing/alias_appended_activity.sql +++ b/macros/utils/aliasing/alias_appended_activity.sql @@ -17,8 +17,15 @@ params: The name of the column that will be aliased. #} +{% set name = activity.relationship.name %} +{% if activity.relationship.name == 'nth_ever' %} + {% set name -%} + {{ name }}_{{ activity.relationship.nth_occurance }} + {%- endset %} +{% endif %} + {% set concatenated_activity_alias %} -{{ activity.relationship.name -}}_{{- activity.name | replace(" ", "_") -}}_{{- column_name -}} +{{ name -}}_{{- activity.name | replace(" ", "_") -}}_{{- column_name -}} {% endset %} {% do return(concatenated_activity_alias) %} diff --git a/pyproject.toml b/pyproject.toml index 4a3b489..6a73271 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dbt-activity-schema" -version = "0.4.0" +version = "0.4.1" description = "A dbt package to create models within the Activity Schema data model framework." authors = ["Teghan Nightengale ", "Bryce Codell "] license = "GNU"