Fix nested filter on self-referencing relationships #3038
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.
Why make this change?
Nested GraphQL filters on self-referencing relationships (e.g., parent/child hierarchies) return incorrect results. The filter fails to use the relationship name when looking up foreign key metadata, causing incorrect join predicates in the generated SQL.
What is this change?
Modified
HandleNestedFilterForSqlto useAddJoinPredicatesForRelationshipinstead ofAddJoinPredicatesForRelatedEntity. The key difference:Before:
After:
The
EntityRelationshipKeyincludes both entity name and relationship name, enabling correct foreign key lookup for self-referencing entities where multiple relationships exist (e.g.,parent_accountvschild_accountsonDimAccount).How was this tested?
Sample Request(s)
Query for accounts whose parent has
AccountKey = 1:{ dbo_DimAccounts(filter: { parent_account: { AccountKey: { eq: 1 }}}) { items { AccountKey ParentAccountKey } } }With test data:
Returns account 2 (the direct child of account 1).
Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug]: Nested filter on Self-Referencing Relationships returns incorrect results</issue_title>
<issue_description>### What happened?
Problem
When using GraphQL nested filters on self-referencing relationships (e.g., parent/child hierarchy), the filter returns incorrect results.
This query gives incorrect results
Expected Behavior
Returns book items with categories whose parent's name contains "Classic".
Proposed Solution
HandleNestedFilterForSql, useAddJoinPredicatesForRelationshipinstead ofAddJoinPredicatesForRelatedEntityEntityRelationshipKeyusing the relationship name (filter field name) to look up the correct FK definitionfkLookupKey:{queryStructure.EntityName, filterField.Name}targetEntityName: the nested filter entity namesubqueryTargetTableAlias: the EXISTS subquery's source aliasIn
BaseGraphQLFilterParsers.cs: