mbutrovich opened a new issue, #26950:
URL: https://github.com/apache/datafusion/issues/26950

   ### Describe the bug
   
   `ceil` and `floor` turn a decimal scalar argument into an array of 
`number_rows` rows and then read row 0 of the result. With `number_rows = 0` 
that array is empty, and the read panics:
   
   ```text
   Trying to access an element at index 0 from a PrimitiveArray of length 0
   ```
   
   The comment above the scalar path says decimals are converted "to array of 
size 1", but the conversion uses `args.number_rows`:
   
   - `ceil`: 
[comment](https://github.com/apache/datafusion/blob/926901f9001977e85df617b96328801b42f6af4a/datafusion/functions/src/math/ceil.rs#L103),
 
[conversion](https://github.com/apache/datafusion/blob/926901f9001977e85df617b96328801b42f6af4a/datafusion/functions/src/math/ceil.rs#L113),
 [read of row 
0](https://github.com/apache/datafusion/blob/926901f9001977e85df617b96328801b42f6af4a/datafusion/functions/src/math/ceil.rs#L194-L195)
   - `floor`: 
[comment](https://github.com/apache/datafusion/blob/926901f9001977e85df617b96328801b42f6af4a/datafusion/functions/src/math/floor.rs#L149),
 
[conversion](https://github.com/apache/datafusion/blob/926901f9001977e85df617b96328801b42f6af4a/datafusion/functions/src/math/floor.rs#L159),
 [read of row 
0](https://github.com/apache/datafusion/blob/926901f9001977e85df617b96328801b42f6af4a/datafusion/functions/src/math/floor.rs#L240-L241)
   
   Float scalars return before this point, so only decimals are affected.
   
   ### To Reproduce
   
   On `main` at 926901f90:
   
   ```rust
   use std::sync::Arc;
   
   use arrow::datatypes::Field;
   use datafusion_common::ScalarValue;
   use datafusion_common::config::ConfigOptions;
   use datafusion_expr::{ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs};
   use datafusion_functions::math::ceil;
   
   let udf = ceil();
   let arg = ScalarValue::Decimal32(Some(150), 9, 2);
   let arg_fields = vec![Arc::new(Field::new("a", arg.data_type(), true))];
   let return_field = udf
       .return_field_from_args(ReturnFieldArgs {
           arg_fields: &arg_fields,
           scalar_arguments: &[Some(&arg)],
       })
       .unwrap();
   let result = udf.invoke_with_args(ScalarFunctionArgs {
       args: vec![ColumnarValue::Scalar(arg)],
       arg_fields,
       number_rows: 0, // with 1: Ok(Scalar(Decimal32(2, 8, 0)))
       return_field,
       config_options: Arc::new(ConfigOptions::default()),
   });
   // panics: Trying to access an element at index 0 from a PrimitiveArray of 
length 0
   ```
   
   `floor` panics the same way. With `number_rows: 1`, `ceil` returns 
`Decimal32(2, 8, 0)` and `floor` returns `Decimal32(1, 8, 0)`.
   
   ### Expected behavior
   
   The same scalar as with `number_rows: 1`, as the comment describes. A scalar 
result doesn't depend on the number of rows.
   
   ### Additional context
   
   Found by property-based tests that call each function directly with 
generated arguments, including scalar arguments with zero rows.
   
   ### Willingness to contribute
   
   I cannot contribute a fix for this bug at this time


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