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