leaves12138 commented on code in PR #37: URL: https://github.com/apache/paimon-cpp/pull/37#discussion_r3338105817
########## src/paimon/common/utils/delta_varint_compressor.cpp: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "paimon/common/utils/delta_varint_compressor.h" + +#include <cstdint> +#include <utility> +#include <vector> + +#include "paimon/result.h" +#include "paimon/status.h" + +namespace paimon { + +std::vector<char> DeltaVarintCompressor::Compress(const std::vector<int64_t>& data) { + if (data.empty()) { + return {}; + } + + // 1. Delta encoding + std::vector<int64_t> deltas; + deltas.reserve(data.size()); + deltas.push_back(data[0]); + for (size_t i = 1; i < data.size(); i++) { + deltas.push_back(data[i] - data[i - 1]); + } + + // 2. ZigZag + Varint + std::vector<char> out; + out.reserve(data.size() * 10); + for (const auto& delta : deltas) { + EncodeVarint(delta, &out); + } + return out; +} + +Result<std::vector<int64_t>> DeltaVarintCompressor::Decompress(const std::vector<char>& bytes) { + if (bytes.empty()) { + return std::vector<int64_t>(); + } + + // 1. Decode ZigZag + Varint → delta + std::vector<int64_t> deltas; + deltas.reserve(bytes.size()); + size_t pos = 0; + while (pos < bytes.size()) { + PAIMON_ASSIGN_OR_RAISE(int64_t delta, DecodeVarint(bytes, &pos)); + deltas.push_back(delta); + } + + // 2. Delta decoding Review Comment: Same issue during reconstruction: for the extreme-value case, relies on signed overflow to become . Please reconstruct using well-defined modulo semantics instead of signed overflow. ########## src/paimon/common/utils/delta_varint_compressor.cpp: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "paimon/common/utils/delta_varint_compressor.h" + +#include <cstdint> +#include <utility> +#include <vector> + +#include "paimon/result.h" +#include "paimon/status.h" + +namespace paimon { + +std::vector<char> DeltaVarintCompressor::Compress(const std::vector<int64_t>& data) { + if (data.empty()) { + return {}; + } + + // 1. Delta encoding + std::vector<int64_t> deltas; + deltas.reserve(data.size()); + deltas.push_back(data[0]); Review Comment: This subtraction can overflow for the tested case. Java arithmetic wraps, but C++ signed overflow is undefined behavior. Please compute the delta with well-defined modulo semantics, for example via unsigned intermediates, before ZigZag encoding. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
