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/24 18:14:54 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request, #3185: Simplify dyn comparator and make NaN ordering consistent (#1941)

tustvold opened a new pull request, #3185:
URL: https://github.com/apache/arrow-rs/pull/3185

   # 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 #1941
   
   # 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.
   -->
   
   Less code, better coverage, consistent sort order
   
   # 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 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 `breaking 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-rs] tustvold closed pull request #3185: Simplify dyn comparator and make NaN ordering consistent (#1941)

Posted by GitBox <gi...@apache.org>.
tustvold closed pull request #3185: Simplify dyn comparator and make NaN ordering consistent (#1941)
URL: https://github.com/apache/arrow-rs/pull/3185


-- 
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-rs] tustvold commented on a diff in pull request #3185: Simplify dyn comparator and make NaN ordering consistent (#1941)

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3185:
URL: https://github.com/apache/arrow-rs/pull/3185#discussion_r1031761178


##########
arrow/src/array/ord.rs:
##########
@@ -20,140 +20,43 @@
 use std::cmp::Ordering;
 
 use crate::array::*;
-use crate::datatypes::TimeUnit;
 use crate::datatypes::*;
 use crate::error::{ArrowError, Result};
 
-use num::Float;
+use arrow_array::{downcast_integer, downcast_primitive};
 
-/// Compare the values at two arbitrary indices in two arrays.
-pub type DynComparator = Box<dyn Fn(usize, usize) -> Ordering + Send + Sync>;
-
-/// compares two floats, placing NaNs at last
-fn cmp_nans_last<T: Float>(a: &T, b: &T) -> Ordering {
-    match (a.is_nan(), b.is_nan()) {
-        (true, true) => Ordering::Equal,
-        (true, false) => Ordering::Greater,
-        (false, true) => Ordering::Less,
-        _ => a.partial_cmp(b).unwrap(),
-    }
+macro_rules! primitive_helper {
+    ($t:ty, $left:ident, $right:ident) => {{
+        let left = PrimitiveArray::<$t>::from($left.data().clone());
+        let right = PrimitiveArray::<$t>::from($right.data().clone());
+        Box::new(move |i, j| left.value(i).compare(right.value(j)))
+    }};
 }
 
-fn compare_primitives<T: ArrowPrimitiveType>(
-    left: &dyn Array,
-    right: &dyn Array,
-) -> DynComparator
-where
-    T::Native: Ord,
-{
-    let left: PrimitiveArray<T> = PrimitiveArray::from(left.data().clone());
-    let right: PrimitiveArray<T> = PrimitiveArray::from(right.data().clone());
-    Box::new(move |i, j| left.value(i).cmp(&right.value(j)))
+macro_rules! array_helper {
+    ($t:ty, $left:ident, $right:ident) => {{
+        let left = <$t>::from($left.data().clone());
+        let right = <$t>::from($right.data().clone());
+        Box::new(move |i, j| left.value(i).cmp(&right.value(j)))
+    }};
 }
 
-fn compare_boolean(left: &dyn Array, right: &dyn Array) -> DynComparator {
-    let left: BooleanArray = BooleanArray::from(left.data().clone());
-    let right: BooleanArray = BooleanArray::from(right.data().clone());
-
-    Box::new(move |i, j| left.value(i).cmp(&right.value(j)))
+macro_rules! dictionary_helper {
+    ($t:ty, $build_compare:ident, $left:ident, $right:ident) => {{
+        let left = DictionaryArray::<$t>::from($left.data().clone());
+        let right = DictionaryArray::<$t>::from($right.data().clone());
+        let compare = $build_compare(left.values().as_ref(), right.values().as_ref())?;
+
+        Box::new(move |i, j| {
+            let left_v = left.keys().value(i);
+            let right_v = right.keys().value(j);
+            compare(left_v as _, right_v as _)

Review Comment:
   This does add an additional dynamic dispatch to dictionary comparisons, I think this is worth the tradeoff of saving significant amounts of compilation time and codegen. Ultimately if people want efficient dynamic comparisons, they should use the row format



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