amorynan commented on code in PR #22911:
URL: https://github.com/apache/doris/pull/22911#discussion_r1292655706
##########
be/src/vec/functions/function_json.cpp:
##########
@@ -936,6 +937,186 @@ class FunctionJsonValid : public IFunction {
}
};
+class FunctionJsonMergePreserve : public IFunction {
+public:
+ static constexpr auto name = "json_merge_preserve";
+ static FunctionPtr create() { return
std::make_shared<FunctionJsonMergePreserve>(); }
+
+ String get_name() const override { return name; }
+
+ size_t get_number_of_arguments() const override { return 0; }
+
+ bool is_variadic() const override { return true; }
+
+ DataTypePtr get_return_type_impl(const DataTypes& arguments) const
override {
+ return make_nullable(std::make_shared<DataTypeString>());
+ }
+
+ bool use_default_implementation_for_nulls() const override { return false;
}
+
+ rapidjson::Value* merge_list(std::vector<rapidjson::Value*> jsons,
+ rapidjson::Document::AllocatorType&
allocator) {
+ rapidjson::Value* result =
+
static_cast<rapidjson::Value*>(allocator.Malloc(sizeof(rapidjson::Value)));
+
+ (*result).SetArray();
+ for (auto json : jsons) {
+ if (json->IsArray()) {
+ for (auto itr = json->Begin(); itr != json->End(); itr++) {
+ rapidjson::Value* node = static_cast<rapidjson::Value*>(
+ allocator.Malloc(sizeof(rapidjson::Value)));
+ node->CopyFrom(*itr, allocator);
+ result->PushBack(*node, allocator);
+ }
+ } else {
+ rapidjson::Value* node =
+
static_cast<rapidjson::Value*>(allocator.Malloc(sizeof(rapidjson::Value)));
+ node->CopyFrom(*json, allocator);
+ result->PushBack(*node, allocator);
+ }
+ }
+ return result;
+ }
+
+ rapidjson::Value* merge_obejcts(std::vector<rapidjson::Value*> objs,
+ rapidjson::Document::AllocatorType&
allocator) {
+ rapidjson::Value* result =
+
static_cast<rapidjson::Value*>(allocator.Malloc(sizeof(rapidjson::Value)));
+ result->SetObject();
+ std::map<std::string, std::vector<rapidjson::Value*>> json_bucket;
+
+ for (rapidjson::Value* obj : objs) {
+ for (auto itr = obj->MemberBegin(); itr != obj->MemberEnd();
++itr) {
+ if (json_bucket.find(itr->name.GetString()) !=
json_bucket.end()) {
+
json_bucket[itr->name.GetString()].push_back(&(itr->value));
+ } else {
+ std::vector<rapidjson::Value*> jsons;
+ jsons.push_back(&(itr->value));
+ json_bucket[itr->name.GetString()] = jsons;
+ }
+ }
+ }
+
+ for (auto itr = json_bucket.begin(); itr != json_bucket.end(); itr++) {
+ auto value = merge_jsons(itr->second, allocator);
+ result->AddMember(rapidjson::Value(itr->first.c_str(), allocator),
*value, allocator);
+ }
+ return result;
+ }
+
+ rapidjson::Value* merge_jsons(std::vector<rapidjson::Value*> jsons,
+ rapidjson::Document::AllocatorType&
allocator) {
+ rapidjson::Value* result =
+
static_cast<rapidjson::Value*>(allocator.Malloc(sizeof(rapidjson::Value)));
+
+ if (jsons.empty()) {
+ return result;
+ }
+
+ if (jsons.size() == 1) {
+ return jsons.at(0);
Review Comment:
just jsons[0] is ok
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]