Skip to content

Fix exact filters for numeric computed view columns - #2717

Closed
Ethan-kkk wants to merge 1 commit into
simonw:mainfrom
Ethan-kkk:fix-view-computed-column-filter
Closed

Ethan-kkk wants to merge 1 commit into
simonw:mainfrom
Ethan-kkk:fix-view-computed-column-filter

Conversation

@Ethan-kkk

Copy link
Copy Markdown

Fixes #2713

This fixes filtering SQLite views by numeric computed columns, such as SUM(...) AS valid_count, where valid_count__exact=0 returned no rows even when the view contained rows with valid_count = 0.

URL query parameters are bound as strings, so valid_count__exact=0 binds "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 integer 0 to text "0" does not match.

This change passes column details into filter SQL generation. For exact and not filters on columns with no declared type, numeric-looking filter values now include an additional guarded numeric comparison using typeof(column) in ('integer', 'real') and CAST(:p AS NUMERIC).

The original text comparison is preserved, so text values such as "00" are not incorrectly matched as numeric 0.

Tests cover:

  • valid_count__exact=0 on a computed numeric view column
  • valid_count__not=0 on the same view
  • preserving exact text matching for a computed text view column

@kokokoXUY kokokoXUY left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@simonw

simonw commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks for this. I landed a different fix in #3111 (9e4cab0).

@simonw simonw closed this Nov 23, 2026
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.

Filtering views on computed SUM columns returns 0 rows

3 participants