From e59bb63efbf3b219dae958bcb56fa393af125b7f Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Mon, 7 Oct 2024 06:09:22 +0000 Subject: [PATCH] Backport #70172 to 24.8: Fix crash in JSON column --- src/Columns/ColumnObject.cpp | 23 +++++++++++++++++++ src/Columns/ColumnObject.h | 2 ++ .../03247_object_column_copy.reference | 0 .../0_stateless/03247_object_column_copy.sql | 9 ++++++++ 4 files changed, 34 insertions(+) create mode 100644 tests/queries/0_stateless/03247_object_column_copy.reference create mode 100644 tests/queries/0_stateless/03247_object_column_copy.sql diff --git a/src/Columns/ColumnObject.cpp b/src/Columns/ColumnObject.cpp index 3577ab1ec82..70eed4788fe 100644 --- a/src/Columns/ColumnObject.cpp +++ b/src/Columns/ColumnObject.cpp @@ -87,6 +87,29 @@ ColumnObject::ColumnObject( shared_data = ColumnArray::create(ColumnTuple::create(std::move(paths_and_values))); } +ColumnObject::ColumnObject(const ColumnObject & other) + : COWHelper, ColumnObject>(other) + , typed_paths(other.typed_paths) + , dynamic_paths(other.dynamic_paths) + , dynamic_paths_ptrs(other.dynamic_paths_ptrs) + , shared_data(other.shared_data) + , max_dynamic_paths(other.max_dynamic_paths) + , global_max_dynamic_paths(other.global_max_dynamic_paths) + , max_dynamic_types(other.max_dynamic_types) + , statistics(other.statistics) +{ + /// We should update string_view in sorted_typed_paths and sorted_dynamic_paths so they + /// point to the new strings in typed_paths and dynamic_paths. + sorted_typed_paths.clear(); + for (const auto & [path, _] : typed_paths) + sorted_typed_paths.emplace_back(path); + std::sort(sorted_typed_paths.begin(), sorted_typed_paths.end()); + + sorted_dynamic_paths.clear(); + for (const auto & [path, _] : dynamic_paths) + sorted_dynamic_paths.emplace(path); +} + ColumnObject::Ptr ColumnObject::create( const std::unordered_map & typed_paths_, const std::unordered_map & dynamic_paths_, diff --git a/src/Columns/ColumnObject.h b/src/Columns/ColumnObject.h index c7f282d9079..9c5ee4fa089 100644 --- a/src/Columns/ColumnObject.h +++ b/src/Columns/ColumnObject.h @@ -54,6 +54,8 @@ private: size_t max_dynamic_types_, const StatisticsPtr & statistics_ = {}); + ColumnObject(const ColumnObject & other); + /// Use StringHashForHeterogeneousLookup hash for hash maps to be able to use std::string_view in find() method. using PathToColumnMap = std::unordered_map; using PathToDynamicColumnPtrMap = std::unordered_map; diff --git a/tests/queries/0_stateless/03247_object_column_copy.reference b/tests/queries/0_stateless/03247_object_column_copy.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/03247_object_column_copy.sql b/tests/queries/0_stateless/03247_object_column_copy.sql new file mode 100644 index 00000000000..37c58217b1d --- /dev/null +++ b/tests/queries/0_stateless/03247_object_column_copy.sql @@ -0,0 +1,9 @@ +SET allow_experimental_json_type = 1; +SET allow_experimental_variant_type = 1; +DROP TABLE IF EXISTS t0; +CREATE TABLE t0 (c0 Int) ENGINE = Memory(); +INSERT INTO t0 (c0) VALUES (1); +ALTER TABLE t0 (ADD COLUMN c1 JSON(c1 Variant(Int,JSON(c1 Int)))); +INSERT INTO t0 (c0, c1) VALUES (2, '{"c1":1}'::JSON); +SELECT kafkaMurmurHash(c1) FROM t0; -- {serverError NOT_IMPLEMENTED} +DROP TABLE t0;