-
Notifications
You must be signed in to change notification settings - Fork 314
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
Add support for comparing jsonb @> with boolean values #399
Add support for comparing jsonb @> with boolean values #399
Conversation
This is tricky. 🤔 Do we have this issue with integers? |
@leandrocp I think may preferred solution to address this is to actually make |
@josevalim seems like the problem goes beyond boolean values:
See this example: https://dbfiddle.uk/?rdbms=postgres_14&fiddle=e55dda0ca5e9d25842d10a1f9a2294a6 I don't know the context of 96f70b5 but a possible solution is to just use the |
I think that operator is not optimizable, which defeats the purpose of the PR. :( So we have two options:
|
|
Wait. I think we documented when we added this syntax that it only works for jsonb. And I think that’s fair. Jsonb is much more efficient and I don’t think it is worth making everything slower for json. Then you can use fragments. |
Cool, so two places I found a mention on jsonb type are https://hexdocs.pm/ecto/Ecto.Query.API.html#json_extract_path/2-warning-indexes-on-postgresql and https://hexdocs.pm/ecto/embedded-schemas.html Wdyt about the following plan? ecto
ecto_sql
|
I don't think we need to update |
💚 💙 💜 💛 ❤️ |
The commit 96f70b5 may have introduced a breaking change when dealing with boolean values.
Given the schema
ecto_sql/test/ecto/adapters/postgres_test.exs
Lines 9 to 26 in c22115b
meta
stored as{"enabled": true}
on v3.7.2 one could write the following query:Note that you can't write
s.meta["enabled"] == true
as that would generate the following invalid query, either on v3.7.2 or v3.8.1:So far, so good.
On v3.8.1 the same query
s.meta["enabled"] == "true"
is now generated as:And turns out that query won't return the expected records, see a live example at https://dbfiddle.uk/?rdbms=postgres_13&fiddle=9987baf6c8972988319559cb0fafb655
In order to make it work the query should be
"meta"@>'{"enabled": true}'
(an actual boolean instead of a string) or using the#>
operator as before.Proposed solution
AFAIK boolean values are expected to be transformed to their text representation
ecto_sql/lib/ecto/adapters/postgres/connection.ex
Lines 758 to 759 in c22115b
@>
wouldn't work or would introduce a new behavior, right? So the proposed solution is to revert to the old query for bool expr and also deal withtrue/false
to makes.meta["enabled"] == true
work.I'm not sure if we should check for
right in ["TRUE", "true", "FALSE", "false"]
but I'm concerned because without such guard it will generate the query using@>
which would fail to return the expected results silently.