morrySnow commented on code in PR #11035:
URL: https://github.com/apache/doris/pull/11035#discussion_r931973043


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -190,11 +194,39 @@ public LogicalPlan 
visitRegularQuerySpecification(RegularQuerySpecificationConte
     /**
      * Create an aliased table reference. This is typically used in FROM 
clauses.
      */
+    private LogicalPlan applyAlias(TableAliasContext ctx, LogicalPlan plan) {
+        if (null != ctx.strictIdentifier()) {
+            String alias = ctx.strictIdentifier().getText();
+            if (null != ctx.identifierList()) {
+                throw new IllegalStateException("Do not implemented");
+                // List<String> colName = 
visitIdentifierSeq(ctx.identifierList().identifierSeq());
+                // TODO: multi-colName
+            } else {
+                return new LogicalSubQueryAlias<>(alias, plan);
+            }
+        }
+        return plan;
+    }
+
     @Override
     public LogicalPlan visitTableName(TableNameContext ctx) {
         List<String> tableId = 
visitMultipartIdentifier(ctx.multipartIdentifier());
-        // TODO: sample and time travel, alias, sub query
-        return new UnboundRelation(tableId);
+        return applyAlias(ctx.tableAlias(), new UnboundRelation(tableId));
+    }
+
+    @Override
+    public LogicalPlan visitAliasedQuery(AliasedQueryContext ctx) {
+        String alias = "__auto_generated_name__";
+        LogicalPlan query = visitQuery(ctx.query());
+        if (null != ctx.tableAlias().strictIdentifier()) {

Review Comment:
   strictIdentifier cannot be null



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/EliminateAliasNode.java:
##########
@@ -0,0 +1,91 @@
+// 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.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Eliminate the logical sub query and alias node after analyze and before 
rewrite
+ * If we match the alias node and return its child node, in the execute() of 
the job
+ * <p>
+ * TODO: refactor group merge strategy to support the feature above
+ */
+public class EliminateAliasNode implements AnalysisRuleFactory {
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.of(
+                RuleType.PROJECT_ELIMINATE_ALIAS_NODE.build(
+                        logicalProject(logicalSubQueryAlias())
+                                .then(project -> 
eliminateSubQueryAliasNode(project, project.children()))
+                ),
+                RuleType.FILTER_ELIMINATE_ALIAS_NODE.build(
+                        logicalFilter(logicalSubQueryAlias())
+                                .then(filter -> 
eliminateSubQueryAliasNode(filter, filter.children()))
+                ),
+                RuleType.AGGREGATE_ELIMINATE_ALIAS_NODE.build(
+                        logicalAggregate(logicalSubQueryAlias())
+                                .then(agg -> eliminateSubQueryAliasNode(agg, 
agg.children()))
+                ),
+                RuleType.JOIN_ELIMINATE_ALIAS_NODE.build(
+                        logicalJoin().then(join -> 
joinEliminateSubQueryAliasNode(join, join.children()))
+                )
+        );
+    }
+
+    private LogicalPlan eliminateSubQueryAliasNode(LogicalPlan node, 
List<Plan> aliasNodes) {
+        List<Plan> nodes = aliasNodes.stream()
+                .map(this::getPlan)
+                .collect(Collectors.toList());
+        return (LogicalPlan) node.withChildren(nodes);
+    }
+
+    private LogicalPlan joinEliminateSubQueryAliasNode(LogicalPlan node, 
List<Plan> aliasNode) {
+        List<Plan> nodes = aliasNode.stream()
+                .map(child -> {
+                    if (checkIsSubQueryAliasNode(child)) {
+                        return ((GroupPlan) child).getGroup()
+                                .getLogicalExpression()
+                                .child(0)
+                                .getLogicalExpression()
+                                .getPlan();
+                    }
+                    return child;
+                })
+                .collect(Collectors.toList());
+        return (LogicalPlan) node.withChildren(nodes);
+    }
+
+    private boolean checkIsSubQueryAliasNode(Plan node) {

Review Comment:
   why not use GroupPlan as parameter's type directly?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -190,11 +194,39 @@ public LogicalPlan 
visitRegularQuerySpecification(RegularQuerySpecificationConte
     /**
      * Create an aliased table reference. This is typically used in FROM 
clauses.
      */
+    private LogicalPlan applyAlias(TableAliasContext ctx, LogicalPlan plan) {
+        if (null != ctx.strictIdentifier()) {
+            String alias = ctx.strictIdentifier().getText();
+            if (null != ctx.identifierList()) {
+                throw new IllegalStateException("Do not implemented");

Review Comment:
   use ParseException instead of IllegalStateException



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/FinalizeAnalyzeJob.java:
##########
@@ -0,0 +1,43 @@
+// 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.jobs.batch;
+
+import org.apache.doris.nereids.PlannerContext;
+import org.apache.doris.nereids.rules.analysis.EliminateAliasNode;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Job to eliminate the logical node of sub query and alias
+ */
+public class FinalizeAnalyzeJob extends BatchRulesJob {
+
+    /**
+     * constructor
+     * @param plannerContext ctx
+     */
+    public FinalizeAnalyzeJob(PlannerContext plannerContext) {
+        super(plannerContext);
+        rulesJob.addAll(ImmutableList.of(
+                bottomUpBatch(ImmutableList.of(
+                                new EliminateAliasNode()
+                        )
+                )
+        ));

Review Comment:
   ```suggestion
           rulesJob.addAll(ImmutableList.of(
                   bottomUpBatch(ImmutableList.of(new EliminateAliasNode()))
           ));
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/GroupExpression.java:
##########
@@ -168,6 +169,11 @@ public boolean equals(Object o) {
             return false;
         }
         GroupExpression that = (GroupExpression) o;
+        // if the plan is LogicalOlapScan, this == that should be true,
+        // because equals() can not divide UnboundRelation and the plan above.

Review Comment:
   this comment is not clear to describe the question. we need to explain that 
if one relation appear in plan more than once, we cannot distinguish them throw 
equals function, since equals function cannot use output info.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -190,11 +194,39 @@ public LogicalPlan 
visitRegularQuerySpecification(RegularQuerySpecificationConte
     /**
      * Create an aliased table reference. This is typically used in FROM 
clauses.
      */
+    private LogicalPlan applyAlias(TableAliasContext ctx, LogicalPlan plan) {

Review Comment:
   ```suggestion
       private LogicalPlan withTableAlias(LogicalPlan plan, TableAliasContext 
ctx) {
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/GroupExpression.java:
##########
@@ -168,6 +169,11 @@ public boolean equals(Object o) {
             return false;
         }
         GroupExpression that = (GroupExpression) o;
+        // if the plan is LogicalOlapScan, this == that should be true,
+        // because equals() can not divide UnboundRelation and the plan above.
+        if (plan instanceof LogicalOlapScan) {

Review Comment:
   we should use LogicalRelation instead of LogicalOlapScan.
   BTW, i think PhysicalRelation also need to use equal operator.



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