Skip to content

Commit

Permalink
Add test case for cumulative metric with non-day granularity (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb authored Jan 24, 2024
1 parent f7b3c74 commit 2486b4b
Show file tree
Hide file tree
Showing 17 changed files with 1,279 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
metric:
name: trailing_3_months_bookings
description: Prior 3 months' bookings. Tests a cumulative metric with non-day granularity.
type: cumulative
type_params:
measure: bookings_monthly
window: 3 month
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,40 @@ integration_test:
OR {{ render_time_constraint("a.ds", "2020-01-04", "2020-01-04") }}
GROUP BY a.ds
ORDER BY a.ds
---
integration_test:
name: cumulative_metric_month_granularity
description: Query a cumulative metric that uses non-day granularity.
model: EXTENDED_DATE_MODEL
metrics: ["trailing_3_months_bookings"]
group_bys: ["metric_time__month"]
order_bys: ["metric_time__month"]
time_constraint: ["2020-03-05", "2021-01-04"]
check_query: |
SELECT
subq_3.metric_time__month
, SUM(subq_2.bookings_monthly) AS trailing_3_months_bookings
FROM (
SELECT
{{ render_date_trunc("ds", TimeGranularity.MONTH) }} AS metric_time__month
FROM {{ source_schema }}.mf_time_spine
WHERE {{ render_time_constraint("ds", "2020-03-01", "2021-01-31") }}
GROUP BY
{{ render_date_trunc("ds", TimeGranularity.MONTH) }}
) subq_3
INNER JOIN (
SELECT
{{ render_date_trunc("ds", TimeGranularity.MONTH) }} AS metric_time__month
, bookings_monthly
FROM {{ source_schema }}.fct_bookings_extended_monthly
WHERE {{ render_time_constraint(render_date_trunc("ds", TimeGranularity.MONTH), "2019-12-01", "2021-01-31") }}
) subq_2
ON (
subq_2.metric_time__month <= subq_3.metric_time__month
) AND (
subq_2.metric_time__month > {{ render_date_sub("subq_3", "metric_time__month", 3, TimeGranularity.MONTH) }}
)
WHERE {{ render_time_constraint("subq_3.metric_time__month", "2020-03-01", "2021-01-31") }}
GROUP BY
subq_3.metric_time__month
ORDER BY subq_3.metric_time__month
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from metricflow.test.fixtures.model_fixtures import ConsistentIdObjectRepository
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState
from metricflow.test.query_rendering.compare_rendered_query import convert_and_check
from metricflow.test.time.metric_time_dimension import MTD_SPEC_MONTH


@pytest.mark.sql_engine_snapshot
Expand Down Expand Up @@ -264,3 +265,33 @@ def test_cumulative_metric_grain_to_date(
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_month(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
extended_date_dataflow_plan_builder: DataflowPlanBuilder,
extended_date_dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
consistent_id_object_repository: ConsistentIdObjectRepository,
sql_client: SqlClient,
) -> None:
"""Tests rendering a query for a cumulative metric based on a monthly time dimension."""
dataflow_plan = extended_date_dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="trailing_3_months_bookings"),),
dimension_specs=(),
time_dimension_specs=(MTD_SPEC_MONTH,),
time_range_constraint=TimeRangeConstraint(
start_time=as_datetime("2020-03-05"), end_time=as_datetime("2021-01-04")
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=extended_date_dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
-- Compute Metrics via Expressions
SELECT
subq_8.metric_time__month
, subq_8.bookings_monthly AS trailing_3_months_bookings
FROM (
-- Aggregate Measures
SELECT
subq_7.metric_time__month
, SUM(subq_7.bookings_monthly) AS bookings_monthly
FROM (
-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00]
SELECT
subq_6.metric_time__month
, subq_6.bookings_monthly
FROM (
-- Pass Only Elements: ['bookings_monthly', 'metric_time__month']
SELECT
subq_5.metric_time__month
, subq_5.bookings_monthly
FROM (
-- Join Self Over Time Range
SELECT
subq_3.metric_time__month AS metric_time__month
, subq_2.monthly_ds__month AS monthly_ds__month
, subq_2.monthly_ds__quarter AS monthly_ds__quarter
, subq_2.monthly_ds__year AS monthly_ds__year
, subq_2.monthly_ds__extract_year AS monthly_ds__extract_year
, subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter
, subq_2.monthly_ds__extract_month AS monthly_ds__extract_month
, subq_2.booking__monthly_ds__month AS booking__monthly_ds__month
, subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter
, subq_2.booking__monthly_ds__year AS booking__monthly_ds__year
, subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year
, subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter
, subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month
, subq_2.metric_time__quarter AS metric_time__quarter
, subq_2.metric_time__year AS metric_time__year
, subq_2.metric_time__extract_year AS metric_time__extract_year
, subq_2.metric_time__extract_quarter AS metric_time__extract_quarter
, subq_2.metric_time__extract_month AS metric_time__extract_month
, subq_2.listing AS listing
, subq_2.booking__listing AS booking__listing
, subq_2.bookings_monthly AS bookings_monthly
FROM (
-- Time Spine
SELECT
DATE_TRUNC(subq_4.ds, month) AS metric_time__month
FROM ***************************.mf_time_spine subq_4
WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04'
GROUP BY
metric_time__month
) subq_3
INNER JOIN (
-- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00]
SELECT
subq_1.monthly_ds__month
, subq_1.monthly_ds__quarter
, subq_1.monthly_ds__year
, subq_1.monthly_ds__extract_year
, subq_1.monthly_ds__extract_quarter
, subq_1.monthly_ds__extract_month
, subq_1.booking__monthly_ds__month
, subq_1.booking__monthly_ds__quarter
, subq_1.booking__monthly_ds__year
, subq_1.booking__monthly_ds__extract_year
, subq_1.booking__monthly_ds__extract_quarter
, subq_1.booking__monthly_ds__extract_month
, subq_1.metric_time__month
, subq_1.metric_time__quarter
, subq_1.metric_time__year
, subq_1.metric_time__extract_year
, subq_1.metric_time__extract_quarter
, subq_1.metric_time__extract_month
, subq_1.listing
, subq_1.booking__listing
, subq_1.bookings_monthly
FROM (
-- Metric Time Dimension 'monthly_ds'
SELECT
subq_0.monthly_ds__month
, subq_0.monthly_ds__quarter
, subq_0.monthly_ds__year
, subq_0.monthly_ds__extract_year
, subq_0.monthly_ds__extract_quarter
, subq_0.monthly_ds__extract_month
, subq_0.booking__monthly_ds__month
, subq_0.booking__monthly_ds__quarter
, subq_0.booking__monthly_ds__year
, subq_0.booking__monthly_ds__extract_year
, subq_0.booking__monthly_ds__extract_quarter
, subq_0.booking__monthly_ds__extract_month
, subq_0.monthly_ds__month AS metric_time__month
, subq_0.monthly_ds__quarter AS metric_time__quarter
, subq_0.monthly_ds__year AS metric_time__year
, subq_0.monthly_ds__extract_year AS metric_time__extract_year
, subq_0.monthly_ds__extract_quarter AS metric_time__extract_quarter
, subq_0.monthly_ds__extract_month AS metric_time__extract_month
, subq_0.listing
, subq_0.booking__listing
, subq_0.bookings_monthly
FROM (
-- Read Elements From Semantic Model 'bookings_monthly_source'
SELECT
bookings_monthly_source_src_10026.bookings_monthly
, DATE_TRUNC(bookings_monthly_source_src_10026.ds, month) AS monthly_ds__month
, DATE_TRUNC(bookings_monthly_source_src_10026.ds, quarter) AS monthly_ds__quarter
, DATE_TRUNC(bookings_monthly_source_src_10026.ds, year) AS monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month
, DATE_TRUNC(bookings_monthly_source_src_10026.ds, month) AS booking__monthly_ds__month
, DATE_TRUNC(bookings_monthly_source_src_10026.ds, quarter) AS booking__monthly_ds__quarter
, DATE_TRUNC(bookings_monthly_source_src_10026.ds, year) AS booking__monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month
, bookings_monthly_source_src_10026.listing_id AS listing
, bookings_monthly_source_src_10026.listing_id AS booking__listing
FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026
) subq_0
) subq_1
WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04'
) subq_2
ON
(
subq_2.metric_time__month <= subq_3.metric_time__month
) AND (
subq_2.metric_time__month > DATE_SUB(CAST(subq_3.metric_time__month AS DATETIME), INTERVAL 3 month)
)
) subq_5
) subq_6
WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04'
) subq_7
GROUP BY
metric_time__month
) subq_8
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Join Self Over Time Range
-- Pass Only Elements: ['bookings_monthly', 'metric_time__month']
-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00]
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_12.metric_time__month AS metric_time__month
, SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings
FROM (
-- Time Spine
SELECT
DATE_TRUNC(ds, month) AS metric_time__month
FROM ***************************.mf_time_spine subq_13
WHERE ds BETWEEN '2020-03-05' AND '2021-01-04'
GROUP BY
metric_time__month
) subq_12
INNER JOIN (
-- Read Elements From Semantic Model 'bookings_monthly_source'
-- Metric Time Dimension 'monthly_ds'
-- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00]
SELECT
DATE_TRUNC(ds, month) AS metric_time__month
, bookings_monthly
FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026
WHERE DATE_TRUNC(ds, month) BETWEEN '2019-12-05' AND '2021-01-04'
) subq_11
ON
(
subq_11.metric_time__month <= subq_12.metric_time__month
) AND (
subq_11.metric_time__month > DATE_SUB(CAST(subq_12.metric_time__month AS DATETIME), INTERVAL 3 month)
)
WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04'
GROUP BY
metric_time__month
Loading

0 comments on commit 2486b4b

Please sign in to comment.