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