Yaoxuan Wu created FLINK-41595:
----------------------------------

             Summary: Planner ClassCastException (Double cannot be cast to 
BigDecimal) in RexSimplify for a range with an exact and an approximate 
literal, e.g. WHERE x > 1.5 AND x < 2.0e5 (regression on master)
                 Key: FLINK-41595
                 URL: https://issues.apache.org/jira/browse/FLINK-41595
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.4.0
            Reporter: Yaoxuan Wu


On master (2.4-SNAPSHOT), a filter or CASE condition that bounds one column 
with an exact numeric literal and an approximate numeric literal fails during 
planning with a ClassCastException. An example is {{x > 1.5 AND x < 2.0e5}}. 
The same queries work on 2.0.1, 2.2.1 and 2.3.0, so this is a regression.

{code:sql}
CREATE TEMPORARY VIEW v AS SELECT * FROM (VALUES (1.0E0), (100000.0E0)) AS 
v(x);   -- x DOUBLE

SELECT x FROM v WHERE x > 1.5 AND x < 2.0e5;
-- expected: 100000.0 (as on 2.3.0)
-- actual:   java.lang.ClassCastException: class java.lang.Double cannot be 
cast to class java.math.BigDecimal

SELECT x FROM v WHERE x > 1.5 OR x = 2.0e5;                       -- same 
exception
SELECT CASE WHEN x > 1.5 AND x < 2.0e5 THEN 1 ELSE 0 END FROM v;  -- same 
exception
SELECT x FROM v WHERE x > 1.5 AND x <> 2.0e5;                     -- same 
exception

SELECT x FROM v WHERE x > 1.5e0 AND x < 2.0e5;     -- works (both literals 
approximate)
SELECT x FROM v WHERE x > 1.5 AND x < 200000.0;    -- works (both literals 
exact)
{code}

{noformat}
java.lang.ClassCastException: class java.lang.Double cannot be cast to class 
java.math.BigDecimal
        at java.math.BigDecimal.compareTo(BigDecimal.java:...)
        at 
org.apache.flink.calcite.shaded.com.google.common.collect.Range.compareOrThrow(Range.java:700)
        at 
org.apache.flink.calcite.shaded.com.google.common.collect.Cut.compareTo(Cut.java:80)
        at 
org.apache.flink.calcite.shaded.com.google.common.collect.Range.isConnected(Range.java:502)
        at 
org.apache.flink.calcite.shaded.com.google.common.collect.ImmutableRangeSet.subRangeSet(ImmutableRangeSet.java:522)
        at org.apache.calcite.rex.RexSimplify.residue(RexSimplify.java:2281)
        at 
org.apache.calcite.rex.RexSimplify.simplifyUsingPredicates(RexSimplify.java:2195)
        at 
org.apache.calcite.rex.RexSimplify.simplifyComparison(RexSimplify.java:859)
        ...
        at org.apache.calcite.rex.RexSimplify.simplifyAnd(RexSimplify.java:1835)
        at 
org.apache.flink.table.planner.plan.utils.FlinkRexUtil$.simplify(FlinkRexUtil.scala:209)
        at 
org.apache.flink.table.planner.plan.rules.logical.SimplifyFilterConditionRule.onMatch(SimplifyFilterConditionRule.java:64)
{noformat}

* Batch and streaming.
* The column type does not matter: DOUBLE, FLOAT and DECIMAL(10, 2) columns all 
fail. Writing the approximate bound as {{CAST(2.0e5 AS DOUBLE)}} fails too.
* The OR form fails with the opposite message (BigDecimal cannot be cast to 
Double).
* Works on 2.0.1, 2.2.1 and 2.3.0. On 2.3.0 the plan is {{Calc(where=[SEARCH(x, 
Sarg[(1.5..2.0E+5)])])}}.
* Fails on master snapshots of 2026-10-06 (a47e4bf) and 2026-11-19 (2f01434).

Cause (partly a guess): the two comparisons keep literals of different Java 
classes (BigDecimal for {{1.5}}, Double for {{2.0e5}}) without a common cast. 
{{RexSimplify.simplifyUsingPredicates}} then puts both into one Guava 
{{RangeSet}}, whose {{Cut.compareTo}} compares a Double with a BigDecimal. 
2.3.0 builds a single Sarg from the same predicates, so the literals are 
presumably made comparable before that point there. This probably came with the 
Calcite upgrades in 2.4.0 (1.38 to 1.41), but I have not checked which step. 
CALCITE-7201 (fixed in 1.41.0) is the same kind of Double / BigDecimal mix, but 
in {{RexInterpreter#search}}.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to