be/src/format/parquet/vparquet_group_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 | | #pragma once |
18 | | #include <stddef.h> |
19 | | #include <stdint.h> |
20 | | |
21 | | #include <limits> |
22 | | #include <memory> |
23 | | #include <string> |
24 | | #include <tuple> |
25 | | #include <unordered_map> |
26 | | #include <unordered_set> |
27 | | #include <utility> |
28 | | #include <vector> |
29 | | |
30 | | #include "common/status.h" |
31 | | #include "core/block/block.h" |
32 | | #include "core/column/column.h" |
33 | | #include "exprs/vexpr_fwd.h" |
34 | | #include "format/parquet/parquet_common.h" |
35 | | #include "format/parquet/vparquet_column_reader.h" |
36 | | #include "format/table/table_format_reader.h" |
37 | | #include "format/table/table_schema_change_helper.h" |
38 | | #include "io/fs/file_reader_writer_fwd.h" |
39 | | #include "storage/id_manager.h" |
40 | | #include "storage/segment/common.h" |
41 | | #include "vparquet_column_reader.h" |
42 | | |
43 | | namespace cctz { |
44 | | class time_zone; |
45 | | } // namespace cctz |
46 | | namespace doris { |
47 | | class ObjectPool; |
48 | | class RowDescriptor; |
49 | | class RuntimeState; |
50 | | class SlotDescriptor; |
51 | | class TupleDescriptor; |
52 | | |
53 | | namespace io { |
54 | | struct IOContext; |
55 | | } // namespace io |
56 | | class Block; |
57 | | class FieldDescriptor; |
58 | | struct RowLineageColumns; |
59 | | } // namespace doris |
60 | | namespace tparquet { |
61 | | class ColumnMetaData; |
62 | | class OffsetIndex; |
63 | | class RowGroup; |
64 | | } // namespace tparquet |
65 | | |
66 | | namespace doris::segment_v2 { |
67 | | class RowIdColumnIteratorV2; |
68 | | } |
69 | | |
70 | | namespace doris { |
71 | | // TODO: we need to determine it by test. |
72 | | |
73 | | class RowGroupReader : public ProfileCollector, public RowPositionProvider { |
74 | | public: |
75 | | std::shared_ptr<TableSchemaChangeHelper::Node> _table_info_node_ptr; |
76 | | static const std::vector<int64_t> NO_DELETE; |
77 | | |
78 | | struct RowGroupIndex { |
79 | | int32_t row_group_id; |
80 | | int64_t first_row; |
81 | | int64_t last_row; |
82 | | RowGroupIndex(int32_t id, int64_t first, int64_t last) |
83 | 268 | : row_group_id(id), first_row(first), last_row(last) {} |
84 | | }; |
85 | | |
86 | | // table name |
87 | | struct LazyReadContext { |
88 | | // all conjuncts: in sql, join runtime filter, topn runtime filter. |
89 | | VExprContextSPtrs conjuncts; |
90 | | |
91 | | phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>> |
92 | | slot_id_to_predicates; |
93 | | bool can_lazy_read = false; |
94 | | // block->rows() returns the number of rows of the first column, |
95 | | // so we should check and resize the first column |
96 | | bool resize_first_column = true; |
97 | | std::vector<std::string> all_read_columns; |
98 | | // include predicate_partition_columns & predicate_missing_columns |
99 | | std::vector<uint32_t> all_predicate_col_ids; |
100 | | // save slot_id to find dict filter column name, because expr column name may |
101 | | // be different with parquet column name |
102 | | // std::pair<std::vector<col_name>, std::vector<slot_id>> |
103 | | std::pair<std::vector<std::string>, std::vector<int>> predicate_columns; |
104 | | std::vector<std::string> lazy_read_columns; |
105 | | std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>> |
106 | | predicate_partition_columns; |
107 | | // lazy read partition columns or all partition columns |
108 | | std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>> |
109 | | partition_columns; |
110 | | std::unordered_map<std::string, VExprContextSPtr> predicate_missing_columns; |
111 | | VExprContextSPtrs missing_columns_conjuncts; |
112 | | // lazy read missing columns or all missing columns |
113 | | std::unordered_map<std::string, VExprContextSPtr> missing_columns; |
114 | | // should turn off filtering by page index, lazy read and dict filter if having complex type |
115 | | bool has_complex_type = false; |
116 | | |
117 | | // ColumnProcessor path: column name lists for each category. |
118 | | // Predicate phase: columns involved in predicate filtering. |
119 | | std::vector<std::string> predicate_partition_col_names; |
120 | | std::vector<std::string> predicate_missing_col_names; |
121 | | // Remaining phase: columns filled after lazy reads. |
122 | | std::vector<std::string> partition_col_names; |
123 | | std::vector<std::string> missing_col_names; |
124 | | }; |
125 | | |
126 | | /** |
127 | | * Support row-level delete in iceberg: |
128 | | * https://iceberg.apache.org/spec/#position-delete-files |
129 | | */ |
130 | | struct PositionDeleteContext { |
131 | | // the filtered rows in current row group |
132 | | const std::vector<int64_t>& delete_rows; |
133 | | // the first row id of current row group in parquet file |
134 | | const int64_t first_row_id; |
135 | | // the number of rows in current row group |
136 | | const int64_t num_rows; |
137 | | const int64_t last_row_id; |
138 | | // current row id to read in the row group |
139 | | int64_t current_row_id; |
140 | | // start index in delete_rows |
141 | | const int64_t start_index; |
142 | | // end index in delete_rows |
143 | | const int64_t end_index; |
144 | | // current index in delete_rows |
145 | | int64_t index; |
146 | | const bool has_filter; |
147 | | |
148 | | PositionDeleteContext(const std::vector<int64_t>& delete_rows, const int64_t num_rows, |
149 | | const int64_t first_row_id, const int64_t start_index, |
150 | | const int64_t end_index) |
151 | 46 | : delete_rows(delete_rows), |
152 | 46 | first_row_id(first_row_id), |
153 | 46 | num_rows(num_rows), |
154 | 46 | last_row_id(first_row_id + num_rows), |
155 | 46 | current_row_id(first_row_id), |
156 | 46 | start_index(start_index), |
157 | 46 | end_index(end_index), |
158 | 46 | index(start_index), |
159 | 46 | has_filter(end_index > start_index) {} |
160 | | |
161 | | PositionDeleteContext(const int64_t num_rows, const int64_t first_row) |
162 | 46 | : PositionDeleteContext(NO_DELETE, num_rows, first_row, 0, 0) {} |
163 | | |
164 | | PositionDeleteContext(const PositionDeleteContext& filter) = default; |
165 | | }; |
166 | | |
167 | | RowGroupReader(io::FileReaderSPtr file_reader, const std::vector<std::string>& read_columns, |
168 | | const int32_t row_group_id, const tparquet::RowGroup& row_group, |
169 | | const cctz::time_zone* ctz, io::IOContext* io_ctx, |
170 | | const PositionDeleteContext& position_delete_ctx, |
171 | | const LazyReadContext& lazy_read_ctx, RuntimeState* state, |
172 | | const std::set<uint64_t>& column_ids, |
173 | | const std::set<uint64_t>& filter_column_ids); |
174 | | |
175 | | ~RowGroupReader(); |
176 | | Status init(const FieldDescriptor& schema, RowRanges& row_ranges, |
177 | | std::unordered_map<int, tparquet::OffsetIndex>& col_offsets, |
178 | | const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor, |
179 | | const std::unordered_map<std::string, int>* colname_to_slot_id, |
180 | | const VExprContextSPtrs* not_single_slot_filter_conjuncts, |
181 | | const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts); |
182 | | Status next_batch(Block* block, size_t batch_size, size_t* read_rows, bool* batch_eof); |
183 | 45 | int64_t lazy_read_filtered_rows() const { return _lazy_read_filtered_rows; } |
184 | 45 | int64_t predicate_filter_time() const { return _predicate_filter_time; } |
185 | 45 | int64_t dict_filter_rewrite_time() const { return _dict_filter_rewrite_time; } |
186 | 24 | int64_t condition_cache_filtered_rows() const { return _condition_cache_filtered_rows; } |
187 | | |
188 | | ParquetColumnReader::ColumnStatistics merged_column_statistics(); |
189 | 0 | void set_remaining_rows(int64_t rows) { _remaining_rows = rows; } |
190 | | |
191 | 0 | int64_t get_remaining_rows() { return _remaining_rows; } |
192 | | |
193 | | // Filters read_ranges by removing row chunks whose condition cache granules are all-false. |
194 | | // Pure algorithm, exposed as static for testability. |
195 | | static RowRanges filter_ranges_by_cache(const RowRanges& read_ranges, |
196 | | const std::vector<bool>& cache, int64_t first_row, |
197 | | int64_t base_granule = 0); |
198 | | |
199 | 45 | void set_table_format_reader(TableFormatReader* reader) { _table_format_reader = reader; } |
200 | | |
201 | | // RowPositionProvider interface |
202 | 5 | const std::vector<rowid_t>& current_batch_row_positions() const override { |
203 | 5 | return _current_batch_row_ids; |
204 | 5 | } |
205 | | |
206 | 0 | void set_row_lineage_columns(std::shared_ptr<RowLineageColumns> row_lineage_columns) { |
207 | 0 | _row_lineage_columns = std::move(row_lineage_columns); |
208 | 0 | } |
209 | | |
210 | 45 | void set_current_row_group_idx(RowGroupIndex row_group_idx) { |
211 | 45 | _current_row_group_idx = row_group_idx; |
212 | 45 | } |
213 | | |
214 | | void set_col_name_to_block_idx( |
215 | 45 | std::unordered_map<std::string, uint32_t>* col_name_to_block_idx) { |
216 | 45 | _col_name_to_block_idx = col_name_to_block_idx; |
217 | 45 | } |
218 | | |
219 | 0 | void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) { |
220 | 0 | _condition_cache_ctx = std::move(ctx); |
221 | 0 | } |
222 | | |
223 | | protected: |
224 | 15 | void _collect_profile_before_close() override { |
225 | 15 | if (_file_reader != nullptr) { |
226 | 15 | _file_reader->collect_profile_before_close(); |
227 | 15 | } |
228 | 15 | } |
229 | | |
230 | | private: |
231 | | Status _read_empty_batch(size_t batch_size, size_t* read_rows, bool* batch_eof); |
232 | | |
233 | | Status _read_column_data(Block* block, const std::vector<std::string>& columns, |
234 | | size_t batch_size, size_t* read_rows, bool* batch_eof, |
235 | | FilterMap& filter_map); |
236 | | |
237 | | Status _do_lazy_read(Block* block, size_t batch_size, size_t* read_rows, bool* batch_eof); |
238 | | Status _rebuild_filter_map(FilterMap& filter_map, |
239 | | DorisUniqueBufferPtr<uint8_t>& filter_map_data, |
240 | | size_t pre_read_rows) const; |
241 | | |
242 | | Status _fill_partition_columns( |
243 | | Block* block, size_t rows, |
244 | | const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>& |
245 | | partition_columns); |
246 | | Status _fill_missing_columns( |
247 | | Block* block, size_t rows, |
248 | | const std::unordered_map<std::string, VExprContextSPtr>& missing_columns); |
249 | | Status _get_block_column_pos(const Block& block, const std::string& column_name, |
250 | | uint32_t* position) const; |
251 | | Status _build_pos_delete_filter(size_t read_rows); |
252 | | Status _filter_block(Block* block, int column_to_keep, |
253 | | const std::vector<uint32_t>& columns_to_filter); |
254 | | Status _filter_block_internal(Block* block, const std::vector<uint32_t>& columns_to_filter, |
255 | | const IColumn::Filter& filter); |
256 | | |
257 | | void _block_dict_filter_for_slots(const VExprSPtr& expr); |
258 | | bool _can_filter_by_dict(int slot_id, const tparquet::ColumnMetaData& column_metadata); |
259 | | bool _need_current_batch_row_positions() const; |
260 | | bool is_dictionary_encoded(const tparquet::ColumnMetaData& column_metadata); |
261 | | Status _rewrite_dict_predicates(); |
262 | | Status _rewrite_dict_conjuncts(std::vector<int32_t>& dict_codes, int slot_id, bool is_nullable); |
263 | | Status _convert_dict_cols_to_string_cols(Block* block); |
264 | | void _filter_read_ranges_by_condition_cache(); |
265 | | void _mark_condition_cache_granules(const uint8_t* filter_data, size_t num_rows, |
266 | | int64_t batch_seq_start); |
267 | | |
268 | | Status _get_current_batch_row_id(size_t read_rows); |
269 | | |
270 | | io::FileReaderSPtr _file_reader; |
271 | | std::vector<std::string> _read_table_columns; |
272 | | |
273 | | const int32_t _row_group_id; |
274 | | const tparquet::RowGroup& _row_group_meta; |
275 | | int64_t _remaining_rows; |
276 | | const cctz::time_zone* _ctz = nullptr; |
277 | | io::IOContext* _io_ctx = nullptr; |
278 | | PositionDeleteContext _position_delete_ctx; |
279 | | std::shared_ptr<RowLineageColumns> _row_lineage_columns; |
280 | | // merge the row ranges generated from page index and position delete. |
281 | | RowRanges _read_ranges; |
282 | | // ParquetColumnReader keeps a reference to _read_ranges, so readers must be destroyed first. |
283 | | std::unordered_map<std::string, std::unique_ptr<ParquetColumnReader>> |
284 | | _column_readers; // table_column_name |
285 | | |
286 | | LazyReadContext _lazy_read_ctx; |
287 | | int64_t _lazy_read_filtered_rows = 0; |
288 | | int64_t _predicate_filter_time = 0; |
289 | | int64_t _dict_filter_rewrite_time = 0; |
290 | | int64_t _condition_cache_filtered_rows = 0; |
291 | | // If continuous batches are skipped, we can cache them to skip a whole page |
292 | | size_t _cached_filtered_rows = 0; |
293 | | std::shared_ptr<ConditionCacheContext> _condition_cache_ctx; |
294 | | std::unique_ptr<IColumn::Filter> _pos_delete_filter_ptr; |
295 | | int64_t _total_read_rows = 0; |
296 | | const TupleDescriptor* _tuple_descriptor = nullptr; |
297 | | const RowDescriptor* _row_descriptor = nullptr; |
298 | | const std::unordered_map<std::string, int>* _col_name_to_slot_id = nullptr; |
299 | | const std::unordered_map<int, VExprContextSPtrs>* _slot_id_to_filter_conjuncts = nullptr; |
300 | | VExprContextSPtrs _dict_filter_conjuncts; |
301 | | VExprContextSPtrs _filter_conjuncts; |
302 | | // std::pair<col_name, slot_id> |
303 | | std::vector<std::pair<std::string, int>> _dict_filter_cols; |
304 | | std::unordered_set<int> _dict_filter_blocked_slot_ids; |
305 | | RuntimeState* _state = nullptr; |
306 | | std::shared_ptr<ObjectPool> _obj_pool; |
307 | | const std::set<uint64_t>& _column_ids; |
308 | | const std::set<uint64_t>& _filter_column_ids; |
309 | | bool _is_row_group_filtered = false; |
310 | | |
311 | | RowGroupIndex _current_row_group_idx {0, 0, 0}; |
312 | | std::vector<rowid_t> _current_batch_row_ids; |
313 | | |
314 | | std::unordered_map<std::string, uint32_t>* _col_name_to_block_idx = nullptr; |
315 | | TableFormatReader* _table_format_reader = nullptr; |
316 | | }; |
317 | | |
318 | | } // namespace doris |