Coverage Report

Created: 2025-12-29 20:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/key_coder.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 <glog/logging.h>
21
#include <limits.h>
22
#include <stdint.h>
23
#include <string.h>
24
25
#include <algorithm>
26
#include <ostream>
27
#include <string>
28
#include <type_traits>
29
30
#include "absl/strings/substitute.h"
31
#include "common/status.h"
32
#include "olap/decimal12.h"
33
#include "olap/olap_common.h"
34
#include "olap/types.h"
35
#include "util/slice.h"
36
#include "vec/common/endian.h"
37
#include "vec/core/extended_types.h"
38
#include "vec/core/types.h"
39
40
namespace doris {
41
42
using FullEncodeAscendingFunc = void (*)(const void* value, std::string* buf);
43
using EncodeAscendingFunc = void (*)(const void* value, size_t index_size, std::string* buf);
44
using DecodeAscendingFunc = Status (*)(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr);
45
46
// Order-preserving binary encoding for values of a particular type so that
47
// those values can be compared by memcpy their encoded bytes.
48
//
49
// To obtain instance of this class, use the `get_key_coder(FieldType)` method.
50
class KeyCoder {
51
public:
52
    template <typename TraitsType>
53
    KeyCoder(TraitsType traits);
54
55
    // encode the provided `value` into `buf`.
56
1.00M
    void full_encode_ascending(const void* value, std::string* buf) const {
57
1.00M
        _full_encode_ascending(value, buf);
58
1.00M
    }
59
60
    // similar to `full_encode_ascending`, but only encode part (the first `index_size` bytes) of the value.
61
    // only applicable to string type
62
12.1k
    void encode_ascending(const void* value, size_t index_size, std::string* buf) const {
63
12.1k
        _encode_ascending(value, index_size, buf);
64
12.1k
    }
65
66
    // Only used for test, should delete it in the future
67
2.02k
    Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) const {
68
2.02k
        return _decode_ascending(encoded_key, index_size, cell_ptr);
69
2.02k
    }
70
71
private:
72
    FullEncodeAscendingFunc _full_encode_ascending;
73
    EncodeAscendingFunc _encode_ascending;
74
    DecodeAscendingFunc _decode_ascending;
75
};
76
77
extern const KeyCoder* get_key_coder(FieldType type);
78
79
template <FieldType field_type, typename Enable = void>
80
class KeyCoderTraits {};
81
82
template <FieldType field_type>
83
class KeyCoderTraits<
84
        field_type,
85
        typename std::enable_if<
86
                IsIntegral<typename CppTypeTraits<field_type>::CppType>::value ||
