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

colinlee pushed a commit to branch colin_fix_null
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit 335bcf286fc1e313b1da099df9d5add6428d56a1
Author: ColinLee <[email protected]>
AuthorDate: Wed Apr 9 01:16:43 2025 +0800

    fix segv when flush data.
---
 cpp/src/writer/time_chunk_writer.cc                    | 10 +++++++---
 cpp/src/writer/value_chunk_writer.cc                   | 13 ++++++++-----
 cpp/test/writer/table_view/tsfile_writer_table_test.cc | 18 +++++++++++++++---
 3 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/cpp/src/writer/time_chunk_writer.cc 
b/cpp/src/writer/time_chunk_writer.cc
index f5b7b240..892c0d1c 100644
--- a/cpp/src/writer/time_chunk_writer.cc
+++ b/cpp/src/writer/time_chunk_writer.cc
@@ -57,6 +57,9 @@ void TimeChunkWriter::reset() {
     }
     if (first_page_statistic_ != nullptr) {
         first_page_statistic_->reset();
+    } else {
+        first_page_statistic_ =
+            StatisticFactory::alloc_statistic(common::VECTOR);
     }
     time_page_writer_.reset();
     chunk_header_.reset();
@@ -64,7 +67,6 @@ void TimeChunkWriter::reset() {
     num_of_pages_ = 0;
 }
 
-
 void TimeChunkWriter::destroy() {
     if (num_of_pages_ == 1) {
         free_first_writer_data();
@@ -144,9 +146,11 @@ void TimeChunkWriter::save_first_page_data(TimePageWriter 
&first_page_writer) {
     first_page_statistic_->deep_copy_from(first_page_writer.get_statistic());
 }
 
-int TimeChunkWriter::write_first_page_data(ByteStream &pages_data, bool 
with_statistic) {
+int TimeChunkWriter::write_first_page_data(ByteStream &pages_data,
+                                           bool with_statistic) {
     int ret = E_OK;
-    if (with_statistic && 
RET_FAIL(first_page_statistic_->serialize_to(pages_data))) {
+    if (with_statistic &&
+        RET_FAIL(first_page_statistic_->serialize_to(pages_data))) {
     } else if (RET_FAIL(
                    pages_data.write_buf(first_page_data_.compressed_buf_,
                                         first_page_data_.compressed_size_))) {
diff --git a/cpp/src/writer/value_chunk_writer.cc 
b/cpp/src/writer/value_chunk_writer.cc
index e29f2565..b6b19a4f 100644
--- a/cpp/src/writer/value_chunk_writer.cc
+++ b/cpp/src/writer/value_chunk_writer.cc
@@ -26,8 +26,8 @@ using namespace common;
 namespace storage {
 
 int ValueChunkWriter::init(const ColumnSchema &col_schema) {
-    return init(col_schema.column_name_, col_schema.data_type_, 
col_schema.encoding_,
-                col_schema.compression_);
+    return init(col_schema.column_name_, col_schema.data_type_,
+                col_schema.encoding_, col_schema.compression_);
 }
 
 int ValueChunkWriter::init(const std::string &measurement_name,
@@ -58,6 +58,8 @@ void ValueChunkWriter::reset() {
     }
     if (first_page_statistic_ != nullptr) {
         first_page_statistic_->reset();
+    } else {
+        first_page_statistic_ = StatisticFactory::alloc_statistic(data_type_);
     }
     value_page_writer_.reset();
     chunk_header_.reset();
@@ -65,7 +67,6 @@ void ValueChunkWriter::reset() {
     num_of_pages_ = 0;
 }
 
-
 void ValueChunkWriter::destroy() {
     if (num_of_pages_ == 1) {
         free_first_writer_data();
@@ -146,9 +147,11 @@ void ValueChunkWriter::save_first_page_data(
     first_page_statistic_->deep_copy_from(first_page_writer.get_statistic());
 }
 
-int ValueChunkWriter::write_first_page_data(ByteStream &pages_data, bool 
with_statistic) {
+int ValueChunkWriter::write_first_page_data(ByteStream &pages_data,
+                                            bool with_statistic) {
     int ret = E_OK;
-    if (with_statistic && 
RET_FAIL(first_page_statistic_->serialize_to(pages_data))) {
+    if (with_statistic &&
+        RET_FAIL(first_page_statistic_->serialize_to(pages_data))) {
     } else if (RET_FAIL(
                    pages_data.write_buf(first_page_data_.compressed_buf_,
                                         first_page_data_.compressed_size_))) {
diff --git a/cpp/test/writer/table_view/tsfile_writer_table_test.cc 
b/cpp/test/writer/table_view/tsfile_writer_table_test.cc
index 3fe1d461..43574dc7 100644
--- a/cpp/test/writer/table_view/tsfile_writer_table_test.cc
+++ b/cpp/test/writer/table_view/tsfile_writer_table_test.cc
@@ -93,11 +93,10 @@ class TsFileWriterTableTest : public ::testing::Test {
     }
 
     static storage::Tablet gen_tablet(TableSchema* table_schema, int offset,
-                                      int device_num) {
+                                      int device_num, int 
num_timestamp_per_device = 10) {
         storage::Tablet tablet(table_schema->get_measurement_names(),
-                               table_schema->get_data_types());
+                               table_schema->get_data_types(), device_num * 
num_timestamp_per_device);
 
-        int num_timestamp_per_device = 10;
         char* literal = new char[std::strlen("device_id") + 1];
         std::strcpy(literal, "device_id");
         String literal_str(literal, std::strlen("device_id"));
@@ -140,6 +139,19 @@ TEST_F(TsFileWriterTableTest, WriteTableTest) {
     delete table_schema;
 }
 
+TEST_F(TsFileWriterTableTest, WriteTableTestMultiFlush) {
+    auto table_schema = gen_table_schema(0);
+    auto tsfile_table_writer_ =
+        std::make_shared<TsFileTableWriter>(&write_file_, table_schema, 2 * 
1024);
+    for (int i = 0; i < 100; i++) {
+        auto tablet = gen_tablet(table_schema, i * 10000, 1, 10000);
+        ASSERT_EQ(tsfile_table_writer_->write_table(tablet), common::E_OK);
+    }
+    ASSERT_EQ(tsfile_table_writer_->flush(), common::E_OK);
+    ASSERT_EQ(tsfile_table_writer_->close(), common::E_OK);
+    delete table_schema;
+}
+
 TEST_F(TsFileWriterTableTest, WriteNonExistColumnTest) {
     auto table_schema = gen_table_schema(0);
     auto tsfile_table_writer_ =

Reply via email to