alamb commented on code in PR #1842:
URL: https://github.com/apache/arrow-rs/pull/1842#discussion_r899459271
##########
arrow/src/array/builder.rs:
##########
@@ -1909,101 +1886,65 @@ struct FieldData {
/// The Arrow data type represented in the `values_buffer`, which is
untyped
data_type: DataType,
/// A buffer containing the values for this field in raw bytes
- values_buffer: Option<MutableBuffer>,
+ values_buffer: Box<dyn FieldDataValues>,
/// The number of array slots represented by the buffer
slots: usize,
/// A builder for the null bitmap
bitmap_builder: BooleanBufferBuilder,
}
+/// A type-erased [`BufferBuilder`] used by [`FieldData`]
+trait FieldDataValues: std::fmt::Debug {
+ fn as_mut_any(&mut self) -> &mut dyn Any;
+
+ fn append_null(&mut self);
+
+ fn finish(&mut self) -> Buffer;
+}
+
+impl<T: ArrowNativeType> FieldDataValues for BufferBuilder<T> {
+ fn as_mut_any(&mut self) -> &mut dyn Any {
+ self
+ }
+
+ fn append_null(&mut self) {
+ self.advance(1)
+ }
+
+ fn finish(&mut self) -> Buffer {
+ self.finish()
+ }
+}
+
impl FieldData {
/// Creates a new `FieldData`.
- fn new(type_id: i8, data_type: DataType) -> Self {
+ fn new<T: ArrowPrimitiveType>(type_id: i8, data_type: DataType) -> Self {
Self {
type_id,
data_type,
- values_buffer: Some(MutableBuffer::new(1)),
slots: 0,
+ values_buffer: Box::new(BufferBuilder::<T::Native>::new(1)),
bitmap_builder: BooleanBufferBuilder::new(1),
}
}
/// Appends a single value to this `FieldData`'s `values_buffer`.
- #[allow(clippy::unnecessary_wraps)]
- fn append_to_values_buffer<T: ArrowPrimitiveType>(
- &mut self,
- v: T::Native,
- ) -> Result<()> {
- let values_buffer = self
- .values_buffer
- .take()
- .expect("Values buffer was never created");
- let mut builder: BufferBuilder<T::Native> =
- mutable_buffer_to_builder(values_buffer, self.slots);
- builder.append(v);
- let mutable_buffer = builder_to_mutable_buffer(builder);
- self.values_buffer = Some(mutable_buffer);
+ fn append_value<T: ArrowPrimitiveType>(&mut self, v: T::Native) {
+ self.values_buffer
+ .as_mut_any()
+ .downcast_mut::<BufferBuilder<T::Native>>()
+ .unwrap()
Review Comment:
```suggestion
.expect("Tried to append unexpected type")
```
--
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]