Skip to content

Commit c3e418b

Browse files
authored
[Refactor](Jsonb) Refactor the JsonBinaryValue and remove unnecessary files. (#51300)
1. Remove unnecessary files. 2. Replace the JsonbParserTSIMD class with a stateless JsonbParser class, and pass JsonbWriter as a parameter. 3. Do not use JsonbErrType anymore; just return Status directly.
1 parent fbef303 commit c3e418b

16 files changed

Lines changed: 234 additions & 979 deletions

‎be/src/exec/es/es_scroll_parser.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,9 @@ Status ScrollParser::fill_columns(const TupleDescriptor* tuple_desc,
847847
break;
848848
}
849849
case TYPE_JSONB: {
850-
JsonBinaryValue binary_val;
851-
RETURN_IF_ERROR(binary_val.init_from_json_string(json_value_to_string(col)));
852-
vectorized::JsonbField json(binary_val.value(), binary_val.size());
850+
JsonBinaryValue jsonb_value;
851+
RETURN_IF_ERROR(jsonb_value.from_json_string(json_value_to_string(col)));
852+
vectorized::JsonbField json(jsonb_value.value(), jsonb_value.size());
853853
col_ptr->insert(vectorized::Field::create_field<TYPE_JSONB>(json));
854854
break;
855855
}

‎be/src/runtime/jsonb_value.cpp‎

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎be/src/runtime/jsonb_value.h‎

Lines changed: 48 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -14,109 +14,72 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
18-
#ifndef DORIS_BE_RUNTIME_JSON_VALUE_H
19-
#define DORIS_BE_RUNTIME_JSON_VALUE_H
20-
21-
#include <glog/logging.h>
22-
17+
#pragma once
2318
#include <cstddef>
24-
#include <ostream>
2519
#include <string>
2620

2721
#include "common/status.h"
28-
#include "util/hash_util.hpp"
2922
#include "util/jsonb_parser_simd.h"
23+
#include "util/jsonb_utils.h"
3024

3125
namespace doris {
26+
#include "common/compile_check_begin.h"
27+
28+
// JsonBinaryValue wraps a Doris jsonb object.
29+
// The jsonb object is written using JsonbWriter.
30+
// JsonBinaryValue is non-movable and non-copyable; it is only a simple wrapper.
31+
// To parse a string to a jsonb object, use it like this:
32+
// JsonBinaryValue jsonb_value;
33+
// RETURN_IF_ERROR(jsonb_value.from_json_string(slice.data, slice.size));
34+
// insert_data(jsonb_value.value(), jsonb_value.size());
35+
// insert_data should use copy semantics.
36+
//
37+
// from_json_string can be called multiple times.
38+
// Example:
39+
// JsonBinaryValue jsonb_value;
40+
// for (;;) {
41+
// RETURN_IF_ERROR(jsonb_value.from_json_string(slice.data, slice.size));
42+
// insert_data(jsonb_value.value(), jsonb_value.size());
43+
// }
3244

33-
struct JsonBinaryValue {
34-
static const int MAX_LENGTH = (1 << 30);
35-
36-
// default nullprt and size 0 for invalid or NULL value
37-
const char* ptr = nullptr;
38-
size_t len = 0;
39-
JsonbParser parser;
45+
struct JsonBinaryValue final {
46+
static constexpr int MAX_LENGTH = (1 << 30);
4047

4148
JsonBinaryValue() = default;
49+
JsonBinaryValue(const JsonBinaryValue&) = delete;
50+
JsonBinaryValue& operator=(const JsonBinaryValue&) = delete;
51+
JsonBinaryValue(JsonBinaryValue&&) = delete;
52+
JsonBinaryValue& operator=(JsonBinaryValue&&) = delete;
4253

4354
const char* value() const { return ptr; }
4455

4556
size_t size() const { return len; }
4657

47-
void replace(const char* ptr, int len) {
48-
this->ptr = ptr;
49-
this->len = len;
50-
}
51-
52-
bool operator==(const JsonBinaryValue& other) const {
53-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
54-
}
55-
// !=
56-
bool ne(const JsonBinaryValue& other) const {
57-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
58-
}
59-
// <=
60-
bool le(const JsonBinaryValue& other) const {
61-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
62-
}
63-
// >=
64-
bool ge(const JsonBinaryValue& other) const {
65-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
66-
}
67-
// <
68-
bool lt(const JsonBinaryValue& other) const {
69-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
70-
}
71-
// >
72-
bool gt(const JsonBinaryValue& other) const {
73-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
74-
}
75-
76-
bool operator!=(const JsonBinaryValue& other) const {
77-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
78-
}
79-
80-
bool operator<=(const JsonBinaryValue& other) const {
81-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
82-
}
83-
84-
bool operator>=(const JsonBinaryValue& other) const {
85-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
86-
}
87-
88-
bool operator<(const JsonBinaryValue& other) const {
89-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
90-
}
91-
92-
bool operator>(const JsonBinaryValue& other) const {
93-
throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported"));
94-
}
95-
96-
Status init_from_json_string(const char* s, size_t len);
97-
98-
Status init_from_json_string(const std::string& s) {
99-
return init_from_json_string(s.c_str(), s.size());
58+
Status from_json_string(const std::string& s) { return from_json_string(s.data(), s.length()); }
59+
Status from_json_string(const char* s, size_t length) {
60+
// reset all fields
61+
ptr = nullptr;
62+
len = 0;
63+
writer.reset();
64+
RETURN_IF_ERROR(JsonbParser::parse(s, length, writer));
65+
ptr = writer.getOutput()->getBuffer();
66+
len = writer.getOutput()->getSize();
67+
if (len > MAX_LENGTH) {
68+
return Status::InternalError(
69+
"Jsonb value length {} exceeds maximum allowed length of {} bytes", len,
70+
MAX_LENGTH);
71+
}
72+
return Status::OK();
10073
}
10174

102-
std::string to_json_string() const;
75+
std::string to_json_string() const { return JsonbToJson::jsonb_to_json_string(ptr, len); }
10376

104-
struct HashOfJsonBinaryValue {
105-
size_t operator()(const JsonBinaryValue& v) const {
106-
return HashUtil::hash(v.ptr, v.len, 0);
107-
}
108-
};
77+
private:
78+
// default nullprt and size 0 for invalid or NULL value
79+
const char* ptr = nullptr;
80+
size_t len = 0;
81+
JsonbWriter writer;
10982
};
11083

111-
// This function must be called 'hash_value' to be picked up by boost.
112-
inline std::size_t hash_value(const JsonBinaryValue& v) {
113-
return HashUtil::hash(v.ptr, v.len, 0);
114-
}
115-
116-
std::ostream& operator<<(std::ostream& os, const JsonBinaryValue& json_value);
117-
118-
std::size_t operator-(const JsonBinaryValue& v1, const JsonBinaryValue& v2);
119-
84+
#include "common/compile_check_end.h"
12085
} // namespace doris
121-
122-
#endif

‎be/src/util/jsonb_error.h‎

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)