Skip to content

Conversation

@rafiss
Copy link
Collaborator

@rafiss rafiss commented Nov 6, 2025

Previously, the subquery depth calculation in setSubqueryDepth only considered
direct entity dependencies when determining when a notJoin subquery should
execute. It tracked the maximum entity slot among the subquery's inputs but
ignored when non-entity variables (like table-id, index-id) were actually bound.
This caused notJoin subqueries to execute too early, before all their required
variables were available, leading to "unbound variable" errors.

This commit fixes the depth calculation by tracking which entity provides each
variable through the facts. When a subquery needs a non-entity variable, we now
find which entity binds that variable and use that entity's slot to determine
the correct execution depth. This ensures notJoin subqueries only execute after
all their input variables have been bound by the appropriate entities in the
join order.

The fix is needed for rules that pass non-entity variables to notJoin
predicates, particularly in constraint rename scenarios where variables may
be bound at different join depths. (At the time of this writing, we
don't yet have rules like this, but we soon will add one.)

informs #148341
Release note: None

@blathers-crl
Copy link

blathers-crl bot commented Nov 6, 2025

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss force-pushed the dsc-fix-subquery-depth branch 3 times, most recently from e536efa to 7aec23f Compare November 7, 2025 04:37
…ity variables

Previously, the subquery depth calculation in setSubqueryDepth only considered
direct entity dependencies when determining when a notJoin subquery should
execute. It tracked the maximum entity slot among the subquery's inputs but
ignored when non-entity variables (like table-id, index-id) were actually bound.
This caused notJoin subqueries to execute too early, before all their required
variables were available, leading to "unbound variable" errors.

This commit fixes the depth calculation by tracking which entity provides each
variable through the facts. When a subquery needs a non-entity variable, we now
find which entity binds that variable and use that entity's slot to determine
the correct execution depth. This ensures notJoin subqueries only execute after
all their input variables have been bound by the appropriate entities in the
join order.

The fix is needed for rules that pass non-entity variables to notJoin
predicates, particularly in constraint rename scenarios where variables may
be bound at different join depths. (At the time of this writing, we
don't yet have rules like this, but we soon will add one.)

Release note: None
@rafiss rafiss force-pushed the dsc-fix-subquery-depth branch from 7aec23f to 74753fd Compare November 7, 2025 05:03
@rafiss rafiss marked this pull request as ready for review November 7, 2025 05:03
@rafiss rafiss requested a review from a team as a code owner November 7, 2025 05:03
@rafiss rafiss requested a review from fqazi November 7, 2025 15:03
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafiss Great find!

:lgtm:

@fqazi reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rafiss)

@rafiss
Copy link
Collaborator Author

rafiss commented Nov 7, 2025

tftr!

bors r+

@craig
Copy link
Contributor

craig bot commented Nov 7, 2025

@craig craig bot merged commit 2c3b545 into cockroachdb:master Nov 7, 2025
25 of 26 checks passed
@rafiss rafiss deleted the dsc-fix-subquery-depth branch November 16, 2025 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants