You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/22 10:52:18 UTC

[GitHub] [arrow-datafusion] jackwener opened a new pull request, #4324: reimplment eliminate_limit to remove `global-state`.

jackwener opened a new pull request, #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Close #4264.
   Part of #4267 
   
   # Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#discussion_r1029180997


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -157,180 +80,148 @@ mod tests {
         sum,
     };
 
-    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
-        let rule = EliminateLimit::new();
-        let optimized_plan = rule
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> {
+        let optimized_plan = EliminateLimit::new()
+            .optimize(plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{:?}", optimized_plan);
+        assert_eq!(formatted_plan, expected);
+        assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
+    }
+
+    fn assert_optimized_plan_eq_with_pushdown(
+        plan: &LogicalPlan,
+        expected: &str,
+    ) -> Result<()> {
+        let optimized_plan = LimitPushDown::new()
             .optimize(plan, &mut OptimizerConfig::new())
             .expect("failed to optimize plan");
+        let optimized_plan = EliminateLimit::new()
+            .optimize(&optimized_plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
         let formatted_plan = format!("{:?}", optimized_plan);
         assert_eq!(formatted_plan, expected);
         assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
     }
 
     #[test]
-    fn limit_0_root() {
+    fn limit_0_root() -> Result<()> {
         let table_scan = test_table_scan().unwrap();
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .build()?;
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_0_nested() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_0_nested() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan1 = LogicalPlanBuilder::from(table_scan.clone())
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .build()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .union(plan1)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .union(plan1)?
+            .build()?;
 
         // Left side is removed
         let expected = "Union\
             \n  EmptyRelation\
             \n  Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n    TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_fetch_with_ancestor_limit_skip() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_fetch_with_ancestor_limit_fetch() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .limit(2, None)
-            .unwrap()
-            .build()
-            .unwrap();
-
-        // No aggregate / scan / limit
-        let expected = "Limit: skip=2, fetch=None\
-            \n  EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn multi_limit_offset_sort_eliminate() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=2, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_fetch_with_ancestor_limit_fetch() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(0, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .sort(vec![col("a")])?
+            .limit(0, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=0, fetch=1\
             \n  Sort: test.a\
             \n    Limit: skip=0, fetch=2\
             \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n        TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_join_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let table_scan_inner = test_table_scan_with_name("test1")?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=3, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_join_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
-        let table_scan_inner = test_table_scan_with_name("test1").unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .limit(2, Some(1))
-            .unwrap()
+            .limit(2, Some(1))?
             .join_using(
                 &table_scan_inner,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
-            )
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            )?
+            .limit(3, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=3, fetch=1\
             \n  Inner Join: Using test.a = test1.a\
             \n    Limit: skip=2, fetch=1\
             \n      TableScan: test\
             \n    TableScan: test1";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn remove_zero_offset() {
-        let table_scan = test_table_scan().unwrap();
+    fn remove_zero_offset() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, None)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, None)?
+            .build()?;
 
         let expected = "Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n  TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
+    }
+
+    #[test]
+    fn limit_fetch_with_ancestor_limit_skip() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .limit(2, None)?
+            .build()?;
+
+        // No aggregate / scan / limit
+        let expected = "EmptyRelation";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)
+    }
+
+    #[test]
+    fn limit_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(2, Some(1))?
+            .sort(vec![col("a")])?
+            .limit(3, Some(1))?
+            .build()?;
+
+        // After remove global-state, we don't record the parent <skip, fetch>
+        // So, bottom don't know parent info, so can't eliminate.
+        let expected = "Limit: skip=3, fetch=1\
+        \n  Sort: test.a, fetch=4\
+        \n    Limit: skip=2, fetch=1\
+        \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
+        \n        TableScan: test";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)

Review Comment:
   Some case need to record information cross plannode.
   Exist regression, but it's trivial.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] waynexia commented on a diff in pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
waynexia commented on code in PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#discussion_r1029316069


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -157,180 +80,148 @@ mod tests {
         sum,
     };
 
