Coverage Report

Created: 2026-07-27 21:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/key/row_key_encoder.h
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
#pragma once
19
20
#include <cstddef>
21
#include <cstdint>
22
#include <string>
23
#include <vector>
24
25
namespace doris {
26
27
class IOlapColumnDataAccessor;
28
class KeyCoder;
29
class TabletColumn;
30
class TabletSchema;
31
32
// Encodes rows into the sortable binary key format shared by the short key
33
// index and the primary key index. Each column is encoded as a one byte
34
// marker (KeyConsts) followed by the KeyCoder encoded value, a null column
35
// is encoded as KEY_NULL_FIRST_MARKER without value bytes.
36
class RowKeyEncoder {
37
public:
38
    RowKeyEncoder(const TabletSchema& schema, bool mow);
39
40
    // Encode the sort key columns at `pos` with full length.
41
    std::string full_encode(const std::vector<IOlapColumnDataAccessor*>& key_columns,
42
                            size_t pos) const;
43
44
    // For a mow table with cluster keys, encode the primary key columns at
45
    // `pos` with full length, producing the key stored in and probed against
46
    // the primary key index.
47
    std::string full_encode_primary_keys(const std::vector<IOlapColumnDataAccessor*>& key_columns,
48
                                         size_t pos) const;
49
50
    // Encode the short key columns at `pos`, each column truncated to its
51
    // index length.
52
    std::string encode_short_keys(const std::vector<IOlapColumnDataAccessor*>& key_columns,
53
                                  size_t pos) const;
54
55
    // Append the encoded sequence column at `pos` to `encoded_keys`. A null
56
    // sequence value is encoded as the minimal value of the column length so
57
    // that it sorts first in the primary key index.
58
    void append_seq_suffix(std::string* encoded_keys, const IOlapColumnDataAccessor* seq_column,
59
                           size_t pos) const;
60
61
    // Append the encoded row id to `encoded_keys`, only used by mow tables
62
    // with cluster keys.
63
    void append_rowid_suffix(std::string* encoded_keys, uint32_t rowid) const;
64
65
9
    size_t num_sort_key_columns() const { return _sort_key_coders.size(); }
66
67
private:
68
    static std::string _full_encode(const std::vector<const KeyCoder*>& key_coders,
69
                                    const std::vector<IOlapColumnDataAccessor*>& key_columns,
70
                                    size_t pos);
71
72
    void _init_mow(const TabletSchema& schema);
73
    void _init_non_mow(const TabletSchema& schema);
74
    void _add_default_sort_key_columns(const TabletSchema& schema);
75
    void _add_sort_key_column(const TabletColumn& column);
76
77
    // The sort-key view: whatever the segment sorts by. Cluster key columns
78
    // for mow tables with cluster keys, primary key columns otherwise. Used by
79
    // full_encode() and encode_short_keys().
80
    std::vector<const KeyCoder*> _sort_key_coders;
81
    std::vector<uint16_t> _sort_key_index_size;
82
    // The separate primary-key view used by mow tables with cluster keys. The
83
    // segment sorts by cluster key columns while its primary key index remains
84
    // based on the schema key columns.
85
    std::vector<const KeyCoder*> _primary_key_coders;
86
    // Mow-only suffix coders for the primary key index.
87
    const KeyCoder* _seq_coder = nullptr;
88
    const KeyCoder* _rowid_coder = nullptr;
89
    size_t _seq_col_length = 0;
90
    size_t _num_short_key_columns = 0;
91
};
92
93
} // namespace doris