Skip to content

fix: return error on offset overflow in ScalarValue::to_array_of_size - #27291

Open
ETS2K7 wants to merge 1 commit into
apache:mainfrom
ETS2K7:fix/scalar-to-array-offset-overflow
Open

ETS2K7 wants to merge 1 commit into
apache:mainfrom
ETS2K7:fix/scalar-to-array-offset-overflow

Conversation

@ETS2K7

@ETS2K7 ETS2K7 commented Nov 10, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

Calling ScalarValue::to_array_of_size on Utf8, LargeUtf8, Binary, or LargeBinary scalars panics when value.len() * size overflows the array offset type (i32::MAX for Utf8/Binary, i64::MAX for LargeUtf8/LargeBinary) or usize, because GenericByteArray::new_repeated invokes OffsetBuffer::from_repeated_length, which panics on overflow. Queries such as SELECT repeat('a', 1048576) AS s FROM generate_series(1, 2048) crash instead of returning an error.

What changes are included in this PR?

  • Extract try_repeated_byte_array<T: ByteArrayType> in datafusion/common/src/scalar/mod.rs and route the Utf8, LargeUtf8, Binary, and LargeBinary arms in ScalarValue::to_array_of_size through it.
  • Guard against usize multiplication overflow, T::Offset overflow (returning ArrowError::OffsetOverflowError), and isize::MAX address-space limits (for Large* on 32-bit targets) before calling GenericByteArray::new_repeated.
  • Document the new overflow error condition in ScalarValue::to_array_of_size's # Errors section.

What is the testing strategy for this PR?

  • Added unit tests in datafusion/common/src/scalar/mod.rs (test_to_array_of_size_byte_offset_overflow) covering i32::MAX overflow (Utf8, Binary), usize multiplication overflow, i64::MAX overflow (LargeUtf8, LargeBinary on 64-bit), and valid empty/zero-size/non-overflow cases.
  • Added an end-to-end sqllogictest query in datafusion/sqllogictest/test_files/string/string_literal.slt executing the SQL reproduction (SELECT repeat('a', 1048576) AS s FROM generate_series(1, 2048);).
  • Verified pre-fix behavior on main (9e0a08fd8): confirmed it panics at arrow-buffer's OffsetBuffer::from_repeated_length with offset overflow: 2147483648 does not fit in i32, while with this patch it returns DataFusion error: Arrow error: Offset overflow error: 2147483648.

Are there any user-facing changes?

Queries that previously panicked on offset overflow in ScalarValue::to_array_of_size now fail gracefully with an ArrowError::OffsetOverflowError or DataFusionError::Execution error.

ScalarValue::to_array_of_size called StringArray::new_repeated,
LargeStringArray::new_repeated, BinaryArray::new_repeated, and
LargeBinaryArray::new_repeated directly, which panic when
value.len() * size overflows the offset type (i32::MAX / i64::MAX)
or usize.

Extract a shared try_repeated_byte_array<T: ByteArrayType> helper that
checks checked_mul, T::Offset::from_usize (returning
ArrowError::OffsetOverflowError), and isize::try_from before
constructing the array.

Add unit tests in datafusion/common/src/scalar/mod.rs and an end-to-end
SQL test in datafusion/sqllogictest/test_files/string/string_literal.slt
for apache#27277.

Signed-off-by: Ebel Thomas Seiko <ebelthomasseiko@gmail.com>
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) common Related to common crate labels Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ScalarValue::to_array_of_size panics when a Utf8 or Binary scalar repeated size times passes i32::MAX bytes

1 participant