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 IColumn; |
28 | | class TabletColumn; |
29 | | class TabletSchema; |
30 | | |
31 | | // Encodes rows into the sortable binary key format shared by the short key |
32 | | // index and the primary key index. Each column is encoded as a one byte |
33 | | // marker (KeyConsts) followed by the KeyCoder encoded value, a null column |
34 | | // is encoded as KEY_NULL_FIRST_MARKER without value bytes. |
35 | | class RowKeyEncoder { |
36 | | public: |
37 | | RowKeyEncoder(const TabletSchema& schema, bool mow); |
38 | | |
39 | | // Encode the sort key columns at `pos` with full length. |
40 | | std::string full_encode(const std::vector<const IColumn*>& key_columns, size_t pos) const; |
41 | | |
42 | | // Encode the primary key columns at `pos` with full length, producing the key stored in and |
43 | | // probed against the primary key index. Every mow table builds this view; a table with cluster |
44 | | // keys is the case where it differs from full_encode(), which follows the segment's sort order. |
45 | | std::string full_encode_primary_keys(const std::vector<const IColumn*>& key_columns, |
46 | | size_t pos) const; |
47 | | |
48 | | // Encode the short key columns at `pos`, each column truncated to its |
49 | | // index length. |
50 | | std::string encode_short_keys(const std::vector<const IColumn*>& key_columns, size_t pos) const; |
51 | | |
52 | | // Append the encoded sequence column at `pos` to `encoded_keys`. A null |
53 | | // sequence value is encoded as the minimal value of the column length so |
54 | | // that it sorts first in the primary key index. |
55 | | void append_seq_suffix(std::string* encoded_keys, const IColumn* seq_column, size_t pos) const; |
56 | | |
57 | | // Append the encoded row id to `encoded_keys`, only used by mow tables |
58 | | // with cluster keys. |
59 | | void append_rowid_suffix(std::string* encoded_keys, uint32_t rowid) const; |
60 | | |
61 | 76 | size_t num_sort_key_columns() const { return _sort_key_coders.size(); } |
62 | | |
63 | | private: |
64 | | // Encodes row `pos` of a key column that is not NULL there: its stored value |
65 | | // through the KeyCoder of its FieldType, except CHAR, which is zero padded |
66 | | // to `length` straight into the key. Bound to the FieldType at init, so a |
67 | | // row costs no type switch. |
68 | | struct KeyColumnCoder { |
69 | | void (*full_encode_ascending)(const IColumn& column, size_t pos, size_t length, |
70 | | std::string* buf) = nullptr; |
71 | | void (*encode_ascending)(const IColumn& column, size_t pos, size_t length, |
72 | | size_t index_size, std::string* buf) = nullptr; |
73 | | // Declared length of the column: a CHAR key is padded to it, and a null |
74 | | // sequence value encodes to this many filler bytes. |
75 | | size_t length = 0; |
76 | | }; |
77 | | static KeyColumnCoder _key_column_coder(const TabletColumn& column); |
78 | | |
79 | | static std::string _full_encode(const std::vector<KeyColumnCoder>& key_coders, |
80 | | const std::vector<const IColumn*>& key_columns, size_t pos); |
81 | | |
82 | | void _init_mow(const TabletSchema& schema); |
83 | | void _init_non_mow(const TabletSchema& schema); |
84 | | void _add_default_sort_key_columns(const TabletSchema& schema); |
85 | | void _add_sort_key_column(const TabletColumn& column); |
86 | | |
87 | | // The sort-key view: whatever the segment sorts by. Cluster key columns |
88 | | // for mow tables with cluster keys, primary key columns otherwise. Used by |
89 | | // full_encode() and encode_short_keys(). |
90 | | std::vector<KeyColumnCoder> _sort_key_coders; |
91 | | std::vector<uint16_t> _sort_key_index_size; |
92 | | // The primary-key view, built for every mow table. It coincides with the |
93 | | // sort-key view unless the table has cluster keys, where the segment sorts |
94 | | // by those while its primary key index stays on the schema key columns. |
95 | | std::vector<KeyColumnCoder> _primary_key_coders; |
96 | | // Mow-only: the sequence column suffix of the primary key index. |
97 | | KeyColumnCoder _seq_coder; |
98 | | size_t _num_short_key_columns = 0; |
99 | | }; |
100 | | |
101 | | } // namespace doris |