Coverage Report

Created: 2026-10-22 01:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/iterator/block_reader.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <parallel_hashmap/phmap.h>
21
#include <stddef.h>
22
#include <sys/types.h>
23
24
#include <cstdint>
25
#include <utility>
26
#include <vector>
27
28
#include "common/config.h"
29
#include "common/status.h"
30
#include "core/block/block.h"
31
#include "core/column/column.h"
32
#include "core/data_type/data_type.h"
33
#include "exprs/aggregate/aggregate_function.h"
34
#include "storage/iterator/vcollect_iterator.h"
35
#include "storage/rowset/rowset_reader.h"
36
#include "storage/tablet/tablet_reader.h"
37
38
namespace doris {
39
class ColumnPredicate;
40
class RuntimeProfile;
41
42
class BlockReader final : public TabletReader {
43
public:
44
    ~BlockReader() override;
45
46
    // Initialize BlockReader with tablet, data version and fetch range.
47
    Status init(const ReaderParams& read_params) override;
48
49
    Status next_block_with_aggregation(Block* block, bool* eof) override;
50
51
0
    void update_profile(RuntimeProfile* profile) override {
52
0
        return _vcollect_iter.update_profile(profile);
53
0
    }
54
55
    // Returns the configured preferred output block byte budget; 0 when adaptive is disabled.
56
296k
    size_t preferred_block_size_bytes() const override {
57
296k
        return config::enable_adaptive_batch_size ? _reader_context.preferred_block_size_bytes : 0;
58
296k
    }
59
60
private:
61
    // Directly read row from rowset and pass to upper caller. No need to do aggregation.
62
    // This is usually used for DUPLICATE KEY tables
63
    Status _direct_next_block(Block* block, bool* eof);
64
    // For normal AGGREGATE KEY tables, read data by a merge heap.
65
    Status _agg_key_next_block(Block* block, bool* eof);
66
    // For UNIQUE KEY tables, read data by a merge heap.
67
    // The difference from _agg_key_next_block is that it will read the data from high version to low version,
68
    // to minimize the comparison time in merge heap.
69
    Status _unique_key_next_block(Block* block, bool* eof);
70
71
    Status _min_delta_next_block(Block* block, bool* eof);
72
73
    Status _detail_change_next_block(Block* block, bool* eof);
74
75
    int64_t _read_changelog_op(const IColumn& col, size_t row) const;
76
77
    Status _write_changelog_op(IColumn& col, int64_t op) const;
78
79
    uint32_t _resolve_source_column_ordinal(uint32_t ordinal, bool use_before) const;
80
81
    bool _min_delta_values_equal(size_t last_row);
82
83
    void _init_pending_row_columns(const Block& block);
84
85
    bool _emit_pending_row(MutableColumns& target_columns, size_t& output_row_count);
86
87
    Status _replace_key_next_block(Block* block, bool* eof);
88
89
    Status _init_collect_iter(const ReaderParams& read_params);
90
91
    Status _init_agg_state(const ReaderParams& read_params);
92
93
    Status _insert_data_normal(MutableColumns& columns);
94
95
    void _compare_sequence_map_and_replace(MutableColumns& columns);
96
97
    // Check if the accumulated output columns have reached the preferred byte budget,
98
    // used to limit the output block size for adaptive batch sizing.
99
    bool _reached_byte_budget(const MutableColumns& columns) const;
100
101
    void _append_agg_data(MutableColumns& columns);
102
103
    void _update_agg_data(MutableColumns& columns);
104
105
    size_t _copy_agg_data();
106
107
    void _update_agg_value(MutableColumns& columns, int begin, int end, bool is_close = true);
108
109
    Status _append_change_row(MutableColumns& target_columns, const Block& src_block,
110
                              size_t row_pos, int64_t output_op, bool use_before);
111
112
    // return false if keys of rowsets are mono ascending and disjoint
113
    bool _rowsets_not_mono_asc_disjoint(const ReaderParams& read_params);
114
115
    VCollectIterator _vcollect_iter;
116
    IteratorRowRef _next_row {{}, -1, false};
117
118
    std::vector<AggregateFunctionPtr> _agg_functions;
119
    std::vector<AggregateDataPtr> _agg_places;
120
121
    // Read-schema ordinals of the non-key columns of AGG tables, folded
122
    // through the aggregate machinery.
123
    std::vector<uint32_t> _agg_columns_idx;
124
125
    std::vector<int> _agg_data_counters;
126
    int _last_agg_data_counter = 0;
127
128
    // Buffer of consecutive rows that share the same primary key, used by
129
    // _min_delta_next_block to fold INSERT/UPDATE/DELETE into a single net change.
130
    // Rows are appended as the merge iterator advances and cleared after each key group.
131
    MutableColumns _stored_data_columns;
132
    std::vector<IteratorRowRef> _stored_row_ref;
133
134
    std::vector<bool> _stored_has_null_tag;
135
    std::vector<bool> _stored_has_variable_length_tag;
136
137
    // One-row carry-over buffer holding the AFTER row of an UPDATE pair when the BEFORE row
138
    // was already emitted on the boundary of batch_max_rows(). Flushed by _emit_pending_row()
139
    // at the start of the next call to *_next_block.
140
    MutableColumns _pending_row_columns;
141
    bool _has_pending_row = false;
142
143
    phmap::flat_hash_map<const Block*, std::vector<std::pair<int, int>>> _temp_ref_map;
144
145
    bool _eof = false;
146
147
    Status (BlockReader::*_next_block_func)(Block* block, bool* eof) = nullptr;
148
149
    ColumnPtr _delete_filter_column;
150
151
    bool _is_rowsets_overlapping = true;
152
153
    // Unsupported compare_at means equality cannot be proven, so MIN_DELTA conservatively keeps
154
    // UPDATE rows. Column types are stable within a reader; cache this after the first exception.
155
    bool _min_delta_value_compare_unsupported = false;
156
    Arena _arena;
157
};
158
159
} // namespace doris