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/07 01:37:06 UTC

[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

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