Skip to content

fix(functions): prevent panic on ceil and floor for decimal scalar with 0 rows (#26950) - #26955

Open
XiaoMingFa wants to merge 1 commit into
apache:mainfrom
XiaoMingFa:fix/ceil-floor-decimal-zero-rows-26950
Open

XiaoMingFa wants to merge 1 commit into
apache:mainfrom
XiaoMingFa:fix/ceil-floor-decimal-zero-rows-26950

Conversation

@XiaoMingFa

Copy link
Copy Markdown

Which issue does this PR close?

Closes #26950.

Rationale for this change

When evaluating ceil or floor on a decimal scalar argument, the scalar is converted into an array to reuse the decimal operator and precision/scale calculations, and the resulting scalar is then extracted from index 0.

Previously, arg.to_array(args.number_rows) was used. When args.number_rows == 0, this produced an empty PrimitiveArray of length 0, causing ScalarValue::try_from_array(&result, 0) to panic:

Trying to access an element at index 0 from a PrimitiveArray of length 0

Furthermore, the architectural comment in both functions explicitly noted:

// For decimals: convert to array of size 1, process, then extract scalar
// This ensures we don't expand the array while reusing overflow validation

Using args.number_rows instead of 1 defeated this optimization when args.number_rows > 1 and panicked when args.number_rows == 0.

What changes are included in this PR?

  • In ceil.rs and floor.rs, when is_scalar is true, convert the scalar to an array of size 1 (arg.to_array(1)).
  • Preserve arg.to_array(args.number_rows) for array inputs (is_scalar == false).
  • Added unit regression tests for both ceil and floor covering decimal scalars evaluated with number_rows: 0.

Are these changes tested?

Yes, new unit tests test_ceil_decimal_scalar_zero_rows and test_floor_decimal_scalar_zero_rows verify this behavior and pass. All existing math tests continue to pass.

Are there any user-facing changes?

No breaking changes. Fixes panics when ceil or floor is called with decimal scalars under zero rows.

…th 0 rows (apache#26950)

When invoking `ceil` or `floor` on a decimal scalar argument, the function
converts the scalar into an array to reuse the decimal operator logic, then
extracts the resulting scalar from index 0. Previously, it converted using
`args.number_rows`. If `args.number_rows` was 0, an empty array of length 0
was created, and reading row 0 panicked with:
"Trying to access an element at index 0 from a PrimitiveArray of length 0".

This change:
- Converts decimal scalars to an array of size 1 via `arg.to_array(1)` instead
  of `arg.to_array(args.number_rows)` when `is_scalar` is true, matching the
  architectural design comment and avoiding unnecessary array expansion or empty arrays.
- Preserves the array path when `is_scalar` is false.
- Adds regression unit tests for both `ceil` and `floor` verifying decimal scalar
  evaluation with `number_rows: 0`.

Closes apache#26950.

Signed-off-by: Mingfa Lim <346975989+XiaoMingFa@users.noreply.github.com>
@github-actions github-actions Bot added the functions Changes to functions implementation label Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant