-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
370f444
commit d0f32cc
Showing
12 changed files
with
3,210 additions
and
0 deletions.
There are no files selected for viewing
412 changes: 412 additions & 0 deletions
412
...larity.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_custom_granularity__plan0.sql
Large diffs are not rendered by default.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
...SqlQueryPlan/BigQuery/test_conversion_metric_with_custom_granularity__plan0_optimized.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
-- Compute Metrics via Expressions | ||
SELECT | ||
metric_time__martian_day | ||
, CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days | ||
FROM ( | ||
-- Combine Aggregated Outputs | ||
SELECT | ||
COALESCE(subq_24.metric_time__martian_day, subq_36.metric_time__martian_day) AS metric_time__martian_day | ||
, MAX(subq_24.visits) AS visits | ||
, MAX(subq_36.buys) AS buys | ||
FROM ( | ||
-- Pass Only Elements: ['visits', 'metric_time__day'] | ||
-- Join to Custom Granularity Dataset | ||
-- Pass Only Elements: ['visits', 'metric_time__martian_day'] | ||
-- Aggregate Measures | ||
SELECT | ||
subq_21.martian_day AS metric_time__martian_day | ||
, SUM(subq_20.visits) AS visits | ||
FROM ( | ||
-- Read Elements From Semantic Model 'visits_source' | ||
-- Metric Time Dimension 'ds' | ||
SELECT | ||
DATETIME_TRUNC(ds, day) AS metric_time__day | ||
, 1 AS visits | ||
FROM ***************************.fct_visits visits_source_src_28000 | ||
) subq_20 | ||
LEFT OUTER JOIN | ||
***************************.mf_time_spine subq_21 | ||
ON | ||
subq_20.metric_time__day = subq_21.ds | ||
GROUP BY | ||
metric_time__martian_day | ||
) subq_24 | ||
FULL OUTER JOIN ( | ||
-- Pass Only Elements: ['buys', 'metric_time__day'] | ||
-- Join to Custom Granularity Dataset | ||
-- Pass Only Elements: ['buys', 'metric_time__martian_day'] | ||
-- Aggregate Measures | ||
SELECT | ||
subq_33.martian_day AS metric_time__martian_day | ||
, SUM(subq_31.buys) AS buys | ||
FROM ( | ||
-- Dedupe the fanout with mf_internal_uuid in the conversion data set | ||
SELECT DISTINCT | ||
FIRST_VALUE(subq_27.visits) OVER ( | ||
PARTITION BY | ||
subq_30.user | ||
, subq_30.ds__day | ||
, subq_30.mf_internal_uuid | ||
ORDER BY subq_27.ds__day DESC | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | ||
) AS visits | ||
, FIRST_VALUE(subq_27.ds__day) OVER ( | ||
PARTITION BY | ||
subq_30.user | ||
, subq_30.ds__day | ||
, subq_30.mf_internal_uuid | ||
ORDER BY subq_27.ds__day DESC | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | ||
) AS ds__day | ||
, FIRST_VALUE(subq_27.metric_time__day) OVER ( | ||
PARTITION BY | ||
subq_30.user | ||
, subq_30.ds__day | ||
, subq_30.mf_internal_uuid | ||
ORDER BY subq_27.ds__day DESC | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | ||
) AS metric_time__day | ||
, FIRST_VALUE(subq_27.user) OVER ( | ||
PARTITION BY | ||
subq_30.user | ||
, subq_30.ds__day | ||
, subq_30.mf_internal_uuid | ||
ORDER BY subq_27.ds__day DESC | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | ||
) AS user | ||
, subq_30.mf_internal_uuid AS mf_internal_uuid | ||
, subq_30.buys AS buys | ||
FROM ( | ||
-- Read Elements From Semantic Model 'visits_source' | ||
-- Metric Time Dimension 'ds' | ||
-- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] | ||
SELECT | ||
DATETIME_TRUNC(ds, day) AS ds__day | ||
, DATETIME_TRUNC(ds, day) AS metric_time__day | ||
, user_id AS user | ||
, 1 AS visits | ||
FROM ***************************.fct_visits visits_source_src_28000 | ||
) subq_27 | ||
INNER JOIN ( | ||
-- Read Elements From Semantic Model 'buys_source' | ||
-- Metric Time Dimension 'ds' | ||
-- Add column with generated UUID | ||
SELECT | ||
DATETIME_TRUNC(ds, day) AS ds__day | ||
, user_id AS user | ||
, 1 AS buys | ||
, GENERATE_UUID() AS mf_internal_uuid | ||
FROM ***************************.fct_buys buys_source_src_28000 | ||
) subq_30 | ||
ON | ||
( | ||
subq_27.user = subq_30.user | ||
) AND ( | ||
( | ||
subq_27.ds__day <= subq_30.ds__day | ||
) AND ( | ||
subq_27.ds__day > DATE_SUB(CAST(subq_30.ds__day AS DATETIME), INTERVAL 7 day) | ||
) | ||
) | ||
) subq_31 | ||
LEFT OUTER JOIN | ||
***************************.mf_time_spine subq_33 | ||
ON | ||
subq_31.metric_time__day = subq_33.ds | ||
GROUP BY | ||
metric_time__martian_day | ||
) subq_36 | ||
ON | ||
subq_24.metric_time__martian_day = subq_36.metric_time__martian_day | ||
GROUP BY | ||
metric_time__martian_day | ||
) subq_37 |
Oops, something went wrong.