Don't suggest array facet if column is only [], closes #610

This commit is contained in:
Simon Willison 2019-11-01 14:45:59 -07:00
commit 7152e76eda
2 changed files with 32 additions and 11 deletions

View file

@ -293,17 +293,24 @@ class ArrayFacet(Facet):
types = tuple(r[0] for r in results.rows)
if types in (("array",), ("array", None)):
# Now sanity check that first 100 arrays contain only strings
first_100 = await self.ds.execute(
self.database,
"select {column} from ({sql}) where {column} is not null limit 100".format(
column=escape_sqlite(column), sql=self.sql
),
self.params,
truncate=False,
custom_time_limit=self.ds.config("facet_suggest_time_limit_ms"),
log_sql_errors=False,
)
if all(self._is_json_array_of_strings(r[0]) for r in first_100):
first_100 = [
v[0]
for v in await self.ds.execute(
self.database,
"select {column} from ({sql}) where {column} is not null and json_array_length({column}) > 0 limit 100".format(
column=escape_sqlite(column), sql=self.sql
),
self.params,
truncate=False,
custom_time_limit=self.ds.config(
"facet_suggest_time_limit_ms"
),
log_sql_errors=False,
)
]
if first_100 and all(
self._is_json_array_of_strings(r) for r in first_100
):
suggested_facets.append(
{
"name": column,

View file

@ -215,6 +215,20 @@ async def test_array_facet_suggest(app_client):
] == suggestions
@pytest.mark.asyncio
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
async def test_array_facet_suggest_not_if_all_empty_arrays(app_client):
facet = ArrayFacet(
app_client.ds,
MockRequest("http://localhost/"),
database="fixtures",
sql="select * from facetable where tags = '[]'",
table="facetable",
)
suggestions = await facet.suggest()
assert [] == suggestions
@pytest.mark.asyncio
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
async def test_array_facet_results(app_client):