Dandandan commented on code in PR #20768:
URL: https://github.com/apache/datafusion/pull/20768#discussion_r2899188658


##########
datafusion/common/src/utils/aggregate.rs:
##########
@@ -0,0 +1,409 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Array-level and scalar-level aggregation utilities (sum, min, max).
+//!
+//! These functions compute aggregate values over Arrow arrays or slices of
+//! [`ScalarValue`]s, returning the result as a [`ScalarValue`]. They use
+//! native Arrow compute kernels for primitive numeric types and fall back
+//! to element-wise [`ScalarValue`] operations for other types.
+
+use arrow::array::ArrayRef;
+
+use crate::stats::Precision;
+use crate::{Result, ScalarValue};
+use arrow::array::*;
+use arrow::compute::{min as arrow_min, sum as arrow_sum};
+use arrow::datatypes::*;
+
+/// Returns true if the [`ScalarValue`] is a primitive numeric type that
+/// benefits from Arrow vectorized kernels over direct `ScalarValue`
+/// comparison.
+pub(crate) fn is_primitive_scalar(value: &ScalarValue) -> bool {
+    matches!(
+        value,
+        ScalarValue::Int8(_)
+            | ScalarValue::Int16(_)
+            | ScalarValue::Int32(_)
+            | ScalarValue::Int64(_)
+            | ScalarValue::UInt8(_)
+            | ScalarValue::UInt16(_)
+            | ScalarValue::UInt32(_)
+            | ScalarValue::UInt64(_)
+            | ScalarValue::Float16(_)
+            | ScalarValue::Float32(_)
+            | ScalarValue::Float64(_)
+            | ScalarValue::Decimal32(_, _, _)
+            | ScalarValue::Decimal64(_, _, _)
+            | ScalarValue::Decimal128(_, _, _)
+            | ScalarValue::Decimal256(_, _, _)
+            | ScalarValue::Date32(_)
+            | ScalarValue::Date64(_)
+    )
+}
+
+/// Compute the minimum of [`ScalarValue`]s using direct `PartialOrd`
+/// comparison.
+pub(crate) fn scalar_min(values: &[ScalarValue]) -> ScalarValue {
+    debug_assert!(!values.is_empty());
+    let mut result = values[0].clone();
+    for v in &values[1..] {
+        if v.is_null() {
+            continue;
+        }
+        if result.is_null() || v < &result {
+            result = v.clone();
+        }
+    }
+    result
+}
+
+/// Compute the maximum of [`ScalarValue`]s using direct `PartialOrd`
+/// comparison.
+pub(crate) fn scalar_max(values: &[ScalarValue]) -> ScalarValue {
+    debug_assert!(!values.is_empty());
+    let mut result = values[0].clone();
+    for v in &values[1..] {
+        if v.is_null() {
+            continue;
+        }
+        if result.is_null() || v > &result {
+            result = v.clone();
+        }
+    }
+    result
+}
+
+/// Wrap a [`ScalarValue`] result with the appropriate [`Precision`] level.
+pub(crate) fn wrap_precision(
+    value: ScalarValue,
+    all_exact: bool,
+) -> Precision<ScalarValue> {
+    if value.is_null() {
+        return Precision::Absent;
+    }
+    if all_exact {
+        Precision::Exact(value)
+    } else {
+        Precision::Inexact(value)
+    }
+}
+
+/// Compute the sum of a collection of [`ScalarValue`]s using vectorized

Review Comment:
   Is this really faster than directly summing the primitives values out of the 
scalarvalues (without creating scalarvalue)?



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