LNSD opened a new issue, #12468:
URL: https://github.com/apache/arrow-rs/issues/12468
**Is your feature request related to a problem or challenge? Please describe
what you are trying to do.**
`MapBuilder` always builds maps with `keys_sorted = false`. `finish_helper`
passes `false` to `MapArray::new_unchecked` and there's no way to change it.
`make_builder` also ignores the flag on `DataType::Map(field, sorted)`.
So a builder made from a schema that has a sorted map field produces a
column that doesn't match its own field. Inside a struct it panics:
```rust
let fields = vec![Field::new_map(
"m",
"entries",
Field::new("keys", DataType::Utf8, false),
Field::new("values", DataType::Int32, true),
true, // keys_sorted
false,
)];
let mut builder = StructBuilder::from_fields(fields, 1);
builder
.field_builder::<MapBuilder<Box<dyn ArrayBuilder>, Box<dyn
ArrayBuilder>>>(0)
.unwrap()
.append(true)
.unwrap();
builder.append(true);
builder.finish();
// panics: Incorrect datatype for StructArray field "m", expected a sorted
Map, got an unsorted one
```
The only workaround today is to finish the map, take it apart with
`into_parts()` and rebuild it with `MapArray::new(.., true)`. That doesn't help
when the map is a child of a `StructBuilder` or `ListBuilder`, because the
parent calls `ArrayBuilder::finish` on the child itself. Downstream crates end
up wrapping `MapBuilder` just to set the flag.
**Describe the solution you'd like**
- Add `MapBuilder::with_keys_sorted(bool)`, next to `with_keys_field` and
`with_values_field`, defaulting to `false`.
- Have `make_builder` pass the flag from `DataType::Map`.
The builder would only set the flag and not check key order, same as
`MapArray::try_new`.
**Describe alternatives you've considered**
Validating the order in `finish` when the flag is set. That would need a
comparison over arbitrary key types and would make `finish` fallible or
panicking for something `MapArray::try_new` doesn't check either, so I'd leave
it out.
**Additional context**
Related: #7100 / #7101 (`with_keys_field`), #11603 (FFI export dropped the
keys sorted flag). I have a branch with the change and tests and can open a PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]