diff --git a/datasette/facets.py b/datasette/facets.py index 7fb0c68b45..6daf7780c3 100644 --- a/datasette/facets.py +++ b/datasette/facets.py @@ -380,23 +380,17 @@ async def 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.* - 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 ) diff --git a/tests/test_facets.py b/tests/test_facets.py index d264f534b5..ea7c8aa81e 100644 --- a/tests/test_facets.py +++ b/tests/test_facets.py @@ -408,21 +408,33 @@ async def test_array_facet_results(ds_client): async def test_array_facet_handle_duplicate_tags(): ds = Datasette([], memory=True) db = ds.add_database(Database(ds, memory_name="test_array_facet")) - await db.execute_write("create table otters(name text, tags text)") - for name, tags in ( - ("Charles", ["friendly", "cunning", "friendly"]), - ("Shaun", ["cunning", "empathetic", "friendly"]), - ("Tracy", ["empathetic", "eager"]), + await db.execute_write("create table otters(tags text)") + for tags in ( + ["friendly", "cunning", "friendly"], + ["cunning", "empathetic", "friendly"], + ["empathetic", "eager"], + ["placid"], + ["placid"], + ["placid"], ): await db.execute_write( - "insert into otters (name, tags) values (?, ?)", [name, json.dumps(tags)] + "insert into otters (tags) values (?)", [json.dumps(tags)] ) response = await ds.client.get("/test_array_facet/otters.json?_facet_array=tags") + + print(response.json()["facet_results"]["tags"]) assert response.json()["facet_results"]["tags"] == { "name": "tags", "type": "array", "results": [ + { + "value": "placid", + "label": "placid", + "count": 3, + "toggle_url": "http://localhost/test_array_facet/otters.json?_facet_array=tags&tags__arraycontains=placid", + "selected": False, + }, { "value": "cunning", "label": "cunning",