Coverage Report

Created: 2026-07-21 11:52

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 <memory>
24
#include <utility>
25
#include <vector>
26
27
#include "common/status.h"
28
#include "exec/common/endian.h"
29
#include "format/table/deletion_vector_reader.h"
30
#include "runtime/runtime_state.h"
31
32
namespace doris {
33
34
namespace {
35
36
constexpr static char PAIMON_BITMAP_MAGIC[] = {'\x5E', '\x43', '\xF2', '\xD0'};
37
38
} // namespace
39
40
9
std::string build_paimon_deletion_vector_cache_key(const TPaimonDeletionFileDesc& deletion_file) {
41
9
    return fmt::format("paimon_dv_{}#{}#{}", deletion_file.path, deletion_file.offset,
42
9
                       deletion_file.length);
43
9
}
44
45
Status validate_paimon_deletion_vector_descriptor(const TPaimonDeletionFileDesc& deletion_file,
46
14
                                                  size_t& bytes_read) {
47
14
    if (!deletion_file.__isset.path || !deletion_file.__isset.offset ||
48
14
        !deletion_file.__isset.length) {
49
1
        return Status::DataQualityError(
50
1
                "Paimon deletion file descriptor misses path/offset/length");
51
1
    }
52
13
    return validate_paimon_deletion_vector_read_range(deletion_file.offset, deletion_file.length,
53
13
                                                      bytes_read);
54
14
}
55
56
Status decode_paimon_deletion_vector_buffer(const char* buf, size_t buffer_size,
57
12
                                            DeletionVector* deletion_vector) {
58
12
    if (deletion_vector == nullptr) {
59
0
        return Status::InvalidArgument("deletion vector output must not be null");
60
0
    }
61
12
    if (buf == nullptr) {
62
1
        return Status::DataQualityError("Paimon deletion vector blob is null");
63
1
    }
64
11
    if (buffer_size < 8) [[unlikely]] {
65
2
        return Status::DataQualityError("Deletion vector file size too small: {}", buffer_size);
66
2
    }
67
68
9
    const uint32_t actual_length = BigEndian::Load32(buf);
69
9
    if (static_cast<uint64_t>(actual_length) + 4 != buffer_size) [[unlikely]] {
70
2
        return Status::DataQualityError(
71
2
                "Paimon deletion vector length mismatch, expected: {}, actual: {}",
72
2
                static_cast<uint64_t>(actual_length) + 4, buffer_size);
73
2
    }
74
75
7
    if (memcmp(buf + sizeof(actual_length), PAIMON_BITMAP_MAGIC, 4) != 0) [[unlikely]] {
76
2
        return Status::DataQualityError(
77
2
                "Paimon deletion vector magic number mismatch, expected: {}, actual: {}",
78
2
                BigEndian::Load32(PAIMON_BITMAP_MAGIC),
79
2
                BigEndian::Load32(buf + sizeof(actual_length)));
80
2
    }
81
82
5
    roaring::Roaring roaring_bitmap;
83
5
    try {
84
5
        roaring_bitmap = roaring::Roaring::readSafe(buf + 8, buffer_size - 8);
85
5
    } catch (const std::runtime_error& e) {
86
2
        return Status::RuntimeError(
87
2
                "DeletionVector deserialize error: failed to deserialize roaring bitmap, {}",
88
2
                e.what());
89
2
    }
90
91
3
    *deletion_vector |= DeletionVector(std::move(roaring_bitmap));
92
3
    return Status::OK();
93
5
}
94
95
namespace {
96
97
template <typename Profile>
98
void init_deletion_vector_cache_profile(RuntimeProfile* profile, const char* parent,
99
2
                                        Profile* counters) {
100
2
    counters->decoded_cache_hit_count =
101
2
            ADD_CHILD_COUNTER(profile, "DeletionVectorDecodedCacheHitCount", TUnit::UNIT, parent);
102
2
    counters->decoded_cache_miss_count =
103
2
            ADD_CHILD_COUNTER(profile, "DeletionVectorDecodedCacheMissCount", TUnit::UNIT, parent);
104
2
    counters->file_cache_hit_count =
105
2
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCacheHitCount", TUnit::UNIT, parent);
106
2
    counters->file_cache_miss_count =
107
2
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCacheMissCount", TUnit::UNIT, parent);
108
2
    counters->file_cache_peer_read_count =
109
2
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCachePeerReadCount", TUnit::UNIT, parent);
110
2
}
paimon_reader.cpp:_ZN5doris12_GLOBAL__N_134init_deletion_vector_cache_profileINS_15PaimonOrcReader13PaimonProfileEEEvPNS_14RuntimeProfileEPKcPT_
Line
Count
Source
99
1
                                        Profile* counters) {
100
1
    counters->decoded_cache_hit_count =
101
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorDecodedCacheHitCount", TUnit::UNIT, parent);
102
1
    counters->decoded_cache_miss_count =
103
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorDecodedCacheMissCount", TUnit::UNIT, parent);
104
1
    counters->file_cache_hit_count =
105
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCacheHitCount", TUnit::UNIT, parent);
106
1
    counters->file_cache_miss_count =
107
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCacheMissCount", TUnit::UNIT, parent);
108
1
    counters->file_cache_peer_read_count =
109
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCachePeerReadCount", TUnit::UNIT, parent);
110
1
}
paimon_reader.cpp:_ZN5doris12_GLOBAL__N_134init_deletion_vector_cache_profileINS_19PaimonParquetReader13PaimonProfileEEEvPNS_14RuntimeProfileEPKcPT_
Line
Count
Source
99
1
                                        Profile* counters) {
100
1
    counters->decoded_cache_hit_count =
101
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorDecodedCacheHitCount", TUnit::UNIT, parent);
102
1
    counters->decoded_cache_miss_count =
103
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorDecodedCacheMissCount", TUnit::UNIT, parent);
104
1
    counters->file_cache_hit_count =
105
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCacheHitCount", TUnit::UNIT, parent);
106
1
    counters->file_cache_miss_count =
107
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCacheMissCount", TUnit::UNIT, parent);
108
1
    counters->file_cache_peer_read_count =
109
1
            ADD_CHILD_COUNTER(profile, "DeletionVectorFileCachePeerReadCount", TUnit::UNIT, parent);
110
1
}
111
112
template <typename Profile>
113
void update_deletion_vector_file_cache_profile(const DeletionVectorReader& reader,
114
0
                                               Profile* counters) {
115
0
    const auto& stats = reader.file_cache_statistics();
116
0
    COUNTER_UPDATE(counters->file_cache_hit_count, stats.num_local_io_total);
117
0
    COUNTER_UPDATE(counters->file_cache_miss_count, stats.num_remote_io_total);
118
0
    COUNTER_UPDATE(counters->file_cache_peer_read_count, stats.num_peer_io_total);
119
0
}
Unexecuted instantiation: paimon_reader.cpp:_ZN5doris12_GLOBAL__N_141update_deletion_vector_file_cache_profileINS_15PaimonOrcReader13PaimonProfileEEEvRKNS_20DeletionVectorReaderEPT_
Unexecuted instantiation: paimon_reader.cpp:_ZN5doris12_GLOBAL__N_141update_deletion_vector_file_cache_profileINS_19PaimonParquetReader13PaimonProfileEEEvRKNS_20DeletionVectorReaderEPT_
120
121
} // namespace
122
123
// ============================================================================
124
// PaimonOrcReader
125
// ============================================================================
126
1
void PaimonOrcReader::_init_paimon_profile() {
127
1
    static const char* paimon_profile = "PaimonProfile";
128
1
    ADD_TIMER(get_profile(), paimon_profile);
129
1
    _paimon_profile.num_delete_rows =
130
1
            ADD_CHILD_COUNTER(get_profile(), "NumDeleteRows", TUnit::UNIT, paimon_profile);
131
1
    _paimon_profile.delete_files_read_time =
132
1
            ADD_CHILD_TIMER(get_profile(), "DeleteFileReadTime", paimon_profile);
133
1
    _paimon_profile.parse_deletion_vector_time =
134
1
            ADD_CHILD_TIMER(get_profile(), "ParseDeletionVectorTime", paimon_profile);
135
1
    init_deletion_vector_cache_profile(get_profile(), paimon_profile, &_paimon_profile);
136
1
}
137
138
0
Status PaimonOrcReader::on_before_init_reader(ReaderInitContext* ctx) {
139
0
    _column_descs = ctx->column_descs;
140
0
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
141
0
    RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
142
0
                                              _fill_partition_values,
143
0
                                              &_fill_partition_value_is_null));
