From 6ab9be45d47b518d6fdca7bc37c8a780c1c5fe6f Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Fri, 20 Sep 2024 14:49:10 -0700 Subject: [PATCH] Add check query tests for custom granularity --- .../test_cases/itest_granularity.yaml | 91 ++++++++++++++++++- 1 file changed, 87 insertions(+), 4 deletions(-) diff --git a/tests_metricflow/integration/test_cases/itest_granularity.yaml b/tests_metricflow/integration/test_cases/itest_granularity.yaml index dafddc3fb7..80b985f8e2 100644 --- a/tests_metricflow/integration/test_cases/itest_granularity.yaml +++ b/tests_metricflow/integration/test_cases/itest_granularity.yaml @@ -360,7 +360,27 @@ integration_test: metrics: [ "trailing_2_months_revenue"] group_bys: ["metric_time__martian_day"] check_query: | - SELECT 1 + SELECT + metric_time__martian_day + , trailing_2_months_revenue + FROM ( + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + SELECT + subq_5.martian_day AS metric_time__martian_day + , ts.ds AS metric_time__day + , SUM(r.revenue) AS txn_revenue + FROM {{ source_schema }}.mf_time_spine ts + INNER JOIN {{ source_schema }}.fct_revenue r + ON (DATE_TRUNC('day', r.created_at) <= ts.ds) AND (DATE_TRUNC('day', r.created_at) > ts.ds - INTERVAL 2 month) + LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_5 + ON ts.ds = subq_5.ds + GROUP BY subq_5.martian_day, ts.ds + ) ts + ) subq_9 + GROUP BY metric_time__martian_day, trailing_2_months_revenue --- integration_test: name: derived_offset_metric_with_custom_granularity @@ -369,13 +389,76 @@ integration_test: metrics: [ "booking_fees_last_week_per_booker_this_week"] group_bys: ["metric_time__martian_day"] check_query: | - SELECT 1 + SELECT + metric_time__martian_day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + SELECT + COALESCE(subq_8.metric_time__martian_day, subq_14.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_8.booking_value) AS booking_value + , MAX(subq_14.bookers) AS bookers + FROM ( + SELECT + subq_5.martian_day AS metric_time__martian_day + , SUM(subq_4.booking_value) AS booking_value + FROM ( + SELECT + ts.ds AS metric_time__day + , b.booking_value AS booking_value + FROM {{ source_schema }}.mf_time_spine ts + INNER JOIN {{ source_schema }}.fct_bookings b + ON ts.ds - INTERVAL 1 week = DATE_TRUNC('day', b.ds) + ) subq_4 + LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_5 + ON subq_4.metric_time__day = subq_5.ds + GROUP BY subq_5.martian_day + ) subq_8 + FULL OUTER JOIN ( + SELECT + subq_11.martian_day AS metric_time__martian_day + , COUNT(DISTINCT b.guest_id) AS bookers + FROM {{ source_schema }}.fct_bookings b + LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_11 + ON DATE_TRUNC('day', b.ds) = subq_11.ds + GROUP BY subq_11.martian_day + ) subq_14 + ON subq_8.metric_time__martian_day = subq_14.metric_time__martian_day + GROUP BY COALESCE(subq_8.metric_time__martian_day, subq_14.metric_time__martian_day) + ) subq_15 --- integration_test: name: multiple_metrics_with_custom_granularity description: A derived metric queried with a custom granularity model: SIMPLE_MODEL metrics: [ "bookings", "listings"] - group_bys: ["metric_time__martian_day", "listing__ds_month"] + group_bys: ["metric_time__martian_day", "listing__ds__month"] check_query: | - SELECT 1 + SELECT + COALESCE(subq_10.metric_time__martian_day, subq_16.metric_time__martian_day) AS metric_time__martian_day + , COALESCE(subq_10.listing__ds__month, subq_16.listing__ds__month) AS listing__ds__month + , MAX(subq_10.bookings) AS bookings + , MAX(subq_16.listings) AS listings + FROM ( + SELECT + ts.martian_day AS metric_time__martian_day + , DATE_TRUNC('month', l.created_at) AS listing__ds__month + , SUM(1) AS bookings + FROM {{ source_schema }}.fct_bookings b + LEFT OUTER JOIN {{ source_schema }}.dim_listings_latest l ON b.listing_id = l.listing_id + LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON DATE_TRUNC('day', b.ds) = ts.ds + GROUP BY ts.martian_day, DATE_TRUNC('month', l.created_at) + ) subq_10 + FULL OUTER JOIN ( + SELECT + ts.martian_day AS metric_time__martian_day + , DATE_TRUNC('month', l.created_at) AS listing__ds__month + , SUM(1) AS listings + FROM {{ source_schema }}.dim_listings_latest l + LEFT OUTER JOIN {{ source_schema }}.mf_time_spine ts ON DATE_TRUNC('day', l.created_at) = ts.ds + GROUP BY ts.martian_day, DATE_TRUNC('month', l.created_at) + ) subq_16 + ON (subq_10.metric_time__martian_day = subq_16.metric_time__martian_day) + AND (subq_10.listing__ds__month = subq_16.listing__ds__month) + GROUP BY + COALESCE(subq_10.metric_time__martian_day, subq_16.metric_time__martian_day) + , COALESCE(subq_10.listing__ds__month, subq_16.listing__ds__month)