adamreeve commented on code in PR #379:
URL: https://github.com/apache/arrow-dotnet/pull/379#discussion_r3725527841


##########
src/Apache.Arrow.Compute/Aggregations.cs:
##########
@@ -0,0 +1,406 @@
+// 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.
+
+using System;
+#if NET8_0_OR_GREATER
+using System.Numerics;
+using System.Numerics.Tensors;
+#endif
+
+namespace Apache.Arrow.Compute
+{
+    /// <summary>
+    /// Aggregation kernels over <see cref="PrimitiveArray{T}"/> 
(Sum/Min/Max/Mean).
+    /// </summary>
+    /// <remarks>
+    /// <para>
+    /// Null entries are skipped and do not contribute to the result. 
<c>Sum</c>, <c>Min</c>,
+    /// <c>Max</c> and <c>Mean</c> return <c>null</c> (<see 
cref="System.Nullable{T}"/>) when the
+    /// array is empty or contains no non-null elements.
+    /// </para>
+    /// <para>
+    /// On net8.0 and later the kernels are generic over 
<c>INumber&lt;T&gt;</c> and, when the
+    /// array has no nulls, dispatch to <c>TensorPrimitives</c> for a 
SIMD-accelerated single
+    /// pass over the contiguous values buffer; when nulls are present they 
fall back to a correct,
+    /// validity-aware scalar loop. On netstandard2.0 and net462 (where 
generic math and
+    /// <c>TensorPrimitives</c> are unavailable) the kernels are provided as 
per-type overloads
+    /// (<see cref="Int32Array"/>, <see cref="Int64Array"/>, <see 
cref="FloatArray"/>,
+    /// <see cref="DoubleArray"/>) backed by scalar loops with the same null 
semantics.
+    /// </para>
+    /// </remarks>
+    public static class Aggregations
+    {
+#if NET8_0_OR_GREATER
+        /// <summary>Sums the non-null elements. Returns null for an empty or 
all-null array.</summary>
+        public static T? Sum<T>(this PrimitiveArray<T> array)
+            where T : unmanaged, INumber<T>
+        {
+            if (array is null) throw new ArgumentNullException(nameof(array));
+
+            ReadOnlySpan<T> values = array.Values;
+
+            if (values.Length == 0 || array.Length - array.NullCount == 0)
+            {
+                return null;
+            }
+
+            if (array.NullCount == 0)
+            {
+                return TensorPrimitives.Sum(values);
+            }
+
+            T acc = T.Zero;
+            for (int i = 0; i < values.Length; i++)
+            {
+                if (array.IsValid(i))
+                {
+                    acc += values[i];
+                }
+            }
+            return acc;
+        }
+
+        /// <summary>Returns the smallest non-null element, or null if there 
are no non-null elements.</summary>
+        public static T? Min<T>(this PrimitiveArray<T> array)
+            where T : unmanaged, INumber<T>, IMinMaxValue<T>
+        {
+            if (array is null) throw new ArgumentNullException(nameof(array));
+
+            ReadOnlySpan<T> values = array.Values;
+
+            if (values.Length == 0 || array.Length - array.NullCount == 0)
+            {
+                return null;
+            }
+
+            if (array.NullCount == 0)
+            {
+                return TensorPrimitives.Min(values);
+            }
+
+            T min = T.MaxValue;
+            for (int i = 0; i < values.Length; i++)
+            {
+                if (!array.IsValid(i)) continue;
+                if (values[i] < min) { min = values[i]; }
+            }
+            return min;

Review Comment:
   Ah my bad, I suggested this change, but Copilot is right.



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

Reply via email to