Parquet content-defined chunking hashes values it does not write for some list columns, and panics on list views

Aperta
#12,391 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Mola-maker ci sta già lavorando.

Dal 22/11/2026.

  • #12394 di @Mola-maker — chiusa senza merge

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
22/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Start with the nested branch of ContentDefinedChunker::calculate in parquet/src/column/chunker/cdc.rs, where the hash reads values[leaf_offset], and with ArrayLevels::slice_for_chunk and non_null_indices in parquet/src/arrow/arrow_writer/levels.rs. Run the four reproduction tests in the issue (sliced_list, fixed_size_list_with_null_rows, list_view_in_reverse, overlapping_list_views) in a debug build. Done when the two page-boundary tests produce identical pages for equal data, and the two list-view tests no longer panic.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

With content-defined chunking enabled, ArrowWriter chooses the page boundaries of a column with repetition levels by hashing values[leaf_offset], where leaf_offset counts the column's leaf levels from 0. This happens in the nested branch of ContentDefinedChunker::calculate, in parquet/src/column/chunker/cdc.rs, where #9644 added leaf_offset. It assumes that the written values are the first slots of the leaf array, in order, which is not true for:

  • lists and maps whose offsets don't start at 0, such as a sliced ListArray, or the parts of a batch that ArrowWriter::write splits between row groups
  • the child slots of the null rows of a FixedSizeListArray, and of null list entries that own child values, which have no levels
  • ListViewArrays, whose offsets can go backwards or overlap

So page boundaries depend on values that are not written, and the same data can be written with different pages. List views can also panic. When offsets go backwards, ArrayLevels::slice_for_chunk subtracts the first index of a chunk from smaller ones. When views overlap, hashing reads past the end of the values.

To Reproduce

As an integration test of the parquet crate:

use std::sync::Arc;

use arrow_array::types::Int32Type;
use arrow_array::{
    Array, ArrayRef, FixedSizeListArray, Int32Array, ListArray, ListViewArray, RecordBatch,
    UInt32Array,
};
use arrow_buffer::{NullBuffer, ScalarBuffer};
use arrow_schema::{DataType, Field};
use arrow_select::take::take;
use bytes::Bytes;
use parquet::arrow::ArrowWriter;
use parquet::file::properties::{CdcOptions, WriterProperties};
use parquet::file::reader::{FileReader, SerializedFileReader};
use parquet::file::serialized_reader::ReadOptionsBuilder;

/// Writes `array` with content-defined chunking, and returns the first row of each page
fn page_starts(array: ArrayRef) -> Vec<i64> {
    let batch = RecordBatch::try_from_iter([("c", array)]).unwrap();
    let cdc = CdcOptions {
        min_chunk_size: 256,
        max_chunk_size: 1024,
        norm_level: 0,
    };
    let props = WriterProperties::builder()
        .set_content_defined_chunking(Some(cdc))
        .build();
    let mut buffer = Vec::new();
    let mut writer = ArrowWriter::try_new(&mut buffer, batch.schema(), Some(props)).unwrap();
    writer.write(&batch).unwrap();
    writer.close().unwrap();
    let options = ReadOptionsBuilder::new().with_page_index().build();
    let reader = SerializedFileReader::new_with_options(Bytes::from(buffer), options).unwrap();
    let pages = reader.metadata().page_index().unwrap().page_locations(0, 0).unwrap();
    pages.iter().map(|page| page.first_row_index).collect()
}

fn values(n: i32) -> impl Iterator<Item = Option<Vec<Option<i32>>>> {
    (0..n).map(|i| Some(vec![Some(i), (i % 7 != 0).then_some(i * 3)]))
}

#[test]
fn sliced_list() {
    let list = ListArray::from_iter_primitive::<Int32Type, _, _>(values(10_000));
    let sliced = list.slice(1000, 5000);
    // The same rows, with offsets that start at 0
    let copy = take(&sliced, &UInt32Array::from_iter_values(0..5000), None).unwrap();
    assert_eq!(sliced.to_data(), copy.to_data());
    assert_eq!(page_starts(Arc::new(sliced)), page_starts(copy));
}

#[test]
fn fixed_size_list_with_null_rows() {
    let item = Arc::new(Field::new_list_field(DataType::Int32, true));
    let nulls = NullBuffer::from_iter((0..10_000).map(|row| row % 3 != 0));
    // The same valid rows, and different values in the slots of null rows
    let list = |null_slot: i32| {
        let values = (0..20_000).map(|v| match v / 2 % 3 {
            0 => Some(null_slot + v),
            _ => (v % 5 != 0).then_some(v),
        });
        let values = Arc::new(Int32Array::from_iter(values));
        FixedSizeListArray::new(item.clone(), 2, values, Some(nulls.clone()))
    };
    let (a, b) = (list(0), list(1_000_000));
    assert_eq!(a.to_data(), b.to_data());
    assert_eq!(page_starts(Arc::new(a)), page_starts(Arc::new(b)));
}

#[test]
fn list_view_in_reverse() {
    let list = ListArray::from_iter_primitive::<Int32Type, _, _>(values(10_000));
    let item = Arc::new(Field::new_list_field(DataType::Int32, true));
    let offsets = ScalarBuffer::from_iter((0..10_000).rev().map(|row| row * 2));
    let sizes = ScalarBuffer::from_iter((0..10_000).map(|_| 2));
    let view = ListViewArray::new(item, offsets, sizes, list.values().clone(), None);
    page_starts(Arc::new(view));
}

#[test]
fn overlapping_list_views() {
    let item = Arc::new(Field::new_list_field(DataType::Int32, true));
    let offsets = ScalarBuffer::from_iter((0..10_000).map(|_| 0));
    let sizes = ScalarBuffer::from_iter((0..10_000).map(|_| 2));
    let values = Arc::new(Int32Array::from(vec![Some(1), None]));
    let view = ListViewArray::new(item, offsets, sizes, values, None);
    page_starts(Arc::new(view));
}

On main (17444b900), in a debug build:

  • sliced_list and fixed_size_list_with_null_rows fail, as the pages start at different rows.
  • list_view_in_reverse panics at parquet/src/arrow/arrow_writer/levels.rs:1285:54 with attempt to subtract with overflow.
  • overlapping_list_views panics at parquet/src/column/chunker/cdc.rs:488:44 with range end index 12 out of range for slice of length 8.
Expected behavior

Page boundaries depend only on the values written, so all four tests pass.

Additional context

This came up in the review of #12350. For ArrowWriter::write_with_selection, that PR hashes the values at the indices the writer writes (ArrayLevels::non_null_indices) rather than at leaf_offset. write could do the same, although that would not fix the panic in slice_for_chunk.

Willingness to contribute

No response

Lingua principale
Rust
Stelle
3.7k
Fork
1.5k
Merge medio
2g 16h
PR unite (30g)
183

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/arrow-rs

Tutte le issue di apache/arrow-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.