This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch rc/2.2.0
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit e9f81a3eeaaf6b1a8d4244d17df48549639c0bce
Author: Colin Lee <[email protected]>
AuthorDate: Fri Oct 31 15:11:38 2025 +0800

    fix ts2diff. (#618)
---
 cpp/src/encoding/ts2diff_decoder.h      | 14 ++++++++------
 cpp/test/encoding/ts2diff_codec_test.cc | 30 ++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/cpp/src/encoding/ts2diff_decoder.h 
b/cpp/src/encoding/ts2diff_decoder.h
index a19e6163..c1e10270 100644
--- a/cpp/src/encoding/ts2diff_decoder.h
+++ b/cpp/src/encoding/ts2diff_decoder.h
@@ -135,13 +135,14 @@ inline int32_t 
TS2DIFFDecoder<int32_t>::decode(common::ByteStream &in) {
         }
         return ret_value;
     }
-    if (current_index_++ >= write_index_) {
-        current_index_ = 0;
-    }
     // although it seems we are reading an int64, bit_width_ guarantees
     // that it does not overflow int32
     stored_value_ = read_long(bit_width_, in);
     ret_value = stored_value_ + first_value_ + delta_min_;
+    if (current_index_++ >= write_index_) {
+        current_index_ = 0;
+        bits_left_ = 0;
+    }
     first_value_ = ret_value;
     return ret_value;
 }
@@ -161,12 +162,13 @@ inline int64_t 
TS2DIFFDecoder<int64_t>::decode(common::ByteStream &in) {
         }
         return ret_value;
     }
-    if (current_index_++ >= write_index_) {
-        current_index_ = 0;
-    }
     stored_value_ = (int64_t)read_long(bit_width_, in);
     ret_value = stored_value_ + first_value_ + delta_min_;
     first_value_ = ret_value;
+    if (current_index_++ >= write_index_) {
+        current_index_ = 0;
+        bits_left_ = 0;
+    }
     return ret_value;
 }
 
diff --git a/cpp/test/encoding/ts2diff_codec_test.cc 
b/cpp/test/encoding/ts2diff_codec_test.cc
index f0f5b1e5..be16d4af 100644
--- a/cpp/test/encoding/ts2diff_codec_test.cc
+++ b/cpp/test/encoding/ts2diff_codec_test.cc
@@ -206,4 +206,34 @@ TEST_F(TS2DIFFCodecTest, LargeDataTest) {
     std::cout << "Decode time: " << decode_duration.count() << "ms\n";
 }
 
+TEST_F(TS2DIFFCodecTest, TestEncodingLast) {
+    common::ByteStream out_stream(1024, common::MOD_TS2DIFF_OBJ, false);
+    common::ByteStream out_stream_int32(1024, common::MOD_TS2DIFF_OBJ, false);
+    const int row_num = 6;
+    int64_t data[row_num];
+    memset(data, 0, sizeof(int64_t) * row_num);
+    for (int i = 0; i < row_num; i++) {
+        data[i] = i * i;
+    }
+
+    for (int i = 0; i < row_num; i++) {
+        EXPECT_EQ(encoder_long_->encode(data[i], out_stream), common::E_OK);
+        EXPECT_EQ(encoder_int_->encode((int32_t)data[i], out_stream_int32),
+                  common::E_OK);
+    }
+    EXPECT_EQ(encoder_long_->flush(out_stream), common::E_OK);
+    EXPECT_EQ(encoder_int_->flush(out_stream_int32), common::E_OK);
+
+    int64_t x;
+    int32_t y;
+    for (int i = 0; i < row_num; i++) {
+        EXPECT_EQ(decoder_long_->read_int64(x, out_stream), common::E_OK);
+        EXPECT_EQ(x, data[i]);
+        EXPECT_EQ(decoder_int_->read_int32(y, out_stream_int32), common::E_OK);
+        EXPECT_EQ(y, data[i]);
+    }
+    EXPECT_FALSE(decoder_long_->has_remaining(out_stream));
+    EXPECT_FALSE(decoder_int_->has_remaining(out_stream_int32));
+}
+
 }  // namespace storage

Reply via email to