yiguolei commented on code in PR #62835:
URL: https://github.com/apache/doris/pull/62835#discussion_r3143174823


##########
be/src/exec/scan/file_scanner.cpp:
##########
@@ -220,6 +235,111 @@ Status FileScanner::init(RuntimeState* state, const 
VExprContextSPtrs& conjuncts
     return Status::OK();
 }
 
+bool FileScanner::_should_enable_adaptive_batch_size(TFileFormatType::type 
format_type) const {
+    // Only enable for readers that support set_batch_size().
+    // Table-format wrappers are covered because they delegate to native 
readers.
+    if (!config::enable_adaptive_batch_size) {
+        return false;
+    }
+    switch (format_type) {
+    case TFileFormatType::FORMAT_PARQUET:
+    case TFileFormatType::FORMAT_ORC:
+    case TFileFormatType::FORMAT_CSV_PLAIN:
+    case TFileFormatType::FORMAT_CSV_GZ:
+    case TFileFormatType::FORMAT_CSV_BZ2:
+    case TFileFormatType::FORMAT_CSV_LZ4FRAME:
+    case TFileFormatType::FORMAT_CSV_LZ4BLOCK:
+    case TFileFormatType::FORMAT_CSV_LZOP:
+    case TFileFormatType::FORMAT_CSV_DEFLATE:
+    case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK:
+    case TFileFormatType::FORMAT_PROTO:
+    case TFileFormatType::FORMAT_TEXT:
+    case TFileFormatType::FORMAT_JSON:
+    case TFileFormatType::FORMAT_JNI:
+        return true;
+    default:
+        return false;
+    }
+}
+
+bool FileScanner::_should_run_adaptive_batch_size() const {
+    return _block_size_predictor != nullptr && _get_push_down_agg_type() != 
TPushAggOp::type::COUNT;
+}
+
+void FileScanner::_reset_adaptive_batch_size_state() {
+    _block_size_predictor.reset();
+    _adaptive_batch_output_column_ids.clear();
+    COUNTER_SET(_adaptive_batch_predicted_rows_counter, int64_t(0));
+    COUNTER_SET(_adaptive_batch_actual_bytes_before_truncate_counter, 
int64_t(0));
+    COUNTER_SET(_adaptive_batch_actual_bytes_after_truncate_counter, 
int64_t(0));
+}
+
+void FileScanner::_init_adaptive_batch_size_state(TFileFormatType::type 
format_type) {
+    _reset_adaptive_batch_size_state();
+    if (!_should_enable_adaptive_batch_size(format_type)) {
+        return;
+    }
+
+    // External file readers do not provide reliable memory-size metadata 
hints. Use a small probe
+    // batch so the predictor can learn from real FileScanner output quickly.
+    _block_size_predictor = std::make_unique<AdaptiveBlockSizePredictor>(
+            _state->preferred_block_size_bytes(),
+            _state->preferred_max_column_in_block_size_bytes(), 0.0,
+            std::unordered_map<ColumnId, double> {}, 
ADAPTIVE_BATCH_INITIAL_PROBE_ROWS,
+            _state->batch_size());
+}
+
+size_t FileScanner::_predict_reader_batch_rows() {
+    DCHECK(_block_size_predictor != nullptr);
+    size_t predicted_rows = _block_size_predictor->predict_next_rows();
+    COUNTER_SET(_adaptive_batch_predicted_rows_counter, 
static_cast<int64_t>(predicted_rows));
+    return predicted_rows;
+}
+
+void FileScanner::_ensure_adaptive_batch_output_column_ids(const Block& block) 
{
+    if (!_adaptive_batch_output_column_ids.empty()) {
+        DCHECK_EQ(_adaptive_batch_output_column_ids.size(), block.columns());
+        return;
+    }
+
+    _adaptive_batch_output_column_ids.reserve(block.columns());
+    for (size_t i = 0; i < block.columns(); ++i) {
+        _adaptive_batch_output_column_ids.push_back(static_cast<ColumnId>(i));
+    }
+}
+
+void FileScanner::_update_adaptive_batch_size_before_truncate(const Block& 
block) {
+    if (!_should_run_adaptive_batch_size()) {
+        return;
+    }
+
+    // Learn from the logical bytes before CHAR/VARCHAR truncation. The 
truncated block can be
+    // much smaller than the data the reader and FileScanner have already 
materialized.
+    COUNTER_SET(_adaptive_batch_actual_bytes_before_truncate_counter,
+                static_cast<int64_t>(block.bytes()));
+    if (block.rows() == 0) {
+        return;
+    }
+
+    _ensure_adaptive_batch_output_column_ids(block);
+    // Count a probe only when we actually obtain the first non-empty sample 
that seeds history.
+    if (!_block_size_predictor->has_history()) {
+        COUNTER_UPDATE(_adaptive_batch_probe_count_counter, 1);
+    }
+    _block_size_predictor->update(block, _adaptive_batch_output_column_ids);

Review Comment:
   这里为什么只考虑 output column id?



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