-    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
-        let rule = EliminateLimit::new();
-        let optimized_plan = rule
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> {
+        let optimized_plan = EliminateLimit::new()
+            .optimize(plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{:?}", optimized_plan);
+        assert_eq!(formatted_plan, expected);
+        assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
+    }
+
+    fn assert_optimized_plan_eq_with_pushdown(
+        plan: &LogicalPlan,
+        expected: &str,
+    ) -> Result<()> {
+        let optimized_plan = LimitPushDown::new()
             .optimize(plan, &mut OptimizerConfig::new())
             .expect("failed to optimize plan");
+        let optimized_plan = EliminateLimit::new()
+            .optimize(&optimized_plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
         let formatted_plan = format!("{:?}", optimized_plan);
         assert_eq!(formatted_plan, expected);
         assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
     }
 
     #[test]
-    fn limit_0_root() {
+    fn limit_0_root() -> Result<()> {
         let table_scan = test_table_scan().unwrap();
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .build()?;
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_0_nested() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_0_nested() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan1 = LogicalPlanBuilder::from(table_scan.clone())
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .build()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .union(plan1)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .union(plan1)?
+            .build()?;
 
         // Left side is removed
         let expected = "Union\
             \n  EmptyRelation\
             \n  Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n    TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_fetch_with_ancestor_limit_skip() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_fetch_with_ancestor_limit_fetch() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .limit(2, None)
-            .unwrap()
-            .build()
-            .unwrap();
-
-        // No aggregate / scan / limit
-        let expected = "Limit: skip=2, fetch=None\
-            \n  EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn multi_limit_offset_sort_eliminate() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=2, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_fetch_with_ancestor_limit_fetch() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(0, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .sort(vec![col("a")])?
+            .limit(0, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=0, fetch=1\
             \n  Sort: test.a\
             \n    Limit: skip=0, fetch=2\
             \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n        TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_join_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let table_scan_inner = test_table_scan_with_name("test1")?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=3, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_join_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
-        let table_scan_inner = test_table_scan_with_name("test1").unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .limit(2, Some(1))
-            .unwrap()
+            .limit(2, Some(1))?
             .join_using(
                 &table_scan_inner,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
-            )
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            )?
+            .limit(3, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=3, fetch=1\
             \n  Inner Join: Using test.a = test1.a\
             \n    Limit: skip=2, fetch=1\
             \n      TableScan: test\
             \n    TableScan: test1";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn remove_zero_offset() {
-        let table_scan = test_table_scan().unwrap();
+    fn remove_zero_offset() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, None)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, None)?
+            .build()?;
 
         let expected = "Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n  TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
+    }
+
+    #[test]
+    fn limit_fetch_with_ancestor_limit_skip() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .limit(2, None)?
+            .build()?;
+
+        // No aggregate / scan / limit
+        let expected = "EmptyRelation";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)
+    }
+
+    #[test]
+    fn limit_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(2, Some(1))?
+            .sort(vec![col("a")])?
+            .limit(3, Some(1))?
+            .build()?;
+
+        // After remove global-state, we don't record the parent <skip, fetch>
+        // So, bottom don't know parent info, so can't eliminate.
+        let expected = "Limit: skip=3, fetch=1\
+        \n  Sort: test.a, fetch=4\
+        \n    Limit: skip=2, fetch=1\
+        \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
+        \n        TableScan: test";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)

Review Comment:
   Removing the global state does look good in some scenarios, but maybe we should keep it where it's necessary?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb merged pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
alamb merged PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#discussion_r1029975021


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -157,180 +80,148 @@ mod tests {
         sum,
     };
 
