Repository navigation
Fall back to rowid when paginating a sort prefix - #2975
Conversation
_next_value_and_url() re-reads the sort column by primary key when that column is not in the SELECT. For a table with no primary key `pks` is `[]`, so the WHERE clause came out empty and the lookup SQL was `select b from items where ` — sqlite3.OperationalError: incomplete input, and a 500. Such tables paginate on rowid, which path_from_row_pks() and the use_rowid handling above already use. Use the same key in the fallback, so the next value is identical to the one the non-fallback path produces. Fixes simonw#2974
82faf07 to
0ac8f60
Compare
|
Hi @simonw — CI has been green here since 09-29 and nothing has changed since, so this is ready for a look whenever you get a chance. One question that would settle the shape of it: for a table with no explicit primary key sorted by a column excluded with |
|
Status is unchanged since 09-29 and CI is green. The open question is whether a table with no primary key should fall back to |
Muguett-DBY
left a comment
There was a problem hiding this comment.
I also checked repeated sort values and NULLs across page boundaries. With 25 rows and _size=4, both ascending and descending _sort=b&_nocol=b traversed all 25 distinct row identifiers in the same order as the corresponding query with b included. The base returned 500 for both excluded-column requests.
This exercised the exact base and PR versions of _next_value_and_url through HTTP using an in-memory SQLite database, with the rest of Datasette from b4c280c on Windows/Python 3.12. I did not build or run the full suite at this PR head.
|
Thanks for the independent check on repeated sort values and NULLs, and for confirming the base 500 on both excluded-column requests — that matches what the PR is meant to fix. One note from the sibling PR #2968: after its latest commit, an empty |
|
Reproduced on https://latest.datasette.io/ using
|
Table HTML pages, and JSON with ?_labels=on, expand foreign key labels, which turns each row into a CustomRow. A CustomRow raises KeyError rather than IndexError for a missing column, so the rowid fallback never ran and those pages still returned a 500. Refs simonw#2974, simonw#2600 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2975 +/- ##
======================================
Coverage 0.00% 0.00%
======================================
Files 78 78
Lines 13392 13662 +270
======================================
- Misses 13392 13662 +270 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
These example links now all work, since the fix has been deployed to https://latest.datasette.io/ |
Fixes #2974.
Sorting a table that has no explicit primary key, by a column excluded from the SELECT with
?_col=or?_nocol=, returns a 500._next_value_and_url()has a fallback that re-reads the sort-column value by primary key when that column is not in the SELECT — added so?_sort=/?_sort_desc=work together with?_col=/?_nocol=(#1773). For a table with no primary key,pksis[], so theWHEREclause is built from an empty list:which SQLite rejects with
sqlite3.OperationalError: incomplete input.The rest of the function already treats
rowidas the key for such tables —path_from_row_pks(rows[-2], pks, use_rowid), and theuse_rowidhandling a few lines above. The fallback was the only place still assuming a declared primary key, so the fix is to use the same key there:used in both the
WHEREclause and the bound parameters.The next value is the same, not just non-crashing
That is the part worth checking, because a fallback that merely stops raising could still paginate to the wrong place. With the fix, all three excluded-column cases produce byte-identical
nexttokens to the control that does not exclude anything —'b10,10'for ascending and'b16,16'for descending:The new test walks the whole
next_urlchain rather than checking one page, and asserts the exact order with no skipped or repeated rows, so a cursor that is merely valid but points somewhere else would still fail. Tables with a primary key are untouched, and a literal$nullsort value still round-trips.Scope
I kept this separate from #2600 even though both sit in the same
except IndexErrorclause. #2600 is a different defect:CustomRow(used with_labels=on) raisesKeyErrorwheresqlite3.RowraisesIndexError, so that path never reaches this handler at all. CatchingKeyErroras well would not fix this — here the handler does run and then builds the broken SQL. Happy to combine them if you would rather have one PR.Checks
pytestcec5e6b2— +3 new parametrizations, nothing else movedruff checkThe baseline's 15
xpassedare pre-existing, not something this change introduced.Not run:
blacken-docs,codespellandsphinx(not installed here, and no.rstchanged),prettier(no JavaScript changed), and the Playwright browser tests (out of scope). Nocogblock is affected —grep -c cogon both changed files finds nothing but the substring inside"unrecognized token", which is pre-existing — souv run cog -r docs/*.rstis not needed.History note:
git log -Sandgit blameon this file need network for the partial clone and time out, so I established the origin withgit log --oneline -2000plusgit show df4fd2d7("_sort= works even if sort column not selected, closes #1773"), which added the fallback and a test only againstsortable— a table that has a primary key.