Skip to content

feat(arrow-buffer): add OverflowError and fallible offset constructors - #10736

Merged
Jefffrey merged 4 commits into
apache:mainfrom
emilk:emilk/overflow-error
Sep 2, 2026
Merged

Jefffrey merged 4 commits into
apache:mainfrom
emilk:emilk/overflow-error

Conversation

@emilk

@emilk emilk commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

OffsetBuffer::<i32>::from_lengths panics once the lengths add up to more than 2 GiB. The other overflow panics in arrow-buffer are the same story.

What changes are included in this PR?

A small OverflowError and a try_ variant for functions that panics on overflow:

  • OffsetBuffer::{try_new_zeroed, try_from_lengths, try_from_repeated_length}
  • OffsetBufferBuilder::{try_push_length, try_finish, try_finish_cloned}
  • NullBuffer::try_expand

The panicking versions delegate to the fallible ones and re-panic with the error's Display, so their panic messages are unchanged.

try_push_length leaves the builder unchanged when it fails.

Are these changes tested?

Yes, new tests for the error paths. The existing should_panic tests are untouched and still pass, which is what pins the panic messages.

Are there any user-facing changes?

New public API only, no breaking changes.

`arrow-buffer` has no `ArrowError`, so following the `MutableBufferError`
precedent from apache#10317, add a small `OverflowError` for the functions that
panic on arithmetic overflow, and give each of them a `try_` variant:

* `OffsetBuffer::{try_new_zeroed, try_from_lengths, try_from_repeated_length}`
* `OffsetBufferBuilder::{try_push_length, try_finish, try_finish_cloned}`
* `NullBuffer::try_expand`

The panicking versions now delegate to the fallible ones, and panic with the
error's `Display`, so their messages are unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-buffer labels Aug 18, 2026
emilk and others added 2 commits August 18, 2026 18:06
`offset overflow` did not say what the offsets did not fit in. It now reads
`offset overflow: 4294967296 does not fit in i32`, which points at the 2 GiB
limit of 32 bit offsets and at `i64` as the fix.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`with_type` was easy to forget, which left errors that did not say what the
value failed to fit in. The type is now a parameter of `new`, so every
`OverflowError` names it: `total length overflow: does not fit in usize`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@emilk
emilk marked this pull request as ready for review August 18, 2026 16:16
`OffsetBuffer::from_repeated_length` now panics with
`total length overflow: does not fit in usize`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@Rich-T-kid Rich-T-kid left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emilk LGTM , A follow up PR will re-wire arrow-rs call sites right?

