yiguolei commented on code in PR #51766:
URL: https://github.com/apache/doris/pull/51766#discussion_r2159819795
##########
be/src/util/jsonb_document.h:
##########
@@ -587,315 +611,204 @@ class JsonbValue {
//Whether to include the jsonbvalue rhs
bool contains(JsonbValue* rhs) const;
- // get the raw byte array of the value
- const char* getValuePtr() const;
-
// find the JSONB value by JsonbPath
JsonbValue* findValue(JsonbPath& path, hDictFind handler);
friend class JsonbDocument;
-protected:
- JsonbType type_; // type info
+ JsonbType type; // type info
+
+ char payload[0]; // payload, which is the packed bytes of the value
+
+ template <typename T>
+ const T* unpack() const {
+ return reinterpret_cast<const T*>(payload);
+ }
+
+ template <typename T>
+ T* unpack() {
+ return reinterpret_cast<T*>(payload);
+ }
- JsonbValue();
+ JsonbValue() = delete;
};
/*
* NumerValT is the template class (derived from JsonbValue) of all number
* types (integers and double).
*/
-template <class T>
-class NumberValT : public JsonbValue {
+template <typename T>
+struct NumberValT {
public:
- T val() const { return num_; }
-
- unsigned int numPackedBytes() const { return sizeof(JsonbValue) +
sizeof(T); }
+ NumberValT() = delete;
+ T val() const { return num; }
- // catch all unknow specialization of the template class
- bool setVal(T value) { return false; }
-
-private:
- T num_;
+ static constexpr unsigned int numPackedBytes() { return sizeof(JsonbValue)
+ sizeof(T); }
- NumberValT();
+ T num;
};
-typedef NumberValT<int8_t> JsonbInt8Val;
-
-// override setVal for Int8Val
-template <>
-inline bool JsonbInt8Val::setVal(int8_t value) {
- if (!isInt8()) {
- return false;
- }
-
- num_ = value;
- return true;
-}
-
-typedef NumberValT<int16_t> JsonbInt16Val;
-
-// override setVal for Int16Val
-template <>
-inline bool JsonbInt16Val::setVal(int16_t value) {
- if (!isInt16()) {
- return false;
- }
-
- num_ = value;
- return true;
-}
-typedef NumberValT<int32_t> JsonbInt32Val;
-
-// override setVal for Int32Val
-template <>
-inline bool JsonbInt32Val::setVal(int32_t value) {
- if (!isInt32()) {
- return false;
- }
-
- num_ = value;
- return true;
-}
-
-typedef NumberValT<int64_t> JsonbInt64Val;
-
-// override setVal for Int64Val
-template <>
-inline bool JsonbInt64Val::setVal(int64_t value) {
- if (!isInt64()) {
- return false;
- }
-
- num_ = value;
- return true;
-}
-
-typedef NumberValT<int128_t> JsonbInt128Val;
-
-// override setVal for Int128Val
-template <>
-inline bool JsonbInt128Val::setVal(int128_t value) {
- if (!isInt128()) {
- return false;
- }
-
- num_ = value;
- return true;
-}
-
-typedef NumberValT<double> JsonbDoubleVal;
-
-// override setVal for DoubleVal
-template <>
-inline bool JsonbDoubleVal::setVal(double value) {
- if (!isDouble()) {
- return false;
- }
-
- num_ = value;
- return true;
+inline ObjectVal* JsonbDocument::operator->() {
+ return (((JsonbValue*)payload_)->unpack<ObjectVal>());
}
-typedef NumberValT<float> JsonbFloatVal;
-
-// override setVal for DoubleVal
-template <>
-inline bool JsonbFloatVal::setVal(float value) {
- if (!isFloat()) {
- return false;
- }
-
- num_ = value;
- return true;
+inline const ObjectVal* JsonbDocument::operator->() const {
+ return (((JsonbValue*)payload_)->unpack<ObjectVal>());
}
+using JsonbInt8Val = NumberValT<int8_t>;
+using JsonbInt16Val = NumberValT<int16_t>;
+using JsonbInt32Val = NumberValT<int32_t>;
+using JsonbInt64Val = NumberValT<int64_t>;
+using JsonbInt128Val = NumberValT<int128_t>;
+using JsonbDoubleVal = NumberValT<double>;
+using JsonbFloatVal = NumberValT<float>;
// A class to get an integer
-class JsonbIntVal : public JsonbValue {
-public:
+struct JsonbIntVal : public JsonbValue {
int128_t val() const {
- switch (type_) {
+ switch (type) {
case JsonbType::T_Int8:
- return ((JsonbInt8Val*)this)->val();
+ return unpack<JsonbInt8Val>()->val();
case JsonbType::T_Int16:
- return ((JsonbInt16Val*)this)->val();
+ return unpack<JsonbInt16Val>()->val();
case JsonbType::T_Int32:
- return ((JsonbInt32Val*)this)->val();
+ return unpack<JsonbInt32Val>()->val();
case JsonbType::T_Int64:
- return ((JsonbInt64Val*)this)->val();
+ return unpack<JsonbInt64Val>()->val();
case JsonbType::T_Int128:
- return ((JsonbInt128Val*)this)->val();
+ return unpack<JsonbInt128Val>()->val();
default:
- return 0;
+ throw Exception(ErrorCode::INTERNAL_ERROR, "Invalid JSONB value
type: {}",
+ static_cast<int32_t>(type));
}
}
- bool setVal(int128_t val) {
- switch (type_) {
- case JsonbType::T_Int8:
- if (val < std::numeric_limits<int8_t>::min() ||
- val > std::numeric_limits<int8_t>::max())
- return false;
- return ((JsonbInt8Val*)this)->setVal((int8_t)val);
- case JsonbType::T_Int16:
- if (val < std::numeric_limits<int16_t>::min() ||
- val > std::numeric_limits<int16_t>::max())
- return false;
- return ((JsonbInt16Val*)this)->setVal((int16_t)val);
- case JsonbType::T_Int32:
- if (val < std::numeric_limits<int32_t>::min() ||
- val > std::numeric_limits<int32_t>::max())
- return false;
- return ((JsonbInt32Val*)this)->setVal((int32_t)val);
- case JsonbType::T_Int64:
- return ((JsonbInt64Val*)this)->setVal((int64_t)val);
- case JsonbType::T_Int128:
- return ((JsonbInt128Val*)this)->setVal(val);
- default:
- return false;
- }
+};
+
+template <JsonbDecimalType T>
+struct JsonbDecimalVal {
+public:
+ using NativeType = typename T::NativeType;
+ JsonbDecimalVal() = delete;
+
+ // get the decimal value
+ T val() const { return T(value); }
+
+ static constexpr int numPackedBytes() {
+ return sizeof(JsonbValue) + sizeof(precision) + sizeof(scale) +
sizeof(value);
}
+
+ uint32_t precision;
+ uint32_t scale;
+ NativeType value;
};
+using JsonbDecimal256 = JsonbDecimalVal<vectorized::Decimal256>;
+using JsonbDecimal128 = JsonbDecimalVal<vectorized::Decimal128V3>;
+using JsonbDecimal64 = JsonbDecimalVal<vectorized::Decimal64>;
+using JsonbDecimal32 = JsonbDecimalVal<vectorized::Decimal32>;
+
/*
* BlobVal is the base class (derived from JsonbValue) for string and binary
- * types. The size_ indicates the total bytes of the payload_.
+ * types. The size indicates the total bytes of the payload.
*/
-class JsonbBlobVal : public JsonbValue {
+struct JsonbBlobVal {
public:
// size of the blob payload only
- unsigned int getBlobLen() const { return size_; }
+ unsigned int getBlobLen() const { return size; }
// return the blob as byte array
- const char* getBlob() const { return payload_; }
+ const char* getBlob() const { return payload; }
// size of the total packed bytes
- unsigned int numPackedBytes() const { return sizeof(JsonbValue) +
sizeof(size_) + size_; }
+ unsigned int numPackedBytes() const { return sizeof(JsonbValue) +
sizeof(size) + size; }
friend class JsonbDocument;
-protected:
- uint32_t size_;
- char payload_[0];
-
- // set new blob bytes
- bool internalSetVal(const char* blob, uint32_t blobSize) {
- // if we cannot fit the new blob, fail the operation
- if (blobSize > size_) {
- return false;
- }
-
- memcpy(payload_, blob, blobSize);
-
- // Set the reset of the bytes to 0. Note we cannot change the size_
of the
- // current payload, as all values are packed.
- memset(payload_ + blobSize, 0, size_ - blobSize);
-
- return true;
- }
+ uint32_t size;
+ char payload[0];
- JsonbBlobVal();
+ JsonbBlobVal() = delete;
};
/*
* Binary type
*/
-class JsonbBinaryVal : public JsonbBlobVal {
-public:
- bool setVal(const char* blob, uint32_t blobSize) {
- if (!isBinary()) {
- return false;
- }
-
- return internalSetVal(blob, blobSize);
- }
-
-private:
- JsonbBinaryVal();
-};
+using JsonbBinaryVal = JsonbBlobVal;
Review Comment:
把blob value 这个名字直接rename 成jsonb binary value吧,感觉这个blob vale 似乎一直是跟binary
value 等价的
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]