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


##########
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:
   Yes, I merged SORT_FETCH_ZERO_INSTANCE and SORT_OFFSET_INSTANCE into a 
single SORT_EMPTY_INSTANCE rule that simply checks 
RelMdUtil.isRelDefinitelyEmpty. This covers LIMIT 0, OFFSET >= maxRowCount, and 
empty input with one rule and no extra complexity. SORT_FETCH_ZERO_INSTANCE is 
kept as a deprecated alias for backward compatibility. This impact should be 
minimal.



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