Bound optional-column reads by the destination length

perfloop-oss/parquet-go · Inefficient Algorithm

https://perfloop.ai/t/oss/case_z0qkh6gq4t

Verdict

VERIFIED · settled 2026-11-24 · merged as parquet-go/parquet-go#763

What happened: The check fails before this change and passes with it.

Hypothesis

Reading one value from a longer optional column can panic on valid data. Applications can obtain this reader through Buffer.ColumnBuffers or GenericBuffer.ColumnBuffers; ColumnBuffer explicitly exposes ValueReaderAt for reads at application-selected positions. This is a correctness defect, not a measured performance claim.

At revision a41c45bf16e816000e1e4e0160109e9baab54e95, optionalColumnBuffer.ReadValuesAt subtracts offset from the full logical length at line 342. When that length exceeds len(values), it never limits length to the destination. It then slices values to length minus the remaining null count at line 350, or addresses positions based on the full length during null expansion. Two non-null rows and a one-value destination therefore cause an out-of-bounds panic. An all-null input can instead report more values than the destination holds.

The bounded native search TestHuntOptionalReadDestinationBoundSearch covered columns of 1–6 rows, all null patterns, offsets 0–N, and destination lengths 1–N+1, stopping at its first counterexample. That counterexample was N=2, both rows present, offset=0, destination length=1. The deterministic replay uses a public GenericBuffer with two *int32 fields containing 11 and 22. `go test . -run '^TestHuntOptionalReadDestinationBoundReplay$' -count=1 -v` failed with exit 1: `slice bounds out of range [:2] with capacity 1`. Logs and search source are retained under $PERFLOOP_PROBE_DIR/optional-read-bound. The attached native replay is a provisional test layout. The next useful Case check is the same bounded matrix with exact values and levels, including zero-length destinations, after correcting the range calculation.

The complete current GitHub open-PR listing contained no change to column_buffer_optional.go. The optional byte-slice work in https://github.com/parquet-go/parquet-go/pull/626 changes typed-write null detection, not random-access read bounds. Prior owner and repair searches found only optional sorting/null-placement and materialization-comparison work, not this read-range repair. CONTRIBUTING.md directs review to GitHub and requires regression tests; no separate review archive is named. The shallow local history shows only the accepted snapshot, so older owner history remains an evidence gap.

Change to test: Limit each optional-column read to the smaller of the destination length and the remaining logical row count before slicing or expanding values. Preserve the requested range, returned count, payloads, and levels for bounded reads.

Where it lives

perfloop-oss/parquet-go

Evidence

The check fails before this change and passes with it: `For optional columns obtained through Buffer.ColumnBuffers, across every null mask of one to six input rows and every in-range offset with destination lengths from zero through one beyond the column length, ReadValuesAt returns exactly the bounded requested values and their levels, reports the number written, leaves the remainder of the destination unchanged, and does not panic.`

The check fails before this change and passes with it: `For repeated columns obtained through Buffer.ColumnBuffers, across every null mask of one to six input rows and every in-range offset with destination lengths from zero through one beyond the column length, ReadValuesAt returns exactly the bounded requested values and their levels, reports the number written, leaves the remainder of the destination unchanged, and does not panic.`

The check fails before this change and passes with it: `For optional columns obtained through GenericBuffer.ColumnBuffers, across every null mask of one to six input rows and every in-range offset with destination lengths from zero through one beyond the column length, ReadValuesAt returns exactly the bounded requested values and their levels, reports the number written, leaves the remainder of the destination unchanged, and does not panic.`

The check fails before this change and passes with it: `For repeated columns obtained through GenericBuffer.ColumnBuffers, across every null mask of one to six input rows and every in-range offset with destination lengths from zero through one beyond the column length, ReadValuesAt returns exactly the bounded requested values and their levels, reports the number written, leaves the remainder of the destination unchanged, and does not panic.`

Checks: 4 of 4 passed. Verification: no defect found.

Timeline