More reliable tie-break ordering for facet results

I was seeing a weird bug where the order of results running tests
on my laptop was inconsistent, causing pytest failures even though
the order of tests in Travis CI was fine.

I think the fix is to explicitly state how facet ordering ties on
the count should be resolved.
This commit is contained in:
Simon Willison 2020-02-12 22:36:42 -08:00
commit 0091dfe3e5

View file

@ -198,7 +198,7 @@ class ColumnFacet(Facet):
{sql} {sql}
) )
where {col} is not null where {col} is not null
group by {col} order by count desc limit {limit} group by {col} order by count desc, value limit {limit}
""".format( """.format(
col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1 col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1
) )
@ -344,7 +344,7 @@ class ArrayFacet(Facet):
select j.value as value, count(*) as count from ( select j.value as value, count(*) as count from (
{sql} {sql}
) join json_each({col}) j ) join json_each({col}) j
group by j.value order by count desc limit {limit} group by j.value order by count desc, value limit {limit}
""".format( """.format(
col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1 col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1
) )
@ -457,7 +457,7 @@ class DateFacet(Facet):
{sql} {sql}
) )
where date({col}) is not null where date({col}) is not null
group by date({col}) order by count desc limit {limit} group by date({col}) order by count desc, value limit {limit}
""".format( """.format(
col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1 col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1
) )