Kùzu 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 "uuid.h"
14
15namespace kuzu {
16namespace common {
17
18class ValueVector;
19
20template<class... Funcs>
21struct overload : Funcs... {
22 explicit overload(Funcs... funcs) : Funcs(funcs)... {}
23 using Funcs::operator()...;
24};
25
26class TypeUtils {
27public:
28 template<typename Func, typename... Types, size_t... indices>
29 static void paramPackForEachHelper(const Func& func, std::index_sequence<indices...>,
30 Types&&... values) {
31 ((func(indices, values)), ...);
32 }
33
34 template<typename Func, typename... Types>
35 static void paramPackForEach(const Func& func, Types&&... values) {
36 paramPackForEachHelper(func, std::index_sequence_for<Types...>(),
37 std::forward<Types>(values)...);
38 }
39
40 static std::string entryToString(const LogicalType& dataType, const uint8_t* value,
41 ValueVector* vector);
42
43 template<typename T>
44 static inline std::string toString(const T& val, void* /*valueVector*/ = nullptr) {
45 if constexpr (std::is_same_v<T, std::string>) {
46 return val;
47 } else if constexpr (std::is_same_v<T, ku_string_t>) {
48 return val.getAsString();
49 } else {
50 static_assert(std::is_same<T, int64_t>::value || std::is_same<T, int32_t>::value ||
51 std::is_same<T, int16_t>::value || std::is_same<T, int8_t>::value ||
52 std::is_same<T, uint64_t>::value || std::is_same<T, uint32_t>::value ||
53 std::is_same<T, uint16_t>::value || std::is_same<T, uint8_t>::value ||
54 std::is_same<T, double>::value || std::is_same<T, float>::value);
55 return std::to_string(val);
56 }
57 }
58 static std::string nodeToString(const struct_entry_t& val, ValueVector* vector);
59 static std::string relToString(const struct_entry_t& val, ValueVector* vector);
60
61 static inline void encodeOverflowPtr(uint64_t& overflowPtr, page_idx_t pageIdx,
62 uint32_t pageOffset) {
63 memcpy(&overflowPtr, &pageIdx, 4);
64 memcpy(((uint8_t*)&overflowPtr) + 4, &pageOffset, 4);
65 }
66 static inline void decodeOverflowPtr(uint64_t overflowPtr, page_idx_t& pageIdx,
67 uint32_t& pageOffset) {
68 pageIdx = 0;
69 memcpy(&pageIdx, &overflowPtr, 4);
70 memcpy(&pageOffset, ((uint8_t*)&overflowPtr) + 4, 4);
71 }
72
73 template<typename T>
75 if constexpr (std::is_same_v<T, int64_t>) {
77 } else if constexpr (std::is_same_v<T, int32_t>) {
79 } else if constexpr (std::is_same_v<T, int16_t>) {
81 } else if constexpr (std::is_same_v<T, int8_t>) {
83 } else if constexpr (std::is_same_v<T, uint64_t>) {
85 } else if constexpr (std::is_same_v<T, uint32_t>) {
87 } else if constexpr (std::is_same_v<T, uint16_t>) {
89 } else if constexpr (std::is_same_v<T, uint8_t>) {
91 } else if constexpr (std::is_same_v<T, float>) {
93 } else if constexpr (std::is_same_v<T, double>) {
95 } else if constexpr (std::is_same_v<T, int128_t>) {
97 } else if constexpr (std::is_same_v<T, interval_t>) {
99 } else if constexpr (std::same_as<T, ku_string_t> || std::same_as<T, std::string> ||
100 std::same_as<T, std::string_view>) {
102 } else {
104 }
105 }
106
107 /*
108 * TypeUtils::visit can be used to call generic code on all or some Logical and Physical type
109 * variants with access to type information.
110 *
111 * E.g.
112 *
113 * std::string result;
114 * visit(dataType, [&]<typename T>(T) {
115 * if constexpr(std::is_same_v<T, ku_string_t>()) {
116 * result = vector->getValue<ku_string_t>(0).getAsString();
117 * } else if (std::integral<T>) {
118 * result = std::to_string(vector->getValue<T>(0));
119 * } else {
120 * KU_UNREACHABLE;
121 * }
122 * });
123 *
124 * or
125 * std::string result;
126 * visit(dataType,
127 * [&](ku_string_t) {
128 * result = vector->getValue<ku_string_t>(0);
129 * },
130 * [&]<std::integral T>(T) {
131 * result = std::to_string(vector->getValue<T>(0));
132 * },
133 * [](auto) { KU_UNREACHABLE; }
134 * );
135 *
136 * Note that when multiple functions are provided, at least one function must match all data
137 * types.
138 *
139 * Also note that implicit conversions may occur with the multi-function variant
140 * if you don't include a generic auto function to cover types which aren't explicitly included.
141 * See https://en.cppreference.com/w/cpp/utility/variant/visit
142 */
143 template<typename... Fs>
144 static inline auto visit(const LogicalType& dataType, Fs... funcs) {
145 // Note: arguments are used only for type deduction and have no meaningful value.
146 // They should be optimized out by the compiler
147 auto func = overload(funcs...);
148 switch (dataType.getLogicalTypeID()) {
149 /* NOLINTBEGIN(bugprone-branch-clone)*/
151 return func(int8_t());
153 return func(uint8_t());
155 return func(int16_t());
157 return func(uint16_t());
159 return func(int32_t());
161 return func(uint32_t());
164 return func(int64_t());
166 return func(uint64_t());
168 return func(bool());
170 return func(int128_t());
172 return func(double());
174 return func(float());
176 switch (dataType.getPhysicalType()) {
178 return func(int16_t());
180 return func(int32_t());
182 return func(int64_t());
184 return func(int128_t());
185 default:
187 }
189 return func(interval_t());
191 return func(internalID_t());
193 return func(ku_string_t());
195 return func(date_t());
197 return func(timestamp_ns_t());
199 return func(timestamp_ms_t());
201 return func(timestamp_sec_t());
203 return func(timestamp_tz_t());
205 return func(timestamp_t());
207 return func(blob_t());
209 return func(ku_uuid_t());
212 return func(list_entry_t());
214 return func(map_entry_t());
219 return func(struct_entry_t());
221 return func(union_entry_t());
222 /* NOLINTEND(bugprone-branch-clone)*/
223 default:
224 // Unsupported type
226 }
227 }
228
229 template<typename... Fs>
230 static inline auto visit(PhysicalTypeID dataType, Fs&&... funcs) {
231 // Note: arguments are used only for type deduction and have no meaningful value.
232 // They should be optimized out by the compiler
233 auto func = overload(funcs...);
234 switch (dataType) {
235 /* NOLINTBEGIN(bugprone-branch-clone)*/
237 return func(int8_t());
239 return func(uint8_t());
241 return func(int16_t());
243 return func(uint16_t());
245 return func(int32_t());
247 return func(uint32_t());
249 return func(int64_t());
251 return func(uint64_t());
253 return func(bool());
255 return func(int128_t());
257 return func(double());
259 return func(float());
261 return func(interval_t());
263 return func(internalID_t());
265 return func(ku_string_t());
268 return func(list_entry_t());
270 return func(struct_entry_t());
271 /* NOLINTEND(bugprone-branch-clone)*/
276 // Unsupported type
278 // Needed for return type deduction to work
279 return func(uint8_t());
280 default:
282 }
283 }
284};
285
286// Forward declaration of template specializations.
287template<>
288std::string TypeUtils::toString(const int128_t& val, void* valueVector);
289template<>
290std::string TypeUtils::toString(const bool& val, void* valueVector);
291template<>
292std::string TypeUtils::toString(const internalID_t& val, void* valueVector);
293template<>
294std::string TypeUtils::toString(const date_t& val, void* valueVector);
295template<>
296std::string TypeUtils::toString(const timestamp_ns_t& val, void* valueVector);
297template<>
298std::string TypeUtils::toString(const timestamp_ms_t& val, void* valueVector);
299template<>
300std::string TypeUtils::toString(const timestamp_sec_t& val, void* valueVector);
301template<>
302std::string TypeUtils::toString(const timestamp_tz_t& val, void* valueVector);
303template<>
304std::string TypeUtils::toString(const timestamp_t& val, void* valueVector);
305template<>
306std::string TypeUtils::toString(const interval_t& val, void* valueVector);
307template<>
308std::string TypeUtils::toString(const ku_string_t& val, void* valueVector);
309template<>
310std::string TypeUtils::toString(const blob_t& val, void* valueVector);
311template<>
312std::string TypeUtils::toString(const ku_uuid_t& val, void* valueVector);
313template<>
314std::string TypeUtils::toString(const list_entry_t& val, void* valueVector);
315template<>
316std::string TypeUtils::toString(const map_entry_t& val, void* valueVector);
317template<>
318std::string TypeUtils::toString(const struct_entry_t& val, void* valueVector);
319template<>
320std::string TypeUtils::toString(const union_entry_t& val, void* valueVector);
321
322} // namespace common
323} // namespace kuzu
#define KU_UNREACHABLE
Definition assert.h:28
Definition types.h:249
KUZU_API LogicalTypeID getLogicalTypeID() const
Definition types.h:272
KUZU_API PhysicalTypeID getPhysicalType() const
Definition types.h:276
Definition type_utils.h:26
static void paramPackForEachHelper(const Func &func, std::index_sequence< indices... >, Types &&... values)
Definition type_utils.h:29
static void paramPackForEach(const Func &func, Types &&... values)
Definition type_utils.h:35
static auto visit(const LogicalType &dataType, Fs... funcs)
Definition type_utils.h:144
static std::string entryToString(const LogicalType &dataType, const uint8_t *value, ValueVector *vector)
static void decodeOverflowPtr(uint64_t overflowPtr, page_idx_t &pageIdx, uint32_t &pageOffset)
Definition type_utils.h:66
static std::string relToString(const struct_entry_t &val, ValueVector *vector)
static auto visit(PhysicalTypeID dataType, Fs &&... funcs)
Definition type_utils.h:230
static constexpr common::PhysicalTypeID getPhysicalTypeIDForType()
Definition type_utils.h:74
static void encodeOverflowPtr(uint64_t &overflowPtr, page_idx_t pageIdx, uint32_t pageOffset)
Definition type_utils.h:61
static std::string nodeToString(const struct_entry_t &val, ValueVector *vector)
static std::string toString(const T &val, void *=nullptr)
Definition type_utils.h:44
Definition value_vector.h:21
PhysicalTypeID
Definition types.h:215
uint32_t page_idx_t
Definition types.h:33
struct KUZU_API int128_t
Definition int128_t.h:17
Definition array_utils.h:7
Definition blob.h:8
Definition date_t.h:16
Definition int128_t.h:20
Definition types.h:87
Definition interval_t.h:35
Definition ku_string.h:12
Definition uuid.h:16
Definition types.h:111
Definition types.h:123
Definition type_utils.h:21
overload(Funcs... funcs)
Definition type_utils.h:22
Definition types.h:119
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 types.h:127