mihaibudiu commented on code in PR #5132:
URL: https://github.com/apache/calcite/pull/5132#discussion_r3677691244


##########
core/src/main/java/org/apache/calcite/rel/rules/PruneEmptyRules.java:
##########
@@ -542,6 +560,30 @@ public interface SortFetchZeroRuleConfig extends 
PruneEmptyRule.Config {
     }
   }
 
+  /** Configuration for a rule that prunes a Sort if its {@code OFFSET} skips 
at
+   * least as many rows as its input can ever produce. */
+  @Value.Immutable
+  public interface SortOffsetGreaterThanMaxRowsRuleConfig extends 
PruneEmptyRule.Config {
+    SortOffsetGreaterThanMaxRowsRuleConfig DEFAULT =
+        ImmutableSortOffsetGreaterThanMaxRowsRuleConfig.of()
+            .withOperandSupplier(b -> b.operand(Sort.class).anyInputs())
+            .withDescription("PruneSortOffsetGreaterThanMaxRows");
+
+    @Override default PruneEmptyRule toRule() {
+      return new RemoveEmptySingleRule(this) {
+        @Override public boolean matches(final RelOptRuleCall call) {
+          final Sort sort = call.rel(0);
+          // Only consider a static (non-dynamic) OFFSET. If the offset skips 
at
+          // least as many rows as the input can ever produce, the Sort returns
+          // no rows. RelMdMaxRowCount#getMaxRowCount(Sort) already subtracts 
the
+          // offset from the input row count, so the Sort is definitely empty.
+          return sort.offset instanceof RexLiteral
+              && RelMdUtil.isRelDefinitelyEmpty(call.getMetadataQuery(), sort);

Review Comment:
   If something is definitely empty, why does it matter if it's a sort or some 
other operation? Why does the offset matter?



##########
core/src/main/java/org/apache/calcite/rel/rules/PruneEmptyRules.java:
##########
@@ -231,6 +231,24 @@ private static boolean isEmpty(RelNode node) {
   public static final RelOptRule SORT_FETCH_ZERO_INSTANCE =
       SortFetchZeroRuleConfig.DEFAULT.toRule();
 
+  /**
+   * Rule that converts a {@link org.apache.calcite.rel.core.Sort}
+   * to empty if its {@code OFFSET} is greater than or equal to the maximum
+   * number of rows its input can produce, so that all rows are skipped.
+   *
+   * <p>Examples:
+   *
+   * <ul>
+   * <li>Sort[offset=5](input with at most 2 rows) becomes Empty
+   * </ul>
+   *
+   * <p>It relies on {@link org.apache.calcite.rel.metadata.RelMdMaxRowCount}

Review Comment:
   How the rule is implemented should not be documented here, but in the rule 
body.



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