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