From 485c24034ceedd3c99276817795cd38be60c0915 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Thu, 26 Nov 2026 11:26:36 +0800 Subject: [PATCH 1/4] fix(cpp): make SPRINTZ INT32/INT64 encoding lossless for wide-range data The residual subtraction (value - prev / value - pred) and the ZigZag mapping ((pred <= 0) ? -2 * pred : 2 * pred - 1) were computed in the signed narrow integer type, so residuals whose magnitude exceeds 2^30 overflowed. The decoder's inverse also used signed division/modulo, mis-decoding any ZigZag value >= 2^31. Wide-range pseudo-random data silently read back wrong values (777/1000 mismatches for both INT32 and INT64). Fix all three: - compute the residual with modular (unsigned) subtraction; - compute the ZigZag in a wider type and map the single INT_MIN residual (which the classic mapping would send to 2^N, needing N+1 bits and colliding with 0) to 0xFFFF...FFFF, keeping the mapping bijective; - decode with unsigned arithmetic so the inverse is exact, and rebuild values with modular addition. Add regression tests for wide-range INT32/INT64 round-trips and for the INT_MIN/INT64_MIN residual sentinel. --- cpp/src/encoding/int32_sprintz_decoder.h | 19 +++-- cpp/src/encoding/int32_sprintz_encoder.h | 22 ++++- cpp/src/encoding/int64_sprintz_decoder.h | 19 +++-- cpp/src/encoding/int64_sprintz_encoder.h | 24 +++++- cpp/test/encoding/sprintz_codec_test.cc | 104 +++++++++++++++++++++++ 5 files changed, 168 insertions(+), 20 deletions(-) diff --git a/cpp/src/encoding/int32_sprintz_decoder.h b/cpp/src/encoding/int32_sprintz_decoder.h index a845fdc8a..620bb330c 100644 --- a/cpp/src/encoding/int32_sprintz_decoder.h +++ b/cpp/src/encoding/int32_sprintz_decoder.h @@ -182,23 +182,28 @@ class Int32SprintzDecoder : public SprintzDecoder { int recalculate() override { int ret = common::E_OK; for (int i = 1; i <= block_size_; ++i) { - if (current_buffer_[i] % 2 == 0) { - current_buffer_[i] = -current_buffer_[i] / 2; - } else { - current_buffer_[i] = (current_buffer_[i] + 1) / 2; - } + // Inverse of the encoder's ZigZag. Unsigned arithmetic decodes a + // residual whose encoded form has bit 31 set (which arrives as a + // negative int32), and maps the encoder's 0xFFFFFFFF sentinel for + // INT32_MIN back to INT32_MIN. + uint32_t z = static_cast(current_buffer_[i]); + current_buffer_[i] = + static_cast((z & 1u) == 0 ? -(z >> 1) : (z >> 1) + 1); } if (predict_scheme_ == "delta") { for (size_t i = 1; i < current_buffer_.size(); ++i) { - current_buffer_[i] += current_buffer_[i - 1]; + current_buffer_[i] = static_cast( + static_cast(current_buffer_[i]) + + static_cast(current_buffer_[i - 1])); } } else if (predict_scheme_ == "fire") { fire_pred_.reset(); for (int i = 1; i <= block_size_; ++i) { int32_t pred = fire_pred_.predict(current_buffer_[i - 1]); int32_t err = current_buffer_[i]; - current_buffer_[i] = pred + err; + current_buffer_[i] = static_cast( + static_cast(pred) + static_cast(err)); fire_pred_.train(current_buffer_[i - 1], current_buffer_[i], err); } diff --git a/cpp/src/encoding/int32_sprintz_encoder.h b/cpp/src/encoding/int32_sprintz_encoder.h index e92f25c3e..c44721f2c 100644 --- a/cpp/src/encoding/int32_sprintz_encoder.h +++ b/cpp/src/encoding/int32_sprintz_encoder.h @@ -168,14 +168,30 @@ class Int32SprintzEncoder : public SprintzEncoder { ASSERT(false); } - return (pred <= 0) ? -2 * pred : 2 * pred - 1; + // ZigZag-encode the residual without signed overflow. The classic + // SPRINTZ mapping ((pred <= 0) ? -2 * pred : 2 * pred - 1) overflows + // at INT32_MIN (it would yield 2^32, which needs 33 bits and would + // collide with 0). Map INT32_MIN to 0xFFFFFFFF instead — a value the + // classic mapping never emits — so the mapping stays a bijection over + // 32 bits and the bit-packer never truncates a residual. + if (pred == INT32_MIN) { + return -1; // 0xFFFFFFFF + } + return static_cast((pred <= 0) + ? -2 * static_cast(pred) + : 2 * static_cast(pred) - 1); } - int32_t delta(int32_t value, int32_t prev) { return value - prev; } + int32_t delta(int32_t value, int32_t prev) { + // value - prev modulo 2^32, well-defined on unsigned arithmetic. + return static_cast(static_cast(value) - + static_cast(prev)); + } int32_t fire(int32_t value, int32_t prev) { int32_t pred = fire_pred_.predict(prev); - int32_t err = value - pred; + int32_t err = static_cast(static_cast(value) - + static_cast(pred)); fire_pred_.train(prev, value, err); return err; } diff --git a/cpp/src/encoding/int64_sprintz_decoder.h b/cpp/src/encoding/int64_sprintz_decoder.h index 57ddab578..18506c6ec 100644 --- a/cpp/src/encoding/int64_sprintz_decoder.h +++ b/cpp/src/encoding/int64_sprintz_decoder.h @@ -183,23 +183,28 @@ class Int64SprintzDecoder : public SprintzDecoder { int recalculate() override { int ret = common::E_OK; for (int i = 1; i <= block_size_; ++i) { - if ((current_buffer_[i] & 1) == 0) { - current_buffer_[i] = -current_buffer_[i] / 2; - } else { - current_buffer_[i] = (current_buffer_[i] + 1) / 2; - } + // Inverse of the encoder's ZigZag. Unsigned arithmetic decodes a + // residual whose encoded form has bit 63 set (which arrives as a + // negative int64), and maps the encoder's 0xFFFFFFFFFFFFFFFF + // sentinel for INT64_MIN back to INT64_MIN. + uint64_t z = static_cast(current_buffer_[i]); + current_buffer_[i] = static_cast( + (z & 1ull) == 0 ? -(z >> 1) : (z >> 1) + 1); } if (predict_scheme_ == "delta") { for (int i = 1; i <= block_size_; ++i) { - current_buffer_[i] += current_buffer_[i - 1]; + current_buffer_[i] = static_cast( + static_cast(current_buffer_[i]) + + static_cast(current_buffer_[i - 1])); } } else if (predict_scheme_ == "fire") { fire_pred_.reset(); for (int i = 1; i <= block_size_; ++i) { int64_t pred = fire_pred_.predict(current_buffer_[i - 1]); int64_t err = current_buffer_[i]; - current_buffer_[i] = pred + err; + current_buffer_[i] = static_cast( + static_cast(pred) + static_cast(err)); fire_pred_.train(current_buffer_[i - 1], current_buffer_[i], err); } diff --git a/cpp/src/encoding/int64_sprintz_encoder.h b/cpp/src/encoding/int64_sprintz_encoder.h index 34151a6cb..05b7e0799 100644 --- a/cpp/src/encoding/int64_sprintz_encoder.h +++ b/cpp/src/encoding/int64_sprintz_encoder.h @@ -167,14 +167,32 @@ class Int64SprintzEncoder : public SprintzEncoder { ASSERT(false); } - return (pred <= 0) ? -2 * pred : 2 * pred - 1; + // ZigZag-encode the residual without signed overflow. The classic + // SPRINTZ mapping ((pred <= 0) ? -2 * pred : 2 * pred - 1) overflows + // at INT64_MIN (it would yield 2^64, which needs 65 bits and would + // collide with 0). Map INT64_MIN to 0xFFFFFFFFFFFFFFFF instead so the + // mapping stays a bijection over 64 bits. + if (pred == INT64_MIN) { + return -1; // 0xFFFFFFFFFFFFFFFF + } + if (pred <= 0) { + // -2 * pred, in [0, 2^64-2]; -pred fits in uint64 here. + uint64_t magnitude = static_cast(-pred); + return static_cast(magnitude << 1); + } + return static_cast((static_cast(pred) << 1) - 1); } - int64_t delta(int64_t value, int64_t prev) { return value - prev; } + int64_t delta(int64_t value, int64_t prev) { + // value - prev modulo 2^64, well-defined on unsigned arithmetic. + return static_cast(static_cast(value) - + static_cast(prev)); + } int64_t fire(int64_t value, int64_t prev) { int64_t pred = fire_pred_.predict(prev); - int64_t err = value - pred; + int64_t err = static_cast(static_cast(value) - + static_cast(pred)); fire_pred_.train(prev, value, err); return err; } diff --git a/cpp/test/encoding/sprintz_codec_test.cc b/cpp/test/encoding/sprintz_codec_test.cc index 9d6bdd778..5bfb610a3 100644 --- a/cpp/test/encoding/sprintz_codec_test.cc +++ b/cpp/test/encoding/sprintz_codec_test.cc @@ -1421,4 +1421,108 @@ TEST_F(SprintzCodecTest, Int32SpecialBlockAtCapacityStillDecodes) { ASSERT_FALSE(decoder.has_remaining(stream)); } +// Wide-range values make consecutive residuals jump across the whole integer +// range, exercising the residual subtraction and ZigZag overflow paths that +// small or fixed-extreme data never reach. These must round-trip losslessly. +TEST_F(SprintzCodecTest, Int32WideRangeRoundTrip) { + std::vector values; + for (int r = 0; r < 1000; ++r) { + values.push_back( + static_cast(static_cast(r) * 2654435761u)); + } + + Int32SprintzEncoder encoder; + ByteStream stream(8192, MOD_ENCODER_OBJ); + for (int32_t v : values) { + ASSERT_EQ(encoder.encode(v, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int32SprintzDecoder decoder; + for (size_t i = 0; i < values.size(); ++i) { + int32_t actual = 0; + ASSERT_TRUE(decoder.has_remaining(stream)) << "i=" << i; + ASSERT_EQ(decoder.read_int32(actual, stream), E_OK); + ASSERT_EQ(actual, values[i]) << "i=" << i; + } + ASSERT_FALSE(decoder.has_remaining(stream)); +} + +TEST_F(SprintzCodecTest, Int64WideRangeRoundTrip) { + std::vector values; + for (int r = 0; r < 1000; ++r) { + values.push_back(static_cast(static_cast(r) * + 0x9E3779B97F4A7C15ull)); + } + + Int64SprintzEncoder encoder; + ByteStream stream(16384, MOD_ENCODER_OBJ); + for (int64_t v : values) { + ASSERT_EQ(encoder.encode(v, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int64SprintzDecoder decoder; + for (size_t i = 0; i < values.size(); ++i) { + int64_t actual = 0; + ASSERT_TRUE(decoder.has_remaining(stream)) << "i=" << i; + ASSERT_EQ(decoder.read_int64(actual, stream), E_OK); + ASSERT_EQ(actual, values[i]) << "i=" << i; + } + ASSERT_FALSE(decoder.has_remaining(stream)); +} + +// With the "delta" predictor, 0 -> INT32_MIN produces a residual of exactly +// INT32_MIN, whose classic ZigZag overflows to 2^32 (33 bits). The encoder +// maps it to the 0xFFFFFFFF sentinel; the decoder must recover INT32_MIN. +TEST_F(SprintzCodecTest, Int32DeltaIntMinResidual) { + std::vector values = {0, INT32_MIN, 1, 2, 3, 4, 5, 6, 7}; + + Int32SprintzEncoder encoder; + encoder.set_predict_method("delta"); + ByteStream stream(512, MOD_ENCODER_OBJ); + for (int32_t v : values) { + ASSERT_EQ(encoder.encode(v, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int32SprintzDecoder decoder; + decoder.set_predict_method("delta"); + // read_exact consumes the whole lone full block; the per-value + // has_remaining() loop stops one short of a stream that ends on a full + // block (documented in CheckBatchParity). + std::vector out(values.size()); + ASSERT_EQ( + ReadExact(decoder, out.data(), static_cast(values.size()), stream), + E_OK); + for (size_t i = 0; i < values.size(); ++i) { + ASSERT_EQ(values[i], out[i]) << "i=" << i; + } +} + +// INT64 analog of Int32DeltaIntMinResidual. +TEST_F(SprintzCodecTest, Int64DeltaIntMinResidual) { + std::vector values = {0, INT64_MIN, 1, 2, 3, 4, 5, 6, 7}; + + Int64SprintzEncoder encoder; + encoder.set_predict_method("delta"); + ByteStream stream(512, MOD_ENCODER_OBJ); + for (int64_t v : values) { + ASSERT_EQ(encoder.encode(v, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int64SprintzDecoder decoder; + decoder.set_predict_method("delta"); + // read_exact consumes the whole lone full block (see + // Int32DeltaIntMinResidual). + std::vector out(values.size()); + ASSERT_EQ( + ReadExact(decoder, out.data(), static_cast(values.size()), stream), + E_OK); + for (size_t i = 0; i < values.size(); ++i) { + ASSERT_EQ(values[i], out[i]) << "i=" << i; + } +} + } // namespace From 37a131d9b90f6d59c6aa01326e4c99213c2025d8 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Fri, 27 Nov 2026 15:47:11 +0800 Subject: [PATCH 2/4] fix(cpp,java): make SPRINTZ INT_MIN handling cross-language and FIRE training overflow-safe Address review feedback on the wide-range SPRINTZ fix: - The Java encoder/decoder did not understand the new INT_MIN/INT64_MIN sentinel: Java decoded 0xFFFFFFFF (resp. 0xFFFFFFFFFFFFFFFF) as 0, so a C++-written block with an INT_MIN residual was silently corrupted on read. Mirror the sentinel in the Java encoders and use an unsigned inverse in the Java decoders so both codecs emit and read identical bytes for every residual. - IntFire/LongFire training still executed signed-overflow UB on wide-range inputs. Make the negation, accumulator update (preserving LongFire's 32-bit narrowing), and delta update modular so training matches Java's wrapping semantics without undefined behavior. Add Java wide-range round-trip regression tests mirroring the C++ ones. --- cpp/src/encoding/fire.h | 26 +++++++--- .../encoding/decoder/IntSprintzDecoder.java | 8 ++- .../encoding/decoder/LongSprintzDecoder.java | 8 ++- .../encoding/encoder/IntSprintzEncoder.java | 12 +++-- .../encoding/encoder/LongSprintzEncoder.java | 12 +++-- .../encoding/decoder/SprintzDecoderTest.java | 52 +++++++++++++++++++ 6 files changed, 98 insertions(+), 20 deletions(-) diff --git a/cpp/src/encoding/fire.h b/cpp/src/encoding/fire.h index 58ad5a494..1a45ccc9d 100644 --- a/cpp/src/encoding/fire.h +++ b/cpp/src/encoding/fire.h @@ -76,9 +76,16 @@ class IntFire : public Fire { } void train(int pre, int val, int err) override { - int gradient = err > 0 ? -delta_ : delta_; - accumulator_ -= gradient; - delta_ = val - pre; + // Wrap the negation and the accumulator/delta updates so training + // never executes signed overflow on wide-range inputs, matching Java's + // wrapping int semantics. + int gradient = + err > 0 ? static_cast(0u - static_cast(delta_)) + : delta_; + accumulator_ = static_cast(static_cast(accumulator_) - + static_cast(gradient)); + delta_ = static_cast(static_cast(val) - + static_cast(pre)); } }; @@ -107,9 +114,16 @@ class LongFire : public Fire { } void train(int64_t pre, int64_t val, int64_t err) override { - int64_t gradient = err > 0 ? -delta_ : delta_; - accumulator_ -= gradient; - delta_ = val - pre; + // Wrap the negation, the 32-bit accumulator update (Java's LongFire + // narrows its accumulator to int), and the delta update so training + // never executes signed overflow on wide-range inputs. + int64_t gradient = + err > 0 ? static_cast(0ull - static_cast(delta_)) + : delta_; + accumulator_ = static_cast(static_cast(accumulator_) - + static_cast(gradient)); + delta_ = static_cast(static_cast(val) - + static_cast(pre)); } }; diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/IntSprintzDecoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/IntSprintzDecoder.java index a647935cf..e51b4d1a8 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/IntSprintzDecoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/IntSprintzDecoder.java @@ -90,8 +90,12 @@ protected void decodeBlock(ByteBuffer in) throws IOException { @Override protected void recalculate() { for (int i = 1; i <= Block_size; i++) { - if (currentBuffer[i] % 2 == 0) currentBuffer[i] = -currentBuffer[i] / 2; - else currentBuffer[i] = (currentBuffer[i] + 1) / 2; + // Unsigned inverse: a residual whose encoded form has bit 31 set arrives + // as a negative int; use a logical shift so it decodes correctly, and so + // the 0xFFFFFFFF sentinel for Integer.MIN_VALUE maps back to MIN_VALUE. + int z = currentBuffer[i]; + if ((z & 1) == 0) currentBuffer[i] = -(z >>> 1); + else currentBuffer[i] = (z >>> 1) + 1; } if (predictScheme.equals("delta")) { for (int i = 1; i < currentBuffer.length; i++) { diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/LongSprintzDecoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/LongSprintzDecoder.java index 5780a38fe..d2b822390 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/LongSprintzDecoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/decoder/LongSprintzDecoder.java @@ -89,8 +89,12 @@ protected void decodeBlock(ByteBuffer in) throws IOException { @Override protected void recalculate() { for (int i = 1; i <= Block_size; i++) { - if (currentBuffer[i] % 2 == 0) currentBuffer[i] = -currentBuffer[i] / 2; - else currentBuffer[i] = (currentBuffer[i] + 1) / 2; + // Unsigned inverse: use a logical shift so a residual whose encoded form + // has bit 63 set decodes correctly, and so the 0xFFFFFFFFFFFFFFFF + // sentinel for Long.MIN_VALUE maps back to MIN_VALUE. + long z = currentBuffer[i]; + if ((z & 1) == 0) currentBuffer[i] = -(z >>> 1); + else currentBuffer[i] = (z >>> 1) + 1; } if (predictScheme.equals("delta")) { for (int i = 1; i < currentBuffer.length; i++) { diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntSprintzEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntSprintzEncoder.java index 97430c19d..03b03f2a9 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntSprintzEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntSprintzEncoder.java @@ -72,12 +72,14 @@ protected Integer predict(Integer value, Integer preVlaue) throws TsFileEncoding throw new TsFileEncodingException( Messages.get("error.encoding.sprintz_encoder_unsupported_predict_method")); } - if (pred <= 0) { - pred = -2 * pred; - } else { - pred = 2 * pred - 1; // TODO:overflow + if (pred == Integer.MIN_VALUE) { + // The classic mapping sends MIN_VALUE to 2^32, which overflows to 0 and + // collides with the zero residual. Map it to 0xFFFFFFFF instead so the + // mapping stays a bijection over 32 bits (matching the C++ codec). + return -1; } - return pred; + // Wraps to the correct 32-bit bit pattern for every other residual. + return (pred <= 0) ? -2 * pred : 2 * pred - 1; } @Override diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongSprintzEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongSprintzEncoder.java index 2a1b91b4c..d4bb83b55 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongSprintzEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongSprintzEncoder.java @@ -73,12 +73,14 @@ protected Long predict(Long value, Long preVlaue) throws TsFileEncodingException throw new TsFileEncodingException( Messages.get("error.encoding.sprintz_encoder_unsupported_predict_method")); } - if (pred <= 0) { - pred = -2 * pred; - } else { - pred = 2 * pred - 1; // TODO:overflow + if (pred == Long.MIN_VALUE) { + // The classic mapping sends MIN_VALUE to 2^64, which overflows to 0 and + // collides with the zero residual. Map it to 0xFFFFFFFFFFFFFFFF instead + // so the mapping stays a bijection over 64 bits (matching the C++ codec). + return -1L; } - return pred; + // Wraps to the correct 64-bit bit pattern for every other residual. + return (pred <= 0) ? -2 * pred : 2 * pred - 1; } @Override diff --git a/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java b/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java index 34f2b9b79..24897ab0e 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java @@ -590,4 +590,56 @@ private void testDouble(int repeatCount) throws Exception { } } } + + @Test + public void testIntWideRangeRoundTrip() throws IOException { + Encoder encoder = new IntSprintzEncoder(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + List values = new ArrayList<>(); + for (int r = 0; r < 1000; r++) { + int v = (int) ((long) r * 2654435761L); + values.add(v); + encoder.encode(v, baos); + } + encoder.flush(baos); + + ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); + Decoder decoder = new IntSprintzDecoder(); + for (int i = 0; i < values.size(); i++) { + if (decoder.hasNext(buffer)) { + assertEquals(values.get(i).intValue(), decoder.readInt(buffer)); + } else { + fail("missing value at index " + i); + } + } + if (decoder.hasNext(buffer)) { + fail("extra values remain"); + } + } + + @Test + public void testLongWideRangeRoundTrip() throws IOException { + Encoder encoder = new LongSprintzEncoder(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + List values = new ArrayList<>(); + for (int r = 0; r < 1000; r++) { + long v = (long) r * 0x9E3779B97F4A7C15L; + values.add(v); + encoder.encode(v, baos); + } + encoder.flush(baos); + + ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); + Decoder decoder = new LongSprintzDecoder(); + for (int i = 0; i < values.size(); i++) { + if (decoder.hasNext(buffer)) { + assertEquals(values.get(i).longValue(), decoder.readLong(buffer)); + } else { + fail("missing value at index " + i); + } + } + if (decoder.hasNext(buffer)) { + fail("extra values remain"); + } + } } From 73e74687dc63838532d78e7dcde309a92881c96f Mon Sep 17 00:00:00 2001 From: ColinLee Date: Sun, 29 Nov 2026 23:50:15 +0800 Subject: [PATCH 3/4] test(cpp,java): pin SPRINTZ wide-range format across implementations Add the cross-language coverage the INT32/INT64 SPRINTZ wide-range fix needs: - Java-generated golden streams for full-range extremes, including the 0 -> INT_MIN residual the encoder maps to its 0xFF..FF sentinel, and for full-range pseudorandom values. The C++ encoder must reproduce the bytes exactly and the C++ decoder must read them back. - INT_MIN sentinel round trips through the default FIRE predictor in both implementations, plus Java sentinel round-trip tests. - A sprintz_wide value profile in the Java/C++ compatibility matrix so the fixture jobs exercise wide-range INT32/INT64 values in both directions. The matrix used to declare full-range SPRINTZ values as an unsupported combination; document the new profile instead. --- compat/README.md | 7 +- compat/compatibility-matrix.csv | 14 +- cpp/test/encoding/sprintz_codec_test.cc | 169 ++++++++++++++++++ ...encoding_compression_compatibility_test.cc | 37 +++- .../EncodingCompressionCompatibilityTest.java | 53 +++++- .../encoding/decoder/SprintzDecoderTest.java | 67 +++++++ 6 files changed, 331 insertions(+), 16 deletions(-) diff --git a/compat/README.md b/compat/README.md index 1a453fba8..c0903dc70 100644 --- a/compat/README.md +++ b/compat/README.md @@ -99,7 +99,8 @@ wrote it. | `ts2diff` | FLOAT/DOUBLE TS_2DIFF values covering scaled, scale-overflow and raw-bit forms (NaN, infinities) across the 129-value block boundary. | | `ts2diff_mpn0` | Same values written with an explicit `max_point_number=0` property (Java only; the C++ writer has no property path). | | `rle_overflow` | FLOAT/DOUBLE RLE with 8-value runs in 64-row pages: ordinary scaled values, scaled overflow only, mixed scaled/unscaled/raw IEEE values (NaN, infinities and finite extremes), then ordinary values again and a single-row final page. | -| `sprintz` | Values inside a range the SPRINTZ FIR predictor can encode (about ±1e8 for INT32 and ±1e12 for INT64). Outside it the predictor itself overflows, which is a codec limitation rather than a cross-language difference. | +| `sprintz` | Predictor-friendly values well inside the SPRINTZ FIRE predictor's ordinary range. | +| `sprintz_wide` | Full-range INT32/INT64 values, including `0 -> INT_MIN`, whose residual is the single value the ZigZag mapping represents with its `0xFF..FF` sentinel, and residuals that need the full 32/64-bit width. | | `runs` | Long runs for RLE encodings. | | `strings` | Empty string, embedded NUL, multi-byte UTF-8, repeated values, long values. | | `dict_high` | One distinct string per row to stress dictionary growth. | @@ -110,6 +111,10 @@ directions. The normal unit suites also verify their three page-header forms and reuse the encoder/decoder across pages, so overflow state cannot leak into the following ordinary page unnoticed. +The `sprintz_wide` rows are supplemental: they declare fewer data scales than +the primary rows and extend value coverage without repeating the full scale +sweep. + ## Running locally ```bash diff --git a/compat/compatibility-matrix.csv b/compat/compatibility-matrix.csv index ed2a211de..ac55d01d4 100644 --- a/compat/compatibility-matrix.csv +++ b/compat/compatibility-matrix.csv @@ -39,7 +39,7 @@ # nullPatterns: '|' separated NONE/MIXED/ALL values. # pageRows: 0 keeps the writer default (10000). chunks: flush boundaries. # devices: number of devices (and therefore chunk groups) in one file. -# valueProfile: default|ts2diff|ts2diff_mpn0|sprintz|runs|rle_overflow|strings|dict_high|blob. +# valueProfile: default|ts2diff|ts2diff_mpn0|sprintz|sprintz_wide|runs|rle_overflow|strings|dict_high|blob. # A 'no' in any write/read column is a declared gap and gapReason must explain # it. Known unsupported combinations that are not in this matrix at all: # - DIFF, BITMAP, REGULAR, FREQ: deprecated and not writable by either side. @@ -48,14 +48,14 @@ # - VECTOR: no writer on either side. # - max_point_number on the C++ side: declared per row below (C++ reads # both forms). -# - Full-range INT32/INT64 extremes under SPRINTZ: the predictor overflows -# when one block spans INT_MIN and INT_MAX, so SPRINTZ rows use the -# 'sprintz' profile (values within +/-1e9). Streaming numeric extremes are -# covered under PLAIN, RLE, TS_2DIFF, GORILLA, CHIMP and RLBE. # - ALP (encoding id 15): C++/Python-only for now. The Java implementation # has no ALP decoder, so this matrix declares no ALP rows and the C++ # completeness sweep stops at CAMEL. Add ALP rows and extend that bound # only together with the Java decoder. +# +# Full-range INT32/INT64 extremes under SPRINTZ -- including the 0 -> INT_MIN +# residual that the ZigZag mapping represents with its 0xFF..FF sentinel -- are +# covered by the 'sprintz_wide' profile rows. id,model,dataType,encoding,compressions,rowCount,pageRows,chunks,devices,valueProfile,nullPatterns,javaWrite,cppWrite,javaRead,cppRead,gapReason,timestampProfile table_int32_plain_r10,table,INT32,PLAIN,ALL,10,4,1,1,default,NONE,yes,yes,yes,yes, @@ -86,6 +86,8 @@ table_int32_sprintz_r10,table,INT32,SPRINTZ,ALL,10,4,1,1,sprintz,NONE,yes,yes,ye table_int32_sprintz_r1000,table,INT32,SPRINTZ,ALL,1000,100,1,1,sprintz,NONE,yes,yes,yes,yes, table_int32_sprintz_r100000,table,INT32,SPRINTZ,ALL,100000,10000,1,1,sprintz,NONE,yes,yes,yes,yes, table_int32_sprintz_r1000000,table,INT32,SPRINTZ,SNAPPY+GZIP+LZ4+ZSTD+LZMA2,1000000,10000,1,1,sprintz,NONE,yes,yes,yes,yes, +table_int32_sprintz_wide_r10,table,INT32,SPRINTZ,ALL,10,4,1,1,sprintz_wide,NONE,yes,yes,yes,yes, +table_int32_sprintz_wide_r1000,table,INT32,SPRINTZ,ALL,1000,100,1,1,sprintz_wide,NONE,yes,yes,yes,yes, table_int32_rlbe_r10,table,INT32,RLBE,ALL,10,4,1,1,default,NONE,yes,yes,yes,yes, table_int32_rlbe_r1000,table,INT32,RLBE,ALL,1000,100,1,1,default,NONE,yes,yes,yes,yes, table_int32_rlbe_r100000,table,INT32,RLBE,ALL,100000,10000,1,1,default,NONE,yes,yes,yes,yes, @@ -150,6 +152,8 @@ table_int64_sprintz_r10,table,INT64,SPRINTZ,ALL,10,4,1,1,sprintz,NONE,yes,yes,ye table_int64_sprintz_r1000,table,INT64,SPRINTZ,ALL,1000,100,1,1,sprintz,NONE,yes,yes,yes,yes, table_int64_sprintz_r100000,table,INT64,SPRINTZ,ALL,100000,10000,1,1,sprintz,NONE,yes,yes,yes,yes, table_int64_sprintz_r1000000,table,INT64,SPRINTZ,SNAPPY+GZIP+LZ4+ZSTD+LZMA2,1000000,10000,1,1,sprintz,NONE,yes,yes,yes,yes, +table_int64_sprintz_wide_r10,table,INT64,SPRINTZ,ALL,10,4,1,1,sprintz_wide,NONE,yes,yes,yes,yes, +table_int64_sprintz_wide_r1000,table,INT64,SPRINTZ,ALL,1000,100,1,1,sprintz_wide,NONE,yes,yes,yes,yes, table_int64_rlbe_r10,table,INT64,RLBE,ALL,10,4,1,1,default,NONE,yes,yes,yes,yes, table_int64_rlbe_r1000,table,INT64,RLBE,ALL,1000,100,1,1,default,NONE,yes,yes,yes,yes, table_int64_rlbe_r100000,table,INT64,RLBE,ALL,100000,10000,1,1,default,NONE,yes,yes,yes,yes, diff --git a/cpp/test/encoding/sprintz_codec_test.cc b/cpp/test/encoding/sprintz_codec_test.cc index 5bfb610a3..ae2639ee4 100644 --- a/cpp/test/encoding/sprintz_codec_test.cc +++ b/cpp/test/encoding/sprintz_codec_test.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1122,6 +1123,91 @@ const int64_t kJavaInt64Timestamps17Values[] = { 1732000012000LL, 1732000013000LL, 1732000014000LL, 1732000015000LL, 1732000016000LL, }; +// JavaInt32Extremes12: the first FIRE residual of the block is INT32_MIN +// itself (0 -> INT32_MIN), and the block also carries full-width residuals; +// the stream ends in a short tail block (Java IntSprintzEncoder, 12 values). +const uint8_t kJavaInt32Extremes12Bytes[] = { + 0x20, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbf, + 0xff, 0xff, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x00, 0xff, 0xff, 0xfd, 0x00, 0x40, 0x00, 0x01, 0x83, 0x23, + 0x20, 0x03, 0x03, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x07, 0xff, + 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; +const int32_t kJavaInt32Extremes12Values[] = { + 0, INT32_MIN, INT32_MAX, -1, 1, 0, + INT32_MIN, INT32_MAX, INT32_MIN, 42, 7, -8, +}; +// JavaInt64Extremes12: INT64 analog, exercising the 0xFFFFFFFFFFFFFFFF +// sentinel (Java LongSprintzEncoder, 12 values). +const uint8_t kJavaInt64Extremes12Bytes[] = { + 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x83, 0x43, 0x40, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; +const int64_t kJavaInt64Extremes12Values[] = { + 0LL, INT64_MIN, INT64_MAX, -1LL, 1LL, 0LL, + INT64_MIN, INT64_MAX, INT64_MIN, 42LL, 7LL, -8LL, +}; +// JavaInt32WideRange17: full-range pseudo-random values that drive the FIRE +// predictor across the whole integer range, one full block plus a short tail +// (Java IntSprintzEncoder, 17 values). +const uint8_t kJavaInt32WideRange17Bytes[] = { + 0x20, 0x00, 0xc3, 0x91, 0x0c, 0x9e, 0xc3, 0x91, 0x0c, 0x9e, 0xc3, 0x23, + 0xc6, 0x00, 0xc3, 0x72, 0xee, 0x56, 0xc4, 0x42, 0x16, 0xae, 0xc2, 0x91, + 0x3f, 0x04, 0xc4, 0x23, 0xf8, 0x68, 0xc2, 0xf3, 0x20, 0xbe, 0x88, 0x23, + 0x20, 0x03, 0x08, 0x8f, 0xf3, 0x47, 0x39, 0x2e, 0x2a, 0xc0, 0xea, 0xcc, + 0x62, 0x3a, 0x9b, 0x6a, 0x99, 0xb4, 0x4c, 0x08, 0xd1, 0x2d, 0xfd, 0xa7, + 0x08, 0xa7, 0xae, 0x45, 0x40, 0x21, 0x5f, 0xe3, 0x77, 0x9b, 0x10, +}; +const int32_t kJavaInt32WideRange17Values[] = { + 0, -1640531535, 1013904226, -626627309, 2027808452, 387276917, + -1253254618, 1401181143, -239350392, -1879881927, 774553834, -865977701, + 1788458060, 147926525, -1492605010, 1161830751, -478700784, +}; +// JavaInt64WideRange17: INT64 analog (Java LongSprintzEncoder, 17 values). +const uint8_t kJavaInt64WideRange17Bytes[] = { + 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x91, 0x0c, + 0x8d, 0x01, 0x6b, 0x07, 0xd6, 0xc3, 0x91, 0x0c, 0x8d, 0x01, 0x6b, 0x07, + 0xd6, 0xc3, 0x91, 0x92, 0x11, 0x80, 0xe2, 0x74, 0xcc, 0xc3, 0x90, 0x77, + 0x68, 0x9f, 0x5f, 0x21, 0xc2, 0xc3, 0x90, 0x39, 0x5c, 0x12, 0x49, 0x8d, + 0x4c, 0xc3, 0x91, 0x1e, 0xb3, 0x30, 0xc6, 0x3a, 0x42, 0xc3, 0x91, 0xa4, + 0x37, 0xb0, 0x3d, 0xa7, 0x38, 0xc3, 0x91, 0xc5, 0xfd, 0xc2, 0x2d, 0x52, + 0xc4, 0x88, 0x43, 0x40, 0x03, 0x08, 0x8f, 0xf3, 0x47, 0x85, 0x79, 0x9e, + 0x5c, 0xbd, 0x2e, 0x2a, 0xc1, 0x3e, 0xf8, 0xe8, 0xd8, 0xd2, 0xcc, 0x62, + 0x3a, 0xf8, 0x78, 0x33, 0x54, 0xe7, 0x6a, 0x99, 0xb4, 0xb1, 0xf7, 0x7d, + 0xd0, 0xfc, 0x08, 0xd1, 0x2e, 0x6b, 0x76, 0xc8, 0x4d, 0x11, 0xa7, 0x08, + 0xa8, 0x24, 0xf6, 0x12, 0xc9, 0x26, 0x45, 0x40, 0x21, 0xde, 0x75, 0x5d, + 0x45, 0x3b, 0xe3, 0x77, 0x9b, 0x97, 0xf4, 0xa7, 0xc1, 0x50, +}; +const int64_t kJavaInt64WideRange17Values[] = { + 0LL, + -7046029254386353131LL, + 4354685564936845354LL, + -2691343689449507777LL, + 8709371129873690708LL, + 1663341875487337577LL, + -5382687378899015554LL, + 6018027440424182931LL, + -1028001813962170200LL, + -8074031068348523331LL, + 3326683750974675154LL, + -3719345503411677977LL, + 7681369315911520508LL, + 635340061525167377LL, + -6410689192861185754LL, + 4990025626462012731LL, + -2056003627924340400LL, +}; // JavaFloatSmooth12: smooth float series (Java FloatSprintzEncoder, 12 values) const uint8_t kJavaFloatSmooth12Bytes[] = { 0x12, 0x41, 0xa0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -1287,6 +1373,45 @@ TEST_F(SprintzCodecTest, JavaGoldenInt64) { kJavaInt64Timestamps17Values, static_cast(values.size())); } +// Full-range residuals, including the INT_MIN/INT64_MIN residual that the +// encoder maps to the 0xFF..FF sentinel, must produce identical bytes in +// Java and C++ and decode in the other language's implementation. +TEST_F(SprintzCodecTest, JavaGoldenInt32Extremes) { + std::vector values = ToVector(kJavaInt32Extremes12Values); + ExpectEncoderMatchesJava( + values, kJavaInt32Extremes12Bytes, sizeof(kJavaInt32Extremes12Bytes)); + CheckJavaGolden( + kJavaInt32Extremes12Bytes, sizeof(kJavaInt32Extremes12Bytes), + kJavaInt32Extremes12Values, static_cast(values.size())); +} + +TEST_F(SprintzCodecTest, JavaGoldenInt64Extremes) { + std::vector values = ToVector(kJavaInt64Extremes12Values); + ExpectEncoderMatchesJava( + values, kJavaInt64Extremes12Bytes, sizeof(kJavaInt64Extremes12Bytes)); + CheckJavaGolden( + kJavaInt64Extremes12Bytes, sizeof(kJavaInt64Extremes12Bytes), + kJavaInt64Extremes12Values, static_cast(values.size())); +} + +TEST_F(SprintzCodecTest, JavaGoldenInt32WideRange) { + std::vector values = ToVector(kJavaInt32WideRange17Values); + ExpectEncoderMatchesJava( + values, kJavaInt32WideRange17Bytes, sizeof(kJavaInt32WideRange17Bytes)); + CheckJavaGolden( + kJavaInt32WideRange17Bytes, sizeof(kJavaInt32WideRange17Bytes), + kJavaInt32WideRange17Values, static_cast(values.size())); +} + +TEST_F(SprintzCodecTest, JavaGoldenInt64WideRange) { + std::vector values = ToVector(kJavaInt64WideRange17Values); + ExpectEncoderMatchesJava( + values, kJavaInt64WideRange17Bytes, sizeof(kJavaInt64WideRange17Bytes)); + CheckJavaGolden( + kJavaInt64WideRange17Bytes, sizeof(kJavaInt64WideRange17Bytes), + kJavaInt64WideRange17Values, static_cast(values.size())); +} + TEST_F(SprintzCodecTest, JavaGoldenFloat) { std::vector values; for (size_t i = 0; i < sizeof(kJavaFloatSmooth12Bits) / sizeof(uint32_t); @@ -1525,4 +1650,48 @@ TEST_F(SprintzCodecTest, Int64DeltaIntMinResidual) { } } +// The default "fire" predictor is reset at each block start, so its first +// prediction is the previous value: 0 -> INT32_MIN is again an INT32_MIN +// residual, exercising the sentinel through the fire path rather than delta. +TEST_F(SprintzCodecTest, Int32FireIntMinResidual) { + std::vector values = {0, INT32_MIN, 1, 2, 3, 4, 5, 6, 7, 42}; + + Int32SprintzEncoder encoder; // default "fire" + ByteStream stream(512, MOD_ENCODER_OBJ); + for (int32_t v : values) { + ASSERT_EQ(encoder.encode(v, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int32SprintzDecoder decoder; // default "fire" + std::vector out(values.size()); + ASSERT_EQ( + ReadExact(decoder, out.data(), static_cast(values.size()), stream), + E_OK); + for (size_t i = 0; i < values.size(); ++i) { + ASSERT_EQ(values[i], out[i]) << "i=" << i; + } +} + +// INT64 analog of Int32FireIntMinResidual. +TEST_F(SprintzCodecTest, Int64FireIntMinResidual) { + std::vector values = {0, INT64_MIN, 1, 2, 3, 4, 5, 6, 7, 42}; + + Int64SprintzEncoder encoder; // default "fire" + ByteStream stream(512, MOD_ENCODER_OBJ); + for (int64_t v : values) { + ASSERT_EQ(encoder.encode(v, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int64SprintzDecoder decoder; // default "fire" + std::vector out(values.size()); + ASSERT_EQ( + ReadExact(decoder, out.data(), static_cast(values.size()), stream), + E_OK); + for (size_t i = 0; i < values.size(); ++i) { + ASSERT_EQ(values[i], out[i]) << "i=" << i; + } +} + } // namespace diff --git a/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc b/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc index a6c3e3940..d999fa67f 100644 --- a/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc +++ b/cpp/test/reader/table_view/encoding_compression_compatibility_test.cc @@ -122,6 +122,7 @@ enum class ValueProfile { TS2DIFF_MPN0, RLE_OVERFLOW, SPRINTZ, + SPRINTZ_WIDE, RUNS, STRINGS, DICT_HIGH, @@ -158,6 +159,8 @@ const char* ProfileName(ValueProfile profile) { return "RLE_OVERFLOW"; case ValueProfile::SPRINTZ: return "SPRINTZ"; + case ValueProfile::SPRINTZ_WIDE: + return "SPRINTZ_WIDE"; case ValueProfile::RUNS: return "RUNS"; case ValueProfile::STRINGS: @@ -359,15 +362,21 @@ int RleOverflowIndex(int row) { return index < 24 ? index : index - 24; } -// SPRINTZ predicts from first-order differences of the stored integers; a -// block spanning INT_MIN and INT_MAX overflows the predictor, so this profile -// stays inside a safe range. Streaming numeric extremes are covered under the -// other encodings. +// SPRINTZ predicts from first-order differences of the stored integers. The +// SPRINTZ profile stays inside the FIRE predictor's comfortable range; +// SPRINTZ_WIDE covers the full INT32/INT64 range, including 0 -> INT_MIN, +// whose residual is the single value the ZigZag mapping represents with the +// 0xFF..FF sentinel, plus residuals that need the full 32/64-bit width. const int32_t kSprintzIntValues[] = { 0, 1, -1, 7, -8, 1024, -1024, 123456, -654321, 100000000, -100000000, 42, 42, 0, -8, 2048, }; +const int32_t kSprintzWideIntValues[] = { + 0, INT32_MIN, INT32_MAX, 1, -1, 7, -8, INT32_MIN, + 1024, INT32_MAX, -1024, 42, INT32_MIN, -654321, INT32_MAX, 0, +}; + const int64_t kSprintzLongValues[] = { 0LL, 1LL, @@ -387,6 +396,12 @@ const int64_t kSprintzLongValues[] = { 2048LL, }; +const int64_t kSprintzWideLongValues[] = { + 0LL, INT64_MIN, INT64_MAX, 1LL, -1LL, 7LL, + -8LL, INT64_MIN, 1024LL, INT64_MAX, -1024LL, 42LL, + INT64_MIN, -654321LL, INT64_MAX, 0LL, +}; + const uint32_t kSprintzFloatBits[] = { 0x3f800000U, 0x3fa00000U, 0x3fc00000U, 0x3fe00000U, 0x40000000U, 0x3fc00000U, 0x3fa00000U, 0x3f800000U, @@ -518,6 +533,9 @@ int32_t IntValue(const FixtureCase& fixture_case, int row) { if (fixture_case.profile == ValueProfile::SPRINTZ) { return IntAt(kSprintzIntValues, row); } + if (fixture_case.profile == ValueProfile::SPRINTZ_WIDE) { + return IntAt(kSprintzWideIntValues, row); + } return IntAt(kIntValues, row); } @@ -530,6 +548,11 @@ int64_t LongValue(const FixtureCase& fixture_case, int row) { sizeof(kSprintzLongValues) / sizeof(kSprintzLongValues[0]); return kSprintzLongValues[static_cast(row) % count]; } + if (fixture_case.profile == ValueProfile::SPRINTZ_WIDE) { + const size_t count = + sizeof(kSprintzWideLongValues) / sizeof(kSprintzWideLongValues[0]); + return kSprintzWideLongValues[static_cast(row) % count]; + } const size_t count = sizeof(kLongValues) / sizeof(kLongValues[0]); return kLongValues[static_cast(row) % count]; } @@ -712,9 +735,9 @@ bool ParseProfile(const std::string& value, ValueProfile& out) { static const ValueProfile kProfiles[] = { ValueProfile::DEFAULT, ValueProfile::TS2DIFF, ValueProfile::TS2DIFF_MPN0, ValueProfile::SPRINTZ, - ValueProfile::RLE_OVERFLOW, ValueProfile::RUNS, - ValueProfile::STRINGS, ValueProfile::DICT_HIGH, - ValueProfile::BLOB, + ValueProfile::SPRINTZ_WIDE, ValueProfile::RLE_OVERFLOW, + ValueProfile::RUNS, ValueProfile::STRINGS, + ValueProfile::DICT_HIGH, ValueProfile::BLOB, }; for (ValueProfile profile : kProfiles) { if (upper == ProfileName(profile)) { diff --git a/java/tsfile/src/test/java/org/apache/tsfile/compatibility/EncodingCompressionCompatibilityTest.java b/java/tsfile/src/test/java/org/apache/tsfile/compatibility/EncodingCompressionCompatibilityTest.java index b83bb5bb6..c5f9c36c1 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/compatibility/EncodingCompressionCompatibilityTest.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/compatibility/EncodingCompressionCompatibilityTest.java @@ -175,6 +175,7 @@ private enum ValueProfile { TS2DIFF_MPN0, RLE_OVERFLOW, SPRINTZ, + SPRINTZ_WIDE, RUNS, STRINGS, DICT_HIGH, @@ -976,13 +977,34 @@ private static int rleOverflowIndex(int row) { return index < 24 ? index : index - 24; } - // SPRINTZ predicts from first-order differences of the stored integers; a - // block that spans INT_MIN and INT_MAX overflows the predictor, so these - // values stay inside a safe range (see compat/README.md). + // SPRINTZ predicts from first-order differences of the stored integers. The + // 'sprintz' profile stays inside the FIRE predictor's comfortable range; + // 'sprintz_wide' covers the full INT32/INT64 range, including 0 -> INT_MIN, + // whose residual is the single value the ZigZag mapping represents with the + // 0xFF..FF sentinel, plus residuals that need the full 32/64-bit width. private static final int[] SPRINTZ_INT_VALUES = { 0, 1, -1, 7, -8, 1024, -1024, 123456, -654321, 100_000_000, -100_000_000, 42, 42, 0, -8, 2048 }; + private static final int[] SPRINTZ_WIDE_INT_VALUES = { + 0, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + 1, + -1, + 7, + -8, + Integer.MIN_VALUE, + 1024, + Integer.MAX_VALUE, + -1024, + 42, + Integer.MIN_VALUE, + -654321, + Integer.MAX_VALUE, + 0 + }; + private static final long[] SPRINTZ_LONG_VALUES = { 0L, 1L, @@ -1002,6 +1024,25 @@ private static int rleOverflowIndex(int row) { 2048L }; + private static final long[] SPRINTZ_WIDE_LONG_VALUES = { + 0L, + Long.MIN_VALUE, + Long.MAX_VALUE, + 1L, + -1L, + 7L, + -8L, + Long.MIN_VALUE, + 1024L, + Long.MAX_VALUE, + -1024L, + 42L, + Long.MIN_VALUE, + -654321L, + Long.MAX_VALUE, + 0L + }; + private static final int[] SPRINTZ_FLOAT_BITS = { 0x3f800000, 0x3fa00000, 0x3fc00000, 0x3fe00000, 0x40000000, 0x3fc00000, 0x3fa00000, 0x3f800000, 0x3fc00000, 0x3fe00000, 0x40000000, 0x3fe00000, @@ -1134,6 +1175,9 @@ private static int intValue(FixtureCase fixtureCase, int row) { if (fixtureCase.profile == ValueProfile.SPRINTZ) { return SPRINTZ_INT_VALUES[row % SPRINTZ_INT_VALUES.length]; } + if (fixtureCase.profile == ValueProfile.SPRINTZ_WIDE) { + return SPRINTZ_WIDE_INT_VALUES[row % SPRINTZ_WIDE_INT_VALUES.length]; + } return INT_VALUES[row % INT_VALUES.length]; } @@ -1144,6 +1188,9 @@ private static long longValue(FixtureCase fixtureCase, int row) { if (fixtureCase.profile == ValueProfile.SPRINTZ) { return SPRINTZ_LONG_VALUES[row % SPRINTZ_LONG_VALUES.length]; } + if (fixtureCase.profile == ValueProfile.SPRINTZ_WIDE) { + return SPRINTZ_WIDE_LONG_VALUES[row % SPRINTZ_WIDE_LONG_VALUES.length]; + } return LONG_VALUES[row % LONG_VALUES.length]; } diff --git a/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java b/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java index 24897ab0e..c0eb941de 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/encoding/decoder/SprintzDecoderTest.java @@ -591,6 +591,73 @@ private void testDouble(int repeatCount) throws Exception { } } + @Test + public void testIntIntMinResidual() throws IOException { + // With the FIRE predictor reset at the start of a block, the first + // prediction equals the previous value, so 0 -> Integer.MIN_VALUE is a + // residual of exactly Integer.MIN_VALUE: the one residual the classic + // ZigZag mapping cannot represent, encoded as the 0xFFFFFFFF sentinel. + Encoder encoder = new IntSprintzEncoder(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + List values = new ArrayList<>(); + values.add(0); + values.add(Integer.MIN_VALUE); + for (int i = 1; i <= 7; i++) { + values.add(i); + } + // A trailing value keeps the final block short so hasNext() drains it. + values.add(42); + for (int value : values) { + encoder.encode(value, baos); + } + encoder.flush(baos); + + ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); + Decoder decoder = new IntSprintzDecoder(); + for (int i = 0; i < values.size(); i++) { + if (decoder.hasNext(buffer)) { + assertEquals(values.get(i).intValue(), decoder.readInt(buffer)); + } else { + fail("missing value at index " + i); + } + } + if (decoder.hasNext(buffer)) { + fail("extra values remain"); + } + } + + @Test + public void testLongIntMinResidual() throws IOException { + // Integer.MIN_VALUE analog: the 0xFFFFFFFFFFFFFFFF sentinel must decode + // back to Long.MIN_VALUE. + Encoder encoder = new LongSprintzEncoder(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + List values = new ArrayList<>(); + values.add(0L); + values.add(Long.MIN_VALUE); + for (long i = 1; i <= 7; i++) { + values.add(i); + } + values.add(42L); + for (long value : values) { + encoder.encode(value, baos); + } + encoder.flush(baos); + + ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); + Decoder decoder = new LongSprintzDecoder(); + for (int i = 0; i < values.size(); i++) { + if (decoder.hasNext(buffer)) { + assertEquals(values.get(i).longValue(), decoder.readLong(buffer)); + } else { + fail("missing value at index " + i); + } + } + if (decoder.hasNext(buffer)) { + fail("extra values remain"); + } + } + @Test public void testIntWideRangeRoundTrip() throws IOException { Encoder encoder = new IntSprintzEncoder(); From f6653ece6a41e9189d006eda1db2bf90c8abc077 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Sun, 29 Nov 2026 23:50:18 +0800 Subject: [PATCH 4/4] refactor(cpp): make SPRINTZ bit-pattern conversions well-defined Reinterpret the INT32/INT64 residual ZigZag bit patterns with in-range offset arithmetic instead of implementation-defined out-of-range narrowing. Encoded bytes are unchanged. --- cpp/src/encoding/encode_utils.h | 21 +++++++++++++++++++++ cpp/src/encoding/int32_sprintz_decoder.h | 9 +++++---- cpp/src/encoding/int32_sprintz_encoder.h | 16 +++++++++------- cpp/src/encoding/int64_sprintz_decoder.h | 10 ++++++---- cpp/src/encoding/int64_sprintz_encoder.h | 12 ++++++------ 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/cpp/src/encoding/encode_utils.h b/cpp/src/encoding/encode_utils.h index a8be851ce..3b0e83213 100644 --- a/cpp/src/encoding/encode_utils.h +++ b/cpp/src/encoding/encode_utils.h @@ -19,10 +19,31 @@ #ifndef ENCODING_ENCODE_UTILS_H #define ENCODING_ENCODE_UTILS_H +#include + #include "utils/util_define.h" namespace storage { +// Reinterpret a ZigZag bit pattern as the signed residual it encodes without +// relying on implementation-defined out-of-range narrowing: when the sign bit +// is set, build the value as INT32_MIN + (bits - 2^31), which stays in range. +FORCE_INLINE int32_t int32_from_bits(uint32_t bits) { + if (bits <= 0x7FFFFFFFU) { + return static_cast(bits); + } + return static_cast(INT32_MIN) + + static_cast(bits - 0x80000000U); +} + +FORCE_INLINE int64_t int64_from_bits(uint64_t bits) { + if (bits <= 0x7FFFFFFFFFFFFFFFULL) { + return static_cast(bits); + } + return static_cast(INT64_MIN) + + static_cast(bits - 0x8000000000000000ULL); +} + FORCE_INLINE int32_t number_of_leading_zeros(int32_t i) { if (i == 0) { return 32; diff --git a/cpp/src/encoding/int32_sprintz_decoder.h b/cpp/src/encoding/int32_sprintz_decoder.h index 620bb330c..00c20e2d1 100644 --- a/cpp/src/encoding/int32_sprintz_decoder.h +++ b/cpp/src/encoding/int32_sprintz_decoder.h @@ -28,6 +28,7 @@ #include #include +#include "encode_utils.h" #include "encoding/fire.h" #include "encoding/int32_rle_decoder.h" #include "int32_packer.h" @@ -187,13 +188,13 @@ class Int32SprintzDecoder : public SprintzDecoder { // negative int32), and maps the encoder's 0xFFFFFFFF sentinel for // INT32_MIN back to INT32_MIN. uint32_t z = static_cast(current_buffer_[i]); - current_buffer_[i] = - static_cast((z & 1u) == 0 ? -(z >> 1) : (z >> 1) + 1); + uint32_t residual = (z & 1u) == 0 ? 0U - (z >> 1) : (z >> 1) + 1U; + current_buffer_[i] = int32_from_bits(residual); } if (predict_scheme_ == "delta") { for (size_t i = 1; i < current_buffer_.size(); ++i) { - current_buffer_[i] = static_cast( + current_buffer_[i] = int32_from_bits( static_cast(current_buffer_[i]) + static_cast(current_buffer_[i - 1])); } @@ -202,7 +203,7 @@ class Int32SprintzDecoder : public SprintzDecoder { for (int i = 1; i <= block_size_; ++i) { int32_t pred = fire_pred_.predict(current_buffer_[i - 1]); int32_t err = current_buffer_[i]; - current_buffer_[i] = static_cast( + current_buffer_[i] = int32_from_bits( static_cast(pred) + static_cast(err)); fire_pred_.train(current_buffer_[i - 1], current_buffer_[i], err); diff --git a/cpp/src/encoding/int32_sprintz_encoder.h b/cpp/src/encoding/int32_sprintz_encoder.h index c44721f2c..0a982b443 100644 --- a/cpp/src/encoding/int32_sprintz_encoder.h +++ b/cpp/src/encoding/int32_sprintz_encoder.h @@ -177,21 +177,23 @@ class Int32SprintzEncoder : public SprintzEncoder { if (pred == INT32_MIN) { return -1; // 0xFFFFFFFF } - return static_cast((pred <= 0) - ? -2 * static_cast(pred) - : 2 * static_cast(pred) - 1); + // The classic mapping in 32-bit unsigned arithmetic, then the bit + // pattern reinterpreted as the signed residual. + uint32_t zigzag = (pred <= 0) ? 0U - 2U * static_cast(pred) + : 2U * static_cast(pred) - 1U; + return int32_from_bits(zigzag); } int32_t delta(int32_t value, int32_t prev) { // value - prev modulo 2^32, well-defined on unsigned arithmetic. - return static_cast(static_cast(value) - - static_cast(prev)); + return int32_from_bits(static_cast(value) - + static_cast(prev)); } int32_t fire(int32_t value, int32_t prev) { int32_t pred = fire_pred_.predict(prev); - int32_t err = static_cast(static_cast(value) - - static_cast(pred)); + int32_t err = int32_from_bits(static_cast(value) - + static_cast(pred)); fire_pred_.train(prev, value, err); return err; } diff --git a/cpp/src/encoding/int64_sprintz_decoder.h b/cpp/src/encoding/int64_sprintz_decoder.h index 18506c6ec..2ad8893ad 100644 --- a/cpp/src/encoding/int64_sprintz_decoder.h +++ b/cpp/src/encoding/int64_sprintz_decoder.h @@ -28,6 +28,7 @@ #include #include "common/allocator/byte_stream.h" +#include "encode_utils.h" #include "encoding/fire.h" #include "encoding/int64_packer.h" #include "encoding/int64_rle_decoder.h" @@ -188,13 +189,14 @@ class Int64SprintzDecoder : public SprintzDecoder { // negative int64), and maps the encoder's 0xFFFFFFFFFFFFFFFF // sentinel for INT64_MIN back to INT64_MIN. uint64_t z = static_cast(current_buffer_[i]); - current_buffer_[i] = static_cast( - (z & 1ull) == 0 ? -(z >> 1) : (z >> 1) + 1); + uint64_t residual = + (z & 1ull) == 0 ? 0ull - (z >> 1) : (z >> 1) + 1ull; + current_buffer_[i] = int64_from_bits(residual); } if (predict_scheme_ == "delta") { for (int i = 1; i <= block_size_; ++i) { - current_buffer_[i] = static_cast( + current_buffer_[i] = int64_from_bits( static_cast(current_buffer_[i]) + static_cast(current_buffer_[i - 1])); } @@ -203,7 +205,7 @@ class Int64SprintzDecoder : public SprintzDecoder { for (int i = 1; i <= block_size_; ++i) { int64_t pred = fire_pred_.predict(current_buffer_[i - 1]); int64_t err = current_buffer_[i]; - current_buffer_[i] = static_cast( + current_buffer_[i] = int64_from_bits( static_cast(pred) + static_cast(err)); fire_pred_.train(current_buffer_[i - 1], current_buffer_[i], err); diff --git a/cpp/src/encoding/int64_sprintz_encoder.h b/cpp/src/encoding/int64_sprintz_encoder.h index 05b7e0799..82ffbe4e1 100644 --- a/cpp/src/encoding/int64_sprintz_encoder.h +++ b/cpp/src/encoding/int64_sprintz_encoder.h @@ -178,21 +178,21 @@ class Int64SprintzEncoder : public SprintzEncoder { if (pred <= 0) { // -2 * pred, in [0, 2^64-2]; -pred fits in uint64 here. uint64_t magnitude = static_cast(-pred); - return static_cast(magnitude << 1); + return int64_from_bits(magnitude << 1); } - return static_cast((static_cast(pred) << 1) - 1); + return int64_from_bits((static_cast(pred) << 1) - 1); } int64_t delta(int64_t value, int64_t prev) { // value - prev modulo 2^64, well-defined on unsigned arithmetic. - return static_cast(static_cast(value) - - static_cast(prev)); + return int64_from_bits(static_cast(value) - + static_cast(prev)); } int64_t fire(int64_t value, int64_t prev) { int64_t pred = fire_pred_.predict(prev); - int64_t err = static_cast(static_cast(value) - - static_cast(pred)); + int64_t err = int64_from_bits(static_cast(value) - + static_cast(pred)); fire_pred_.train(prev, value, err); return err; }