Coverage Report

Created: 2026-07-23 12:09

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 "exprs/vexpr_context.h"
26
#include "format/table/deletion_vector_reader.h"
27
#include "format/table/paimon_reader.h"
28
#include "format_v2/column_mapper.h"
29
#include "format_v2/jni/paimon_jni_reader.h"
30
#include "format_v2/table/schema_history_util.h"
31
#include "gen_cpp/PlanNodes_types.h"
32
33
namespace doris::format::paimon {
34
35
9
Status PaimonReader::prepare_split(const format::SplitReadOptions& options) {
36
9
    {
37
        // Derived schema selection is additive to, not nested around, the common base timers.
38
9
        SCOPED_TIMER(_profile.total_timer);
39
9
        SCOPED_TIMER(_profile.prepare_split_timer);
40
9
        _split_schema_id = -1;
41
9
        const auto& paimon_params = options.current_range.table_format_params.paimon_params;
42
9
        if (paimon_params.__isset.schema_id) {
43
5
            _split_schema_id = paimon_params.schema_id;
44
5
        }
45
9
    }
46
9
    RETURN_IF_ERROR(format::TableReader::prepare_split(options));
47
9
    SCOPED_TIMER(_profile.total_timer);
48
9
    SCOPED_TIMER(_profile.prepare_split_timer);
49
9
    if (current_split_pruned()) {
50
0
        return Status::OK();
51
0
    }
52
    // Paimon commits data-file changes by adding and logically deleting files in snapshots.
53
    // Compaction also writes replacement files and commits them in a new snapshot instead of
54
    // modifying an existing Parquet/ORC file in place. Native Paimon data files are therefore
55
    // safe to cache by path and size when the split does not provide mtime. Serialized JNI splits
56
    // do not reach this reader.
57
9
    mark_current_data_file_immutable();
58
9
    return Status::OK();
59
9
}
60
61
10
format::TableColumnMappingMode PaimonReader::mapping_mode() const {
62
10
    return format::can_map_by_history_schema(_scan_params, _split_schema_id)
63
10
                   ? format::TableColumnMappingMode::BY_FIELD_ID
64
10
                   : format::TableColumnMappingMode::BY_NAME;
65
10
}
66
67
4
Status PaimonReader::annotate_file_schema(std::vector<format::ColumnDefinition>* file_schema) {
68
4
    DORIS_CHECK(file_schema != nullptr);
69
4
    if (mapping_mode() != format::TableColumnMappingMode::BY_FIELD_ID) {
70
3
        return Status::OK();
71
3
    }
72
1
    return format::annotate_file_schema_from_history(_scan_params, _split_schema_id, file_schema);
73
4
}
74
75
Status PaimonReader::_parse_deletion_vector_file(const TTableFormatFileDesc& t_desc,
76
13
                                                 DeleteFileDesc* desc, bool* has_delete_file) {
77
13
    DORIS_CHECK(desc != nullptr);
78
13
    DORIS_CHECK(has_delete_file != nullptr);
79
13
    *has_delete_file = false;
80
13
    const auto& table_desc = t_desc.paimon_params;
81
13
    if (!table_desc.__isset.deletion_file) {
82
8
        return Status::OK();
83
8
    }
84
5
    const auto& deletion_file = table_desc.deletion_file;
85
5
    size_t bytes_read = 0;
86
5
    RETURN_IF_ERROR(validate_paimon_deletion_vector_descriptor(deletion_file, bytes_read));
87
88
4
    desc->key = build_paimon_deletion_vector_cache_key(deletion_file);
89
4
    desc->path = deletion_file.path;
90
4
    desc->start_offset = deletion_file.offset;
91
4
    desc->size = static_cast<int64_t>(bytes_read);
92
4
    desc->file_size = -1;
93
4
    desc->format = DeleteFileDesc::Format::PAIMON;
94
4
    *has_delete_file = true;
95
4
    return Status::OK();
96
5
}
97
98
3
Status PaimonHybridReader::init(format::TableReadOptions&& options) {
99
3
    return format::TableReader::init(std::move(options));
100
3
}
101
102
5
Status PaimonHybridReader::prepare_split(const format::SplitReadOptions& options) {
103
    // Child initialization uses the scanner profile too; hybrid dispatch must not nest the same
104
    // timer around the first native or JNI child and double-count that initialization.
105
5
    RETURN_IF_ERROR(_ensure_current_split_reader(options));
106
5
    DORIS_CHECK(_current_split_reader != nullptr);
107
5
    return _current_split_reader->prepare_split(options);
108
5
}
109
110
1
Status PaimonHybridReader::get_block(Block* block, bool* eos) {
111
1
    DORIS_CHECK(_current_split_reader != nullptr);
112
1
    return _current_split_reader->get_block(block, eos);
113
1
}
114
115
0
bool PaimonHybridReader::current_split_pruned() const {
116
0
    DORIS_CHECK(_current_split_reader != nullptr);
117
0
    return _current_split_reader->current_split_pruned();
118
0
}
119
120
1
bool PaimonHybridReader::current_split_uses_metadata_count() const {
121
1
    DORIS_CHECK(_current_split_reader != nullptr);
122
1
    return _current_split_reader->current_split_uses_metadata_count();
123
1
}
124
125
0
Status PaimonHybridReader::abort_split() {
126
0
    DORIS_CHECK(_current_split_reader != nullptr);
127
0
    return _current_split_reader->abort_split();
128
0
}
129
130
2
Status PaimonHybridReader::close() {
131
2
    Status close_status = Status::OK();
132
2
    if (_native_reader != nullptr) {
133
2
        close_status = _native_reader->close();
134
2
    }
135
2
    if (_jni_reader != nullptr) {
136
1
        auto status = _jni_reader->close();
137
1
        if (!status.ok() && close_status.ok()) {
138
0
            close_status = std::move(status);
139
0
        }
140
1
    }
141
2
    _current_split_reader = nullptr;
142
2
    return close_status;
143
2
}
144
145
1
void PaimonHybridReader::set_batch_size(size_t batch_size) {
146
1
    format::TableReader::set_batch_size(batch_size);
147
1
    if (_native_reader != nullptr) {
148
1
        _native_reader->set_batch_size(_batch_size);
149
1
    }
150
1
    if (_jni_reader != nullptr) {
151
1
        _jni_reader->set_batch_size(_batch_size);
152
1
    }
153
1
}
154
155
5
Status PaimonHybridReader::_ensure_current_split_reader(const format::SplitReadOptions& options) {
156
5
    if (_is_jni_split(options.current_range)) {
157
2
        DCHECK(options.current_split_format == format::FileFormat::JNI);
158
2
        if (_jni_reader == nullptr) {
159
2
#ifdef BE_TEST
160
2
            if (_test_jni_reader_factory) {
161
1
                _jni_reader = _test_jni_reader_factory();
162
1
            } else {
163
1
                _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
164
1
            }
165
#else
166
            _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
167
#endif
168
2
            RETURN_IF_ERROR(_init_child_reader(_jni_reader.get(), format::FileFormat::JNI));
169
2
        }
170
2
        _current_split_reader = _jni_reader.get();
171
3
    } else {
172
3
        format::FileFormat file_format;
173
3
        RETURN_IF_ERROR(_to_file_format(options.current_range, &file_format));
174
3
        DCHECK(options.current_split_format == file_format);
175
3
        DCHECK(file_format == format::FileFormat::PARQUET ||
176
3
               file_format == format::FileFormat::ORC);
177
3
        if (_native_reader == nullptr) {
178
3
#ifdef BE_TEST
179
3
            if (_test_native_reader_factory) {
180
1
                _native_reader = _test_native_reader_factory();
181
2
            } else {
182
2
                _native_reader = format::paimon::PaimonReader::create_unique();
183
2
            }
184
#else
185
            _native_reader = format::paimon::PaimonReader::create_unique();
186
#endif
187
3
            RETURN_IF_ERROR(_init_child_reader(_native_reader.get(), file_format));
188
3
        }
189
3
        _current_split_reader = _native_reader.get();
190
3
    }
191
5
    return Status::OK();
192
5
}
193
194
Status PaimonHybridReader::_init_child_reader(format::TableReader* reader,
195
5
                                              format::FileFormat file_format) {
196
5
    DORIS_CHECK(reader != nullptr);
197
5
    VExprContextSPtrs conjuncts;
198
5
    RETURN_IF_ERROR(_clone_conjuncts(&conjuncts));
199
5
    RETURN_IF_ERROR(reader->init({
200
5
            .projected_columns = _projected_columns,
201
5
            .conjuncts = std::move(conjuncts),
202
5
            .format = file_format,
203
5
            .scan_params = _scan_params,
204
5
            .io_ctx = _io_ctx,
205
5
            .runtime_state = _runtime_state,
206
5
            .scanner_profile = _scanner_profile,
207
5
            .push_down_agg_type = _push_down_agg_type,
208
5
            .push_down_count_columns = _push_down_count_columns,
209
5
            .condition_cache_digest = _condition_cache_digest,
210
5
    }));
211
    // Zero means no adaptive prediction has been produced yet. Preserve the child's normal
212
    // runtime default until FileScannerV2 supplies the first positive prediction.
213
5
    if (_batch_size > 0) {
214
0
        reader->set_batch_size(_batch_size);
215
0
    }
216
5
    return Status::OK();
217
5
}
218
219
5
Status PaimonHybridReader::_clone_conjuncts(VExprContextSPtrs* conjuncts) const {
220
5
    DORIS_CHECK(conjuncts != nullptr);
221
5
    conjuncts->clear();
222
5
    conjuncts->reserve(_conjuncts.size());
223
5
    for (const auto& conjunct : _conjuncts) {
224
0
        VExprSPtr root;
225
0
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
226
0
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
227
0
    }
228
5
    return Status::OK();
229
5
}
230
231
8
bool PaimonHybridReader::_is_jni_split(const TFileRangeDesc& range) {
232
8
    return range.__isset.table_format_params && range.table_format_params.__isset.paimon_params &&
233
8
           range.table_format_params.paimon_params.__isset.reader_type &&
234
8
           range.table_format_params.paimon_params.reader_type == TPaimonReaderType::PAIMON_JNI;
235
8
}
236
237
Status PaimonHybridReader::_to_file_format(const TFileRangeDesc& range,
238
6
                                           format::FileFormat* file_format) {
239
6
    DORIS_CHECK(file_format != nullptr);
240
6
    const auto format_type =
241
6
            range.__isset.format_type ? range.format_type : TFileFormatType::FORMAT_PARQUET;
242
6
    switch (format_type) {
243
4
    case TFileFormatType::FORMAT_PARQUET:
244
4
        *file_format = format::FileFormat::PARQUET;
245
4
        return Status::OK();
246
1
    case TFileFormatType::FORMAT_ORC:
247
1
        *file_format = format::FileFormat::ORC;
248
1
        return Status::OK();
249
1
    default:
250
1
        return Status::NotSupported("Unsupported native Paimon file format {}",
251
1
                                    to_string(format_type));
252
6
    }
253
6
}
254
255
} // namespace doris::format::paimon