mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Don't suggest array facet if column is only [], closes #610
This commit is contained in:
parent
ba5414f16b
commit
7152e76eda
2 changed files with 32 additions and 11 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue