Skip to content

Commit 1e1cca2

Browse files
committed
Replace panicking extend calls with fallible try_extend
Remove deprecated documentation from extend methods and update callers to use try_extend for proper error handling instead of panicking on overflow. Also optimize struct field name computation to avoid repeated lookups.
1 parent 579e1d7 commit 1e1cca2

4 files changed

Lines changed: 13 additions & 28 deletions

File tree

‎arrow-data/src/transform/mod.rs‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,6 @@ impl<'a> MutableArrayData<'a> {
760760

761761
/// Extends the in progress array with a region of the input arrays.
762762
///
763-
/// # Deprecated
764-
/// Use [`try_extend`](Self::try_extend) instead, which returns an [`ArrowError`] on overflow
765-
/// rather than panicking.
766-
///
767763
/// # Panic
768764
/// This function panics if there is an invalid index,
769765
/// i.e. `index` >= the number of source arrays,
@@ -799,10 +795,6 @@ impl<'a> MutableArrayData<'a> {
799795

800796
/// Extends the in progress array with null elements, ignoring the input arrays.
801797
///
802-
/// # Deprecated
803-
/// Use [`try_extend_nulls`](Self::try_extend_nulls) instead, which returns an [`ArrowError`]
804-
/// on overflow rather than panicking.
805-
///
806798
/// # Panics
807799
///
808800
/// Panics if [`MutableArrayData`] not created with `use_nulls` or nullable source arrays,

‎arrow-data/src/transform/structure.rs‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,33 @@ use arrow_schema::{ArrowError, DataType};
2222
pub(super) fn build_extend(_: &ArrayData) -> Extend<'_> {
2323
Box::new(
2424
move |mutable: &mut _MutableArrayData, index: usize, start: usize, len: usize| {
25-
// Collect field names before the mutable borrow of child_data.
26-
let field_names = struct_field_names(&mutable.data_type);
2725
for (col_idx, child) in mutable.child_data.iter_mut().enumerate() {
2826
child
2927
.try_extend(index, start, start + len)
30-
.map_err(|e| wrap_column_error(e, col_idx, &field_names))?;
28+
.map_err(|e| wrap_column_error(e, col_idx, &mutable.data_type))?
3129
}
3230
Ok(())
3331
},
3432
)
3533
}
3634

3735
pub(super) fn extend_nulls(mutable: &mut _MutableArrayData, len: usize) -> Result<(), ArrowError> {
38-
let field_names = struct_field_names(&mutable.data_type);
3936
for (col_idx, child) in mutable.child_data.iter_mut().enumerate() {
4037
child
4138
.try_extend_nulls(len)
42-
.map_err(|e| wrap_column_error(e, col_idx, &field_names))?;
39+
.map_err(|e| wrap_column_error(e, col_idx, &mutable.data_type))?;
4340
}
4441
Ok(())
4542
}
4643

47-
fn struct_field_names(data_type: &DataType) -> Vec<String> {
48-
if let DataType::Struct(fields) = data_type {
49-
fields.iter().map(|f| f.name().to_string()).collect()
44+
fn wrap_column_error(e: ArrowError, col_idx: usize, data_type: &DataType) -> ArrowError {
45+
let name_ctx = if let DataType::Struct(fields) = data_type {
46+
fields
47+
.get(col_idx)
48+
.map(|f| format!(" (\"{}\")", f.name()))
49+
.unwrap_or_default()
5050
} else {
51-
vec![]
52-
}
53-
}
54-
55-
fn wrap_column_error(e: ArrowError, col_idx: usize, field_names: &[String]) -> ArrowError {
56-
let name_ctx = field_names
57-
.get(col_idx)
58-
.map(|n| format!(" (\"{n}\")"))
59-
.unwrap_or_default();
51+
String::new()
52+
};
6053
ArrowError::InvalidArgumentError(format!("struct column {col_idx}{name_ctx} failed: {e}"))
6154
}

‎arrow-select/src/interleave.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ fn interleave_list_view_copy<O: OffsetSizeTrait>(
633633
let start = list.offsets()[row_idx].as_usize();
634634
let size = list.sizes()[row_idx].as_usize();
635635
if size > 0 {
636-
mutable_child.extend(array_idx, start, start + size);
636+
mutable_child.try_extend(array_idx, start, start + size)?;
637637
}
638638
}
639639

‎arrow-select/src/take.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ where
671671
let ix = index.as_usize();
672672
let start = list_offsets[ix].as_usize();
673673
let end = list_offsets[ix + 1].as_usize();
674-
array_data.extend(0, start, end);
674+
array_data.try_extend(0, start, end)?;
675675
new_offsets.push(OffsetType::Native::from_usize(array_data.len()).unwrap());
676676
}
677677
}
@@ -690,7 +690,7 @@ where
690690
let ix = unsafe { indices.value_unchecked(i) }.as_usize();
691691
let start = list_offsets[ix].as_usize();
692692
let end = list_offsets[ix + 1].as_usize();
693-
array_data.extend(0, start, end);
693+
array_data.try_extend(0, start, end)?;
694694
new_offsets.push(OffsetType::Native::from_usize(array_data.len()).unwrap());
695695
last_filled = i + 1;
696696
}

0 commit comments

Comments
 (0)