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

englefly 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 0dd43fb3b1a [fix](table stream) Preserve runtime filters built from 
table streams (#70480)
0dd43fb3b1a is described below

commit 0dd43fb3b1a2d986a825ca31b6ef59cf08084e00
Author: minghong <[email protected]>
AuthorDate: Wed Dec 2 11:17:15 2026 +0800

    [fix](table stream) Preserve runtime filters built from table streams 
(#70480)
    
    ### What problem does this PR solve?
    
    Issue Number: N/A
    
    Related PR: N/A
    
    Problem Summary:
    
    Runtime filters built from a table stream's incremental scans must be
    retained. Previously, `RuntimeFilterPruner` could remove them when
    join-column statistics appeared non-selective or were unknown, even
    though base-table statistics do not describe the incremental data.
    
    Mark `PhysicalOlapScan` nodes backed by `OlapChangelogTableWrapper` as
    `NATIVE` effective sources. Existing effective-source propagation then
    preserves their runtime filters through filters, projects, aggregates,
    and joins. Ordinary OLAP scans retain their existing pruning behavior.
    
    Add tests for `MIN_DELTA` and `APPEND_ONLY` build sources with known and
    unknown statistics, aggregation on the build side, and the negative case
    where a stream is only the probe side.
    
    ### Release note
    
    Preserve runtime filters built from incremental table-stream scans even
    when static statistics appear non-selective or are unknown.
    
    ### Check List (For Author)
    
    - Test
        - [ ] Regression test
        - [x] Unit Test
    - `bash run-fe-ut.sh --run
    org.apache.doris.nereids.processor.post.RuntimeFilterPrunerTest` — 13
    tests passed, 0 failures, 0 errors.
            - `cd fe && mvn checkstyle:check -pl fe-core` — 0 violations.
            - `git diff --check` — passed.
        - [ ] Manual test
        - [ ] No need to test or manual test.
    
    - Behavior changed:
        - [ ] No.
    - [x] Yes. Runtime filters built from changelog scans are retained
    regardless of estimated selectivity.
    
    - Does this need documentation?
        - [x] No.
        - [ ] Yes.
    
    ### Check List (For Reviewer who merge this PR)
    
    - [ ] Confirm the release note
    - [ ] Confirm test cases
    - [ ] Confirm document
    - [ ] Add branch pick label
---
 .../processor/post/RuntimeFilterContext.java       |  13 +-
 .../processor/post/RuntimeFilterPruner.java        | 161 +++++++++----
 .../processor/post/RuntimeFilterPrunerTest.java    | 248 ++++++++++++++++++++-
 ...p_table_stream_mixed_history_incremental.groovy |  15 ++
 4 files changed, 377 insertions(+), 60 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterContext.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterContext.java
index 64a321ff038..43a6e7fd6f8 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterContext.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterContext.java
@@ -66,7 +66,7 @@ public class RuntimeFilterContext {
 
     private final Map<Slot, ScanNode> scanNodeOfLegacyRuntimeFilterTarget = 
Maps.newLinkedHashMap();
 
-    private final Map<Plan, EffectiveSrcType> effectiveSrcNodes = 
Maps.newHashMap();
+    private final Map<Plan, EffectiveSrcType> effectiveSrcNodes = 
Maps.newIdentityHashMap();
 
     private final SessionVariable sessionVariable;
 
@@ -219,18 +219,15 @@ public class RuntimeFilterContext {
     }
 
     /**
-     * the selectivity produced by predicate or rf
+     * Effective source types in increasing priority for propagation.
      */
     public enum EffectiveSrcType {
-        NATIVE, REF
+        REF, NATIVE, CHANGELOG
     }
 
     public void addEffectiveSrcNode(Plan node, EffectiveSrcType type) {
-        effectiveSrcNodes.put(node, type);
-    }
-
-    public boolean isEffectiveSrcNode(Plan node) {
-        return effectiveSrcNodes.keySet().contains(node);
+        effectiveSrcNodes.merge(node, type,
+                (existing, incoming) -> existing.compareTo(incoming) >= 0 ? 
existing : incoming);
     }
 
     public EffectiveSrcType getEffectiveSrcType(Plan plan) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterPruner.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterPruner.java
index 1e35768cf19..c4dd1420751 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterPruner.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterPruner.java
@@ -17,7 +17,9 @@
 
 package org.apache.doris.nereids.processor.post;
 
+import org.apache.doris.catalog.OlapChangelogTableWrapper;
 import org.apache.doris.nereids.CascadesContext;
+import org.apache.doris.nereids.trees.expressions.CTEId;
 import org.apache.doris.nereids.trees.expressions.EqualTo;
 import org.apache.doris.nereids.trees.expressions.ExprId;
 import org.apache.doris.nereids.trees.expressions.Expression;
@@ -27,16 +29,21 @@ import org.apache.doris.nereids.trees.plans.AbstractPlan;
 import org.apache.doris.nereids.trees.plans.Plan;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalAssertNumRows;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEAnchor;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEConsumer;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalHashJoin;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalIntersect;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalNestedLoopJoin;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapScan;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalRecursiveUnion;
+import 
org.apache.doris.nereids.trees.plans.physical.PhysicalRecursiveUnionAnchor;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalRelation;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalSetOperation;
+import 
org.apache.doris.nereids.trees.plans.physical.PhysicalStorageLayerAggregate;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalTopN;
+import 
org.apache.doris.nereids.trees.plans.physical.PhysicalWorkTableReference;
 import org.apache.doris.nereids.trees.plans.physical.RuntimeFilter;
 import org.apache.doris.statistics.model.ColumnStatistic;
 import org.apache.doris.statistics.model.Statistics;
@@ -44,6 +51,7 @@ import org.apache.doris.statistics.model.Statistics;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -56,11 +64,14 @@ import java.util.Set;
  * 2. the build column ndv is less than that of probe column, OR
  * 3. the build column's ColumnStats.selectivity < 1, OR
  * 4. the build column is reduced by another RF, which satisfies above 
criterions, OR
- * 5. the RF can eliminate whole scan ranges.
+ * 5. the RF can eliminate whole scan ranges, OR
+ * 6. the build subtree reads a table stream's incremental data.
  *
  * TODO: item 2 is not used since the estimation is not accurate now.
  */
 public class RuntimeFilterPruner extends PlanPostProcessor {
+    private final Set<CTEId> changelogCteIds = new HashSet<>();
+    private final Set<CTEId> changelogWorkTableIds = new HashSet<>();
 
     @Override
     public Plan visit(Plan plan, CascadesContext context) {
@@ -68,38 +79,65 @@ public class RuntimeFilterPruner extends PlanPostProcessor {
             Preconditions.checkArgument(plan.children().size() == 1,
                     plan.getClass().getSimpleName()
                     + " has more than one child, needs its own visitor 
implementation");
-            plan.child(0).accept(this, context);
-            if 
(context.getRuntimeFilterContext().isEffectiveSrcNode(plan.child(0))) {
-                RuntimeFilterContext.EffectiveSrcType childType = 
context.getRuntimeFilterContext()
-                        .getEffectiveSrcType(plan.child(0));
-                context.getRuntimeFilterContext().addEffectiveSrcNode(plan, 
childType);
-            }
+            return propagateEffectiveSrc(plan, context);
         }
         return plan;
     }
 
     @Override
-    public PhysicalRecursiveUnion visitPhysicalRecursiveUnion(
+    public PhysicalRecursiveUnion<? extends Plan, ? extends Plan> 
visitPhysicalRecursiveUnion(
             PhysicalRecursiveUnion<? extends Plan, ? extends Plan> 
recursiveUnion, CascadesContext context) {
-        for (Plan child : recursiveUnion.children()) {
-            child.accept(this, context);
-        }
+        visitChildrenAndPropagateChangelog(recursiveUnion, context);
         return recursiveUnion;
     }
 
+    @Override
+    public PhysicalRecursiveUnionAnchor<? extends Plan> 
visitPhysicalRecursiveUnionAnchor(
+            PhysicalRecursiveUnionAnchor<? extends Plan> anchor, 
CascadesContext context) {
+        // The recursive union visits the seed before the recursive term reads 
the work table.
+        propagateEffectiveSrc(anchor, context);
+        if (context.getRuntimeFilterContext().getEffectiveSrcType(anchor)
+                == RuntimeFilterContext.EffectiveSrcType.CHANGELOG) {
+            changelogWorkTableIds.add(anchor.getCteId());
+        }
+        return anchor;
+    }
+
+    @Override
+    public PhysicalWorkTableReference visitPhysicalWorkTableReference(
+            PhysicalWorkTableReference reference, CascadesContext context) {
+        if (changelogWorkTableIds.contains(reference.getCteId())) {
+            context.getRuntimeFilterContext().addEffectiveSrcNode(reference,
+                    RuntimeFilterContext.EffectiveSrcType.CHANGELOG);
+        } else {
+            visitPhysicalRelation(reference, context);
+        }
+        return reference;
+    }
+
     @Override
     public PhysicalSetOperation visitPhysicalSetOperation(PhysicalSetOperation 
setOperation, CascadesContext context) {
-        for (Plan child : setOperation.children()) {
+        visitChildrenAndPropagateChangelog(setOperation, context);
+        return setOperation;
+    }
+
+    private void visitChildrenAndPropagateChangelog(Plan plan, CascadesContext 
context) {
+        RuntimeFilterContext rfContext = context.getRuntimeFilterContext();
+        for (Plan child : plan.children()) {
             child.accept(this, context);
+            propagateChangelog(plan, child, rfContext);
+        }
+    }
+
+    private void propagateChangelog(Plan plan, Plan child, 
RuntimeFilterContext rfContext) {
+        if (rfContext.getEffectiveSrcType(child) == 
RuntimeFilterContext.EffectiveSrcType.CHANGELOG) {
+            rfContext.addEffectiveSrcNode(plan, 
RuntimeFilterContext.EffectiveSrcType.CHANGELOG);
         }
-        return setOperation;
     }
 
     @Override
     public PhysicalIntersect visitPhysicalIntersect(PhysicalIntersect 
intersect, CascadesContext context) {
-        for (Plan child : intersect.children()) {
-            child.accept(this, context);
-        }
+        visitPhysicalSetOperation(intersect, context);
         context.getRuntimeFilterContext().addEffectiveSrcNode(intersect, 
RuntimeFilterContext.EffectiveSrcType.NATIVE);
         return intersect;
     }
@@ -110,11 +148,10 @@ public class RuntimeFilterPruner extends 
PlanPostProcessor {
             CascadesContext context) {
         join.right().accept(this, context);
         join.left().accept(this, context);
-        if 
(context.getRuntimeFilterContext().isEffectiveSrcNode(join.child(0))) {
-            RuntimeFilterContext.EffectiveSrcType childType = 
context.getRuntimeFilterContext()
-                    .getEffectiveSrcType(join.child(0));
-            context.getRuntimeFilterContext().addEffectiveSrcNode(join, 
childType);
-        }
+        RuntimeFilterContext rfContext = context.getRuntimeFilterContext();
+        propagateEffectiveSrc(join, join.left(), rfContext);
+        // The right stream can affect the result even when the join emits 
only left slots.
+        propagateChangelog(join, join.right(), rfContext);
         return join;
     }
 
@@ -122,14 +159,31 @@ public class RuntimeFilterPruner extends 
PlanPostProcessor {
     public PhysicalCTEAnchor<? extends Plan, ? extends Plan> 
visitPhysicalCTEAnchor(
             PhysicalCTEAnchor<? extends Plan, ? extends Plan> cteAnchor,
             CascadesContext context) {
-        cteAnchor.child(0).accept(this, context);
-        cteAnchor.child(1).accept(this, context);
+        RuntimeFilterContext rfContext = context.getRuntimeFilterContext();
+        // Visit the producer first so that its source is available to all 
consumers in the query.
+        cteAnchor.left().accept(this, context);
+        if (rfContext.getEffectiveSrcType(cteAnchor.left()) == 
RuntimeFilterContext.EffectiveSrcType.CHANGELOG) {
+            changelogCteIds.add(cteAnchor.getCteId());
+        }
+        cteAnchor.right().accept(this, context);
+        propagateChangelog(cteAnchor, cteAnchor.right(), rfContext);
         return cteAnchor;
     }
 
+    @Override
+    public PhysicalCTEConsumer visitPhysicalCTEConsumer(PhysicalCTEConsumer 
consumer, CascadesContext context) {
+        if (changelogCteIds.contains(consumer.getCteId())) {
+            context.getRuntimeFilterContext().addEffectiveSrcNode(consumer,
+                    RuntimeFilterContext.EffectiveSrcType.CHANGELOG);
+        } else {
+            visitPhysicalRelation(consumer, context);
+        }
+        return consumer;
+    }
+
     @Override
     public PhysicalTopN<? extends Plan> visitPhysicalTopN(PhysicalTopN<? 
extends Plan> topN, CascadesContext context) {
-        topN.child().accept(this, context);
+        propagateEffectiveSrc(topN, context);
         context.getRuntimeFilterContext().addEffectiveSrcNode(topN, 
RuntimeFilterContext.EffectiveSrcType.NATIVE);
         return topN;
     }
@@ -137,7 +191,7 @@ public class RuntimeFilterPruner extends PlanPostProcessor {
     public PhysicalLimit<? extends Plan> visitPhysicalLimit(
             PhysicalLimit<? extends Plan> limit,
             CascadesContext context) {
-        limit.child().accept(this, context);
+        propagateEffectiveSrc(limit, context);
         context.getRuntimeFilterContext().addEffectiveSrcNode(limit, 
RuntimeFilterContext.EffectiveSrcType.NATIVE);
         return limit;
     }
@@ -148,10 +202,9 @@ public class RuntimeFilterPruner extends PlanPostProcessor 
{
             CascadesContext context) {
         join.right().accept(this, context);
         RuntimeFilterContext rfContext = context.getRuntimeFilterContext();
-        if (rfContext.isEffectiveSrcNode(join.right())) {
-            RuntimeFilterContext.EffectiveSrcType childType =
-                    rfContext.getEffectiveSrcType(join.right());
-            context.getRuntimeFilterContext().addEffectiveSrcNode(join, 
childType);
+        RuntimeFilterContext.EffectiveSrcType buildSource = 
rfContext.getEffectiveSrcType(join.right());
+        if (buildSource != null) {
+            rfContext.addEffectiveSrcNode(join, buildSource);
         } else {
             List<ExprId> exprIds = rfContext.getTargetExprIdByFilterJoin(join);
             if (exprIds != null && !exprIds.isEmpty()) {
@@ -176,17 +229,7 @@ public class RuntimeFilterPruner extends PlanPostProcessor 
{
             }
         }
         join.left().accept(this, context);
-        if (rfContext.isEffectiveSrcNode(join.left())) {
-            RuntimeFilterContext.EffectiveSrcType leftType =
-                    rfContext.getEffectiveSrcType(join.left());
-            RuntimeFilterContext.EffectiveSrcType rightType =
-                    rfContext.getEffectiveSrcType(join.right());
-            if (rightType == null
-                    || (rightType == RuntimeFilterContext.EffectiveSrcType.REF
-                        && leftType == 
RuntimeFilterContext.EffectiveSrcType.NATIVE)) {
-                rfContext.addEffectiveSrcNode(join, leftType);
-            }
-        }
+        propagateEffectiveSrc(join, join.left(), rfContext);
         return join;
     }
 
@@ -225,6 +268,26 @@ public class RuntimeFilterPruner extends PlanPostProcessor 
{
         return filter;
     }
 
+    @Override
+    public PhysicalOlapScan visitPhysicalOlapScan(PhysicalOlapScan scan, 
CascadesContext context) {
+        if (scan.getTable() instanceof OlapChangelogTableWrapper) {
+            // Base-table statistics do not describe the stream's incremental 
data.
+            context.getRuntimeFilterContext().addEffectiveSrcNode(scan,
+                    RuntimeFilterContext.EffectiveSrcType.CHANGELOG);
+        } else {
+            visitPhysicalRelation(scan, context);
+        }
+        return scan;
+    }
+
+    @Override
+    public Plan 
visitPhysicalStorageLayerAggregate(PhysicalStorageLayerAggregate aggregate, 
CascadesContext context) {
+        PhysicalStorageLayerAggregate visited =
+                (PhysicalStorageLayerAggregate) 
super.visitPhysicalStorageLayerAggregate(aggregate, context);
+        propagateChangelog(visited, visited.getRelation(), 
context.getRuntimeFilterContext());
+        return visited;
+    }
+
     @Override
     public PhysicalRelation visitPhysicalRelation(PhysicalRelation scan, 
CascadesContext context) {
         RuntimeFilterContext rfCtx = context.getRuntimeFilterContext();
@@ -242,7 +305,7 @@ public class RuntimeFilterPruner extends PlanPostProcessor {
     @Override
     public PhysicalAssertNumRows 
visitPhysicalAssertNumRows(PhysicalAssertNumRows<? extends Plan> assertNumRows,
             CascadesContext context) {
-        assertNumRows.child().accept(this, context);
+        propagateEffectiveSrc(assertNumRows, context);
         context.getRuntimeFilterContext().addEffectiveSrcNode(assertNumRows,
                 RuntimeFilterContext.EffectiveSrcType.NATIVE);
         return assertNumRows;
@@ -264,15 +327,19 @@ public class RuntimeFilterPruner extends 
PlanPostProcessor {
      * RF on q1 is not effective, but RF on q2 is. Let RF judge by ndv.
      */
     private <T extends Plan> T propagateEffectiveSrc(T plan, CascadesContext 
context) {
-        RuntimeFilterContext ctx = context.getRuntimeFilterContext();
-        plan.child(0).accept(this, context);
-        if (ctx.isEffectiveSrcNode(plan.child(0))) {
-            RuntimeFilterContext.EffectiveSrcType childType = 
ctx.getEffectiveSrcType(plan.child(0));
-            ctx.addEffectiveSrcNode(plan, childType);
-        }
+        Plan child = plan.child(0);
+        child.accept(this, context);
+        propagateEffectiveSrc(plan, child, context.getRuntimeFilterContext());
         return plan;
     }
 
+    private void propagateEffectiveSrc(Plan plan, Plan child, 
RuntimeFilterContext rfContext) {
+        RuntimeFilterContext.EffectiveSrcType type = 
rfContext.getEffectiveSrcType(child);
+        if (type != null) {
+            rfContext.addEffectiveSrcNode(plan, type);
+        }
+    }
+
     /**
      * consider L join R on L.a=R.b
      * runtime-filter: L.a<-R.b is effective,
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/processor/post/RuntimeFilterPrunerTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/processor/post/RuntimeFilterPrunerTest.java
index 5557b0bb570..6c0e8296da5 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/processor/post/RuntimeFilterPrunerTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/processor/post/RuntimeFilterPrunerTest.java
@@ -17,14 +17,28 @@
 
 package org.apache.doris.nereids.processor.post;
 
+import org.apache.doris.common.Config;
+import org.apache.doris.nereids.CTEContext;
+import org.apache.doris.nereids.CascadesContext;
 import org.apache.doris.nereids.glue.translator.PhysicalPlanTranslator;
 import org.apache.doris.nereids.glue.translator.PlanTranslatorContext;
 import org.apache.doris.nereids.trees.expressions.EqualTo;
 import org.apache.doris.nereids.trees.expressions.Slot;
 import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.physical.AbstractPhysicalJoin;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalCTEConsumer;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalExcept;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalGenerate;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalHashJoin;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalNestedLoopJoin;
 import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalRecursiveUnion;
+import 
org.apache.doris.nereids.trees.plans.physical.PhysicalStorageLayerAggregate;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalUnion;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalWindow;
+import 
org.apache.doris.nereids.trees.plans.physical.PhysicalWorkTableReference;
 import org.apache.doris.nereids.trees.plans.physical.RuntimeFilter;
+import org.apache.doris.nereids.util.MemoTestUtils;
 import org.apache.doris.nereids.util.PlanChecker;
 import org.apache.doris.statistics.model.ColumnStatistic;
 import org.apache.doris.statistics.model.ColumnStatisticBuilder;
@@ -34,14 +48,19 @@ import org.apache.doris.utframe.TestWithFeService;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 import org.junit.jupiter.params.provider.ValueSource;
 
 import java.util.List;
 import java.util.Set;
 
 class RuntimeFilterPrunerTest extends TestWithFeService {
+    private boolean originalEnableFeatureChangelog;
+
     @Override
     protected void runBeforeAll() throws Exception {
+        originalEnableFeatureChangelog = Config.enable_feature_changelog;
+        Config.enable_feature_changelog = true;
         createDatabase("rf_pruner_test");
         useDatabase("rf_pruner_test");
         connectContext.getSessionVariable().setDisableJoinReorder(true);
@@ -49,7 +68,7 @@ class RuntimeFilterPrunerTest extends TestWithFeService {
         connectContext.getSessionVariable().setEnableRuntimeFilterPrune(true);
         
connectContext.getSessionVariable().setRuntimeFilterType(TRuntimeFilterType.BLOOM.getValue());
 
-        createTable("CREATE TABLE rf_prune_fact (id INT, agent_code INT, 
username VARCHAR(32)) "
+        createTable("CREATE TABLE rf_prune_fact (id INT, agent_code INT, 
username VARCHAR(32), k BIGINT) "
                 + "DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1 "
                 + "PROPERTIES('replication_num'='1')");
         // Merge-on-read keeps a hidden delete-sign Filter above the agent 
scan.
@@ -59,6 +78,15 @@ class RuntimeFilterPrunerTest extends TestWithFeService {
         createTable("CREATE TABLE rf_prune_bloc (id INT, bloc_code 
VARCHAR(32)) "
                 + "DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1 "
                 + "PROPERTIES('replication_num'='1')");
+        createTable("CREATE TABLE rf_prune_stream_base (id INT, agent_code 
INT) "
+                + "UNIQUE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1 "
+                + "PROPERTIES('replication_num'='1', 
'enable_unique_key_merge_on_write'='true', "
+                + "'changelog.enable'='true', 
'changelog.need_historical_value'='true')");
+    }
+
+    @Override
+    protected void runAfterAll() {
+        Config.enable_feature_changelog = originalEnableFeatureChangelog;
     }
 
     @ParameterizedTest
@@ -103,8 +131,218 @@ class RuntimeFilterPrunerTest extends TestWithFeService {
         Assertions.assertTrue(context.prunedRF.isEmpty());
     }
 
+    @ParameterizedTest
+    @CsvSource({"true, MIN_DELTA", "false, MIN_DELTA", "true, APPEND_ONLY", 
"false, APPEND_ONLY"})
+    void testStreamSourceKeepsRuntimeFilter(boolean knownStats, String 
streamType) {
+        RuntimeFilterContext context = getRuntimeFilterContext(
+                "SELECT f.id FROM rf_prune_fact f "
+                        + "JOIN [broadcast] 
rf_prune_stream_base@incr('incrementType'='" + streamType + "') s "
+                        + "ON f.agent_code = s.agent_code", knownStats);
+
+        Assertions.assertEquals(1, context.getNereidsRuntimeFilter().size());
+        Assertions.assertTrue(context.prunedRF.isEmpty());
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testStreamSourceKeepsRuntimeFilterThroughAggregate(boolean 
knownStats) {
+        RuntimeFilterContext context = getRuntimeFilterContext(
+                "SELECT f.id FROM rf_prune_fact f JOIN [broadcast] "
+                        + "(SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') "
+                        + "GROUP BY agent_code) s ON f.agent_code = 
s.agent_code", knownStats);
+
+        Assertions.assertEquals(1, context.getNereidsRuntimeFilter().size());
+        Assertions.assertTrue(context.prunedRF.isEmpty());
+    }
+
+    @Test
+    void testStorageAggregateKeepsStreamRuntimeFilter() {
+        assertBuildKeepsRuntimeFilter("SELECT f.id FROM rf_prune_fact f JOIN 
[broadcast] ("
+                + "SELECT COUNT(*) AS n FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA')"
+                + ") b ON f.k = b.n", false, 
PhysicalStorageLayerAggregate.class);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testMixedUnionKeepsRuntimeFilter(boolean knownStats) {
+        RuntimeFilterContext context = getRuntimeFilterContext(
+                "SELECT f.id FROM rf_prune_fact f JOIN [broadcast] ("
+                        + "SELECT agent_code FROM rf_prune_agent "
+                        + "UNION ALL SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') "
+                        + "WHERE id > 0) s ON f.agent_code = s.agent_code", 
knownStats);
+
+        Assertions.assertEquals(1, context.getNereidsRuntimeFilter().size());
+        Assertions.assertTrue(context.prunedRF.isEmpty());
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testMaterializedCteKeepsStreamRuntimeFilter(boolean knownStats) {
+        assertBuildKeepsRuntimeFilter("WITH s AS ("
+                + "SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA')) "
+                + "SELECT f.id FROM rf_prune_fact f JOIN [broadcast] s ON 
f.agent_code = s.agent_code "
+                + "UNION ALL SELECT agent_code FROM s", knownStats, 
PhysicalCTEConsumer.class);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testExceptKeepsStreamRuntimeFilter(boolean knownStats) {
+        assertBuildKeepsRuntimeFilter("SELECT f.id FROM rf_prune_fact f JOIN 
[broadcast] ("
+                + "SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') "
+                + "EXCEPT SELECT agent_code FROM rf_prune_agent) s ON 
f.agent_code = s.agent_code",
+                knownStats, PhysicalExcept.class);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testNestedLoopBuildKeepsStreamRuntimeFilter(boolean knownStats) {
+        assertBuildKeepsRuntimeFilter("SELECT f.id FROM rf_prune_fact f JOIN 
[broadcast] ("
+                + "SELECT s.agent_code FROM rf_prune_agent a "
+                + "JOIN rf_prune_stream_base@incr('incrementType'='MIN_DELTA') 
s "
+                + "ON COALESCE(a.agent_code, 0) < s.agent_code"
+                + ") b ON f.agent_code = b.agent_code", knownStats, 
PhysicalNestedLoopJoin.class);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testNestedLoopLeftSemiBuildKeepsStreamRuntimeFilter(boolean 
knownStats) {
+        assertBuildKeepsRuntimeFilter("SELECT f.id FROM rf_prune_fact f JOIN 
[broadcast] ("
+                + "SELECT a.agent_code FROM rf_prune_agent a LEFT SEMI JOIN "
+                + "rf_prune_stream_base@incr('incrementType'='MIN_DELTA') s ON 
a.agent_code < s.agent_code"
+                + ") b ON f.agent_code = b.agent_code", knownStats, 
PhysicalNestedLoopJoin.class);
+    }
+
+    @ParameterizedTest
+    @CsvSource({"true, true, MIN_DELTA", "true, false, MIN_DELTA",
+            "false, true, MIN_DELTA", "false, false, MIN_DELTA",
+            "true, true, APPEND_ONLY", "true, false, APPEND_ONLY"})
+    void testOuterJoinChangelogKeepsRuntimeFilterForEitherKey(boolean 
hashJoin, boolean streamKey, String streamType) {
+        String condition = hashJoin ? "a.id = s.id" : "COALESCE(a.id, 0) < 
s.id";
+        String key = streamKey ? "stream_key" : "preserved_key";
+        RuntimeFilterContext context = getRuntimeFilterContext(
+                "SELECT f.id, b.preserved_key, b.stream_key FROM rf_prune_fact 
f JOIN [broadcast] ("
+                        + "SELECT a.agent_code AS preserved_key, 
COALESCE(s.agent_code, -1) AS stream_key "
+                        + "FROM rf_prune_fact a LEFT JOIN [broadcast] "
+                        + "rf_prune_stream_base@incr('incrementType'='" + 
streamType + "') s ON " + condition
+                        + ") b ON f.agent_code = b." + key, true);
+
+        List<RuntimeFilter> retained = context.getNereidsRuntimeFilter();
+        Assertions.assertEquals(1, retained.size());
+        Assertions.assertTrue(context.prunedRF.isEmpty());
+        PhysicalHashJoin<?, ?> builder = (PhysicalHashJoin<?, ?>) 
retained.get(0).getBuilderNode();
+        
Assertions.assertEquals(RuntimeFilterContext.EffectiveSrcType.CHANGELOG,
+                context.getEffectiveSrcType(builder.right()));
+        Assertions.assertTrue(builder.right().anyMatch(plan ->
+                (hashJoin ? plan instanceof PhysicalHashJoin : plan instanceof 
PhysicalNestedLoopJoin)
+                        && ((AbstractPhysicalJoin<?, ?>) 
plan).getJoinType().isLeftOuterJoin()),
+                () -> builder.right().treeString());
+    }
+
+    @Test
+    void testWindowBuildKeepsStreamRuntimeFilter() {
+        assertBuildKeepsRuntimeFilter(
+                "SELECT f.id, b.preserved_key FROM rf_prune_fact f JOIN 
[broadcast] ("
+                        + "SELECT a.agent_code AS preserved_key, 
SUM(s.agent_code) OVER (PARTITION BY a.id) AS total "
+                        + "FROM rf_prune_fact a LEFT JOIN [broadcast] "
+                        + 
"rf_prune_stream_base@incr('incrementType'='MIN_DELTA') s ON a.id = s.id"
+                        + ") b ON f.k = b.total", true, PhysicalWindow.class);
+    }
+
+    @Test
+    void testGenerateBuildKeepsStreamRuntimeFilter() {
+        assertBuildKeepsRuntimeFilter(
+                "SELECT f.id, b.preserved_key FROM rf_prune_fact f JOIN 
[broadcast] ("
+                        + "SELECT a.preserved_key, generated_key FROM ("
+                        + "SELECT a.agent_code AS preserved_key, s.agent_code 
AS stream_key "
+                        + "FROM rf_prune_fact a LEFT JOIN [broadcast] "
+                        + 
"rf_prune_stream_base@incr('incrementType'='MIN_DELTA') s ON a.id = s.id"
+                        + ") a LATERAL VIEW 
explode_numbers(COALESCE(a.stream_key, 1)) lv AS generated_key"
+                        + ") b ON f.agent_code = b.generated_key", true, 
PhysicalGenerate.class);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testRecursiveUnionKeepsStreamRuntimeFilter(boolean knownStats) {
+        assertBuildKeepsRuntimeFilter("WITH RECURSIVE s(agent_code) AS ("
+                + "SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') "
+                + "UNION DISTINCT SELECT CAST(0 AS INT) FROM s) "
+                + "SELECT f.id FROM rf_prune_fact f JOIN [broadcast] s ON 
f.agent_code = s.agent_code",
+                knownStats, PhysicalRecursiveUnion.class);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testRecursiveTermKeepsWorkTableRuntimeFilter(boolean knownStats) {
+        assertBuildKeepsRuntimeFilter("WITH RECURSIVE walk(agent_code) AS ("
+                + "SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') "
+                + "UNION ALL SELECT e.agent_code FROM rf_prune_fact e "
+                + "JOIN [broadcast] walk w ON e.agent_code = w.agent_code) "
+                + "SELECT agent_code FROM walk", knownStats, 
PhysicalWorkTableReference.class);
+    }
+
+    @Test
+    void testOrdinaryUnionDoesNotKeepIneffectiveRuntimeFilter() {
+        RuntimeFilterContext context = getRuntimeFilterContext(
+                "SELECT f.id FROM rf_prune_fact f JOIN [broadcast] ("
+                        + "SELECT agent_code FROM rf_prune_agent WHERE bloc_id 
= 1 "
+                        + "UNION ALL SELECT agent_code FROM rf_prune_fact) s 
ON f.agent_code = s.agent_code", true);
+
+        Assertions.assertTrue(context.getNereidsRuntimeFilter().isEmpty());
+        Assertions.assertEquals(1, context.prunedRF.size());
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testEqualLimitsDoNotShareChangelogSource(boolean streamFirst) {
+        String stream = "(SELECT agent_code FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') LIMIT 10)";
+        String ordinary = "(SELECT f.agent_code FROM rf_prune_fact f JOIN 
[broadcast] ("
+                + "(SELECT agent_code FROM rf_prune_fact LIMIT 10) "
+                + "UNION ALL SELECT agent_code FROM rf_prune_agent) b ON 
f.agent_code = b.agent_code)";
+        String sql = streamFirst ? stream + " UNION ALL " + ordinary : 
ordinary + " UNION ALL " + stream;
+        RuntimeFilterContext context = getRuntimeFilterContext(sql, true);
+
+        Assertions.assertTrue(context.getNereidsRuntimeFilter().isEmpty());
+        Assertions.assertEquals(1, context.prunedRF.size());
+        PhysicalHashJoin<?, ?> join = (PhysicalHashJoin<?, ?>) 
context.prunedRF.get(0).getBuilderNode();
+        PhysicalUnion union = (PhysicalUnion) 
join.right().collect(PhysicalUnion.class::isInstance)
+                .iterator().next();
+        Assertions.assertNull(context.getEffectiveSrcType(union));
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testStreamProbeDoesNotKeepIneffectiveRuntimeFilter(boolean 
knownStats) {
+        RuntimeFilterContext context = getRuntimeFilterContext(
+                "SELECT s.id FROM 
rf_prune_stream_base@incr('incrementType'='MIN_DELTA') s "
+                        + "JOIN [broadcast] rf_prune_fact f ON s.agent_code = 
f.agent_code", knownStats);
+
+        Assertions.assertTrue(context.getNereidsRuntimeFilter().isEmpty());
+        Assertions.assertEquals(1, context.prunedRF.size());
+    }
+
+    private void assertBuildKeepsRuntimeFilter(String sql, boolean knownStats, 
Class<? extends Plan> buildType) {
+        RuntimeFilterContext context = getRuntimeFilterContext(sql, 
knownStats);
+        List<RuntimeFilter> hashJoinFilters = 
context.getNereidsRuntimeFilter().stream()
+                .filter(filter -> filter.getBuilderNode() instanceof 
PhysicalHashJoin)
+                .toList();
+        Assertions.assertEquals(1, hashJoinFilters.size());
+        PhysicalHashJoin<?, ?> join = (PhysicalHashJoin<?, ?>) 
hashJoinFilters.get(0).getBuilderNode();
+        Assertions.assertTrue(join.right().anyMatch(buildType::isInstance), () 
-> join.right().treeString());
+        
Assertions.assertEquals(RuntimeFilterContext.EffectiveSrcType.CHANGELOG,
+                context.getEffectiveSrcType(join.right()), () -> 
join.right().treeString());
+        Assertions.assertTrue(context.prunedRF.isEmpty());
+    }
+
     private RuntimeFilterContext getRuntimeFilterContext(String sql, boolean 
knownStats) {
-        PlanChecker checker = 
PlanChecker.from(connectContext).analyze(sql).rewrite().optimize();
+        PlanChecker checker = PlanChecker.from(connectContext).parse(sql);
+        CascadesContext cascadesContext = checker.getCascadesContext();
+        cascadesContext.newTableCollector(true).collect();
+        
cascadesContext.getStatementContext().getOrRegisterChangelogReferenceTso(() -> 
2000L);
+        // Match the planner's reset before analysis, so recursive references 
bind to the work table.
+        cascadesContext.setCteContext(new CTEContext());
+        cascadesContext.newAnalyzer().analyze();
+        MemoTestUtils.initMemoAndValidState(cascadesContext);
+        checker.rewrite().optimize();
         PhysicalPlan plan = checker.getBestPlanTree();
         ColumnStatistic nonSelectiveStats = new ColumnStatisticBuilder(100)
                 .setNdv(100)
@@ -124,8 +362,8 @@ class RuntimeFilterPrunerTest extends TestWithFeService {
                 join.right().getStats().addColumnStats(buildSlot, 
nonSelectiveStats);
             }
         }
-        plan = new 
PlanPostProcessors(checker.getCascadesContext()).process(plan);
-        new PhysicalPlanTranslator(new 
PlanTranslatorContext(checker.getCascadesContext())).translatePlan(plan);
-        return checker.getCascadesContext().getRuntimeFilterContext();
+        plan = new PlanPostProcessors(cascadesContext).process(plan);
+        new PhysicalPlanTranslator(new 
PlanTranslatorContext(cascadesContext)).translatePlan(plan);
+        return cascadesContext.getRuntimeFilterContext();
     }
 }
diff --git 
a/regression-test/suites/table_stream_p0/test_olap_table_stream_mixed_history_incremental.groovy
 
b/regression-test/suites/table_stream_p0/test_olap_table_stream_mixed_history_incremental.groovy
index 8b5d0219f88..bbd499f24f3 100644
--- 
a/regression-test/suites/table_stream_p0/test_olap_table_stream_mixed_history_incremental.groovy
+++ 
b/regression-test/suites/table_stream_p0/test_olap_table_stream_mixed_history_incremental.groovy
@@ -67,4 +67,19 @@ suite("test_olap_table_stream_mixed_history_incremental") {
         GROUP BY sid
         ORDER BY sid
     """
+
+    sql "SET enable_runtime_filter_prune = true"
+    sql "SET runtime_filter_type = 'BLOOM_FILTER'"
+    sql "DROP STATS tbl_mixed_history_incremental(sname)"
+    explain {
+        // sname is neither the partition key nor the bucket key of the probe 
scan.
+        sql """
+            SELECT p.sid
+            FROM tbl_mixed_history_incremental p
+            JOIN [broadcast] s_mixed_history_incremental s ON p.sname = s.sname
+        """
+        contains "VUNION"
+        contains "join op: INNER JOIN(BROADCAST)"
+        contains "[bloom] -> sname"
+    }
 }


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

Reply via email to