alamb commented on code in PR #14038:
URL: https://github.com/apache/datafusion/pull/14038#discussion_r1907236855


##########
datafusion/physical-expr-common/src/sort_expr.rs:
##########
@@ -540,6 +557,33 @@ impl LexRequirement {
                 .collect(),
         )
     }
+
+    /// Constructs a duplicate-free `LexOrderingReq` by filtering out
+    /// duplicate entries that have same physical expression inside.
+    ///
+    /// For example, `vec![a Some(ASC), a Some(DESC)]` collapses to `vec![a
+    /// Some(ASC)]`.
+    ///
+    /// It will also filter out entries that are ordered if the next entry is;
+    /// for instance, `vec![floor(a) Some(ASC), a Some(ASC)]` will be 
collapsed to
+    /// `vec![a Some(ASC)]`.
+    pub fn collapse(self) -> Self {
+        let mut output = Vec::<PhysicalSortRequirement>::new();
+        let mut exprs = IndexSet::new();
+        let mut reqs = vec![];
+        for item in self {
+            let PhysicalSortRequirement { expr, options: req } = item;
+            // new insertion
+            if exprs.insert(expr) {
+                reqs.push(req);
+            }
+        }
+        debug_assert_eq!(reqs.len(), exprs.len());
+        for (expr, req) in izip!(exprs, reqs) {
+            output.push(PhysicalSortRequirement::new(expr, req));
+        }
+        LexRequirement::new(output)
+    }

Review Comment:
   This is a good point -- I may have been over eager to merge in the PR from 
@akurmustafa  -- I will think about this more carefully and either restore the 
original version or apply the same type of optimzization to 
LexOrdering::collapse



-- 
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-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to