{"html_url": "https://github.com/simonw/datasette/issues/448#issuecomment-969557008", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/448", "id": 969557008, "node_id": "IC_kwDOBm6k_c45ykQQ", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-11-16T00:56:09Z", "updated_at": "2021-11-16T00:59:59Z", "author_association": "OWNER", "body": "This looks like it might work:\r\n```sql\r\nwith inner as (\r\n select\r\n *\r\n from\r\n ads_with_targets\r\n where\r\n :p0 in (\r\n select\r\n value\r\n from\r\n json_each([ads_with_targets].[target_names])\r\n )\r\n),\r\ndeduped_array_items as (\r\n select\r\n distinct j.value,\r\n inner.*\r\n from\r\n json_each([inner].[target_names]) j\r\n join inner\r\n)\r\nselect\r\n value,\r\n count(*)\r\nfrom\r\n deduped_array_items\r\ngroup by\r\n value\r\norder by\r\n count(*) desc\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 440222719, "label": "_facet_array should work against views"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/448#issuecomment-969557972", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/448", "id": 969557972, "node_id": "IC_kwDOBm6k_c45ykfU", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-11-16T00:56:58Z", "updated_at": "2021-11-16T00:56:58Z", "author_association": "OWNER", "body": "It uses a CTE which were introduced in SQLite 3.8 - and AWS Lambda Python 3.9 still provides 3.7 - but I've checked and I can use `pysqlite3-binary` to work around that there so I'm OK relying on CTEs for this.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 440222719, "label": "_facet_array should work against views"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/448#issuecomment-969572281", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/448", "id": 969572281, "node_id": "IC_kwDOBm6k_c45yn-5", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-11-16T01:05:11Z", "updated_at": "2021-11-16T01:05:11Z", "author_association": "OWNER", "body": "I tried this and it seems to work correctly:\r\n```python\r\n for source_and_config in self.get_configs():\r\n config = source_and_config[\"config\"]\r\n source = source_and_config[\"source\"]\r\n column = config.get(\"column\") or config[\"simple\"]\r\n facet_sql = \"\"\"\r\n with inner as ({sql}),\r\n deduped_array_items as (\r\n select\r\n distinct j.value,\r\n inner.*\r\n from\r\n json_each([inner].{col}) j\r\n join inner\r\n )\r\n select\r\n value as value,\r\n count(*) as count\r\n from\r\n deduped_array_items\r\n group by\r\n value\r\n order by\r\n count(*) desc limit {limit}\r\n \"\"\".format(\r\n col=escape_sqlite(column), sql=self.sql, limit=facet_size + 1\r\n )\r\n```\r\nThe queries are _very_ slow though - I had to bump up to 2s time limit even against only a view returning 3,499 rows.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 440222719, "label": "_facet_array should work against views"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/448#issuecomment-969578466", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/448", "id": 969578466, "node_id": "IC_kwDOBm6k_c45ypfi", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-11-16T01:08:29Z", "updated_at": "2021-11-16T01:08:29Z", "author_association": "OWNER", "body": "Actually with the cache warmed up it looks like the facet query is taking 150ms which is good enough.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 440222719, "label": "_facet_array should work against views"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/448#issuecomment-969582098", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/448", "id": 969582098, "node_id": "IC_kwDOBm6k_c45yqYS", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-11-16T01:10:28Z", "updated_at": "2021-11-16T01:10:28Z", "author_association": "OWNER", "body": "Also note that this demo data is using a SQL view to create the JSON arrays - the view is defined as such:\r\n\r\n```sql\r\nCREATE VIEW ads_with_targets as\r\nselect\r\n ads.*,\r\n json_group_array(targets.name) as target_names\r\nfrom\r\n ads\r\n join ad_targets on ad_targets.ad_id = ads.id\r\n join targets on ad_targets.target_id = targets.id\r\ngroup by\r\n ad_targets.ad_id;\r\n```\r\nSo running JSON faceting on top of that view is a pretty big ask!", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 440222719, "label": "_facet_array should work against views"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/448#issuecomment-969621662", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/448", "id": 969621662, "node_id": "IC_kwDOBm6k_c45y0Ce", "user": {"value": 9599, "label": "simonw"}, "created_at": "2021-11-16T01:32:04Z", "updated_at": "2021-11-16T01:32:04Z", "author_association": "OWNER", "body": "Tests are failing and I think it's because the facets come back in different orders, need a tie-breaker. https://github.com/simonw/datasette/runs/4219325197?check_suite_focus=true", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 440222719, "label": "_facet_array should work against views"}, "performed_via_github_app": null}