-
I'm building logics to handle complex types, here I can construct an array of struct of struct like: #[test]
fn test_struct_array_of_struct() {
let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
let int = Arc::new(Int64Array::from(vec![42, 28, 19, 31]));
let object = Arc::new(StructArray::from(vec![
(
Arc::new(Field::new("x", DataType::Boolean, false)),
boolean.clone() as ArrayRef,
),
(
Arc::new(Field::new("y", DataType::Int64, false)),
int.clone() as ArrayRef,
),
]));
let struct_array = StructArray::from(vec![
(
Arc::new(Field::new(
"a",
DataType::Struct(Fields::from(vec![
Field::new("x", DataType::Boolean, false),
Field::new("y", DataType::Int64, false),
])),
false,
)),
object.clone() as ArrayRef,
),
(
Arc::new(Field::new("b", DataType::Boolean, false)),
boolean.clone() as ArrayRef,
),
(
Arc::new(Field::new("c", DataType::Int64, false)),
int.clone() as ArrayRef,
),
]);
dbg!(struct_array);
} But failed to construct an array of list of list, or an array of list of struct. |
Beta Was this translation helpful? Give feedback.
Answered by
tisonkun
Oct 26, 2024
Replies: 2 comments
-
Found: use arrow::array::{Int32Builder, Int64Builder, StructBuilder};
use arrow::array::ListBuilder;
use arrow::datatypes::DataType;
use arrow::datatypes::Field;
use arrow::datatypes::Fields;
use super::*;
#[test]
fn test_list_array_of_list() {
let mut builder = ListBuilder::new(ListBuilder::new(Int64Builder::new()));
builder.values().append_value([Some(1), None, Some(3)]);
builder.values().append_null();
builder.append(true);
builder.values().append_value([Some(4)]);
builder.values().append_value([]);
builder.append(true);
builder.values().append_value([Some(5), Some(6)]);
builder.values().append_value([None, None]);
builder.append(true);
builder.values().append_null();
builder.values().append_null();
builder.append(false);
let list_array = builder.finish();
dbg!(list_to_vector(&list_array).unwrap());
}
#[test]
fn test_list_array_of_struct() {
// define schema
let int_field = Field::new("a", DataType::Int32, true);
let fields = Fields::from([Arc::new(int_field)]);
let item_field = Field::new("item", DataType::Struct(fields.clone()), true);
let list_field = Field::new("list", DataType::List(Arc::new(item_field)), true);
let int_builder = Int32Builder::with_capacity(10);
let struct_builder = StructBuilder::new(fields, vec![Box::new(int_builder)]);
let mut list_builder = ListBuilder::new(struct_builder);
// [{a: 1}], [], null, [null, null], [{a: null}], [{a: 2}]
//
// [{a: 1}]
let values = list_builder.values();
values
.field_builder::<Int32Builder>(0)
.unwrap()
.append_value(1);
values.append(true);
list_builder.append(true);
// []
list_builder.append(true);
// null
list_builder.append(false);
// [null, null]
let values = list_builder.values();
values
.field_builder::<Int32Builder>(0)
.unwrap()
.append_null();
values.append(false);
values
.field_builder::<Int32Builder>(0)
.unwrap()
.append_null();
values.append(false);
list_builder.append(true);
// [{a: null}]
let values = list_builder.values();
values
.field_builder::<Int32Builder>(0)
.unwrap()
.append_null();
values.append(true);
list_builder.append(true);
// [{a: 2}]
let values = list_builder.values();
values
.field_builder::<Int32Builder>(0)
.unwrap()
.append_value(2);
values.append(true);
list_builder.append(true);
let list_array = list_builder.finish();
dbg!(list_to_vector(&list_array).unwrap());
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
tisonkun
-
Thanks @tisonkun -- should we perhaps add this as an example to the arrow-rs documentation? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found: