diff --git a/tests_metricflow/integration/test_cases/itest_granularity.yaml b/tests_metricflow/integration/test_cases/itest_granularity.yaml index b83cbeb03..baf6f7f25 100644 --- a/tests_metricflow/integration/test_cases/itest_granularity.yaml +++ b/tests_metricflow/integration/test_cases/itest_granularity.yaml @@ -961,3 +961,164 @@ integration_test: GROUP BY subq_2.martian_day ) subq_5 ON subq_6.metric_time__martian_day = subq_5.metric_time__martian_day +--- +integration_test: + name: custom_offset_window + description: Test querying a metric with a custom offset window + model: SIMPLE_MODEL + metrics: ["bookings_offset_one_martian_day"] + group_bys: ["metric_time__day"] + check_query: | + WITH cte AS ( + SELECT + martian_day AS ds__martian_day + , FIRST_VALUE(ds) OVER ( + PARTITION BY martian_day + ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value + , LAST_VALUE(ds) OVER ( + PARTITION BY martian_day + ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value + , ROW_NUMBER() OVER ( + PARTITION BY martian_day + ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day__row_number + FROM {{ source_schema }}.mf_time_spine ts + ) + + SELECT + subq_10.metric_time__day + , SUM(1) AS bookings_offset_one_martian_day + FROM ( + SELECT + CASE + WHEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day <= subq_7.ds__martian_day__last_value__offset + THEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day + ELSE subq_7.ds__martian_day__last_value__offset + END AS metric_time__day + FROM cte + INNER JOIN ( + SELECT + ds__martian_day + , LAG(ds__martian_day__first_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value__offset + , LAG(ds__martian_day__last_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value__offset + FROM ( + SELECT + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + FROM cte + GROUP BY + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + ) subq_5 + ) subq_7 + ON cte.ds__martian_day = subq_7.ds__martian_day + ) subq_10 + INNER JOIN {{ source_schema }}.fct_bookings b ON subq_10.metric_time__day = {{ render_date_trunc("b.ds", TimeGranularity.DAY) }} + GROUP BY subq_10.metric_time__day +--- +integration_test: + name: custom_offset_window_with_grain_and_date_part + description: Test querying a metric with a custom offset window + model: SIMPLE_MODEL + metrics: ["bookings_offset_one_martian_day"] + group_by_objs: [{"name": "booking__ds", "grain": "week"}, {"name": "metric_time", "date_part": "month"}, {"name": "booking__ds", "grain": "martian_day"}] + check_query: | + WITH cte AS ( + SELECT + martian_day AS ds__martian_day + , FIRST_VALUE(ds) OVER ( + PARTITION BY martian_day + ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value + , LAST_VALUE(ds) OVER ( + PARTITION BY martian_day + ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value + , ROW_NUMBER() OVER ( + PARTITION BY martian_day + ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day__row_number + FROM {{ source_schema }}.mf_time_spine ts + ) + + SELECT + subq_11.martian_day AS booking__ds__martian_day + , subq_10.booking__ds__week + , subq_10.metric_time__extract_month + , SUM(1) AS bookings_offset_one_martian_day + FROM ( + SELECT + CASE + WHEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day <= subq_7.ds__martian_day__last_value__offset + THEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day + ELSE subq_7.ds__martian_day__last_value__offset + END AS metric_time__day + , CASE + WHEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day <= subq_7.ds__martian_day__last_value__offset + THEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day + ELSE subq_7.ds__martian_day__last_value__offset + END AS booking__ds__day + , {{ render_date_trunc( + """CASE + WHEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day <= subq_7.ds__martian_day__last_value__offset + THEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day + ELSE subq_7.ds__martian_day__last_value__offset + END""" + , TimeGranularity.WEEK + ) }} AS booking__ds__week + , {{ render_extract( + """CASE + WHEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day <= subq_7.ds__martian_day__last_value__offset + THEN subq_7.ds__martian_day__first_value__offset + INTERVAL (cte.ds__day__row_number - 1) day + ELSE subq_7.ds__martian_day__last_value__offset + END""" + , DatePart.MONTH + ) }} AS metric_time__extract_month + FROM cte + INNER JOIN ( + SELECT + ds__martian_day + , LAG(ds__martian_day__first_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value__offset + , LAG(ds__martian_day__last_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value__offset + FROM ( + SELECT + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + FROM cte + GROUP BY + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + ) subq_5 + ) subq_7 + ON cte.ds__martian_day = subq_7.ds__martian_day + ) subq_10 + INNER JOIN {{ source_schema }}.fct_bookings b ON subq_10.metric_time__day = {{ render_date_trunc("b.ds", TimeGranularity.DAY) }} + LEFT OUTER JOIN {{ source_schema }}.mf_time_spine subq_11 ON subq_10.booking__ds__day = subq_11.ds + GROUP BY + subq_11.martian_day + , subq_10.booking__ds__week + , subq_10.metric_time__extract_month