Skip to content

Commit

Permalink
Don't execute facets/counts for _shape=array or object, closes #263
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 2, 2021
1 parent a866172 commit 82b938e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
17 changes: 10 additions & 7 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ async def data(
if is_view:
order_by = ""

nocount = request.args.get("_nocount")
nofacet = request.args.get("_nofacet")

if request.args.get("_shape") in ("array", "object"):
nocount = True
nofacet = True

# Ensure we don't drop anything with an empty value e.g. ?name__exact=
args = MultiParams(
urllib.parse.parse_qs(request.query_string, keep_blank_values=True)
Expand Down Expand Up @@ -697,11 +704,7 @@ async def data(
except KeyError:
pass

if (
count_sql
and filtered_table_rows_count is None
and not request.args.get("_nocount")
):
if count_sql and filtered_table_rows_count is None and not nocount:
try:
count_rows = list(await db.execute(count_sql, from_sql_params))
filtered_table_rows_count = count_rows[0][0]
Expand Down Expand Up @@ -735,7 +738,7 @@ async def data(
)
)

if not request.args.get("_nofacet"):
if not nofacet:
for facet in facet_instances:
(
instance_facet_results,
Expand Down Expand Up @@ -833,7 +836,7 @@ async def data(
self.ds.setting("suggest_facets")
and self.ds.setting("allow_facet")
and not _next
and not request.args.get("_nofacet")
and not nofacet
):
for facet in facet_instances:
suggested_facets.extend(await facet.suggest())
Expand Down
5 changes: 5 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,11 @@ def test_nocount(app_client, nocount, expected_count):
assert response.json["filtered_table_rows_count"] == expected_count


def test_nocount_nofacet_if_shape_is_object(app_client):
response = app_client.get("/fixtures/facetable.json?_trace=1&_shape=object")
assert "count(*)" not in response.text


def test_expand_labels(app_client):
response = app_client.get(
"/fixtures/facetable.json?_shape=object&_labels=1&_size=2"
Expand Down

0 comments on commit 82b938e

Please # to comment.