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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/AnalyzeSubquery.java:
##########
@@ -0,0 +1,172 @@
+// 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.analysis;
+
+import org.apache.doris.nereids.PlannerContext;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.BooleanLiteral;
+import org.apache.doris.nereids.trees.expressions.Exists;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.InSubquery;
+import org.apache.doris.nereids.trees.expressions.ScalarSubquery;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SubqueryExpr;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.JoinType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalApply;
+import org.apache.doris.nereids.trees.plans.logical.LogicalCorrelatedJoin;
+import org.apache.doris.nereids.trees.plans.logical.LogicalEnforceSingleRow;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * AnalyzeSubquery. translate from subquery to apply/correlatedJoin.
+ */
+public class AnalyzeSubquery implements AnalysisRuleFactory {
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.of(
+                RuleType.ANALYZE_PROJECT_SUBQUERY.build(
+                        logicalProject().thenApply(ctx -> {
+                            LogicalProject<GroupPlan> project = ctx.root;
+                            List<SubqueryExpr> subqueryExprs = new 
ArrayList<>();
+                            project.getProjects()
+                                    .forEach(expr -> 
subqueryExprs.addAll(extractSubquery(expr)));
+                            if (subqueryExprs.size() == 0) {
+                                return project;
+                            }
+                            return new LogicalProject(project.getProjects(),
+                                    analyzedSubquery(subqueryExprs,
+                                            project.child(), 
ctx.plannerContext));
+                        })
+                ),
+                RuleType.ANALYZE_FILTER_SUBQUERY.build(
+                        logicalFilter().thenApply(ctx -> {
+                            LogicalFilter<GroupPlan> filter = ctx.root;
+                            List<SubqueryExpr> subqueryExprs = 
extractSubquery(filter.getPredicates());
+                            if (subqueryExprs.size() == 0) {
+                                return filter;
+                            }
+                            return new LogicalFilter<>(filter.getPredicates(),
+                                    analyzedSubquery(subqueryExprs,
+                                            filter.child(), 
ctx.plannerContext));
+                        })
+                ),
+                RuleType.ANALYZE_AGGREGATE_SUBQUERY.build(
+                        logicalAggregate().thenApply(ctx -> {
+                            LogicalAggregate<GroupPlan> agg = ctx.root;
+                            List<SubqueryExpr> subqueryExprs = new 
ArrayList<>();
+                            agg.getGroupByExpressions().forEach(expr -> 
subqueryExprs.addAll(extractSubquery(expr)));
+                            agg.getOutputExpressions().forEach(expr -> 
subqueryExprs.addAll(extractSubquery(expr)));
+                            if (subqueryExprs.size() == 0) {
+                                return agg;
+                            }
+                            return new 
LogicalAggregate<>(agg.getGroupByExpressions(), agg.getOutputExpressions(),
+                                    agg.isDisassembled(), agg.getAggPhase(),
+                                    analyzedSubquery(subqueryExprs, 
agg.child(), ctx.plannerContext));
+                        })
+                ),
+                RuleType.ANALYZE_SORT_SUBQUERY.build(
+                        logicalSort().thenApply(ctx -> {
+                            LogicalSort<GroupPlan> sort = ctx.root;
+                            List<SubqueryExpr> subqueryExprs = new 
ArrayList<>();
+                            sort.getOrderKeys().forEach(orderKey -> 
subqueryExprs.addAll(extractSubquery(
+                                    orderKey.getExpr())));
+                            if (subqueryExprs.size() == 0) {
+                                return sort;
+                            }
+                            return new LogicalSort<>(sort.getOrderKeys(),
+                                    analyzedSubquery(subqueryExprs, 
sort.child(), ctx.plannerContext));
+                        })
+                )
+        );
+    }
+
+    private List<SubqueryExpr> extractSubquery(Expression expression) {
+        if (expression instanceof SubqueryExpr) {
+            return ImmutableList.of((SubqueryExpr) expression);
+        }
+        Builder<SubqueryExpr> builder = ImmutableList.<SubqueryExpr>builder();
+        getAllSubquery(expression, builder);
+        return builder.build();
+    }
+
+    private void getAllSubquery(Expression expression, Builder builder) {
+        for (Expression expr : expression.children()) {
+            if (expr instanceof SubqueryExpr) {
+                builder.add(expr);
+            } else {
+                getAllSubquery(expr, builder);
+            }
+        }
+    }
+
+    private LogicalPlan analyzedSubquery(List<SubqueryExpr> subqueryExprs, 
LogicalPlan childPlan, PlannerContext ctx) {
+        for (SubqueryExpr subqueryExpr : subqueryExprs) {
+            if (!subqueryExpr.isAnalyzed()) {
+                if (subqueryExpr instanceof InSubquery) {
+                    return addInSubqueryApplyNodes((InSubquery) subqueryExpr, 
childPlan, ctx);
+                } else if (subqueryExpr instanceof ScalarSubquery) {
+                    return addScalarSubqueryCorrelatedJoins((ScalarSubquery) 
subqueryExpr, childPlan, ctx);
+                } else if (subqueryExpr instanceof Exists) {
+                    return addExistsApplyNodes((Exists) subqueryExpr, 
childPlan, ctx);
+                }
+            }
+        }
+        return childPlan;
+    }
+
+    private LogicalPlan addScalarSubqueryCorrelatedJoins(ScalarSubquery 
scalarSubquery,
+            LogicalPlan childPlan, PlannerContext ctx) {
+        LogicalPlan enforce = new 
LogicalEnforceSingleRow<>(scalarSubquery.getQueryPlan());
+        scalarSubquery.setAnalyzed(true);

Review Comment:
   Immutable plan should not has mutable field. if we need some mutable state, 
we should depend the plan type to compute state, and change it when replace 
children
   
   e.g.
   UnboundExression.isAnalyzed() = false.
   other expression.isAnalyzed() = Suppliers.memoized(() -> 
children().allMatch(Expression::isAnalyzed)).



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