Skip to content
Merged
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/legal-cooks-sink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db-ivm": patch
---

Fix bug with setWindow on ordered queries that have no limit.
5 changes: 5 additions & 0 deletions .changeset/open-cups-lose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db": patch
---

Add support for orderBy and limit in currentStateAsChanges function
21 changes: 17 additions & 4 deletions packages/db-ivm/src/operators/topKWithFractionalIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,25 @@ class TopKArray<V> implements TopK<V> {
}): TopKMoveChanges<V> {
const oldOffset = this.#topKStart
const oldLimit = this.#topKEnd - this.#topKStart
const oldRange: HRange = [this.#topKStart, this.#topKEnd]

this.#topKStart = offset ?? oldOffset
this.#topKEnd = this.#topKStart + (limit ?? oldLimit)
// `this.#topKEnd` can be `Infinity` if it has no limit
// but `diffHalfOpen` expects a finite range
// so we restrict it to the size of the topK if topKEnd is infinite
const oldRange: HRange = [
this.#topKStart,
this.#topKEnd === Infinity ? this.#topKStart + this.size : this.#topKEnd,
]

const newRange: HRange = [this.#topKStart, this.#topKEnd]
this.#topKStart = offset ?? oldOffset
this.#topKEnd = this.#topKStart + (limit ?? oldLimit) // can be `Infinity` if limit is `Infinity`

// Also handle `Infinity` in the newRange
const newRange: HRange = [
this.#topKStart,
this.#topKEnd === Infinity
? Math.max(this.#topKStart + this.size, oldRange[1]) // since the new limit is Infinity we need to take everything (so we need to take the biggest (finite) topKEnd)
: this.#topKEnd,
]
const { onlyInA, onlyInB } = diffHalfOpen(oldRange, newRange)

const moveIns: Array<IndexedValue<V>> = []
Expand Down
Loading
Loading