This is an automated email from the ASF dual-hosted git repository.
yujun777 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new d2006fb18d2 [fix](fe) Visit shared plan nodes once during existence
searches (#71034)
d2006fb18d2 is described below
commit d2006fb18d2c602ef7ab23049783a1bd8c2d9507
Author: feiniaofeiafei <[email protected]>
AuthorDate: Tue Dec 1 17:04:34 2026 +0800
[fix](fe) Visit shared plan nodes once during existence searches (#71034)
### Background
When multiple consumers read a materialized CTE, their `ExchangeNode`s
share the producer's plan root. The FE follows these links when it
checks for a null-aware left anti join before choosing how to execute a
fragment.
### What problem does this PR solve?
Issue Number: None
Related PR: #31690 (introduced the recursive join check), #51053
(switched the check to `anyMatch`)
PR Type: Bugfix
Problem Summary:
Planning can stall when nested CTEs reuse producers. In the regression
test, two levels of reuse made the FE evaluate the join-search predicate
13 times on a plan with seven nodes. The fix evaluates it seven times
and can still find a join across an exchange.
### Details
- **Frequency:** Always when a search reaches the same producer by
multiple paths before finding a match. Each additional level of reuse
can multiply the visits.
- **Root cause:** `PlanFragment.hasNullAwareLeftAntiJoin()` used a tree
search. Each incoming exchange caused it to search the producer's inputs
again.
- **Why existing tests missed it:** `PlanNodeTest` covered projection
and `test_cte_multicast_complex` checked SQL results. Both could pass
with repeated visits.
- **Fix:** Use an explicit stack and a per-search identity set for
`PlanNode`. Pushing children in reverse order preserves left-to-right
depth-first order and early return. A search from one root takes O(V +
E) time for a constant-time predicate.
- **Other paths and branches:** The traversal covers `anyMatch`, both
instance `contains` overloads, `containsSubclass` and `findFirstOf`. The
recursive join check was also present in the inspected 3.1, 4.0, 4.1 and
4.2 release refs.
### Testing
- `PlanNodeTest.testSearchSharedExchangeInputs` failed on the parent
commit with `expected: <7> but was: <13>`. It passed with the fix. A
separate test verified that an exchange still exposes a null-aware left
anti join in its input.
- All 51 local FE tests across `PlanNodeTest`,
`LocalShuffleNodeCoverageTest`, `ExprToConnectorExpressionConverterTest`
and `RemoteDorisScanNodeTest` passed. Checkstyle reported zero
violations in `fe-common` and `fe-core`.
### Release note
Queries that reuse nested materialized CTEs can spend less time in FE
planning when checking for null-aware left anti joins.
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: OpenAI Codex (GPT-6)
- Generated: Implementation, tests and PR description
- Human review: Not stated
---
.../java/org/apache/doris/common/TreeNode.java | 55 ++++++----------------
.../java/org/apache/doris/planner/PlanNode.java | 24 ++++++++++
.../org/apache/doris/planner/PlanNodeTest.java | 55 ++++++++++++++++++++++
3 files changed, 93 insertions(+), 41 deletions(-)
diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/TreeNode.java
b/fe/fe-common/src/main/java/org/apache/doris/common/TreeNode.java
index 9e9cda18c52..0dd7650fb81 100644
--- a/fe/fe-common/src/main/java/org/apache/doris/common/TreeNode.java
+++ b/fe/fe-common/src/main/java/org/apache/doris/common/TreeNode.java
@@ -152,15 +152,7 @@ public class TreeNode<NodeType extends TreeNode<NodeType>>
{
}
public boolean contains(Class cl) {
- if (cl.isAssignableFrom(this.getClass()) &&
this.getClass().isAssignableFrom(cl)) {
- return true;
- }
- for (NodeType child : children) {
- if (child.contains(cl)) {
- return true;
- }
- }
- return false;
+ return findFirstMatching(node -> cl.equals(node.getClass())) != null;
}
/**
@@ -168,15 +160,7 @@ public class TreeNode<NodeType extends TreeNode<NodeType>>
{
*/
public <C extends TreeNode<NodeType>> boolean contains(
Predicate<? super C> predicate) {
- if (predicate.apply((C) this)) {
- return true;
- }
- for (NodeType child : children) {
- if (child.contains(predicate)) {
- return true;
- }
- }
- return false;
+ return findFirstMatching(node -> predicate.apply((C) node)) != null;
}
/**
@@ -207,15 +191,7 @@ public class TreeNode<NodeType extends TreeNode<NodeType>>
{
}
public boolean containsSubclass(Class cl) {
- if (cl.isAssignableFrom(this.getClass())) {
- return true;
- }
- for (NodeType child : children) {
- if (child.containsSubclass(cl)) {
- return true;
- }
- }
- return false;
+ return findFirstMatching(cl::isInstance) != null;
}
/**
@@ -223,13 +199,19 @@ public class TreeNode<NodeType extends
TreeNode<NodeType>> {
* Looks for matching children via depth-first, left-to-right traversal.
*/
public <C extends NodeType> C findFirstOf(Class<C> cl) {
- if (this.getClass().equals(cl)) {
- return (C) this;
+ return (C) findFirstMatching(node -> node.getClass().equals(cl));
+ }
+
+ /** Return the first match in left-to-right depth-first order, or null. */
+ protected TreeNode<NodeType> findFirstMatching(
+ java.util.function.Predicate<? super TreeNode<NodeType>>
predicate) {
+ if (predicate.test(this)) {
+ return this;
}
for (NodeType child : children) {
- NodeType result = child.findFirstOf(cl);
+ TreeNode<NodeType> result = child.findFirstMatching(predicate);
if (result != null) {
- return (C) result;
+ return result;
}
}
return null;
@@ -248,16 +230,7 @@ public class TreeNode<NodeType extends TreeNode<NodeType>>
{
/** anyMatch */
public boolean anyMatch(Predicate<TreeNode<? extends NodeType>> func) {
- if (func.apply(this)) {
- return true;
- }
-
- for (NodeType child : children) {
- if (child.anyMatch(func)) {
- return true;
- }
- }
- return false;
+ return findFirstMatching(func) != null;
}
/** foreachDown */
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 3fa8a9698a2..3ccb15eed0a 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
@@ -53,13 +53,16 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
+import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -827,6 +830,27 @@ public abstract class PlanNode extends TreeNode<PlanNode> {
return substitutionMap;
}
+ @Override
+ protected PlanNode findFirstMatching(Predicate<? super TreeNode<PlanNode>>
predicate) {
+ // Exchanges for different CTE consumers can share the same producer
root.
+ Set<PlanNode> visited = Sets.newIdentityHashSet();
+ Deque<PlanNode> pending = new ArrayDeque<>();
+ pending.push(this);
+ while (!pending.isEmpty()) {
+ PlanNode node = pending.pop();
+ if (!visited.add(node)) {
+ continue;
+ }
+ if (predicate.test(node)) {
+ return node;
+ }
+ for (int i = node.children.size() - 1; i >= 0; i--) {
+ pending.push(node.children.get(i));
+ }
+ }
+ return null;
+ }
+
public <T extends PlanNode> List<T>
collectInCurrentFragment(Predicate<PlanNode> predicate) {
List<PlanNode> result = Lists.newArrayList();
foreachDownInCurrentFragment(child -> {
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/planner/PlanNodeTest.java
b/fe/fe-core/src/test/java/org/apache/doris/planner/PlanNodeTest.java
index 76395a6b0e8..746c3cb6bb1 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/planner/PlanNodeTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/planner/PlanNodeTest.java
@@ -20,6 +20,7 @@ package org.apache.doris.planner;
import org.apache.doris.analysis.ArithmeticExpr;
import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.IntLiteral;
+import org.apache.doris.analysis.JoinOperator;
import org.apache.doris.analysis.SlotDescriptor;
import org.apache.doris.analysis.SlotId;
import org.apache.doris.analysis.SlotRef;
@@ -28,6 +29,7 @@ import org.apache.doris.analysis.TupleId;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Function.NullableMode;
import org.apache.doris.catalog.Type;
+import org.apache.doris.common.TreeNode;
import org.apache.doris.thrift.TPlanNode;
import com.google.common.collect.Lists;
@@ -35,11 +37,64 @@ import com.google.common.collect.Sets;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PlanNodeTest {
+ @Test
+ public void testSearchSharedExchangeInputs() {
+ TestPlanNode leaf = new TestPlanNode();
+ UnionNode producer = new UnionNode(new PlanNodeId(1), new TupleId(0));
+ ExchangeNode lowerLeft = new ExchangeNode(new PlanNodeId(2), leaf);
+ ExchangeNode lowerRight = new ExchangeNode(new PlanNodeId(3), leaf);
+ producer.addChild(lowerLeft);
+ producer.addChild(lowerRight);
+ UnionNode root = new UnionNode(new PlanNodeId(4), new TupleId(1));
+ ExchangeNode upperLeft = new ExchangeNode(new PlanNodeId(5), producer);
+ ExchangeNode upperRight = new ExchangeNode(new PlanNodeId(6),
producer);
+ root.addChild(upperLeft);
+ root.addChild(upperRight);
+
+ List<PlanNode> expectedOrder = List.of(root, upperLeft, producer,
lowerLeft, leaf, lowerRight, upperRight);
+ List<TreeNode<?>> visited = new ArrayList<>();
+ Assertions.assertFalse(root.anyMatch(node -> {
+ visited.add(node);
+ return false;
+ }));
+ Assertions.assertEquals(expectedOrder.size(), visited.size());
+ Assertions.assertEquals(expectedOrder, visited);
+
+ visited.clear();
+ Assertions.assertFalse(root.contains(node -> {
+ visited.add(node);
+ return false;
+ }));
+ Assertions.assertEquals(expectedOrder, visited);
+
+ visited.clear();
+ Assertions.assertTrue(root.anyMatch(node -> {
+ visited.add(node);
+ return node == leaf;
+ }));
+ Assertions.assertEquals(List.of(root, upperLeft, producer, lowerLeft,
leaf), visited);
+ Assertions.assertSame(upperLeft, root.findFirstOf(ExchangeNode.class));
+ Assertions.assertNull(root.findFirstOf(PlanNode.class));
+ Assertions.assertTrue(root.contains(TestPlanNode.class));
+ Assertions.assertFalse(root.contains(PlanNode.class));
+ Assertions.assertTrue(root.containsSubclass(PlanNode.class));
+ }
+
+ @Test
+ public void testNullAwareLeftAntiJoinAcrossExchange() {
+ PlanNode join = new NestedLoopJoinNode(new PlanNodeId(1), new
TestPlanNode(), new TestPlanNode(),
+ List.of(), JoinOperator.NULL_AWARE_LEFT_ANTI_JOIN, false);
+ PlanNode exchange = new ExchangeNode(new PlanNodeId(2), join);
+ PlanFragment fragment = new PlanFragment(new PlanFragmentId(0),
exchange, DataPartition.UNPARTITIONED);
+ Assertions.assertTrue(fragment.hasNullAwareLeftAntiJoin());
+ }
+
@Test
public void testGetPointQueryProjectListFlattensIntermediateProjection() {
TestPlanNode planNode = new TestPlanNode();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]