-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: return execution error instead of capacity overflow panic in array_resize #23306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,6 +219,14 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>( | |
| } | ||
| } | ||
|
|
||
| if output_values_len > max_resize_values(&data_type) | ||
| || O::from_usize(output_values_len).is_none() | ||
| { | ||
| return exec_err!( | ||
| "array_resize: resulting array of {output_values_len} elements exceeds the maximum array size" | ||
| ); | ||
| } | ||
|
|
||
| // The fast path is valid when at least one row grows and every row would | ||
| // use the same fill value. | ||
| let use_bulk_fill = max_extra > 0 | ||
|
|
@@ -342,12 +350,26 @@ where | |
| )?)) | ||
| } | ||
|
|
||
| /// Largest element count whose eager value buffer stays within `isize::MAX` | ||
| /// bytes, so `array_resize` rejects oversized results instead of panicking. | ||
| /// Only primitive and `FixedSizeBinary` leaves are byte-exact. | ||
| fn max_resize_values(value_type: &DataType) -> usize { | ||
| let element_width = match value_type { | ||
| DataType::FixedSizeBinary(size) if *size > 0 => *size as usize, | ||
| _ => value_type.primitive_width().unwrap_or(size_of::<u128>()), | ||
| }; | ||
|
|
||
| (isize::MAX as usize) / element_width.max(1) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::array_resize_inner; | ||
| use arrow::array::{ArrayRef, AsArray, Int64Array, ListArray}; | ||
| use arrow::buffer::{NullBuffer, ScalarBuffer}; | ||
| use arrow::datatypes::Int32Type; | ||
| use arrow::array::{ | ||
| ArrayRef, AsArray, FixedSizeBinaryArray, Int64Array, LargeListArray, ListArray, | ||
| }; | ||
| use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; | ||
| use arrow::datatypes::{DataType, Field, Int32Type, Int64Type}; | ||
| use datafusion_common::Result; | ||
| use std::sync::Arc; | ||
|
|
||
|
|
@@ -373,4 +395,73 @@ mod tests { | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I think these tests are somewhat redundant -- the SLT is probably enough. |
||
| fn test_array_resize_large_size_errors_without_panicking() { | ||
| let array: ArrayRef = | ||
| Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![ | ||
| Some(vec![Some(1)]), | ||
| ])); | ||
| let size: ArrayRef = Arc::new(Int64Array::from(vec![i64::MAX])); | ||
| let fill: ArrayRef = Arc::new(Int64Array::from(vec![0])); | ||
|
|
||
| let err = array_resize_inner(&[array, size, fill]).unwrap_err(); | ||
| assert!( | ||
| err.to_string().contains("exceeds the maximum array size"), | ||
| "unexpected error: {err}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_array_resize_fixed_size_binary_large_size_errors_without_panicking() { | ||
| let values = | ||
| FixedSizeBinaryArray::try_from_iter(vec![vec![0u8; 32]].into_iter()).unwrap(); | ||
| let elem_field = | ||
| Arc::new(Field::new_list_field(DataType::FixedSizeBinary(32), true)); | ||
| let offsets = OffsetBuffer::<i64>::new(vec![0i64, 1].into()); | ||
| let array: ArrayRef = Arc::new(LargeListArray::new( | ||
| elem_field, | ||
| offsets, | ||
| Arc::new(values) as ArrayRef, | ||
| None, | ||
| )); | ||
| // Passes the width-16 bound (isize::MAX / 16) but overflows at width 32. | ||
| let size: ArrayRef = Arc::new(Int64Array::from(vec![400_000_000_000_000_000i64])); | ||
|
|
||
| let err = array_resize_inner(&[array, size]).unwrap_err(); | ||
| assert!( | ||
| err.to_string().contains("exceeds the maximum array size"), | ||
| "unexpected error: {err}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_array_resize_accumulates_values_across_rows() { | ||
| // Each row's target (6e17) is individually under the width-8 cap | ||
| // (isize::MAX / 8), but their sum (1.2e18) exceeds it, so the guard | ||
| // must reject based on the accumulated total rather than per row. | ||
| let values = Int64Array::from(vec![1, 2]); | ||
| let offsets = OffsetBuffer::<i64>::new(vec![0i64, 1, 2].into()); | ||
| let elem_field = Arc::new(Field::new_list_field(DataType::Int64, true)); | ||
| let array: ArrayRef = Arc::new(LargeListArray::new( | ||
| elem_field, | ||
| offsets, | ||
| Arc::new(values) as ArrayRef, | ||
| None, | ||
| )); | ||
| let size: ArrayRef = Arc::new(Int64Array::from(vec![ | ||
| 600_000_000_000_000_000i64, | ||
| 600_000_000_000_000_000i64, | ||
| ])); | ||
|
|
||
| let err = array_resize_inner(&[array, size]).unwrap_err(); | ||
| assert!( | ||
| err.to_string().contains("1200000000000000000"), | ||
| "expected accumulated total in error: {err}" | ||
| ); | ||
| assert!( | ||
| err.to_string().contains("exceeds the maximum array size"), | ||
| "unexpected error: {err}" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the compiler prob materialize it, but would it be better to prematerialize it manually through
static LazyLock<HashMap<DataType, usize>>?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there are parameterized types I would need to keep match right? I'm not sure it would be better but can't say I've a strong opinion. If you say go for it I can change it that way