Skip to content

Commit

Permalink
Add test for time offset metric based on non-default granularity (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb authored Nov 13, 2023
1 parent 87b5b63 commit 45a970d
Show file tree
Hide file tree
Showing 17 changed files with 833 additions and 3 deletions.
4 changes: 2 additions & 2 deletions metricflow/plan_conversion/dataflow_to_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,8 +1043,8 @@ def visit_metric_time_dimension_transform_node(self, node: MetricTimeDimensionTr
semantic_model_reference=measure_instance.origin_semantic_model_reference.semantic_model_reference
)
assert semantic_model is not None, (
f"{measure_instance} was defined from {measure_instance.origin_semantic_model_reference}, but that"
f"can't be found"
f"{measure_instance} was defined from "
f"{measure_instance.origin_semantic_model_reference.semantic_model_reference}, but that can't be found"
)
aggregation_time_dimension_for_measure = semantic_model.checked_agg_time_dimension_for_measure(
measure_reference=measure_instance.spec.as_reference
Expand Down
12 changes: 12 additions & 0 deletions metricflow/test/fixtures/dataflow_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def dataflow_plan_builder( # noqa: D
)


@pytest.fixture
def extended_date_dataflow_plan_builder( # noqa: D
extended_date_semantic_manifest_lookup: SemanticManifestLookup,
consistent_id_object_repository: ConsistentIdObjectRepository,
) -> DataflowPlanBuilder:
return DataflowPlanBuilder(
source_nodes=consistent_id_object_repository.extended_date_model_source_nodes,
read_nodes=list(consistent_id_object_repository.extended_date_model_read_nodes.values()),
semantic_manifest_lookup=extended_date_semantic_manifest_lookup,
)


@pytest.fixture
def multihop_dataflow_plan_builder( # noqa: D
multi_hop_join_semantic_manifest_lookup: SemanticManifestLookup,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
metric:
name: bookings_last_month
description: bookings the prior month, based on dimension with month granularity
type: derived
type_params:
expr: bookings_last_month
metrics:
- name: bookings_monthly
offset_window: 1 month
alias: bookings_last_month
10 changes: 10 additions & 0 deletions metricflow/test/fixtures/sql_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ def dataflow_to_sql_converter( # noqa: D
column_association_resolver=DunderColumnAssociationResolver(simple_semantic_manifest_lookup),
semantic_manifest_lookup=simple_semantic_manifest_lookup,
)


@pytest.fixture(scope="session")
def extended_date_dataflow_to_sql_converter( # noqa: D
extended_date_semantic_manifest_lookup: SemanticManifestLookup,
) -> DataflowToSqlQueryPlanConverter:
return DataflowToSqlQueryPlanConverter(
column_association_resolver=DunderColumnAssociationResolver(extended_date_semantic_manifest_lookup),
semantic_manifest_lookup=extended_date_semantic_manifest_lookup,
)
32 changes: 31 additions & 1 deletion metricflow/test/query_rendering/test_derived_metric_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
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
from metricflow.test.time.metric_time_dimension import (
MTD_SPEC_DAY,
MTD_SPEC_MONTH,
MTD_SPEC_QUARTER,
MTD_SPEC_WEEK,
MTD_SPEC_YEAR,
)


@pytest.mark.sql_engine_snapshot
Expand Down Expand Up @@ -223,6 +229,30 @@ def test_derived_metric_with_offset_window_and_granularity( # noqa: D
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric_with_month_dimension_and_offset_window( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
extended_date_dataflow_plan_builder: DataflowPlanBuilder,
extended_date_dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
dataflow_plan = extended_date_dataflow_plan_builder.build_plan(
MetricFlowQuerySpec(
metric_specs=(MetricSpec(element_name="bookings_last_month"),),
time_dimension_specs=(MTD_SPEC_MONTH,),
)
)

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,
)


@pytest.mark.sql_engine_snapshot
def test_derived_metric_with_offset_to_grain_and_granularity( # noqa: D
request: FixtureRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
-- Compute Metrics via Expressions
SELECT
subq_7.metric_time__month
, bookings_last_month AS bookings_last_month
FROM (
-- Compute Metrics via Expressions
SELECT
subq_6.metric_time__month
, subq_6.bookings_monthly AS bookings_last_month
FROM (
-- Aggregate Measures
SELECT
subq_5.metric_time__month
, SUM(subq_5.bookings_monthly) AS bookings_monthly
FROM (
-- Pass Only Elements:
-- ['bookings_monthly', 'metric_time__month']
SELECT
subq_4.metric_time__month
, subq_4.bookings_monthly
FROM (
-- Join to Time Spine Dataset
SELECT
subq_2.metric_time__month AS metric_time__month
, subq_1.monthly_ds__month AS monthly_ds__month
, subq_1.monthly_ds__quarter AS monthly_ds__quarter
, subq_1.monthly_ds__year AS monthly_ds__year
, subq_1.monthly_ds__extract_year AS monthly_ds__extract_year
, subq_1.monthly_ds__extract_quarter AS monthly_ds__extract_quarter
, subq_1.monthly_ds__extract_month AS monthly_ds__extract_month
, subq_1.booking__monthly_ds__month AS booking__monthly_ds__month
, subq_1.booking__monthly_ds__quarter AS booking__monthly_ds__quarter
, subq_1.booking__monthly_ds__year AS booking__monthly_ds__year
, subq_1.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year
, subq_1.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter
, subq_1.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month
, subq_1.listing AS listing
, subq_1.booking__listing AS booking__listing
, subq_1.bookings_monthly AS bookings_monthly
FROM (
-- Date Spine
SELECT
DATE_TRUNC(subq_3.ds, month) AS metric_time__month
FROM ***************************.mf_time_spine subq_3
GROUP BY
metric_time__month
) subq_2
INNER JOIN (
-- 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_10024.bookings_monthly
, DATE_TRUNC(bookings_monthly_source_src_10024.ds, month) AS monthly_ds__month
, DATE_TRUNC(bookings_monthly_source_src_10024.ds, quarter) AS monthly_ds__quarter
, DATE_TRUNC(bookings_monthly_source_src_10024.ds, year) AS monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_10024.ds) AS monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_10024.ds) AS monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_10024.ds) AS monthly_ds__extract_month
, DATE_TRUNC(bookings_monthly_source_src_10024.ds, month) AS booking__monthly_ds__month
, DATE_TRUNC(bookings_monthly_source_src_10024.ds, quarter) AS booking__monthly_ds__quarter
, DATE_TRUNC(bookings_monthly_source_src_10024.ds, year) AS booking__monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__extract_month
, bookings_monthly_source_src_10024.listing_id AS listing
, bookings_monthly_source_src_10024.listing_id AS booking__listing
FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10024
) subq_0
) subq_1
ON
DATE_SUB(CAST(subq_2.metric_time__month AS DATETIME), INTERVAL 1 month) = subq_1.metric_time__month
) subq_4
) subq_5
GROUP BY
metric_time__month
) subq_6
) subq_7
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Compute Metrics via Expressions
SELECT
metric_time__month
, bookings_last_month AS bookings_last_month
FROM (
-- Join to Time Spine Dataset
-- Pass Only Elements:
-- ['bookings_monthly', 'metric_time__month']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_10.metric_time__month AS metric_time__month
, SUM(bookings_monthly_source_src_10024.bookings_monthly) AS bookings_last_month
FROM (
-- Date Spine
SELECT
DATE_TRUNC(ds, month) AS metric_time__month
FROM ***************************.mf_time_spine subq_11
GROUP BY
metric_time__month
) subq_10
INNER JOIN
***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10024
ON
DATE_SUB(CAST(subq_10.metric_time__month AS DATETIME), INTERVAL 1 month) = DATE_TRUNC(bookings_monthly_source_src_10024.ds, month)
GROUP BY
metric_time__month
) subq_15
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
-- Compute Metrics via Expressions
SELECT
subq_7.metric_time__month
, bookings_last_month AS bookings_last_month
FROM (
-- Compute Metrics via Expressions
SELECT
subq_6.metric_time__month
, subq_6.bookings_monthly AS bookings_last_month
FROM (
-- Aggregate Measures
SELECT
subq_5.metric_time__month
, SUM(subq_5.bookings_monthly) AS bookings_monthly
FROM (
-- Pass Only Elements:
-- ['bookings_monthly', 'metric_time__month']
SELECT
subq_4.metric_time__month
, subq_4.bookings_monthly
FROM (
-- Join to Time Spine Dataset
SELECT
subq_2.metric_time__month AS metric_time__month
, subq_1.monthly_ds__month AS monthly_ds__month
, subq_1.monthly_ds__quarter AS monthly_ds__quarter
, subq_1.monthly_ds__year AS monthly_ds__year
, subq_1.monthly_ds__extract_year AS monthly_ds__extract_year
, subq_1.monthly_ds__extract_quarter AS monthly_ds__extract_quarter
, subq_1.monthly_ds__extract_month AS monthly_ds__extract_month
, subq_1.booking__monthly_ds__month AS booking__monthly_ds__month
, subq_1.booking__monthly_ds__quarter AS booking__monthly_ds__quarter
, subq_1.booking__monthly_ds__year AS booking__monthly_ds__year
, subq_1.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year
, subq_1.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter
, subq_1.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month
, subq_1.listing AS listing
, subq_1.booking__listing AS booking__listing
, subq_1.bookings_monthly AS bookings_monthly
FROM (
-- Date Spine
SELECT
DATE_TRUNC('month', subq_3.ds) AS metric_time__month
FROM ***************************.mf_time_spine subq_3
GROUP BY
DATE_TRUNC('month', subq_3.ds)
) subq_2
INNER JOIN (
-- 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_10024.bookings_monthly
, DATE_TRUNC('month', bookings_monthly_source_src_10024.ds) AS monthly_ds__month
, DATE_TRUNC('quarter', bookings_monthly_source_src_10024.ds) AS monthly_ds__quarter
, DATE_TRUNC('year', bookings_monthly_source_src_10024.ds) AS monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_10024.ds) AS monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_10024.ds) AS monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_10024.ds) AS monthly_ds__extract_month
, DATE_TRUNC('month', bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__month
, DATE_TRUNC('quarter', bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__quarter
, DATE_TRUNC('year', bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_10024.ds) AS booking__monthly_ds__extract_month
, bookings_monthly_source_src_10024.listing_id AS listing
, bookings_monthly_source_src_10024.listing_id AS booking__listing
FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10024
) subq_0
) subq_1
ON
DATEADD(month, -1, subq_2.metric_time__month) = subq_1.metric_time__month
) subq_4
) subq_5
GROUP BY
subq_5.metric_time__month
) subq_6
) subq_7
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Compute Metrics via Expressions
SELECT
metric_time__month
, bookings_last_month AS bookings_last_month
FROM (
-- Join to Time Spine Dataset
-- Pass Only Elements:
-- ['bookings_monthly', 'metric_time__month']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_10.metric_time__month AS metric_time__month
, SUM(bookings_monthly_source_src_10024.bookings_monthly) AS bookings_last_month
FROM (
-- Date Spine
SELECT
DATE_TRUNC('month', ds) AS metric_time__month
FROM ***************************.mf_time_spine subq_11
GROUP BY
DATE_TRUNC('month', ds)
) subq_10
INNER JOIN
***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10024
ON
DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', bookings_monthly_source_src_10024.ds)
GROUP BY
subq_10.metric_time__month
) subq_15
Loading

0 comments on commit 45a970d

Please sign in to comment.