You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "mingmwang (via GitHub)" <gi...@apache.org> on 2023/03/15 03:27:21 UTC

[GitHub] [arrow-datafusion] mingmwang opened a new pull request, #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

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

   # 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.
   -->
   
   Closes #.
   Follow up mentioned in the PR
   https://github.com/apache/arrow-datafusion/pull/5462
   
   # 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] alamb commented on a diff in pull request #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1137331638


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -41,15 +42,28 @@ impl OptimizerRule for EliminateDuplicatedExpr {
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Sort(sort) => {
+                let normalized_sort_keys = sort
+                    .expr
+                    .iter()
+                    .map(|e| match e {
+                        Expr::Sort(ExprSort { expr, .. }) => {
+                            Expr::Sort(ExprSort::new(expr.clone(), true, false))

Review Comment:
   I am not sure about always using `true`, `fase`
   
   Using the example
   ```sql
   select * from t1 order by id desc, id, name, id asc;
   select * from t1 order by id asc, id, name, id desc;
   ```
   
   Is the sort key always the same ? It seems like in the first query the sort key should be `id desc, name` and the second it should be `id asc, name`
   
   Maybe I am missing something



-- 
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] mingmwang commented on pull request #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#issuecomment-1469259607

   @jackwener 


-- 
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 #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1138861668


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -41,15 +42,28 @@ impl OptimizerRule for EliminateDuplicatedExpr {
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Sort(sort) => {
+                let normalized_sort_keys = sort
+                    .expr
+                    .iter()
+                    .map(|e| match e {
+                        Expr::Sort(ExprSort { expr, .. }) => {
+                            Expr::Sort(ExprSort::new(expr.clone(), true, false))

Review Comment:
   Maybe we can add a test to cover this case as well?



-- 
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 #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1140042872


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -41,15 +42,28 @@ impl OptimizerRule for EliminateDuplicatedExpr {
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Sort(sort) => {
+                let normalized_sort_keys = sort
+                    .expr
+                    .iter()
+                    .map(|e| match e {
+                        Expr::Sort(ExprSort { expr, .. }) => {
+                            Expr::Sort(ExprSort::new(expr.clone(), true, false))
+                        }
+                        _ => e.clone(),
+                    })
+                    .collect::<Vec<_>>();
+
                 // dedup sort.expr and keep order
                 let mut dedup_expr = Vec::new();
                 let mut dedup_set = HashSet::new();
-                for expr in &sort.expr {
-                    if !dedup_set.contains(expr) {
-                        dedup_expr.push(expr);
-                        dedup_set.insert(expr.clone());
-                    }
-                }
+                sort.expr.iter().zip(normalized_sort_keys.iter()).for_each(
+                    |(expr, normalized_expr)| {

Review Comment:
   I think I missed the case that the expr/normalized expr match 👍 



-- 
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 #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1136614678


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -18,8 +18,9 @@
 use crate::optimizer::ApplyOrder;
 use crate::{OptimizerConfig, OptimizerRule};
 use datafusion_common::Result;
+use datafusion_expr::expr::Sort as ExprSort;

Review Comment:
   ```suggestion
   ```



-- 
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] mingmwang commented on a diff in pull request #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1138877028


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -41,15 +42,28 @@ impl OptimizerRule for EliminateDuplicatedExpr {
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Sort(sort) => {
+                let normalized_sort_keys = sort
+                    .expr
+                    .iter()
+                    .map(|e| match e {
+                        Expr::Sort(ExprSort { expr, .. }) => {
+                            Expr::Sort(ExprSort::new(expr.clone(), true, false))

Review Comment:
   Sure, I will add one tomorrow.



-- 
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 #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607


-- 
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] mingmwang commented on a diff in pull request #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1137987655


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -41,15 +42,28 @@ impl OptimizerRule for EliminateDuplicatedExpr {
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Sort(sort) => {
+                let normalized_sort_keys = sort
+                    .expr
+                    .iter()
+                    .map(|e| match e {
+                        Expr::Sort(ExprSort { expr, .. }) => {
+                            Expr::Sort(ExprSort::new(expr.clone(), true, false))

Review Comment:
   Your conclusion is right.  The `true`, `fase` is just for normalize and dedup.



-- 
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] mingmwang commented on a diff in pull request #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "mingmwang (via GitHub)" <gi...@apache.org>.
mingmwang commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1140450056


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -41,15 +42,28 @@ impl OptimizerRule for EliminateDuplicatedExpr {
     ) -> Result<Option<LogicalPlan>> {
         match plan {
             LogicalPlan::Sort(sort) => {
+                let normalized_sort_keys = sort
+                    .expr
+                    .iter()
+                    .map(|e| match e {
+                        Expr::Sort(ExprSort { expr, .. }) => {
+                            Expr::Sort(ExprSort::new(expr.clone(), true, false))

Review Comment:
   > Maybe we can add a test to cover this case as well?
   
   Done.



-- 
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 #5607: [FOLLOWUP] eliminate the duplicated sort keys in Order By clause

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #5607:
URL: https://github.com/apache/arrow-datafusion/pull/5607#discussion_r1136553437


##########
datafusion/optimizer/src/eliminate_duplicated_expr.rs:
##########
@@ -100,4 +115,23 @@ mod tests {
         \n    TableScan: test";
         assert_optimized_plan_eq(&plan, expected)
     }
+
+    #[test]
+    fn eliminate_sort_exprs_with_options() -> Result<()> {
+        let table_scan = test_table_scan().unwrap();
+        let sort_exprs = vec![
+            Expr::Sort(ExprSort::new(Box::new(col("a")), true, true)),
+            Expr::Sort(ExprSort::new(Box::new(col("b")), true, false)),
+            Expr::Sort(ExprSort::new(Box::new(col("a")), false, false)),
+            Expr::Sort(ExprSort::new(Box::new(col("b")), false, true)),

Review Comment:
   ```suggestion
               col("a").sort(true, true),
               col("b").sort(true, false),
               col("a").sort(false, false),
               col("b").sort(false, true),
   ```



-- 
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