Repository navigation
Apply the primary key tie-breaker to the first page of a sorted table - #3055
Conversation
The primary keys were only added to the sort order when ?_next= was set, so the first page returned ties on the sort column in whatever order SQLite picked while the cursor assumed primary key order. Tables with a text or compound primary key repeated some rows and skipped others when following next_url.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3055 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 79 79
Lines 13875 13875
=====================================
Misses 13875 13875 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Demo of the problem: https://lite.datasette.io/?ref=1.0a42&sql=https://gist.github.com/simonw/3260bb75c70f3869734659898e7ac845#/data/tickets?_size=2&_sort=status - then click next page. Page 2 looks like this:
If you view https://lite.datasette.io/?ref=1.0a42&sql=https://gist.github.com/simonw/3260bb75c70f3869734659898e7ac845#/data/tickets you see this:
|
|
With this fix applied clicking "next" on the first page takes you to https://lite.datasette.io/?ref=e1ce601b0c5d221c1c089d81dc29e7be9b7247c5&sql=https://gist.github.com/simonw/3260bb75c70f3869734659898e7ac845#/data/tickets?_size=2&_next=b
Which is correct. |



Closes:
The primary keys were only added to the sort as a tie-breaker when
?_next=was set. So the first page of a sorted table returned ties in whatever order SQLite picked, while the cursor assumes primary key order. On tables with a text or compound primary key, followingnext_urlrepeats some rows and skips others. The issue has a script that shows it.The tie-breaker now applies to the first page as well, for tables only, since a view has no primary keys to add. Later pages already used this order, so the first page now pays the same sort cost they do. I did not measure that on a large table.
test_sort_ties_paginate_without_skips_or_repeatscovers_sortand_sort_descon a text key and a compound key. With the tie-breaker limited to_nextagain, all four cases fail, for example withassert ['a', 'm', 'q', 'y', 'z', 'z'] == ['a', 'c', 'm', 'q', 'y', 'z']. With the change,tests/test_table_api.py,test_table_html.py,test_csv.py,test_html.py,test_facets.pyandtest_api.pygive 657 passed, 4 skipped, 1 xfailed, 10 xpassed, andruff checkandblack --checkare clean. I ran this in a Linux container and did not run the full suite or the Playwright tests.