Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc example of constructing a MapArray #4385

Closed
alamb opened this issue Jun 8, 2023 · 1 comment · Fixed by #4382
Closed

Add doc example of constructing a MapArray #4385

alamb opened this issue Jun 8, 2023 · 1 comment · Fixed by #4382
Labels
arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog

Comments

@alamb
Copy link
Contributor

alamb commented Jun 8, 2023

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
People on a discord channel were recently confused about creating MapArrays

The current documentation https://docs.rs/arrow/41.0.0/arrow/array/struct.MapArray.html does not give an example of how to create MapArrays

Describe the solution you'd like
A clear example of how to construct a MapArray linked from the MapArray docs

Describe alternatives you've considered

Additional context
@tustvold suggests "using the builder" which I think means https://docs.rs/arrow/41.0.0/arrow/array/struct.MapBuilder.html

Here is the example code for constructing it:

fn build_map_array(list: Vec<HashMap<String, proto::Value>>) -> Result<Arc<MapArray>> {
    let mut entry_offset = vec![];
    let mut keys = vec![];
    let mut values = vec![];

    let mut offset = 0;
    for kv in list {
        entry_offset.push(offset as u64);
        offset += kv.len();

        for (key, value) in kv {
            keys.push(key);
            values.push(format!("{}:{}", value.type_name(), value));
        }
    }
    println!("offset list: {:?}", entry_offset);

    let map_data = ArrayDataBuilder::new(datatype_map())
        .len(3)
        .add_buffer(Buffer::from_vec(entry_offset))
        .add_child_data(
            StructArray::from(vec![
                (
                    Arc::new(Field::new("keys", DataType::Utf8, false)),
                    Arc::new(StringArray::from(keys)) as ArrayRef,
                ),
                (
                    Arc::new(Field::new("values", DataType::Utf8, false)),
                    Arc::new(StringArray::from(values)) as ArrayRef,
                ),
            ])
            .into_data(),
        )
        .build()?;

    Ok(Arc::new(MapArray::from(map_data)))
}

(which I am not sure is right)

@alamb alamb added the enhancement Any new improvement worthy of a entry in the changelog label Jun 8, 2023
tustvold added a commit to tustvold/arrow-rs that referenced this issue Jun 8, 2023
tustvold added a commit that referenced this issue Jun 8, 2023
* Add MapArray constructors

* Clippy

* Review feedback

* Link to builder (#4385)

* Clippy

* Further docs tweaks
@alamb
Copy link
Contributor Author

alamb commented Jun 16, 2023

label_issue.py automatically added labels {'arrow'} from #4382

@alamb alamb added the arrow Changes to the arrow crate label Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant