Skip to content

Apache DataFusion SQL Query Engine

Fix `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. (#25577)

Mason700ad9cf2adb
diff --git a/datafusion/optimizer/src/eliminate_limit.rs b/datafusion/optimizer/src/eliminate_limit.rs
index 1ec3c85608..951c1443b4 100644
--- a/datafusion/optimizer/src/eliminate_limit.rs
+++ b/datafusion/optimizer/src/eliminate_limit.rs
@@ -34,7 +34,7 @@ use std::sync::Arc;
pub struct EliminateLimit;
impl EliminateLimit {
- #[expect(missing_docs)]
+ /// Create a new `EliminateLimit`
pub fn new() -> Self {
Self {}
}
@@ -76,9 +76,8 @@ impl OptimizerRule for EliminateLimit {
}
} else if matches!(limit.get_skip_type()?, SkipType::Literal(0)) {
// If fetch is `None` and skip is 0, then Limit takes no effect and
- // we can remove it. Its input also can be Limit, so we should apply again.
- #[expect(clippy::used_underscore_binding)]
- return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);
+ // we can remove it.
+ return Ok(Transformed::yes(Arc::unwrap_or_clone(limit.input)));
}
Ok(Transformed::no(LogicalPlan::Limit(limit)))
}
@@ -301,4 +300,21 @@ mod tests {
"
)
}
+
+ #[test]
+ fn remove_noop_limit_under_extension() -> Result<()> {
+ let table_scan = test_table_scan()?;
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .limit(0, None)?
+ .build()?;
+ let plan = user_defined::new(plan);
+
+ assert_optimized_plan_equal!(
+ plan,
+ @ r"
+ TestUserDefined
+ TableScan: test
+ "
+ )
+ }
}
diff --git a/datafusion/optimizer/src/optimizer.rs b/datafusion/optimizer/src/optimizer.rs
index b6e93a3b16..6bf68c7d6d 100644
--- a/datafusion/optimizer/src/optimizer.rs
+++ b/datafusion/optimizer/src/optimizer.rs
@@ -466,9 +466,7 @@ fn map_children_mut<F: FnMut(&mut LogicalPlan) -> Result<bool>>(
Ok(plan)
})
.collect::<Result<Vec<_>>>()?;
- if changed {
- *node = node.with_exprs_and_inputs(exprs, new_inputs)?;
- }
+ *node = node.with_exprs_and_inputs(exprs, new_inputs)?;
changed
}
}