{"html_url": "https://github.com/simonw/datasette/issues/1671#issuecomment-1074478299", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1671", "id": 1074478299, "node_id": "IC_kwDOBm6k_c5ACzzb", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-03-21T22:20:26Z", "updated_at": "2022-03-21T22:20:26Z", "author_association": "OWNER", "body": "Thinking about options for fixing this...\r\n\r\nThe following query works fine:\r\n```sql\r\nselect * from test_view where cast(has_expired as text) = '1'\r\n```\r\nI don't want to start using this for every query, because one of the goals of Datasette is to help people who are learning SQL:\r\n- #1613\r\n\r\nIf someone clicks on \"View and edit SQL\" from a filtered table page I don't want them to have to wonder why that `cast` is there.\r\n\r\nBut... for querying views, the `cast` turns out to be necessary.\r\n\r\nSo one fix would be to get the SQL generating logic to use casts like this any time it is operating against a view.\r\n\r\nAn even better fix would be to detect which columns in a view come from a table and which ones might not, and only use casts for the columns that aren't definitely from a table.\r\n\r\nThe trick I was exploring here might be able to help with that:\r\n- #1293 ", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1174655187, "label": "Filters fail to work correctly against calculated numeric columns returned by SQL views because type affinity rules do not apply"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1671#issuecomment-1074470568", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1671", "id": 1074470568, "node_id": "IC_kwDOBm6k_c5ACx6o", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-03-21T22:11:14Z", "updated_at": "2022-03-21T22:12:49Z", "author_association": "OWNER", "body": "I wonder if this will be a problem with generated columns, or with SQLite strict tables?\r\n\r\nMy hunch is that strict tables will continue to work without any changes, because https://www.sqlite.org/stricttables.html says nothing about their impact on comparison operations. I should test this to make absolutely sure though.\r\n\r\nGenerated columns have a type, so my hunch is they will continue to work fine too.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1174655187, "label": "Filters fail to work correctly against calculated numeric columns returned by SQL views because type affinity rules do not apply"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1671#issuecomment-1074468450", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1671", "id": 1074468450, "node_id": "IC_kwDOBm6k_c5ACxZi", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-03-21T22:08:35Z", "updated_at": "2022-03-21T22:10:00Z", "author_association": "OWNER", "body": "Relevant section of the SQLite documentation: [3.2. Affinity Of Expressions](https://www.sqlite.org/datatype3.html#affinity_of_expressions):\r\n\r\n> When an expression is a simple reference to a column of a real table (not a [VIEW](https://www.sqlite.org/lang_createview.html) or subquery) then the expression has the same affinity as the table column.\r\n\r\nIn your example, `has_expired` is no longer a simple reference to a column of a real table, hence the bug.\r\n\r\nThen [4.2. Type Conversions Prior To Comparison](https://www.sqlite.org/datatype3.html#type_conversions_prior_to_comparison) fills in the rest:\r\n\r\n> SQLite may attempt to convert values between the storage classes INTEGER, REAL, and/or TEXT before performing a comparison. Whether or not any conversions are attempted before the comparison takes place depends on the type affinity of the operands. ", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1174655187, "label": "Filters fail to work correctly against calculated numeric columns returned by SQL views because type affinity rules do not apply"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1671#issuecomment-1074465536", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1671", "id": 1074465536, "node_id": "IC_kwDOBm6k_c5ACwsA", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-03-21T22:04:31Z", "updated_at": "2022-03-21T22:04:31Z", "author_association": "OWNER", "body": "Oh this is fascinating! I replicated the bug (thanks for the steps to reproduce) and it looks like this is down to the following:\r\n\r\n\"image\"\r\n\r\nAgainst views, `where has_expired = 1` returns different results from `where has_expired = '1'`\r\n\r\nThis doesn't happen against tables because of SQLite's [type affinity](https://www.sqlite.org/datatype3.html#type_affinity) mechanism, which handles the type conversion automatically.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1174655187, "label": "Filters fail to work correctly against calculated numeric columns returned by SQL views because type affinity rules do not apply"}, "performed_via_github_app": null}