Skip to content
Closed
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
33 changes: 29 additions & 4 deletions be/src/exprs/json_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace doris {

// static const re2::RE2 JSON_PATTERN("^([a-zA-Z0-9_\\-\\:\\s#\\|\\.]*)(?:\\[([0-9]+)\\])?");
// json path cannot contains: ", [, ]
static const re2::RE2 JSON_PATTERN("^([^\\\"\\[\\]]*)(?:\\[([0-9]+)\\])?");
static const re2::RE2 JSON_PATTERN("^([^\\\"\\[\\]]*)(?:\\[([0-9]+|\\*)\\])?");

void JsonFunctions::init() {
}
Expand Down Expand Up @@ -159,6 +159,7 @@ rapidjson::Value* JsonFunctions::get_json_object(
return document;
}

bool is_arr_set_by_last = false;
rapidjson::Value* root = document;
rapidjson::Value* array_obj = NULL;
for (int i = 1; i < (*parsed_paths).size(); i++) {
Expand All @@ -170,12 +171,14 @@ rapidjson::Value* JsonFunctions::get_json_object(

if (UNLIKELY(!(*parsed_paths)[i].is_valid)) {
root->SetNull();
continue;
}

std::string& col = (*parsed_paths)[i].key;
int index = (*parsed_paths)[i].idx;
if (LIKELY(!col.empty())) {
if (root->IsArray()) {
if (root->IsArray() && is_arr_set_by_last) {
is_arr_set_by_last = false;
array_obj = static_cast<rapidjson::Value*>(
document->GetAllocator().Malloc(sizeof(rapidjson::Value)));
array_obj->SetArray();
Expand Down Expand Up @@ -209,26 +212,44 @@ rapidjson::Value* JsonFunctions::get_json_object(
} else if (root->IsObject()){
if (!root->HasMember(col.c_str())) {
root->SetNull();
continue;
} else {
root = &((*root)[col.c_str()]);
}
} else {
// root is not a nested type, return NULL
root->SetNull();
continue;
}
}

if (UNLIKELY(index != -1)) {
// judge the rapidjson:Value, which base the top's result,
// if not array return NULL;else get the index value from the array
if (root->IsArray()) {
if (root->IsNull() || index >= root->Size()) {
if (root->IsNull()) {
root->SetNull();
continue;
} else if (index == -2) {
// [*]
array_obj = static_cast<rapidjson::Value*>(
document->GetAllocator().Malloc(sizeof(rapidjson::Value)));
array_obj->SetArray();

for (int j = 0; j < root->Size(); j++) {
array_obj->PushBack((*root)[j], document->GetAllocator());
}
root = array_obj;
is_arr_set_by_last = true;
} else if (index >= root->Size()) {
root->SetNull();
continue;
} else {
root = &((*root)[index]);
}
} else {
root->SetNull();
continue;
}
}
}
Expand Down Expand Up @@ -280,7 +301,11 @@ void JsonFunctions::get_parsed_paths(
} else {
int idx = -1;
if (!index.empty()) {
idx = atoi(index.c_str());
if (index == "*") {
idx = -2;
} else {
idx = atoi(index.c_str());
}
}
parsed_paths->emplace_back(col, idx, true);
}
Expand Down