Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from joshtemple/fix/support-bq
Browse files Browse the repository at this point in the history
Fix: Support BigQuery by adding cross-db utils
  • Loading branch information
clrcrl authored Feb 5, 2019
2 parents 4ac41bf + e217a01 commit 9dd189f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 41 deletions.
15 changes: 7 additions & 8 deletions macros/cross-adapter-modeling/base/segment_web_page_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ renamed as (
search as page_url_query,

referrer,
ltrim({{ dbt_utils.get_url_host('referrer') }}, 'www.')::varchar
as referrer_host,

ltrim({{ dbt_utils.get_url_host('referrer') }}, 'www.') as referrer_host,

context_campaign_source as utm_source,
context_campaign_medium as utm_medium,
context_campaign_name as utm_campaign,
Expand All @@ -44,10 +43,10 @@ renamed as (
context_ip as ip,
context_user_agent as user_agent,
case
when context_user_agent ilike '%Android%' then 'Android'
when lower(context_user_agent) like '%android%' then 'Android'
else replace(
split_part(split_part(context_user_agent,'(', 2), ' ', 1),
';','')
{{ dbt_utils.split_part(dbt_utils.split_part('context_user_agent', "'('", 2), "' '", 1) }},
';', '')
end as device

from source
Expand All @@ -63,12 +62,12 @@ final as (
when device = 'Android' then 'Android'
when device in ('iPad', 'iPod') then 'Tablet'
when device in ('Windows', 'Macintosh', 'X11') then 'Desktop'
else 'uncategorized'
else 'Uncategorized'
end as device_category
from renamed

)

select * from final

{% endmacro %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ with pageviews as (
where anonymous_id in (
select distinct anonymous_id
from {{ref('segment_web_page_views')}}
where tstamp >= (select dateadd(hour, -{{var('segment_sessionization_trailing_window')}}, max(tstamp)) from {{this}})
where tstamp >= (
select {{
dbt_utils.safe_cast(
dbt_utils.dateadd(
'hour',
-var('segment_sessionization_trailing_window'),
'max(tstamp)'),
'timestamp') }}
from {{ this }})
)
{% endif %}
Expand Down Expand Up @@ -82,7 +90,7 @@ diffed as (

select
*,
datediff(s, previous_tstamp, tstamp) as period_of_inactivity
{{ dbt_utils.datediff('previous_tstamp', 'tstamp', 'second') }} as period_of_inactivity
from lagged

),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ with sessions as (

{% if is_incremental() %}
where session_start_tstamp > (
select
dateadd(
hour,
-{{var('segment_sessionization_trailing_window')}},
max(session_start_tstamp)
)
select {{
dbt_utils.safe_cast(
dbt_utils.dateadd(
'hour',
-var('segment_sessionization_trailing_window'),
'max(session_start_tstamp)'),
'timestamp') }}
from {{this}})
{% endif %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
{% set partition_by = "partition by session_id" %}

{% set window_clause = "
partition by session_id
order by page_view_number
partition by session_id
order by page_view_number
rows between unbounded preceding and unbounded following
" %}

{% set first_values = {
'utm_source' : 'utm_source',
'utm_content' : 'utm_content',
Expand All @@ -38,7 +38,7 @@
'device' : 'device',
'device_category' : 'device_category'
} %}

{% set last_values = {
'page_url' : 'last_page_url',
'page_url_host' : 'last_page_url_host',
Expand All @@ -51,7 +51,15 @@ with pageviews as (
select * from {{ref('segment_web_page_views__sessionized')}}

{% if is_incremental() %}
where tstamp > (select dateadd(hour, -{{var('segment_sessionization_trailing_window')}}, max(session_start_tstamp)) from {{this}})
where tstamp > (
select {{
dbt_utils.safe_cast(
dbt_utils.dateadd(
'hour',
-var('segment_sessionization_trailing_window'),
'max(session_start_tstamp)'),
'timestamp') }}
from {{ this }})
{% endif %}

),
Expand All @@ -65,49 +73,49 @@ agg as (
min(tstamp) over ( {{partition_by}} ) as session_start_tstamp,
max(tstamp) over ( {{partition_by}} ) as session_end_tstamp,
count(*) over ( {{partition_by}} ) as page_views,

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

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

from pageviews

),

diffs as (

select

*,
datediff(s, session_start_tstamp, session_end_tstamp) as duration_in_s

{{ dbt_utils.datediff('session_start_tstamp', 'session_end_tstamp', 'second') }} as duration_in_s

from agg

),

tiers as (

select

*,

case
when duration_in_s between 0 and 9 then '0s to 9s'
when duration_in_s between 10 and 29 then '10s to 29s'
when duration_in_s between 30 and 59 then '30s to 59s'
when duration_in_s > 59 then '60s or more'
else null
end as duration_in_s_tier

from diffs

)

select * from tiers

{% endmacro %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ with sessions as (
select * from {{ref('segment_web_sessions__initial')}}

{% if is_incremental() %}
where session_start_tstamp > (select dateadd(hour, -{{var('segment_sessionization_trailing_window')}}, max(session_start_tstamp)) from {{this}})
where session_start_tstamp > (
select {{
dbt_utils.safe_cast(
dbt_utils.dateadd(
'hour',
-var('segment_sessionization_trailing_window'),
'max(session_start_tstamp)'),
'timestamp') }}
from {{ this }})
{% endif %}

),
Expand All @@ -32,18 +40,18 @@ id_stitching as (

joined as (

select
select

sessions.*,
coalesce(id_stitching.user_id, sessions.anonymous_id)

coalesce(id_stitching.user_id, sessions.anonymous_id)
as blended_user_id

from sessions
left join id_stitching using (anonymous_id)

)

select * from joined

{% endmacro %}
{% endmacro %}

0 comments on commit 9dd189f

Please sign in to comment.