be/src/format_v2/parquet/parquet_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_v2/parquet/parquet_reader.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <map> |
22 | | #include <memory> |
23 | | #include <optional> |
24 | | #include <ranges> |
25 | | #include <unordered_set> |
26 | | #include <utility> |
27 | | #include <vector> |
28 | | |
29 | | #include "common/cast_set.h" |
30 | | #include "core/assert_cast.h" |
31 | | #include "core/block/block.h" |
32 | | #include "core/data_type/data_type_array.h" |
33 | | #include "core/data_type/data_type_factory.hpp" |
34 | | #include "core/data_type/data_type_map.h" |
35 | | #include "core/data_type/data_type_nullable.h" |
36 | | #include "core/data_type/data_type_struct.h" |
37 | | #include "format_v2/column_mapper.h" |
38 | | #include "format_v2/parquet/parquet_column_schema.h" |
39 | | #include "format_v2/parquet/parquet_file_context.h" |
40 | | #include "format_v2/parquet/parquet_scan.h" |
41 | | #include "format_v2/parquet/parquet_statistics.h" |
42 | | #include "format_v2/parquet/reader/column_reader.h" |
43 | | #include "io/io_common.h" |
44 | | #include "runtime/runtime_state.h" |
45 | | |
46 | | namespace doris::format::parquet { |
47 | | |
48 | | struct ParquetReaderScanState { |
49 | | ParquetFileContext file_context; |
50 | | std::vector<std::unique_ptr<ParquetColumnSchema>> file_schema; |
51 | | RowGroupScanPlan scan_plan; |
52 | | ParquetScanScheduler scheduler; |
53 | | const RuntimeState* runtime_state = nullptr; |
54 | | const cctz::time_zone* timezone = nullptr; |
55 | | bool enable_bloom_filter = false; |
56 | | bool enable_page_cache = false; |
57 | | bool enable_strict_mode = false; |
58 | | }; |
59 | | |
60 | 228 | int64_t column_chunk_start_offset(const ::parquet::ColumnChunkMetaData& column_metadata) { |
61 | 228 | return column_metadata.has_dictionary_page() |
62 | 228 | ? cast_set<int64_t>(column_metadata.dictionary_page_offset()) |
63 | 228 | : cast_set<int64_t>(column_metadata.data_page_offset()); |
64 | 228 | } |
65 | | |
66 | | void collect_all_leaf_column_ids(const ParquetColumnSchema& column_schema, |
67 | 215 | std::unordered_set<int>* leaf_column_ids) { |
68 | 215 | DORIS_CHECK(leaf_column_ids != nullptr); |
69 | 215 | if (column_schema.kind == ParquetColumnSchemaKind::PRIMITIVE) { |
70 | 206 | if (column_schema.leaf_column_id >= 0) { |
71 | 206 | leaf_column_ids->insert(column_schema.leaf_column_id); |
72 | 206 | } |
73 | 206 | return; |
74 | 206 | } |
75 | 13 | for (const auto& child : column_schema.children) { |
76 | 13 | DORIS_CHECK(child != nullptr); |
77 | 13 | collect_all_leaf_column_ids(*child, leaf_column_ids); |
78 | 13 | } |
79 | 9 | } |
80 | | |
81 | | void collect_projected_leaf_column_ids(const ParquetColumnSchema& column_schema, |
82 | | const format::LocalColumnIndex& projection, |
83 | 209 | std::unordered_set<int>* leaf_column_ids) { |
84 | 209 | DORIS_CHECK(leaf_column_ids != nullptr); |
85 | 209 | if (projection.project_all_children || projection.children.empty()) { |
86 | 202 | collect_all_leaf_column_ids(column_schema, leaf_column_ids); |
87 | 202 | return; |
88 | 202 | } |
89 | 8 | for (const auto& child_projection : projection.children) { |
90 | 8 | const auto child_it = |
91 | 13 | std::ranges::find_if(column_schema.children, [&](const auto& child_schema) { |
92 | 13 | return child_schema->local_id == child_projection.local_id(); |
93 | 13 | }); |
94 | 8 | DORIS_CHECK(child_it != column_schema.children.end()); |
95 | 8 | collect_projected_leaf_column_ids(**child_it, child_projection, leaf_column_ids); |
96 | 8 | } |
97 | 7 | } |
98 | | |
99 | | void collect_request_leaf_column_ids( |
100 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
101 | 161 | const format::FileScanRequest& request, std::unordered_set<int>* leaf_column_ids) { |
102 | 161 | DORIS_CHECK(leaf_column_ids != nullptr); |
103 | 232 | auto collect_scan_column = [&](const format::LocalColumnIndex& projection) { |
104 | 232 | const auto local_id = projection.local_id(); |
105 | 232 | if (local_id == format::ROW_POSITION_COLUMN_ID || |
106 | 232 | local_id == format::GLOBAL_ROWID_COLUMN_ID) { |
107 | 31 | return; |
108 | 31 | } |
109 | 201 | DORIS_CHECK(local_id >= 0 && local_id < static_cast<int32_t>(file_schema.size())); |
110 | 201 | DORIS_CHECK(file_schema[local_id] != nullptr); |
111 | 201 | collect_projected_leaf_column_ids(*file_schema[local_id], projection, leaf_column_ids); |
112 | 201 | }; |
113 | 161 | for (const auto& column : request.predicate_columns) { |
114 | 85 | collect_scan_column(column); |
115 | 85 | } |
116 | 161 | for (const auto& column : request.non_predicate_columns) { |
117 | 147 | collect_scan_column(column); |
118 | 147 | } |
119 | 161 | } |
120 | | |
121 | | std::vector<ParquetPageCacheRange> build_page_cache_ranges( |
122 | | const ::parquet::FileMetaData& metadata, |
123 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
124 | 161 | const format::FileScanRequest& request, const RowGroupScanPlan& row_group_plan) { |
125 | 161 | std::unordered_set<int> leaf_column_ids; |
126 | 161 | collect_request_leaf_column_ids(file_schema, request, &leaf_column_ids); |
127 | 161 | std::vector<ParquetPageCacheRange> ranges; |
128 | 161 | ranges.reserve(row_group_plan.row_groups.size() * leaf_column_ids.size()); |
129 | 184 | for (const auto& row_group_plan_item : row_group_plan.row_groups) { |
130 | 184 | auto row_group_metadata = metadata.RowGroup(row_group_plan_item.row_group_id); |
131 | 184 | DORIS_CHECK(row_group_metadata != nullptr); |
132 | 228 | for (const auto leaf_column_id : leaf_column_ids) { |
133 | 228 | DORIS_CHECK(leaf_column_id >= 0 && leaf_column_id < row_group_metadata->num_columns()); |
134 | 228 | auto column_metadata = row_group_metadata->ColumnChunk(leaf_column_id); |
135 | 228 | DORIS_CHECK(column_metadata != nullptr); |
136 | 228 | const int64_t offset = column_chunk_start_offset(*column_metadata); |
137 | 228 | const int64_t size = column_metadata->total_compressed_size(); |
138 | 228 | DORIS_CHECK(offset >= 0); |
139 | 228 | DORIS_CHECK(size >= 0); |
140 | 228 | if (size > 0) { |
141 | 228 | ranges.push_back(ParquetPageCacheRange {.offset = offset, .size = size}); |
142 | 228 | } |
143 | 228 | } |
144 | 184 | } |
145 | 161 | return ranges; |
146 | 161 | } |
147 | | |
148 | | const ParquetColumnSchema& projected_root_schema( |
149 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
150 | 6 | const format::LocalColumnIndex& projection) { |
151 | 6 | const auto local_id = projection.local_id(); |
152 | 6 | DORIS_CHECK(local_id >= 0 && local_id < static_cast<int32_t>(file_schema.size())); |
153 | 6 | DORIS_CHECK(file_schema[local_id] != nullptr); |
154 | 6 | return *file_schema[local_id]; |
155 | 6 | } |
156 | | |
157 | | int64_t count_loaded_non_null_values(const ParquetColumnSchema& root_schema, |
158 | | const ParquetColumnReader& shape_reader, |
159 | 7 | int64_t expected_rows) { |
160 | 7 | const auto& def_levels = shape_reader.nested_definition_levels(); |
161 | 7 | const auto& rep_levels = shape_reader.nested_repetition_levels(); |
162 | 7 | const int64_t levels_written = shape_reader.nested_levels_written(); |
163 | 7 | DORIS_CHECK(levels_written >= expected_rows); |
164 | 7 | if (root_schema.max_repetition_level == 0) { |
165 | 3 | DORIS_CHECK(levels_written == expected_rows); |
166 | 3 | const int16_t non_null_definition_level = root_schema.nullable_definition_level; |
167 | 3 | int64_t count = 0; |
168 | 12 | for (int64_t level_idx = 0; level_idx < levels_written; ++level_idx) { |
169 | 9 | count += def_levels[level_idx] >= non_null_definition_level ? 1 : 0; |
170 | 9 | } |
171 | 3 | return count; |
172 | 3 | } |
173 | | |
174 | | // For repeated encodings, repetition level zero starts a top-level row. Empty MAP/LIST rows |
175 | | // have no entries but still carry a level slot; they are non-NULL and must be counted by |
176 | | // count(col). The root nullable level distinguishes a NULL top-level value from a non-NULL |
177 | | // value regardless of which repeated leaf represents its shape. |
178 | 4 | const int16_t non_null_definition_level = root_schema.nullable_definition_level; |
179 | 4 | int64_t counted_rows = 0; |
180 | 4 | int64_t non_null_rows = 0; |
181 | 26 | for (int64_t level_idx = 0; level_idx < levels_written && counted_rows < expected_rows; |
182 | 22 | ++level_idx) { |
183 | 22 | if (rep_levels[level_idx] != 0) { |
184 | 2 | continue; |
185 | 2 | } |
186 | 20 | ++counted_rows; |
187 | 20 | non_null_rows += def_levels[level_idx] >= non_null_definition_level ? 1 : 0; |
188 | 20 | } |
189 | 4 | DORIS_CHECK(counted_rows == expected_rows); |
190 | 4 | return non_null_rows; |
191 | 7 | } |
192 | | |
193 | 0 | DataTypePtr nullable_like_original(const DataTypePtr& type, DataTypePtr nested_type) { |
194 | 0 | return type != nullptr && type->is_nullable() ? make_nullable(nested_type) : nested_type; |
195 | 0 | } |
196 | | |
197 | 3 | int timestamp_tz_scale(const ParquetTypeDescriptor& type_descriptor) { |
198 | 3 | switch (type_descriptor.time_unit) { |
199 | 0 | case ParquetTimeUnit::MILLIS: |
200 | 0 | return 3; |
201 | 2 | case ParquetTimeUnit::MICROS: |
202 | 3 | case ParquetTimeUnit::UNKNOWN: |
203 | 3 | default: |
204 | 3 | return 6; |
205 | 3 | } |
206 | 3 | } |
207 | | |
208 | 4 | bool should_map_to_timestamp_tz(const ParquetColumnSchema& column_schema) { |
209 | 4 | const auto& type_descriptor = column_schema.type_descriptor; |
210 | 4 | return type_descriptor.physical_type == ::parquet::Type::INT96 || |
211 | 4 | (type_descriptor.is_timestamp && type_descriptor.timestamp_is_adjusted_to_utc); |
212 | 4 | } |
213 | | |
214 | 4 | DataTypePtr apply_timestamp_tz_mapping(ParquetColumnSchema* column_schema) { |
215 | 4 | DORIS_CHECK(column_schema != nullptr); |
216 | 4 | if (column_schema->kind == ParquetColumnSchemaKind::PRIMITIVE) { |
217 | 4 | if (should_map_to_timestamp_tz(*column_schema)) { |
218 | 3 | const bool nullable = |
219 | 3 | column_schema->type != nullptr && column_schema->type->is_nullable(); |
220 | 3 | const auto scale = timestamp_tz_scale(column_schema->type_descriptor); |
221 | 3 | column_schema->type = DataTypeFactory::instance().create_data_type(TYPE_TIMESTAMPTZ, |
222 | 3 | nullable, 0, scale); |
223 | 3 | column_schema->type_descriptor.doris_type = column_schema->type; |
224 | 3 | } |
225 | 4 | return column_schema->type; |
226 | 4 | } |
227 | | |
228 | 0 | std::vector<DataTypePtr> child_types; |
229 | 0 | child_types.reserve(column_schema->children.size()); |
230 | 0 | for (auto& child : column_schema->children) { |
231 | 0 | child_types.push_back(apply_timestamp_tz_mapping(child.get())); |
232 | 0 | } |
233 | |
|
234 | 0 | if (column_schema->kind == ParquetColumnSchemaKind::LIST) { |
235 | 0 | DORIS_CHECK(child_types.size() == 1); |
236 | 0 | column_schema->type = nullable_like_original( |
237 | 0 | column_schema->type, std::make_shared<DataTypeArray>(child_types[0])); |
238 | 0 | } else if (column_schema->kind == ParquetColumnSchemaKind::MAP) { |
239 | 0 | DORIS_CHECK(child_types.size() == 2); |
240 | 0 | column_schema->type = nullable_like_original( |
241 | 0 | column_schema->type, std::make_shared<DataTypeMap>(make_nullable(child_types[0]), |
242 | 0 | make_nullable(child_types[1]))); |
243 | 0 | } else if (column_schema->kind == ParquetColumnSchemaKind::STRUCT) { |
244 | 0 | Strings child_names; |
245 | 0 | child_names.reserve(column_schema->children.size()); |
246 | 0 | for (const auto& child : column_schema->children) { |
247 | 0 | child_names.push_back(child->name); |
248 | 0 | } |
249 | 0 | column_schema->type = nullable_like_original( |
250 | 0 | column_schema->type, std::make_shared<DataTypeStruct>(child_types, child_names)); |
251 | 0 | } |
252 | 0 | return column_schema->type; |
253 | 4 | } |
254 | | |
255 | | static Status find_projected_minmax_leaf(const ParquetColumnSchema& column_schema, |
256 | | const format::LocalColumnIndex& projection, |
257 | 15 | const ParquetColumnSchema** leaf_schema) { |
258 | 15 | DORIS_CHECK(leaf_schema != nullptr); |
259 | 15 | if (projection.project_all_children || projection.children.empty()) { |
260 | 13 | if (column_schema.leaf_column_id < 0) { |
261 | 2 | return Status::NotSupported( |
262 | 2 | "Parquet aggregate pushdown only supports primitive column {}", |
263 | 2 | column_schema.name); |
264 | 2 | } |
265 | 11 | if (column_schema.max_repetition_level > 0) { |
266 | 0 | return Status::NotSupported( |
267 | 0 | "Parquet aggregate pushdown does not support repeated column {}", |
268 | 0 | column_schema.name); |
269 | 0 | } |
270 | 11 | *leaf_schema = &column_schema; |
271 | 11 | return Status::OK(); |
272 | 11 | } |
273 | 2 | if (projection.children.size() != 1) { |
274 | 0 | return Status::NotSupported( |
275 | 0 | "Parquet aggregate pushdown only supports a single nested leaf under column {}", |
276 | 0 | column_schema.name); |
277 | 0 | } |
278 | 2 | const auto& child_projection = projection.children[0]; |
279 | 2 | const auto child_schema_it = |
280 | 2 | std::ranges::find_if(column_schema.children, [&](const auto& child_schema) { |
281 | 2 | return child_schema->local_id == child_projection.local_id(); |
282 | 2 | }); |
283 | 2 | if (child_schema_it != column_schema.children.end()) { |
284 | 2 | return find_projected_minmax_leaf(**child_schema_it, child_projection, leaf_schema); |
285 | 2 | } |
286 | 0 | return Status::InvalidArgument("Invalid parquet aggregate projection local id {} for column {}", |
287 | 0 | child_projection.local_id(), column_schema.name); |
288 | 2 | } |
289 | | |
290 | 11 | static Status validate_minmax_aggregate_statistics(const ParquetColumnSchema& column_schema) { |
291 | 11 | DORIS_CHECK(column_schema.descriptor != nullptr); |
292 | 11 | switch (column_schema.descriptor->physical_type()) { |
293 | 1 | case ::parquet::Type::BYTE_ARRAY: |
294 | 2 | case ::parquet::Type::FIXED_LEN_BYTE_ARRAY: |
295 | | // Arrow 17 does not expose Parquet's min/max exactness flags. Binary statistics may be |
296 | | // truncated bounds rather than values present in the file, so they are safe for pruning |
297 | | // but cannot be returned as exact aggregate results. |
298 | 2 | return Status::NotSupported( |
299 | 2 | "Parquet MIN/MAX aggregate pushdown requires exact statistics for column {}", |
300 | 2 | column_schema.name); |
301 | 9 | default: |
302 | 9 | return Status::OK(); |
303 | 11 | } |
304 | 11 | } |
305 | | |
306 | | void ParquetReader::_fill_column_definition(const ParquetColumnSchema& column_schema, |
307 | 324 | format::ColumnDefinition* field) const { |
308 | 324 | if (column_schema.parquet_field_id >= 0) { |
309 | 134 | field->identifier = Field::create_field<TYPE_INT>(column_schema.parquet_field_id); |
310 | 190 | } else { |
311 | 190 | field->identifier = Field::create_field<TYPE_STRING>(column_schema.name); |
312 | 190 | } |
313 | 324 | field->local_id = column_schema.local_id; |
314 | 324 | field->name = column_schema.name; |
315 | 324 | field->type = column_schema.type != nullptr && !column_schema.type->is_nullable() |
316 | 324 | ? make_nullable(column_schema.type) |
317 | 324 | : column_schema.type; |
318 | 324 | field->children.clear(); |
319 | 324 | field->children.reserve(column_schema.children.size()); |
320 | 324 | for (const auto& child : column_schema.children) { |
321 | 28 | format::ColumnDefinition child_field; |
322 | 28 | _fill_column_definition(*child, &child_field); |
323 | 28 | field->children.push_back(std::move(child_field)); |
324 | 28 | } |
325 | 324 | } |
326 | | |
327 | | ParquetReader::ParquetReader(std::shared_ptr<io::FileSystemProperties>& system_properties, |
328 | | std::unique_ptr<io::FileDescription>& file_description, |
329 | | std::shared_ptr<io::IOContext> io_ctx, RuntimeProfile* profile, |
330 | | std::optional<format::GlobalRowIdContext> global_rowid_context, |
331 | | bool enable_mapping_timestamp_tz) |
332 | 166 | : FileReader(system_properties, file_description, io_ctx, profile), |
333 | 166 | _global_rowid_context(global_rowid_context), |
334 | 166 | _enable_mapping_timestamp_tz(enable_mapping_timestamp_tz) {} |
335 | | |
336 | 166 | ParquetReader::~ParquetReader() = default; |
337 | | |
338 | 165 | Status ParquetReader::init(RuntimeState* state) { |
339 | 165 | if (_io_ctx != nullptr && _io_ctx->should_stop) { |
340 | 0 | return Status::EndOfFile("stop"); |
341 | 0 | } |
342 | 165 | RETURN_IF_ERROR(format::FileReader::init(state)); |
343 | 165 | if (_profile != nullptr) { |
344 | 77 | COUNTER_UPDATE(_parquet_profile.file_reader_create_time, |
345 | 77 | _reader_statistics.file_reader_create_time); |
346 | 77 | COUNTER_UPDATE(_parquet_profile.open_file_num, _reader_statistics.open_file_num); |
347 | 77 | } |
348 | 165 | _state = std::make_unique<ParquetReaderScanState>(); |
349 | 165 | _state->enable_bloom_filter = |
350 | 165 | state != nullptr && state->query_options().enable_parquet_filter_by_bloom_filter; |
351 | 165 | _state->enable_page_cache = |
352 | 165 | state != nullptr && state->query_options().enable_parquet_file_page_cache; |
353 | 165 | if (state != nullptr) { |
354 | 165 | _state->runtime_state = state; |
355 | 165 | _state->timezone = &state->timezone_obj(); |
356 | 165 | _state->enable_strict_mode = state->enable_strict_mode(); |
357 | 165 | _state->scheduler.set_timezone(&state->timezone_obj()); |
358 | 165 | _state->scheduler.set_enable_strict_mode(_state->enable_strict_mode); |
359 | 165 | } |
360 | 165 | int64_t merge_read_slice_size = -1; |
361 | 165 | if (state != nullptr && state->query_options().__isset.merge_read_slice_size) { |
362 | 165 | merge_read_slice_size = state->query_options().merge_read_slice_size; |
363 | 165 | } |
364 | 165 | _state->scheduler.set_merge_read_options(_profile, merge_read_slice_size); |
365 | 165 | _state->scheduler.set_batch_size(_batch_size); |
366 | | // Open parquet file and parse metadata to get file schema. |
367 | 165 | RETURN_IF_ERROR(_state->file_context.open(_tracing_file_reader, _io_ctx.get(), |
368 | 165 | _state->enable_page_cache, *_file_description)); |
369 | | // Build file schema from parquet metadata. |
370 | | // A file reader may expose raw file identifiers, such as Parquet field_id, through ColumnDefinition::identifier |
371 | 165 | RETURN_IF_ERROR( |
372 | 165 | build_parquet_column_schema(*_state->file_context.schema, &_state->file_schema)); |
373 | 165 | if (_enable_mapping_timestamp_tz) { |
374 | 4 | for (auto& column_schema : _state->file_schema) { |
375 | 4 | apply_timestamp_tz_mapping(column_schema.get()); |
376 | 4 | } |
377 | 3 | } |
378 | 165 | return Status::OK(); |
379 | 165 | } |
380 | | |
381 | 1 | void ParquetReader::set_batch_size(size_t batch_size) { |
382 | 1 | _batch_size = std::max<size_t>(1, batch_size); |
383 | 1 | if (_state != nullptr) { |
384 | 0 | _state->scheduler.set_batch_size(_batch_size); |
385 | 0 | } |
386 | 1 | } |
387 | | |
388 | 150 | Status ParquetReader::get_schema(std::vector<format::ColumnDefinition>* file_schema) const { |
389 | 150 | if (file_schema == nullptr) { |
390 | 0 | return Status::InvalidArgument("file_schema is null"); |
391 | 0 | } |
392 | 150 | file_schema->clear(); |
393 | 150 | if (_state == nullptr || _state->file_context.schema == nullptr) { |
394 | 0 | return Status::Uninitialized("ParquetReader is not open"); |
395 | 0 | } |
396 | | |
397 | 150 | file_schema->reserve(_state->file_schema.size()); |
398 | 446 | for (size_t column_idx = 0; column_idx < _state->file_schema.size(); ++column_idx) { |
399 | 296 | format::ColumnDefinition field; |
400 | 296 | _fill_column_definition(*_state->file_schema[column_idx], &field); |
401 | 296 | DORIS_CHECK(field.local_id == static_cast<int32_t>(column_idx)); |
402 | 296 | file_schema->push_back(std::move(field)); |
403 | 296 | } |
404 | 150 | if (_global_rowid_context.has_value()) { |
405 | 2 | file_schema->push_back(format::global_rowid_column_definition()); |
406 | 2 | } |
407 | 150 | return Status::OK(); |
408 | 150 | } |
409 | | |
410 | | std::unique_ptr<format::TableColumnMapper> ParquetReader::create_column_mapper( |
411 | 80 | format::TableColumnMapperOptions options) const { |
412 | 80 | return std::make_unique<format::ParquetColumnMapper>(std::move(options)); |
413 | 80 | } |
414 | | |
415 | 161 | Status ParquetReader::open(std::shared_ptr<format::FileScanRequest> request) { |
416 | 161 | if (_state == nullptr || _state->file_context.metadata == nullptr || |
417 | 161 | _state->file_context.schema == nullptr) { |
418 | 0 | return Status::Uninitialized("ParquetReader is not open"); |
419 | 0 | } |
420 | 161 | auto request_snapshot = request; |
421 | 161 | DORIS_CHECK(request_snapshot != nullptr); |
422 | 161 | RETURN_IF_ERROR(format::FileReader::open(std::move(request))); |
423 | | |
424 | | // `local_positions.empty()` means all columns are needed by table reader |
425 | | // TODO(gabriel): It will happen only for TVF `select *` query. |
426 | 161 | if (request_snapshot->local_positions.empty()) { |
427 | 36 | for (const auto& col : request_snapshot->predicate_columns) { |
428 | 13 | request_snapshot->local_positions.emplace(col.column_id(), |
429 | 13 | format::LocalIndex(col.column_id().value())); |
430 | 13 | } |
431 | 36 | for (const auto& col : request_snapshot->non_predicate_columns) { |
432 | 17 | request_snapshot->local_positions.emplace(col.column_id(), |
433 | 17 | format::LocalIndex(col.column_id().value())); |
434 | 17 | } |
435 | 36 | } |
436 | | |
437 | 161 | const auto num_fields = static_cast<int32_t>(_state->file_schema.size()); |
438 | 161 | for (const auto& col : request_snapshot->predicate_columns) { |
439 | 85 | DORIS_CHECK(request_snapshot->local_positions.count(col.column_id()) > 0); |
440 | 85 | const auto local_id = col.local_id(); |
441 | 85 | if (local_id == format::ROW_POSITION_COLUMN_ID || |
442 | 85 | local_id == format::GLOBAL_ROWID_COLUMN_ID) { |
443 | 17 | continue; |
444 | 17 | } |
445 | 68 | DORIS_CHECK(local_id >= 0 && local_id < num_fields); |
446 | 68 | } |
447 | 161 | for (const auto& col : request_snapshot->non_predicate_columns) { |
448 | 147 | DORIS_CHECK(request_snapshot->local_positions.count(col.column_id()) > 0); |
449 | 147 | const auto local_id = col.local_id(); |
450 | 147 | if (local_id == format::ROW_POSITION_COLUMN_ID || |
451 | 147 | local_id == format::GLOBAL_ROWID_COLUMN_ID) { |
452 | 14 | continue; |
453 | 14 | } |
454 | 133 | DORIS_CHECK(local_id >= 0 && local_id < num_fields); |
455 | 133 | } |
456 | | |
457 | 161 | RowGroupScanPlan row_group_plan; |
458 | 161 | ParquetScanRange scan_range; |
459 | 161 | scan_range.start_offset = _file_description->range_start_offset; |
460 | 161 | scan_range.size = _file_description->range_size; |
461 | 161 | scan_range.file_size = _file_description->file_size; |
462 | | // Get selected ranges in row groups according to metadata (Row-Group level index and Page Index including Zonemap, Dictionary, Bloom Filter). |
463 | 161 | RETURN_IF_ERROR(plan_parquet_row_groups( |
464 | 161 | *_state->file_context.metadata, _state->file_context.file_reader.get(), |
465 | 161 | _state->file_schema, *request_snapshot, scan_range, _state->enable_bloom_filter, |
466 | 161 | &row_group_plan, _state->timezone, _state->runtime_state)); |
467 | 161 | if (_profile != nullptr) { |
468 | 77 | _parquet_profile.update_pruning_stats(row_group_plan.pruning_stats); |
469 | 77 | } |
470 | 161 | if (_state->enable_page_cache) { |
471 | 161 | _state->file_context.register_page_cache_ranges( |
472 | 161 | build_page_cache_ranges(*_state->file_context.metadata, _state->file_schema, |
473 | 161 | *request_snapshot, row_group_plan)); |
474 | 161 | } |
475 | 161 | _state->scan_plan = row_group_plan; |
476 | 161 | _state->scheduler.set_page_skip_profile(_parquet_profile.page_skip_profile()); |
477 | 161 | _state->scheduler.set_global_rowid_context(_global_rowid_context); |
478 | 161 | _state->scheduler.set_scan_profile(_parquet_profile.scan_profile()); |
479 | 161 | _state->scheduler.set_plan(std::move(row_group_plan)); |
480 | 161 | _eof = _state->scheduler.empty(); |
481 | 161 | return Status::OK(); |
482 | 161 | } |
483 | | |
484 | 236 | Status ParquetReader::get_block(Block* file_block, size_t* rows, bool* eof) { |
485 | 236 | if (_state == nullptr || _state->file_context.file_reader == nullptr || |
486 | 236 | _state->file_context.schema == nullptr) { |
487 | 0 | return Status::Uninitialized("ParquetReader is not open"); |
488 | 0 | } |
489 | 236 | *rows = 0; |
490 | 236 | if (_io_ctx != nullptr && _io_ctx->should_stop) { |
491 | 0 | *eof = true; |
492 | 0 | return Status::OK(); |
493 | 0 | } |
494 | 236 | if (_eof) { |
495 | 2 | *eof = true; |
496 | 2 | return Status::OK(); |
497 | 2 | } |
498 | 234 | auto request_snapshot = _request; |
499 | 234 | if (request_snapshot == nullptr) { |
500 | 0 | return Status::Cancelled("ParquetReader is closed"); |
501 | 0 | } |
502 | | |
503 | 234 | const auto predicate_filtered_rows_before = _state->scheduler.predicate_filtered_rows(); |
504 | 234 | const auto raw_rows_read_before = _state->scheduler.raw_rows_read(); |
505 | 234 | Status st = _state->scheduler.read_next_batch(_state->file_context, _state->file_schema, |
506 | 234 | *request_snapshot, file_block, rows, eof); |
507 | 234 | if (!st.ok()) { |
508 | 0 | if (_io_ctx != nullptr && _io_ctx->should_stop) { |
509 | 0 | *rows = 0; |
510 | 0 | *eof = true; |
511 | 0 | return Status::OK(); |
512 | 0 | } |
513 | 0 | return st; |
514 | 0 | } |
515 | 234 | _sync_page_cache_profile(); |
516 | 234 | if (_io_ctx != nullptr) { |
517 | 106 | _io_ctx->predicate_filtered_rows += |
518 | 106 | _state->scheduler.predicate_filtered_rows() - predicate_filtered_rows_before; |
519 | 106 | } |
520 | 234 | const auto raw_rows_read = _state->scheduler.raw_rows_read(); |
521 | 234 | DORIS_CHECK(raw_rows_read >= raw_rows_read_before); |
522 | 234 | _record_scan_rows(raw_rows_read - raw_rows_read_before); |
523 | 234 | _eof = *eof; |
524 | 234 | return Status::OK(); |
525 | 234 | } |
526 | | |
527 | 24 | bool ParquetReader::_should_stop() const { |
528 | 24 | return _io_ctx != nullptr && _io_ctx->should_stop; |
529 | 24 | } |
530 | | |
531 | 14 | Status ParquetReader::_stop_status_if_requested(const Status& status) const { |
532 | 14 | if (!status.ok() && _should_stop()) { |
533 | 0 | return Status::EndOfFile("stop"); |
534 | 0 | } |
535 | 14 | return status; |
536 | 14 | } |
537 | | |
538 | 338 | void ParquetReader::_sync_page_cache_profile() { |
539 | 338 | if (_profile == nullptr || _state == nullptr) { |
540 | 151 | return; |
541 | 151 | } |
542 | 187 | const auto stats = _state->file_context.page_cache_stats(); |
543 | 187 | COUNTER_UPDATE(_parquet_profile.page_read_counter, |
544 | 187 | stats.read_count - _reported_page_cache_stats.read_count); |
545 | 187 | COUNTER_UPDATE(_parquet_profile.page_cache_write_counter, |
546 | 187 | stats.write_count - _reported_page_cache_stats.write_count); |
547 | 187 | COUNTER_UPDATE( |
548 | 187 | _parquet_profile.page_cache_compressed_write_counter, |
549 | 187 | stats.compressed_write_count - _reported_page_cache_stats.compressed_write_count); |
550 | 187 | COUNTER_UPDATE(_parquet_profile.page_cache_hit_counter, |
551 | 187 | stats.hit_count - _reported_page_cache_stats.hit_count); |
552 | 187 | COUNTER_UPDATE(_parquet_profile.page_cache_missing_counter, |
553 | 187 | stats.miss_count - _reported_page_cache_stats.miss_count); |
554 | 187 | COUNTER_UPDATE(_parquet_profile.page_cache_compressed_hit_counter, |
555 | 187 | stats.compressed_hit_count - _reported_page_cache_stats.compressed_hit_count); |
556 | 187 | _reported_page_cache_stats = stats; |
557 | 187 | } |
558 | | |
559 | 2 | void ParquetReader::set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) { |
560 | 2 | if (_state == nullptr) { |
561 | 0 | return; |
562 | 0 | } |
563 | 2 | _state->scheduler.set_condition_cache_context(std::move(ctx)); |
564 | 2 | if (_io_ctx != nullptr) { |
565 | | // Condition-cache HIT filters row ranges before batch reading, so skipped rows never belong |
566 | | // to a later get_block() batch. Report the plan-level skipped rows at the same point where |
567 | | // the scan plan is rewritten. |
568 | 1 | _io_ctx->condition_cache_filtered_rows += _state->scheduler.condition_cache_filtered_rows(); |
569 | 1 | } |
570 | 2 | } |
571 | | |
572 | 0 | int64_t ParquetReader::get_total_rows() const { |
573 | 0 | if (_state == nullptr) { |
574 | 0 | return 0; |
575 | 0 | } |
576 | 0 | int64_t rows = 0; |
577 | 0 | for (const auto& row_group_plan : _state->scan_plan.row_groups) { |
578 | 0 | rows += row_group_plan.row_group_rows; |
579 | 0 | } |
580 | 0 | return rows; |
581 | 0 | } |
582 | | |
583 | | Status ParquetReader::get_aggregate_result(const format::FileAggregateRequest& request, |
584 | 24 | format::FileAggregateResult* result) { |
585 | 24 | DORIS_CHECK(result != nullptr); |
586 | 24 | if (_state == nullptr || _state->file_context.metadata == nullptr || |
587 | 24 | _state->file_context.schema == nullptr) { |
588 | 0 | return Status::Uninitialized("ParquetReader is not open"); |
589 | 0 | } |
590 | 24 | if (_should_stop()) { |
591 | 1 | return Status::EndOfFile("stop"); |
592 | 1 | } |
593 | 23 | result->count = 0; |
594 | 23 | result->columns.clear(); |
595 | 23 | if (request.agg_type != TPushAggOp::type::COUNT && |
596 | 23 | request.agg_type != TPushAggOp::type::MINMAX) { |
597 | 1 | return Status::NotSupported("Unsupported parquet aggregate pushdown type {}", |
598 | 1 | request.agg_type); |
599 | 1 | } |
600 | | |
601 | | // Aggregate row count in all selected row groups. For MIN/MAX aggregate, this is used to determine whether there is no row group selected. |
602 | 37 | for (const auto& row_group_plan : _state->scan_plan.row_groups) { |
603 | 37 | auto row_group_metadata = |
604 | 37 | _state->file_context.metadata->RowGroup(row_group_plan.row_group_id); |
605 | 37 | DORIS_CHECK(row_group_metadata != nullptr); |
606 | 37 | result->count += row_group_metadata->num_rows(); |
607 | 37 | } |
608 | 22 | if (request.agg_type == TPushAggOp::type::COUNT) { |
609 | 10 | if (request.columns.empty()) { |
610 | 4 | return Status::OK(); |
611 | 4 | } |
612 | 6 | if (request.columns.size() != 1) { |
613 | 0 | return Status::NotSupported("Parquet COUNT pushdown only supports one count column"); |
614 | 0 | } |
615 | 6 | const auto& count_projection = request.columns[0].projection; |
616 | 6 | const auto& root_schema = projected_root_schema(_state->file_schema, count_projection); |
617 | 6 | result->count = 0; |
618 | 7 | for (const auto& row_group_plan : _state->scan_plan.row_groups) { |
619 | 7 | std::shared_ptr<::parquet::RowGroupReader> row_group; |
620 | 7 | try { |
621 | 7 | row_group = _state->file_context.file_reader->RowGroup(row_group_plan.row_group_id); |
622 | 7 | } catch (const ::parquet::ParquetException& e) { |
623 | 0 | if (_should_stop()) { |
624 | 0 | return Status::EndOfFile("stop"); |
625 | 0 | } |
626 | 0 | return Status::Corruption("Failed to open parquet row group {}: {}", |
627 | 0 | row_group_plan.row_group_id, e.what()); |
628 | 0 | } catch (const std::exception& e) { |
629 | 0 | if (_should_stop()) { |
630 | 0 | return Status::EndOfFile("stop"); |
631 | 0 | } |
632 | 0 | return Status::InternalError("Failed to open parquet row group {}: {}", |
633 | 0 | row_group_plan.row_group_id, e.what()); |
634 | 0 | } |
635 | | |
636 | 7 | ParquetColumnReaderFactory column_reader_factory( |
637 | 7 | row_group, _state->file_context.schema->num_columns(), |
638 | 7 | &row_group_plan.page_skip_plans, _parquet_profile.page_skip_profile(), |
639 | 7 | _state->timezone, _state->enable_strict_mode, |
640 | 7 | _parquet_profile.scan_profile().column_reader_profile); |
641 | 7 | std::unique_ptr<ParquetColumnReader> shape_reader; |
642 | 7 | RETURN_IF_ERROR(column_reader_factory.create_count_shape_reader( |
643 | 7 | root_schema, &count_projection, &shape_reader)); |
644 | 7 | DORIS_CHECK(shape_reader != nullptr); |
645 | | |
646 | 7 | int64_t row_group_cursor = 0; |
647 | 7 | for (const auto& selected_range : row_group_plan.selected_ranges) { |
648 | 7 | DORIS_CHECK(selected_range.start >= row_group_cursor); |
649 | 7 | RETURN_IF_ERROR(_stop_status_if_requested( |
650 | 7 | shape_reader->skip(selected_range.start - row_group_cursor))); |
651 | 7 | row_group_cursor = selected_range.start; |
652 | | |
653 | 7 | int64_t range_rows_read = 0; |
654 | 14 | while (range_rows_read < selected_range.length) { |
655 | 7 | const int64_t batch_rows = |
656 | 7 | std::min<int64_t>(_batch_size, selected_range.length - range_rows_read); |
657 | | // COUNT(col) only needs the top-level NULL state. The shape reader loads |
658 | | // def/rep levels from one representative leaf and does not build value_indices |
659 | | // or values_column. MAP chooses the key leaf; ARRAY/STRUCT may choose a string |
660 | | // leaf, but the levels-only protocol still avoids Doris-side string |
661 | | // materialization for that leaf. |
662 | 7 | RETURN_IF_ERROR(_stop_status_if_requested( |
663 | 7 | shape_reader->load_nested_levels_batch(batch_rows))); |
664 | 7 | _record_scan_rows(batch_rows); |
665 | 7 | result->count += |
666 | 7 | count_loaded_non_null_values(root_schema, *shape_reader, batch_rows); |
667 | 7 | range_rows_read += batch_rows; |
668 | 7 | row_group_cursor += batch_rows; |
669 | 7 | } |
670 | 7 | } |
671 | 7 | } |
672 | 6 | return Status::OK(); |
673 | 6 | } |
674 | | |
675 | 12 | result->columns.resize(request.columns.size()); |
676 | 20 | for (size_t request_column_idx = 0; request_column_idx < request.columns.size(); |
677 | 14 | ++request_column_idx) { |
678 | 14 | const auto file_column_id = request.columns[request_column_idx].projection.local_id(); |
679 | 14 | if (file_column_id < 0 || |
680 | 14 | file_column_id >= static_cast<int32_t>(_state->file_schema.size())) { |
681 | 1 | return Status::InvalidArgument("Invalid parquet aggregate column id {}", |
682 | 1 | file_column_id); |
683 | 1 | } |
684 | 13 | const auto& column_schema = _state->file_schema[file_column_id]; |
685 | 13 | DORIS_CHECK(column_schema != nullptr); |
686 | 13 | const ParquetColumnSchema* leaf_schema = nullptr; |
687 | 13 | RETURN_IF_ERROR(find_projected_minmax_leaf( |
688 | 13 | *column_schema, request.columns[request_column_idx].projection, &leaf_schema)); |
689 | 11 | DORIS_CHECK(leaf_schema != nullptr); |
690 | 11 | RETURN_IF_ERROR(validate_minmax_aggregate_statistics(*leaf_schema)); |
691 | | |
692 | 9 | auto& aggregate_column = result->columns[request_column_idx]; |
693 | 9 | aggregate_column.projection = request.columns[request_column_idx].projection; |
694 | 17 | for (const auto& row_group_plan : _state->scan_plan.row_groups) { |
695 | 17 | auto row_group_metadata = |
696 | 17 | _state->file_context.metadata->RowGroup(row_group_plan.row_group_id); |
697 | 17 | DORIS_CHECK(row_group_metadata != nullptr); |
698 | 17 | auto column_chunk = row_group_metadata->ColumnChunk(leaf_schema->leaf_column_id); |
699 | 17 | DORIS_CHECK(column_chunk != nullptr); |
700 | 17 | const auto statistics = ParquetStatisticsUtils::TransformColumnStatistics( |
701 | 17 | *leaf_schema, column_chunk->statistics(), _state->timezone); |
702 | 17 | if (!statistics.has_min_max) { |
703 | 1 | return Status::NotSupported("Missing parquet min/max statistics for column {}", |
704 | 1 | leaf_schema->name); |
705 | 1 | } |
706 | 16 | if (!aggregate_column.has_min || statistics.min_value < aggregate_column.min_value) { |
707 | 8 | aggregate_column.min_value = statistics.min_value; |
708 | 8 | aggregate_column.has_min = true; |
709 | 8 | } |
710 | 16 | if (!aggregate_column.has_max || aggregate_column.max_value < statistics.max_value) { |
711 | 16 | aggregate_column.max_value = statistics.max_value; |
712 | 16 | aggregate_column.has_max = true; |
713 | 16 | } |
714 | 16 | } |
715 | 8 | if (!aggregate_column.has_min || !aggregate_column.has_max) { |
716 | 0 | return Status::NotSupported("No parquet row group selected for min/max pushdown"); |
717 | 0 | } |
718 | 8 | } |
719 | 6 | return Status::OK(); |
720 | 12 | } |
721 | | |
722 | 104 | Status ParquetReader::close() { |
723 | 104 | if (_state != nullptr) { |
724 | 104 | _sync_page_cache_profile(); |
725 | 104 | RETURN_IF_ERROR(_state->file_context.close()); |
726 | 104 | } |
727 | 104 | return FileReader::close(); |
728 | 104 | } |
729 | | |
730 | 165 | void ParquetReader::_init_profile() { |
731 | 165 | _parquet_profile.init(_profile); |
732 | 165 | } |
733 | | |
734 | | } // namespace doris::format::parquet |