Skip to content

Commit 87798a9

Browse files
committed
fix: count null groups before skipping least-common analysis
1 parent 8e5dfdd commit 87798a9

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎sqlite_utils/db.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5375,7 +5375,11 @@ def sort_key(pair):
53755375
]
53765376
most_common_results.sort(key=sort_key, reverse=True)
53775377
if least_common:
5378-
if num_distinct <= common_limit:
5378+
# GROUP BY includes a null group, unlike count(distinct).
5379+
if (
5380+
most_common_results is not None
5381+
and num_distinct + bool(num_null) <= common_limit
5382+
):
53795383
# No need to run the query if it will just return the results in reverse order
53805384
least_common_results = None
53815385
else:

‎tests/test_analyze.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ def test_analyze_column_least_common_ties_with_nulls(fresh_db):
109109
}
110110

111111

112+
@pytest.mark.parametrize(
113+
"values,common_limit,most_common,expected_most,expected_least",
114+
(
115+
(["a", "a", "a", None], 1, True, [("a", 3)], [(None, 1)]),
116+
([None, None, None, "a"], 1, True, [(None, 3)], [("a", 1)]),
117+
(["a", "a", "a", "b"], 2, True, [("a", 3), ("b", 1)], None),
118+
(["a", "a", "a", None], 2, True, [("a", 3), (None, 1)], None),
119+
(["a", "a", "a", None], 2, False, None, [(None, 1), ("a", 3)]),
120+
(["a", "a", "a", None], 0, True, [], []),
121+
),
122+
)
123+
def test_analyze_column_only_skips_least_common_when_all_groups_are_returned(
124+
fresh_db, values, common_limit, most_common, expected_most, expected_least
125+
):
126+
fresh_db["t"].insert_all([{"c": value} for value in values])
127+
details = fresh_db["t"].analyze_column(
128+
"c", common_limit=common_limit, most_common=most_common
129+
)
130+
assert details.most_common == expected_most
131+
assert details.least_common == expected_least
132+
133+
112134
def test_analyze_column_does_not_render_null_as_the_string_none(fresh_db):
113135
# analyze-tables passes value_truncate=80, and the str() of a null
114136
# was indistinguishable from a column that really holds "None"

0 commit comments

Comments
 (0)