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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/PlannerContext.java:
##########
@@ -110,6 +116,16 @@ public PlannerContext setJobContext(PhysicalProperties 
physicalProperties) {
         return this;
     }
 
+    public SubqueryExpr getSubquery(LogicalPlan plan) {
+        return logicalPlanToSubquery.get(plan);
+    }
+
+    public void setLogicalPlanToSubquery(LogicalPlan plan, SubqueryExpr 
subqueryExpr) {

Review Comment:
   ```suggestion
       public void setLogicalPlanToSubqueryIfAbsent(LogicalPlan plan, 
SubqueryExpr subqueryExpr) {
   ```



##########
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.
   UnBoundFunction.isAnalyzed() = false.
   other plan.isAnalyzed() = Suppliers.memoized(() -> 
children().allMatch(Plan::isAnalyzed)).



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -156,7 +179,12 @@ public Expression visitUnboundAlias(UnboundAlias 
unboundAlias, Void context) {
         }
 
         @Override
-        public Slot visitUnboundSlot(UnboundSlot unboundSlot, Void context) {
+        public Slot visitUnboundSlot(UnboundSlot unboundSlot, PlannerContext 
context) {
+            List<Slot> tmpBound = bindSlot(unboundSlot, getScope().getSlots());
+            boolean hasCorrelate = false;

Review Comment:
   this position should has a variable named as `boolean foundInThisScope = 
!tmpBound.isEmpty()`;
   and if foundInThisScope is false, then set `boolean foundInOuterScope = true`
   
   and then is the switch statement, you can declare `boolean hasCorrelate = 
foundInOuterScope` or use `foundInOuterScope` directly



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/Scope.java:
##########
@@ -50,6 +55,15 @@ public Optional<Scope> getOuterScope() {
         return outerScope;
     }
 
+    public boolean hasSlot(Slot slot) {
+        return checkSlots.contains(slot);
+    }
+
+    public void addSlot(Slot slot) {

Review Comment:
   checkSlots is final, so can not addSlot



##########
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) {

Review Comment:
   don't need to add extractSubquery function and getAllSubquery function, the 
shortly function is TreeNode::collect
   ```suggestion
   expression.collect(SubqueryExpr.class::isInstance)
   ```



##########
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);
+        return new LogicalCorrelatedJoin(childPlan, enforce,
+                
ctx.getSubquery(scalarSubquery.getQueryPlan()).getCorrelateSlots(),
+                JoinType.INNER_JOIN, Optional.of(BooleanLiteral.TRUE));
+    }
+
+    private LogicalPlan addInSubqueryApplyNodes(InSubquery inSubquery,
+            LogicalPlan childPlan, PlannerContext ctx) {
+        inSubquery.setAnalyzed(true);
+        return appendApplyNode(inSubquery, childPlan,
+                
ctx.getSubquery(inSubquery.getQueryPlan()).getCorrelateSlots());
+    }
+
+    private LogicalPlan addExistsApplyNodes(Exists exists,
+            LogicalPlan childPlan, PlannerContext ctx) {
+        exists.setAnalyzed(true);
+        return appendApplyNode(exists, childPlan,
+                ctx.getSubquery(exists.getQueryPlan()).getCorrelateSlots());
+    }
+
+    private LogicalPlan appendApplyNode(SubqueryExpr subqueryExpr, LogicalPlan 
childPlan,

Review Comment:
   this function not necessary, because the body is only one line and method 
parameter num is same, and this function is not about 'append'



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

Review Comment:
   sort and aggregate should not has subqueryExpr? And currently doris not 
support select subquery.
   So you don't need to process LogicalProject, LogicalAggregate and 
LogicalSort.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -156,7 +179,12 @@ public Expression visitUnboundAlias(UnboundAlias 
unboundAlias, Void context) {
         }
 
         @Override
-        public Slot visitUnboundSlot(UnboundSlot unboundSlot, Void context) {
+        public Slot visitUnboundSlot(UnboundSlot unboundSlot, PlannerContext 
context) {
+            List<Slot> tmpBound = bindSlot(unboundSlot, getScope().getSlots());
+            boolean hasCorrelate = false;
+            if (tmpBound.size() == 0) {
+                hasCorrelate = true;
+            }
             Optional<List<Slot>> boundedOpt = getScope()

Review Comment:
   why compute bind slot in thisScope again, I think it should be
   ```java
   if (!foundInThisScope && getScope.getParentScope().isPresent()) {
      Optional<List<Slot>> boundedOpt = getScope()
           .getParentScope()
           .get()
           .toScopeLink()
           .stream()
           ...
   }
   ```



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