87
                vectorized::IsDecimalNumber<typename CppTypeTraits<field_type>::CppType>>::type> {
88
public:
89
    using CppType = typename CppTypeTraits<field_type>::CppType;
90
    using UnsignedCppType = typename CppTypeTraits<field_type>::UnsignedCppType;
91
92
1.06M
    static void full_encode_ascending(const void* value, std::string* buf) {
93
1.06M
        UnsignedCppType unsigned_val;
94
1.06M
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
1.06M
        if (IsSigned<CppType>::value) {
97
1.01M
            unsigned_val ^=
98
1.01M
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
1.01M
        }
100
        // make it bigendian
101
1.06M
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
1.06M
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
1.06M
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE7EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
30.2k
    static void full_encode_ascending(const void* value, std::string* buf) {
93
30.2k
        UnsignedCppType unsigned_val;
94
30.2k
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
30.3k
        if (IsSigned<CppType>::value) {
97
30.3k
            unsigned_val ^=
98
30.3k
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
30.3k
        }
100
        // make it bigendian
101
30.2k
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
30.2k
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
30.2k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE5EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
980k
    static void full_encode_ascending(const void* value, std::string* buf) {
93
980k
        UnsignedCppType unsigned_val;
94
980k
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
980k
        if (IsSigned<CppType>::value) {
97
980k
            unsigned_val ^=
98
980k
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
980k
        }
100
        // make it bigendian
101
980k
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
980k
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
980k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE1EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
289
    static void full_encode_ascending(const void* value, std::string* buf) {
93
289
        UnsignedCppType unsigned_val;
94
289
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
289
        if (IsSigned<CppType>::value) {
97
289
            unsigned_val ^=
98
289
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
289
        }
100
        // make it bigendian
101
289
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
289
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
289
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE3EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
298
    static void full_encode_ascending(const void* value, std::string* buf) {
93
298
        UnsignedCppType unsigned_val;
94
298
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
298
        if (IsSigned<CppType>::value) {
97
298
            unsigned_val ^=
98
298
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
298
        }
100
        // make it bigendian
101
298
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
298
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
298
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE6EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
257
    static void full_encode_ascending(const void* value, std::string* buf) {
93
257
        UnsignedCppType unsigned_val;
94
257
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
257
        if (IsSigned<CppType>::value) {
97
0
            unsigned_val ^=
98
0
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
0
        }
100
        // make it bigendian
101
257
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
257
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
257
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE8EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
54.7k
    static void full_encode_ascending(const void* value, std::string* buf) {
93
54.7k
        UnsignedCppType unsigned_val;
94
54.7k
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
54.7k
        if (IsSigned<CppType>::value) {
97
0
            unsigned_val ^=
98
0
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
0
        }
100
        // make it bigendian
101
54.7k
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
54.7k
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
54.7k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE9EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
262
    static void full_encode_ascending(const void* value, std::string* buf) {
93
262
        UnsignedCppType unsigned_val;
94
262
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
262
        if (IsSigned<CppType>::value) {
97
262
            unsigned_val ^=
98
262
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
262
        }
100
        // make it bigendian
101
262
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
262
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
262
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE15EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
265
    static void full_encode_ascending(const void* value, std::string* buf) {
93
265
        UnsignedCppType unsigned_val;
94
265
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
265
        if (IsSigned<CppType>::value) {
97
265
            unsigned_val ^=
98
265
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
265
        }
100
        // make it bigendian
101
265
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
265
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
265
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE24EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
58
    static void full_encode_ascending(const void* value, std::string* buf) {
93
58
        UnsignedCppType unsigned_val;
94
58
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
58
        if (IsSigned<CppType>::value) {
97
0
            unsigned_val ^=
98
0
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
0
        }
100
        // make it bigendian
101
58
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
58
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
58
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE31EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
51
    static void full_encode_ascending(const void* value, std::string* buf) {
93
51
        UnsignedCppType unsigned_val;
94
51
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
51
        if (IsSigned<CppType>::value) {
97
51
            unsigned_val ^=
98
51
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
51
        }
100
        // make it bigendian
101
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
51
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE32EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
51
    static void full_encode_ascending(const void* value, std::string* buf) {
93
51
        UnsignedCppType unsigned_val;
94
51
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
51
        if (IsSigned<CppType>::value) {
97
51
            unsigned_val ^=
98
51
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
51
        }
100
        // make it bigendian
101
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
51
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE33EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
51
    static void full_encode_ascending(const void* value, std::string* buf) {
93
51
        UnsignedCppType unsigned_val;
94
51
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
51
        if (IsSigned<CppType>::value) {
97
51
            unsigned_val ^=
98
51
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
51
        }
100
        // make it bigendian
101
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
51
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE37EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
51
    static void full_encode_ascending(const void* value, std::string* buf) {
93
51
        UnsignedCppType unsigned_val;
94
51
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
51
        if (IsSigned<CppType>::value) {
97
51
            unsigned_val ^=
98
51
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
51
        }
100
        // make it bigendian
101
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
51
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE38EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
51
    static void full_encode_ascending(const void* value, std::string* buf) {
93
51
        UnsignedCppType unsigned_val;
94
51
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
51
        if (IsSigned<CppType>::value) {
97
0
            unsigned_val ^=
98
0
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
0
        }
100
        // make it bigendian
101
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
51
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE39EvE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
92
51
    static void full_encode_ascending(const void* value, std::string* buf) {
93
51
        UnsignedCppType unsigned_val;
94
51
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
95
        // swap MSB to encode integer
96
51
        if (IsSigned<CppType>::value) {
97
0
            unsigned_val ^=
98
0
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
99
0
        }
100
        // make it bigendian
101
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
102
103
51
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
104
51
    }
105
106
11.6k
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
11.6k
        full_encode_ascending(value, buf);
108
11.6k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE1EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
209
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
209
        full_encode_ascending(value, buf);
108
209
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE3EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
212
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
212
        full_encode_ascending(value, buf);
108
212
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE5EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
10.2k
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
10.2k
        full_encode_ascending(value, buf);
108
10.2k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE6EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
202
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
202
        full_encode_ascending(value, buf);
108
202
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE7EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
203
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
203
        full_encode_ascending(value, buf);
108
203
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE8EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
202
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
202
        full_encode_ascending(value, buf);
108
202
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE9EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
203
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
203
        full_encode_ascending(value, buf);
108
203
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE15EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
106
202
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
107
202
        full_encode_ascending(value, buf);
108
202
    }
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE24EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE31EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE32EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE33EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE37EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE38EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris14KeyCoderTraitsILNS_9FieldTypeE39EvE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
109
110
22.1k
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
22.1k
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
22.1k
        UnsignedCppType unsigned_val;
118
22.1k
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
22.1k
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
22.1k
        if (IsSigned<CppType>::value) {
121
1.55k
            unsigned_val ^=
122
1.55k
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
1.55k
        }
124
22.1k
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
22.1k
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
22.1k
        return Status::OK();
127
22.1k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE7EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
1.02k
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
1.02k
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
1.02k
        UnsignedCppType unsigned_val;
118
1.02k
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
1.02k
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
1.02k
        if (IsSigned<CppType>::value) {
121
1.02k
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
1.02k
        }
124
1.02k
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
1.02k
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
1.02k
        return Status::OK();
127
1.02k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE5EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
117
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
117
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
117
        UnsignedCppType unsigned_val;
118
117
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
117
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
117
        if (IsSigned<CppType>::value) {
121
117
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
117
        }
124
117
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
117
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
117
        return Status::OK();
127
117
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE1EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
53
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
53
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
53
        UnsignedCppType unsigned_val;
118
53
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
53
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
53
        if (IsSigned<CppType>::value) {
121
53
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
53
        }
124
53
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
53
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
53
        return Status::OK();
127
53
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE3EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
53
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
53
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
53
        UnsignedCppType unsigned_val;
118
53
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
53
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
53
        if (IsSigned<CppType>::value) {
121
53
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
53
        }
124
53
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
53
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
53
        return Status::OK();
127
53
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE6EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
53
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
53
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
53
        UnsignedCppType unsigned_val;
118
53
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
53
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
53
        if (IsSigned<CppType>::value) {
121
0
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
0
        }
124
53
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
53
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
53
        return Status::OK();
127
53
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE8EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
20.4k
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
20.4k
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
20.4k
        UnsignedCppType unsigned_val;
118
20.4k
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
20.4k
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
20.4k
        if (IsSigned<CppType>::value) {
121
0
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
0
        }
124
20.4k
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
20.4k
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
20.4k
        return Status::OK();
127
20.4k
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE9EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
53
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
53
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
53
        UnsignedCppType unsigned_val;
118
53
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
53
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
53
        if (IsSigned<CppType>::value) {
121
53
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
53
        }
124
53
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
53
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
53
        return Status::OK();
127
53
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE15EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
53
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
53
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
53
        UnsignedCppType unsigned_val;
118
53
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
53
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
53
        if (IsSigned<CppType>::value) {
121
53
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
53
        }
124
53
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
53
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
53
        return Status::OK();
127
53
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE24EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
0
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
0
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE31EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
51
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
51
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE32EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
51
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
51
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE33EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
51
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
51
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE37EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
51
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
51
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE38EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
0
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
0
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
_ZN5doris14KeyCoderTraitsILNS_9FieldTypeE39EvE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
110
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
111
        // decode_ascending only used in orinal index page, maybe should remove it in the future.
112
        // currently, we reduce the usage of this method.
113
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
114
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
115
0
                                           sizeof(UnsignedCppType), encoded_key->size);
116
0
        }
117
51
        UnsignedCppType unsigned_val;
118
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
119
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
120
51
        if (IsSigned<CppType>::value) {
121
0
            unsigned_val ^=
122
                    (static_cast<UnsignedCppType>(1) << (sizeof(UnsignedCppType) * CHAR_BIT - 1));
123
0
        }
124
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
125
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
126
51
        return Status::OK();
127
51
    }
128
};
129
130
template <>
131
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_DATE> {
132
public:
133
    using CppType = typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATE>::CppType;
134
    using UnsignedCppType =
135
            typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATE>::UnsignedCppType;
136
137
public:
138
266
    static void full_encode_ascending(const void* value, std::string* buf) {
139
266
        UnsignedCppType unsigned_val;
140
266
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
141
        // make it bigendian
142
266
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
143
266
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
144
266
    }
145
146
203
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
147
203
        full_encode_ascending(value, buf);
148
203
    }
149
150
53
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
151
53
        if (encoded_key->size < sizeof(UnsignedCppType)) {
152
0
            return Status::InvalidArgument("Key too short, need={} vs real={}",
153
0
                                           sizeof(UnsignedCppType), encoded_key->size);
154
0
        }
155
53
        UnsignedCppType unsigned_val;
156
53
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
157
53
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
158
53
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
159
53
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
160
53
        return Status::OK();
161
53
    }
162
};
163
164
template <>
165
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_DATEV2> {
166
public:
167
    using CppType = typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATEV2>::CppType;
168
    using UnsignedCppType =
169
            typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATEV2>::UnsignedCppType;
170
171
public:
172
59
    static void full_encode_ascending(const void* value, std::string* buf) {
173
59
        UnsignedCppType unsigned_val;
174
59
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
175
        // make it bigendian
176
59
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
177
59
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
178
59
    }
179
180
0
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
181
0
        full_encode_ascending(value, buf);
182
0
    }
183
184
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
185
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
186
0
            return Status::InvalidArgument(absl::Substitute("Key too short, need=$0 vs real=$1",
187
0
                                                            sizeof(UnsignedCppType),
188
0
                                                            encoded_key->size));
189
0
        }
190
51
        UnsignedCppType unsigned_val;
191
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
192
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
193
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
194
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
195
51
        return Status::OK();
196
51
    }
197
};
198
199
template <>
200
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_DATETIMEV2> {
201
public:
202
    using CppType = typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATETIMEV2>::CppType;
203
    using UnsignedCppType =
204
            typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATETIMEV2>::UnsignedCppType;
205
206
public:
207
57
    static void full_encode_ascending(const void* value, std::string* buf) {
208
57
        UnsignedCppType unsigned_val;
209
57
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
210
        // make it bigendian
211
57
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
212
57
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
213
57
    }
214
215
0
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
216
0
        full_encode_ascending(value, buf);
217
0
    }
218
219
51
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
220
51
        if (encoded_key->size < sizeof(UnsignedCppType)) {
221
0
            return Status::InvalidArgument(absl::Substitute("Key too short, need=$0 vs real=$1",
222
0
                                                            sizeof(UnsignedCppType),
223
0
                                                            encoded_key->size));
224
0
        }
225
51
        UnsignedCppType unsigned_val;
226
51
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
227
51
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
228
51
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
229
51
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
230
51
        return Status::OK();
231
51
    }
232
};
233
234
template <>
235
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ> {
236
public:
237
    using CppType = typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ>::CppType;
238
    using UnsignedCppType =
239
            typename CppTypeTraits<FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ>::UnsignedCppType;
240
241
public:
242
29
    static void full_encode_ascending(const void* value, std::string* buf) {
243
29
        UnsignedCppType unsigned_val;
244
29
        memcpy(&unsigned_val, value, sizeof(unsigned_val));
245
        // make it bigendian
246
29
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
247
29
        buf->append((char*)&unsigned_val, sizeof(unsigned_val));
248
29
    }
249
250
0
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
251
0
        full_encode_ascending(value, buf);
252
0
    }
253
254
0
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
255
0
        if (encoded_key->size < sizeof(UnsignedCppType)) {
256
0
            return Status::InvalidArgument(absl::Substitute("Key too short, need=$0 vs real=$1",
257
0
                                                            sizeof(UnsignedCppType),
258
0
                                                            encoded_key->size));
259
0
        }
260
0
        UnsignedCppType unsigned_val;
261
0
        memcpy(&unsigned_val, encoded_key->data, sizeof(UnsignedCppType));
262
0
        unsigned_val = to_endian<std::endian::big>(unsigned_val);
263
0
        memcpy(cell_ptr, &unsigned_val, sizeof(UnsignedCppType));
264
0
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
265
0
        return Status::OK();
266
0
    }
267
};
268
269
template <>
270
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_DECIMAL> {
271
public:
272
69
    static void full_encode_ascending(const void* value, std::string* buf) {
273
69
        decimal12_t decimal_val;
274
69
        memcpy(&decimal_val, value, sizeof(decimal12_t));
275
69
        KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_BIGINT>::full_encode_ascending(
276
69
                &decimal_val.integer, buf);
277
69
        KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_INT>::full_encode_ascending(&decimal_val.fraction,
278
69
                                                                              buf);
279
69
    } // namespace doris
280
281
4
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
282
4
        full_encode_ascending(value, buf);
283
4
    }
284
285
64
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
286
64
        decimal12_t decimal_val = {0, 0};
287
64
        RETURN_IF_ERROR(KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_BIGINT>::decode_ascending(
288
64
                encoded_key, sizeof(decimal_val.integer), (uint8_t*)&decimal_val.integer));
289
64
        RETURN_IF_ERROR(KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_INT>::decode_ascending(
290
64
                encoded_key, sizeof(decimal_val.fraction), (uint8_t*)&decimal_val.fraction));
291
64
        memcpy(cell_ptr, &decimal_val, sizeof(decimal12_t));
292
64
        return Status::OK();
293
64
    }
294
};
295
296
template <>
297
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_CHAR> {
298
public:
299
2
    NO_SANITIZE_UNDEFINED static void full_encode_ascending(const void* value, std::string* buf) {
300
2
        auto slice = reinterpret_cast<const Slice*>(value);
301
2
        buf->append(slice->get_data(), slice->get_size());
302
2
    }
303
304
    NO_SANITIZE_UNDEFINED static void encode_ascending(const void* value, size_t index_size,
305
50
                                                       std::string* buf) {
306
50
        const Slice* slice = (const Slice*)value;
307
50
        CHECK(index_size <= slice->size)
308
0
                << "index size is larger than char size, index=" << index_size
309
0
                << ", char=" << slice->size;
310
50
        buf->append(slice->data, index_size);
311
50
    }
312
313
0
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
314
0
        throw Exception(Status::FatalError("decode_ascending is not implemented"));
315
0
    }
