SPY-Github22 opened a new pull request, #27116:
URL: https://github.com/apache/datafusion/pull/27116

   ## Which issue does this PR close?
   
   - Closes #26950.
   
   ## Rationale for this change
   
   When calling `ceil` or `floor` on a decimal scalar with `number_rows: 0`, 
DataFusion panics with:
   ```text
   Trying to access an element at index 0 from a PrimitiveArray of length 0
   ```
   This is because both functions previously converted decimal scalars using 
`arg.to_array(args.number_rows)?`. When `args.number_rows` is 0, this created 
an empty array of length 0, so extracting row 0 
(`ScalarValue::try_from_array(&result, 0)`) panicked. Furthermore, when 
`args.number_rows` is large, expanding the scalar to `args.number_rows` items 
only to read index 0 wastes memory and allocations, contradicting the comment 
above the code:
   ```rust
   // For decimals: convert to array of size 1, process, then extract scalar
   // This ensures we don't expand the array while reusing overflow validation
   ```
   
   ## What changes are included in this PR?
   
   - In `datafusion/functions/src/math/ceil.rs` and 
`datafusion/functions/src/math/floor.rs`, when the input is a scalar 
(`is_scalar == true`), convert to an array of size 1 via `arg.to_array(1)?` 
rather than `arg.to_array(args.number_rows)?`.
   - Added unit tests to `ceil.rs` and `floor.rs` covering:
     - `Decimal32`, `Decimal64`, `Decimal128`, and `Decimal256` scalar 
arguments with `number_rows == 0` (verifying they match `number_rows == 1`).
     - Positive, negative, and null decimal scalars.
     - Empty 0-row array inputs.
   
   ## What is the testing strategy for this PR?
   
   - Added `test_ceil_decimal_scalar_zero_rows` and 
`test_ceil_decimal_array_zero_rows` in `datafusion/functions/src/math/ceil.rs`.
   - Added `test_floor_decimal_scalar_zero_rows` and 
`test_floor_decimal_array_zero_rows` in 
`datafusion/functions/src/math/floor.rs`.
   - Ran `cargo test -p datafusion-functions --lib math::` (76 passed, 0 
failed).
   - Ran `cargo clippy -p datafusion-functions --all-targets -- -D warnings`.
   - Ran `cargo fmt --check`.
   
   ## Are there any user-facing changes?
   
   No breaking API changes; prevents a panic when evaluating `ceil` or `floor` 
on a decimal scalar with 0 rows.
   


-- 
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