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)
|
types = tuple(r[0] for r in results.rows)
|
||||||
if types in (("array",), ("array", None)):
|
if types in (("array",), ("array", None)):
|
||||||
# Now sanity check that first 100 arrays contain only strings
|
# Now sanity check that first 100 arrays contain only strings
|
||||||
first_100 = await self.ds.execute(
|
first_100 = [
|
||||||
self.database,
|
v[0]
|
||||||
"select {column} from ({sql}) where {column} is not null limit 100".format(
|
for v in await self.ds.execute(
|
||||||
column=escape_sqlite(column), sql=self.sql
|
self.database,
|
||||||
),
|
"select {column} from ({sql}) where {column} is not null and json_array_length({column}) > 0 limit 100".format(
|
||||||
self.params,
|
column=escape_sqlite(column), sql=self.sql
|
||||||
truncate=False,
|
),
|
||||||
custom_time_limit=self.ds.config("facet_suggest_time_limit_ms"),
|
self.params,
|
||||||
log_sql_errors=False,
|
truncate=False,
|
||||||
)
|
custom_time_limit=self.ds.config(
|
||||||
if all(self._is_json_array_of_strings(r[0]) for r in first_100):
|
"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(
|
suggested_facets.append(
|
||||||
{
|
{
|
||||||
"name": column,
|
"name": column,
|
||||||
|
|
|
||||||
|
|
@ -215,6 +215,20 @@ async def test_array_facet_suggest(app_client):
|
||||||
] == suggestions
|
] == 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.asyncio
|
||||||
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
|
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
|
||||||
async def test_array_facet_results(app_client):
|
async def test_array_facet_results(app_client):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue