Skip to content

Commit 923f098

Browse files
authored
Minor: improve performance of ScalarValue::Binary* debug (apache#12323)
* Minor: improve performance of ScalarValue debug * reduce allocations * simplify loop * Add test for single list * Add another test for display
1 parent 1a44ad2 commit 923f098

File tree

1 file changed

+69
-11
lines changed
  • datafusion/common/src/scalar

1 file changed

+69
-11
lines changed

datafusion/common/src/scalar/mod.rs

+69-11
Original file line numberDiff line numberDiff line change
@@ -3653,18 +3653,20 @@ fn fmt_list(arr: ArrayRef, f: &mut fmt::Formatter) -> fmt::Result {
36533653
write!(f, "{value_formatter}")
36543654
}
36553655

3656+
/// writes a byte array to formatter. `[1, 2, 3]` ==> `"1,2,3"`
3657+
fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
3658+
let mut iter = data.iter();
3659+
if let Some(b) = iter.next() {
3660+
write!(f, "{b}")?;
3661+
}
3662+
for b in iter {
3663+
write!(f, ",{b}")?;
3664+
}
3665+
Ok(())
3666+
}
3667+
36563668
impl fmt::Debug for ScalarValue {
36573669
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3658-
fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
3659-
write!(
3660-
f,
3661-
"{}",
3662-
data.iter()
3663-
.map(|v| format!("{v}"))
3664-
.collect::<Vec<_>>()
3665-
.join(",")
3666-
)
3667-
}
36683670
match self {
36693671
ScalarValue::Decimal128(_, _, _) => write!(f, "Decimal128({self})"),
36703672
ScalarValue::Decimal256(_, _, _) => write!(f, "Decimal256({self})"),
@@ -3714,7 +3716,7 @@ impl fmt::Debug for ScalarValue {
37143716
write!(f, "FixedSizeBinary({size}, {self})")
37153717
}
37163718
ScalarValue::FixedSizeBinary(size, Some(b)) => {
3717-
write!(f, "Binary({size}, \"")?;
3719+
write!(f, "FixedSizeBinary({size}, \"")?;
37183720
fmt_binary(b.as_slice(), f)?;
37193721
write!(f, "\")")
37203722
}
@@ -6659,6 +6661,8 @@ mod tests {
66596661
fn test_binary_display() {
66606662
let no_binary_value = ScalarValue::Binary(None);
66616663
assert_eq!(format!("{no_binary_value}"), "NULL");
6664+
let single_binary_value = ScalarValue::Binary(Some(vec![42u8]));
6665+
assert_eq!(format!("{single_binary_value}"), "2A");
66626666
let small_binary_value = ScalarValue::Binary(Some(vec![1u8, 2, 3]));
66636667
assert_eq!(format!("{small_binary_value}"), "010203");
66646668
let large_binary_value =
@@ -6692,6 +6696,60 @@ mod tests {
66926696
assert_eq!(format!("{large_binary_value}"), "0102030405060708090A...");
66936697
}
66946698

6699+
#[test]
6700+
fn test_binary_debug() {
6701+
let no_binary_value = ScalarValue::Binary(None);
6702+
assert_eq!(format!("{no_binary_value:?}"), "Binary(NULL)");
6703+
let single_binary_value = ScalarValue::Binary(Some(vec![42u8]));
6704+
assert_eq!(format!("{single_binary_value:?}"), "Binary(\"42\")");
6705+
let small_binary_value = ScalarValue::Binary(Some(vec![1u8, 2, 3]));
6706+
assert_eq!(format!("{small_binary_value:?}"), "Binary(\"1,2,3\")");
6707+
let large_binary_value =
6708+
ScalarValue::Binary(Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
6709+
assert_eq!(
6710+
format!("{large_binary_value:?}"),
6711+
"Binary(\"1,2,3,4,5,6,7,8,9,10,11\")"
6712+
);
6713+
6714+
let no_binary_value = ScalarValue::BinaryView(None);
6715+
assert_eq!(format!("{no_binary_value:?}"), "BinaryView(NULL)");
6716+
let small_binary_value = ScalarValue::BinaryView(Some(vec![1u8, 2, 3]));
6717+
assert_eq!(format!("{small_binary_value:?}"), "BinaryView(\"1,2,3\")");
6718+
let large_binary_value =
6719+
ScalarValue::BinaryView(Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
6720+
assert_eq!(
6721+
format!("{large_binary_value:?}"),
6722+
"BinaryView(\"1,2,3,4,5,6,7,8,9,10,11\")"
6723+
);
6724+
6725+
let no_binary_value = ScalarValue::LargeBinary(None);
6726+
assert_eq!(format!("{no_binary_value:?}"), "LargeBinary(NULL)");
6727+
let small_binary_value = ScalarValue::LargeBinary(Some(vec![1u8, 2, 3]));
6728+
assert_eq!(format!("{small_binary_value:?}"), "LargeBinary(\"1,2,3\")");
6729+
let large_binary_value =
6730+
ScalarValue::LargeBinary(Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
6731+
assert_eq!(
6732+
format!("{large_binary_value:?}"),
6733+
"LargeBinary(\"1,2,3,4,5,6,7,8,9,10,11\")"
6734+
);
6735+
6736+
let no_binary_value = ScalarValue::FixedSizeBinary(3, None);
6737+
assert_eq!(format!("{no_binary_value:?}"), "FixedSizeBinary(3, NULL)");
6738+
let small_binary_value = ScalarValue::FixedSizeBinary(3, Some(vec![1u8, 2, 3]));
6739+
assert_eq!(
6740+
format!("{small_binary_value:?}"),
6741+
"FixedSizeBinary(3, \"1,2,3\")"
6742+
);
6743+
let large_binary_value = ScalarValue::FixedSizeBinary(
6744+
11,
6745+
Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
6746+
);
6747+
assert_eq!(
6748+
format!("{large_binary_value:?}"),
6749+
"FixedSizeBinary(11, \"1,2,3,4,5,6,7,8,9,10,11\")"
6750+
);
6751+
}
6752+
66956753
#[test]
66966754
fn test_build_timestamp_millisecond_list() {
66976755
let values = vec![ScalarValue::TimestampMillisecond(Some(1), None)];

0 commit comments

Comments
 (0)