Coverage Report

Created: 2026-04-13 08:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/format_common.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 "core/types.h"
21
#include "exec/common/int_exp.h"
22
23
namespace doris {
24
25
struct DecimalScaleParams {
26
    enum ScaleType {
27
        NOT_INIT,
28
        NO_SCALE,
29
        SCALE_UP,
30
        SCALE_DOWN,
31
    };
32
    ScaleType scale_type = ScaleType::NOT_INIT;
33
    int64_t scale_factor = 1;
34
35
    template <PrimitiveType DecimalPrimitiveType>
36
    static inline constexpr typename PrimitiveTypeTraits<DecimalPrimitiveType>::CppType::NativeType
37
5.70k
    get_scale_factor(int32_t n) {
38
5.70k
        if constexpr (DecimalPrimitiveType == TYPE_DECIMAL32) {
39
3.35k
            return common::exp10_i32(n);
40
3.35k
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL64) {
41
1.94k
            return common::exp10_i64(n);
42
1.94k
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMALV2) {
43
0
            return common::exp10_i128(n);
44
405
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL128I) {
45
405
            return common::exp10_i128(n);
46
405
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL256) {
47
0
            return common::exp10_i256(n);
48
        } else {
49
            static_assert(!sizeof(typename PrimitiveTypeTraits<DecimalPrimitiveType>::CppType),
50
                          "All types must be matched with if constexpr.");
51
        }
52
5.70k
    }
_ZN5doris18DecimalScaleParams16get_scale_factorILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEi
Line
Count
Source
37
3.35k
    get_scale_factor(int32_t n) {
38
3.35k
        if constexpr (DecimalPrimitiveType == TYPE_DECIMAL32) {
39
3.35k
            return common::exp10_i32(n);
40
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL64) {
41
            return common::exp10_i64(n);
42
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMALV2) {
43
            return common::exp10_i128(n);
44
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL128I) {
45
            return common::exp10_i128(n);
46
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL256) {
47
            return common::exp10_i256(n);
48
        } else {
49
            static_assert(!sizeof(typename PrimitiveTypeTraits<DecimalPrimitiveType>::CppType),
50
                          "All types must be matched with if constexpr.");
51
        }
52
3.35k
    }
_ZN5doris18DecimalScaleParams16get_scale_factorILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEi
Line
Count
Source
37
1.94k
    get_scale_factor(int32_t n) {
38
        if constexpr (DecimalPrimitiveType == TYPE_DECIMAL32) {
39
            return common::exp10_i32(n);
40
1.94k
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL64) {
41
1.94k
            return common::exp10_i64(n);
42
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMALV2) {
43
            return common::exp10_i128(n);
44
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL128I) {
45
            return common::exp10_i128(n);
46
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL256) {
47
            return common::exp10_i256(n);
48
        } else {
49
            static_assert(!sizeof(typename PrimitiveTypeTraits<DecimalPrimitiveType>::CppType),
50
                          "All types must be matched with if constexpr.");
51
        }
52
1.94k
    }
Unexecuted instantiation: _ZN5doris18DecimalScaleParams16get_scale_factorILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEi
_ZN5doris18DecimalScaleParams16get_scale_factorILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEi
Line
Count
Source
37
405
    get_scale_factor(int32_t n) {
38
        if constexpr (DecimalPrimitiveType == TYPE_DECIMAL32) {
39
            return common::exp10_i32(n);
40
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL64) {
41
            return common::exp10_i64(n);
42
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMALV2) {
43
            return common::exp10_i128(n);
44
405
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL128I) {
45
405
            return common::exp10_i128(n);
46
        } else if constexpr (DecimalPrimitiveType == TYPE_DECIMAL256) {
47
            return common::exp10_i256(n);
48
        } else {
49
            static_assert(!sizeof(typename PrimitiveTypeTraits<DecimalPrimitiveType>::CppType),
50
                          "All types must be matched with if constexpr.");
51
        }
52
405
    }
Unexecuted instantiation: _ZN5doris18DecimalScaleParams16get_scale_factorILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEi
53
};
54
55
/**
56
 * Key-Value Cache Helper.
57
 *
58
 * It store a object instance global. User can invoke get method by key and a
59
 * object creator callback. If there is a instance stored in cache, then it will
60
 * return a void pointer of it, otherwise, it will invoke creator callback, create
61
 * a new instance store global, and return it.
62
 *
63
 * The stored objects will be deleted when deconstructing, so user do not need to
64
 * delete the returned pointer.
65
 *
66
 * User can invoke erase method by key to delete data.
67
 *
68
 * @tparam KType is the key type
69
 */
70
template <typename KType>
71
class KVCache {
72
public:
73
2.80k
    KVCache() = default;
74
75
2.80k
    ~KVCache() {
76
2.80k
        for (auto& kv : _storage) {
77
0
            _delete_fn[kv.first](kv.second);
78
0
        }
79
2.80k
    }
80
81
    void erase(const KType& key) {
82
        std::lock_guard<std::mutex> lock(_lock);
83
        auto it = _storage.find(key);
84
        if (it != _storage.end()) {
85
            _delete_fn[key](_storage[key]);
86
            _storage.erase(key);
87
            _delete_fn.erase(key);
88
        }
89
    }
90
91
    template <class T>
92
0
    T* get(const KType& key, const std::function<T*()> create_func) {
93
0
        std::lock_guard<std::mutex> lock(_lock);
94
0
        auto it = _storage.find(key);
95
0
        if (it != _storage.end()) {
96
0
            return reinterpret_cast<T*>(it->second);
97
0
        } else {
98
0
            T* rawPtr = create_func();
99
0
            if (rawPtr != nullptr) {
100
0
                _delete_fn[key] = [](void* obj) { delete reinterpret_cast<T*>(obj); };
Unexecuted instantiation: _ZZN5doris7KVCacheINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE3getIN5phmap22parallel_flat_hash_mapIS6_St10unique_ptrISt6vectorIlSaIlEESt14default_deleteISE_EESt4hashIS6_ESt8equal_toIvESaISt4pairIKS6_SH_EELm8ESt5mutexEEEEPT_RSN_St8functionIFST_vEEENKUlPvE_clESY_
Unexecuted instantiation: _ZZN5doris7KVCacheINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE3getISt6vectorIlSaIlEEEEPT_RKS6_St8functionIFSD_vEEENKUlPvE_clESJ_
101
0
                _storage[key] = rawPtr;
102
0
            }
103
0
            return rawPtr;
104
0
        }
105
0
    }
Unexecuted instantiation: _ZN5doris7KVCacheINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE3getIN5phmap22parallel_flat_hash_mapIS6_St10unique_ptrISt6vectorIlSaIlEESt14default_deleteISE_EESt4hashIS6_ESt8equal_toIvESaISt4pairIKS6_SH_EELm8ESt5mutexEEEEPT_RSN_St8functionIFST_vEE
Unexecuted instantiation: _ZN5doris7KVCacheINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE3getISt6vectorIlSaIlEEEEPT_RKS6_St8functionIFSD_vEE
106
107
private:
108
    using DeleteFn = void (*)(void*);
109
110
    std::mutex _lock;
111
    std::unordered_map<KType, DeleteFn> _delete_fn;
112
    std::unordered_map<KType, void*> _storage;
113
};
114
115
class ShardedKVCache {
116
public:
117
2.65k
    ShardedKVCache(uint32_t num_shards) : _num_shards(num_shards) {
118
2.65k
        _shards = new (std::nothrow) KVCache<std::string>*[_num_shards];
119
5.45k
        for (uint32_t i = 0; i < _num_shards; i++) {
120
2.80k
            _shards[i] = new KVCache<std::string>();
121
2.80k
        }
122
2.65k
    }
123
124
2.65k
    ~ShardedKVCache() {
125
5.45k
        for (uint32_t i = 0; i < _num_shards; i++) {
126
2.80k
            delete _shards[i];
127
2.80k
        }
128
2.65k
        delete[] _shards;
129
2.65k
    }
130
131
    template <class T>
132
0
    T* get(const std::string& key, const std::function<T*()> create_func) {
133
0
        return _shards[_get_idx(key)]->get(key, create_func);
134
0
    }
Unexecuted instantiation: _ZN5doris14ShardedKVCache3getIN5phmap22parallel_flat_hash_mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt10unique_ptrISt6vectorIlSaIlEESt14default_deleteISD_EESt4hashIS9_ESt8equal_toIvESaISt4pairIKS9_SG_EELm8ESt5mutexEEEEPT_RSM_St8functionIFSS_vEE
Unexecuted instantiation: _ZN5doris14ShardedKVCache3getISt6vectorIlSaIlEEEEPT_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt8functionIFS6_vEE
135
136
private:
137
0
    uint32_t _get_idx(const std::string& key) const {
138
0
        return (uint32_t)std::hash<std::string>()(key) % _num_shards;
139
0
    }
140
141
    uint32_t _num_shards;
142
    KVCache<std::string>** _shards = nullptr;
143
};
144
145
} // namespace doris