Apache DataFusion SQL Query Engine
Remove offset if its zero (#3102)
gorkemdbfb0e6ff9bc
diff --git a/datafusion/optimizer/src/eliminate_limit.rs b/datafusion/optimizer/src/eliminate_limit.rsindex 25c6ac2409..6d1e1cde59 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 / limitlet 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);+ }}