Repository navigation
Conversation
kokokoXUY
left a comment
There was a problem hiding this comment.
I reproduced an identifier-quoting regression on current 2f54c2d. The new numeric comparison interpolates the column as f'"{column}"' rather than using the existing SQLite identifier quoting helper. A valid computed view column containing a double quote then produces invalid SQL.
Minimal reproduction (no Datasette server needed):
import sqlite3
from types import SimpleNamespace
from datasette.filters import Filters
column = 'numeric"value'
clauses, params = Filters(((column + '__exact', '1'),)).build_where_clauses(
'computed', {column: SimpleNamespace(type='')}
)
conn = sqlite3.connect(':memory:')
conn.execute('create table items (n integer)')
conn.execute('insert into items values (1)')
conn.execute('create view computed as select n + 0 as "numeric""value" from items')
conn.execute('select * from computed where ' + ' and '.join(clauses), params)This raises sqlite3.OperationalError: near "value": syntax error because the generated clause contains "numeric"value". On the unmodified base, Filters.build_where_clauses() quotes that identifier as "numeric""value" and the query executes (it returns no row because of the original numeric-view bug). Please use the same identifier-quoting path in the new branch and add a quoted-column regression. I also see GitHub currently reports merge conflicts against main, so this needs rebasing before it can land.
I ran this against the current PR source and the unmodified main checkout. Review prepared with Codex assistance.
Fixes #2713
This fixes filtering SQLite views by numeric computed columns, such as
SUM(...) AS valid_count, wherevalid_count__exact=0returned no rows even when the view contained rows withvalid_count = 0.URL query parameters are bound as strings, so
valid_count__exact=0binds"0"as text. Regular INTEGER table columns can rely on SQLite affinity for comparison, but computed view columns may have no declared type, so comparing integer0to text"0"does not match.This change passes column details into filter SQL generation. For
exactandnotfilters on columns with no declared type, numeric-looking filter values now include an additional guarded numeric comparison usingtypeof(column) in ('integer', 'real')andCAST(:p AS NUMERIC).The original text comparison is preserved, so text values such as
"00"are not incorrectly matched as numeric0.Tests cover:
valid_count__exact=0on a computed numeric view columnvalid_count__not=0on the same view