wangxlong commented on a change in pull request #1878: [CALCITE-3782]Bitwise agg operator Bit_And, Bit_OR and Bit_XOR support binary and varbinary type URL: https://github.com/apache/calcite/pull/1878#discussion_r405959068
########## File path: core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java ########## @@ -1074,23 +1075,92 @@ private static RuntimeException notComparable(String op, Object b0, } // & - /** Helper function for implementing <code>BIT_AND</code> */ + /** Helper function for implementing <code>BIT_AND</code> applied to integer values */ public static long bitAnd(long b0, long b1) { return b0 & b1; } + /** Helper function for implementing <code>BIT_AND</code> applied to binary values */ + public static ByteString bitAnd(ByteString b0, ByteString b1) { + + if (b0.length() == 0) { + return b1; + } + if (b1.length() == 0) { + return b0; + } + + if (b0.length() != b1.length()) { + throw RESOURCE.differentLengthForBitwiseOperands(b0.length(), b1.length()).ex(); + } + byte[] bytes0 = b0.getBytes(); + byte[] bytes1 = b1.getBytes(); + byte[] result = new byte[b0.length()]; + + IntStream.range(0, bytes0.length).forEach(i -> { + result[i] = (byte) (bytes0[i] & bytes1[i]); + }); + return new ByteString(result); + } + // | - /** Helper function for implementing <code>BIT_OR</code> */ + /** Helper function for implementing <code>BIT_OR</code> applied to integer values */ public static long bitOr(long b0, long b1) { return b0 | b1; } + /** Helper function for implementing <code>BIT_OR</code> applied to binary values */ + public static ByteString bitOr(ByteString b0, ByteString b1) { + + if (b0.length() == 0) { + return b1; + } + if (b1.length() == 0) { + return b0; + } + + if (b0.length() != b1.length()) { + throw RESOURCE.differentLengthForBitwiseOperands(b0.length(), b1.length()).ex(); + } + byte[] bytes0 = b0.getBytes(); + byte[] bytes1 = b1.getBytes(); + byte[] result = new byte[b0.length()]; + + IntStream.range(0, bytes0.length).forEach(i -> { Review comment: Sorry for reply later. The variable definition of the function is the same, but the main processing mode is different between BIT_AND, BIT_OR, BIT_XOR which are &, | and ^. So We don't need to move it to utility function. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services