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