From 13564f1e3d2c9a286fa61b2972e59347b74acc41 Mon Sep 17 00:00:00 2001
From: 761417898 <761417898@qq.com>
Date: Wed, 19 Nov 2025 18:52:07 +0800
Subject: [PATCH 1/2] fix tree-interface get_device_schema

---
 cpp/src/file/tsfile_io_reader.cc              | 29 ++++++++++++++-----
 .../tree_view/tsfile_reader_tree_test.cc      |  7 +++++
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc
index c70c429e4..4a2810dd4 100644
--- a/cpp/src/file/tsfile_io_reader.cc
+++ b/cpp/src/file/tsfile_io_reader.cc
@@ -701,13 +701,28 @@ int TsFileIOReader::do_load_all_timeseries_index(
     for (const auto &index_node_entry : index_node_entry_list) {
         int64_t start_offset = index_node_entry.first->get_offset(),
                 end_offset = index_node_entry.second;
-        const std::string target_measurement_name(
-            index_node_entry.first->get_name().to_std_string());
-        ITimeseriesIndex *ts_idx;
-        ret = do_load_timeseries_index(target_measurement_name, start_offset,
-                                       end_offset, in_timeseries_index_pa,
-                                       ts_idx);
-        if (IS_SUCC(ret)) {
+        int32_t read_size = (int32_t)(end_offset - start_offset);
+        int32_t ret_read_len = 0;
+        char *ti_buf = in_timeseries_index_pa.alloc(read_size);
+        if (IS_NULL(ti_buf)) {
+            return E_OOM;
+        }
+        if (RET_FAIL(read_file_->read(start_offset, ti_buf, read_size,
+                                      ret_read_len))) {
+            return ret;
+        }
+        ByteStream bs;
+        bs.wrap_from(ti_buf, read_size);
+        while (bs.has_remaining()) {
+            TimeseriesIndex cur_timeseries_index;
+            if (RET_FAIL(cur_timeseries_index.deserialize_from(
+                    bs, &in_timeseries_index_pa))) {
+                return ret;
+            }
+            if (cur_timeseries_index.get_measurement_name().len_ == 0) continue;
+            void *buf = in_timeseries_index_pa.alloc(sizeof(TimeseriesIndex));
+            auto ts_idx = new (buf) TimeseriesIndex;
+            ts_idx->clone_from(cur_timeseries_index, &in_timeseries_index_pa);
             ts_indexs.push_back(ts_idx);
         }
     }
diff --git a/cpp/test/reader/tree_view/tsfile_reader_tree_test.cc b/cpp/test/reader/tree_view/tsfile_reader_tree_test.cc
index e7466c7b9..477ab24b5 100644
--- a/cpp/test/reader/tree_view/tsfile_reader_tree_test.cc
+++ b/cpp/test/reader/tree_view/tsfile_reader_tree_test.cc
@@ -113,6 +113,7 @@ TEST_F(TsFileTreeReaderTest, ExtendedRowsAndColumnsTest) {
     std::vector<std::string> device_ids = {"device_1", "device_2", "device_3"};
     std::vector<std::string> measurement_ids = {"temperature", "humidity",
                                                 "pressure", "voltage"};
+    std::sort(measurement_ids.begin(), measurement_ids.end());
     std::vector<TSDataType> data_types = {INT64, DOUBLE, FLOAT, INT32};
     std::vector<MeasurementSchema*> measurements;
     for (size_t i = 0; i < measurement_ids.size(); ++i) {
@@ -168,6 +169,12 @@ TEST_F(TsFileTreeReaderTest, ExtendedRowsAndColumnsTest) {
         EXPECT_EQ(read_device_ids[i], device_ids[i]);
     }
 
+    auto device_schema = reader.get_device_schema(device_ids[0]);
+    for (int i = 0; i < measurements.size(); ++i) {
+        EXPECT_EQ(measurements[i]->measurement_name_,
+                  device_schema[i].measurement_name_);
+    }
+
     ResultSet* result;
     int ret =
         reader.query(device_ids, measurement_ids, 0, NUM_ROWS * 1000, result);

From d462d0460c268386014ba1facc2e942a6085b289 Mon Sep 17 00:00:00 2001
From: 761417898 <761417898@qq.com>
Date: Wed, 19 Nov 2025 18:56:39 +0800
Subject: [PATCH 2/2] fix tree-interface get_device_schema

---
 cpp/src/file/tsfile_io_reader.cc | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc
index 4a2810dd4..273f09a44 100644
--- a/cpp/src/file/tsfile_io_reader.cc
+++ b/cpp/src/file/tsfile_io_reader.cc
@@ -714,15 +714,13 @@ int TsFileIOReader::do_load_all_timeseries_index(
         ByteStream bs;
         bs.wrap_from(ti_buf, read_size);
         while (bs.has_remaining()) {
-            TimeseriesIndex cur_timeseries_index;
-            if (RET_FAIL(cur_timeseries_index.deserialize_from(
-                    bs, &in_timeseries_index_pa))) {
-                return ret;
-            }
-            if (cur_timeseries_index.get_measurement_name().len_ == 0) continue;
             void *buf = in_timeseries_index_pa.alloc(sizeof(TimeseriesIndex));
             auto ts_idx = new (buf) TimeseriesIndex;
-            ts_idx->clone_from(cur_timeseries_index, &in_timeseries_index_pa);
+            if (RET_FAIL(
+                    ts_idx->deserialize_from(bs, &in_timeseries_index_pa))) {
+                return ret;
+            }
+            if (ts_idx->get_measurement_name().len_ == 0) continue;
             ts_indexs.push_back(ts_idx);
         }
     }
