Skip to content

Add OffsetBuffer::first/last to always return offset & remove some unwraps - #10759

Merged
Jefffrey merged 14 commits into
apache:mainfrom
emilk:emilk/panics-refactor
Aug 29, 2026
Merged

Jefffrey merged 14 commits into
apache:mainfrom
emilk:emilk/panics-refactor

Conversation

@emilk

@emilk emilk commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

This is one of four PRs splitting up the clippy::missing_panics_doc work.

  1. Return errors instead of panicking in fallible functions #10755 - return errors from fallible functions
  2. Add OffsetBuffer::first/last to always return offset & remove some unwraps #10759 - remove unreachable panics
  3. Document the panics of public functions #10760 - document the panics that genuinely remain
  4. Mark unreachable panics with #[expect(clippy::missing_panics_doc)] #10761 - #[expect] the unreachable ones, so the lint can be turned on

What changes are included in this PR?

Replaces unwrap/expect calls that cannot fail with non-panicking equivalents.

Are these changes tested?

Covered by the existing tests. Every change here is a rewrite of code whose
panic was unreachable, so there is no new behavior to test.

Are there any user-facing changes?

Yes - OffsetBuffer::last/first no longer returns Option

Comment thread arrow-buffer/src/pool.rs
Comment on lines +150 to +159
/// Lock a memory reservation, recovering from a poisoned lock.
///
/// A poisoned lock only means that some other thread panicked. The reservation it
/// guards is plain size accounting, so there is no broken invariant to protect, and
/// recovering it is always preferable to panicking.
pub(crate) fn lock_reservation(
reservation: &Mutex<Option<Box<dyn MemoryReservation>>>,
) -> MutexGuard<'_, Option<Box<dyn MemoryReservation>>> {
reservation.lock().unwrap_or_else(PoisonError::into_inner)
}

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.

Maybe a bit weird. Alternatives to consider:

  • Do a single unwrap() here and just document crate-wide that poisoning causes panics
  • Use parking_lots non-poisoning mutexes

