fix: apply each combination part to the whole of what precedes it - #244
Merged
zachdaniel merged 1 commit intoAug 3, 2026
Conversation
`Ash.Query.combination_of/2` takes an ordered list of parts, and each part applies to the result of everything before it. There is no precedence between the combination types — the order in the list is the order of application. `combination_of/4` appended each operation onto the accumulated query, which renders as one flat chain of set operations. SQL then applies its own precedence to that chain, and `INTERSECT` binds tighter than `UNION` and `EXCEPT`, so a combination spanning more than one precedence level was regrouped: given `base`, `union`, `intersect`, the intersect was applied to the union's right operand rather than to the running result. The effect was a query whose meaning depended on its data layer. `Ash.DataLayer.Ets` applies the parts in the order given, so the two answered differently — for one three-part combination, `["alpha", "beta", "gamma"]` here against `["alpha", "gamma"]` there. Nest what has accumulated before applying the next part, so each operation takes everything preceding it as its left operand. A part already wrapped as a subquery is left alone, so this adds one level of nesting per part rather than one per part plus a trailing one. Verified against ash_postgres built with `ASH_SQL_VERSION=local`: its combination suite (18 tests) and full suite (828) stay green, and a three-part case returns the order given only with this change.
7 tasks
Contributor
|
🚀 Thank you for your contribution! 🚀 |
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.
Contributor checklist
Leave anything that you believe does not apply unchecked.
Fixes #243.
Ash.Query.combination_of/2takes an ordered list of parts, and each part applies to the resultof everything before it. There is no precedence between the combination types — the order in the
list is the order of application.
combination_of/4appends each operation onto the accumulated query, which renders as one flatchain of set operations. SQL then applies its own precedence to that chain, and
INTERSECTbindstighter than
UNION/EXCEPT, so a combination spanning more than one precedence level getsregrouped. Given
base,union,intersect, the intersect is applied to the union's rightoperand rather than to the running result:
The consequence is a query whose meaning depends on its data layer.
Ash.DataLayer.Etsappliesthe parts in the order given, so on identical data the two answer differently:
AshPostgres.DataLayer["alpha", "beta", "gamma"]Ash.DataLayer.Ets["alpha", "gamma"]The change
Nest what has accumulated before applying the next part, so each operation takes everything
preceding it as its left operand:
A part already wrapped as a subquery is left alone, so this adds one level of nesting per part
rather than one per part plus a trailing one.
Verification
ash_sql's own suite has no database and no combination coverage, so this was verified againstash_postgresbuilt withASH_SQL_VERSION=local, onash_postgresfc165f3e:test/combination_test.exs— 18 passed, unchanged. All 18 stop at two parts, which is why thishas not surfaced: two-part combinations always agree, and
union/exceptshare a precedenceand are left-associative in SQL, so those agree too. It needs three or more parts spanning more
than one precedence level.
["alpha", "gamma"]with it,["alpha", "beta", "gamma"]without.ash_sql0.6.6, withash_sqlmain, and withthis branch —
874/885in all three, and the failing set is byte-identical across them. Those11 failures are all in
AshPostgres.AggregateReadActionTest, reproduce against releasedash_sql, and are unrelated to this change.Tests
The regression test is in ash_postgres#811, following the pattern used for #239 and
ash_postgres#797 —
ash_sqlhas no database of its own, and all the combination tests live overthere. It fails against
ash_sql0.6.6 and passes with this branch, so it doubles as thereproduction.