Yaoxuan Wu created FLINK-41038:
----------------------------------

             Summary: WHERE CAST(x AS DECIMAL(10, 2)) IS NULL returns NULL for 
x when the cast overflows
                 Key: FLINK-41038
                 URL: https://issues.apache.org/jira/browse/FLINK-41038
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.3.0
            Reporter: Yaoxuan Wu


{code:python}
from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes as D
t = TableEnvironment.create(EnvironmentSettings.in_batch_mode())
t.create_temporary_view('v', t.from_elements([(2147483647,)], 
D.ROW([D.FIELD('x', D.INT())])))

t.execute_sql("SELECT x, CAST(x AS DECIMAL(10, 2)) FROM v").print()
# (2147483647, NULL)   the cast overflows to NULL

t.execute_sql("SELECT x, x IS NULL FROM v WHERE CAST(x AS DECIMAL(10, 2)) IS 
NULL").print()
# expected: (2147483647, FALSE)
# actual:   (NULL, TRUE)
{code}
The cast overflows, so the filter correctly keeps the row, but the planner then 
replaces {{x}} with a NULL literal ({{Calc(select=[null:INTEGER AS x], 
where=[...])}}). It infers {{x IS NULL}} from the filter because Calcite's 
{{RexUtil.canAssignFrom}} treats INT -> DECIMAL(10, 2) as a widening cast; it 
compares precision and scale separately, although DECIMAL(10, 2) has only 8 
integer digits. Streaming mode returns the same wrong value.

Reproduces on 2.0.1, 2.2.1, 2.3.0 and master.




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to