From f25bf2e51669ae939a0f528b57af81baab00375c Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Thu, 10 Sep 2026 16:07:47 -0700 Subject: [PATCH 1/2] fix: report a negative array_resize size as a user error The size comes from the query, so a negative value is the caller's input, not a broken invariant. Raising an internal error told them to file a bug report for their own argument. Generated-by: GitHub Copilot CLI (Claude Opus 5) Signed-off-by: 1fanwang <1fannnw@gmail.com> --- datafusion/functions-nested/src/resize.rs | 24 ++++++++++++------- .../test_files/array/array_resize.slt | 11 +++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/datafusion/functions-nested/src/resize.rs b/datafusion/functions-nested/src/resize.rs index 0332ea7d5d7cb..92c3c0900dfa6 100644 --- a/datafusion/functions-nested/src/resize.rs +++ b/datafusion/functions-nested/src/resize.rs @@ -24,14 +24,16 @@ use arrow::array::{ }; use arrow::buffer::OffsetBuffer; use arrow::datatypes::DataType; -use arrow::datatypes::{ArrowNativeType, Field}; +use arrow::datatypes::Field; use arrow::datatypes::{ DataType::{LargeList, List}, FieldRef, }; use datafusion_common::cast::{as_int64_array, as_large_list_array, as_list_array}; use datafusion_common::utils::ListCoercion; -use datafusion_common::{Result, ScalarValue, exec_err, internal_datafusion_err}; +use datafusion_common::{ + Result, ScalarValue, exec_datafusion_err, exec_err, internal_datafusion_err, +}; use datafusion_expr::{ ArrayFunctionArgument, ArrayFunctionSignature, ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility, @@ -186,6 +188,16 @@ fn array_resize_inner(arg: &[ArrayRef]) -> Result { } } +/// Resolve the requested size for one row. A negative size is rejected as a +/// user error; reporting it as an internal error asks the caller to file a bug +/// report for input they control. +fn resize_count(count_array: &Int64Array, idx: usize) -> Result { + let c = count_array.value(idx); + usize::try_from(c).map_err(|_| { + exec_datafusion_err!("array_resize: size must not be negative, got {c}") + }) +} + /// array_resize keep the original array and append the default element to the end fn general_list_resize>( array: &GenericListArray, @@ -206,9 +218,7 @@ fn general_list_resize>( if array.is_null(row_index) || count_array.is_null(row_index) { continue; } - let target_count = count_array.value(row_index).to_usize().ok_or_else(|| { - internal_datafusion_err!("array_resize: failed to convert size to usize") - })?; + let target_count = resize_count(count_array, row_index)?; output_values_len = output_values_len.checked_add(target_count).ok_or_else(|| { internal_datafusion_err!("array_resize: output size overflow") @@ -324,9 +334,7 @@ where } null_builder.append_non_null(); - let count = count_array.value(row_index).to_usize().ok_or_else(|| { - internal_datafusion_err!("array_resize: failed to convert size to usize") - })?; + let count = resize_count(count_array, row_index)?; let count = O::usize_as(count); let start = offset_window[0]; if start + count > offset_window[1] { diff --git a/datafusion/sqllogictest/test_files/array/array_resize.slt b/datafusion/sqllogictest/test_files/array/array_resize.slt index 37f8f2c6935c6..88c1479d13801 100644 --- a/datafusion/sqllogictest/test_files/array/array_resize.slt +++ b/datafusion/sqllogictest/test_files/array/array_resize.slt @@ -193,4 +193,15 @@ NULL [61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 9, 9, 9, 9, 9] +# a negative size is the caller's error, so it must not surface as an internal error +query error DataFusion error: Execution error: array_resize: size must not be negative, got \-1 +select array_resize(make_array(1, 2, 3), -1, 0); + +query error DataFusion error: Execution error: array_resize: size must not be negative, got \-9223372036854775808 +select array_resize(make_array(1, 2, 3), -9223372036854775808, 0); + +query error DataFusion error: Execution error: array_resize: size must not be negative, got \-1 +select array_resize(arrow_cast(make_array(1, 2, 3), 'LargeList(Int64)'), -1, 0); + + include ./cleanup.slt.part From 847b155277d6dfa630b78310aa7407060e968be3 Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Fri, 11 Sep 2026 13:22:56 -0700 Subject: [PATCH 2/2] Remove the resize error history comment Signed-off-by: 1fanwang <1fannnw@gmail.com> --- datafusion/functions-nested/src/resize.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/datafusion/functions-nested/src/resize.rs b/datafusion/functions-nested/src/resize.rs index 92c3c0900dfa6..540db7d7fb5bf 100644 --- a/datafusion/functions-nested/src/resize.rs +++ b/datafusion/functions-nested/src/resize.rs @@ -188,9 +188,6 @@ fn array_resize_inner(arg: &[ArrayRef]) -> Result { } } -/// Resolve the requested size for one row. A negative size is rejected as a -/// user error; reporting it as an internal error asks the caller to file a bug -/// report for input they control. fn resize_count(count_array: &Int64Array, idx: usize) -> Result { let c = count_array.value(idx); usize::try_from(c).map_err(|_| {