-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #871 from dbt-labs/fix-incorrect-cumulative-metric…
…-output
- Loading branch information
Showing
46 changed files
with
1,201 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: Fix error in cumulative metric output when start-time and end-time are specified | ||
time: 2023-11-14T17:11:37.462241-08:00 | ||
custom: | ||
Author: tlento | ||
Issue: "869" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
metricflow/test/integration/query_output/test_cumulative_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
|
||
import pytest | ||
from _pytest.fixtures import FixtureRequest | ||
|
||
from metricflow.engine.metricflow_engine import MetricFlowQueryRequest | ||
from metricflow.protocols.sql_client import SqlClient | ||
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState | ||
from metricflow.test.integration.conftest import IntegrationTestHelpers | ||
from metricflow.test.snapshot_utils import assert_object_snapshot_equal | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_simple_cumulative_metric( | ||
request: FixtureRequest, | ||
mf_test_session_state: MetricFlowTestSessionState, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Tests a query of a cumulative metric with a monthly window and a time constraint adjustment.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=["trailing_2_months_revenue"], | ||
group_by_names=["metric_time"], | ||
order_by_names=["metric_time"], | ||
time_constraint_start=datetime.datetime(2020, 2, 1), | ||
time_constraint_end=datetime.datetime(2020, 4, 30), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_object_snapshot_equal( | ||
request=request, | ||
mf_test_session_state=mf_test_session_state, | ||
obj_id="query_output", | ||
obj=query_result.result_df.to_string(), | ||
sql_client=sql_client, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_multiple_cumulative_metrics( | ||
request: FixtureRequest, | ||
mf_test_session_state: MetricFlowTestSessionState, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Tests a query with multiple cumulative metrics to ensure date selections align.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=["revenue_all_time", "trailing_2_months_revenue"], | ||
group_by_names=["metric_time"], | ||
order_by_names=["metric_time"], | ||
time_constraint_start=datetime.datetime(2020, 3, 31), | ||
time_constraint_end=datetime.datetime(2020, 5, 31), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_object_snapshot_equal( | ||
request=request, | ||
mf_test_session_state=mf_test_session_state, | ||
obj_id="query_output", | ||
obj=query_result.result_df.to_string(), | ||
sql_client=sql_client, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_non_additive_cumulative_metric( | ||
request: FixtureRequest, | ||
mf_test_session_state: MetricFlowTestSessionState, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Tests a query with a non-additive cumulative metric to ensure the non-additive constraint is applied.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=["every_two_days_bookers"], | ||
group_by_names=["metric_time"], | ||
order_by_names=["metric_time"], | ||
time_constraint_start=datetime.datetime(2019, 12, 31), | ||
time_constraint_end=datetime.datetime(2020, 1, 3), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_object_snapshot_equal( | ||
request=request, | ||
mf_test_session_state=mf_test_session_state, | ||
obj_id="query_output", | ||
obj=query_result.result_df.to_string(), | ||
sql_client=sql_client, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_grain_to_date_cumulative_metric( | ||
request: FixtureRequest, | ||
mf_test_session_state: MetricFlowTestSessionState, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Tests a month to date cumulative metric with a constraint to ensure all necessary input data is included.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=["revenue_mtd"], | ||
group_by_names=["metric_time"], | ||
order_by_names=["metric_time"], | ||
time_constraint_start=datetime.datetime(2021, 1, 3), | ||
time_constraint_end=datetime.datetime(2021, 1, 6), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_object_snapshot_equal( | ||
request=request, | ||
mf_test_session_state=mf_test_session_state, | ||
obj_id="query_output", | ||
obj=query_result.result_df.to_string(), | ||
sql_client=sql_client, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_cumulative_metric_with_non_adjustable_filter( | ||
request: FixtureRequest, | ||
mf_test_session_state: MetricFlowTestSessionState, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Tests a cumulative metric with a filter that cannot be adjusted to ensure all data is included.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=["trailing_2_months_revenue"], | ||
group_by_names=["metric_time"], | ||
order_by_names=["metric_time"], | ||
where_constraint=( | ||
"{{ TimeDimension('metric_time', 'day') }} = '2020-03-15' or " | ||
"{{ TimeDimension('metric_time', 'day') }} = '2020-04-30'" | ||
), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_object_snapshot_equal( | ||
request=request, | ||
mf_test_session_state=mf_test_session_state, | ||
obj_id="query_output", | ||
obj=query_result.result_df.to_string(), | ||
sql_client=sql_client, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.