-    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
-        let rule = EliminateLimit::new();
-        let optimized_plan = rule
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> {
+        let optimized_plan = EliminateLimit::new()
+            .optimize(plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{:?}", optimized_plan);
+        assert_eq!(formatted_plan, expected);
+        assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
+    }
+
+    fn assert_optimized_plan_eq_with_pushdown(
+        plan: &LogicalPlan,
+        expected: &str,
+    ) -> Result<()> {
+        let optimized_plan = LimitPushDown::new()
             .optimize(plan, &mut OptimizerConfig::new())
             .expect("failed to optimize plan");
+        let optimized_plan = EliminateLimit::new()
+            .optimize(&optimized_plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
         let formatted_plan = format!("{:?}", optimized_plan);
         assert_eq!(formatted_plan, expected);
         assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
     }
 
     #[test]
-    fn limit_0_root() {
+    fn limit_0_root() -> Result<()> {
         let table_scan = test_table_scan().unwrap();
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .build()?;
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_0_nested() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_0_nested() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan1 = LogicalPlanBuilder::from(table_scan.clone())
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .build()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .union(plan1)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .union(plan1)?
+            .build()?;
 
         // Left side is removed
         let expected = "Union\
             \n  EmptyRelation\
             \n  Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n    TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_fetch_with_ancestor_limit_skip() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_fetch_with_ancestor_limit_fetch() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .limit(2, None)
-            .unwrap()
-            .build()
-            .unwrap();
-
-        // No aggregate / scan / limit
-        let expected = "Limit: skip=2, fetch=None\
-            \n  EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn multi_limit_offset_sort_eliminate() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=2, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_fetch_with_ancestor_limit_fetch() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(0, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .sort(vec![col("a")])?
+            .limit(0, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=0, fetch=1\
             \n  Sort: test.a\
             \n    Limit: skip=0, fetch=2\
             \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n        TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_join_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let table_scan_inner = test_table_scan_with_name("test1")?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=3, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_join_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
-        let table_scan_inner = test_table_scan_with_name("test1").unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .limit(2, Some(1))
-            .unwrap()
+            .limit(2, Some(1))?
             .join_using(
                 &table_scan_inner,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
-            )
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            )?
+            .limit(3, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=3, fetch=1\
             \n  Inner Join: Using test.a = test1.a\
             \n    Limit: skip=2, fetch=1\
             \n      TableScan: test\
             \n    TableScan: test1";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn remove_zero_offset() {
-        let table_scan = test_table_scan().unwrap();
+    fn remove_zero_offset() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, None)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, None)?
+            .build()?;
 
         let expected = "Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n  TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
+    }
+
+    #[test]
+    fn limit_fetch_with_ancestor_limit_skip() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .limit(2, None)?
+            .build()?;
+
+        // No aggregate / scan / limit
+        let expected = "EmptyRelation";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)
+    }
+
+    #[test]
+    fn limit_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(2, Some(1))?
+            .sort(vec![col("a")])?
+            .limit(3, Some(1))?
+            .build()?;
+
+        // After remove global-state, we don't record the parent <skip, fetch>
+        // So, bottom don't know parent info, so can't eliminate.
+        let expected = "Limit: skip=3, fetch=1\
+        \n  Sort: test.a, fetch=4\
+        \n    Limit: skip=2, fetch=1\
+        \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
+        \n        TableScan: test";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)

Review Comment:
   basically unnecessary.
   - Currently global-state is passed with parameters in a recursive function,  can't only some places are reserved
   - In fact, it basically does not affect, and other optimizers basically do not consider these corner case.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
jackwener commented on PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#issuecomment-1328239147

   Sorry for change UT. It indeed make review hard. I restore them.
   
   original
   - I delete `multi_limit_offset_sort_eliminate`, because I think difference between it and `limit_fetch_with_ancestor_limit_fetch` is very small.
   - reorder is because : I put the more comprehensive tests later


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] ursabot commented on pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#issuecomment-1330832817

   Benchmark runs are scheduled for baseline = dd3f72ad13df3e3ab2efde73eba546012eaf10fd and contender = 02da32e2ef7bc40a89f024a3c6f1a9540412f636. 02da32e2ef7bc40a89f024a3c6f1a9540412f636 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/f1aa0d8c75614ed9bfa4c7638fdc51aa...1a14d1c7d5b544f79976ac26472a5034/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/442c3f64f9e043aba04da9f9c6fcf5a5...6125d0bb3cfd4142af2147e14f4a7702/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/c5563624f52a45f7b45868995344992e...6b6ee46569104275b94d47661fa554a5/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/6446db20287341b3b9cb6d988a8018fb...ab456070d7c94bc5b55c9b98c536c442/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#discussion_r1034897947


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -157,180 +80,166 @@ mod tests {
         sum,
     };
 
