You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: clarifying comments in struct_builder.rs #5494 (#5499)
* feat: clarifying comments in struct_builder.rs
Added clarifying comments to StructBuilder about creating collection columns
* fixed commented line, improved comments
* Removed redundant line in comment
* fixed slightly misleading comment
* moved example code to comment
* better comment
* fixed comment type
Copy file name to clipboardexpand all lines: arrow-array/src/builder/struct_builder.rs
+77
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,81 @@ use std::sync::Arc;
25
25
///
26
26
/// Note that callers should make sure that methods of all the child field builders are
27
27
/// properly called to maintain the consistency of the data structure.
28
+
///
29
+
///
30
+
/// Handling arrays with complex layouts, such as `List<Struct<List<Struct>>>`, in Rust can be challenging due to its strong typing system.
31
+
/// To construct a collection builder ([`ListBuilder`], [`LargeListBuilder`], or [`MapBuilder`]) using [`make_builder`], multiple calls are required. This complexity arises from the recursive approach utilized by [`StructBuilder::from_fields`].
32
+
///
33
+
/// Initially, [`StructBuilder::from_fields`] invokes [`make_builder`], which returns a `Box<dyn ArrayBuilder>`. To obtain the specific collection builder, one must first use [`StructBuilder::field_builder`] to get a `Collection<[Box<dyn ArrayBuilder>]>`. Subsequently, the `values()` result from this operation can be downcast to the desired builder type.
34
+
///
35
+
/// For example, when working with [`ListBuilder`], you would first call [`StructBuilder::field_builder::<ListBuilder<Box<dyn ArrayBuilder>>>`] and then downcast the [`Box<dyn ArrayBuilder>`] to the specific [`StructBuilder`] you need.
36
+
///
37
+
/// For a practical example see the code below:
38
+
///
39
+
/// ```rust
40
+
/// use arrow_array::builder::{ArrayBuilder, ListBuilder, StringBuilder, StructBuilder};
41
+
/// use arrow_schema::{DataType, Field, Fields};
42
+
/// use std::sync::Arc;
43
+
///
44
+
/// // This is an example column that has a List<Struct<List<Struct>>> layout
45
+
/// let mut example_col = ListBuilder::new(StructBuilder::from_fields(
46
+
/// vec![Field::new(
47
+
/// "value_list",
48
+
/// DataType::List(Arc::new(Field::new(
49
+
/// "item",
50
+
/// DataType::Struct(Fields::from(vec![
51
+
/// Field::new("key", DataType::Utf8, true),
52
+
/// Field::new("value", DataType::Utf8, true),
53
+
/// ])), //In this example we are trying to get to this builder and insert key/value pairs
54
+
/// true,
55
+
/// ))),
56
+
/// true,
57
+
/// )],
58
+
/// 0,
59
+
/// ));
60
+
///
61
+
/// // We can obtain the StructBuilder without issues, because example_col was created with StructBuilder
62
+
/// let col_struct_builder: &mut StructBuilder = example_col.values();
63
+
///
64
+
/// // We can't obtain the ListBuilder<StructBuilder> with the expected generic types, because under the hood
65
+
/// // the StructBuilder was returned as a Box<dyn ArrayBuilder> and passed as such to the ListBuilder constructor
66
+
///
67
+
/// // This panics in runtime, even though we know that the builder is a ListBuilder<StructBuilder>.
0 commit comments