Calculation with arguments used in another calculation for sorting
I have a calculation that uses a postgres function to return a json object:
It produces a number of fields like
total_hours
etc that I'd like to sort on.
I know I need to extract the field with another calculation and sort on that, but I'm getting a little hung up on how to handle calling a calculation with args inside another calculation with args. It's also a fairly expensive operation, so if possible I'd like to somehow tell it to use the already-loaded calculation and not require the arguments at all.
Any pointers? :thinkies:12 Replies
FWIW, I load it with this action:
And this code_interface:
👋
okay, so there isn't currently anything to reuse a calculation that is being selected, unfortunately
so it will depend on how postgres does its work TBH
In fact, this might be more than problematic for your case, you'll have to test it
because each thing that depends on a single field might need to rerun the calculation. I'd hope that postgres was smart enough not to do that, but honestly they probably aren't?
This will likely need to be optimized at the
ash_postgres
layer, but it is complicated.
Lemme show you what it would look like in ash-land, and then we can see if its necessary to optimize
OK, how do I use that calculation to sort, because sort can't take args, right? 🤔
yep, it can 🙂
Oooh, OK. Does that work w/ ad-hoc calcs as well?
yeah, I'm pretty sure you can say
sort({Calculation.new(....), {:asc, %{arg1: :value}}})
maybe
try it out 😆🤯
Almost there, running into trouble making this access dynamic:
Is there a different syntax to make that dynamic?
(it's a json field, so string-keyed)
^arg(:field)
on that caseStill same error with
[^arg(:field)]
.oh
right
[]
expects static values
you might need to use a fragmentGot it! 🚀
🔥