This is an automated email from the ASF dual-hosted git repository.

mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new c3478b9718 [CALCITE-6721] Incorrect implementation of 
SqlFunction.checkedDivide
c3478b9718 is described below

commit c3478b9718718337c9ef290ab906be8754d15d03
Author: Mihai Budiu <[email protected]>
AuthorDate: Sun Dec 8 23:33:51 2024 -0800

    [CALCITE-6721] Incorrect implementation of SqlFunction.checkedDivide
    
    Signed-off-by: Mihai Budiu <[email protected]>
---
 .../main/java/org/apache/calcite/runtime/SqlFunctions.java   |  2 +-
 .../main/java/org/apache/calcite/test/SqlOperatorTest.java   | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index a5f03669da..ed27b3e360 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -2604,7 +2604,7 @@ public class SqlFunctions {
   }
 
   public static short checkedDivide(short b0, short b1) {
-    return intToShort(b0 * b1);
+    return intToShort(b0 / b1);
   }
 
   public static int checkedDivide(int b0, int b1) {
diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index 8f1b0d3c0b..1d8e3669dd 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -3489,6 +3489,18 @@ public class SqlOperatorTest {
         DECIMAL_OVERFLOW, true);
   }
 
+  /**
+   * Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-6721";>
+   * Incorrect implementation of SqlFunction.checkedDivide</a>. */
+  @Test void testCheckedDivideOperator() {
+    final SqlOperatorFixture f = fixture()
+        .setFor(SqlStdOperatorTable.MULTIPLY, VmName.EXPAND)
+        // BigQuery requires arithmetic overflows
+        .withConformance(SqlConformanceEnum.BIG_QUERY);
+    f.checkFails("CAST(-32768 as SMALLINT) / CAST(0 AS SMALLINT)",
+        "/ by zero", true);
+  }
+
   @Test void testMultiplyIntervals() {
     final SqlOperatorFixture f = fixture();
     f.checkScalar("interval '2:2' hour to minute * 3",

Reply via email to