Skip to content

fix(base): TableRows.__len__ read a SELECT's rowcount (-1 on SQLite) - #8

Merged
thorwhalen merged 1 commit into
masterfrom
fix/tablerows-len-rowcount
Sep 22, 2026
Merged

thorwhalen merged 1 commit into
masterfrom
fix/tablerows-len-rowcount

Conversation

@thorwhalen

Copy link
Copy Markdown
Member

Post-merge refute review of #5.

#5 fixed the SELECT-rowcount trap in SqlBaseKvStore.__setitem__ (and #4 in SqlBaseKvReader.__len__), and the fix itself holds up. But the same trap remains in the exported TableRows.__len__, which returned result.rowcount of a SELECT *: on SQLite (and any driver that does not pre-buffer) that is -1, so len(TableRows(...)) raises ValueError: __len__() should return >= 0.

Fix. Count with SELECT count(*) ... [WHERE filt], honouring the filt the rows were built with.

Tests. sqldol/tests/test_table_rows_len.py: unfiltered and filtered len against in-memory SQLite. Both fail on master with the ValueError, pass here. Full suite + doctests: 104 passed locally (Python 3.10).

Also checked in #5 and found fine: .first() releases the SELECT before the UPDATE/INSERT on the same connection; duplicate rows for a key (from before #5) are all updated rather than adding another.

Not changed (pre-existing, noted only): sql_base.iter_rows stops on a falsy rowcount, which is -1 (truthy) on sqlite3, so without a limit it keeps issuing empty pages until limit (default 1e12) -- the existing injection test already works around it with a bounded batch.

Self-reviewed only (the dispatching run disallowed sub-agents).

🤖 Generated with Claude Code

https://claude.ai/code/session_011HSBVhDjRU4apSLcRkavv9

The same rowcount trap #4 fixed in SqlBaseKvReader.__len__ and #5 fixed in
SqlBaseKvStore.__setitem__ was still in TableRows.__len__, so len() of a
TableRows over SQLite raised "ValueError: __len__() should return >= 0".
Ask the database for COUNT(*), honouring the row filter.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@thorwhalen
thorwhalen merged commit 088c99c into master Sep 22, 2026
6 checks passed
@thorwhalen
thorwhalen deleted the fix/tablerows-len-rowcount branch September 22, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant