Backport #70374 to 24.8: Fix crash during GROUP BY JSON sub-object subcolumn

This commit is contained in:
robot-clickhouse 2024-10-08 18:07:48 +00:00
parent a0d201c044
commit 8da36da3a9
5 changed files with 29 additions and 7 deletions

View File

@ -323,6 +323,19 @@ void ColumnObject::setDynamicPaths(const std::vector<String> & paths)
}
}
void ColumnObject::setDynamicPaths(const std::vector<std::pair<String, ColumnPtr>> & paths)
{
if (paths.size() > max_dynamic_paths)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot set dynamic paths to Object column, the number of paths ({}) exceeds the limit ({})", paths.size(), max_dynamic_paths);
for (const auto & [path, column] : paths)
{
auto it = dynamic_paths.emplace(path, column).first;
dynamic_paths_ptrs[path] = assert_cast<ColumnDynamic *>(it->second.get());
sorted_dynamic_paths.insert(it->first);
}
}
void ColumnObject::insert(const Field & x)
{
const auto & object = x.safeGet<Object>();

View File

@ -222,6 +222,7 @@ public:
void addNewDynamicPath(std::string_view path);
void setDynamicPaths(const std::vector<String> & paths);
void setDynamicPaths(const std::vector<std::pair<String, ColumnPtr>> & paths);
void setMaxDynamicPaths(size_t max_dynamic_paths_);
void setStatistics(const StatisticsPtr & statistics_) { statistics = statistics_; }

View File

@ -348,17 +348,13 @@ std::unique_ptr<ISerialization::SubstreamData> DataTypeObject::getDynamicSubcolu
result_typed_columns[getSubPath(path, prefix)] = column;
}
auto & result_dynamic_columns = result_object_column.getDynamicPaths();
auto & result_dynamic_columns_ptrs = result_object_column.getDynamicPathsPtrs();
std::vector<std::pair<String, ColumnPtr>> result_dynamic_paths;
for (const auto & [path, column] : object_column.getDynamicPaths())
{
if (path.starts_with(prefix) && path.size() != prefix.size())
{
auto sub_path = getSubPath(path, prefix);
result_dynamic_columns[sub_path] = column;
result_dynamic_columns_ptrs[sub_path] = assert_cast<ColumnDynamic *>(result_dynamic_columns[sub_path].get());
}
result_dynamic_paths.emplace_back(getSubPath(path, prefix), column);
}
result_object_column.setDynamicPaths(result_dynamic_paths);
const auto & shared_data_offsets = object_column.getSharedDataOffsets();
const auto [shared_data_paths, shared_data_values] = object_column.getSharedDataPathsAndValues();

View File

@ -0,0 +1,2 @@
{"b":{"c":0,"d":"0","e":"str_0"}}
{"b":{"c":0,"d":"1","e":"str_1"}}

View File

@ -0,0 +1,10 @@
set allow_experimental_json_type = 1;
set allow_experimental_variant_type = 1;
set use_variant_as_common_type = 1;
drop table if exists test;
create table test (json JSON(max_dynamic_paths = 20, `a.b.c` UInt32)) engine = Memory;
insert into test select toJSONString(map('a.b.d', number::UInt32, 'a.b.e', 'str_' || toString(number))) from numbers(2);
select json.^a from test group by json.^a order by toString(json.^a);
drop table test;