Skip to content

test: reproduce parent() scoping bug in nested exists with calculation#717

Merged
zachdaniel merged 1 commit intomainfrom
fix/nested-exists-parent-scope
Mar 22, 2026
Merged

test: reproduce parent() scoping bug in nested exists with calculation#717
zachdaniel merged 1 commit intomainfrom
fix/nested-exists-parent-scope

Conversation

@barnabasJ
Copy link
Copy Markdown
Contributor

Bug Description

When a calculation on resource B uses parent() inside an unrelated exists(), and resource A references that calculation via exists(relationship_to_b, b_calculation), the parent() references incorrectly resolve to resource A instead of resource B.

Reproduction

Post has a calculation using an unrelated exists with parent():

calculate :has_matching_author_by_unrelated_exists,
          :boolean,
          expr(exists(Author, first_name == parent(title)))

When loaded directly on a Post, parent(title) correctly refers to Post.title.

Author has a calculation that uses the Post calculation via a relationship exists:

calculate :has_post_matching_author_via_nested_exists,
          :boolean,
          expr(exists(posts, has_matching_author_by_unrelated_exists))

When loaded on Author, the generated SQL resolves parent(title) to authors.title instead of posts.title:

-- Generated (incorrect): authors.title
SELECT ... exists(... ssa0."first_name"::text = a0."title"::text ...)
--                                               ^^^ a0 = authors table

-- Expected: posts.title
SELECT ... exists(... ssa0."first_name"::text = sp0."title"::text ...)
--                                               ^^^ sp0 = posts table

What's Included

  • Failing test demonstrating the bug
  • Passing sanity test confirming the calculation works correctly when loaded directly on the child resource
  • Test resource additions (calculation on Post and Author)

Impact

This prevents using exists(relationship, calculation) when the calculation itself contains parent() references inside an unrelated exists(). The workaround is to inline the calculation logic with explicit parent scoping or use a module calculation instead.

When a calculation on resource B uses `parent()` inside an unrelated
`exists()`, and resource A references that calculation via
`exists(relationship_to_b, b_calculation)`, the `parent()` references
incorrectly resolve to resource A instead of resource B.

Example: Post has `exists(Author, first_name == parent(title))` where
parent(title) should refer to Post.title. When Author uses
`exists(posts, that_post_calculation)`, the generated SQL produces
`authors.title` instead of `posts.title`.
@zachdaniel zachdaniel merged commit f270bbe into main Mar 22, 2026
64 of 67 checks passed
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.

2 participants