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/08/23 14:40:40 UTC

[GitHub] [doris] morrySnow opened a new pull request, #12013: [feature](Nereids)add normalize aggregate rule

morrySnow opened a new pull request, #12013:
URL: https://github.com/apache/doris/pull/12013

   # Proposed changes
   
   TBD
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: 
       - [ ] Yes
       - [x] No
       - [ ] I don't know
   2. Has unit tests been added:
       - [x] Yes
       - [ ] No
       - [ ] No Need
   3. Has document been added or modified:
       - [ ] Yes
       - [ ] No
       - [x] No Need
   4. Does it need to update dependencies:
       - [ ] Yes
       - [x] No
   5. Are there any changes that cannot be rolled back:
       - [ ] Yes (If Yes, please explain WHY)
       - [x] No
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   
   


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


[GitHub] [doris] Kikyou1997 commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
Kikyou1997 commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953389481


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();

Review Comment:
   I think you  could use Visitor to do the same things



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


[GitHub] [doris] morrySnow commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
morrySnow commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953438977


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();
+            List<Expression> keys = aggregate.getGroupByExpressions();
+            List<NamedExpression> newOutputs = Lists.newArrayList();
+
+            // keys
+            Map<Boolean, List<Expression>> partitionedKeys = keys.stream()
+                    .collect(Collectors.groupingBy(SlotReference.class::isInstance));
+            List<Expression> newKeys = Lists.newArrayList();
+            List<NamedExpression> bottomProjections = Lists.newArrayList();
+            if (partitionedKeys.containsKey(false)) {
+                // process non-SlotReference keys
+                newKeys.addAll(partitionedKeys.get(false).stream()
+                        .map(e -> new Alias(e, e.toSql()))
+                        .peek(a -> substitutionMap.put(a.child(), a.toSlot()))
+                        .peek(bottomProjections::add)
+                        .map(Alias::toSlot)
+                        .collect(Collectors.toList()));
+            }
+            if (partitionedKeys.containsKey(true)) {
+                // process SlotReference keys
+                partitionedKeys.get(true).stream()
+                        .map(SlotReference.class::cast)
+                        .peek(s -> substitutionMap.put(s, s))

Review Comment:
   merge into one need use lambda statement instead of lambda expression. It is look ugly



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


[GitHub] [doris] github-actions[bot] commented on pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #12013:
URL: https://github.com/apache/doris/pull/12013#issuecomment-1225529003

   PR approved by at least one committer and no changes requested.


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


[GitHub] [doris] github-actions[bot] commented on pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #12013:
URL: https://github.com/apache/doris/pull/12013#issuecomment-1225529046

   PR approved by anyone and no changes requested.


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


[GitHub] [doris] morrySnow commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
morrySnow commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953438977


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();
+            List<Expression> keys = aggregate.getGroupByExpressions();
+            List<NamedExpression> newOutputs = Lists.newArrayList();
+
+            // keys
+            Map<Boolean, List<Expression>> partitionedKeys = keys.stream()
+                    .collect(Collectors.groupingBy(SlotReference.class::isInstance));
+            List<Expression> newKeys = Lists.newArrayList();
+            List<NamedExpression> bottomProjections = Lists.newArrayList();
+            if (partitionedKeys.containsKey(false)) {
+                // process non-SlotReference keys
+                newKeys.addAll(partitionedKeys.get(false).stream()
+                        .map(e -> new Alias(e, e.toSql()))
+                        .peek(a -> substitutionMap.put(a.child(), a.toSlot()))
+                        .peek(bottomProjections::add)
+                        .map(Alias::toSlot)
+                        .collect(Collectors.toList()));
+            }
+            if (partitionedKeys.containsKey(true)) {
+                // process SlotReference keys
+                partitionedKeys.get(true).stream()
+                        .map(SlotReference.class::cast)
+                        .peek(s -> substitutionMap.put(s, s))

Review Comment:
   merge into one need use lambda statement instead of lambda expression. It is look urgly



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


[GitHub] [doris] morrySnow commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
morrySnow commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953440653


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();
+            List<Expression> keys = aggregate.getGroupByExpressions();
+            List<NamedExpression> newOutputs = Lists.newArrayList();

Review Comment:
   the new Output's purpose is not to normalize order of output, but to pump up complex expression to top project. And the top project is used to maintain the output order of aggregate.



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


[GitHub] [doris] Kikyou1997 commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
Kikyou1997 commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953425094


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();
+            List<Expression> keys = aggregate.getGroupByExpressions();
+            List<NamedExpression> newOutputs = Lists.newArrayList();

Review Comment:
   rename to `outputsWithNormalizedOrder`?



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


[GitHub] [doris] Kikyou1997 commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
Kikyou1997 commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953389481


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();

Review Comment:
   I think you  could use Visitor to do the same things



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


[GitHub] [doris] morrySnow commented on pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
morrySnow commented on PR #12013:
URL: https://github.com/apache/doris/pull/12013#issuecomment-1224569688

   @Kikyou1997 @924060929 PTAL


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


[GitHub] [doris] Kikyou1997 commented on a diff in pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
Kikyou1997 commented on code in PR #12013:
URL: https://github.com/apache/doris/pull/12013#discussion_r953391467


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java:
##########
@@ -0,0 +1,138 @@
+// 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.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate
+ * to hold to order of aggregate output, since aggregate output's order could change when we do translate.
+ *
+ * Apply this rule could simplify the processing of enforce and translate.
+ *
+ * Original Plan:
+ * Aggregate(
+ *   keys:[k1#1, K2#2 + 1],
+ *   outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6,
+ *            Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8])
+ *
+ * After rule:
+ * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8)
+ * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11])
+ *     +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3)
+ *
+ * More example could get from UT {@link NormalizeAggregateTest}
+ */
+public class NormalizeAggregate extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> {
+            // substitution map used to substitute expression in aggregate's output to use it as top projections
+            Map<Expression, Expression> substitutionMap = Maps.newHashMap();
+            List<Expression> keys = aggregate.getGroupByExpressions();
+            List<NamedExpression> newOutputs = Lists.newArrayList();
+
+            // keys
+            Map<Boolean, List<Expression>> partitionedKeys = keys.stream()
+                    .collect(Collectors.groupingBy(SlotReference.class::isInstance));
+            List<Expression> newKeys = Lists.newArrayList();
+            List<NamedExpression> bottomProjections = Lists.newArrayList();
+            if (partitionedKeys.containsKey(false)) {
+                // process non-SlotReference keys
+                newKeys.addAll(partitionedKeys.get(false).stream()
+                        .map(e -> new Alias(e, e.toSql()))
+                        .peek(a -> substitutionMap.put(a.child(), a.toSlot()))
+                        .peek(bottomProjections::add)
+                        .map(Alias::toSlot)
+                        .collect(Collectors.toList()));
+            }
+            if (partitionedKeys.containsKey(true)) {
+                // process SlotReference keys
+                partitionedKeys.get(true).stream()
+                        .map(SlotReference.class::cast)
+                        .peek(s -> substitutionMap.put(s, s))

Review Comment:
   Can merge two peek operation into one



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


[GitHub] [doris] yiguolei merged pull request #12013: [feature](Nereids)add normalize aggregate rule

Posted by GitBox <gi...@apache.org>.
yiguolei merged PR #12013:
URL: https://github.com/apache/doris/pull/12013


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