Coverage Report

Created: 2026-07-07 22:17

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