Coverage Report

Created: 2026-07-13 19:07

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