macroguo-ghy commented on code in PR #3370:
URL: https://github.com/apache/calcite/pull/3370#discussion_r1296637290


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -1714,6 +1714,64 @@ public static int multiply(int b0, int b1) {
     throw notArithmetic("*", b0, b1);
   }
 
+  /** SQL <code>SAFE_ADD</code> function applied to long values. */
+  public static @Nullable Long safeAdd(long b0, long b1) {
+    try {
+      return Math.addExact(b0, b1);
+    } catch (ArithmeticException e) {
+      return null;
+    }
+  }
+
+  /** SQL <code>SAFE_ADD</code> function applied to long and BigDecimal 
values. */
+  public static @Nullable BigDecimal safeAdd(long b0, BigDecimal b1) {
+    BigDecimal ans = BigDecimal.valueOf(b0).add(b1);
+    return safeDecimal(ans) ? ans : null;
+  }
+
+  /** SQL <code>SAFE_ADD</code> function applied to BigDecimal and long 
values. */
+  public static @Nullable BigDecimal safeAdd(BigDecimal b0, long b1) {
+    BigDecimal ans = b0.add(BigDecimal.valueOf(b1));

Review Comment:
   I believe using `return safeAdd(b1, b0)` would be better. 
   Same comment for 
   `Double safeAdd(long b0, double b1)`
   `Double safeAdd(BigDecimal b0, double b1)`



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