Issues with sorting on `first` aggregate
Originally posted by @l00ker , moved to a support thread
33 Replies
Lets chat here 🙂
How are you invoking the action?
like running
Api.read
? Or via an api extension?There's a
code_interface
:
So I'm doing TVEpisodes.TVEpisode.published()
so strange that we're getting different queries
Try not loading the calculations as well
Lets try to trim it down to its bare minimum
maybe even remove
|> Ash.Query.filter(air_dates.status == :published)
With the
air_dates
action I'm just doing TVEpisodes.AirDate |> Ash.Query.for_read(:published_episodes) |> TVEpisodes.Api.read!()
So you want the SQL for that?first, are they still out of order?
No they are in order with the raw SQL
There are some with different statuses in the dataset I'm using so I had to check
Okay, so lets add bits back in until it breaks
start with the published filter
I just added the published filter back and it's not in order
🤔
Can you also load the aggregate?
How are you confirming they aren't in order?
I'm just running a query in psql to get the titles
can you add
Ash.Query.load(:latest_air_date)
to your query?
And confirm that the latest air dates its returning are in fact out of orderWhen I do the load, the dates seem to be descending by month but starting in 2011
It's like it's get the last 10 results
This dataset goes back a few years
🤔
So it could be in reverse order
Yeah
I just pushed something up to
main
could you try that and lemme know how it goes?So it's like this (latest_air_date only)
Okay. I'll let ya know probably sometime tomorrow. Thanks for your time Zach!
no problem 🙂 This is a pretty high priority bug in my book so just let me know once you've tried it out 🙂 I'll also try and reproduce it myself
10-4
okay, so I fixed the issue, but its not the most optimal behavior that it has right now
Luckily for you, when I find a better way to do it, you won't have to change anything aside from upgrading ash_postgres
although there is way to make it more optimized
they are semantically equivalent (which should tell you something about how I'll implement the optimization)
Going to mark this as solved, feel free to point out if it doesn't work (after updating to latest main again)
Just updated to ash_postgres main a few min before this post and ran the query. It's close, but still not right. 😦
I removed most of the attributes leaving just the id & title and removed the calculations etc. Here is the stripped down module right now with your changes:
😢
This is the first item of the result after running
TVEpisodes.TVEpisode.published()
. I loaded the :air_dates
to help illustrate the issue I'm seeing:
Notice the latest_air_date: ~U[2022-11-11 18:00:00Z]
. It matches the second air_date which has status: :scheduled
. So I think what's happening is that the query is ordering the air_dates
by start: :desc
and the first
aggregate is using the first air_date in the array to pick the episode id with the status being ignored in that case.I think that actually makes sense?
The
latest_air_date
doesn't have a filter on status == :published
Sorry for the late reply BTW. I got caught up with something and couldn't get away.
no worries 😄
Lemme try that.
Success! 🥳 That nailed it!
🎉
It definitely takes some getting used to building with these building blocks, and its a bummer that you had these issues (although good for Ash in general because they are fixed and I've also added some optimizations around them in the process)
but the nice thing about it is that these pieces are reusable and composable, in increasingly useful fashions
i.e
latest_air_date
can be loaded directly, or filtered on, that kind of thingYes. I can see that and that's why I hung in there!
noice 😄
I got to learn a few things here as well
Another thing you might do is change this:
That ought to perform better since it will use the aggregate that you're already loading
And then, to further enhance that, you could potentially do this:
At which point you can say
So you're saying to add that as an additional filter?
I'd replace your existing filter
with that calculation
Ahh
The more time I get under my belt with Ash I should start to see things better. I know I've already said it before, but thank you for hanging in there with me!
Its my pleasure 😄