ORCA: fix CTE column-pruning misalignment across consumers - #1883
Open
yjhjstz wants to merge 1 commit into
Open
Conversation
When a CTE has multiple consumers requiring different column subsets, the producer's shared-scan output is pruned to the union of all consumers' required columns (CTranslatorDXLToExpr::PruneCTEs). But each consumer independently decided its own output columns in CPhysicalCTEConsumer from its own per-column GetUsage(). A consumer that considers all of its columns used (e.g. SELECT a.*) kept every column with an identity index map, while the producer emitted only the pruned union. The consumer then read the shared tuple by stale positions, producing wrong results (a join key read from the wrong slot -> LEFT JOIN yields NULLs) and "invalid attnum N for relation shareX_refY" during EXPLAIN. Drive the consumer's kept columns from the producer's finalized used mask (CLogicalCTEProducer::UsedMask) -- the single source of truth -- so every consumer exposes exactly the producer's surviving columns. When the producer was not pruned the mask is NULL and behavior is unchanged. Add a regression test (cte_prune_multi_consumer) covering multi-consumer CTEs where a SELECT * consumer is referenced directly with a join key. It asserts the EXPLAIN no longer errors and returns correct results; the ORCA path is exercised with shared scans and matches the Postgres planner.
yjhjstz
marked this pull request as ready for review
August 5, 2026 05:14
jiaqizho
reviewed
Aug 5, 2026
| CCTEInfo *pcteinfo = COptCtxt::PoctxtFromTLS()->Pcteinfo(); | ||
| CLogicalCTEProducer *popProducer = | ||
| CLogicalCTEProducer::PopConvert(pcteinfo->PexprCTEProducer(m_id)->Pop()); | ||
| BOOL *producer_umask = popProducer->UsedMask(); |
Contributor
There was a problem hiding this comment.
nit: Since producer_umask represents the finalized layout and is only read here, declared as const BOOL *?
my-ship-it
approved these changes
Aug 5, 2026
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.
When a CTE has multiple consumers requiring different column subsets, the producer's shared-scan output is pruned to the union of all consumers' required columns (CTranslatorDXLToExpr::PruneCTEs). But each consumer independently decided its own output columns in CPhysicalCTEConsumer from its own per-column GetUsage(). A consumer that considers all of its columns used (e.g. SELECT a.*) kept every column with an identity index map, while the producer emitted only the pruned union. The consumer then read the shared tuple by stale positions, producing wrong results (a join key read from the wrong slot -> LEFT JOIN yields NULLs) and "invalid attnum N for relation shareX_refY" during EXPLAIN.
Drive the consumer's kept columns from the producer's finalized used mask (CLogicalCTEProducer::UsedMask) -- the single source of truth -- so every consumer exposes exactly the producer's surviving columns. When the producer was not pruned the mask is NULL and behavior is unchanged.
Add a regression test (cte_prune_multi_consumer) covering multi-consumer CTEs where a SELECT * consumer is referenced directly with a join key. It asserts the EXPLAIN no longer errors and returns correct results; the ORCA path is exercised with shared scans and matches the Postgres planner.
Fixes #ISSUE_Number
What does this PR do?
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions