jorgecarleitao / arrow2

Transmute-free Rust library to work with the Arrow format
Apache License 2.0
1.07k stars 223 forks source link

Create a ListArray from a Vec<String> #1466

Closed glennpierce closed 1 year ago

glennpierce commented 1 year ago

Hi is there a simple example of creating an ListArray from a vector of strings ?

Best I have is

    let s: Vec<Option<String>> = vec![Some("test1".to_string()), Some("test2".to_string())];
    let utf8_array = Box::new(Utf8Array::<i64>::from(&s));
    let data = ArrayRef::from(utf8_array);

    let mut offsets: Vec<i32> = Vec::with_capacity(key_value_strings.len());
    for i in 0..key_value_strings.len() {
        offsets.push(i as i32);
    }                                                                                            

   unsafe {
       let buffer: Buffer<i32> = offsets.into();
       let offsets_buffer = OffsetsBuffer::new_unchecked(buffer);
       Box::new(ListArray::try_new(DataType::List(Box::new(DataType::Utf8)), offsets_buffer, data, None).unwrap())
   }

the last line

 Box::new(ListArray::try_new(DataType::List(Box::new(DataType::Utf8)), offsets_buffer, data, None).unwrap())

is wrong but because I get

panicked at 'called Result::unwrap() on an Err value: OutOfSpec("ListArray expects DataType::List")'

What do I need to put here. PS I am not sure this is how to create a ListArray from Strings. I hate the unsafe part but I am running out of ideas.

Thanks

glennpierce commented 1 year ago

I managed to solve it. Main problem was finding documentation for the offsets param. Had to turn to GPT-4 for help :)