Skip to content

Commit

Permalink
[improve](json)improve json support empty keys (apache#36762)
Browse files Browse the repository at this point in the history
support empty key for json 
behavior reference MSYQL
  • Loading branch information
amorynan authored Jul 1, 2024
1 parent 707cb40 commit 6a9f003
Show file tree
Hide file tree
Showing 11 changed files with 3,739 additions and 5 deletions.
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

0 comments on commit 6a9f003

Please sign in to comment.