fix: Preserve relationship fields on queries that filter via JOIN or subquery#2149
Merged
Merged
Conversation
…subquery Signed-off-by: Marvin Froeder <marvin@datasqrl.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2149 +/- ##
============================================
- Coverage 14.67% 14.67% -0.01%
Complexity 909 909
============================================
Files 607 607
Lines 17515 17516 +1
Branches 2125 2125
============================================
Hits 2571 2571
- Misses 14702 14703 +1
Partials 242 242 ☔ View full report in Codecov by Harness. |
ferenc-csaky
approved these changes
Jun 16, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #2148.
A nested relationship field (e.g.
Thing.detail) was only carried onto a query when that query was a clean passthrough (SELECT * FROM Table WHERE <flat predicate>). As soon as the body used a JOIN or subquery, the compiler synthesized a new result type named after the query containing only the projected scalar columns and silently dropped every relationship/nested field.Root cause
In
SQRLLogicalPlanAnalyzer, base-table detection — which decides whether a query reuses the source table's GraphQL type (keeping its relationship fields) or synthesizes a fresh scalar-only type — only inspected the last source table viaIterables.getLast(sourceTables).For a clean passthrough the only source is the base table, so it matched. But with a JOIN/subquery filter, the right-most source is the filter table (e.g.
Tombstone), whose row type doesn't match the projected result — so no base table was found and the relationship fields were dropped.Fix
Scan the source tables in reverse and pick the right-most one whose row type matches the result, instead of checking only the last one. This matches the existing code comment ("the right-most table in the relational tree that has the same type as the result"), so a JOIN/subquery used purely as a filter now correctly resolves back to the projected base table and preserves its relationship fields.
Tests
relationship-filterreproducing the issue (clean / subquery / anti-join queries over aThing.detailrelationship). Its snapshot confirms all three now return[Thing!]withdetailretained.DAGPlannerTest/subqueryTestsnapshot, which was itself capturing the buggy behavior:AdminOrderCustomers(SELECT c.* FROM Customer ... NOT EXISTS subquery) andCustomerWithOrders(... WHERE EXISTS(...)) now correctly reuse theCustomertype instead of generating duplicate types.Verification:
UseCaseCompileTest(61),DAGPlannerTest(90), and allsqrl-plannerunit tests (257) pass. Across all those snapshots onlysubqueryTestchanged — a small, intended blast radius.