Skip to content
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
12 changes: 4 additions & 8 deletions be/src/exec/json_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,11 @@ size_t JsonReader::get_data_by_jsonpath(const std::vector<SlotDescriptor*>& slot
}

// if jsonValues is null, because not match in jsondata.
rapidjson::Value* json_values = JsonFunctions::get_json_object_from_parsed_json(path.GetString(), &_json_doc, _json_doc.GetAllocator());
rapidjson::Value* json_values = JsonFunctions::get_json_array_from_parsed_json(path.GetString(), &_json_doc, _json_doc.GetAllocator());
if (json_values == nullptr) {
return -1;
}
if (json_values->IsArray()) {
max_lines = std::max(max_lines, (size_t)json_values->Size());
} else {
max_lines = std::max(max_lines, (size_t)1);
}
max_lines = std::max(max_lines, (size_t)json_values->Size());
_jmap.emplace(slot_descs[i]->col_name(), json_values);
}

Expand Down Expand Up @@ -376,7 +372,7 @@ Status JsonReader::set_tuple_value(rapidjson::Value& objectValue, Tuple* tuple,
/**
* handle input a simple json
* For example:
* case 1. {"RECORDS": [{"colunm1":"value1", "colunm2":10}, {"colunm1":"value2", "colunm2":30}]}
* case 1. [{"colunm1":"value1", "colunm2":10}, {"colunm1":"value2", "colunm2":30}]
* case 2. {"colunm1":"value1", "colunm2":10}
*/
Status JsonReader::handle_simple_json(Tuple* tuple, const std::vector<SlotDescriptor*>& slot_descs, MemPool* tuple_pool, bool* eof) {
Expand Down Expand Up @@ -497,7 +493,7 @@ Status JsonReader::handle_flat_array_complex_json(Tuple* tuple, const std::vecto
}

// if jsonValues is null, because not match in jsondata.
rapidjson::Value* json_values = JsonFunctions::get_json_object_from_parsed_json(path.GetString(), &objectValue, _json_doc.GetAllocator());
rapidjson::Value* json_values = JsonFunctions::get_json_array_from_parsed_json(path.GetString(), &objectValue, _json_doc.GetAllocator());
if (json_values == nullptr) {
if (slot_descs[i]->is_nullable()) {
tuple->set_null(slot_descs[i]->null_indicator_offset());
Expand Down
9 changes: 8 additions & 1 deletion be/src/exprs/json_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ rapidjson::Value* JsonFunctions::get_json_object(
}


rapidjson::Value* JsonFunctions::get_json_object_from_parsed_json (
rapidjson::Value* JsonFunctions::get_json_array_from_parsed_json (
const std::string& path_string,
rapidjson::Value* document,
rapidjson::Document::AllocatorType& mem_allocator) {
Expand Down Expand Up @@ -293,6 +293,13 @@ rapidjson::Value* JsonFunctions::get_json_object_from_parsed_json (
rapidjson::Value* root = match_value(parsed_paths, document, mem_allocator, true);
if (root == document) {// not found
return nullptr;
} else if (!root->IsArray()) {
rapidjson::Value* array_obj = nullptr;
array_obj = static_cast<rapidjson::Value*>(
mem_allocator.Malloc(sizeof(rapidjson::Value)));
array_obj->SetArray();
array_obj->PushBack(*root, mem_allocator);
return array_obj;
}
return root;
}
Expand Down
3 changes: 2 additions & 1 deletion be/src/exprs/json_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ class JsonFunctions {

/**
* The `document` parameter must be has parsed.
* return Value Is Array object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to change its name to get_json_array_from_parsed_json() to make it more explict.

And in src//exec/json_scanner.cpp line 270, you can this method, but judge the return value as either array or non-array? the else should not happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method MUST return ArrayObject.
else if (!root->IsArray()) is true, then I warp a ArrayObject in root outside.
else case MUST be ArrayObject

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name changed to get_json_objects_from_parsed_json ??

*/
static rapidjson::Value* get_json_object_from_parsed_json(
static rapidjson::Value* get_json_array_from_parsed_json(
const std::string& path_string,
rapidjson::Value* document,
rapidjson::Document::AllocatorType& mem_allocator);
Expand Down
10 changes: 5 additions & 5 deletions be/test/exprs/json_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ TEST_F(JsonFunctionTest, json_path1)
ASSERT_TRUE(false);
}
rapidjson::Value* res3;
res3 = JsonFunctions::get_json_object_from_parsed_json("$.[*].keyname.ip", &jsonDoc, jsonDoc.GetAllocator());
res3 = JsonFunctions::get_json_array_from_parsed_json("$.[*].keyname.ip", &jsonDoc, jsonDoc.GetAllocator());
ASSERT_TRUE(res3->IsArray());
for (int i = 0; i < res3->Size(); i++) {
std::cout<< (*res3)[i].GetString() << std::endl;
}

res3 = JsonFunctions::get_json_object_from_parsed_json("$.[*].k1", &jsonDoc, jsonDoc.GetAllocator());
res3 = JsonFunctions::get_json_array_from_parsed_json("$.[*].k1", &jsonDoc, jsonDoc.GetAllocator());
ASSERT_TRUE(res3->IsArray());
for (int i = 0; i < res3->Size(); i++) {
std::cout<< (*res3)[i].GetString() << std::endl;
Expand All @@ -216,7 +216,7 @@ TEST_F(JsonFunctionTest, json_path_get_nullobject)
ASSERT_TRUE(false);
}

rapidjson::Value* res3 = JsonFunctions::get_json_object_from_parsed_json("$.[*].b", &jsonDoc, jsonDoc.GetAllocator());
rapidjson::Value* res3 = JsonFunctions::get_json_array_from_parsed_json("$.[*].b", &jsonDoc, jsonDoc.GetAllocator());
ASSERT_TRUE(res3->IsArray());
ASSERT_EQ(res3->Size(), 3);
for (int i = 0; i < res3->Size(); i++) {
Expand All @@ -239,7 +239,7 @@ TEST_F(JsonFunctionTest, json_path_test)
ASSERT_TRUE(false);
}

rapidjson::Value* res3 = JsonFunctions::get_json_object_from_parsed_json("$.[*].a", &jsonDoc, jsonDoc.GetAllocator());
rapidjson::Value* res3 = JsonFunctions::get_json_array_from_parsed_json("$.[*].a", &jsonDoc, jsonDoc.GetAllocator());
ASSERT_TRUE(res3->IsArray());
ASSERT_EQ(res3->Size(), 2);
for (int i = 0; i < res3->Size(); i++) {
Expand All @@ -258,7 +258,7 @@ TEST_F(JsonFunctionTest, json_path_test)
ASSERT_TRUE(false);
}

rapidjson::Value* res3 = JsonFunctions::get_json_object_from_parsed_json("$.a", &jsonDoc, jsonDoc.GetAllocator());
rapidjson::Value* res3 = JsonFunctions::get_json_array_from_parsed_json("$.a", &jsonDoc, jsonDoc.GetAllocator());
ASSERT_TRUE(res3->IsArray());
ASSERT_EQ(res3->Size(), 2);
for (int i = 0; i < res3->Size(); i++) {
Expand Down