Improvements to most/least common values

* Record total_rows for each column
* Record (value, count) if there is just a single distinct value
* Do not calculate most/least common if all values are distinct
* Calculate table count once per table, not once per column
This commit is contained in:
Simon Willison 2020-12-11 21:41:07 -08:00
commit d4b8d9e7a5
2 changed files with 11 additions and 5 deletions

View file

@ -56,6 +56,7 @@ ColumnDetails = namedtuple(
(
"table",
"column",
"total_rows",
"num_null",
"num_blank",
"num_distinct",
@ -1942,9 +1943,11 @@ class Table(Queryable):
)
return self
def analyze_column(self, column, common_limit=10):
def analyze_column(self, column, common_limit=10, total_rows=None):
db = self.db
table = self.name
if total_rows is None:
total_rows = db[table].count
num_null = db.execute(
"select count(*) from [{}] where [{}] is null".format(table, column)
).fetchone()[0]
@ -1960,8 +1963,8 @@ class Table(Queryable):
value = db.execute(
"select [{}] from [{}] limit 1".format(column, table)
).fetchone()[0]
most_common = [value]
else:
most_common = [(value, total_rows)]
elif num_distinct != total_rows:
most_common = [
(r[0], r[1])
for r in db.execute(
@ -1985,6 +1988,7 @@ class Table(Queryable):
return ColumnDetails(
self.name,
column,
total_rows,
num_null,
num_blank,
num_distinct,