Skip to content

analyze_column() handles columns containing nulls - #972

Merged
simonw merged 1 commit into
mainfrom
analyze-column-nulls
Nov 29, 2026
Merged

simonw merged 1 commit into
mainfrom
analyze-column-nulls

Conversation

@simonw

@simonw simonw commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Fixes:

Table.analyze_column() gave wrong results, or raised TypeError, for columns that contain nulls.

Claude Fable 5.2 Medium
db["t"].insert_all([{"c": None}, {"c": None}, {"c": "a"}, {"c": "a"}])
db["t"].analyze_column("c").most_common

Current buggy behavior produces:

[(None, 4)]

With this fix applied you get:

[('a', 2), (None, 2)]

What was wrong

All four problems come from nulls not being accounted for in analyze_column():

  1. Wrong count for a column of nulls plus one other value. count(distinct col) ignores nulls, so such a column took the "only one distinct value" shortcut and reported that value with the total row count. The shortcut now only runs when the column has no nulls.
  2. TypeError when counts tie. Ties were broken by comparing the values themselves, and None cannot be compared with a string (nor an int with a str in a typeless column). Ties are now ordered by SQLite's own type order - null, numbers, text, blobs - so values of different types are never compared.
  3. Null shown as the string "None". With value_truncate set, which sqlite-utils analyze-tables always does, a null was turned into "None" and could not be told apart from that text value. truncate() now leaves None alone.
  4. A value missing from both lists. least_common is skipped when everything already fits in most_common, but that check used num_distinct, which does not count the null group. It now uses the number of groups.

Things a user could notice

  • analyze-tables --save now stores JSON null for a null value where it used to store "None".
  • When a null ties on count with other values it sorts last among the ties in most_common and first among them in least_common, following SQLite's type order.
  • num_distinct is unchanged: it is still count(distinct), so it does not count null. A test pins that.
  • The terminal output of analyze-tables still prints a null as None.

Testing

New tests in tests/test_analyze_tables.py; 9 of them fail on main and pass here. Full suite: 1586 passed, 4 skipped. The fix adds no SQL, so older SQLite versions are unaffected.

🤖 Generated with Claude Code


📚 Documentation preview 📚: https://sqlite-utils--972.org.readthedocs.build/en/972/

count(distinct col) ignores nulls, which analyze_column() did not account for:

- A column of nulls plus a single other value took the "one distinct
  value" shortcut and reported that value with the total row count.
  The shortcut now requires the column to contain no nulls.
- Sorting most_common / least_common raised TypeError when a null (or
  values of different types, e.g. int and str) tied on count. Ties are
  now broken using SQLite's type ordering: null, numbers, text, blobs.
- value_truncate turned a null into the string "None", making it
  indistinguishable from the text "None" (including in --save output).
  Nulls are now returned unchanged.
- least_common was skipped when num_distinct <= common_limit even though
  the null group made one more group than that, so a value could be
  missing from both lists. The null group is now counted for that check.

num_distinct keeps its existing count(distinct) meaning, which excludes null.

Refs #887

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonw simonw added the bug Something isn't working label Nov 29, 2026
@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.79%. Comparing base (1c271c3) to head (539979f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #972      +/-   ##
==========================================
+ Coverage   94.78%   94.79%   +0.01%     
==========================================
  Files          10       10              
  Lines        4585     4595      +10     
==========================================
+ Hits         4346     4356      +10     
  Misses        239      239              

☔ 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.

@simonw
simonw merged commit cfcf5d7 into main Nov 29, 2026
83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant