github-actions[bot] commented on code in PR #62383:
URL: https://github.com/apache/doris/pull/62383#discussion_r3111876511


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterGenerator.java:
##########
@@ -473,65 +465,242 @@ public PhysicalPlan 
visitPhysicalNestedLoopJoin(PhysicalNestedLoopJoin<? extends
     @Override
     public PhysicalPlan visitPhysicalProject(PhysicalProject<? extends Plan> 
project, CascadesContext context) {
         project.child().accept(this, context);
-        RuntimeFilterContext ctx = context.getRuntimeFilterContext();
-        // change key when encounter alias.
-        // TODO: same action will be taken for set operation
-        for (Expression expression : project.getProjects()) {
-            if (expression.children().isEmpty()) {
-                continue;
-            }
-            Expression expr = 
ExpressionUtils.getSingleNumericSlotOrExpressionCoveredByCast(expression.child(0));
-            if (expr instanceof NamedExpression
-                    && ctx.aliasTransferMapContains((NamedExpression) expr)) {
-                if (expression instanceof Alias) {
-                    Alias alias = ((Alias) expression);
-                    ctx.aliasTransferMapPut(alias.toSlot(), 
ctx.getAliasTransferPair((NamedExpression) expr));
-                }
-            }
-        }
         return project;
     }
 
     @Override
     public Plan visitPhysicalOneRowRelation(PhysicalOneRowRelation 
oneRowRelation, CascadesContext context) {
-        // TODO: OneRowRelation will be translated to union. Union node cannot 
apply runtime filter now
-        //  so, just return itself now, until runtime filter could apply on 
any node.
         return oneRowRelation;
     }
 
     @Override
     public PhysicalRelation visitPhysicalRelation(PhysicalRelation relation, 
CascadesContext context) {
-        if (relation instanceof PhysicalSchemaScan) {
-            return relation;
-        }
-        // add all the slots in map.
-        RuntimeFilterContext ctx = context.getRuntimeFilterContext();
-        relation.getOutput().forEach(slot -> ctx.aliasTransferMapPut(slot, 
Pair.of(relation, slot)));
         return relation;
     }
 
     @Override
     public Plan 
visitPhysicalLazyMaterializeOlapScan(PhysicalLazyMaterializeOlapScan scan, 
CascadesContext context) {
-        RuntimeFilterContext ctx = context.getRuntimeFilterContext();
-        scan.getOutput().forEach(slot -> ctx.aliasTransferMapPut(slot, 
Pair.of(scan, slot)));
         return scan;
     }
 
     @Override
     public PhysicalSetOperation visitPhysicalSetOperation(PhysicalSetOperation 
setOperation, CascadesContext context) {
         setOperation.children().forEach(child -> child.accept(this, context));
+        return setOperation;
+    }
+
+    @Override
+    public Plan visitPhysicalIntersect(PhysicalIntersect intersect, 
CascadesContext context) {
+        visitPhysicalSetOperation(intersect, context);
+        generateRuntimeFilterForSetOperation(intersect, context);
+        return intersect;
+    }
+
+    @Override
+    public Plan visitPhysicalExcept(PhysicalExcept except, CascadesContext 
context) {
+        visitPhysicalSetOperation(except, context);
+        generateRuntimeFilterForSetOperation(except, context);
+        return except;
+    }
+
+    private void generateRuntimeFilterForSetOperation(PhysicalSetOperation 
setOp, CascadesContext context) {
+        AbstractPlan child0 = (AbstractPlan) setOp.child(0);
+        if (child0.getStats() == null
+                || ConnectContext.get() == null
+                || ConnectContext.get().getSessionVariable() == null
+                || child0.getStats().getRowCount()
+                    >= 
ConnectContext.get().getSessionVariable().runtimeFilterMaxBuildRowCount) {
+            return;
+        }
         RuntimeFilterContext ctx = context.getRuntimeFilterContext();
-        if (!setOperation.getRegularChildrenOutputs().isEmpty()) {
-            // example: RegularChildrenOutputs is empty
-            // "select 1 a, 2 b union all select 3, 4 union all select 10 e, 
20 f;"
-            for (int i = 0; i < setOperation.getOutput().size(); i++) {
-                Pair childSlotPair = 
ctx.getAliasTransferPair(setOperation.getRegularChildOutput(0).get(i));
-                if (childSlotPair != null) {
-                    ctx.aliasTransferMapPut(setOperation.getOutput().get(i), 
childSlotPair);
+        List<TRuntimeFilterType> legalTypes = 
Arrays.stream(TRuntimeFilterType.values())
+                .filter(type -> 
ctx.getSessionVariable().allowedRuntimeFilterType(type))
+                .filter(type -> type != TRuntimeFilterType.BITMAP)
+                .collect(Collectors.toList());
+
+        boolean hasUnknownColStats = 
context.getStatementContext().isHasUnknownColStats();
+        for (int slotIdx : chooseSourceSlotsForSetOp(setOp)) {
+            Expression sourceExpression = 
setOp.getRegularChildrenOutputs().get(0).get(slotIdx);
+            long buildNdvOrRowCount = computeBuildNdvOrRowCount(child0, 
sourceExpression);
+            for (int childId = 1; childId < setOp.children().size(); 
childId++) {
+                Expression targetExpression = 
setOp.getRegularChildrenOutputs().get(childId).get(slotIdx);
+                pushDownSetOpRuntimeFilter(setOp.child(childId), 
targetExpression,
+                        sourceExpression, setOp, ctx, legalTypes,
+                        buildNdvOrRowCount, slotIdx, hasUnknownColStats);
+            }
+        }
+    }
+
+    /**
+     * Push down runtime filter for set operations (INTERSECT/EXCEPT) by tree 
traversal.
+     * Unlike join RF push-down which uses aliasTransferMap, this method 
rewrites the target
+     * expression through projects and resolves to scans directly.
+     */
+    private boolean pushDownSetOpRuntimeFilter(Plan plan, Expression 
targetExpr,
+            Expression srcExpr, PhysicalSetOperation setOp, 
RuntimeFilterContext ctx,
+            List<TRuntimeFilterType> legalTypes, long buildNdvOrRowCount, int 
exprOrder,
+            boolean hasUnknownColStats) {
+        if (plan instanceof PhysicalRelation) {
+            PhysicalRelation relation = (PhysicalRelation) plan;
+            if (!(relation instanceof PhysicalCatalogRelation) && !(relation 
instanceof PhysicalCTEConsumer)) {
+                return false;
+            }
+            if (!relation.canPushDownRuntimeFilter()) {
+                return false;
+            }
+            if (targetExpr.getInputSlots().size() != 1) {
+                return false;
+            }
+            Slot targetSlot = targetExpr.getInputSlots().iterator().next();
+            if (!relation.getOutputSet().contains(targetSlot)) {
+                return false;
+            }
+            for (TRuntimeFilterType type : legalTypes) {
+                RuntimeFilter filter = 
ctx.getRuntimeFilterBySrcAndType(srcExpr, type, setOp);
+                if (filter != null) {
+                    if (!filter.hasTargetScan(relation)) {
+                        relation.addAppliedRuntimeFilter(filter);
+                        filter.addTargetSlot(targetSlot, targetExpr, relation);
+                        ctx.addJoinToTargetMap(setOp, targetSlot.getExprId());
+                        ctx.setTargetExprIdToFilter(targetSlot.getExprId(), 
filter);
+                        ctx.setTargetsOnScanNode(relation, targetSlot);
+                    }
+                } else {
+                    filter = new 
RuntimeFilter(ctx.getRuntimeFilterIdGen().getNextId(),
+                            srcExpr, ImmutableList.of(targetSlot), 
ImmutableList.of(targetExpr),
+                            type, exprOrder, setOp, false, buildNdvOrRowCount,
+                            !hasUnknownColStats, 
TMinMaxRuntimeFilterType.MIN_MAX, relation);
+                    relation.addAppliedRuntimeFilter(filter);
+                    ctx.addJoinToTargetMap(setOp, targetSlot.getExprId());
+                    ctx.setTargetExprIdToFilter(targetSlot.getExprId(), 
filter);
+                    ctx.setTargetsOnScanNode(relation, targetSlot);
+                    ctx.setRuntimeFilterIdentityToFilter(srcExpr, type, setOp, 
filter);
                 }
             }
+            return true;
         }
-        return setOperation;
+        if (plan instanceof PhysicalProject) {
+            PhysicalProject<?> project = (PhysicalProject<?>) plan;
+            if 
(!project.getOutputSet().containsAll(targetExpr.getInputSlots())) {
+                return false;
+            }
+            Map<Slot, Expression> replaceMap = 
ExpressionUtils.generateReplaceMap(project.getProjects());
+            Expression newTarget = targetExpr.rewriteDownShortCircuit(
+                    e -> replaceMap.getOrDefault(e, e));
+            if (newTarget.getInputSlots().size() == 1) {

Review Comment:
   Can we add the same `PhysicalRepeat` guard here that 
`RuntimeFilterPushDownVisitor.visitPhysicalRepeat()` now has? The new set-op 
traversal still falls through to this generic recursion for `PhysicalRepeat`, 
so an `INTERSECT`/`EXCEPT` child containing `GROUPING SETS` can push an RF 
below `Repeat` even when the filtered slot is not present in every grouping 
set. That is the same wrong-result case the new join-side tests 
(`testRuntimeFilterBlockByGroupingSetsPartialColumn` / `...CommonColumn`) are 
fixing, but there is no corresponding guard or coverage on the set-op path yet.



-- 
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