Coverage Report

Created: 2026-07-07 17:25

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