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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/pattern/GroupExpressionMatching.java:
##########
@@ -102,9 +104,12 @@ public GroupExpressionIterator(Pattern<Plan, Plan> 
pattern, GroupExpression grou
                     Group childGroup = groupExpression.child(i);
                     List<Plan> childrenPlan = matchingChildGroup(pattern, 
childGroup, i);
                     childrenPlans.add(childrenPlan);
+                    if (childrenPlans.isEmpty()) {

Review Comment:
   childrenPlans could not be empty. do u want to add `childrenPlan.isEmpty()`?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -50,50 +55,99 @@ public List<Rule<Plan>> buildRules() {
         return ImmutableList.of(
             RuleType.BINDING_PROJECT_SLOT.build(
                 logicalProject().then(project -> {
-                    List<NamedExpression> boundSlots = 
bind(project.operator.getProjects(), project.children());
-                    return plan(new LogicalProject(boundSlots), 
project.child());
+                    List<NamedExpression> boundSlots =
+                            bind(project.operator.getProjects(), 
project.children(), true);
+                    return plan(new LogicalProject(flatBoundStar(boundSlots)), 
project.child());
                 })
             ),
             RuleType.BINDING_FILTER_SLOT.build(
                 logicalFilter().then(filter -> {
-                    Expression boundPredicates = 
bind(filter.operator.getPredicates(), filter.children());
+                    Expression boundPredicates = bind(
+                            filter.operator.getPredicates(), 
filter.children(), false);
                     return plan(new LogicalFilter(boundPredicates), 
filter.child());
                 })
             ),
             RuleType.BINDING_JOIN_SLOT.build(
                 logicalJoin().then(join -> {
-                    Optional<Expression> cond = 
join.operator.getCondition().map(expr -> bind(expr, join.children()));
+                    Optional<Expression> cond = join.operator.getCondition()
+                            .map(expr -> bind(expr, join.children(), false));
                     LogicalJoin op = new 
LogicalJoin(join.operator.getJoinType(), cond);
                     return plan(op, join.left(), join.right());
                 })
+            ),
+            RuleType.BINDING_AGGREGATE_SLOT.build(
+                logicalAggregate().then(agg -> {
+                    List<Expression> groupBy = bind(
+                            agg.operator.getGroupByExprList(), agg.children(), 
false);
+                    List<NamedExpression> output = bind(
+                            agg.operator.getOutputExpressionList(), 
agg.children(), false);
+                    LogicalAggregate op = new LogicalAggregate(groupBy, 
output);
+                    return plan(op, agg.child());
+                })
+            ),
+            RuleType.BINDING_SORT_SLOT.build(
+                logicalSort().then(sort -> {
+                    List<OrderKey> sortItemList = sort.operator.getOrderKeys()
+                            .stream()
+                            .map(orderKey -> {
+                                Expression item = bind(orderKey.getExpr(), 
sort.children(), false);
+                                return new OrderKey(item, orderKey.isAsc(), 
orderKey.isNullFirst());
+                            }).collect(Collectors.toList());
+
+                    LogicalSort op = new LogicalSort(sortItemList);
+                    return plan(op, sort.child());
+                })
             )
         );
     }
 
-    private <E extends Expression> List<E> bind(List<E> exprList, List<Plan> 
inputs) {
+    private List<NamedExpression> flatBoundStar(List<NamedExpression> 
boundSlots) {
+        return boundSlots
+            .stream()
+            .flatMap(slot -> {
+                if (slot instanceof BoundStar) {
+                    return ((BoundStar) slot).getSlots().stream();
+                } else {
+                    return Stream.of(slot);
+                }
+            }).collect(Collectors.toList());
+    }
+
+    private <E extends Expression> List<E> bind(List<E> exprList, List<Plan> 
inputs, boolean isProjection) {

Review Comment:
   if only project use `isProjection = true`, should we provide a wrapper 
function that set isProjection default to true?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ProjectToGlobalAggregate.java:
##########
@@ -0,0 +1,53 @@
+// 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.operators.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.Plan;
+
+import com.google.common.collect.ImmutableList;
+
+/** ProjectToGlobalAggregate. */
+public class ProjectToGlobalAggregate extends OneAnalysisRuleFactory {

Review Comment:
   coool~



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -114,35 +168,60 @@ public Slot visitUnboundSlot(UnboundSlot unboundSlot, 
Void context) {
 
         @Override
         public Expression visitUnboundStar(UnboundStar unboundStar, Void 
context) {
+            if (!isProjection) {
+                throw new AnalysisException("UnboundStar must exists in 
Projection");
+            }
             List<String> qualifier = unboundStar.getQualifier();
-            List<Slot> boundSlots = Lists.newArrayList();
             switch (qualifier.size()) {
                 case 0: // select *
-                    boundSlots.addAll(boundSlots);
-                    break;
+                    return new BoundStar(boundSlots);
                 case 1: // select table.*
-                    analyzeBoundSlots(qualifier, context);
-                    break;
                 case 2: // select db.table.*
-                    analyzeBoundSlots(qualifier, context);
-                    break;
+                    return bindQualifiedStar(qualifier, context);
                 default:
                     throw new AnalysisException("Not supported qualifier: "
                         + StringUtils.join(qualifier, "."));
             }
-            return null;
         }
 
-        private void analyzeBoundSlots(List<String> qualifier, Void context) {
-            this.boundSlots.stream()
-                    .forEach(slot ->
-                        boundSlots.add(visitUnboundSlot(new UnboundSlot(
-                            ImmutableList.<String>builder()
-                                .addAll(qualifier)
-                                .add(slot.getName())
-                                .build()
-                        ), context))
-                    );
+        private BoundStar bindQualifiedStar(List<String> qualifierStar, Void 
context) {
+            List<Slot> slots = boundSlots.stream().filter(boundSlot -> {
+                switch (qualifierStar.size()) {
+                    // table.*
+                    case 1:
+                        List<String> boundSlotQualifier = 
boundSlot.getQualifier();
+                        switch (boundSlotQualifier.size()) {
+                            // bound slot is `column` and no qualified
+                            case 0: return false;
+                            case 1: // bound slot is `table`.`column`
+                                return 
qualifierStar.get(0).equalsIgnoreCase(boundSlotQualifier.get(0));
+                            case 2:// bound slot is `db`.`table`.`column`
+                                return 
qualifierStar.get(0).equalsIgnoreCase(boundSlotQualifier.get(1));

Review Comment:
   if we have to table, one's qualifier is db.t1 and another's qualifier is t1. 
When we bind t1.*, what will happen? and what should happen? i think maybe we 
need to throw a ambiguous exception?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java:
##########
@@ -46,4 +48,27 @@
 
     NODE_TYPE withChildren(List<NODE_TYPE> children);
 
+    default void foreach(Consumer<TreeNode<NODE_TYPE>> func) {

Review Comment:
   this is a top-down foreach, so maybe it is better to add traverse order info 
into function name



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.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.analyzer.UnboundFunction;
+import org.apache.doris.nereids.operators.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.operators.plans.logical.LogicalProject;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.DefaultExpressionRewriter;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.functions.Sum;
+import org.apache.doris.nereids.trees.plans.Plan;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * BindFunction.
+ */
+public class BindFunction implements AnalysisRuleFactory {
+    @Override
+    public List<Rule<Plan>> buildRules() {
+        return ImmutableList.of(
+            RuleType.BINDING_PROJECT_FUNCTION.build(
+                logicalProject().then(project -> {
+                    List<NamedExpression> boundExpr = 
bind(project.operator.getProjects());
+                    LogicalProject op = new LogicalProject(boundExpr);
+                    return plan(op, project.child());
+                })
+            ),
+            RuleType.BINDING_AGGREGATE_FUNCTION.build(
+                logicalAggregate().then(agg -> {
+                    List<Expression> groupBy = 
bind(agg.operator.getGroupByExprList());
+                    List<NamedExpression> output = 
bind(agg.operator.getOutputExpressionList());
+                    LogicalAggregate op = new LogicalAggregate(groupBy, 
output);
+                    return plan(op, agg.child());
+                })
+            )
+        );
+    }
+
+    private <E extends Expression> List<E> bind(List<E> exprList) {
+        return exprList.stream()
+            .map(expr -> bind(expr))
+            .collect(Collectors.toList());
+    }
+
+    private <E extends Expression> E bind(E expr) {
+        return new FunctionBinder().bind(expr);

Review Comment:
   could use singleton for `new FunctionBinder()`?



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