be/src/format/table/paimon_cpp_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_cpp_reader.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <mutex> |
22 | | #include <utility> |
23 | | |
24 | | #include "arrow/c/bridge.h" |
25 | | #include "arrow/record_batch.h" |
26 | | #include "arrow/result.h" |
27 | | #include "core/block/block.h" |
28 | | #include "core/block/column_with_type_and_name.h" |
29 | | #include "format/table/paimon_doris_file_system.h" |
30 | | #include "paimon/defs.h" |
31 | | #include "paimon/memory/memory_pool.h" |
32 | | #include "paimon/read_context.h" |
33 | | #include "paimon/table/source/table_read.h" |
34 | | #include "runtime/descriptors.h" |
35 | | #include "runtime/runtime_state.h" |
36 | | #include "util/url_coding.h" |
37 | | |
38 | | namespace doris { |
39 | | #include "common/compile_check_begin.h" |
40 | | |
41 | | namespace { |
42 | | constexpr const char* VALUE_KIND_FIELD = "_VALUE_KIND"; |
43 | | |
44 | | } // namespace |
45 | | |
46 | | PaimonCppReader::PaimonCppReader(const std::vector<SlotDescriptor*>& file_slot_descs, |
47 | | RuntimeState* state, RuntimeProfile* profile, |
48 | | const TFileRangeDesc& range, |
49 | | const TFileScanRangeParams* range_params) |
50 | 2 | : _file_slot_descs(file_slot_descs), |
51 | 2 | _state(state), |
52 | 2 | _profile(profile), |
53 | 2 | _range(range), |
54 | 2 | _range_params(range_params) { |
55 | 2 | TimezoneUtils::find_cctz_time_zone(TimezoneUtils::default_time_zone, _ctzz); |
56 | 2 | if (range.__isset.table_format_params && |
57 | 2 | range.table_format_params.__isset.table_level_row_count) { |
58 | 2 | _remaining_table_level_row_count = range.table_format_params.table_level_row_count; |
59 | 2 | } else { |
60 | 0 | _remaining_table_level_row_count = -1; |
61 | 0 | } |
62 | 2 | } |
63 | | |
64 | 2 | PaimonCppReader::~PaimonCppReader() = default; |
65 | | |
66 | 2 | Status PaimonCppReader::init_reader() { |
67 | 2 | if (_push_down_agg_type == TPushAggOp::type::COUNT && _remaining_table_level_row_count >= 0) { |
68 | 1 | return Status::OK(); |
69 | 1 | } |
70 | 1 | return _init_paimon_reader(); |
71 | 2 | } |
72 | | |
73 | 3 | Status PaimonCppReader::get_next_block(Block* block, size_t* read_rows, bool* eof) { |
74 | 3 | if (_push_down_agg_type == TPushAggOp::type::COUNT && _remaining_table_level_row_count >= 0) { |
75 | 3 | auto rows = std::min(_remaining_table_level_row_count, |
76 | 3 | (int64_t)_state->query_options().batch_size); |
77 | 3 | _remaining_table_level_row_count -= rows; |
78 | 3 | auto mutate_columns = block->mutate_columns(); |
79 | 3 | for (auto& col : mutate_columns) { |
80 | 0 | col->resize(rows); |
81 | 0 | } |
82 | 3 | block->set_columns(std::move(mutate_columns)); |
83 | 3 | *read_rows = rows; |
84 | 3 | *eof = false; |
85 | 3 | if (_remaining_table_level_row_count == 0) { |
86 | 2 | *eof = true; |
87 | 2 | } |
88 | 3 | return Status::OK(); |
89 | 3 | } |
90 | | |
91 | 0 | if (!_batch_reader) { |
92 | 0 | return Status::InternalError("paimon-cpp reader is not initialized"); |
93 | 0 | } |
94 | | |
95 | 0 | if (_col_name_to_block_idx.empty()) { |
96 | 0 | _col_name_to_block_idx = block->get_name_to_pos_map(); |
97 | 0 | } |
98 | |
|
99 | 0 | auto batch_result = _batch_reader->NextBatch(); |
100 | 0 | if (!batch_result.ok()) { |
101 | 0 | return Status::InternalError("paimon-cpp read batch failed: {}", |
102 | 0 | batch_result.status().ToString()); |
103 | 0 | } |
104 | 0 | auto batch = std::move(batch_result).value(); |
105 | 0 | if (paimon::BatchReader::IsEofBatch(batch)) { |
106 | 0 | *read_rows = 0; |
107 | 0 | *eof = true; |
108 | 0 | return Status::OK(); |
109 | 0 | } |
110 | | |
111 | 0 | arrow::Result<std::shared_ptr<arrow::RecordBatch>> import_result = |
112 | 0 | arrow::ImportRecordBatch(batch.first.get(), batch.second.get()); |
113 | 0 | if (!import_result.ok()) { |
114 | 0 | return Status::InternalError("failed to import paimon-cpp arrow batch: {}", |
115 | 0 | import_result.status().message()); |
116 | 0 | } |
117 | | |
118 | 0 | auto record_batch = std::move(import_result).ValueUnsafe(); |
119 | 0 | const auto num_rows = static_cast<size_t>(record_batch->num_rows()); |
120 | 0 | const auto num_columns = record_batch->num_columns(); |
121 | 0 | for (int c = 0; c < num_columns; ++c) { |
122 | 0 | const auto& field = record_batch->schema()->field(c); |
123 | 0 | if (field->name() == VALUE_KIND_FIELD) { |
124 | 0 | continue; |
125 | 0 | } |
126 | | |
127 | 0 | auto it = _col_name_to_block_idx.find(field->name()); |
128 | 0 | if (it == _col_name_to_block_idx.end()) { |
129 | | // Skip columns that are not in the block (e.g., partition columns handled elsewhere) |
130 | 0 | continue; |
131 | 0 | } |
132 | 0 | const ColumnWithTypeAndName& column_with_name = block->get_by_position(it->second); |
133 | 0 | try { |
134 | 0 | RETURN_IF_ERROR(column_with_name.type->get_serde()->read_column_from_arrow( |
135 | 0 | column_with_name.column->assume_mutable_ref(), record_batch->column(c).get(), 0, |
136 | 0 | num_rows, _ctzz)); |
137 | 0 | } catch (Exception& e) { |
138 | 0 | return Status::InternalError("Failed to convert from arrow to block: {}", e.what()); |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | 0 | *read_rows = num_rows; |
143 | 0 | *eof = false; |
144 | 0 | return Status::OK(); |
145 | 0 | } |
146 | | |
147 | | Status PaimonCppReader::get_columns(std::unordered_map<std::string, DataTypePtr>* name_to_type, |
148 | 0 | std::unordered_set<std::string>* missing_cols) { |
149 | 0 | for (const auto& slot : _file_slot_descs) { |
150 | 0 | name_to_type->emplace(slot->col_name(), slot->type()); |
151 | 0 | } |
152 | 0 | return Status::OK(); |
153 | 0 | } |
154 | | |
155 | 0 | Status PaimonCppReader::close() { |
156 | 0 | if (_batch_reader) { |
157 | 0 | _batch_reader->Close(); |
158 | 0 | } |
159 | 0 | return Status::OK(); |
160 | 0 | } |
161 | | |
162 | 1 | Status PaimonCppReader::_init_paimon_reader() { |
163 | 1 | register_paimon_doris_file_system(); |
164 | 1 | RETURN_IF_ERROR(_decode_split(&_split)); |
165 | | |
166 | 0 | auto table_path_opt = _resolve_table_path(); |
167 | 0 | if (!table_path_opt.has_value()) { |
168 | 0 | return Status::InternalError( |
169 | 0 | "paimon-cpp missing paimon_table; cannot resolve paimon table root path"); |
170 | 0 | } |
171 | 0 | auto options = _build_options(); |
172 | 0 | auto read_columns = _build_read_columns(); |
173 | | |
174 | | // Avoid moving strings across module boundaries to prevent allocator mismatches in ASAN builds. |
175 | 0 | std::string table_path = table_path_opt.value(); |
176 | 0 | static std::once_flag options_log_once; |
177 | 0 | std::call_once(options_log_once, [&]() { |
178 | 0 | auto has_key = [&](const char* key) { |
179 | 0 | auto it = options.find(key); |
180 | 0 | return (it != options.end() && !it->second.empty()) ? "set" : "empty"; |
181 | 0 | }; |
182 | 0 | auto value_or = [&](const char* key) { |
183 | 0 | auto it = options.find(key); |
184 | 0 | return it != options.end() ? it->second : std::string("<unset>"); |
185 | 0 | }; |
186 | 0 | LOG(INFO) << "paimon-cpp options summary: table_path=" << table_path |
187 | 0 | << " AWS_ACCESS_KEY=" << has_key("AWS_ACCESS_KEY") |
188 | 0 | << " AWS_SECRET_KEY=" << has_key("AWS_SECRET_KEY") |
189 | 0 | << " AWS_TOKEN=" << has_key("AWS_TOKEN") |
190 | 0 | << " AWS_ENDPOINT=" << value_or("AWS_ENDPOINT") |
191 | 0 | << " AWS_REGION=" << value_or("AWS_REGION") |
192 | 0 | << " use_path_style=" << value_or("use_path_style") |
193 | 0 | << " fs.oss.endpoint=" << value_or("fs.oss.endpoint") |
194 | 0 | << " fs.s3a.endpoint=" << value_or("fs.s3a.endpoint"); |
195 | 0 | }); |
196 | 0 | paimon::ReadContextBuilder builder(table_path); |
197 | 0 | if (!read_columns.empty()) { |
198 | 0 | builder.SetReadSchema(read_columns); |
199 | 0 | } |
200 | 0 | if (!options.empty()) { |
201 | 0 | builder.SetOptions(options); |
202 | 0 | } |
203 | 0 | if (_predicate) { |
204 | 0 | builder.SetPredicate(_predicate); |
205 | 0 | builder.EnablePredicateFilter(true); |
206 | 0 | } |
207 | |
|
208 | 0 | auto context_result = builder.Finish(); |
209 | 0 | if (!context_result.ok()) { |
210 | 0 | return Status::InternalError("paimon-cpp build read context failed: {}", |
211 | 0 | context_result.status().ToString()); |
212 | 0 | } |
213 | 0 | auto context = std::move(context_result).value(); |
214 | |
|
215 | 0 | auto table_read_result = paimon::TableRead::Create(std::move(context)); |
216 | 0 | if (!table_read_result.ok()) { |
217 | 0 | return Status::InternalError("paimon-cpp create table read failed: {}", |
218 | 0 | table_read_result.status().ToString()); |
219 | 0 | } |
220 | 0 | auto table_read = std::move(table_read_result).value(); |
221 | 0 | auto reader_result = table_read->CreateReader(_split); |
222 | 0 | if (!reader_result.ok()) { |
223 | 0 | return Status::InternalError("paimon-cpp create reader failed: {}", |
224 | 0 | reader_result.status().ToString()); |
225 | 0 | } |
226 | 0 | _table_read = std::move(table_read); |
227 | 0 | _batch_reader = std::move(reader_result).value(); |
228 | 0 | return Status::OK(); |
229 | 0 | } |
230 | | |
231 | 1 | Status PaimonCppReader::_decode_split(std::shared_ptr<paimon::Split>* split) { |
232 | 1 | if (!_range.__isset.table_format_params || !_range.table_format_params.__isset.paimon_params || |
233 | 1 | !_range.table_format_params.paimon_params.__isset.paimon_split) { |
234 | 1 | return Status::InternalError("paimon-cpp missing paimon_split in scan range"); |
235 | 1 | } |
236 | 0 | const auto& encoded_split = _range.table_format_params.paimon_params.paimon_split; |
237 | 0 | std::string decoded_split; |
238 | 0 | if (!base64_decode(encoded_split, &decoded_split)) { |
239 | 0 | return Status::InternalError("paimon-cpp base64 decode paimon_split failed"); |
240 | 0 | } |
241 | 0 | auto pool = paimon::GetDefaultPool(); |
242 | 0 | auto split_result = |
243 | 0 | paimon::Split::Deserialize(decoded_split.data(), decoded_split.size(), pool); |
244 | 0 | if (!split_result.ok()) { |
245 | 0 | return Status::InternalError("paimon-cpp deserialize split failed: {}", |
246 | 0 | split_result.status().ToString()); |
247 | 0 | } |
248 | 0 | *split = std::move(split_result).value(); |
249 | 0 | return Status::OK(); |
250 | 0 | } |
251 | | |
252 | 0 | std::optional<std::string> PaimonCppReader::_resolve_table_path() const { |
253 | 0 | if (_range.__isset.table_format_params && _range.table_format_params.__isset.paimon_params && |
254 | 0 | _range.table_format_params.paimon_params.__isset.paimon_table && |
255 | 0 | !_range.table_format_params.paimon_params.paimon_table.empty()) { |
256 | 0 | return _range.table_format_params.paimon_params.paimon_table; |
257 | 0 | } |
258 | 0 | return std::nullopt; |
259 | 0 | } |
260 | | |
261 | 0 | std::vector<std::string> PaimonCppReader::_build_read_columns() const { |
262 | 0 | std::vector<std::string> columns; |
263 | 0 | columns.reserve(_file_slot_descs.size()); |
264 | 0 | for (const auto& slot : _file_slot_descs) { |
265 | 0 | columns.emplace_back(slot->col_name()); |
266 | 0 | } |
267 | 0 | return columns; |
268 | 0 | } |
269 | | |
270 | 0 | std::map<std::string, std::string> PaimonCppReader::_build_options() const { |
271 | 0 | std::map<std::string, std::string> options; |
272 | 0 | if (_range.__isset.table_format_params && _range.table_format_params.__isset.paimon_params && |
273 | 0 | _range.table_format_params.paimon_params.__isset.paimon_options) { |
274 | 0 | options.insert(_range.table_format_params.paimon_params.paimon_options.begin(), |
275 | 0 | _range.table_format_params.paimon_params.paimon_options.end()); |
276 | 0 | } |
277 | |
|
278 | 0 | if (_range_params && _range_params->__isset.properties && !_range_params->properties.empty()) { |
279 | 0 | for (const auto& kv : _range_params->properties) { |
280 | 0 | options[kv.first] = kv.second; |
281 | 0 | } |
282 | 0 | } else if (_range.__isset.table_format_params && |
283 | 0 | _range.table_format_params.__isset.paimon_params && |
284 | 0 | _range.table_format_params.paimon_params.__isset.hadoop_conf) { |
285 | 0 | for (const auto& kv : _range.table_format_params.paimon_params.hadoop_conf) { |
286 | 0 | options[kv.first] = kv.second; |
287 | 0 | } |
288 | 0 | } |
289 | |
|
290 | 0 | auto copy_if_missing = [&](const char* from_key, const char* to_key) { |
291 | 0 | if (options.find(to_key) != options.end()) { |
292 | 0 | return; |
293 | 0 | } |
294 | 0 | auto it = options.find(from_key); |
295 | 0 | if (it != options.end() && !it->second.empty()) { |
296 | 0 | options[to_key] = it->second; |
297 | 0 | } |
298 | 0 | }; |
299 | | |
300 | | // Map common OSS/S3 Hadoop configs to Doris S3 property keys. |
301 | 0 | copy_if_missing("fs.oss.accessKeyId", "AWS_ACCESS_KEY"); |
302 | 0 | copy_if_missing("fs.oss.accessKeySecret", "AWS_SECRET_KEY"); |
303 | 0 | copy_if_missing("fs.oss.sessionToken", "AWS_TOKEN"); |
304 | 0 | copy_if_missing("fs.oss.endpoint", "AWS_ENDPOINT"); |
305 | 0 | copy_if_missing("fs.oss.region", "AWS_REGION"); |
306 | 0 | copy_if_missing("fs.s3a.access.key", "AWS_ACCESS_KEY"); |
307 | 0 | copy_if_missing("fs.s3a.secret.key", "AWS_SECRET_KEY"); |
308 | 0 | copy_if_missing("fs.s3a.session.token", "AWS_TOKEN"); |
309 | 0 | copy_if_missing("fs.s3a.endpoint", "AWS_ENDPOINT"); |
310 | 0 | copy_if_missing("fs.s3a.region", "AWS_REGION"); |
311 | 0 | copy_if_missing("fs.s3a.path.style.access", "use_path_style"); |
312 | | |
313 | | // FE currently does not pass paimon_options in scan ranges. |
314 | | // Backfill file.format/manifest.format from split file_format to avoid |
315 | | // paimon-cpp falling back to default manifest.format=avro. |
316 | 0 | if (_range.__isset.table_format_params && _range.table_format_params.__isset.paimon_params && |
317 | 0 | _range.table_format_params.paimon_params.__isset.file_format && |
318 | 0 | !_range.table_format_params.paimon_params.file_format.empty()) { |
319 | 0 | const auto& split_file_format = _range.table_format_params.paimon_params.file_format; |
320 | 0 | auto file_format_it = options.find(paimon::Options::FILE_FORMAT); |
321 | 0 | if (file_format_it == options.end() || file_format_it->second.empty()) { |
322 | 0 | options[paimon::Options::FILE_FORMAT] = split_file_format; |
323 | 0 | } |
324 | 0 | auto manifest_format_it = options.find(paimon::Options::MANIFEST_FORMAT); |
325 | 0 | if (manifest_format_it == options.end() || manifest_format_it->second.empty()) { |
326 | 0 | options[paimon::Options::MANIFEST_FORMAT] = split_file_format; |
327 | 0 | } |
328 | 0 | } |
329 | |
|
330 | 0 | options[paimon::Options::FILE_SYSTEM] = "doris"; |
331 | 0 | return options; |
332 | 0 | } |
333 | | |
334 | | #include "common/compile_check_end.h" |
335 | | } // namespace doris |