Conditions in expr (calculations)
I have a
calculation
(taken from the getting started guide):
The issue here is (it's the representative assigned to a ticket) that this will error if a representative has no tickets.
I tried to solve this with the if
function to no avail.
This returns 100.0
for the total_tickets == 0
case but 0.0
for the rest.
The resulting query also looks a bit weird (to me at least). It might be a problem that open_tickets
and total_tickets
themselves are aggregates.
Has anyone an idea?23 Replies
try this
that was quick š
Perhaps a bug, because the default value for a count aggregate should already be
0
This did not change anything unfortunately.
ah, well perhaps a good thing š
š¤
That's the output

the 0.0 ones should be around
0.75
I wonder...what if you cast them to floats first
type(value, :float)
when dividing themThat's the resulting query
We do some excessive type casting, still need to clean that up, but it shouldn't be problematic
looks a bit massive as I have the archival extension enabled
will try the casting
Yeah, casting before dividing may be the thing. I think perhaps a bug in ash_postgres, because we want it to beahave like elixir does, and so when dividing we should automatically cast the operators to floats
I think that will do the trick
that was it
Yeah, so its the difference between these two expressions:
Can you open an issue on ash_postgres?
You shouldn't have had to do that
will do (maybe you need to clarify the description as I have no idea what the root cause is)
thanks for this awesomely quick help!
is this an AshPostgres or Ash.Query issue?
Its an
AshPostgres
issue, that its treatment of the /
operator is not the same as Elixir
and to get the same treatment as elixir it has to first cast the left/right side to a float
or a decimalOK!
The goal of Ash expressions is to be data layer agnostic and to have the expressions behave the same in each context, a sort of adaptability layer
Created a ticket: https://github.com/ash-project/ash_postgres/issues/128
Will close here. Thanks again!
GitHub
Treatment of the
/
operator in expr Ā· Issue #128 Ā· ash-project/as...Describe the bug I have a calculation on a resource looking like this: calculations do calculate :percent_open, :float, expr(if(total_tickets > 0, open_tickets / total_tickets, 100.0)) end t...