Skip to content

Commit

Permalink
Add test for derived metric offsets with time filters
Browse files Browse the repository at this point in the history
Derived metric offsets handle time filters correctly provided
the query depends on the `metric_time` dimension. Since the
DataflowPlanBuilder has an invariant in its runtime to ensure that
metric_time is included in the requested query specs, this is
always true in practice. Additional tests for different query
patterns may be warranted when we expand support.

For the time being, we simply add an illustrative query rendering
test for the behavior when filter spans are at odds with the
offset window selection.
  • Loading branch information
tlento committed Nov 8, 2023
1 parent dcccab2 commit 5e6ea1f
Show file tree
Hide file tree
Showing 13 changed files with 3,800 additions and 0 deletions.
38 changes: 38 additions & 0 deletions metricflow/test/query_rendering/test_derived_metric_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

import pytest
from _pytest.fixtures import FixtureRequest
from dbt_semantic_interfaces.implementations.filters.where_filter import PydanticWhereFilter

from metricflow.dataflow.builder.dataflow_plan_builder import DataflowPlanBuilder
from metricflow.plan_conversion.dataflow_to_sql import DataflowToSqlQueryPlanConverter
from metricflow.protocols.sql_client import SqlClient
from metricflow.specs.column_assoc import ColumnAssociationResolver
from metricflow.specs.specs import (
MetricFlowQuerySpec,
MetricSpec,
)
from metricflow.specs.where_filter_transform import WhereSpecFactory
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_DAY, MTD_SPEC_QUARTER, MTD_SPEC_WEEK, MTD_SPEC_YEAR
Expand Down Expand Up @@ -89,6 +92,41 @@ def test_derived_metric_with_offset_window( # noqa: D
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric_with_offset_window_and_time_filter( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
column_association_resolver: ColumnAssociationResolver,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
dataflow_plan = dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="bookings_growth_2_weeks"),),
time_dimension_specs=(MTD_SPEC_DAY,),
where_constraint=WhereSpecFactory(
column_association_resolver=column_association_resolver,
).create_from_where_filter(
PydanticWhereFilter(
where_sql_template=(
"{{ TimeDimension('metric_time', 'day') }} = '2020-01-01' "
"or {{ TimeDimension('metric_time', 'day') }} = '2020-01-14'"
)
),
),
)
)

convert_and_check(
request=request,
mf_test_session_state=mf_test_session_state,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric_with_offset_to_grain( # noqa: D
request: FixtureRequest,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
-- Compute Metrics via Expressions
SELECT
metric_time__day
, bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks
FROM (
-- Combine Metrics
SELECT
COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day
, MAX(subq_21.bookings) AS bookings
, MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago
FROM (
-- Constrain Output with WHERE
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements:
-- ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC(ds, day) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10001
) subq_18
WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14'
GROUP BY
metric_time__day
) subq_21
FULL OUTER JOIN (
-- Constrain Output with WHERE
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings_2_weeks_ago
FROM (
-- Join to Time Spine Dataset
-- Pass Only Elements:
-- ['bookings', 'metric_time__day']
SELECT
subq_25.ds AS metric_time__day
, subq_23.bookings AS bookings
FROM ***************************.mf_time_spine subq_25
INNER JOIN (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
SELECT
DATE_TRUNC(ds, day) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10001
) subq_23
ON
DATE_SUB(CAST(subq_25.ds AS DATETIME), INTERVAL 14 day) = subq_23.metric_time__day
) subq_27
WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14'
GROUP BY
metric_time__day
) subq_30
ON
subq_21.metric_time__day = subq_30.metric_time__day
GROUP BY
metric_time__day
) subq_31
Loading

0 comments on commit 5e6ea1f

Please sign in to comment.