mrhhsg commented on code in PR #62835:
URL: https://github.com/apache/doris/pull/62835#discussion_r3143274078
##########
be/src/format/csv/csv_reader.cpp:
##########
@@ -465,7 +480,33 @@ 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;
Review Comment:
每读一行数据都会马上解析填充到 block 中,读到的 size 和填充后的 block 的 bytes 不一定一致,有可能填充后的 block 会变大
##########
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:
因为在 file scanner 不像内表有列的统计信息记录在 meta
信息中,所以这里_adaptive_batch_output_column_ids 就是读到的 block 的所有列的 0-N 的映射。
内表里面,需要将 block 中的列和 meta 中的列对应起来,这个字段才有实际的意义。
##########
be/src/runtime/runtime_state.h:
##########
@@ -138,7 +138,56 @@ 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` when adaptive
+ // is disabled by BE config so the value is always a legal byte budget;
callers that need
+ // to know whether adaptive batch size is active should test
+ // `config::enable_adaptive_batch_size` explicitly.
+ 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` when adaptive
+ // is disabled by BE config so the value is always a legal byte budget;
callers that need
+ // to know whether adaptive batch size is active should test
+ // `config::enable_adaptive_batch_size` explicitly.
+ MOCK_FUNCTION size_t preferred_max_column_in_block_size_bytes() const {
Review Comment:
单列过大更容易造成 cache miss,可以通过这个值让以整列数据全部落在 L2 缓存里
--
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]