fix: don't drop the tail of a multi-hop exists path with an unanchored predicate - #259
Merged
zachdaniel merged 1 commit intoSep 2, 2026
Conversation
…d predicate Joins inside an exists subquery are derived from the refs in the predicate and are left joins. A predicate with no refs (`exists(a.bs, true)`, or an unfiltered exists aggregate over a multi-hop path) never joined the remaining path, so the exists degraded to "the first relationship exists" and wrongly returned true with zero related rows. A null-satisfiable predicate (e.g. `exists(a.bs, is_nil(name))`) was satisfied by the null-extended rows of the left joins. Anchor the subquery filter with `not is_nil(primary_key)` at every hop of the remaining path, which is a no-op for real rows and excludes both failure modes. Anchoring only the last hop would not be enough: a no_attributes? relationship joins with `on: true`, so its rows join even when an earlier hop of the chain is null-extended. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
The bug
An
existsaggregate orexists/2expression over a multi-hop relationship path wrongly returnstruewhen there are zero matching related rows, whenever the predicate contains no refs:existsaggregates:exists :has_policy?, [:service_line, :policies]expr(exists(service_line.policies, true))Joins inside the exists subquery are derived from the refs in the predicate (and are left joins). A predicate with no refs never joins the remaining path, so the query degrades to "the first relationship exists":
Relatedly, a null-satisfiable predicate (e.g.
exists(bucket.items, is_nil(name))) is satisfied by the null-extended rows of the left joins.The fix
Anchor the subquery filter with
not is_nil(primary_key)at every hop of the remaining path. The anchors are a no-op for real rows, force the joins to materialize, and exclude null-extended rows:Anchoring only the last hop would not be enough:
no_attributes?relationships join withon: true, so their rows join even when an earlier hop of the chain is null-extended.Tests
Regression tests live in the companion ash_postgres PR (ash-project/ash_postgres#843), since exercising this end-to-end needs a real data layer: zero-row multi-hop exists, three-hop chains broken at each level, a
no_attributes?hop after a broken middle hop, and predicate scoping. The full ash_postgres suite passes against this branch (ASH_SQL_VERSION=local), including all pre-existing exists/aggregate tests.🤖 Generated with Claude Code