This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new dbfb0e6ff Remove offset if its zero (#3102)
dbfb0e6ff is described below

commit dbfb0e6ff9bc6fb933ed7cfd15bd9127ac0d443a
Author: gorkem <[email protected]>
AuthorDate: Thu Aug 11 14:30:22 2022 -0700

    Remove offset if its zero (#3102)
    
    * Remove offset if its zero
    
    * Add rust doc
---
 datafusion/optimizer/src/eliminate_limit.rs | 30 ++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/datafusion/optimizer/src/eliminate_limit.rs 
b/datafusion/optimizer/src/eliminate_limit.rs
index 25c6ac240..6d1e1cde5 100644
--- a/datafusion/optimizer/src/eliminate_limit.rs
+++ b/datafusion/optimizer/src/eliminate_limit.rs
@@ -18,6 +18,7 @@
 //! Optimizer rule to replace `LIMIT 0` or
 //! `LIMIT whose ancestor LIMIT's skip is greater than or equal to current's 
fetch`
 //! on a plan with an empty relation.
+//! This rule also removes OFFSET 0 from the [LogicalPlan]
 //! This saves time in planning and executing the query.
 use crate::{OptimizerConfig, OptimizerRule};
 use datafusion_common::Result;
@@ -28,7 +29,8 @@ use datafusion_expr::{
 
 /// Optimization rule that replaces LIMIT 0 or
 /// LIMIT whose ancestor LIMIT's skip is greater than or equal to current's 
fetch
-/// with an [LogicalPlan::EmptyRelation]
+/// with an [LogicalPlan::EmptyRelation].
+/// This rule also removes OFFSET 0 from the [LogicalPlan]
 #[derive(Default)]
 pub struct EliminateLimit;
 
@@ -51,6 +53,7 @@ enum Ancestor {
 /// replaces LIMIT 0 with an [LogicalPlan::EmptyRelation]
 /// replaces LIMIT node whose ancestor LIMIT's skip is greater than or equal 
to current's fetch
 /// with an [LogicalPlan::EmptyRelation]
+/// removes OFFSET 0 from the [LogicalPlan]
 fn eliminate_limit(
     _optimizer: &EliminateLimit,
     ancestor: &Ancestor,
@@ -78,7 +81,13 @@ fn eliminate_limit(
                         }));
                     }
                 }
-                None => {}
+                None => {
+                    match skip {
+                        // If there is no LIMIT and OFFSET is zero, 
LIMIT/OFFSET can be removed
+                        Some(skip) if *skip == 0 => return 
Ok(input.as_ref().clone()),
+                        _ => {}
+                    }
+                }
             }
 
             let expr = plan.expressions();
@@ -169,7 +178,6 @@ mod tests {
             .unwrap()
             .build()
             .unwrap();
-
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
         assert_optimized_plan_eq(&plan, expected);
@@ -310,4 +318,20 @@ mod tests {
             \n    TableScan: test1";
         assert_optimized_plan_eq(&plan, expected);
     }
+
+    #[test]
+    fn remove_zero_offset() {
+        let table_scan = test_table_scan().unwrap();
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])
+            .unwrap()
+            .limit(Some(0), None)
+            .unwrap()
+            .build()
+            .unwrap();
+
+        let expected = "Aggregate: groupBy=[[#test.a]], aggr=[[SUM(#test.b)]]\
+            \n  TableScan: test";
+        assert_optimized_plan_eq(&plan, expected);
+    }
 }

Reply via email to