Skip to content

Commit

Permalink
Add test to demonstrate bug
Browse files Browse the repository at this point in the history
Here you can see that in the input metric subquery, the specs for the metric and query-level filters are included during aggregation when they should not be.
  • Loading branch information
courtneyholcomb committed Nov 14, 2024
1 parent df48dd8 commit 9b3d1c0
Show file tree
Hide file tree
Showing 4 changed files with 806 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ metric:
alias: bookings_2_weeks_ago
---
metric:
name: "bookings_offset_once"
name: bookings_offset_once
description: bookings metric offset once.
type: derived
type_params:
Expand All @@ -710,7 +710,23 @@ metric:
offset_window: 5 days
---
metric:
name: "bookings_offset_twice"
name: bookings_offset_with_tiered_filters
description: bookings metric offset once.
type: derived
type_params:
expr: bookings
metrics:
- name: bookings
offset_window: 2 days
filter:
- "{{ TimeDimension('metric_time', 'month') }} >= '2019-01-01'"
- "{{ Dimension('booking__is_instant') }}"
filter:
- "{{ TimeDimension('metric_time', 'year') }} >= '2020-01-01'"
- "{{ Entity('listing') }} IS NOT NULL"
---
metric:
name: bookings_offset_twice
description: bookings metric offset twice.
type: derived
type_params:
Expand Down
37 changes: 37 additions & 0 deletions tests_metricflow/query_rendering/test_derived_metric_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,3 +821,40 @@ def test_derived_metric_that_defines_the_same_alias_in_different_components(
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_offset_metric_with_tiered_filters(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
query_parser: MetricFlowQueryParser,
sql_client: SqlClient,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
) -> None:
"""Tests that filters at different tiers are applied appropriately for derived metrics.
This includes filters at the input metric, metric, and query level. At each tier there are filters on both
metric_time / agg time and another dimension, which might have different behaviors.
"""
# TODO: test with Trino, hard-coded filters might fail
query_spec = query_parser.parse_and_validate_query(
metric_names=("bookings_offset_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('listing__created_at', 'day') }} = '2021-01-01'")
),
],
).query_spec

render_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)
Loading

0 comments on commit 9b3d1c0

Please sign in to comment.