Coverage Report

Created: 2024-11-18 11:49

/root/doris/be/src/util/key_util.h
Line
Count
Source (jump to first uncovered line)
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 <gen_cpp/segment_v2.pb.h>
21
22
#include <cstdint>
23
#include <iterator>
24
#include <string>
25
#include <vector>
26
27
#include "common/status.h"
28
#include "util/debug_util.h"
29
#include "util/faststring.h"
30
#include "util/slice.h"
31
32
namespace doris {
33
34
// In our system, we have more complicated situation.
35
// First, our keys can be nullptr.
36
// Second, when key columns are not complete we want to distinguish GT and GE. For example,
37
// there are two key columns a and b, we have only one condition a > 1. We can only encode
38
// a prefix key 1, which is less than 1|2. This will make our read more data than
39
// we actually need. So we want to add more marker.
40
// a > 1: will be encoded into 1|\xFF
41
// a >= 1: will be encoded into 1|\x00
42
// a = 1 and b > 1: will be encoded into 1|\x02|1
43
// a = 1 and b is null: will be encoded into 1|\x01
44
45
// Used to represent minimal value for that field
46
constexpr uint8_t KEY_MINIMAL_MARKER = 0x00;
47
// Used to represent a null field, which value is seemed as minimal than other values
48
constexpr uint8_t KEY_NULL_FIRST_MARKER = 0x01;
49
// Used to represent a normal field, which content is encoded after this marker
50
constexpr uint8_t KEY_NORMAL_MARKER = 0x02;
51
// Used to represent maximal value for that field
52
constexpr uint8_t KEY_MAXIMAL_MARKER = 0xFF;
53
// Used to represent a value greater than the normal marker by 1, using by MoW
54
constexpr uint8_t KEY_NORMAL_NEXT_MARKER = 0x03;
55
56
// Encode one row into binary according given num_keys.
57
// A cell will be encoded in the format of a marker and encoded content.
58
// When function encoding row, if any cell isn't found in row, this function will
59
// fill a marker and return. If padding_minimal is true, KEY_MINIMAL_MARKER will
60
// be added, if padding_minimal is false, KEY_MAXIMAL_MARKER will be added.
61
// If all num_keys are found in row, no marker will be added.
62
template <typename RowType, bool is_mow = false>
63
void encode_key_with_padding(std::string* buf, const RowType& row, size_t num_keys,
64
2
                             bool padding_minimal) {
65
6
    for (auto cid = 0; cid < num_keys; cid++) {
66
6
        auto field = row.schema()->column(cid);
67
6
        if (field == nullptr) {
68
2
            if (padding_minimal) {
69
1
                buf->push_back(KEY_MINIMAL_MARKER);
70
1
            } else {
71
1
                if (is_mow) {
72
0
                    buf->push_back(KEY_NORMAL_NEXT_MARKER);
73
1
                } else {
74
1
                    buf->push_back(KEY_MAXIMAL_MARKER);
75
1
                }
76
1
            }
77
2
            break;
78
2
        }
79
80
4
        auto cell = row.cell(cid);
81
4
        if (cell.is_null()) {
82
1
            buf->push_back(KEY_NULL_FIRST_MARKER);
83
1
            continue;
84
1
        }
85
3
        buf->push_back(KEY_NORMAL_MARKER);
86
3
        if (is_mow) {
87
0
            field->full_encode_ascending(cell.cell_ptr(), buf);
88
3
        } else {
89
3
            field->encode_ascending(cell.cell_ptr(), buf);
90
3
        }
91
3
    }
92
2
}
_ZN5doris23encode_key_with_paddingINS_9RowCursorELb0EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_mb
Line
Count
Source
64
2
                             bool padding_minimal) {
65
6
    for (auto cid = 0; cid < num_keys; cid++) {
66
6
        auto field = row.schema()->column(cid);
67
6
        if (field == nullptr) {
68
2
            if (padding_minimal) {
69
1
                buf->push_back(KEY_MINIMAL_MARKER);
70
1
            } else {
71
1
                if (is_mow) {
72
0
                    buf->push_back(KEY_NORMAL_NEXT_MARKER);
73
1
                } else {
74
1
                    buf->push_back(KEY_MAXIMAL_MARKER);
75
1
                }
76
1
            }
77
2
            break;
78
2
        }
79
80
4
        auto cell = row.cell(cid);
81
4
        if (cell.is_null()) {
82
1
            buf->push_back(KEY_NULL_FIRST_MARKER);
83
1
            continue;
84
1
        }
85
3
        buf->push_back(KEY_NORMAL_MARKER);
86
3
        if (is_mow) {
87
0
            field->full_encode_ascending(cell.cell_ptr(), buf);
88
3
        } else {
89
3
            field->encode_ascending(cell.cell_ptr(), buf);
90
3
        }
91
3
    }
92
2
}
Unexecuted instantiation: _ZN5doris23encode_key_with_paddingINS_9RowCursorELb1EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_mb
93
94
// Encode one row into binary according given num_keys.
95
// Client call this function must assure that row contains the first
96
// num_keys columns.
97
template <typename RowType, bool full_encode = false>
98
128k
void encode_key(std::string* buf, const RowType& row, size_t num_keys) {
99
386k
    for (auto cid = 0; cid < num_keys; cid++) {
100
257k
        auto cell = row.cell(cid);
101
257k
        if (cell.is_null()) {
102
1
            buf->push_back(KEY_NULL_FIRST_MARKER);
103
1
            continue;
104
1
        }
105
257k
        buf->push_back(KEY_NORMAL_MARKER);
106
257k
        if (full_encode) {
107
257k
            row.schema()->column(cid)->full_encode_ascending(cell.cell_ptr(), buf);
108
257k
        } else {
109
1
            row.schema()->column(cid)->encode_ascending(cell.cell_ptr(), buf);
110
1
        }
111
257k
    }
112
128k
}
_ZN5doris10encode_keyINS_9RowCursorELb0EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_m
Line
Count
Source
98
1
void encode_key(std::string* buf, const RowType& row, size_t num_keys) {
99
3
    for (auto cid = 0; cid < num_keys; cid++) {
100
2
        auto cell = row.cell(cid);
101
2
        if (cell.is_null()) {
102
1
            buf->push_back(KEY_NULL_FIRST_MARKER);
103
1
            continue;
104
1
        }
105
1
        buf->push_back(KEY_NORMAL_MARKER);
106
1
        if (full_encode) {
107
0
            row.schema()->column(cid)->full_encode_ascending(cell.cell_ptr(), buf);
108
1
        } else {
109
1
            row.schema()->column(cid)->encode_ascending(cell.cell_ptr(), buf);
110
1
        }
111
1
    }
112
1
}
_ZN5doris10encode_keyINS_9RowCursorELb1EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_m
Line
Count
Source
98
128k
void encode_key(std::string* buf, const RowType& row, size_t num_keys) {
99
386k
    for (auto cid = 0; cid < num_keys; cid++) {
100
257k
        auto cell = row.cell(cid);
101
257k
        if (cell.is_null()) {
102
0
            buf->push_back(KEY_NULL_FIRST_MARKER);
103
0
            continue;
104
0
        }
105
257k
        buf->push_back(KEY_NORMAL_MARKER);
106
257k
        if (full_encode) {
107
257k
            row.schema()->column(cid)->full_encode_ascending(cell.cell_ptr(), buf);
108
257k
        } else {
109
0
            row.schema()->column(cid)->encode_ascending(cell.cell_ptr(), buf);
110
0
        }
111
257k
    }
112
128k
}
113
114
} // namespace doris