mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 08:40:50 +00:00
Merge pull request #57379 from evillique/fix-jsonmergepatch
Fix function jsonMergePatch for partially const columns
This commit is contained in:
commit
7f59eb438d
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
{"id":1,"foo":["bar"]} {"id":1,"foo":["bar","baz"]}
|
3
tests/queries/0_stateless/02920_fix_json_merge_patch.sql
Normal file
3
tests/queries/0_stateless/02920_fix_json_merge_patch.sql
Normal 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;
|
Loading…
Reference in New Issue
Block a user