Subodh-17 opened a new pull request, #27643:
URL: https://github.com/apache/datafusion/pull/27643

   > [!IMPORTANT]
   >
   > **Fork CI setup:** For pull requests from a fork to `main`, enable 
workflows in your fork's **Actions** tab before you push.
   > See the [fork CI setup 
guide](https://datafusion.apache.org/contributor-guide/#pull-requests-from-forks).
   
   ## Which issue does this PR close?
   
   Closes #27623.
   
   ## Rationale for this change
   
   In `repeat` on standard Arrow `Utf8` string arrays 
(`datafusion/functions/src/string/repeat.rs`), each row's repeated length was 
capped at `i32::MAX` bytes, but the cumulative total across all rows in a batch 
was not bounded before passing `total_capacity` into 
`GenericStringArrayBuilder::<i32>::with_capacity`.
   
   When an array contains multiple rows whose combined output exceeds 
`i32::MAX` (2,147,483,647 bytes), the builder attempts a multi-gigabyte buffer 
allocation upfront and triggers an offset overflow panic (`"byte array offset 
overflow"`) upon appending values because standard Arrow `Utf8` offsets are 
32-bit signed integers (`i32`).
   
   PR #26957 previously fixed the equivalent cumulative overflow issue for 
`LargeUtf8` (`LARGE_UTF8_MAX_SIZE`), but standard `Utf8` remained vulnerable 
when multiple rows combined to exceed `i32::MAX`.
   
   ## What changes are included in this PR?
   
   - Added a check in `repeat()` under the `Utf8` match arm:
     ```rust
     if total_capacity > i32::MAX as usize {
         return exec_err!(
             "string size overflow on repeat, max size is {}, but got {}",
             i32::MAX,
             total_capacity
         );
     }
     ```
     This immediately returns a `DataFusionError::Execution` before any builder 
memory is allocated.
   - Maintained symmetry with the existing `LargeUtf8` check and error message 
format.
   - Left `Utf8View` uncapped, as it supports multi-buffer allocation without a 
2 GiB offset limit.
   
   ## What is the testing strategy for this PR?
   
   Added two deterministic, memory-safe unit tests in 
`datafusion/functions/src/string/repeat.rs`:
   - `test_repeat_string_array_total_exceeds_max_size`: Large array (131,072 
rows) where each row is within `i32::MAX` but the cumulative total exceeds 
`i32::MAX`. Fails immediately before builder buffer allocation (< 1 ms runtime).
   - `test_repeat_string_array_total_overflow_small_batch`: Two rows of ~1 GiB 
each totalling `i32::MAX + 1` bytes, verifying exact off-by-one boundary 
rejection.
   
   ## Are there any user-facing changes?
   
   No breaking changes. Queries calling `repeat` on `Utf8` arrays whose 
cumulative result exceeds `i32::MAX` will now cleanly return a 
`DataFusionError::Execution` instead of panicking or aborting the process.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to