-
-
Notifications
You must be signed in to change notification settings - Fork 711
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
Filters fail to work correctly against calculated numeric columns returned by SQL views because type affinity rules do not apply #1671
Comments
Oh this is fascinating! I replicated the bug (thanks for the steps to reproduce) and it looks like this is down to the following: Against views, This doesn't happen against tables because of SQLite's type affinity mechanism, which handles the type conversion automatically. |
Relevant section of the SQLite documentation: 3.2. Affinity Of Expressions:
In your example, Then 4.2. Type Conversions Prior To Comparison fills in the rest:
|
I wonder if this will be a problem with generated columns, or with SQLite strict tables? My 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. Generated columns have a type, so my hunch is they will continue to work fine too. |
Thinking about options for fixing this... The following query works fine: select * from test_view where cast(has_expired as text) = '1' I 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: If someone clicks on "View and edit SQL" from a filtered table page I don't want them to have to wonder why that But... for querying views, the So one fix would be to get the SQL generating logic to use casts like this any time it is operating against a view. An 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. The trick I was exploring here might be able to help with that: |
The alternative to using This feels a bit neater to me, but I still then need to solve the problem of how to identify the "type" of a column that I want to use in a query. |
No, I think I need to use Even the details from the |
Note that Datasette does already have special logic to convert parameters to integers for numeric comparisons like datasette/datasette/filters.py Lines 203 to 212 in c4c9dbd
Though... it looks like there's a bug in that? It doesn't account for |
Also made me realize that this query: select * from sortable where sortable > :p0 Only works here thanks to the column affinity thing kicking in too: https://latest.datasette.io/fixtures?sql=select+*+from+sortable+where+sortable+%3E+%3Ap0&p0=70 |
I found a strange behavior, and I'm not sure if it's related to views and boolean values perhaps, or if there's something else weird going on here, but I'll provide an example that may help show what I'm seeing happen.
Thanks again and let me know if you want me to provide anything else!
The text was updated successfully, but these errors were encountered: