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