Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce use of window function in sessions #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
profile: dbt-clickhouse
name: 'jitsu'
version: '0.1.1'
version: '0.1.2'
require-dbt-version: ">=0.20.0"
config-version: 2

Expand All @@ -22,7 +23,7 @@ clean-targets: # directories to be removed by `dbt clean`
# Configuring models
# Full documentation: https://docs.getdbt.com/docs/configuring-models
models:
materialized: "{{var('jitsu_model_materialized', 'incremental')}}"
materialized: "{{var('jitsu_model_materialized', 'table')}}"
pre-hook:
- "{{ onrunstart() }}" # On clickhouse enables allow_experimental_window_functions flag

Expand All @@ -33,7 +34,7 @@ dispatch:

vars:
# location of source data table
jitsu_events_table:
jitsu_events_table: "{{ source('jitsu', 'pageviews') }}"

# length of trailing window in minutes for session attribution.
# We recommend enabling builtin Retrospective User Recognition feature for user stitching
Expand All @@ -49,4 +50,6 @@ vars:
# define them here. Columns will be included in the `jitsu_sessions`
# model as `first_<column>` and `last_<column>`.
jitsu_pass_through_columns: []


jitsu_events_table_filter: "event_type='pageview'"
jitsu_model_materialized: 'table'
6 changes: 5 additions & 1 deletion models/sessions/jitsu_events.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{{ config(
materialized = 'view'
)}}
{% if var('jitsu_events_table_filter') is defined %}
select * from {{var('jitsu_events_table')}} where user_anonymous_id is not null and {{var('jitsu_events_table_filter')}}
{% else %}
select * from {{var('jitsu_events_table')}} where user_anonymous_id is not null
{% endif %}

select * from {{var('jitsu_events_table')}} where user_anonymous_id is not null
22 changes: 12 additions & 10 deletions models/sessions/jitsu_sessions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,26 @@ referrer_mapping as (

agg as (

select distinct
select

session_id,
user_anonymous_id,
last_value(coalesce(user_anonymous_id, user_internal_id, user_email)) over ({{window_clause}}) as blended_user_id,
min(_timestamp) over ( {{partition_by}} ) as session_start_timestamp,
max(_timestamp) over ( {{partition_by}} ) as session_end_timestamp,
count(*) over ( {{partition_by}} ) as pageviews,
coalesce(user_anonymous_id, user_id, user_email) as blended_user_id,
max(user_anonymous_id) as user_anonymous_id,
max(user_id) as user_id,
max(user_email) as user_email,
min(_timestamp) as session_start_timestamp,
max(_timestamp) session_end_timestamp,
count() as pageviews,

{% for (key, value) in first_values.items() %}
first_value({{key}}) over ({{window_clause}}) as {{value}},
min({{key}}) as {{value}},
{% endfor %}

{% for (key, value) in last_values.items() %}
last_value({{key}}) over ({{window_clause}}) as {{value}}{% if not loop.last %},{% endif %}
max({{key}}) as {{value}}{% if not loop.last %},{% endif %}
{% endfor %}

from pageviews_sessionized
from pageviews_sessionized GROUP BY session_id

),

Expand Down Expand Up @@ -104,7 +106,6 @@ tiers as (
from diffs

),

mapped as (

select
Expand All @@ -119,3 +120,4 @@ mapped as (
)

select * from mapped