Weiwei Su created FLINK-41834:
---------------------------------
Summary: Wrong result for IN predicate: RHS literals are narrowed
to LHS INT type, causing silent INT32 overflow wrap-around and DECIMAL
truncation
Key: FLINK-41834
URL: https://issues.apache.org/jira/browse/FLINK-41834
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner
Affects Versions: 2.3.0
Environment: Apache Flink 2.3.0 (SQL Gateway / Batch mode)
Reporter: Weiwei Su
h3. Summary
In Flink SQL (2.3.0), when an {{INT}} column or expression is compared against
an {{IN (...)}} list containing wider numeric literals ({{BIGINT}} out of
{{INT32}} range, or {{DECIMAL}}), the planner narrows each RHS literal to
{{INTEGER}} instead of widening both sides to their common supertype. During
constant folding, {{CAST(2147483648:BIGINT):INTEGER}} silently wraps around to
{{-2147483648}} and {{CAST(1.5:DECIMAL(2, 1)):INTEGER}} truncates to {{1}},
producing silently wrong query results that also contradict the equivalent
{{=}} / {{OR}} predicate.
h3. Minimal Reproduction (Self-Contained, No External Table)
{code:sql}
-- Case 1: INT32 overflow wrap-around in IN list (returns [-2147483648],
expected empty)
SELECT x FROM (VALUES (-2147483648)) AS t(x) WHERE x IN (1, 2147483648);
-- Case 2: Mixed BIGINT + DECIMAL literals in IN list (returns [-2147483648],
expected empty)
SELECT x FROM (VALUES (-2147483648)) AS t(x) WHERE x IN (1, 1.5, 2147483648);
-- Control: Equivalent equality predicate works correctly (returns empty)
SELECT x FROM (VALUES (-2147483648)) AS t(x) WHERE x = 2147483648;
{code}
h3. Observed vs. Expected Results
* *Flink 2.3.0*:
** Case 1 ({{x IN (1, 2147483648)}}): returns {{-2147483648}} (WRONG)
** Case 2 ({{x IN (1, 1.5, 2147483648)}}): returns {{-2147483648}} (WRONG)
** Control ({{x = 2147483648}}): returns empty (correct)
* *Expected (and observed on Spark 4.1.2, Trino 484, DuckDB 2.0.1, Polars
2.2.0)*:
** Both Case 1 and Case 2 return empty (0 rows).
h3. EXPLAIN Plan (Flink 2.3.0)
Running:
{code:sql}
EXPLAIN SELECT x FROM (VALUES (-2147483648)) AS t(x) WHERE x IN (1, 1.5,
2147483648);
{code}
produces:
{noformat}
== Abstract Syntax Tree ==
LogicalProject(x=[$0])
+- LogicalFilter(condition=[OR(=($0, 1), =($0, CAST(1.5:DECIMAL(2, 1)):INTEGER
NOT NULL), =($0, CAST(2147483648:BIGINT):INTEGER NOT NULL))])
+- LogicalValues(tuples=[[{ -2147483648 }]])
== Optimized Physical Plan ==
Calc(select=[x], where=[SEARCH(x, Sarg[-2147483648, 1])])
+- Values(tuples=[[{ -2147483648 }]], values=[x])
== Optimized Execution Plan ==
Calc(select=[x], where=[SEARCH(x, Sarg[-2147483648, 1])])
+- Values(tuples=[[{ -2147483648 }]], values=[x])
{noformat}
h3. Root Cause Analysis
# In the AST, {{x IN (1, 1.5, 2147483648)}} is expanded by casting every RHS
literal down to the LHS type ({{INTEGER NOT NULL}}): {{CAST(1.5:DECIMAL(2,
1)):INTEGER NOT NULL}} and {{CAST(2147483648:BIGINT):INTEGER NOT NULL}}.
# Constant folding then evaluates these narrowing casts into {{1}} (truncating
{{1.5}}) and {{-2147483648}} (two's-complement wrap-around of {{2147483648L}}),
collapsing the filter into {{SEARCH(x, Sarg[-2147483648, 1])}}.
# By contrast, {{x = 2147483648}} widens {{x}} to {{BIGINT}} and returns the
correct empty result.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)