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


##########
core/src/main/java/org/apache/calcite/rel/rules/PruneEmptyRules.java:
##########
@@ -525,20 +541,20 @@ public interface IntersectEmptyPruneRuleConfig extends 
PruneEmptyRule.Config {
     }
   }
 
-  /** Configuration for a rule that prunes a Sort if it has limit 0. */
+  /** Configuration for a rule that prunes a Sort if it is definitely empty,
+   * for example because its input is empty, it has {@code LIMIT 0}, or its
+   * {@code OFFSET} skips more rows than the input can produce. */
   @Value.Immutable
-  public interface SortFetchZeroRuleConfig extends PruneEmptyRule.Config {
-    SortFetchZeroRuleConfig DEFAULT = ImmutableSortFetchZeroRuleConfig.of()
+  public interface SortEmptyRuleConfig extends PruneEmptyRule.Config {
+    SortEmptyRuleConfig DEFAULT = ImmutableSortEmptyRuleConfig.of()
         .withOperandSupplier(b -> b.operand(Sort.class).anyInputs())
-        .withDescription("PruneSortLimit0");
+        .withDescription("PruneSortIfEmpty");
 
     @Override default PruneEmptyRule toRule() {
       return new RemoveEmptySingleRule(this) {
         @Override public boolean matches(final RelOptRuleCall call) {
-          Sort sort = call.rel(0);
-          return sort.fetch != null
-              && !(sort.fetch instanceof RexDynamicParam)
-              && 
RexLiteral.bigDecimalValue(sort.fetch).equals(BigDecimal.ZERO);
+          final Sort sort = call.rel(0);

Review Comment:
   You're right that `isRelDefinitelyEmpty` itself is generic. 
   The reason this rule is specific to Sort is that the replacement logic is 
not generic: `RemoveEmptySingleRule` can safely turn any `SingleRel` into an 
empty Values, but Join, Union, Intersect, Minus, and Aggregate each need their 
own logic when they are empty. This rule reuses RemoveEmptySingleRule because 
Sort is a SingleRel. A fully generic prune anything that is definitely empty 
rule would need a way to know how to rewrite each operator type correctly. The 
existing EMPTY_TABLE_INSTANCE follows the same pattern: it uses 
isRelDefinitelyEmpty but only for TableScan.



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