Kikyou1997 commented on code in PR #11805:
URL: https://github.com/apache/doris/pull/11805#discussion_r969261760


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/SingleSidePredicate.java:
##########
@@ -0,0 +1,103 @@
+// 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.rules.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * push down expression which is not slot reference
+ */
+public class SingleSidePredicate extends OneRewriteRuleFactory {
+    /*
+     * rewrite example:
+     *       join(t1.a + 1 = t2.b + 2)                            join(c = d)
+     *             /       \                                         /    \
+     *            /         \                                       /      \
+     *           /           \             ====>                   /        \
+     *          /             \                                   /          \
+     *  olapScan(t1)     olapScan(t2)            project(t1.a + 1 as c)  
project(t2.b + 2 as d)
+     *                                                        |                
   |
+     *                                                        |                
   |
+     *                                                        |                
   |
+     *                                                        |                
   |
+     *                                                     olapScan(t1)        
olapScan(t2)
+     *TODO: now t1.a + t2.a = t1.b is not in hashJoinConjuncts. The rule will 
not handle it.
+     */
+    @Override
+    public Rule build() {
+        return logicalJoin()
+                .when(join -> 
join.getHashJoinConjuncts().stream().anyMatch(expr ->
+                        expr.children().stream().anyMatch(expr1 -> !(expr1 
instanceof Slot))))
+                .then(join -> {
+                    List<List<Expression>> exprsOfJoinRelation =
+                            Lists.newArrayList(Lists.newArrayList(), 
Lists.newArrayList());
+                    Map<Expression, Alias> exprMap = Maps.newHashMap();
+                    Plan left = join.left();
+                    join.getHashJoinConjuncts().forEach(conjunct -> {
+                        Preconditions.checkArgument(conjunct instanceof 
EqualTo);
+                        Expression[] exprs = conjunct.children().toArray(new 
Expression[0]);
+                        int tag = checkIfSwap(exprs[0], left);
+                        exprsOfJoinRelation.get(0).add(exprs[tag]);
+                        exprsOfJoinRelation.get(1).add(exprs[tag ^ 1]);
+                        Arrays.stream(exprs).sequential().forEach(expr ->
+                                exprMap.put(expr, new Alias(expr, "expr_" + 
expr.hashCode())));
+                    });
+                    LogicalJoin<Plan, Plan> join1 = 
join.withHashJoinConjuncts(join.getHashJoinConjuncts()
+                            .stream().map(equalTo -> 
equalTo.withChildren(equalTo.children()
+                                    .stream().map(expr -> 
exprMap.get(expr).toSlot())
+                                    .collect(Collectors.toList())))
+                            .collect(Collectors.toList()));
+                    Iterator<List<Expression>> iter = 
exprsOfJoinRelation.iterator();
+                    return join1.withChildren(join1.children().stream().map(
+                            plan -> new LogicalProject<>(new 
ImmutableList.Builder<NamedExpression>()
+                                    .addAll(iter.next().stream().map(expr -> 
exprMap.get(expr))
+                                            .collect(Collectors.toList()))
+                                    .addAll(plan.getOutput())
+                                    .build(), plan))

Review Comment:
   Is it possible produce redundant slot here, such as slots those used only in 
the hash join expressions



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to