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


##########
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 (_state->preferred_block_size_bytes() == 0) {
+        return false;
+    }

Review Comment:
   _should_enable_adaptive_batch_size only checks preferred_block_size_bytes() 
== 0, but RuntimeState currently returns a non-zero value even when 
config::enable_adaptive_batch_size is false. That means adaptive batching may 
still run when the BE config is disabled. Please gate this on 
config::enable_adaptive_batch_size (or rely on preferred_block_size_bytes() 
returning 0 when disabled) so the config flag reliably disables the feature.



##########
be/src/format/csv/csv_reader.cpp:
##########
@@ -377,26 +380,25 @@ Status CsvReader::_do_init_reader(ReaderInitContext* 
base_ctx) {
     return Status::OK();
 }
 
+void CsvReader::set_batch_size(size_t batch_size) {
+    _batch_size = batch_size;
+}
+
 // !FIXME: Here we should use MutableBlock
 Status CsvReader::_do_get_next_block(Block* block, size_t* read_rows, bool* 
eof) {
     if (_line_reader_eof) {
         *eof = true;
         return Status::OK();
     }
 
-    const int batch_size = std::max(_state->batch_size(), 
(int)_MIN_BATCH_SIZE);
-    const int64_t max_block_bytes =
-            (_state->query_type() == TQueryType::LOAD && 
config::load_reader_max_block_bytes > 0)
-                    ? config::load_reader_max_block_bytes
-                    : 0;
+    const size_t batch_size = _batch_size;
+    const auto max_block_bytes = _state->preferred_block_size_bytes();

Review Comment:
   set_batch_size() stores the value directly into _batch_size, and 
_do_get_next_block uses it as the loop upper bound. If _batch_size is set to 0 
(the header comment says 0 means “not set”), the reader will return empty 
blocks and also sets `*eof = (rows == 0)` which can incorrectly signal EOF. 
Please enforce batch_size >= 1 (or interpret 0 as “use 
state->batch_size()/default”) inside set_batch_size() or before the read loop.



##########
be/src/format/csv/csv_reader.cpp:
##########
@@ -465,7 +476,30 @@ Status CsvReader::_do_get_next_block(Block* block, size_t* 
read_rows, bool* eof)
                 continue;
             }
             RETURN_IF_ERROR(_fill_dest_columns(Slice(ptr, size), block, 
columns, &rows));
-            block_bytes += size;
+
+            // Adaptive block-size guard:
+            // Instead of checking bytes on every appended row, we probe at 
sampled row counts.
+            // Each probe estimates bytes-per-row and schedules the next probe 
closer to the
+            // expected byte limit, so we keep overhead low while still 
stopping near max size.
+            if (max_block_bytes > 0 && rows == next_checking_rows) {
+                // Check block size only at sampled row counts to avoid 
per-row byte accounting cost.
+                const auto bytes = get_block_bytes();
+                if (bytes == 0) {
+                    // Early rows may still report zero bytes (e.g. constant 
columns), so retry soon.
+                    next_checking_rows++;
+                    continue;
+                } else if (bytes >= max_block_bytes) {
+                    // Stop filling this block once the configured byte limit 
is reached.
+                    break;
+                }
+
+                // Predict how many more rows can be appended before hitting 
max_block_bytes.
+                const auto remaining_bytes = max_block_bytes - bytes;
+                const auto bytes_per_row = bytes / rows;

Review Comment:
   bytes_per_row is computed as `bytes / rows` and can evaluate to 0 when 
bytes>0 but bytes<rows (e.g. constant/low-footprint columns), which would make 
`remaining_bytes / bytes_per_row` divide by zero. Please clamp bytes_per_row to 
at least 1 (or handle bytes_per_row==0) before computing remaining_rows.
   ```suggestion
                   const auto bytes_per_row = std::max<size_t>(1, bytes / rows);
   ```



