xuzifu666 commented on code in PR #5165:
URL: https://github.com/apache/calcite/pull/5165#discussion_r3754805402


##########
core/src/main/java/org/apache/calcite/tools/RelBuilder.java:
##########
@@ -5114,12 +5114,73 @@ private OverCall 
orderBy_(ImmutableList<RexFieldCollation> sortKeys) {
             }
           };
       final RelDataType type = op.inferReturnType(bind);
+      final ImmutableList<RexNode> newPartitionKeys =
+          simplifyPartitionKeys(partitionKeys);
+      final ImmutableList<RexFieldCollation> newSortKeys =
+          simplifySortKeys(newPartitionKeys, sortKeys);
       final RexNode over = getRexBuilder()
-          .makeOver(pos, type, op, operands, partitionKeys, sortKeys,
+          .makeOver(pos, type, op, operands, newPartitionKeys, newSortKeys,
               lowerBound, upperBound, exclude, rows, allowPartial, 
nullWhenCountZero,
               distinct, ignoreNulls);
       return aliasMaybe(over, alias);
     }
+
+    /** Removes constant keys from a window's {@code PARTITION BY}. A constant
+     * partition key places every row in the same partition, so it does not
+     * partition the data and can be dropped. */
+    private ImmutableList<RexNode> simplifyPartitionKeys(
+        List<RexNode> partitionKeys) {
+      final ImmutableList.Builder<RexNode> newKeys = ImmutableList.builder();
+      for (RexNode key : partitionKeys) {
+        if (!RexUtil.isConstant(key)) {
+          newKeys.add(key);
+        }
+      }
+      return newKeys.build();
+    }
+
+    /** Removes redundant keys from a window's {@code ORDER BY}. A sort key is
+     * redundant if it is constant, or if it is functionally determined by the
+     * partition keys and earlier sort keys (those columns are fixed within a
+     * partition, so the key cannot affect the ordering). For example, with
+     * {@code PARTITION BY x, y ORDER BY x + y, z} the key {@code x + y} only
+     * references fixed columns and is dropped, leaving {@code ORDER BY z}. */
+    private ImmutableList<RexFieldCollation> simplifySortKeys(
+        List<RexNode> partitionKeys, List<RexFieldCollation> sortKeys) {
+      // A RANGE frame with a value offset (e.g. RANGE BETWEEN 5 PRECEDING)

Review Comment:
   Thank you for pointing out, I had added a test 
`testProjectOverRangeOffsetKeepsSortKey` to cover it. The new test would fail 
without the following modification.



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