diff --git a/src/Columns/ReverseIndex.h b/src/Columns/ReverseIndex.h index 9f13dbef1f9..1f49e2cb225 100644 --- a/src/Columns/ReverseIndex.h +++ b/src/Columns/ReverseIndex.h @@ -317,7 +317,7 @@ class ReverseIndex { public: ReverseIndex(UInt64 num_prefix_rows_to_skip_, UInt64 base_index_) - : num_prefix_rows_to_skip(num_prefix_rows_to_skip_), base_index(base_index_), saved_hash_ptr(nullptr) {} + : num_prefix_rows_to_skip(num_prefix_rows_to_skip_), base_index(base_index_), external_saved_hash_ptr(nullptr) {} void setColumn(ColumnType * column_); @@ -352,14 +352,14 @@ public: if (!use_saved_hash) return nullptr; - UInt64 * ptr = saved_hash_ptr.load(); + UInt64 * ptr = external_saved_hash_ptr.load(); if (!ptr) { auto hash = calcHashes(); ptr = &hash->getData()[0]; UInt64 * expected = nullptr; - if (saved_hash_ptr.compare_exchange_strong(expected, ptr)) - saved_hash = std::move(hash); + if (external_saved_hash_ptr.compare_exchange_strong(expected, ptr)) + external_saved_hash = std::move(hash); else ptr = expected; } @@ -379,7 +379,9 @@ private: /// Lazy initialized. std::unique_ptr index; mutable ColumnUInt64::MutablePtr saved_hash; - mutable std::atomic saved_hash_ptr; + /// For usage during GROUP BY + mutable ColumnUInt64::MutablePtr external_saved_hash; + mutable std::atomic external_saved_hash_ptr; void buildIndex(); diff --git a/tests/queries/0_stateless/02046_low_cardinality_parallel_group_by.reference b/tests/queries/0_stateless/02046_low_cardinality_parallel_group_by.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02046_low_cardinality_parallel_group_by.sh b/tests/queries/0_stateless/02046_low_cardinality_parallel_group_by.sh new file mode 100755 index 00000000000..ec58f36cede --- /dev/null +++ b/tests/queries/0_stateless/02046_low_cardinality_parallel_group_by.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +# This is the regression test for parallel usage of LowCardinality column +# via Buffer engine. +# +# See also: +# - https://github.com/ClickHouse/ClickHouse/issues/24158 +# - https://github.com/ClickHouse/ClickHouse/pull/3138 + +$CLICKHOUSE_CLIENT -q "DROP TABLE IF EXISTS low_card_buffer_test" +$CLICKHOUSE_CLIENT -q "CREATE TABLE low_card_buffer_test (test_text LowCardinality(String)) ENGINE=Buffer('', '', 16, 60, 360, 100, 1000, 10000, 100000)" + +$CLICKHOUSE_BENCHMARK -d 0 -i 1000 -c 5 <<<"SELECT count() FROM low_card_buffer_test GROUP BY test_text format Null" 2>/dev/null & +$CLICKHOUSE_BENCHMARK -d 0 -i 1000 -c 2 <<<"INSERT INTO low_card_buffer_test values('TEST1')" 2>/dev/null & +wait + +# server is alive +$CLICKHOUSE_CLIENT -q "SELECT 1 FORMAT Null"