Weiwei Su created FLINK-41778:
---------------------------------
Summary: DECIMAL NOT IN with out-of-range INT literal silently
overflows to NULL and drops all rows
Key: FLINK-41778
URL: https://issues.apache.org/jira/browse/FLINK-41778
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner
Affects Versions: 2.3.0
Environment: Apache Flink 2.3.0 (Docker, SQL Gateway, batch execution
mode, Java 17, default configuration)
Reporter: Weiwei Su
In Apache Flink 2.3.0, comparing a {{DECIMAL(p, s)}} column in a {{NOT IN
(...)}} predicate against an {{INT}} literal that exceeds the integer-digit
capacity of {{DECIMAL(p, s)}} silently narrows the {{INT}} literal into
{{DECIMAL(p, s)}} instead of widening both operands to a lossless common
{{DECIMAL}} type. During constant folding, the overflowing cast silently
evaluates to {{{}NULL{}}}, turning the entire {{NOT IN}} predicate into
{{UNKNOWN}} under SQL three-valued logic and silently dropping all rows.
h3. Environment
* {*}Apache Flink{*}: 2.3.0 (SQL Gateway, batch execution mode, default
configuration)
h3. Minimal Reproduction
{code:sql}
SELECT COUNT(*)
FROM (
VALUES (CAST(12.34 AS DECIMAL(9, 2)))
) AS t(amt)
WHERE amt NOT IN (42, -2147483648);
{code}
h3. Actual Result (Flink 2.3.0)
{noformat}
0
{noformat}
h3. Expected Result
{noformat}
1
{noformat}
(Verified {{1}} on Apache Spark 4.1.2, Trino 484, and DuckDB 2.0.1.)
h3. Execution Plan (EXPLAIN)
{noformat}
== Abstract Syntax Tree ==
LogicalAggregate(group=[{}], EXPR$0=[COUNT()])
+- LogicalFilter(condition=[NOT(OR(=($0, 42), =($0,
CAST(-2147483648):DECIMAL(9, 2) NOT NULL)))])
+- LogicalProject(amt=[$0])
+- LogicalProject(EXPR$0=[12.34:DECIMAL(9, 2)])
+- LogicalValues(tuples=[[{ 0 }]])
== Optimized Physical Plan ==
Values(tuples=[[{ 0 }]], values=[EXPR$0])
== Optimized Execution Plan ==
Values(tuples=[[{ 0 }]], values=[EXPR$0])
{noformat}
h3. Root Cause Analysis
# {{amt}} has type {{DECIMAL(9, 2)}} (at most 7 integer digits, max magnitude
{{{}9,999,999.99{}}}), while {{-2147483648}} is a 10-digit {{INT}} literal.
# Type coercion narrows {{-2147483648}} to {{CAST(-2147483648):DECIMAL(9, 2)
NOT NULL}} instead of promoting the comparison to a wider lossless decimal type
(e.g., {{{}DECIMAL(12, 2){}}}).
# During optimization/constant folding, the overflowing {{DECIMAL(9, 2)}} cast
silently evaluates to {{{}NULL{}}}, so {{NOT(OR(amt = 42, amt = NULL))}}
evaluates to {{UNKNOWN}} and folds the query to an empty result ({{{}0{}}}
rows).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)