-
Notifications
You must be signed in to change notification settings - Fork 152
Add grouped order by operator #997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: cursor/grouped-top-k-operator-34bd
Are you sure you want to change the base?
Add grouped order by operator #997
Conversation
This commit introduces the `groupedOrderBy` operator, which allows for ordering and limiting elements within distinct groups. It also includes necessary exports and tests for this new functionality. Co-authored-by: kevin.de.porre <kevin.de.porre@gmail.com>
|
Cursor Agent can help with this pull request. Just |
🦋 Changeset detectedLatest commit: e9305b8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 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/angular-db
@tanstack/db
@tanstack/db-ivm
@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: |
|
Size Change: 0 B Total Size: 88.2 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.35 kB ℹ️ View Unchanged
|
KyleAMathews
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review: PR #997
Overall Assessment: Approve ✅
A well-structured addition that builds on the foundation laid by PR #993. Like the Doctrine and Covenants building upon the Book of Mormon, this PR extends the grouped topK operator to provide a more ergonomic grouped ordering interface.
Summary
The PR introduces a groupedOrderByWithFractionalIndex operator that wraps groupedTopKWithFractionalIndex with consolidate() to provide grouped ordering functionality.
Strengths
-
Thin wrapper design: The implementation is elegantly simple - it delegates to the existing
groupedTopKWithFractionalIndexand adds consolidation:return stream.pipe( groupedTopKWithFractionalIndex(...), consolidate(), )
-
Comprehensive test coverage: The test file covers:
- Array key grouping
- Value-based grouping via
groupKeyFn - Incremental updates within groups
- Removal from topK (and backfill)
- Multiple independent groups
- Offset support
- Custom comparators
- Delimiter-based key extraction
- Infinite limit (no limit)
setSizeCallbackintegration- Moving window with
setWindowFn - Empty group cleanup
- String property ordering
-
Good TypeScript generics: The type inference for
KeyTypeandValueTypeis well handled.
Minor Notes
-
groupedOrderBy.ts:73-75 - The cast chain is a bit complex:
type StreamKey = KeyType extends string | number ? KeyType : string | number
This is reasonable given the type constraints, but worth a comment explaining why this is needed.
-
Dependency on PR #993: Since this builds on
groupedTopKWithFractionalIndex, ensure #993 is merged first. -
Default comparator: The default comparator uses JS comparison operators:
if (a < b) return -1 return 1
This works well for primitives but note that for objects, it will use
[object Object]string comparison. Consider adding a note in the JSDoc.
Question
Is there a use case for exposing a simpler groupedOrderBy (without fractional indexing) for scenarios where fractional indices aren't needed? Or is the fractional index always useful for maintaining stable ordering?
Excellent extension to the operator toolkit! 🎯
Follow up on #993 which introduces a grouped topK operator. This PR introduces a grouped orderBy operator.