Skip to content
Source
Sign inSign up
TanStack/db

fix(query): lazy-join loads through the collection the join key resolves to #1614

Mergedkevin-dp(GitHub profile, opens in a new tab)merged 5 commits intomain fromrefs/pull/1614/headmerged by kevin-dp(GitHub profile, opens in a new tab)

Showing 4 changed files with 24 additions and 5 deletions (loading 1).

x
0 of 4 viewed, 4 left
7 additions and 0 deletions.changeset/lazy-join-resolved-collection.mdAdded
View file
@@ -0,0 +1,7 @@
+---
+'@tanstack/db': patch
+---
+​
+fix(query): drive lazy-join loading through the collection the join key resolves to
+​
+When a subquery used in a JOIN clause selects its join key from a _joined_ source rather than from its own `from` clause, the lazy-join loader subscribed to the wrong inner source: it used the subquery's `from` alias while computing the index requirement against the collection the key actually resolves to. This produced a misleading `Join requires an index` warning naming an already-indexed collection and an unnecessary full-load fallback. `followRef` now reports the resolved source alias, so lazy loading subscribes to the correct collection and loads through its index.
7 additions and 1 deletionpackages/db/src/query/compiler/lazy-targets.tsModified
View file
@@ -49,9 +49,15 @@
return []
​
+ // The subscription we drive lazy loading through must be the one for the
+ // collection the join key actually resolves to. When the key traces through a
+ // subquery's select into a *joined* source, that collection differs from the
+ // subquery's from clause (which is what `aliasRemapping[lazyAlias]` yields),
+ // so prefer the alias reported by `followRef`. Fall back to the from-clause
+ // remapping when the key resolves directly to the from source.
return [
- alias: aliasRemapping[lazyAlias] || lazyAlias,
+ alias: followRefResult.alias || aliasRemapping[lazyAlias] || lazyAlias,
collection: followRefResult.collection,