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 2022/07/28 11:57:30 UTC

[GitHub] [doris] 924060929 commented on a diff in pull request #11300: [feature](Nereids) Add subquery analyze

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalEnforceSingleRow.java:
##########
@@ -0,0 +1,94 @@
+// 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.trees.plans.logical;
+
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.LogicalProperties;
+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.PlanType;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Guaranteed to return a result of 1 row.

Review Comment:
   add comment:
   
   ```
   e.g. select max(id) from tbl
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/Scope.java:
##########
@@ -0,0 +1,49 @@
+// 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.trees.expressions.Slot;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * The slot range required for expression analyze.
+ */
+public class Scope {
+    private final Optional<Scope> outerScope;
+    private final List<Slot> slots;
+
+    public Scope(Optional<Scope> outerScope, List<Slot> slots) {
+        this.outerScope = outerScope;
+        this.slots = slots;
+    }
+
+    public Scope(List<Slot> slots) {
+        this.outerScope = Optional.empty();
+        this.slots = slots;
+    }
+
+    public List<Slot> getSlots() {
+        return slots;
+    }
+
+    public Optional<Scope> getOuterScope() {
+        return outerScope;
+    }

Review Comment:
   ```suggestion
       /** get scope link from inner to outer. */
       public List<Scope> toScopeLink() {
           Scope scope = this;
           Builder<Scope> builder = ImmutableList.<Scope>builder().add(scope);
           while (scope.getOuterScope().isPresent()) {
               scope = scope.getOuterScope().get();
               builder.add(scope);
           }
           return builder.build();
       }
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalCorrelatedJoin.java:
##########
@@ -0,0 +1,184 @@
+// 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.trees.plans.logical;
+
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.LogicalProperties;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.plans.JoinType;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * A relational algebra node with join type converted from apply node to subquery.
+ * @param <LEFT_CHILD_TYPE> input.
+ * @param <RIGHT_CHILD_TYPE> subquery.
+ */
+public class LogicalCorrelatedJoin<LEFT_CHILD_TYPE extends Plan, RIGHT_CHILD_TYPE extends Plan>
+        extends LogicalBinary<LEFT_CHILD_TYPE, RIGHT_CHILD_TYPE> {
+
+    /**
+     * Coorelated Join Type.
+     */
+    public enum Type {
+        INNER(JoinType.INNER_JOIN),
+        LEFT(JoinType.LEFT_OUTER_JOIN),
+        RIGHT(JoinType.RIGHT_OUTER_JOIN),
+        FULL(JoinType.FULL_OUTER_JOIN);

Review Comment:
   why re-define join type?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -144,15 +158,25 @@ public Expression visitUnboundAlias(UnboundAlias unboundAlias, Void context) {
 
         @Override
         public Slot visitUnboundSlot(UnboundSlot unboundSlot, Void context) {
-            List<Slot> bounded = bindSlot(unboundSlot, boundSlots);
+            List<Slot> bounded = new ArrayList<>();
+            List<Slot> tmpSlots = new ArrayList<>();
+            tmpSlots.addAll(getScope().getSlots());
+            bounded.addAll(bindSlot(unboundSlot, tmpSlots));
+            Scope scope = getScope();
+            while (bounded.size() == 0 && scope.getOuterScope().isPresent()) {
+                tmpSlots.clear();
+                tmpSlots.addAll(scope.getOuterScope().get().getSlots());
+                bounded.addAll(bindSlot(unboundSlot, tmpSlots));
+                scope = scope.getOuterScope().get();
+            }

Review Comment:
   ```suggestion
           @Override
           public Slot visitUnboundSlot(UnboundSlot unboundSlot, Void context) {
               Optional<List<Slot>> boundedOpt = getScope()
                       .toScopeLink() // from inner scope to outer scope
                       .stream()
                       .map(scope -> bindSlot(unboundSlot, scope.getSlots()))
                       .filter(slots -> !slots.isEmpty())
                       .findFirst();
   
               if (!boundedOpt.isPresent()) {
                   throw new AnalysisException("Cannot resolve " + unboundSlot.toString());
               }
   
               List<Slot> bounded = boundedOpt.get();
               if (bounded.size() == 1) {
                   return bounded.get(0);
               }
   
               throw new AnalysisException(unboundSlot + " is ambiguous: "
                       + bounded.stream()
                       .map(Slot::toString)
                       .collect(Collectors.joining(", ")));
           }
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/NereidsAnalyzer.java:
##########
@@ -40,14 +41,21 @@ public NereidsAnalyzer(ConnectContext connectContext) {
      * Analyze plan.
      */
     public LogicalPlan analyze(Plan plan) {
-        return (LogicalPlan) analyzeWithPlannerContext(plan).getMemo().copyOut();
+        return analyze(plan, null);
+    }
+
+    /**
+     * Analyze plan with scope.
+     */
+    public LogicalPlan analyze(Plan plan, Scope scope) {

Review Comment:
   ```suggestion
       public LogicalPlan analyze(Plan plan, Optional<Scope> scope) {
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/DefaultSubExprRewriter.java:
##########
@@ -0,0 +1,57 @@
+// 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.trees.expressions.visitor;
+
+import org.apache.doris.nereids.analyzer.NereidsAnalyzer;
+import org.apache.doris.nereids.rules.analysis.Scope;
+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.SubqueryExpr;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.qe.ConnectContext;
+
+/**
+ * Use the visitor to iterate sub expression.
+ */
+public class DefaultSubExprRewriter<C> extends DefaultExpressionRewriter<C> {
+    private final Scope scope;
+
+    public DefaultSubExprRewriter(Scope scope) {
+        this.scope = scope;
+    }
+
+    @Override
+    public Expression visit(Expression expr, C context) {
+        if (expr instanceof SubqueryExpr) {
+            NereidsAnalyzer subAnalyzer = new NereidsAnalyzer(ConnectContext.get());
+            LogicalPlan analyzed = subAnalyzer.analyze(((SubqueryExpr) expr).getQueryPlan(), scope);
+            if (expr instanceof InSubquery) {

Review Comment:
   refactor to visitInSubquery and visitExists



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