asolimando commented on a change in pull request #2740:
URL: https://github.com/apache/calcite/pull/2740#discussion_r821458303



##########
File path: 
core/src/main/java/org/apache/calcite/rel/rules/SortProjectPullUpConstantsRule.java
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptPredicateList;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.Pair;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NavigableMap;
+import java.util.TreeMap;
+import java.util.stream.Collectors;
+
+/**
+ * Planner rule that removes constant keys from an
+ * {@link Sort}.
+ *
+ * <p>Constant fields are deduced using
+ * {@link RelMetadataQuery#getPulledUpPredicates(RelNode)}; the input does not
+ * need to be a {@link org.apache.calcite.rel.core.Project}.
+ *
+ * <p>Since the transformed relational expression has to match the original
+ * relational expression, the constants are placed in a projection above the
+ * reduced sort. If those constants are not used, another rule will remove
+ * them from the project.
+ */
[email protected]
+public class SortProjectPullUpConstantsRule
+    extends RelRule<SortProjectPullUpConstantsRule.Config>
+    implements TransformationRule {
+
+  /** Creates an SortProjectPullUpConstantsRule. */
+  protected SortProjectPullUpConstantsRule(Config config) {
+    super(config);
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Sort sort = call.rel(0);
+    final RelNode input = call.rel(1);
+
+    final RelCollation collation = sort.getCollation();
+    final List<Integer> fieldCollationIndexes = collation.getFieldCollations()
+        .stream()
+        .map(RelFieldCollation::getFieldIndex)
+        .collect(Collectors.toList());
+    if (fieldCollationIndexes.size() <= 0) {
+      return;
+    }
+
+    final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
+    final RelMetadataQuery mq = call.getMetadataQuery();
+    final RelOptPredicateList predicates =
+        mq.getPulledUpPredicates(input);
+    if (RelOptPredicateList.isEmpty(predicates)) {
+      return;
+    }
+
+    final NavigableMap<Integer, RexNode> map = new TreeMap<>();

Review comment:
       What's the rationale for using a `NavigableMap` \ `TreeMap` over a 
`HashMap` here?
   
   Below you use random access and you never iterate, I think `HashMap` would 
be a better fit unless I missed something.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to