Ramin Gharib created FLINK-41789:
------------------------------------
Summary: DECIMAL arithmetic that overflows writes NULL into a NOT
NULL result
Key: FLINK-41789
URL: https://issues.apache.org/jira/browse/FLINK-41789
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner, Table SQL / Runtime
Affects Versions: 2.3.0
Reporter: Ramin Gharib
A DECIMAL arithmetic result that needs more than 38 digits is NULL, such as {{d
+ d}} for the largest DECIMAL(38, 0). With NOT NULL operands, the result type
is NOT NULL too, so the planner and the runtime treat the NULL as a value.
{code:sql}
SELECT d, d + d AS s
FROM (VALUES
(CAST(99999999999999999999999999999999999999 AS DECIMAL(38, 0))),
(CAST(1 AS DECIMAL(38, 0)))) AS t(d);
{code}
{noformat}
EnforcerException: Column 's' is NOT NULL, however, a null value is being
written into it. You can set job configuration
'table.exec.sink.not-null-enforcer'='DROP' to suppress this exception and drop
such records silently.
{noformat}
{{TYPEOF(d + d)}} is {{DECIMAL(38, 0) NOT NULL}}. For the largest {{d}}:
||Expression||Expected||Actual||
|{{d + d}}|NULL, or an error|The job fails with {{EnforcerException}}|
|{{d + d IS NULL}}|TRUE|FALSE|
|{{COALESCE(d + d, -1)}}|-1|The job fails with {{EnforcerException}}|
|{{IFNULL(d + d, -1)}}|-1|-1|
|{{CAST(d + d AS STRING)}}|NULL|An empty string|
|{{CHAR_LENGTH(CAST(d + d AS STRING))}}|NULL|-1|
The same in batch and streaming mode. With nullable operands, {{d + d}} and
{{COALESCE(d + d, -1)}} are right, but {{d + d IS NULL}} is still FALSE, which
FLINK-14030 describes.
h3. Cause
{{DecimalData#fromBigDecimal}} returns null when the value needs more than 38
digits ({{DecimalData.java:208}}), but the result type is NOT NULL when both
operands are. The optimized plan shows what follows: {{d + d IS NULL}} becomes
{{false}}, and {{COALESCE(d + d, -1)}} becomes {{d + d}}, while {{IFNULL}}
stays. The cast to STRING doesn't check a NOT NULL input for NULL, which
probably gives the empty string of length -1.
h3. Proposal
Make the result type of DECIMAL {{+}}, {{-}}, {{*}} and {{/}} nullable when its
precision is capped at 38, since only then can the result overflow. FLINK-24847
discusses whether an overflow should fail instead. Until that is decided, the
type should admit the NULL that the runtime returns.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)