mailmindlin opened a new issue, #12453:
URL: https://github.com/apache/arrow-rs/issues/12453
### Describe the bug
Decoding an IPC message that contains a dense `UnionArray` panics if the
input buffer is not 4-byte aligned with `Memory pointer is not aligned with the
specified scalar type`.
`with_require_alignment = false` documents that misaligned buffers are
copied to aligned ones, and `with_require_alignment = true` that they are an
error. Neither holds for the offsets of a dense union, which are converted with
`ScalarBuffer::<i32>::from` and so never reach
`ArrayDataBuilder::align_buffers`.
This affects everything built on `RecordBatchDecoder`: `StreamDecoder`,
`FileDecoder` and Flight decoding, where the body alignment depends on how the
bytes arrived.
### To Reproduce
```rust
use std::sync::Arc;
use arrow_array::builder::UnionBuilder;
use arrow_array::types::{Float64Type, Int32Type};
use arrow_array::{Array, RecordBatch};
use arrow_buffer::Buffer;
use arrow_ipc::reader::StreamDecoder;
use arrow_ipc::writer::StreamWriter;
use arrow_schema::{Field, Schema};
fn main() {
let union = {
let mut builder = UnionBuilder::new_dense();
builder.append::<Int32Type>("a", 1).unwrap();
builder.append::<Float64Type>("b", 2.5).unwrap();
builder.build().unwrap()
};
let schema = Arc::new(Schema::new(vec![Field::new(
"u",
union.data_type().clone(),
false,
)]));
let batch = RecordBatch::try_new(schema.clone(),
vec![Arc::new(union)]).unwrap();
let mut stream = Vec::new();
let mut writer = StreamWriter::try_new(&mut stream, &schema).unwrap();
writer.write(&batch).unwrap();
writer.finish().unwrap();
drop(writer);
// Place the stream one byte past an aligned address, as can happen for
any
// `Buffer` that was sliced out of a larger allocation (e.g. a network
read)
let mut shifted = vec![0_u8];
shifted.extend_from_slice(&stream);
let mut buffer = Buffer::from(shifted).slice(1);
// panics
let decoded = StreamDecoder::new().decode(&mut buffer).unwrap().unwrap();
assert_eq!(decoded, batch);
}
```
### Expected behavior
The batch decodes, as it does for every other array type. With
`with_require_alignment(true)` it should return the usual `Misaligned
buffers[..]` error instead of panicking.
### Additional context
Present on `main` and 58.4.2 (and I assume everything between). The panic is
currently pinned by a `#[should_panic]` test, `test_misaligned_dense_union` in
`arrow-flight/tests/encode_decode.rs`.
A related panic with the same message: `with_require_alignment(true)`
combined with `with_skip_validation(true)` panics on a misaligned buffer of any
type with a multi-byte buffer (e.g. `Int32`). The alignment check is part of
the validation that is skipped, so the array constructor's assertion fires
instead. Alignment depends on where the caller's buffer is allocated rather
than on the validity of the encoded data, so I'd expect the same `Misaligned
buffers[..]` error.
### 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]