Ramin Gharib created FLINK-41804:
------------------------------------

             Summary: LPAD and RPAD return NULL under a NOT NULL result type
                 Key: FLINK-41804
                 URL: https://issues.apache.org/jira/browse/FLINK-41804
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / API, Table SQL / Planner
    Affects Versions: 2.3.0, 1.20.3
            Reporter: Ramin Gharib


{{LPAD}} and {{RPAD}} return NULL for a negative length or an empty pad string. 
With NOT NULL arguments, their result type is {{STRING NOT NULL}}, so the NULL 
reaches operators that rely on the type:

{code:sql}
SELECT str, len, LPAD(str, len, '*') AS r
FROM (VALUES ('ab', -1), ('cd', 3)) AS t(str, len);
{code}

|| Query on the row ('ab', -1) || Expected || Actual ||
| {{LPAD(str, len, '*')}} selected | NULL | The job fails: {{Column 'r' is NOT 
NULL, however, a null value is being written into it.}} |
| {{COALESCE(LPAD(str, len, '*'), 'none')}} | {{none}} | The job fails with the 
same error |
| {{CHAR_LENGTH(LPAD(str, len, '*'))}} | NULL | {{-1}} |
| {{CASE WHEN LPAD(str, len, '*') IS NULL THEN 'null' ELSE 'not null' END}} | 
{{null}} | {{not null}} |
| {{WHERE LPAD(str, len, '*') IS NOT NULL}} | row dropped | row kept |

{{RPAD(str, 4, '')}} behaves the same. Batch and streaming mode give the same 
results.

h3. Cause

FLINK-13783 in 1.12 moved both functions to the new type inference, with a 
result type that is nullable only for a nullable argument: 
{{nullable(explicit(DataTypes.STRING()))}}, today {{nullableIfArgs}}. Before, 
the SQL operator declared the result always nullable, 
{{VARCHAR_2000_NULLABLE}}. The runtime still returns NULL for a negative length 
and an empty pad string, so the type is wrong for those inputs.

h3. Proposal

Declare the result type of {{LPAD}} and {{RPAD}} as always nullable, as before 
1.12. Until then, the function pages of FLINK-41737 tell users to check the 
arguments, as in {{CASE WHEN len < 0 OR pad_str = '' THEN NULL ELSE LPAD(str, 
len, pad_str) END}}.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to