westonpace commented on code in PR #6096:
URL: https://github.com/apache/arrow-rs/pull/6096#discussion_r1771419577
##########
arrow-row/src/lib.rs:
##########
@@ -868,6 +885,24 @@ impl Rows {
+ self.buffer.len()
+ self.offsets.len() * std::mem::size_of::<usize>()
}
+
+ /// Create a [BinaryArray] from the [Rows] data without reallocating the
+ /// underlying bytes.
+ pub fn into_binary(self) -> BinaryArray {
+ assert!(
+ self.buffer.len() <= i32::MAX as usize,
+ "rows buffer too large"
+ );
+ let offsets_scalar =
ScalarBuffer::from_iter(self.offsets.into_iter().map(i32::usize_as));
Review Comment:
I agree with @bkirwi 's logic here. If we assume that `self.offsets` is
well formed with respect to `self.buffer` then we shouldn't need to check the
individual offsets.
##########
arrow-row/src/lib.rs:
##########
@@ -738,6 +738,42 @@ impl RowConverter {
}
}
+ /// Create a new [Rows] instance from the given binary data.
+ ///
+ /// ```
+ /// # use std::sync::Arc;
+ /// # use std::collections::HashSet;
+ /// # use arrow_array::cast::AsArray;
+ /// # use arrow_array::StringArray;
+ /// # use arrow_row::{OwnedRow, Row, RowConverter, RowParser, SortField};
+ /// # use arrow_schema::DataType;
+ /// #
+ /// let converter =
RowConverter::new(vec![SortField::new(DataType::Utf8)]).unwrap();
+ /// let array = StringArray::from(vec!["hello", "world", "a", "a",
"hello"]);
+ /// let rows = converter.convert_columns(&[Arc::new(array)]).unwrap();
+ ///
+ /// // We can convert rows into binary format and back in batch.
+ /// let values: Vec<OwnedRow> = rows.iter().map(|r| r.owned()).collect();
+ /// let binary = rows.try_into_binary().expect("small");
Review Comment:
I got a little confused by `.expect("small")`. What does "small" mean in
this context? Why not just `.unwrap()`?
##########
arrow-row/src/lib.rs:
##########
@@ -738,6 +738,42 @@ impl RowConverter {
}
}
+ /// Create a new [Rows] instance from the given binary data.
+ ///
+ /// ```
+ /// # use std::sync::Arc;
+ /// # use std::collections::HashSet;
+ /// # use arrow_array::cast::AsArray;
+ /// # use arrow_array::StringArray;
+ /// # use arrow_row::{OwnedRow, Row, RowConverter, RowParser, SortField};
+ /// # use arrow_schema::DataType;
+ /// #
+ /// let converter =
RowConverter::new(vec![SortField::new(DataType::Utf8)]).unwrap();
+ /// let array = StringArray::from(vec!["hello", "world", "a", "a",
"hello"]);
+ /// let rows = converter.convert_columns(&[Arc::new(array)]).unwrap();
+ ///
+ /// // We can convert rows into binary format and back in batch.
+ /// let values: Vec<OwnedRow> = rows.iter().map(|r| r.owned()).collect();
+ /// let binary = rows.try_into_binary().expect("small");
+ /// let converted = converter.from_binary(binary.clone());
+ /// assert!(converted.iter().eq(values.iter().map(|r| r.row())));
+ /// ```
+ pub fn from_binary(&self, array: BinaryArray) -> Rows {
+ assert_eq!(
+ array.null_count(),
+ 0,
+ "can't construct Rows instance from array with nulls"
+ );
+ Rows {
+ buffer: array.values().to_vec(),
+ offsets: array.offsets().iter().map(|&i| i.as_usize()).collect(),
+ config: RowConfig {
+ fields: Arc::clone(&self.fields),
Review Comment:
More for my curiosity than anything but why `Arc::clone(&self.fields)`
instead of `self.fields.clone()`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]