Skip to content
Source
Sign inSign up
TanStack/db

fix: correlated subquery resolves empty when correlation targets a joined alias #1705

ClosedMohith26(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1705/headopened

Showing 3 changed files with 170 additions and 1 deletion.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/plenty-hoops-shake.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/db': patch
+---
+​
+Fix: `materialize()` correlated subquery silently resolves to an empty array when the correlation predicate references a joined alias instead of the subquery's own `from` alias. The parent-key filter is now deferred until after joins when the correlation field lives on a joined source.
64 additions and 1 deletionpackages/db/src/query/compiler/index.tsModified
View file
@@ -243,7 +243,20 @@
// The inner join happens BEFORE namespace wrapping / WHERE / SELECT / ORDER BY,
// so the child pipeline only processes rows that match parents.
let pipeline: NamespacedAndKeyedStream = initialPipeline
- if (!isUnionFrom && parentKeyStream && childCorrelationField) {
+ // When the correlation field references a joined alias (not the main from
+ // alias), the correlation value isn't available on the raw input rows, so
+ // the parent-key filter must be deferred until after joins are processed.
+ const correlationOnJoinedAlias: boolean =
+ !isUnionFrom &&
+ !!parentKeyStream &&
+ !!childCorrelationField &&
+ childCorrelationField.path[0] !== mainSource
+ if (
+ !isUnionFrom &&
+ parentKeyStream &&
+ childCorrelationField &&