Alena0704 opened a new pull request, #1818: URL: https://github.com/apache/cloudberry/pull/1818
Generate a native ORCA plan for a replicated CTE in scalar subqueries When a CTE over a DISTRIBUTED REPLICATED table is referenced from several scalar subqueries, ORCA puts the SharedScan Producer and Consumer on different slices. That cross-slice SharedScan used to hang. Until now we just avoided the hang: FHasCrossSliceReplicatedCTEConsumer detected this shape before DXL translation and fell back to the Postgres planner. This change lets ORCA handle the scalar-subquery case natively, so no fallback is needed: the replicated CTE is materialized once per consumer slice and shared by all references inside ORCA's own plan. The fix is in apply_shareinput_xslice (src/backend/cdb/cdbmutate.c). When a cross-slice ShareInputScan Consumer is found inside a SubPlan and the Producer's whole subtree is replicated -- it contains no Motion and every base-relation scan is over a replicated table (shareinput_subtree_is_replicated) -- the Consumer gets its own deep copy of that subtree with a fresh share_id and becomes a local, intra-slice producer (cross_slice = false, producer_slice_id = its own slice). The source is replicated, so every segment already holds the full data and the local copy is equivalent; the cross-slice coordination is gone. Sibling Consumers of the same CTE in the same slice reuse this copy (tracked by (orig_share_id, motId) -> new_share_id), so the CTE is materialized once and read by all references. cleanup_orphaned_producers then drops the original Producers that no longer have a Consumer. The reuse map, consumer counts and related state live in new ApplyShareInputContext fields in src/in clude/nodes/pathnodes.h. Checking the whole Producer subtree (instead of a single base Scan leaf) covers Producers built from UNION ALL / Append, aggregates, partitioned scans and joins of replicated tables, which otherwise produced the same hanging cross-slice SharedScan. A Producer whose subtree contains a Motion is left alone -- a local copy would not be equivalent. The transformation only runs for ORCA plans (apply_shareinput_xslice takes an is_orca flag). In the standard planner this pass runs after set_plan_references / replace_shareinput_targetlists / slice-table construction, so rewriting the tree there is unsafe -- and the Postgres fallback never produces the problematic cross-slice replicated SharedScan anyway (it uses InitPlans). The pre-DXL fallback check (CUtils::FHasCrossSliceReplicatedCTEConsumer) was too broad -- it fired for every cross-slice replicated CTE Consumer. Narrow it to the join case: a CTE Consumer under a duplicate-hazard / broadcast Motion (greengage 51fe92e). The scalar-subquery case no longer matches here and reaches the new materialization path instead. Add regression tests (shared_scan, qp_orca_fallback) covering the native materialization (single scan, UNION ALL/Append, repeated references) and the join case that ORCA still handles by pinning the Producer to one segment. <!-- Thank you for your contribution to Apache Cloudberry (Incubating)! --> Fixes #ISSUE_Number ### What does this PR do? <!-- Brief overview of the changes, including any major features or fixes --> ### 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 <!-- Remove if not applicable. If yes, explain impact and migration path --> ### Test Plan <!-- How did you test these changes? --> - [ ] Unit tests added/updated - [ ] Integration tests added/updated - [ ] Passed `make installcheck` - [ ] Passed `make -C src/test installcheck-cbdb-parallel` ### Impact <!-- Remove sections that don't apply --> **Performance:** <!-- Any performance implications? --> **User-facing changes:** <!-- Any changes visible to users? --> **Dependencies:** <!-- New dependencies or version changes? --> ### Checklist - [ ] Followed [contribution guide](https://cloudberry.apache.org/contribute/code) - [ ] Added/updated documentation - [ ] Reviewed code for security implications - [ ] This PR contains AI-assisted code generation - [ ] Requested review from [cloudberry committers](https://github.com/orgs/apache/teams/cloudberry-committers) ### Additional Context <!-- Any other information that would help reviewers? Remove if none --> ### CI Skip Instructions <!-- To skip CI builds, add the appropriate CI skip identifier to your PR title. The identifier must: - Be in square brackets [] - Include the word "ci" and either "skip" or "no" - Only use for documentation-only changes or when absolutely necessary --> --- <!-- Join our community: - Mailing list: [[email protected]](https://lists.apache.org/[email protected]) (subscribe: [email protected]) - Discussions: https://github.com/apache/cloudberry/discussions --> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
