github-actions[bot] commented on code in PR #62263:
URL: https://github.com/apache/doris/pull/62263#discussion_r3128042090


##########
be/src/storage/merger.cpp:
##########
@@ -548,13 +564,114 @@ Status Merger::vertical_merge_rowsets(BaseTabletSPtr 
tablet, ReaderType reader_t
         std::unique_lock<std::mutex> lock(sample_info_lock);
         sample_infos.resize(column_groups.size());
     }
+    // Collect per-column raw_data_bytes from segment footer for first-time 
batch size estimation.
+    // raw_data_bytes is the original data size before encoding, close to 
runtime Block::bytes().
+    // Only collect when needed: skip if manual batch_size override is set, or 
if ALL groups
+    // already have historical sampling data. Use per-group granularity so 
that schema evolution
+    // (new groups without history) still gets footer-based estimation.
+    struct ColumnRawSizeInfo {
+        int64_t total_raw_bytes = 0;
+        int64_t rows_with_data = 0;
+    };
+    std::unordered_map<int32_t, ColumnRawSizeInfo> column_raw_sizes;
+    bool need_footer_collection = false;
+    if (config::compaction_batch_size == -1) {
+        std::unique_lock<std::mutex> lock(sample_info_lock);
+        for (const auto& info : sample_infos) {
+            if (info.group_data_size <= 0 && info.bytes <= 0 && info.rows <= 
0) {
+                need_footer_collection = true;
+                break;
+            }
+        }
+    }
+    if (need_footer_collection) {
+        for (const auto& rs_reader : src_rowset_readers) {
+            auto beta_rowset = 
std::dynamic_pointer_cast<BetaRowset>(rs_reader->rowset());
+            if (!beta_rowset) {
+                continue;
+            }
+            std::vector<segment_v2::SegmentSharedPtr> segments;
+            auto st = beta_rowset->load_segments(&segments);
+            if (!st.ok()) {
+                LOG(WARNING) << "Failed to load segments for footer 
raw_data_bytes collection"
+                             << ", tablet_id: " << tablet->tablet_id()
+                             << ", rowset_id: " << beta_rowset->rowset_id() << 
", status: " << st;
+                continue;
+            }
+            for (const auto& segment : segments) {
+                int64_t row_count = segment->num_rows();
+                auto collect_st = segment->traverse_column_meta_pbs(
+                        [&](const segment_v2::ColumnMetaPB& meta) {
+                            int32_t uid = meta.unique_id();
+                            if (uid >= 0 && meta.has_raw_data_bytes()) {
+                                auto& info = column_raw_sizes[uid];
+                                info.total_raw_bytes += meta.raw_data_bytes();
+                                info.rows_with_data += row_count;
+                            }
+                        });
+                if (!collect_st.ok()) {
+                    LOG(WARNING) << "Failed to traverse column meta for footer 
collection"
+                                 << ", tablet_id: " << tablet->tablet_id()
+                                 << ", status: " << collect_st;
+                }
+            }
+        }
+    }
+
+    // Pre-compute per-row estimate for each column group from footer data.
+    std::vector<int64_t> group_per_row_from_footer(column_groups.size(), 0);
+    std::vector<bool> group_footer_fallback(column_groups.size(), false);
+    for (size_t i = 0; i < column_groups.size(); ++i) {
+        int64_t group_per_row = 0;
+        bool need_fallback = false;
+        for (uint32_t col_ordinal : column_groups[i]) {
+            const auto& col = tablet_schema.column(col_ordinal);
+            int32_t uid = col.unique_id();
+
+            // Variant columns (root or subcolumn): raw_data_bytes is 0 (TODO 
in writer),
+            // cannot estimate from footer, fallback to default for the entire 
group.
+            if (uid < 0 || col.is_variant_type()) {
+                need_fallback = true;
+                break;
+            }
+
+            int64_t col_per_row = 0;
+            auto it = column_raw_sizes.find(uid);
+            if (it != column_raw_sizes.end() && it->second.rows_with_data > 0) 
{
+                col_per_row = it->second.total_raw_bytes / 
it->second.rows_with_data;
+
+                // Structural overhead compensation for scalar columns only.
+                // Only add when footer data actually exists for this column,
+                // to avoid creating a non-zero estimate purely from 
structural overhead.
+                // Complex types (ARRAY/MAP/STRUCT) have raw_data_bytes 
recursively aggregated
+                // from sub-writers, so no additional compensation is needed.
+                if (col.type() != FieldType::OLAP_FIELD_TYPE_ARRAY &&

Review Comment:
   `raw_data_bytes / rows_with_data` only reflects serialized payload for 
non-null values, but the compaction reader still materializes the full nested 
column for null rows. `ScalarColumnWriter::append_nulls()` does not increase 
`_raw_data_bytes`, while `FileColumnIterator::next_batch()` handles a null run 
through `ColumnNullable::insert_many_defaults()`, which allocates both the null 
map and the nested default vector.
   
   So a nullable fixed-width column is underestimated here whenever nulls are 
present: for example, a nullable `INT` with many nulls is estimated close to `1 
+ payload_of_non_null_rows / total_rows` bytes per row, while the runtime block 
still needs about `1 + 4` bytes per row. A first vertical compaction of a 
sparse wide table can still pick a much too large `batch_size` and reproduce 
the OOM this patch is trying to avoid.



##########
be/src/storage/merger.cpp:
##########
@@ -548,13 +564,114 @@ Status Merger::vertical_merge_rowsets(BaseTabletSPtr 
tablet, ReaderType reader_t
         std::unique_lock<std::mutex> lock(sample_info_lock);
         sample_infos.resize(column_groups.size());
     }
+    // Collect per-column raw_data_bytes from segment footer for first-time 
batch size estimation.
+    // raw_data_bytes is the original data size before encoding, close to 
runtime Block::bytes().
+    // Only collect when needed: skip if manual batch_size override is set, or 
if ALL groups
+    // already have historical sampling data. Use per-group granularity so 
that schema evolution
+    // (new groups without history) still gets footer-based estimation.
+    struct ColumnRawSizeInfo {
+        int64_t total_raw_bytes = 0;
+        int64_t rows_with_data = 0;
+    };
+    std::unordered_map<int32_t, ColumnRawSizeInfo> column_raw_sizes;
+    bool need_footer_collection = false;
+    if (config::compaction_batch_size == -1) {
+        std::unique_lock<std::mutex> lock(sample_info_lock);
+        for (const auto& info : sample_infos) {
+            if (info.group_data_size <= 0 && info.bytes <= 0 && info.rows <= 
0) {
+                need_footer_collection = true;
+                break;
+            }
+        }
+    }
+    if (need_footer_collection) {
+        for (const auto& rs_reader : src_rowset_readers) {
+            auto beta_rowset = 
std::dynamic_pointer_cast<BetaRowset>(rs_reader->rowset());
+            if (!beta_rowset) {
+                continue;
+            }
+            std::vector<segment_v2::SegmentSharedPtr> segments;
+            auto st = beta_rowset->load_segments(&segments);
+            if (!st.ok()) {
+                LOG(WARNING) << "Failed to load segments for footer 
raw_data_bytes collection"
+                             << ", tablet_id: " << tablet->tablet_id()
+                             << ", rowset_id: " << beta_rowset->rowset_id() << 
", status: " << st;
+                continue;
+            }
+            for (const auto& segment : segments) {
+                int64_t row_count = segment->num_rows();

Review Comment:
   This silently treats `raw_data_bytes` as if it were present on every input 
segment. That is not true for rowsets written before this footer field existed, 
so on the first compaction after a rolling upgrade you can easily have a mix of 
old and new segments here. In that case `rows_with_data` is accumulated only 
from the subset that has the field, but the estimator still uses the result for 
the whole merge input instead of falling back.
   
   That makes the batch size depend on an incomplete sample of rowsets, which 
can under- or over-estimate badly depending on which segments happen to carry 
`raw_data_bytes`. This path should treat any missing footer data for a 
column/group as a fallback case rather than averaging only the available subset.



-- 
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]


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

Reply via email to