-
Notifications
You must be signed in to change notification settings - Fork 228
fix(db): boundary expansion for multi-column orderBy pagination #1556
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
Open
v-anton
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
v-anton:fix/boundary-expansion-multi-orderby
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+398
−0
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| 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 |
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
154 changes: 154 additions & 0 deletions
154
packages/db/tests/boundary-expansion-multi-orderby.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { createCollection } from '../src/collection/index.js' | ||
| import { createLiveQueryCollection, eq } from '../src/query/index.js' | ||
| import { flushPromises, mockSyncCollectionOptions } from './utils.js' | ||
|
|
||
| type Item = { | ||
| id: string | ||
| category: string | ||
| name: string | ||
| } | ||
|
|
||
| describe(`Boundary expansion for multi-column orderBy`, () => { | ||
| it(`should include all boundary items when paginating with multi-column orderBy`, async () => { | ||
| // Items with same category (first orderBy col) but different names (second orderBy col). | ||
| // BTree indexes by (category, _id) only. When paginating, items sharing the | ||
| // boundary category but with different _id ordering may be missed by BTree's take(). | ||
| // The boundary expansion step should send them so D2 picks the correct top-K. | ||
| const initialData: Array<Item> = [ | ||
| { id: `a1`, category: `A`, name: `Zeta` }, | ||
| { id: `a2`, category: `A`, name: `Alpha` }, | ||
| { id: `a3`, category: `A`, name: `Beta` }, | ||
| { id: `b1`, category: `B`, name: `Gamma` }, | ||
| { id: `b2`, category: `B`, name: `Delta` }, | ||
| { id: `c1`, category: `C`, name: `Epsilon` }, | ||
| ] | ||
|
|
||
| const sourceCollection = createCollection( | ||
| mockSyncCollectionOptions({ | ||
| id: `boundary-multi-orderby-source`, | ||
| getKey: (item: Item) => item.id, | ||
| initialData, | ||
| autoIndex: `eager`, | ||
| }), | ||
| ) | ||
|
|
||
| await sourceCollection.preload() | ||
|
|
||
| const liveQuery = createLiveQueryCollection((q) => | ||
| q | ||
| .from({ items: sourceCollection }) | ||
| .orderBy(({ items }) => items.category, `asc`) | ||
| .orderBy(({ items }) => items.name, `asc`) | ||
| .limit(4) | ||
| .select(({ items }) => ({ | ||
| id: items.id, | ||
| category: items.category, | ||
| name: items.name, | ||
| })), | ||
| ) | ||
|
|
||
| await liveQuery.preload() | ||
| await flushPromises() | ||
|
|
||
| const results = Array.from(liveQuery.values()) | ||
|
|
||
| // Full multi-column sort (category asc, name asc): | ||
| // A-Alpha (a2), A-Beta (a3), A-Zeta (a1), B-Delta (b2), B-Gamma (b1), C-Epsilon (c1) | ||
| // Top 4 should be: A-Alpha, A-Beta, A-Zeta, B-Delta | ||
| expect(results).toHaveLength(4) | ||
| expect(results.map((r) => r.id)).toEqual([`a2`, `a3`, `a1`, `b2`]) | ||
| }) | ||
|
|
||
| it(`should return correct results when all items share the same first orderBy value`, async () => { | ||
| const initialData: Array<Item> = [ | ||
| { id: `x3`, category: `X`, name: `Cherry` }, | ||
| { id: `x1`, category: `X`, name: `Apple` }, | ||
| { id: `x2`, category: `X`, name: `Banana` }, | ||
| { id: `x4`, category: `X`, name: `Date` }, | ||
| { id: `x5`, category: `X`, name: `Elderberry` }, | ||
| ] | ||
|
|
||
| const sourceCollection = createCollection( | ||
| mockSyncCollectionOptions({ | ||
| id: `boundary-same-first-col`, | ||
| getKey: (item: Item) => item.id, | ||
| initialData, | ||
| autoIndex: `eager`, | ||
| }), | ||
| ) | ||
|
|
||
| await sourceCollection.preload() | ||
|
|
||
| const liveQuery = createLiveQueryCollection((q) => | ||
| q | ||
| .from({ items: sourceCollection }) | ||
| .orderBy(({ items }) => items.category, `asc`) | ||
| .orderBy(({ items }) => items.name, `asc`) | ||
| .limit(3) | ||
| .select(({ items }) => ({ | ||
| id: items.id, | ||
| category: items.category, | ||
| name: items.name, | ||
| })), | ||
| ) | ||
|
|
||
| await liveQuery.preload() | ||
| await flushPromises() | ||
|
|
||
| const results = Array.from(liveQuery.values()) | ||
|
|
||
| // All share category X, sorted by name: Apple (x1), Banana (x2), Cherry (x3), Date (x4), Elderberry (x5) | ||
| // Top 3: Apple, Banana, Cherry | ||
| expect(results).toHaveLength(3) | ||
| expect(results.map((r) => r.id)).toEqual([`x1`, `x2`, `x3`]) | ||
| }) | ||
|
|
||
| it(`should handle multi-column orderBy with where filter correctly`, async () => { | ||
| const initialData: Array<Item & { active: boolean }> = [ | ||
| { id: `a1`, category: `A`, name: `Zeta`, active: true }, | ||
| { id: `a2`, category: `A`, name: `Alpha`, active: false }, | ||
| { id: `a3`, category: `A`, name: `Beta`, active: true }, | ||
| { id: `b1`, category: `B`, name: `Gamma`, active: true }, | ||
| { id: `b2`, category: `B`, name: `Delta`, active: true }, | ||
| { id: `c1`, category: `C`, name: `Epsilon`, active: true }, | ||
| ] | ||
|
|
||
| const sourceCollection = createCollection( | ||
| mockSyncCollectionOptions({ | ||
| id: `boundary-multi-orderby-where`, | ||
| getKey: (item: (typeof initialData)[0]) => item.id, | ||
| initialData, | ||
| autoIndex: `eager`, | ||
| }), | ||
| ) | ||
|
|
||
| await sourceCollection.preload() | ||
|
|
||
| const liveQuery = createLiveQueryCollection((q) => | ||
| q | ||
| .from({ items: sourceCollection }) | ||
| .where(({ items }) => eq(items.active, true)) | ||
| .orderBy(({ items }) => items.category, `asc`) | ||
| .orderBy(({ items }) => items.name, `asc`) | ||
| .limit(3) | ||
| .select(({ items }) => ({ | ||
| id: items.id, | ||
| category: items.category, | ||
| name: items.name, | ||
| })), | ||
| ) | ||
|
|
||
| await liveQuery.preload() | ||
| await flushPromises() | ||
|
|
||
| const results = Array.from(liveQuery.values()) | ||
|
|
||
| // Active items sorted by (category asc, name asc): | ||
| // A-Beta (a3), A-Zeta (a1), B-Delta (b2), B-Gamma (b1), C-Epsilon (c1) | ||
| // (a2 Alpha is inactive, filtered out) | ||
| // Top 3: A-Beta, A-Zeta, B-Delta | ||
| expect(results).toHaveLength(3) | ||
| expect(results.map((r) => r.id)).toEqual([`a3`, `a1`, `b2`]) | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.