Skip to content
Source
Sign inSign up
TanStack/db

fix(db): boundary expansion for multi-column orderBy pagination #1556

Closedv-anton(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/1556/headopened

Showing 3 changed files with 25 additions and 0 deletions (loading 1).

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/fix-boundary-expansion-multi-orderby.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+"@tanstack/db": patch
+---
+​
+fix(db): expand boundary items for multi-column orderBy in loadNextItems
20 additions and 0 deletionspackages/db/src/collection/subscription.tsModified
View file
@@ -543,6 +543,26 @@
keys = index.take(valuesNeeded(), biggestObservedValue!, filterFn)
​
+ // Boundary expansion for multi-column orderBy:
+ // BTree orders by (first_col, _id) only. Items sharing the same first-column
+ // value as biggestObservedValue but not selected by BTree may rank higher by
+ // the full comparator. Send all of them so D2 picks the correct top-K.
+ if (orderBy.length > 1 && biggestObservedValue !== undefined && valueExtractor) {
+ const alreadyAddedKeys = new Set(changes.map((c) => c.key))
+ const boundaryChanges = this.collection.currentStateAsChanges({
+ where: eq(orderByExpression, new Value(biggestObservedValue)),
+ })
+ if (boundaryChanges) {
+ for (const { key, value } of boundaryChanges) {
+ if (!alreadyAddedKeys.has(key) && !this.sentKeys.has(key)) {
+ if (value !== undefined && (whereFilterFn?.(value) ?? true)) {