Make keyset cursor pagination NULL-aware#1331
Open
SachaProbo wants to merge 1 commit into
Open
Conversation
fdd28b0 to
b0cce4e
Compare
cfee0e2 to
4b58313
Compare
gearnode
reviewed
Jun 16, 2026
| package page | ||
|
|
||
| import ( | ||
| "strings" |
957e758 to
e2cf8d6
Compare
Cursor.SQLFragment emitted a seek predicate with no NULL handling, so once a page boundary landed on a NULL order value the WHERE matched no rows and pagination terminated early, silently dropping the remaining rows. Rewrite SQLFragment to render the predicate from a template that emits explicit NULLS placement and a null-aware, type-agnostic seek, mirroring the direction and NULLS placement on the Head/Tail inversion so backward paging stays an exact inverse. For NOT NULL columns it reduces to the previous predicate. Cover the whole cursor stack with tests: a real-Postgres pagination walk (e2e/page) that drives the production SQLFragment/SQLArguments forward and backward through every NULL edge case, plus unit tests for the generated SQL and arguments, cursor-key serialization, page trimming, and order direction parsing. Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
e2cf8d6 to
00d4d5f
Compare
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.
Cursor.SQLFragment emitted a seek predicate with no NULL handling, so once a page boundary landed on a NULL order value the WHERE matched no rows and pagination terminated early, silently dropping the remaining rows.
Rewrite SQLFragment to emit explicit NULLS placement and a null-aware, type-agnostic seek predicate, mirroring the direction and NULLS placement on the Head/Tail inversion so backward paging stays an exact inverse. For NOT NULL columns it reduces to the previous predicate.
Add white-box pagination tests that evaluate the generated SQL with three-valued logic and walk the whole set forward and backward across the NULL boundary.
Summary by cubic
Makes keyset cursor pagination NULL-aware so pages don’t truncate when the sort column has NULLs. Adds explicit NULLS placement and a null-aware seek; backward paging mirrors forward exactly.
Tests
+785-0@cursor_field_valueappears only for non-NULL seeks.CursorKeyround-trip and invalid formats,OrderDirectiontext marshaling, andPagetrimming/HasNext/HasPrev for Head/Tail.Service
+67-21Cursor.SQLFragmentvia a template that sets ASC/DESC, NULLS FIRST/LAST, and id tiebreak; Tail flips both for symmetry.@cursor_field_valueonly when non-NULL.Other
+2-2./e2e/page/....Written for commit 00d4d5f. Summary will update on new commits.