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


##########
be/src/runtime/runtime_state.h:
##########
@@ -138,7 +138,41 @@ 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 (default 65535, range [1, 65535]).
+    // Consistent with SessionVariable.batchSize on FE side.
+    MOCK_FUNCTION int batch_size() const {
+        static constexpr int kDefault = 65535;
+        auto v = _query_options.batch_size;
+        return std::min(std::max(1, v), kDefault);
+    }
+
+    // Target byte budget per output block (default 8MB).
+    // FE validates the range [1MB, 512MB] in SessionVariable; BE trusts that
+    // but applies a default when the field is unset.
+    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 (_query_options.__isset.preferred_block_size_bytes) [[likely]] {
+            return std::max(kMin, 
std::min(_query_options.preferred_block_size_bytes, kMax));
+        }
+        return kDefault;

Review Comment:
   `preferred_block_size_bytes()` returning an 8MB default when the thrift 
field is *unset* breaks the feature flag / compatibility contract. In 
mixed-version clusters, an old FE cannot send field 214 at all, but new BEs 
will still enable adaptive sizing because callers like `OlapScanner`, 
`Scanner`, `Union`, `Sort`, etc. read this accessor directly. That changes 
execution semantics during rolling upgrade and also means 
`enable_adaptive_batch_size=false` does not fully disable the feature for paths 
that only consult `RuntimeState`. The unset case needs to preserve legacy 
behavior (typically `0`) unless the feature was explicitly requested by FE.



##########
be/src/storage/iterator/block_reader.cpp:
##########
@@ -476,9 +493,16 @@ Status BlockReader::_agg_key_next_block(Block* block, 
bool* eof) {
         }
 
         if (!_next_row.is_same) {

Review Comment:
   This check is too sparse for the path that actually assembles the final 
block. Because it only runs every 64 groups, a wide-row workload can overshoot 
the byte budget by dozens of rows before the first stop condition is even 
evaluated. There is no later storage-layer trimming step here, so `UNIQUE_KEYS` 
/ `AGG_KEYS` can still return blocks far above the configured target (for 
example, 1MB rows with an 8MB budget still grow to ~64MB before the first 
check). That defeats the core invariant this PR is trying to enforce.



##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -6035,6 +6063,30 @@ public void checkBatchSize(String batchSize) {
         }
     }
 
+    private static final long PREFERRED_BLOCK_SIZE_BYTES_MIN = 1048576L;      
// 1MB
+    private static final long PREFERRED_BLOCK_SIZE_BYTES_MAX = 536870912L;    
// 512MB
+

Review Comment:
   The new API/behavior contract is internally inconsistent here. The thrift 
comment says `preferred_block_size_bytes = 0` disables adaptive sizing, and the 
PR description also documents `SET preferred_block_size_bytes = 0` as the 
per-query off switch. But this validator rejects `0`, and BE currently clamps 
an explicitly sent `0` up to `1MB` instead of honoring it. So users cannot 
actually disable the feature per query as documented, and non-FE clients 
following the thrift contract will get the opposite behavior. Please make FE 
validation, thrift comments, and BE handling agree on the same sentinel 
semantics.



##########
be/src/exec/sort/vsorted_run_merger.cpp:
##########
@@ -196,6 +223,14 @@ Status VSortedRunMerger::get_next(Block* output_block, 
bool* eos) {
                 do_insert();
                 return Status::OK();

Review Comment:
   The multi-run merge path still enforces the byte budget too late. Rows are 
buffered and materialized into `m_block` only every 256 rows, and the byte 
limit is checked only after that batched insert. For wide rows, that means the 
first stop point can already be orders of magnitude above the target (for 
example, ~256MB before checking against an 8MB budget with 1MB rows). Since 
`ExchangeSourceOperator` and the sort sources now wire this budget through, 
this path should enforce it at a finer granularity or with a safe estimate 
before materializing another large batch.



##########
be/src/storage/iterator/vcollect_iterator.cpp:
##########
@@ -488,10 +495,56 @@ Status VCollectIterator::_topn_next(Block* block) {
                << " mutable_block.rows()=" << mutable_block.rows();
     *block = mutable_block.to_block();
 
+    // If byte budget is active and the result exceeds it, split into chunks.
+    size_t preferred_bytes = _reader->preferred_block_size_bytes();
+    if (preferred_bytes > 0 && block->rows() > 1 && block->bytes() > 
preferred_bytes) {
+        _topn_result_block = std::move(*block);
+        _topn_result_offset = 0;
+        return _topn_next_chunk(block);
+    }
+
     _topn_eof = true;
     return block->rows() > 0 ? Status::OK() : Status::Error<END_OF_FILE>("");
 }
 
+Status VCollectIterator::_topn_next_chunk(Block* block) {
+    size_t total_rows = _topn_result_block.rows();
+    if (_topn_result_offset >= total_rows) {
+        _topn_result_block.clear();
+        _topn_eof = true;
+        return Status::Error<END_OF_FILE>("");
+    }
+
+    size_t remaining = total_rows - _topn_result_offset;
+    size_t chunk_rows = remaining;
+
+    // Cap by batch_max_rows.
+    chunk_rows = std::min(chunk_rows, (size_t)_reader->batch_max_rows());
+
+    // Cap by byte budget.
+    size_t preferred_bytes = _reader->preferred_block_size_bytes();

Review Comment:
   This chunking logic uses the *global average* row width of the full TopN 
result to choose `chunk_rows`, but it never checks the actual byte size of the 
prefix being emitted. With skewed row sizes, the first chunk can still exceed 
`preferred_block_size_bytes` by a large margin even when a smaller prefix would 
fit. Example: one very large leading row plus many tiny rows keeps the average 
low, so `chunk_rows` stays > 1 and the first returned chunk still violates the 
budget. To preserve the byte-budget contract, this needs a prefix-size based 
split rather than an average over the whole result block.



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