Dwrite commented on code in PR #4478:
URL: https://github.com/apache/calcite/pull/4478#discussion_r2312234135


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -3506,6 +3506,152 @@ private static ByteString binaryOperator(
     return new ByteString(result);
   }
 
+  /**
+   * Performs bitwise shift on two integers.
+   * If the shift amount is positive, a left shift is applied.
+   * If the shift amount is negative, a logical right shift is applied.
+   * The shift amount is taken modulo 32.
+   */
+
+  public static int leftShift(int x, int y) {
+    int shift = Math.abs(y % 32);
+    return y >= 0 ? x << shift : x >>> shift;
+  }
+
+  /**
+   * Performs bitwise shift on a long value.
+   * If the shift amount is positive, a left shift is applied.
+   * If the shift amount is negative, a logical right shift is applied.
+   * The shift amount is taken modulo 64.
+   */
+
+  public static long leftShift(long x, int y) {
+    int shift = Math.abs(y % 64);
+    return y >= 0 ? x << shift : x >>> shift;
+  }
+
+  /**
+   * Performs bitwise shift with long shift amount.
+   * Note: input x is int, so shift mod 32 is used.
+   * Returns long to prevent overflow.
+   */
+  public static long leftShift(int x, long y) {
+    int shift = (int) Math.abs(y % 32);
+    return y >= 0 ? (long) x << shift : (long) x >>> shift;
+  }
+
+  /**
+   * Performs bitwise shift on a byte array.
+   * A positive shift amount performs a left shift.
+   * A negative shift amount performs a logical right shift.
+   */
+  public static ByteString leftShift(ByteString bytes, int y) {
+    byte[] result = leftShift(bytes.getBytes(), y);
+    return new ByteString(result);
+  }
+
+  /**
+   * Performs bitwise shift on a byte array.
+   * A positive shift amount performs a left shift.
+   * A negative shift amount performs a logical right shift.
+   */
+  public static byte[] leftShift(byte[] bytes, int y) {

Review Comment:
   I think 
   ```
    f.checkScalar("CAST(X'4000' AS BINARY(2)) << 1", "8000", "BINARY(2) NOT 
NULL");
       f.checkScalar("CAST(X'0F' AS BINARY(1)) << -4", "f0", "BINARY(1) NOT 
NULL");
   ``` already cover this fun.



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