[ORCA] Narrow CTE Producer columns in CXformGbAggWithMDQA2Join - #1914
Open
yjhjstz wants to merge 1 commit into
Open
[ORCA] Narrow CTE Producer columns in CXformGbAggWithMDQA2Join#1914yjhjstz wants to merge 1 commit into
yjhjstz wants to merge 1 commit into
Conversation
When rewriting a GbAgg with multiple distinct-qualified aggregates
(MDQAs) into a join of single-DQA aggregates, the transform declared
every column the child expression could produce on the CTE Producer.
On the partitioned-table path the over-declared columns included
unreferenced and system columns whose usage state is EUnknown/EUnused,
which fails the assertion "col_ref->GetUsage() != CColRef::EUnknown"
in CPhysicalCTEProducer::PcrsRequired on assert builds and crashes
with SIGSEGV in MakeDXLTableDescr on release builds:
CREATE TABLE mdqa_part(a int, b int, c int, d int)
DISTRIBUTED BY (a)
PARTITION BY RANGE(a) (START (0) END (100) EVERY (50),
DEFAULT PARTITION other);
SET optimizer_enable_multiple_distinct_aggs = on;
SELECT a, count(DISTINCT b), count(DISTINCT c)
FROM mdqa_part WHERE b BETWEEN 0 AND 2 GROUP BY a;
Fix by restricting the CTE Producer/Consumer columns to the columns
the GbAgg actually needs: group-by keys plus aggregate-argument
references, clipped to what the child can produce. Besides fixing the
crash, this also simplifies MDQA plans on non-partitioned tables: the
narrowed producer no longer carries unused and system columns, and
ORCA can now place a single Redistribute Motion below the producer
instead of one per consumer.
Ported from warehouse-pg/warehouse-pg#149, minidump changes omitted.
yjhjstz
force-pushed
the
orca-mdqa-narrow-cte-producer-columns
branch
from
August 20, 2026 17:41
1c8141c to
6893bf9
Compare
yjhjstz
added a commit
to quantumiodb/pgorca
that referenced
this pull request
Aug 20, 2026
Port of apache/cloudberry#1914 (code only; the upstream regression-test output is MPP-specific). PexprMDQAs2Join built the CTE producer over the child's *entire* derived output column set. For a Get the child output is every column of the table descriptor -- including the system columns -- so a query grouping on one column and taking two DISTINCT aggregates over two others declared a 9-column producer where 3 columns suffice. Restrict the producer to the columns the GbAgg actually reads: the group-by keys plus everything referenced from the aggregate list, intersected with what the child can produce. Upstream this is a crash fix: Cloudberry's CPhysicalCTEProducer:: PcrsRequired asserts every declared column has a known CColRef usage, and the over-declared system columns are EUnknown; release builds instead segfault later in MakeDXLTableDescr. Neither path exists here -- our CPhysicalCTEProducer has no usage assertion and the DXL table descriptor is built differently -- so for pg_orca this only stops the producer from materializing columns nobody consumes. Taking it anyway keeps the xform aligned with upstream. Verified: the PR's repro shape (GROUP BY a with count(DISTINCT b) and count(DISTINCT c)) now hands PexprAddCTEProducer 3 columns instead of 9. test.sh --orca-tests 19/19 pass, olap_mdqa included. The 6 failures under --pg-tests --ignore-plans (subselect, aggregates, join_hash, select_parallel, window, stats) produce a byte-identical regression.diffs on the pre-change tree. cost_align.sh is unchanged (total=311, same-plan <=2%=192, off=4, diff plan=115); its 4 off rows are correlated-subquery/NOT EXISTS/NOT IN items and cost_align.sql has no multiple-DQA query, so Exfp never fires there.
jiaqizho
approved these changes
Aug 21, 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.
On release builds the assertion is compiled out and the same over-declared column list crashes later with SIGSEGV in MakeDXLTableDescr during DXL translation.
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