You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2023/01/04 09:26:52 UTC

[GitHub] [doris] 924060929 commented on a diff in pull request #15591: [Feature] (Nereids) support un equals conjuncts in un scalar sub query

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateFilterUnderApplyProject.java:
##########
@@ -0,0 +1,97 @@
+// 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.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalApply;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.PlanUtils;
+import org.apache.doris.nereids.util.Utils;
+
+import com.google.common.collect.ImmutableSet;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Adjust the order of Project and apply in correlated subqueries.
+ *
+ * before:
+ *              apply
+ *         /              \
+ * Input(output:b)    Project(output:a)
+ *                         |
+ *                     Filter(Correlated predicate/UnCorrelated predicate)
+ *                         |
+ *                       child
+ *
+ * after:
+ *                apply(Correlated predicate)
+ *          /               \
+ * Input(output:b)         Project(output:a)
+ *                           |
+ *                         Filter(UnCorrelated predicate)
+ *                           |
+ *                          child
+ */
+public class EliminateFilterUnderApplyProject extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalApply(group(), logicalProject(logicalFilter()))
+                .when(LogicalApply::isCorrelated)
+                .when(LogicalApply::isIn)
+                .then(apply -> {
+                    LogicalProject<LogicalFilter<GroupPlan>> project = apply.right();
+                    LogicalFilter<GroupPlan> filter = project.child();
+                    Set<Expression> conjuncts = filter.getConjuncts();
+                    Map<Boolean, List<Expression>> split = Utils.splitCorrelatedConjuncts(
+                            conjuncts, apply.getCorrelationSlot());
+                    List<Expression> correlatedPredicate = split.get(true);
+                    List<Expression> unCorrelatedPredicate = split.get(false);
+
+                    // the representative has experienced the rule and added the correlated predicate to the apply node
+                    if (correlatedPredicate.isEmpty()) {
+                        return apply;
+                    }
+
+                    Plan child = PlanUtils.filterOrSelf(ImmutableSet.copyOf(unCorrelatedPredicate), filter.child());
+                    List<NamedExpression> projects = new ArrayList<>();
+                    projects.addAll(project.getProjects());
+                    ExpressionUtils.collect(correlatedPredicate, SlotReference.class::isInstance).stream()
+                            .filter(e -> filter.child().getOutput().contains(e))
+                            .filter(e -> !projects.contains(e))
+                            .map(NamedExpression.class::cast)
+                            .forEach(projects::add);
+                    LogicalProject newProject = new LogicalProject(projects, child);

Review Comment:
   You can use `Set<NamedExpression>`



-- 
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-unsubscribe@doris.apache.org

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


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