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