Coverage Report

Created: 2026-03-31 12:22

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