Repository navigation
analyze_column() handles columns containing nulls - #972
Merged
Merged
Conversation
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>
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes:
Table.analyze_column()gave wrong results, or raisedTypeError, for columns that contain nulls.Claude Fable 5.2 Medium
Current buggy behavior produces:
With this fix applied you get:
What was wrong
All four problems come from nulls not being accounted for in
analyze_column():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.TypeErrorwhen counts tie. Ties were broken by comparing the values themselves, andNonecannot be compared with a string (nor anintwith astrin 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."None". Withvalue_truncateset, whichsqlite-utils analyze-tablesalways does, a null was turned into"None"and could not be told apart from that text value.truncate()now leavesNonealone.least_commonis skipped when everything already fits inmost_common, but that check usednum_distinct, which does not count the null group. It now uses the number of groups.Things a user could notice
analyze-tables --savenow stores JSONnullfor a null value where it used to store"None".most_commonand first among them inleast_common, following SQLite's type order.num_distinctis unchanged: it is stillcount(distinct), so it does not count null. A test pins that.analyze-tablesstill prints a null asNone.Testing
New tests in
tests/test_analyze_tables.py; 9 of them fail onmainand 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/