Skip to content

Commit

Permalink
Minor: reduce allocations in ScalarValue::*Binary display (#12322)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb authored Sep 4, 2024
1 parent e5a6cd5 commit e6caf62
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions datafusion/common/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3517,12 +3517,13 @@ impl fmt::Display for ScalarValue {
| ScalarValue::FixedSizeBinary(_, e)
| ScalarValue::LargeBinary(e)
| ScalarValue::BinaryView(e) => match e {
Some(l) => {
let data = l.iter().map(|v| format!("{v:02X}")).collect::<Vec<_>>();
if data.len() > 10 {
write!(f, "{}...", data[..10].join(""))?;
} else {
write!(f, "{}", data.join(""))?;
Some(bytes) => {
// print up to first 10 bytes, with trailing ... if needed
for b in bytes.iter().take(10) {
write!(f, "{b:02X}")?;
}
if bytes.len() > 10 {
write!(f, "...")?;
}
}
None => write!(f, "NULL")?,
Expand Down Expand Up @@ -6544,24 +6545,32 @@ mod tests {

#[test]
fn test_binary_display() {
let no_binary_value = ScalarValue::Binary(None);
assert_eq!(format!("{no_binary_value}"), "NULL");
let small_binary_value = ScalarValue::Binary(Some(vec![1u8, 2, 3]));
assert_eq!(format!("{small_binary_value}"), "010203");
let large_binary_value =
ScalarValue::Binary(Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
assert_eq!(format!("{large_binary_value}"), "0102030405060708090A...");

let no_binary_value = ScalarValue::BinaryView(None);
assert_eq!(format!("{no_binary_value}"), "NULL");
let small_binary_value = ScalarValue::BinaryView(Some(vec![1u8, 2, 3]));
assert_eq!(format!("{small_binary_value}"), "010203");
let large_binary_value =
ScalarValue::BinaryView(Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
assert_eq!(format!("{large_binary_value}"), "0102030405060708090A...");

let no_binary_value = ScalarValue::LargeBinary(None);
assert_eq!(format!("{no_binary_value}"), "NULL");
let small_binary_value = ScalarValue::LargeBinary(Some(vec![1u8, 2, 3]));
assert_eq!(format!("{small_binary_value}"), "010203");
let large_binary_value =
ScalarValue::LargeBinary(Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
assert_eq!(format!("{large_binary_value}"), "0102030405060708090A...");

let no_binary_value = ScalarValue::FixedSizeBinary(3, None);
assert_eq!(format!("{no_binary_value}"), "NULL");
let small_binary_value = ScalarValue::FixedSizeBinary(3, Some(vec![1u8, 2, 3]));
assert_eq!(format!("{small_binary_value}"), "010203");
let large_binary_value = ScalarValue::FixedSizeBinary(
Expand Down

0 comments on commit e6caf62

Please sign in to comment.