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

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new c56bcc7c15b branch-4.2: [opt](variant) Release materialized V2 writers 
one column at a time (#71108)
c56bcc7c15b is described below

commit c56bcc7c15b41d526530004bcaee3eb30d3fc7c1
Author: lihangyu <[email protected]>
AuthorDate: Fri Dec 4 09:57:14 2026 +0800

    branch-4.2: [opt](variant) Release materialized V2 writers one column at a 
time (#71108)
    
    cherry-pick #71103
---
 be/src/storage/segment/column_writer.h             |   2 +
 be/src/storage/segment/segment_writer.cpp          |   5 +
 be/src/storage/segment/segment_writer.h            |   1 +
 .../segment/variant/v2/variant_column_writer.cpp   |  71 ++++++------
 .../segment/variant/v2/variant_column_writer.h     |   9 +-
 .../segment/variant/variant_column_writer_impl.cpp |  52 ++++++++-
 .../segment/variant/variant_column_writer_impl.h   |   3 +
 be/src/storage/segment/vertical_segment_writer.cpp |   5 +
 be/src/storage/segment/vertical_segment_writer.h   |   1 +
 .../cloud_file_cache_write_index_only_test.cpp     | 124 +++++++++++++++++++++
 .../variant/variant_column_writer_reader_test.cpp  |  66 +++++++++--
 11 files changed, 283 insertions(+), 56 deletions(-)

diff --git a/be/src/storage/segment/column_writer.h 
b/be/src/storage/segment/column_writer.h
index da9e96741b5..a6f0f9f2465 100644
--- a/be/src/storage/segment/column_writer.h
+++ b/be/src/storage/segment/column_writer.h
@@ -96,6 +96,8 @@ struct ColumnWriterOptions {
 
     SegmentFooterPB* footer = nullptr;
     io::FileWriter* file_writer = nullptr;
+    // Count inline index pages that materialized VARIANT paths write 
alongside their data.
+    uint64_t* inline_index_bytes = nullptr;
     CompressionTypePB compression_type = UNKNOWN_COMPRESSION;
     RowsetWriterContext* rowset_ctx = nullptr;
     // For collect segment statistics for compaction
diff --git a/be/src/storage/segment/segment_writer.cpp 
b/be/src/storage/segment/segment_writer.cpp
index 3a55ed11c42..b36de868604 100644
--- a/be/src/storage/segment/segment_writer.cpp
+++ b/be/src/storage/segment/segment_writer.cpp
@@ -318,6 +318,7 @@ Status SegmentWriter::_create_column_writer(uint32_t cid, 
const TabletColumn& co
 
     opts.rowset_ctx = _opts.rowset_ctx;
     opts.file_writer = _file_writer;
+    opts.inline_index_bytes = &_inline_index_bytes;
     opts.compression_type = _opts.compression_type;
     opts.footer = &_footer;
     if (_opts.rowset_ctx != nullptr) {
@@ -1032,6 +1033,10 @@ Status 
SegmentWriter::_finalize_columns_index_impl(uint64_t* index_size) {
         }
     }
     uint64_t file_index_end = _file_writer->bytes_appended();
+    // Readers fetch inline VARIANT indexes through their page pointers. 
Preloading them would
+    // fetch whole cache blocks, including data pages between nearby indexes 
in wide segments.
+    *index_size += _inline_index_bytes;
+    _inline_index_bytes = 0;
     _index_file_cache_info.add_index_range(index_start, file_index_end - 
index_start);
     // reset all column writers and data_conveter
     clear();
diff --git a/be/src/storage/segment/segment_writer.h 
b/be/src/storage/segment/segment_writer.h
index 5b6b7537e58..f5f9a956a2c 100644
--- a/be/src/storage/segment/segment_writer.h
+++ b/be/src/storage/segment/segment_writer.h
@@ -215,6 +215,7 @@ private:
 
     SegmentFooterPB _footer;
     SegmentIndexFileCacheInfo _index_file_cache_info;
+    uint64_t _inline_index_bytes = 0;
     // for mow tables with cluster key, the sort key is the cluster keys not 
unique keys
     // for other tables, the sort key is the keys
     size_t _num_sort_key_columns;
diff --git a/be/src/storage/segment/variant/v2/variant_column_writer.cpp 
b/be/src/storage/segment/variant/v2/variant_column_writer.cpp
index d600c7967c8..d6953dcf7c0 100644
--- a/be/src/storage/segment/variant/v2/variant_column_writer.cpp
+++ b/be/src/storage/segment/variant/v2/variant_column_writer.cpp
@@ -18,6 +18,7 @@
 #include "storage/segment/variant/v2/variant_column_writer.h"
 
 #include <algorithm>
+#include <functional>
 #include <limits>
 #include <utility>
 
@@ -31,6 +32,7 @@
 #include "core/column/variant_v2/column_variant_v2.h"
 #include "exec/common/variant_util.h"
 #include "exprs/function/parse/variant_jsonb_parse.h"
+#include "io/fs/file_writer.h"
 #include "storage/index/index_file_writer.h"
 #include "storage/iterator/olap_data_convertor.h"
 #include "storage/rowset/rowset_writer_context.h"
@@ -248,9 +250,27 @@ Status VariantV2ColumnWriter::_write_materialized(const 
VariantShreddedColumns&
         RETURN_IF_ERROR(variant_writer_helpers::append_sparse_converted_column(
                 tablet_column, writer.get(), converter, current_column_id, 
path_column.type,
                 path_column.column, path_column.rowids, _num_rows));
-        _subcolumn_indexes.push_back(std::move(indexes));
-        _subcolumn_opts.push_back(opts);
-        _subcolumn_writers.push_back(std::move(writer));
+        // Drain this path before the next writer allocates its page and index 
builders.
+        RETURN_IF_ERROR(writer->finish());
+        RETURN_IF_ERROR(writer->write_data());
+        _materialized_raw_data_bytes += writer->get_raw_data_bytes();
+        _materialized_uncompressed_data_bytes += 
writer->get_total_uncompressed_data_pages_bytes();
+        _materialized_compressed_data_bytes += 
writer->get_total_compressed_data_pages_bytes();
+        const uint64_t index_start = _opts.file_writer->bytes_appended();
+        RETURN_IF_ERROR(writer->write_ordinal_index());
+        if (opts.need_zone_map) {
+            RETURN_IF_ERROR(writer->write_zone_map());
+        }
+        if (opts.need_bloom_filter) {
+            RETURN_IF_ERROR(writer->write_bloom_filter_index());
+        }
+        if (_opts.inline_index_bytes != nullptr) {
+            *_opts.inline_index_bytes += _opts.file_writer->bytes_appended() - 
index_start;
+        }
+        if (opts.need_inverted_index) {
+            // Finalize the column index while the segment still owns its 
files for packing.
+            RETURN_IF_ERROR(writer->write_inverted_index());
+        }
     }
     return Status::OK();
 }
@@ -375,19 +395,9 @@ Status VariantV2ColumnWriter::finalize() {
     return Status::OK();
 }
 
-Status VariantV2ColumnWriter::_for_each_column_writer(
-        const std::function<Status(ColumnWriter*)>& function) {
-    DORIS_CHECK(_root_writer != nullptr);
-    RETURN_IF_ERROR(function(_root_writer.get()));
-    for (auto& writer : _subcolumn_writers) {
-        RETURN_IF_ERROR(function(writer.get()));
-    }
-    return Status::OK();
-}
-
 Status VariantV2ColumnWriter::finish() {
     RETURN_IF_ERROR(finalize());
-    RETURN_IF_ERROR(_for_each_column_writer([](ColumnWriter* writer) { return 
writer->finish(); }));
+    RETURN_IF_ERROR(_root_writer->finish());
     if (_binary_writer != nullptr) {
         RETURN_IF_ERROR(_binary_writer->finish());
     }
@@ -396,8 +406,7 @@ Status VariantV2ColumnWriter::finish() {
 
 Status VariantV2ColumnWriter::write_data() {
     RETURN_IF_ERROR(finalize());
-    RETURN_IF_ERROR(
-            _for_each_column_writer([](ColumnWriter* writer) { return 
writer->write_data(); }));
+    RETURN_IF_ERROR(_root_writer->write_data());
     if (_binary_writer != nullptr) {
         RETURN_IF_ERROR(_binary_writer->write_data());
     }
@@ -406,8 +415,7 @@ Status VariantV2ColumnWriter::write_data() {
 
 Status VariantV2ColumnWriter::write_ordinal_index() {
     DORIS_CHECK(_is_finalized);
-    RETURN_IF_ERROR(_for_each_column_writer(
-            [](ColumnWriter* writer) { return writer->write_ordinal_index(); 
}));
+    RETURN_IF_ERROR(_root_writer->write_ordinal_index());
     if (_binary_writer != nullptr) {
         RETURN_IF_ERROR(_binary_writer->write_ordinal_index());
     }
@@ -416,11 +424,6 @@ Status VariantV2ColumnWriter::write_ordinal_index() {
 
 Status VariantV2ColumnWriter::write_zone_map() {
     DORIS_CHECK(_is_finalized);
-    for (size_t index = 0; index < _subcolumn_writers.size(); ++index) {
-        if (_subcolumn_opts[index].need_zone_map) {
-            RETURN_IF_ERROR(_subcolumn_writers[index]->write_zone_map());
-        }
-    }
     if (_binary_writer != nullptr) {
         RETURN_IF_ERROR(_binary_writer->write_zone_map());
     }
@@ -432,11 +435,6 @@ Status VariantV2ColumnWriter::write_inverted_index() {
     for (auto& writer : _values_index_writers) {
         RETURN_IF_ERROR(writer->finish());
     }
-    for (size_t index = 0; index < _subcolumn_writers.size(); ++index) {
-        if (_subcolumn_opts[index].need_inverted_index) {
-            RETURN_IF_ERROR(_subcolumn_writers[index]->write_inverted_index());
-        }
-    }
     if (_binary_writer != nullptr) {
         RETURN_IF_ERROR(_binary_writer->write_inverted_index());
     }
@@ -445,11 +443,6 @@ Status VariantV2ColumnWriter::write_inverted_index() {
 
 Status VariantV2ColumnWriter::write_bloom_filter_index() {
     DORIS_CHECK(_is_finalized);
-    for (size_t index = 0; index < _subcolumn_writers.size(); ++index) {
-        if (_subcolumn_opts[index].need_bloom_filter) {
-            
RETURN_IF_ERROR(_subcolumn_writers[index]->write_bloom_filter_index());
-        }
-    }
     if (_binary_writer != nullptr) {
         RETURN_IF_ERROR(_binary_writer->write_bloom_filter_index());
     }
@@ -466,9 +459,6 @@ uint64_t VariantV2ColumnWriter::estimate_buffer_size() {
         return _outer_nulls->byte_size() + _shredder->byte_size();
     }
     uint64_t size = _root_writer->estimate_buffer_size();
-    for (const auto& writer : _subcolumn_writers) {
-        size += writer->estimate_buffer_size();
-    }
     if (_binary_writer != nullptr) {
         size += _binary_writer->estimate_buffer_size();
     }
@@ -479,8 +469,13 @@ uint64_t VariantV2ColumnWriter::get_total_data_pages_bytes(
         ColumnWriter::DataBytesGetter getter) const {
     DORIS_CHECK(_is_finalized);
     uint64_t size = std::invoke(getter, _root_writer.get());
-    for (const auto& writer : _subcolumn_writers) {
-        size += std::invoke(getter, writer.get());
+    if (getter == &ColumnWriter::get_raw_data_bytes) {
+        size += _materialized_raw_data_bytes;
+    } else if (getter == 
&ColumnWriter::get_total_uncompressed_data_pages_bytes) {
+        size += _materialized_uncompressed_data_bytes;
+    } else {
+        DCHECK(getter == &ColumnWriter::get_total_compressed_data_pages_bytes);
+        size += _materialized_compressed_data_bytes;
     }
     if (_binary_writer != nullptr) {
         size += _binary_writer->get_total_data_pages_bytes(getter);
diff --git a/be/src/storage/segment/variant/v2/variant_column_writer.h 
b/be/src/storage/segment/variant/v2/variant_column_writer.h
index 14bbc9f7c09..fb598c2c775 100644
--- a/be/src/storage/segment/variant/v2/variant_column_writer.h
+++ b/be/src/storage/segment/variant/v2/variant_column_writer.h
@@ -17,7 +17,6 @@
 
 #pragma once
 
-#include <functional>
 #include <memory>
 #include <span>
 #include <vector>
@@ -73,7 +72,6 @@ private:
                                OlapBlockDataConvertor* converter, int& 
column_id);
     Status _write_binary(const VariantShreddedColumns& shredded, 
OlapBlockDataConvertor* converter,
                          int& column_id);
-    Status _for_each_column_writer(const std::function<Status(ColumnWriter*)>& 
function);
 
     ColumnWriterOptions _opts;
     const TabletColumn* _tablet_column = nullptr;
@@ -84,9 +82,10 @@ private:
     bool _is_finalized = false;
 
     std::unique_ptr<ColumnWriter> _root_writer;
-    std::vector<std::unique_ptr<ColumnWriter>> _subcolumn_writers;
-    std::vector<ColumnWriterOptions> _subcolumn_opts;
-    std::vector<TabletIndexes> _subcolumn_indexes;
+    // Keep the data-page statistics after the materialized writers are 
released.
+    uint64_t _materialized_raw_data_bytes = 0;
+    uint64_t _materialized_uncompressed_data_bytes = 0;
+    uint64_t _materialized_compressed_data_bytes = 0;
     std::unique_ptr<VariantBinaryWriter> _binary_writer;
     std::vector<std::unique_ptr<VariantValuesIndexWriter>> 
_values_index_writers;
 };
diff --git a/be/src/storage/segment/variant/variant_column_writer_impl.cpp 
b/be/src/storage/segment/variant/variant_column_writer_impl.cpp
index 2b8f57bd3d3..f69c2219d16 100644
--- a/be/src/storage/segment/variant/variant_column_writer_impl.cpp
+++ b/be/src/storage/segment/variant/variant_column_writer_impl.cpp
@@ -52,6 +52,7 @@
 #include "exec/common/variant_util.h"
 #include "exprs/function/simple_function_factory.h"
 #include "exprs/function_context.h"
+#include "io/fs/file_writer.h"
 #include "runtime/runtime_state.h"
 #include "storage/index/indexed_column_writer.h"
 #include "storage/iterator/olap_data_convertor.h"
@@ -1513,10 +1514,43 @@ Status 
VariantV1ColumnWriter::_process_regular_doc_value_staging(ColumnVariant*
     RETURN_IF_ERROR(execute_doc_write_pipeline(
             *ptr, num_rows, 0 /* materialize selected paths regardless of row 
count */, column_id,
             [this, num_rows, converter](SubcolumnWriteEntry& entry, int& 
materialized_column_id) {
-                return write_materialized_subcolumn(*_tablet_column, 
entry.path, *entry.subcolumn,
-                                                    num_rows, converter, 
materialized_column_id,
-                                                    _opts, 
&_subcolumns_indexes, &_subcolumn_opts,
-                                                    &_subcolumn_writers, 
entry.rowids);
+                const size_t writer_count = _subcolumn_writers.size();
+                RETURN_IF_ERROR(write_materialized_subcolumn(
+                        *_tablet_column, entry.path, *entry.subcolumn, 
num_rows, converter,
+                        materialized_column_id, _opts, &_subcolumns_indexes, 
&_subcolumn_opts,
+                        &_subcolumn_writers, entry.rowids));
+                if (_subcolumn_writers.size() == writer_count) {
+                    return Status::OK();
+                }
+
+                auto* writer = _subcolumn_writers.back().get();
+                const auto& opts = _subcolumn_opts.back();
+                RETURN_IF_ERROR(finish_and_write_column_writer(writer));
+                _materialized_raw_data_bytes += writer->get_raw_data_bytes();
+                _materialized_uncompressed_data_bytes +=
+                        writer->get_total_uncompressed_data_pages_bytes();
+                _materialized_compressed_data_bytes +=
+                        writer->get_total_compressed_data_pages_bytes();
+                const uint64_t index_start = 
_opts.file_writer->bytes_appended();
+                RETURN_IF_ERROR(writer->write_ordinal_index());
+                if (opts.need_zone_map) {
+                    RETURN_IF_ERROR(writer->write_zone_map());
+                }
+                if (opts.need_bloom_filter) {
+                    RETURN_IF_ERROR(writer->write_bloom_filter_index());
+                }
+                if (_opts.inline_index_bytes != nullptr) {
+                    *_opts.inline_index_bytes += 
_opts.file_writer->bytes_appended() - index_start;
+                }
+                if (opts.need_inverted_index) {
+                    RETURN_IF_ERROR(writer->write_inverted_index());
+                }
+                // Release the child before opening the next path. The segment 
keeps the
+                // finalized inverted-index files for packing and writes the 
footer later.
+                _subcolumn_writers.pop_back();
+                _subcolumn_opts.pop_back();
+                _subcolumns_indexes.pop_back();
+                return Status::OK();
             },
             [this, ptr, num_rows, converter, &plan](int& binary_column_id) {
                 if 
(!variant_util::should_write_variant_binary_columns(*_tablet_column)) {
@@ -1967,7 +2001,15 @@ uint64_t VariantColumnWriterImpl::estimate_buffer_size() 
{
 
 uint64_t VariantV1ColumnWriter::get_total_data_pages_bytes(
         ColumnWriter::DataBytesGetter getter) const {
-    uint64_t size = 0;
+    uint64_t size;
+    if (getter == &ColumnWriter::get_raw_data_bytes) {
+        size = _materialized_raw_data_bytes;
+    } else if (getter == 
&ColumnWriter::get_total_uncompressed_data_pages_bytes) {
+        size = _materialized_uncompressed_data_bytes;
+    } else {
+        DCHECK(getter == &ColumnWriter::get_total_compressed_data_pages_bytes);
+        size = _materialized_compressed_data_bytes;
+    }
     if (_root_writer != nullptr) {
         size += std::invoke(getter, _root_writer.get());
     }
diff --git a/be/src/storage/segment/variant/variant_column_writer_impl.h 
b/be/src/storage/segment/variant/variant_column_writer_impl.h
index ad198d32933..6364cb571df 100644
--- a/be/src/storage/segment/variant/variant_column_writer_impl.h
+++ b/be/src/storage/segment/variant/variant_column_writer_impl.h
@@ -236,6 +236,9 @@ private:
     std::unique_ptr<ColumnWriter> _root_writer;
     std::vector<std::unique_ptr<ColumnWriter>> _subcolumn_writers;
     std::vector<ColumnWriterOptions> _subcolumn_opts;
+    uint64_t _materialized_raw_data_bytes = 0;
+    uint64_t _materialized_uncompressed_data_bytes = 0;
+    uint64_t _materialized_compressed_data_bytes = 0;
     std::unique_ptr<VariantBinaryWriter> _binary_writer;
     // hold the references of subcolumns indexes
     std::vector<TabletIndexes> _subcolumns_indexes;
diff --git a/be/src/storage/segment/vertical_segment_writer.cpp 
b/be/src/storage/segment/vertical_segment_writer.cpp
index ee233193252..10d72a4b736 100644
--- a/be/src/storage/segment/vertical_segment_writer.cpp
+++ b/be/src/storage/segment/vertical_segment_writer.cpp
@@ -332,6 +332,7 @@ Status 
VerticalSegmentWriter::_create_column_writer(uint32_t cid, const TabletCo
 
     opts.rowset_ctx = _opts.rowset_ctx;
     opts.file_writer = _file_writer;
+    opts.inline_index_bytes = &_inline_index_bytes;
     opts.compression_type = _opts.compression_type;
     opts.footer = &_footer;
     opts.input_rs_readers = _opts.rowset_ctx->input_rs_readers;
@@ -1400,6 +1401,10 @@ Status 
VerticalSegmentWriter::_finalize_columns_index_impl(uint64_t* index_size)
         *index_size = _file_writer->bytes_appended() - index_start;
     }
     uint64_t file_index_end = _file_writer->bytes_appended();
+    // Readers fetch inline VARIANT indexes through their page pointers. 
Preloading them would
+    // fetch whole cache blocks, including data pages between nearby indexes 
in wide segments.
+    *index_size += _inline_index_bytes;
+    _inline_index_bytes = 0;
     _index_file_cache_info.add_index_range(index_start, file_index_end - 
index_start);
 
     // reset all column writers and data_conveter
diff --git a/be/src/storage/segment/vertical_segment_writer.h 
b/be/src/storage/segment/vertical_segment_writer.h
index 6b4dae32f80..4b3f24b48d9 100644
--- a/be/src/storage/segment/vertical_segment_writer.h
+++ b/be/src/storage/segment/vertical_segment_writer.h
@@ -226,6 +226,7 @@ private:
 
     SegmentFooterPB _footer;
     SegmentIndexFileCacheInfo _index_file_cache_info;
+    uint64_t _inline_index_bytes = 0;
     // for mow tables with cluster key, the sort key is the cluster keys not 
unique keys
     // for other tables, the sort key is the keys
     size_t _num_sort_key_columns;
diff --git a/be/test/storage/cloud_file_cache_write_index_only_test.cpp 
b/be/test/storage/cloud_file_cache_write_index_only_test.cpp
index 1ee187c1a51..bf1d52fee4c 100644
--- a/be/test/storage/cloud_file_cache_write_index_only_test.cpp
+++ b/be/test/storage/cloud_file_cache_write_index_only_test.cpp
@@ -23,14 +23,19 @@
 #include <mutex>
 #include <string>
 #include <string_view>
+#include <tuple>
 #include <utility>
 #include <vector>
 
 #include "cloud/config.h"
 #include "common/config.h"
 #include "core/block/block.h"
+#include "core/column/variant_v2/column_variant_v2.h"
+#include "core/data_type/data_type_variant_v2.h"
+#include "core/data_type_serde/data_type_variant_v2_serde.h"
 #include "cpp/obj-client/s3_obj_storage_client.h"
 #include "cpp/sync_point.h"
+#include "fmt/format.h"
 #include "io/cache/block_file_cache_factory.h"
 #include "io/fs/file_writer.h"
 #include "io/fs/local_file_system.h"
@@ -44,7 +49,11 @@
 #include "storage/rowset/rowset_writer.h"
 #include "storage/rowset/rowset_writer_context.h"
 #include "storage/segment/segment_index_file_cache_loader.h"
+#include "storage/segment/segment_writer.h"
+#include "storage/segment/vertical_segment_writer.h"
 #include "storage/storage_engine.h"
+#include "testutil/variant_util.h"
+#include "util/defer_op.h"
 #include "util/threadpool.h"
 #include "util/time.h"
 
@@ -671,6 +680,121 @@ TEST_F(CloudFileCacheWriteIndexOnlyTest,
     expect_loader_open_file_is_mocked_out(s3_write_counters);
 }
 
+class CloudFileCacheWriteIndexOnlyVariantTest
+        : public CloudFileCacheWriteIndexOnlyTest,
+          public testing::WithParamInterface<std::tuple<bool, bool>> {};
+
+TEST_P(CloudFileCacheWriteIndexOnlyVariantTest, 
InlineIndexesCountTowardSizeWithoutPreloading) {
+    const auto [use_variant_v2, use_vertical_writer] = GetParam();
+    const auto original_parse_mode = config::variant_storage_parse_mode;
+    Defer restore_parse_mode {[&] { config::variant_storage_parse_mode = 
original_parse_mode; }};
+    config::variant_storage_parse_mode = 2; // V1 uses KV staging.
+
+    TabletSchemaPB schema_pb;
+    create_schema()->to_schema_pb(&schema_pb);
+    auto* variant = schema_pb.mutable_column(1);
+    variant->set_type("VARIANT");
+    variant->set_length(0);
+    variant->set_index_length(0);
+    variant->set_is_bf_column(true);
+    variant->set_variant_max_subcolumns_count(1);
+    auto* typed_path = variant->add_children_columns();
+    typed_path->set_unique_id(-1);
+    typed_path->set_name("hot");
+    typed_path->set_type("STRING");
+    typed_path->set_is_nullable(true);
+    typed_path->set_pattern_type(PatternTypePB::MATCH_NAME);
+    auto schema = std::make_shared<TabletSchema>();
+    schema->init_from_pb(schema_pb);
+    auto context = create_context(schema, DataWriteType::TYPE_DIRECT);
+
+    auto block = schema->create_block();
+    auto columns = std::move(block).mutate_columns();
+    if (use_variant_v2) {
+        block.get_by_position(1).type = std::make_shared<DataTypeVariantV2>(1, 
false);
+        columns[1] = ColumnVariantV2::create();
+    }
+    DataTypeVariantV2SerDe serde;
+    DataTypeSerDe::FormatOptions serde_opts;
+    for (int32_t row = 0; row < 4; ++row) {
+        columns[0]->insert_data(reinterpret_cast<const char*>(&row), 
sizeof(row));
+        const std::string json = row == 1 ? "{}" : 
fmt::format(R"({{"hot":"value{}"}})", row);
+        if (use_variant_v2) {
+            Slice slice(json.data(), json.size());
+            auto st = serde.deserialize_one_cell_from_json(*columns[1], slice, 
serde_opts);
+            ASSERT_TRUE(st.ok()) << st;
+        } else {
+            
VariantUtil::insert_root_scalar_field(assert_cast<ColumnVariant&>(*columns[1]),
+                                                  
Field::create_field<TYPE_STRING>(json));
+        }
+    }
+    block.set_columns(std::move(columns));
+
+    const std::string path = fmt::format("{}/inline_variant_indexes.dat", 
kTestDir);
+    io::FileWriterPtr file_writer;
+    ASSERT_TRUE(io::global_local_filesystem()->create_file(path, 
&file_writer).ok());
+    uint64_t tail_start = 0;
+    uint64_t index_size = 0;
+    uint64_t segment_size = 0;
+    segment_v2::SegmentIndexFileCacheInfo preload;
+    if (use_vertical_writer) {
+        segment_v2::VerticalSegmentWriterOptions opts;
+        opts.rowset_ctx = &context;
+        opts.write_type = context.write_type;
+        segment_v2::VerticalSegmentWriter writer(file_writer.get(), 0, schema, 
nullptr, nullptr,
+                                                 opts, nullptr);
+        ASSERT_TRUE(writer.init().ok());
+        ASSERT_TRUE(writer.batch_block(&block, 0, block.rows()).ok());
+        ASSERT_TRUE(writer.write_batch().ok());
+        tail_start = file_writer->bytes_appended();
+        auto st = writer.finalize(&segment_size, &index_size, &preload);
+        ASSERT_TRUE(st.ok()) << st;
+    } else {
+        segment_v2::SegmentWriterOptions opts;
+        opts.rowset_ctx = &context;
+        opts.write_type = context.write_type;
+        segment_v2::SegmentWriter writer(file_writer.get(), 0, schema, 
nullptr, nullptr, opts,
+                                         nullptr);
+        ASSERT_TRUE(writer.init().ok());
+        ASSERT_TRUE(writer.append_block(&block, 0, block.rows()).ok());
+        ASSERT_TRUE(writer.finalize_columns_data().ok());
+        tail_start = file_writer->bytes_appended();
+        auto st = writer.finalize_columns_index(&index_size);
+        ASSERT_TRUE(st.ok()) << st;
+        st = writer.finalize_footer(&segment_size, &preload);
+        ASSERT_TRUE(st.ok()) << st;
+    }
+    ASSERT_TRUE(file_writer->close().ok());
+
+    std::vector<segment_v2::SegmentIndexFileCacheRange> warmed_ranges;
+    SyncPoint::CallbackGuard load_guard;
+    auto* sync_point = SyncPoint::get_instance();
+    sync_point->enable_processing();
+    sync_point->set_call_back(
+            "SegmentIndexFileCacheLoader::load_segment_index_to_file_cache",
+            [&warmed_ranges](auto&& args) {
+                const auto* ctx = try_any_cast<const 
SegmentIndexFileCacheLoadContext*>(args[0]);
+                warmed_ranges.push_back(ctx->range);
+                *try_any_cast_ret<Status>(args) = {Status::OK(), true};
+            },
+            &load_guard);
+    auto st = 
segment_v2::SegmentIndexFileCacheLoader::preload_segment_index_to_file_cache(
+            context, 0, context.segment_path(0), preload);
+    ASSERT_TRUE(st.ok()) << st;
+    ASSERT_EQ(warmed_ranges.size(), 2);
+    EXPECT_EQ(warmed_ranges[0].offset, tail_start);
+    EXPECT_EQ(warmed_ranges[1].offset, tail_start + warmed_ranges[0].size);
+    EXPECT_EQ(warmed_ranges[1].offset + warmed_ranges[1].size, segment_size);
+    EXPECT_GT(index_size, warmed_ranges[0].size);
+}
+
+INSTANTIATE_TEST_SUITE_P(VariantWriters, 
CloudFileCacheWriteIndexOnlyVariantTest,
+                         testing::Combine(testing::Bool(), testing::Bool()), 
[](const auto& info) {
+                             return fmt::format(
+                                     "V{}{}", std::get<0>(info.param) ? 2 : 1,
+                                     std::get<1>(info.param) ? "Vertical" : 
"Horizontal");
+                         });
+
 TEST_F(CloudFileCacheWriteIndexOnlyTest,
        
VerticalCompactionUsesSegmentWriterAndPreloadsAfterAllSegmentFilesClosed) {
     auto tablet_schema = create_schema(true);
diff --git a/be/test/storage/variant/variant_column_writer_reader_test.cpp 
b/be/test/storage/variant/variant_column_writer_reader_test.cpp
index 12d6c047967..67cb73378bd 100644
--- a/be/test/storage/variant/variant_column_writer_reader_test.cpp
+++ b/be/test/storage/variant/variant_column_writer_reader_test.cpp
@@ -1628,6 +1628,15 @@ TEST(VariantColumnIndexBindingTest, 
RenamedRootKeepsStoredSubpathIndexSuffix) {
     }
 }
 
+static void expect_data_and_inline_index_bytes(const ColumnWriter& writer,
+                                               const io::FileWriter& 
file_writer,
+                                               uint64_t inline_index_bytes) {
+    // Materialized paths write inline indexes alongside their data. The 
compressed data count
+    // still includes those paths' data pages and excludes their index pages.
+    EXPECT_EQ(writer.get_total_compressed_data_pages_bytes() + 
inline_index_bytes,
+              file_writer.bytes_appended());
+}
+
 class VariantColumnWriterReaderTest : public testing::Test {
 public:
     // Parent VARIANT column with an extracted VARIANT subcolumn `v.payload`, 
i.e. the layout
@@ -1727,8 +1736,10 @@ protected:
         
ASSERT_TRUE(io::global_local_filesystem()->create_directory(_tablet->tablet_path()).ok());
     }
 
-    RowsetSharedPtr create_variant_rowset(const 
std::vector<std::vector<std::string>>& batches,
-                                          int64_t version, int64_t 
max_rows_per_segment = 200) {
+    RowsetSharedPtr create_variant_rowset(
+            const std::vector<std::vector<std::string>>& batches, int64_t 
version,
+            int64_t max_rows_per_segment = 200,
+            ParseConfig::ParseTo v1_parse_target = ParseConfig {}.parse_to) {
         RowsetWriterContext ctx;
         RowsetId rowset_id;
         rowset_id.init(version + 1000);
@@ -1770,6 +1781,7 @@ protected:
                     json_col->insert_data(json.data(), json.size());
                 }
                 ParseConfig config;
+                config.parse_to = v1_parse_target;
                 variant_util::parse_json_to_variant(*variant_col, *json_col, 
config);
                 columns[0] = std::move(variant_col);
             }
@@ -2069,9 +2081,11 @@ protected:
         rowset_ctx.tablet_path = _tablet->tablet_path();
 
         TabletColumn column = _tablet_schema->column(0);
+        uint64_t inline_index_bytes = 0;
         ColumnWriterOptions opts;
         opts.meta = footer->add_columns();
         opts.index_file_writer = index_file_writer.get();
+        opts.inline_index_bytes = &inline_index_bytes;
         opts.compression_type = CompressionTypePB::LZ4;
         opts.file_writer = file_writer.get();
         opts.footer = footer;
@@ -2107,8 +2121,7 @@ protected:
 
         RETURN_IF_ERROR(writer->finish());
         RETURN_IF_ERROR(writer->write_data());
-        // The file holds only the data pages of this variant so far.
-        EXPECT_EQ(writer->get_total_compressed_data_pages_bytes(), 
file_writer->bytes_appended());
+        expect_data_and_inline_index_bytes(*writer, *file_writer, 
inline_index_bytes);
         RETURN_IF_ERROR(writer->write_ordinal_index());
         RETURN_IF_ERROR(writer->write_zone_map());
         if (index_write_policy == VariantIndexWritePolicy::BLOOM_AND_INVERTED) 
{
@@ -2369,7 +2382,8 @@ protected:
     Status write_storage_parsed_segment(const std::vector<std::string>& jsons,
                                         std::string_view rowset_id, 
SegmentFooterPB* footer,
                                         std::string* file_path, bool 
write_inverted_index = false,
-                                        VariantStorageParseWriteResult* result 
= nullptr) {
+                                        VariantStorageParseWriteResult* result 
= nullptr,
+                                        uint64_t* inline_index_bytes = 
nullptr) {
         if (footer == nullptr || file_path == nullptr) {
             return Status::InvalidArgument("footer or file_path is null");
         }
@@ -2398,6 +2412,7 @@ protected:
         opts.compression_type = CompressionTypePB::LZ4;
         opts.file_writer = file_writer.get();
         opts.footer = footer;
+        opts.inline_index_bytes = inline_index_bytes;
         opts.rowset_ctx = &rowset_ctx;
         _init_column_meta(opts.meta, 0, parent_column, opts);
 
@@ -4565,7 +4580,9 @@ TEST_F(VariantColumnWriterReaderTest, 
test_write_data_normal) {
 
     // 4. create column_writer
     SegmentFooterPB footer;
+    uint64_t inline_index_bytes = 0;
     ColumnWriterOptions opts;
+    opts.inline_index_bytes = &inline_index_bytes;
     opts.meta = footer.add_columns();
     opts.compression_type = CompressionTypePB::LZ4;
     opts.file_writer = file_writer.get();
@@ -4601,8 +4618,7 @@ TEST_F(VariantColumnWriterReaderTest, 
test_write_data_normal) {
     EXPECT_TRUE(st.ok()) << st.msg();
     st = writer->write_data();
     EXPECT_TRUE(st.ok()) << st.msg();
-    // The root, the subcolumns and the sparse columns all count to the 
variant.
-    EXPECT_EQ(writer->get_total_compressed_data_pages_bytes(), 
file_writer->bytes_appended());
+    expect_data_and_inline_index_bytes(*writer, *file_writer, 
inline_index_bytes);
     EXPECT_GT(writer->get_total_uncompressed_data_pages_bytes(), 0);
     EXPECT_GT(writer->get_raw_data_bytes(), 0);
     st = writer->write_ordinal_index();
@@ -6881,6 +6897,36 @@ TEST_F(VariantColumnWriterReaderTest,
     
EXPECT_TRUE(io::global_local_filesystem()->delete_directory(_tablet->tablet_path()).ok());
 }
 
+TEST_F(VariantColumnWriterReaderTest, 
test_v1_doc_value_staging_preserves_footer_data_bytes) {
+    init_variant_tablet(33013, 2);
+    _tablet_schema->mutable_column(0).set_variant_is_v2(false);
+
+    const std::vector<std::vector<std::string>> batches {
+            {R"({"a":1000,"b":"first payload"})", R"({"a":2000,"b":"second 
payload"})"}};
+    auto parse_time_rowset =
+            create_variant_rowset(batches, 1, 200, 
ParseConfig::ParseTo::OnlySubcolumns);
+    auto staging_rowset =
+            create_variant_rowset(batches, 2, 200, 
ParseConfig::ParseTo::OnlyDocValueColumn);
+
+    SegmentFooterPB parse_time_footer;
+    SegmentFooterPB staging_footer;
+    ASSERT_TRUE(read_rowset_metas(parse_time_rowset, &parse_time_footer).ok());
+    ASSERT_TRUE(read_rowset_metas(staging_rowset, &staging_footer).ok());
+    ASSERT_GT(parse_time_footer.columns_size(), 0);
+    ASSERT_NE(find_footer_column_meta_by_relative_path(staging_footer, "a"), 
nullptr);
+    ASSERT_NE(find_footer_column_meta_by_relative_path(staging_footer, "b"), 
nullptr);
+
+    const auto& expected = parse_time_footer.columns(0);
+    const auto& actual = staging_footer.columns(0);
+    ASSERT_EQ(actual.unique_id(), _tablet_schema->column(0).unique_id());
+    ASSERT_GT(expected.raw_data_bytes(), 0);
+    ASSERT_GT(expected.compressed_data_bytes(), 0);
+    ASSERT_GT(expected.uncompressed_data_bytes(), 0);
+    EXPECT_EQ(actual.raw_data_bytes(), expected.raw_data_bytes());
+    EXPECT_EQ(actual.compressed_data_bytes(), 
expected.compressed_data_bytes());
+    EXPECT_EQ(actual.uncompressed_data_bytes(), 
expected.uncompressed_data_bytes());
+}
+
 TEST_F(VariantColumnWriterReaderTest, 
test_storage_parse_kv_reduces_sparse_parse_write_footprint) {
     constexpr size_t kRows = 2048;
     constexpr size_t kDenseKeys = 2;
@@ -6903,15 +6949,19 @@ TEST_F(VariantColumnWriterReaderTest, 
test_storage_parse_kv_reduces_sparse_parse
     }
 
     VariantStorageParseWriteResult kv_result;
+    uint64_t inline_index_bytes = 0;
     {
         ScopedVariantStorageParseMode guard(2);
         SegmentFooterPB footer;
         std::string file_path;
         auto st = write_storage_parsed_segment(jsons, "force_doc_value", 
&footer, &file_path, false,
-                                               &kv_result);
+                                               &kv_result, 
&inline_index_bytes);
         ASSERT_TRUE(st.ok()) << st.to_string();
     }
 
+    ASSERT_GT(inline_index_bytes, 0);
+    EXPECT_LT(inline_index_bytes, kv_result.segment_file_size);
+
     EXPECT_EQ(parse_time_result.num_rows, kRows);
     EXPECT_EQ(kv_result.num_rows, kRows);
     EXPECT_EQ(parse_time_result.doc_value_entries, static_cast<size_t>(0));


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to