jackwener commented on code in PR #4618:
URL: https://github.com/apache/arrow-datafusion/pull/4618#discussion_r1048587713
##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -40,37 +42,59 @@ impl OptimizerRule for EliminateLimit {
fn optimize(
&self,
plan: &LogicalPlan,
- optimizer_config: &mut OptimizerConfig,
+ _optimizer_config: &mut OptimizerConfig,
) -> Result<LogicalPlan> {
- if let LogicalPlan::Limit(limit) = plan {
- match limit.fetch {
- Some(fetch) => {
- if fetch == 0 {
- return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
- produce_one_row: false,
- schema: limit.input.schema().clone(),
- }));
- }
+ Ok(self
+ .try_optimize(plan, _optimizer_config)?
+ .unwrap_or_else(|| plan.clone()))
+ }
+
+ fn try_optimize(
+ &self,
+ plan: &LogicalPlan,
+ _optimizer_config: &mut OptimizerConfig,
+ ) -> Result<Option<LogicalPlan>> {
+ let limit = match plan {
+ LogicalPlan::Limit(limit) => limit,
+ _ => return Ok(None),
+ };
+
+ match limit.fetch {
+ Some(fetch) => {
+ if fetch == 0 {
+ return Ok(Some(LogicalPlan::EmptyRelation(EmptyRelation {
+ produce_one_row: false,
+ schema: limit.input.schema().clone(),
+ })));
}
- None => {
- if limit.skip == 0 {
- let input = &*limit.input;
- return utils::optimize_children(self, input,
optimizer_config);
- }
+ }
+ None => {
+ if limit.skip == 0 {
+ let input = limit.input.as_ref();
+ // input also can be Limit, so we should apply again.
+ return Ok(Some(
+ self.try_optimize(input, _optimizer_config)?
+ .unwrap_or_else(|| input.clone()),
+ ));
Review Comment:
🎉we don't need to recurse optimize children inside rule.
--
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]