You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/09/23 16:04:11 UTC

[GitHub] [spark] cloud-fan commented on a change in pull request #34056: [SPARK-36811][SQL] Add SQL functions for the BINARY data type for AND, OR, XOR, and NOT

cloud-fan commented on a change in pull request #34056:
URL: https://github.com/apache/spark/pull/34056#discussion_r714944760



##########
File path: common/unsafe/src/main/java/org/apache/spark/unsafe/types/ByteArray.java
##########
@@ -101,4 +101,95 @@ public static long getPrefix(byte[] bytes) {
     }
     return result;
   }
+
+  // Return the bitwise AND of two byte arrays. The byte length of the result is equal to the
+  // maximum byte length of the two inputs. The two input byte arrays are aligned with respect
+  // to their least significant (right-most) bytes.
+  public static byte[] bitwiseAnd(byte[] bytes1, byte[] bytes2) {
+    if (bytes1 == null || bytes2 == null) return null;
+    // Compute the length of the result (maximum of the lengths of the inputs).
+    final int len1 = bytes1.length;
+    final int len2 = bytes2.length;
+    final int maxLen = Math.max(len1, len2);
+    if (maxLen == 0) {
+      return EMPTY_BYTE;
+    }
+    final byte[] result = new byte[maxLen];
+    final int minLen = Math.min(len1, len2);
+    // Initialize the first `maxLen - minLen` bytes to 0.
+    Platform.setMemory(result, Platform.BYTE_ARRAY_OFFSET, maxLen - minLen, (byte)0);
+    // Compute the right-most minLen bytes of the result.
+    for (int j = 0; j < minLen; ++j) {
+      result[maxLen - 1 - j] = (byte)(bytes1[len1 - 1 - j] & bytes2[len2 - 1 - j]);
+    }
+    return result;
+  }
+
+  // Return the bitwise OR of two byte arrays. The byte length of the result is equal to the
+  // maximum byte length of the two inputs. The two input byte arrays are aligned with respect
+  // to their least significant (right-most) bytes.
+  public static byte[] bitwiseOr(byte[] bytes1, byte[] bytes2) {
+    if (bytes1 == null || bytes2 == null) return null;
+    // Compute the length of the result (maximum of the lengths of the inputs).
+    final int len1 = bytes1.length;
+    final int len2 = bytes2.length;
+    final int maxLen = Math.max(len1, len2);
+    if (maxLen == 0) {
+      return EMPTY_BYTE;
+    }
+    final byte[] result = new byte[maxLen];
+    final int minLen = Math.min(len1, len2);
+    // Copy the first `maxLen - minLen` bytes of the longer byte array into the result buffer.
+    final byte[] maxLenBytes = (len1 == maxLen) ? bytes1 : bytes2;
+    Platform.copyMemory(
+            maxLenBytes, Platform.BYTE_ARRAY_OFFSET,
+            result, Platform.BYTE_ARRAY_OFFSET,
+            maxLen - minLen);

Review comment:
       is it a no-op if `maxLen - minLen` is 0? or do we need to add a `if-else` manually to avoid calling `copyMemory`?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org