Lbug C++ API
Loading...
Searching...
No Matches
type_utils.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5#include "assert.h"
6#include "blob.h"
7#include "date_t.h"
8#include "int128_t.h"
9#include "interval_t.h"
10#include "ku_string.h"
11#include "timestamp_t.h"
12#include "types.h"
13#include "uint128_t.h"
14#include "uuid.h"
15#include "value_vector.h"
16
17namespace lbug {
18namespace common {
19
20class ValueVector;
21
22template<class... Funcs>
23struct overload : Funcs... {
24 explicit overload(Funcs... funcs) : Funcs(funcs)... {}
25 using Funcs::operator()...;
26};
27
28class TypeUtils {
29public:
30 template<typename Func, typename... Types, size_t... indices>
31 static void paramPackForEachHelper(const Func& func, std::index_sequence<indices...>,
32 Types&&... values) {
33 ((func(indices, values)), ...);
34 }
35
36 template<typename Func, typename... Types>
37 static void paramPackForEach(const Func& func, Types&&... values) {
38 paramPackForEachHelper(func, std::index_sequence_for<Types...>(),
39 std::forward<Types>(values)...);
40 }
41
42 static std::string entryToString(const LogicalType& dataType, const uint8_t* value,
43 ValueVector* vector);
44
45 template<typename T>
46 static inline std::string toString(const T& val, void* /*valueVector*/ = nullptr) {
47 if constexpr (std::is_same_v<T, std::string>) {
48 return val;
49 } else if constexpr (std::is_same_v<T, ku_string_t>) {
50 return val.getAsString();
51 } else {
52 static_assert(std::is_same<T, int64_t>::value || std::is_same<T, int32_t>::value ||
53 std::is_same<T, int16_t>::value || std::is_same<T, int8_t>::value ||
54 std::is_same<T, uint64_t>::value || std::is_same<T, uint32_t>::value ||
55 std::is_same<T, uint16_t>::value || std::is_same<T, uint8_t>::value ||
56 std::is_same<T, double>::value || std::is_same<T, float>::value);
57 return std::to_string(val);
58 }
59 }
60 static std::string nodeToString(const struct_entry_t& val, ValueVector* vector);
61 static std::string relToString(const struct_entry_t& val, ValueVector* vector);
62
63 static inline void encodeOverflowPtr(uint64_t& overflowPtr, page_idx_t pageIdx,
64 uint32_t pageOffset) {
65 memcpy(&overflowPtr, &pageIdx, 4);
66 memcpy(((uint8_t*)&overflowPtr) + 4, &pageOffset, 4);
67 }
68 static inline void decodeOverflowPtr(uint64_t overflowPtr, page_idx_t& pageIdx,
69 uint32_t& pageOffset) {
70 pageIdx = 0;
71 memcpy(&pageIdx, &overflowPtr, 4);
72 memcpy(&pageOffset, ((uint8_t*)&overflowPtr) + 4, 4);
73 }
74
75 template<typename T>
77 if constexpr (std::is_same_v<T, int64_t>) {
79 } else if constexpr (std::is_same_v<T, int32_t>) {
81 } else if constexpr (std::is_same_v<T, int16_t>) {
83 } else if constexpr (std::is_same_v<T, int8_t>) {
85 } else if constexpr (std::is_same_v<T, uint64_t>) {
87 } else if constexpr (std::is_same_v<T, uint32_t>) {
89 } else if constexpr (std::is_same_v<T, uint16_t>) {
91 } else if constexpr (std::is_same_v<T, uint8_t>) {
93 } else if constexpr (std::is_same_v<T, float>) {
95 } else if constexpr (std::is_same_v<T, double>) {
97 } else if constexpr (std::is_same_v<T, int128_t>) {
99 } else if constexpr (std::is_same_v<T, interval_t>) {
101 } else if constexpr (std::is_same_v<T, uint128_t>) {
103 } else if constexpr (std::same_as<T, ku_string_t> || std::same_as<T, std::string> ||
104 std::same_as<T, std::string_view>) {
106 } else {
108 }
109 }
110
111 /*
112 * TypeUtils::visit can be used to call generic code on all or some Logical and Physical type
113 * variants with access to type information.
114 *
115 * E.g.
116 *
117 * std::string result;
118 * visit(dataType, [&]<typename T>(T) {
119 * if constexpr(std::is_same_v<T, ku_string_t>()) {
120 * result = vector->getValue<ku_string_t>(0).getAsString();
121 * } else if (std::integral<T>) {
122 * result = std::to_string(vector->getValue<T>(0));
123 * } else {
124 * KU_UNREACHABLE;
125 * }
126 * });
127 *
128 * or
129 * std::string result;
130 * visit(dataType,
131 * [&](ku_string_t) {
132 * result = vector->getValue<ku_string_t>(0);
133 * },
134 * [&]<std::integral T>(T) {
135 * result = std::to_string(vector->getValue<T>(0));
136 * },
137 * [](auto) { KU_UNREACHABLE; }
138 * );
139 *
140 * Note that when multiple functions are provided, at least one function must match all data
141 * types.
142 *
143 * Also note that implicit conversions may occur with the multi-function variant
144 * if you don't include a generic auto function to cover types which aren't explicitly included.
145 * See https://en.cppreference.com/w/cpp/utility/variant/visit
146 */
147 template<typename... Fs>
148 static inline auto visit(const LogicalType& dataType, Fs... funcs) {
149 // Note: arguments are used only for type deduction and have no meaningful value.
150 // They should be optimized out by the compiler
151 auto func = overload(funcs...);
152 switch (dataType.getLogicalTypeID()) {
153 /* NOLINTBEGIN(bugprone-branch-clone)*/
155 return func(int8_t());
157 return func(uint8_t());
159 return func(int16_t());
161 return func(uint16_t());
163 return func(int32_t());
165 return func(uint32_t());
168 return func(int64_t());
170 return func(uint64_t());
172 return func(bool());
174 return func(int128_t());
176 return func(double());
178 return func(float());
180 switch (dataType.getPhysicalType()) {
182 return func(int16_t());
184 return func(int32_t());
186 return func(int64_t());
188 return func(int128_t());
189 default:
191 }
193 return func(interval_t());
195 return func(internalID_t());
197 return func(uint128_t());
199 return func(ku_string_t());
201 return func(date_t());
203 return func(timestamp_ns_t());
205 return func(timestamp_ms_t());
207 return func(timestamp_sec_t());
209 return func(timestamp_tz_t());
211 return func(timestamp_t());
213 return func(blob_t());
215 return func(ku_uuid_t());
218 return func(list_entry_t());
220 return func(map_entry_t());
225 return func(struct_entry_t());
227 return func(union_entry_t());
228 /* NOLINTEND(bugprone-branch-clone)*/
229 default:
230 // Unsupported type
232 }
233 }
234
235 template<typename... Fs>
236 static inline auto visit(PhysicalTypeID dataType, Fs&&... funcs) {
237 // Note: arguments are used only for type deduction and have no meaningful value.
238 // They should be optimized out by the compiler
239 auto func = overload(funcs...);
240 switch (dataType) {
241 /* NOLINTBEGIN(bugprone-branch-clone)*/
243 return func(int8_t());
245 return func(uint8_t());
247 return func(int16_t());
249 return func(uint16_t());
251 return func(int32_t());
253 return func(uint32_t());
255 return func(int64_t());
257 return func(uint64_t());
259 return func(bool());
261 return func(int128_t());
263 return func(double());
265 return func(float());
267 return func(interval_t());
269 return func(internalID_t());
271 return func(uint128_t());
273 return func(ku_string_t());
276 return func(list_entry_t());
278 return func(struct_entry_t());
279 /* NOLINTEND(bugprone-branch-clone)*/
284 // Unsupported type
286 // Needed for return type deduction to work
287 return func(uint8_t());
288 default:
290 }
291 }
292};
293
294// Forward declaration of template specializations.
295template<>
296std::string TypeUtils::toString(const int128_t& val, void* valueVector);
297template<>
298std::string TypeUtils::toString(const uint128_t& val, void* valueVector);
299template<>
300std::string TypeUtils::toString(const bool& val, void* valueVector);
301template<>
302std::string TypeUtils::toString(const internalID_t& val, void* valueVector);
303template<>
304std::string TypeUtils::toString(const date_t& val, void* valueVector);
305template<>
306std::string TypeUtils::toString(const timestamp_ns_t& val, void* valueVector);
307template<>
308std::string TypeUtils::toString(const timestamp_ms_t& val, void* valueVector);
309template<>
310std::string TypeUtils::toString(const timestamp_sec_t& val, void* valueVector);
311template<>
312std::string TypeUtils::toString(const timestamp_tz_t& val, void* valueVector);
313template<>
314std::string TypeUtils::toString(const timestamp_t& val, void* valueVector);
315template<>
316std::string TypeUtils::toString(const interval_t& val, void* valueVector);
317template<>
318std::string TypeUtils::toString(const ku_string_t& val, void* valueVector);
319template<>
320std::string TypeUtils::toString(const blob_t& val, void* valueVector);
321template<>
322std::string TypeUtils::toString(const ku_uuid_t& val, void* valueVector);
323template<>
324std::string TypeUtils::toString(const list_entry_t& val, void* valueVector);
325template<>
326std::string TypeUtils::toString(const map_entry_t& val, void* valueVector);
327template<>
328std::string TypeUtils::toString(const struct_entry_t& val, void* valueVector);
329template<>
330std::string TypeUtils::toString(const union_entry_t& val, void* valueVector);
331
332} // namespace common
333} // namespace lbug
#define KU_UNREACHABLE
Definition assert.h:30
Definition types.h:265
PhysicalTypeID getPhysicalType() const
Definition types.h:292
LogicalTypeID getLogicalTypeID() const
Definition types.h:288
Definition type_utils.h:28
static auto visit(PhysicalTypeID dataType, Fs &&... funcs)
Definition type_utils.h:236
static void decodeOverflowPtr(uint64_t overflowPtr, page_idx_t &pageIdx, uint32_t &pageOffset)
Definition type_utils.h:68
static auto visit(const LogicalType &dataType, Fs... funcs)
Definition type_utils.h:148
static std::string entryToString(const LogicalType &dataType, const uint8_t *value, ValueVector *vector)
static std::string relToString(const struct_entry_t &val, ValueVector *vector)
static void paramPackForEachHelper(const Func &func, std::index_sequence< indices... >, Types &&... values)
Definition type_utils.h:31
static void paramPackForEach(const Func &func, Types &&... values)
Definition type_utils.h:37
static void encodeOverflowPtr(uint64_t &overflowPtr, page_idx_t pageIdx, uint32_t pageOffset)
Definition type_utils.h:63
static std::string nodeToString(const struct_entry_t &val, ValueVector *vector)
static std::string toString(const T &val, void *=nullptr)
Definition type_utils.h:46
static constexpr common::PhysicalTypeID getPhysicalTypeIDForType()
Definition type_utils.h:76
Definition value_vector.h:21
Definition array_utils.h:7
uint32_t page_idx_t
Definition types.h:33
struct LBUG_API int128_t
Definition int128_t.h:17
PhysicalTypeID
Definition types.h:230
@ UINT32
Definition types.h:239
@ INTERVAL
Definition types.h:245
@ ALP_EXCEPTION_DOUBLE
Definition types.h:248
@ LIST
Definition types.h:253
@ UINT16
Definition types.h:240
@ INT64
Definition types.h:234
@ INT16
Definition types.h:236
@ STRING
Definition types.h:252
@ INT32
Definition types.h:235
@ UINT64
Definition types.h:238
@ ANY
Definition types.h:232
@ INTERNAL_ID
Definition types.h:246
@ POINTER
Definition types.h:256
@ BOOL
Definition types.h:233
@ STRUCT
Definition types.h:255
@ INT128
Definition types.h:242
@ ARRAY
Definition types.h:254
@ ALP_EXCEPTION_FLOAT
Definition types.h:247
@ UINT128
Definition types.h:249
@ FLOAT
Definition types.h:244
@ UINT8
Definition types.h:241
@ INT8
Definition types.h:237
@ DOUBLE
Definition types.h:243
@ NODE
Definition types.h:186
@ DECIMAL
Definition types.h:212
@ BLOB
Definition types.h:217
@ UINT32
Definition types.h:199
@ INTERVAL
Definition types.h:211
@ LIST
Definition types.h:219
@ RECURSIVE_REL
Definition types.h:188
@ TIMESTAMP_NS
Definition types.h:209
@ UINT16
Definition types.h:200
@ INT64
Definition types.h:194
@ UUID
Definition types.h:226
@ MAP
Definition types.h:222
@ INT16
Definition types.h:196
@ DATE
Definition types.h:205
@ STRING
Definition types.h:216
@ INT32
Definition types.h:195
@ UINT64
Definition types.h:198
@ SERIAL
Definition types.h:191
@ TIMESTAMP
Definition types.h:206
@ INTERNAL_ID
Definition types.h:213
@ BOOL
Definition types.h:193
@ STRUCT
Definition types.h:221
@ TIMESTAMP_TZ
Definition types.h:210
@ INT128
Definition types.h:202
@ ARRAY
Definition types.h:220
@ UINT128
Definition types.h:214
@ TIMESTAMP_MS
Definition types.h:208
@ TIMESTAMP_SEC
Definition types.h:207
@ FLOAT
Definition types.h:204
@ REL
Definition types.h:187
@ UNION
Definition types.h:223
@ UINT8
Definition types.h:201
@ INT8
Definition types.h:197
@ DOUBLE
Definition types.h:203
Definition array_utils.h:7
Definition blob.h:8
Definition date_t.h:16
Definition int128_t.h:21
Definition types.h:90
Definition interval_t.h:35
Definition ku_string.h:12
Definition uuid.h:16
Definition types.h:114
Definition types.h:126
Definition type_utils.h:23
overload(Funcs... funcs)
Definition type_utils.h:24
Definition types.h:122
Definition timestamp_t.h:49
Definition timestamp_t.h:46
Definition timestamp_t.h:52
Definition timestamp_t.h:10
Definition timestamp_t.h:43
Definition uint128_t.h:15
Definition types.h:130