mihaibudiu commented on code in PR #5073:
URL: https://github.com/apache/calcite/pull/5073#discussion_r3554870978


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -3914,6 +3914,148 @@ public static ULong leftShift(ULong x, int y) {
     return ULong.valueOf(val);
   }
 
+  /**
+   * Performs PostgresSQL-style bitwise shift on a 32-bit integer.
+   *
+   * @param x the integer value to shift
+   * @param y the shift amount (positive: right shift, negative: left shift)
+   * @return the shifted integer
+   */
+  public static int rightShift(int x, int y) {
+    int shift = ((y % 32) + 32) % 32; // normalize to 0~31
+    return y >= 0 ? x >> shift : x << shift; // arithmetic right shift
+  }
+
+
+  // ----------------- long -----------------
+  /**
+   * Performs PostgresSQL-style bitwise shift on a 64-bit long value.
+   *
+   * @param x the long value to shift
+   * @param y the shift amount
+   * @return the shifted long value
+   */
+  public static long rightShift(long x, int y) {
+    int shift = ((y % 64) + 64) % 64; // normalize to 0~63
+    return y >= 0 ? x >> shift : x << shift;
+  }
+
+  /**
+   * Performs PostgresSQL-style bitwise shift on an int value with a long 
shift amount.
+   *
+   * @param x the int value to shift
+   * @param y the long shift amount
+   * @return the shifted value as long
+   */
+  public static long rightShift(int x, long y) {

Review Comment:
   Please file an issue about unsigned amounts not being supported. I think you 
will agree that's not normal.
   But if this implementation will need to be altered to support that better, 
maybe you can prepare it now.
   I suspect the right way will be to cast any of these unsigned values to a 
long. The overflow for BIGINT UNSIGNED may require special handling.



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