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

Fix query for suggested facets with column named value #2209

Open
wants to merge 3 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
8 changes: 4 additions & 4 deletions datasette/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ async def suggest(self):
if column in already_enabled:
continue
suggested_facet_sql = """
select {column} as value, count(*) as n from (
select {column}, count(*) from (
{sql}
) where value is not null
group by value
) where {column} is not null
group by {column}
limit {limit}
""".format(
column=escape_sqlite(column), sql=self.sql, limit=facet_size + 1
Expand All @@ -188,7 +188,7 @@ async def suggest(self):
1 < num_distinct_values < row_count
and num_distinct_values <= facet_size
# And at least one has n > 1
and any(r["n"] > 1 for r in distinct_values)
and any(r["count(*)"] > 1 for r in distinct_values)
):
suggested_facets.append(
{
Expand Down
33 changes: 17 additions & 16 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,26 +590,27 @@ def generate_sortable_rows(num):
complex_array text,
distinct_some_null,
n text,
value integer,
FOREIGN KEY ("_city_id") REFERENCES [facet_cities](id)
);
INSERT INTO facetable
(created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n)
(created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n, value)
VALUES
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Mission', '["tag1", "tag2"]', '[{"foo": "bar"}]', 'one', 'n1'),
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Dogpatch', '["tag1", "tag3"]', '[]', 'two', 'n2'),
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'SOMA', '[]', '[]', null, null),
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Tenderloin', '[]', '[]', null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 1, 'Bernal Heights', '[]', '[]', null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 1, 'Hayes Valley', '[]', '[]', null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 2, 'Hollywood', '[]', '[]', null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 2, 'Downtown', '[]', '[]', null, null),
("2019-01-16 08:00:00", 1, 1, 'CA', 2, 'Los Feliz', '[]', '[]', null, null),
("2019-01-16 08:00:00", 1, 1, 'CA', 2, 'Koreatown', '[]', '[]', null, null),
("2019-01-16 08:00:00", 1, 1, 'MI', 3, 'Downtown', '[]', '[]', null, null),
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Greektown', '[]', '[]', null, null),
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Corktown', '[]', '[]', null, null),
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Mexicantown', '[]', '[]', null, null),
("2019-01-17 08:00:00", 2, 0, 'MC', 4, 'Arcadia Planitia', '[]', '[]', null, null)
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Mission', '["tag1", "tag2"]', '[{"foo": "bar"}]', 'one', 'n1', 1),
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Dogpatch', '["tag1", "tag3"]', '[]', 'two', 'n2', 2),
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'SOMA', '[]', '[]', null, null, null),
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Tenderloin', '[]', '[]', null, null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 1, 'Bernal Heights', '[]', '[]', null, null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 1, 'Hayes Valley', '[]', '[]', null, null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 2, 'Hollywood', '[]', '[]', null, null, null),
("2019-01-15 08:00:00", 1, 1, 'CA', 2, 'Downtown', '[]', '[]', null, null, null),
("2019-01-16 08:00:00", 1, 1, 'CA', 2, 'Los Feliz', '[]', '[]', null, null, null),
("2019-01-16 08:00:00", 1, 1, 'CA', 2, 'Koreatown', '[]', '[]', null, null, null),
("2019-01-16 08:00:00", 1, 1, 'MI', 3, 'Downtown', '[]', '[]', null, null, null),
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Greektown', '[]', '[]', null, null, null),
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Corktown', '[]', '[]', null, null, null),
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Mexicantown', '[]', '[]', null, null, null),
("2019-01-17 08:00:00", 2, 0, 'MC', 4, 'Arcadia Planitia', '[]', '[]', null, null, null)
;

CREATE TABLE binary_data (
Expand Down
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ async def test_database_page(ds_client):
"complex_array",
"distinct_some_null",
"n",
"value",
],
"primary_keys": ["pk"],
"count": 15,
Expand Down
32 changes: 16 additions & 16 deletions tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
)

