This is an automated email from the ASF dual-hosted git repository.

huajianlan pushed a commit to branch fe_local_shuffle
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 3a027cc5a8d25f05c9a778b925ee4eabd3c67ade
Author: 924060929 <[email protected]>
AuthorDate: Tue Apr 14 12:39:11 2026 +0800

    [refactor](local shuffle) Unify 
enforceChild/enforceChildExchange/forceEnforceChildExchange into enforceRequire
    
    Replace the three enforce methods with a single enforceRequire() framework 
method
    in PlanNode that handles Layer 1 (shouldSkipLE) + serial flag propagation + 
child
    recursion + satisfy check + LE insertion in one unified flow.
    
    Key changes:
    - PlanNode: add enforceRequire(), remove enforceChild/enforceChildExchange/
      forceEnforceChildExchange/deriveAndEnforceChildLocalExchange. Rename
      insertLocalExchange to createLocalExchange (protected).
    - ExchangeNode.isSerialOperator(): match toThrift condition
      (staticSerial || hasSerialScanNode) && useSerialSource, so callers get
      accurate BE serial status without needing isSerialChildInThrift.
    - HashJoinNode: delete isSerialChildInThrift, use useSerialSource &&
      child.isSerialOperator() directly.
    - NLJ: probe side uses manual force-enforce (serial flag + recurse +
      unconditional insert); childUsePoolingScan tightened to
      useSerialSource && hasSerialScanNode.
    - AGG/AnalyticEvalNode: require source keeps useSerialSource gate
      (ScanNode.isSerialOperator has circular dependency via hasSerialScanNode).
    - All 15 Node files updated to use enforceRequire.
---
 .../org/apache/doris/planner/AggregationNode.java  |  12 +-
 .../org/apache/doris/planner/AnalyticEvalNode.java |  14 +--
 .../apache/doris/planner/AssertNumRowsNode.java    |   4 +-
 .../org/apache/doris/planner/ExchangeNode.java     |  25 ++--
 .../org/apache/doris/planner/HashJoinNode.java     |  47 +++-----
 .../apache/doris/planner/MaterializationNode.java  |   4 +-
 .../apache/doris/planner/NestedLoopJoinNode.java   |  56 +++++----
 .../apache/doris/planner/PartitionSortNode.java    |   2 +-
 .../java/org/apache/doris/planner/PlanNode.java    | 132 +++++++--------------
 .../org/apache/doris/planner/RecursiveCteNode.java |   4 +-
 .../java/org/apache/doris/planner/RepeatNode.java  |   2 +-
 .../java/org/apache/doris/planner/SelectNode.java  |   2 +-
 .../org/apache/doris/planner/SetOperationNode.java |   2 +-
 .../java/org/apache/doris/planner/SortNode.java    |   2 +-
 .../apache/doris/planner/TableFunctionNode.java    |   4 +-
 15 files changed, 126 insertions(+), 186 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java
