From 0091dfe3e5a3db94af8881038d3f1b8312bb857d Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 12 Feb 2020 22:36:42 -0800 Subject: [PATCH] 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. --- datasette/facets.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datasette/facets.py b/datasette/facets.py index a314faaf..18558754 100644 --- a/datasette/facets.py +++ b/datasette/facets.py @@ -198,7 +198,7 @@ class ColumnFacet(Facet): {sql} ) 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( 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 ( {sql} ) 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( col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1 ) @@ -457,7 +457,7 @@ class DateFacet(Facet): {sql} ) 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( col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1 )