EXPECTED_TABLE_WITH_LABELS_CSV = """
pk,created,planet_int,on_earth,state,_city_id,_city_id_label,_neighborhood,tags,complex_array,distinct_some_null,n
1,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Mission,"[""tag1"", ""tag2""]","[{""foo"": ""bar""}]",one,n1
2,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Dogpatch,"[""tag1"", ""tag3""]",[],two,n2
3,2019-01-14 08:00:00,1,1,CA,1,San Francisco,SOMA,[],[],,
4,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Tenderloin,[],[],,
5,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Bernal Heights,[],[],,
6,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Hayes Valley,[],[],,
7,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Hollywood,[],[],,
8,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Downtown,[],[],,
9,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Los Feliz,[],[],,
10,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Koreatown,[],[],,
11,2019-01-16 08:00:00,1,1,MI,3,Detroit,Downtown,[],[],,
12,2019-01-17 08:00:00,1,1,MI,3,Detroit,Greektown,[],[],,
13,2019-01-17 08:00:00,1,1,MI,3,Detroit,Corktown,[],[],,
14,2019-01-17 08:00:00,1,1,MI,3,Detroit,Mexicantown,[],[],,
15,2019-01-17 08:00:00,2,0,MC,4,Memnonia,Arcadia Planitia,[],[],,
pk,created,planet_int,on_earth,state,_city_id,_city_id_label,_neighborhood,tags,complex_array,distinct_some_null,n,value
1,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Mission,"[""tag1"", ""tag2""]","[{""foo"": ""bar""}]",one,n1,1
2,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Dogpatch,"[""tag1"", ""tag3""]",[],two,n2,2
3,2019-01-14 08:00:00,1,1,CA,1,San Francisco,SOMA,[],[],,,
4,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Tenderloin,[],[],,,
5,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Bernal Heights,[],[],,,
6,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Hayes Valley,[],[],,,
7,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Hollywood,[],[],,,
8,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Downtown,[],[],,,
9,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Los Feliz,[],[],,,
10,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Koreatown,[],[],,,
11,2019-01-16 08:00:00,1,1,MI,3,Detroit,Downtown,[],[],,,
12,2019-01-17 08:00:00,1,1,MI,3,Detroit,Greektown,[],[],,,
13,2019-01-17 08:00:00,1,1,MI,3,Detroit,Corktown,[],[],,,
14,2019-01-17 08:00:00,1,1,MI,3,Detroit,Mexicantown,[],[],,,
15,2019-01-17 08:00:00,2,0,MC,4,Memnonia,Arcadia Planitia,[],[],,,
""".lstrip().replace(
"\n", "\r\n"
)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_internals_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ async def test_view_exists(db, view, expected):
"complex_array",
"distinct_some_null",
"n",
"value",
],
),
(
Expand Down Expand Up @@ -235,6 +236,15 @@ async def test_table_columns(db, table, expected):
is_pk=0,
hidden=0,
),
Column(
cid=11,
name="value",
type="integer",
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
],
),
(
Expand Down
6 changes: 4 additions & 2 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ async def test_hook_register_output_renderer_all_parameters(ds_client):
"complex_array",
"distinct_some_null",
"n",
"value",
],
"rows": [
"<sqlite3.Row object at 0xXXX>",
Expand All @@ -470,7 +471,7 @@ async def test_hook_register_output_renderer_all_parameters(ds_client):
"<sqlite3.Row object at 0xXXX>",
"<sqlite3.Row object at 0xXXX>",
],
"sql": "select pk, created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n from facetable order by pk limit 51",
"sql": "select pk, created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n, value from facetable order by pk limit 51",
"query_name": None,
"database": "fixtures",
"table": "facetable",
Expand Down Expand Up @@ -550,8 +551,9 @@ async def test_hook_register_output_renderer_can_render(ds_client):
"complex_array",
"distinct_some_null",
"n",
"value",
],
"sql": "select pk, created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n from facetable order by pk limit 51",
"sql": "select pk, created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n, value from facetable order by pk limit 51",
"query_name": None,
"database": "fixtures",
"table": "facetable",
Expand Down
8 changes: 8 additions & 0 deletions tests/test_table_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ async def test_table_filter_json_arraycontains(ds_client):
'[{"foo": "bar"}]',
"one",
"n1",
1,
],
[
2,
Expand All @@ -594,6 +595,7 @@ async def test_table_filter_json_arraycontains(ds_client):
"[]",
"two",
"n2",
2,
],
]

Expand All @@ -617,6 +619,7 @@ async def test_table_filter_json_arraynotcontains(ds_client):
'[{"foo": "bar"}]',
"one",
"n1",
1,
]
]

Expand All @@ -639,6 +642,7 @@ async def test_table_filter_extra_where(ds_client):
"[]",
"two",
"n2",
2,
]
] == response.json()["rows"]

Expand Down Expand Up @@ -1088,6 +1092,7 @@ async def test_expand_labels(ds_client):
"complex_array": "[]",
"distinct_some_null": "two",
"n": "n2",
"value": 2,
},
"13": {
"pk": 13,
Expand All @@ -1101,6 +1106,7 @@ async def test_expand_labels(ds_client):
"complex_array": "[]",
"distinct_some_null": None,
"n": None,
"value": None,
},
}

Expand Down Expand Up @@ -1304,6 +1310,7 @@ def test_generated_columns_are_visible_in_datasette():
"complex_array",
"distinct_some_null",
"n",
"value",
],
),
(
Expand Down Expand Up @@ -1332,6 +1339,7 @@ def test_generated_columns_are_visible_in_datasette():
"complex_array",
"distinct_some_null",
"n",
"value",
],
),
(
Expand Down