Yaoxuan Wu created FLINK-41753:
----------------------------------
Summary: Pandas (vectorized) scalar UDF fails when an argument is
a literal (ArrayIndexOutOfBoundsException in ArrowWriter.finish / TypeError in
check_pandas_udf_result)
Key: FLINK-41753
URL: https://issues.apache.org/jira/browse/FLINK-41753
Project: Flink
Issue Type: Bug
Components: API / Python
Affects Versions: 2.3.0
Reporter: Yaoxuan Wu
A pandas (vectorized) scalar UDF fails when one of its arguments is a literal
and the literal is the first argument or the only kind of argument. The literal
does not have to be written in the query: the planner propagates {{WHERE g =
2}} into the UDF call. A general Python UDF works in the same queries.
{code:python}
import os, tempfile
from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes
from pyflink.table.udf import udf
t = TableEnvironment.create(EnvironmentSettings.in_batch_mode())
d = tempfile.mkdtemp()
with open(os.path.join(d, 'part-0.json'), 'w') as f:
f.write('{"g": 1, "s": "1"}\n{"g": 2, "s": "2"}\n{"g": 3, "s": "3"}\n')
t.execute_sql(f"CREATE TEMPORARY TABLE f (g INT, s STRING) "
f"WITH ('connector' = 'filesystem', 'path' = 'file://{d}',
'format' = 'json')")
t.create_temporary_function('pinc', udf(lambda a: a + 1,
result_type=DataTypes.BIGINT(), func_type='pandas'))
t.create_temporary_function('padd', udf(lambda a, b: a + b,
result_type=DataTypes.BIGINT(), func_type='pandas'))
t.create_temporary_function('pcat', udf(lambda a, b: a + b,
result_type=DataTypes.STRING(), func_type='pandas'))
t.create_temporary_function('inc', udf(lambda a: a + 1,
result_type=DataTypes.BIGINT()))
t.execute_sql("SELECT g, inc(5) FROM f").print() # works: (1, 6),
(2, 6), (3, 6)
t.execute_sql("SELECT g, pinc(5) FROM f").print() #
ArrayIndexOutOfBoundsException
t.execute_sql("SELECT g, pinc(g) FROM f WHERE g = 2").print() #
ArrayIndexOutOfBoundsException (plan: pinc(2))
t.execute_sql("SELECT g, padd(g, 7) FROM f").print() # works: (1, 8),
(2, 9), (3, 10)
t.execute_sql("SELECT g, padd(7, g) FROM f").print() # TypeError:
object of type 'int' has no len()
t.execute_sql("SELECT g, pcat('ab', s) FROM f").print() #
AssertionError: The result length '3' ... is not equal to the input length '2'
t.execute_sql("SELECT g, pcat('abc', s) FROM f").print() # works, only
because len('abc') == 3 rows
{code}
{noformat}
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for
length 0
at
org.apache.flink.table.runtime.arrow.ArrowWriter.finish(ArrowWriter.java:62)
File ".../pyflink/fn_execution/utils/operation_utils.py", line 90, in
check_pandas_udf_result
assert len(output) == len(input_args[0]), \
TypeError: object of type 'int' has no len()
{noformat}
* Expected: the same results as with a general Python UDF ({{pinc(5)}} gives 6
per row, {{padd(7, g)}} gives g + 7, {{pcat('ab', s)}} gives 'ab' || s).
* Actual: the errors above. A literal that is not the first argument works
({{padd(g, 7)}}).
* {{SELECT h, padd(g, h) FROM f WHERE g = 2}} also fails (planned as {{padd(2,
h)}}).
* Same in batch and streaming mode, on 2.0.1, 2.2.1, 2.3.0 and master
(2.4-SNAPSHOT, 2f01434, 2026-11-19).
* pandas UDAFs with a literal argument work.
Cause:
* Literal arguments are not Arrow input columns; they are sent in the function
proto as {{inputConstant}}. When no argument is a column, the Arrow writer of
{{ArrowPythonScalarFunctionOperator}} has no field writers and
{{ArrowWriter.finish()}} does {{root.setRowCount(fieldWriters[0].getCount())}}.
* On the Python side, {{operation_utils._extract_input}} passes the literal as
a plain Python scalar, and {{check_pandas_udf_result}} compares the result
length with {{len(input_args[0])}}, the length of the first argument (a scalar,
or a string literal).
A possible fix: broadcast literal arguments of pandas UDFs to a Series of the
batch length (or send them as Arrow columns), keep the Arrow row count
independent of the field writers, and check the result length against the batch
length.
Possibly related: FLINK-38871 (constant propagation into Python UDF inputs).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)