fix(parquet): do not cache an ALP preset built from an empty first page - #11004
fix(parquet): do not cache an ALP preset built from an empty first page#11004Abdallah-Afifi wants to merge 1 commit into
Conversation
When the first data page of a column chunk has no values, build_preset falls back to exponent 0 / factor 0. flush_buffer cached that preset for the whole chunk, so every fractional value in every later page became an exception. Only cache the preset once a page actually has values. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
AndreaBozzo
left a comment
There was a problem hiding this comment.
The change looks clean to me, i'll wait for workflows to run and for someone else to eventually flag something more.
I think the workflows need maintainer approval to start. Also, should I get on the rest of the subtasks related to ALP encoding? I would be happy to work on that. Thank you! |
|
FYI @sdf-jkl |
There was a problem hiding this comment.
Thanks @Abdallah-Afifi, everything is great. Let's polish a little
(also cargo fmt)
| None => { | ||
| let built = build_preset(values); | ||
| let page = encode_page(values, &built, scratch)?; | ||
| let had_values = !values.is_empty(); |
There was a problem hiding this comment.
We can move this check above, and if the page is empty encode without building a preset
| // The first flush builds the preset from the whole buffered page and | ||
| // encodes it in one pass; that also arms streaming for later pages. |
There was a problem hiding this comment.
| // The first nonempty flush builds the preset from the whole buffered | |
| // page and encodes it in one pass; that also arms streaming for later pages. |
We should change the doc comments around the impl to show that only a nonempty flush builds the preset.
| assert!( | ||
| after_empty.len() < values.len() * std::mem::size_of::<f64>(), | ||
| "page after an empty first page ({} bytes) is no smaller than raw f64 ({} bytes); \ | ||
| the same values encoded as the first page take {} bytes", | ||
| after_empty.len(), | ||
| values.len() * std::mem::size_of::<f64>(), | ||
| baseline.len() | ||
| ); | ||
|
|
||
| assert!( | ||
| after_empty.len() <= baseline.len() + baseline.len() / 10, | ||
| "empty first page poisoned the preset: {} bytes vs {} bytes when encoded first ({:.1}x larger)", | ||
| after_empty.len(), | ||
| baseline.len(), | ||
| after_empty.len() as f64 / baseline.len() as f64 | ||
| ); |
There was a problem hiding this comment.
| assert!( | |
| after_empty.len() < values.len() * std::mem::size_of::<f64>(), | |
| "page after an empty first page ({} bytes) is no smaller than raw f64 ({} bytes); \ | |
| the same values encoded as the first page take {} bytes", | |
| after_empty.len(), | |
| values.len() * std::mem::size_of::<f64>(), | |
| baseline.len() | |
| ); | |
| assert!( | |
| after_empty.len() <= baseline.len() + baseline.len() / 10, | |
| "empty first page poisoned the preset: {} bytes vs {} bytes when encoded first ({:.1}x larger)", | |
| after_empty.len(), | |
| baseline.len(), | |
| after_empty.len() as f64 / baseline.len() as f64 | |
| ); | |
| assert_eq!( | |
| after_empty, baseline, | |
| "a leading empty page must not affect encoding of the first nonempty page" | |
| ); |
If the preset is deterministic, why not compare directly to the baseline?
Which issue does this PR close?
Rationale for this change
When a column chunk's first data page has no values,
build_presetreturns the fallback pair (exponent 0, factor 0).flush_buffercached that for the whole chunk, andselect_paramsshort-circuits on a single-candidate preset, so every later page was stuck on an integer scale and every fractional value became an exception.It's reachable from the normal write path, not just the encoder API: the column writer counts levels rather than values, so a first page of all nulls flushes an empty buffer.
Encoding 3000 values of
i * 0.01(raw f64 would be 24,000 bytes):Before the fix the chunk ends up bigger than leaving it unencoded. After, a leading empty page costs only its 7-byte header.
What changes are included in this PR?
flush_bufferonly caches the preset when the page had values.putalready branches on whether the preset is set, so the next page with values builds it.The issue asks only for regression coverage and @alamb suggested the fix as a follow-up — this has both. Happy to split them if you'd rather.
Are these changes tested?
test_empty_first_page_does_not_poison_preset, which fails on main. Fullcargo test -p parquetpasses (1558 tests); fmt and clippy clean.One thing I'd like a second opinion on: with the preset left unset, an all-null first page no longer arms the streaming path, so the second page takes the buffered path. The tests agree that's correct, but it's the part of this I'm least certain about.
Per the AI policy in CONTRIBUTING.md: I used Claude Code to trace the encoder and draft the fix and test. I've reviewed all of it and verified the behaviour myself — the numbers above are measured locally on this branch and on main.