Skip to content

Commit 9ae29cb

Browse files
committed
Allocate a buffer of the correct length for ScalarValue::FixedSizeBinary in ScalarValue::to_array_of_size
1 parent 76b9e12 commit 9ae29cb

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

  • datafusion/common/src/scalar

datafusion/common/src/scalar/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ mod cache;
2121
mod consts;
2222
mod struct_builder;
2323

24+
use arrow::buffer::MutableBuffer;
25+
use arrow::buffer::NullBuffer;
2426
use std::borrow::Borrow;
2527
use std::cmp::Ordering;
2628
use std::collections::{HashSet, VecDeque};
@@ -3055,7 +3057,19 @@ impl ScalarValue {
30553057
)
30563058
.unwrap(),
30573059
),
3058-
None => Arc::new(FixedSizeBinaryArray::new_null(*s, size)),
3060+
None => {
3061+
// TODO: Replace with FixedSizeBinaryArray::new_null once a fix for
3062+
// https://github.com/apache/arrow-rs/issues/8900 is in the used arrow-rs
3063+
// version.
3064+
let capacity_in_bytes =
3065+
s.to_usize().unwrap().checked_mul(size).unwrap();
3066+
Arc::new(FixedSizeBinaryArray::try_new(
3067+
*s,
3068+
// MutableBuffer::new_null is in bits.
3069+
MutableBuffer::new_null(capacity_in_bytes * 8).into(),
3070+
Some(NullBuffer::new_null(size)),
3071+
)?)
3072+
}
30593073
},
30603074
ScalarValue::LargeBinary(e) => match e {
30613075
Some(value) => Arc::new(
@@ -5314,6 +5328,18 @@ mod tests {
53145328
assert_eq!(empty_array.len(), 0);
53155329
}
53165330

5331+
/// See https://github.com/apache/datafusion/issues/18870
5332+
#[test]
5333+
fn test_to_array_of_size_for_none_fsb() {
5334+
let sv = ScalarValue::FixedSizeBinary(5, None);
5335+
let result = sv
5336+
.to_array_of_size(2)
5337+
.expect("Failed to convert to array of size");
5338+
assert_eq!(result.len(), 2);
5339+
assert_eq!(result.null_count(), 2);
5340+
assert_eq!(result.as_fixed_size_binary().values().len(), 10);
5341+
}
5342+
53175343
#[test]
53185344
fn test_list_to_array_string() {
53195345
let scalars = vec![

0 commit comments

Comments
 (0)