Coverage Report

Created: 2026-06-04 15:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/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/table/paimon_reader.h"
19
20
#include <vector>
21
22
#include "common/status.h"
23
#include "format/table/deletion_vector_reader.h"
24
#include "runtime/runtime_state.h"
25
26
namespace doris {
27
28
// ============================================================================
29
// PaimonOrcReader
30
// ============================================================================
31
1.03k
void PaimonOrcReader::_init_paimon_profile() {
32
1.03k
    static const char* paimon_profile = "PaimonProfile";
33
1.03k
    ADD_TIMER(get_profile(), paimon_profile);
34
1.03k
    _paimon_profile.num_delete_rows =
35
1.03k
            ADD_CHILD_COUNTER(get_profile(), "NumDeleteRows", TUnit::UNIT, paimon_profile);
36
1.03k
    _paimon_profile.delete_files_read_time =
37
1.03k
            ADD_CHILD_TIMER(get_profile(), "DeleteFileReadTime", paimon_profile);
38
1.03k
    _paimon_profile.parse_deletion_vector_time =
39
1.03k
            ADD_CHILD_TIMER(get_profile(), "ParseDeletionVectorTime", paimon_profile);
40
1.03k
}
41
42
1.03k
Status PaimonOrcReader::on_before_init_reader(ReaderInitContext* ctx) {
43
1.03k
    _column_descs = ctx->column_descs;
44
1.03k
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
45
1.03k
    const orc::Type* orc_type_ptr = nullptr;
46
1.03k
    RETURN_IF_ERROR(get_file_type(&orc_type_ptr));
47
48
1.03k
    RETURN_IF_ERROR(gen_table_info_node_by_field_id(
49
1.03k
            get_scan_params(), get_scan_range().table_format_params.paimon_params.schema_id,
50
1.03k
            get_tuple_descriptor(), orc_type_ptr));
51
1.03k
    ctx->table_info_node = table_info_node_ptr;
52
53
6.21k
    for (const auto& desc : *ctx->column_descs) {
54
6.21k
        if (desc.category == ColumnCategory::REGULAR ||
55
6.22k
            desc.category == ColumnCategory::GENERATED) {
56
6.22k
            ctx->column_names.push_back(desc.name);
57
6.22k
        }
58
6.21k
    }
59
1.03k
    return Status::OK();
60
1.03k
}
61
62
1.04k
Status PaimonOrcReader::on_after_init_reader(ReaderInitContext* /*ctx*/) {
63
1.04k
    return _init_deletion_vector();
64
1.04k
}
65
66
1.04k
Status PaimonOrcReader::_init_deletion_vector() {
67
1.04k
    const auto& table_desc = get_scan_range().table_format_params.paimon_params;
68
1.04k
    if (!table_desc.__isset.deletion_file) {
69
1.03k
        return Status::OK();
70
1.03k
    }
71
72
    // Cannot do count push down if there are delete files
73
10
    if (!get_scan_range().table_format_params.paimon_params.__isset.row_count) {
74
10
        set_push_down_agg_type(TPushAggOp::NONE);
75
10
    }
76
10
    const auto& deletion_file = table_desc.deletion_file;
77
78
10
    Status create_status = Status::OK();
79
80
10
    std::string key;
81
10
    key.resize(deletion_file.path.size() + sizeof(deletion_file.offset));
82
10
    memcpy(key.data(), deletion_file.path.data(), deletion_file.path.size());
83
10
    memcpy(key.data() + deletion_file.path.size(), &deletion_file.offset,
84
10
           sizeof(deletion_file.offset));
85
86
10
    SCOPED_TIMER(_paimon_profile.delete_files_read_time);
87
10
    using DeleteRows = std::vector<int64_t>;
88
10
    _delete_rows = _kv_cache->get<DeleteRows>(key, [&]() -> DeleteRows* {
89
10
        auto* delete_rows = new DeleteRows;
90
91
10
        TFileRangeDesc delete_range;
92
10
        delete_range.__set_fs_name(get_scan_range().fs_name);
93
10
        delete_range.path = deletion_file.path;
94
10
        delete_range.start_offset = deletion_file.offset;
95
10
        delete_range.size = deletion_file.length + 4;
96
10
        delete_range.file_size = -1;
97
98
10
        DeletionVectorReader dv_reader(get_state(), get_profile(), get_scan_params(), delete_range,
99
10
                                       get_io_ctx());
100
10
        create_status = dv_reader.open();
101
10
        if (!create_status.ok()) [[unlikely]] {
102
0
            return nullptr;
103
0
        }
104
105
10
        size_t bytes_read = deletion_file.length + 4;
106
10
        std::vector<char> buffer(bytes_read);
107
10
        create_status = dv_reader.read_at(deletion_file.offset, {buffer.data(), bytes_read});
108
10
        if (!create_status.ok()) [[unlikely]] {
109
0
            return nullptr;
110
0
        }
111
112
10
        const char* buf = buffer.data();
113
10
        uint32_t actual_length;
114
10
        std::memcpy(reinterpret_cast<char*>(&actual_length), buf, 4);
115
10
        std::reverse(reinterpret_cast<char*>(&actual_length),
116
10
                     reinterpret_cast<char*>(&actual_length) + 4);
117
10
        buf += 4;
118
10
        if (actual_length != bytes_read - 4) [[unlikely]] {
119
0
            create_status = Status::RuntimeError(
120
0
                    "DeletionVector deserialize error: length not match, "
121
0
                    "actual length: {}, expect length: {}",
122
0
                    actual_length, bytes_read - 4);
123
0
            return nullptr;
124
0
        }
125
10
        uint32_t magic_number;
126
10
        std::memcpy(reinterpret_cast<char*>(&magic_number), buf, 4);
127
10
        std::reverse(reinterpret_cast<char*>(&magic_number),
128
10
                     reinterpret_cast<char*>(&magic_number) + 4);
129
10
        buf += 4;
130
10
        const static uint32_t MAGIC_NUMBER = 1581511376;
131
10
        if (magic_number != MAGIC_NUMBER) [[unlikely]] {
132
0
            create_status = Status::RuntimeError(
133
0
                    "DeletionVector deserialize error: invalid magic number {}", magic_number);
134
0
            return nullptr;
135
0
        }
136
137
10
        roaring::Roaring roaring_bitmap;
138
10
        SCOPED_TIMER(_paimon_profile.parse_deletion_vector_time);
139
10
        try {
140
10
            roaring_bitmap = roaring::Roaring::readSafe(buf, bytes_read - 4);
141
10
        } catch (const std::runtime_error& e) {
142
0
            create_status = Status::RuntimeError(
143
0
                    "DeletionVector deserialize error: failed to deserialize roaring bitmap, {}",
144
0
                    e.what());
145
0
            return nullptr;
146
0
        }
147
10
        delete_rows->reserve(roaring_bitmap.cardinality());
148
20
        for (auto it = roaring_bitmap.begin(); it != roaring_bitmap.end(); it++) {
149
10
            delete_rows->push_back(*it);
150
10
        }
151
10
        COUNTER_UPDATE(_paimon_profile.num_delete_rows, delete_rows->size());
152
10
        return delete_rows;
153
10
    });
154
10
    RETURN_IF_ERROR(create_status);
155
10
    if (!_delete_rows->empty()) [[likely]] {
156
10
        set_position_delete_rowids(_delete_rows);
157
10
    }
158
10
    return Status::OK();
159
10
}
160
161
// ============================================================================
162
// PaimonParquetReader
163
// ============================================================================
164
1.34k
void PaimonParquetReader::_init_paimon_profile() {
165
1.34k
    static const char* paimon_profile = "PaimonProfile";
166
1.34k
    ADD_TIMER(get_profile(), paimon_profile);
167
1.34k
    _paimon_profile.num_delete_rows =
168
1.34k
            ADD_CHILD_COUNTER(get_profile(), "NumDeleteRows", TUnit::UNIT, paimon_profile);
169
1.34k
    _paimon_profile.delete_files_read_time =
170
1.34k
            ADD_CHILD_TIMER(get_profile(), "DeleteFileReadTime", paimon_profile);
171
1.34k
    _paimon_profile.parse_deletion_vector_time =
172
1.34k
            ADD_CHILD_TIMER(get_profile(), "ParseDeletionVectorTime", paimon_profile);
173
1.34k
}
174
175
1.33k
Status PaimonParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
176
1.33k
    _column_descs = ctx->column_descs;
177
1.33k
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
178
1.33k
    const FieldDescriptor* field_desc = nullptr;
179
1.33k
    RETURN_IF_ERROR(get_file_metadata_schema(&field_desc));
180
1.33k
    DCHECK(field_desc != nullptr);
181
182
1.33k
    RETURN_IF_ERROR(gen_table_info_node_by_field_id(
183
1.33k
            get_scan_params(), get_scan_range().table_format_params.paimon_params.schema_id,
184
1.33k
            get_tuple_descriptor(), *field_desc));
185
1.33k
    ctx->table_info_node = table_info_node_ptr;
186
187
3.74k
    for (const auto& desc : *ctx->column_descs) {
188
3.74k
        if (desc.category == ColumnCategory::REGULAR ||
189
3.75k
            desc.category == ColumnCategory::GENERATED) {
190
3.75k
            ctx->column_names.push_back(desc.name);
191
3.75k
        }
192
3.74k
    }
193
1.33k
    return Status::OK();
194
1.33k
}
195
196
1.33k
Status PaimonParquetReader::on_after_init_reader(ReaderInitContext* /*ctx*/) {
197
1.33k
    return _init_deletion_vector();
198
1.33k
}
199
200
1.33k
Status PaimonParquetReader::_init_deletion_vector() {
201
1.33k
    const auto& table_desc = get_scan_range().table_format_params.paimon_params;
202
1.33k
    if (!table_desc.__isset.deletion_file) {
203
1.31k
        return Status::OK();
204
1.31k
    }
205
206
17
    if (!get_scan_range().table_format_params.paimon_params.__isset.row_count) {
207
14
        set_push_down_agg_type(TPushAggOp::NONE);
208
14
    }
209
17
    const auto& deletion_file = table_desc.deletion_file;
210
211
17
    Status create_status = Status::OK();
212
213
17
    std::string key;
214
17
    key.resize(deletion_file.path.size() + sizeof(deletion_file.offset));
215
17
    memcpy(key.data(), deletion_file.path.data(), deletion_file.path.size());
216
17
    memcpy(key.data() + deletion_file.path.size(), &deletion_file.offset,
217
17
           sizeof(deletion_file.offset));
218
219
17
    SCOPED_TIMER(_paimon_profile.delete_files_read_time);
220
17
    using DeleteRows = std::vector<int64_t>;
221
17
    _delete_rows = _kv_cache->get<DeleteRows>(key, [&]() -> DeleteRows* {
222
14
        auto* delete_rows = new DeleteRows;
223
224
14
        TFileRangeDesc delete_range;
225
14
        delete_range.__set_fs_name(get_scan_range().fs_name);
226
14
        delete_range.path = deletion_file.path;
227
14
        delete_range.start_offset = deletion_file.offset;
228
14
        delete_range.size = deletion_file.length + 4;
229
14
        delete_range.file_size = -1;
230
231
14
        DeletionVectorReader dv_reader(get_state(), get_profile(), get_scan_params(), delete_range,
232
14
                                       get_io_ctx());
233
14
        create_status = dv_reader.open();
234
14
        if (!create_status.ok()) [[unlikely]] {
235
0
            return nullptr;
236
0
        }
237
238
14
        size_t bytes_read = deletion_file.length + 4;
239
14
        std::vector<char> buffer(bytes_read);
240
14
        create_status = dv_reader.read_at(deletion_file.offset, {buffer.data(), bytes_read});
241
14
        if (!create_status.ok()) [[unlikely]] {
242
0
            return nullptr;
243
0
        }
244
245
14
        const char* buf = buffer.data();
246
14
        uint32_t actual_length;
247
14
        std::memcpy(reinterpret_cast<char*>(&actual_length), buf, 4);
248
14
        std::reverse(reinterpret_cast<char*>(&actual_length),
249
14
                     reinterpret_cast<char*>(&actual_length) + 4);
250
14
        buf += 4;
251
14
        if (actual_length != bytes_read - 4) [[unlikely]] {
252
0
            create_status = Status::RuntimeError(
253
0
                    "DeletionVector deserialize error: length not match, "
254
0
                    "actual length: {}, expect length: {}",
255
0
                    actual_length, bytes_read - 4);
256
0
            return nullptr;
257
0
        }
258
14
        uint32_t magic_number;
259
14
        std::memcpy(reinterpret_cast<char*>(&magic_number), buf, 4);
260
14
        std::reverse(reinterpret_cast<char*>(&magic_number),
261
14
                     reinterpret_cast<char*>(&magic_number) + 4);
262
14
        buf += 4;
263
14
        const static uint32_t MAGIC_NUMBER = 1581511376;
264
14
        if (magic_number != MAGIC_NUMBER) [[unlikely]] {
265
0
            create_status = Status::RuntimeError(
266
0
                    "DeletionVector deserialize error: invalid magic number {}", magic_number);
267
0
            return nullptr;
268
0
        }
269
270
14
        roaring::Roaring roaring_bitmap;
271
14
        SCOPED_TIMER(_paimon_profile.parse_deletion_vector_time);
272
14
        try {
273
14
            roaring_bitmap = roaring::Roaring::readSafe(buf, bytes_read - 4);
274
14
        } catch (const std::runtime_error& e) {
275
0
            create_status = Status::RuntimeError(
276
0
                    "DeletionVector deserialize error: failed to deserialize roaring bitmap, {}",
277
0
                    e.what());
278
0
            return nullptr;
279
0
        }
280
14
        delete_rows->reserve(roaring_bitmap.cardinality());
281
28
        for (auto it = roaring_bitmap.begin(); it != roaring_bitmap.end(); it++) {
282
14
            delete_rows->push_back(*it);
283
14
        }
284
14
        COUNTER_UPDATE(_paimon_profile.num_delete_rows, delete_rows->size());
285
14
        return delete_rows;
286
14
    });
287
17
    RETURN_IF_ERROR(create_status);
288
17
    if (!_delete_rows->empty()) [[likely]] {
289
14
        ParquetReader::set_delete_rows(_delete_rows);
290
14
    }
291
17
    return Status::OK();
292
17
}
293
294
} // namespace doris