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/12/06 11:47:16 UTC

[GitHub] [doris] Kikyou1997 opened a new pull request, #14862: [feature](nereids) Support order by constant

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

   # Proposed changes
   
   Issue Number: close #xxx
   
   ## Problem summary
   
   Describe your changes.
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: 
       - [ ] Yes
       - [ ] No
       - [ ] I don't know
   2. Has unit tests been added:
       - [ ] Yes
       - [ ] No
       - [ ] No Need
   3. Has document been added or modified:
       - [ ] Yes
       - [ ] No
       - [ ] No Need
   4. Does it need to update dependencies:
       - [ ] Yes
       - [ ] No
   5. Are there any changes that cannot be rolled back:
       - [ ] Yes (If Yes, please explain WHY)
       - [ ] 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] github-actions[bot] commented on pull request #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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

   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] englefly commented on a diff in pull request #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);

Review Comment:
   We could remove constant expression from order-by  key list.
   `select 1, r_name from region order by 2, 1`
   is equivalent to 
   `select 1, r_name from region order by 2`.
   The group-by  runs the same.
   



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);
+                                if (expression instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) expression;
+                                    int ord = i.getIntValue();
+                                    checkOrd(ord, childOutput.size());
+                                    orderKeysWithoutConst
+                                            .add(new OrderKey(childOutput.get(ord - 1), k.isAsc(), k.isNullFirst()));
+                                } else {
+                                    orderKeysWithoutConst.add(k);
+                                }
+                            }
+                            return sortNode.withOrderByKey(orderKeysWithoutConst);
+                        })
+                ))
+                .add(RuleType.ResolveOrdinalInGroupBy.build(
+                        logicalAggregate().then(agg -> {
+                            List<NamedExpression> aggOutput = agg.getOutputExpressions();
+                            List<Expression> groupByWithoutConst = new ArrayList<>();
+                            for (Expression groupByExpr : agg.getGroupByExpressions()) {
+                                groupByExpr = FoldConstantRule.INSTANCE.rewrite(groupByExpr);
+                                if (groupByExpr instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) groupByExpr;
+                                    int ord = i.getIntValue();
+                                    Expression aggExpr = aggOutput.get(ord - 1);
+                                    if (!CollectionUtils.isEmpty(aggExpr.children())
+                                            && aggExpr.child(0) instanceof Literal) {
+                                        continue;
+                                    }
+                                    checkOrd(ord, aggOutput.size());
+                                    groupByWithoutConst.add(aggExpr);
+                                } else {
+                                    groupByWithoutConst.add(groupByExpr);
+                                }
+                            }
+                            return new LogicalAggregate(groupByWithoutConst, agg.getOutputExpressions(), agg.child());
+                        }))).build();
+    }
+
+    private void checkOrd(int ord, int childOutputSize) {
+        if (ord < 1 || ord > childOutputSize) {
+            throw new IllegalStateException(String.format("INVALID ORD: %s", ord));

Review Comment:
   add Rule Name in  exception message please.
   it help us find where goes wrong by log.



-- 
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 #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateGroupByConstant.java:
##########
@@ -50,6 +51,7 @@ public Rule build() {
             List<NamedExpression> outputExprs = aggregate.getOutputExpressions();
             Set<Expression> slotGroupByExprs = Sets.newLinkedHashSet();
             for (Expression expression : groupByExprs) {
+                expression = FoldConstantRule.INSTANCE.rewrite(expression);

Review Comment:
   这个地方碰到没有折叠的常量 难道不该折叠么?



-- 
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 merged pull request #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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


-- 
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 #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -70,6 +70,9 @@ public enum RuleType {
 
     CHECK_ROW_POLICY(RuleTypeClass.REWRITE),
 
+    ResolveOrdinalInOrderBy(RuleTypeClass.REWRITE),
+    ResolveOrdinalInGroupBy(RuleTypeClass.REWRITE),

Review Comment:
   do not reslove at all



-- 
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 #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -70,6 +70,9 @@ public enum RuleType {
 
     CHECK_ROW_POLICY(RuleTypeClass.REWRITE),
 
+    ResolveOrdinalInOrderBy(RuleTypeClass.REWRITE),
+    ResolveOrdinalInGroupBy(RuleTypeClass.REWRITE),

Review Comment:
   name format~



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);
+                                if (expression instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) expression;
+                                    int ord = i.getIntValue();
+                                    checkOrd(ord, childOutput.size());
+                                    orderKeysWithoutConst
+                                            .add(new OrderKey(childOutput.get(ord - 1), k.isAsc(), k.isNullFirst()));
+                                } else {
+                                    orderKeysWithoutConst.add(k);
+                                }
+                            }
+                            return sortNode.withOrderByKey(orderKeysWithoutConst);
+                        })
+                ))
+                .add(RuleType.ResolveOrdinalInGroupBy.build(
+                        logicalAggregate().then(agg -> {
+                            List<NamedExpression> aggOutput = agg.getOutputExpressions();
+                            List<Expression> groupByWithoutConst = new ArrayList<>();
+                            for (Expression groupByExpr : agg.getGroupByExpressions()) {
+                                groupByExpr = FoldConstantRule.INSTANCE.rewrite(groupByExpr);
+                                if (groupByExpr instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) groupByExpr;
+                                    int ord = i.getIntValue();
+                                    Expression aggExpr = aggOutput.get(ord - 1);
+                                    if (!CollectionUtils.isEmpty(aggExpr.children())
+                                            && aggExpr.child(0) instanceof Literal) {
+                                        continue;
+                                    }
+                                    checkOrd(ord, aggOutput.size());
+                                    groupByWithoutConst.add(aggExpr);
+                                } else {
+                                    groupByWithoutConst.add(groupByExpr);
+                                }
+                            }
+                            return new LogicalAggregate(groupByWithoutConst, agg.getOutputExpressions(), agg.child());
+                        }))).build();
+    }
+
+    private void checkOrd(int ord, int childOutputSize) {
+        if (ord < 1 || ord > childOutputSize) {
+            throw new IllegalStateException(String.format("INVALID ORD: %s", ord));

Review Comment:
   not use all uppercase in error msg, and not use abbreviation, leave a msg with more ditail for easy understand.
   the legacy planner's error msg is "ordinal exceeds number of items in select list: 3"



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {

Review Comment:
   in nereids, not use xxxNode as a plan var name. just use sort or logicalSort



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);
+                                if (expression instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) expression;
+                                    int ord = i.getIntValue();
+                                    checkOrd(ord, childOutput.size());
+                                    orderKeysWithoutConst
+                                            .add(new OrderKey(childOutput.get(ord - 1), k.isAsc(), k.isNullFirst()));
+                                } else {
+                                    orderKeysWithoutConst.add(k);
+                                }
+                            }
+                            return sortNode.withOrderByKey(orderKeysWithoutConst);
+                        })
+                ))
+                .add(RuleType.ResolveOrdinalInGroupBy.build(
+                        logicalAggregate().then(agg -> {
+                            List<NamedExpression> aggOutput = agg.getOutputExpressions();
+                            List<Expression> groupByWithoutConst = new ArrayList<>();
+                            for (Expression groupByExpr : agg.getGroupByExpressions()) {
+                                groupByExpr = FoldConstantRule.INSTANCE.rewrite(groupByExpr);
+                                if (groupByExpr instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) groupByExpr;
+                                    int ord = i.getIntValue();
+                                    Expression aggExpr = aggOutput.get(ord - 1);
+                                    if (!CollectionUtils.isEmpty(aggExpr.children())
+                                            && aggExpr.child(0) instanceof Literal) {
+                                        continue;
+                                    }
+                                    checkOrd(ord, aggOutput.size());
+                                    groupByWithoutConst.add(aggExpr);

Review Comment:
   ditto



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);

Review Comment:
   u could get connectcontext in ctx, when u use `thenapply` instead of `then`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);
+                                if (expression instanceof IntegerLikeLiteral) {
+                                    IntegerLikeLiteral i = (IntegerLikeLiteral) expression;
+                                    int ord = i.getIntValue();
+                                    checkOrd(ord, childOutput.size());
+                                    orderKeysWithoutConst
+                                            .add(new OrderKey(childOutput.get(ord - 1), k.isAsc(), k.isNullFirst()));
+                                } else {
+                                    orderKeysWithoutConst.add(k);
+                                }
+                            }
+                            return sortNode.withOrderByKey(orderKeysWithoutConst);

Review Comment:
   if no ordinal in orderkeys, we should return original object for fast equals to reduce plan time



##########
regression-test/suites/nereids_syntax_p0/group_by_constant.groovy:
##########
@@ -29,4 +29,11 @@ suite("group_by_constant") {
     qt_select_1 """ 
         select 'str', sum(lo_tax), lo_orderkey, max(lo_discount), 1 from lineorder, customer group by 3, 5, 'str', 1, lo_orderkey order by lo_orderkey;
     """
+
+    qt_sql """SELECT lo_custkey, lo_partkey, SUM(lo_tax) FROM lineorder GROUP BY 1, 2"""
+
+    qt_sql """SELECT lo_partkey, lo_custkey, SUM(lo_tax) FROM lineorder GROUP BY 1, 2"""
+
+    qt_sql """SELECT lo_partkey, 1, SUM(lo_tax) FROM lineorder GROUP BY 1,  1 + 1"""

Review Comment:
   add some other type constant cases



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateOrderByConstant.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.rewrite.logical;
+
+import org.apache.doris.nereids.properties.OrderKey;
+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.literal.Literal;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * SELECT * FROM lineorder ORDER BY 'f' -> SELECT * FROM lineorder
+ */
+public class EliminateOrderByConstant extends OneRewriteRuleFactory {
+
+    @Override
+    public Rule build() {
+        return logicalSort().then(sort -> {
+            List<OrderKey> orderKeysWithoutConst = sort
+                    .getOrderKeys()
+                    .stream()
+                    .filter(k -> !(k.getExpr() instanceof Literal))

Review Comment:
   use Expression#isConstant



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();
+                            for (OrderKey k : orderKeys) {
+                                Expression expression = k.getExpr();
+                                expression = FoldConstantRule.INSTANCE.rewrite(expression);

Review Comment:
   remove constant is in another rule



##########
regression-test/suites/nereids_syntax_p0/order_by_const.groovy:
##########
@@ -0,0 +1,25 @@
+// 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.
+
+suite("order_by_const") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"

Review Comment:
   enable vectorized engine explicitly



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveOrdinalInOrderByAndGroupBy.java:
##########
@@ -0,0 +1,96 @@
+// 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.properties.OrderKey;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.rewrite.rules.FoldConstantRule;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SELECT col1, col2 FROM t1 ORDER BY 1 -> SELECT col1, col2 FROM t1 ORDER BY col1
+ * SELECT col1, SUM(col2) FROM t1 GROUP BY 1 -> SELECT col1, SUM(col2) FROM t1 GROUP BY col1
+ */
+public class ResolveOrdinalInOrderByAndGroupBy implements AnalysisRuleFactory {
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.<Rule>builder()
+                .add(RuleType.ResolveOrdinalInOrderBy.build(
+                        logicalSort().then(sortNode -> {
+                            List<Slot> childOutput = sortNode.child().getOutput();
+                            List<OrderKey> orderKeys = sortNode.getOrderKeys();
+                            List<OrderKey> orderKeysWithoutConst = new ArrayList<>();

Review Comment:
   if only process ordinal, the var's name should be `orderkeysAfterReplaceOrdinal`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateGroupByConstant.java:
##########
@@ -50,6 +51,7 @@ public Rule build() {
             List<NamedExpression> outputExprs = aggregate.getOutputExpressions();
             Set<Expression> slotGroupByExprs = Sets.newLinkedHashSet();
             for (Expression expression : groupByExprs) {
+                expression = FoldConstantRule.INSTANCE.rewrite(expression);

Review Comment:
   remove constant do not need to do FoldConstantRule, just use Expression#isConstant



-- 
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 #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateGroupByConstant.java:
##########
@@ -50,6 +51,7 @@ public Rule build() {
             List<NamedExpression> outputExprs = aggregate.getOutputExpressions();
             Set<Expression> slotGroupByExprs = Sets.newLinkedHashSet();
             for (Expression expression : groupByExprs) {
+                expression = FoldConstantRule.INSTANCE.rewrite(expression);

Review Comment:
   We could do this after the process for FoldConstant  is complete, for now, there still might be some expression like `1+1` not get folded here



-- 
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] hello-stephen commented on pull request #14862: [feature](nereids) Support order by constant

Posted by GitBox <gi...@apache.org>.
hello-stephen commented on PR #14862:
URL: https://github.com/apache/doris/pull/14862#issuecomment-1339447438

   TeamCity pipeline, clickbench performance test result:
    the sum of best hot time: 36.02 seconds
    load time: 454 seconds
    storage size: 17123356338 Bytes
    https://doris-community-test-1308700295.cos.ap-hongkong.myqcloud.com/tmp/20221206141102_clickbench_pr_58776.html


-- 
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 #14862: [feature](nereids) Support orderby and groupby constant as ordinal of the select list expr

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

   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