be/src/format_v2/table/iceberg_reader.h
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 | | #pragma once |
19 | | |
20 | | #include <memory> |
21 | | #include <optional> |
22 | | #include <string> |
23 | | #include <unordered_map> |
24 | | #include <vector> |
25 | | |
26 | | #include "common/status.h" |
27 | | #include "core/block/block.h" |
28 | | #include "format/table/iceberg_delete_file_reader_helper.h" |
29 | | #include "format_v2/file_reader.h" |
30 | | #include "format_v2/table/iceberg_schema_utils.h" |
31 | | #include "format_v2/table_reader.h" |
32 | | #include "gen_cpp/PlanNodes_types.h" |
33 | | |
34 | | namespace doris { |
35 | | class Block; |
36 | | struct DeleteFileDesc; |
37 | | namespace io { |
38 | | struct FileDescription; |
39 | | struct FileSystemProperties; |
40 | | } // namespace io |
41 | | } // namespace doris |
42 | | |
43 | | namespace doris::format::iceberg { |
44 | | |
45 | | // Iceberg table-level reader. |
46 | | // It reuses TableReader for split orchestration, dynamic partition pruning and table-block |
47 | | // finalization, while composing a FileReader for physical data-file reads instead of inheriting |
48 | | // from a concrete file-format reader. |
49 | | class IcebergTableReader : public format::TableReader { |
50 | | public: |
51 | 59 | ~IcebergTableReader() override = default; |
52 | 51 | Status init(format::TableReadOptions&& options) override { |
53 | 51 | RETURN_IF_ERROR(format::TableReader::init(std::move(options))); |
54 | 51 | _mapper_options.mode = format::TableColumnMappingMode::BY_FIELD_ID; |
55 | 51 | return Status::OK(); |
56 | 51 | } |
57 | | |
58 | | Status prepare_split(const format::SplitReadOptions& options) override; |
59 | | std::string debug_string() const override; |
60 | 69 | format::TableColumnMappingMode mapping_mode() const override { |
61 | 69 | const bool has_field_ids = supports_iceberg_scan_semantics_v1(_scan_params) |
62 | 69 | ? schema_has_any_field_id(_data_reader.file_schema) |
63 | 69 | : schema_has_all_field_ids(_data_reader.file_schema); |
64 | 69 | if (!_data_reader.file_schema.empty() && has_field_ids) { |
65 | 60 | return format::TableColumnMappingMode::BY_FIELD_ID; |
66 | 60 | } |
67 | 9 | return format::TableColumnMappingMode::BY_NAME; |
68 | 69 | } |
69 | | |
70 | | protected: |
71 | 47 | void configure_mapper_options(format::TableColumnMapperOptions* options) const override { |
72 | 47 | options->allow_idless_complex_wrapper_projection = |
73 | 47 | supports_iceberg_scan_semantics_v1(_scan_params) && _format == FileFormat::PARQUET; |
74 | 47 | } |
75 | | |
76 | | Status materialize_virtual_columns(Block* table_block) override; |
77 | | |
78 | | Status customize_file_scan_request(format::FileScanRequest* file_request) override; |
79 | | |
80 | | bool _supports_aggregate_pushdown(TPushAggOp::type agg_type) const override; |
81 | | |
82 | | Status _parse_deletion_vector_file(const TTableFormatFileDesc& t_desc, DeleteFileDesc* desc, |
83 | | bool* has_delete_file) override; |
84 | | |
85 | | Status _init_delete_predicates(const TTableFormatFileDesc& t_desc); |
86 | | |
87 | | private: |
88 | | struct EqualityDeleteFilter; |
89 | | static constexpr int MIN_SUPPORT_DELETE_FILES_VERSION = 2; |
90 | | static constexpr int POSITION_DELETE = 1; |
91 | | static constexpr int EQUALITY_DELETE = 2; |
92 | | static constexpr int DELETION_VECTOR = 3; |
93 | | |
94 | | struct RowLineageColumns { |
95 | | int64_t first_row_id = -1; |
96 | | int64_t last_updated_sequence_number = -1; |
97 | | }; |
98 | | |
99 | | static constexpr const char* ICEBERG_FILE_PATH = "file_path"; |
100 | | static constexpr const char* ICEBERG_ROW_POS = "pos"; |
101 | | static constexpr size_t ICEBERG_FILE_PATH_BLOCK_POSITION = 0; |
102 | | static constexpr size_t ICEBERG_ROW_POS_BLOCK_POSITION = 1; |
103 | | |
104 | | class PositionDeleteRowsCollector final { |
105 | | public: |
106 | | using PositionDeleteFile = std::unordered_map<std::string, format::DeleteRows>; |
107 | | |
108 | | explicit PositionDeleteRowsCollector(PositionDeleteFile* rows_by_data_file); |
109 | | |
110 | | Status collect(const Block& block, size_t read_rows); |
111 | | |
112 | | private: |
113 | | PositionDeleteFile* _rows_by_data_file = nullptr; |
114 | | }; |
115 | | |
116 | | static std::shared_ptr<io::FileSystemProperties> _delete_file_system_properties( |
117 | | const TFileScanRangeParams& scan_params); |
118 | | |
119 | | static std::unique_ptr<io::FileDescription> _delete_file_description( |
120 | | const TFileRangeDesc& range); |
121 | | |
122 | | std::string _data_file_path() const; |
123 | | |
124 | | // Append row position column to file scan request for position delete handling. |
125 | | Status _append_row_position_output_column(format::FileScanRequest* request); |
126 | | // Append equality delete predicates to file scan request based on the delete files in iceberg |
127 | | // params. DeleteVector and position delete files use the common DeleteRows path in TableReader. |
128 | | Status _append_equality_delete_predicates(format::FileScanRequest* request); |
129 | | const format::ColumnDefinition* _find_equality_delete_data_field( |
130 | | const EqualityDeleteFilter& filter, size_t key_idx) const; |
131 | | std::optional<format::ColumnDefinition> _find_equality_delete_table_field( |
132 | | const EqualityDeleteFilter& filter, size_t key_idx) const; |
133 | | void _append_equality_delete_row_count_carrier(format::FileScanRequest* request); |
134 | | std::string _delete_file_cache_key(const char* prefix, const std::string& path) const; |
135 | | |
136 | | Status _init_equality_delete_predicates( |
137 | | const std::vector<TIcebergDeleteFileDesc>& delete_files); |
138 | | |
139 | | // Read equality/position delete files. |
140 | | Status _create_delete_file_reader(const TIcebergDeleteFileDesc& delete_file, |
141 | | const TFileScanRangeParams& scan_params, |
142 | | IcebergDeleteFileIOContext* delete_io_ctx, |
143 | | std::unique_ptr<format::FileReader>* reader); |
144 | | Status _read_equality_delete_file(const TIcebergDeleteFileDesc& delete_file, |
145 | | const TFileScanRangeParams& scan_params, |
146 | | IcebergDeleteFileIOContext* delete_io_ctx); |
147 | | Status _load_equality_delete_file(const TIcebergDeleteFileDesc& delete_file, |
148 | | const TFileScanRangeParams& scan_params, |
149 | | IcebergDeleteFileIOContext* delete_io_ctx, |
150 | | EqualityDeleteFilter* result); |
151 | | Status _resolve_equality_delete_fields(const TIcebergDeleteFileDesc& delete_file, |
152 | | const std::vector<format::ColumnDefinition>& schema, |
153 | | std::vector<format::ColumnDefinition>* delete_fields, |
154 | | EqualityDeleteFilter* result) const; |
155 | | Status _read_position_delete_file(const TIcebergDeleteFileDesc& delete_file, |
156 | | const TFileScanRangeParams& scan_params, |
157 | | IcebergDeleteFileIOContext* delete_io_ctx, |
158 | | PositionDeleteRowsCollector* collector); |
159 | | |
160 | | // Read position delete files and collect deleted row positions to update DeletePredicate. |
161 | | Status _init_position_delete_rows(const std::vector<TIcebergDeleteFileDesc>& delete_files); |
162 | | |
163 | | // Materialize row lineage virtual columns based on the position delete file. |
164 | | Status _materialize_iceberg_rowid(Block* table_block, size_t column_idx); |
165 | | Status _materialize_row_lineage_row_id(Block* table_block, size_t column_idx); |
166 | | Status _materialize_row_lineage_last_updated_sequence_number(Block* table_block, |
167 | | size_t column_idx); |
168 | | |
169 | | RowLineageColumns _row_lineage_columns; |
170 | | size_t _row_position_block_position = 0; |
171 | | std::optional<TIcebergFileDesc> _iceberg_params; |
172 | | bool _delete_predicates_initialized = false; |
173 | | format::DeleteRows _position_delete_rows_storage; |
174 | | struct EqualityDeleteFilter { |
175 | | std::vector<int> field_ids; |
176 | | // Delete-file names are retained for Iceberg tables imported from formats that did not |
177 | | // persist field ids. In BY_NAME mode they are the fallback binding key. |
178 | | std::vector<std::string> field_names; |
179 | | std::vector<DataTypePtr> key_types; |
180 | | Block delete_block; |
181 | | }; |
182 | | std::vector<EqualityDeleteFilter> _equality_delete_filters; |
183 | | // Scanner-shared cache supplied in SplitReadOptions. Parsed delete files outlive one data-file |
184 | | // split and can be reused by every split referencing the same delete file. |
185 | | ShardedKVCache* _split_cache = nullptr; |
186 | | |
187 | | bool _need_row_lineage_row_id() const; |
188 | | bool _need_iceberg_rowid() const; |
189 | | }; |
190 | | |
191 | | } // namespace doris::format::iceberg |