Repository navigation
Conversation
Alena0704
marked this pull request as ready for review
October 30, 2026 19:43
yjhjstz
reviewed
Oct 30, 2026
Keep a Result node when replacing its child's target list would drop a SubPlan reference after Motion slices have been allocated. Build the proposed projection on copies so rejecting the fold leaves the child expressions intact. Add regression coverage for EXISTS and NOT EXISTS with nullable outer joins, LIMIT/OFFSET, and multiple unused SubPlans.
An outer Apply introduced below grouping for an existential or quantified subquery can have a multirow inner input. If decorrelation fails, implementing it as a scalar SubPlan raises a spurious cardinality violation before the grouping can run. Only generate that scalar correlated alternative when the inner input is proven to return at most one row. Keep the correlated alternatives generated from the original subquery. Add regression coverage for nullable FULL JOIN correlations, LIMIT/OFFSET, quantified subqueries, and genuine scalar cardinality errors.
Alena0704
force-pushed
the
fix/orca-exists-subqueries
branch
from
October 31, 2026 05:38
ffc7321 to
c88b7ad
Compare
Alena0704
requested review from
leborchuk and
yjhjstz
and removed request for
leborchuk
October 31, 2026 06:02
yjhjstz
reviewed
Nov 2, 2026
| -> Seq Scan on motion_r r | ||
| SubPlan 2 | ||
| -> Result | ||
| -> Result |
Member
There was a problem hiding this comment.
why still have two Result node ?
yjhjstz
reviewed
Nov 2, 2026
| -> Seq Scan on card_l l | ||
| SubPlan 2 | ||
| -> Result | ||
| -> Result |
Member
There was a problem hiding this comment.
remove_redundant_results_mutator does not work ?
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.
Fixes #2221
What does this PR do?
Fix two planning errors for correlated EXISTS/NOT EXISTS queries containing LIMIT/OFFSET and a nested scalar subquery:
Example 1: missing MotionState
This is the minimal reproduction from #2221:
The only value of
y.idis 1, soy.id IS NULLis false and the query must return zero rows.Before the fix, with optimizer=on:
After the fix, with optimizer=on, and with optimizer=off on either build:
Before the fix — EXPLAIN (COSTS OFF), optimizer=on
After the fix — EXPLAIN (COSTS OFF), optimizer=on
When removing a Result node, the planner loses the reference to SubPlan 1, which computes count(*).
The executor still expects this subquery’s Motion slices, but cannot find them in the execution tree.
The fix keeps the Result node and SubPlan 1, so the query runs successfully and returns the expected zero rows.
Example 2: a scalar cardinality error inside EXISTS
Run this example in a fresh connection:
For
r.id = 10,r.k = 7matches two rows incard_s.This is valid: EXISTS tests whether any row exists. The nested
SELECT count(*) FROM card_sisa separate scalar subquery returning exactly one row; it does not make the surrounding EXISTS input scalar.
LIMIT NULL OFFSET NULLpreserves the input rows.Before the fix, with optimizer=on:
After the fix, with optimizer=on, and with optimizer=off on either build:
Rows are sorted here for presentation, with NULL displayed explicitly. The example has no ORDER BY; results were compared as multisets.
Before the fix — EXPLAIN (COSTS OFF), optimizer=on
After the fix — EXPLAIN (COSTS OFF), optimizer=on
The failing alternative introduces an outer Apply below a grouping operation. When decorrelation fails, ORCA can implement this Apply as a scalar SubPlan even though its inner input can produce multiple rows. The resulting scalar cardinality check fails before the grouping above it can run.
The fix allows this scalar plan only when ORCA can prove that the input returns at most one row, otherwise, ORCA can choose a plan that handles EXISTS correctly. In this example, the query succeeds and the outer HashAggregate is no longer needed.
Implementation
The changes are split into two commits:
orca.c, preserve every SubPlan referenced by a child's target list when folding Result projections. Build the proposed replacement on copies and keep the Result if folding would lose a reference.CXformApply2Join, restrict scalar correlated outer-Apply generation to inner inputs whose maximum cardinality is at most one. Other correlated alternatives remain available, and genuine scalar cardinality violations remain errors.Both fixes are required: the cardinality fix removes the incorrect 21000 error, but the new plan still fails with a missing MotionState unless the other fix is also applied.
The regression tests cover correlated EXISTS and NOT EXISTS queries with outer joins, NULL values, and LIMIT/OFFSET. They also check nested subqueries, ANY/ALL, and scalar aggregates, and verify that scalar subqueries still raise an error when they return more than one row.
Type of Change
Test Plan
orca_motion_subplanandorca_exists_cardinalitytogreenplum_schedule, outside the parallel group containingbfv_temp.optimizer=off/optimizer=onexecutions: all 12 SELECT pairs return identical expected rows. A genuine scalar cardinality error is also checked in both modes.make installcheckmake -C src/test installcheck-cbdb-parallelChecklist