for key in names {
let (name, value) = self.infos.get_key_value(&key).unwrap();
// `infos` is a `BTreeMap`, so it iterates in sorted order already
for (name, value) in &self.infos {

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.

nice little speed-up

emilk and others added 7 commits August 19, 2026 21:20
Replace `unwrap`/`expect` calls that can never fail with non-panicking
equivalents:

* `from_usize(0).unwrap()` -> `usize_as(0)`
* `NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()` -> `NaiveDate::default()`
* `NaiveTime::from_hms_opt(0, 0, 0).unwrap()` -> `NaiveTime::MIN`
* `x[0..4].try_into().unwrap()` -> `[x[0], x[1], x[2], x[3]]`
* `i256::from(10).checked_pow(64).unwrap()` -> square of a `10^32` literal
* `last_mut().unwrap()` -> `if let ... && let Some(last)`
* `NullBufferBuilder::materialize_if_needed` now returns the builder
* `partial_cmp(..).unwrap()` -> `as_usize().cmp(..)` in `RunEndBuffer`
* let-else and `is_some_and` in `regexp_match` and `is_ascii`
* `SqlInfoDataBuilder::build` sorts the entries instead of looking them up
* `IpcWriteOptions::try_new` matches on the alignment

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A poisoned lock only means that some other thread panicked. The reservation it
guards is plain size accounting, so there is no broken invariant to protect,
and `MutableBuffer::truncate`, `try_resize`, `clear` and friends have no reason
to panic for it.

Adds `pool::lock_reservation`, so the recovery is written once instead of at
each of the ten lock sites.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`infos` is a `BTreeMap`, so it already iterates in key order. Collecting the
entries into a `Vec` and sorting them was redundant before this branch too;
this drops the copy and the sort instead of rewriting them.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`ordered_indices` is built from `0..indices_len` and the `indices_len == 0`
case already returns above, so the `last()` fallback could never run. Index
the last entry directly and say why that is in range.

Also spells out the two other invariants the same function and
`GenericByteArray::is_ascii` rely on.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Rewriting `x[..4].try_into().unwrap()` as `[x[0], x[1], x[2], x[3]]` keeps the
same panic, it just hides it from `missing_panics_doc`. Bring back the reason
each read is in bounds, and use `as_chunks` in the Bloom filter so the word
length is a type-level guarantee rather than a comment.

`read_footer_length` and `FooterTail::try_new` read from fixed-size arrays, so
the compiler already rejects an out-of-bounds index there.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`truncate` and `append_n` only touch the last byte when the bit length is not
a multiple of eight, and a non-zero remainder already implies at least one
byte. The `last_mut()` fallbacks could never run, so they hid the invariant
rather than upholding it. Index the last byte and say why it is there.

Note that these were never reachable panics either, so `truncate` and
`append_n` behave exactly as before.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`try_schema_from_ipc_buffer` read its length prefix through
`try_into().unwrap()`. Read it with `split_first_chunk::<4>()` instead, which
makes the length a type-level guarantee and folds in the manual `len < 8`
check.

`read_record_batch` unwrapped the array it had just decoded, to reuse it for a
duplicate projection entry. Hold the decoded array in the `Option` it already
had instead.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
emilk and others added 2 commits August 24, 2026 12:02
* Use `OffsetSize::zero()` in `GenericListViewBuilder::append_null`
* Replace the computed `10^32`/`10^64` in `i256::checked_ilog10` with
  `POW10_32`/`POW10_64` constants, covered by a new test
* Derive the view prefix with `first_chunk`, so the non-inlined path in
  `GenericByteViewBuilder::try_append_value` no longer indexes at all
* Go back to `last_mut()` in `BooleanBufferBuilder`, guarded with `if let`:
  no `unwrap`, and no indexing either
* Drop a redundant comment in `Sbbf::new`

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
An `OffsetBuffer` is never empty: every constructor either asserts it
(`new`), documents it as a safety requirement (`new_unchecked`), or
produces at least one offset. The `first`/`last` reached through
`Deref<Target = [O]>` still returned an `Option`, so callers had to
`unwrap` an invariant the type already guarantees.

These two inherent methods return the offset directly and shadow the
slice ones, which removes ten `unwrap`s across the workspace.

BREAKING: `OffsetBuffer::first`/`last` now return `O` rather than
`Option<&O>`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions github-actions Bot added arrow-select arrow-row arrow-row crate labels Aug 24, 2026
emilk and others added 2 commits August 24, 2026 12:12
`unwrap_or_default` would paper over such a bug with a plausible-looking
zero offset. `expect` catches it instead.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Covers the append paths this PR touched, plus `append_non_null` and
`materialize` as controls for the paths it did not.

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

emilk commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Is this good as is, or should I split it into smaller PRs?

@Jefffrey

Copy link
Copy Markdown
Contributor

ill take a look at this tomorrow

can keep it as one PR 👍

@Jefffrey Jefffrey added the api-change Breaking API change, needs to wait for a major version release label Aug 28, 2026
Comment on lines +191 to +196
pub fn first(&self) -> O {
self.0
.first()
.copied()
.expect("An `OffsetBuffer` is never empty")
}

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.

this is nice to have, though i wonder if itll cause churn downstream; a softer alternative is naming it first_offset() but does mean we can't really get people to move to the new api (dont think we can deprecate first() since it comes from a deref 🤔)

fyi @alamb

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 am not sure why it would cause churn downstream 🤔 first_offset is more explicit and maybe less likely to be confused with an iterator name

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.

Oh, I see now that this changed the signature

this is nice to have, though i wonder if itll cause churn downstream; a softer alternative is naming it first_offset() but does mean we can't really get people to move to the new api (dont think we can deprecate first() since it comes from a deref 🤔)

I like this idea now -- rename this to first_offset() and deprecate first() (and the same for last)

This (arrow 60) already has a bunch of breaking changes. It might be nice to minimize the number of additional changes needed

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.

Can't deprecate first() - it is part of &[T] which this derefs into.

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.

I think removing .unwrap() from users code is the good type of churn

@Jefffrey Jefffrey changed the title Remove some unwraps Add OffsetBuffer::first/last to always return offset & remove some unwraps Aug 28, 2026

@alamb alamb 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.

Thank you @emilk @Rich-T-kid and @Jefffrey

/// assert_eq!(OffsetBuffer::<i32>::new_empty().last(), 0);
/// ```
#[inline]
pub fn last(&self) -> O {

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 this would be confusing to people / get confused with std::iterator::last 🤔

https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.last

However, since the OffsetBuffer isn't an interator, this seems good to me

Comment on lines +191 to +196
pub fn first(&self) -> O {
self.0
.first()
.copied()
.expect("An `OffsetBuffer` is never empty")
}

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 am not sure why it would cause churn downstream 🤔 first_offset is more explicit and maybe less likely to be confused with an iterator name

@alamb
alamb requested a review from Rich-T-kid August 28, 2026 11:12

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

this PR looks very nice. thank you @emilk

Comment thread arrow-buffer/benches/null_builder.rs Resolved
Comment thread arrow-buffer/src/builder/null.rs Resolved
This reverts commit 0eeb3ba.

Co-Authored-By: Claude Opus 5 <[email protected]>
@Jefffrey
Jefffrey merged commit 684d64a into apache:main Aug 29, 2026
36 checks passed
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks all

@Jefffrey

Jefffrey commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

after clicking merge and trying this locally, realized that the new first() -> O can only be used when you import OffsetBuffer otherwise itll still use the deref first() -> Option<O> 🤔

so it might be confusing that different methods are used based on import, so we might not have the benefit of getting rid of all the old first() -> Option<O> calls if the compiler doesnt have access to the new first() -> O in scope

edit: actually might be my rust analyzer playing up a bit

@alamb

alamb commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Thanks @emilk and @Jefffrey and @Rich-T-kid

Jefffrey added a commit that referenced this pull request Sep 1, 2026
# Which issue does this PR close?

- Part of #10553

# Rationale for this change

While clearing `clippy::missing_panics_doc`, a pattern turned up:
functions that
already return `Result`, but report some failures by panicking. Several
are
reachable from untrusted input, so the panic is a denial of service
rather than a
bug report.

Documenting those panics would normalise them, so this returns errors
instead.

This is one of four PRs splitting up the work.

1. **this PR** - return errors from fallible functions
2. #10759 - remove unreachable panics
3. #10760 - document the panics that genuinely remain
4. #10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?

Functions that could report the failure and did not.

# Are these changes tested?

Covered by the existing tests, plus new tests for the `b64_decode` and
`ArrayData::validate_values` error paths.

# Are there any user-facing changes?

No API changes. Some calls that used to panic now return an `Err`, which
is the
point of the PR.

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Jeffrey Vo <[email protected]>
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 2, 2026
…e `unwrap`s (apache#10759)

# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

This is one of four PRs splitting up the `clippy::missing_panics_doc`
work.

1. apache#10755 - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?
Replaces `unwrap`/`expect` calls that cannot fail with non-panicking
equivalents.

# Are these changes tested?

Covered by the existing tests. Every change here is a rewrite of code
whose
panic was unreachable, so there is no new behavior to test.

# Are there any user-facing changes?
Yes - `OffsetBuffer::last/first` no longer returns `Option`

---------

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 18, 2026
…e `unwrap`s (apache#10759)

# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

This is one of four PRs splitting up the `clippy::missing_panics_doc`
work.

1. apache#10755 - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?
Replaces `unwrap`/`expect` calls that cannot fail with non-panicking
equivalents.

# Are these changes tested?

Covered by the existing tests. Every change here is a rewrite of code
whose
panic was unreachable, so there is no new behavior to test.

# Are there any user-facing changes?
Yes - `OffsetBuffer::last/first` no longer returns `Option`

---------

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 18, 2026
# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

While clearing `clippy::missing_panics_doc`, a pattern turned up:
functions that
already return `Result`, but report some failures by panicking. Several
are
reachable from untrusted input, so the panic is a denial of service
rather than a
bug report.

Documenting those panics would normalise them, so this returns errors
instead.

This is one of four PRs splitting up the work.

1. **this PR** - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?

Functions that could report the failure and did not.

# Are these changes tested?

Covered by the existing tests, plus new tests for the `b64_decode` and
`ArrayData::validate_values` error paths.

# Are there any user-facing changes?

No API changes. Some calls that used to panic now return an `Err`, which
is the
point of the PR.

---------

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

# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

This is one of four PRs splitting up the `clippy::missing_panics_doc`
work.

1. apache#10755 - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?
Replaces `unwrap`/`expect` calls that cannot fail with non-panicking
equivalents.

# Are these changes tested?

Covered by the existing tests. Every change here is a rewrite of code
whose
panic was unreachable, so there is no new behavior to test.

# Are there any user-facing changes?
Yes - `OffsetBuffer::last/first` no longer returns `Option`

---------

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
# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

While clearing `clippy::missing_panics_doc`, a pattern turned up:
functions that
already return `Result`, but report some failures by panicking. Several
are
reachable from untrusted input, so the panic is a denial of service
rather than a
bug report.

Documenting those panics would normalise them, so this returns errors
instead.

This is one of four PRs splitting up the work.

1. **this PR** - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?

Functions that could report the failure and did not.

# Are these changes tested?

Covered by the existing tests, plus new tests for the `b64_decode` and
`ArrayData::validate_values` error paths.

# Are there any user-facing changes?

No API changes. Some calls that used to panic now return an `Err`, which
is the
point of the PR.

---------

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

# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

This is one of four PRs splitting up the `clippy::missing_panics_doc`
work.

1. apache#10755 - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?
Replaces `unwrap`/`expect` calls that cannot fail with non-panicking
equivalents.

# Are these changes tested?

Covered by the existing tests. Every change here is a rewrite of code
whose
panic was unreachable, so there is no new behavior to test.

# Are there any user-facing changes?
Yes - `OffsetBuffer::last/first` no longer returns `Option`

---------

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
# Which issue does this PR close?

- Part of apache#10553

# Rationale for this change

While clearing `clippy::missing_panics_doc`, a pattern turned up:
functions that
already return `Result`, but report some failures by panicking. Several
are
reachable from untrusted input, so the panic is a denial of service
rather than a
bug report.

Documenting those panics would normalise them, so this returns errors
instead.

This is one of four PRs splitting up the work.

1. **this PR** - return errors from fallible functions
2. apache#10759 - remove unreachable panics
3. apache#10760 - document the panics that genuinely remain
4. apache#10761 - `#[expect]` the unreachable ones, so the lint can be turned
on

# What changes are included in this PR?

Functions that could report the failure and did not.

# Are these changes tested?

Covered by the existing tests, plus new tests for the `b64_decode` and
`ArrayData::validate_values` error paths.

# Are there any user-facing changes?

No API changes. Some calls that used to panic now return an `Err`, which
is the
point of the PR.

---------

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

Labels

api-change Breaking API change, needs to wait for a major version release arrow Changes to the arrow crate arrow-array arrow-buffer arrow-cast arrow-flight arrow-flight crate arrow-ipc arrow-ipc crate arrow-row arrow-row crate arrow-select arrow-string parquet parquet crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants