Fix ambiguous entity path resolution for PRIMARY/FOREIGN entity relationships #1797
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.
[ai-assisted]
see: #1780
Fix MetricFlow Issue #1780: Enable PRIMARY/FOREIGN Entity Path Resolution
Description
This PR fixes Issue #1780 where MetricFlow fails to resolve queries that use the same dimension accessed through different entity paths when one metric uses a PRIMARY entity and another uses a FOREIGN entity.
Problem
Previously, MetricFlow would fail with "the given input does not match to a common item" when querying:
This occurred because:
job_count_primarymetric (using PRIMARY entity) accesses the dimension asbusinessunit__businessunit_namematerial_costsmetric (using FOREIGN entity) accesses it asjob__businessunit__businessunit_nameSolution
The fix implements semantic equivalence recognition between PRIMARY and FOREIGN entity paths while preserving the user's requested dimension naming in output columns. This is achieved through three key changes:
Enhanced Pattern Matching (
entity_link_pattern.py):job__businessunit__businessunit_nameto matchbusinessunit__businessunit_nameSemantic Equivalence Recognition (
linkable_element_set.py):_path_keys_are_semantically_equivalentto recognize equivalence when paths differ by exactly one entity linkintersection_by_path_keyto properly group semantically equivalent elements during intersection operationsOutput Column Name Preservation (
query_resolver.py):_resolve_group_by_item_inputto preserve the original requested entity linksjob__businessunit__businessunit_name)Technical Details
Conservative Approach: The suffix matching only activates when there's exactly a 1-level difference in entity links, preventing overly broad matching that could break existing behavior.
Time Dimension Handling: Fixed
_match_time_granularitiesto properly pass through non-time dimension specs, resolving issues with the new matching logic.Test Impact:
test_non_resolvable_ambiguous_entity_path_due_to_mismatchwhich now succeeds (positive change)Testing
Breaking Changes
No breaking changes. Queries that previously failed due to PRIMARY/FOREIGN entity path differences will now succeed. All existing working queries continue to work as before.
Example
Before this fix:
After this fix:
Files Changed
metricflow-semantics/metricflow_semantics/specs/patterns/entity_link_pattern.py_match_entity_linksto add PRIMARY/FOREIGN suffix matching for dimensions_match_time_granularitiesto properly handle non-time dimension specsmetricflow-semantics/metricflow_semantics/model/semantics/linkable_element_set.py_path_keys_are_semantically_equivalentfor recognizing equivalent pathsintersection_by_path_keyto group semantically equivalent elementsmetricflow-semantics/metricflow_semantics/query/query_resolver.pyTest updates:
test_ambiguous_entity_path.pyto reflect more flexible behaviortest_primary_foreign_suffix_matching.pyfor new functionalityFixes #1780