Lbug C++ API
Loading...
Searching...
No Matches
value.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdexcept>
4#include <utility>
5
6#include "api.h"
7#include "date_t.h"
8#include "int128_t.h"
9#include "interval_t.h"
10#include "ku_list.h"
11#include "timestamp_t.h"
12#include "uint128_t.h"
13#include "uuid.h"
14
15namespace lbug {
16namespace common {
17
18class NodeVal;
19class RelVal;
20struct FileInfo;
21class NestedVal;
22class RecursiveRelVal;
23class ArrowRowBatch;
24class ValueVector;
25class Serializer;
26class Deserializer;
27
28class Value {
29 friend class NodeVal;
30 friend class RelVal;
31 friend class NestedVal;
32 friend class RecursiveRelVal;
33 friend class ArrowRowBatch;
34 friend class ValueVector;
35
36public:
45 LBUG_API static Value createNullValue(const LogicalType& dataType);
54 LBUG_API explicit Value(bool val_);
58 LBUG_API explicit Value(int8_t val_);
62 LBUG_API explicit Value(int16_t val_);
66 LBUG_API explicit Value(int32_t val_);
70 LBUG_API explicit Value(int64_t val_);
74 LBUG_API explicit Value(uint8_t val_);
78 LBUG_API explicit Value(uint16_t val_);
82 LBUG_API explicit Value(uint32_t val_);
86 LBUG_API explicit Value(uint64_t val_);
90 LBUG_API explicit Value(int128_t val_);
94 LBUG_API explicit Value(ku_uuid_t val_);
98 LBUG_API explicit Value(double val_);
102 LBUG_API explicit Value(float val_);
106 LBUG_API explicit Value(date_t val_);
126 LBUG_API explicit Value(timestamp_t val_);
130 LBUG_API explicit Value(interval_t val_);
138 LBUG_API explicit Value(uint128_t val_);
142 LBUG_API explicit Value(const char* val_);
146 LBUG_API explicit Value(const std::string& val_);
150 LBUG_API explicit Value(uint8_t* val_);
155 LBUG_API explicit Value(LogicalType type, std::string val_);
160 LBUG_API explicit Value(LogicalType dataType, std::vector<std::unique_ptr<Value>> children);
164 LBUG_API Value(const Value& other);
165
169 LBUG_API Value(Value&& other) = default;
170 LBUG_API Value& operator=(Value&& other) = default;
171 LBUG_API bool operator==(const Value& rhs) const;
172
177 LBUG_API void setDataType(const LogicalType& dataType_);
186 LBUG_API void setNull(bool flag);
194 LBUG_API bool isNull() const;
199 LBUG_API void copyFromRowLayout(const uint8_t* value);
204 LBUG_API void copyFromColLayout(const uint8_t* value, ValueVector* vec = nullptr);
209 LBUG_API void copyValueFrom(const Value& other);
213 template<class T>
214 T getValue() const {
215 throw std::runtime_error("Unimplemented template for Value::getValue()");
216 }
217
220 template<class T>
222 throw std::runtime_error("Unimplemented template for Value::getValueReference()");
223 }
224
227 template<class T>
228 static Value createValue(T /*value*/) {
229 throw std::runtime_error("Unimplemented template for Value::createValue()");
230 }
231
235 LBUG_API std::unique_ptr<Value> copy() const;
239 LBUG_API std::string toString() const;
240
241 LBUG_API void serialize(Serializer& serializer) const;
242
243 LBUG_API static std::unique_ptr<Value> deserialize(Deserializer& deserializer);
244
246
248 bool allowTypeChange() const;
249
250 uint64_t computeHash() const;
251
252 uint32_t getChildrenSize() const { return childrenSize; }
253
254private:
255 Value();
256 explicit Value(const LogicalType& dataType);
257
258 void resizeChildrenVector(uint64_t size, const LogicalType& childType);
259 void copyFromRowLayoutList(const ku_list_t& list, const LogicalType& childType);
260 void copyFromColLayoutList(const list_entry_t& list, ValueVector* vec);
261 void copyFromRowLayoutStruct(const uint8_t* kuStruct);
262 void copyFromColLayoutStruct(const struct_entry_t& structEntry, ValueVector* vec);
263 void copyFromUnion(const uint8_t* kuUnion);
264
265 std::string mapToString() const;
266 std::string listToString() const;
267 std::string structToString() const;
268 std::string nodeToString() const;
269 std::string relToString() const;
270 std::string decimalToString() const;
271
272public:
273 union Val {
274 constexpr Val() : booleanVal{false} {}
277 int64_t int64Val;
278 int32_t int32Val;
279 int16_t int16Val;
280 int8_t int8Val;
281 uint64_t uint64Val;
282 uint32_t uint32Val;
283 uint16_t uint16Val;
284 uint8_t uint8Val;
285 double doubleVal;
286 float floatVal;
287 // TODO(Ziyi): Should we remove the val suffix from all values in Val? Looks redundant.
288 uint8_t* pointer;
293 std::string strVal;
294
295private:
296 LogicalType dataType;
297 bool isNull_;
298
299 // Note: ALWAYS use childrenSize over children.size(). We do NOT resize children when
300 // iterating with nested value. So children.size() reflects the capacity() rather the actual
301 // size.
302 std::vector<std::unique_ptr<Value>> children;
303 uint32_t childrenSize;
304};
305
309template<>
310inline bool Value::getValue() const {
311 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::BOOL);
312 return val.booleanVal;
313}
314
318template<>
319inline int8_t Value::getValue() const {
320 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT8);
321 return val.int8Val;
322}
323
327template<>
328inline int16_t Value::getValue() const {
329 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT16);
330 return val.int16Val;
331}
332
336template<>
337inline int32_t Value::getValue() const {
338 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT32);
339 return val.int32Val;
340}
341
345template<>
346inline int64_t Value::getValue() const {
347 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT64);
348 return val.int64Val;
349}
350
354template<>
355inline uint64_t Value::getValue() const {
356 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT64);
357 return val.uint64Val;
358}
359
363template<>
364inline uint32_t Value::getValue() const {
365 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT32);
366 return val.uint32Val;
367}
368
372template<>
373inline uint16_t Value::getValue() const {
374 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT16);
375 return val.uint16Val;
376}
377
381template<>
382inline uint8_t Value::getValue() const {
383 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT8);
384 return val.uint8Val;
385}
386
390template<>
391inline int128_t Value::getValue() const {
392 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT128);
393 return val.int128Val;
394}
395
399template<>
400inline float Value::getValue() const {
401 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::FLOAT);
402 return val.floatVal;
403}
404
408template<>
409inline double Value::getValue() const {
410 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::DOUBLE);
411 return val.doubleVal;
412}
413
417template<>
418inline date_t Value::getValue() const {
419 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::DATE);
420 return date_t{val.int32Val};
421}
422
426template<>
428 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP);
429 return timestamp_t{val.int64Val};
430}
431
435template<>
437 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_NS);
438 return timestamp_ns_t{val.int64Val};
439}
440
444template<>
446 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_MS);
447 return timestamp_ms_t{val.int64Val};
448}
449
453template<>
455 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_SEC);
456 return timestamp_sec_t{val.int64Val};
457}
458
462template<>
464 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_TZ);
465 return timestamp_tz_t{val.int64Val};
466}
467
471template<>
473 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::INTERVAL);
474 return val.intervalVal;
475}
476
480template<>
482 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::INTERNAL_ID);
483 return val.internalIDVal;
484}
485
489template<>
491 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT128);
492 return val.uint128Val;
493}
494
498template<>
499inline std::string Value::getValue() const {
500 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::STRING ||
501 dataType.getLogicalTypeID() == LogicalTypeID::BLOB ||
502 dataType.getLogicalTypeID() == LogicalTypeID::UUID);
503 return strVal;
504}
505
509template<>
510inline uint8_t* Value::getValue() const {
511 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::POINTER);
512 return val.pointer;
513}
514
518template<>
520 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::BOOL);
521 return val.booleanVal;
522}
523
527template<>
528inline int8_t& Value::getValueReference() {
529 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT8);
530 return val.int8Val;
531}
532
536template<>
537inline int16_t& Value::getValueReference() {
538 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT16);
539 return val.int16Val;
540}
541
545template<>
546inline int32_t& Value::getValueReference() {
547 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT32);
548 return val.int32Val;
549}
550
554template<>
555inline int64_t& Value::getValueReference() {
556 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT64);
557 return val.int64Val;
558}
559
563template<>
564inline uint8_t& Value::getValueReference() {
565 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT8);
566 return val.uint8Val;
567}
568
572template<>
573inline uint16_t& Value::getValueReference() {
574 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT16);
575 return val.uint16Val;
576}
577
581template<>
582inline uint32_t& Value::getValueReference() {
583 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT32);
584 return val.uint32Val;
585}
586
590template<>
591inline uint64_t& Value::getValueReference() {
592 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT64);
593 return val.uint64Val;
594}
595
599template<>
601 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::INT128);
602 return val.int128Val;
603}
604
608template<>
610 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::FLOAT);
611 return val.floatVal;
612}
613
617template<>
618inline double& Value::getValueReference() {
619 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::DOUBLE);
620 return val.doubleVal;
621}
622
626template<>
628 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::DATE);
629 return *reinterpret_cast<date_t*>(&val.int32Val);
630}
631
635template<>
637 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP);
638 return *reinterpret_cast<timestamp_t*>(&val.int64Val);
639}
640
644template<>
646 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_MS);
647 return *reinterpret_cast<timestamp_ms_t*>(&val.int64Val);
648}
649
653template<>
655 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_NS);
656 return *reinterpret_cast<timestamp_ns_t*>(&val.int64Val);
657}
658
662template<>
664 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_SEC);
665 return *reinterpret_cast<timestamp_sec_t*>(&val.int64Val);
666}
667
671template<>
673 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::TIMESTAMP_TZ);
674 return *reinterpret_cast<timestamp_tz_t*>(&val.int64Val);
675}
676
680template<>
682 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::INTERVAL);
683 return val.intervalVal;
684}
685
689template<>
691 KU_ASSERT(dataType.getPhysicalType() == PhysicalTypeID::UINT128);
692 return val.uint128Val;
693}
694
698template<>
700 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::INTERNAL_ID);
701 return val.internalIDVal;
702}
703
707template<>
708inline std::string& Value::getValueReference() {
709 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::STRING);
710 return strVal;
711}
712
716template<>
717inline uint8_t*& Value::getValueReference() {
718 KU_ASSERT(dataType.getLogicalTypeID() == LogicalTypeID::POINTER);
719 return val.pointer;
720}
721
726template<>
728 return Value(val);
729}
730
731template<>
733 return Value(val);
734}
735
740template<>
741inline Value Value::createValue(int16_t val) {
742 return Value(val);
743}
744
749template<>
750inline Value Value::createValue(int32_t val) {
751 return Value(val);
752}
753
758template<>
759inline Value Value::createValue(int64_t val) {
760 return Value(val);
761}
762
767template<>
768inline Value Value::createValue(uint8_t val) {
769 return Value(val);
770}
771
776template<>
777inline Value Value::createValue(uint16_t val) {
778 return Value(val);
779}
780
785template<>
786inline Value Value::createValue(uint32_t val) {
787 return Value(val);
788}
789
794template<>
795inline Value Value::createValue(uint64_t val) {
796 return Value(val);
797}
798
803template<>
805 return Value(val);
806}
807
812template<>
814 return Value(val);
815}
816
821template<>
823 return Value(val);
824}
825
830template<>
832 return Value(val);
833}
834
839template<>
841 return Value(val);
842}
843
848template<>
850 return Value(val);
851}
852
857template<>
859 return Value(val);
860}
861
866template<>
867inline Value Value::createValue(std::string val) {
868 return Value(LogicalType::STRING(), std::move(val));
869}
870
875template<>
876inline Value Value::createValue(const char* value) {
877 return Value(LogicalType::STRING(), std::string(value));
878}
879
884template<>
885inline Value Value::createValue(uint8_t* val) {
886 return Value(val);
887}
888
893template<>
895 return Value(val);
896}
897
898} // namespace common
899} // namespace lbug
#define LBUG_API
Definition api.h:25
#define KU_ASSERT(condition)
Definition assert.h:24
Definition types.h:265
static LogicalType STRING()
Definition types.h:344
Definition nested.h:12
NodeVal represents a node in the graph and stores the nodeID, label and properties of that node.
Definition node.h:20
RecursiveRelVal represents a path in the graph and stores the corresponding rels and nodes of that pa...
Definition recursive_rel.h:14
RelVal represents a rel in the graph and stores the relID, src/dst nodes and properties of that rel.
Definition rel.h:20
LBUG_API void setNull()
Sets the null flag of the Value to true.
T getValue() const
Definition value.h:214
LBUG_API Value(int128_t val_)
LBUG_API Value(timestamp_tz_t val_)
LBUG_API Value(int8_t val_)
LBUG_API void serialize(Serializer &serializer) const
LBUG_API Value(uint16_t val_)
LBUG_API bool isNull() const
LBUG_API void copyFromRowLayout(const uint8_t *value)
Copies from the row layout value.
LBUG_API std::string toString() const
bool allowTypeChange() const
friend class ValueVector
Definition value.h:34
LBUG_API Value(int64_t val_)
LBUG_API Value(interval_t val_)
friend class RecursiveRelVal
Definition value.h:32
friend class ArrowRowBatch
Definition value.h:33
LBUG_API std::unique_ptr< Value > copy() const
LBUG_API Value(timestamp_sec_t val_)
bool hasNoneNullChildren() const
friend class NestedVal
Definition value.h:31
LBUG_API Value(double val_)
LBUG_API Value(int32_t val_)
static Value createValue(T)
Definition value.h:228
LBUG_API Value(float val_)
LBUG_API Value(uint64_t val_)
T & getValueReference()
Definition value.h:221
friend class RelVal
Definition value.h:30
LBUG_API Value(LogicalType dataType, std::vector< std::unique_ptr< Value > > children)
LBUG_API Value(const Value &other)
LBUG_API Value(timestamp_ms_t val_)
LBUG_API void setNull(bool flag)
Sets the null flag of the Value.
LBUG_API void validateType(common::LogicalTypeID targetTypeID) const
LBUG_API Value(bool val_)
LBUG_API void setDataType(const LogicalType &dataType_)
Sets the data type of the Value.
LBUG_API Value(LogicalType type, std::string val_)
LBUG_API Value(Value &&other)=default
LBUG_API Value(const char *val_)
LBUG_API void copyFromColLayout(const uint8_t *value, ValueVector *vec=nullptr)
Copies from the col layout value.
uint64_t computeHash() const
LBUG_API Value(timestamp_ns_t val_)
friend class NodeVal
Definition value.h:29
union lbug::common::Value::Val val
LBUG_API void copyValueFrom(const Value &other)
Copies from the other.
LBUG_API bool operator==(const Value &rhs) const
LBUG_API Value(timestamp_t val_)
LBUG_API Value & operator=(Value &&other)=default
static LBUG_API Value createNullValue(const LogicalType &dataType)
LBUG_API Value(uint32_t val_)
LBUG_API const LogicalType & getDataType() const
LBUG_API Value(internalID_t val_)
LBUG_API Value(ku_uuid_t val_)
LBUG_API Value(int16_t val_)
LBUG_API Value(uint8_t *val_)
LBUG_API Value(uint128_t val_)
static LBUG_API Value createDefaultValue(const LogicalType &dataType)
std::string strVal
Definition value.h:293
LBUG_API Value(date_t val_)
LBUG_API Value(const std::string &val_)
uint32_t getChildrenSize() const
Definition value.h:252
static LBUG_API Value createNullValue()
static LBUG_API std::unique_ptr< Value > deserialize(Deserializer &deserializer)
LBUG_API Value(uint8_t val_)
Definition value_vector.h:21
Definition array_utils.h:7
internalID_t nodeID_t
Definition types.h:83
@ UINT32
Definition types.h:239
@ UINT16
Definition types.h:240
@ INT64
Definition types.h:234
@ INT16
Definition types.h:236
@ INT32
Definition types.h:235
@ UINT64
Definition types.h:238
@ BOOL
Definition types.h:233
@ INT128
Definition types.h:242
@ 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
LogicalTypeID
Definition types.h:184
@ BLOB
Definition types.h:217
@ INTERVAL
Definition types.h:211
@ TIMESTAMP_NS
Definition types.h:209
@ UUID
Definition types.h:226
@ DATE
Definition types.h:205
@ STRING
Definition types.h:216
@ TIMESTAMP
Definition types.h:206
@ INTERNAL_ID
Definition types.h:213
@ POINTER
Definition types.h:224
@ TIMESTAMP_TZ
Definition types.h:210
@ TIMESTAMP_MS
Definition types.h:208
@ TIMESTAMP_SEC
Definition types.h:207
Definition array_utils.h:7
Definition date_t.h:16
Definition int128_t.h:21
Definition types.h:90
Definition interval_t.h:35
Definition ku_list.h:8
Definition uuid.h:16
Definition types.h:114
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
int16_t int16Val
Definition value.h:279
int8_t int8Val
Definition value.h:280
uint8_t * pointer
Definition value.h:288
uint64_t uint64Val
Definition value.h:281
interval_t intervalVal
Definition value.h:289
int32_t int32Val
Definition value.h:278
bool booleanVal
Definition value.h:275
constexpr Val()
Definition value.h:274
uint128_t uint128Val
Definition value.h:291
uint32_t uint32Val
Definition value.h:282
double doubleVal
Definition value.h:285
uint16_t uint16Val
Definition value.h:283
int128_t int128Val
Definition value.h:276
internalID_t internalIDVal
Definition value.h:290
uint8_t uint8Val
Definition value.h:284
int64_t int64Val
Definition value.h:277
float floatVal
Definition value.h:286