Initial prototype of sqlite-utils analyze-tables, refs #207

This commit is contained in:
Simon Willison 2020-12-11 21:27:00 -08:00
commit ca2ccccac2
2 changed files with 97 additions and 0 deletions

View file

@ -51,6 +51,18 @@ except ImportError:
Column = namedtuple(
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk")
)
ColumnDetails = namedtuple(
"ColumnDetails",
(
"table",
"column",
"num_null",
"num_blank",
"num_distinct",
"most_common",
"least_common",
),
)
ForeignKey = namedtuple(
"ForeignKey", ("table", "column", "other_table", "other_column")
)
@ -1930,6 +1942,57 @@ class Table(Queryable):
)
return self
def analyze_column(self, column, common_limit=10):
db = self.db
table = self.name
num_null = db.execute(
"select count(*) from [{}] where [{}] is null".format(table, column)
).fetchone()[0]
num_blank = db.execute(
"select count(*) from [{}] where [{}] = ''".format(table, column)
).fetchone()[0]
num_distinct = db.execute(
"select count(distinct [{}]) from [{}]".format(column, table)
).fetchone()[0]
most_common = None
least_common = None
if num_distinct == 1:
value = db.execute(
"select [{}] from [{}] limit 1".format(column, table)
).fetchone()[0]
most_common = [value]
least_common = [value]
else:
most_common = [
(r[0], r[1])
for r in db.execute(
"select [{}], count(*) from [{}] group by [{}] order by count(*) desc limit {}".format(
column, table, column, common_limit
)
).fetchall()
]
if num_distinct <= common_limit:
# No need to run the query if it will just return the results in revers order
least_common = most_common[::-1]
else:
least_common = [
(r[0], r[1])
for r in db.execute(
"select [{}], count(*) from [{}] group by [{}] order by count(*) limit {}".format(
column, table, column, common_limit
)
).fetchall()
]
return ColumnDetails(
self.name,
column,
num_null,
num_blank,
num_distinct,
most_common,
least_common,
)
class View(Queryable):
def exists(self):