xuzifu666 commented on code in PR #5132:
URL: https://github.com/apache/calcite/pull/5132#discussion_r3679470451
##########
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:
The rule targets Sort specifically because PruneEmptyRules traditionally
separates structural pruning (child is empty Values) from metadata-based
pruning. This rule fills the metadata-based gap for Sort: an OFFSET that skips
all rows.
The offset check is there for clarity and to avoid invoking the metadata
provider on Sorts without an OFFSET, where emptiness is already covered by
SORT_INSTANCE or SORT_FETCH_ZERO_INSTANCE. Maybe we could consider unifying the
three Sort rules into one metadata-based rule in the future.
--
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]