-
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.
Update aggregated conversion node to only have queried linkable specs (…
…#1381) ## Context When you try to query a conversion metric by supplying a filter with a dimension that exists in the base measure's semantic model without providing it in the group by, the rendering fails. This is because with the logic in the conversion metric builder, it ended up "requiring" all the linkable specs from the query (group by + filter), so the group by provided in the filter gets added to the resulting `SELECT` even though it was not supplied in the group by. This caused an error with the join as it didn't expect that additional element. This PR updates it so that instead of using all the required linkable specs, we should only use the queried linkable specs when building out the conversion aggregate node. Resolves #1210 Resolves SL-2777
- Loading branch information
1 parent
85dac0f
commit 8c2e064
Showing
161 changed files
with
23,482 additions
and
13,558 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: Fixes bug where conversion metric query fails when filter with base semantic model's dimension is provided | ||
time: 2024-08-23T12:31:08.257686-04:00 | ||
custom: | ||
Author: WilliamDee | ||
Issue: "1210" |
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
178 changes: 178 additions & 0 deletions
178
tests_metricflow/integration/query_output/test_conversion_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,178 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
|
||
import pytest | ||
from _pytest.fixtures import FixtureRequest | ||
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration | ||
|
||
from metricflow.engine.metricflow_engine import MetricFlowQueryRequest | ||
from metricflow.protocols.sql_client import SqlClient | ||
from tests_metricflow.integration.conftest import IntegrationTestHelpers | ||
from tests_metricflow.snapshot_utils import assert_str_snapshot_equal | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_conversion_metric( | ||
request: FixtureRequest, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Test query against a conversion metric.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=("visit_buy_conversion_rate",), | ||
group_by_names=("metric_time",), | ||
order_by_names=("metric_time",), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="query_output", | ||
snapshot_str=query_result.result_df.text_format(), | ||
sql_engine=sql_client.sql_engine_type, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_conversion_metric_with_window( | ||
request: FixtureRequest, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Test query against a conversion metric with a window.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=("visit_buy_conversion_rate_7days",), | ||
group_by_names=("metric_time",), | ||
order_by_names=("metric_time",), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="query_output", | ||
snapshot_str=query_result.result_df.text_format(), | ||
sql_engine=sql_client.sql_engine_type, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_conversion_metric_with_categorical_filter( | ||
request: FixtureRequest, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Test query against a conversion metric with a categorical filter.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=("visit_buy_conversion_rate",), | ||
group_by_names=("metric_time", "visit__referrer_id"), | ||
order_by_names=("metric_time", "visit__referrer_id"), | ||
where_constraints=("{{ Dimension('visit__referrer_id') }} = 'fb_ad_1'",), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="query_output", | ||
snapshot_str=query_result.result_df.text_format(), | ||
sql_engine=sql_client.sql_engine_type, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_conversion_metric_with_time_constraint( | ||
request: FixtureRequest, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Test query against a conversion metric with a time constraint and categorical filter.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=("visit_buy_conversion_rate",), | ||
group_by_names=("visit__referrer_id",), | ||
order_by_names=("visit__referrer_id",), | ||
where_constraints=("{{ Dimension('visit__referrer_id') }} = 'fb_ad_1'",), | ||
time_constraint_start=datetime.datetime(2020, 1, 1), | ||
time_constraint_end=datetime.datetime(2020, 1, 2), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="query_output", | ||
snapshot_str=query_result.result_df.text_format(), | ||
sql_engine=sql_client.sql_engine_type, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_conversion_metric_with_window_and_time_constraint( | ||
request: FixtureRequest, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Test query against a conversion metric with a window, time constraint, and categorical filter.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=("visit_buy_conversion_rate_7days",), | ||
group_by_names=( | ||
"metric_time", | ||
"visit__referrer_id", | ||
), | ||
order_by_names=("metric_time", "visit__referrer_id"), | ||
where_constraints=("{{ Dimension('visit__referrer_id') }} = 'fb_ad_1'",), | ||
time_constraint_start=datetime.datetime(2020, 1, 1), | ||
time_constraint_end=datetime.datetime(2020, 1, 2), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="query_output", | ||
snapshot_str=query_result.result_df.text_format(), | ||
sql_engine=sql_client.sql_engine_type, | ||
) | ||
|
||
|
||
@pytest.mark.sql_engine_snapshot | ||
def test_conversion_metric_with_filter_not_in_group_by( | ||
request: FixtureRequest, | ||
mf_test_configuration: MetricFlowTestConfiguration, | ||
sql_client: SqlClient, | ||
it_helpers: IntegrationTestHelpers, | ||
) -> None: | ||
"""Test query against a conversion metric with a filter that doesn't exist in group by.""" | ||
query_result = it_helpers.mf_engine.query( | ||
MetricFlowQueryRequest.create_with_random_request_id( | ||
metric_names=("visit_buy_conversions",), | ||
time_constraint_start=datetime.datetime(2020, 1, 1), | ||
time_constraint_end=datetime.datetime(2020, 1, 1), | ||
) | ||
) | ||
assert query_result.result_df is not None, "Unexpected empty result." | ||
|
||
assert_str_snapshot_equal( | ||
request=request, | ||
mf_test_configuration=mf_test_configuration, | ||
snapshot_id="query_output", | ||
snapshot_str=query_result.result_df.text_format(), | ||
sql_engine=sql_client.sql_engine_type, | ||
) |
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.