Comment thread arrow-buffer/src/error.rs
Comment on lines +34 to +39
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OverflowError {
what: &'static str,
value: Option<usize>,
type_name: &'static str,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are your thoughts on consildating this error types with the enum introduced in https://github.com/apache/arrow-rs/pull/10317/changes#diff-371342744df1b634b0bd9d90f4fe38c1eb0096df322fd3cc2fbc513f3428046cR38, I.E adding another variant.

I think this is different enough to warrent a seperate type but I thought id throw the idea out there

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we need an enum that contains OverflowError as one of its kinds. I'm not sure MutableBufferError is the right name for it, but I do think we should only have one such error enum per crate.

.checked_mul(n)
.ok_or_else(|| OverflowError::new::<usize>("total length"))?;

// Check for overflow

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we should try and keep the comments from main

/// # Errors
///
/// Errors if `self.len() * count` overflows `usize`
pub fn try_expand(&self, count: usize) -> Result<Self, OverflowError> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes sense to add a new variant to ArrowError / use an existing one rather than a whole new type. It might be strange to have entirely new types for errrs when the rest of the crates use the same unified error type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use ArrowError without adding a dependency on arrow-schema. But in a future PR I want to add a variant to ArrowError that just wraps OverflowError

@emilk

emilk commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

A follow up PR will re-wire arrow-rs call sites right?

Yes exactly. There are also some out-of-bounds-errors I need to add try_-methods for in arrow-buffer

@Jefffrey Jefffrey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me; main question is how we plan to deal with these error variants in future, if we want to consolidate etc. or just leave it as is?

@Jefffrey Jefffrey added the enhancement New feature/addition label Sep 1, 2026
@emilk

emilk commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

My plan was to have a few specific error types (e.g. OverflowError) plus one crate-level enum BufferError that joins all of them (with impl From<OverflowError> for BufferError). I think that approach strikes a good balance between specific ("this function can only fail due to overflows") and ergonomic ("this function can fail for several reasons; we'll document which").

We would also impl From<BufferError> for ArrowError.

(For instance: #10737 adds OutOfBoundsError)

@Jefffrey
Jefffrey merged commit 98ad0f3 into apache:main Sep 2, 2026
34 checks passed
@Jefffrey

Jefffrey commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

thanks @emilk, @alamb & @Rich-T-kid

Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…ors (apache#10736)

# Which issue does this PR close?

* Part of apache#10553
* Follows the `MutableBufferError` precedent from
apache#10317

# Rationale for this change

`OffsetBuffer::<i32>::from_lengths` panics once the lengths add up to
more than 2 GiB. The other overflow panics in `arrow-buffer` are the
same story.

# What changes are included in this PR?

A small `OverflowError` and a `try_` variant for functions that panics
on overflow:

* `OffsetBuffer::{try_new_zeroed, try_from_lengths,
try_from_repeated_length}`
* `OffsetBufferBuilder::{try_push_length, try_finish,
try_finish_cloned}`
* `NullBuffer::try_expand`

The panicking versions delegate to the fallible ones and re-panic with
the error's `Display`, so their panic messages are unchanged.

`try_push_length` leaves the builder unchanged when it fails.

# Are these changes tested?

Yes, new tests for the error paths. The existing `should_panic` tests
are untouched and still pass, which is what pins the panic messages.

# Are there any user-facing changes?

New public API only, no breaking changes.

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…ors (apache#10736)

# Which issue does this PR close?

* Part of apache#10553
* Follows the `MutableBufferError` precedent from
apache#10317

# Rationale for this change

`OffsetBuffer::<i32>::from_lengths` panics once the lengths add up to
more than 2 GiB. The other overflow panics in `arrow-buffer` are the
same story.

# What changes are included in this PR?

A small `OverflowError` and a `try_` variant for functions that panics
on overflow:

* `OffsetBuffer::{try_new_zeroed, try_from_lengths,
try_from_repeated_length}`
* `OffsetBufferBuilder::{try_push_length, try_finish,
try_finish_cloned}`
* `NullBuffer::try_expand`

The panicking versions delegate to the fallible ones and re-panic with
the error's `Display`, so their panic messages are unchanged.

`try_push_length` leaves the builder unchanged when it fails.

# Are these changes tested?

Yes, new tests for the error paths. The existing `should_panic` tests
are untouched and still pass, which is what pins the panic messages.

# Are there any user-facing changes?

New public API only, no breaking changes.

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 25, 2026
…ors (apache#10736)

# Which issue does this PR close?

* Part of apache#10553
* Follows the `MutableBufferError` precedent from
apache#10317

# Rationale for this change

`OffsetBuffer::<i32>::from_lengths` panics once the lengths add up to
more than 2 GiB. The other overflow panics in `arrow-buffer` are the
same story.

# What changes are included in this PR?

A small `OverflowError` and a `try_` variant for functions that panics
on overflow:

* `OffsetBuffer::{try_new_zeroed, try_from_lengths,
try_from_repeated_length}`
* `OffsetBufferBuilder::{try_push_length, try_finish,
try_finish_cloned}`
* `NullBuffer::try_expand`

The panicking versions delegate to the fallible ones and re-panic with
the error's `Display`, so their panic messages are unchanged.

`try_push_length` leaves the builder unchanged when it fails.

# Are these changes tested?

Yes, new tests for the error paths. The existing `should_panic` tests
are untouched and still pass, which is what pins the panic messages.

# Are there any user-facing changes?

New public API only, no breaking changes.

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate arrow-array arrow-buffer enhancement New feature/addition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants