Merge pull request #57379 from evillique/fix-jsonmergepatch

Fix function jsonMergePatch for partially const columns
This commit is contained in:
Nikolay Degterinsky 2023-12-05 15:14:30 +01:00 committed by GitHub
commit 7f59eb438d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 6 deletions

View File

@ -102,8 +102,12 @@ namespace
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Wrong JSON string to merge. Expected JSON object");
};
const auto * first_string = typeid_cast<const ColumnString *>(arguments[0].column.get());
if (!first_string)
const bool is_first_const = isColumnConst(*arguments[0].column);
const auto * first_column_arg_string = is_first_const
? checkAndGetColumnConstData<ColumnString>(arguments[0].column.get())
: checkAndGetColumn<ColumnString>(arguments[0].column.get());
if (!first_column_arg_string)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Arguments of function {} must be strings", getName());
std::vector<rapidjson::Document> merged_jsons;
@ -112,19 +116,29 @@ namespace
for (size_t i = 0; i < input_rows_count; ++i)
{
auto & merged_json = merged_jsons.emplace_back(rapidjson::Type::kObjectType, &allocator);
parse_json_document(*first_string, merged_json, i);
if (is_first_const)
parse_json_document(*first_column_arg_string, merged_json, 0);
else
parse_json_document(*first_column_arg_string, merged_json, i);
}
for (size_t col_idx = 1; col_idx < arguments.size(); ++col_idx)
{
const auto * column_string = typeid_cast<const ColumnString *>(arguments[col_idx].column.get());
if (!column_string)
const bool is_const = isColumnConst(*arguments[col_idx].column);
const auto * column_arg_string = is_const
? checkAndGetColumnConstData<ColumnString>(arguments[col_idx].column.get())
: checkAndGetColumn<ColumnString>(arguments[col_idx].column.get());
if (!column_arg_string)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Arguments of function {} must be strings", getName());
for (size_t i = 0; i < input_rows_count; ++i)
{
rapidjson::Document document(&allocator);
parse_json_document(*column_string, document, i);
if (is_const)
parse_json_document(*column_arg_string, document, 0);
else
parse_json_document(*column_arg_string, document, i);
merge_objects(merged_jsons[i], document);
}
}

View File

@ -0,0 +1 @@
{"id":1,"foo":["bar"]} {"id":1,"foo":["bar","baz"]}

View File

@ -0,0 +1,3 @@
-- Tags: no-fasttest
select '{"id":1,"foo":["bar"]}' as a, jsonMergePatch(a,toJSONString(map('foo',arrayPushBack(arrayMap(x->JSONExtractString(x),JSONExtractArrayRaw(a, 'foo')),'baz')))) as b;