##########
be/src/runtime/runtime_state.h:
##########
@@ -138,7 +138,52 @@ class RuntimeState {
 
     const DescriptorTbl& desc_tbl() const { return *_desc_tbl; }
     void set_desc_tbl(const DescriptorTbl* desc_tbl) { _desc_tbl = desc_tbl; }
-    MOCK_FUNCTION int batch_size() const { return _query_options.batch_size; }
+
+    // Row-count limit for output blocks. Clamp to [1, 65535].
+    // Adaptive byte budgeting still uses this as the hard row ceiling.
+    MOCK_FUNCTION int batch_size() const {
+        static constexpr int kMax = 65535;
+        auto v = _query_options.batch_size;
+        return std::min(std::max(1, v), kMax);
+    }
+
+    // Target byte budget per output block (default 8MB when adaptive is 
enabled).
+    // The public FE/session contract is [1MB, 512MB]; this accessor still 
clamps any direct
+    // thrift or mixed-version out-of-range value into that range. Returns 
`kMax` only when adaptive
+    // is disabled by BE config.
+    MOCK_FUNCTION size_t preferred_block_size_bytes() const {
+        static constexpr int64_t kDefault = 8388608L; // 8MB
+        static constexpr int64_t kMax = 536870912L;   // 512MB
+        static constexpr int64_t kMin = 1048576L;     // 1MB
+        if (!config::enable_adaptive_batch_size) [[unlikely]] {
+            return kMax;
+        }
+        if (_query_options.__isset.preferred_block_size_bytes) [[likely]] {
+            return std::max<int64_t>(
+                    kMin, 
std::min<int64_t>(_query_options.preferred_block_size_bytes, kMax));
+        }
+        return kDefault;
+    }
+
+    // Per-column byte limit for adaptive chunk rows (default 1MB when 
adaptive is enabled).
+    // The public FE/session contract is [256KB, 128MB]; this accessor still 
clamps any direct
+    // thrift or mixed-version out-of-range value into that range. Returns 
`kMax` only when adaptive
+    // is disabled by BE config.
+    MOCK_FUNCTION size_t preferred_max_column_in_block_size_bytes() const {
+        static constexpr int64_t kDefault = 1048576L; // 1MB
+        static constexpr int64_t kMax = 134217728L;   // 128MB
+        static constexpr int64_t kMin = 262144L;      // 256KB
+        if (!config::enable_adaptive_batch_size) [[unlikely]] {
+            return kMax;
+        }
+        if (_query_options.__isset.preferred_max_column_in_block_size_bytes) 
[[likely]] {
+            return std::max<int64_t>(
+                    kMin, 
std::min<int64_t>(_query_options.preferred_max_column_in_block_size_bytes,
+                                            kMax));
+        }
+        return kDefault;
+    }

Review Comment:
   preferred_block_size_bytes()/preferred_max_column_in_block_size_bytes() 
return the *max clamp* when config::enable_adaptive_batch_size is false. 
Several call sites (e.g. FileScanner::_should_enable_adaptive_batch_size) use a 
return value of 0 to mean “disabled”, so returning 512MB/128MB can 
inadvertently keep adaptive logic active and change behavior even when the BE 
config is off. Recommend returning 0 when adaptive is disabled (and updating 
call sites/tests accordingly), keeping clamping only for non-zero values.



