fix: use single-column comparator for BTree index in multi-column orderBy#1401
Merged
kevin-dp merged 6 commits intoTanStack:mainfrom Mar 24, 2026
Merged
Conversation
…erBy When a query has multiple orderBy columns (e.g. `.orderBy(createdAt, 'desc').orderBy(id, 'desc')`), the order-by compiler creates a multi-column comparator that expects array values. Previously, `ensureIndexForField` received this multi-column comparator to create a single-column BTree index on the first field. The BTree stored individual field values (numbers), but the multi-column comparator treated them as arrays — indexing into a number returns `undefined`, so all values compared as NaN/equal. This collapsed the entire BTree to a single entry, causing `takeFromStart()` to return at most 1 key. Any live query subscription created after data was already in the collection would see 0 results. The fix passes `makeComparator(compareOpts)` — a proper single-column comparator built from the first orderBy column's compare options — instead of the multi-column `compare` function. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-browser-wa-sqlite-persisted-collection
@tanstack/db-capacitor-sqlite-persisted-collection
@tanstack/db-cloudflare-do-sqlite-persisted-collection
@tanstack/db-electron-sqlite-persisted-collection
@tanstack/db-expo-sqlite-persisted-collection
@tanstack/db-ivm
@tanstack/db-node-sqlite-persisted-collection
@tanstack/db-react-native-sqlite-persisted-collection
@tanstack/db-sqlite-persisted-collection-core
@tanstack/db-tauri-sqlite-persisted-collection
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Move the single-column comparator and multi-column orderBy tests into the existing BTreeIndex describe block in deterministic-ordering.test.ts rather than keeping them in a separate file. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Merged
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
orderBycolumns (e.g..orderBy(createdAt, 'desc').orderBy(id, 'desc'))ensureIndexForFieldwas passing the multi-column array comparator to create a single-column BTree index[col1, col2]arrays but receives individual field values (numbers) — indexing into a number returnsundefined, so all values compare as NaN/equaltakeFromStart()to return at most 1 key, breaking live query subscriptions for pre-existing dataThe fix
One line change in
order-by.ts: passmakeComparator(compareOpts)(single-column comparator) instead ofcompare(multi-column comparator) toensureIndexForField.Test plan
btree-index-wrong-comparator.test.tsdocumenting the root cause and verifying the fix🤖 Generated with Claude Code