Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-boundary-expansion-multi-orderby.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db": patch
---

fix(db): expand boundary items for multi-column orderBy in loadNextItems
20 changes: 20 additions & 0 deletions packages/db/src/collection/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,26 @@ export class CollectionSubscription
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)) {
changes.push({ type: `insert`, key, value })
}
}
}
}
}

// Track row count for offset-based pagination (before sending to callback)
// Use the current count as the offset for this load
const currentOffset = this.limitedSnapshotRowCount
Expand Down
Loading