Coverage Report

Created: 2026-07-15 03:26

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