Fix iter_rows_keyset NULL hang and silent row loss on ties#4
Merged
Conversation
The 0.6 keyset cursor used `WHERE by > last_seen` which broke in two ways: NULL at a page boundary stalled the cursor (#1), and tied non-NULL values were silently skipped because `WHERE by > X` excludes everything with `by = X`. Replace with a compound cursor: `[by] + [p for p in __pk__ if p != by]`. PK tail breaks ties and lets the cursor paginate through NULL values. ORDER BY uses explicit NULLS FIRST so iteration order is deterministic across backends (Postgres default would be NULLS LAST; SQLite default is already NULLS FIRST). WHERE has three shapes branched on cursor state: - first page -- no cursor predicate - NULL region -- (by IS NULL AND <pk_cursor>) OR by IS NOT NULL - past NULL -- (by, *pk_tail) > (last_by, *last_pk_tail) The OR-group is fully parenthesized so filters compose correctly. Single-column cursor (by IS pk, single PK) degenerates to scalar `by > $k` so no one-element row-value syntax is emitted. API change: sql.select_keyset's `last_seen` parameter shape goes from scalar to Sequence aligned with the compound cursor. The ValueError introduced in 0.6 for NULL-at-page-boundary is gone -- the cursor handles NULL properly now. SQLite 3.30+ is required for the NULLS FIRST keyword; mainstream environments comply. Fixes #1.
This was referenced May 13, 2026
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.
Summary
Replaces the narrow ValueError fix from #2 with proper compound-cursor pagination.
iter_rows_keysetnow correctly paginates through NULL values in `by` and through tied non-NULL values; both failure modes are gone.Re #1 (already closed via #2; this PR replaces that narrow fix with the proper one).
What changed
API change
`sql.select_keyset`'s `last_seen` parameter shape changes from scalar to `Sequence` aligned with the compound cursor. This is a breaking change for direct emitter consumers / `db.compose` previewers; `db.iter_rows_keyset`'s public API is unchanged.
Behavior change for nullable `by` columns
NULL rows now iterate first on Postgres (was implicit NULLS LAST). Non-nullable `by` is unaffected. Users who were hitting the ValueError now get correct pagination instead. No previously-working iteration becomes broken.
SQLite requirement
`NULLS FIRST` needs SQLite 3.30+ (2019). All mainstream environments comply (macOS 3.43, Ubuntu 22.04 3.37, Ubuntu 24.04 3.45+). Documented in README.
Test plan