Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions compat/compatibility-matrix.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions cpp/src/encoding/encode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,31 @@
#ifndef ENCODING_ENCODE_UTILS_H
#define ENCODING_ENCODE_UTILS_H

#include <cstdint>

#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<int32_t>(bits);
}
return static_cast<int32_t>(INT32_MIN) +
static_cast<int32_t>(bits - 0x80000000U);
}

FORCE_INLINE int64_t int64_from_bits(uint64_t bits) {
if (bits <= 0x7FFFFFFFFFFFFFFFULL) {
return static_cast<int64_t>(bits);
}
return static_cast<int64_t>(INT64_MIN) +
static_cast<int64_t>(bits - 0x8000000000000000ULL);
}

FORCE_INLINE int32_t number_of_leading_zeros(int32_t i) {
if (i == 0) {
return 32;
Expand Down
26 changes: 20 additions & 6 deletions cpp/src/encoding/fire.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ class IntFire : public Fire<int> {
}

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<int>(0u - static_cast<uint32_t>(delta_))
: delta_;
accumulator_ = static_cast<int>(static_cast<uint32_t>(accumulator_) -
static_cast<uint32_t>(gradient));
delta_ = static_cast<int>(static_cast<uint32_t>(val) -
static_cast<uint32_t>(pre));
}
};

Expand Down Expand Up @@ -107,9 +114,16 @@ class LongFire : public Fire<int64_t> {
}

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<int64_t>(0ull - static_cast<uint64_t>(delta_))
: delta_;
accumulator_ = static_cast<int>(static_cast<uint32_t>(accumulator_) -
static_cast<uint32_t>(gradient));
delta_ = static_cast<int64_t>(static_cast<uint64_t>(val) -
static_cast<uint64_t>(pre));
}
};

Expand Down
20 changes: 13 additions & 7 deletions cpp/src/encoding/int32_sprintz_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <string>
#include <vector>

#include "encode_utils.h"
#include "encoding/fire.h"
#include "encoding/int32_rle_decoder.h"
#include "int32_packer.h"
Expand Down Expand Up @@ -182,23 +183,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<uint32_t>(current_buffer_[i]);
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] += current_buffer_[i - 1];
current_buffer_[i] = int32_from_bits(
static_cast<uint32_t>(current_buffer_[i]) +
static_cast<uint32_t>(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] = int32_from_bits(
static_cast<uint32_t>(pred) + static_cast<uint32_t>(err));
fire_pred_.train(current_buffer_[i - 1], current_buffer_[i],
err);
}
Expand Down
24 changes: 21 additions & 3 deletions cpp/src/encoding/int32_sprintz_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,32 @@ 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
Comment on lines +177 to +178
}
// 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<uint32_t>(pred)
: 2U * static_cast<uint32_t>(pred) - 1U;
return int32_from_bits(zigzag);
}

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 int32_from_bits(static_cast<uint32_t>(value) -
static_cast<uint32_t>(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 = int32_from_bits(static_cast<uint32_t>(value) -
static_cast<uint32_t>(pred));
fire_pred_.train(prev, value, err);
return err;
}
Expand Down
21 changes: 14 additions & 7 deletions cpp/src/encoding/int64_sprintz_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <vector>

#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"
Expand Down Expand Up @@ -183,23 +184,29 @@ 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<uint64_t>(current_buffer_[i]);
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] += current_buffer_[i - 1];
current_buffer_[i] = int64_from_bits(
static_cast<uint64_t>(current_buffer_[i]) +
static_cast<uint64_t>(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] = int64_from_bits(
static_cast<uint64_t>(pred) + static_cast<uint64_t>(err));
fire_pred_.train(current_buffer_[i - 1], current_buffer_[i],
err);
}
Expand Down
24 changes: 21 additions & 3 deletions cpp/src/encoding/int64_sprintz_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +175 to +176
}
if (pred <= 0) {
// -2 * pred, in [0, 2^64-2]; -pred fits in uint64 here.
uint64_t magnitude = static_cast<uint64_t>(-pred);
return int64_from_bits(magnitude << 1);
}
return int64_from_bits((static_cast<uint64_t>(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 int64_from_bits(static_cast<uint64_t>(value) -
static_cast<uint64_t>(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 = int64_from_bits(static_cast<uint64_t>(value) -
static_cast<uint64_t>(pred));
fire_pred_.train(prev, value, err);
return err;
}
Expand Down
Loading
Loading