This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 677c543aef Optimize memory footprint of view arrays from
`ScalarValue::to_array_of_size` (#19441)
677c543aef is described below
commit 677c543aef6ce8de714c97c9f5a85a57025e2059
Author: Jeffrey Vo <[email protected]>
AuthorDate: Tue Dec 23 17:40:11 2025 +0900
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.
-->
---
datafusion/common/src/scalar/mod.rs | 68 +++++++++++++++++++++++++++----------
1 file changed, 51 insertions(+), 17 deletions(-)
diff --git a/datafusion/common/src/scalar/mod.rs
b/datafusion/common/src/scalar/mod.rs
index 2eb7e970df..fe0c92e0ec 100644
--- a/datafusion/common/src/scalar/mod.rs
+++ b/datafusion/common/src/scalar/mod.rs
@@ -57,19 +57,20 @@ use crate::utils::SingleRowListArrayBuilder;
use crate::{_internal_datafusion_err, arrow_datafusion_err};
use arrow::array::{
Array, ArrayData, ArrayRef, ArrowNativeTypeOp, ArrowPrimitiveType, AsArray,
- BinaryArray, BinaryViewArray, BooleanArray, Date32Array, Date64Array,
Decimal32Array,
- Decimal64Array, Decimal128Array, Decimal256Array, DictionaryArray,
- DurationMicrosecondArray, DurationMillisecondArray,
DurationNanosecondArray,
- DurationSecondArray, FixedSizeBinaryArray, FixedSizeBinaryBuilder,
- FixedSizeListArray, Float16Array, Float32Array, Float64Array,
GenericListArray,
- Int8Array, Int16Array, Int32Array, Int64Array, IntervalDayTimeArray,
- IntervalMonthDayNanoArray, IntervalYearMonthArray, LargeBinaryArray,
LargeListArray,
- LargeStringArray, ListArray, MapArray, MutableArrayData, OffsetSizeTrait,
- PrimitiveArray, Scalar, StringArray, StringViewArray, StructArray,
- Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
- Time64NanosecondArray, TimestampMicrosecondArray,
TimestampMillisecondArray,
- TimestampNanosecondArray, TimestampSecondArray, UInt8Array, UInt16Array,
UInt32Array,
- UInt64Array, UnionArray, new_empty_array, new_null_array,
+ BinaryArray, BinaryViewArray, BinaryViewBuilder, BooleanArray, Date32Array,
+ Date64Array, Decimal32Array, Decimal64Array, Decimal128Array,
Decimal256Array,
+ DictionaryArray, DurationMicrosecondArray, DurationMillisecondArray,
+ DurationNanosecondArray, DurationSecondArray, FixedSizeBinaryArray,
+ FixedSizeBinaryBuilder, FixedSizeListArray, Float16Array, Float32Array,
Float64Array,
+ GenericListArray, Int8Array, Int16Array, Int32Array, Int64Array,
+ IntervalDayTimeArray, IntervalMonthDayNanoArray, IntervalYearMonthArray,
+ LargeBinaryArray, LargeListArray, LargeStringArray, ListArray, MapArray,
+ MutableArrayData, OffsetSizeTrait, PrimitiveArray, Scalar, StringArray,
+ StringViewArray, StringViewBuilder, StructArray, Time32MillisecondArray,
+ Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray,
+ TimestampMicrosecondArray, TimestampMillisecondArray,
TimestampNanosecondArray,
+ TimestampSecondArray, UInt8Array, UInt16Array, UInt32Array, UInt64Array,
UnionArray,
+ new_empty_array, new_null_array,
};
use arrow::buffer::{BooleanBuffer, ScalarBuffer};
use arrow::compute::kernels::cast::{CastOptions, cast_with_options};
@@ -3027,7 +3028,13 @@ impl ScalarValue {
},
ScalarValue::Utf8View(e) => match e {
Some(value) => {
- Arc::new(StringViewArray::from_iter_values(repeat_n(value,
size)))
+ let mut builder =
+
StringViewBuilder::with_capacity(size).with_deduplicate_strings();
+ for _ in 0..size {
+ builder.append_value(value);
+ }
+ let array = builder.finish();
+ Arc::new(array)
}
None => new_null_array(&DataType::Utf8View, size),
},
@@ -3042,9 +3049,15 @@ impl ScalarValue {
None => new_null_array(&DataType::Binary, size),
},
ScalarValue::BinaryView(e) => match e {
- Some(value) => Arc::new(
- repeat_n(Some(value.as_slice()),
size).collect::<BinaryViewArray>(),
- ),
+ Some(value) => {
+ let mut builder =
+
BinaryViewBuilder::with_capacity(size).with_deduplicate_strings();
+ for _ in 0..size {
+ builder.append_value(value);
+ }
+ let array = builder.finish();
+ Arc::new(array)
+ }
None => new_null_array(&DataType::BinaryView, size),
},
ScalarValue::FixedSizeBinary(s, e) => match e {
@@ -9232,6 +9245,27 @@ mod tests {
}
}
+ #[test]
+ fn test_views_minimize_memory() {
+ let value = "this string is longer than 12 bytes".to_string();
+
+ let scalar = ScalarValue::Utf8View(Some(value.clone()));
+ let array = scalar.to_array_of_size(10).unwrap();
+ let array = array.as_string_view();
+ let buffers = array.data_buffers();
+ assert_eq!(1, buffers.len());
+ // Ensure we only have a single copy of the value string
+ assert_eq!(value.len(), buffers[0].len());
+
+ // Same but for BinaryView
+ let scalar = ScalarValue::BinaryView(Some(value.bytes().collect()));
+ let array = scalar.to_array_of_size(10).unwrap();
+ let array = array.as_binary_view();
+ let buffers = array.data_buffers();
+ assert_eq!(1, buffers.len());
+ assert_eq!(value.len(), buffers[0].len());
+ }
+
#[test]
fn test_convert_array_to_scalar_vec() {
// 1: Regular ListArray
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]