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