316
};
317
318
template <>
319
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_VARCHAR> {
320
public:
321
613
    NO_SANITIZE_UNDEFINED static void full_encode_ascending(const void* value, std::string* buf) {
322
613
        auto slice = reinterpret_cast<const Slice*>(value);
323
613
        buf->append(slice->get_data(), slice->get_size());
324
613
    }
325
326
    NO_SANITIZE_UNDEFINED static void encode_ascending(const void* value, size_t index_size,
327
183
                                                       std::string* buf) {
328
183
        const Slice* slice = (const Slice*)value;
329
183
        size_t copy_size = std::min(index_size, slice->size);
330
183
        buf->append(slice->data, copy_size);
331
183
    }
332
333
0
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
334
0
        throw Exception(Status::FatalError("decode_ascending is not implemented"));
335
0
    }
336
};
337
338
template <>
339
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_STRING> {
340
public:
341
884
    NO_SANITIZE_UNDEFINED static void full_encode_ascending(const void* value, std::string* buf) {
342
884
        auto slice = reinterpret_cast<const Slice*>(value);
343
884
        buf->append(slice->get_data(), slice->get_size());
344
884
    }
345
346
    NO_SANITIZE_UNDEFINED static void encode_ascending(const void* value, size_t index_size,
347
48
                                                       std::string* buf) {
348
48
        const Slice* slice = (const Slice*)value;
349
48
        size_t copy_size = std::min(index_size, slice->size);
350
48
        buf->append(slice->data, copy_size);
351
48
    }
352
353
0
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
354
0
        throw Exception(Status::FatalError("decode_ascending is not implemented"));
355
0
    }
