Coverage Report

Created: 2026-07-23 19: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
5
Status PaimonHybridReader::init(format::TableReadOptions&& options) {
99
5
    return format::TableReader::init(std::move(options));
100
5
}
101
102
8
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
8
    RETURN_IF_ERROR(_ensure_current_split_reader(options));
106
8
    DORIS_CHECK(_current_split_reader != nullptr);
107
8
    return _current_split_reader->prepare_split(options);
108
8
}
109
110
4
Status PaimonHybridReader::get_block(Block* block, bool* eos) {
111
4
    DORIS_CHECK(_current_split_reader != nullptr);
112
4
    return _current_split_reader->get_block(block, eos);
113
4
}
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
1
Status PaimonHybridReader::append_conjuncts(const VExprContextSPtrs& conjuncts) {
156
    // The wrapper snapshot initializes future children, while every existing child needs the same
157
    // late RF immediately so active and later reused splits keep identical predicate ownership.
158
1
    const size_t owned_count =
159
1
            _appended_table_reader_owned_conjunct_count.value_or(conjuncts.size());
160
1
    RETURN_IF_ERROR(format::TableReader::append_conjuncts(conjuncts));
161
1
    if (_native_reader != nullptr) {
162
1
        RETURN_IF_ERROR(_native_reader->append_conjuncts_with_ownership(conjuncts, owned_count));
163
1
    }
164
1
    if (_jni_reader != nullptr) {
165
1
        RETURN_IF_ERROR(_jni_reader->append_conjuncts_with_ownership(conjuncts, owned_count));
166
1
    }
167
1
    return Status::OK();
168
1
}
169
170
1
const format::MaterializedBlockStats& PaimonHybridReader::last_materialized_block_stats() const {
171
    // FileScannerV2 budgets cooperative work from the child that actually materialized the block.
172
1
    return _current_split_reader != nullptr ? _current_split_reader->last_materialized_block_stats()
173
1
                                            : format::TableReader::last_materialized_block_stats();
174
1
}
175
176
1
int64_t PaimonHybridReader::condition_cache_hit_count() const {
177
    // Both children survive split switches, so the wrapper must publish their cumulative totals;
178
    // returning only the active child would make FileScannerV2's monotonic delta go backwards.
179
1
    return (_native_reader == nullptr ? 0 : _native_reader->condition_cache_hit_count()) +
180
1
           (_jni_reader == nullptr ? 0 : _jni_reader->condition_cache_hit_count());
181
1
}
182
183
8
Status PaimonHybridReader::_ensure_current_split_reader(const format::SplitReadOptions& options) {
184
8
    if (_is_jni_split(options.current_range)) {
185
3
        DCHECK(options.current_split_format == format::FileFormat::JNI);
186
3
        if (_jni_reader == nullptr) {
187
3
#ifdef BE_TEST
188
3
            if (_test_jni_reader_factory) {
189
2
                _jni_reader = _test_jni_reader_factory();
190
2
            } else {
191
1
                _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
192
1
            }
193
#else
194
            _jni_reader = std::make_unique<format::paimon::PaimonJniReader>();
195
#endif
196
3
            RETURN_IF_ERROR(_init_child_reader(_jni_reader.get(), format::FileFormat::JNI));
197
3
        }
198
3
        _current_split_reader = _jni_reader.get();
199
5
    } else {
200
5
        format::FileFormat file_format;
201
5
        RETURN_IF_ERROR(_to_file_format(options.current_range, &file_format));
202
5
        DCHECK(options.current_split_format == file_format);
203
5
        DCHECK(file_format == format::FileFormat::PARQUET ||
204
5
               file_format == format::FileFormat::ORC);
205
5
        if (_native_reader == nullptr) {
206
4
#ifdef BE_TEST
207
4
            if (_test_native_reader_factory) {
208
2
                _native_reader = _test_native_reader_factory();
209
2
            } else {
210
2
                _native_reader = format::paimon::PaimonReader::create_unique();
211
2
            }
212
#else
213
            _native_reader = format::paimon::PaimonReader::create_unique();
214
#endif
215
4
            RETURN_IF_ERROR(_init_child_reader(_native_reader.get(), file_format));
216
4
        }
217
5
        _current_split_reader = _native_reader.get();
218
5
    }
219
8
    return Status::OK();
220
8
}
221
222
Status PaimonHybridReader::_init_child_reader(format::TableReader* reader,
223
7
                                              format::FileFormat file_format) {
224
7
    DORIS_CHECK(reader != nullptr);
225
7
    VExprContextSPtrs conjuncts;
226
7
    RETURN_IF_ERROR(_clone_conjuncts(&conjuncts));
227
7
    RETURN_IF_ERROR(reader->init({
228
7
            .projected_columns = _projected_columns,
229
7
            .conjuncts = std::move(conjuncts),
230
7
            .table_reader_owned_conjunct_count = _table_reader_owned_conjunct_count,
231
7
            .format = file_format,
232
7
            .scan_params = _scan_params,
233
7
            .io_ctx = _io_ctx,
234
7
            .runtime_state = _runtime_state,
235
7
            .scanner_profile = _scanner_profile,
236
7
            .push_down_agg_type = _push_down_agg_type,
237
7
            .push_down_count_columns = _push_down_count_columns,
238
7
            .condition_cache_digest = _condition_cache_digest,
239
7
    }));
240
    // Zero means no adaptive prediction has been produced yet. Preserve the child's normal
241
    // runtime default until FileScannerV2 supplies the first positive prediction.
242
7
    if (_batch_size > 0) {
243
0
        reader->set_batch_size(_batch_size);
244
0
    }
245
7
    return Status::OK();
246
7
}
247
248
7
Status PaimonHybridReader::_clone_conjuncts(VExprContextSPtrs* conjuncts) const {
249
7
    DORIS_CHECK(conjuncts != nullptr);
250
7
    conjuncts->clear();
251
7
    conjuncts->reserve(_conjuncts.size());
252
7
    for (const auto& conjunct : _conjuncts) {
253
0
        VExprSPtr root;
254
0
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
255
0
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
256
0
    }
257
7
    return Status::OK();
258
7
}
259
260
11
bool PaimonHybridReader::_is_jni_split(const TFileRangeDesc& range) {
261
11
    return range.__isset.table_format_params && range.table_format_params.__isset.paimon_params &&
262
11
           range.table_format_params.paimon_params.__isset.reader_type &&
263
11
           range.table_format_params.paimon_params.reader_type == TPaimonReaderType::PAIMON_JNI;
264
11
}
265
266
Status PaimonHybridReader::_to_file_format(const TFileRangeDesc& range,
267
8
                                           format::FileFormat* file_format) {
268
8
    DORIS_CHECK(file_format != nullptr);
269
8
    const auto format_type =
270
8
            range.__isset.format_type ? range.format_type : TFileFormatType::FORMAT_PARQUET;
271
8
    switch (format_type) {
272
6
    case TFileFormatType::FORMAT_PARQUET:
273
6
        *file_format = format::FileFormat::PARQUET;
274
6
        return Status::OK();
275
1
    case TFileFormatType::FORMAT_ORC:
276
1
        *file_format = format::FileFormat::ORC;
277
1
        return Status::OK();
278
1
    default:
279
1
        return Status::NotSupported("Unsupported native Paimon file format {}",
280
1
                                    to_string(format_type));
281
8
    }
282
8
}
283
284
} // namespace doris::format::paimon