Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array facet: don't materialize unnecessary columns #2008

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions datasette/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
24 changes: 18 additions & 6 deletions tests/test_facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down