This is an automated email from the ASF dual-hosted git repository.

mneumann 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 e6caf6206b Minor: reduce allocations in ScalarValue::*Binary display 
(#12322)
e6caf6206b is described below

commit e6caf6206b516fe97fc2d2962900030d1782e7d5
Author: Andrew Lamb <[email protected]>
AuthorDate: Wed Sep 4 12:25:43 2024 -0400

    Minor: reduce allocations in ScalarValue::*Binary display (#12322)
---
 datafusion/common/src/scalar/mod.rs | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/datafusion/common/src/scalar/mod.rs 
b/datafusion/common/src/scalar/mod.rs
index 3cff0731dc..88802af23a 100644
--- a/datafusion/common/src/scalar/mod.rs
+++ b/datafusion/common/src/scalar/mod.rs
@@ -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")?,
@@ -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(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to