Repository navigation
ORCA: prune CTEs created by xforms to match their consumers - #2465
Merged
Merged
Conversation
my-ship-it
approved these changes
Dec 2, 2026
leborchuk
approved these changes
Dec 3, 2026
reshke
approved these changes
Dec 3, 2026
CTEs built during optimization (full join expansion, MDQA to join, LOJ to UNION ALL, unnest TVF, ordered aggregates) go through CXformUtils::PexprAddCTEProducer and are never seen by CTranslatorDXLToExpr::PruneCTEs, so their producer had no used mask and its shared scan emitted every column. Each physical consumer, however, kept only the columns it used. ShareInputScan does not project and setrefs rewrites the consumer target list to positional references, so a consumer whose kept columns were not a prefix of the producer's read them from the wrong slots. With optimizer_expand_fulljoin, selecting gp_segment_id or tableoid through a full join returned 0 or '-', computed columns behind system columns came back wrong, and a join condition on system columns returned wrong rows. Fix it on both sides and keep the mask intact: - PexprAddCTEProducer computes the producer's used mask the same way PruneCTEs does, so the shared scan emits only columns some consumer needs. This also keeps columns already pruned below (a pruned CTE consumer still lists them in its output) out of the producer, which used to trip an assertion in CPhysicalCTEProducer::PcrsRequired or fall back with "Attribute number N not found in project list". If no column is used at all, keep the columns the producer requires from its child anyway: a zero-column shared scan cannot be planned. - CPhysicalCTEConsumer keeps every column when the producer has no used mask, since the shared tuple then carries every column. This also stops ORCA from falling back on count(*) over FULL JOIN ... ON true. - CLogicalCTEProducer::PopCopyWithRemappedColumns keeps the used mask and the can-be-pruned flag, so a copied producer does not emit more columns than its consumers expose. Add regression test cte_prune_xform, run on its own because the preceding parallel group is capped at 19 tests. Fixes apache#2458
reshke
force-pushed
the
fix/orca-xform-cte-used-mask
branch
from
December 3, 2026 18:56
5418410 to
1cdcac8
Compare
reshke
enabled auto-merge (squash)
December 3, 2026 18:56
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.
CTEs built during optimization (full join expansion, MDQA to join, LOJ to UNION ALL, unnest TVF, ordered aggregates) go through CXformUtils::PexprAddCTEProducer and are never seen by CTranslatorDXLToExpr::PruneCTEs, so their producer had no used mask and its shared scan emitted every column. Each physical consumer, however, kept only the columns it used. ShareInputScan does not project and setrefs rewrites the consumer target list to positional references, so a consumer whose kept columns were not a prefix of the producer's read them from the wrong slots. With optimizer_expand_fulljoin, selecting gp_segment_id or tableoid through a full join returned 0 or '-', computed columns behind system columns came back wrong, and a join condition on system columns returned wrong rows.
Fix it on both sides and keep the mask intact:
PexprAddCTEProducer computes the producer's used mask the same way PruneCTEs does, so the shared scan emits only columns some consumer needs. This also keeps columns already pruned below (a pruned CTE consumer still lists them in its output) out of the producer, which used to trip an assertion in CPhysicalCTEProducer::PcrsRequired or fall back with "Attribute number N not found in project list". If no column is used at all, keep the columns the producer requires from its child anyway: a zero-column shared scan cannot be planned.
CPhysicalCTEConsumer keeps every column when the producer has no used mask, since the shared tuple then carries every column. This also stops ORCA from falling back on count(*) over FULL JOIN ... ON true.
CLogicalCTEProducer::PopCopyWithRemappedColumns keeps the used mask and the can-be-pruned flag, so a copied producer does not emit more columns than its consumers expose.
Add regression test cte_prune_xform, run on its own because the preceding parallel group is capped at 19 tests.
Fixes #2458
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