Small residuals, fixed-width arithmetic, and bit packing
Derive delta residual widths, handle specified wraparound correctly, and distinguish a teaching sequence from a valid format block.
On this page
The question for today
What you’ll understand
- Calculate deltas, a block minimum, and nonnegative residual widths.
- Explain why fixed-width overflow semantics belong to the format contract.
- Distinguish encoding blocks and miniblocks from pages, row groups, and generic compression.
A monotonically increasing integer sequence can contain large absolute values and tiny differences. Delta encoding changes the representation so the smaller quantity becomes the one stored repeatedly. Bit packing then removes unused high bits. These transformations are lossless only when arithmetic, block boundaries, padding, and value counts are defined precisely. A mathematically plausible decoder can still be incompatible if its programming language treats overflow differently from the file format.
Subtract a predictable part before packing
For a sequence of integers, preserve the first value and form differences between consecutive values. A block can then choose its minimum delta as a reference and store nonnegative residuals relative to that minimum. If every delta equals the same step, all residuals are zero, requiring no residual payload bits in the ideal case. The step and framing still require representation. This is different from dictionary encoding: a steadily increasing timestamp column may have almost every absolute value distinct while its deltas remain highly predictable.
Parquet DELTA_BINARY_PACKED supports its specified INT32 and INT64 streams and uses blocks subdivided into miniblocks with their own bit widths. The block header, reference deltas, and counts establish how values are reconstructed. Actual block sizes have format constraints, so a four-number classroom example is an arithmetic illustration, not a literal valid tiny Parquet block. A reader must stop at the declared value count rather than expose padding as real rows. Byte-array delta-length and prefix encodings are related but have different layouts and supported types; their names do not make them interchangeable.
Overflow behavior is part of lossless decoding
Fixed-width integers complicate the exact-arithmetic derivation. Parquet specifies two's-complement wrapping behavior for the relevant delta subtractions and inverse additions when overflow occurs. Implementations must arrange that behavior deliberately in languages where signed overflow is undefined or where integers have unbounded precision. An unsigned-domain implementation can preserve bit patterns, but must still apply the required signed interpretation at the correct stages. Simply enabling a compiler optimization and hoping signed subtraction wraps is not a portable encoding strategy.
An outlier can increase the width needed for its miniblock without forcing every value in the entire file to use that width. That adaptability trades compactness against headers, decoding branches, vectorization opportunities, and implementation complexity. Original integer-compression research shows why decoder throughput matters alongside compression ratio; do not copy historical performance numbers to a different CPU or distribution. Test boundaries near minimum and maximum representable values, negative steps, constant deltas, short final blocks, and changing miniblock widths. Round-trip tests should check exact bit patterns and declared row counts.
Validate a representation rather than guessing from sorted data
Sorting often reduces changes between neighbors, but delta encoding can still help unsorted data when differences have a narrow local distribution. Conversely, a monotone sequence with wildly varying gaps may compress less effectively than expected. Inspect actual encoded pages and decode cost with representative gaps, nulls, and groups. Nulls are carried through structural information rather than invented integer values in the nonnull stream. Preserve the relationship between that stream and the reconstructed rows; a decoder that produces the right integers at wrong row positions is still incorrect.
The modular lab isolates wrapping with a small visible modulus. Reset, set both modulus m=16 and additive step a=3, predict residue zero initially and residue three after one Step, then inspect it. Each Step advances half a second and one addition because n=floor(2t). Further steps produce six and nine before wrapping eventually. The sixteen-element cycle is a four-bit arithmetic analogy, not a DELTA_BINARY_PACKED encoder; the lab has no minimum-delta selection, miniblocks, page headers, or INT32 overflow test. Changing controls retains elapsed time, so Reset before a separate comparison.
Work it through
Pack the residuals of a nearly regular sequence
For the teaching sequence 1,000, 1,003, 1,007, 1,010, compute deltas and residuals. Estimate their ideal residual bit count, explicitly excluding actual Parquet block framing and padding.
The three deltas are 3, 4, and 3 in the column's units. Their minimum is three.
Subtract that minimum to obtain residuals zero, one, zero. The maximum residual is one, so one bit per residual suffices: three payload bits in this teaching calculation.
Reconstruct from the first value: 1,000 + 3 + 0 = 1,003; then add 3 + 1 to reach 1,007; then add 3 + 0 to reach 1,010. A real encoded block also stores framing, the first value, reference information, and format-constrained padding.
Small residuals explain the opportunity, while three bits alone are not a complete encoded representation or a valid four-value Parquet block layout.
The living notebook
Explore the model
Interactive lab · modular
Inspect wraparound without undefined arithmetic
Reset, set both m=16 and additive step a=3, predict r0=0 and r1=3, then Step once. Each half-second Step advances one addition; Play advances two per second. Reset and re-enter both controls before another case because edits retain elapsed time. This modular-addition model is not a Parquet delta encoder or a signed-overflow checker.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forLossless reconstruction depends on explicitly specified arithmetic and framing, not just on recognizing a small difference.
Pause & reason
Check your understanding
Your turn
Try it yourself
A teaching sequence is 50, 55, 61, 66, 74. Find its deltas, minimum delta, residuals, and ideal fixed width for those residuals before headers or padding.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Subtract neighbors, then subtract the smallest delta from every delta.
Compare with the solution
Deltas are 5, 6, 5, and 8. The minimum is five, giving residuals zero, one, zero, and three. Two bits represent residual values zero through three, so the ideal four-residual payload is eight bits. This is only the arithmetic component, not the complete byte count of a valid format block.
Keep the idea
Take it with you
- Delta references expose small residuals that can be packed with few bits.
- Fixed-width overflow and padding rules are required for exact interoperable reconstruction.
- Compression ratio, decoder throughput, and row/structural alignment need separate verification.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.