Skip to content

Filters match numbers in columns with no declared type - #3111

Merged
simonw merged 2 commits into
mainfrom
view-filter-affinity
Nov 22, 2026
Merged

simonw merged 2 commits into
mainfrom
view-filter-affinity

Conversation

@simonw

@simonw simonw commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Closes:

A column in a view that is calculated by an expression, such as count(*) or sum(amount), has no type affinity. SQLite does not convert the text value from the query string to a number when comparing against it, so ?total=5 returned no rows even when every row had a total of 5. Facet links are ?column=value filters, so facets on those columns linked to empty pages.

The fix

For columns with no declared type, and only for values that look like numbers, the exact, not, in and notin filters now compare the value as both text and a number:

where "total" in (:p0, cast(:p0 as numeric))
Claude Fable 5.2 Medium
  • Columns with a declared type get exactly the SQL they got before.
  • The parameter is still the one string from the query string, so "View and edit SQL" returns the same rows.
  • The in form is deliberate. "total" = cast(:p0 as numeric) would apply numeric affinity to the column too, so ?col=1 would also match the text '1.0', and it stops SQLite using an index on the column.
  • __gt, __gte, __lt and __lte already worked, because those convert the value in Python.

_table_filters() looks up the column types only when the request has filter arguments. That lookup is served from the schema cache added in #3102.

Behaviour changes to be aware of

  • Table columns declared without a type are affected too. ?column=1 on such a column now matches the number 1 as well as the text "1". It previously matched only the text.
  • One case is still imperfect. If an untyped column holds the same number in several forms (integer 1, real 1.0, text '1'), each of its facet links now returns all of those rows, which is more than the count shown beside the link. A facet link cannot say which type it means. On main the text links were right and the number link returned too few rows.

Testing

I ran 16 filter URLs and 14 facet links against a view with sum(), count(), avg(), a case expression and a text column, plus typed and untyped table columns:

main This PR
Filter URLs returning the wrong rows 8 of 16 0 of 16
Facet links not matching their count 9 of 14 3 of 14 (the mixed-type case above)

The fixtures database has a new view, facetable_by_state, with three columns calculated by aggregate functions:

create view facetable_by_state as
    select state, count(*) as row_count, sum(planet_int) as planet_total, avg(on_earth) as avg_on_earth
    from facetable group by state

/fixtures/facetable_by_state?row_count=4 returns no rows on main and the MI row with this change.

Tests in tests/test_filters.py:

  • 19 filter URLs and every facet link on those three columns, against the fixture view. 17 of those 22 tests fail if the table view does not pass the column types through.
  • A smaller test that still builds its own in-memory database, for cases the fixtures have no data for: table columns declared without a type, text that looks like a number, and numbers that are not whole.
  • Unit tests for the generated SQL and for what counts as a number.

Three existing tests changed because of the new view: two lists of fixture views, and test_database_list_respects_view_table, which now asks for ?database=data so that it does not depend on how many items the fixtures put on the homepage.

The full suite passes locally (3,727 passed).

Other PRs for these issues

This PR is based on a patch from the 1.0 milestone review, rebased onto main.

🤖 Generated with Claude Code

A column in a view that is calculated by an expression, such as
count(*) or sum(amount), has no type affinity. SQLite does not convert
the text value from the query string to a number when comparing against
it, so ?total=5 returned no rows and facet links led to empty pages.

For columns with no declared type, the exact, not, in and notin filters
now compare a value that looks like a number as both text and a number:

    "total" in (:p0, cast(:p0 as numeric))

Columns with a declared type get the same SQL as before.

Closes #2073, closes #2713

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (9f2da6b) to head (984a49a).

Files with missing lines Patch % Lines
datasette/filters.py 0.00% 35 Missing ⚠️
datasette/views/table.py 0.00% 5 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main   #3111   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         81      81           
  Lines      14582   14612   +30     
=====================================
- Misses     14582   14612   +30     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@simonw

simonw commented Nov 22, 2026

Copy link
Copy Markdown
Owner Author

We need a view in fixtures.db to help demonstrate this.

@simonw simonw added the bug label Nov 22, 2026
@simonw simonw added this to the Datasette 1.0 stable milestone Nov 22, 2026
The view has three columns calculated by aggregate functions, which
have no declared type: row_count, planet_total and avg_on_earth.

The filter and facet link tests now use it in place of a database of
their own. One smaller in-memory test stays for cases the fixtures have
no data for: untyped table columns, text that looks like a number and
numbers that are not whole.

test_database_list_respects_view_table asks for ?database=data, so it
no longer depends on how many items the fixtures put on the homepage.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@simonw
simonw merged commit 9e4cab0 into main Nov 22, 2026
16 checks passed
@simonw
simonw deleted the view-filter-affinity branch November 22, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant