Coverage Report

Created: 2026-04-22 13:18

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