You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by al...@apache.org on 2024/09/04 12:20:52 UTC
(datafusion) branch main updated: Improve binary scalars display (#12192)
This is an automated email from the ASF dual-hosted git repository.
alamb 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 e5a6cd5f67 Improve binary scalars display (#12192)
e5a6cd5f67 is described below
commit e5a6cd5f6784f1d1b3d559d5356a6154a73e077c
Author: 张林伟 <le...@gmail.com>
AuthorDate: Wed Sep 4 20:20:46 2024 +0800
Improve binary scalars display (#12192)
* Improve binary scalar display
* Reduce allocation & add test
* Display binary scalar as hex string
---
datafusion/common/src/scalar/mod.rs | 79 +++++++++++++++++++++++++++++++------
1 file changed, 66 insertions(+), 13 deletions(-)
diff --git a/datafusion/common/src/scalar/mod.rs b/datafusion/common/src/scalar/mod.rs
index 5acc2b6f18..3cff0731dc 100644
--- a/datafusion/common/src/scalar/mod.rs
+++ b/datafusion/common/src/scalar/mod.rs
@@ -3517,14 +3517,14 @@ impl fmt::Display for ScalarValue {
| ScalarValue::FixedSizeBinary(_, e)
| ScalarValue::LargeBinary(e)
| ScalarValue::BinaryView(e) => match e {
- Some(l) => write!(
- f,
- "{}",
- l.iter()
- .map(|v| format!("{v}"))
- .collect::<Vec<_>>()
- .join(",")
- )?,
+ 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(""))?;
+ }
+ }
None => write!(f, "NULL")?,
},
ScalarValue::List(arr) => fmt_list(arr.to_owned() as ArrayRef, f)?,
@@ -3648,6 +3648,16 @@ fn fmt_list(arr: ArrayRef, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Debug for ScalarValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ data.iter()
+ .map(|v| format!("{v}"))
+ .collect::<Vec<_>>()
+ .join(",")
+ )
+ }
match self {
ScalarValue::Decimal128(_, _, _) => write!(f, "Decimal128({self})"),
ScalarValue::Decimal256(_, _, _) => write!(f, "Decimal256({self})"),
@@ -3682,17 +3692,31 @@ impl fmt::Debug for ScalarValue {
ScalarValue::LargeUtf8(None) => write!(f, "LargeUtf8({self})"),
ScalarValue::LargeUtf8(Some(_)) => write!(f, "LargeUtf8(\"{self}\")"),
ScalarValue::Binary(None) => write!(f, "Binary({self})"),
- ScalarValue::Binary(Some(_)) => write!(f, "Binary(\"{self}\")"),
+ ScalarValue::Binary(Some(b)) => {
+ write!(f, "Binary(\"")?;
+ fmt_binary(b.as_slice(), f)?;
+ write!(f, "\")")
+ }
ScalarValue::BinaryView(None) => write!(f, "BinaryView({self})"),
- ScalarValue::BinaryView(Some(_)) => write!(f, "BinaryView(\"{self}\")"),
+ ScalarValue::BinaryView(Some(b)) => {
+ write!(f, "BinaryView(\"")?;
+ fmt_binary(b.as_slice(), f)?;
+ write!(f, "\")")
+ }
ScalarValue::FixedSizeBinary(size, None) => {
write!(f, "FixedSizeBinary({size}, {self})")
}
- ScalarValue::FixedSizeBinary(size, Some(_)) => {
- write!(f, "FixedSizeBinary({size}, \"{self}\")")
+ ScalarValue::FixedSizeBinary(size, Some(b)) => {
+ write!(f, "Binary({size}, \"")?;
+ fmt_binary(b.as_slice(), f)?;
+ write!(f, "\")")
}
ScalarValue::LargeBinary(None) => write!(f, "LargeBinary({self})"),
- ScalarValue::LargeBinary(Some(_)) => write!(f, "LargeBinary(\"{self}\")"),
+ ScalarValue::LargeBinary(Some(b)) => {
+ write!(f, "LargeBinary(\"")?;
+ fmt_binary(b.as_slice(), f)?;
+ write!(f, "\")")
+ }
ScalarValue::FixedSizeList(_) => write!(f, "FixedSizeList({self})"),
ScalarValue::List(_) => write!(f, "List({self})"),
ScalarValue::LargeList(_) => write!(f, "LargeList({self})"),
@@ -6518,6 +6542,35 @@ mod tests {
assert_batches_eq!(&expected, &[batch]);
}
+ #[test]
+ fn test_binary_display() {
+ 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 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 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 small_binary_value = ScalarValue::FixedSizeBinary(3, Some(vec![1u8, 2, 3]));
+ assert_eq!(format!("{small_binary_value}"), "010203");
+ let large_binary_value = ScalarValue::FixedSizeBinary(
+ 11,
+ Some(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
+ );
+ assert_eq!(format!("{large_binary_value}"), "0102030405060708090A...");
+ }
+
#[test]
fn test_build_timestamp_millisecond_list() {
let values = vec![ScalarValue::TimestampMillisecond(Some(1), None)];
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org