englefly commented on code in PR #70480:
URL: https://github.com/apache/doris/pull/70480#discussion_r4706132640


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterPruner.java:
##########
@@ -56,50 +73,126 @@
  * 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 side scans 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 Map<CTEId, Plan> changelogCteProducers = new HashMap<>();
+    private final Set<CTEId> changelogWorkTableIds = new HashSet<>();
 
     @Override
     public Plan visit(Plan plan, CascadesContext context) {
         if (!plan.children().isEmpty()) {
             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);
-        }
+        visitSetOperationChildren(recursiveUnion, 
recursiveUnion.getRegularChildrenOutputs(), true, 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()) {
+        visitSetOperationChildren(setOperation, 
setOperation.getRegularChildrenOutputs(),
+                setOperation instanceof PhysicalUnion, context);
+        return setOperation;
+    }
+
+    private void visitSetOperationChildren(Plan plan, 
List<List<SlotReference>> childOutputs,
+            boolean union, CascadesContext context) {
+        RuntimeFilterContext rfContext = context.getRuntimeFilterContext();
+        for (int i = 0; i < plan.arity(); i++) {
+            Plan child = plan.child(i);
             child.accept(this, context);
+            if (rfContext.hasChangelogSource(child)) {
+                if (union) {
+                    Set<Slot> sourceSlots = 
rfContext.getChangelogSourceSlots(child);
+                    Set<Slot> outputSlots = new HashSet<>();
+                    List<SlotReference> inputs = childOutputs.get(i);
+                    List<Slot> outputs = plan.getOutput();
+                    for (int j = 0; j < inputs.size(); j++) {
+                        if (sourceSlots.contains(inputs.get(j))) {
+                            outputSlots.add(outputs.get(j));
+                        }
+                    }
+                    rfContext.addChangelogSource(plan, outputSlots);
+                } else {
+                    // Membership in EXCEPT and INTERSECT depends on the 
entire tuple.
+                    rfContext.addEffectiveSrcNode(plan, 
RuntimeFilterContext.EffectiveSrcType.CHANGELOG);
+                }
+            }
         }
-        return setOperation;
+    }
+
+    private void propagateChangelog(Plan plan, Plan child, 
RuntimeFilterContext rfContext) {
+        if (!rfContext.hasChangelogSource(child)) {
+            return;
+        }
+        Set<Slot> sourceSlots = rfContext.getChangelogSourceSlots(child);
+        if (sourceSlots.containsAll(child.getOutputSet())
+                || (plan instanceof PhysicalFilter && 
plan.getExpressions().stream()
+                        .anyMatch(predicate -> 
!Collections.disjoint(predicate.getInputSlots(), sourceSlots)))) {
+            rfContext.addEffectiveSrcNode(plan, 
RuntimeFilterContext.EffectiveSrcType.CHANGELOG);
+            return;
+        }
+        Set<Slot> outputs = new HashSet<>();
+        if (plan instanceof PhysicalProject) {
+            for (NamedExpression project : ((PhysicalProject<?>) 
plan).getProjects()) {
+                if (!Collections.disjoint(project.getInputSlots(), 
sourceSlots)) {
+                    outputs.add(project.toSlot());
+                }
+            }
+        } else if (plan instanceof PhysicalHashAggregate) {
+            for (NamedExpression output : ((PhysicalHashAggregate<?>) 
plan).getOutputExpressions()) {
+                // An aggregate can depend on row multiplicity even when its 
input key is unchanged.
+                if (output.anyMatch(AggregateExpression.class::isInstance)
+                        || !Collections.disjoint(output.getInputSlots(), 
sourceSlots)) {
+                    outputs.add(output.toSlot());
+                }
+            }
+        } else {
+            outputs.addAll(plan.getOutputSet());
+            outputs.retainAll(sourceSlots);

Review Comment:
   Fixed in `224af456f12`. `CHANGELOG` now propagates at the physical plan node 
level, so `PhysicalWindow` and `PhysicalGenerate` inherit the marker even when 
they produce new output slots. This deliberately also keeps filters on 
preserved-side keys when the build subtree is marked.
   
   The new FE tests keep an ordinary output alongside the window or generator 
output and verify that the outer hash join retains the runtime filter on the 
derived key. All 39 `RuntimeFilterPrunerTest` cases passed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to