Skip to content

ceil and floor panic on a decimal scalar when number_rows is 0 #26950

Description

@mbutrovich

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:

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:

Float scalars return before this point, so only decimals are affected.

To Reproduce

On main at 926901f:

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

Activity

  1. added theissue type on Oct 30, 2026
  2. aruadecarvalho commented on Oct 30, 2026

    @aruadecarvalho

    take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions