You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2021/07/09 11:49:35 UTC
[arrow-rs] branch master updated: Supports `StringArray` creation
from String Vec. (#522)
This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 04d2057 Supports `StringArray` creation from String Vec. (#522)
04d2057 is described below
commit 04d2057d871265bf3da82f49539efed6fa1d893a
Author: Steven <as...@163.com>
AuthorDate: Fri Jul 9 19:49:28 2021 +0800
Supports `StringArray` creation from String Vec. (#522)
---
arrow/src/array/array_string.rs | 28 +++++++++++++++++++++++++---
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/arrow/src/array/array_string.rs b/arrow/src/array/array_string.rs
index ba28507..ec703e2 100644
--- a/arrow/src/array/array_string.rs
+++ b/arrow/src/array/array_string.rs
@@ -149,7 +149,10 @@ impl<OffsetSize: StringOffsetSizeTrait> GenericStringArray<OffsetSize> {
Self::from(data)
}
- pub(crate) fn from_vec(v: Vec<&str>) -> Self {
+ pub(crate) fn from_vec<Ptr>(v: Vec<Ptr>) -> Self
+ where
+ Ptr: AsRef<str>,
+ {
let mut offsets =
MutableBuffer::new((v.len() + 1) * std::mem::size_of::<OffsetSize>());
let mut values = MutableBuffer::new(0);
@@ -158,9 +161,9 @@ impl<OffsetSize: StringOffsetSizeTrait> GenericStringArray<OffsetSize> {
offsets.push(length_so_far);
for s in &v {
- length_so_far += OffsetSize::from_usize(s.len()).unwrap();
+ length_so_far += OffsetSize::from_usize(s.as_ref().len()).unwrap();
offsets.push(length_so_far);
- values.extend_from_slice(s.as_bytes());
+ values.extend_from_slice(s.as_ref().as_bytes());
}
let array_data = ArrayData::builder(OffsetSize::DATA_TYPE)
.len(v.len())
@@ -327,6 +330,14 @@ impl<OffsetSize: StringOffsetSizeTrait> From<Vec<&str>>
}
}
+impl<OffsetSize: StringOffsetSizeTrait> From<Vec<String>>
+ for GenericStringArray<OffsetSize>
+{
+ fn from(v: Vec<String>) -> Self {
+ GenericStringArray::<OffsetSize>::from_vec(v)
+ }
+}
+
/// An array where each element is a variable-sized sequence of bytes representing a string
/// whose maximum length (in bytes) is represented by a i32.
///
@@ -530,4 +541,15 @@ mod tests {
// but the actual number of items in the array should be 10
assert_eq!(string_array.len(), 10);
}
+
+ #[test]
+ fn test_string_array_from_string_vec() {
+ let data = vec!["Foo".to_owned(), "Bar".to_owned(), "Baz".to_owned()];
+ let array = StringArray::from(data);
+
+ assert_eq!(array.len(), 3);
+ assert_eq!(array.value(0), "Foo");
+ assert_eq!(array.value(1), "Bar");
+ assert_eq!(array.value(2), "Baz");
+ }
}