Jefffrey commented on code in PR #22062:
URL: https://github.com/apache/datafusion/pull/22062#discussion_r3226191947


##########
datafusion/functions-aggregate-common/src/min_max.rs:
##########
@@ -37,443 +37,353 @@ use datafusion_common::{
 use datafusion_expr_common::accumulator::Accumulator;
 use std::{cmp::Ordering, mem::size_of_val};
 
-// min/max of two non-string scalar values.
-macro_rules! typed_min_max {
-    ($VALUE:expr, $DELTA:expr, $SCALAR:ident, $OP:ident $(, 
$EXTRA_ARGS:ident)*) => {{
-        ScalarValue::$SCALAR(
-            match ($VALUE, $DELTA) {
-                (None, None) => None,
-                (Some(a), None) => Some(*a),
-                (None, Some(b)) => Some(*b),
-                (Some(a), Some(b)) => Some((*a).$OP(*b)),
-            },
-            $($EXTRA_ARGS.clone()),*
-        )
-    }};
+macro_rules! choose_min_max {
+    (min) => {
+        std::cmp::Ordering::Greater
+    };
+    (max) => {
+        std::cmp::Ordering::Less
+    };
 }
 
-macro_rules! typed_min_max_float {
-    ($VALUE:expr, $DELTA:expr, $SCALAR:ident, $OP:ident) => {{
-        ScalarValue::$SCALAR(match ($VALUE, $DELTA) {
-            (None, None) => None,
-            (Some(a), None) => Some(*a),
-            (None, Some(b)) => Some(*b),
-            (Some(a), Some(b)) => match a.total_cmp(b) {
-                choose_min_max!($OP) => Some(*b),
-                _ => Some(*a),
-            },
-        })
-    }};
+macro_rules! min_max {
+    ($VALUE:expr, $DELTA:expr, $OP:ident) => {{ min_max_scalar($VALUE, $DELTA, 
choose_min_max!($OP)) }};
 }
 
-// min/max of two scalar string values.
-macro_rules! typed_min_max_string {
-    ($VALUE:expr, $DELTA:expr, $SCALAR:ident, $OP:ident) => {{
-        ScalarValue::$SCALAR(match ($VALUE, $DELTA) {
-            (None, None) => None,
-            (Some(a), None) => Some(a.clone()),
-            (None, Some(b)) => Some(b.clone()),
-            (Some(a), Some(b)) => Some((a).$OP(b).clone()),
-        })
-    }};
+fn min_max_option<T: Clone + Ord>(
+    lhs: &Option<T>,
+    rhs: &Option<T>,
+    ordering: Ordering,
+) -> Option<T> {
+    match (lhs, rhs) {
+        (None, None) => None,
+        (Some(a), None) => Some(a.clone()),
+        (None, Some(b)) => Some(b.clone()),
+        (Some(a), Some(b)) if a.cmp(b) == ordering => Some(b.clone()),
+        (Some(a), Some(_)) => Some(a.clone()),
+    }
 }
 
-// min/max of two scalar string values with a prefix argument.
-macro_rules! typed_min_max_string_arg {
-    ($VALUE:expr, $DELTA:expr, $SCALAR:ident, $OP:ident, $ARG:expr) => {{
-        ScalarValue::$SCALAR(
-            $ARG,
-            match ($VALUE, $DELTA) {
-                (None, None) => None,
-                (Some(a), None) => Some(a.clone()),
-                (None, Some(b)) => Some(b.clone()),
-                (Some(a), Some(b)) => Some((a).$OP(b).clone()),
-            },
-        )
-    }};
+fn min_max_float_option<T: Copy>(
+    lhs: &Option<T>,
+    rhs: &Option<T>,
+    ordering: Ordering,
+    cmp: impl Fn(&T, &T) -> Ordering,
+) -> Option<T> {
+    match (lhs, rhs) {
+        (None, None) => None,
+        (Some(a), None) => Some(*a),
+        (None, Some(b)) => Some(*b),
+        (Some(a), Some(b)) if cmp(a, b) == ordering => Some(*b),
+        (Some(a), Some(_)) => Some(*a),
+    }
 }
 
-macro_rules! choose_min_max {
-    (min) => {
-        std::cmp::Ordering::Greater
-    };
-    (max) => {
-        std::cmp::Ordering::Less
-    };
+fn ensure_decimal_compatibility(
+    lhs: &ScalarValue,
+    rhs: &ScalarValue,
+    lhs_type: (u8, i8),
+    rhs_type: (u8, i8),
+) -> Result<()> {
+    if lhs_type == rhs_type {
+        Ok(())
+    } else {
+        internal_err!(
+            "MIN/MAX is not expected to receive scalars of incompatible types 
{:?}",
+            (lhs, rhs)
+        )
+    }
 }
 
-macro_rules! interval_min_max {
-    ($OP:tt, $LHS:expr, $RHS:expr) => {{
-        match $LHS.partial_cmp(&$RHS) {
-            Some(choose_min_max!($OP)) => $RHS.clone(),
-            Some(_) => $LHS.clone(),
-            None => {
-                return internal_err!(
-                    "Comparison error while computing interval min/max"
-                );
-            }
+fn min_max_generic_scalar(
+    lhs: &ScalarValue,
+    rhs: &ScalarValue,
+    ordering: Ordering,
+) -> ScalarValue {
+    if lhs.is_null() {
+        let mut rhs_copy = rhs.clone();
+        rhs_copy.compact();

Review Comment:
   It would be good to keep the original comments regarding why only `rhs` is 
compacted



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to