This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 65878110 fix(parquet/internal/encoding): keep whole numbers past 2^22
out of the ALP exceptions (#1555)
65878110 is described below
commit 658781105c84297c0fa415f22b7c4d8d16e2ad9f
Author: Matt Topol <[email protected]>
AuthorDate: Mon Nov 16 12:54:21 2026 -0500
fix(parquet/internal/encoding): keep whole numbers past 2^22 out of the ALP
exceptions (#1555)
### Rationale for this change
The ALP encoder rounds a scaled value to an integer by adding and
subtracting a constant, 2^23 + 2^22 for `FLOAT` and 2^52 + 2^51 for
`DOUBLE`. That is only correct below 2^22 and 2^51. Past those, the sum
has no bit left for the units, and what comes back is a different
integer. The integer then fails the round trip, and the value is stored
as an exception.
A float32 holds every whole number up to 2^24, so much of what a `FLOAT`
column can hold exactly became exceptions. A column of whole numbers
below 2^24 came out larger than `PLAIN`.
Bits per value for a page of 131,072 values:
| Data | Before | After |
|---|---|---|
| `FLOAT`, whole numbers below 2^24 | 41.3 | 24.1 |
| `FLOAT`, 0 to 1,000,000 with one decimal | 41.9 | 33.8 |
| `FLOAT`, whole numbers below 2^22 | 22.1 | 22.1 |
| `FLOAT`, 0 to 10,000 with two decimals | 32.3 | 32.3 |
| `DOUBLE`, whole numbers below 2^53 | 74.4 | 53.1 |
The format leaves the rounding to the encoder, since every integer is
checked against the decoding it specifies. Pages written before this
change read as before, and so do the pages written after it.
### What changes are included in this PR?
`alpScaling.round` uses `math.RoundToEven`.
Rounding can no longer carry a value past the limits `encode` checks,
which are whole numbers. The check that a `FLOAT` integer fits int32
could therefore never fail, and is removed with the fields it used.
On an Apple M4 the existing encoding benchmarks take the same time as
before, within noise. I have no amd64 timings. Without `GOAMD64=v2` or
later, `math.RoundToEven` checks for SSE4.1 at run time before it uses
the instruction.
The scaling loops in #1553 reproduce the rounding as it is on `main`.
Whichever of the two is merged second needs the same one-line change
there.
### Are these changes tested?
Yes.
### Are there any user-facing changes?
`FLOAT` and `DOUBLE` columns with values past 2^22 and 2^51 once scaled
compress better.
---
parquet/internal/encoding/alp_encoding_test.go | 48 ++++++++++++++++++++----
parquet/internal/encoding/alp_scaling.go | 51 ++++++--------------------
parquet/internal/encoding/alp_scaling_test.go | 34 ++++++++++++++++-
3 files changed, 86 insertions(+), 47 deletions(-)
diff --git a/parquet/internal/encoding/alp_encoding_test.go
b/parquet/internal/encoding/alp_encoding_test.go
index d42a1beb..8c77fef5 100644
--- a/parquet/internal/encoding/alp_encoding_test.go
+++ b/parquet/internal/encoding/alp_encoding_test.go
@@ -1255,19 +1255,53 @@ func TestALPEncoderSetVectorSizeRejects(t *testing.T) {
require.NoError(t, enc.SetVectorSize(32), "a flushed page leaves
nothing buffered")
}
-// A FLOAT value scaled to just under 2^31 can round up to 2^31, which a vector
-// cannot hold: readers rebuild its integers in int32 and would flip the sign.
-func TestALPFloat32RoundingPastInt32IsAnException(t *testing.T) {
+// The largest FLOAT value a scaling keeps lands on the limit, which is a whole
+// number inside int32: readers rebuild a vector's integers in int32, so one
+// past it would flip the sign.
+func TestALPFloat32ScalesUpToTheInt32Limit(t *testing.T) {
s := newALPScaling[float32](9, 6)
- _, ok := s.tryEncode(2147483.5)
- assert.False(t, ok, "2147483.5 at (9,6) rounds to 2^31")
+ encoded, ok := s.tryEncode(2147483.5)
+ assert.True(t, ok)
+ assert.LessOrEqual(t, encoded, int64(math.MaxInt32))
+ encoded, ok = s.tryEncode(-2147483.5)
+ assert.True(t, ok)
+ assert.GreaterOrEqual(t, encoded, int64(math.MinInt32))
alpRoundTrip(t, []float32{-24.875, 2147483.5, 12})
alpRoundTrip(t, []float32{-24.875, -2147483.5, 12})
}
-// Page fields near the int32 limits must be rejected, not overflow int on
-// 32-bit platforms.
+// Every whole number a float32 holds exactly, up to 2^24, is an integer a
+// vector can store, so a column of them has no exceptions.
+func TestALPFloat32WholeNumbersAreNotExceptions(t *testing.T) {
+ values := make([]float32, 4*alpDefaultVectorSize)
+ for i := range values {
+ // Spread over the whole range, odd and even.
+ values[i] = float32((i * 4099) % (1 << 24))
+ if i%2 == 1 {
+ values[i] = -values[i]
+ }
+ }
+
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+ enc := newALPEncoder[float32](format.Encoding_ALP, newFloat32Column(),
mem)
+ defer enc.Release()
+ enc.Put(values)
+ buf, err := enc.FlushValues()
+ require.NoError(t, err)
+ defer buf.Release()
+
+ page := buf.Bytes()
+ for v := range len(values) / alpDefaultVectorSize {
+ vectorPos := alpHeaderSize +
int(binary.LittleEndian.Uint32(page[alpHeaderSize+4*v:]))
+ assert.Zero(t, binary.LittleEndian.Uint16(page[vectorPos+2:]),
"vector %d exception count", v)
+ }
+ assert.Less(t, len(page), 4*len(values), "smaller than PLAIN")
+
+ alpRoundTrip(t, values)
+}
+
func TestALPDecoderHugeFields(t *testing.T) {
dec := newALPDecoder[float32](format.Encoding_ALP, newFloat32Column())
assert.Error(t, dec.SetData(1, alpTestPage(math.MaxInt32)),
diff --git a/parquet/internal/encoding/alp_scaling.go
b/parquet/internal/encoding/alp_scaling.go
index 8ff42e61..5270ac76 100644
--- a/parquet/internal/encoding/alp_scaling.go
+++ b/parquet/internal/encoding/alp_scaling.go
@@ -27,9 +27,6 @@ import (
)
const (
- alpMagicFloat32 = float32(12582912.0) // 2^22 + 2^23
- alpMagicFloat64 = float64(6755399441055744.0) // 2^51 + 2^52
-
// The largest and smallest values of each type that convert to an
integer
// of the matching width. They stop short of the integer limits because
// neither type can represent every integer that close to them.
@@ -66,17 +63,6 @@ var (
}
)
-func alpMagic[T alpFloat]() T {
- var z T
- switch any(z).(type) {
- case float32:
- return T(alpMagicFloat32)
- case float64:
- return T(alpMagicFloat64)
- }
- panic("parquet: ALP: unsupported float type")
-}
-
func alpPow10[T alpFloat](i int) T {
var z T
switch any(z).(type) {
@@ -176,18 +162,11 @@ type alpScaling[T alpFloat] struct {
encExp, encFac T
decFac, decExp T
- lo, hi, magic T
- // The range of the integer type a vector stores: int32 for FLOAT, int64
- // for DOUBLE.
- intMin, intMax int64
+ lo, hi T
}
func newALPScaling[T alpFloat](exponent, factor int) alpScaling[T] {
lo, hi := alpEncodingLimits[T]()
- intMin, intMax := int64(math.MinInt64), int64(math.MaxInt64)
- if alpEncodedBytes[T]() == 4 {
- intMin, intMax = math.MinInt32, math.MaxInt32
- }
return alpScaling[T]{
encExp: alpPow10[T](exponent),
encFac: alpNegPow10[T](factor),
@@ -195,27 +174,24 @@ func newALPScaling[T alpFloat](exponent, factor int)
alpScaling[T] {
decExp: alpNegPow10[T](exponent),
lo: lo,
hi: hi,
- magic: alpMagic[T](),
- intMin: intMin,
- intMax: intMax,
}
}
-// round adds and subtracts a magic constant, which rounds to the nearest
-// integer without a library call.
+// round rounds to the nearest integer, and a tie to the even one.
+//
+// Adding and subtracting 2^52 + 2^51 (2^23 + 2^22 for float32) does the same
+// without a library call, but only below 2^51 (2^22): past that the sum has no
+// bit left for the units, and comes back as a different integer. A float32
+// holds whole numbers up to 2^24, so that trick turned much of what a FLOAT
+// column can hold exactly into exceptions.
func (s *alpScaling[T]) round(v T) int64 {
- if v >= 0 {
- return int64((v + s.magic) - s.magic)
- }
- return int64((v - s.magic) + s.magic)
+ return int64(math.RoundToEven(float64(v)))
}
// encode returns the integer v scales to. NaNs, infinities, out-of-range
// values, and negative zero cannot round-trip through an integer, so they get
a
// sentinel that cannot decode to the original value.
func (s *alpScaling[T]) encode(v T) int64 {
- // The conversion keeps the product from fusing with the addition in
round,
- // so every platform rounds the same product.
scaled := T(v * s.encExp * s.encFac)
if scaled != scaled || scaled > s.hi || scaled < s.lo || (scaled == 0
&& math.Signbit(float64(scaled))) {
return int64(s.hi)
@@ -232,13 +208,10 @@ func (s *alpScaling[T]) decode(encoded int64) T {
// tryEncode reports whether v survives the required round trip. Values that
// fail are stored verbatim as exceptions.
//
-// Rounding a scaled value just under the limit can still reach 2^31, which a
-// FLOAT vector cannot store: readers rebuild its integers in int32. Such a
-// value is an exception even though it would round-trip in int64.
+// The integer always fits the type a vector stores, int32 for FLOAT and int64
+// for DOUBLE: encode only rounds what lies within the limits, which are whole
+// numbers, so rounding cannot carry a value past them.
func (s *alpScaling[T]) tryEncode(v T) (encoded int64, ok bool) {
encoded = s.encode(v)
- if encoded < s.intMin || encoded > s.intMax {
- return encoded, false
- }
return encoded, s.decode(encoded) == v
}
diff --git a/parquet/internal/encoding/alp_scaling_test.go
b/parquet/internal/encoding/alp_scaling_test.go
index bd1bf0d9..91c5c0cd 100644
--- a/parquet/internal/encoding/alp_scaling_test.go
+++ b/parquet/internal/encoding/alp_scaling_test.go
@@ -33,13 +33,29 @@ func TestALPFastRoundFloat32(t *testing.T) {
{"positive integer", 5.0, 5},
{"positive round up", 2.7, 3},
{"positive round down", 2.3, 2},
- {"positive half", 2.5, 2}, // round-to-even via magic trick
+ {"positive half", 2.5, 2}, // a tie goes to the even integer
{"negative integer", -5.0, -5},
{"negative round up", -2.3, -2},
{"negative round down", -2.7, -3},
{"zero", 0.0, 0},
{"small positive", 0.1, 0},
{"large integer", 12345.0, 12345},
+ // Adding and subtracting 2^23 + 2^22 rounds whole numbers
correctly
+ // only up to 2^22.
+ {"below 2^22", 1<<22 - 1, 1<<22 - 1},
+ {"2^22", 1 << 22, 1 << 22},
+ {"above 2^22", 1<<22 + 1, 1<<22 + 1},
+ {"negative below 2^22", -(1<<22 - 1), -(1<<22 - 1)},
+ {"negative 2^22", -(1 << 22), -(1 << 22)},
+ {"negative above 2^22", -(1<<22 + 1), -(1<<22 + 1)},
+ // A float32 holds every whole number up to 2^24, and only even
ones
+ // past it.
+ {"below 2^24", 1<<24 - 1, 1<<24 - 1},
+ {"2^24", 1 << 24, 1 << 24},
+ {"above 2^24", 1<<24 + 2, 1<<24 + 2},
+ {"negative below 2^24", -(1<<24 - 1), -(1<<24 - 1)},
+ {"negative 2^24", -(1 << 24), -(1 << 24)},
+ {"negative above 2^24", -(1<<24 + 2), -(1<<24 + 2)},
}
for _, tt := range tests {
@@ -66,6 +82,22 @@ func TestALPFastRoundFloat64(t *testing.T) {
{"zero", 0.0, 0},
{"small positive", 0.1, 0},
{"large integer", 123456789.0, 123456789},
+ // Adding and subtracting 2^52 + 2^51 rounds whole numbers
correctly
+ // only up to 2^51.
+ {"below 2^51", 1<<51 - 1, 1<<51 - 1},
+ {"2^51", 1 << 51, 1 << 51},
+ {"above 2^51", 1<<51 + 1, 1<<51 + 1},
+ {"negative below 2^51", -(1<<51 - 1), -(1<<51 - 1)},
+ {"negative 2^51", -(1 << 51), -(1 << 51)},
+ {"negative above 2^51", -(1<<51 + 1), -(1<<51 + 1)},
+ // A float64 holds every whole number up to 2^53, and only even
ones
+ // past it.
+ {"below 2^53", 1<<53 - 1, 1<<53 - 1},
+ {"2^53", 1 << 53, 1 << 53},
+ {"above 2^53", 1<<53 + 2, 1<<53 + 2},
+ {"negative below 2^53", -(1<<53 - 1), -(1<<53 - 1)},
+ {"negative 2^53", -(1 << 53), -(1 << 53)},
+ {"negative above 2^53", -(1<<53 + 2), -(1<<53 + 2)},
}
for _, tt := range tests {