##########
regression-test/suites/unique_with_mow_c_p0/test_compact_multi_segments.groovy:
##########
@@ -45,13 +45,16 @@ suite("test_compact_multi_segments", "nonConcurrent") {
 
     // batch_size is 4164 in csv_reader.cpp
     // _batch_size is 8192 in vtablet_writer.cpp
+
     def backendId_to_params = get_be_param("doris_scanner_row_bytes")
     onFinish {
         GetDebugPoint().disableDebugPointForAllBEs("MemTable.need_flush")
         set_original_be_param("doris_scanner_row_bytes", backendId_to_params)
+        set_be_param.call("enable_adaptive_batch_size", "true")
     }

Review Comment:
   onFinish unconditionally sets enable_adaptive_batch_size=true, but the suite 
disables it during the test. This can leak state into subsequent tests because 
it doesn’t restore the prior value. Please save the original 
enable_adaptive_batch_size BE param and restore it in onFinish (or use 
reset_be_param).



##########
be/src/format/json/new_json_reader.cpp:
##########
@@ -273,6 +276,27 @@ Status NewJsonReader::_do_get_next_block(Block* block, 
size_t* read_rows, bool*
             continue;
         }
         ++(*read_rows);
+
+        // Adaptive block-size guard:
+        // Instead of checking bytes on every appended row, we probe at 
sampled row counts.
+        // Each probe estimates bytes-per-row and schedules the next probe 
closer to the
+        // expected byte limit, so we keep overhead low while still stopping 
near max size.
+        if (block->rows() > 0 && max_block_bytes > 0 && *read_rows == 
next_checking_rows) {
+            const auto bytes = block->bytes();
+
+            if (bytes == 0) {
+                next_checking_rows++;
+                continue;
+            }
+            if (bytes >= max_block_bytes) {
+                break;
+            }
+
+            const auto remaining_bytes = max_block_bytes - bytes;
+            const auto bytes_per_row = bytes / block->rows();

Review Comment:
   bytes_per_row is computed as `bytes / block->rows()` and can become 0 for 
low-byte constant columns (bytes>0 but bytes<rows), which would lead to 
division-by-zero when computing remaining_rows. Please clamp bytes_per_row to 
at least 1 (similar to other places using avg_row_bytes) before dividing, or 
handle the 0 case explicitly.
   ```suggestion
               const auto bytes_per_row = std::max<size_t>(1, bytes / 
block->rows());
   ```



##########
be/src/format/json/new_json_reader.cpp:
##########
@@ -242,20 +244,21 @@ Status NewJsonReader::_do_init_reader(ReaderInitContext* 
base_ctx) {
     return Status::OK();
 }
 
+void NewJsonReader::set_batch_size(size_t batch_size) {
+    _batch_size = batch_size;
+}
+
 Status NewJsonReader::_do_get_next_block(Block* block, size_t* read_rows, 
bool* eof) {
     if (_reader_eof) {
         *eof = true;
         return Status::OK();
     }
 
-    const int batch_size = std::max(_state->batch_size(), 
(int)_MIN_BATCH_SIZE);
-    const int64_t max_block_bytes =
-            (_state->query_type() == TQueryType::LOAD && 
config::load_reader_max_block_bytes > 0)
-                    ? config::load_reader_max_block_bytes
-                    : 0;
+    const auto batch_size = _batch_size;
+    const auto max_block_bytes = _state->preferred_block_size_bytes();
 
-    while (block->rows() < batch_size && !_reader_eof &&
-           (max_block_bytes <= 0 || (int64_t)block->bytes() < 
max_block_bytes)) {
+    size_t next_checking_rows = *read_rows + 1;
+    while (block->rows() < batch_size && !_reader_eof) {
         if (UNLIKELY(_read_json_by_line && _skip_first_line)) {

Review Comment:
   set_batch_size() allows setting _batch_size to 0, but _do_get_next_block 
uses `while (block->rows() < batch_size ...)`. If batch_size becomes 0, this 
loop never runs and the method returns without setting eof, which can cause the 
scanner to spin with empty blocks. Please enforce batch_size >= 1 (or treat 0 
as “reset to default”, e.g. max(state->batch_size(), _MIN_BATCH_SIZE)).



##########
regression-test/suites/unique_with_mow_c_p0/test_schema_change_add_key_column.groovy:
##########
@@ -60,10 +60,12 @@ suite("test_schema_change_add_key_column", "nonConcurrent") 
{
     onFinish {
         GetDebugPoint().clearDebugPointsForAllBEs()
         set_original_be_param("doris_scanner_row_bytes", backendId_to_params)
+        set_be_param.call("enable_adaptive_batch_size", "true")
     }

Review Comment:
   onFinish unconditionally sets enable_adaptive_batch_size=true, but the test 
body sets it to false. This doesn’t restore the original BE config and can leak 
into later suites (especially if a prior suite intentionally disabled it). 
Please capture the original enable_adaptive_batch_size value (similar to 
doris_scanner_row_bytes) and restore it in onFinish (or call reset_be_param).



##########
be/src/storage/tablet/tablet_reader.h:
##########
@@ -31,6 +31,7 @@
 #include <vector>
 
 #include "agent/be_exec_version_manager.h"
+#include "common/config.h"
 #include "common/status.h"
 #include "exprs/function_filter.h"

Review Comment:
   common/config.h is included here but not used anywhere in this header (only 
referenced in a comment). Consider dropping the include to reduce transitive 
dependencies and compile time.



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