From 2d5361fe0d6ff82e91e722b81a7b8f898d5053d5 Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Fri, 15 Nov 2024 10:11:29 -0800 Subject: [PATCH] Update tests to include more detail and use filters that work with source data --- .../simple_manifest/metrics.yaml | 22 ++++++++++++------- .../test_derived_metric_rendering.py | 9 ++++---- ...dow_metric_with_tiered_filters__plan0.sql} | 15 ++++++++----- ..._with_tiered_filters__plan0_optimized.sql} | 20 +++++++++-------- 4 files changed, 40 insertions(+), 26 deletions(-) rename tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/{test_nested_offset_metric_with_tiered_filters__plan0.sql => test_nested_offset_window_metric_with_tiered_filters__plan0.sql} (99%) rename tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/{test_nested_offset_metric_with_tiered_filters__plan0_optimized.sql => test_nested_offset_window_metric_with_tiered_filters__plan0_optimized.sql} (84%) diff --git a/metricflow-semantics/metricflow_semantics/test_helpers/semantic_manifest_yamls/simple_manifest/metrics.yaml b/metricflow-semantics/metricflow_semantics/test_helpers/semantic_manifest_yamls/simple_manifest/metrics.yaml index 5c115bd52..9d9b0594f 100644 --- a/metricflow-semantics/metricflow_semantics/test_helpers/semantic_manifest_yamls/simple_manifest/metrics.yaml +++ b/metricflow-semantics/metricflow_semantics/test_helpers/semantic_manifest_yamls/simple_manifest/metrics.yaml @@ -507,16 +507,22 @@ metric: alias: bookings_at_start_of_month --- metric: - name: bookings_offset_to_grain_twice - description: tests a nested offset_to_grain metric + name: bookings_offset_to_grain_twice_with_tiered_filters + description: tests a nested offset_to_grain metric with filters at each level type: derived type_params: - expr: bookings_qtd * 2 + expr: bookings_this_month_wtd - bookings metrics: + - name: bookings - name: bookings_growth_since_start_of_month - offset_to_grain: quarter - alias: bookings_qtd - filter: "{{ TimeDimension('metric_time', 'month') }} >= '2020-01-01'" + offset_to_grain: week + alias: bookings_this_month_wtd + filter: + - "{{ TimeDimension('booking__ds', 'year') }} >= '2019-01-01'" + - "{{ TimeDimension('listing__created_at', 'day') }} >= '2020-01-02'" + filter: + - "{{ TimeDimension('metric_time', 'month') }} >= '2020-01-01'" + - "{{ Dimension('booking__is_instant') }}" --- metric: name: "bookings_month_start_compared_to_1_month_prior" @@ -741,10 +747,10 @@ metric: - name: bookings_offset_once offset_window: 1 month filter: - - "{{ TimeDimension('metric_time', 'month') }} >= '2019-01-01'" + - "{{ TimeDimension('metric_time', 'month') }} >= '2019-12-01'" - "{{ Dimension('booking__is_instant') }}" filter: - - "{{ TimeDimension('metric_time', 'year') }} >= '2020-01-01'" + - "{{ TimeDimension('metric_time', 'year') }} >= '2019-01-01'" - "{{ Entity('listing') }} IS NOT NULL" --- metric: diff --git a/tests_metricflow/query_rendering/test_derived_metric_rendering.py b/tests_metricflow/query_rendering/test_derived_metric_rendering.py index 21129cfb7..690026e0e 100644 --- a/tests_metricflow/query_rendering/test_derived_metric_rendering.py +++ b/tests_metricflow/query_rendering/test_derived_metric_rendering.py @@ -824,7 +824,7 @@ def test_derived_metric_that_defines_the_same_alias_in_different_components( @pytest.mark.sql_engine_snapshot -def test_nested_offset_metric_with_tiered_filters( +def test_nested_offset_window_metric_with_tiered_filters( request: FixtureRequest, mf_test_configuration: MetricFlowTestConfiguration, dataflow_plan_builder: DataflowPlanBuilder, @@ -843,9 +843,9 @@ def test_nested_offset_metric_with_tiered_filters( group_by_names=("metric_time__day",), where_constraints=[ # `booking_ds` is the agg_time_dimension - PydanticWhereFilter(where_sql_template=("{{ TimeDimension('booking__ds', 'quarter') }} = '2021-01-01'")), + PydanticWhereFilter(where_sql_template=("{{ TimeDimension('booking__ds', 'quarter') }} >= '2020-01-01'")), PydanticWhereFilter( - where_sql_template=("{{ TimeDimension('listing__created_at', 'day') }} = '2021-01-01'") + where_sql_template=("{{ TimeDimension('listing__created_at', 'day') }} = '2020-01-01'") ), ], ).query_spec @@ -872,10 +872,11 @@ def test_nested_offset_to_grain_metric_with_tiered_filters( """Tests that filters at different tiers are applied appropriately for derived metrics with offset to grain.""" # TODO: test with Trino, hard-coded filters might fail query_spec = query_parser.parse_and_validate_query( - metric_names=("bookings_offset_to_grain_twice",), + metric_names=("bookings_offset_to_grain_twice_with_tiered_filters",), group_by_names=("metric_time__day",), where_constraints=[ PydanticWhereFilter(where_sql_template=("{{ Entity('listing') }} IS NOT NULL")), + PydanticWhereFilter(where_sql_template=("{{ TimeDimension('metric_time', 'quarter') }} >= '2020-01-01'")), ], ).query_spec diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_metric_with_tiered_filters__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_window_metric_with_tiered_filters__plan0.sql similarity index 99% rename from tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_metric_with_tiered_filters__plan0.sql rename to tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_window_metric_with_tiered_filters__plan0.sql index 95ef47c10..9e4b6d6f9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_metric_with_tiered_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_window_metric_with_tiered_filters__plan0.sql @@ -1,4 +1,4 @@ -test_name: test_nested_offset_metric_with_tiered_filters +test_name: test_nested_offset_window_metric_with_tiered_filters test_filename: test_derived_metric_rendering.py docstring: Tests that filters at different tiers are applied appropriately for derived metrics. @@ -23,11 +23,16 @@ FROM ( FROM ( -- Time Spine SELECT - subq_16.ds AS metric_time__day + DATE_TRUNC('quarter', subq_16.ds) AS booking__ds__quarter + , subq_16.ds AS metric_time__day , DATE_TRUNC('year', subq_16.ds) AS metric_time__year FROM ***************************.mf_time_spine subq_16 ) subq_17 - WHERE metric_time__year >= '2020-01-01' + WHERE ( + booking__ds__quarter >= '2020-01-01' + ) AND ( + metric_time__year >= '2019-01-01' + ) ) subq_15 INNER JOIN ( -- Compute Metrics via Expressions @@ -365,7 +370,7 @@ FROM ( , DATE_TRUNC('month', subq_3.ds) AS metric_time__month FROM ***************************.mf_time_spine subq_3 ) subq_4 - WHERE metric_time__month >= '2019-01-01' + WHERE metric_time__month >= '2019-12-01' ) subq_2 INNER JOIN ( -- Metric Time Dimension 'ds' @@ -706,7 +711,7 @@ FROM ( ON subq_5.listing = subq_8.listing ) subq_9 - WHERE (((booking__ds__quarter = '2021-01-01') AND (listing__created_at__day = '2021-01-01')) AND (listing IS NOT NULL)) AND (booking__is_instant) + WHERE ((listing__created_at__day = '2020-01-01') AND (listing IS NOT NULL)) AND (booking__is_instant) ) subq_10 ) subq_11 GROUP BY diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_metric_with_tiered_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_window_metric_with_tiered_filters__plan0_optimized.sql similarity index 84% rename from tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_metric_with_tiered_filters__plan0_optimized.sql rename to tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_window_metric_with_tiered_filters__plan0_optimized.sql index 84963d4ae..66660a539 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_metric_with_tiered_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offset_window_metric_with_tiered_filters__plan0_optimized.sql @@ -1,4 +1,4 @@ -test_name: test_nested_offset_metric_with_tiered_filters +test_name: test_nested_offset_window_metric_with_tiered_filters test_filename: test_derived_metric_rendering.py docstring: Tests that filters at different tiers are applied appropriately for derived metrics. @@ -23,11 +23,16 @@ FROM ( FROM ( -- Time Spine SELECT - ds AS metric_time__day + DATE_TRUNC('quarter', ds) AS booking__ds__quarter + , ds AS metric_time__day , DATE_TRUNC('year', ds) AS metric_time__year FROM ***************************.mf_time_spine subq_35 ) subq_36 - WHERE metric_time__year >= '2020-01-01' + WHERE ( + booking__ds__quarter >= '2020-01-01' + ) AND ( + metric_time__year >= '2019-01-01' + ) ) subq_34 INNER JOIN ( -- Compute Metrics via Expressions @@ -47,7 +52,6 @@ FROM ( SELECT DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day , subq_24.metric_time__day AS metric_time__day - , subq_24.booking__ds__quarter AS booking__ds__quarter , subq_24.listing AS listing , subq_24.booking__is_instant AS booking__is_instant , subq_24.bookings AS bookings @@ -55,7 +59,6 @@ FROM ( -- Join to Time Spine Dataset SELECT subq_21.metric_time__day AS metric_time__day - , subq_20.booking__ds__quarter AS booking__ds__quarter , subq_20.listing AS listing , subq_20.booking__is_instant AS booking__is_instant , subq_20.bookings AS bookings @@ -70,14 +73,13 @@ FROM ( , DATE_TRUNC('month', ds) AS metric_time__month FROM ***************************.mf_time_spine subq_22 ) subq_23 - WHERE metric_time__month >= '2019-01-01' + WHERE metric_time__month >= '2019-12-01' ) subq_21 INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('quarter', ds) AS booking__ds__quarter - , DATE_TRUNC('day', ds) AS metric_time__day + DATE_TRUNC('day', ds) AS metric_time__day , listing_id AS listing , is_instant AS booking__is_instant , 1 AS bookings @@ -91,7 +93,7 @@ FROM ( ON subq_24.listing = listings_latest_src_28000.listing_id ) subq_28 - WHERE (((booking__ds__quarter = '2021-01-01') AND (listing__created_at__day = '2021-01-01')) AND (listing IS NOT NULL)) AND (booking__is_instant) + WHERE ((listing__created_at__day = '2020-01-01') AND (listing IS NOT NULL)) AND (booking__is_instant) GROUP BY metric_time__day ) subq_32