Skip to content

Commit

Permalink
Add database argument to get_sde_or_context (Close #164)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlh1994 committed Mar 11, 2024
1 parent 9e63d0f commit d944a80
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ This macro takes a provided statement as argument and generates the SQL command



### get_sde_or_context ([source](macros/utils/get_context_or_sde.sql))
### get_sde_or_context ([source](macros/utils/get_sde_or_context.sql))

This macro exists for Redshift and Postgres users to more easily select their self-describing event and context tables and apply de-duplication before joining onto their (already de-duplicated) events table. The `root_id` and `root_tstamp` columns are by default returned as `schema_name_id` and `schema_name_tstamp` respectively, where `schema_name` is the value in the `schema_name` column of the table. In the case where multiple entities may be sent in the context (e.g. products in a search results), you should set the `single_entity` argument to `false` and use an additional criteria in your join (see [the snowplow docs](https://docs.snowplow.io/docs/modeling-your-data/modeling-your-data-with-dbt/dbt-advanced-usage/dbt-duplicates/) for further details).

Expand Down
4 changes: 2 additions & 2 deletions macros/base/base_create_snowplow_events_this_run.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ You may obtain a copy of the Snowplow Personal and Academic License Version 1.0
{% if unique_session_identifiers -%}
{% for identifier in unique_session_identifiers.values() %}
{% if identifier['schema']|lower != 'atomic' %}
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, identifier['schema'], lower_limit, upper_limit, identifier['prefix']) }},
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, identifier['schema'], lower_limit, upper_limit, identifier['prefix'], database=snowplow_events_database) }},
{%- endif -%}
{% endfor %}
{% endif %}
Expand All @@ -144,7 +144,7 @@ You may obtain a copy of the Snowplow Personal and Academic License Version 1.0
{%- set single_entity = ent_or_sde['single_entity'] -%}
{%- endif %}
{% if ent_or_sde['schema'] not in unique_session_identifiers.keys() %} {# Exclude any that we have already made above #}
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, name, lower_limit, upper_limit, prefix, single_entity) }},
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, name, lower_limit, upper_limit, prefix, single_entity, database=snowplow_events_database) }},
{% endif %}
{% endfor -%}
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ You may obtain a copy of the Snowplow Personal and Academic License Version 1.0
{% if session_identifiers %}
{% for identifier in session_identifiers %}
{% if identifier['schema']|lower != 'atomic' and identifier['schema'] not in unique_identifiers %}
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, identifier['schema'], lower_limit, upper_limit, identifier['prefix']) }},
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, identifier['schema'], lower_limit, upper_limit, identifier['prefix'], database=snowplow_events_database) }},
{% do unique_identifiers.update({identifier['schema']: identifier}) %}
{%- endif -%}
{% endfor %}
Expand All @@ -160,7 +160,7 @@ You may obtain a copy of the Snowplow Personal and Academic License Version 1.0
{% if user_identifiers%}
{% for identifier in user_identifiers %}
{% if identifier['schema']|lower != 'atomic' and identifier['schema'] not in unique_identifiers %}
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, identifier['schema'], lower_limit, upper_limit, identifier['prefix']) }},
{{ snowplow_utils.get_sde_or_context(snowplow_events_schema, identifier['schema'], lower_limit, upper_limit, identifier['prefix'], database=snowplow_events_database) }},
{% do unique_identifiers.update({identifier['schema']: identifier}) %}
{%- endif -%}
{% endfor %}
Expand Down
10 changes: 5 additions & 5 deletions macros/utils/get_sde_or_context.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ This program is licensed to you under the Snowplow Personal and Academic License
and you may not use this file except in compliance with the Snowplow Personal and Academic License Version 1.0.
You may obtain a copy of the Snowplow Personal and Academic License Version 1.0 at https://docs.snowplow.io/personal-and-academic-license-1.0/
#}
{% macro get_sde_or_context(schema, identifier, lower_limit, upper_limit, prefix = none, single_entity = true) %}
{{ return(adapter.dispatch('get_sde_or_context', 'snowplow_utils')(schema, identifier, lower_limit, upper_limit, prefix, single_entity)) }}
{% macro get_sde_or_context(schema, identifier, lower_limit, upper_limit, prefix = none, single_entity = true, database = target.database) %}
{{ return(adapter.dispatch('get_sde_or_context', 'snowplow_utils')(schema, identifier, lower_limit, upper_limit, prefix, single_entity, database)) }}
{% endmacro %}

{% macro default__get_sde_or_context(schema, identifier, lower_limit, upper_limit, prefix = none, single_entity = true) %}
{% macro default__get_sde_or_context(schema, identifier, lower_limit, upper_limit, prefix = none, single_entity = true, database = target.database) %}
{% if execute %}
{% do exceptions.raise_compiler_error('Macro get_sde_or_context is only for Postgres or Redshift, it is not supported for' ~ target.type) %}
{% endif %}
{% endmacro %}


{% macro postgres__get_sde_or_context(schema, identifier, lower_limit, upper_limit, prefix = none, single_entity = true) %}
{% macro postgres__get_sde_or_context(schema, identifier, lower_limit, upper_limit, prefix = none, single_entity = true, database = target.database) %}
{# Create a relation from the inputs then get all columns in that context/sde table #}
{% set relation = api.Relation.create(schema = schema, identifier = identifier) %}
{% set relation = api.Relation.create(database = database, schema = schema, identifier = identifier) %}
{# Get the schema name to be able to alias the timestamp and id #}
{% set schema_get_query %}
select schema_name from {{ relation }}
Expand Down
6 changes: 6 additions & 0 deletions macros/utils/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ macros:
- name: prefix
type: string
description: A string to prefix (additional `_` added automatically) the column names with. If not provided `root_id` and `root_tstamp` will be prefixed with the schema name.
- name: single_entity
type: boolean
description: Whether the entity is a single context or not (default true)
- name: database
type: string
description: The database for the context or sde table (default `target.database`)
- name: parse_agg_dict
description: '{{ doc("macro_parse_agg_dict") }}'
arguments:
Expand Down

0 comments on commit d944a80

Please sign in to comment.