Coverage Report

Created: 2026-08-06 18:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/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_v2/table/paimon_reader.h"
19
20
#include <glog/logging.h>
21
22
#include <string>
23
#include <utility>
24
25
#include "core/data_type/data_type_array.h"
26
#include "core/data_type/data_type_factory.hpp"
27
#include "core/data_type/data_type_map.h"
28
#include "core/data_type/data_type_nullable.h"
29
#include "core/data_type/data_type_struct.h"
30
#include "exprs/vexpr_context.h"
31
#include "format/table/deletion_vector_reader.h"
32
#include "format/table/paimon_reader.h"
33
#include "format_v2/column_mapper.h"
34
#include "format_v2/jni/paimon_jni_reader.h"
35
#include "format_v2/table/schema_history_util.h"
36
#include "gen_cpp/PlanNodes_types.h"
37
38
namespace doris::format::paimon {
39
namespace {
40
41
2
DataTypePtr nullable_like_original(const DataTypePtr& original, DataTypePtr nested) {
42
2
    return original != nullptr && original->is_nullable() ? make_nullable(nested) : nested;
43
2
}
44
45
7
DataTypePtr apply_paimon_timestamp_semantics(format::ColumnDefinition* column) {
46
7
    DORIS_CHECK(column != nullptr);
47
7
    DORIS_CHECK(column->type != nullptr);
48
7
    const auto primitive = remove_nullable(column->type)->get_primitive_type();
49
7
    if (column->timestamp_is_adjusted_to_utc.has_value() &&
50
7
        (primitive == TYPE_DATETIMEV2 || primitive == TYPE_TIMESTAMPTZ)) {
51
2
        const auto target =
52
2
                *column->timestamp_is_adjusted_to_utc ? TYPE_TIMESTAMPTZ : TYPE_DATETIMEV2;
53
2
        column->type = DataTypeFactory::instance().create_data_type(
54
2
                target, column->type->is_nullable(), 0, column->type->get_scale());
55
2
        return column->type;
56
2
    }
57
58
5
    std::vector<DataTypePtr> child_types;
59
5
    child_types.reserve(column->children.size());
60
5
    for (auto& child : column->children) {
61
3
        child_types.push_back(apply_paimon_timestamp_semantics(&child));
62
3
    }
63
5
    if (primitive == TYPE_ARRAY && child_types.size() == 1) {
64
1
        column->type = nullable_like_original(column->type,
65
1
                                              std::make_shared<DataTypeArray>(child_types.front()));
66
4
    } else if (primitive == TYPE_MAP && child_types.size() == 2) {
67
1
        column->type = nullable_like_original(
68
1
                column->type, std::make_shared<DataTypeMap>(make_nullable(child_types[0]),
69
1
                                                            make_nullable(child_types[1])));
70
3
    } else if (primitive == TYPE_STRUCT && child_types.size() == column->children.size()) {
71
0
        Strings child_names;
72
0
        child_names.reserve(column->children.size());
73
0
        for (const auto& child : column->children) {
74
0
            child_names.push_back(child.name);
75
0
        }
76
0
        column->type = nullable_like_original(
77
0
                column->type, std::make_shared<DataTypeStruct>(child_types, child_names));
78
0
    }
79
5
    return column->type;
80
7
}
81
82
} // namespace
83
84
10
Status PaimonReader::prepare_split(const format::SplitReadOptions& options) {
85
10
    {
86
        // Derived schema selection is additive to, not nested around, the common base timers.
87
10
        SCOPED_TIMER(_profile.total_timer);
88
10
        SCOPED_TIMER(_profile.prepare_split_timer);
89
10
        _split_schema_id = -1;
90
10
        const auto& paimon_params = options.current_range.table_format_params.paimon_params;
91
10
        if (paimon_params.__isset.schema_id) {
92
6
            _split_schema_id = paimon_params.schema_id;
93
6
        }
94
10
    }
95
10
    RETURN_IF_ERROR(format::TableReader::prepare_split(options));
96
10
    SCOPED_TIMER(_profile.total_timer);
97
10
    SCOPED_TIMER(_profile.prepare_split_timer);
98
10
    if (current_split_pruned()) {
99
0
        return Status::OK();
100
0
    }
101
    // Paimon commits data-file changes by adding and logically deleting files in snapshots.
102
    // Compaction also writes replacement files and commits them in a new snapshot instead of
103
    // modifying an existing Parquet/ORC file in place. Native Paimon data files are therefore
104
    // safe to cache by path and size when the split does not provide mtime. Serialized JNI splits
105
    // do not reach this reader.
106
10
    mark_current_data_file_immutable();
107
10
    return Status::OK();
108
10
}
109
110
11
format::TableColumnMappingMode PaimonReader::mapping_mode() const {
111
11
    return format::can_map_by_history_schema(_scan_params, _split_schema_id)
112
11
                   ? format::TableColumnMappingMode::BY_FIELD_ID
113
11
                   : format::TableColumnMappingMode::BY_NAME;
114
11
}
115
116
5
Status PaimonReader::annotate_file_schema(std::vector<format::ColumnDefinition>* file_schema) {
117
5
    DORIS_CHECK(file_schema != nullptr);
118
5
    if (mapping_mode() != format::TableColumnMappingMode::BY_FIELD_ID) {
119
3
        return Status::OK();
120
3
    }
121
2
    RETURN_IF_ERROR(
122
2
            format::annotate_file_schema_from_history(_scan_params, _split_schema_id, file_schema));
123
2
    if (_format == format::FileFormat::PARQUET) {
124
4
        for (auto& column : *file_schema) {
125
4
            apply_paimon_timestamp_semantics(&column);
126
4
        }
127
2
    }
128
2
    return Status::OK();
129
2
}
130
131
Status PaimonReader::_parse_deletion_vector_file(const TTableFormatFileDesc& t_desc,
132
14
                                                 DeleteFileDesc* desc, bool* has_delete_file) {
133
14
    DORIS_CHECK(desc != nullptr);
134
14
    DORIS_CHECK(has_delete_file != nullptr);
135
14
    *has_delete_file = false;
136
14
    const auto& table_desc = t_desc.paimon_params;
137
14
    if (!table_desc.__isset.deletion_file) {
138
9
        return Status::OK();
139
9
    }
140
5
    const auto& deletion_file = table_desc.deletion_file;
141
5
    size_t bytes_read = 0;
142
5
    RETURN_IF_ERROR(validate_paimon_deletion_vector_descriptor(deletion_file, bytes_read));
143
144
4
    desc->key = build_paimon_deletion_vector_cache_key(deletion_file);
145
4
    desc->path = deletion_file.path;
146
4
    desc->start_offset = deletion_file.offset;
147
4
    desc->size = static_cast<int64_t>(bytes_read);
148
4
    desc->file_size = -1;
149
4
    desc->format = DeleteFileDesc::Format::PAIMON;
150
4
    *has_delete_file = true;
151
4
    return Status::OK();
152
5
}
153
154
5
Status PaimonHybridReader::init(format::TableReadOptions&& options) {
155
5
    return format::TableReader::init(std::move(options));
156
5
}
157
158
7
Status PaimonHybridReader::prepare_split(const format::SplitReadOptions& options) {
159
    // Child initialization uses the scanner profile too; hybrid dispatch must not nest the same
160
    // timer around the first native or JNI child and double-count that initialization.
161
7
    RETURN_IF_ERROR(_ensure_current_split_reader(options));
162
7
    DORIS_CHECK(_current_split_reader != nullptr);
163
7
    if (!_is_jni_split(options.current_range)) {
164
5
        auto native_options = options;
165
        // Legacy FE plans wrap native files in FORMAT_JNI; normalize the child contract so the
166
        // physical reader does not overwrite its recovered Parquet/ORC format with that wrapper.
167
5
        RETURN_IF_ERROR(
168
5
                _to_file_format(options.current_range, &native_options.current_split_format));
169
5
        return _current_split_reader->prepare_split(native_options);
170
5
    }
171
2
    return _current_split_reader->prepare_split(options);
172
7
}
173
174
1
Status PaimonHybridReader::refresh_conjuncts(VExprContextSPtrs conjuncts) {
175
1
    RETURN_IF_ERROR(format::TableReader::refresh_conjuncts(std::move(conjuncts)));
176
1
    if (_current_split_reader == nullptr) {
177
0
        return Status::OK();
178
0
    }
179
1
    VExprContextSPtrs child_conjuncts;
180
1
    RETURN_IF_ERROR(_clone_conjuncts(&child_conjuncts));
181
    // The hybrid wrapper owns no physical reader; forward a clone so the active child, rather than
182
    // only the wrapper snapshot, observes late predicates for the remainder of this split.
183
1
    return _current_split_reader->refresh_conjuncts(std::move(child_conjuncts));
184
1
}
185
186
1
Status PaimonHybridReader::get_block(Block* block, bool* eos) {
187
1
    DORIS_CHECK(_current_split_reader != nullptr);
188
1
    return _current_split_reader->get_block(block, eos);
189
1
}
190
191
0
bool PaimonHybridReader::current_split_pruned() const {
192
0
    DORIS_CHECK(_current_split_reader != nullptr);
193
0
    return _current_split_reader->current_split_pruned();
194
0
}
195
196
1
bool PaimonHybridReader::current_split_uses_metadata_count() const {
197
1
    DORIS_CHECK(_current_split_reader != nullptr);
198
1
    return _current_split_reader->current_split_uses_metadata_count();
199
1
}
200
201
0
Status PaimonHybridReader::abort_split() {
202
0
    DORIS_CHECK(_current_split_reader != nullptr);
203
0
    return _current_split_reader->abort_split();
204
0
}
205
206
2
Status PaimonHybridReader::close() {
207
2
    Status close_status = Status::OK();
208
2
    if (_native_reader != nullptr) {
209
2
        close_status = _native_reader->close();
210
2
    }
211
2
    if (_jni_reader != nullptr) {
212
1
        auto status = _jni_reader->close();
213
1
        if (!status.ok() && close_status.ok()) {
214
0
            close_status = std::move(status);
215
0
        }
216
1
    }
217
2
    _current_split_reader = nullptr;
218
2
    return close_status;
219
2
}
220
221
1
void PaimonHybridReader::set_batch_size(size_t batch_size) {
222
1
    format::TableReader::set_batch_size(batch_size);
223
1
    if (_native_reader != nullptr) {
224
1
        _native_reader->set_batch_size(_batch_size);
225
1
    }
226
1
    if (_jni_reader != nullptr) {
227
1
        _jni_reader->set_batch_size(_batch_size);
228
1
    }
229
1
}
230
231
1
int64_t PaimonHybridReader::condition_cache_hit_count() const {
232
    // Both children survive split switches, so the wrapper must publish their cumulative totals;
233
    // returning only the active child would make FileScannerV2's monotonic delta go backwards.
234
1
    return (_native_reader == nullptr ? 0 : _native_reader->condition_cache_hit_count()) +
235
1
           (_jni_reader == nullptr ? 0 : _jni_reader->condition_cache_hit_count());
236
1
}
237
238
7
Status PaimonHybridReader::_ensure_current_split_reader(const format::SplitReadOptions& options) {
239
7
    if (_is_jni_split(options.current_range)) {
240
2
        DCHECK(options.current_split_format == format::FileFormat::JNI);
241
2
        if (_jni_reader == nullptr) {
242
2
#ifdef BE_TEST
243
2
            if (_test_jni_reader_factory) {
244
1
                _jni_reader = _test_jni_reader_factory();
245
1
            } else {
246
1
                _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
247
1
            }
248
#else
249
            _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
250
#endif
251
2
            RETURN_IF_ERROR(_init_child_reader(_jni_reader.get(), format::FileFormat::JNI));
252
2
        }
253
2
        _current_split_reader = _jni_reader.get();
254
5
    } else {
255
5
        format::FileFormat file_format;
256
5
        RETURN_IF_ERROR(_to_file_format(options.current_range, &file_format));
257
        // Old FE plans encoded a native file as FORMAT_JNI without paimon_split and carried the
258
        // physical format only in paimon_params.file_format.
259
5
        DCHECK(options.current_split_format == file_format ||
260
5
               options.current_split_format == format::FileFormat::JNI);
261
5
        DCHECK(file_format == format::FileFormat::PARQUET ||
262
5
               file_format == format::FileFormat::ORC);
263
5
        if (_native_reader == nullptr) {
264
5
#ifdef BE_TEST
265
5
            if (_test_native_reader_factory) {
266
3
                _native_reader = _test_native_reader_factory();
267
3
            } else {
268
2
                _native_reader = format::paimon::PaimonReader::create_unique();
269
2
            }
270
#else
271
            _native_reader = format::paimon::PaimonReader::create_unique();
272
#endif
273
5
            RETURN_IF_ERROR(_init_child_reader(_native_reader.get(), file_format));
274
5
        }
275
5
        _current_split_reader = _native_reader.get();
276
5
    }
277
7
    return Status::OK();
278
7
}
279
280
Status PaimonHybridReader::_init_child_reader(format::TableReader* reader,
281
7
                                              format::FileFormat file_format) {
282
7
    DORIS_CHECK(reader != nullptr);
283
7
    VExprContextSPtrs conjuncts;
284
7
    RETURN_IF_ERROR(_clone_conjuncts(&conjuncts));
285
7
    RETURN_IF_ERROR(reader->init({
286
7
            .projected_columns = _projected_columns,
287
7
            .conjuncts = std::move(conjuncts),
288
7
            .format = file_format,
289
7
            .scan_params = _scan_params,
290
7
            .io_ctx = _io_ctx,
291
7
            .runtime_state = _runtime_state,
292
7
            .scanner_profile = _scanner_profile,
293
7
            .push_down_agg_type = _push_down_agg_type,
294
7
            .push_down_count_columns = _push_down_count_columns,
295
7
            .condition_cache_digest = _condition_cache_digest,
296
7
    }));
297
    // Zero means no adaptive prediction has been produced yet. Preserve the child's normal
298
    // runtime default until FileScannerV2 supplies the first positive prediction.
299
7
    if (_batch_size > 0) {
300
0
        reader->set_batch_size(_batch_size);
301
0
    }
302
7
    return Status::OK();
303
7
}
304
305
8
Status PaimonHybridReader::_clone_conjuncts(VExprContextSPtrs* conjuncts) const {
306
8
    DORIS_CHECK(conjuncts != nullptr);
307
8
    conjuncts->clear();
308
8
    conjuncts->reserve(_conjuncts.size());
309
8
    for (const auto& conjunct : _conjuncts) {
310
0
        VExprSPtr root;
311
0
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
312
0
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
313
0
    }
314
8
    return Status::OK();
315
8
}
316
317
22
bool PaimonHybridReader::_is_jni_split(const TFileRangeDesc& range) {
318
22
    if (!range.__isset.table_format_params || !range.table_format_params.__isset.paimon_params) {
319
0
        return false;
320
0
    }
321
22
    const auto& params = range.table_format_params.paimon_params;
322
22
    return params.__isset.paimon_split &&
323
22
           (!params.__isset.reader_type || params.reader_type == TPaimonReaderType::PAIMON_JNI);
324
22
}
325
326
Status PaimonHybridReader::_to_file_format(const TFileRangeDesc& range,
327
15
                                           format::FileFormat* file_format) {
328
15
    DORIS_CHECK(file_format != nullptr);
329
15
    auto format_type =
330
15
            range.__isset.format_type ? range.format_type : TFileFormatType::FORMAT_PARQUET;
331
    // JNI splits also carry file_format metadata; only a split without paimon_split can use
332
    // FORMAT_JNI as the legacy encoding of a native file.
333
15
    if (format_type == TFileFormatType::FORMAT_JNI && !_is_jni_split(range) &&
334
15
        range.__isset.table_format_params && range.table_format_params.__isset.paimon_params) {
335
4
        const auto& params = range.table_format_params.paimon_params;
336
4
        if (params.__isset.file_format && params.file_format == "orc") {
337
1
            format_type = TFileFormatType::FORMAT_ORC;
338
3
        } else if (params.__isset.file_format && params.file_format == "parquet") {
339
3
            format_type = TFileFormatType::FORMAT_PARQUET;
340
3
        }
341
4
    }
342
15
    switch (format_type) {
343
12
    case TFileFormatType::FORMAT_PARQUET:
344
12
        *file_format = format::FileFormat::PARQUET;
345
12
        return Status::OK();
346
2
    case TFileFormatType::FORMAT_ORC:
347
2
        *file_format = format::FileFormat::ORC;
348
2
        return Status::OK();
349
1
    default:
350
1
        return Status::NotSupported("Unsupported native Paimon file format {}",
351
1
                                    to_string(format_type));
352
15
    }
353
15
}
354
355
} // namespace doris::format::paimon