Skip to content

Commit

Permalink
Check query tests for conversion metrics with custom grain
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Oct 23, 2024
1 parent d0f32cc commit c93914d
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions tests_metricflow/integration/test_cases/itest_granularity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,158 @@ integration_test:
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts1 ON ts.ds = ts1.ds
WHERE {{ render_time_constraint("ts1.martian_day", "2020-01-01") }}
GROUP BY ts.ds
---
integration_test:
name: test_conversion_metric_with_custom_granularity
description: Test querying a simple metric with a custom grain on a multi-hop dimension
model: SIMPLE_MODEL
metrics: ["visit_buy_conversion_rate"]
group_bys: ["metric_time__martian_day"]
check_query: |
SELECT
metric_time__martian_day
, CAST(buys AS {{ double_data_type_name }}) / CAST(NULLIF(visits, 0) AS {{ double_data_type_name }}) AS visit_buy_conversion_rate
FROM (
SELECT
COALESCE(subq_5.metric_time__martian_day, subq_17.metric_time__martian_day) AS metric_time__martian_day
, MAX(subq_5.visits) AS visits
, MAX(subq_17.buys) AS buys
FROM (
SELECT
ts.martian_day AS metric_time__martian_day
, SUM(1) AS visits
FROM {{ source_schema }}.fct_visits v
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON {{ render_date_trunc("V.ds", TimeGranularity.DAY) }} = ts.ds
GROUP BY ts.martian_day
) subq_5
FULL OUTER JOIN (
SELECT
ts2.martian_day AS metric_time__martian_day
, SUM(subq_12.buys) AS buys
FROM (
SELECT DISTINCT
FIRST_VALUE(subq_8.visits) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS visits
, FIRST_VALUE(subq_8.ds__day) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS ds__day
, FIRST_VALUE(subq_8.metric_time__day) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS metric_time__day
, FIRST_VALUE(subq_8.user) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS user
, subq_11.mf_internal_uuid AS mf_internal_uuid
, subq_11.buys AS buys
FROM (
SELECT
{{ render_date_trunc("ds", TimeGranularity.DAY) }} AS ds__day
, {{ render_date_trunc("ds", TimeGranularity.DAY) }} AS metric_time__day
, user_id AS user
, 1 AS visits
FROM {{ source_schema }}.fct_visits v
) subq_8
INNER JOIN (
SELECT
{{ render_date_trunc("ds", TimeGranularity.DAY) }} AS ds__day
, user_id AS user
, 1 AS buys
, GEN_RANDOM_UUID() AS mf_internal_uuid
FROM {{ source_schema }}.fct_buys b
) subq_11
ON subq_8.user = subq_11.user AND subq_8.ds__day <= subq_11.ds__day
) subq_12
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts2 ON subq_12.metric_time__day = ts2.ds
GROUP BY ts2.martian_day
) subq_17
ON subq_5.metric_time__martian_day = subq_17.metric_time__martian_day
GROUP BY COALESCE(subq_5.metric_time__martian_day, subq_17.metric_time__martian_day)
) subq_18
---
integration_test:
name: test_windowed_conversion_metric_with_custom_granularity
description: Test querying a simple metric with a custom grain on a multi-hop dimension
model: SIMPLE_MODEL
metrics: ["visit_buy_conversion_rate_7days"]
group_bys: ["metric_time__martian_day"]
check_query: |
SELECT
metric_time__martian_day
, CAST(buys AS {{ double_data_type_name }}) / CAST(NULLIF(visits, 0) AS {{ double_data_type_name }}) AS visit_buy_conversion_rate_7days
FROM (
SELECT
COALESCE(subq_5.metric_time__martian_day, subq_17.metric_time__martian_day) AS metric_time__martian_day
, MAX(subq_5.visits) AS visits
, MAX(subq_17.buys) AS buys
FROM (
SELECT
ts.martian_day AS metric_time__martian_day
, SUM(1) AS visits
FROM {{ source_schema }}.fct_visits v
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON {{ render_date_trunc("V.ds", TimeGranularity.DAY) }} = ts.ds
GROUP BY ts.martian_day
) subq_5
FULL OUTER JOIN (
SELECT
ts2.martian_day AS metric_time__martian_day
, SUM(subq_12.buys) AS buys
FROM (
SELECT DISTINCT
FIRST_VALUE(subq_8.visits) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS visits
, FIRST_VALUE(subq_8.ds__day) OVER (
PARTITION BY
subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS ds__day
, FIRST_VALUE(subq_8.metric_time__day) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS metric_time__day
, FIRST_VALUE(subq_8.user) OVER (
PARTITION BY subq_11.user, subq_11.ds__day, subq_11.mf_internal_uuid
ORDER BY subq_8.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS user
, subq_11.mf_internal_uuid AS mf_internal_uuid
, subq_11.buys AS buys
FROM (
SELECT
{{ render_date_trunc("ds", TimeGranularity.DAY) }} AS ds__day
, {{ render_date_trunc("ds", TimeGranularity.DAY) }} AS metric_time__day
, user_id AS user
, 1 AS visits
FROM {{ source_schema }}.fct_visits v
) subq_8
INNER JOIN (
SELECT
{{ render_date_trunc("ds", TimeGranularity.DAY) }} AS ds__day
, user_id AS user
, 1 AS buys
, GEN_RANDOM_UUID() AS mf_internal_uuid
FROM {{ source_schema }}.fct_buys b
) subq_11
ON subq_8.user = subq_11.user AND subq_8.ds__day <= subq_11.ds__day
AND subq_8.ds__day > {{ render_date_sub("subq_11", "ds__day", 7, TimeGranularity.DAY) }}
) subq_12
LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts2
ON subq_12.metric_time__day = ts2.ds
GROUP BY ts2.martian_day
) subq_17
ON subq_5.metric_time__martian_day = subq_17.metric_time__martian_day
GROUP BY COALESCE(subq_5.metric_time__martian_day, subq_17.metric_time__martian_day)
) subq_18

0 comments on commit c93914d

Please sign in to comment.