mobiusklein commented on code in PR #257:
URL: https://github.com/apache/arrow-dotnet/pull/257#discussion_r2838913625


##########
src/Apache.Arrow.Operations/Comparison.cs:
##########
@@ -0,0 +1,390 @@
+// 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;
+using System.Numerics;
+using Apache.Arrow;
+using Apache.Arrow.Memory;
+using Apache.Arrow.Types;
+
+namespace Apache.Arrow.Operations;
+
+public static class Comparison
+{
+    /// <summary>
+    /// Negate a boolean array, flipping true to false, false to true. Nulls 
remain null
+    /// </summary>
+    /// <param name="mask"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    public static BooleanArray Invert(BooleanArray mask, MemoryAllocator? 
allocator = null)
+    {
+        var builder = new BooleanArray.Builder();
+        builder.Reserve(mask.Length);
+        foreach (var val in mask)
+        {
+            if (val != null)
+            {
+                builder.Append(!(bool)val);
+            }
+            else
+            {
+                builder.AppendNull();
+            }
+        }
+        return builder.Build(allocator);
+    }
+
+    /// <summary>
+    /// Perform a pairwise boolean AND operation.
+    /// </summary>
+    /// <param name="lhs"></param>
+    /// <param name="rhs"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    /// <exception cref="InvalidOperationException"></exception>
+    public static BooleanArray And(BooleanArray lhs, BooleanArray rhs, 
MemoryAllocator? allocator = null)
+    {
+        if (lhs.Length != rhs.Length) throw new 
InvalidOperationException("Arrays must have the same length");
+        var builder = new BooleanArray.Builder();
+        builder.Reserve(lhs.Length);
+        for (int i = 0; i < lhs.Length; i++)
+        {
+            var a = lhs.GetValue(i);
+            var b = rhs.GetValue(i);
+            if (a != null && b != null)
+            {
+                builder.Append((bool)a && (bool)b);
+            }
+            else
+            {
+                builder.AppendNull();
+            }
+        }
+        return builder.Build(allocator);
+    }
+
+    /// <summary>
+    /// Performa a pairwise boolean OR operation.
+    /// </summary>
+    /// <param name="lhs"></param>
+    /// <param name="rhs"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    /// <exception cref="InvalidOperationException"></exception>
+    public static BooleanArray Or(BooleanArray lhs, BooleanArray rhs, 
MemoryAllocator? allocator = null)
+    {
+        if (lhs.Length != rhs.Length) throw new 
InvalidOperationException("Arrays must have the same length");
+        var builder = new BooleanArray.Builder();
+        builder.Reserve(lhs.Length);
+        for (int i = 0; i < lhs.Length; i++)
+        {
+            var a = lhs.GetValue(i);
+            var b = rhs.GetValue(i);
+            if (a != null && b != null)
+            {
+                builder.Append((bool)a || (bool)b);
+            }
+            else
+            {
+                builder.AppendNull();
+            }
+        }
+        return builder.Build(allocator);
+    }
+
+    /// <summary>
+    /// Performa a pairwise boolean XOR operation.
+    /// </summary>
+    /// <param name="lhs"></param>
+    /// <param name="rhs"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    /// <exception cref="InvalidOperationException"></exception>
+    public static BooleanArray Xor(BooleanArray lhs, BooleanArray rhs, 
MemoryAllocator? allocator = null)
+    {
+        if (lhs.Length != rhs.Length) throw new 
InvalidOperationException("Arrays must have the same length");
+        var builder = new BooleanArray.Builder();
+        builder.Reserve(lhs.Length);
+        for (int i = 0; i < lhs.Length; i++)
+        {
+            var a = lhs.GetValue(i);
+            var b = rhs.GetValue(i);
+            if (a != null && b != null)
+            {
+                builder.Append((bool)a ^ (bool)b);
+            }
+            else
+            {
+                builder.AppendNull();
+            }
+        }
+        return builder.Build(allocator);
+    }
+
+    /// <summary>
+    /// Compare each value in `lhs` to a scalar `rhs`, returning boolean mask
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="lhs"></param>
+    /// <param name="rhs"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    public static BooleanArray Equal<T>(PrimitiveArray<T> lhs, T rhs, 
MemoryAllocator? allocator = null) where T : struct, INumber<T>
+    {
+        var cmp = new BooleanArray.Builder();
+        for (int i = 0; i < lhs.Length; i++)
+        {
+            var a = lhs.GetValue(i);
+            var flag = a == rhs;
+            cmp.Append(flag);
+        }
+        return cmp.Build(allocator);
+    }
+
+    /// <summary>
+    /// Perform a pairwise comparison between each position in `lhs` and 
`rhs`, returning a boolean mask
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="lhs"></param>
+    /// <param name="rhs"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    /// <exception cref="InvalidOperationException"></exception>
+    public static BooleanArray Equal<T>(PrimitiveArray<T> lhs, 
PrimitiveArray<T> rhs, MemoryAllocator? allocator = null) where T : struct, 
INumber<T>
+    {
+        var cmp = new BooleanArray.Builder();
+        if (lhs.Length != rhs.Length) throw new 
InvalidOperationException("Arrays must have the same length");
+        for (int i = 0; i < lhs.Length; i++)
+        {
+            var a = lhs.GetValue(i);
+            var b = rhs.GetValue(i);
+            var flag = a == b;
+            cmp.Append(flag);
+        }
+        return cmp.Build(allocator);
+    }
+
+    /// <summary>
+    /// Compare each value in `lhs` to a scalar `rhs`, returning boolean mask
+    /// </summary>
+    /// <param name="lhs"></param>
+    /// <param name="rhs"></param>
+    /// <param name="allocator"></param>
+    /// <returns></returns>
+    public static BooleanArray Equal(StringArray lhs, string rhs, 
MemoryAllocator? allocator = null)

Review Comment:
   Good point. Is it preferable to use SQL semantics? Alternatively, it could 
be configured with a flag argument, in which case the default behavior would 
still need to be documented.



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