Generated filter expression is incorrect - BUG
Hello, I think I found a bug in Ash's query builder.
I have the following filter expression in my resource action:
As you can see, this expression basically says "If shared? is everyone, return it, or, if shared? is organization and organization_id matches, return it, or, if shared? is school and school_id matches, return it".
I was expecting the SQL generated from it being something like this:
But what Ash.Query generated was:
Note: I passed
nil
as arg(:organization_id)
and arg(:school_id)
in this case.
Which is incorrect since when shared? is everyone, it will only return it if organization_id matches.
What I believe happened here is that Ash.Query
ignored the parenthesis in this part of the expression (shared? == :organization and organization_id == ^arg(:organization_id))
since if you remove it, then the generated where expression makes sense.11 Replies
This looks like an expression optimization bug
Specifically we're lifting up a value out of an
or ==
into an in
incorrectlyIs this a Ash or AshPostgres issue, do you want me to create a issue for this in one of these repos?
Its in
Ash
I'm looking into it at the moment actually (pretty serious bug that should be fixed ASAP)
Just released a new version with the fix
2.13.4
Thanks @Zach Daniel I just tested it and it is working great. Sorry to interrupt your well deserved vacation with this 😅
I upgradet to main this morning and I'm seeing a really weird query for an update action
This is the policy for the action
It loggs this select query first:
afterwards, it starts the transaction for the update
Can you show me what it was doing before/highlight the part that is strange?
The last
OR
circumvents the other where clauses
after the update, the action was accessible by users that didn't match the customer id🥶 it was basically a typo
I'm away from my computer
will fix ASAP
@Blibs you should probably update again
@barnabasj thanks for the heads up, will do.
That actually made me wonder, is there some way to disable that query optimizer from Ash.Query? I mean, correct me if I'm wrong, but AFAIK, when using ecto, the query that I write will be exactly the query that ecto will output, there is no optimization from their part. I kinda like that behavior since it makes the developer responsibility to make the best query possible.
The query optimizer we have is generally very simple, specifically for turning things like (a == 1 or a == 2) into (a in [1, 2])
Beyond that it doesn’t do much