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

snuyanzin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 149dfbf2a6d [FLINK-40920][table-planner] Fix ClassCastException when 
reducing a division by a constant expression
149dfbf2a6d is described below

commit 149dfbf2a6d1bff850e068869bd13ef31e37746a
Author: Ankur Partap Kotwal <[email protected]>
AuthorDate: Wed Oct 7 04:16:08 2026 -0700

    [FLINK-40920][table-planner] Fix ClassCastException when reducing a 
division by a constant expression
---
 .../table/planner/codegen/ExpressionReducer.scala  | 25 +++++++++++++---------
 .../planner/expressions/SqlExpressionTest.scala    |  5 +++++
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git 
a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/ExpressionReducer.scala
 
b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/ExpressionReducer.scala
index 3645c7b46c1..7c75f7d7dc9 100644
--- 
a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/ExpressionReducer.scala
+++ 
b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/ExpressionReducer.scala
@@ -281,18 +281,23 @@ class ExpressionReducer(
           // to ensure the division is non-zero when the operator is DIVIDE
           if (call.getOperator.getKind.equals(SqlKind.DIVIDE)) {
             val ops = call.getOperands
-            val divisionLiteral = ops.get(ops.size() - 1)
+            val divisor = ops.get(ops.size() - 1)
 
             // according to BuiltInFunctionDefinitions, the DEVIDE's second op 
must be numeric
-            assert(RexUtil.isDeterministic(divisionLiteral))
-            val divisionValue = 
divisionLiteral.asInstanceOf[RexLiteral].getValue
-            val isZero = divisionValue match {
-              case d: java.lang.Double => d == 0.0
-              case bd: java.math.BigDecimal => bd.signum() == 0
-              case _ => false
-            }
-            if (isZero) {
-              throw new ArithmeticException("Division by zero")
+            assert(RexUtil.isDeterministic(divisor))
+            // The divisor may itself be an unreduced constant expression, 
e.g. 1 / (2 + 3),
+            // in which case it is evaluated together with the division below.
+            divisor match {
+              case divisionLiteral: RexLiteral =>
+                val isZero = divisionLiteral.getValue match {
+                  case d: java.lang.Double => d == 0.0
+                  case bd: java.math.BigDecimal => bd.signum() == 0
+                  case _ => false
+                }
+                if (isZero) {
+                  throw new ArithmeticException("Division by zero")
+                }
+              case _ =>
             }
           }
           // Exclude some JSON functions which behave differently
diff --git 
a/flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala
 
b/flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala
index 0df0f3f52d3..b5f95314ab9 100644
--- 
a/flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala
+++ 
b/flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala
@@ -157,6 +157,11 @@ class SqlExpressionTest extends ExpressionTestBase {
     // Decimal(2,1) / SmallInt => Decimal(8,7)
     testSqlApi("1.0/cast(10000 as SMALLINT)", "0.0001000")
 
+    // divisor is a constant expression rather than a literal
+    testSqlApi("1 / (2 + 3)", "0")
+    testSqlApi("6 / (2 * 3)", "1")
+    testSqlApi("1.0 / (2 * 3)", "0.166666666667")
+
     // invalid division
     val divisorZeroException = "Division by zero"
     testExpectedSqlException(

Reply via email to