356
};
357
358
template <FieldType field_type>
359
class KeyCoderTraitsForFloat {
360
public:
361
    using CppType = typename CppTypeTraits<field_type>::CppType;
362
    using UnsignedCppType = typename CppTypeTraits<field_type>::UnsignedCppType;
363
364
518
    static UnsignedCppType encode_float(CppType value) {
365
518
        return sortable_float_bits(float_to_int_bits(value));
366
518
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE12encode_floatEf
Line
Count
Source
364
259
    static UnsignedCppType encode_float(CppType value) {
365
259
        return sortable_float_bits(float_to_int_bits(value));
366
259
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE12encode_floatEd
Line
Count
Source
364
259
    static UnsignedCppType encode_float(CppType value) {
365
259
        return sortable_float_bits(float_to_int_bits(value));
366
259
    }
367
368
128
    static CppType decode_float(UnsignedCppType sortable_bits) {
369
128
        return int_bits_to_float(unsortable_float_bits(sortable_bits));
370
128
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE12decode_floatEj
Line
Count
Source
368
64
    static CppType decode_float(UnsignedCppType sortable_bits) {
369
64
        return int_bits_to_float(unsortable_float_bits(sortable_bits));
370
64
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE12decode_floatEm
Line
Count
Source
368
64
    static CppType decode_float(UnsignedCppType sortable_bits) {
369
64
        return int_bits_to_float(unsortable_float_bits(sortable_bits));
370
64
    }
371
372
    // -infinity < -100.0 < -1.0 < -0.0 < 0.0 < 1.0 < 100.0 < infinity < NaN
373
518
    static void full_encode_ascending(const void* value, std::string* buf) {
374
518
        CppType val;
375
518
        std::memcpy(&val, value, sizeof(CppType));
376
518
        UnsignedCppType sortable_val = encode_float(val);
377
518
        constexpr UnsignedCppType sign_bit = UnsignedCppType(1)
378
518
                                             << (sizeof(UnsignedCppType) * 8 - 1);
379
518
        sortable_val ^= sign_bit;
380
518
        sortable_val = to_endian<std::endian::big>(sortable_val);
381
518
        buf->append(reinterpret_cast<const char*>(&sortable_val), sizeof(UnsignedCppType));
382
518
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
373
259
    static void full_encode_ascending(const void* value, std::string* buf) {
374
259
        CppType val;
375
259
        std::memcpy(&val, value, sizeof(CppType));
376
259
        UnsignedCppType sortable_val = encode_float(val);
377
259
        constexpr UnsignedCppType sign_bit = UnsignedCppType(1)
378
259
                                             << (sizeof(UnsignedCppType) * 8 - 1);
379
259
        sortable_val ^= sign_bit;
380
259
        sortable_val = to_endian<std::endian::big>(sortable_val);
381
259
        buf->append(reinterpret_cast<const char*>(&sortable_val), sizeof(UnsignedCppType));
382
259
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE21full_encode_ascendingEPKvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
373
259
    static void full_encode_ascending(const void* value, std::string* buf) {
374
259
        CppType val;
375
259
        std::memcpy(&val, value, sizeof(CppType));
376
259
        UnsignedCppType sortable_val = encode_float(val);
377
259
        constexpr UnsignedCppType sign_bit = UnsignedCppType(1)
378
259
                                             << (sizeof(UnsignedCppType) * 8 - 1);
379
259
        sortable_val ^= sign_bit;
380
259
        sortable_val = to_endian<std::endian::big>(sortable_val);
381
259
        buf->append(reinterpret_cast<const char*>(&sortable_val), sizeof(UnsignedCppType));
382
259
    }
383
384
0
    static void encode_ascending(const void* value, size_t index_size, std::string* buf) {
385
0
        full_encode_ascending(value, buf);
386
0
    }
Unexecuted instantiation: _ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE16encode_ascendingEPKvmPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
387
388
128
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
389
128
        if (encoded_key->size < sizeof(UnsignedCppType)) {
390
0
            return Status::InvalidArgument(absl::Substitute("Key too short, need=$0 vs real=$1",
391
0
                                                            sizeof(UnsignedCppType),
392
0
                                                            encoded_key->size));
393
0
        }
394
128
        UnsignedCppType sortable_val;
395
128
        std::memcpy(&sortable_val, encoded_key->data, sizeof(UnsignedCppType));
396
128
        sortable_val = to_endian<std::endian::big>(sortable_val);
397
128
        constexpr UnsignedCppType sign_bit = UnsignedCppType(1)
398
128
                                             << (sizeof(UnsignedCppType) * 8 - 1);
399
128
        sortable_val ^= sign_bit;
400
128
        CppType val = decode_float(sortable_val);
401
128
        std::memcpy(cell_ptr, &val, sizeof(CppType));
402
128
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
403
128
        return Status::OK();
404
128
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
388
64
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
389
64
        if (encoded_key->size < sizeof(UnsignedCppType)) {
390
0
            return Status::InvalidArgument(absl::Substitute("Key too short, need=$0 vs real=$1",
391
0
                                                            sizeof(UnsignedCppType),
392
0
                                                            encoded_key->size));
393
0
        }
394
64
        UnsignedCppType sortable_val;
395
64
        std::memcpy(&sortable_val, encoded_key->data, sizeof(UnsignedCppType));
396
64
        sortable_val = to_endian<std::endian::big>(sortable_val);
397
64
        constexpr UnsignedCppType sign_bit = UnsignedCppType(1)
398
64
                                             << (sizeof(UnsignedCppType) * 8 - 1);
399
64
        sortable_val ^= sign_bit;
400
64
        CppType val = decode_float(sortable_val);
401
64
        std::memcpy(cell_ptr, &val, sizeof(CppType));
402
64
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
403
64
        return Status::OK();
404
64
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE16decode_ascendingEPNS_5SliceEmPh
Line
Count
Source
388
64
    static Status decode_ascending(Slice* encoded_key, size_t index_size, uint8_t* cell_ptr) {
389
64
        if (encoded_key->size < sizeof(UnsignedCppType)) {
390
0
            return Status::InvalidArgument(absl::Substitute("Key too short, need=$0 vs real=$1",
391
0
                                                            sizeof(UnsignedCppType),
392
0
                                                            encoded_key->size));
393
0
        }
394
64
        UnsignedCppType sortable_val;
395
64
        std::memcpy(&sortable_val, encoded_key->data, sizeof(UnsignedCppType));
396
64
        sortable_val = to_endian<std::endian::big>(sortable_val);
397
64
        constexpr UnsignedCppType sign_bit = UnsignedCppType(1)
398
64
                                             << (sizeof(UnsignedCppType) * 8 - 1);
399
64
        sortable_val ^= sign_bit;
400
64
        CppType val = decode_float(sortable_val);
401
64
        std::memcpy(cell_ptr, &val, sizeof(CppType));
402
64
        encoded_key->remove_prefix(sizeof(UnsignedCppType));
403
64
        return Status::OK();
404
64
    }
405
406
private:
407
518
    static UnsignedCppType float_to_int_bits(CppType value) {
408
518
        if (std::isnan(value)) {
409
46
            if constexpr (std::is_same_v<CppType, float>) {
410
23
                return 0x7FC00000U;
411
23
            } else {
412
23
                return 0x7FF8000000000000ULL;
413
23
            }
414
46
        }
415
416
0
        UnsignedCppType result;
417
518
        std::memcpy(&result, &value, sizeof(CppType));
418
518
        return result;
419
518
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE17float_to_int_bitsEf
Line
Count
Source
407
259
    static UnsignedCppType float_to_int_bits(CppType value) {
408
259
        if (std::isnan(value)) {
409
23
            if constexpr (std::is_same_v<CppType, float>) {
410
23
                return 0x7FC00000U;
411
            } else {
412
                return 0x7FF8000000000000ULL;
413
            }
414
23
        }
415
416
0
        UnsignedCppType result;
417
259
        std::memcpy(&result, &value, sizeof(CppType));
418
259
        return result;
419
259
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE17float_to_int_bitsEd
Line
Count
Source
407
259
    static UnsignedCppType float_to_int_bits(CppType value) {
408
259
        if (std::isnan(value)) {
409
            if constexpr (std::is_same_v<CppType, float>) {
410
                return 0x7FC00000U;
411
23
            } else {
412
23
                return 0x7FF8000000000000ULL;
413
23
            }
414
23
        }
415
416
0
        UnsignedCppType result;
417
259
        std::memcpy(&result, &value, sizeof(CppType));
418
259
        return result;
419
259
    }
420
421
646
    static UnsignedCppType sortable_float_bits(UnsignedCppType bits) {
422
646
        constexpr int32_t shift = sizeof(UnsignedCppType) * 8 - 1;
423
646
        constexpr UnsignedCppType sign_bit = static_cast<UnsignedCppType>(1) << shift;
424
646
        if ((bits & sign_bit) != 0) {
425
194
            return bits ^ (sign_bit - 1);
426
452
        } else {
427
452
            return bits;
428
452
        }
429
646
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE19sortable_float_bitsEj
Line
Count
Source
421
323
    static UnsignedCppType sortable_float_bits(UnsignedCppType bits) {
422
323
        constexpr int32_t shift = sizeof(UnsignedCppType) * 8 - 1;
423
323
        constexpr UnsignedCppType sign_bit = static_cast<UnsignedCppType>(1) << shift;
424
323
        if ((bits & sign_bit) != 0) {
425
97
            return bits ^ (sign_bit - 1);
426
226
        } else {
427
226
            return bits;
428
226
        }
429
323
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE19sortable_float_bitsEm
Line
Count
Source
421
323
    static UnsignedCppType sortable_float_bits(UnsignedCppType bits) {
422
323
        constexpr int32_t shift = sizeof(UnsignedCppType) * 8 - 1;
423
323
        constexpr UnsignedCppType sign_bit = static_cast<UnsignedCppType>(1) << shift;
424
323
        if ((bits & sign_bit) != 0) {
425
97
            return bits ^ (sign_bit - 1);
426
226
        } else {
427
226
            return bits;
428
226
        }
429
323
    }
430
431
128
    static CppType int_bits_to_float(UnsignedCppType bits) {
432
128
        CppType result;
433
128
        std::memcpy(&result, &bits, sizeof(CppType));
434
128
        return result;
435
128
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE17int_bits_to_floatEj
Line
Count
Source
431
64
    static CppType int_bits_to_float(UnsignedCppType bits) {
432
64
        CppType result;
433
64
        std::memcpy(&result, &bits, sizeof(CppType));
434
64
        return result;
435
64
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE17int_bits_to_floatEm
Line
Count
Source
431
64
    static CppType int_bits_to_float(UnsignedCppType bits) {
432
64
        CppType result;
433
64
        std::memcpy(&result, &bits, sizeof(CppType));
434
64
        return result;
435
64
    }
436
437
128
    static UnsignedCppType unsortable_float_bits(UnsignedCppType sortable_bits) {
438
128
        return sortable_float_bits(sortable_bits);
439
128
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE10EE21unsortable_float_bitsEj
Line
Count
Source
437
64
    static UnsignedCppType unsortable_float_bits(UnsignedCppType sortable_bits) {
438
64
        return sortable_float_bits(sortable_bits);
439
64
    }
_ZN5doris22KeyCoderTraitsForFloatILNS_9FieldTypeE11EE21unsortable_float_bitsEm
Line
Count
Source
437
64
    static UnsignedCppType unsortable_float_bits(UnsignedCppType sortable_bits) {
438
64
        return sortable_float_bits(sortable_bits);
439
64
    }
440
};
441
442
template <>
443
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_FLOAT>
444
        : public KeyCoderTraitsForFloat<FieldType::OLAP_FIELD_TYPE_FLOAT> {};
445
446
template <>
447
class KeyCoderTraits<FieldType::OLAP_FIELD_TYPE_DOUBLE>
448
        : public KeyCoderTraitsForFloat<FieldType::OLAP_FIELD_TYPE_DOUBLE> {};
449
450
} // namespace doris