Modify topK and orderBy to track by key instead of by object reference#405
Merged
Modify topK and orderBy to track by key instead of by object reference#405
Conversation
🦋 Changeset detectedLatest commit: beace86 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Contributor
|
Size Change: 0 B Total Size: 58.5 kB ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 1.05 kB ℹ️ View Unchanged
|
samwillis
reviewed
Aug 13, 2025
Collaborator
samwillis
left a comment
There was a problem hiding this comment.
There is a failed test in the react-db package - need to look at that.
Contributor
Author
Seems to be a flake. CI is green now. |
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.
This PR modifies the topK and orderBy operators. Previoiusly, the topK was computing a topK per key and it tracked multiplicities for each value associated to that key. We were always keying by
nullin ts/db and the values were entire rows, i.e. objects. However, the objects are always different objects (even for the same row) so they were always treated as a new value.As reported by community members, this led to a bug where the results of an
orderByquery with e.g.limit: 10would end up with just a single item in the result. This would happen in the following case:To solve this problem i modified the
topKoperator such that it takes a keyed stream, i.e. a stream of tuples:[Key, Value]. It computes the topK across all keys (i.e. not per key as it did before) and uses the key to identify values. As such, when a row is received we lookup its multiplicity based on the key instead of the object reference.