dayan02dev opened a new issue, #12493:
URL: https://github.com/apache/arrow-rs/issues/12493
### Describe the bug
Two problems with `RunEndEncoded` columns in the arrow-avro writer:
1. A sliced `RunEndEncoded` array (any slice with a non-zero offset, at the
top level or as the values of a struct, list or map) is written with the wrong
values. The file is valid, so nothing reports an error. Sliced `Boolean` values
inside a list or map are also written with the wrong values (second cause under
1 below).
2. A nullable `RunEndEncoded` field inside a struct, list, large list,
fixed-size list or map is written with two union branch indexes per value while
the schema declares one union. The write succeeds but the file can't be read
back. Without the `avro_custom_types` feature only; top-level nullable
`RunEndEncoded` columns are fine.
### To Reproduce
Dependencies: `arrow-array`, `arrow-schema`, `arrow-avro` (60.2.0 or main).
```rust
use std::io::Cursor;
use std::sync::Arc;
use arrow_array::types::Int32Type;
use arrow_array::{Array, ArrayRef, Int32Array, RecordBatch, RunArray,
StringArray, StructArray};
use arrow_avro::reader::ReaderBuilder;
use arrow_avro::writer::AvroWriter;
use arrow_schema::Field;
fn round_trip(batch: &RecordBatch) -> Result<RecordBatch, Box<dyn
std::error::Error>> {
let mut writer = AvroWriter::new(Vec::new(),
batch.schema().as_ref().clone())?;
writer.write(batch)?;
writer.finish()?;
let mut reader =
ReaderBuilder::new().build(Cursor::new(writer.into_inner()))?;
Ok(reader.next().unwrap()?)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Logical values: x x y z z z w w
let ree = RunArray::<Int32Type>::try_new(
&Int32Array::from(vec![2, 3, 6, 8]),
&StringArray::from(vec!["x", "y", "z", "w"]),
)?;
// 1. Sliced REE column, expected z z z w
let sliced: ArrayRef = Arc::new(ree.slice(3, 4));
let batch = RecordBatch::try_from_iter_with_nullable([("c", sliced,
false)])?;
println!("{:?}", round_trip(&batch)?.column(0));
// 2. Nullable REE field inside a struct
let fields = vec![Field::new("r", ree.data_type().clone(), true)];
let nested: ArrayRef = Arc::new(StructArray::new(fields.into(),
vec![Arc::new(ree)], None));
let batch = RecordBatch::try_from_iter_with_nullable([("s", nested,
false)])?;
println!("{:?}", round_trip(&batch).map(|b| b.column(0).len()));
Ok(())
}
```
Output:
```
StringArray
[
"x",
"x",
"x",
"x",
]
Err(AvroError("EOF: Unexpected EOF reading bytes"))
```
fastavro reads both files the same way (`['x', 'x', 'x', 'x']`, and an
`IndexError` for the struct file), so the bytes on disk are wrong, not the
reader.
### Expected behavior
The first column reads back as `z, z, z, w`, and the struct column reads
back with its 8 rows.
### Additional context
The `RunEndEncoded` problems affect every release since `RunEndEncoded`
support was added (#8584): 57.0.0 through 60.2.0, and main. The sliced
`Boolean` list/map problem also reproduces on 56.2.1.
Causes, in `arrow-avro/src/writer/encoder.rs` on main:
1. `RunEncodedEncoder` (lines 2331-2375) compares slice-relative row indexes
against the unsliced run ends and reads values by run count from the start of
the slice instead of by physical index, so `run_ends().offset()` is ignored.
Separately, `ListEncoder` and `MapEncoder` (lines 2018/2030 and 1813/1841)
subtract `values().offset()` from list offsets that already index the sliced
child. `offset()` is non-zero only for `RunArray` and `BooleanArray`, so for
those two types the wrong list or map item is written.
2. The writer leaves the union branch of a `RunEndEncoded` site to its
values encoder, but only top-level columns drop the site's own nullability
(lines 886-890); struct, list and map children pass it through to
`FieldEncoder::make_encoder`, so the branch index is written twice.
### Willingness to contribute
I plan to contribute a fix for this bug independently
--
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]