144
0
    const orc::Type* orc_type_ptr = nullptr;
145
0
    RETURN_IF_ERROR(get_file_type(&orc_type_ptr));
146
147
0
    RETURN_IF_ERROR(gen_table_info_node_by_field_id(
148
0
            get_scan_params(), get_scan_range().table_format_params.paimon_params.schema_id,
149
0
            get_tuple_descriptor(), orc_type_ptr));
150
0
    ctx->table_info_node = table_info_node_ptr;
151
152
0
    for (const auto& desc : *ctx->column_descs) {
153
0
        if (desc.category == ColumnCategory::REGULAR ||
154
0
            desc.category == ColumnCategory::GENERATED) {
155
0
            ctx->column_names.push_back(desc.name);
156
0
        }
157
0
    }
158
0
    return Status::OK();
159
0
}
160
161
0
Status PaimonOrcReader::on_after_init_reader(ReaderInitContext* /*ctx*/) {
162
0
    return _init_deletion_vector();
163
0
}
164
165
1
Status PaimonOrcReader::_init_deletion_vector() {
166
1
    const auto& table_desc = get_scan_range().table_format_params.paimon_params;
167
1
    if (!table_desc.__isset.deletion_file) {
168
0
        return Status::OK();
169
0
    }
170
171
    // Cannot do count push down if there are delete files
172
1
    if (!get_scan_range().table_format_params.paimon_params.__isset.row_count) {
173
1
        set_push_down_agg_type(TPushAggOp::NONE);
174
1
    }
175
1
    const auto& deletion_file = table_desc.deletion_file;
176
1
    size_t bytes_read = 0;
177
1
    RETURN_IF_ERROR(validate_paimon_deletion_vector_descriptor(deletion_file, bytes_read));
178
179
1
    Status create_status = Status::OK();
180
181
1
    SCOPED_TIMER(_paimon_profile.delete_files_read_time);
182
1
    bool decoded_cache_hit = false;
183
1
    _deletion_vector = _kv_cache->get<DeletionVector>(
184
1
            build_paimon_deletion_vector_cache_key(deletion_file),
185
1
            [&]() -> DeletionVector* {
186
1
                auto deletion_vector = std::make_unique<DeletionVector>();
187
188
1
                TFileRangeDesc delete_range;
189
1
                delete_range.__set_fs_name(get_scan_range().fs_name);
190
1
                delete_range.path = deletion_file.path;
191
1
                delete_range.start_offset = deletion_file.offset;
192
1
                delete_range.size = static_cast<int64_t>(bytes_read);
193
1
                delete_range.file_size = -1;
194
195
1
                DeletionVectorReader dv_reader(get_state(), get_profile(), get_scan_params(),
196
1
                                               delete_range, get_io_ctx());
197
1
                create_status = dv_reader.open();
198
1
                if (!create_status.ok()) [[unlikely]] {
199
1
                    return nullptr;
200
1
                }
201
202
0
                std::vector<char> buffer(bytes_read);
203
0
                create_status =
204
0
                        dv_reader.read_at(deletion_file.offset, {buffer.data(), bytes_read});
205
0
                update_deletion_vector_file_cache_profile(dv_reader, &_paimon_profile);
206
0
                if (!create_status.ok()) [[unlikely]] {
207
0
                    return nullptr;
208
0
                }
209
210
0
                SCOPED_TIMER(_paimon_profile.parse_deletion_vector_time);
211
0
                create_status = decode_paimon_deletion_vector_buffer(buffer.data(), bytes_read,
212
0
                                                                     deletion_vector.get());
213
0
                if (!create_status.ok()) [[unlikely]] {
214
0
                    return nullptr;
215
0
                }
216
0
                COUNTER_UPDATE(_paimon_profile.num_delete_rows, deletion_vector->cardinality());
217
0
                return deletion_vector.release();
218
0
            },
219
1
            &decoded_cache_hit);
220
1
    RETURN_IF_ERROR(create_status);
221
0
    COUNTER_UPDATE(decoded_cache_hit ? _paimon_profile.decoded_cache_hit_count
222
0
                                     : _paimon_profile.decoded_cache_miss_count,
223
0
                   1);
224
0
    if (!_deletion_vector->isEmpty()) [[likely]] {
225
0
        set_deletion_vector(_deletion_vector);
226
0
    }
227
0
    return Status::OK();
228
1
}
229
230
// ============================================================================
231
// PaimonParquetReader
232
// ============================================================================
233
1
void PaimonParquetReader::_init_paimon_profile() {
234
1
    static const char* paimon_profile = "PaimonProfile";
235
1
    ADD_TIMER(get_profile(), paimon_profile);
236
1
    _paimon_profile.num_delete_rows =
237
1
            ADD_CHILD_COUNTER(get_profile(), "NumDeleteRows", TUnit::UNIT, paimon_profile);
238
1
    _paimon_profile.delete_files_read_time =
239
1
            ADD_CHILD_TIMER(get_profile(), "DeleteFileReadTime", paimon_profile);
240
1
    _paimon_profile.parse_deletion_vector_time =
241
1
            ADD_CHILD_TIMER(get_profile(), "ParseDeletionVectorTime", paimon_profile);
242
1
    init_deletion_vector_cache_profile(get_profile(), paimon_profile, &_paimon_profile);
243
1
}
244
245
0
Status PaimonParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
246
0
    _column_descs = ctx->column_descs;
