[MODEUS-219] Fix udp_report_releases to guarantee sorted array ordering#214
Merged
Conversation
|
slaemmer
approved these changes
Mar 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



https://folio-org.atlassian.net/browse/MODEUS-219
Purpose
The
udp_report_releasesSQL function returns thereportReleasesarray in unspecified order. The function usesjsonb_agg(DISTINCT ...)with anORDER BYclause at the query level, but since the query returns a single aggregated row, theORDER BYhas no effect on the array element ordering.The PostgreSQL documentation states:
The documentation on aggregate expressions further clarifies that
DISTINCTalone does not guarantee ordering - it only ensures each distinct value is processed once. To control ordering, anORDER BYclause must be placed within the aggregate call itself:The existing tests in
SQLTriggersITassert sorted order and currently pass, but only by coincidence - PostgreSQL'sDISTINCTimplementation happens to use sort-based deduplication, which is not a documented guarantee.Approach
Refactor
udp_report_releasesto use a subquery pattern, matching the approach already used byudp_report_typesandudp_report_errorsin the same file. This movesDISTINCTandORDER BYinto a subquery so that sorting occurs before aggregation, making the ordering explicit and reliable.An alternative would be to use the inline
ORDER BYwithin the aggregate call:The subquery approach was chosen for consistency with the other functions in the same file.
Related
folio-org/ui-erm-usage#609