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

Martijn Visser updated FLINK-41609:
-----------------------------------
    Labels: correctness starter  (was: correctness)

> ARRAY_AGG over a NOT NULL argument is typed NOT NULL but returns NULL on an 
> empty input (IS NULL is FALSE, CARDINALITY is -1, COALESCE dropped)
> -----------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: FLINK-41609
>                 URL: https://issues.apache.org/jira/browse/FLINK-41609
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / Planner, Table SQL / Runtime
>    Affects Versions: 2.0.1, 2.3.0, 2.2.1
>            Reporter: Yaoxuan Wu
>            Priority: Major
>              Labels: correctness, starter
>
> {{ARRAY_AGG}} over a NOT NULL argument has a NOT NULL result type, but it 
> returns NULL for an empty input. Every expression that trusts the declared 
> type then gives a wrong answer: {{IS NULL}} is FALSE, {{CARDINALITY}} is -1, 
> {{CAST(.. AS STRING)}} is an empty string, and {{COALESCE}} or a plain sink 
> fails in the NOT NULL enforcer.
> {code:sql}
> -- t(x INT NOT NULL) with the rows 1, 2   (batch mode)
> SELECT ARRAY_AGG(x) IS NULL FROM t WHERE x > 5;               -- expected 
> TRUE,  actual FALSE
> SELECT CARDINALITY(ARRAY_AGG(x)) FROM t WHERE x > 5;          -- expected 
> NULL,  actual -1
> SELECT CAST(ARRAY_AGG(x) AS STRING) FROM t WHERE x > 5;       -- expected 
> NULL,  actual ''
> SELECT COALESCE(ARRAY_AGG(x), ARRAY[42]) FROM t WHERE x > 5;  -- expected 
> [42], actual: job fails
> SELECT ARRAY_AGG(x) FROM t WHERE x > 5;                       -- expected 
> NULL,  actual: job fails
> -- EnforcerException: Column 'EXPR$0' is NOT NULL, however, a null value is 
> being written into it.
> {code}
> The table is built in PyFlink as {{from_elements([(1,), (2,)], 
> ROW([FIELD('x', INT(False))]))}}; any source with a NOT NULL column behaves 
> the same, and so does the Table API ({{$("x").arrayAgg()}}). The result type 
> is {{ARRAY<INT NOT NULL> NOT NULL}}. With a nullable argument the result type 
> is nullable and all of the above are correct.
> * Batch mode. In streaming mode a global aggregate over an empty input emits 
> no row (FLINK-41098), which hides it.
> * Same on 2.0.1, 2.2.1, 2.3.0 and master (2.4-SNAPSHOT, 2f01434). On 2.0.1 
> the failing queries end with a job failure instead of the enforcer message.
> Cause: {{BuiltInFunctionDefinitions.ARRAY_AGG}} declares 
> {{outputTypeStrategy(nullableIfArgs(SpecificTypeStrategies.ARRAY))}}, so the 
> result is NOT NULL when the argument is NOT NULL. An aggregate is NULL over 
> an empty input whatever its argument; the other aggregates that can return 
> NULL (MIN, MAX, SUM, AVG, FIRST_VALUE, LAST_VALUE, ...) use a forced-nullable 
> strategy ({{aggArg0(.., true)}}), LISTAGG uses 
> {{explicit(STRING().nullable())}}.
> A possible fix: a forced-nullable output type for ARRAY_AGG, e.g. 
> {{forceNullable(SpecificTypeStrategies.ARRAY)}}.
> Related: FLINK-21949 (ARRAY_AGG added), FLINK-23385 (COALESCE nullability).



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

Reply via email to