Skip to content

Commit 049ca15

Browse files
authored
Create ArrayScalarBuilder for creating single element List arrays (#13623)
* Create `ArrayScalarBuilder` for creating single element List arrays * tweak * rename SingleRowArrayBuilder --> SingleRowListArrayBuilder
1 parent 143ef97 commit 049ca15

File tree

8 files changed

+218
-123
lines changed

8 files changed

+218
-123
lines changed

datafusion/common/src/scalar/mod.rs

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ use crate::cast::{
3939
};
4040
use crate::error::{DataFusionError, Result, _exec_err, _internal_err, _not_impl_err};
4141
use crate::hash_utils::create_hashes;
42-
use crate::utils::{
43-
array_into_fixed_size_list_array_with_field_name, array_into_large_list_array,
44-
array_into_large_list_array_with_field_name, array_into_list_array,
45-
array_into_list_array_with_field_name,
46-
};
42+
use crate::utils::SingleRowListArrayBuilder;
4743
use arrow::compute::kernels::numeric::*;
4844
use arrow::util::display::{array_value_to_string, ArrayFormatter, FormatOptions};
4945
use arrow::{
@@ -2092,7 +2088,11 @@ impl ScalarValue {
20922088
} else {
20932089
Self::iter_to_array(values.iter().cloned()).unwrap()
20942090
};
2095-
Arc::new(array_into_list_array(values, nullable))
2091+
Arc::new(
2092+
SingleRowListArrayBuilder::new(values)
2093+
.with_nullable(nullable)
2094+
.build_list_array(),
2095+
)
20962096
}
20972097

20982098
/// Same as [`ScalarValue::new_list`] but with nullable set to true.
@@ -2148,7 +2148,11 @@ impl ScalarValue {
21482148
} else {
21492149
Self::iter_to_array(values).unwrap()
21502150
};
2151-
Arc::new(array_into_list_array(values, nullable))
2151+
Arc::new(
2152+
SingleRowListArrayBuilder::new(values)
2153+
.with_nullable(nullable)
2154+
.build_list_array(),
2155+
)
21522156
}
21532157

21542158
/// Converts `Vec<ScalarValue>` where each element has type corresponding to
@@ -2185,7 +2189,7 @@ impl ScalarValue {
21852189
} else {
21862190
Self::iter_to_array(values.iter().cloned()).unwrap()
21872191
};
2188-
Arc::new(array_into_large_list_array(values))
2192+
Arc::new(SingleRowListArrayBuilder::new(values).build_large_list_array())
21892193
}
21902194

21912195
/// Converts a scalar value into an array of `size` rows.
@@ -2665,38 +2669,27 @@ impl ScalarValue {
26652669
let list_array = array.as_list::<i32>();
26662670
let nested_array = list_array.value(index);
26672671
// Produces a single element `ListArray` with the value at `index`.
2668-
let arr = Arc::new(array_into_list_array_with_field_name(
2669-
nested_array,
2670-
field.is_nullable(),
2671-
field.name(),
2672-
));
2673-
2674-
ScalarValue::List(arr)
2672+
SingleRowListArrayBuilder::new(nested_array)
2673+
.with_field(field)
2674+
.build_list_scalar()
26752675
}
26762676
DataType::LargeList(field) => {
26772677
let list_array = as_large_list_array(array);
26782678
let nested_array = list_array.value(index);
26792679
// Produces a single element `LargeListArray` with the value at `index`.
2680-
let arr = Arc::new(array_into_large_list_array_with_field_name(
2681-
nested_array,
2682-
field.name(),
2683-
));
2684-
2685-
ScalarValue::LargeList(arr)
2680+
SingleRowListArrayBuilder::new(nested_array)
2681+
.with_field(field)
2682+
.build_large_list_scalar()
26862683
}
26872684
// TODO: There is no test for FixedSizeList now, add it later
26882685
DataType::FixedSizeList(field, _) => {
26892686
let list_array = as_fixed_size_list_array(array)?;
26902687
let nested_array = list_array.value(index);
2691-
// Produces a single element `ListArray` with the value at `index`.
2688+
// Produces a single element `FixedSizeListArray` with the value at `index`.
26922689
let list_size = nested_array.len();
2693-
let arr = Arc::new(array_into_fixed_size_list_array_with_field_name(
2694-
nested_array,
2695-
list_size,
2696-
field.name(),
2697-
));
2698-
2699-
ScalarValue::FixedSizeList(arr)
2690+
SingleRowListArrayBuilder::new(nested_array)
2691+
.with_field(field)
2692+
.build_fixed_size_list_scalar(list_size)
27002693
}
27012694
DataType::Date32 => typed_cast!(array, index, Date32Array, Date32)?,
27022695
DataType::Date64 => typed_cast!(array, index, Date64Array, Date64)?,
@@ -3895,7 +3888,6 @@ mod tests {
38953888
};
38963889

