be/src/format/table/paimon_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/table/paimon_reader.h" |
19 | | |
20 | | #include <vector> |
21 | | |
22 | | #include "common/status.h" |
23 | | #include "format/table/deletion_vector_reader.h" |
24 | | #include "runtime/runtime_state.h" |
25 | | |
26 | | namespace doris { |
27 | | |
28 | | // ============================================================================ |
29 | | // PaimonOrcReader |
30 | | // ============================================================================ |
31 | 0 | void PaimonOrcReader::_init_paimon_profile() { |
32 | 0 | static const char* paimon_profile = "PaimonProfile"; |
33 | 0 | ADD_TIMER(get_profile(), paimon_profile); |
34 | 0 | _paimon_profile.num_delete_rows = |
35 | 0 | ADD_CHILD_COUNTER(get_profile(), "NumDeleteRows", TUnit::UNIT, paimon_profile); |
36 | 0 | _paimon_profile.delete_files_read_time = |
37 | 0 | ADD_CHILD_TIMER(get_profile(), "DeleteFileReadTime", paimon_profile); |
38 | 0 | _paimon_profile.parse_deletion_vector_time = |
39 | 0 | ADD_CHILD_TIMER(get_profile(), "ParseDeletionVectorTime", paimon_profile); |
40 | 0 | } |
41 | | |
42 | 0 | Status PaimonOrcReader::on_before_init_reader(ReaderInitContext* ctx) { |
43 | 0 | _column_descs = ctx->column_descs; |
44 | 0 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
45 | 0 | const orc::Type* orc_type_ptr = nullptr; |
46 | 0 | RETURN_IF_ERROR(get_file_type(&orc_type_ptr)); |
47 | | |
48 | 0 | RETURN_IF_ERROR(gen_table_info_node_by_field_id( |
49 | 0 | get_scan_params(), get_scan_range().table_format_params.paimon_params.schema_id, |
50 | 0 | get_tuple_descriptor(), orc_type_ptr)); |
51 | 0 | ctx->table_info_node = table_info_node_ptr; |
52 | |
|
53 | 0 | for (const auto& desc : *ctx->column_descs) { |
54 | 0 | if (desc.category == ColumnCategory::REGULAR || |
55 | 0 | desc.category == ColumnCategory::GENERATED) { |
56 | 0 | ctx->column_names.push_back(desc.name); |
57 | 0 | } |
58 | 0 | } |
59 | 0 | return Status::OK(); |
60 | 0 | } |
61 | | |
62 | 0 | Status PaimonOrcReader::on_after_init_reader(ReaderInitContext* /*ctx*/) { |
63 | 0 | return _init_deletion_vector(); |
64 | 0 | } |
65 | | |
66 | 0 | Status PaimonOrcReader::_init_deletion_vector() { |
67 | 0 | const auto& table_desc = get_scan_range().table_format_params.paimon_params; |
68 | 0 | if (!table_desc.__isset.deletion_file) { |
69 | 0 | return Status::OK(); |
70 | 0 | } |
71 | | |
72 | | // Cannot do count push down if there are delete files |
73 | 0 | if (!get_scan_range().table_format_params.paimon_params.__isset.row_count) { |
74 | 0 | set_push_down_agg_type(TPushAggOp::NONE); |
75 | 0 | } |
76 | 0 | const auto& deletion_file = table_desc.deletion_file; |
77 | |
|
78 | 0 | Status create_status = Status::OK(); |
79 | |
|
80 | 0 | std::string key; |
81 | 0 | key.resize(deletion_file.path.size() + sizeof(deletion_file.offset)); |
82 | 0 | memcpy(key.data(), deletion_file.path.data(), deletion_file.path.size()); |
83 | 0 | memcpy(key.data() + deletion_file.path.size(), &deletion_file.offset, |
84 | 0 | sizeof(deletion_file.offset)); |
85 | |
|
86 | 0 | SCOPED_TIMER(_paimon_profile.delete_files_read_time); |
87 | 0 | using DeleteRows = std::vector<int64_t>; |
88 | 0 | _delete_rows = _kv_cache->get<DeleteRows>(key, [&]() -> DeleteRows* { |
89 | 0 | auto* delete_rows = new DeleteRows; |
90 | |
|
91 | 0 | TFileRangeDesc delete_range; |
92 | 0 | delete_range.__set_fs_name(get_scan_range().fs_name); |
93 | 0 | delete_range.path = deletion_file.path; |
94 | 0 | delete_range.start_offset = deletion_file.offset; |
95 | 0 | delete_range.size = deletion_file.length + 4; |
96 | 0 | delete_range.file_size = -1; |
97 | |
|
98 | 0 | DeletionVectorReader dv_reader(get_state(), get_profile(), get_scan_params(), delete_range, |
99 | 0 | get_io_ctx()); |
100 | 0 | create_status = dv_reader.open(); |
101 | 0 | if (!create_status.ok()) [[unlikely]] { |
102 | 0 | return nullptr; |
103 | 0 | } |
104 | | |
105 | 0 | size_t bytes_read = deletion_file.length + 4; |
106 | 0 | std::vector<char> buffer(bytes_read); |
107 | 0 | create_status = dv_reader.read_at(deletion_file.offset, {buffer.data(), bytes_read}); |
108 | 0 | if (!create_status.ok()) [[unlikely]] { |
109 | 0 | return nullptr; |
110 | 0 | } |
111 | | |
112 | 0 | const char* buf = buffer.data(); |
113 | 0 | uint32_t actual_length; |
114 | 0 | std::memcpy(reinterpret_cast<char*>(&actual_length), buf, 4); |
115 | 0 | std::reverse(reinterpret_cast<char*>(&actual_length), |
116 | 0 | reinterpret_cast<char*>(&actual_length) + 4); |
117 | 0 | buf += 4; |
118 | 0 | if (actual_length != bytes_read - 4) [[unlikely]] { |
119 | 0 | create_status = Status::RuntimeError( |
120 | 0 | "DeletionVector deserialize error: length not match, " |
121 | 0 | "actual length: {}, expect length: {}", |
122 | 0 | actual_length, bytes_read - 4); |
123 | 0 | return nullptr; |
124 | 0 | } |
125 | 0 | uint32_t magic_number; |
126 | 0 | std::memcpy(reinterpret_cast<char*>(&magic_number), buf, 4); |
127 | 0 | std::reverse(reinterpret_cast<char*>(&magic_number), |
128 | 0 | reinterpret_cast<char*>(&magic_number) + 4); |
129 | 0 | buf += 4; |
130 | 0 | const static uint32_t MAGIC_NUMBER = 1581511376; |
131 | 0 | if (magic_number != MAGIC_NUMBER) [[unlikely]] { |
132 | 0 | create_status = Status::RuntimeError( |
133 | 0 | "DeletionVector deserialize error: invalid magic number {}", magic_number); |
134 | 0 | return nullptr; |
135 | 0 | } |
136 | | |
137 | 0 | roaring::Roaring roaring_bitmap; |
138 | 0 | SCOPED_TIMER(_paimon_profile.parse_deletion_vector_time); |
139 | 0 | try { |
140 | 0 | roaring_bitmap = roaring::Roaring::readSafe(buf, bytes_read - 4); |
141 | 0 | } catch (const std::runtime_error& e) { |
142 | 0 | create_status = Status::RuntimeError( |
143 | 0 | "DeletionVector deserialize error: failed to deserialize roaring bitmap, {}", |
144 | 0 | e.what()); |
145 | 0 | return nullptr; |
146 | 0 | } |
147 | 0 | delete_rows->reserve(roaring_bitmap.cardinality()); |
148 | 0 | for (auto it = roaring_bitmap.begin(); it != roaring_bitmap.end(); it++) { |
149 | 0 | delete_rows->push_back(*it); |
150 | 0 | } |
151 | 0 | COUNTER_UPDATE(_paimon_profile.num_delete_rows, delete_rows->size()); |
152 | 0 | return delete_rows; |
153 | 0 | }); |
154 | 0 | RETURN_IF_ERROR(create_status); |
155 | 0 | if (!_delete_rows->empty()) [[likely]] { |
156 | 0 | set_position_delete_rowids(_delete_rows); |
157 | 0 | } |
158 | 0 | return Status::OK(); |
159 | 0 | } |
160 | | |
161 | | // ============================================================================ |
162 | | // PaimonParquetReader |
163 | | // ============================================================================ |
164 | 0 | void PaimonParquetReader::_init_paimon_profile() { |
165 | 0 | static const char* paimon_profile = "PaimonProfile"; |
166 | 0 | ADD_TIMER(get_profile(), paimon_profile); |
167 | 0 | _paimon_profile.num_delete_rows = |
168 | 0 | ADD_CHILD_COUNTER(get_profile(), "NumDeleteRows", TUnit::UNIT, paimon_profile); |
169 | 0 | _paimon_profile.delete_files_read_time = |
170 | 0 | ADD_CHILD_TIMER(get_profile(), "DeleteFileReadTime", paimon_profile); |
171 | 0 | _paimon_profile.parse_deletion_vector_time = |
172 | 0 | ADD_CHILD_TIMER(get_profile(), "ParseDeletionVectorTime", paimon_profile); |
173 | 0 | } |
174 | | |
175 | 0 | Status PaimonParquetReader::on_before_init_reader(ReaderInitContext* ctx) { |
176 | 0 | _column_descs = ctx->column_descs; |
177 | 0 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
178 | 0 | const FieldDescriptor* field_desc = nullptr; |
179 | 0 | RETURN_IF_ERROR(get_file_metadata_schema(&field_desc)); |
180 | 0 | DCHECK(field_desc != nullptr); |
181 | |
|
182 | 0 | RETURN_IF_ERROR(gen_table_info_node_by_field_id( |
183 | 0 | get_scan_params(), get_scan_range().table_format_params.paimon_params.schema_id, |
184 | 0 | get_tuple_descriptor(), *field_desc)); |
185 | 0 | ctx->table_info_node = table_info_node_ptr; |
186 | |
|
187 | 0 | for (const auto& desc : *ctx->column_descs) { |
188 | 0 | if (desc.category == ColumnCategory::REGULAR || |
189 | 0 | desc.category == ColumnCategory::GENERATED) { |
190 | 0 | ctx->column_names.push_back(desc.name); |
191 | 0 | } |
192 | 0 | } |
193 | 0 | return Status::OK(); |
194 | 0 | } |
195 | | |
196 | 0 | Status PaimonParquetReader::on_after_init_reader(ReaderInitContext* /*ctx*/) { |
197 | 0 | return _init_deletion_vector(); |
198 | 0 | } |
199 | | |
200 | 0 | Status PaimonParquetReader::_init_deletion_vector() { |
201 | 0 | const auto& table_desc = get_scan_range().table_format_params.paimon_params; |
202 | 0 | if (!table_desc.__isset.deletion_file) { |
203 | 0 | return Status::OK(); |
204 | 0 | } |
205 | | |
206 | 0 | if (!get_scan_range().table_format_params.paimon_params.__isset.row_count) { |
207 | 0 | set_push_down_agg_type(TPushAggOp::NONE); |
208 | 0 | } |
209 | 0 | const auto& deletion_file = table_desc.deletion_file; |
210 | |
|
211 | 0 | Status create_status = Status::OK(); |
212 | |
|
213 | 0 | std::string key; |
214 | 0 | key.resize(deletion_file.path.size() + sizeof(deletion_file.offset)); |
215 | 0 | memcpy(key.data(), deletion_file.path.data(), deletion_file.path.size()); |
216 | 0 | memcpy(key.data() + deletion_file.path.size(), &deletion_file.offset, |
217 | 0 | sizeof(deletion_file.offset)); |
218 | |
|
219 | 0 | SCOPED_TIMER(_paimon_profile.delete_files_read_time); |
220 | 0 | using DeleteRows = std::vector<int64_t>; |
221 | 0 | _delete_rows = _kv_cache->get<DeleteRows>(key, [&]() -> DeleteRows* { |
222 | 0 | auto* delete_rows = new DeleteRows; |
223 | |
|
224 | 0 | TFileRangeDesc delete_range; |
225 | 0 | delete_range.__set_fs_name(get_scan_range().fs_name); |
226 | 0 | delete_range.path = deletion_file.path; |
227 | 0 | delete_range.start_offset = deletion_file.offset; |
228 | 0 | delete_range.size = deletion_file.length + 4; |
229 | 0 | delete_range.file_size = -1; |
230 | |
|
231 | 0 | DeletionVectorReader dv_reader(get_state(), get_profile(), get_scan_params(), delete_range, |
232 | 0 | get_io_ctx()); |
233 | 0 | create_status = dv_reader.open(); |
234 | 0 | if (!create_status.ok()) [[unlikely]] { |
235 | 0 | return nullptr; |
236 | 0 | } |
237 | | |
238 | 0 | size_t bytes_read = deletion_file.length + 4; |
239 | 0 | std::vector<char> buffer(bytes_read); |
240 | 0 | create_status = dv_reader.read_at(deletion_file.offset, {buffer.data(), bytes_read}); |
241 | 0 | if (!create_status.ok()) [[unlikely]] { |
242 | 0 | return nullptr; |
243 | 0 | } |
244 | | |
245 | 0 | const char* buf = buffer.data(); |
246 | 0 | uint32_t actual_length; |
247 | 0 | std::memcpy(reinterpret_cast<char*>(&actual_length), buf, 4); |
248 | 0 | std::reverse(reinterpret_cast<char*>(&actual_length), |
249 | 0 | reinterpret_cast<char*>(&actual_length) + 4); |
250 | 0 | buf += 4; |
251 | 0 | if (actual_length != bytes_read - 4) [[unlikely]] { |
252 | 0 | create_status = Status::RuntimeError( |
253 | 0 | "DeletionVector deserialize error: length not match, " |
254 | 0 | "actual length: {}, expect length: {}", |
255 | 0 | actual_length, bytes_read - 4); |
256 | 0 | return nullptr; |
257 | 0 | } |
258 | 0 | uint32_t magic_number; |
259 | 0 | std::memcpy(reinterpret_cast<char*>(&magic_number), buf, 4); |
260 | 0 | std::reverse(reinterpret_cast<char*>(&magic_number), |
261 | 0 | reinterpret_cast<char*>(&magic_number) + 4); |
262 | 0 | buf += 4; |
263 | 0 | const static uint32_t MAGIC_NUMBER = 1581511376; |
264 | 0 | if (magic_number != MAGIC_NUMBER) [[unlikely]] { |
265 | 0 | create_status = Status::RuntimeError( |
266 | 0 | "DeletionVector deserialize error: invalid magic number {}", magic_number); |
267 | 0 | return nullptr; |
268 | 0 | } |
269 | | |
270 | 0 | roaring::Roaring roaring_bitmap; |
271 | 0 | SCOPED_TIMER(_paimon_profile.parse_deletion_vector_time); |
272 | 0 | try { |
273 | 0 | roaring_bitmap = roaring::Roaring::readSafe(buf, bytes_read - 4); |
274 | 0 | } catch (const std::runtime_error& e) { |
275 | 0 | create_status = Status::RuntimeError( |
276 | 0 | "DeletionVector deserialize error: failed to deserialize roaring bitmap, {}", |
277 | 0 | e.what()); |
278 | 0 | return nullptr; |
279 | 0 | } |
280 | 0 | delete_rows->reserve(roaring_bitmap.cardinality()); |
281 | 0 | for (auto it = roaring_bitmap.begin(); it != roaring_bitmap.end(); it++) { |
282 | 0 | delete_rows->push_back(*it); |
283 | 0 | } |
284 | 0 | COUNTER_UPDATE(_paimon_profile.num_delete_rows, delete_rows->size()); |
285 | 0 | return delete_rows; |
286 | 0 | }); |
287 | 0 | RETURN_IF_ERROR(create_status); |
288 | 0 | if (!_delete_rows->empty()) [[likely]] { |
289 | 0 | ParquetReader::set_delete_rows(_delete_rows); |
290 | 0 | } |
291 | 0 | return Status::OK(); |
292 | 0 | } |
293 | | |
294 | | } // namespace doris |