Skip to content

Commit 13a6e30

Browse files
authored
chore: use new OffsetBuffer::subtract helper (#23424)
## Which issue does this PR close? N/A ## Rationale for this change Replace manually written code with the newly added helper I added to arrow-rs in: - apache/arrow-rs#10120 ## What changes are included in this PR? use `OffsetBuffer::subtract` ## Are these changes tested? Existing tests ## Are there any user-facing changes? No
1 parent f87e817 commit 13a6e30

4 files changed

Lines changed: 12 additions & 38 deletions

File tree

datafusion/common/src/utils/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,16 +1236,7 @@ pub fn adjust_offsets_for_slice<O: OffsetSizeTrait>(
12361236
) -> OffsetBuffer<O> {
12371237
let offsets = list.offsets();
12381238

1239-
if let (Some(first), Some(last)) = (offsets.first(), offsets.last())
1240-
&& (!first.is_zero() || last.as_usize() != list.values().len())
1241-
{
1242-
let offsets = offsets.iter().map(|offset| *offset - *first).collect();
1243-
1244-
//todo: use unsafe Offset::new_unchecked?
1245-
return OffsetBuffer::new(offsets);
1246-
}
1247-
1248-
offsets.clone()
1239+
offsets.clone().subtract(offsets[0])
12491240
}
12501241

12511242
/// For lists and large lists, truncates the sublist of null values

datafusion/functions-nested/src/sort.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,7 @@ fn take_by_indices<OffsetSize: OffsetSizeTrait>(
471471
fn rebase_offsets<OffsetSize: OffsetSizeTrait>(
472472
offsets: &OffsetBuffer<OffsetSize>,
473473
) -> OffsetBuffer<OffsetSize> {
474-
if offsets[0].as_usize() == 0 {
475-
offsets.clone()
476-
} else {
477-
let rebased: Vec<OffsetSize> = offsets.iter().map(|o| *o - offsets[0]).collect();
478-
OffsetBuffer::new(rebased.into())
479-
}
474+
offsets.clone().subtract(offsets[0])
480475
}
481476

482477
fn order_desc(modifier: &str) -> Result<bool> {

datafusion/functions/src/string/common.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use arrow::array::{
2727
Array, ArrayRef, AsArray, GenericStringArray, NullBufferBuilder, OffsetSizeTrait,
2828
StringViewArray, new_null_array,
2929
};
30-
use arrow::buffer::{Buffer, OffsetBuffer, ScalarBuffer};
30+
use arrow::buffer::{Buffer, ScalarBuffer};
3131
use arrow::datatypes::DataType;
3232
use datafusion_common::Result;
3333
use datafusion_common::cast::{as_generic_string_array, as_string_view_array};
@@ -636,15 +636,10 @@ fn case_conversion_ascii_array<O: OffsetSizeTrait>(
636636
let values = Buffer::from_vec(converted);
637637

638638
// Shift offsets from `start`-based to 0-based so they index into `values`.
639-
let offsets = if start == 0 {
640-
string_array.offsets().clone()
641-
} else {
642-
let s = O::usize_as(start);
643-
let rebased: Vec<O> = value_offsets.iter().map(|&o| o - s).collect();
644-
// SAFETY: subtracting a constant from monotonic offsets preserves
645-
// monotonicity, and `start` is the minimum offset, so no underflow.
646-
unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(rebased)) }
647-
};
639+
let offsets = string_array
640+
.offsets()
641+
.clone()
642+
.subtract(string_array.offsets()[0]);
648643

649644
let nulls = string_array.nulls().cloned();
650645
// SAFETY: offsets are monotonic and in-bounds for `values`; nulls

datafusion/functions/src/unicode/initcap.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use std::sync::Arc;
1919

2020
use arrow::array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait};
21-
use arrow::buffer::{Buffer, OffsetBuffer};
21+
use arrow::buffer::Buffer;
2222
use arrow::datatypes::DataType;
2323

2424
use crate::strings::{GenericStringArrayBuilder, StringViewArrayBuilder};
@@ -217,17 +217,10 @@ fn initcap_ascii_array<T: OffsetSizeTrait>(
217217
}
218218

219219
let values = Buffer::from_vec(out);
220-
let out_offsets = if first_offset == 0 {
221-
offsets.clone()
222-
} else {
223-
// For sliced arrays, we need to rebase the offsets to reflect that the
224-
// output only contains the bytes in the visible slice.
225-
let rebased_offsets = offsets
226-
.iter()
227-
.map(|offset| T::usize_as(offset.as_usize() - first_offset))
228-
.collect::<Vec<_>>();
229-
OffsetBuffer::<T>::new(rebased_offsets.into())
230-
};
220+
221+
// Rebase offsets for sliced arrays to reflect that the
222+
// output only contains the bytes in the visible slice.
223+
let out_offsets = offsets.clone().subtract(offsets[0]);
231224

232225
// SAFETY: ASCII case conversion preserves byte length, so the original
233226
// string boundaries are preserved. `out_offsets` is either identical to

0 commit comments

Comments
 (0)