Skip to content

Commit 9238779

Browse files
authored
minor: refactor array reverse internals (#18445)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> N/A ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> When reviewing #18424 I noticed some refactoring that could be applied to existing array reverse implementation. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> See my comments for the refactors & justifications. Existing tests. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> No. <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent efcc216 commit 9238779

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

datafusion/functions-nested/src/reverse.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use datafusion_expr::{
3333
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
3434
};
3535
use datafusion_macros::user_doc;
36+
use itertools::Itertools;
3637
use std::any::Any;
3738
use std::sync::Arc;
3839

@@ -137,49 +138,40 @@ pub fn array_reverse_inner(arg: &[ArrayRef]) -> Result<ArrayRef> {
137138
}
138139
}
139140

140-
fn general_array_reverse<O: OffsetSizeTrait + TryFrom<i64>>(
141+
fn general_array_reverse<O: OffsetSizeTrait>(
141142
array: &GenericListArray<O>,
142143
field: &FieldRef,
143144
) -> Result<ArrayRef> {
144145
let values = array.values();
145146
let original_data = values.to_data();
146147
let capacity = Capacities::Array(original_data.len());
147148
let mut offsets = vec![O::usize_as(0)];
148-
let mut nulls = vec![];
149149
let mut mutable =
150150
MutableArrayData::with_capacities(vec![&original_data], false, capacity);
151151

152-
for (row_index, offset_window) in array.offsets().windows(2).enumerate() {
152+
for (row_index, (&start, &end)) in array.offsets().iter().tuple_windows().enumerate()
153+
{
153154
// skip the null value
154155
if array.is_null(row_index) {
155-
nulls.push(false);
156-
offsets.push(offsets[row_index] + O::one());
157-
mutable.extend(0, 0, 1);
156+
offsets.push(offsets[row_index]);
158157
continue;
159-
} else {
160-
nulls.push(true);
161158
}
162159

163-
let start = offset_window[0];
164-
let end = offset_window[1];
165-
166160
let mut index = end - O::one();
167-
let mut cnt = 0;
168-
169161
while index >= start {
170162
mutable.extend(0, index.to_usize().unwrap(), index.to_usize().unwrap() + 1);
171163
index = index - O::one();
172-
cnt += 1;
173164
}
174-
offsets.push(offsets[row_index] + O::usize_as(cnt));
165+
let size = end - start;
166+
offsets.push(offsets[row_index] + size);
175167
}
176168

177169
let data = mutable.freeze();
178170
Ok(Arc::new(GenericListArray::<O>::try_new(
179171
Arc::clone(field),
180172
OffsetBuffer::<O>::new(offsets.into()),
181173
arrow::array::make_array(data),
182-
Some(nulls.into()),
174+
array.nulls().cloned(),
183175
)?))
184176
}
185177

@@ -190,19 +182,15 @@ fn fixed_size_array_reverse(
190182
let values = array.values();
191183
let original_data = values.to_data();
192184
let capacity = Capacities::Array(original_data.len());
193-
let mut nulls = vec![];
194185
let mut mutable =
195186
MutableArrayData::with_capacities(vec![&original_data], false, capacity);
196187
let value_length = array.value_length() as usize;
197188

198189
for row_index in 0..array.len() {
199190
// skip the null value
200191
if array.is_null(row_index) {
201-
nulls.push(false);
202192
mutable.extend(0, 0, value_length);
203193
continue;
204-
} else {
205-
nulls.push(true);
206194
}
207195
let start = row_index * value_length;
208196
let end = start + value_length;
@@ -216,6 +204,6 @@ fn fixed_size_array_reverse(
216204
Arc::clone(field),
217205
array.value_length(),
218206
arrow::array::make_array(data),
219-
Some(nulls.into()),
207+
array.nulls().cloned(),
220208
)?))
221209
}

0 commit comments

Comments
 (0)