924060929 commented on code in PR #70516:
URL: https://github.com/apache/doris/pull/70516#discussion_r4674753380


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/RuntimeFilterPushDownVisitor.java:
##########
@@ -308,12 +318,31 @@ && canPushThroughJoinChild(join, false, ctx)) {
                 }
                 if (newTarget != null && newTarget.getInputSlots().size() == 1
                         && !newTarget.equals(ctx.srcExpr)) {
+                    if (expandByOuterJoin) {
+                        Plan preserved = join.getJoinType().isLeftOuterJoin() 
? leftNode : rightNode;
+                        Plan nonPreserved = 
join.getJoinType().isLeftOuterJoin() ? rightNode : leftNode;
+                        // For (L LEFT JOIN R ON L.k = R.k) JOIN E ON L.k = 
E.k, an R row
+                        // matching a needed L row must pass the same RF(E.k). 
Only derive
+                        // from the preserved side: outer join output keys are 
not equivalent.
+                        // Limit this to direct keys with identical types so 
both targets use
+                        // the same equality and RF encoding without 
additional conversions.
+                        // A computed key such as R.k + 1 may already be a 
projected slot;
+                        // unlike a direct column alias, it has no original 
column metadata.
+                        if (!(equalTo instanceof EqualTo)
+                                || !PlanUtils.isColumnRef(ctx.probeExpr) || 
!PlanUtils.isColumnRef(newTarget)
+                                || 
!ctx.srcExpr.getDataType().equals(ctx.probeExpr.getDataType())
+                                || 
!ctx.probeExpr.getDataType().equals(newTarget.getDataType())
+                                || 
!preserved.getOutputSet().contains(ctx.probeExpr)
+                                || 
!nonPreserved.getOutputSet().contains(newTarget)) {
+                            continue;
+                        }
+                    }
                     PushDownContext expanded = 
ctx.withNewProbeExpression(newTarget);
                     if 
(leftNode.getOutputSet().containsAll(newTarget.getInputSlots())) {
-                        pushed |= leftNode.accept(this, expanded);
+                        pushed |= pushDown(leftNode, expanded);
                     }
                     if 
(rightNode.getOutputSet().containsAll(newTarget.getInputSlots())) {
-                        pushed |= rightNode.accept(this, expanded);
+                        pushed |= pushDown(rightNode, expanded);

Review Comment:
   **[P2] Reuse the pushdown result when two equality paths reach the same 
probe**
   
   Consider a runtime filter on `A.a` above `A LEFT JOIN (B LEFT JOIN C ...)`. 
If the outer join condition is `A.a = B.a AND A.a = B.b`, this loop visits the 
right subtree once for `B.a` and once for `B.b`. If the inner left join 
condition is `B.a = C.a AND B.b = C.a`, both visits recurse into the same `C` 
subtree with the same probe `C.a`. Scan-level deduplication happens after that 
traversal.
   
   Repeating those two levels doubles the work each time for a single source 
filter. In a small Java fixture calling this revision’s 
`visitPhysicalHashJoin`, 4, 8 and 12 nested left joins reached the same final 
scan key 4, 16 and 64 times, respectively. These are visitor call counts. The 
resulting plan can show only one registered filter on that scan while the FE 
has repeatedly traversed the subtree during planning.
   
   Could we cache the boolean pushdown result for each physical node and 
rewritten probe expression within one source-filter traversal? That would keep 
the distinct `B.a` and `B.b` paths while visiting `(C, C.a)` only once.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/AbstractRuntimeFilterPushDownVisitor.java:
##########
@@ -0,0 +1,85 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.processor.post;
+
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+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.PhysicalHashAggregate;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalPartitionTopN;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalRelation;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalRepeat;
+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.visitor.PlanVisitor;
+
+import java.util.Set;
+
+/** Applies the shared runtime filter pushdown rules before dispatching to 
each physical operator. */
+abstract class AbstractRuntimeFilterPushDownVisitor<C> extends 
PlanVisitor<Boolean, C> {
+
+    /** Starts or continues pushdown. Every recursive step must use this entry 
point. */
+    public final boolean pushDown(Plan plan, C context) {
+        return !blocksRuntimeFilterPushDown(plan, getProbeExpression(context)) 
&& plan.accept(this, context);
+    }
+
+    protected abstract Expression getProbeExpression(C context);
+
+    private static boolean blocksRuntimeFilterPushDown(Plan plan, Expression 
probe) {
+        // Filtering the input could hide a cardinality error or change which 
rows are selected.
+        if (plan instanceof PhysicalAssertNumRows || plan instanceof 
PhysicalLimit || plan instanceof PhysicalTopN) {
+            return true;
+        }
+        Set<Slot> probeSlots = probe.getInputSlots();
+        if (plan instanceof PhysicalPartitionTopN) {
+            PhysicalPartitionTopN<?> topN = (PhysicalPartitionTopN<?>) plan;
+            // Removing a whole partition preserves the choices of the other 
partitions.
+            if (topN.hasGlobalLimit() || 
!topN.getPartitionKeys().containsAll(probeSlots)) {
+                return true;
+            }
+        }
+        if (plan instanceof PhysicalHashAggregate) {
+            PhysicalHashAggregate<?> aggregate = (PhysicalHashAggregate<?>) 
plan;
+            if (aggregate.getGroupByExpressions().isEmpty()
+                    || 
!aggregate.getGroupByExpressions().containsAll(probeSlots)) {
+                return true;
+            }
+        }
+        if (plan instanceof PhysicalRepeat
+                && !((PhysicalRepeat<?>) 
plan).getCommonGroupingSetExpressions().containsAll(probeSlots)) {
+            // Repeat erases keys absent from a grouping set to NULL.
+            return true;
+        }
+
+        return hasSensitiveExpressions(plan)
+                || (plan instanceof PhysicalRelation && 
probe.containsVolatileOrNoneMovableExpression());
+    }
+
+    static boolean hasSensitiveExpressions(Plan plan) {
+        // A scan filter can remove rows before any expression on this 
operator is evaluated.
+        // For example, assert_true on an unrelated Project output must still 
see those rows.
+        return 
plan.getExpressions().stream().anyMatch(Expression::containsVolatileOrNoneMovableExpression);

Review Comment:
   **[P1] Include the `UNNEST` join predicate in the pushdown check**
   
   For `JOIN LATERAL UNNEST ... ON`, `PhysicalGenerate.getExpressions()` 
returns only the generators. The translator reads `getConjuncts()` separately 
and sends those predicates to the BE. That lets an expanded runtime filter pass 
this check even when the `ON` clause contains `assert_true`.
   
   ```sql
   SELECT l.k, u.x
   FROM l
   LEFT JOIN (
       SELECT r.k, t.x
       FROM r INNER JOIN LATERAL UNNEST(r.xs) AS t(x)
           ON assert_true(t.x > 0, 'bad')
   ) u ON l.k = u.k
   INNER JOIN e ON l.k = e.k
   ```
   
   Take `L.k = {1, 2}`, `E.k = {2}` and `R(k, xs) = {(1, [0]), (2, [1])}`, with 
INT keys. Keep this join order, disable RF pruning and have the scans wait for 
an IN filter. With expansion off, building the right side of the left join 
evaluates the assertion on `0` and raises `bad`. With expansion on, the scan of 
`R` can drop key 1 first, so the query can succeed and return `(2, 1)`. I 
traced this path through the Doris translator and checked the two evaluation 
orders in an independent SQL model.
   
   Could `PhysicalGenerate.getExpressions()` include both the generators and 
their conjuncts, as `LogicalGenerate` already does? A regression for this query 
should require `bad` with expansion both on and off.



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