[ 
https://issues.apache.org/jira/browse/FLINK-41789?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Ramin Gharib updated FLINK-41789:
---------------------------------
    Description: 
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, on 2.3.0 and on master.

Nullable operands fail the same way wherever the plan checks {{d IS NOT NULL}} 
instead of the sum:

||Expression, nullable {{d}}||Optimized plan||Expected||Actual||
|{{d + d IS NULL}}|{{IS NULL(d)}}|TRUE|FALSE, which FLINK-14030 describes|
|{{CASE WHEN d + d IS NOT NULL THEN d + d ELSE -1 END}}|{{CASE(IS NOT NULL(d), 
CAST(+(d, d) AS DECIMAL(38, 0)), -1)}}|-1|The job fails with 
{{EnforcerException}}|
|{{COALESCE(d + d, -1)}}|{{COALESCE(+(d, d), -1)}}|-1|-1|
|{{IFNULL(d + d, -1)}}|{{IFNULL(+(d, d), -1)}}|-1|-1|

The planner types the CASE as NOT NULL, since {{d + d}} can't be NULL once 
{{d}} isn't, so the sum that overflows fails the job.

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. For the 
same operators, the simplification must not reduce {{IS NULL}} or {{IS NOT 
NULL}} of the result to its operands, as CALCITE-3368 proposes. FLINK-24847 
discusses whether an overflow should fail instead. Until that is decided, the 
type should admit the NULL that the runtime returns.


  was:
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.



> 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
>            Priority: Major
>
> 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, on 2.3.0 and on master.
> Nullable operands fail the same way wherever the plan checks {{d IS NOT 
> NULL}} instead of the sum:
> ||Expression, nullable {{d}}||Optimized plan||Expected||Actual||
> |{{d + d IS NULL}}|{{IS NULL(d)}}|TRUE|FALSE, which FLINK-14030 describes|
> |{{CASE WHEN d + d IS NOT NULL THEN d + d ELSE -1 END}}|{{CASE(IS NOT 
> NULL(d), CAST(+(d, d) AS DECIMAL(38, 0)), -1)}}|-1|The job fails with 
> {{EnforcerException}}|
> |{{COALESCE(d + d, -1)}}|{{COALESCE(+(d, d), -1)}}|-1|-1|
> |{{IFNULL(d + d, -1)}}|{{IFNULL(+(d, d), -1)}}|-1|-1|
> The planner types the CASE as NOT NULL, since {{d + d}} can't be NULL once 
> {{d}} isn't, so the sum that overflows fails the job.
> 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. For 
> the same operators, the simplification must not reduce {{IS NULL}} or {{IS 
> NOT NULL}} of the result to its operands, as CALCITE-3368 proposes. 
> 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)

Reply via email to