Skip to content

Commit

Permalink
Add check query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Dec 19, 2024
1 parent e2c1f0e commit 948ebe6
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions tests_metricflow/integration/test_cases/itest_granularity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 948ebe6

Please sign in to comment.