Skip to content

Commit b765274

Browse files
authored
Docs: Add additional documentation and example for make_array (#9112)
# Which issue does this PR close? - Part of #9061 # Rationale for this change @tustvold says #9058 (comment) > My 2 cents is it would be better to move the codepaths relying on ArrayData over to using the typed arrays directly, this should not only cut down on allocations but unnecessary validation and dispatch overheads. Let's make sure the documentation also encodes this wisdom # What changes are included in this PR? Improve the documentation for `make_array` to include the information about `make_array` # Are these changes tested? By CI # Are there any user-facing changes? Only docs and an example, no functional changes
1 parent b2aeab1 commit b765274

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

arrow-array/src/array/mod.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,36 @@ impl<R: RunEndIndexType> PartialEq for RunArray<R> {
757757
}
758758
}
759759

760-
/// Constructs an array using the input `data`.
761-
/// Returns a reference-counted `Array` instance.
760+
/// Constructs an [`ArrayRef`] from an [`ArrayData`].
761+
///
762+
/// # Notes:
763+
///
764+
/// It is more efficient to directly construct the concrete array type rather
765+
/// than using this function as creating an `ArrayData` requires at least one
766+
/// additional allocation (the Vec of buffers).
767+
///
768+
/// # Example:
769+
/// ```
770+
/// # use std::sync::Arc;
771+
/// # use arrow_data::ArrayData;
772+
/// # use arrow_array::{make_array, ArrayRef, Int32Array};
773+
/// # use arrow_buffer::{Buffer, ScalarBuffer};
774+
/// # use arrow_schema::DataType;
775+
/// // Create an Int32Array with values [1, 2, 3]
776+
/// let values_buffer = Buffer::from_slice_ref(&[1, 2, 3]);
777+
/// // ArrayData can be constructed using ArrayDataBuilder
778+
/// let builder = ArrayData::builder(DataType::Int32)
779+
/// .len(3)
780+
/// .add_buffer(values_buffer.clone());
781+
/// let array_data = builder.build().unwrap();
782+
/// // Create the ArrayRef from the ArrayData
783+
/// let array = make_array(array_data);
784+
///
785+
/// // It is equivalent to directly constructing the Int32Array
786+
/// let scalar_buffer = ScalarBuffer::from(values_buffer);
787+
/// let int32_array: ArrayRef = Arc::new(Int32Array::new(scalar_buffer, None));
788+
/// assert_eq!(&array, &int32_array);
789+
/// ```
762790
pub fn make_array(data: ArrayData) -> ArrayRef {
763791
match data.data_type() {
764792
DataType::Boolean => Arc::new(BooleanArray::from(data)) as ArrayRef,

0 commit comments

Comments
 (0)