From d086c4604081611023fc971b108ba8601accf38d Mon Sep 17 00:00:00 2001 From: Colin Dellow Date: Sat, 28 Jan 2023 23:51:15 -0500 Subject: [PATCH] faster array facet and more correct. progress on #34, see also https://github.com/simonw/datasette/pull/2008 --- datasette_ui_extras/facet_patches.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/datasette_ui_extras/facet_patches.py b/datasette_ui_extras/facet_patches.py index 72c88fe..9bec5a8 100644 --- a/datasette_ui_extras/facet_patches.py +++ b/datasette_ui_extras/facet_patches.py @@ -22,23 +22,17 @@ async def ArrayFacet_facet_results(self): # https://github.com/simonw/datasette/issues/448 facet_sql = """ with inner as ({sql}), - deduped_array_items as ( - select - distinct j.value, - inner.{col} - from - json_each([inner].{col}) j - join inner - ) + with_ids as (select row_number() over () as row_number, {col} as array from inner), + array_items as (select row_number, each.value from json_each(with_ids.array) each, with_ids) select value as value, - count(*) as count + count(distinct row_number) as count from - deduped_array_items + array_items group by value order by - count(*) desc, value limit {limit} + count(distinct row_number) desc, value limit {limit} """.format( col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1 ) @@ -94,4 +88,5 @@ async def ArrayFacet_facet_results(self): return facet_results, facets_timed_out + facets.ArrayFacet.facet_results = ArrayFacet_facet_results