247
0
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
248
0
    RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
249
0
                                              _fill_partition_values,
250
0
                                              &_fill_partition_value_is_null));
251
0
    const FieldDescriptor* field_desc = nullptr;
252
0
    RETURN_IF_ERROR(get_file_metadata_schema(&field_desc));
253
0
    DCHECK(field_desc != nullptr);
254
255
0
    RETURN_IF_ERROR(gen_table_info_node_by_field_id(
256
0
            get_scan_params(), get_scan_range().table_format_params.paimon_params.schema_id,
257
0
            get_tuple_descriptor(), *field_desc));
258
0
    ctx->table_info_node = table_info_node_ptr;
259
260
0
    for (const auto& desc : *ctx->column_descs) {
261
0
        if (desc.category == ColumnCategory::REGULAR ||
262
0
            desc.category == ColumnCategory::GENERATED) {
263
0
            ctx->column_names.push_back(desc.name);
264
0
        }
265
0
    }
266
0
    return Status::OK();
267
0
}
268
269
0
Status PaimonParquetReader::on_after_init_reader(ReaderInitContext* /*ctx*/) {
270
0
    return _init_deletion_vector();
271
0
}
272
273
1
Status PaimonParquetReader::_init_deletion_vector() {
274
1
    const auto& table_desc = get_scan_range().table_format_params.paimon_params;
275
1
    if (!table_desc.__isset.deletion_file) {
276
0
        return Status::OK();
277
0
    }
278
279
1
    if (!get_scan_range().table_format_params.paimon_params.__isset.row_count) {
280
1
        set_push_down_agg_type(TPushAggOp::NONE);
281
1
    }
282
1
    const auto& deletion_file = table_desc.deletion_file;
283
1
    size_t bytes_read = 0;
284
1
    RETURN_IF_ERROR(validate_paimon_deletion_vector_descriptor(deletion_file, bytes_read));
285
286
1
    Status create_status = Status::OK();
287
288
1
    SCOPED_TIMER(_paimon_profile.delete_files_read_time);
289
1
    bool decoded_cache_hit = false;
290
1
    _deletion_vector = _kv_cache->get<DeletionVector>(
291
1
            build_paimon_deletion_vector_cache_key(deletion_file),
292
1
            [&]() -> DeletionVector* {
293
1
                auto deletion_vector = std::make_unique<DeletionVector>();
294
295
1
                TFileRangeDesc delete_range;
296
1
                delete_range.__set_fs_name(get_scan_range().fs_name);
297
1
                delete_range.path = deletion_file.path;
298
1
                delete_range.start_offset = deletion_file.offset;
299
1
                delete_range.size = static_cast<int64_t>(bytes_read);
300
1
                delete_range.file_size = -1;
301
302
1
                DeletionVectorReader dv_reader(get_state(), get_profile(), get_scan_params(),
303
1
                                               delete_range, get_io_ctx());
304
1
                create_status = dv_reader.open();
305
1
                if (!create_status.ok()) [[unlikely]] {
306
1
                    return nullptr;
307
1
                }
308
309
0
                std::vector<char> buffer(bytes_read);
310
0
                create_status =
311
0
                        dv_reader.read_at(deletion_file.offset, {buffer.data(), bytes_read});
312
0
                update_deletion_vector_file_cache_profile(dv_reader, &_paimon_profile);
313
0
                if (!create_status.ok()) [[unlikely]] {
314
0
                    return nullptr;
315
0
                }
316
317
0
                SCOPED_TIMER(_paimon_profile.parse_deletion_vector_time);
318
0
                create_status = decode_paimon_deletion_vector_buffer(buffer.data(), bytes_read,
319
0
                                                                     deletion_vector.get());
320
0
                if (!create_status.ok()) [[unlikely]] {
321
0
                    return nullptr;
322
0
                }
323
0
                COUNTER_UPDATE(_paimon_profile.num_delete_rows, deletion_vector->cardinality());
324
0
                return deletion_vector.release();
325
0
            },
326
1
            &decoded_cache_hit);
327
1
    RETURN_IF_ERROR(create_status);
328
0
    COUNTER_UPDATE(decoded_cache_hit ? _paimon_profile.decoded_cache_hit_count
329
0
                                     : _paimon_profile.decoded_cache_miss_count,
330
0
                   1);
331
0
    if (!_deletion_vector->isEmpty()) [[likely]] {
332
0
        ParquetReader::set_deletion_vector(_deletion_vector);
333
0
    }
334
0
    return Status::OK();
335
1
}
336
337
} // namespace doris