-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update aggregated conversion node to only have queried linkable specs #1381
Open
WilliamDee
wants to merge
4
commits into
main
Choose a base branch
from
will/fix-extra-linkable-specs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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_constraint="{{ 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_constraint="{{ 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_constraint="{{ 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the things I love most about GitHub is how they don't allow me to comment on lines that haven't been modified. Seriously, have these people ever even reviewed code before?
Anyway, this does not work because of line 269 above. The queried_linkable_specs do not include any filter specs. So if you don't include required_local_linkable_specs you miss out on propagating any specs requested as filter elements that originate in the base measure dataset.
That's why the missing spec assertion is tripping in that conversion metric test. When we try to run a query that filters on visit__referrer_id, which is local to the base measure, that only gets added to the set of required local specs generated on line 269.
I do not know how to fix this while also fixing the other issue you're trying to resolve here. I don't even know if that's possible without a major overhaul to how we deal with filters. That said, it might be worth postponing a fix for this, since it's not clear to me what our filter behaviors against these nodes are meant to be. There are some fuzzy edge cases around applying a filter to two parts of a conversion calculation, and my sense is we do not correctly differentiate between input-measure-level filters and query-level filters, so whatever we do is going to produce unexpected results for somebody.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the issue here was that the final aggregated conversion node had the extraneous group bys, could we just pass the final aggregated node through a
FilterElementNode
instead of fixing it like this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe? To be honest, I don't even know what's supposed to happen here, much less whether or not removing that column from the base output will solve this problem in a reasonable way.
The basic question here is how should query-time filters get applied? If I run a query for conversions computed as visits / buys, and I filter to something like
WHERE visit__referrer_id = 1234 OR buy__coupon_id = 1234
in order to get all conversions from partner ID 1234, what is supposed to happen? If I add a filter to a conversion measure input to excludebuyer__is_campaign_sourced
for ourorganic_reach_conversion
metric, what should happen? What should happen if I apply the query-time filter for partner 1234 with the organic_reach_conversion metric?I don't think we ever answered these questions, and while some of them are pretty obvious (the organice_reach_conversion filter should be properly applied) the others are less obvious to me, and I think there are a few where it really is just a product decision around how we want this to behave and what kind of traade-offs our users will have in the configuration experience.