You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "zeroshade (via GitHub)" <gi...@apache.org> on 2026/08/28 20:45:08 UTC
[PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
zeroshade opened a new pull request, #1256:
URL: https://github.com/apache/arrow-go/pull/1256
### Rationale for this change
Fixes #1255.
A `BYTE_STREAM_SPLIT` `FIXED_LEN_BYTE_ARRAY` column that contains nulls **and** spans more than one data page decodes values shifted by one position — silently, with no error returned.
`spacedExpand` moves values into their spaced positions with `copy` and deliberately does not clean up the null slots:
```go
// because we technically don't care what is in the null slots we don't actually have to clean
// up after ourselves ... Any data that happens to be left in the null slots is fine
```
That reasoning holds for the scalar column types, whose buffers hold values. But `ByteArray` / `FixedLenByteArray` buffers hold **slice headers**, so `copy` leaves *duplicate headers* behind: after expansion a null slot and a valid slot can reference the same backing array.
That is still harmless for a decoder that *replaces* the header, which is what `PlainFixedLenByteArrayDecoder` does:
```go
out[idx] = pflba.data[:pflba.typeLen] // replaces
```
`ByteStreamSplitFixedLenByteArrayDecoder` instead writes **through** the caller's existing slice:
```go
out[idx] = out[idx][:dec.typeLen] // reuses caller storage
...
out[element][stream] = data[encLoc] // writes through it
```
So once `flbaRecordReader` reused its value buffer for the next page, two output slots shared one backing array and clobbered each other. This explains the full shape of the bug: BYTE_STREAM_SPLIT only, nulls required (to create the duplicates), and two or more pages required (the first page creates the aliases, the second decodes into them).
### What changes are included in this PR?
- Add `spacedExpandSwap`, which swaps instead of copying so the buffer remains a permutation of its original elements — no slot aliases another, and every slot keeps its reusable capacity.
- Use it from `ByteStreamSplitFixedLenByteArrayDecoder.DecodeSpaced`.
- Leave `spacedExpand` itself untouched, so every other column type and the non-spaced path are unaffected.
- Add a decoder-level regression test (two pages through one reused buffer, widths 2/3/4/7/8/16) and a randomized differential test asserting `spacedExpandSwap` places values in exactly the same slots as `spacedExpand` while never leaving duplicates.
- Add a `pqarrow` round-trip test over a multi-page, nullable BSS FLBA column — the integration-level case that was returning wrong data.
The decoder-level regression test fails at every width without the fix.
### Why swap rather than the simpler alternatives?
I measured two other approaches and rejected both:
**Making `spacedExpand` itself swap** is correct but replaces `memmove` with element-wise swaps for *every* column type, which is far too expensive on sparse-null runs:
```
SpacedExpandInt64/n65536/nullEvery0 4.457µ -> 81.265µ +1723%
SpacedExpandFLBA/n65536/nullEvery0 3.969µ -> 173.959µ +4283%
```
**`clear(out[:toRead])` in `DecodeSpaced`** is a one-liner, but discards all reusable headers. On 4096 slots / 3511 values / width 16 that is 3511 allocations per `DecodeSpaced` on `main` today (it would drop to 1 once #1172 lands, but the fix should not depend on that).
Swapping keeps the reuse. Cost on the affected path only (`BenchmarkBSSFLBADecodeSpaced`, 8192 slots):
```
w4/nullEvery100 41.64µ -> 60.55µ +45%
w4/nullEvery7 49.45µ -> 71.95µ +46%
w4/nullEvery2 95.83µ -> 89.59µ -7%
w16/nullEvery100 213.9µ -> 385.2µ +80%
w16/nullEvery7 244.7µ -> 251.3µ ~
w16/nullEvery2 154.3µ -> 160.4µ ~
geomean +25%
```
Steady-state allocations stay at 1–2 per call. A ~25% geomean cost on a path that is currently returning **incorrect data** seemed clearly worth it, and nothing outside BSS FLBA spaced decoding is touched.
### Are these changes tested?
Yes — new tests described above. `parquet/...` passes in full with `PARQUET_TEST_DATA` supplied, and `-race` is clean on `parquet/internal/encoding`.
### Are there any user-facing changes?
Yes: BYTE_STREAM_SPLIT FIXED_LEN_BYTE_ARRAY columns with nulls spanning multiple data pages now decode correctly. Previously affected reads returned silently incorrect values.
**This PR contains a "Critical Fix".** Reading an affected file produced wrong values with no error, which could have been persisted or acted on downstream without any indication of a problem.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5511973049
@fallintoplace confirmed, and thank you — you were right, and the underlying problem is broader than either of the two cases you named. Pushed 09060510.
I wrote your `RLE_DICTIONARY -> BYTE_STREAM_SPLIT` repro as a test first. It fails on the previous commit exactly as you predicted, at every width:
```
--- FAIL: .../width=2/after_RLE_DICTIONARY
expected: parquet.FixedLenByteArray{0x64, 0x65}
actual : parquet.FixedLenByteArray{0x66, 0x67}
Messages: slot 0 decoded incorrectly
```
`dictConverter.Copy` does `o[idx] = dc.dict[val]`, and `Fill` copies one header across an RLE run, so repeated indices leave several slots sharing one dictionary-backed slice. And as you said, no nulls needed.
While writing it I checked `PLAIN -> BYTE_STREAM_SPLIT` again and found my previous commit had only papered over it. `spacedExpandSwap` removes *duplicate* headers, but PLAIN's `Decode` does `out[idx] = pflba.data[:typeLen]`, so every slot points into the page buffer even with no duplicates at all. BSS then overwrites that buffer wholesale:
```
--- FAIL: .../width=2/after_PLAIN
expected: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5}
actual : []byte{0x64, 0x65, 0x66, 0x67, 0x68, 0x69}
Messages: decoding wrote through into the PLAIN page buffer
```
So the root cause isn't aliasing specifically — it's that a decoder which writes *through* caller headers was claiming them on capacity alone, while the value buffer is shared across pages and encodings. Capacity can't distinguish our storage from a dictionary entry or a page buffer.
Reuse is now gated on provenance: a header is only written through if it falls inside the block this decoder allocated; anything else is re-pointed at a fresh block.
### This narrows your GH-1172 contract, so please push back if you disagree
`TestByteStreamSplitFixedLenByteArrayDecoderReusesProvidedOutput` asserted that a caller-provided buffer with capacity gets filled in place. That is precisely the unsafe behavior, and I could not find a way to keep it — the record reader hands us the same `valueBuf` whether the headers are ours or were left by the previous page's decoder. I replaced it with two tests: one pinning that we do *not* write through caller buffers, one pinning that we still reuse our own storage.
Allocation is unchanged where it matters. Preparing only the decoded prefix regressed `DecodeSpaced` to 1 alloc/op, because the expansion permutes headers across the whole window and unowned ones migrate into the prefix. Preparing the whole window once, null slots included, restores it:
| | allocs/op | correct? |
| --- | --- | --- |
| before this commit | 0 | **no** |
| prefix-only preparation | 1 | yes |
| this commit | **0** | yes |
(4096 slots / 3009 values / width 16, steady state.) The dense `Decode` path is untouched — successive windows still allocate one block each, as before, and earlier windows keep their values.
`parquet/...` and `arrow/...` pass in full, `-race` is clean on `encoding`, `file` and `pqarrow`, and `golangci-lint` reports 0 issues. Both new tests fail without the fix.
One thing I'd like your read on: `owns` compares `unsafe.SliceData` pointers against the block bounds. It's the honest expression of "did I allocate this", and `go vet` is clean, but if you'd rather not have pointer-provenance in this path I'm open to alternatives — the only other one I found that is clearly correct costs an allocation per page.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade merged PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5484650546
@fallintoplace I believe I've addressed your comment, it's a good point. Let me know what you think
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "fallintoplace (via GitHub)" <gi...@apache.org>.
fallintoplace commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5484830495
I think there may still be a similar issue with `RLE_DICTIONARY -> BYTE_STREAM_SPLIT`.
Possible repro:
1. Decode an FLBA dictionary page with repeated values, e.g. indexes `[0, 0, 1]`. The first two entries in `valueBuf` can point to the same dictionary-backed slice.
2. Reuse the same `valueBuf` for a BYTE_STREAM_SPLIT page with distinct values `[A, B, C]`.
3. BSS writes through the existing slices before `spacedExpandSwap` runs, so writing `B` can overwrite `A` because the first two entries alias.
This one shouldn't need nulls. Could we add a `RLE_DICTIONARY -> BYTE_STREAM_SPLIT` regression test too?
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5457836107
Checked how this composes with #1172, since both touch `fixed_len_byte_array_decoder.go`.
They merge cleanly with no conflict (`prepareOutput` and `spacedExpandSwap` are independent), and the merged result passes `parquet/internal/encoding`, `parquet/file`, and `parquet/pqarrow` in full, including both PRs' own test sets.
Allocation behaviour for `DecodeSpaced` on 4096 slots / 3511 values / width 16:
| | allocs/op | correct? |
| --- | --- | --- |
| `main` today | 0 | **no** |
| `main` + this PR | 16 | yes |
| #1172 + this PR | **0** | yes |
So the two are complementary rather than competing: #1172's contiguous cold-output block absorbs the entries that need storage, and `spacedExpandSwap` preserves every header so the steady state needs no allocation at all. Either merge order works.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "fallintoplace (via GitHub)" <gi...@apache.org>.
fallintoplace commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5464472096
I would request changes here because I think there is still one case this doesn't handle.
I tried `PLAIN` nullable FLBA first, then `BYTE_STREAM_SPLIT` FLBA on the next page, reusing the same `valueBuf`.
After the PLAIN page, `spacedExpand` can leave two entries pointing to the same slice. Then BSS uses that same buffer and writes into the slices before `spacedExpandSwap` happens.
So something like `[P0, P1, P1, _, P2]` can be left from the first page. Then BSS decodes into the first 3 entries, and the second and third values can both write into `P1`.
So I think one value can already be overwritten before `spacedExpandSwap` gets a chance to fix the layout.
Maybe worth adding a `PLAIN -> BYTE_STREAM_SPLIT` multi-page test for this case.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5457879484
Rebased onto current `main`, which now carries both #1172 and #1220.
#1220 added an early return to `spacedExpand` once the decoded prefix is already aligned. I mirrored it in `spacedExpandSwap` — at that point every remaining swap is a self-swap, so the same reasoning applies and the two functions stay consistent:
```go
idxDecode -= run.Length
// Once the decoded prefix is already aligned every remaining swap is a
// self-swap, so there is nothing left to do. Mirrors spacedExpand.
if idxDecode == run.Pos {
return numValues
}
```
Re-verified on the new base: the randomized differential test now compares against the early-returning `spacedExpand` and still agrees at every valid position while never leaving duplicates; the decoder-level regression test still fails at all six widths without the fix, and the `pqarrow` round-trip still fails without it. `parquet/...` passes in full and `golangci-lint` reports 0 issues.
The earlier measurement stands: with #1172 now on `main`, `DecodeSpaced` is 0 allocs/op in steady state with this fix applied.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(parquet): stop BYTE_STREAM_SPLIT FLBA spaced decode aliasing output [arrow-go]
Posted by "fallintoplace (via GitHub)" <gi...@apache.org>.
fallintoplace commented on PR #1256:
URL: https://github.com/apache/arrow-go/pull/1256#issuecomment-5516380665
Everything passed on my side.
No further changes from me. Thanks for following this through
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org