-    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
-        let rule = EliminateLimit::new();
-        let optimized_plan = rule
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> {
+        let optimized_plan = EliminateLimit::new()
             .optimize(plan, &mut OptimizerConfig::new())
             .expect("failed to optimize plan");
         let formatted_plan = format!("{:?}", optimized_plan);
         assert_eq!(formatted_plan, expected);
         assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
+    }
+
+    fn assert_optimized_plan_eq_with_pushdown(
+        plan: &LogicalPlan,
+        expected: &str,
+    ) -> Result<()> {
+        let optimized_plan = LimitPushDown::new()
+            .optimize(plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let optimized_plan = EliminateLimit::new()
+            .optimize(&optimized_plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{:?}", optimized_plan);
+        assert_eq!(formatted_plan, expected);
+        assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
     }
 
     #[test]
-    fn limit_0_root() {
+    fn limit_0_root() -> Result<()> {
         let table_scan = test_table_scan().unwrap();
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .build()?;
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_0_nested() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_0_nested() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan1 = LogicalPlanBuilder::from(table_scan.clone())
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .build()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .union(plan1)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .union(plan1)?
+            .build()?;
 
         // Left side is removed
         let expected = "Union\
             \n  EmptyRelation\
             \n  Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n    TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_fetch_with_ancestor_limit_skip() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_fetch_with_ancestor_limit_skip() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .limit(2, None)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .limit(2, None)?
+            .build()?;
 
         // No aggregate / scan / limit
-        let expected = "Limit: skip=2, fetch=None\
-            \n  EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        let expected = "EmptyRelation";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)
     }
 
     #[test]
-    fn multi_limit_offset_sort_eliminate() {
-        let table_scan = test_table_scan().unwrap();
+    fn multi_limit_offset_sort_eliminate() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .sort(vec![col("a")])?
+            .limit(2, Some(1))?
+            .build()?;
+
+        // After remove global-state, we don't record the parent <skip, fetch>
+        // So, bottom don't know parent info, so can't eliminate.
         let expected = "Limit: skip=2, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        \n  Sort: test.a, fetch=3\
+        \n    Limit: skip=0, fetch=2\

Review Comment:
   It took me a while to convince myself this test change was correct
   
   The original plan `fetch`es 2 rows after the aggregate, so the output will only by 2 rows and then does a `skip 2`, `fetch 1` after wards -- so actually this plan will always return no rows but the plan here looks fine and the original actually looks wrong



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4324: reimplment `eliminate_limit` to remove `global-state`.

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4324:
URL: https://github.com/apache/arrow-datafusion/pull/4324#discussion_r1032915489


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -157,180 +80,148 @@ mod tests {
         sum,
     };
 
-    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
-        let rule = EliminateLimit::new();
-        let optimized_plan = rule
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> {
+        let optimized_plan = EliminateLimit::new()
+            .optimize(plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{:?}", optimized_plan);
+        assert_eq!(formatted_plan, expected);
+        assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
+    }
+
+    fn assert_optimized_plan_eq_with_pushdown(
+        plan: &LogicalPlan,
+        expected: &str,
+    ) -> Result<()> {
+        let optimized_plan = LimitPushDown::new()
             .optimize(plan, &mut OptimizerConfig::new())
             .expect("failed to optimize plan");
+        let optimized_plan = EliminateLimit::new()
+            .optimize(&optimized_plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
         let formatted_plan = format!("{:?}", optimized_plan);
         assert_eq!(formatted_plan, expected);
         assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
     }
 
     #[test]
-    fn limit_0_root() {
+    fn limit_0_root() -> Result<()> {
         let table_scan = test_table_scan().unwrap();
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .build()?;
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_0_nested() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_0_nested() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan1 = LogicalPlanBuilder::from(table_scan.clone())
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .build()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .union(plan1)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .union(plan1)?
+            .build()?;
 
         // Left side is removed
         let expected = "Union\
             \n  EmptyRelation\
             \n  Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n    TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_fetch_with_ancestor_limit_skip() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_fetch_with_ancestor_limit_fetch() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .limit(2, None)
-            .unwrap()
-            .build()
-            .unwrap();
-
-        // No aggregate / scan / limit
-        let expected = "Limit: skip=2, fetch=None\
-            \n  EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn multi_limit_offset_sort_eliminate() {

Review Comment:
   why was this test removed?



##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -157,180 +80,148 @@ mod tests {
         sum,
     };
 
-    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
-        let rule = EliminateLimit::new();
-        let optimized_plan = rule
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) -> Result<()> {
+        let optimized_plan = EliminateLimit::new()
+            .optimize(plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{:?}", optimized_plan);
+        assert_eq!(formatted_plan, expected);
+        assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
+    }
+
+    fn assert_optimized_plan_eq_with_pushdown(
+        plan: &LogicalPlan,
+        expected: &str,
+    ) -> Result<()> {
+        let optimized_plan = LimitPushDown::new()
             .optimize(plan, &mut OptimizerConfig::new())
             .expect("failed to optimize plan");
+        let optimized_plan = EliminateLimit::new()
+            .optimize(&optimized_plan, &mut OptimizerConfig::new())
+            .expect("failed to optimize plan");
         let formatted_plan = format!("{:?}", optimized_plan);
         assert_eq!(formatted_plan, expected);
         assert_eq!(plan.schema(), optimized_plan.schema());
+        Ok(())
     }
 
     #[test]
-    fn limit_0_root() {
+    fn limit_0_root() -> Result<()> {
         let table_scan = test_table_scan().unwrap();
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .build()?;
         // No aggregate / scan / limit
         let expected = "EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_0_nested() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_0_nested() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan1 = LogicalPlanBuilder::from(table_scan.clone())
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .build()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(0))
-            .unwrap()
-            .union(plan1)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(0))?
+            .union(plan1)?
+            .build()?;
 
         // Left side is removed
         let expected = "Union\
             \n  EmptyRelation\
             \n  Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n    TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_fetch_with_ancestor_limit_skip() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_fetch_with_ancestor_limit_fetch() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .limit(2, None)
-            .unwrap()
-            .build()
-            .unwrap();
-
-        // No aggregate / scan / limit
-        let expected = "Limit: skip=2, fetch=None\
-            \n  EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn multi_limit_offset_sort_eliminate() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=2, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_fetch_with_ancestor_limit_fetch() {
-        let table_scan = test_table_scan().unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, Some(2))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(0, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .sort(vec![col("a")])?
+            .limit(0, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=0, fetch=1\
             \n  Sort: test.a\
             \n    Limit: skip=0, fetch=2\
             \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n        TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn limit_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
+    fn limit_join_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let table_scan_inner = test_table_scan_with_name("test1")?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(2, Some(1))
-            .unwrap()
-            .sort(vec![col("a")])
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
-
-        let expected = "Limit: skip=3, fetch=1\
-            \n  Sort: test.a\
-            \n    EmptyRelation";
-        assert_optimized_plan_eq(&plan, expected);
-    }
-
-    #[test]
-    fn limit_join_with_ancestor_limit() {
-        let table_scan = test_table_scan().unwrap();
-        let table_scan_inner = test_table_scan_with_name("test1").unwrap();
-        let plan = LogicalPlanBuilder::from(table_scan)
-            .limit(2, Some(1))
-            .unwrap()
+            .limit(2, Some(1))?
             .join_using(
                 &table_scan_inner,
                 JoinType::Inner,
                 vec![Column::from_name("a".to_string())],
-            )
-            .unwrap()
-            .limit(3, Some(1))
-            .unwrap()
-            .build()
-            .unwrap();
+            )?
+            .limit(3, Some(1))?
+            .build()?;
 
         let expected = "Limit: skip=3, fetch=1\
             \n  Inner Join: Using test.a = test1.a\
             \n    Limit: skip=2, fetch=1\
             \n      TableScan: test\
             \n    TableScan: test1";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
     }
 
     #[test]
-    fn remove_zero_offset() {
-        let table_scan = test_table_scan().unwrap();
+    fn remove_zero_offset() -> Result<()> {
+        let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .aggregate(vec![col("a")], vec![sum(col("b"))])
-            .unwrap()
-            .limit(0, None)
-            .unwrap()
-            .build()
-            .unwrap();
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, None)?
+            .build()?;
 
         let expected = "Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
             \n  TableScan: test";
-        assert_optimized_plan_eq(&plan, expected);
+        assert_optimized_plan_eq(&plan, expected)
+    }
+
+    #[test]
+    fn limit_fetch_with_ancestor_limit_skip() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(0, Some(2))?
+            .limit(2, None)?
+            .build()?;
+
+        // No aggregate / scan / limit
+        let expected = "EmptyRelation";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)
+    }
+
+    #[test]
+    fn limit_with_ancestor_limit() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(vec![col("a")], vec![sum(col("b"))])?
+            .limit(2, Some(1))?
+            .sort(vec![col("a")])?
+            .limit(3, Some(1))?
+            .build()?;
+
+        // After remove global-state, we don't record the parent <skip, fetch>
+        // So, bottom don't know parent info, so can't eliminate.
+        let expected = "Limit: skip=3, fetch=1\
+        \n  Sort: test.a, fetch=4\
+        \n    Limit: skip=2, fetch=1\
+        \n      Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b)]]\
+        \n        TableScan: test";
+        assert_optimized_plan_eq_with_pushdown(&plan, expected)

Review Comment:
   I think it would make it easier to see the changes in this PR if the order of the tests was the same (not sure if you reordered the tests on purpose or if that is something related to how github is displaying them)



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org