Skip to content

Commit f431de0

Browse files
authored
Allow converting bytes::Bytes into a Binary Scalar (delta-io#1373)
## What changes are proposed in this pull request? Allow converting `bytes::Bytes` into a Binary Scalar. <!-- Please clarify what changes you are proposing and why the changes are needed. The purpose of this section is to outline the changes, why they are needed, and how this PR fixes the issue. If the reason for the change is already explained clearly in an issue, then it does not need to be restated here. 1. If you propose a new API or feature, clarify the use case for a new API or feature. 2. If you fix a bug, you can clarify why it is a bug. --> <!-- Uncomment this section if there are any changes affecting public APIs: ### This PR affects the following public APIs If there are breaking changes, please ensure the `breaking-changes` label gets added by CI, and describe why the changes are needed. Note that _new_ public APIs are not considered breaking. --> ## How was this change tested? <!-- Please make sure to add test cases that check the changes thoroughly including negative and positive cases if possible. If it was tested in a way different from regular unit tests, please clarify how you tested, ideally via a reproducible test documented in the PR description. -->
1 parent 01e7cb1 commit f431de0

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

kernel/src/expressions/scalars.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ impl From<&[u8]> for Scalar {
546546
}
547547
}
548548

549+
impl From<bytes::Bytes> for Scalar {
550+
fn from(b: bytes::Bytes) -> Self {
551+
Self::Binary(b.into())
552+
}
553+
}
554+
549555
impl<T> TryFrom<Vec<T>> for Scalar
550556
where
551557
T: Into<Scalar> + ToDataType,
@@ -1239,4 +1245,35 @@ mod tests {
12391245

12401246
Ok(())
12411247
}
1248+
1249+
#[test]
1250+
fn test_bytes_conversion() {
1251+
// Test with non-empty bytes
1252+
let bytes = bytes::Bytes::from(vec![1, 2, 3, 4, 5]);
1253+
let scalar: Scalar = bytes.into();
1254+
1255+
// Verify the scalar is of Binary type
1256+
assert!(matches!(scalar, Scalar::Binary(_)));
1257+
1258+
// Verify the data type
1259+
assert_eq!(scalar.data_type(), DataType::BINARY);
1260+
1261+
// Extract the binary data and verify contents
1262+
if let Scalar::Binary(data) = scalar {
1263+
assert_eq!(data, vec![1, 2, 3, 4, 5]);
1264+
} else {
1265+
panic!("Expected Binary scalar");
1266+
}
1267+
1268+
// Test with empty bytes
1269+
let empty_bytes = bytes::Bytes::new();
1270+
let empty_scalar: Scalar = empty_bytes.into();
1271+
1272+
assert!(matches!(empty_scalar, Scalar::Binary(_)));
1273+
if let Scalar::Binary(data) = empty_scalar {
1274+
assert!(data.is_empty());
1275+
} else {
1276+
panic!("Expected Binary scalar");
1277+
}
1278+
}
12421279
}

0 commit comments

Comments
 (0)