38973890
use crate::assert_batches_eq;
3898-
use crate::utils::array_into_list_array_nullable;
38993891
use arrow::buffer::OffsetBuffer;
39003892
use arrow::compute::{is_null, kernels};
39013893
use arrow::error::ArrowError;
@@ -4071,14 +4063,15 @@ mod tests {
40714063

40724064
let result = ScalarValue::new_list_nullable(scalars.as_slice(), &DataType::Utf8);
40734065

4074-
let expected = array_into_list_array_nullable(Arc::new(StringArray::from(vec![
4075-
"rust",
4076-
"arrow",
4077-
"data-fusion",
4078-
])));
4066+
let expected = single_row_list_array(vec!["rust", "arrow", "data-fusion"]);
40794067
assert_eq!(*result, expected);
40804068
}
40814069

4070+
fn single_row_list_array(items: Vec<&str>) -> ListArray {
4071+
SingleRowListArrayBuilder::new(Arc::new(StringArray::from(items)))
4072+
.build_list_array()
4073+
}
4074+
40824075
fn build_list<O: OffsetSizeTrait>(
40834076
values: Vec<Option<Vec<Option<i64>>>>,
40844077
) -> Vec<ScalarValue> {
@@ -4281,12 +4274,8 @@ mod tests {
42814274

42824275
#[test]
42834276
fn iter_to_array_string_test() {
4284-
let arr1 = array_into_list_array_nullable(Arc::new(StringArray::from(vec![
4285-
"foo", "bar", "baz",
4286-
])));
4287-
let arr2 = array_into_list_array_nullable(Arc::new(StringArray::from(vec![
4288-
"rust", "world",
4289-
])));
4277+
let arr1 = single_row_list_array(vec!["foo", "bar", "baz"]);
4278+
let arr2 = single_row_list_array(vec!["rust", "world"]);
42904279

42914280
let scalars = vec![
42924281
ScalarValue::List(Arc::new(arr1)),
@@ -5743,13 +5732,13 @@ mod tests {
57435732
// Define list-of-structs scalars
57445733

57455734
let nl0_array = ScalarValue::iter_to_array(vec![s0, s1.clone()]).unwrap();
5746-
let nl0 = ScalarValue::List(Arc::new(array_into_list_array_nullable(nl0_array)));
5735+
let nl0 = SingleRowListArrayBuilder::new(nl0_array).build_list_scalar();
57475736

57485737
let nl1_array = ScalarValue::iter_to_array(vec![s2]).unwrap();
5749-
let nl1 = ScalarValue::List(Arc::new(array_into_list_array_nullable(nl1_array)));
5738+
let nl1 = SingleRowListArrayBuilder::new(nl1_array).build_list_scalar();
57505739

57515740
let nl2_array = ScalarValue::iter_to_array(vec![s1]).unwrap();
5752-
let nl2 = ScalarValue::List(Arc::new(array_into_list_array_nullable(nl2_array)));
5741+
let nl2 = SingleRowListArrayBuilder::new(nl2_array).build_list_scalar();
57535742

57545743
// iter_to_array for list-of-struct
57555744
let array = ScalarValue::iter_to_array(vec![nl0, nl1, nl2]).unwrap();

0 commit comments

Comments
 (0)