Skip to content

fix: apply each combination part to the whole of what precedes it - #244

Merged
zachdaniel merged 1 commit into
ash-project:mainfrom
matt-beanland:fix/combination-order-not-sql-precedence
Aug 3, 2026
Merged

fix: apply each combination part to the whole of what precedes it#244
zachdaniel merged 1 commit into
ash-project:mainfrom
matt-beanland:fix/combination-order-not-sql-precedence

Conversation

@matt-beanland

Copy link
Copy Markdown
Contributor

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Fixes #243.

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 appends 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/EXCEPT, so a combination spanning more than one precedence level gets
regrouped. Given base, union, intersect, the intersect is applied to the union's right
operand rather than to the running result:

SELECT ... FROM (SELECT ... WHERE region = $1) AS ss0
UNION (SELECT ... WHERE name = $2)
INTERSECT (SELECT ... WHERE price = $3)

The consequence is a query whose meaning depends on its data layer. Ash.DataLayer.Ets applies
the parts in the order given, so on identical data the two answer differently:

data layer result
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:

query =
  case query do
    %Ecto.SubQuery{} -> query
    query -> subquery(query)
  end

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 against
ash_postgres built with ASH_SQL_VERSION=local, on ash_postgres fc165f3e:

  • test/combination_test.exs — 18 passed, unchanged. All 18 stop at two parts, which is why this
    has not surfaced: two-part combinations always agree, and union/except share a precedence
    and are left-associative in SQL, so those agree too. It needs three or more parts spanning more
    than one precedence level.
  • The three-part case added in ash_postgres#811 returns the order as given only with this change —
    ["alpha", "gamma"] with it, ["alpha", "beta", "gamma"] without.
  • Full suite: identical results with the released ash_sql 0.6.6, with ash_sql main, and with
    this branch — 874/885 in all three, and the failing set is byte-identical across them. Those
    11 failures are all in AshPostgres.AggregateReadActionTest, reproduce against released
    ash_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_sql has no database of its own, and all the combination tests live over
there. It fails against ash_sql 0.6.6 and passes with this branch, so it doubles as the
reproduction.

`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.
@zachdaniel
zachdaniel merged commit f0a1759 into ash-project:main Aug 3, 2026
@zachdaniel

Copy link
Copy Markdown
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

combination_of does not honour the order of Ash's combination parts - SQL precedence regroups them

2 participants