Coverage Report

Created: 2026-07-20 19:43

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