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:17:50 UTC

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3185: Simplify dyn comparator and make NaN ordering consistent (#1941)

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