Skip to content

Commit e2ccc63

Browse files
committed
lint
1 parent 80d7558 commit e2ccc63

2 files changed

Lines changed: 32 additions & 123 deletions

File tree

datafusion/functions-nested/benches/array_resize.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use arrow::array::{ArrayRef, Int64Array, ListArray};
1919
use arrow::buffer::OffsetBuffer;
2020
use arrow::datatypes::{DataType, Field};
2121
use criterion::{
22-
criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup,
23-
Criterion,
22+
BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::WallTime,
2423
};
2524
use datafusion_common::config::ConfigOptions;
2625
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl};
@@ -32,16 +31,14 @@ const NUM_ROWS: usize = 1_000;
3231

3332
fn criterion_benchmark(c: &mut Criterion) {
3433
let mut group = c.benchmark_group("array_resize_i64");
35-
let list_field: Arc<Field> =
36-
Field::new_list_field(DataType::Int64, true).into();
34+
let list_field: Arc<Field> = Field::new_list_field(DataType::Int64, true).into();
3735
let list_data_type = DataType::List(Arc::clone(&list_field));
3836
let arg_fields = vec![
3937
Field::new("array", list_data_type.clone(), true).into(),
4038
Field::new("size", DataType::Int64, false).into(),
4139
Field::new("value", DataType::Int64, true).into(),
4240
];
43-
let return_field: Arc<Field> =
44-
Field::new("result", list_data_type, true).into();
41+
let return_field: Arc<Field> = Field::new("result", list_data_type, true).into();
4542
let config_options = Arc::new(ConfigOptions::default());
4643
let two_arg_fields = arg_fields[..2].to_vec();
4744

@@ -164,9 +161,9 @@ fn distinct_fill_array() -> ArrayRef {
164161
}
165162

166163
fn mixed_size_array() -> ArrayRef {
167-
Arc::new(Int64Array::from_iter((0..NUM_ROWS).map(|i| {
168-
Some(if i % 2 == 0 { 200_i64 } else { 10_i64 })
169-
})))
164+
Arc::new(Int64Array::from_iter(
165+
(0..NUM_ROWS).map(|i| Some(if i % 2 == 0 { 200_i64 } else { 10_i64 })),
166+
))
170167
}
171168

172169
criterion_group!(benches, criterion_benchmark);

datafusion/functions-nested/src/resize.rs

Lines changed: 26 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
2020
use crate::utils::make_scalar_function;
2121
use arrow::array::{
22-
new_null_array, Array, ArrayRef, Capacities, GenericListArray, Int64Array,
23-
MutableArrayData, NullBufferBuilder, OffsetSizeTrait,
22+
Array, ArrayRef, Capacities, GenericListArray, Int64Array, MutableArrayData,
23+
NullBufferBuilder, OffsetSizeTrait, new_null_array,
2424
};
2525
use arrow::buffer::OffsetBuffer;
2626
use arrow::datatypes::DataType;
@@ -31,7 +31,7 @@ use arrow::datatypes::{
3131
};
3232
use datafusion_common::cast::{as_int64_array, as_large_list_array, as_list_array};
3333
use datafusion_common::utils::ListCoercion;
34-
use datafusion_common::{exec_err, internal_datafusion_err, Result, ScalarValue};
34+
use datafusion_common::{Result, ScalarValue, exec_err, internal_datafusion_err};
3535
use datafusion_expr::{
3636
ArrayFunctionArgument, ArrayFunctionSignature, ColumnarValue, Documentation,
3737
ScalarUDFImpl, Signature, TypeSignature, Volatility,
@@ -213,12 +213,9 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
213213
if array.is_null(row_index) {
214214
continue;
215215
}
216-
let target_count =
217-
count_array.value(row_index).to_usize().ok_or_else(|| {
218-
internal_datafusion_err!(
219-
"array_resize: failed to convert size to usize"
220-
)
221-
})?;
216+
let target_count = count_array.value(row_index).to_usize().ok_or_else(|| {
217+
internal_datafusion_err!("array_resize: failed to convert size to usize")
218+
})?;
222219
let current_len = (offset_window[1] - offset_window[0]).to_usize().unwrap();
223220
if target_count > current_len {
224221
let extra = target_count - current_len;
@@ -232,20 +229,20 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
232229
// use the same fill value.
233230
let is_uniform_fill = max_extra > 0
234231
&& match &default_element {
235-
None => true,
236-
Some(fill_array) => {
237-
let len = fill_array.len();
238-
let null_count = fill_array.logical_null_count();
239-
240-
len <= 1
241-
|| null_count == len
242-
|| (null_count == 0 && {
243-
let first = fill_array.slice(0, 1);
244-
(1..len)
245-
.all(|i| fill_array.slice(i, 1).as_ref() == first.as_ref())
246-
})
247-
}
248-
};
232+
None => true,
233+
Some(fill_array) => {
234+
let len = fill_array.len();
235+
let null_count = fill_array.logical_null_count();
236+
237+
len <= 1
238+
|| null_count == len
239+
|| (null_count == 0 && {
240+
let first = fill_array.slice(0, 1);
241+
(1..len)
242+
.all(|i| fill_array.slice(i, 1).as_ref() == first.as_ref())
243+
})
244+
}
245+
};
249246

250247
// Fast path: at least one row needs to grow and all rows share
251248
// the same fill value.
@@ -255,15 +252,12 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
255252
Some(fill_array) if fill_array.logical_null_count() == fill_array.len() => {
256253
ScalarValue::try_from(&data_type)?
257254
}
258-
Some(fill_array) => {
259-
ScalarValue::try_from_array(fill_array.as_ref(), 0)?
260-
}
255+
Some(fill_array) => ScalarValue::try_from_array(fill_array.as_ref(), 0)?,
261256
};
262257
let default_element = fill_scalar.to_array_of_size(max_extra)?;
263258
let default_value_data = default_element.to_data();
264259

265-
let capacity =
266-
Capacities::Array(original_data.len() + default_value_data.len());
260+
let capacity = Capacities::Array(original_data.len() + default_value_data.len());
267261
let mut offsets = vec![O::usize_as(0)];
268262
let mut mutable = MutableArrayData::with_capacities(
269263
vec![&original_data, &default_value_data],
@@ -282,29 +276,18 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
282276
null_builder.append_non_null();
283277

284278
let count = count_array.value(row_index).to_usize().ok_or_else(|| {
285-
internal_datafusion_err!(
286-
"array_resize: failed to convert size to usize"
287-
)
279+
internal_datafusion_err!("array_resize: failed to convert size to usize")
288280
})?;
289281
let count = O::usize_as(count);
290282
let start = offset_window[0];
291283
if start + count > offset_window[1] {
292-
let extra_count =
293-
(start + count - offset_window[1]).to_usize().unwrap();
284+
let extra_count = (start + count - offset_window[1]).to_usize().unwrap();
294285
let end = offset_window[1];
295-
mutable.extend(
296-
0,
297-
(start).to_usize().unwrap(),
298-
(end).to_usize().unwrap(),
299-
);
286+
mutable.extend(0, (start).to_usize().unwrap(), (end).to_usize().unwrap());
300287
mutable.extend(1, 0, extra_count);
301288
} else {
302289
let end = start + count;
303-
mutable.extend(
304-
0,
305-
(start).to_usize().unwrap(),
306-
(end).to_usize().unwrap(),
307-
);
290+
mutable.extend(0, (start).to_usize().unwrap(), (end).to_usize().unwrap());
308291
};
309292
offsets.push(offsets[row_index] + count);
310293
}
@@ -381,74 +364,3 @@ fn general_list_resize<O: OffsetSizeTrait + TryInto<i64>>(
381364
null_builder.finish(),
382365
)?))
383366
}
384-
385-
#[cfg(test)]
386-
mod tests {
387-
use super::*;
388-
use arrow::array::{AsArray, ListArray};
389-
use arrow::datatypes::Int64Type;
390-
391-
fn list_values(array: &ListArray) -> Vec<Option<Vec<Option<i64>>>> {
392-
array
393-
.iter()
394-
.map(|row| {
395-
row.map(|values| {
396-
values
397-
.as_primitive::<Int64Type>()
398-
.iter()
399-
.collect::<Vec<Option<i64>>>()
400-
})
401-
})
402-
.collect()
403-
}
404-
405-
#[test]
406-
fn test_array_resize_preserves_row_fill_values() -> Result<()> {
407-
let list = ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
408-
Some(vec![Some(1)]),
409-
Some(vec![Some(2)]),
410-
]);
411-
let new_len = Int64Array::from(vec![3, 2]);
412-
let fill = Int64Array::from(vec![9, 8]);
413-
414-
let args: Vec<ArrayRef> = vec![
415-
Arc::new(list),
416-
Arc::new(new_len),
417-
Arc::new(fill),
418-
];
419-
let result = array_resize_inner(&args)?;
420-
let result = result.as_list::<i32>();
421-
422-
let expected = vec![
423-
Some(vec![Some(1), Some(9), Some(9)]),
424-
Some(vec![Some(2), Some(8)]),
425-
];
426-
assert_eq!(expected, list_values(result));
427-
Ok(())
428-
}
429-
430-
#[test]
431-
fn test_array_resize_uniform_fill_fast_path() -> Result<()> {
432-
let list = ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
433-
Some(vec![Some(1)]),
434-
Some(vec![Some(2)]),
435-
]);
436-
let new_len = Int64Array::from(vec![3, 2]);
437-
let fill = Int64Array::from(vec![9, 9]);
438-
439-
let args: Vec<ArrayRef> = vec![
440-
Arc::new(list),
441-
Arc::new(new_len),
442-
Arc::new(fill),
443-
];
444-
let result = array_resize_inner(&args)?;
445-
let result = result.as_list::<i32>();
446-
447-
let expected = vec![
448-
Some(vec![Some(1), Some(9), Some(9)]),
449-
Some(vec![Some(2), Some(9)]),
450-
];
451-
assert_eq!(expected, list_values(result));
452-
Ok(())
453-
}
454-
}

0 commit comments

Comments
 (0)