Nested aggregates and calculations

I have a calculation that looks like this conceptually:
some_documentation_created (calculation) refers to:
count_of_documented_skills (aggregate) filters by:
status_calculated (calculation) refers to:
latest_documentation_status (aggregate) sorts by:
timestamp (calculation)
all_documentation_approved (calculation) refers to:
count_of_skills (aggregate)
count_of_approved_skills (aggregate) filters by:
status_calculated (calculation) refers to:
latest_documentation_status (aggregate) sorts by:
timestamp (calculation)
some_documentation_created (calculation) refers to:
count_of_documented_skills (aggregate) filters by:
status_calculated (calculation) refers to:
latest_documentation_status (aggregate) sorts by:
timestamp (calculation)
all_documentation_approved (calculation) refers to:
count_of_skills (aggregate)
count_of_approved_skills (aggregate) filters by:
status_calculated (calculation) refers to:
latest_documentation_status (aggregate) sorts by:
timestamp (calculation)
Can a calculation refer to an aggregate that filters by a calculation (that refers to an aggregate)? If it should be possible, the issue is illustrated here: https://gist.github.com/ahey/2c3fcb0dd215f3a82620563e96344949
Gist
nested_aggregates.ex
GitHub Gist: instantly share code, notes, and snippets.
6 Replies
ZachDaniel
ZachDanielโ€ข2y ago
I believe it should work, yes. This will be pretty hard to debug without a reproduction. Would you be able to reproduce in ash_postgres tests?
Alan Heywood
Alan HeywoodOPโ€ข2y ago
Thanks, good to know it should work. I have submitted a PR that reproduces the issue here: https://github.com/ash-project/ash_postgres/pull/164
GitHub
test: add failing test to demonstrate potential bug by ahey ยท Pull ...
test complex calculation (AshPostgres.Test.ComplexCalculationsTest) test/complex_calculations_test.exs:5 ** (RuntimeError) Error while building reference: latest_documentation_status code: |> As...
ZachDaniel
ZachDanielโ€ข2y ago
man, this one was tough Had to put my brain in a blender for this one ๐Ÿ˜† Try main of ash_postgres when you get the chance ๐Ÿ™‚
Alan Heywood
Alan HeywoodOPโ€ข2y ago
OK this is brilliant, thank you! It gives me confidence that I can continue to build up calculations and aggregates and they will be composable.
ZachDaniel
ZachDanielโ€ข2y ago
Yep! The only main thing is that aggregates can't point at other aggregates currently but they can point at calculations, which can just be references to aggregates its pretty hacky but doable ๐Ÿ™‚
Alan Heywood
Alan HeywoodOPโ€ข2y ago
Ok good to know, and that there's a workaround ๐Ÿ˜€

Did you find this page helpful?