Repository navigation
Add OffsetBuffer::first/last to always return offset & remove some unwraps - #10759
Conversation
| /// 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) | ||
| } |
There was a problem hiding this comment.
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 { |
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]>
aaeb570 to
b59a5d8
Compare
* 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]>
`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]>
|
Is this good as is, or should I split it into smaller PRs? |
|
ill take a look at this tomorrow can keep it as one PR 👍 |
| pub fn first(&self) -> O { | ||
| self.0 | ||
| .first() | ||
| .copied() | ||
| .expect("An `OffsetBuffer` is never empty") | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Can't deprecate first() - it is part of &[T] which this derefs into.
There was a problem hiding this comment.
I think removing .unwrap() from users code is the good type of churn
unwrapsOffsetBuffer::first/last to always return offset & remove some unwraps
alamb
left a comment
There was a problem hiding this comment.
Thank you @emilk @Rich-T-kid and @Jefffrey
| /// assert_eq!(OffsetBuffer::<i32>::new_empty().last(), 0); | ||
| /// ``` | ||
| #[inline] | ||
| pub fn last(&self) -> O { |
There was a problem hiding this comment.
I wonder if this would be confusing to people / get confused with std::iterator::last 🤔
However, since the OffsetBuffer isn't an interator, this seems good to me
| pub fn first(&self) -> O { | ||
| self.0 | ||
| .first() | ||
| .copied() | ||
| .expect("An `OffsetBuffer` is never empty") | ||
| } |
There was a problem hiding this comment.
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
Rich-T-kid
left a comment
There was a problem hiding this comment.
this PR looks very nice. thank you @emilk
This reverts commit 0eeb3ba. Co-Authored-By: Claude Opus 5 <[email protected]>
|
thanks all |
|
edit: actually might be my rust analyzer playing up a bit |
|
Thanks @emilk and @Jefffrey and @Rich-T-kid |
# 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]>
…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]>
…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]>
# 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]>
…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]>
# 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]>
…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]>
# 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]>
Which issue does this PR close?
Rationale for this change
This is one of four PRs splitting up the
clippy::missing_panics_docwork.OffsetBuffer::first/lastto always return offset & remove someunwraps #10759 - remove unreachable panics#[expect(clippy::missing_panics_doc)]#10761 -#[expect]the unreachable ones, so the lint can be turned onWhat changes are included in this PR?
Replaces
unwrap/expectcalls 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/firstno longer returnsOption