Yaoxuan Wu created FLINK-41754:
----------------------------------

             Summary: PyFlink pandas UDF / UDAF input and Table.to_pandas() 
fail with 'Map key type should be non-nullable' for MAP columns with the 
default (nullable) key type
                 Key: FLINK-41754
                 URL: https://issues.apache.org/jira/browse/FLINK-41754
             Project: Flink
          Issue Type: Bug
          Components: API / Python
    Affects Versions: 2.3.0
            Reporter: Yaoxuan Wu


A pandas (vectorized) scalar UDF or pandas UDAF that takes a MAP column fails 
when the MAP's key type is nullable. Table.to_pandas() fails the same way. A 
nullable key type is the default ({{MAP<STRING, INT>}} in DDL, 
{{DataTypes.MAP(DataTypes.STRING(), ...)}}), so almost every MAP column is 
affected.

{code:python}
import json, os, tempfile
from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes
from pyflink.table.udf import udf, udaf

t = TableEnvironment.create(EnvironmentSettings.in_batch_mode())
d = tempfile.mkdtemp()
with open(os.path.join(d, 'part-0.json'), 'w') as f:
    f.write(json.dumps({'g': 1, 'm': {'a': 1}}) + '\n')
    f.write(json.dumps({'g': 1, 'm': {'b': 2, 'c': 3}}) + '\n')
    f.write(json.dumps({'g': 2, 'm': None}) + '\n')
for name, map_type in [('t', 'MAP<STRING, INT>'), ('t_nn', 'MAP<STRING NOT 
NULL, INT>')]:
    t.execute_sql(f"CREATE TEMPORARY TABLE {name} (g INT, m {map_type}) "
                  f"WITH ('connector' = 'filesystem', 'path' = 'file://{d}', 
'format' = 'json')")

t.create_temporary_function('map_size', udf(
    lambda s: s.map(lambda m: -1 if m is None else len(m)),
    result_type=DataTypes.BIGINT(), func_type='pandas'))
t.create_temporary_function('row_count', udaf(
    lambda s: len(s), result_type=DataTypes.BIGINT(), func_type='pandas'))

t.execute_sql("SELECT g, map_size(m) FROM t").print()                # fails
t.execute_sql("SELECT g, row_count(m) FROM t GROUP BY g").print()    # fails
t.from_path('t').to_pandas()                                          # fails
t.execute_sql("SELECT g, map_size(m) FROM t_nn").print()             # works: 
(1, 1), (1, 2), (2, -1)
t.execute_sql("SELECT g, row_count(m) FROM t_nn GROUP BY g").print() # works: 
(1, 2), (2, 1)
{code}

{noformat}
java.lang.IllegalArgumentException: Map key type should be non-nullable
    at org.apache.flink.util.Preconditions.checkArgument(Preconditions.java:138)
    at 
org.apache.flink.table.runtime.arrow.ArrowUtils.toArrowField(ArrowUtils.java:224)
    at 
org.apache.flink.table.runtime.arrow.ArrowUtils.toArrowSchema(ArrowUtils.java:201)
    at 
org.apache.flink.table.runtime.arrow.serializers.ArrowSerializer.open(ArrowSerializer.java:92)
    at 
org.apache.flink.table.runtime.operators.python.scalar.arrow.ArrowPythonScalarFunctionOperator.open(ArrowPythonScalarFunctionOperator.java:73)
{noformat}

* Expected: the same results as for the {{MAP<STRING NOT NULL, INT>}} table 
(the data has no NULL key).
* Actual: IllegalArgumentException when the Python operator opens (scalar UDF 
in batch and streaming mode, pandas UDAF, to_pandas()).
* A pandas UDF that returns {{MAP<STRING, BIGINT>}} (nullable key) works; only 
the input side is affected.
* Same on 2.0.1, 2.2.1, 2.3.0 and master (2.4-SNAPSHOT, 2f01434, 2026-11-19).

Cause:
* {{ArrowUtils.toArrowField}} has 
{{Preconditions.checkArgument(!mapType.getKeyType().isNullable(), "Map key type 
should be non-nullable")}} (added with MAP support in FLINK-30607). Arrow 
requires a non-nullable key field, but Flink's MAP key type is nullable unless 
declared NOT NULL.

A possible fix: always declare the Arrow key field non-nullable 
({{toArrowField("key", mapType.getKeyType().copy(false))}}) and fail only if an 
actual NULL key is written. Spark's ArrowUtils does the same (map keys are 
always nullable = false).




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

Reply via email to