Coverage Report

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