Skip to content

ORCA: prune CTEs created by xforms to match their consumers - #2465

Merged
reshke merged 1 commit into
apache:mainfrom
yjhjstz:fix/orca-xform-cte-used-mask
Dec 3, 2026
Merged

reshke merged 1 commit into
apache:mainfrom
yjhjstz:fix/orca-xform-cte-used-mask

Conversation

@yjhjstz

@yjhjstz yjhjstz commented Dec 1, 2026

Copy link
Copy Markdown
Member

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

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context


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
reshke force-pushed the fix/orca-xform-cte-used-mask branch from 5418410 to 1cdcac8 Compare December 3, 2026 18:56
@reshke
reshke enabled auto-merge (squash) December 3, 2026 18:56
@reshke
reshke merged commit fac9ee7 into apache:main Dec 3, 2026
161 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.

[Bug] ORCA returns wrong system column values through a partially pruned CTE consumer

4 participants