Coverage Report

Created: 2026-07-26 22:28

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
4
Status PaimonHybridReader::init(format::TableReadOptions&& options) {
99
4
    return format::TableReader::init(std::move(options));
100
4
}
101
102
6
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
6
    RETURN_IF_ERROR(_ensure_current_split_reader(options));
106
6
    DORIS_CHECK(_current_split_reader != nullptr);
107
6
    if (!_is_jni_split(options.current_range)) {
108
4
        auto native_options = options;
109
        // Legacy FE plans wrap native files in FORMAT_JNI; normalize the child contract so the
110
        // physical reader does not overwrite its recovered Parquet/ORC format with that wrapper.
111
4
        RETURN_IF_ERROR(
112
4
                _to_file_format(options.current_range, &native_options.current_split_format));
113
4
        return _current_split_reader->prepare_split(native_options);
114
4
    }
115
2
    return _current_split_reader->prepare_split(options);
116
6
}
117
118
1
Status PaimonHybridReader::get_block(Block* block, bool* eos) {
119
1
    DORIS_CHECK(_current_split_reader != nullptr);
120
1
    return _current_split_reader->get_block(block, eos);
121
1
}
122
123
0
bool PaimonHybridReader::current_split_pruned() const {
124
0
    DORIS_CHECK(_current_split_reader != nullptr);
125
0
    return _current_split_reader->current_split_pruned();
126
0
}
127
128
1
bool PaimonHybridReader::current_split_uses_metadata_count() const {
129
1
    DORIS_CHECK(_current_split_reader != nullptr);
130
1
    return _current_split_reader->current_split_uses_metadata_count();
131
1
}
132
133
0
Status PaimonHybridReader::abort_split() {
134
0
    DORIS_CHECK(_current_split_reader != nullptr);
135
0
    return _current_split_reader->abort_split();
136
0
}
137
138
2
Status PaimonHybridReader::close() {
139
2
    Status close_status = Status::OK();
140
2
    if (_native_reader != nullptr) {
141
2
        close_status = _native_reader->close();
142
2
    }
143
2
    if (_jni_reader != nullptr) {
144
1
        auto status = _jni_reader->close();
145
1
        if (!status.ok() && close_status.ok()) {
146
0
            close_status = std::move(status);
147
0
        }
148
1
    }
149
2
    _current_split_reader = nullptr;
150
2
    return close_status;
151
2
}
152
153
1
void PaimonHybridReader::set_batch_size(size_t batch_size) {
154
1
    format::TableReader::set_batch_size(batch_size);
155
1
    if (_native_reader != nullptr) {
156
1
        _native_reader->set_batch_size(_batch_size);
157
1
    }
158
1
    if (_jni_reader != nullptr) {
159
1
        _jni_reader->set_batch_size(_batch_size);
160
1
    }
161
1
}
162
163
1
int64_t PaimonHybridReader::condition_cache_hit_count() const {
164
    // Both children survive split switches, so the wrapper must publish their cumulative totals;
165
    // returning only the active child would make FileScannerV2's monotonic delta go backwards.
166
1
    return (_native_reader == nullptr ? 0 : _native_reader->condition_cache_hit_count()) +
167
1
           (_jni_reader == nullptr ? 0 : _jni_reader->condition_cache_hit_count());
168
1
}
169
170
6
Status PaimonHybridReader::_ensure_current_split_reader(const format::SplitReadOptions& options) {
171
6
    if (_is_jni_split(options.current_range)) {
172
2
        DCHECK(options.current_split_format == format::FileFormat::JNI);
173
2
        if (_jni_reader == nullptr) {
174
2
#ifdef BE_TEST
175
2
            if (_test_jni_reader_factory) {
176
1
                _jni_reader = _test_jni_reader_factory();
177
1
            } else {
178
1
                _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
179
1
            }
180
#else
181
            _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
182
#endif
183
2
            RETURN_IF_ERROR(_init_child_reader(_jni_reader.get(), format::FileFormat::JNI));
184
2
        }
185
2
        _current_split_reader = _jni_reader.get();
186
4
    } else {
187
4
        format::FileFormat file_format;
188
4
        RETURN_IF_ERROR(_to_file_format(options.current_range, &file_format));
189
        // Old FE plans encoded a native file as FORMAT_JNI without paimon_split and carried the
190
        // physical format only in paimon_params.file_format.
191
4
        DCHECK(options.current_split_format == file_format ||
192
4
               options.current_split_format == format::FileFormat::JNI);
193
4
        DCHECK(file_format == format::FileFormat::PARQUET ||
194
4
               file_format == format::FileFormat::ORC);
195
4
        if (_native_reader == nullptr) {
196
4
#ifdef BE_TEST
197
4
            if (_test_native_reader_factory) {
198
2
                _native_reader = _test_native_reader_factory();
199
2
            } else {
200
2
                _native_reader = format::paimon::PaimonReader::create_unique();
201
2
            }
202
#else
203
            _native_reader = format::paimon::PaimonReader::create_unique();
204
#endif
205
4
            RETURN_IF_ERROR(_init_child_reader(_native_reader.get(), file_format));
206
4
        }
207
4
        _current_split_reader = _native_reader.get();
208
4
    }
209
6
    return Status::OK();
210
6
}
211
212
Status PaimonHybridReader::_init_child_reader(format::TableReader* reader,
213
6
                                              format::FileFormat file_format) {
214
6
    DORIS_CHECK(reader != nullptr);
215
6
    VExprContextSPtrs conjuncts;
216
6
    RETURN_IF_ERROR(_clone_conjuncts(&conjuncts));
217
6
    RETURN_IF_ERROR(reader->init({
218
6
            .projected_columns = _projected_columns,
219
6
            .conjuncts = std::move(conjuncts),
220
6
            .format = file_format,
221
6
            .scan_params = _scan_params,
222
6
            .io_ctx = _io_ctx,
223
6
            .runtime_state = _runtime_state,
224
6
            .scanner_profile = _scanner_profile,
225
6
            .push_down_agg_type = _push_down_agg_type,
226
6
            .push_down_count_columns = _push_down_count_columns,
227
6
            .condition_cache_digest = _condition_cache_digest,
228
6
    }));
229
    // Zero means no adaptive prediction has been produced yet. Preserve the child's normal
230
    // runtime default until FileScannerV2 supplies the first positive prediction.
231
6
    if (_batch_size > 0) {
232
0
        reader->set_batch_size(_batch_size);
233
0
    }
234
6
    return Status::OK();
235
6
}
236
237
6
Status PaimonHybridReader::_clone_conjuncts(VExprContextSPtrs* conjuncts) const {
238
6
    DORIS_CHECK(conjuncts != nullptr);
239
6
    conjuncts->clear();
240
6
    conjuncts->reserve(_conjuncts.size());
241
6
    for (const auto& conjunct : _conjuncts) {
242
0
        VExprSPtr root;
243
0
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
244
0
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
245
0
    }
246
6
    return Status::OK();
247
6
}
248
249
20
bool PaimonHybridReader::_is_jni_split(const TFileRangeDesc& range) {
250
20
    if (!range.__isset.table_format_params || !range.table_format_params.__isset.paimon_params) {
251
0
        return false;
252
0
    }
253
20
    const auto& params = range.table_format_params.paimon_params;
254
20
    return params.__isset.paimon_split &&
255
20
           (!params.__isset.reader_type || params.reader_type == TPaimonReaderType::PAIMON_JNI);
256
20
}
257
258
Status PaimonHybridReader::_to_file_format(const TFileRangeDesc& range,
259
13
                                           format::FileFormat* file_format) {
260
13
    DORIS_CHECK(file_format != nullptr);
261
13
    auto format_type =
262
13
            range.__isset.format_type ? range.format_type : TFileFormatType::FORMAT_PARQUET;
263
    // JNI splits also carry file_format metadata; only a split without paimon_split can use
264
    // FORMAT_JNI as the legacy encoding of a native file.
265
13
    if (format_type == TFileFormatType::FORMAT_JNI && !_is_jni_split(range) &&
266
13
        range.__isset.table_format_params && range.table_format_params.__isset.paimon_params) {
267
4
        const auto& params = range.table_format_params.paimon_params;
268
4
        if (params.__isset.file_format && params.file_format == "orc") {
269
1
            format_type = TFileFormatType::FORMAT_ORC;
270
3
        } else if (params.__isset.file_format && params.file_format == "parquet") {
271
3
            format_type = TFileFormatType::FORMAT_PARQUET;
272
3
        }
273
4
    }
274
13
    switch (format_type) {
275
10
    case TFileFormatType::FORMAT_PARQUET:
276
10
        *file_format = format::FileFormat::PARQUET;
277
10
        return Status::OK();
278
2
    case TFileFormatType::FORMAT_ORC:
279
2
        *file_format = format::FileFormat::ORC;
280
2
        return Status::OK();
281
1
    default:
282
1
        return Status::NotSupported("Unsupported native Paimon file format {}",
283
1
                                    to_string(format_type));
284
13
    }
285
13
}
286
287
} // namespace doris::format::paimon