Skip to content

Commit 677c543

Browse files
authored
Optimize memory footprint of view arrays from ScalarValue::to_array_of_size (#19441)
## 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. --> - Closes #19440 ## 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 we have view scalars (utf8/binary) and we call `to_array_of_size`, the data buffers the resultant arrays have contains duplicate data. This is because the APIs we use don't deduplicate the data, instead appending it each time even though the data is exactly duplicated. ## 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. --> Manually use a builder with deduplication enabled. ## 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)? --> Added test. ## 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 e6faacb commit 677c543

File tree

1 file changed

+51
-17
lines changed
  • datafusion/common/src/scalar

1 file changed

+51
-17
lines changed

datafusion/common/src/scalar/mod.rs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,20 @@ use crate::utils::SingleRowListArrayBuilder;
5757
use crate::{_internal_datafusion_err, arrow_datafusion_err};
5858
use arrow::array::{
5959
Array, ArrayData, ArrayRef, ArrowNativeTypeOp, ArrowPrimitiveType, AsArray,
60-
BinaryArray, BinaryViewArray, BooleanArray, Date32Array, Date64Array, Decimal32Array,
61-
Decimal64Array, Decimal128Array, Decimal256Array, DictionaryArray,
62-
DurationMicrosecondArray, DurationMillisecondArray, DurationNanosecondArray,
63-
DurationSecondArray, FixedSizeBinaryArray, FixedSizeBinaryBuilder,
64-
FixedSizeListArray, Float16Array, Float32Array, Float64Array, GenericListArray,
65-
Int8Array, Int16Array, Int32Array, Int64Array, IntervalDayTimeArray,
66-
IntervalMonthDayNanoArray, IntervalYearMonthArray, LargeBinaryArray, LargeListArray,
67-
LargeStringArray, ListArray, MapArray, MutableArrayData, OffsetSizeTrait,
68-
PrimitiveArray, Scalar, StringArray, StringViewArray, StructArray,
69-
Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
70-
Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray,
71-
TimestampNanosecondArray, TimestampSecondArray, UInt8Array, UInt16Array, UInt32Array,
72-
UInt64Array, UnionArray, new_empty_array, new_null_array,
60+
BinaryArray, BinaryViewArray, BinaryViewBuilder, BooleanArray, Date32Array,
61+
Date64Array, Decimal32Array, Decimal64Array, Decimal128Array, Decimal256Array,
62+
DictionaryArray, DurationMicrosecondArray, DurationMillisecondArray,
63+
DurationNanosecondArray, DurationSecondArray, FixedSizeBinaryArray,
64+
FixedSizeBinaryBuilder, FixedSizeListArray, Float16Array, Float32Array, Float64Array,
65+
GenericListArray, Int8Array, Int16Array, Int32Array, Int64Array,
66+
IntervalDayTimeArray, IntervalMonthDayNanoArray, IntervalYearMonthArray,
67+
LargeBinaryArray, LargeListArray, LargeStringArray, ListArray, MapArray,
68+
MutableArrayData, OffsetSizeTrait, PrimitiveArray, Scalar, StringArray,
69+
StringViewArray, StringViewBuilder, StructArray, Time32MillisecondArray,
70+
Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray,
71+
TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray,
72+
TimestampSecondArray, UInt8Array, UInt16Array, UInt32Array, UInt64Array, UnionArray,
73+
new_empty_array, new_null_array,
7374
};
7475
use arrow::buffer::{BooleanBuffer, ScalarBuffer};
7576
use arrow::compute::kernels::cast::{CastOptions, cast_with_options};
@@ -3027,7 +3028,13 @@ impl ScalarValue {
30273028
},
30283029
ScalarValue::Utf8View(e) => match e {
30293030
Some(value) => {
3030-
Arc::new(StringViewArray::from_iter_values(repeat_n(value, size)))
3031+
let mut builder =
3032+
StringViewBuilder::with_capacity(size).with_deduplicate_strings();
3033+
for _ in 0..size {
3034+
builder.append_value(value);
3035+
}
3036+
let array = builder.finish();
3037+
Arc::new(array)
30313038
}
30323039
None => new_null_array(&DataType::Utf8View, size),
30333040
},
@@ -3042,9 +3049,15 @@ impl ScalarValue {
30423049
None => new_null_array(&DataType::Binary, size),
30433050
},
30443051
ScalarValue::BinaryView(e) => match e {
3045-
Some(value) => Arc::new(
3046-
repeat_n(Some(value.as_slice()), size).collect::<BinaryViewArray>(),
3047-
),
3052+
Some(value) => {
3053+
let mut builder =
3054+
BinaryViewBuilder::with_capacity(size).with_deduplicate_strings();
3055+
for _ in 0..size {
3056+
builder.append_value(value);
3057+
}
3058+
let array = builder.finish();
3059+
Arc::new(array)
3060+
}
30483061
None => new_null_array(&DataType::BinaryView, size),
30493062
},
30503063
ScalarValue::FixedSizeBinary(s, e) => match e {
@@ -9232,6 +9245,27 @@ mod tests {
92329245
}
92339246
}
92349247

9248+
#[test]
9249+
fn test_views_minimize_memory() {
9250+
let value = "this string is longer than 12 bytes".to_string();
9251+
9252+
let scalar = ScalarValue::Utf8View(Some(value.clone()));
9253+
let array = scalar.to_array_of_size(10).unwrap();
9254+
let array = array.as_string_view();
9255+
let buffers = array.data_buffers();
9256+
assert_eq!(1, buffers.len());
9257+
// Ensure we only have a single copy of the value string
9258+
assert_eq!(value.len(), buffers[0].len());
9259+
9260+
// Same but for BinaryView
9261+
let scalar = ScalarValue::BinaryView(Some(value.bytes().collect()));
9262+
let array = scalar.to_array_of_size(10).unwrap();
9263+
let array = array.as_binary_view();
9264+
let buffers = array.data_buffers();
9265+
assert_eq!(1, buffers.len());
9266+
assert_eq!(value.len(), buffers[0].len());
9267+
}
9268+
92359269
#[test]
92369270
fn test_convert_array_to_scalar_vec() {
92379271
// 1: Regular ListArray

0 commit comments

Comments
 (0)