fix: deduplicate and filter null join keys in lazy join subset queries#1448
Merged
KyleAMathews merged 5 commits intomainfrom Apr 3, 2026
Merged
fix: deduplicate and filter null join keys in lazy join subset queries#1448KyleAMathews merged 5 commits intomainfrom
KyleAMathews merged 5 commits intomainfrom
Conversation
…ests When a lazy join collected foreign keys for subset queries, duplicate IDs and null values were passed through to the ANY() SQL param, producing bloated queries. Filter nulls, deduplicate via Set, and skip the request entirely when no valid keys remain. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Both branches added new test describe blocks at the end of live-query-collection.test.ts — kept both. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Contributor
|
Size Change: +40 B (+0.04%) Total Size: 113 kB 📦 View Changed
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 4.24 kB ℹ️ View Unchanged
|
Assert capturedOptions is empty rather than checking for absence of inArray expressions. This catches regressions where the fallback path issues an expensive full collection load with no where clause. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Fix bloated
ANY()SQL params in lazy join subset queries. When multiple rows reference the same foreign key or have null foreign keys, the join key array now gets deduplicated and null-filtered before being sent to Electric.Root Cause
In
joins.ts, the lazy join path collects foreign keys from all rows via.map(([[joinKey]]) => joinKey)and passes them directly toinArray(). With real data where multiple rows share a foreign key (e.g., 5 tasks in the same project) or have nullable foreign keys, the resultingANY()param contains repeated IDs and NULLs:Approach
Three-line fix at the collection point (
joins.ts:305-317):.filter((key) => key != null)— remove null/undefined keys (loose equality catches both)new Set(...)— deduplicate remaining keysKey Invariants
undefinedfor unmatched rowsinArray(ref, [])calls to the sync layerSetequality works correctly for the primitive types used as join keys (strings, numbers)Non-goals
Verification
Files Changed
packages/db/src/query/compiler/joins.tspackages/db/tests/query/live-query-collection.test.ts.changeset/fix-lazy-join-key-dedup.md🤖 Generated with Claude Code