Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve](json)improve json support empty keys #36762

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions be/src/util/jsonb_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ typedef std::underlying_type<JsonbType>::type JsonbTypeUnder;
*/
class JsonbKeyValue {
public:
// now we use sMaxKeyId to represent an empty key
static const int sMaxKeyId = 65535;
typedef uint16_t keyid_type;

Expand Down
7 changes: 6 additions & 1 deletion be/src/util/jsonb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ class JsonbToJson {
if (iter->klen()) {
string_to_json(iter->getKeyStr(), iter->klen());
} else {
os_.write(iter->getKeyId());
// NOTE: we use sMaxKeyId to represent an empty key. see jsonb_writer.h
if (iter->getKeyId() == JsonbKeyValue::sMaxKeyId) {
string_to_json(nullptr, 0);
} else {
os_.write(iter->getKeyId());
}
}
os_.put(':');

Expand Down
15 changes: 11 additions & 4 deletions be/src/util/jsonb_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class JsonbWriterT {

// write a key string (or key id if an external dict is provided)
uint32_t writeKey(const char* key, uint8_t len, hDictInsert handler = nullptr) {
if (len && !stack_.empty() && verifyKeyState()) {
if (!stack_.empty() && verifyKeyState()) {
int key_id = -1;
if (handler) {
key_id = handler(key, len);
Expand All @@ -88,9 +88,16 @@ class JsonbWriterT {
uint32_t size = sizeof(uint8_t);
if (key_id < 0) {
os_->put(len);
os_->write(key, len);
size += len;
} else if (key_id <= JsonbKeyValue::sMaxKeyId) {
if (len == 0) {
// NOTE: we use sMaxKeyId to represent an empty key
JsonbKeyValue::keyid_type idx = JsonbKeyValue::sMaxKeyId;
os_->write((char*)&idx, sizeof(JsonbKeyValue::keyid_type));
size += sizeof(JsonbKeyValue::keyid_type);
} else {
os_->write(key, len);
size += len;
}
} else if (key_id < JsonbKeyValue::sMaxKeyId) {
JsonbKeyValue::keyid_type idx = key_id;
os_->put(0);
os_->write((char*)&idx, sizeof(JsonbKeyValue::keyid_type));
Expand Down
Loading
Loading