index 65d9792c807..13e126031ce 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java
@@ -299,8 +299,9 @@ public class AggregationNode extends PlanNode {
                 requireChild = LocalExchangeTypeRequire.requirePassthrough();
             } else if (fragment != null && 
fragment.useSerialSource(connectContext)
                     && children.get(0).isSerialOperator()) {
-                // Mirrors BE 
StreamingAggOperatorX::required_data_distribution():
-                //   return _child->is_serial_operator() ? PASSTHROUGH : NOOP
+                // BE: _child->is_serial_operator() ? PASSTHROUGH : NOOP
+                // useSerialSource gate needed: ScanNode.isSerialOperator() 
can be true in
+                // non-pooling mode (scanRanges < pptn) but isn't serial on BE 
without useSerialSource.
                 requireChild = LocalExchangeTypeRequire.requirePassthrough();
             } else {
                 requireChild = LocalExchangeTypeRequire.noRequire();
@@ -312,8 +313,9 @@ public class AggregationNode extends PlanNode {
                     // Finalize agg, no group key: NOOP (single serial 
instance collects all)
                     requireChild = LocalExchangeTypeRequire.noRequire();
                 } else {
-                    // Serialize agg, no group key: base class → child serial 
→ PASSTHROUGH, else NOOP
-                    if (fragment != null && 
fragment.useSerialSource(connectContext)) {
+                    // Serialize agg, no group key: BE → 
_child->is_serial_operator() ? PT : NOOP
+                    if (fragment != null && 
fragment.useSerialSource(connectContext)
+                            && children.get(0).isSerialOperator()) {
                         requireChild = 
LocalExchangeTypeRequire.requirePassthrough();
                     } else {
                         requireChild = LocalExchangeTypeRequire.noRequire();
@@ -337,7 +339,7 @@ public class AggregationNode extends PlanNode {
         }
 
         Pair<PlanNode, LocalExchangeType> enforceResult
-                = enforceChild(translatorContext, requireChild, 
children.get(0));
+                = enforceRequire(translatorContext, children.get(0), 0, 
requireChild);
         children = Lists.newArrayList(enforceResult.first);
         return Pair.of(this, enforceResult.second);
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticEvalNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticEvalNode.java
index e7a859dc824..e93ebd9f017 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticEvalNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticEvalNode.java
@@ -176,14 +176,12 @@ public class AnalyticEvalNode extends PlanNode {
             // Must NOT have any LocalExchange between AnalyticEval and its 
child.
             // On BE, AnalyticSink and AnalyticSource share state 
(source_deps/sink_deps).
             // A LocalExchange below would restore the AnalyticSink pipeline 
to _num_instances
-            // tasks while the serial AnalyticSource pipeline stays at 1 task. 
For instance_idx > 0,
-            // the AnalyticSource task is never created, leaving source_deps 
empty →
-            // crash when AnalyticSink calls set_ready_to_read(0).
+            // tasks while the serial AnalyticSource pipeline stays at 1 task.
             //
-            // We call enforceChild with noRequire to traverse children, then 
strip any
+            // Use enforceRequire with noRequire to traverse children, then 
strip any
             // LocalExchange the child inserted (e.g., Exchange wrapping 
itself with PASSTHROUGH).
             Pair<PlanNode, LocalExchangeType> enforceResult
-                    = enforceChild(translatorContext, 
LocalExchangeTypeRequire.noRequire(), children.get(0));
+                    = enforceRequire(translatorContext, children.get(0), 0, 
LocalExchangeTypeRequire.noRequire());
             PlanNode newChild = enforceResult.first;
             if (newChild instanceof LocalExchangeNode) {
                 newChild = newChild.getChild(0);
@@ -204,7 +202,9 @@ public class AnalyticEvalNode extends PlanNode {
                 outputType = LocalExchangeType.NOOP;
             }
         } else if 
(fragment.useSerialSource(translatorContext.getConnectContext())
-                && children.get(0) instanceof ScanNode) {
+                && children.get(0).isSerialOperator()) {
+            // BE base class: _child->is_serial_operator() ? PASSTHROUGH : NOOP
+            // useSerialSource gate: ScanNode.isSerialOperator() can be true 
in non-pooling mode
             requireChild = LocalExchangeTypeRequire.requirePassthrough();
             outputType = LocalExchangeType.PASSTHROUGH;
         } else {
@@ -213,7 +213,7 @@ public class AnalyticEvalNode extends PlanNode {
         }
 
         Pair<PlanNode, LocalExchangeType> enforceResult
-                = enforceChild(translatorContext, requireChild, 
children.get(0));
+                = enforceRequire(translatorContext, children.get(0), 0, 
requireChild);
         children = Lists.newArrayList(enforceResult.first);
         if (outputType == null) {
             outputType = enforceResult.second;
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/AssertNumRowsNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/AssertNumRowsNode.java
index c1671361bf8..2b6e3c5fabf 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/AssertNumRowsNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/AssertNumRowsNode.java
@@ -92,8 +92,8 @@ public class AssertNumRowsNode extends PlanNode {
     public Pair<PlanNode, LocalExchangeType> 
enforceAndDeriveLocalExchange(PlanTranslatorContext translatorContext,
             PlanNode parent, LocalExchangeTypeRequire parentRequire) {
 
-        Pair<PlanNode, LocalExchangeType> enforceResult = enforceChild(
-                translatorContext, 
LocalExchangeTypeRequire.requirePassthrough(), children.get(0));
+        Pair<PlanNode, LocalExchangeType> enforceResult = enforceRequire(
+                translatorContext, children.get(0), 0, 
LocalExchangeTypeRequire.requirePassthrough());
         children = Lists.newArrayList(enforceResult.first);
         return Pair.of(this, LocalExchangeType.PASSTHROUGH);
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/ExchangeNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/ExchangeNode.java
index ae27e4a4a03..8c6eb5c8885 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/ExchangeNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/ExchangeNode.java
@@ -159,8 +159,19 @@ public class ExchangeNode extends PlanNode {
      */
     @Override
     public boolean isSerialOperator() {
-        return (ConnectContext.get() != null && 
ConnectContext.get().getSessionVariable().isUseSerialExchange()
+        // Must match toThrift: (staticSerial || hasSerialScanNode) && 
useSerialSource.
+        // Without useSerialSource gate, non-pooling UNPARTITIONED exchanges 
would be treated
+        // as serial, causing incorrect PASS_TO_ONE insertion in broadcast 
joins.
+        ConnectContext ctx = ConnectContext.get();
+        if (ctx == null || fragment == null) {
+            return false;
+        }
+        if (!fragment.useSerialSource(ctx)) {
+            return false;
+        }
+        boolean staticSerial = (ctx.getSessionVariable().isUseSerialExchange()
                 || partitionType == TPartitionType.UNPARTITIONED) && mergeInfo 
== null;
+        return staticSerial || fragment.hasSerialScanNode();
     }
 
     @Override
@@ -176,16 +187,8 @@ public class ExchangeNode extends PlanNode {
     @Override
     public Pair<PlanNode, LocalExchangeType> 
enforceAndDeriveLocalExchange(PlanTranslatorContext translatorContext,
             PlanNode parent, LocalExchangeTypeRequire parentRequire) {
-        // Must match the BE serial condition in toThrift(): 
isSerialOperator() && useSerialSource().
-        // Only insert PASSTHROUGH when the exchange will actually be serial 
on the BE.
-        // Without useSerialSource() check, we'd insert PASSTHROUGH in 
non-pooling fragments
-        // where the exchange has N tasks, corrupting broadcast join data 
distribution
-        // (PASSTHROUGH round-robin splits complete-dataset sinks into 1/N 
subsets per source).
-        // Must match toThrift: include hasSerialScanNode() — when sibling 
scan is pooling,
-        // Exchange becomes serial on BE even if Exchange itself isn't 
inherently serial.
-        boolean willBeSerialOnBe = (isSerialOperator() || 
fragment.hasSerialScanNode())
-                && fragment != null
-                && fragment.useSerialSource(ConnectContext.get());
+        // isSerialOperator() now matches toThrift: (staticSerial || 
hasSerialScanNode) && useSerialSource
+        boolean willBeSerialOnBe = isSerialOperator();
         if (willBeSerialOnBe) {
             // If there is already a serial ancestor in the same pipeline 
(e.g., serial NLJ
             // for RIGHT_OUTER/FULL_OUTER join), don't insert any local 
exchange. The serial
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/HashJoinNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/HashJoinNode.java
index 946b5899944..daff478885c 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/HashJoinNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/HashJoinNode.java
@@ -306,8 +306,14 @@ public class HashJoinNode extends JoinNodeBase {
             buildSideRequire = probeSideRequire = 
LocalExchangeTypeRequire.noRequire();
             outputType = LocalExchangeType.NOOP;
         } else if (distrMode == DistributionMode.BROADCAST) {
-            boolean probeChildSerial = 
isSerialChildInThrift(translatorContext, children.get(0));
-            boolean buildChildSerial = 
isSerialChildInThrift(translatorContext, children.get(1));
+            // BE: _child->is_serial_operator() ? PASSTHROUGH/PASS_TO_ONE : 
NOOP
+            // useSerialSource gate: ScanNode.isSerialOperator() can be true 
in non-pooling
+            // (scanRanges < pptn) but isn't serial on BE. 
ExchangeNode.isSerialOperator()
+            // already includes useSerialSource, so the gate is harmless for 
Exchange children.
+            boolean useSerialSource = fragment != null
+                    && 
fragment.useSerialSource(translatorContext.getConnectContext());
+            boolean probeChildSerial = useSerialSource && 
children.get(0).isSerialOperator();
+            boolean buildChildSerial = useSerialSource && 
children.get(1).isSerialOperator();
             probeSideRequire = probeChildSerial
                     ? LocalExchangeTypeRequire.requirePassthrough()
                     : LocalExchangeTypeRequire.noRequire();
@@ -315,11 +321,7 @@ public class HashJoinNode extends JoinNodeBase {
                     ? LocalExchangeTypeRequire.requirePassToOne()
                     : LocalExchangeTypeRequire.noRequire();
             // For serial probe: output is PASSTHROUGH (data from single 
instance).
-            // For non-serial probe: propagate probe side's distribution (null 
→ falls through to
-            // probeSideOutput.second below). This mirrors BE's ScanOperator 
returning
-            // BUCKET_HASH_SHUFFLE and !(hash && hash) check in 
need_to_local_exchange: if
-            // probe side is already hash-distributed, parent hash requirement 
is satisfied
-            // without inserting a redundant local exchange.
+            // For non-serial probe: propagate probe side's actual 
distribution.
             outputType = probeChildSerial ? LocalExchangeType.PASSTHROUGH : 
null;
         } else if (AddLocalExchange.isColocated(this) || isBucketShuffle()) {
             probeSideRequire = LocalExchangeTypeRequire.requireBucketHash();
@@ -328,7 +330,7 @@ public class HashJoinNode extends JoinNodeBase {
             // hash table mechanism — PASS_TO_ONE routes all data to task 0 
while tasks 1..N-1
             // build empty hash tables, losing rows. BUCKET_HASH_SHUFFLE 
correctly distributes
             // build data by bucket to match the probe side's bucket 
distribution.
-            // The serial exchange returns NOOP, so enforceChildExchange() 
will insert a
+            // The serial exchange returns NOOP, so enforceRequire() will 
insert a
             // BUCKET_HASH_SHUFFLE local exchange (with PASSTHROUGH fan-out 
for heavy-ops
             // bottleneck avoidance).
             buildSideRequire = LocalExchangeTypeRequire.requireBucketHash();
@@ -346,10 +348,10 @@ public class HashJoinNode extends JoinNodeBase {
             outputType = null; // derived from probeResult.second below
         }
 
-        Pair<PlanNode, LocalExchangeType> probeResult = enforceChildExchange(
-                translatorContext, probeSideRequire, children.get(0), 0);
-        Pair<PlanNode, LocalExchangeType> buildResult = enforceChildExchange(
-                translatorContext, buildSideRequire, children.get(1), 1);
+        Pair<PlanNode, LocalExchangeType> probeResult = enforceRequire(
+                translatorContext, children.get(0), 0, probeSideRequire);
+        Pair<PlanNode, LocalExchangeType> buildResult = enforceRequire(
+                translatorContext, children.get(1), 1, buildSideRequire);
         this.children = Lists.newArrayList(probeResult.first, 
buildResult.first);
         if (outputType == null) {
             outputType = probeResult.second;
@@ -357,27 +359,6 @@ public class HashJoinNode extends JoinNodeBase {
         return Pair.of(this, outputType);
     }
 
-    private boolean isSerialChildInThrift(PlanTranslatorContext 
translatorContext, PlanNode child) {
-        PlanFragment childFragment = child.getFragment();
-        boolean useSerialSource = childFragment != null
-                && 
childFragment.useSerialSource(translatorContext.getConnectContext());
-        if (!useSerialSource) {
-            return false;
-        }
-        // Only consider the child serial for LE decisions when it's an actual 
pooling
-        // ScanNode. For serial non-Scan children (Exchange, AGG, NLJ), 
useSerialSource()
-        // may return true due to serial operators in the fragment, but 
inserting
-        // PASSTHROUGH/PASS_TO_ONE creates pipeline splits with mismatched 
task counts
-        // → HashJoinBuildSink source_deps empty → crash in 
set_ready_to_read().
-        if (!(child instanceof ScanNode)) {
-            if (child instanceof ExchangeNode) {
-                return child.isSerialOperator() || 
childFragment.hasSerialScanNode();
-            }
-            return false;
-        }
-        return child.isSerialOperator();
-    }
-
     @Override
     protected boolean shouldResetSerialFlagForChild(int childIndex) {
         return childIndex == 1;
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/MaterializationNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/MaterializationNode.java
index d5a923af190..b258a42682a 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/MaterializationNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/MaterializationNode.java
@@ -237,8 +237,8 @@ public class MaterializationNode extends PlanNode {
     @Override
     public Pair<PlanNode, LocalExchangeType> enforceAndDeriveLocalExchange(
             PlanTranslatorContext translatorContext, PlanNode parent, 
LocalExchangeTypeRequire parentRequire) {
-        Pair<PlanNode, LocalExchangeType> enforceResult = enforceChild(
-                translatorContext, 
LocalExchangeTypeRequire.requirePassthrough(), children.get(0));
+        Pair<PlanNode, LocalExchangeType> enforceResult = enforceRequire(
+                translatorContext, children.get(0), 0, 
LocalExchangeTypeRequire.requirePassthrough());
         children = new ArrayList<>();
         children.add(enforceResult.first);
         return Pair.of(this, LocalExchangeType.PASSTHROUGH);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/NestedLoopJoinNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/NestedLoopJoinNode.java
index 62e2163a7b7..d9395e7c9a4 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/NestedLoopJoinNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/NestedLoopJoinNode.java
@@ -179,15 +179,11 @@ public class NestedLoopJoinNode extends JoinNodeBase {
     public Pair<PlanNode, LocalExchangeType> 
enforceAndDeriveLocalExchange(PlanTranslatorContext translatorContext,
             PlanNode parent, LocalExchangeTypeRequire parentRequire) {
 
-        // Only request APT/BROADCAST when the probe child is an actual serial 
ScanNode
-        // (pooling scan). For serial non-Scan children (Exchange, AGG, NLJ), 
the
-        // insertLocalExchange guard skips APT on probe side (serial non-Scan 
→ no LE),
-        // but BROADCAST on build side would still be inserted → probe has 1 
task but
-        // build has N tasks → NLJ source_deps empty for tasks 1..N-1 → crash.
-        // Original comment: nested NLJs need pooling handling — but that case 
is now
-        // covered by the serial NLJ branch (isSerialOperator) which skips 
both sides.
+        // Pooling scan: useSerialSource() AND actually has a pooling scan 
node in the fragment.
+        // Without hasSerialScanNode(), useSerialSource() can be true for 
fragments with only
+        // serial Exchanges (use_serial_exchange=true) but no pooling scan, 
causing false positives.
         boolean childUsePoolingScan = 
fragment.useSerialSource(translatorContext.getConnectContext())
-                && children.get(0) instanceof ScanNode;
+                && fragment.hasSerialScanNode();
 
         LocalExchangeTypeRequire probeSideRequire;
         LocalExchangeTypeRequire buildSideRequire;
@@ -196,14 +192,9 @@ public class NestedLoopJoinNode extends JoinNodeBase {
             probeSideRequire = buildSideRequire = 
LocalExchangeTypeRequire.noRequire();
             outputType = LocalExchangeType.NOOP;
         } else if (isSerialOperator()) {
-            // RIGHT_OUTER/RIGHT_SEMI/RIGHT_ANTI/FULL_OUTER: probe side must 
be serial (1 task)
-            // to avoid duplicate unmatched rows from build side. No exchange 
needed for probe.
-            // Build side: noRequire() regardless of pooling scan. The probe 
pipeline has 1 task
-            // (serial), so inserting BROADCAST on the build side would 
inflate build pipeline's
-            // num_tasks to _num_instances while probe stays at 1. This causes 
instance 1+ to
-            // create build tasks without corresponding probe tasks, leaving 
source_deps empty
-            // and crashing in set_ready_to_read(). BE-native avoids this via 
the
-            // num_tasks_of_parent()<=1 check in _add_local_exchange.
+            // RIGHT_OUTER/RIGHT_SEMI/RIGHT_ANTI/FULL_OUTER: probe side must 
be serial (1 task).
+            // Build side: noRequire() — inserting BROADCAST would inflate 
build pipeline's
+            // num_tasks while probe stays at 1, crashing in 
set_ready_to_read().
             probeSideRequire = LocalExchangeTypeRequire.noRequire();
             buildSideRequire = LocalExchangeTypeRequire.noRequire();
             outputType = LocalExchangeType.NOOP;
@@ -217,16 +208,29 @@ public class NestedLoopJoinNode extends JoinNodeBase {
             outputType = LocalExchangeType.ADAPTIVE_PASSTHROUGH;
         }
 
-        // NLJ creates a pipeline boundary in BE (build side splits into a 
separate
-        // pipeline).  BE's need_to_local_exchange Step 4 always inserts a 
local exchange
-        // for non-hash distribution types, even if the child already outputs 
the same
-        // type.  Use forceEnforceChildExchange for the probe side to match BE 
behavior
-        // — always insert ADAPTIVE_PASSTHROUGH even when the child (e.g., 
another NLJ)
-        // already outputs ADAPTIVE_PASSTHROUGH.
-        PlanNode probeSide = forceEnforceChildExchange(
-                translatorContext, probeSideRequire, children.get(0), 0).first;
-        PlanNode buildSide = enforceChildExchange(
-                translatorContext, buildSideRequire, children.get(1), 1).first;
+        // Probe side: force-enforce. NLJ creates a pipeline boundary in BE; 
Step 4 always
+        // inserts non-hash exchanges regardless of current distribution. Must 
always insert
+        // ADAPTIVE_PASSTHROUGH even when child already outputs it (e.g., 
nested NLJ).
+        // Manually propagate serial flag + recurse + force-insert (no 
enforceRequire —
+        // enforceRequire would skip insertion when child already satisfies 
the requirement).
+        boolean probeChildHasSerialAncestor = shouldResetSerialFlagForChild(0)
+                ? false : translatorContext.hasSerialAncestorInPipeline(this) 
|| isSerialOperator();
+        translatorContext.setHasSerialAncestorInPipeline(children.get(0), 
probeChildHasSerialAncestor);
+        Pair<PlanNode, LocalExchangeType> probeChildOutput = children.get(0)
+                .enforceAndDeriveLocalExchange(translatorContext, this, 
probeSideRequire);
+        PlanNode probeSide;
+        if (probeSideRequire.preferType() != LocalExchangeType.NOOP) {
+            LocalExchangeType preferType = 
AddLocalExchange.resolveExchangeType(
+                    probeSideRequire, translatorContext, this, 
probeChildOutput.first);
+            List<Expr> distributeExprs = getChildDistributeExprList(0);
+            probeSide = createLocalExchange(translatorContext, 
probeChildOutput.first, preferType, distributeExprs);
+        } else {
+            probeSide = probeChildOutput.first;
+        }
+
+        // Build side: normal enforce (enforceRequire handles satisfy + 
shouldSkipLE)
+        PlanNode buildSide = enforceRequire(
+                translatorContext, children.get(1), 1, buildSideRequire).first;
         this.children = Lists.newArrayList(probeSide, buildSide);
         return Pair.of(this, outputType);
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java
index 8eaaaf60384..a68d5567ebf 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/PartitionSortNode.java
@@ -191,7 +191,7 @@ public class PartitionSortNode extends PlanNode {
             outputType = LocalExchangeType.PASSTHROUGH;
         }
         Pair<PlanNode, LocalExchangeType> enforceResult
-                = enforceChild(translatorContext, requireChild, 
children.get(0));
+                = enforceRequire(translatorContext, children.get(0), 0, 
requireChild);
         this.children = Lists.newArrayList(enforceResult.first);
         return Pair.of(this, outputType != null ? outputType : 
enforceResult.second);
     }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java
index 018b83d8a4a..b3be7238d9d 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java
@@ -957,9 +957,8 @@ public abstract class PlanNode extends TreeNode<PlanNode> {
             PlanTranslatorContext translatorContext, PlanNode parent, 
LocalExchangeTypeRequire parentRequire) {
         ArrayList<PlanNode> newChildren = Lists.newArrayList();
         for (int i = 0; i < children.size(); i++) {
-            PlanNode child = children.get(i);
             Pair<PlanNode, LocalExchangeType> childOutput
-                    = deriveAndEnforceChildLocalExchange(translatorContext, 
child, parentRequire, i);
+                    = enforceRequire(translatorContext, children.get(i), i, 
LocalExchangeTypeRequire.noRequire());
             newChildren.add(childOutput.first);
         }
         this.children = newChildren;
@@ -967,109 +966,60 @@ public abstract class PlanNode extends 
TreeNode<PlanNode> {
     }
 
     /**
-     * Enforce a local exchange requirement on a child, respecting 
serial-ancestor semantics.
-     * If this node or an ancestor in the same pipeline is serial, the 
exchange is skipped
-     * (mirrors BE's need_to_local_exchange: any_of(operators[idx..end], 
is_serial) → skip).
-     * Returns (resultNode, outputType) where outputType is the resolved 
exchange type
-     * (preferType when an exchange is inserted).
+     * Unified framework method: propagate serial flag → recurse child → 
satisfy check → Layer 1 skip → insert LE.
+     * Replaces the old 
enforceChild/enforceChildExchange/forceEnforceChildExchange trio.
+     *
+     * Layer 1 (shouldSkipLE): mirrors BE's need_to_local_exchange — skip when 
this node or
+     * an ancestor in the same pipeline is serial (operators[idx..end] has 
serial → skip).
+     * Layer 2 (require/output): each Node declares require and output in 
enforceAndDeriveLocalExchange.
      */
-    protected Pair<PlanNode, LocalExchangeType> enforceChild(
-            PlanTranslatorContext translatorContext, LocalExchangeTypeRequire 
requireChild, PlanNode originChild) {
-        int childIndex = children.indexOf(originChild);
-        Pair<PlanNode, LocalExchangeType> childOutput = 
deriveAndEnforceChildLocalExchange(
-                translatorContext, originChild, requireChild, childIndex);
-        if (!requireChild.satisfy(childOutput.second)) {
-            if (translatorContext.hasSerialAncestorInPipeline(this) || 
isSerialOperator()) {
-                return childOutput;
-            }
-            LocalExchangeType preferType = 
AddLocalExchange.resolveExchangeType(
-                    requireChild, translatorContext, this, childOutput.first);
-            if (childOutput.second == preferType) {
-                return childOutput;
-            }
-            List<Expr> distributeExprs = childIndex >= 0 ? 
getChildDistributeExprList(childIndex) : null;
-            PlanNode wrapped = insertLocalExchange(
-                    translatorContext, childOutput.first, preferType, 
distributeExprs);
-            return Pair.of(wrapped, preferType);
-        } else {
+    protected Pair<PlanNode, LocalExchangeType> enforceRequire(
+            PlanTranslatorContext translatorContext, PlanNode child, int 
childIndex,
+            LocalExchangeTypeRequire require) {
+        // 1. Propagate serial-ancestor flag to child
+        boolean childHasSerialAncestor = 
shouldResetSerialFlagForChild(childIndex)
+                ? false : translatorContext.hasSerialAncestorInPipeline(this) 
|| isSerialOperator();
+        translatorContext.setHasSerialAncestorInPipeline(child, 
childHasSerialAncestor);
+
+        // 2. Recurse child (Layer 2: child declares its own require/output)
+        Pair<PlanNode, LocalExchangeType> childOutput =
+                child.enforceAndDeriveLocalExchange(translatorContext, this, 
require);
+
+        // 3. Satisfy check: child output meets requirement → done
+        if (require.satisfy(childOutput.second)) {
             return childOutput;
         }
-    }
-
-    protected Pair<PlanNode, LocalExchangeType> 
deriveAndEnforceChildLocalExchange(
-            PlanTranslatorContext translatorContext, PlanNode child, 
LocalExchangeTypeRequire requireChild,
-            int childIndex) {
-        boolean hasSerialAncestorInPipeline = 
translatorContext.hasSerialAncestorInPipeline(this);
-        boolean childHasSerialAncestorInPipeline = 
shouldResetSerialFlagForChild(childIndex)
-                ? false
-                : hasSerialAncestorInPipeline || isSerialOperator();
-        translatorContext.setHasSerialAncestorInPipeline(child, 
childHasSerialAncestorInPipeline);
-        return child.enforceAndDeriveLocalExchange(translatorContext, this, 
requireChild);
-    }
 
-    /**
-     * Enforce a local exchange requirement on a child without the 
serial-ancestor check.
-     * Use for nodes whose children's distribution requirements must be 
satisfied regardless
-     * of serial ancestors in the same pipeline (joins, set operations, etc.).
-     * Returns (resultNode, childOutputType) where childOutputType is the 
child's reported
-     * output distribution before any inserted exchange (useful for deriving 
the parent's output).
-     */
-    protected Pair<PlanNode, LocalExchangeType> enforceChildExchange(
-            PlanTranslatorContext translatorContext, LocalExchangeTypeRequire 
require,
-            PlanNode child, int childIndex) {
-        Pair<PlanNode, LocalExchangeType> childOutput = 
deriveAndEnforceChildLocalExchange(
-                translatorContext, child, require, childIndex);
-        if (!require.satisfy(childOutput.second)) {
-            LocalExchangeType preferType = 
AddLocalExchange.resolveExchangeType(
-                    require, translatorContext, this, childOutput.first);
-            List<Expr> distributeExprs = 
getChildDistributeExprList(childIndex);
-            PlanNode wrapped = insertLocalExchange(
-                    translatorContext, childOutput.first, preferType, 
distributeExprs);
-            return Pair.of(wrapped, childOutput.second);
+        // 4. Layer 1: skip LE when serial operator or ancestor in same 
pipeline
+        // Equivalent to BE's need_to_local_exchange: 
any_of(operators[idx..end], is_serial) → skip
+        if (translatorContext.hasSerialAncestorInPipeline(this) || 
isSerialOperator()) {
+            return childOutput;
         }
-        return childOutput;
-    }
 
-    /**
-     * Like {@link #enforceChildExchange} but always inserts a local exchange
-     * when the require is not NOOP, even if the child already outputs a 
matching distribution.
-     * Mirrors BE's need_to_local_exchange Step 4 which always inserts 
non-hash exchanges
-     * regardless of the current distribution. Used by NLJ probe side where 
each NLJ creates
-     * a pipeline boundary and data must be re-partitioned.
-     */
-    protected Pair<PlanNode, LocalExchangeType> forceEnforceChildExchange(
-            PlanTranslatorContext translatorContext, LocalExchangeTypeRequire 
require,
-            PlanNode child, int childIndex) {
-        Pair<PlanNode, LocalExchangeType> childOutput = 
deriveAndEnforceChildLocalExchange(
-                translatorContext, child, require, childIndex);
-        if (require.preferType() != LocalExchangeType.NOOP) {
-            LocalExchangeType preferType = 
AddLocalExchange.resolveExchangeType(
-                    require, translatorContext, this, childOutput.first);
-            List<Expr> distributeExprs = 
getChildDistributeExprList(childIndex);
-            PlanNode wrapped = insertLocalExchange(
-                    translatorContext, childOutput.first, preferType, 
distributeExprs);
-            return Pair.of(wrapped, childOutput.second);
-        }
-        return childOutput;
+        // 5. Resolve exchange type and create LE node
+        LocalExchangeType preferType = AddLocalExchange.resolveExchangeType(
+                require, translatorContext, this, childOutput.first);
+        List<Expr> distributeExprs = getChildDistributeExprList(childIndex);
+        PlanNode leNode = createLocalExchange(translatorContext, 
childOutput.first, preferType, distributeExprs);
+        return Pair.of(leNode, preferType);
     }
 
     /**
-     * Insert a LocalExchangeNode wrapping {@code child} with the given 
exchange type.
+     * Create a LocalExchangeNode wrapping child with the given exchange type.
+     *
+     * Skip PT/APT insertion when child is a serial non-Scan operator: serial 
Exchanges/AGGs
+     * have 1 task; inserting PASSTHROUGH creates a pipeline split that breaks 
shared state
+     * or causes data loss. HASH/BUCKET/BROADCAST/PASS_TO_ONE are NOT skipped 
— they properly
+     * redistribute or replicate data. Serial ScanNodes (pooling scan) need 
fan-out and are
+     * handled by the heavy-ops path below.
+     *
      * Handles heavy-ops bottleneck avoidance (mirrors BE 
pipeline_fragment_context.cpp):
      * when upstream has 1 task (serial source) and exchange is heavy 
(hash/bucket/adaptive),
      * insert a PASSTHROUGH fan-out first to avoid single-task bottleneck on 
the heavy
-     * exchange sink. Only applies to local-shuffle (pooling scan) fragments 
where
-     * _parallel_instances=1 causes serial pipelines to have 1 task.
+     * exchange sink. Only applies to local-shuffle (pooling scan) fragments.
      */
-    private PlanNode insertLocalExchange(PlanTranslatorContext 
translatorContext,
+    protected PlanNode createLocalExchange(PlanTranslatorContext 
translatorContext,
             PlanNode child, LocalExchangeType exchangeType, List<Expr> 
distributeExprs) {
-        // Mirrors BE need_to_local_exchange: skip 
PASSTHROUGH/ADAPTIVE_PASSTHROUGH LE when
-        // child is a serial non-Scan operator. Serial Exchanges/AGGs have 1 
task; inserting
-        // PASSTHROUGH/APT creates a pipeline split that breaks shared state 
(COREDUMP) or
-        // causes data loss (routes 1-task output to only 1 of N downstream 
tasks).
-        // HASH/BUCKET/BROADCAST/PASS_TO_ONE are NOT skipped — they properly 
redistribute
-        // or replicate data and are needed even with serial upstream.
-        // Serial ScanNodes (pooling scan) need fan-out and are handled by the 
heavy-ops path below.
         if (child.isSerialOperator() && !(child instanceof ScanNode)
                 && (exchangeType == LocalExchangeType.PASSTHROUGH
                     || exchangeType == 
LocalExchangeType.ADAPTIVE_PASSTHROUGH)) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/RecursiveCteNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/RecursiveCteNode.java
index f94f52071b0..3a7bdd63745 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/RecursiveCteNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/RecursiveCteNode.java
@@ -98,8 +98,8 @@ public class RecursiveCteNode extends PlanNode {
         ArrayList<PlanNode> newChildren = Lists.newArrayList();
         for (int i = 0; i < children.size(); i++) {
             PlanNode child = children.get(i);
-            Pair<PlanNode, LocalExchangeType> childOutput = 
deriveAndEnforceChildLocalExchange(
-                    translatorContext, child, 
LocalExchangeTypeRequire.noRequire(), i);
+            Pair<PlanNode, LocalExchangeType> childOutput = enforceRequire(
+                    translatorContext, child, i, 
LocalExchangeTypeRequire.noRequire());
             newChildren.add(childOutput.first);
         }
         this.children = newChildren;
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/RepeatNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/RepeatNode.java
index 53f39415a56..b049dde9faa 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/RepeatNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/RepeatNode.java
@@ -102,7 +102,7 @@ public class RepeatNode extends PlanNode {
     public Pair<PlanNode, LocalExchangeType> enforceAndDeriveLocalExchange(
             PlanTranslatorContext translatorContext, PlanNode parent, 
LocalExchangeTypeRequire parentRequire) {
         Pair<PlanNode, LocalExchangeType> enforceResult
-                = enforceChild(translatorContext, parentRequire, 
children.get(0));
+                = enforceRequire(translatorContext, children.get(0), 0, 
parentRequire);
         children = new java.util.ArrayList<>();
         children.add(enforceResult.first);
         return Pair.of(this, enforceResult.second);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SelectNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/SelectNode.java
index f8a00b9d389..0d26bd1cef3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/SelectNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SelectNode.java
@@ -69,7 +69,7 @@ public class SelectNode extends PlanNode {
     public Pair<PlanNode, LocalExchangeType> enforceAndDeriveLocalExchange(
             PlanTranslatorContext translatorContext, PlanNode parent, 
LocalExchangeTypeRequire parentRequire) {
         Pair<PlanNode, LocalExchangeType> enforceResult
-                = enforceChild(translatorContext, parentRequire, 
children.get(0));
+                = enforceRequire(translatorContext, children.get(0), 0, 
parentRequire);
         this.children = new ArrayList<>();
         this.children.add(enforceResult.first);
         return Pair.of(this, enforceResult.second);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/SetOperationNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/SetOperationNode.java
index bf1c2421e8c..a7d89278f0c 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/SetOperationNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SetOperationNode.java
@@ -232,7 +232,7 @@ public abstract class SetOperationNode extends PlanNode {
 
         ArrayList<PlanNode> newChildren = Lists.newArrayList();
         for (int i = 0; i < children.size(); i++) {
-            newChildren.add(enforceChildExchange(translatorContext, 
requireChild, children.get(i), i).first);
+            newChildren.add(enforceRequire(translatorContext, children.get(i), 
i, requireChild).first);
         }
         this.children = newChildren;
         return Pair.of(this, outputType);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java
index f7beb670c01..2dd62338c55 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java
@@ -278,7 +278,7 @@ public class SortNode extends PlanNode {
         }
 
         Pair<PlanNode, LocalExchangeType> enforceResult
-                = enforceChild(translatorContext, requireChild, 
children.get(0));
+                = enforceRequire(translatorContext, children.get(0), 0, 
requireChild);
         this.children = Lists.newArrayList(enforceResult.first);
         if (outputType == null) {
             outputType = enforceResult.second;
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/TableFunctionNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/TableFunctionNode.java
index 1980089d29b..c363b30c318 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/TableFunctionNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/TableFunctionNode.java
@@ -139,8 +139,8 @@ public class TableFunctionNode extends PlanNode {
         // In BE, need_to_local_exchange() Step 4 treats non-hash exchanges 
(PASSTHROUGH)
         // as always needing insertion, so "PASSTHROUGH doesn't satisfy 
PASSTHROUGH" —
         // which is equivalent to our FE model of require=PASSTHROUGH, 
output=NOOP.
-        Pair<PlanNode, LocalExchangeType> enforceResult = enforceChild(
-                translatorContext, 
LocalExchangeTypeRequire.requirePassthrough(), children.get(0));
+        Pair<PlanNode, LocalExchangeType> enforceResult = enforceRequire(
+                translatorContext, children.get(0), 0, 
LocalExchangeTypeRequire.requirePassthrough());
         children = Lists.newArrayList(enforceResult.first);
         return Pair.of(this, LocalExchangeType.NOOP);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to