be/src/format/parquet/vparquet_reader.cpp
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 | | #include "format/parquet/vparquet_reader.h" |
19 | | |
20 | | #include <gen_cpp/Metrics_types.h> |
21 | | #include <gen_cpp/PlanNodes_types.h> |
22 | | #include <gen_cpp/parquet_types.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <functional> |
27 | | #include <utility> |
28 | | |
29 | | #include "common/config.h" |
30 | | #include "common/status.h" |
31 | | #include "core/block/block.h" |
32 | | #include "core/block/column_with_type_and_name.h" |
33 | | #include "core/column/column.h" |
34 | | #include "core/data_type/define_primitive_type.h" |
35 | | #include "core/typeid_cast.h" |
36 | | #include "core/types.h" |
37 | | #include "exec/scan/file_scanner.h" |
38 | | #include "exprs/vbloom_predicate.h" |
39 | | #include "exprs/vdirect_in_predicate.h" |
40 | | #include "exprs/vexpr.h" |
41 | | #include "exprs/vexpr_context.h" |
42 | | #include "exprs/vin_predicate.h" |
43 | | #include "exprs/vruntimefilter_wrapper.h" |
44 | | #include "exprs/vslot_ref.h" |
45 | | #include "exprs/vtopn_pred.h" |
46 | | #include "format/column_type_convert.h" |
47 | | #include "format/parquet/parquet_block_split_bloom_filter.h" |
48 | | #include "format/parquet/parquet_common.h" |
49 | | #include "format/parquet/parquet_predicate.h" |
50 | | #include "format/parquet/parquet_thrift_util.h" |
51 | | #include "format/parquet/schema_desc.h" |
52 | | #include "format/parquet/vparquet_file_metadata.h" |
53 | | #include "format/parquet/vparquet_group_reader.h" |
54 | | #include "format/parquet/vparquet_page_index.h" |
55 | | #include "information_schema/schema_scanner.h" |
56 | | #include "io/file_factory.h" |
57 | | #include "io/fs/buffered_reader.h" |
58 | | #include "io/fs/file_reader.h" |
59 | | #include "io/fs/file_reader_writer_fwd.h" |
60 | | #include "io/fs/tracing_file_reader.h" |
61 | | #include "runtime/descriptors.h" |
62 | | #include "util/slice.h" |
63 | | #include "util/string_util.h" |
64 | | #include "util/timezone_utils.h" |
65 | | |
66 | | namespace cctz { |
67 | | class time_zone; |
68 | | } // namespace cctz |
69 | | namespace doris { |
70 | | class RowDescriptor; |
71 | | class RuntimeState; |
72 | | class SlotDescriptor; |
73 | | class TupleDescriptor; |
74 | | namespace io { |
75 | | struct IOContext; |
76 | | enum class FileCachePolicy : uint8_t; |
77 | | } // namespace io |
78 | | class Block; |
79 | | } // namespace doris |
80 | | |
81 | | namespace doris { |
82 | | |
83 | | #include "common/compile_check_begin.h" |
84 | | ParquetReader::ParquetReader(RuntimeProfile* profile, const TFileScanRangeParams& params, |
85 | | const TFileRangeDesc& range, size_t batch_size, |
86 | | const cctz::time_zone* ctz, io::IOContext* io_ctx, RuntimeState* state, |
87 | | FileMetaCache* meta_cache, bool enable_lazy_mat) |
88 | 98 | : _profile(profile), |
89 | 98 | _scan_params(params), |
90 | 98 | _scan_range(range), |
91 | 98 | _batch_size(std::max(batch_size, _MIN_BATCH_SIZE)), |
92 | 98 | _range_start_offset(range.start_offset), |
93 | 98 | _range_size(range.size), |
94 | 98 | _ctz(ctz), |
95 | 98 | _io_ctx(io_ctx), |
96 | 98 | _state(state), |
97 | 98 | _enable_lazy_mat(enable_lazy_mat), |
98 | | _enable_filter_by_min_max( |
99 | 98 | state == nullptr ? true |
100 | 98 | : state->query_options().enable_parquet_filter_by_min_max), |
101 | | _enable_filter_by_bloom_filter( |
102 | 98 | state == nullptr ? true |
103 | 98 | : state->query_options().enable_parquet_filter_by_bloom_filter) { |
104 | 98 | _meta_cache = meta_cache; |
105 | 98 | _init_profile(); |
106 | 98 | _init_system_properties(); |
107 | 98 | _init_file_description(); |
108 | 98 | } |
109 | | |
110 | | ParquetReader::ParquetReader(RuntimeProfile* profile, const TFileScanRangeParams& params, |
111 | | const TFileRangeDesc& range, size_t batch_size, |
112 | | const cctz::time_zone* ctz, |
113 | | std::shared_ptr<io::IOContext> io_ctx_holder, RuntimeState* state, |
114 | | FileMetaCache* meta_cache, bool enable_lazy_mat) |
115 | 0 | : _profile(profile), |
116 | 0 | _scan_params(params), |
117 | 0 | _scan_range(range), |
118 | 0 | _batch_size(std::max(batch_size, _MIN_BATCH_SIZE)), |
119 | 0 | _range_start_offset(range.start_offset), |
120 | 0 | _range_size(range.size), |
121 | 0 | _ctz(ctz), |
122 | 0 | _io_ctx(io_ctx_holder ? io_ctx_holder.get() : nullptr), |
123 | 0 | _io_ctx_holder(std::move(io_ctx_holder)), |
124 | 0 | _state(state), |
125 | 0 | _enable_lazy_mat(enable_lazy_mat), |
126 | | _enable_filter_by_min_max( |
127 | 0 | state == nullptr ? true |
128 | 0 | : state->query_options().enable_parquet_filter_by_min_max), |
129 | | _enable_filter_by_bloom_filter( |
130 | 0 | state == nullptr ? true |
131 | 0 | : state->query_options().enable_parquet_filter_by_bloom_filter) { |
132 | 0 | _meta_cache = meta_cache; |
133 | 0 | _init_profile(); |
134 | 0 | _init_system_properties(); |
135 | 0 | _init_file_description(); |
136 | 0 | } |
137 | | |
138 | | ParquetReader::ParquetReader(const TFileScanRangeParams& params, const TFileRangeDesc& range, |
139 | | io::IOContext* io_ctx, RuntimeState* state, FileMetaCache* meta_cache, |
140 | | bool enable_lazy_mat) |
141 | 5 | : _profile(nullptr), |
142 | 5 | _scan_params(params), |
143 | 5 | _scan_range(range), |
144 | 5 | _io_ctx(io_ctx), |
145 | 5 | _state(state), |
146 | 5 | _enable_lazy_mat(enable_lazy_mat), |
147 | | _enable_filter_by_min_max( |
148 | 5 | state == nullptr ? true |
149 | 5 | : state->query_options().enable_parquet_filter_by_min_max), |
150 | | _enable_filter_by_bloom_filter( |
151 | 5 | state == nullptr ? true |
152 | 5 | : state->query_options().enable_parquet_filter_by_bloom_filter) { |
153 | 5 | _meta_cache = meta_cache; |
154 | 5 | _init_system_properties(); |
155 | 5 | _init_file_description(); |
156 | 5 | } |
157 | | |
158 | | ParquetReader::ParquetReader(const TFileScanRangeParams& params, const TFileRangeDesc& range, |
159 | | std::shared_ptr<io::IOContext> io_ctx_holder, RuntimeState* state, |
160 | | FileMetaCache* meta_cache, bool enable_lazy_mat) |
161 | 0 | : _profile(nullptr), |
162 | 0 | _scan_params(params), |
163 | 0 | _scan_range(range), |
164 | 0 | _io_ctx(io_ctx_holder ? io_ctx_holder.get() : nullptr), |
165 | 0 | _io_ctx_holder(std::move(io_ctx_holder)), |
166 | 0 | _state(state), |
167 | 0 | _enable_lazy_mat(enable_lazy_mat), |
168 | | _enable_filter_by_min_max( |
169 | 0 | state == nullptr ? true |
170 | 0 | : state->query_options().enable_parquet_filter_by_min_max), |
171 | | _enable_filter_by_bloom_filter( |
172 | 0 | state == nullptr ? true |
173 | 0 | : state->query_options().enable_parquet_filter_by_bloom_filter) { |
174 | 0 | _meta_cache = meta_cache; |
175 | 0 | _init_system_properties(); |
176 | 0 | _init_file_description(); |
177 | 0 | } |
178 | | |
179 | 103 | ParquetReader::~ParquetReader() { |
180 | 103 | _close_internal(); |
181 | 103 | } |
182 | | |
183 | | #ifdef BE_TEST |
184 | | // for unit test |
185 | 69 | void ParquetReader::set_file_reader(io::FileReaderSPtr file_reader) { |
186 | 69 | _file_reader = file_reader; |
187 | 69 | _tracing_file_reader = file_reader; |
188 | 69 | } |
189 | | #endif |
190 | | |
191 | 98 | void ParquetReader::_init_profile() { |
192 | 98 | if (_profile != nullptr) { |
193 | 50 | static const char* parquet_profile = "ParquetReader"; |
194 | 50 | ADD_TIMER_WITH_LEVEL(_profile, parquet_profile, 1); |
195 | | |
196 | 50 | _parquet_profile.filtered_row_groups = ADD_CHILD_COUNTER_WITH_LEVEL( |
197 | 50 | _profile, "RowGroupsFiltered", TUnit::UNIT, parquet_profile, 1); |
198 | 50 | _parquet_profile.filtered_row_groups_by_min_max = ADD_CHILD_COUNTER_WITH_LEVEL( |
199 | 50 | _profile, "RowGroupsFilteredByMinMax", TUnit::UNIT, parquet_profile, 1); |
200 | 50 | _parquet_profile.filtered_row_groups_by_bloom_filter = ADD_CHILD_COUNTER_WITH_LEVEL( |
201 | 50 | _profile, "RowGroupsFilteredByBloomFilter", TUnit::UNIT, parquet_profile, 1); |
202 | 50 | _parquet_profile.to_read_row_groups = ADD_CHILD_COUNTER_WITH_LEVEL( |
203 | 50 | _profile, "RowGroupsReadNum", TUnit::UNIT, parquet_profile, 1); |
204 | 50 | _parquet_profile.total_row_groups = ADD_CHILD_COUNTER_WITH_LEVEL( |
205 | 50 | _profile, "RowGroupsTotalNum", TUnit::UNIT, parquet_profile, 1); |
206 | 50 | _parquet_profile.filtered_group_rows = ADD_CHILD_COUNTER_WITH_LEVEL( |
207 | 50 | _profile, "FilteredRowsByGroup", TUnit::UNIT, parquet_profile, 1); |
208 | 50 | _parquet_profile.filtered_page_rows = ADD_CHILD_COUNTER_WITH_LEVEL( |
209 | 50 | _profile, "FilteredRowsByPage", TUnit::UNIT, parquet_profile, 1); |
210 | 50 | _parquet_profile.lazy_read_filtered_rows = ADD_CHILD_COUNTER_WITH_LEVEL( |
211 | 50 | _profile, "FilteredRowsByLazyRead", TUnit::UNIT, parquet_profile, 1); |
212 | 50 | _parquet_profile.filtered_bytes = ADD_CHILD_COUNTER_WITH_LEVEL( |
213 | 50 | _profile, "FilteredBytes", TUnit::BYTES, parquet_profile, 1); |
214 | 50 | _parquet_profile.raw_rows_read = ADD_CHILD_COUNTER_WITH_LEVEL( |
215 | 50 | _profile, "RawRowsRead", TUnit::UNIT, parquet_profile, 1); |
216 | 50 | _parquet_profile.column_read_time = |
217 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "ColumnReadTime", parquet_profile, 1); |
218 | 50 | _parquet_profile.parse_meta_time = |
219 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "ParseMetaTime", parquet_profile, 1); |
220 | 50 | _parquet_profile.parse_footer_time = |
221 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "ParseFooterTime", parquet_profile, 1); |
222 | 50 | _parquet_profile.file_reader_create_time = |
223 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "FileReaderCreateTime", parquet_profile, 1); |
224 | 50 | _parquet_profile.open_file_num = |
225 | 50 | ADD_CHILD_COUNTER_WITH_LEVEL(_profile, "FileNum", TUnit::UNIT, parquet_profile, 1); |
226 | 50 | _parquet_profile.page_index_read_calls = |
227 | 50 | ADD_COUNTER_WITH_LEVEL(_profile, "PageIndexReadCalls", TUnit::UNIT, 1); |
228 | 50 | _parquet_profile.page_index_filter_time = |
229 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "PageIndexFilterTime", parquet_profile, 1); |
230 | 50 | _parquet_profile.read_page_index_time = |
231 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "PageIndexReadTime", parquet_profile, 1); |
232 | 50 | _parquet_profile.parse_page_index_time = |
233 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "PageIndexParseTime", parquet_profile, 1); |
234 | 50 | _parquet_profile.row_group_filter_time = |
235 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "RowGroupFilterTime", parquet_profile, 1); |
236 | 50 | _parquet_profile.file_footer_read_calls = |
237 | 50 | ADD_COUNTER_WITH_LEVEL(_profile, "FileFooterReadCalls", TUnit::UNIT, 1); |
238 | 50 | _parquet_profile.file_footer_hit_cache = |
239 | 50 | ADD_COUNTER_WITH_LEVEL(_profile, "FileFooterHitCache", TUnit::UNIT, 1); |
240 | 50 | _parquet_profile.decompress_time = |
241 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "DecompressTime", parquet_profile, 1); |
242 | 50 | _parquet_profile.decompress_cnt = ADD_CHILD_COUNTER_WITH_LEVEL( |
243 | 50 | _profile, "DecompressCount", TUnit::UNIT, parquet_profile, 1); |
244 | 50 | _parquet_profile.page_read_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
245 | 50 | _profile, "PageReadCount", TUnit::UNIT, parquet_profile, 1); |
246 | 50 | _parquet_profile.page_cache_write_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
247 | 50 | _profile, "PageCacheWriteCount", TUnit::UNIT, parquet_profile, 1); |
248 | 50 | _parquet_profile.page_cache_compressed_write_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
249 | 50 | _profile, "PageCacheCompressedWriteCount", TUnit::UNIT, parquet_profile, 1); |
250 | 50 | _parquet_profile.page_cache_decompressed_write_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
251 | 50 | _profile, "PageCacheDecompressedWriteCount", TUnit::UNIT, parquet_profile, 1); |
252 | 50 | _parquet_profile.page_cache_hit_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
253 | 50 | _profile, "PageCacheHitCount", TUnit::UNIT, parquet_profile, 1); |
254 | 50 | _parquet_profile.page_cache_missing_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
255 | 50 | _profile, "PageCacheMissingCount", TUnit::UNIT, parquet_profile, 1); |
256 | 50 | _parquet_profile.page_cache_compressed_hit_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
257 | 50 | _profile, "PageCacheCompressedHitCount", TUnit::UNIT, parquet_profile, 1); |
258 | 50 | _parquet_profile.page_cache_decompressed_hit_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
259 | 50 | _profile, "PageCacheDecompressedHitCount", TUnit::UNIT, parquet_profile, 1); |
260 | 50 | _parquet_profile.decode_header_time = |
261 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "PageHeaderDecodeTime", parquet_profile, 1); |
262 | 50 | _parquet_profile.read_page_header_time = |
263 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "PageHeaderReadTime", parquet_profile, 1); |
264 | 50 | _parquet_profile.decode_value_time = |
265 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "DecodeValueTime", parquet_profile, 1); |
266 | 50 | _parquet_profile.decode_dict_time = |
267 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "DecodeDictTime", parquet_profile, 1); |
268 | 50 | _parquet_profile.decode_level_time = |
269 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "DecodeLevelTime", parquet_profile, 1); |
270 | 50 | _parquet_profile.decode_null_map_time = |
271 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "DecodeNullMapTime", parquet_profile, 1); |
272 | 50 | _parquet_profile.skip_page_header_num = ADD_CHILD_COUNTER_WITH_LEVEL( |
273 | 50 | _profile, "SkipPageHeaderNum", TUnit::UNIT, parquet_profile, 1); |
274 | 50 | _parquet_profile.parse_page_header_num = ADD_CHILD_COUNTER_WITH_LEVEL( |
275 | 50 | _profile, "ParsePageHeaderNum", TUnit::UNIT, parquet_profile, 1); |
276 | 50 | _parquet_profile.predicate_filter_time = |
277 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "PredicateFilterTime", parquet_profile, 1); |
278 | 50 | _parquet_profile.dict_filter_rewrite_time = |
279 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "DictFilterRewriteTime", parquet_profile, 1); |
280 | 50 | _parquet_profile.bloom_filter_read_time = |
281 | 50 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "BloomFilterReadTime", parquet_profile, 1); |
282 | 50 | } |
283 | 98 | } |
284 | | |
285 | 14 | Status ParquetReader::close() { |
286 | 14 | _close_internal(); |
287 | 14 | return Status::OK(); |
288 | 14 | } |
289 | | |
290 | 117 | void ParquetReader::_close_internal() { |
291 | 117 | if (!_closed) { |
292 | 103 | _closed = true; |
293 | 103 | } |
294 | 117 | } |
295 | | |
296 | 106 | Status ParquetReader::_open_file() { |
297 | 106 | if (UNLIKELY(_io_ctx && _io_ctx->should_stop)) { |
298 | 0 | return Status::EndOfFile("stop"); |
299 | 0 | } |
300 | 106 | if (_file_reader == nullptr) { |
301 | 15 | SCOPED_RAW_TIMER(&_reader_statistics.file_reader_create_time); |
302 | 15 | ++_reader_statistics.open_file_num; |
303 | 15 | _file_description.mtime = |
304 | 15 | _scan_range.__isset.modification_time ? _scan_range.modification_time : 0; |
305 | 15 | io::FileReaderOptions reader_options = |
306 | 15 | FileFactory::get_reader_options(_state, _file_description); |
307 | 15 | _file_reader = DORIS_TRY(io::DelegateReader::create_file_reader( |
308 | 15 | _profile, _system_properties, _file_description, reader_options, |
309 | 15 | io::DelegateReader::AccessMode::RANDOM, _io_ctx)); |
310 | 15 | _tracing_file_reader = _io_ctx ? std::make_shared<io::TracingFileReader>( |
311 | 15 | _file_reader, _io_ctx->file_reader_stats) |
312 | 15 | : _file_reader; |
313 | 15 | } |
314 | | |
315 | 106 | if (_file_metadata == nullptr) { |
316 | 84 | SCOPED_RAW_TIMER(&_reader_statistics.parse_footer_time); |
317 | 84 | if (_tracing_file_reader->size() <= sizeof(PARQUET_VERSION_NUMBER)) { |
318 | | // Some system may generate parquet file with only 4 bytes: PAR1 |
319 | | // Should consider it as empty file. |
320 | 0 | return Status::EndOfFile("open file failed, empty parquet file {} with size: {}", |
321 | 0 | _scan_range.path, _tracing_file_reader->size()); |
322 | 0 | } |
323 | 84 | size_t meta_size = 0; |
324 | 84 | bool enable_mapping_varbinary = _scan_params.__isset.enable_mapping_varbinary |
325 | 84 | ? _scan_params.enable_mapping_varbinary |
326 | 84 | : false; |
327 | 84 | bool enable_mapping_timestamp_tz = _scan_params.__isset.enable_mapping_timestamp_tz |
328 | 84 | ? _scan_params.enable_mapping_timestamp_tz |
329 | 84 | : false; |
330 | 84 | if (_meta_cache == nullptr) { |
331 | | // wrap _file_metadata with unique ptr, so that it can be released finally. |
332 | 48 | RETURN_IF_ERROR(parse_thrift_footer(_tracing_file_reader, &_file_metadata_ptr, |
333 | 48 | &meta_size, _io_ctx, enable_mapping_varbinary, |
334 | 48 | enable_mapping_timestamp_tz)); |
335 | 48 | _file_metadata = _file_metadata_ptr.get(); |
336 | | // parse magic number & parse meta data |
337 | 48 | _reader_statistics.file_footer_read_calls += 1; |
338 | 48 | } else { |
339 | 36 | const auto& file_meta_cache_key = |
340 | 36 | FileMetaCache::get_key(_tracing_file_reader, _file_description); |
341 | 36 | if (!_meta_cache->lookup(file_meta_cache_key, &_meta_cache_handle)) { |
342 | 24 | RETURN_IF_ERROR(parse_thrift_footer(_tracing_file_reader, &_file_metadata_ptr, |
343 | 24 | &meta_size, _io_ctx, enable_mapping_varbinary, |
344 | 24 | enable_mapping_timestamp_tz)); |
345 | | // _file_metadata_ptr.release() : move control of _file_metadata to _meta_cache_handle |
346 | 24 | _meta_cache->insert(file_meta_cache_key, _file_metadata_ptr.release(), |
347 | 24 | &_meta_cache_handle); |
348 | 24 | _file_metadata = _meta_cache_handle.data<FileMetaData>(); |
349 | 24 | _reader_statistics.file_footer_read_calls += 1; |
350 | 24 | } else { |
351 | 12 | _reader_statistics.file_footer_hit_cache++; |
352 | 12 | } |
353 | 36 | _file_metadata = _meta_cache_handle.data<FileMetaData>(); |
354 | 36 | } |
355 | | |
356 | 84 | if (_file_metadata == nullptr) { |
357 | 0 | return Status::InternalError("failed to get file meta data: {}", |
358 | 0 | _file_description.path); |
359 | 0 | } |
360 | 84 | } |
361 | 106 | return Status::OK(); |
362 | 106 | } |
363 | | |
364 | 40 | Status ParquetReader::get_file_metadata_schema(const FieldDescriptor** ptr) { |
365 | 40 | RETURN_IF_ERROR(_open_file()); |
366 | 40 | DCHECK(_file_metadata != nullptr); |
367 | 40 | *ptr = &_file_metadata->schema(); |
368 | 40 | return Status::OK(); |
369 | 40 | } |
370 | | |
371 | 103 | void ParquetReader::_init_system_properties() { |
372 | 103 | if (_scan_range.__isset.file_type) { |
373 | | // for compatibility |
374 | 0 | _system_properties.system_type = _scan_range.file_type; |
375 | 103 | } else { |
376 | 103 | _system_properties.system_type = _scan_params.file_type; |
377 | 103 | } |
378 | 103 | _system_properties.properties = _scan_params.properties; |
379 | 103 | _system_properties.hdfs_params = _scan_params.hdfs_params; |
380 | 103 | if (_scan_params.__isset.broker_addresses) { |
381 | 0 | _system_properties.broker_addresses.assign(_scan_params.broker_addresses.begin(), |
382 | 0 | _scan_params.broker_addresses.end()); |
383 | 0 | } |
384 | 103 | } |
385 | | |
386 | 103 | void ParquetReader::_init_file_description() { |
387 | 103 | _file_description.path = _scan_range.path; |
388 | 103 | _file_description.file_size = _scan_range.__isset.file_size ? _scan_range.file_size : -1; |
389 | 103 | if (_scan_range.__isset.fs_name) { |
390 | 0 | _file_description.fs_name = _scan_range.fs_name; |
391 | 0 | } |
392 | 103 | if (_scan_range.__isset.file_cache_admission) { |
393 | 0 | _file_description.file_cache_admission = _scan_range.file_cache_admission; |
394 | 0 | } |
395 | 103 | } |
396 | | |
397 | 9 | Status ParquetReader::on_before_init_reader(ReaderInitContext* ctx) { |
398 | 9 | _column_descs = ctx->column_descs; |
399 | 9 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
400 | 9 | RETURN_IF_ERROR( |
401 | 9 | _extract_partition_values(*ctx->range, ctx->tuple_descriptor, _fill_partition_values)); |
402 | 21 | for (auto& desc : *ctx->column_descs) { |
403 | 21 | if (desc.category == ColumnCategory::REGULAR || |
404 | 21 | desc.category == ColumnCategory::GENERATED) { |
405 | 21 | ctx->column_names.push_back(desc.name); |
406 | 21 | } |
407 | 21 | } |
408 | | |
409 | | // Build table_info_node from Parquet file metadata with case-insensitive recursive matching. |
410 | | // File is already opened by init_reader before this hook, so metadata is available. |
411 | | // tuple_descriptor may be null in unit tests that only set column_descs. |
412 | 9 | if (ctx->tuple_descriptor != nullptr) { |
413 | 7 | const FieldDescriptor* field_desc = nullptr; |
414 | 7 | RETURN_IF_ERROR(get_file_metadata_schema(&field_desc)); |
415 | 7 | RETURN_IF_ERROR(TableSchemaChangeHelper::BuildTableInfoUtil::by_parquet_name( |
416 | 7 | ctx->tuple_descriptor, *field_desc, ctx->table_info_node)); |
417 | 7 | } |
418 | | |
419 | 9 | return Status::OK(); |
420 | 9 | } |
421 | | |
422 | 66 | Status ParquetReader::_open_file_reader(ReaderInitContext* /*ctx*/) { |
423 | 66 | return _open_file(); |
424 | 66 | } |
425 | | |
426 | 66 | Status ParquetReader::_do_init_reader(ReaderInitContext* base_ctx) { |
427 | 66 | auto* ctx = checked_context_cast<ParquetInitContext>(base_ctx); |
428 | 66 | _col_name_to_block_idx = base_ctx->col_name_to_block_idx; |
429 | 66 | _tuple_descriptor = ctx->tuple_descriptor; |
430 | 66 | _row_descriptor = ctx->row_descriptor; |
431 | 66 | _colname_to_slot_id = ctx->colname_to_slot_id; |
432 | 66 | _not_single_slot_filter_conjuncts = ctx->not_single_slot_filter_conjuncts; |
433 | 66 | _slot_id_to_filter_conjuncts = ctx->slot_id_to_filter_conjuncts; |
434 | 66 | _filter_groups = ctx->filter_groups; |
435 | 66 | _table_info_node_ptr = base_ctx->table_info_node; |
436 | 66 | _column_ids = base_ctx->column_ids; |
437 | 66 | _filter_column_ids = base_ctx->filter_column_ids; |
438 | | |
439 | | // _open_file_reader (called by init_reader NVI before hooks) must have opened the file. |
440 | 66 | DCHECK(_file_metadata != nullptr) |
441 | 0 | << "ParquetReader::_do_init_reader called without _open_file_reader"; |
442 | 66 | _t_metadata = &(_file_metadata->to_thrift()); |
443 | | |
444 | 66 | SCOPED_RAW_TIMER(&_reader_statistics.parse_meta_time); |
445 | 66 | _total_groups = _t_metadata->row_groups.size(); |
446 | 66 | if (_total_groups == 0) { |
447 | 0 | return Status::EndOfFile("init reader failed, empty parquet file: " + _scan_range.path); |
448 | 0 | } |
449 | 66 | _current_row_group_index = RowGroupReader::RowGroupIndex {-1, 0, 0}; |
450 | | |
451 | | // Compute missing columns and file↔table column mapping. |
452 | | // This runs in _do_init_reader (not on_before_init_reader) because table-format readers |
453 | | // (Iceberg, Paimon, Hive, Hudi) override on_before_init_reader completely. |
454 | 66 | if (has_column_descs()) { |
455 | 24 | _fill_missing_cols.clear(); |
456 | 24 | _fill_missing_defaults.clear(); |
457 | 74 | for (const auto& col_name : base_ctx->column_names) { |
458 | 74 | if (!_table_info_node_ptr->children_column_exists(col_name)) { |
459 | 5 | _fill_missing_cols.insert(col_name); |
460 | 5 | } |
461 | 74 | } |
462 | 24 | if (_column_descs && !_fill_missing_cols.empty()) { |
463 | 13 | for (const auto& desc : *_column_descs) { |
464 | 13 | if (_fill_missing_cols.contains(desc.name) && |
465 | 13 | !_fill_partition_values.contains(desc.name)) { |
466 | 0 | _fill_missing_defaults[desc.name] = desc.default_expr; |
467 | 0 | } |
468 | 13 | } |
469 | 5 | } |
470 | 24 | } |
471 | | // Resolve file-column ↔ table-column mapping in file-schema order. |
472 | | // _init_read_columns handles both normal path (missing cols populated above) |
473 | | // and standalone path (_fill_missing_cols empty, _table_info_node_ptr may be null). |
474 | 66 | _init_read_columns(base_ctx->column_names); |
475 | | |
476 | | // Register row-position-based synthesized column handler. |
477 | | // _row_id_column_iterator_pair and _row_lineage_columns are set before init_reader |
478 | | // by FileScanner. This must be outside has_column_descs() guard because standalone |
479 | | // readers also need synthesized column handlers. |
480 | 66 | if (_row_id_column_iterator_pair.first != nullptr || |
481 | 66 | (_row_lineage_columns != nullptr && |
482 | 61 | (_row_lineage_columns->need_row_ids() || |
483 | 5 | _row_lineage_columns->has_last_updated_sequence_number_column()))) { |
484 | 5 | register_synthesized_column_handler( |
485 | 5 | BeConsts::ROWID_COL, [this](Block* block, size_t rows) -> Status { |
486 | 5 | if (_current_group_reader) { |
487 | 5 | return _current_group_reader->fill_topn_row_id(block, rows); |
488 | 5 | } |
489 | 0 | return Status::OK(); |
490 | 5 | }); |
491 | 5 | } |
492 | | |
493 | | // build column predicates for column lazy read |
494 | 66 | if (ctx->conjuncts != nullptr) { |
495 | 66 | _lazy_read_ctx.conjuncts = *ctx->conjuncts; |
496 | 66 | } |
497 | 66 | if (ctx->slot_id_to_predicates != nullptr) { |
498 | 66 | _lazy_read_ctx.slot_id_to_predicates = *ctx->slot_id_to_predicates; |
499 | 66 | } |
500 | | |
501 | | // ---- Inlined set_fill_columns logic (partition/missing/synthesized classification) ---- |
502 | | |
503 | | // 1. Collect predicate columns from conjuncts for lazy materialization |
504 | 66 | std::unordered_map<std::string, std::pair<uint32_t, int>> predicate_columns; |
505 | 66 | _collect_predicate_columns_from_conjuncts(predicate_columns); |
506 | | |
507 | | // 2. Classify read/partition/missing/synthesized columns into lazy vs predicate groups |
508 | 66 | _classify_columns_for_lazy_read(predicate_columns, _fill_partition_values, |
509 | 66 | _fill_missing_defaults); |
510 | | |
511 | | // 3. Populate col_names vectors for ColumnProcessor path |
512 | 66 | for (auto& kv : _lazy_read_ctx.predicate_partition_columns) { |
513 | 5 | _lazy_read_ctx.predicate_partition_col_names.emplace_back(kv.first); |
514 | 5 | } |
515 | 66 | for (auto& kv : _lazy_read_ctx.predicate_missing_columns) { |
516 | 0 | _lazy_read_ctx.predicate_missing_col_names.emplace_back(kv.first); |
517 | 0 | } |
518 | 66 | for (auto& kv : _lazy_read_ctx.partition_columns) { |
519 | 3 | _lazy_read_ctx.partition_col_names.emplace_back(kv.first); |
520 | 3 | } |
521 | 66 | for (auto& kv : _lazy_read_ctx.missing_columns) { |
522 | 0 | _lazy_read_ctx.missing_col_names.emplace_back(kv.first); |
523 | 0 | } |
524 | | |
525 | 66 | if (_filter_groups && (_total_groups == 0 || _t_metadata->num_rows == 0 || _range_size < 0)) { |
526 | 0 | return Status::EndOfFile("No row group to read"); |
527 | 0 | } |
528 | | |
529 | 66 | return Status::OK(); |
530 | 66 | } |
531 | | |
532 | 66 | void ParquetReader::_init_read_columns(const std::vector<std::string>& column_names) { |
533 | | // Build file_col_name → table_col_name map, skipping missing columns. |
534 | | // Must iterate file schema in physical order so that _generate_random_access_ranges |
535 | | // sees monotonically increasing chunk offsets. |
536 | 66 | auto schema_desc = _file_metadata->schema(); |
537 | 66 | std::map<std::string, std::string> required_file_columns; |
538 | 422 | for (const auto& col_name : column_names) { |
539 | 422 | if (_fill_missing_cols.contains(col_name)) { |
540 | 5 | continue; |
541 | 5 | } |
542 | 417 | std::string file_col = col_name; |
543 | 417 | if (_table_info_node_ptr && _table_info_node_ptr->children_column_exists(col_name)) { |
544 | 417 | file_col = _table_info_node_ptr->children_file_column_name(col_name); |
545 | 417 | } |
546 | 417 | required_file_columns[file_col] = col_name; |
547 | 417 | } |
548 | 725 | for (int i = 0; i < schema_desc.size(); ++i) { |
549 | 659 | const auto& name = schema_desc.get_column(i)->name; |
550 | 659 | if (required_file_columns.contains(name)) { |
551 | 417 | _read_file_columns.emplace_back(name); |
552 | 417 | _read_table_columns.emplace_back(required_file_columns[name]); |
553 | 417 | _read_table_columns_set.insert(required_file_columns[name]); |
554 | 417 | } |
555 | 659 | } |
556 | 66 | } |
557 | | |
558 | 0 | bool ParquetReader::_exists_in_file(const std::string& expr_name) const { |
559 | | // `_read_table_columns_set` is used to ensure that only columns actually read are subject to min-max filtering. |
560 | | // This primarily handles cases where partition columns also exist in a file. The reason it's not modified |
561 | | // in `_table_info_node_ptr` is that Iceberg、Hudi has inconsistent requirements for this node; |
562 | | // Iceberg partition evolution need read partition columns from a file. |
563 | | // hudi set `hoodie.datasource.write.drop.partition.columns=false` not need read partition columns from a file. |
564 | 0 | return _table_info_node_ptr->children_column_exists(expr_name) && |
565 | 0 | _read_table_columns_set.contains(expr_name); |
566 | 0 | } |
567 | | |
568 | 0 | bool ParquetReader::_type_matches(const int cid) const { |
569 | 0 | auto* slot = _tuple_descriptor->slots()[cid]; |
570 | 0 | auto table_col_type = remove_nullable(slot->type()); |
571 | |
|
572 | 0 | const auto& file_col_name = _table_info_node_ptr->children_file_column_name(slot->col_name()); |
573 | 0 | const auto& file_col_type = |
574 | 0 | remove_nullable(_file_metadata->schema().get_column(file_col_name)->data_type); |
575 | |
|
576 | 0 | return (table_col_type->get_primitive_type() == file_col_type->get_primitive_type()) && |
577 | 0 | !is_complex_type(table_col_type->get_primitive_type()); |
578 | 0 | } |
579 | | |
580 | | void ParquetReader::_collect_predicate_columns_from_conjuncts( |
581 | 66 | std::unordered_map<std::string, std::pair<uint32_t, int>>& predicate_columns) { |
582 | 66 | std::function<void(VExpr * expr)> visit_slot = [&](VExpr* expr) { |
583 | 39 | if (expr->is_slot_ref()) { |
584 | 13 | VSlotRef* slot_ref = static_cast<VSlotRef*>(expr); |
585 | 13 | auto expr_name = slot_ref->expr_name(); |
586 | 13 | predicate_columns.emplace(expr_name, |
587 | 13 | std::make_pair(slot_ref->column_id(), slot_ref->slot_id())); |
588 | 13 | if (slot_ref->column_id() == 0) { |
589 | 3 | _lazy_read_ctx.resize_first_column = false; |
590 | 3 | } |
591 | 13 | return; |
592 | 13 | } |
593 | 26 | for (auto& child : expr->children()) { |
594 | 26 | visit_slot(child.get()); |
595 | 26 | } |
596 | 26 | }; |
597 | | |
598 | 66 | for (const auto& conjunct : _lazy_read_ctx.conjuncts) { |
599 | 13 | auto expr = conjunct->root(); |
600 | 13 | if (expr->is_rf_wrapper()) { |
601 | 0 | VRuntimeFilterWrapper* runtime_filter = assert_cast<VRuntimeFilterWrapper*>(expr.get()); |
602 | 0 | auto filter_impl = runtime_filter->get_impl(); |
603 | 0 | visit_slot(filter_impl.get()); |
604 | 13 | } else { |
605 | 13 | visit_slot(expr.get()); |
606 | 13 | } |
607 | 13 | } |
608 | | |
609 | 66 | if (!_lazy_read_ctx.slot_id_to_predicates.empty()) { |
610 | 0 | auto and_pred = AndBlockColumnPredicate::create_unique(); |
611 | 0 | for (const auto& entry : _lazy_read_ctx.slot_id_to_predicates) { |
612 | 0 | for (const auto& pred : entry.second) { |
613 | 0 | if (!_exists_in_file(pred->col_name()) || !_type_matches(pred->column_id())) { |
614 | 0 | continue; |
615 | 0 | } |
616 | 0 | and_pred->add_column_predicate( |
617 | 0 | SingleColumnBlockPredicate::create_unique(pred->clone(pred->column_id()))); |
618 | 0 | } |
619 | 0 | } |
620 | 0 | if (and_pred->num_of_column_predicate() > 0) { |
621 | 0 | _push_down_predicates.push_back(std::move(and_pred)); |
622 | 0 | } |
623 | 0 | } |
624 | 66 | } |
625 | | |
626 | | void ParquetReader::_classify_columns_for_lazy_read( |
627 | | const std::unordered_map<std::string, std::pair<uint32_t, int>>& predicate_columns, |
628 | | const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>& |
629 | | partition_columns, |
630 | 66 | const std::unordered_map<std::string, VExprContextSPtr>& missing_columns) { |
631 | 66 | const FieldDescriptor& schema = _file_metadata->schema(); |
632 | | |
633 | 66 | auto check_iceberg_row_lineage_column_idx = [&](const auto& col_name) -> int { |
634 | 4 | if (_row_lineage_columns != nullptr) { |
635 | 0 | if (col_name == IcebergTableReader::ROW_LINEAGE_ROW_ID) { |
636 | 0 | return _row_lineage_columns->row_id_column_idx; |
637 | 0 | } else if (col_name == IcebergTableReader::ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) { |
638 | 0 | return _row_lineage_columns->last_updated_sequence_number_column_idx; |
639 | 0 | } |
640 | 0 | } |
641 | 4 | return -1; |
642 | 4 | }; |
643 | | |
644 | 417 | for (auto& read_table_col : _read_table_columns) { |
645 | 417 | _lazy_read_ctx.all_read_columns.emplace_back(read_table_col); |
646 | | |
647 | 417 | auto file_column_name = _table_info_node_ptr->children_file_column_name(read_table_col); |
648 | 417 | PrimitiveType column_type = |
649 | 417 | schema.get_column(file_column_name)->data_type->get_primitive_type(); |
650 | 417 | if (is_complex_type(column_type)) { |
651 | 2 | _lazy_read_ctx.has_complex_type = true; |
652 | 2 | } |
653 | 417 | if (predicate_columns.size() > 0) { |
654 | 12 | auto iter = predicate_columns.find(read_table_col); |
655 | 12 | if (iter == predicate_columns.end()) { |
656 | 4 | if (auto row_lineage_idx = check_iceberg_row_lineage_column_idx(read_table_col); |
657 | 4 | row_lineage_idx != -1) { |
658 | 0 | _lazy_read_ctx.predicate_columns.first.emplace_back(read_table_col); |
659 | | // row lineage column can not dict filter. |
660 | 0 | int slot_id = 0; |
661 | 0 | for (auto slot : _tuple_descriptor->slots()) { |
662 | 0 | if (slot->col_name_lower_case() == read_table_col) { |
663 | 0 | slot_id = slot->id(); |
664 | 0 | } |
665 | 0 | } |
666 | 0 | _lazy_read_ctx.predicate_columns.second.emplace_back(slot_id); |
667 | 0 | _lazy_read_ctx.all_predicate_col_ids.emplace_back(row_lineage_idx); |
668 | 4 | } else { |
669 | 4 | _lazy_read_ctx.lazy_read_columns.emplace_back(read_table_col); |
670 | 4 | } |
671 | 8 | } else { |
672 | 8 | _lazy_read_ctx.predicate_columns.first.emplace_back(iter->first); |
673 | 8 | _lazy_read_ctx.predicate_columns.second.emplace_back(iter->second.second); |
674 | 8 | _lazy_read_ctx.all_predicate_col_ids.emplace_back(iter->second.first); |
675 | 8 | } |
676 | 12 | } |
677 | 417 | } |
678 | 66 | if (_row_id_column_iterator_pair.first != nullptr) { |
679 | 5 | _lazy_read_ctx.all_predicate_col_ids.emplace_back(_row_id_column_iterator_pair.second); |
680 | 5 | } |
681 | | |
682 | 66 | for (auto& kv : partition_columns) { |
683 | 5 | auto iter = predicate_columns.find(kv.first); |
684 | 5 | if (iter == predicate_columns.end()) { |
685 | 0 | _lazy_read_ctx.partition_columns.emplace(kv.first, kv.second); |
686 | 5 | } else { |
687 | 5 | _lazy_read_ctx.predicate_partition_columns.emplace(kv.first, kv.second); |
688 | 5 | _lazy_read_ctx.all_predicate_col_ids.emplace_back(iter->second.first); |
689 | 5 | } |
690 | 5 | } |
691 | | |
692 | 66 | for (auto& kv : missing_columns) { |
693 | 0 | auto iter = predicate_columns.find(kv.first); |
694 | 0 | if (iter != predicate_columns.end()) { |
695 | | //For check missing column : missing column == xx, missing column is null,missing column is not null. |
696 | 0 | if (_slot_id_to_filter_conjuncts->find(iter->second.second) != |
697 | 0 | _slot_id_to_filter_conjuncts->end()) { |
698 | 0 | for (auto& ctx : _slot_id_to_filter_conjuncts->find(iter->second.second)->second) { |
699 | 0 | _lazy_read_ctx.missing_columns_conjuncts.emplace_back(ctx); |
700 | 0 | } |
701 | 0 | } |
702 | 0 | _lazy_read_ctx.predicate_missing_columns.emplace(kv.first, kv.second); |
703 | 0 | _lazy_read_ctx.all_predicate_col_ids.emplace_back(iter->second.first); |
704 | 0 | } else if (auto row_lineage_idx = check_iceberg_row_lineage_column_idx(kv.first); |
705 | 0 | row_lineage_idx != -1) { |
706 | 0 | _lazy_read_ctx.predicate_missing_columns.emplace(kv.first, kv.second); |
707 | 0 | _lazy_read_ctx.all_predicate_col_ids.emplace_back(row_lineage_idx); |
708 | 0 | } else { |
709 | 0 | _lazy_read_ctx.missing_columns.emplace(kv.first, kv.second); |
710 | 0 | } |
711 | 0 | } |
712 | | |
713 | 66 | if (_enable_lazy_mat && _lazy_read_ctx.predicate_columns.first.size() > 0 && |
714 | 66 | _lazy_read_ctx.lazy_read_columns.size() > 0) { |
715 | 2 | _lazy_read_ctx.can_lazy_read = true; |
716 | 2 | } |
717 | | |
718 | 66 | if (!_lazy_read_ctx.can_lazy_read) { |
719 | 64 | for (auto& kv : _lazy_read_ctx.predicate_partition_columns) { |
720 | 3 | _lazy_read_ctx.partition_columns.emplace(kv.first, kv.second); |
721 | 3 | } |
722 | 64 | for (auto& kv : _lazy_read_ctx.predicate_missing_columns) { |
723 | 0 | _lazy_read_ctx.missing_columns.emplace(kv.first, kv.second); |
724 | 0 | } |
725 | 64 | } |
726 | 66 | } |
727 | | |
728 | | // init file reader and file metadata for parsing schema |
729 | 0 | Status ParquetReader::init_schema_reader() { |
730 | 0 | RETURN_IF_ERROR(_open_file()); |
731 | 0 | _t_metadata = &(_file_metadata->to_thrift()); |
732 | 0 | return Status::OK(); |
733 | 0 | } |
734 | | |
735 | | Status ParquetReader::get_parsed_schema(std::vector<std::string>* col_names, |
736 | 0 | std::vector<DataTypePtr>* col_types) { |
737 | 0 | _total_groups = _t_metadata->row_groups.size(); |
738 | 0 | auto schema_desc = _file_metadata->schema(); |
739 | 0 | for (int i = 0; i < schema_desc.size(); ++i) { |
740 | | // Get the Column Reader for the boolean column |
741 | 0 | col_names->emplace_back(schema_desc.get_column(i)->name); |
742 | 0 | col_types->emplace_back(make_nullable(schema_desc.get_column(i)->data_type)); |
743 | 0 | } |
744 | 0 | return Status::OK(); |
745 | 0 | } |
746 | | |
747 | | Status ParquetReader::_get_columns_impl( |
748 | 14 | std::unordered_map<std::string, DataTypePtr>* name_to_type) { |
749 | 14 | const auto& schema_desc = _file_metadata->schema(); |
750 | 14 | std::unordered_set<std::string> column_names; |
751 | 14 | schema_desc.get_column_names(&column_names); |
752 | 210 | for (auto& name : column_names) { |
753 | 210 | auto field = schema_desc.get_column(name); |
754 | 210 | name_to_type->emplace(name, field->data_type); |
755 | 210 | } |
756 | 14 | return Status::OK(); |
757 | 14 | } |
758 | | |
759 | 50 | Status ParquetReader::_do_get_next_block(Block* block, size_t* read_rows, bool* eof) { |
760 | 50 | if (_current_group_reader == nullptr || _row_group_eof) { |
761 | 38 | Status st = _next_row_group_reader(); |
762 | 38 | if (!st.ok() && !st.is<ErrorCode::END_OF_FILE>()) { |
763 | 0 | return st; |
764 | 0 | } |
765 | 38 | if (_current_group_reader == nullptr || _row_group_eof || st.is<ErrorCode::END_OF_FILE>()) { |
766 | 0 | _current_group_reader.reset(nullptr); |
767 | 0 | _row_group_eof = true; |
768 | 0 | *read_rows = 0; |
769 | 0 | *eof = true; |
770 | 0 | return Status::OK(); |
771 | 0 | } |
772 | 38 | } |
773 | | |
774 | | // Limit memory per batch for load paths. |
775 | | // _load_bytes_per_row is updated after each batch so the *next* call pre-shrinks _batch_size |
776 | | // before reading, ensuring the current batch is already within the limit (from call 2 onward). |
777 | 50 | const int64_t max_block_bytes = |
778 | 50 | (_state != nullptr && _state->query_type() == TQueryType::LOAD && |
779 | 50 | config::load_reader_max_block_bytes > 0) |
780 | 50 | ? config::load_reader_max_block_bytes |
781 | 50 | : 0; |
782 | 50 | if (max_block_bytes > 0 && _load_bytes_per_row > 0) { |
783 | 0 | _batch_size = std::max((size_t)1, |
784 | 0 | (size_t)((int64_t)max_block_bytes / (int64_t)_load_bytes_per_row)); |
785 | 0 | } |
786 | | |
787 | 50 | SCOPED_RAW_TIMER(&_reader_statistics.column_read_time); |
788 | 50 | Status batch_st = |
789 | 50 | _current_group_reader->next_batch(block, _batch_size, read_rows, &_row_group_eof); |
790 | 50 | if (batch_st.is<ErrorCode::END_OF_FILE>()) { |
791 | 0 | block->clear_column_data(); |
792 | 0 | _current_group_reader.reset(nullptr); |
793 | 0 | *read_rows = 0; |
794 | 0 | *eof = true; |
795 | 0 | return Status::OK(); |
796 | 0 | } |
797 | | |
798 | 50 | if (!batch_st.ok()) { |
799 | 0 | return Status::InternalError("Read parquet file {} failed, reason = {}", _scan_range.path, |
800 | 0 | batch_st.to_string()); |
801 | 0 | } |
802 | | |
803 | 50 | if (max_block_bytes > 0 && *read_rows > 0) { |
804 | 0 | _load_bytes_per_row = block->bytes() / *read_rows; |
805 | 0 | } |
806 | | |
807 | 50 | if (_row_group_eof) { |
808 | 38 | auto column_st = _current_group_reader->merged_column_statistics(); |
809 | 38 | _column_statistics.merge(column_st); |
810 | 38 | _reader_statistics.lazy_read_filtered_rows += |
811 | 38 | _current_group_reader->lazy_read_filtered_rows(); |
812 | 38 | _reader_statistics.predicate_filter_time += _current_group_reader->predicate_filter_time(); |
813 | 38 | _reader_statistics.dict_filter_rewrite_time += |
814 | 38 | _current_group_reader->dict_filter_rewrite_time(); |
815 | 38 | if (_io_ctx) { |
816 | 15 | _io_ctx->condition_cache_filtered_rows += |
817 | 15 | _current_group_reader->condition_cache_filtered_rows(); |
818 | 15 | } |
819 | | |
820 | 38 | if (_current_row_group_index.row_group_id + 1 == _total_groups) { |
821 | 37 | *eof = true; |
822 | 37 | } else { |
823 | 1 | *eof = false; |
824 | 1 | } |
825 | 38 | } |
826 | 50 | return Status::OK(); |
827 | 50 | } |
828 | | |
829 | | RowGroupReader::PositionDeleteContext ParquetReader::_get_position_delete_ctx( |
830 | 38 | const tparquet::RowGroup& row_group, const RowGroupReader::RowGroupIndex& row_group_index) { |
831 | 38 | LOG(INFO) << "[PosDeleteDebug] _get_position_delete_ctx: _delete_rows=" |
832 | 38 | << (_delete_rows ? "set(" + std::to_string(_delete_rows->size()) + ")" : "null") |
833 | 38 | << " row_group.num_rows=" << row_group.num_rows |
834 | 38 | << " first_row=" << row_group_index.first_row; |
835 | 38 | if (_delete_rows == nullptr) { |
836 | 38 | LOG(INFO) << "[PosDeleteDebug] _get_position_delete_ctx: NO delete rows, returning " |
837 | 38 | "no-filter ctx"; |
838 | 38 | return RowGroupReader::PositionDeleteContext(row_group.num_rows, row_group_index.first_row); |
839 | 38 | } |
840 | 0 | const int64_t* delete_rows = &(*_delete_rows)[0]; |
841 | 0 | const int64_t* delete_rows_end = delete_rows + _delete_rows->size(); |
842 | 0 | const int64_t* start_pos = std::lower_bound(delete_rows + _delete_rows_index, delete_rows_end, |
843 | 0 | row_group_index.first_row); |
844 | 0 | int64_t start_index = start_pos - delete_rows; |
845 | 0 | const int64_t* end_pos = std::lower_bound(start_pos, delete_rows_end, row_group_index.last_row); |
846 | 0 | int64_t end_index = end_pos - delete_rows; |
847 | 0 | _delete_rows_index = end_index; |
848 | 0 | return RowGroupReader::PositionDeleteContext(*_delete_rows, row_group.num_rows, |
849 | 0 | row_group_index.first_row, start_index, end_index); |
850 | 38 | } |
851 | | |
852 | 38 | Status ParquetReader::_next_row_group_reader() { |
853 | 38 | if (_current_group_reader != nullptr) { |
854 | 1 | _current_group_reader->collect_profile_before_close(); |
855 | 1 | } |
856 | | |
857 | 38 | RowRanges candidate_row_ranges; |
858 | 38 | while (++_current_row_group_index.row_group_id < _total_groups) { |
859 | 38 | const auto& row_group = _t_metadata->row_groups[_current_row_group_index.row_group_id]; |
860 | 38 | _current_row_group_index.first_row = _current_row_group_index.last_row; |
861 | 38 | _current_row_group_index.last_row = _current_row_group_index.last_row + row_group.num_rows; |
862 | | |
863 | 38 | if (_filter_groups && _is_misaligned_range_group(row_group)) { |
864 | 0 | continue; |
865 | 0 | } |
866 | | |
867 | 38 | candidate_row_ranges.clear(); |
868 | | // The range of lines to be read is determined by the push down predicate. |
869 | 38 | RETURN_IF_ERROR(_process_min_max_bloom_filter( |
870 | 38 | _current_row_group_index, row_group, _push_down_predicates, &candidate_row_ranges)); |
871 | | |
872 | 38 | std::function<int64_t(const FieldSchema*)> column_compressed_size = |
873 | 138 | [&row_group, &column_compressed_size](const FieldSchema* field) -> int64_t { |
874 | 138 | if (field->physical_column_index >= 0) { |
875 | 124 | int parquet_col_id = field->physical_column_index; |
876 | 124 | if (row_group.columns[parquet_col_id].__isset.meta_data) { |
877 | 124 | return row_group.columns[parquet_col_id].meta_data.total_compressed_size; |
878 | 124 | } |
879 | 0 | return 0; |
880 | 124 | } |
881 | 14 | int64_t size = 0; |
882 | 30 | for (const FieldSchema& child : field->children) { |
883 | 30 | size += column_compressed_size(&child); |
884 | 30 | } |
885 | 14 | return size; |
886 | 138 | }; |
887 | 38 | int64_t group_size = 0; // only calculate the needed columns |
888 | 108 | for (auto& read_col : _read_file_columns) { |
889 | 108 | const FieldSchema* field = _file_metadata->schema().get_column(read_col); |
890 | 108 | group_size += column_compressed_size(field); |
891 | 108 | } |
892 | | |
893 | 38 | _reader_statistics.read_rows += candidate_row_ranges.count(); |
894 | 38 | if (_io_ctx) { |
895 | 15 | _io_ctx->file_reader_stats->read_rows += candidate_row_ranges.count(); |
896 | 15 | } |
897 | | |
898 | 38 | if (candidate_row_ranges.count() != 0) { |
899 | | // need read this row group. |
900 | 38 | _reader_statistics.read_row_groups++; |
901 | 38 | _reader_statistics.filtered_page_rows += |
902 | 38 | row_group.num_rows - candidate_row_ranges.count(); |
903 | 38 | break; |
904 | 38 | } else { |
905 | | // this row group be filtered. |
906 | 0 | _reader_statistics.filtered_row_groups++; |
907 | 0 | _reader_statistics.filtered_bytes += group_size; |
908 | 0 | _reader_statistics.filtered_group_rows += row_group.num_rows; |
909 | 0 | } |
910 | 38 | } |
911 | | |
912 | 38 | if (_current_row_group_index.row_group_id == _total_groups) { |
913 | 0 | _row_group_eof = true; |
914 | 0 | _current_group_reader.reset(nullptr); |
915 | 0 | return Status::EndOfFile("No next RowGroupReader"); |
916 | 0 | } |
917 | | |
918 | | // process page index and generate the ranges to read |
919 | 38 | auto& row_group = _t_metadata->row_groups[_current_row_group_index.row_group_id]; |
920 | | |
921 | 38 | RowGroupReader::PositionDeleteContext position_delete_ctx = |
922 | 38 | _get_position_delete_ctx(row_group, _current_row_group_index); |
923 | 38 | io::FileReaderSPtr group_file_reader; |
924 | 38 | if (typeid_cast<io::InMemoryFileReader*>(_file_reader.get())) { |
925 | | // InMemoryFileReader has the ability to merge small IO |
926 | 0 | group_file_reader = _file_reader; |
927 | 38 | } else { |
928 | 38 | size_t avg_io_size = 0; |
929 | 38 | const std::vector<io::PrefetchRange> io_ranges = |
930 | 38 | _generate_random_access_ranges(_current_row_group_index, &avg_io_size); |
931 | 38 | int64_t merged_read_slice_size = -1; |
932 | 38 | if (_state != nullptr && _state->query_options().__isset.merge_read_slice_size) { |
933 | 26 | merged_read_slice_size = _state->query_options().merge_read_slice_size; |
934 | 26 | } |
935 | | // The underlying page reader will prefetch data in column. |
936 | | // Using both MergeRangeFileReader and BufferedStreamReader simultaneously would waste a lot of memory. |
937 | 38 | group_file_reader = |
938 | 38 | avg_io_size < io::MergeRangeFileReader::SMALL_IO |
939 | 38 | ? std::make_shared<io::MergeRangeFileReader>( |
940 | 38 | _profile, _file_reader, io_ranges, merged_read_slice_size) |
941 | 38 | : _file_reader; |
942 | 38 | } |
943 | 38 | _current_group_reader.reset(new RowGroupReader( |
944 | 38 | _io_ctx ? std::make_shared<io::TracingFileReader>(group_file_reader, |
945 | 15 | _io_ctx->file_reader_stats) |
946 | 38 | : group_file_reader, |
947 | 38 | _read_table_columns, _current_row_group_index.row_group_id, row_group, _ctz, _io_ctx, |
948 | 38 | position_delete_ctx, _lazy_read_ctx, _state, _column_ids, _filter_column_ids)); |
949 | 38 | _row_group_eof = false; |
950 | | |
951 | 38 | _current_group_reader->set_current_row_group_idx(_current_row_group_index); |
952 | 38 | _current_group_reader->set_row_id_column_iterator(_row_id_column_iterator_pair); |
953 | 38 | _current_group_reader->set_row_lineage_columns(_row_lineage_columns); |
954 | 38 | _current_group_reader->set_col_name_to_block_idx(_col_name_to_block_idx); |
955 | 38 | if (_condition_cache_ctx) { |
956 | 0 | _current_group_reader->set_condition_cache_context(_condition_cache_ctx); |
957 | 0 | } |
958 | 38 | _current_group_reader->set_table_format_reader(this); |
959 | | |
960 | 38 | _current_group_reader->_table_info_node_ptr = _table_info_node_ptr; |
961 | 38 | return _current_group_reader->init(_file_metadata->schema(), candidate_row_ranges, _col_offsets, |
962 | 38 | _tuple_descriptor, _row_descriptor, _colname_to_slot_id, |
963 | 38 | _not_single_slot_filter_conjuncts, |
964 | 38 | _slot_id_to_filter_conjuncts); |
965 | 38 | } |
966 | | |
967 | | std::vector<io::PrefetchRange> ParquetReader::_generate_random_access_ranges( |
968 | 38 | const RowGroupReader::RowGroupIndex& group, size_t* avg_io_size) { |
969 | 38 | std::vector<io::PrefetchRange> result; |
970 | 38 | int64_t last_chunk_end = -1; |
971 | 38 | size_t total_io_size = 0; |
972 | 38 | std::function<void(const FieldSchema*, const tparquet::RowGroup&)> scalar_range = |
973 | 136 | [&](const FieldSchema* field, const tparquet::RowGroup& row_group) { |
974 | 136 | if (_column_ids.empty() || |
975 | 136 | _column_ids.find(field->get_column_id()) != _column_ids.end()) { |
976 | 132 | if (field->data_type->get_primitive_type() == TYPE_ARRAY) { |
977 | 2 | scalar_range(&field->children[0], row_group); |
978 | 130 | } else if (field->data_type->get_primitive_type() == TYPE_MAP) { |
979 | 0 | scalar_range(&field->children[0], row_group); |
980 | 0 | scalar_range(&field->children[1], row_group); |
981 | 130 | } else if (field->data_type->get_primitive_type() == TYPE_STRUCT) { |
982 | 37 | for (int i = 0; i < field->children.size(); ++i) { |
983 | 26 | scalar_range(&field->children[i], row_group); |
984 | 26 | } |
985 | 119 | } else { |
986 | 119 | const tparquet::ColumnChunk& chunk = |
987 | 119 | row_group.columns[field->physical_column_index]; |
988 | 119 | auto& chunk_meta = chunk.meta_data; |
989 | 119 | int64_t chunk_start = has_dict_page(chunk_meta) |
990 | 119 | ? chunk_meta.dictionary_page_offset |
991 | 119 | : chunk_meta.data_page_offset; |
992 | 119 | int64_t chunk_end = chunk_start + chunk_meta.total_compressed_size; |
993 | 119 | DCHECK_GE(chunk_start, last_chunk_end); |
994 | 119 | result.emplace_back(chunk_start, chunk_end); |
995 | 119 | total_io_size += chunk_meta.total_compressed_size; |
996 | 119 | last_chunk_end = chunk_end; |
997 | 119 | } |
998 | 132 | } |
999 | 136 | }; |
1000 | 38 | const tparquet::RowGroup& row_group = _t_metadata->row_groups[group.row_group_id]; |
1001 | 108 | for (const auto& read_col : _read_file_columns) { |
1002 | 108 | const FieldSchema* field = _file_metadata->schema().get_column(read_col); |
1003 | 108 | scalar_range(field, row_group); |
1004 | 108 | } |
1005 | 38 | if (!result.empty()) { |
1006 | 37 | *avg_io_size = total_io_size / result.size(); |
1007 | 37 | } |
1008 | 38 | return result; |
1009 | 38 | } |
1010 | | |
1011 | 37 | bool ParquetReader::_is_misaligned_range_group(const tparquet::RowGroup& row_group) const { |
1012 | 37 | int64_t start_offset = _get_column_start_offset(row_group.columns[0].meta_data); |
1013 | | |
1014 | 37 | auto& last_column = row_group.columns[row_group.columns.size() - 1].meta_data; |
1015 | 37 | int64_t end_offset = _get_column_start_offset(last_column) + last_column.total_compressed_size; |
1016 | | |
1017 | 37 | int64_t row_group_mid = start_offset + (end_offset - start_offset) / 2; |
1018 | 37 | if (!(row_group_mid >= _range_start_offset && |
1019 | 37 | row_group_mid < _range_start_offset + _range_size)) { |
1020 | 0 | return true; |
1021 | 0 | } |
1022 | 37 | return false; |
1023 | 37 | } |
1024 | | |
1025 | 0 | int64_t ParquetReader::get_total_rows() const { |
1026 | 0 | if (!_t_metadata) return 0; |
1027 | 0 | if (!_filter_groups) return _t_metadata->num_rows; |
1028 | 0 | int64_t total = 0; |
1029 | 0 | for (const auto& rg : _t_metadata->row_groups) { |
1030 | 0 | if (!_is_misaligned_range_group(rg)) { |
1031 | 0 | total += rg.num_rows; |
1032 | 0 | } |
1033 | 0 | } |
1034 | 0 | return total; |
1035 | 0 | } |
1036 | | |
1037 | 0 | void ParquetReader::set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) { |
1038 | 0 | _condition_cache_ctx = std::move(ctx); |
1039 | 0 | if (!_condition_cache_ctx || !_t_metadata || !_filter_groups) { |
1040 | 0 | return; |
1041 | 0 | } |
1042 | | // Find the first assigned row group to compute base_granule. |
1043 | 0 | int64_t first_row = 0; |
1044 | 0 | for (const auto& rg : _t_metadata->row_groups) { |
1045 | 0 | if (!_is_misaligned_range_group(rg)) { |
1046 | 0 | _condition_cache_ctx->base_granule = first_row / ConditionCacheContext::GRANULE_SIZE; |
1047 | 0 | return; |
1048 | 0 | } |
1049 | 0 | first_row += rg.num_rows; |
1050 | 0 | } |
1051 | 0 | } |
1052 | | |
1053 | | Status ParquetReader::_process_page_index_filter( |
1054 | | const tparquet::RowGroup& row_group, const RowGroupReader::RowGroupIndex& row_group_index, |
1055 | | const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred, |
1056 | 18 | RowRanges* candidate_row_ranges) { |
1057 | 18 | if (UNLIKELY(_io_ctx && _io_ctx->should_stop)) { |
1058 | 0 | return Status::EndOfFile("stop"); |
1059 | 0 | } |
1060 | | |
1061 | 18 | std::function<void()> read_whole_row_group = [&]() { |
1062 | 18 | candidate_row_ranges->add(RowRange {0, row_group.num_rows}); |
1063 | 18 | }; |
1064 | | |
1065 | | // Check if the page index is available and if it exists. |
1066 | 18 | PageIndex page_index; |
1067 | 18 | if (!config::enable_parquet_page_index || _colname_to_slot_id == nullptr || |
1068 | 18 | !page_index.check_and_get_page_index_ranges(row_group.columns)) { |
1069 | 18 | read_whole_row_group(); |
1070 | 18 | return Status::OK(); |
1071 | 18 | } |
1072 | | |
1073 | 0 | std::vector<int> parquet_col_ids; |
1074 | 0 | for (size_t idx = 0; idx < _read_table_columns.size(); idx++) { |
1075 | 0 | const auto& read_table_col = _read_table_columns[idx]; |
1076 | 0 | const auto& read_file_col = _read_file_columns[idx]; |
1077 | 0 | if (!_colname_to_slot_id->contains(read_table_col)) { |
1078 | 0 | continue; |
1079 | 0 | } |
1080 | 0 | auto* field = _file_metadata->schema().get_column(read_file_col); |
1081 | |
|
1082 | 0 | std::function<void(FieldSchema * field)> f = [&](FieldSchema* field) { |
1083 | 0 | if (!_column_ids.empty() && |
1084 | 0 | _column_ids.find(field->get_column_id()) == _column_ids.end()) { |
1085 | 0 | return; |
1086 | 0 | } |
1087 | | |
1088 | 0 | if (field->data_type->get_primitive_type() == TYPE_ARRAY) { |
1089 | 0 | f(&field->children[0]); |
1090 | 0 | } else if (field->data_type->get_primitive_type() == TYPE_MAP) { |
1091 | 0 | f(&field->children[0]); |
1092 | 0 | f(&field->children[1]); |
1093 | 0 | } else if (field->data_type->get_primitive_type() == TYPE_STRUCT) { |
1094 | 0 | for (int i = 0; i < field->children.size(); ++i) { |
1095 | 0 | f(&field->children[i]); |
1096 | 0 | } |
1097 | 0 | } else { |
1098 | 0 | int parquet_col_id = field->physical_column_index; |
1099 | 0 | if (parquet_col_id >= 0) { |
1100 | 0 | parquet_col_ids.push_back(parquet_col_id); |
1101 | 0 | } |
1102 | 0 | } |
1103 | 0 | }; |
1104 | |
|
1105 | 0 | f(field); |
1106 | 0 | } |
1107 | |
|
1108 | 0 | auto parse_offset_index = [&]() -> Status { |
1109 | 0 | std::vector<uint8_t> off_index_buff(page_index._offset_index_size); |
1110 | 0 | Slice res(off_index_buff.data(), page_index._offset_index_size); |
1111 | 0 | size_t bytes_read = 0; |
1112 | 0 | { |
1113 | 0 | SCOPED_RAW_TIMER(&_reader_statistics.read_page_index_time); |
1114 | 0 | RETURN_IF_ERROR(_tracing_file_reader->read_at(page_index._offset_index_start, res, |
1115 | 0 | &bytes_read, _io_ctx)); |
1116 | 0 | } |
1117 | 0 | _column_statistics.page_index_read_calls++; |
1118 | 0 | _col_offsets.clear(); |
1119 | |
|
1120 | 0 | for (auto parquet_col_id : parquet_col_ids) { |
1121 | 0 | auto& chunk = row_group.columns[parquet_col_id]; |
1122 | 0 | if (chunk.offset_index_length == 0) [[unlikely]] { |
1123 | 0 | continue; |
1124 | 0 | } |
1125 | 0 | tparquet::OffsetIndex offset_index; |
1126 | 0 | SCOPED_RAW_TIMER(&_reader_statistics.parse_page_index_time); |
1127 | 0 | RETURN_IF_ERROR( |
1128 | 0 | page_index.parse_offset_index(chunk, off_index_buff.data(), &offset_index)); |
1129 | 0 | _col_offsets[parquet_col_id] = offset_index; |
1130 | 0 | } |
1131 | 0 | return Status::OK(); |
1132 | 0 | }; |
1133 | | |
1134 | | // from https://github.com/apache/doris/pull/55795 |
1135 | 0 | RETURN_IF_ERROR(parse_offset_index()); |
1136 | | |
1137 | | // Check if page index is needed for min-max filter. |
1138 | 0 | if (!_enable_filter_by_min_max || push_down_pred.empty()) { |
1139 | 0 | read_whole_row_group(); |
1140 | 0 | return Status::OK(); |
1141 | 0 | } |
1142 | | |
1143 | | // read column index. |
1144 | 0 | std::vector<uint8_t> col_index_buff(page_index._column_index_size); |
1145 | 0 | size_t bytes_read = 0; |
1146 | 0 | Slice result(col_index_buff.data(), page_index._column_index_size); |
1147 | 0 | { |
1148 | 0 | SCOPED_RAW_TIMER(&_reader_statistics.read_page_index_time); |
1149 | 0 | RETURN_IF_ERROR(_tracing_file_reader->read_at(page_index._column_index_start, result, |
1150 | 0 | &bytes_read, _io_ctx)); |
1151 | 0 | } |
1152 | 0 | _column_statistics.page_index_read_calls++; |
1153 | |
|
1154 | 0 | SCOPED_RAW_TIMER(&_reader_statistics.page_index_filter_time); |
1155 | | |
1156 | | // Construct a cacheable page index structure to avoid repeatedly reading the page index of the same column. |
1157 | 0 | ParquetPredicate::CachedPageIndexStat cached_page_index; |
1158 | 0 | cached_page_index.ctz = _ctz; |
1159 | 0 | std::function<bool(ParquetPredicate::PageIndexStat**, int)> get_stat_func = |
1160 | 0 | [&](ParquetPredicate::PageIndexStat** ans, const int cid) -> bool { |
1161 | 0 | if (cached_page_index.stats.contains(cid)) { |
1162 | 0 | *ans = &cached_page_index.stats[cid]; |
1163 | 0 | return (*ans)->available; |
1164 | 0 | } |
1165 | 0 | cached_page_index.stats.emplace(cid, ParquetPredicate::PageIndexStat {}); |
1166 | 0 | auto& sig_stat = cached_page_index.stats[cid]; |
1167 | |
|
1168 | 0 | auto* slot = _tuple_descriptor->slots()[cid]; |
1169 | 0 | if (!_table_info_node_ptr->children_column_exists(slot->col_name())) { |
1170 | | // table column not exist in file, may be schema change. |
1171 | 0 | return false; |
1172 | 0 | } |
1173 | | |
1174 | 0 | const auto& file_col_name = |
1175 | 0 | _table_info_node_ptr->children_file_column_name(slot->col_name()); |
1176 | 0 | const FieldSchema* col_schema = _file_metadata->schema().get_column(file_col_name); |
1177 | 0 | int parquet_col_id = col_schema->physical_column_index; |
1178 | |
|
1179 | 0 | if (parquet_col_id < 0) { |
1180 | | // complex type, not support page index yet. |
1181 | 0 | return false; |
1182 | 0 | } |
1183 | 0 | if (!_col_offsets.contains(parquet_col_id)) { |
1184 | | // If the file contains partition columns and the query applies filters on those |
1185 | | // partition columns, then reading the page index is unnecessary. |
1186 | 0 | return false; |
1187 | 0 | } |
1188 | | |
1189 | 0 | auto& column_chunk = row_group.columns[parquet_col_id]; |
1190 | 0 | if (column_chunk.column_index_length == 0 || column_chunk.offset_index_length == 0) { |
1191 | | // column no page index. |
1192 | 0 | return false; |
1193 | 0 | } |
1194 | | |
1195 | 0 | tparquet::ColumnIndex column_index; |
1196 | 0 | { |
1197 | 0 | SCOPED_RAW_TIMER(&_reader_statistics.parse_page_index_time); |
1198 | 0 | RETURN_IF_ERROR(page_index.parse_column_index(column_chunk, col_index_buff.data(), |
1199 | 0 | &column_index)); |
1200 | 0 | } |
1201 | 0 | const int64_t num_of_pages = column_index.null_pages.size(); |
1202 | 0 | if (num_of_pages <= 0) [[unlikely]] { |
1203 | | // no page. (maybe this row group no data.) |
1204 | 0 | return false; |
1205 | 0 | } |
1206 | 0 | DCHECK_EQ(column_index.min_values.size(), column_index.max_values.size()); |
1207 | 0 | if (!column_index.__isset.null_counts) { |
1208 | | // not set null or null counts; |
1209 | 0 | return false; |
1210 | 0 | } |
1211 | | |
1212 | 0 | auto& offset_index = _col_offsets[parquet_col_id]; |
1213 | 0 | const auto& page_locations = offset_index.page_locations; |
1214 | |
|
1215 | 0 | sig_stat.col_schema = col_schema; |
1216 | 0 | sig_stat.num_of_pages = num_of_pages; |
1217 | 0 | sig_stat.encoded_min_value = column_index.min_values; |
1218 | 0 | sig_stat.encoded_max_value = column_index.max_values; |
1219 | 0 | sig_stat.is_all_null.resize(num_of_pages); |
1220 | 0 | sig_stat.has_null.resize(num_of_pages); |
1221 | 0 | sig_stat.ranges.resize(num_of_pages); |
1222 | |
|
1223 | 0 | for (int page_id = 0; page_id < num_of_pages; page_id++) { |
1224 | 0 | sig_stat.is_all_null[page_id] = column_index.null_pages[page_id]; |
1225 | 0 | sig_stat.has_null[page_id] = column_index.null_counts[page_id] > 0; |
1226 | |
|
1227 | 0 | int64_t from = page_locations[page_id].first_row_index; |
1228 | 0 | int64_t to = 0; |
1229 | 0 | if (page_id == page_locations.size() - 1) { |
1230 | 0 | to = row_group_index.last_row; |
1231 | 0 | } else { |
1232 | 0 | to = page_locations[page_id + 1].first_row_index; |
1233 | 0 | } |
1234 | 0 | sig_stat.ranges[page_id] = RowRange {from, to}; |
1235 | 0 | } |
1236 | |
|
1237 | 0 | sig_stat.available = true; |
1238 | 0 | *ans = &sig_stat; |
1239 | 0 | return true; |
1240 | 0 | }; |
1241 | 0 | cached_page_index.row_group_range = {0, row_group.num_rows}; |
1242 | 0 | cached_page_index.get_stat_func = get_stat_func; |
1243 | |
|
1244 | 0 | candidate_row_ranges->add({0, row_group.num_rows}); |
1245 | 0 | for (const auto& predicate : push_down_pred) { |
1246 | 0 | RowRanges tmp_row_range; |
1247 | 0 | if (!predicate->evaluate_and(&cached_page_index, &tmp_row_range)) { |
1248 | | // no need read this row group. |
1249 | 0 | candidate_row_ranges->clear(); |
1250 | 0 | return Status::OK(); |
1251 | 0 | } |
1252 | 0 | RowRanges::ranges_intersection(*candidate_row_ranges, tmp_row_range, candidate_row_ranges); |
1253 | 0 | } |
1254 | 0 | return Status::OK(); |
1255 | 0 | } |
1256 | | |
1257 | | Status ParquetReader::_process_min_max_bloom_filter( |
1258 | | const RowGroupReader::RowGroupIndex& row_group_index, const tparquet::RowGroup& row_group, |
1259 | | const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred, |
1260 | 38 | RowRanges* row_ranges) { |
1261 | 38 | SCOPED_RAW_TIMER(&_reader_statistics.row_group_filter_time); |
1262 | 38 | if (!_filter_groups) { |
1263 | | // No row group filtering is needed; |
1264 | | // for example, Iceberg reads position delete files. |
1265 | 1 | row_ranges->add({0, row_group.num_rows}); |
1266 | 1 | return Status::OK(); |
1267 | 1 | } |
1268 | | |
1269 | 37 | if (_read_by_rows) { |
1270 | 19 | auto group_start = row_group_index.first_row; |
1271 | 19 | auto group_end = row_group_index.last_row; |
1272 | | |
1273 | 47 | while (!_row_ids.empty()) { |
1274 | 28 | auto v = _row_ids.front(); |
1275 | 28 | if (v < group_start) { |
1276 | 0 | continue; |
1277 | 28 | } else if (v < group_end) { |
1278 | 28 | row_ranges->add(RowRange {v - group_start, v - group_start + 1}); |
1279 | 28 | _row_ids.pop_front(); |
1280 | 28 | } else { |
1281 | 0 | break; |
1282 | 0 | } |
1283 | 28 | } |
1284 | 19 | } else { |
1285 | 18 | bool filter_this_row_group = false; |
1286 | 18 | bool filtered_by_min_max = false; |
1287 | 18 | bool filtered_by_bloom_filter = false; |
1288 | 18 | RETURN_IF_ERROR(_process_column_stat_filter(row_group, push_down_pred, |
1289 | 18 | &filter_this_row_group, &filtered_by_min_max, |
1290 | 18 | &filtered_by_bloom_filter)); |
1291 | | // Update statistics based on filter type |
1292 | 18 | if (filter_this_row_group) { |
1293 | 0 | if (filtered_by_min_max) { |
1294 | 0 | _reader_statistics.filtered_row_groups_by_min_max++; |
1295 | 0 | } |
1296 | 0 | if (filtered_by_bloom_filter) { |
1297 | 0 | _reader_statistics.filtered_row_groups_by_bloom_filter++; |
1298 | 0 | } |
1299 | 0 | } |
1300 | | |
1301 | 18 | if (!filter_this_row_group) { |
1302 | 18 | RETURN_IF_ERROR(_process_page_index_filter(row_group, row_group_index, push_down_pred, |
1303 | 18 | row_ranges)); |
1304 | 18 | } |
1305 | 18 | } |
1306 | | |
1307 | 37 | return Status::OK(); |
1308 | 37 | } |
1309 | | |
1310 | | Status ParquetReader::_process_column_stat_filter( |
1311 | | const tparquet::RowGroup& row_group, |
1312 | | const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred, |
1313 | 20 | bool* filter_group, bool* filtered_by_min_max, bool* filtered_by_bloom_filter) { |
1314 | | // If both filters are disabled, skip filtering |
1315 | 20 | if (!_enable_filter_by_min_max && !_enable_filter_by_bloom_filter) { |
1316 | 0 | return Status::OK(); |
1317 | 0 | } |
1318 | | |
1319 | | // Cache bloom filters for each column to avoid reading the same bloom filter multiple times |
1320 | | // when there are multiple predicates on the same column |
1321 | 20 | std::unordered_map<int, std::unique_ptr<ParquetBlockSplitBloomFilter>> bloom_filter_cache; |
1322 | | |
1323 | | // Initialize output parameters |
1324 | 20 | *filtered_by_min_max = false; |
1325 | 20 | *filtered_by_bloom_filter = false; |
1326 | | |
1327 | 20 | for (const auto& predicate : _push_down_predicates) { |
1328 | 2 | std::function<bool(ParquetPredicate::ColumnStat*, int)> get_stat_func = |
1329 | 4 | [&](ParquetPredicate::ColumnStat* stat, const int cid) { |
1330 | | // Check if min-max filter is enabled |
1331 | 4 | if (!_enable_filter_by_min_max) { |
1332 | 0 | return false; |
1333 | 0 | } |
1334 | 4 | auto* slot = _tuple_descriptor->slots()[cid]; |
1335 | 4 | if (!_table_info_node_ptr->children_column_exists(slot->col_name())) { |
1336 | 0 | return false; |
1337 | 0 | } |
1338 | 4 | const auto& file_col_name = |
1339 | 4 | _table_info_node_ptr->children_file_column_name(slot->col_name()); |
1340 | 4 | const FieldSchema* col_schema = |
1341 | 4 | _file_metadata->schema().get_column(file_col_name); |
1342 | 4 | int parquet_col_id = col_schema->physical_column_index; |
1343 | 4 | auto meta_data = row_group.columns[parquet_col_id].meta_data; |
1344 | 4 | stat->col_schema = col_schema; |
1345 | 4 | return ParquetPredicate::read_column_stats(col_schema, meta_data, |
1346 | 4 | &_ignored_stats, |
1347 | 4 | _t_metadata->created_by, stat) |
1348 | 4 | .ok(); |
1349 | 4 | }; |
1350 | 2 | std::function<bool(ParquetPredicate::ColumnStat*, int)> get_bloom_filter_func = |
1351 | 2 | [&](ParquetPredicate::ColumnStat* stat, const int cid) { |
1352 | 0 | auto* slot = _tuple_descriptor->slots()[cid]; |
1353 | 0 | if (!_table_info_node_ptr->children_column_exists(slot->col_name())) { |
1354 | 0 | return false; |
1355 | 0 | } |
1356 | 0 | const auto& file_col_name = |
1357 | 0 | _table_info_node_ptr->children_file_column_name(slot->col_name()); |
1358 | 0 | const FieldSchema* col_schema = |
1359 | 0 | _file_metadata->schema().get_column(file_col_name); |
1360 | 0 | int parquet_col_id = col_schema->physical_column_index; |
1361 | 0 | auto meta_data = row_group.columns[parquet_col_id].meta_data; |
1362 | 0 | if (!meta_data.__isset.bloom_filter_offset) { |
1363 | 0 | return false; |
1364 | 0 | } |
1365 | 0 | auto primitive_type = |
1366 | 0 | remove_nullable(col_schema->data_type)->get_primitive_type(); |
1367 | 0 | if (!ParquetPredicate::bloom_filter_supported(primitive_type)) { |
1368 | 0 | return false; |
1369 | 0 | } |
1370 | | |
1371 | | // Check if bloom filter is enabled |
1372 | 0 | if (!_enable_filter_by_bloom_filter) { |
1373 | 0 | return false; |
1374 | 0 | } |
1375 | | |
1376 | | // Check cache first |
1377 | 0 | auto cache_iter = bloom_filter_cache.find(parquet_col_id); |
1378 | 0 | if (cache_iter != bloom_filter_cache.end()) { |
1379 | | // Bloom filter already loaded for this column, reuse it |
1380 | 0 | stat->bloom_filter = std::move(cache_iter->second); |
1381 | 0 | bloom_filter_cache.erase(cache_iter); |
1382 | 0 | return stat->bloom_filter != nullptr; |
1383 | 0 | } |
1384 | | |
1385 | 0 | if (!stat->bloom_filter) { |
1386 | 0 | SCOPED_RAW_TIMER(&_reader_statistics.bloom_filter_read_time); |
1387 | 0 | auto st = ParquetPredicate::read_bloom_filter( |
1388 | 0 | meta_data, _tracing_file_reader, _io_ctx, stat); |
1389 | 0 | if (!st.ok()) { |
1390 | 0 | LOG(WARNING) << "Failed to read bloom filter for column " |
1391 | 0 | << col_schema->name << " in file " << _scan_range.path |
1392 | 0 | << ", status: " << st.to_string(); |
1393 | 0 | stat->bloom_filter.reset(); |
1394 | 0 | return false; |
1395 | 0 | } |
1396 | 0 | } |
1397 | 0 | return stat->bloom_filter != nullptr; |
1398 | 0 | }; |
1399 | 2 | ParquetPredicate::ColumnStat stat; |
1400 | 2 | stat.ctz = _ctz; |
1401 | 2 | stat.get_stat_func = &get_stat_func; |
1402 | 2 | stat.get_bloom_filter_func = &get_bloom_filter_func; |
1403 | | |
1404 | 2 | if (!predicate->evaluate_and(&stat)) { |
1405 | 1 | *filter_group = true; |
1406 | | |
1407 | | // Track which filter was used for filtering |
1408 | | // If bloom filter was loaded, it means bloom filter was used |
1409 | 1 | if (stat.bloom_filter) { |
1410 | 0 | *filtered_by_bloom_filter = true; |
1411 | 0 | } |
1412 | | // If col_schema was set but no bloom filter, it means min-max stats were used |
1413 | 1 | if (stat.col_schema && !stat.bloom_filter) { |
1414 | 1 | *filtered_by_min_max = true; |
1415 | 1 | } |
1416 | | |
1417 | 1 | return Status::OK(); |
1418 | 1 | } |
1419 | | |
1420 | | // After evaluating, if the bloom filter was used, cache it for subsequent predicates |
1421 | 1 | if (stat.bloom_filter) { |
1422 | | // Find the column id for caching |
1423 | 0 | for (auto* slot : _tuple_descriptor->slots()) { |
1424 | 0 | if (_table_info_node_ptr->children_column_exists(slot->col_name())) { |
1425 | 0 | const auto& file_col_name = |
1426 | 0 | _table_info_node_ptr->children_file_column_name(slot->col_name()); |
1427 | 0 | const FieldSchema* col_schema = |
1428 | 0 | _file_metadata->schema().get_column(file_col_name); |
1429 | 0 | int parquet_col_id = col_schema->physical_column_index; |
1430 | 0 | if (stat.col_schema == col_schema) { |
1431 | 0 | bloom_filter_cache[parquet_col_id] = std::move(stat.bloom_filter); |
1432 | 0 | break; |
1433 | 0 | } |
1434 | 0 | } |
1435 | 0 | } |
1436 | 0 | } |
1437 | 1 | } |
1438 | | |
1439 | | // Update filter statistics if this row group was not filtered |
1440 | | // The statistics will be updated in _init_row_groups when filter_group is true |
1441 | 19 | return Status::OK(); |
1442 | 20 | } |
1443 | | |
1444 | 74 | int64_t ParquetReader::_get_column_start_offset(const tparquet::ColumnMetaData& column) const { |
1445 | 74 | return has_dict_page(column) ? column.dictionary_page_offset : column.data_page_offset; |
1446 | 74 | } |
1447 | | |
1448 | 14 | void ParquetReader::_collect_profile() { |
1449 | 14 | if (_profile == nullptr) { |
1450 | 0 | return; |
1451 | 0 | } |
1452 | | |
1453 | 14 | if (_current_group_reader != nullptr) { |
1454 | 14 | _current_group_reader->collect_profile_before_close(); |
1455 | 14 | } |
1456 | 14 | COUNTER_UPDATE(_parquet_profile.filtered_row_groups, _reader_statistics.filtered_row_groups); |
1457 | 14 | COUNTER_UPDATE(_parquet_profile.filtered_row_groups_by_min_max, |
1458 | 14 | _reader_statistics.filtered_row_groups_by_min_max); |
1459 | 14 | COUNTER_UPDATE(_parquet_profile.filtered_row_groups_by_bloom_filter, |
1460 | 14 | _reader_statistics.filtered_row_groups_by_bloom_filter); |
1461 | 14 | COUNTER_UPDATE(_parquet_profile.to_read_row_groups, _reader_statistics.read_row_groups); |
1462 | 14 | COUNTER_UPDATE(_parquet_profile.total_row_groups, _total_groups); |
1463 | 14 | COUNTER_UPDATE(_parquet_profile.filtered_group_rows, _reader_statistics.filtered_group_rows); |
1464 | 14 | COUNTER_UPDATE(_parquet_profile.filtered_page_rows, _reader_statistics.filtered_page_rows); |
1465 | 14 | COUNTER_UPDATE(_parquet_profile.lazy_read_filtered_rows, |
1466 | 14 | _reader_statistics.lazy_read_filtered_rows); |
1467 | 14 | COUNTER_UPDATE(_parquet_profile.filtered_bytes, _reader_statistics.filtered_bytes); |
1468 | 14 | COUNTER_UPDATE(_parquet_profile.raw_rows_read, _reader_statistics.read_rows); |
1469 | 14 | COUNTER_UPDATE(_parquet_profile.column_read_time, _reader_statistics.column_read_time); |
1470 | 14 | COUNTER_UPDATE(_parquet_profile.parse_meta_time, _reader_statistics.parse_meta_time); |
1471 | 14 | COUNTER_UPDATE(_parquet_profile.parse_footer_time, _reader_statistics.parse_footer_time); |
1472 | 14 | COUNTER_UPDATE(_parquet_profile.file_reader_create_time, |
1473 | 14 | _reader_statistics.file_reader_create_time); |
1474 | 14 | COUNTER_UPDATE(_parquet_profile.open_file_num, _reader_statistics.open_file_num); |
1475 | 14 | COUNTER_UPDATE(_parquet_profile.page_index_filter_time, |
1476 | 14 | _reader_statistics.page_index_filter_time); |
1477 | 14 | COUNTER_UPDATE(_parquet_profile.read_page_index_time, _reader_statistics.read_page_index_time); |
1478 | 14 | COUNTER_UPDATE(_parquet_profile.parse_page_index_time, |
1479 | 14 | _reader_statistics.parse_page_index_time); |
1480 | 14 | COUNTER_UPDATE(_parquet_profile.row_group_filter_time, |
1481 | 14 | _reader_statistics.row_group_filter_time); |
1482 | 14 | COUNTER_UPDATE(_parquet_profile.file_footer_read_calls, |
1483 | 14 | _reader_statistics.file_footer_read_calls); |
1484 | 14 | COUNTER_UPDATE(_parquet_profile.file_footer_hit_cache, |
1485 | 14 | _reader_statistics.file_footer_hit_cache); |
1486 | | |
1487 | 14 | COUNTER_UPDATE(_parquet_profile.skip_page_header_num, _column_statistics.skip_page_header_num); |
1488 | 14 | COUNTER_UPDATE(_parquet_profile.parse_page_header_num, |
1489 | 14 | _column_statistics.parse_page_header_num); |
1490 | 14 | COUNTER_UPDATE(_parquet_profile.predicate_filter_time, |
1491 | 14 | _reader_statistics.predicate_filter_time); |
1492 | 14 | COUNTER_UPDATE(_parquet_profile.dict_filter_rewrite_time, |
1493 | 14 | _reader_statistics.dict_filter_rewrite_time); |
1494 | 14 | COUNTER_UPDATE(_parquet_profile.bloom_filter_read_time, |
1495 | 14 | _reader_statistics.bloom_filter_read_time); |
1496 | 14 | COUNTER_UPDATE(_parquet_profile.page_index_read_calls, |
1497 | 14 | _column_statistics.page_index_read_calls); |
1498 | 14 | COUNTER_UPDATE(_parquet_profile.decompress_time, _column_statistics.decompress_time); |
1499 | 14 | COUNTER_UPDATE(_parquet_profile.decompress_cnt, _column_statistics.decompress_cnt); |
1500 | 14 | COUNTER_UPDATE(_parquet_profile.page_read_counter, _column_statistics.page_read_counter); |
1501 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_write_counter, |
1502 | 14 | _column_statistics.page_cache_write_counter); |
1503 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_compressed_write_counter, |
1504 | 14 | _column_statistics.page_cache_compressed_write_counter); |
1505 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_decompressed_write_counter, |
1506 | 14 | _column_statistics.page_cache_decompressed_write_counter); |
1507 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_hit_counter, |
1508 | 14 | _column_statistics.page_cache_hit_counter); |
1509 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_missing_counter, |
1510 | 14 | _column_statistics.page_cache_missing_counter); |
1511 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_compressed_hit_counter, |
1512 | 14 | _column_statistics.page_cache_compressed_hit_counter); |
1513 | 14 | COUNTER_UPDATE(_parquet_profile.page_cache_decompressed_hit_counter, |
1514 | 14 | _column_statistics.page_cache_decompressed_hit_counter); |
1515 | 14 | COUNTER_UPDATE(_parquet_profile.decode_header_time, _column_statistics.decode_header_time); |
1516 | 14 | COUNTER_UPDATE(_parquet_profile.read_page_header_time, |
1517 | 14 | _column_statistics.read_page_header_time); |
1518 | 14 | COUNTER_UPDATE(_parquet_profile.decode_value_time, _column_statistics.decode_value_time); |
1519 | 14 | COUNTER_UPDATE(_parquet_profile.decode_dict_time, _column_statistics.decode_dict_time); |
1520 | 14 | COUNTER_UPDATE(_parquet_profile.decode_level_time, _column_statistics.decode_level_time); |
1521 | 14 | COUNTER_UPDATE(_parquet_profile.decode_null_map_time, _column_statistics.decode_null_map_time); |
1522 | 14 | } |
1523 | | |
1524 | 14 | void ParquetReader::_collect_profile_before_close() { |
1525 | 14 | _collect_profile(); |
1526 | 14 | } |
1527 | | |
1528 | | #include "common/compile_check_end.h" |
1529 | | } // namespace doris |