From 0a6142a039dfebb086fed6bb7cb38121ed904a33 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Sun, 7 Nov 2021 20:20:35 +0800 Subject: [PATCH 001/342] add map function mapFromArrays --- src/Functions/map.cpp | 68 +++++++++++++++++++ .../0_stateless/01651_map_functions.reference | 3 + .../0_stateless/01651_map_functions.sql | 9 ++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/Functions/map.cpp b/src/Functions/map.cpp index 03a9da404c2..994eda1ec62 100644 --- a/src/Functions/map.cpp +++ b/src/Functions/map.cpp @@ -26,6 +26,7 @@ namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; extern const int ILLEGAL_TYPE_OF_ARGUMENT; + extern const int SIZES_OF_ARRAYS_DOESNT_MATCH; } namespace @@ -144,6 +145,72 @@ public: } }; +// mapFromArrays(keys, values) is a function that allows you to make key-value pair from a pair of arrays +class FunctionMapFromArrays : public IFunction +{ +public: + static constexpr auto name = "mapFromArrays"; + + static FunctionPtr create(ContextPtr) + { + return std::make_shared(); + } + + String getName() const override + { + return name; + } + + size_t getNumberOfArguments() const override + { + return 2; + } + + bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } + bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } + + DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override + { + if (arguments.size() != 2) + throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, + "Function {} requires 2 arguments, but {} given", getName(), arguments.size()); + + const auto * keys_type = checkAndGetDataType(arguments[0].get()); + if (!keys_type) + throw Exception{"First argument for function " + getName() + " must be a Array", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT}; + + const auto * values_type = checkAndGetDataType(arguments[1].get()); + if (!values_type) + throw Exception{"Second argument for function " + getName() + " must be a Array", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT}; + + DataTypes tmp; + const auto & key_type = keys_type->getNestedType(); + const auto & value_type = values_type->getNestedType(); + tmp.emplace_back(key_type); + tmp.emplace_back(value_type); + return std::make_shared(tmp); + } + + ColumnPtr executeImpl( + const ColumnsWithTypeAndName & arguments, const DataTypePtr & /* result_type */, size_t /* input_rows_count */) const override + { + const auto * col_keys = typeid_cast(arguments[0].column.get()); + const auto * col_values = typeid_cast(arguments[1].column.get()); + if (!col_keys->hasEqualOffsets(*col_values)) + throw Exception("Array arguments for function " + getName() + " must have equal sizes", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH); + + const auto & data_keys = col_keys->getDataPtr(); + const auto & data_values = col_values->getDataPtr(); + const auto & offsets = col_keys->getOffsetsPtr(); + auto nested_column = ColumnArray::create( + ColumnTuple::create(Columns{std::move(data_keys), std::move(data_values)}), + std::move(offsets)); + return ColumnMap::create(nested_column); + } +}; struct NameMapContains { static constexpr auto name = "mapContains"; }; @@ -528,6 +595,7 @@ void registerFunctionsMap(FunctionFactory & factory) factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); + factory.registerFunction(); } } diff --git a/tests/queries/0_stateless/01651_map_functions.reference b/tests/queries/0_stateless/01651_map_functions.reference index 06adaf48cfd..30579f8c4ee 100644 --- a/tests/queries/0_stateless/01651_map_functions.reference +++ b/tests/queries/0_stateless/01651_map_functions.reference @@ -8,6 +8,8 @@ 0 ['name','age'] ['name','gender'] +{'name':'zhangsan','age':'10'} +{'name':'lisi','gender':'female'} 1 0 0 1 0 1 1 0 0 @@ -21,3 +23,4 @@ {'aa':4,'bb':5} 1 0 {0:0} 1 {0:0} 0 +{'aa':4,'bb':5} diff --git a/tests/queries/0_stateless/01651_map_functions.sql b/tests/queries/0_stateless/01651_map_functions.sql index bbaaf9bee84..83ee42f17a5 100644 --- a/tests/queries/0_stateless/01651_map_functions.sql +++ b/tests/queries/0_stateless/01651_map_functions.sql @@ -2,14 +2,15 @@ set allow_experimental_map_type = 1; -- String type drop table if exists table_map; -create table table_map (a Map(String, String), b String) engine = Memory; -insert into table_map values ({'name':'zhangsan', 'age':'10'}, 'name'), ({'name':'lisi', 'gender':'female'},'age'); +create table table_map (a Map(String, String), b String, c Array(String), d Array(String)) engine = Memory; +insert into table_map values ({'name':'zhangsan', 'age':'10'}, 'name', ['name', 'age'], ['zhangsan', '10']), ({'name':'lisi', 'gender':'female'},'age',['name', 'gender'], ['lisi', 'female']); select mapContains(a, 'name') from table_map; select mapContains(a, 'gender') from table_map; select mapContains(a, 'abc') from table_map; select mapContains(a, b) from table_map; select mapContains(a, 10) from table_map; -- { serverError 386 } select mapKeys(a) from table_map; +select mapFromArrays(c, d) from table_map; drop table if exists table_map; CREATE TABLE table_map (a Map(UInt8, Int), b UInt8, c UInt32) engine = MergeTree order by tuple(); @@ -27,3 +28,7 @@ select map( 'aa', 4, 'bb' , 5) as m, mapKeys(m), mapValues(m); select map( 'aa', 4, 'bb' , 5) as m, mapContains(m, 'aa'), mapContains(m, 'k'); select map(0, 0) as m, mapContains(m, number % 2) from numbers(2); + +select mapFromArrays(['aa', 'bb'], [4, 5]); +select mapFromArrays(['aa', 'bb'], [4, 5], [6, 7]); -- { serverError 42 } +select mapFromArrays(['aa', 'bb'], [4, 5, 6]); -- { serverError 190 } From 38a1afaca70d93c562b8e71f05a55fb7465f170b Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Sun, 7 Nov 2021 20:51:14 +0800 Subject: [PATCH 002/342] add docs --- .../functions/tuple-map-functions.md | 32 +++++++++++++++++++ .../0_stateless/01651_map_functions.reference | 3 ++ .../0_stateless/01651_map_functions.sql | 5 +-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/en/sql-reference/functions/tuple-map-functions.md b/docs/en/sql-reference/functions/tuple-map-functions.md index 47e33806930..3c86dc9c9bb 100644 --- a/docs/en/sql-reference/functions/tuple-map-functions.md +++ b/docs/en/sql-reference/functions/tuple-map-functions.md @@ -429,6 +429,38 @@ Result: │ {} │ └────────────────────────────┘ ``` + + +## mapFromArrays {#mapFromArrays} + +**Syntax** + +```sql +mapFromArrays(keys, values) +``` + +**Parameters** + +- `keys` — Given key array to create a map from. +- `values` - Given value array to create a map from. + +**Returned value** + +- A map whose keys and values are from input arrays respectively. + +**Example** + +Query: + +```sql +select mapFromArrays(['a', 'b', 'c'], [1, 2, 3]) +``` + +```text +┌─mapFromArrays(['a', 'b', 'c'], [1, 2, 3])─┐ +│ {'a':1,'b':2,'c':3} │ +└───────────────────────────────────────────┘ +``` [Original article](https://clickhouse.com/docs/en/sql-reference/functions/tuple-map-functions/) diff --git a/tests/queries/0_stateless/01651_map_functions.reference b/tests/queries/0_stateless/01651_map_functions.reference index 30579f8c4ee..05dcb8f2cf5 100644 --- a/tests/queries/0_stateless/01651_map_functions.reference +++ b/tests/queries/0_stateless/01651_map_functions.reference @@ -19,6 +19,9 @@ [1000] [1001] [1002] +{'1000':'2000','1000':'3000','1000':'4000'} +{'1001':'2002','1001':'3003','1001':'4004'} +{'1002':'2004','1002':'3006','1002':'4008'} {'aa':4,'bb':5} ['aa','bb'] [4,5] {'aa':4,'bb':5} 1 0 {0:0} 1 diff --git a/tests/queries/0_stateless/01651_map_functions.sql b/tests/queries/0_stateless/01651_map_functions.sql index 83ee42f17a5..6ecd5bce494 100644 --- a/tests/queries/0_stateless/01651_map_functions.sql +++ b/tests/queries/0_stateless/01651_map_functions.sql @@ -13,13 +13,14 @@ select mapKeys(a) from table_map; select mapFromArrays(c, d) from table_map; drop table if exists table_map; -CREATE TABLE table_map (a Map(UInt8, Int), b UInt8, c UInt32) engine = MergeTree order by tuple(); -insert into table_map select map(number, number), number, number from numbers(1000, 3); +CREATE TABLE table_map (a Map(UInt8, Int), b UInt8, c UInt32, d Array(String), e Array(String)) engine = MergeTree order by tuple(); +insert into table_map select map(number, number), number, number, [number, number, number], [number*2, number*3, number*4] from numbers(1000, 3); select mapContains(a, b), mapContains(a, c), mapContains(a, 233) from table_map; select mapContains(a, 'aaa') from table_map; -- { serverError 386 } select mapContains(b, 'aaa') from table_map; -- { serverError 43 } select mapKeys(a) from table_map; select mapValues(a) from table_map; +select mapFromArrays(d, e) from table_map; drop table if exists table_map; From 21aadec53de0d460c48678600e98509ba6f8f0b8 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Sun, 7 Nov 2021 20:59:46 +0800 Subject: [PATCH 003/342] fix code style --- src/Functions/map.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Functions/map.cpp b/src/Functions/map.cpp index 994eda1ec62..a3a33453e0c 100644 --- a/src/Functions/map.cpp +++ b/src/Functions/map.cpp @@ -175,7 +175,7 @@ public: if (arguments.size() != 2) throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires 2 arguments, but {} given", getName(), arguments.size()); - + const auto * keys_type = checkAndGetDataType(arguments[0].get()); if (!keys_type) throw Exception{"First argument for function " + getName() + " must be a Array", From 42646c0bba6fe0140fd39567f1407b798c12b5ee Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Mon, 8 Nov 2021 12:27:31 +0800 Subject: [PATCH 004/342] fix bug --- src/Functions/map.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Functions/map.cpp b/src/Functions/map.cpp index a3a33453e0c..3756c296bb4 100644 --- a/src/Functions/map.cpp +++ b/src/Functions/map.cpp @@ -27,6 +27,7 @@ namespace ErrorCodes extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int SIZES_OF_ARRAYS_DOESNT_MATCH; + extern const int ILLEGAL_COLUMN; } namespace @@ -197,8 +198,33 @@ public: ColumnPtr executeImpl( const ColumnsWithTypeAndName & arguments, const DataTypePtr & /* result_type */, size_t /* input_rows_count */) const override { - const auto * col_keys = typeid_cast(arguments[0].column.get()); - const auto * col_values = typeid_cast(arguments[1].column.get()); + bool is_keys_const = isColumnConst(*arguments[0].column); + const ColumnArray * col_keys; + if (is_keys_const) + { + auto tmp = arguments[0].column->convertToFullColumnIfConst(); + col_keys = checkAndGetColumn(tmp.get()); + } + else + { + col_keys = checkAndGetColumn(arguments[0].column.get()); + } + + bool is_values_const = isColumnConst(*arguments[1].column); + const ColumnArray * col_values; + if (is_values_const) + { + auto tmp = arguments[1].column->convertToFullColumnIfConst(); + col_values = checkAndGetColumn(tmp.get()); + } + else + { + col_values = checkAndGetColumn(arguments[1].column.get()); + } + + if (!col_keys || !col_values) + throw Exception("Arguments of function " + getName() + " must be array.", ErrorCodes::ILLEGAL_COLUMN); + if (!col_keys->hasEqualOffsets(*col_values)) throw Exception("Array arguments for function " + getName() + " must have equal sizes", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH); From 12834902c060dd941ae486330950ff32ed1a1f85 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Mon, 8 Nov 2021 17:04:33 +0800 Subject: [PATCH 005/342] fix mem leak --- src/Functions/map.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Functions/map.cpp b/src/Functions/map.cpp index 3756c296bb4..4f2e072d08a 100644 --- a/src/Functions/map.cpp +++ b/src/Functions/map.cpp @@ -198,24 +198,26 @@ public: ColumnPtr executeImpl( const ColumnsWithTypeAndName & arguments, const DataTypePtr & /* result_type */, size_t /* input_rows_count */) const override { + ColumnPtr holder_keys; bool is_keys_const = isColumnConst(*arguments[0].column); const ColumnArray * col_keys; if (is_keys_const) { - auto tmp = arguments[0].column->convertToFullColumnIfConst(); - col_keys = checkAndGetColumn(tmp.get()); + holder_keys = arguments[0].column->convertToFullColumnIfConst(); + col_keys = checkAndGetColumn(holder_keys.get()); } else { col_keys = checkAndGetColumn(arguments[0].column.get()); } + ColumnPtr holder_values; bool is_values_const = isColumnConst(*arguments[1].column); const ColumnArray * col_values; if (is_values_const) { - auto tmp = arguments[1].column->convertToFullColumnIfConst(); - col_values = checkAndGetColumn(tmp.get()); + holder_values = arguments[1].column->convertToFullColumnIfConst(); + col_values = checkAndGetColumn(holder_values.get()); } else { From 8c93e08fd4ff8a4d5ee137e130ab020ba1519ec4 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Mon, 8 Nov 2021 17:09:23 +0800 Subject: [PATCH 006/342] add some full columns test cases --- tests/queries/0_stateless/01651_map_functions.reference | 4 ++++ tests/queries/0_stateless/01651_map_functions.sql | 2 ++ 2 files changed, 6 insertions(+) diff --git a/tests/queries/0_stateless/01651_map_functions.reference b/tests/queries/0_stateless/01651_map_functions.reference index 05dcb8f2cf5..284e27a1b0d 100644 --- a/tests/queries/0_stateless/01651_map_functions.reference +++ b/tests/queries/0_stateless/01651_map_functions.reference @@ -27,3 +27,7 @@ {0:0} 1 {0:0} 0 {'aa':4,'bb':5} +{'aa':4,'bb':5} +{'aa':4,'bb':5} +{'aa':4,'bb':5} +{'aa':4,'bb':5} diff --git a/tests/queries/0_stateless/01651_map_functions.sql b/tests/queries/0_stateless/01651_map_functions.sql index 6ecd5bce494..57023d41470 100644 --- a/tests/queries/0_stateless/01651_map_functions.sql +++ b/tests/queries/0_stateless/01651_map_functions.sql @@ -31,5 +31,7 @@ select map( 'aa', 4, 'bb' , 5) as m, mapContains(m, 'aa'), mapContains(m, 'k'); select map(0, 0) as m, mapContains(m, number % 2) from numbers(2); select mapFromArrays(['aa', 'bb'], [4, 5]); +select mapFromArrays(['aa', 'bb'], materialize([4, 5])); +select mapFromArrays(['aa', 'bb'], materialize([4, 5])) from numbers(3); select mapFromArrays(['aa', 'bb'], [4, 5], [6, 7]); -- { serverError 42 } select mapFromArrays(['aa', 'bb'], [4, 5, 6]); -- { serverError 190 } From fbe4808b6b73523d756bcebeb8ffb0e38b6a2084 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 10 Jan 2023 12:28:13 +0000 Subject: [PATCH 007/342] Add support for custom key in parallel replicas --- src/Core/Settings.h | 2 + src/Core/SettingsEnums.cpp | 4 ++ src/Core/SettingsEnums.h | 9 +++ src/Interpreters/Cluster.cpp | 1 - src/Interpreters/InterpreterSelectQuery.cpp | 15 +++++ .../MergeTree/MergeTreeDataSelectExecutor.cpp | 60 +++++++++++++++++-- src/Storages/SelectQueryInfo.h | 2 + 7 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 2357948a1f6..a714fc449b7 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -153,6 +153,8 @@ static constexpr UInt64 operator""_GiB(unsigned long long value) M(UInt64, max_parallel_replicas, 1, "The maximum number of replicas of each shard used when the query is executed. For consistency (to get different parts of the same partition), this option only works for the specified sampling key. The lag of the replicas is not controlled.", 0) \ M(UInt64, parallel_replicas_count, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the number of parallel replicas participating in query processing.", 0) \ M(UInt64, parallel_replica_offset, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the index of the replica participating in query processing among parallel replicas.", 0) \ + M(String, parallel_replicas_custom_key, "", "Custom key for parallel replicas", 0) \ + M(ParallelReplicasMode, parallel_replicas_mode, ParallelReplicasMode::SAMPLE_KEY, "How to process query using multiple replicas.", 0) \ \ M(Bool, allow_experimental_parallel_reading_from_replicas, false, "If true, ClickHouse will send a SELECT query to all replicas of a table. It will work for any kind on MergeTree table.", 0) \ \ diff --git a/src/Core/SettingsEnums.cpp b/src/Core/SettingsEnums.cpp index 632587106a1..eaeed9dc44c 100644 --- a/src/Core/SettingsEnums.cpp +++ b/src/Core/SettingsEnums.cpp @@ -162,4 +162,8 @@ IMPLEMENT_SETTING_ENUM(MsgPackUUIDRepresentation , ErrorCodes::BAD_ARGUMENTS, IMPLEMENT_SETTING_ENUM(Dialect, ErrorCodes::BAD_ARGUMENTS, {{"clickhouse", Dialect::clickhouse}, {"kusto", Dialect::kusto}}) + +IMPLEMENT_SETTING_ENUM(ParallelReplicasMode, ErrorCodes::BAD_ARGUMENTS, + {{"sample_key", ParallelReplicasMode::SAMPLE_KEY}, + {"custom_key", ParallelReplicasMode::CUSTOM_KEY}}) } diff --git a/src/Core/SettingsEnums.h b/src/Core/SettingsEnums.h index 97c4275c4d2..a2041634d4b 100644 --- a/src/Core/SettingsEnums.h +++ b/src/Core/SettingsEnums.h @@ -191,4 +191,13 @@ enum class Dialect }; DECLARE_SETTING_ENUM(Dialect) + +enum class ParallelReplicasMode : uint8_t +{ + SAMPLE_KEY, + CUSTOM_KEY, +}; + +DECLARE_SETTING_ENUM(ParallelReplicasMode) + } diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index b76434b23e7..9f0a9d3b35c 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -509,7 +509,6 @@ Cluster::Cluster(const Poco::Util::AbstractConfiguration & config, shard_local_addresses.push_back(replica); shard_all_addresses.push_back(replica); } - ConnectionPoolWithFailoverPtr shard_pool = std::make_shared( all_replicas_pools, settings.load_balancing, settings.distributed_replica_error_half_life.totalSeconds(), settings.distributed_replica_error_cap); diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index ba2c845ead1..2d0436fa9d3 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -319,6 +319,15 @@ static ASTPtr parseAdditionalFilterConditionForTable( return nullptr; } +static ASTPtr parseParallelReplicaCustomKey(const String & setting, const Context & context) +{ + ParserExpression parser; + const auto & settings = context.getSettingsRef(); + return parseQuery( + parser, setting.data(), setting.data() + setting.size(), + "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); +} + /// Returns true if we should ignore quotas and limits for a specified table in the system database. static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) { @@ -501,6 +510,12 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.additional_filter_ast = parseAdditionalFilterConditionForTable( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); + if (settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY && !settings.parallel_replicas_custom_key.value.empty()) + { + query_info.parallel_replica_custom_key_ast = parseParallelReplicaCustomKey( + settings.parallel_replicas_custom_key, *context); + } + auto analyze = [&] (bool try_move_to_prewhere) { /// Allow push down and other optimizations for VIEW: replace with subquery and rewrite it. diff --git a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index 1ca1779e4b0..3d48c333727 100644 --- a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,8 @@ #include #include +#include +#include namespace DB { @@ -61,6 +64,7 @@ namespace ErrorCodes extern const int DUPLICATED_PART_UUIDS; extern const int NO_SUCH_COLUMN_IN_TABLE; extern const int PROJECTION_NOT_USED; + extern const int BAD_ARGUMENTS; } @@ -470,6 +474,25 @@ QueryPlanPtr MergeTreeDataSelectExecutor::read( return plan; } +namespace +{ + +bool supportsSamplingForParallelReplicas(const SelectQueryInfo & select_query_info, const MergeTreeData & data, const Settings & settings) +{ + if (settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + { + /// maybe just don't use sampling or try to fallback to SAMPLE_KEY? + if (select_query_info.parallel_replica_custom_key_ast == nullptr) + throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but no 'parallel_replicas_custom_key' defined"); + + return true; + } + + return data.supportsSampling(); +} + +} + MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( const SelectQueryInfo & select_query_info, NamesAndTypesList available_real_columns, @@ -587,9 +610,10 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( * It is also important that the entire universe can be covered using SAMPLE 0.1 OFFSET 0, ... OFFSET 0.9 and similar decimals. */ + bool supports_sampling_for_parallel_replicas = supportsSamplingForParallelReplicas(select_query_info, data, settings); /// Parallel replicas has been requested but there is no way to sample data. /// Select all data from first replica and no data from other replicas. - if (settings.parallel_replicas_count > 1 && !data.supportsSampling() && settings.parallel_replica_offset > 0) + if (settings.parallel_replicas_count > 1 && !supports_sampling_for_parallel_replicas && settings.parallel_replica_offset > 0) { LOG_DEBUG(log, "Will use no data on this replica because parallel replicas processing has been requested" " (the setting 'max_parallel_replicas') but the table does not support sampling and this replica is not the first."); @@ -597,16 +621,34 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( return sampling; } - sampling.use_sampling = relative_sample_size > 0 || (settings.parallel_replicas_count > 1 && data.supportsSampling()); + sampling.use_sampling = relative_sample_size > 0 || (settings.parallel_replicas_count > 1 && supports_sampling_for_parallel_replicas); bool no_data = false; /// There is nothing left after sampling. + std::optional parallel_replicas_custom_key_description; + const auto get_sampling_key = [&]() -> const KeyDescription & + { + if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + { + assert(select_query_info.parallel_replica_custom_key_ast); + + LOG_INFO(log, "Using custom key for sampling while processing with multiple replicas"); + + if (!parallel_replicas_custom_key_description) + parallel_replicas_custom_key_description = KeyDescription::getKeyFromAST(select_query_info.parallel_replica_custom_key_ast, metadata_snapshot->columns, context); + + return *parallel_replicas_custom_key_description; + } + + return metadata_snapshot->getSamplingKey(); + }; + if (sampling.use_sampling) { if (sample_factor_column_queried && relative_sample_size != RelativeSize(0)) sampling.used_sample_factor = 1.0 / boost::rational_cast(relative_sample_size); RelativeSize size_of_universum = 0; - const auto & sampling_key = metadata_snapshot->getSamplingKey(); + const auto & sampling_key = get_sampling_key(); DataTypePtr sampling_column_type = sampling_key.data_types[0]; if (sampling_key.data_types.size() == 1) @@ -681,7 +723,7 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( /// If sample and final are used together no need to calculate sampling expression twice. /// The first time it was calculated for final, because sample key is a part of the PK. /// So, assume that we already have calculated column. - ASTPtr sampling_key_ast = metadata_snapshot->getSamplingKeyAST(); + ASTPtr sampling_key_ast = sampling_key.definition_ast; if (final) { @@ -693,8 +735,11 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( if (has_lower_limit) { if (!key_condition.addCondition( - sampling_key.column_names[0], Range::createLeftBounded(lower, true, sampling_key.data_types[0]->isNullable()))) + sampling_key.column_names[0], Range::createLeftBounded(lower, true, sampling_key.data_types[0]->isNullable())) + && (settings.parallel_replicas_count <= 1 || settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY)) + { throw Exception("Sampling column not in primary key", ErrorCodes::ILLEGAL_COLUMN); + } ASTPtr args = std::make_shared(); args->children.push_back(sampling_key_ast); @@ -711,8 +756,11 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( if (has_upper_limit) { if (!key_condition.addCondition( - sampling_key.column_names[0], Range::createRightBounded(upper, false, sampling_key.data_types[0]->isNullable()))) + sampling_key.column_names[0], Range::createRightBounded(upper, false, sampling_key.data_types[0]->isNullable())) + && (settings.parallel_replicas_count <= 1 || settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY)) + { throw Exception("Sampling column not in primary key", ErrorCodes::ILLEGAL_COLUMN); + } ASTPtr args = std::make_shared(); args->children.push_back(sampling_key_ast); diff --git a/src/Storages/SelectQueryInfo.h b/src/Storages/SelectQueryInfo.h index a8a8ae4e877..ec3af64d93a 100644 --- a/src/Storages/SelectQueryInfo.h +++ b/src/Storages/SelectQueryInfo.h @@ -209,6 +209,8 @@ struct SelectQueryInfo /// It is needed for PK analysis based on row_level_policy and additional_filters. ASTs filter_asts; + ASTPtr parallel_replica_custom_key_ast; + /// Filter actions dag for current storage ActionsDAGPtr filter_actions_dag; From 19e5988d3312194be35ab0d26c5c2966890a643a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Wed, 11 Jan 2023 09:57:13 +0000 Subject: [PATCH 008/342] Better --- src/Interpreters/InterpreterSelectQuery.cpp | 63 ++++++++++++++++--- src/Interpreters/InterpreterSelectQuery.h | 2 + .../MergeTree/MergeTreeDataSelectExecutor.cpp | 60 ++---------------- 3 files changed, 61 insertions(+), 64 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 2d0436fa9d3..1cd5d0004fa 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -510,10 +510,37 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.additional_filter_ast = parseAdditionalFilterConditionForTable( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); - if (settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY && !settings.parallel_replicas_custom_key.value.empty()) + + ASTPtr parallel_replicas_custom_filter_ast = nullptr; + if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) { - query_info.parallel_replica_custom_key_ast = parseParallelReplicaCustomKey( + if (settings.parallel_replicas_custom_key.value.empty()) + throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); + + auto custom_key_ast = parseParallelReplicaCustomKey( settings.parallel_replicas_custom_key, *context); + + // first we do modulo with replica count + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(settings.parallel_replicas_count.value)); + + auto modulo_function = std::make_shared(); + modulo_function->name = "positiveModulo"; + modulo_function->arguments = args; + modulo_function->children.push_back(modulo_function->arguments); + + /// then we compare result to the current replica number (offset) + args = std::make_shared(); + args->children.push_back(modulo_function); + args->children.push_back(std::make_shared(settings.parallel_replica_offset.value)); + + auto equals_function = std::make_shared(); + equals_function->name = "equals"; + equals_function->arguments = args; + equals_function->children.push_back(equals_function->arguments); + + parallel_replicas_custom_filter_ast = equals_function; } auto analyze = [&] (bool try_move_to_prewhere) @@ -653,6 +680,16 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.filter_asts.push_back(query_info.additional_filter_ast); } + if (parallel_replicas_custom_filter_ast) + { + custom_key_filter_info = generateFilterActions( + table_id, parallel_replicas_custom_filter_ast, context, storage, storage_snapshot, metadata_snapshot, required_columns, + prepared_sets); + + custom_key_filter_info->do_remove_column = true; + query_info.filter_asts.push_back(parallel_replicas_custom_filter_ast); + } + source_header = storage_snapshot->getSampleBlockForColumns(required_columns); } @@ -1396,17 +1433,23 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, std::optional

( + auto filter_step = std::make_unique( query_plan.getCurrentDataStream(), - additional_filter_info->actions, - additional_filter_info->column_name, - additional_filter_info->do_remove_column); + new_filter_info->actions, + new_filter_info->column_name, + new_filter_info->do_remove_column); - additional_filter_step->setStepDescription("Additional filter"); - query_plan.addStep(std::move(additional_filter_step)); - } + filter_step->setStepDescription(description); + query_plan.addStep(std::move(filter_step)); + }; + + if (additional_filter_info) + add_filter_step(additional_filter_info, "Additional filter"); + + if (custom_key_filter_info) + add_filter_step(custom_key_filter_info, "Paralel replica custom key filter"); if (expressions.before_array_join) { diff --git a/src/Interpreters/InterpreterSelectQuery.h b/src/Interpreters/InterpreterSelectQuery.h index 761eea8e1b8..5667c0e1fa7 100644 --- a/src/Interpreters/InterpreterSelectQuery.h +++ b/src/Interpreters/InterpreterSelectQuery.h @@ -224,6 +224,8 @@ private: /// For additional_filter setting. FilterDAGInfoPtr additional_filter_info; + FilterDAGInfoPtr custom_key_filter_info; + QueryProcessingStage::Enum from_stage = QueryProcessingStage::FetchColumns; /// List of columns to read to execute the query. diff --git a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index 3d48c333727..d55e3f86f30 100644 --- a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -34,7 +34,6 @@ #include #include -#include #include #include #include @@ -45,8 +44,6 @@ #include #include -#include -#include namespace DB { @@ -64,7 +61,6 @@ namespace ErrorCodes extern const int DUPLICATED_PART_UUIDS; extern const int NO_SUCH_COLUMN_IN_TABLE; extern const int PROJECTION_NOT_USED; - extern const int BAD_ARGUMENTS; } @@ -474,25 +470,6 @@ QueryPlanPtr MergeTreeDataSelectExecutor::read( return plan; } -namespace -{ - -bool supportsSamplingForParallelReplicas(const SelectQueryInfo & select_query_info, const MergeTreeData & data, const Settings & settings) -{ - if (settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) - { - /// maybe just don't use sampling or try to fallback to SAMPLE_KEY? - if (select_query_info.parallel_replica_custom_key_ast == nullptr) - throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but no 'parallel_replicas_custom_key' defined"); - - return true; - } - - return data.supportsSampling(); -} - -} - MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( const SelectQueryInfo & select_query_info, NamesAndTypesList available_real_columns, @@ -610,10 +587,9 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( * It is also important that the entire universe can be covered using SAMPLE 0.1 OFFSET 0, ... OFFSET 0.9 and similar decimals. */ - bool supports_sampling_for_parallel_replicas = supportsSamplingForParallelReplicas(select_query_info, data, settings); /// Parallel replicas has been requested but there is no way to sample data. /// Select all data from first replica and no data from other replicas. - if (settings.parallel_replicas_count > 1 && !supports_sampling_for_parallel_replicas && settings.parallel_replica_offset > 0) + if (settings.parallel_replicas_count > 1 && !data.supportsSampling() && settings.parallel_replica_offset > 0) { LOG_DEBUG(log, "Will use no data on this replica because parallel replicas processing has been requested" " (the setting 'max_parallel_replicas') but the table does not support sampling and this replica is not the first."); @@ -621,34 +597,16 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( return sampling; } - sampling.use_sampling = relative_sample_size > 0 || (settings.parallel_replicas_count > 1 && supports_sampling_for_parallel_replicas); + sampling.use_sampling = relative_sample_size > 0 || (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY && data.supportsSampling()); bool no_data = false; /// There is nothing left after sampling. - std::optional parallel_replicas_custom_key_description; - const auto get_sampling_key = [&]() -> const KeyDescription & - { - if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) - { - assert(select_query_info.parallel_replica_custom_key_ast); - - LOG_INFO(log, "Using custom key for sampling while processing with multiple replicas"); - - if (!parallel_replicas_custom_key_description) - parallel_replicas_custom_key_description = KeyDescription::getKeyFromAST(select_query_info.parallel_replica_custom_key_ast, metadata_snapshot->columns, context); - - return *parallel_replicas_custom_key_description; - } - - return metadata_snapshot->getSamplingKey(); - }; - if (sampling.use_sampling) { if (sample_factor_column_queried && relative_sample_size != RelativeSize(0)) sampling.used_sample_factor = 1.0 / boost::rational_cast(relative_sample_size); RelativeSize size_of_universum = 0; - const auto & sampling_key = get_sampling_key(); + const auto & sampling_key = metadata_snapshot->getSamplingKey(); DataTypePtr sampling_column_type = sampling_key.data_types[0]; if (sampling_key.data_types.size() == 1) @@ -723,7 +681,7 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( /// If sample and final are used together no need to calculate sampling expression twice. /// The first time it was calculated for final, because sample key is a part of the PK. /// So, assume that we already have calculated column. - ASTPtr sampling_key_ast = sampling_key.definition_ast; + ASTPtr sampling_key_ast = metadata_snapshot->getSamplingKeyAST(); if (final) { @@ -735,11 +693,8 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( if (has_lower_limit) { if (!key_condition.addCondition( - sampling_key.column_names[0], Range::createLeftBounded(lower, true, sampling_key.data_types[0]->isNullable())) - && (settings.parallel_replicas_count <= 1 || settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY)) - { + sampling_key.column_names[0], Range::createLeftBounded(lower, true, sampling_key.data_types[0]->isNullable()))) throw Exception("Sampling column not in primary key", ErrorCodes::ILLEGAL_COLUMN); - } ASTPtr args = std::make_shared(); args->children.push_back(sampling_key_ast); @@ -756,11 +711,8 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( if (has_upper_limit) { if (!key_condition.addCondition( - sampling_key.column_names[0], Range::createRightBounded(upper, false, sampling_key.data_types[0]->isNullable())) - && (settings.parallel_replicas_count <= 1 || settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY)) - { + sampling_key.column_names[0], Range::createRightBounded(upper, false, sampling_key.data_types[0]->isNullable()))) throw Exception("Sampling column not in primary key", ErrorCodes::ILLEGAL_COLUMN); - } ASTPtr args = std::make_shared(); args->children.push_back(sampling_key_ast); From f950b75ef0adb6543b2c1f24117d51b35482f1bd Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Wed, 11 Jan 2023 10:42:01 +0000 Subject: [PATCH 009/342] Fix --- src/Interpreters/InterpreterSelectQuery.cpp | 9 +++++---- src/Interpreters/InterpreterSelectQuery.h | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 1cd5d0004fa..8f03d83c1c9 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -112,6 +112,7 @@ namespace ErrorCodes extern const int INVALID_WITH_FILL_EXPRESSION; extern const int ACCESS_DENIED; extern const int UNKNOWN_IDENTIFIER; + extern const int BAD_ARGUMENTS; } /// Assumes `storage` is set and the table filter (row-level security) is not empty. @@ -682,11 +683,11 @@ InterpreterSelectQuery::InterpreterSelectQuery( if (parallel_replicas_custom_filter_ast) { - custom_key_filter_info = generateFilterActions( + parallel_replicas_custom_filter_info = generateFilterActions( table_id, parallel_replicas_custom_filter_ast, context, storage, storage_snapshot, metadata_snapshot, required_columns, prepared_sets); - custom_key_filter_info->do_remove_column = true; + parallel_replicas_custom_filter_info->do_remove_column = true; query_info.filter_asts.push_back(parallel_replicas_custom_filter_ast); } @@ -1448,8 +1449,8 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, std::optional

Date: Thu, 12 Jan 2023 07:56:15 +0000 Subject: [PATCH 010/342] Add READ_TASKS mode --- src/Core/SettingsEnums.cpp | 3 ++- src/Core/SettingsEnums.h | 1 + .../MergeTree/MergeTreeDataSelectExecutor.cpp | 13 +++++++++---- src/Storages/StorageDistributed.cpp | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Core/SettingsEnums.cpp b/src/Core/SettingsEnums.cpp index eaeed9dc44c..18abac0fe4a 100644 --- a/src/Core/SettingsEnums.cpp +++ b/src/Core/SettingsEnums.cpp @@ -165,5 +165,6 @@ IMPLEMENT_SETTING_ENUM(Dialect, ErrorCodes::BAD_ARGUMENTS, IMPLEMENT_SETTING_ENUM(ParallelReplicasMode, ErrorCodes::BAD_ARGUMENTS, {{"sample_key", ParallelReplicasMode::SAMPLE_KEY}, - {"custom_key", ParallelReplicasMode::CUSTOM_KEY}}) + {"custom_key", ParallelReplicasMode::CUSTOM_KEY}, + {"read_tasks", ParallelReplicasMode::READ_TASKS}}) } diff --git a/src/Core/SettingsEnums.h b/src/Core/SettingsEnums.h index a2041634d4b..4acf2889e37 100644 --- a/src/Core/SettingsEnums.h +++ b/src/Core/SettingsEnums.h @@ -196,6 +196,7 @@ enum class ParallelReplicasMode : uint8_t { SAMPLE_KEY, CUSTOM_KEY, + READ_TASKS, }; DECLARE_SETTING_ENUM(ParallelReplicasMode) diff --git a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index d55e3f86f30..3a4e509163b 100644 --- a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -589,16 +589,21 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( /// Parallel replicas has been requested but there is no way to sample data. /// Select all data from first replica and no data from other replicas. - if (settings.parallel_replicas_count > 1 && !data.supportsSampling() && settings.parallel_replica_offset > 0) + if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY + && !data.supportsSampling() && settings.parallel_replica_offset > 0) { - LOG_DEBUG(log, "Will use no data on this replica because parallel replicas processing has been requested" + LOG_DEBUG( + log, + "Will use no data on this replica because parallel replicas processing has been requested" " (the setting 'max_parallel_replicas') but the table does not support sampling and this replica is not the first."); sampling.read_nothing = true; return sampling; } - sampling.use_sampling = relative_sample_size > 0 || (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY && data.supportsSampling()); - bool no_data = false; /// There is nothing left after sampling. + sampling.use_sampling = relative_sample_size > 0 + || (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY + && data.supportsSampling()); + bool no_data = false; /// There is nothing left after sampling. if (sampling.use_sampling) { diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 6473e00cdf2..04b98fa8d20 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -697,7 +697,8 @@ void StorageDistributed::read( auto settings = local_context->getSettingsRef(); - bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas && !settings.use_hedged_requests; + bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas + && !settings.use_hedged_requests && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS; if (parallel_replicas) ClusterProxy::executeQueryWithParallelReplicas( From 5589bec33beeced38d3088c8e70800ca5c71a9eb Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 13 Jan 2023 11:35:37 +0000 Subject: [PATCH 011/342] Update tests --- ...4_shard_distributed_with_many_replicas.sql | 1 - ...here_max_parallel_replicas_distributed.sql | 2 -- .../01034_sample_final_distributed.sql | 2 -- ...099_parallel_distributed_insert_select.sql | 1 - .../01517_select_final_distributed.sql | 2 -- .../01557_max_parallel_replicas_no_sample.sql | 2 -- .../02221_parallel_replicas_bug.sh | 2 +- ...arallel_reading_from_replicas_benchmark.sh | 1 + .../02404_memory_bound_merging.sql | 1 + ...max_parallel_replicas_custom_key.reference | 0 ...02527_max_parallel_replicas_custom_key.sql | 36 +++++++++++++++++++ 11 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference create mode 100644 tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql diff --git a/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql b/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql index 901b818cbc0..ab4c433ba47 100644 --- a/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql +++ b/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql @@ -1,6 +1,5 @@ -- Tags: replica, distributed -SET allow_experimental_parallel_reading_from_replicas = 0; SET max_parallel_replicas = 2; DROP TABLE IF EXISTS report; diff --git a/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql b/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql index 86c84427297..b26c9af14a9 100644 --- a/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql +++ b/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql @@ -1,7 +1,5 @@ -- Tags: replica, distributed -set allow_experimental_parallel_reading_from_replicas=0; - drop table if exists test_max_parallel_replicas_lr; -- If you wonder why the table is named with "_lr" suffix in this test. diff --git a/tests/queries/0_stateless/01034_sample_final_distributed.sql b/tests/queries/0_stateless/01034_sample_final_distributed.sql index a81fef645db..b784b35cbb3 100644 --- a/tests/queries/0_stateless/01034_sample_final_distributed.sql +++ b/tests/queries/0_stateless/01034_sample_final_distributed.sql @@ -1,7 +1,5 @@ -- Tags: distributed -set allow_experimental_parallel_reading_from_replicas = 0; - drop table if exists sample_final; create table sample_final (CounterID UInt32, EventDate Date, EventTime DateTime, UserID UInt64, Sign Int8) engine = CollapsingMergeTree(Sign) order by (CounterID, EventDate, intHash32(UserID), EventTime) sample by intHash32(UserID); insert into sample_final select number / (8192 * 4), toDate('2019-01-01'), toDateTime('2019-01-01 00:00:01') + number, number / (8192 * 2), number % 3 = 1 ? -1 : 1 from numbers(1000000); diff --git a/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql b/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql index aa924218360..a75a5b2c33d 100644 --- a/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql +++ b/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql @@ -2,7 +2,6 @@ -- set insert_distributed_sync = 1; -- see https://github.com/ClickHouse/ClickHouse/issues/18971 -SET allow_experimental_parallel_reading_from_replicas = 0; -- see https://github.com/ClickHouse/ClickHouse/issues/34525 SET prefer_localhost_replica = 1; DROP TABLE IF EXISTS local_01099_a; diff --git a/tests/queries/0_stateless/01517_select_final_distributed.sql b/tests/queries/0_stateless/01517_select_final_distributed.sql index 701828b0b38..a3d1fcfc185 100644 --- a/tests/queries/0_stateless/01517_select_final_distributed.sql +++ b/tests/queries/0_stateless/01517_select_final_distributed.sql @@ -1,7 +1,5 @@ -- Tags: distributed -SET allow_experimental_parallel_reading_from_replicas = 0; - DROP TABLE IF EXISTS test5346; CREATE TABLE test5346 (`Id` String, `Timestamp` DateTime, `updated` DateTime) diff --git a/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql b/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql index 04777f5b31c..2b1a66147a4 100644 --- a/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql +++ b/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql @@ -1,7 +1,5 @@ -- Tags: replica -SET allow_experimental_parallel_reading_from_replicas=0; - DROP TABLE IF EXISTS t; CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; INSERT INTO t VALUES ('Hello'); diff --git a/tests/queries/0_stateless/02221_parallel_replicas_bug.sh b/tests/queries/0_stateless/02221_parallel_replicas_bug.sh index cce32bf8272..1a703b20d2a 100755 --- a/tests/queries/0_stateless/02221_parallel_replicas_bug.sh +++ b/tests/queries/0_stateless/02221_parallel_replicas_bug.sh @@ -4,4 +4,4 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) . "$CURDIR"/../shell_config.sh -${CLICKHOUSE_CLIENT} --allow_experimental_parallel_reading_from_replicas=1 -nm < "$CURDIR"/01099_parallel_distributed_insert_select.sql > /dev/null +${CLICKHOUSE_CLIENT} --allow_experimental_parallel_reading_from_replicas=1 --parallel_replicas_mode='read_tasks' -nm < "$CURDIR"/01099_parallel_distributed_insert_select.sql > /dev/null diff --git a/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh b/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh index 2a163746e20..283c51cf0a2 100755 --- a/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh +++ b/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh @@ -15,6 +15,7 @@ as select * from numbers(1); # Logical error: 'Coordinator for parallel reading from replicas is not initialized'. opts=( --allow_experimental_parallel_reading_from_replicas 1 + --parallel_replicas_mode 'read_tasks' --max_parallel_replicas 3 --iterations 1 diff --git a/tests/queries/0_stateless/02404_memory_bound_merging.sql b/tests/queries/0_stateless/02404_memory_bound_merging.sql index c41e2d3abae..fb0c65e6a7c 100644 --- a/tests/queries/0_stateless/02404_memory_bound_merging.sql +++ b/tests/queries/0_stateless/02404_memory_bound_merging.sql @@ -49,6 +49,7 @@ select a, count() from dist_t_different_dbs group by a, b order by a limit 5 off -- { echoOff } -- set allow_experimental_parallel_reading_from_replicas = 1; +set parallel_replicas_mode = 'read_tasks'; set max_parallel_replicas = 3; set use_hedged_requests = 0; diff --git a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql new file mode 100644 index 00000000000..b716600b9d5 --- /dev/null +++ b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql @@ -0,0 +1,36 @@ +-- Tags: replica + +DROP TABLE IF EXISTS t; + +CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; +INSERT INTO t VALUES ('Hello'); + +SET max_parallel_replicas = 3; +SET parallel_replicas_mode = 'custom_key'; + +SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'sipHash64(x)'; + +DROP TABLE t; + +CREATE TABLE t (x String, y Int32) ENGINE = MergeTree ORDER BY cityHash64(x) SAMPLE BY cityHash64(x); +INSERT INTO t SELECT toString(number), number FROM numbers(1000); + +SET max_parallel_replicas = 1; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; + +SET max_parallel_replicas = 2; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; + +SET max_parallel_replicas = 3; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; + +DROP TABLE t; From e60c2fa77f6c00fd69ea25e7117bf0f8eeba36bf Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 13 Jan 2023 12:47:38 +0000 Subject: [PATCH 012/342] Update reference --- ...02527_max_parallel_replicas_custom_key.reference | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference index e69de29bb2d..c51a7e10fb2 100644 --- a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference +++ b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference @@ -0,0 +1,13 @@ +Hello +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 From 57e771226cb3962941be802e2b272a753024c567 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 16 Jan 2023 10:11:21 +0000 Subject: [PATCH 013/342] Add range mode for custom key --- src/Core/Settings.h | 3 +- src/Core/SettingsEnums.cpp | 4 + src/Core/SettingsEnums.h | 8 + src/Interpreters/InterpreterSelectQuery.cpp | 186 ++++++++++++++---- ...rallel_replicas_custom_key_range.reference | 13 ++ ...max_parallel_replicas_custom_key_range.sql | 37 ++++ 6 files changed, 214 insertions(+), 37 deletions(-) create mode 100644 tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference create mode 100644 tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 573d742ff58..61d1c62bcff 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -153,7 +153,8 @@ static constexpr UInt64 operator""_GiB(unsigned long long value) M(UInt64, max_parallel_replicas, 1, "The maximum number of replicas of each shard used when the query is executed. For consistency (to get different parts of the same partition), this option only works for the specified sampling key. The lag of the replicas is not controlled.", 0) \ M(UInt64, parallel_replicas_count, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the number of parallel replicas participating in query processing.", 0) \ M(UInt64, parallel_replica_offset, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the index of the replica participating in query processing among parallel replicas.", 0) \ - M(String, parallel_replicas_custom_key, "", "Custom key for parallel replicas", 0) \ + M(String, parallel_replicas_custom_key, "", "Custom key for parallel replicas using modulo operation on the key for assigning work to replicas.", 0) \ + M(ParallelReplicasCustomKeyFilterType, parallel_replicas_custom_key_filter_type, ParallelReplicasCustomKeyFilterType::DEFAULT, "Type of filter to use with custom key for parallel replicas. default - use modulo operation on the custom key, range - use range filter on custom key using all possible values for the value type of custom key.", 0) \ M(ParallelReplicasMode, parallel_replicas_mode, ParallelReplicasMode::SAMPLE_KEY, "How to process query using multiple replicas.", 0) \ \ M(Bool, allow_experimental_parallel_reading_from_replicas, false, "If true, ClickHouse will send a SELECT query to all replicas of a table. It will work for any kind on MergeTree table.", 0) \ diff --git a/src/Core/SettingsEnums.cpp b/src/Core/SettingsEnums.cpp index 33aba922e95..69fd78d7eb3 100644 --- a/src/Core/SettingsEnums.cpp +++ b/src/Core/SettingsEnums.cpp @@ -168,6 +168,10 @@ IMPLEMENT_SETTING_ENUM(ParallelReplicasMode, ErrorCodes::BAD_ARGUMENTS, {"custom_key", ParallelReplicasMode::CUSTOM_KEY}, {"read_tasks", ParallelReplicasMode::READ_TASKS}}) +IMPLEMENT_SETTING_ENUM(ParallelReplicasCustomKeyFilterType, ErrorCodes::BAD_ARGUMENTS, + {{"default", ParallelReplicasCustomKeyFilterType::DEFAULT}, + {"range", ParallelReplicasCustomKeyFilterType::RANGE}}) + IMPLEMENT_SETTING_ENUM(LocalFSReadMethod, ErrorCodes::BAD_ARGUMENTS, {{"mmap", LocalFSReadMethod::mmap}, {"pread", LocalFSReadMethod::pread}, diff --git a/src/Core/SettingsEnums.h b/src/Core/SettingsEnums.h index 83c12477165..52c1b369f4b 100644 --- a/src/Core/SettingsEnums.h +++ b/src/Core/SettingsEnums.h @@ -202,5 +202,13 @@ enum class ParallelReplicasMode : uint8_t DECLARE_SETTING_ENUM(ParallelReplicasMode) +enum class ParallelReplicasCustomKeyFilterType : uint8_t +{ + DEFAULT, + RANGE, +}; + +DECLARE_SETTING_ENUM(ParallelReplicasCustomKeyFilterType) + DECLARE_SETTING_ENUM(LocalFSReadMethod) } diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 28f63d5967c..6b66c75e54f 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -94,6 +94,9 @@ #include #include #include +#include "Core/SettingsEnums.h" + +#include namespace DB { @@ -113,6 +116,7 @@ namespace ErrorCodes extern const int ACCESS_DENIED; extern const int UNKNOWN_IDENTIFIER; extern const int BAD_ARGUMENTS; + extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } /// Assumes `storage` is set and the table filter (row-level security) is not empty. @@ -228,10 +232,13 @@ InterpreterSelectQuery::InterpreterSelectQuery( InterpreterSelectQuery::~InterpreterSelectQuery() = default; +namespace +{ + /** There are no limits on the maximum size of the result for the subquery. * Since the result of the query is not the result of the entire query. */ -static ContextPtr getSubqueryContext(const ContextPtr & context) +ContextPtr getSubqueryContext(const ContextPtr & context) { auto subquery_context = Context::createCopy(context); Settings subquery_settings = context->getSettings(); @@ -243,7 +250,7 @@ static ContextPtr getSubqueryContext(const ContextPtr & context) return subquery_context; } -static void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, const String & database, const Settings & settings) +void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, const String & database, const Settings & settings) { ASTSelectQuery & select = query->as(); @@ -263,7 +270,7 @@ static void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & table } /// Checks that the current user has the SELECT privilege. -static void checkAccessRightsForSelect( +void checkAccessRightsForSelect( const ContextPtr & context, const StorageID & table_id, const StorageMetadataPtr & table_metadata, @@ -293,7 +300,7 @@ static void checkAccessRightsForSelect( context->checkAccess(AccessType::SELECT, table_id, syntax_analyzer_result.requiredSourceColumnsForAccessCheck()); } -static ASTPtr parseAdditionalFilterConditionForTable( +ASTPtr parseAdditionalFilterConditionForTable( const Map & setting, const DatabaseAndTableWithAlias & target, const Context & context) @@ -320,7 +327,7 @@ static ASTPtr parseAdditionalFilterConditionForTable( return nullptr; } -static ASTPtr parseParallelReplicaCustomKey(const String & setting, const Context & context) +ASTPtr parseParallelReplicaCustomKey(const String & setting, const Context & context) { ParserExpression parser; const auto & settings = context.getSettingsRef(); @@ -329,8 +336,142 @@ static ASTPtr parseParallelReplicaCustomKey(const String & setting, const Contex "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); } +ASTPtr getCustomKeyFilterForParallelReplica(const Settings & settings, const StoragePtr & storage, const ContextPtr & context) +{ + assert(settings.parallel_replicas_count > 1); + + if (settings.parallel_replicas_custom_key.value.empty()) + throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); + + auto custom_key_ast = parseParallelReplicaCustomKey( + settings.parallel_replicas_custom_key, *context); + + if (settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) + { + // first we do modulo with replica count + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(settings.parallel_replicas_count.value)); + + auto modulo_function = std::make_shared(); + modulo_function->name = "positiveModulo"; + modulo_function->arguments = args; + modulo_function->children.push_back(modulo_function->arguments); + + /// then we compare result to the current replica number (offset) + args = std::make_shared(); + args->children.push_back(modulo_function); + args->children.push_back(std::make_shared(settings.parallel_replica_offset.value)); + + auto equals_function = std::make_shared(); + equals_function->name = "equals"; + equals_function->arguments = args; + equals_function->children.push_back(equals_function->arguments); + + return equals_function; + } + + // create range query + assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); + + if (!storage) + throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); + + KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, storage->getInMemoryMetadataPtr()->columns, context); + + using RelativeSize = boost::rational; + + RelativeSize size_of_universum = 0; + DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; + + if (custom_key_description.data_types.size() == 1) + { + if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + } + + if (size_of_universum == RelativeSize(0)) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER, + "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); + + RelativeSize relative_range_size = RelativeSize(1) / settings.parallel_replicas_count.value; + RelativeSize relative_range_offset = relative_range_size * RelativeSize(settings.parallel_replica_offset.value); + + /// Calculate the half-interval of `[lower, upper)` column values. + bool has_lower_limit = false; + bool has_upper_limit = false; + + RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; + RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; + + UInt64 lower = boost::rational_cast(lower_limit_rational); + UInt64 upper = boost::rational_cast(upper_limit_rational); + + if (lower > 0) + has_lower_limit = true; + + if (upper_limit_rational < size_of_universum) + has_upper_limit = true; + + assert(has_lower_limit || has_upper_limit); + + /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. + std::shared_ptr lower_function; + std::shared_ptr upper_function; + + if (has_lower_limit) + { + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(lower)); + + lower_function = std::make_shared(); + lower_function->name = "greaterOrEquals"; + lower_function->arguments = args; + lower_function->children.push_back(lower_function->arguments); + + if (!has_upper_limit) + return lower_function; + } + + if (has_upper_limit) + { + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(upper)); + + upper_function = std::make_shared(); + upper_function->name = "less"; + upper_function->arguments = args; + upper_function->children.push_back(upper_function->arguments); + + if (!has_lower_limit) + return upper_function; + } + + assert(has_lower_limit && has_upper_limit); + + ASTPtr args = std::make_shared(); + args->children.push_back(lower_function); + args->children.push_back(upper_function); + + auto filter_function = std::make_shared(); + filter_function->name = "and"; + filter_function->arguments = args; + filter_function->children.push_back(filter_function->arguments); + + return filter_function; +} + /// Returns true if we should ignore quotas and limits for a specified table in the system database. -static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) +bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) { if (table_id.database_name == DatabaseCatalog::SYSTEM_DATABASE) { @@ -341,6 +482,8 @@ static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) return false; } +} + InterpreterSelectQuery::InterpreterSelectQuery( const ASTPtr & query_ptr_, const ContextPtr & context_, @@ -511,38 +654,9 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.additional_filter_ast = parseAdditionalFilterConditionForTable( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); - ASTPtr parallel_replicas_custom_filter_ast = nullptr; if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) - { - if (settings.parallel_replicas_custom_key.value.empty()) - throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); - - auto custom_key_ast = parseParallelReplicaCustomKey( - settings.parallel_replicas_custom_key, *context); - - // first we do modulo with replica count - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(settings.parallel_replicas_count.value)); - - auto modulo_function = std::make_shared(); - modulo_function->name = "positiveModulo"; - modulo_function->arguments = args; - modulo_function->children.push_back(modulo_function->arguments); - - /// then we compare result to the current replica number (offset) - args = std::make_shared(); - args->children.push_back(modulo_function); - args->children.push_back(std::make_shared(settings.parallel_replica_offset.value)); - - auto equals_function = std::make_shared(); - equals_function->name = "equals"; - equals_function->arguments = args; - equals_function->children.push_back(equals_function->arguments); - - parallel_replicas_custom_filter_ast = equals_function; - } + parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica(settings, storage, context); auto analyze = [&] (bool try_move_to_prewhere) { diff --git a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference new file mode 100644 index 00000000000..c51a7e10fb2 --- /dev/null +++ b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference @@ -0,0 +1,13 @@ +Hello +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 +1000 diff --git a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql new file mode 100644 index 00000000000..0c1cb7fb48b --- /dev/null +++ b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql @@ -0,0 +1,37 @@ +-- Tags: replica + +DROP TABLE IF EXISTS t; + +CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; +INSERT INTO t VALUES ('Hello'); + +SET max_parallel_replicas = 3; +SET parallel_replicas_mode = 'custom_key'; +SET parallel_replicas_custom_key_filter_type = 'range'; + +SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'sipHash64(x)'; + +DROP TABLE t; + +CREATE TABLE t (x String, y Int32) ENGINE = MergeTree ORDER BY cityHash64(x) SAMPLE BY cityHash64(x); +INSERT INTO t SELECT toString(number), number FROM numbers(1000); + +SET max_parallel_replicas = 1; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; + +SET max_parallel_replicas = 2; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; + +SET max_parallel_replicas = 3; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; +SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; + +DROP TABLE t; From 5adf44775830d92e1bfba53ebfba5c71a956d48e Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 16 Jan 2023 11:22:12 +0000 Subject: [PATCH 014/342] Use unsigned integer for column --- .../02528_max_parallel_replicas_custom_key_range.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql index 0c1cb7fb48b..a8ef9e58d40 100644 --- a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql +++ b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql @@ -13,7 +13,7 @@ SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_ DROP TABLE t; -CREATE TABLE t (x String, y Int32) ENGINE = MergeTree ORDER BY cityHash64(x) SAMPLE BY cityHash64(x); +CREATE TABLE t (x String, y UInt32) ENGINE = MergeTree ORDER BY cityHash64(x) SAMPLE BY cityHash64(x); INSERT INTO t SELECT toString(number), number FROM numbers(1000); SET max_parallel_replicas = 1; From bd352068d749d428d909c0b5d81f299a7e85e3f1 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 17 Jan 2023 12:34:42 +0000 Subject: [PATCH 015/342] Turn replicas into shard for custom_key --- .../ClusterProxy/executeQuery.cpp | 8 +- src/Interpreters/ClusterProxy/executeQuery.h | 3 +- src/Interpreters/InterpreterSelectQuery.cpp | 157 ----------------- src/Storages/StorageDistributed.cpp | 162 +++++++++++++++++- 4 files changed, 169 insertions(+), 161 deletions(-) diff --git a/src/Interpreters/ClusterProxy/executeQuery.cpp b/src/Interpreters/ClusterProxy/executeQuery.cpp index 8ea6298c50b..fc490306e08 100644 --- a/src/Interpreters/ClusterProxy/executeQuery.cpp +++ b/src/Interpreters/ClusterProxy/executeQuery.cpp @@ -127,7 +127,8 @@ void executeQuery( const ASTPtr & query_ast, ContextPtr context, const SelectQueryInfo & query_info, const ExpressionActionsPtr & sharding_key_expr, const std::string & sharding_key_column_name, - const ClusterPtr & not_optimized_cluster) + const ClusterPtr & not_optimized_cluster, + std::function add_additional_shard_filter = {}) { const Settings & settings = context->getSettingsRef(); @@ -177,7 +178,10 @@ void executeQuery( visitor.visit(query_ast_for_shard); } else - query_ast_for_shard = query_ast; + query_ast_for_shard = query_ast->clone(); + + if (add_additional_shard_filter) + add_additional_shard_filter(query_ast_for_shard, shard_info.shard_num); stream_factory.createForShard(shard_info, query_ast_for_shard, main_table, table_func_ptr, diff --git a/src/Interpreters/ClusterProxy/executeQuery.h b/src/Interpreters/ClusterProxy/executeQuery.h index 662fe47ca65..9519fa00efe 100644 --- a/src/Interpreters/ClusterProxy/executeQuery.h +++ b/src/Interpreters/ClusterProxy/executeQuery.h @@ -50,7 +50,8 @@ void executeQuery( const ASTPtr & query_ast, ContextPtr context, const SelectQueryInfo & query_info, const ExpressionActionsPtr & sharding_key_expr, const std::string & sharding_key_column_name, - const ClusterPtr & not_optimized_cluster); + const ClusterPtr & not_optimized_cluster, + std::function add_additional_shard_filter); void executeQueryWithParallelReplicas( diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 6b66c75e54f..3afe83ffac3 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -327,149 +327,6 @@ ASTPtr parseAdditionalFilterConditionForTable( return nullptr; } -ASTPtr parseParallelReplicaCustomKey(const String & setting, const Context & context) -{ - ParserExpression parser; - const auto & settings = context.getSettingsRef(); - return parseQuery( - parser, setting.data(), setting.data() + setting.size(), - "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); -} - -ASTPtr getCustomKeyFilterForParallelReplica(const Settings & settings, const StoragePtr & storage, const ContextPtr & context) -{ - assert(settings.parallel_replicas_count > 1); - - if (settings.parallel_replicas_custom_key.value.empty()) - throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); - - auto custom_key_ast = parseParallelReplicaCustomKey( - settings.parallel_replicas_custom_key, *context); - - if (settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) - { - // first we do modulo with replica count - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(settings.parallel_replicas_count.value)); - - auto modulo_function = std::make_shared(); - modulo_function->name = "positiveModulo"; - modulo_function->arguments = args; - modulo_function->children.push_back(modulo_function->arguments); - - /// then we compare result to the current replica number (offset) - args = std::make_shared(); - args->children.push_back(modulo_function); - args->children.push_back(std::make_shared(settings.parallel_replica_offset.value)); - - auto equals_function = std::make_shared(); - equals_function->name = "equals"; - equals_function->arguments = args; - equals_function->children.push_back(equals_function->arguments); - - return equals_function; - } - - // create range query - assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); - - if (!storage) - throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); - - KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, storage->getInMemoryMetadataPtr()->columns, context); - - using RelativeSize = boost::rational; - - RelativeSize size_of_universum = 0; - DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; - - if (custom_key_description.data_types.size() == 1) - { - if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - } - - if (size_of_universum == RelativeSize(0)) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER, - "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); - - RelativeSize relative_range_size = RelativeSize(1) / settings.parallel_replicas_count.value; - RelativeSize relative_range_offset = relative_range_size * RelativeSize(settings.parallel_replica_offset.value); - - /// Calculate the half-interval of `[lower, upper)` column values. - bool has_lower_limit = false; - bool has_upper_limit = false; - - RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; - RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; - - UInt64 lower = boost::rational_cast(lower_limit_rational); - UInt64 upper = boost::rational_cast(upper_limit_rational); - - if (lower > 0) - has_lower_limit = true; - - if (upper_limit_rational < size_of_universum) - has_upper_limit = true; - - assert(has_lower_limit || has_upper_limit); - - /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. - std::shared_ptr lower_function; - std::shared_ptr upper_function; - - if (has_lower_limit) - { - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(lower)); - - lower_function = std::make_shared(); - lower_function->name = "greaterOrEquals"; - lower_function->arguments = args; - lower_function->children.push_back(lower_function->arguments); - - if (!has_upper_limit) - return lower_function; - } - - if (has_upper_limit) - { - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(upper)); - - upper_function = std::make_shared(); - upper_function->name = "less"; - upper_function->arguments = args; - upper_function->children.push_back(upper_function->arguments); - - if (!has_lower_limit) - return upper_function; - } - - assert(has_lower_limit && has_upper_limit); - - ASTPtr args = std::make_shared(); - args->children.push_back(lower_function); - args->children.push_back(upper_function); - - auto filter_function = std::make_shared(); - filter_function->name = "and"; - filter_function->arguments = args; - filter_function->children.push_back(filter_function->arguments); - - return filter_function; -} - /// Returns true if we should ignore quotas and limits for a specified table in the system database. bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) { @@ -654,10 +511,6 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.additional_filter_ast = parseAdditionalFilterConditionForTable( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); - ASTPtr parallel_replicas_custom_filter_ast = nullptr; - if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) - parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica(settings, storage, context); - auto analyze = [&] (bool try_move_to_prewhere) { /// Allow push down and other optimizations for VIEW: replace with subquery and rewrite it. @@ -823,16 +676,6 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.filter_asts.push_back(query_info.additional_filter_ast); } - if (parallel_replicas_custom_filter_ast) - { - parallel_replicas_custom_filter_info = generateFilterActions( - table_id, parallel_replicas_custom_filter_ast, context, storage, storage_snapshot, metadata_snapshot, required_columns, - prepared_sets); - - parallel_replicas_custom_filter_info->do_remove_column = true; - query_info.filter_asts.push_back(parallel_replicas_custom_filter_ast); - } - source_header = storage_snapshot->getSampleBlockForColumns(required_columns, parameter_values); } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 671ec91609e..56e5bfe9189 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -27,6 +27,7 @@ #include #include #include +#include "Core/SettingsEnums.h" #include #include @@ -88,6 +89,8 @@ #include #include +#include + namespace fs = std::filesystem; @@ -440,6 +443,11 @@ QueryProcessingStage::Enum StorageDistributed::getQueryProcessingStage( const auto & settings = local_context->getSettingsRef(); ClusterPtr cluster = getCluster(); + + // if it's custom_key we will turn replicas into shards and filter specific data on each of them + if (settings.max_parallel_replicas > 1 && cluster->getShardCount() == 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + cluster = cluster->getClusterWithReplicasAsShards(settings); + query_info.cluster = cluster; size_t nodes = getClusterQueriedNodes(settings, cluster); @@ -749,6 +757,158 @@ void StorageDistributed::read( bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas && !settings.use_hedged_requests && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS; + ParserExpression parser; + auto custom_key_ast = parseQuery( + parser, + settings.parallel_replicas_custom_key.value.data(), + settings.parallel_replicas_custom_key.value.data() + settings.parallel_replicas_custom_key.value.size(), + "parallel replicas custom key", + settings.max_query_size, + settings.max_parser_depth); + + auto shard_count = query_info.getCluster()->getShardCount(); + + std::function add_additional_shard_filter; + if (settings.max_parallel_replicas > 1 + && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + { + add_additional_shard_filter = [&](ASTPtr & query, uint64_t shard_num) + { + ParserExpression parser; + auto custom_key_ast = parseQuery( + parser, settings.parallel_replicas_custom_key.value.data(), settings.parallel_replicas_custom_key.value.data() + settings.parallel_replicas_custom_key.value.size(), + "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); + + if (settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) + { + // first we do modulo with replica count + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(shard_count)); + + auto modulo_function = std::make_shared(); + modulo_function->name = "positiveModulo"; + modulo_function->arguments = args; + modulo_function->children.push_back(modulo_function->arguments); + + /// then we compare result to the current replica number (offset) + args = std::make_shared(); + args->children.push_back(modulo_function); + args->children.push_back(std::make_shared(shard_num - 1)); + + auto equals_function = std::make_shared(); + equals_function->name = "equals"; + equals_function->arguments = args; + equals_function->children.push_back(equals_function->arguments); + + auto & select_query = query->as(); + select_query.setExpression(ASTSelectQuery::Expression::WHERE, std::move(equals_function)); + } + else + { + assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); + auto filter_function = [&] + { + KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, getInMemoryMetadataPtr()->columns, local_context); + + using RelativeSize = boost::rational; + + RelativeSize size_of_universum = 0; + DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; + + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + if (custom_key_description.data_types.size() == 1) + { + if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + } + + if (size_of_universum == RelativeSize(0)) + throw Exception( + ErrorCodes::LOGICAL_ERROR, + "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); + + RelativeSize relative_range_size = RelativeSize(1) / query_info.getCluster()->getShardCount(); + RelativeSize relative_range_offset = relative_range_size * RelativeSize(shard_num - 1); + + /// Calculate the half-interval of `[lower, upper)` column values. + bool has_lower_limit = false; + bool has_upper_limit = false; + + RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; + RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; + + UInt64 lower = boost::rational_cast(lower_limit_rational); + UInt64 upper = boost::rational_cast(upper_limit_rational); + + if (lower > 0) + has_lower_limit = true; + + if (upper_limit_rational < size_of_universum) + has_upper_limit = true; + + assert(has_lower_limit || has_upper_limit); + + /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. + std::shared_ptr lower_function; + std::shared_ptr upper_function; + + if (has_lower_limit) + { + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(lower)); + + lower_function = std::make_shared(); + lower_function->name = "greaterOrEquals"; + lower_function->arguments = args; + lower_function->children.push_back(lower_function->arguments); + + if (!has_upper_limit) + return lower_function; + } + + if (has_upper_limit) + { + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(upper)); + + upper_function = std::make_shared(); + upper_function->name = "less"; + upper_function->arguments = args; + upper_function->children.push_back(upper_function->arguments); + + if (!has_lower_limit) + return upper_function; + } + + assert(has_lower_limit && has_upper_limit); + + ASTPtr args = std::make_shared(); + args->children.push_back(lower_function); + args->children.push_back(upper_function); + + auto f = std::make_shared(); + f->name = "and"; + f->arguments = args; + f->children.push_back(f->arguments); + + return f; + }; + + auto & select_query = query->as(); + select_query.setExpression(ASTSelectQuery::Expression::WHERE, filter_function()); + } + }; + } + if (parallel_replicas) ClusterProxy::executeQueryWithParallelReplicas( query_plan, main_table, remote_table_function_ptr, @@ -763,7 +923,7 @@ void StorageDistributed::read( select_stream_factory, log, modified_query_ast, local_context, query_info, sharding_key_expr, sharding_key_column_name, - query_info.cluster); + query_info.cluster, add_additional_shard_filter); /// This is a bug, it is possible only when there is no shards to query, and this is handled earlier. if (!query_plan.isInitialized()) From dd31de18a3dc1f05a90f646216830d03f91fed03 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 17 Jan 2023 13:31:09 +0000 Subject: [PATCH 016/342] Extend where correctly --- src/Interpreters/InterpreterSelectQuery.cpp | 2 -- src/Storages/StorageDistributed.cpp | 38 ++++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 3afe83ffac3..6969a068949 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -115,8 +115,6 @@ namespace ErrorCodes extern const int INVALID_WITH_FILL_EXPRESSION; extern const int ACCESS_DENIED; extern const int UNKNOWN_IDENTIFIER; - extern const int BAD_ARGUMENTS; - extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } /// Assumes `storage` is set and the table filter (row-level security) is not empty. diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 56e5bfe9189..0309aeabbc7 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -133,6 +133,7 @@ namespace ErrorCodes extern const int ARGUMENT_OUT_OF_BOUND; extern const int TOO_LARGE_DISTRIBUTED_DEPTH; extern const int UNSUPPORTED_METHOD; + extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } namespace ActionLocks @@ -779,6 +780,7 @@ void StorageDistributed::read( parser, settings.parallel_replicas_custom_key.value.data(), settings.parallel_replicas_custom_key.value.data() + settings.parallel_replicas_custom_key.value.size(), "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); + ASTPtr shard_filter = nullptr ; if (settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) { // first we do modulo with replica count @@ -801,8 +803,7 @@ void StorageDistributed::read( equals_function->arguments = args; equals_function->children.push_back(equals_function->arguments); - auto & select_query = query->as(); - select_query.setExpression(ASTSelectQuery::Expression::WHERE, std::move(equals_function)); + shard_filter = equals_function; } else { @@ -895,16 +896,37 @@ void StorageDistributed::read( args->children.push_back(lower_function); args->children.push_back(upper_function); - auto f = std::make_shared(); - f->name = "and"; - f->arguments = args; - f->children.push_back(f->arguments); + auto and_function = std::make_shared(); + and_function->name = "and"; + and_function->arguments = args; + and_function->children.push_back(and_function->arguments); - return f; + return and_function; }; + shard_filter = filter_function(); + } + + if (shard_filter) + { auto & select_query = query->as(); - select_query.setExpression(ASTSelectQuery::Expression::WHERE, filter_function()); + + auto where_expression = select_query.where(); + if (where_expression) + { + ASTPtr args = std::make_shared(); + args->children.push_back(where_expression); + args->children.push_back(shard_filter); + + auto and_function = std::make_shared(); + and_function->name = "and"; + and_function->arguments = args; + and_function->children.push_back(and_function->arguments); + + shard_filter = std::move(and_function); + } + + select_query.setExpression(ASTSelectQuery::Expression::WHERE, std::move(shard_filter)); } }; } From 2a1f35e6612fb07bd12090b9f3563753a0fe7183 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 07:59:57 +0000 Subject: [PATCH 017/342] Revert some changes --- src/Interpreters/InterpreterSelectQuery.cpp | 42 +++++++-------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 6969a068949..48326afda45 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -94,9 +94,6 @@ #include #include #include -#include "Core/SettingsEnums.h" - -#include namespace DB { @@ -230,13 +227,10 @@ InterpreterSelectQuery::InterpreterSelectQuery( InterpreterSelectQuery::~InterpreterSelectQuery() = default; -namespace -{ - /** There are no limits on the maximum size of the result for the subquery. * Since the result of the query is not the result of the entire query. */ -ContextPtr getSubqueryContext(const ContextPtr & context) +static ContextPtr getSubqueryContext(const ContextPtr & context) { auto subquery_context = Context::createCopy(context); Settings subquery_settings = context->getSettings(); @@ -248,7 +242,7 @@ ContextPtr getSubqueryContext(const ContextPtr & context) return subquery_context; } -void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, const String & database, const Settings & settings) +static void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, const String & database, const Settings & settings) { ASTSelectQuery & select = query->as(); @@ -268,7 +262,7 @@ void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, cons } /// Checks that the current user has the SELECT privilege. -void checkAccessRightsForSelect( +static void checkAccessRightsForSelect( const ContextPtr & context, const StorageID & table_id, const StorageMetadataPtr & table_metadata, @@ -298,7 +292,7 @@ void checkAccessRightsForSelect( context->checkAccess(AccessType::SELECT, table_id, syntax_analyzer_result.requiredSourceColumnsForAccessCheck()); } -ASTPtr parseAdditionalFilterConditionForTable( +static ASTPtr parseAdditionalFilterConditionForTable( const Map & setting, const DatabaseAndTableWithAlias & target, const Context & context) @@ -326,7 +320,7 @@ ASTPtr parseAdditionalFilterConditionForTable( } /// Returns true if we should ignore quotas and limits for a specified table in the system database. -bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) +static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) { if (table_id.database_name == DatabaseCatalog::SYSTEM_DATABASE) { @@ -337,8 +331,6 @@ bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) return false; } -} - InterpreterSelectQuery::InterpreterSelectQuery( const ASTPtr & query_ptr_, const ContextPtr & context_, @@ -1417,23 +1409,17 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, std::optional

( - query_plan.getCurrentDataStream(), - new_filter_info->actions, - new_filter_info->column_name, - new_filter_info->do_remove_column); - - filter_step->setStepDescription(description); - query_plan.addStep(std::move(filter_step)); - }; - if (additional_filter_info) - add_filter_step(additional_filter_info, "Additional filter"); + { + auto additional_filter_step = std::make_unique( + query_plan.getCurrentDataStream(), + additional_filter_info->actions, + additional_filter_info->column_name, + additional_filter_info->do_remove_column); - if (parallel_replicas_custom_filter_info) - add_filter_step(parallel_replicas_custom_filter_info, "Parallel replica custom key filter"); + additional_filter_step->setStepDescription("Additional filter"); + query_plan.addStep(std::move(additional_filter_step)); + } if (expressions.before_array_join) { From 1c0a3e38c0adb78ee719310161fdf329393ded55 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 08:13:59 +0000 Subject: [PATCH 018/342] Fix queries with Distributed storage --- .../ClusterProxy/executeQuery.cpp | 31 ++- src/Interpreters/ClusterProxy/executeQuery.h | 3 +- src/Storages/StorageDistributed.cpp | 236 ++++++++---------- 3 files changed, 137 insertions(+), 133 deletions(-) diff --git a/src/Interpreters/ClusterProxy/executeQuery.cpp b/src/Interpreters/ClusterProxy/executeQuery.cpp index fc490306e08..d8d55b5486b 100644 --- a/src/Interpreters/ClusterProxy/executeQuery.cpp +++ b/src/Interpreters/ClusterProxy/executeQuery.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include + namespace DB { @@ -128,7 +130,7 @@ void executeQuery( const ExpressionActionsPtr & sharding_key_expr, const std::string & sharding_key_column_name, const ClusterPtr & not_optimized_cluster, - std::function add_additional_shard_filter = {}) + AdditionalShardFilterGenerator shard_filter_generator) { const Settings & settings = context->getSettingsRef(); @@ -180,8 +182,31 @@ void executeQuery( else query_ast_for_shard = query_ast->clone(); - if (add_additional_shard_filter) - add_additional_shard_filter(query_ast_for_shard, shard_info.shard_num); + if (shard_filter_generator) + { + auto shard_filter = shard_filter_generator(shard_info.shard_num); + if (shard_filter) + { + auto & select_query = query_ast_for_shard->as(); + + auto where_expression = select_query.where(); + if (where_expression) + { + ASTPtr args = std::make_shared(); + args->children.push_back(where_expression); + args->children.push_back(shard_filter); + + auto and_function = std::make_shared(); + and_function->name = "and"; + and_function->arguments = args; + and_function->children.push_back(and_function->arguments); + + shard_filter = std::move(and_function); + } + + select_query.setExpression(ASTSelectQuery::Expression::WHERE, std::move(shard_filter)); + } + } stream_factory.createForShard(shard_info, query_ast_for_shard, main_table, table_func_ptr, diff --git a/src/Interpreters/ClusterProxy/executeQuery.h b/src/Interpreters/ClusterProxy/executeQuery.h index 9519fa00efe..25827ebf29a 100644 --- a/src/Interpreters/ClusterProxy/executeQuery.h +++ b/src/Interpreters/ClusterProxy/executeQuery.h @@ -37,6 +37,7 @@ class SelectStreamFactory; ContextMutablePtr updateSettingsForCluster( const Cluster & cluster, ContextPtr context, const Settings & settings, const StorageID & main_table, const SelectQueryInfo * query_info = nullptr, Poco::Logger * log = nullptr); +using AdditionalShardFilterGenerator = std::function; /// Execute a distributed query, creating a query plan, from which the query pipeline can be built. /// `stream_factory` object encapsulates the logic of creating plans for a different type of query /// (currently SELECT, DESCRIBE). @@ -51,7 +52,7 @@ void executeQuery( const ExpressionActionsPtr & sharding_key_expr, const std::string & sharding_key_column_name, const ClusterPtr & not_optimized_cluster, - std::function add_additional_shard_filter); + AdditionalShardFilterGenerator shard_filter_generator = {}); void executeQueryWithParallelReplicas( diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 0b36c6c0026..a7c0dafaf62 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -758,22 +758,26 @@ void StorageDistributed::read( bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas && !settings.use_hedged_requests && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS; - ParserExpression parser; - auto custom_key_ast = parseQuery( - parser, - settings.parallel_replicas_custom_key.value.data(), - settings.parallel_replicas_custom_key.value.data() + settings.parallel_replicas_custom_key.value.size(), - "parallel replicas custom key", - settings.max_query_size, - settings.max_parser_depth); - auto shard_count = query_info.getCluster()->getShardCount(); - std::function add_additional_shard_filter; + ClusterProxy::AdditionalShardFilterGenerator additional_shard_filter_generator; if (settings.max_parallel_replicas > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) { - add_additional_shard_filter = [&](ASTPtr & query, uint64_t shard_num) + + const std::string_view custom_key = settings.parallel_replicas_custom_key.value; + assert(!custom_key.empty()); + + ParserExpression parser; + auto custom_key_ast = parseQuery( + parser, + custom_key.data(), + custom_key.data() + custom_key.size(), + "parallel replicas custom key", + settings.max_query_size, + settings.max_parser_depth); + + additional_shard_filter_generator = [&](uint64_t shard_num) -> ASTPtr { ParserExpression parser; auto custom_key_ast = parseQuery( @@ -803,130 +807,104 @@ void StorageDistributed::read( equals_function->arguments = args; equals_function->children.push_back(equals_function->arguments); - shard_filter = equals_function; + return equals_function; } else { assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); - auto filter_function = [&] + + KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, getInMemoryMetadataPtr()->columns, local_context); + + using RelativeSize = boost::rational; + + RelativeSize size_of_universum = 0; + DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; + + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + if (custom_key_description.data_types.size() == 1) { - KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, getInMemoryMetadataPtr()->columns, local_context); - - using RelativeSize = boost::rational; - - RelativeSize size_of_universum = 0; - DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; - - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - if (custom_key_description.data_types.size() == 1) - { - if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - } - - if (size_of_universum == RelativeSize(0)) - throw Exception( - ErrorCodes::LOGICAL_ERROR, - "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); - - RelativeSize relative_range_size = RelativeSize(1) / query_info.getCluster()->getShardCount(); - RelativeSize relative_range_offset = relative_range_size * RelativeSize(shard_num - 1); - - /// Calculate the half-interval of `[lower, upper)` column values. - bool has_lower_limit = false; - bool has_upper_limit = false; - - RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; - RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; - - UInt64 lower = boost::rational_cast(lower_limit_rational); - UInt64 upper = boost::rational_cast(upper_limit_rational); - - if (lower > 0) - has_lower_limit = true; - - if (upper_limit_rational < size_of_universum) - has_upper_limit = true; - - assert(has_lower_limit || has_upper_limit); - - /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. - std::shared_ptr lower_function; - std::shared_ptr upper_function; - - if (has_lower_limit) - { - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(lower)); - - lower_function = std::make_shared(); - lower_function->name = "greaterOrEquals"; - lower_function->arguments = args; - lower_function->children.push_back(lower_function->arguments); - - if (!has_upper_limit) - return lower_function; - } - - if (has_upper_limit) - { - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(upper)); - - upper_function = std::make_shared(); - upper_function->name = "less"; - upper_function->arguments = args; - upper_function->children.push_back(upper_function->arguments); - - if (!has_lower_limit) - return upper_function; - } - - assert(has_lower_limit && has_upper_limit); - - ASTPtr args = std::make_shared(); - args->children.push_back(lower_function); - args->children.push_back(upper_function); - - auto and_function = std::make_shared(); - and_function->name = "and"; - and_function->arguments = args; - and_function->children.push_back(and_function->arguments); - - return and_function; - }; - - shard_filter = filter_function(); - } - - if (shard_filter) - { - auto & select_query = query->as(); - - auto where_expression = select_query.where(); - if (where_expression) - { - ASTPtr args = std::make_shared(); - args->children.push_back(where_expression); - args->children.push_back(shard_filter); - - auto and_function = std::make_shared(); - and_function->name = "and"; - and_function->arguments = args; - and_function->children.push_back(and_function->arguments); - - shard_filter = std::move(and_function); + if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); } - select_query.setExpression(ASTSelectQuery::Expression::WHERE, std::move(shard_filter)); + if (size_of_universum == RelativeSize(0)) + throw Exception( + ErrorCodes::LOGICAL_ERROR, + "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); + + RelativeSize relative_range_size = RelativeSize(1) / query_info.getCluster()->getShardCount(); + RelativeSize relative_range_offset = relative_range_size * RelativeSize(shard_num - 1); + + /// Calculate the half-interval of `[lower, upper)` column values. + bool has_lower_limit = false; + bool has_upper_limit = false; + + RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; + RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; + + UInt64 lower = boost::rational_cast(lower_limit_rational); + UInt64 upper = boost::rational_cast(upper_limit_rational); + + if (lower > 0) + has_lower_limit = true; + + if (upper_limit_rational < size_of_universum) + has_upper_limit = true; + + assert(has_lower_limit || has_upper_limit); + + /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. + std::shared_ptr lower_function; + std::shared_ptr upper_function; + + if (has_lower_limit) + { + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(lower)); + + lower_function = std::make_shared(); + lower_function->name = "greaterOrEquals"; + lower_function->arguments = args; + lower_function->children.push_back(lower_function->arguments); + + if (!has_upper_limit) + return lower_function; + } + + if (has_upper_limit) + { + ASTPtr args = std::make_shared(); + args->children.push_back(custom_key_ast); + args->children.push_back(std::make_shared(upper)); + + upper_function = std::make_shared(); + upper_function->name = "less"; + upper_function->arguments = args; + upper_function->children.push_back(upper_function->arguments); + + if (!has_lower_limit) + return upper_function; + } + + assert(has_lower_limit && has_upper_limit); + + ASTPtr args = std::make_shared(); + args->children.push_back(lower_function); + args->children.push_back(upper_function); + + auto and_function = std::make_shared(); + and_function->name = "and"; + and_function->arguments = args; + and_function->children.push_back(and_function->arguments); + + return and_function; } }; } @@ -945,7 +923,7 @@ void StorageDistributed::read( select_stream_factory, log, modified_query_ast, local_context, query_info, sharding_key_expr, sharding_key_column_name, - query_info.cluster, add_additional_shard_filter); + query_info.cluster, additional_shard_filter_generator); /// This is a bug, it is possible only when there is no shards to query, and this is handled earlier. if (!query_plan.isInitialized()) From 7a75144ce372da6134ae624668006c1348889d1e Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 09:20:40 +0000 Subject: [PATCH 019/342] Refactor --- src/Interpreters/Cluster.cpp | 11 +- src/Interpreters/Cluster.h | 4 +- .../ClusterProxy/executeQuery.cpp | 13 +- src/Storages/StorageDistributed.cpp | 207 +++++++----------- 4 files changed, 92 insertions(+), 143 deletions(-) diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index 9f0a9d3b35c..7e3e1baf6f2 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -653,9 +653,9 @@ void Cluster::initMisc() } } -std::unique_ptr Cluster::getClusterWithReplicasAsShards(const Settings & settings) const +std::unique_ptr Cluster::getClusterWithReplicasAsShards(const Settings & settings, size_t max_replicas_from_shard) const { - return std::unique_ptr{ new Cluster(ReplicasAsShardsTag{}, *this, settings)}; + return std::unique_ptr{ new Cluster(ReplicasAsShardsTag{}, *this, settings, max_replicas_from_shard)}; } std::unique_ptr Cluster::getClusterWithSingleShard(size_t index) const @@ -668,7 +668,7 @@ std::unique_ptr Cluster::getClusterWithMultipleShards(const std::vector return std::unique_ptr{ new Cluster(SubclusterTag{}, *this, indices) }; } -Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Settings & settings) +Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Settings & settings, size_t max_replicas_from_shard) { if (from.addresses_with_failover.empty()) throw Exception("Cluster is empty", ErrorCodes::LOGICAL_ERROR); @@ -678,6 +678,7 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti for (size_t shard_index : collections::range(0, from.shards_info.size())) { const auto & replicas = from.addresses_with_failover[shard_index]; + size_t replicas_used = 0; for (const auto & address : replicas) { if (!unique_hosts.emplace(address.host_name, address.port).second) @@ -685,6 +686,7 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti ShardInfo info; info.shard_num = ++shard_num; + ++replicas_used; if (address.is_local) info.local_addresses.push_back(address); @@ -711,6 +713,9 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti addresses_with_failover.emplace_back(Addresses{address}); shards_info.emplace_back(std::move(info)); + + if (max_replicas_from_shard && replicas_used == max_replicas_from_shard) + break; } } diff --git a/src/Interpreters/Cluster.h b/src/Interpreters/Cluster.h index ada04aa1cae..77e87e48ca7 100644 --- a/src/Interpreters/Cluster.h +++ b/src/Interpreters/Cluster.h @@ -250,7 +250,7 @@ public: std::unique_ptr getClusterWithMultipleShards(const std::vector & indices) const; /// Get a new Cluster that contains all servers (all shards with all replicas) from existing cluster as independent shards. - std::unique_ptr getClusterWithReplicasAsShards(const Settings & settings) const; + std::unique_ptr getClusterWithReplicasAsShards(const Settings & settings, size_t max_replicas_from_shard = 0) const; /// Returns false if cluster configuration doesn't allow to use it for cross-replication. /// NOTE: true does not mean, that it's actually a cross-replication cluster. @@ -271,7 +271,7 @@ private: /// For getClusterWithReplicasAsShards implementation struct ReplicasAsShardsTag {}; - Cluster(ReplicasAsShardsTag, const Cluster & from, const Settings & settings); + Cluster(ReplicasAsShardsTag, const Cluster & from, const Settings & settings, size_t max_replicas_from_shard); /// Inter-server secret String secret; diff --git a/src/Interpreters/ClusterProxy/executeQuery.cpp b/src/Interpreters/ClusterProxy/executeQuery.cpp index d8d55b5486b..02673b9f7ac 100644 --- a/src/Interpreters/ClusterProxy/executeQuery.cpp +++ b/src/Interpreters/ClusterProxy/executeQuery.cpp @@ -191,18 +191,7 @@ void executeQuery( auto where_expression = select_query.where(); if (where_expression) - { - ASTPtr args = std::make_shared(); - args->children.push_back(where_expression); - args->children.push_back(shard_filter); - - auto and_function = std::make_shared(); - and_function->name = "and"; - and_function->arguments = args; - and_function->children.push_back(and_function->arguments); - - shard_filter = std::move(and_function); - } + shard_filter = makeASTFunction("and", where_expression, shard_filter); select_query.setExpression(ASTSelectQuery::Expression::WHERE, std::move(shard_filter)); } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index a7c0dafaf62..82c38868cb4 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -445,10 +445,6 @@ QueryProcessingStage::Enum StorageDistributed::getQueryProcessingStage( ClusterPtr cluster = getCluster(); - // if it's custom_key we will turn replicas into shards and filter specific data on each of them - if (settings.max_parallel_replicas > 1 && cluster->getShardCount() == 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) - cluster = cluster->getClusterWithReplicasAsShards(settings); - query_info.cluster = cluster; size_t nodes = getClusterQueriedNodes(settings, cluster); @@ -758,15 +754,19 @@ void StorageDistributed::read( bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas && !settings.use_hedged_requests && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS; - auto shard_count = query_info.getCluster()->getShardCount(); - ClusterProxy::AdditionalShardFilterGenerator additional_shard_filter_generator; if (settings.max_parallel_replicas > 1 - && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY + && getCluster()->getShardCount() == 1) { + LOG_INFO(log, "Single shard cluster used with custom_key, transforming replicas into shards"); + + query_info.cluster = getCluster()->getClusterWithReplicasAsShards(settings, settings.max_parallel_replicas); + query_info.optimized_cluster = nullptr; // it's a single shard cluster so nothing could've been optimized const std::string_view custom_key = settings.parallel_replicas_custom_key.value; - assert(!custom_key.empty()); + if (custom_key.empty()) + throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); ParserExpression parser; auto custom_key_ast = parseQuery( @@ -777,135 +777,90 @@ void StorageDistributed::read( settings.max_query_size, settings.max_parser_depth); - additional_shard_filter_generator = [&](uint64_t shard_num) -> ASTPtr + additional_shard_filter_generator = [&, custom_key_ast = std::move(custom_key_ast), shard_count = query_info.cluster->getShardCount()](uint64_t shard_num) mutable -> ASTPtr { - ParserExpression parser; - auto custom_key_ast = parseQuery( - parser, settings.parallel_replicas_custom_key.value.data(), settings.parallel_replicas_custom_key.value.data() + settings.parallel_replicas_custom_key.value.size(), - "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); - - ASTPtr shard_filter = nullptr ; if (settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) { // first we do modulo with replica count - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(shard_count)); - - auto modulo_function = std::make_shared(); - modulo_function->name = "positiveModulo"; - modulo_function->arguments = args; - modulo_function->children.push_back(modulo_function->arguments); + auto modulo_function = makeASTFunction("positiveModulo", custom_key_ast, std::make_shared(shard_count)); /// then we compare result to the current replica number (offset) - args = std::make_shared(); - args->children.push_back(modulo_function); - args->children.push_back(std::make_shared(shard_num - 1)); - - auto equals_function = std::make_shared(); - equals_function->name = "equals"; - equals_function->arguments = args; - equals_function->children.push_back(equals_function->arguments); + auto equals_function = makeASTFunction("equals", std::move(modulo_function), std::make_shared(shard_num - 1)); return equals_function; } - else + + assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); + + KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, getInMemoryMetadataPtr()->columns, local_context); + + using RelativeSize = boost::rational; + + RelativeSize size_of_universum = 0; + DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; + + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + if (custom_key_description.data_types.size() == 1) { - assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); - - KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, getInMemoryMetadataPtr()->columns, local_context); - - using RelativeSize = boost::rational; - - RelativeSize size_of_universum = 0; - DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; - - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - if (custom_key_description.data_types.size() == 1) - { - if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - } - - if (size_of_universum == RelativeSize(0)) - throw Exception( - ErrorCodes::LOGICAL_ERROR, - "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); - - RelativeSize relative_range_size = RelativeSize(1) / query_info.getCluster()->getShardCount(); - RelativeSize relative_range_offset = relative_range_size * RelativeSize(shard_num - 1); - - /// Calculate the half-interval of `[lower, upper)` column values. - bool has_lower_limit = false; - bool has_upper_limit = false; - - RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; - RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; - - UInt64 lower = boost::rational_cast(lower_limit_rational); - UInt64 upper = boost::rational_cast(upper_limit_rational); - - if (lower > 0) - has_lower_limit = true; - - if (upper_limit_rational < size_of_universum) - has_upper_limit = true; - - assert(has_lower_limit || has_upper_limit); - - /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. - std::shared_ptr lower_function; - std::shared_ptr upper_function; - - if (has_lower_limit) - { - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(lower)); - - lower_function = std::make_shared(); - lower_function->name = "greaterOrEquals"; - lower_function->arguments = args; - lower_function->children.push_back(lower_function->arguments); - - if (!has_upper_limit) - return lower_function; - } - - if (has_upper_limit) - { - ASTPtr args = std::make_shared(); - args->children.push_back(custom_key_ast); - args->children.push_back(std::make_shared(upper)); - - upper_function = std::make_shared(); - upper_function->name = "less"; - upper_function->arguments = args; - upper_function->children.push_back(upper_function->arguments); - - if (!has_lower_limit) - return upper_function; - } - - assert(has_lower_limit && has_upper_limit); - - ASTPtr args = std::make_shared(); - args->children.push_back(lower_function); - args->children.push_back(upper_function); - - auto and_function = std::make_shared(); - and_function->name = "and"; - and_function->arguments = args; - and_function->children.push_back(and_function->arguments); - - return and_function; + if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); } + + if (size_of_universum == RelativeSize(0)) + throw Exception( + ErrorCodes::LOGICAL_ERROR, + "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); + + RelativeSize relative_range_size = RelativeSize(1) / shard_count; + RelativeSize relative_range_offset = relative_range_size * RelativeSize(shard_num - 1); + + /// Calculate the half-interval of `[lower, upper)` column values. + bool has_lower_limit = false; + bool has_upper_limit = false; + + RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; + RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; + + UInt64 lower = boost::rational_cast(lower_limit_rational); + UInt64 upper = boost::rational_cast(upper_limit_rational); + + if (lower > 0) + has_lower_limit = true; + + if (upper_limit_rational < size_of_universum) + has_upper_limit = true; + + assert(has_lower_limit || has_upper_limit); + + /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. + std::shared_ptr lower_function; + std::shared_ptr upper_function; + + if (has_lower_limit) + { + lower_function = makeASTFunction("greaterOrEquals", custom_key_ast, std::make_shared(lower)); + + if (!has_upper_limit) + return lower_function; + } + + if (has_upper_limit) + { + upper_function = makeASTFunction("less", custom_key_ast, std::make_shared(upper)); + + if (!has_lower_limit) + return upper_function; + } + + assert(upper_function && lower_function); + + return makeASTFunction("and", std::move(lower_function), std::move(upper_function)); }; } From 3b0c63551ec244ae1d9ff38e70c04c98b5342b7d Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 10:26:38 +0000 Subject: [PATCH 020/342] Combine approaches --- src/Interpreters/InterpreterSelectQuery.cpp | 84 +++++++++++--- .../getCustomKeyFilterForParallelReplicas.cpp | 109 ++++++++++++++++++ .../getCustomKeyFilterForParallelReplicas.h | 21 ++++ src/Storages/StorageDistributed.cpp | 89 +------------- 4 files changed, 206 insertions(+), 97 deletions(-) create mode 100644 src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp create mode 100644 src/Interpreters/getCustomKeyFilterForParallelReplicas.h diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 48326afda45..3612458196c 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -94,6 +95,9 @@ #include #include #include +#include "Core/SettingsEnums.h" + +#include namespace DB { @@ -112,6 +116,8 @@ namespace ErrorCodes extern const int INVALID_WITH_FILL_EXPRESSION; extern const int ACCESS_DENIED; extern const int UNKNOWN_IDENTIFIER; + extern const int BAD_ARGUMENTS; + extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } /// Assumes `storage` is set and the table filter (row-level security) is not empty. @@ -227,10 +233,13 @@ InterpreterSelectQuery::InterpreterSelectQuery( InterpreterSelectQuery::~InterpreterSelectQuery() = default; +namespace +{ + /** There are no limits on the maximum size of the result for the subquery. * Since the result of the query is not the result of the entire query. */ -static ContextPtr getSubqueryContext(const ContextPtr & context) +ContextPtr getSubqueryContext(const ContextPtr & context) { auto subquery_context = Context::createCopy(context); Settings subquery_settings = context->getSettings(); @@ -242,7 +251,7 @@ static ContextPtr getSubqueryContext(const ContextPtr & context) return subquery_context; } -static void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, const String & database, const Settings & settings) +void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & tables, const String & database, const Settings & settings) { ASTSelectQuery & select = query->as(); @@ -262,7 +271,7 @@ static void rewriteMultipleJoins(ASTPtr & query, const TablesWithColumns & table } /// Checks that the current user has the SELECT privilege. -static void checkAccessRightsForSelect( +void checkAccessRightsForSelect( const ContextPtr & context, const StorageID & table_id, const StorageMetadataPtr & table_metadata, @@ -292,7 +301,7 @@ static void checkAccessRightsForSelect( context->checkAccess(AccessType::SELECT, table_id, syntax_analyzer_result.requiredSourceColumnsForAccessCheck()); } -static ASTPtr parseAdditionalFilterConditionForTable( +ASTPtr parseAdditionalFilterConditionForTable( const Map & setting, const DatabaseAndTableWithAlias & target, const Context & context) @@ -319,8 +328,20 @@ static ASTPtr parseAdditionalFilterConditionForTable( return nullptr; } +ASTPtr parseParallelReplicaCustomKey(const String & setting, const Context & context) +{ + if (setting.empty()) + throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); + + ParserExpression parser; + const auto & settings = context.getSettingsRef(); + return parseQuery( + parser, setting.data(), setting.data() + setting.size(), + "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); +} + /// Returns true if we should ignore quotas and limits for a specified table in the system database. -static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) +bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) { if (table_id.database_name == DatabaseCatalog::SYSTEM_DATABASE) { @@ -331,6 +352,8 @@ static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) return false; } +} + InterpreterSelectQuery::InterpreterSelectQuery( const ASTPtr & query_ptr_, const ContextPtr & context_, @@ -501,7 +524,24 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.additional_filter_ast = parseAdditionalFilterConditionForTable( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); - auto analyze = [&] (bool try_move_to_prewhere) + ASTPtr parallel_replicas_custom_filter_ast = nullptr; + if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + { + LOG_INFO(log, "Processing query on a replica using custom_key"); + if (!storage) + throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); + + auto custom_key_ast = parseParallelReplicaCustomKey(settings.parallel_replicas_custom_key, *context); + parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica( + settings.parallel_replicas_count, + settings.parallel_replica_offset, + std::move(custom_key_ast), + settings.parallel_replicas_custom_key_filter_type, + *storage, + context); + } + + auto analyze = [&](bool try_move_to_prewhere) { /// Allow push down and other optimizations for VIEW: replace with subquery and rewrite it. ASTPtr view_table; @@ -666,6 +706,16 @@ InterpreterSelectQuery::InterpreterSelectQuery( query_info.filter_asts.push_back(query_info.additional_filter_ast); } + if (parallel_replicas_custom_filter_ast) + { + parallel_replicas_custom_filter_info = generateFilterActions( + table_id, parallel_replicas_custom_filter_ast, context, storage, storage_snapshot, metadata_snapshot, required_columns, + prepared_sets); + + parallel_replicas_custom_filter_info->do_remove_column = true; + query_info.filter_asts.push_back(parallel_replicas_custom_filter_ast); + } + source_header = storage_snapshot->getSampleBlockForColumns(required_columns, parameter_values); } @@ -1409,17 +1459,23 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, std::optional

( + auto filter_step = std::make_unique( query_plan.getCurrentDataStream(), - additional_filter_info->actions, - additional_filter_info->column_name, - additional_filter_info->do_remove_column); + new_filter_info->actions, + new_filter_info->column_name, + new_filter_info->do_remove_column); - additional_filter_step->setStepDescription("Additional filter"); - query_plan.addStep(std::move(additional_filter_step)); - } + filter_step->setStepDescription(description); + query_plan.addStep(std::move(filter_step)); + }; + + if (additional_filter_info) + add_filter_step(additional_filter_info, "Additional filter"); + + if (parallel_replicas_custom_filter_info) + add_filter_step(parallel_replicas_custom_filter_info, "Parallel replica custom key filter"); if (expressions.before_array_join) { diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp new file mode 100644 index 00000000000..8e7fa8386fe --- /dev/null +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp @@ -0,0 +1,109 @@ +#include + +#include +#include + +#include + +#include + + +namespace DB +{ + +ASTPtr getCustomKeyFilterForParallelReplica( + size_t replicas_count, + size_t replica_num, + ASTPtr custom_key_ast, + ParallelReplicasCustomKeyFilterType filter_type, + const IStorage & storage, + const ContextPtr & context) +{ + assert(replicas_count > 1); + if (filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) + { + // first we do modulo with replica count + auto modulo_function = makeASTFunction("positiveModulo", custom_key_ast, std::make_shared(replicas_count)); + + /// then we compare result to the current replica number (offset) + auto equals_function = makeASTFunction("equals", std::move(modulo_function), std::make_shared(replica_num)); + + return equals_function; + } + + assert(filter_type == ParallelReplicasCustomKeyFilterType::RANGE); + + KeyDescription custom_key_description + = KeyDescription::getKeyFromAST(custom_key_ast, storage.getInMemoryMetadataPtr()->columns, context); + + using RelativeSize = boost::rational; + + RelativeSize size_of_universum = 0; + DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; + + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + if (custom_key_description.data_types.size() == 1) + { + if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + else if (typeid_cast(custom_key_column_type.get())) + size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); + } + + if (size_of_universum == RelativeSize(0)) + throw Exception( + ErrorCodes::LOGICAL_ERROR, + "Invalid custom key column type: {}. Must be one unsigned integer type", + custom_key_column_type->getName()); + + RelativeSize relative_range_size = RelativeSize(1) / replicas_count; + RelativeSize relative_range_offset = relative_range_size * RelativeSize(replica_num); + + /// Calculate the half-interval of `[lower, upper)` column values. + bool has_lower_limit = false; + bool has_upper_limit = false; + + RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; + RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; + + UInt64 lower = boost::rational_cast(lower_limit_rational); + UInt64 upper = boost::rational_cast(upper_limit_rational); + + if (lower > 0) + has_lower_limit = true; + + if (upper_limit_rational < size_of_universum) + has_upper_limit = true; + + assert(has_lower_limit || has_upper_limit); + + /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. + std::shared_ptr lower_function; + std::shared_ptr upper_function; + + if (has_lower_limit) + { + lower_function = makeASTFunction("greaterOrEquals", custom_key_ast, std::make_shared(lower)); + + if (!has_upper_limit) + return lower_function; + } + + if (has_upper_limit) + { + upper_function = makeASTFunction("less", custom_key_ast, std::make_shared(upper)); + + if (!has_lower_limit) + return upper_function; + } + + assert(upper_function && lower_function); + + return makeASTFunction("and", std::move(lower_function), std::move(upper_function)); +} + +} diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h new file mode 100644 index 00000000000..3d830d1d606 --- /dev/null +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include +#include "Core/SettingsEnums.h" + +namespace DB +{ + +/// Get AST for filter created from custom_key +/// replica_num is the number of the replica for which we are generating filter starting from 0 +ASTPtr getCustomKeyFilterForParallelReplica( + size_t replicas_count, + size_t replica_num, + ASTPtr custom_key_ast, + ParallelReplicasCustomKeyFilterType filter_type, + const IStorage & storage, + const ContextPtr & context); + +} diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 82c38868cb4..a0aa646869f 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -61,6 +61,8 @@ #include #include #include +#include + #include #include #include @@ -777,90 +779,11 @@ void StorageDistributed::read( settings.max_query_size, settings.max_parser_depth); - additional_shard_filter_generator = [&, custom_key_ast = std::move(custom_key_ast), shard_count = query_info.cluster->getShardCount()](uint64_t shard_num) mutable -> ASTPtr + additional_shard_filter_generator = + [&, custom_key_ast = std::move(custom_key_ast), shard_count = query_info.cluster->getShardCount()](uint64_t shard_num) -> ASTPtr { - if (settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::DEFAULT) - { - // first we do modulo with replica count - auto modulo_function = makeASTFunction("positiveModulo", custom_key_ast, std::make_shared(shard_count)); - - /// then we compare result to the current replica number (offset) - auto equals_function = makeASTFunction("equals", std::move(modulo_function), std::make_shared(shard_num - 1)); - - return equals_function; - } - - assert(settings.parallel_replicas_custom_key_filter_type == ParallelReplicasCustomKeyFilterType::RANGE); - - KeyDescription custom_key_description = KeyDescription::getKeyFromAST(custom_key_ast, getInMemoryMetadataPtr()->columns, local_context); - - using RelativeSize = boost::rational; - - RelativeSize size_of_universum = 0; - DataTypePtr custom_key_column_type = custom_key_description.data_types[0]; - - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - if (custom_key_description.data_types.size() == 1) - { - if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - else if (typeid_cast(custom_key_column_type.get())) - size_of_universum = RelativeSize(std::numeric_limits::max()) + RelativeSize(1); - } - - if (size_of_universum == RelativeSize(0)) - throw Exception( - ErrorCodes::LOGICAL_ERROR, - "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); - - RelativeSize relative_range_size = RelativeSize(1) / shard_count; - RelativeSize relative_range_offset = relative_range_size * RelativeSize(shard_num - 1); - - /// Calculate the half-interval of `[lower, upper)` column values. - bool has_lower_limit = false; - bool has_upper_limit = false; - - RelativeSize lower_limit_rational = relative_range_offset * size_of_universum; - RelativeSize upper_limit_rational = (relative_range_offset + relative_range_size) * size_of_universum; - - UInt64 lower = boost::rational_cast(lower_limit_rational); - UInt64 upper = boost::rational_cast(upper_limit_rational); - - if (lower > 0) - has_lower_limit = true; - - if (upper_limit_rational < size_of_universum) - has_upper_limit = true; - - assert(has_lower_limit || has_upper_limit); - - /// Let's add the conditions to cut off something else when the index is scanned again and when the request is processed. - std::shared_ptr lower_function; - std::shared_ptr upper_function; - - if (has_lower_limit) - { - lower_function = makeASTFunction("greaterOrEquals", custom_key_ast, std::make_shared(lower)); - - if (!has_upper_limit) - return lower_function; - } - - if (has_upper_limit) - { - upper_function = makeASTFunction("less", custom_key_ast, std::make_shared(upper)); - - if (!has_lower_limit) - return upper_function; - } - - assert(upper_function && lower_function); - - return makeASTFunction("and", std::move(lower_function), std::move(upper_function)); + return getCustomKeyFilterForParallelReplica( + shard_count, shard_num - 1, custom_key_ast, settings.parallel_replicas_custom_key_filter_type, *this, local_context); }; } From ddfb913f9991b4e410744ebb0eac260d842df4e2 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 11:28:26 +0000 Subject: [PATCH 021/342] better --- src/Interpreters/InterpreterSelectQuery.cpp | 18 +---- .../getCustomKeyFilterForParallelReplicas.cpp | 23 +++++- .../getCustomKeyFilterForParallelReplicas.h | 4 +- src/Storages/StorageDistributed.cpp | 75 ++++++++++--------- 4 files changed, 64 insertions(+), 56 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 3612458196c..0aa48ca1998 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -95,9 +95,6 @@ #include #include #include -#include "Core/SettingsEnums.h" - -#include namespace DB { @@ -117,7 +114,6 @@ namespace ErrorCodes extern const int ACCESS_DENIED; extern const int UNKNOWN_IDENTIFIER; extern const int BAD_ARGUMENTS; - extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } /// Assumes `storage` is set and the table filter (row-level security) is not empty. @@ -328,18 +324,6 @@ ASTPtr parseAdditionalFilterConditionForTable( return nullptr; } -ASTPtr parseParallelReplicaCustomKey(const String & setting, const Context & context) -{ - if (setting.empty()) - throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); - - ParserExpression parser; - const auto & settings = context.getSettingsRef(); - return parseQuery( - parser, setting.data(), setting.data() + setting.size(), - "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); -} - /// Returns true if we should ignore quotas and limits for a specified table in the system database. bool shouldIgnoreQuotaAndLimits(const StorageID & table_id) { @@ -531,7 +515,7 @@ InterpreterSelectQuery::InterpreterSelectQuery( if (!storage) throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); - auto custom_key_ast = parseParallelReplicaCustomKey(settings.parallel_replicas_custom_key, *context); + auto custom_key_ast = parseParallelReplicaCustomKey(settings.parallel_replicas_custom_key.value, *context); parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica( settings.parallel_replicas_count, settings.parallel_replica_offset, diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp index 8e7fa8386fe..306ca129e16 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp @@ -2,6 +2,10 @@ #include #include +#include +#include + +#include #include @@ -11,6 +15,12 @@ namespace DB { +namespace ErrorCodes +{ + extern const int LOGICAL_ERROR; + extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; +} + ASTPtr getCustomKeyFilterForParallelReplica( size_t replicas_count, size_t replica_num, @@ -56,7 +66,7 @@ ASTPtr getCustomKeyFilterForParallelReplica( if (size_of_universum == RelativeSize(0)) throw Exception( - ErrorCodes::LOGICAL_ERROR, + ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER, "Invalid custom key column type: {}. Must be one unsigned integer type", custom_key_column_type->getName()); @@ -106,4 +116,15 @@ ASTPtr getCustomKeyFilterForParallelReplica( return makeASTFunction("and", std::move(lower_function), std::move(upper_function)); } +ASTPtr parseParallelReplicaCustomKey(std::string_view custom_key, const Context & context) +{ + if (custom_key.empty()) + throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); + + ParserExpression parser; + const auto & settings = context.getSettingsRef(); + return parseQuery( + parser, custom_key.data(), custom_key.data() + custom_key.size(), + "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); +} } diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h index 3d830d1d606..251e72e0a97 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h @@ -3,7 +3,7 @@ #include #include #include -#include "Core/SettingsEnums.h" +#include namespace DB { @@ -18,4 +18,6 @@ ASTPtr getCustomKeyFilterForParallelReplica( const IStorage & storage, const ContextPtr & context); +ASTPtr parseParallelReplicaCustomKey(std::string_view custom_key, const Context & context); + } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index a0aa646869f..e66a363003e 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -135,7 +135,6 @@ namespace ErrorCodes extern const int ARGUMENT_OUT_OF_BOUND; extern const int TOO_LARGE_DISTRIBUTED_DEPTH; extern const int UNSUPPORTED_METHOD; - extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } namespace ActionLocks @@ -447,29 +446,40 @@ QueryProcessingStage::Enum StorageDistributed::getQueryProcessingStage( ClusterPtr cluster = getCluster(); - query_info.cluster = cluster; - size_t nodes = getClusterQueriedNodes(settings, cluster); - /// Always calculate optimized cluster here, to avoid conditions during read() - /// (Anyway it will be calculated in the read()) - if (nodes > 1 && settings.optimize_skip_unused_shards) + if (settings.max_parallel_replicas > 1 + && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY + && cluster->getShardCount() == 1) { - ClusterPtr optimized_cluster = getOptimizedCluster(local_context, storage_snapshot, query_info.query); - if (optimized_cluster) - { - LOG_DEBUG(log, "Skipping irrelevant shards - the query will be sent to the following shards of the cluster (shard numbers): {}", - makeFormattedListOfShards(optimized_cluster)); + LOG_INFO(log, "Single shard cluster used with custom_key, transforming replicas into virtual shards"); - cluster = optimized_cluster; - query_info.optimized_cluster = cluster; + query_info.cluster = cluster->getClusterWithReplicasAsShards(settings, settings.max_parallel_replicas); + } + else + { + query_info.cluster = cluster; - nodes = getClusterQueriedNodes(settings, cluster); - } - else + if (nodes > 1 && settings.optimize_skip_unused_shards) { - LOG_DEBUG(log, "Unable to figure out irrelevant shards from WHERE/PREWHERE clauses - the query will be sent to all shards of the cluster{}", - has_sharding_key ? "" : " (no sharding key)"); + /// Always calculate optimized cluster here, to avoid conditions during read() + /// (Anyway it will be calculated in the read()) + ClusterPtr optimized_cluster = getOptimizedCluster(local_context, storage_snapshot, query_info.query); + if (optimized_cluster) + { + LOG_DEBUG(log, "Skipping irrelevant shards - the query will be sent to the following shards of the cluster (shard numbers): {}", + makeFormattedListOfShards(optimized_cluster)); + + cluster = optimized_cluster; + query_info.optimized_cluster = cluster; + + nodes = getClusterQueriedNodes(settings, cluster); + } + else + { + LOG_DEBUG(log, "Unable to figure out irrelevant shards from WHERE/PREWHERE clauses - the query will be sent to all shards of the cluster{}", + has_sharding_key ? "" : " (no sharding key)"); + } } } @@ -751,33 +761,21 @@ void StorageDistributed::read( storage_snapshot, processed_stage); - auto settings = local_context->getSettingsRef(); - bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas - && !settings.use_hedged_requests && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS; ClusterProxy::AdditionalShardFilterGenerator additional_shard_filter_generator; if (settings.max_parallel_replicas > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY && getCluster()->getShardCount() == 1) { - LOG_INFO(log, "Single shard cluster used with custom_key, transforming replicas into shards"); + if (query_info.getCluster()->getShardCount() == 1) + { + // we are reading from single shard replica but didn't transform replicas + // into virtual shards with custom_key set + throw Exception(ErrorCodes::LOGICAL_ERROR, "Replicas weren't transformed into virtual shards"); + } - query_info.cluster = getCluster()->getClusterWithReplicasAsShards(settings, settings.max_parallel_replicas); - query_info.optimized_cluster = nullptr; // it's a single shard cluster so nothing could've been optimized - - const std::string_view custom_key = settings.parallel_replicas_custom_key.value; - if (custom_key.empty()) - throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); - - ParserExpression parser; - auto custom_key_ast = parseQuery( - parser, - custom_key.data(), - custom_key.data() + custom_key.size(), - "parallel replicas custom key", - settings.max_query_size, - settings.max_parser_depth); + auto custom_key_ast = parseParallelReplicaCustomKey(settings.parallel_replicas_custom_key.value, *local_context); additional_shard_filter_generator = [&, custom_key_ast = std::move(custom_key_ast), shard_count = query_info.cluster->getShardCount()](uint64_t shard_num) -> ASTPtr @@ -787,6 +785,9 @@ void StorageDistributed::read( }; } + bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas + && !settings.use_hedged_requests && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS; + if (parallel_replicas) ClusterProxy::executeQueryWithParallelReplicas( query_plan, main_table, remote_table_function_ptr, From 53b53a1ec90c385c2b68806e4a7f4078ecf64945 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 12:04:07 +0000 Subject: [PATCH 022/342] Add replcia shuffling --- src/Interpreters/Cluster.cpp | 92 ++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 31 deletions(-) diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index 7e3e1baf6f2..97970691025 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -15,6 +15,7 @@ #include #include +#include namespace DB { @@ -677,45 +678,74 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti std::set> unique_hosts; for (size_t shard_index : collections::range(0, from.shards_info.size())) { - const auto & replicas = from.addresses_with_failover[shard_index]; - size_t replicas_used = 0; - for (const auto & address : replicas) + auto create_shards_from_replicas = [&](std::span replicas) { - if (!unique_hosts.emplace(address.host_name, address.port).second) - continue; /// Duplicate host, skip. + for (const auto & address : replicas) + { + if (!unique_hosts.emplace(address.host_name, address.port).second) + continue; /// Duplicate host, skip. - ShardInfo info; - info.shard_num = ++shard_num; - ++replicas_used; + ShardInfo info; + info.shard_num = ++shard_num; - if (address.is_local) - info.local_addresses.push_back(address); + if (address.is_local) + info.local_addresses.push_back(address); - info.all_addresses.push_back(address); + info.all_addresses.push_back(address); - auto pool = ConnectionPoolFactory::instance().get( - static_cast(settings.distributed_connections_pool_size), - address.host_name, - address.port, - address.default_database, - address.user, - address.password, - address.quota_key, - address.cluster, - address.cluster_secret, - "server", - address.compression, - address.secure, - address.priority); + auto pool = ConnectionPoolFactory::instance().get( + static_cast(settings.distributed_connections_pool_size), + address.host_name, + address.port, + address.default_database, + address.user, + address.password, + address.quota_key, + address.cluster, + address.cluster_secret, + "server", + address.compression, + address.secure, + address.priority); - info.pool = std::make_shared(ConnectionPoolPtrs{pool}, settings.load_balancing); - info.per_replica_pools = {std::move(pool)}; + info.pool = std::make_shared(ConnectionPoolPtrs{pool}, settings.load_balancing); + info.per_replica_pools = {std::move(pool)}; - addresses_with_failover.emplace_back(Addresses{address}); - shards_info.emplace_back(std::move(info)); + addresses_with_failover.emplace_back(Addresses{address}); + shards_info.emplace_back(std::move(info)); + } + }; - if (max_replicas_from_shard && replicas_used == max_replicas_from_shard) - break; + const auto & replicas = from.addresses_with_failover[shard_index]; + if (!max_replicas_from_shard || replicas.size() <= max_replicas_from_shard) + { + create_shards_from_replicas(replicas); + } + else + { + std::random_device rd; + std::mt19937 gen{rd()}; + auto shuffled_replicas = replicas; + + if (settings.prefer_localhost_replica) + { + auto local_replica = std::find_if(shuffled_replicas.begin(), shuffled_replicas.end(), [](const auto & replica) { return replica.is_local; }); + if (local_replica != shuffled_replicas.end()) + { + std::swap(*shuffled_replicas.begin(), *local_replica); + std::shuffle(shuffled_replicas.begin() + 1, shuffled_replicas.end(), gen); + } + else + { + std::shuffle(shuffled_replicas.begin(), shuffled_replicas.end(), gen); + } + } + else + { + std::shuffle(shuffled_replicas.begin(), shuffled_replicas.end(), gen); + } + + create_shards_from_replicas(std::span{shuffled_replicas.begin(), shuffled_replicas.begin() + max_replicas_from_shard}); } } From d8adff35c3e1851e3f85ffa0ffa3780b0114bd51 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 12:08:32 +0000 Subject: [PATCH 023/342] Fix style --- src/Interpreters/Cluster.cpp | 6 +++++- src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index 97970691025..9661b3f4e19 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -723,16 +723,20 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti } else { + // shuffle replicas so we don't always pick the same subset std::random_device rd; std::mt19937 gen{rd()}; auto shuffled_replicas = replicas; if (settings.prefer_localhost_replica) { + // force for local replica to always be included auto local_replica = std::find_if(shuffled_replicas.begin(), shuffled_replicas.end(), [](const auto & replica) { return replica.is_local; }); if (local_replica != shuffled_replicas.end()) { - std::swap(*shuffled_replicas.begin(), *local_replica); + if (local_replica != shuffled_replicas.begin()) + std::swap(*shuffled_replicas.begin(), *local_replica); + std::shuffle(shuffled_replicas.begin() + 1, shuffled_replicas.end(), gen); } else diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp index 306ca129e16..fbc1d214164 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp @@ -17,8 +17,8 @@ namespace DB namespace ErrorCodes { - extern const int LOGICAL_ERROR; extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; + extern const int BAD_ARGUMENTS; } ASTPtr getCustomKeyFilterForParallelReplica( From d93cb3e1ddb5192f208e259fa57d4ccd5f29dffa Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 13:24:35 +0000 Subject: [PATCH 024/342] More correct check --- src/Storages/StorageDistributed.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index e66a363003e..ea7cb9d33cb 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -307,6 +307,11 @@ size_t getClusterQueriedNodes(const Settings & settings, const ClusterPtr & clus return (num_remote_shards + num_local_shards) * settings.max_parallel_replicas; } +bool useVirtualShards(const Settings & settings, const Cluster & cluster) +{ + return settings.max_parallel_replicas > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY + && cluster.getShardCount() == 1 && cluster.getShardsInfo()[0].getAllNodeCount() > 1; +} } @@ -448,9 +453,7 @@ QueryProcessingStage::Enum StorageDistributed::getQueryProcessingStage( size_t nodes = getClusterQueriedNodes(settings, cluster); - if (settings.max_parallel_replicas > 1 - && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY - && cluster->getShardCount() == 1) + if (useVirtualShards(settings, *cluster)) { LOG_INFO(log, "Single shard cluster used with custom_key, transforming replicas into virtual shards"); @@ -764,13 +767,11 @@ void StorageDistributed::read( auto settings = local_context->getSettingsRef(); ClusterProxy::AdditionalShardFilterGenerator additional_shard_filter_generator; - if (settings.max_parallel_replicas > 1 - && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY - && getCluster()->getShardCount() == 1) + if (useVirtualShards(settings, *getCluster())) { if (query_info.getCluster()->getShardCount() == 1) { - // we are reading from single shard replica but didn't transform replicas + // we are reading from single shard with multiple replicas but didn't transform replicas // into virtual shards with custom_key set throw Exception(ErrorCodes::LOGICAL_ERROR, "Replicas weren't transformed into virtual shards"); } From 6394a004fb8014f81baf98b500163a779640478c Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 14:47:58 +0000 Subject: [PATCH 025/342] Refactor --- src/Interpreters/Cluster.cpp | 54 +++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index 9661b3f4e19..76609cf1a5b 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -669,6 +669,33 @@ std::unique_ptr Cluster::getClusterWithMultipleShards(const std::vector return std::unique_ptr{ new Cluster(SubclusterTag{}, *this, indices) }; } +namespace +{ + +void shuffleReplicas(auto & replicas, const Settings & settings) +{ + std::random_device rd; + std::mt19937 gen{rd()}; + + if (settings.prefer_localhost_replica) + { + // force for local replica to always be included + auto local_replica = std::find_if(replicas.begin(), replicas.end(), [](const auto & replica) { return replica.is_local; }); + if (local_replica != replicas.end()) + { + if (local_replica != replicas.begin()) + std::swap(*replicas.begin(), *local_replica); + + std::shuffle(replicas.begin() + 1, replicas.end(), gen); + return; + } + } + + std::shuffle(replicas.begin(), replicas.end(), gen); +} + +} + Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Settings & settings, size_t max_replicas_from_shard) { if (from.addresses_with_failover.empty()) @@ -723,32 +750,9 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti } else { - // shuffle replicas so we don't always pick the same subset - std::random_device rd; - std::mt19937 gen{rd()}; auto shuffled_replicas = replicas; - - if (settings.prefer_localhost_replica) - { - // force for local replica to always be included - auto local_replica = std::find_if(shuffled_replicas.begin(), shuffled_replicas.end(), [](const auto & replica) { return replica.is_local; }); - if (local_replica != shuffled_replicas.end()) - { - if (local_replica != shuffled_replicas.begin()) - std::swap(*shuffled_replicas.begin(), *local_replica); - - std::shuffle(shuffled_replicas.begin() + 1, shuffled_replicas.end(), gen); - } - else - { - std::shuffle(shuffled_replicas.begin(), shuffled_replicas.end(), gen); - } - } - else - { - std::shuffle(shuffled_replicas.begin(), shuffled_replicas.end(), gen); - } - + // shuffle replicas so we don't always pick the same subset + shuffleReplicas(shuffled_replicas, settings); create_shards_from_replicas(std::span{shuffled_replicas.begin(), shuffled_replicas.begin() + max_replicas_from_shard}); } } From ad429a9312146bb48058af48283ff627cf094b5c Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 19 Jan 2023 15:16:05 +0000 Subject: [PATCH 026/342] Add missing checks --- src/Client/HedgedConnections.cpp | 4 +++- src/Client/MultiplexedConnections.cpp | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Client/HedgedConnections.cpp b/src/Client/HedgedConnections.cpp index c7392a86a7e..12f1850c282 100644 --- a/src/Client/HedgedConnections.cpp +++ b/src/Client/HedgedConnections.cpp @@ -175,7 +175,9 @@ void HedgedConnections::sendQuery( modified_settings.group_by_two_level_threshold_bytes = 0; } - const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 && !settings.allow_experimental_parallel_reading_from_replicas; + const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 + && (settings.parallel_replicas_mode != ParallelReplicasMode::READ_TASKS + || !settings.allow_experimental_parallel_reading_from_replicas); if (offset_states.size() > 1 && enable_sample_offset_parallel_processing) { diff --git a/src/Client/MultiplexedConnections.cpp b/src/Client/MultiplexedConnections.cpp index 87eda765a7a..81bae7f501b 100644 --- a/src/Client/MultiplexedConnections.cpp +++ b/src/Client/MultiplexedConnections.cpp @@ -134,6 +134,7 @@ void MultiplexedConnections::sendQuery( } bool parallel_reading_from_replicas = settings.max_parallel_replicas > 1 + && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS && settings.allow_experimental_parallel_reading_from_replicas /// To avoid trying to coordinate with clickhouse-benchmark, /// since it uses the same code. @@ -146,7 +147,9 @@ void MultiplexedConnections::sendQuery( } } - const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 && !settings.allow_experimental_parallel_reading_from_replicas; + const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 + && (settings.parallel_replicas_mode != ParallelReplicasMode::READ_TASKS + || !settings.allow_experimental_parallel_reading_from_replicas); size_t num_replicas = replica_states.size(); if (num_replicas > 1) From 9600d16e9f9983c8df4c79b985a97e389a832ace Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 20 Jan 2023 10:09:26 +0000 Subject: [PATCH 027/342] better tests for custom_key --- .../02404_memory_bound_merging.sql | 2 +- ...max_parallel_replicas_custom_key.reference | 13 - ...02527_max_parallel_replicas_custom_key.sql | 36 - ...max_parallel_replicas_custom_key.reference | 659 ++++++++++++++++++ .../02535_max_parallel_replicas_custom_key.sh | 47 ++ 5 files changed, 707 insertions(+), 50 deletions(-) delete mode 100644 tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference delete mode 100644 tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql create mode 100644 tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference create mode 100755 tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh diff --git a/tests/queries/0_stateless/02404_memory_bound_merging.sql b/tests/queries/0_stateless/02404_memory_bound_merging.sql index fb0c65e6a7c..f7db31dabb3 100644 --- a/tests/queries/0_stateless/02404_memory_bound_merging.sql +++ b/tests/queries/0_stateless/02404_memory_bound_merging.sql @@ -1,4 +1,4 @@ --- Tags: no-parallel +-- Tags: no-parallel, long create table t(a UInt64, b UInt64) engine=MergeTree order by a; system stop merges t; diff --git a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference deleted file mode 100644 index c51a7e10fb2..00000000000 --- a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.reference +++ /dev/null @@ -1,13 +0,0 @@ -Hello -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 diff --git a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql b/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql deleted file mode 100644 index b716600b9d5..00000000000 --- a/tests/queries/0_stateless/02527_max_parallel_replicas_custom_key.sql +++ /dev/null @@ -1,36 +0,0 @@ --- Tags: replica - -DROP TABLE IF EXISTS t; - -CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; -INSERT INTO t VALUES ('Hello'); - -SET max_parallel_replicas = 3; -SET parallel_replicas_mode = 'custom_key'; - -SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'sipHash64(x)'; - -DROP TABLE t; - -CREATE TABLE t (x String, y Int32) ENGINE = MergeTree ORDER BY cityHash64(x) SAMPLE BY cityHash64(x); -INSERT INTO t SELECT toString(number), number FROM numbers(1000); - -SET max_parallel_replicas = 1; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; - -SET max_parallel_replicas = 2; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; - -SET max_parallel_replicas = 3; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; - -DROP TABLE t; diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference new file mode 100644 index 00000000000..01f48778b34 --- /dev/null +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference @@ -0,0 +1,659 @@ +query='SELECT * FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='sipHash64(x)' +filter_type='default' max_replicas=1 prefer_localhost_replica=0 +Hello +(ReadFromRemote) +filter_type='default' max_replicas=2 prefer_localhost_replica=0 +Hello +(ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=0 +Hello +(ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=0 +Hello +(ReadFromRemote) +filter_type='range' max_replicas=2 prefer_localhost_replica=0 +Hello +(ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=0 +Hello +(ReadFromRemote) +filter_type='default' max_replicas=1 prefer_localhost_replica=1 +Hello +(Expression) +ExpressionTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 +filter_type='default' max_replicas=2 prefer_localhost_replica=1 +Hello +(Union) + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=1 +Hello +(Union) + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=1 +Hello +(Expression) +ExpressionTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 +filter_type='range' max_replicas=2 prefer_localhost_replica=1 +Hello +(Union) + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=1 +Hello +(Union) + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x)' +filter_type='default' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='default' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='range' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='default' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='range' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='y' +filter_type='default' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='default' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='range' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='default' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='range' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x) + y' +filter_type='default' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='default' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='range' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='default' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='range' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x) + 1' +filter_type='default' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='default' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=0 +1000 +(ReadFromRemote) +filter_type='range' max_replicas=2 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=0 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='default' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='default' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=1 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform + (ReadFromStorage) + AggregatingTransform + ExpressionTransform + SourceFromSingleChunk 0 → 1 +filter_type='range' max_replicas=2 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 2 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) +filter_type='range' max_replicas=3 prefer_localhost_replica=1 +1000 +(Expression) +ExpressionTransform × 4 + (MergingAggregated) + Resize 1 → 4 + SortingAggregatedTransform 4 → 1 + MergingAggregatedBucketTransform × 4 + Resize 1 → 4 + GroupingAggregatedTransform 3 → 1 + (Union) + (Aggregating) + AggregatingTransform + (Expression) + ExpressionTransform + (Filter) + FilterTransform + (ReadFromMergeTree) + MergeTreeInOrder 0 → 1 + (ReadFromRemote) diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh new file mode 100755 index 00000000000..76138842e9a --- /dev/null +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +function run_with_custom_key { + echo "query='$1' with custom_key='$2'" + for prefer_localhost_replica in 0 1; do + for filter_type in 'default' 'range'; do + for max_replicas in {1..3}; do + echo "filter_type='$filter_type' max_replicas=$max_replicas prefer_localhost_replica=$prefer_localhost_replica" + query="$1 SETTINGS max_parallel_replicas=$max_replicas\ + , parallel_replicas_mode='custom_key'\ + , parallel_replicas_custom_key='$2'\ + , parallel_replicas_custom_key_filter_type='$filter_type'\ + , max_threads=4\ + , prefer_localhost_replica=$prefer_localhost_replica" + $CLICKHOUSE_CLIENT --query="$query" + $CLICKHOUSE_CLIENT --query="EXPLAIN PIPELINE $query" + done + done + done +} + +$CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS 02535_custom_key"; + +$CLICKHOUSE_CLIENT --query="CREATE TABLE 02535_custom_key (x String) ENGINE = MergeTree ORDER BY x"; +$CLICKHOUSE_CLIENT --query="INSERT INTO 02535_custom_key VALUES ('Hello')"; + +run_with_custom_key "SELECT * FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)" "sipHash64(x)" + +$CLICKHOUSE_CLIENT --query="DROP TABLE 02535_custom_key" + +$CLICKHOUSE_CLIENT --query="CREATE TABLE 02535_custom_key (x String, y Int32) ENGINE = MergeTree ORDER BY cityHash64(x)" +$CLICKHOUSE_CLIENT --query="INSERT INTO 02535_custom_key SELECT toString(number), number FROM numbers(1000)" + +function run_count_with_custom_key { + run_with_custom_key "SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)" "$1" +} + +run_count_with_custom_key "cityHash64(x)" +run_count_with_custom_key "y" +run_count_with_custom_key "cityHash64(x) + y" +run_count_with_custom_key "cityHash64(x) + 1" + +$CLICKHOUSE_CLIENT --query="DROP TABLE 02535_custom_key" From 2cb106f1f9f5e893431e776c66c0b26c689a560a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 20 Jan 2023 12:55:26 +0000 Subject: [PATCH 028/342] Add integration test for custom_key --- .../__init__.py | 0 .../configs/remote_servers.xml | 50 ++++++++++ .../test_parallel_replicas_custom_key/test.py | 94 +++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 tests/integration/test_parallel_replicas_custom_key/__init__.py create mode 100644 tests/integration/test_parallel_replicas_custom_key/configs/remote_servers.xml create mode 100644 tests/integration/test_parallel_replicas_custom_key/test.py diff --git a/tests/integration/test_parallel_replicas_custom_key/__init__.py b/tests/integration/test_parallel_replicas_custom_key/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/integration/test_parallel_replicas_custom_key/configs/remote_servers.xml b/tests/integration/test_parallel_replicas_custom_key/configs/remote_servers.xml new file mode 100644 index 00000000000..308db461498 --- /dev/null +++ b/tests/integration/test_parallel_replicas_custom_key/configs/remote_servers.xml @@ -0,0 +1,50 @@ + + + + + false + + n1 + 9000 + + + n2 + 9000 + + + + false + + n3 + 9000 + + + n4 + 9000 + + + + + + false + + n1 + 9000 + + + n2 + 9000 + + + n3 + 9000 + + + n4 + 9000 + + + + + + diff --git a/tests/integration/test_parallel_replicas_custom_key/test.py b/tests/integration/test_parallel_replicas_custom_key/test.py new file mode 100644 index 00000000000..0787234a7b1 --- /dev/null +++ b/tests/integration/test_parallel_replicas_custom_key/test.py @@ -0,0 +1,94 @@ +import pytest +from helpers.cluster import ClickHouseCluster + +cluster = ClickHouseCluster(__file__) + +n1 = cluster.add_instance( + "n1", main_configs=["configs/remote_servers.xml"], with_zookeeper=True +) +n2 = cluster.add_instance( + "n2", main_configs=["configs/remote_servers.xml"], with_zookeeper=True +) +n3 = cluster.add_instance( + "n3", main_configs=["configs/remote_servers.xml"], with_zookeeper=True +) +n4 = cluster.add_instance( + "n4", main_configs=["configs/remote_servers.xml"], with_zookeeper=True +) +nodes = [n1, n2, n3, n4] + + +@pytest.fixture(scope="module", autouse=True) +def start_cluster(): + try: + cluster.start() + yield cluster + finally: + cluster.shutdown() + + +def create_tables(cluster): + n1.query("DROP TABLE IF EXISTS dist_table") + n1.query(f"DROP TABLE IF EXISTS test_table ON CLUSTER {cluster}") + + n1.query( + f"CREATE TABLE test_table ON CLUSTER {cluster} (key Int32, value String) Engine=MergeTree ORDER BY (key, sipHash64(value))" + ) + n1.query( + f""" + CREATE TABLE dist_table AS test_table + Engine=Distributed( + {cluster}, + currentDatabase(), + test_table, + rand() + ) + """ + ) + + +def insert_data(cluster, row_num): + create_tables(cluster) + n1.query(f"INSERT INTO dist_table SELECT number, number FROM numbers({row_num})") + n1.query("SYSTEM FLUSH DISTRIBUTED dist_table") + + +@pytest.mark.parametrize("custom_key", ["sipHash64(value)", "key"]) +@pytest.mark.parametrize("filter_type", ["default", "range"]) +@pytest.mark.parametrize( + "cluster", + ["test_multiple_shards_multiple_replicas", "test_single_shard_multiple_replicas"], +) +def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter_type): + for node in nodes: + node.rotate_logs() + + row_num = 1000 + insert_data(cluster, row_num) + assert ( + int( + n1.query( + "SELECT count() FROM dist_table", + settings={ + "prefer_localhost_replica": 0, + "max_parallel_replicas": 3, + "parallel_replicas_mode": "custom_key", + "parallel_replicas_custom_key": custom_key, + "parallel_replicas_custom_key_filter_type": filter_type, + }, + ) + ) + == row_num + ) + + if cluster == "test_multiple_shards_multiple_replicas": + # we simply process query on all replicas for each shard by appending the filter on replica + assert all( + node.contains_in_log("Processing query on a replica using custom_key") + for node in nodes + ) + else: + # we first transform all replicas into shards and then append for each shard filter + assert n1.contains_in_log( + "Single shard cluster used with custom_key, transforming replicas into virtual shards" + ) From ec149c4585c5a7655a275126be59b1694aeafd22 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 20 Jan 2023 12:56:20 +0000 Subject: [PATCH 029/342] Remove old test --- ...rallel_replicas_custom_key_range.reference | 13 ------- ...max_parallel_replicas_custom_key_range.sql | 37 ------------------- 2 files changed, 50 deletions(-) delete mode 100644 tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference delete mode 100644 tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql diff --git a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference deleted file mode 100644 index c51a7e10fb2..00000000000 --- a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.reference +++ /dev/null @@ -1,13 +0,0 @@ -Hello -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 diff --git a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql b/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql deleted file mode 100644 index a8ef9e58d40..00000000000 --- a/tests/queries/0_stateless/02528_max_parallel_replicas_custom_key_range.sql +++ /dev/null @@ -1,37 +0,0 @@ --- Tags: replica - -DROP TABLE IF EXISTS t; - -CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; -INSERT INTO t VALUES ('Hello'); - -SET max_parallel_replicas = 3; -SET parallel_replicas_mode = 'custom_key'; -SET parallel_replicas_custom_key_filter_type = 'range'; - -SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'sipHash64(x)'; - -DROP TABLE t; - -CREATE TABLE t (x String, y UInt32) ENGINE = MergeTree ORDER BY cityHash64(x) SAMPLE BY cityHash64(x); -INSERT INTO t SELECT toString(number), number FROM numbers(1000); - -SET max_parallel_replicas = 1; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; - -SET max_parallel_replicas = 2; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; - -SET max_parallel_replicas = 3; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + y'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x)'; -SELECT count() FROM remote('127.0.0.{2|3|4}', currentDatabase(), t) SETTINGS parallel_replicas_custom_key = 'cityHash64(x) + 1'; - -DROP TABLE t; From 7bfaf88666a28d76d47a5ba4e3bd2b2925ce90fb Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 20 Jan 2023 16:35:14 +0000 Subject: [PATCH 030/342] Fix stateless tests --- .../0_stateless/00124_shard_distributed_with_many_replicas.sql | 1 + .../01034_prewhere_max_parallel_replicas_distributed.sql | 3 +++ tests/queries/0_stateless/01034_sample_final_distributed.sql | 3 +++ .../0_stateless/01099_parallel_distributed_insert_select.sql | 1 + tests/queries/0_stateless/01517_select_final_distributed.sql | 2 ++ .../0_stateless/01557_max_parallel_replicas_no_sample.sql | 1 + .../0_stateless/02535_max_parallel_replicas_custom_key.sh | 1 + 7 files changed, 12 insertions(+) diff --git a/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql b/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql index ab4c433ba47..795551e5dfa 100644 --- a/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql +++ b/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql @@ -1,6 +1,7 @@ -- Tags: replica, distributed SET max_parallel_replicas = 2; +SET parallel_replicas_mode = 'sample_key'; DROP TABLE IF EXISTS report; set allow_deprecated_syntax_for_merge_tree=1; diff --git a/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql b/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql index b26c9af14a9..96d52d7e60e 100644 --- a/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql +++ b/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql @@ -9,6 +9,9 @@ CREATE TABLE test_max_parallel_replicas_lr (timestamp UInt64) ENGINE = MergeTree INSERT INTO test_max_parallel_replicas_lr select number as timestamp from system.numbers limit 100; SET max_parallel_replicas = 2; +SET parallel_replicas_mode = 'sample_key'; +SET allow_experimental_parallel_reading_from_replicas = 0; + select count() FROM remote('127.0.0.{2|3}', currentDatabase(), test_max_parallel_replicas_lr) PREWHERE timestamp > 0; drop table test_max_parallel_replicas_lr; diff --git a/tests/queries/0_stateless/01034_sample_final_distributed.sql b/tests/queries/0_stateless/01034_sample_final_distributed.sql index b784b35cbb3..1b1832f7a0d 100644 --- a/tests/queries/0_stateless/01034_sample_final_distributed.sql +++ b/tests/queries/0_stateless/01034_sample_final_distributed.sql @@ -13,7 +13,10 @@ select count() from sample_final sample 1/2; select 'count sample final'; select count() from sample_final final sample 1/2; select 'count final max_parallel_replicas'; + +set allow_experimental_parallel_reading_from_replicas = 0; set max_parallel_replicas=2; +set parallel_replicas_mode='sample_key'; select count() from remote('127.0.0.{2|3}', currentDatabase(), sample_final) final; drop table if exists sample_final; diff --git a/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql b/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql index a75a5b2c33d..aa924218360 100644 --- a/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql +++ b/tests/queries/0_stateless/01099_parallel_distributed_insert_select.sql @@ -2,6 +2,7 @@ -- set insert_distributed_sync = 1; -- see https://github.com/ClickHouse/ClickHouse/issues/18971 +SET allow_experimental_parallel_reading_from_replicas = 0; -- see https://github.com/ClickHouse/ClickHouse/issues/34525 SET prefer_localhost_replica = 1; DROP TABLE IF EXISTS local_01099_a; diff --git a/tests/queries/0_stateless/01517_select_final_distributed.sql b/tests/queries/0_stateless/01517_select_final_distributed.sql index a3d1fcfc185..701828b0b38 100644 --- a/tests/queries/0_stateless/01517_select_final_distributed.sql +++ b/tests/queries/0_stateless/01517_select_final_distributed.sql @@ -1,5 +1,7 @@ -- Tags: distributed +SET allow_experimental_parallel_reading_from_replicas = 0; + DROP TABLE IF EXISTS test5346; CREATE TABLE test5346 (`Id` String, `Timestamp` DateTime, `updated` DateTime) diff --git a/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql b/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql index 2b1a66147a4..c44c335700f 100644 --- a/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql +++ b/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql @@ -4,6 +4,7 @@ DROP TABLE IF EXISTS t; CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; INSERT INTO t VALUES ('Hello'); +SET parallel_replicas_mode = 'sample_key'; SET max_parallel_replicas = 3; SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t); diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh index 76138842e9a..e97643bd366 100755 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# Tags: no-parallel, long CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh From 40f42aa0789b6e501e7e13ce391896317778a316 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Sun, 22 Jan 2023 14:15:21 +0000 Subject: [PATCH 031/342] Remove pipeline from test --- ...max_parallel_replicas_custom_key.reference | 534 ------------------ .../02535_max_parallel_replicas_custom_key.sh | 2 - 2 files changed, 536 deletions(-) diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference index 01f48778b34..21ed26b7579 100644 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference @@ -1,659 +1,125 @@ query='SELECT * FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='sipHash64(x)' filter_type='default' max_replicas=1 prefer_localhost_replica=0 Hello -(ReadFromRemote) filter_type='default' max_replicas=2 prefer_localhost_replica=0 Hello -(ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=0 Hello -(ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=0 Hello -(ReadFromRemote) filter_type='range' max_replicas=2 prefer_localhost_replica=0 Hello -(ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=0 Hello -(ReadFromRemote) filter_type='default' max_replicas=1 prefer_localhost_replica=1 Hello -(Expression) -ExpressionTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 filter_type='default' max_replicas=2 prefer_localhost_replica=1 Hello -(Union) - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=1 Hello -(Union) - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=1 Hello -(Expression) -ExpressionTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 filter_type='range' max_replicas=2 prefer_localhost_replica=1 Hello -(Union) - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=1 Hello -(Union) - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x)' filter_type='default' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='default' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='range' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='default' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='default' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='range' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='y' filter_type='default' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='default' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='range' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='default' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='default' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='range' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x) + y' filter_type='default' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='default' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='range' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='default' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='default' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='range' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x) + 1' filter_type='default' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='default' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=0 1000 -(ReadFromRemote) filter_type='range' max_replicas=2 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=0 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (ReadFromRemote) filter_type='default' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='default' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='default' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=1 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform - (ReadFromStorage) - AggregatingTransform - ExpressionTransform - SourceFromSingleChunk 0 → 1 filter_type='range' max_replicas=2 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 2 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) filter_type='range' max_replicas=3 prefer_localhost_replica=1 1000 -(Expression) -ExpressionTransform × 4 - (MergingAggregated) - Resize 1 → 4 - SortingAggregatedTransform 4 → 1 - MergingAggregatedBucketTransform × 4 - Resize 1 → 4 - GroupingAggregatedTransform 3 → 1 - (Union) - (Aggregating) - AggregatingTransform - (Expression) - ExpressionTransform - (Filter) - FilterTransform - (ReadFromMergeTree) - MergeTreeInOrder 0 → 1 - (ReadFromRemote) diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh index e97643bd366..40f62d39769 100755 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh @@ -15,10 +15,8 @@ function run_with_custom_key { , parallel_replicas_mode='custom_key'\ , parallel_replicas_custom_key='$2'\ , parallel_replicas_custom_key_filter_type='$filter_type'\ - , max_threads=4\ , prefer_localhost_replica=$prefer_localhost_replica" $CLICKHOUSE_CLIENT --query="$query" - $CLICKHOUSE_CLIENT --query="EXPLAIN PIPELINE $query" done done done From 3a0941573376d4e2001b9c5937715a2a021a483a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Sun, 22 Jan 2023 14:20:25 +0000 Subject: [PATCH 032/342] Refactor integration test --- .../test_parallel_replicas_custom_key/test.py | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/integration/test_parallel_replicas_custom_key/test.py b/tests/integration/test_parallel_replicas_custom_key/test.py index 0787234a7b1..0819aceb8c7 100644 --- a/tests/integration/test_parallel_replicas_custom_key/test.py +++ b/tests/integration/test_parallel_replicas_custom_key/test.py @@ -3,19 +3,12 @@ from helpers.cluster import ClickHouseCluster cluster = ClickHouseCluster(__file__) -n1 = cluster.add_instance( - "n1", main_configs=["configs/remote_servers.xml"], with_zookeeper=True -) -n2 = cluster.add_instance( - "n2", main_configs=["configs/remote_servers.xml"], with_zookeeper=True -) -n3 = cluster.add_instance( - "n3", main_configs=["configs/remote_servers.xml"], with_zookeeper=True -) -n4 = cluster.add_instance( - "n4", main_configs=["configs/remote_servers.xml"], with_zookeeper=True -) -nodes = [n1, n2, n3, n4] +nodes = [ + cluster.add_instance( + f"n{i}", main_configs=["configs/remote_servers.xml"], with_zookeeper=True + ) + for i in range(1, 5) +] @pytest.fixture(scope="module", autouse=True) @@ -28,6 +21,7 @@ def start_cluster(): def create_tables(cluster): + n1 = nodes[0] n1.query("DROP TABLE IF EXISTS dist_table") n1.query(f"DROP TABLE IF EXISTS test_table ON CLUSTER {cluster}") @@ -49,6 +43,7 @@ def create_tables(cluster): def insert_data(cluster, row_num): create_tables(cluster) + n1 = nodes[0] n1.query(f"INSERT INTO dist_table SELECT number, number FROM numbers({row_num})") n1.query("SYSTEM FLUSH DISTRIBUTED dist_table") @@ -65,6 +60,8 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter row_num = 1000 insert_data(cluster, row_num) + + n1 = nodes[0] assert ( int( n1.query( From c15128491bd3fc451abd07807e9ed95cee335710 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 23 Jan 2023 17:24:48 +0100 Subject: [PATCH 033/342] Update max_parallel_replicas docs --- docs/en/operations/settings/settings.md | 54 ++++++++++++++++++++++++- docs/en/sql-reference/operators/in.md | 13 ++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/docs/en/operations/settings/settings.md b/docs/en/operations/settings/settings.md index e3f7bc11ddf..9fd4ccafb18 100644 --- a/docs/en/operations/settings/settings.md +++ b/docs/en/operations/settings/settings.md @@ -1221,7 +1221,9 @@ Possible values: Default value: 1. :::warning -Disable this setting if you use [max_parallel_replicas](#settings-max_parallel_replicas). +Disable this setting if you use [max_parallel_replicas](#settings-max_parallel_replicas) with [parallel_replicas_mode](#settings-parallel_replicas_mode) set to `sample_key` or `read_tasks`. +If [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `custom_key`, disable this setting only if it's used on a cluster with multiple shards containing multiple replicas. +If it's used on a cluster with a single shard and multiple replicas, disabling this setting will have negative effects. ::: ## totals_mode {#totals-mode} @@ -1246,17 +1248,65 @@ Default value: `1`. **Additional Info** -This setting is useful for replicated tables with a sampling key. A query may be processed faster if it is executed on several servers in parallel. But the query performance may degrade in the following cases: +This options will produce different results depending on the value of [parallel_replicas_mode](#settings-parallel_replicas_mode). + +### `sample_key` + +If [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `sample_key`, this setting is useful for replicated tables with a sampling key. +A query may be processed faster if it is executed on several servers in parallel. But the query performance may degrade in the following cases: - The position of the sampling key in the partitioning key does not allow efficient range scans. - Adding a sampling key to the table makes filtering by other columns less efficient. - The sampling key is an expression that is expensive to calculate. - The cluster latency distribution has a long tail, so that querying more servers increases the query overall latency. +### `custom_key` + +If [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `custom_key`, this setting is useful for any replicated table. +A query may be processed faster if it is executed on several servers in parallel but it depends on the used [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key) +and [parallel_replicas_custom_key_filter_type](#settings-parallel_replicas_custom_key_filter_type). + +Use `default` for [parallel_replicas_custom_key_filter_type](#settings-parallel_replicas_custom_key_filter_type) unless the data is split across the entire integer space (e.g. column contains hash values), +then `range` should be used. +Simple expressions using primary keys are preferred. + +If the `custom_key` mode is used on a cluster that consists of a single shard with multiple replicas, those replicas will be converted into virtual shards. +Otherwise, it will behave same as `sample_key` mode, it will use multiple replicas of each shard. + :::warning This setting will produce incorrect results when joins or subqueries are involved, and all tables don't meet certain requirements. See [Distributed Subqueries and max_parallel_replicas](../../sql-reference/operators/in.md/#max_parallel_replica-subqueries) for more details. ::: +## parallel_replicas_mode {#settings-parallel_replicas_mode} + +Mode of splitting work between replicas. + +Possible values: + +- `sample_key` — Use `SAMPLE` key defined in the `SAMPLE BY` clause to split the work between replicas. +- `custom_key` — Define an arbitrary integer expression to use for splitting work between replicas. +- `read_tasks` — Split tasks for reading physical parts between replicas. + +Default value: `sample_key`. + +## parallel_replicas_custom_key {#settings-parallel_replicas_custom_key} + +Arbitrary integer expression that will be used to split work between replicas. +Used only if `parallel_replicas_mode` is set to `custom_key`. + +Default value: `''`. + +## parallel_replicas_custom_key_filter_type {#settings-parallel_replicas_custom_key_filter_type} + +How to use `parallel_replicas_custom_key` expression for splitting work between replicas. + +Possible values: + +- `default` — Use the default implementation using modulo operation on the `parallel_replicas_custom_key`. +- `range` — Split the entire value space of the expression in the ranges. This type of filtering is useful if values of `parallel_replicas_custom_key` are uniformly spread across the entire integer space, e.g. hash values. + +Default value: `default`. + ## compile_expressions {#compile-expressions} Enables or disables compilation of frequently used simple functions and operators to native code with LLVM at runtime. diff --git a/docs/en/sql-reference/operators/in.md b/docs/en/sql-reference/operators/in.md index 58119cfc4f5..e1e4118524a 100644 --- a/docs/en/sql-reference/operators/in.md +++ b/docs/en/sql-reference/operators/in.md @@ -233,11 +233,12 @@ If `some_predicate` is not selective enough, it will return large amount of data ### Distributed Subqueries and max_parallel_replicas -When max_parallel_replicas is greater than 1, distributed queries are further transformed. For example, the following: +When [max_parallel_replicas](#settings-max_parallel_replicas) is greater than 1, distributed queries are further transformed. +For example, if [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `sample_key`, the following: ```sql SELECT CounterID, count() FROM distributed_table_1 WHERE UserID IN (SELECT UserID FROM local_table_2 WHERE CounterID < 100) -SETTINGS max_parallel_replicas=3 +SETTINGS max_parallel_replicas=3, parallel_replicas_mode='sample_key' ``` is transformed on each server into @@ -247,8 +248,12 @@ SELECT CounterID, count() FROM local_table_1 WHERE UserID IN (SELECT UserID FROM SETTINGS parallel_replicas_count=3, parallel_replicas_offset=M ``` -where M is between 1 and 3 depending on which replica the local query is executing on. These settings affect every MergeTree-family table in the query and have the same effect as applying `SAMPLE 1/3 OFFSET (M-1)/3` on each table. +where M is between 1 and 3 depending on which replica the local query is executing on. -Therefore adding the max_parallel_replicas setting will only produce correct results if both tables have the same replication scheme and are sampled by UserID or a subkey of it. In particular, if local_table_2 does not have a sampling key, incorrect results will be produced. The same rule applies to JOIN. +These settings affect every MergeTree-family table in the query and have the same effect as applying `SAMPLE 1/3 OFFSET (M-1)/3` on each table. + +Therefore adding the [max_parallel_replicas](#settings-max_parallel_replicas) setting will only produce correct results if both tables have the same replication scheme and are sampled by UserID or a subkey of it. In particular, if local_table_2 does not have a sampling key, incorrect results will be produced. The same rule applies to JOIN. One workaround if local_table_2 does not meet the requirements, is to use `GLOBAL IN` or `GLOBAL JOIN`. + +If a table doesn't have a sampling key, more flexible options for [parallel_replicas_mode](#settings-parallel_replicas_mode) can be used that can produce different and more optimal behaviour. From 37b62b3a589d0dd57dcfc72a5043a2608b0c2f20 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 24 Jan 2023 10:46:47 +0000 Subject: [PATCH 034/342] Use Map for custom_key --- docs/en/operations/settings/settings.md | 8 +- src/Core/Settings.h | 2 +- src/Interpreters/InterpreterSelectQuery.cpp | 24 +++--- .../getCustomKeyFilterForParallelReplicas.cpp | 44 +++++++++-- .../getCustomKeyFilterForParallelReplicas.h | 5 +- src/Storages/StorageDistributed.cpp | 78 ++++++++++++++----- .../test_parallel_replicas_custom_key/test.py | 46 ++++++++++- .../02535_max_parallel_replicas_custom_key.sh | 2 +- 8 files changed, 166 insertions(+), 43 deletions(-) diff --git a/docs/en/operations/settings/settings.md b/docs/en/operations/settings/settings.md index 9fd4ccafb18..68690e8a173 100644 --- a/docs/en/operations/settings/settings.md +++ b/docs/en/operations/settings/settings.md @@ -1291,10 +1291,14 @@ Default value: `sample_key`. ## parallel_replicas_custom_key {#settings-parallel_replicas_custom_key} -Arbitrary integer expression that will be used to split work between replicas. +Map of arbitrary integer expression that can be used to split work between replicas for a specific table. +If it's used with `cluster` function, the key can be name of the local table defined inside the `cluster` function. +If it's used with `Distributed` engine, the key can be name of the distributed table, alias or the local table for which the `Distributed` engine is created. +The value can be any integer expression. + Used only if `parallel_replicas_mode` is set to `custom_key`. -Default value: `''`. +Default value: `{}`. ## parallel_replicas_custom_key_filter_type {#settings-parallel_replicas_custom_key_filter_type} diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 89686c92960..2c8b1c90faa 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -154,7 +154,7 @@ static constexpr UInt64 operator""_GiB(unsigned long long value) M(UInt64, max_parallel_replicas, 1, "The maximum number of replicas of each shard used when the query is executed. For consistency (to get different parts of the same partition), this option only works for the specified sampling key. The lag of the replicas is not controlled.", 0) \ M(UInt64, parallel_replicas_count, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the number of parallel replicas participating in query processing.", 0) \ M(UInt64, parallel_replica_offset, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the index of the replica participating in query processing among parallel replicas.", 0) \ - M(String, parallel_replicas_custom_key, "", "Custom key for parallel replicas using modulo operation on the key for assigning work to replicas.", 0) \ + M(Map, parallel_replicas_custom_key, "", "Custom key for parallel replicas using modulo operation on the key for assigning work to replicas.", 0) \ M(ParallelReplicasCustomKeyFilterType, parallel_replicas_custom_key_filter_type, ParallelReplicasCustomKeyFilterType::DEFAULT, "Type of filter to use with custom key for parallel replicas. default - use modulo operation on the custom key, range - use range filter on custom key using all possible values for the value type of custom key.", 0) \ M(ParallelReplicasMode, parallel_replicas_mode, ParallelReplicasMode::SAMPLE_KEY, "How to process query using multiple replicas.", 0) \ \ diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 817d40c086a..d174912f66d 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -513,18 +513,20 @@ InterpreterSelectQuery::InterpreterSelectQuery( ASTPtr parallel_replicas_custom_filter_ast = nullptr; if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) { - LOG_INFO(log, "Processing query on a replica using custom_key"); - if (!storage) - throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); + if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, joined_tables.tablesWithColumns().front().table, *context)) + { + LOG_INFO(log, "Processing query on a replica using custom_key"); + if (!storage) + throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); - auto custom_key_ast = parseParallelReplicaCustomKey(settings.parallel_replicas_custom_key.value, *context); - parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica( - settings.parallel_replicas_count, - settings.parallel_replica_offset, - std::move(custom_key_ast), - settings.parallel_replicas_custom_key_filter_type, - *storage, - context); + parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica( + settings.parallel_replicas_count, + settings.parallel_replica_offset, + std::move(custom_key_ast), + settings.parallel_replicas_custom_key_filter_type, + *storage, + context); + } } auto analyze = [&](bool try_move_to_prewhere) diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp index fbc1d214164..cfc7cfd6194 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp @@ -116,15 +116,43 @@ ASTPtr getCustomKeyFilterForParallelReplica( return makeASTFunction("and", std::move(lower_function), std::move(upper_function)); } -ASTPtr parseParallelReplicaCustomKey(std::string_view custom_key, const Context & context) +ASTPtr parseCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context) { - if (custom_key.empty()) - throw Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Parallel replicas mode set to 'custom_key' but 'parallel_replicas_custom_key' has no value"); + for (size_t i = 0; i < custom_keys.size(); ++i) + { + const auto & tuple = custom_keys[i].safeGet(); + auto & table = tuple.at(0).safeGet(); + auto & filter = tuple.at(1).safeGet(); - ParserExpression parser; - const auto & settings = context.getSettingsRef(); - return parseQuery( - parser, custom_key.data(), custom_key.data() + custom_key.size(), - "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); + if (table == target.alias || + (table == target.table && context.getCurrentDatabase() == target.database) || + (table == target.database + '.' + target.table)) + { + /// Try to parse expression + ParserExpression parser; + const auto & settings = context.getSettingsRef(); + return parseQuery( + parser, filter.data(), filter.data() + filter.size(), + "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); + } + } + + return nullptr; } + +bool containsCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context) +{ + for (size_t i = 0; i < custom_keys.size(); ++i) + { + const auto & tuple = custom_keys[i].safeGet(); + auto & table = tuple.at(0).safeGet(); + + if (table == target.alias || + (table == target.table && context.getCurrentDatabase() == target.database) || + (table == target.database + '.' + target.table)) + return true; + } + return false; +} + } diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h index 251e72e0a97..fc515132487 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace DB { @@ -18,6 +19,8 @@ ASTPtr getCustomKeyFilterForParallelReplica( const IStorage & storage, const ContextPtr & context); -ASTPtr parseParallelReplicaCustomKey(std::string_view custom_key, const Context & context); +ASTPtr parseCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context); + +bool containsCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context); } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index bf9ae2fdd20..7a3bd89e5ff 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -27,7 +27,6 @@ #include #include #include -#include "Core/SettingsEnums.h" #include #include @@ -47,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +80,7 @@ #include #include +#include #include #include @@ -91,8 +92,6 @@ #include #include -#include - namespace fs = std::filesystem; @@ -307,13 +306,13 @@ size_t getClusterQueriedNodes(const Settings & settings, const ClusterPtr & clus return (num_remote_shards + num_local_shards) * settings.max_parallel_replicas; } -bool useVirtualShards(const Settings & settings, const Cluster & cluster) +bool canUseCustomKey(const Settings & settings, const Cluster & cluster) { return settings.max_parallel_replicas > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY && cluster.getShardCount() == 1 && cluster.getShardsInfo()[0].getAllNodeCount() > 1; } -} +} /// For destruction of std::unique_ptr of type that is incomplete in class definition. StorageDistributed::~StorageDistributed() = default; @@ -453,7 +452,33 @@ QueryProcessingStage::Enum StorageDistributed::getQueryProcessingStage( size_t nodes = getClusterQueriedNodes(settings, cluster); - if (useVirtualShards(settings, *cluster)) + const auto use_virtual_shards = [&] + { + if (!canUseCustomKey(settings, *cluster)) + return false; + + auto distributed_table = DatabaseAndTableWithAlias( + *getTableExpression(query_info.query->as(), 0), local_context->getCurrentDatabase()); + + if (containsCustomKeyForTable(settings.parallel_replicas_custom_key, distributed_table, *local_context)) + { + LOG_INFO(log, "Found custom_key for {}", distributed_table.getQualifiedNamePrefix(false)); + return true; + } + + DatabaseAndTableWithAlias remote_table_info; + remote_table_info.database = remote_database; + remote_table_info.table = remote_table; + if (containsCustomKeyForTable(settings.parallel_replicas_custom_key, remote_table_info, *local_context)) + { + LOG_INFO(log, "Found custom_key for {}", remote_table_info.getQualifiedNamePrefix(false)); + return true; + } + + return false; + }; + + if (use_virtual_shards()) { LOG_INFO(log, "Single shard cluster used with custom_key, transforming replicas into virtual shards"); @@ -767,23 +792,40 @@ void StorageDistributed::read( auto settings = local_context->getSettingsRef(); ClusterProxy::AdditionalShardFilterGenerator additional_shard_filter_generator; - if (useVirtualShards(settings, *getCluster())) + if (canUseCustomKey(settings, *getCluster())) { - if (query_info.getCluster()->getShardCount() == 1) + const auto get_custom_key_ast = [&]() -> ASTPtr { - // we are reading from single shard with multiple replicas but didn't transform replicas - // into virtual shards with custom_key set - throw Exception(ErrorCodes::LOGICAL_ERROR, "Replicas weren't transformed into virtual shards"); - } + auto distributed_table = DatabaseAndTableWithAlias( + *getTableExpression(query_info.query->as(), 0), local_context->getCurrentDatabase()); + if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, distributed_table, *local_context)) + return custom_key_ast; - auto custom_key_ast = parseParallelReplicaCustomKey(settings.parallel_replicas_custom_key.value, *local_context); + DatabaseAndTableWithAlias remote_table_info; + remote_table_info.database = remote_database; + remote_table_info.table = remote_table; + if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, remote_table_info, *local_context)) + return custom_key_ast; - additional_shard_filter_generator = - [&, custom_key_ast = std::move(custom_key_ast), shard_count = query_info.cluster->getShardCount()](uint64_t shard_num) -> ASTPtr - { - return getCustomKeyFilterForParallelReplica( - shard_count, shard_num - 1, custom_key_ast, settings.parallel_replicas_custom_key_filter_type, *this, local_context); + return nullptr; }; + + if (auto custom_key_ast = get_custom_key_ast()) + { + if (query_info.getCluster()->getShardCount() == 1) + { + // we are reading from single shard with multiple replicas but didn't transform replicas + // into virtual shards with custom_key set + throw Exception(ErrorCodes::LOGICAL_ERROR, "Replicas weren't transformed into virtual shards"); + } + + additional_shard_filter_generator = + [&, custom_key_ast = std::move(custom_key_ast), shard_count = query_info.cluster->getShardCount()](uint64_t shard_num) -> ASTPtr + { + return getCustomKeyFilterForParallelReplica( + shard_count, shard_num - 1, custom_key_ast, settings.parallel_replicas_custom_key_filter_type, *this, local_context); + }; + } } bool parallel_replicas = settings.max_parallel_replicas > 1 && settings.allow_experimental_parallel_reading_from_replicas diff --git a/tests/integration/test_parallel_replicas_custom_key/test.py b/tests/integration/test_parallel_replicas_custom_key/test.py index 0819aceb8c7..c11be610611 100644 --- a/tests/integration/test_parallel_replicas_custom_key/test.py +++ b/tests/integration/test_parallel_replicas_custom_key/test.py @@ -70,7 +70,7 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter "prefer_localhost_replica": 0, "max_parallel_replicas": 3, "parallel_replicas_mode": "custom_key", - "parallel_replicas_custom_key": custom_key, + "parallel_replicas_custom_key": f"{{'test_table': '{custom_key}'}}", "parallel_replicas_custom_key_filter_type": filter_type, }, ) @@ -89,3 +89,47 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter assert n1.contains_in_log( "Single shard cluster used with custom_key, transforming replicas into virtual shards" ) + + +def test_custom_key_different_table_names(start_cluster): + def run(table_source, table_name_for_custom_key): + for node in nodes: + node.rotate_logs() + + row_num = 1000 + insert_data("test_single_shard_multiple_replicas", row_num) + + n1 = nodes[0] + assert ( + int( + n1.query( + f"SELECT count() FROM {table_source}", + settings={ + "prefer_localhost_replica": 0, + "max_parallel_replicas": 3, + "parallel_replicas_mode": "custom_key", + "parallel_replicas_custom_key": f"{{'{table_name_for_custom_key}': 'sipHash64(value)'}}", + }, + ) + ) + == row_num + ) + + # we first transform all replicas into shards and then append for each shard filter + assert n1.contains_in_log( + "Single shard cluster used with custom_key, transforming replicas into virtual shards" + ) + + run("dist_table", "dist_table") + run("dist_table as d", "d") + run("dist_table as d", "dist_table") + run("dist_table as d", "test_table") + run( + "cluster('test_single_shard_multiple_replicas', default.test_table)", + "test_table", + ) + run("cluster('test_single_shard_multiple_replicas', default.test_table) as d", "d") + run( + "cluster('test_single_shard_multiple_replicas', default.test_table) as d", + "test_table", + ) diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh index 40f62d39769..b6e5c853dc2 100755 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh @@ -13,7 +13,7 @@ function run_with_custom_key { echo "filter_type='$filter_type' max_replicas=$max_replicas prefer_localhost_replica=$prefer_localhost_replica" query="$1 SETTINGS max_parallel_replicas=$max_replicas\ , parallel_replicas_mode='custom_key'\ - , parallel_replicas_custom_key='$2'\ + , parallel_replicas_custom_key={'02535_custom_key': '$2'}\ , parallel_replicas_custom_key_filter_type='$filter_type'\ , prefer_localhost_replica=$prefer_localhost_replica" $CLICKHOUSE_CLIENT --query="$query" From 7df545fbe3af5c6ea688c8ee0a06d1004c6fa349 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 24 Jan 2023 11:21:09 +0000 Subject: [PATCH 035/342] Remove unused define --- src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp index cfc7cfd6194..229668ceff4 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp @@ -18,7 +18,6 @@ namespace DB namespace ErrorCodes { extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; - extern const int BAD_ARGUMENTS; } ASTPtr getCustomKeyFilterForParallelReplica( From ffa3d1d603d0e77b7cf0fa71a70ac3f53e7671ff Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 24 Jan 2023 13:58:42 +0000 Subject: [PATCH 036/342] Run query on 1 replica if no custom_key --- src/Interpreters/InterpreterSelectQuery.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 996efe2b4cf..9cecd53f6c8 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -511,7 +511,7 @@ InterpreterSelectQuery::InterpreterSelectQuery( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); ASTPtr parallel_replicas_custom_filter_ast = nullptr; - if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY) + if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY && !joined_tables.tablesWithColumns().empty()) { if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, joined_tables.tablesWithColumns().front().table, *context)) { @@ -527,6 +527,14 @@ InterpreterSelectQuery::InterpreterSelectQuery( *storage, context); } + else if (settings.parallel_replica_offset > 0) + { + LOG_DEBUG( + log, + "Will use no data on this replica because parallel replicas processing with custom_key has been requested" + " (setting 'max_parallel_replicas') but the table does not have custom_key defined for it (settings `parallel_replicas_custom_key`)"); + parallel_replicas_custom_filter_ast = std::make_shared(false); + } } auto analyze = [&](bool try_move_to_prewhere) From 6da9dc2407cd11aa3347f64d3869a0ccfaac48d0 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 24 Jan 2023 14:16:26 +0000 Subject: [PATCH 037/342] Correctly handle case with missing custom_key --- .../test_parallel_replicas_custom_key/test.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_parallel_replicas_custom_key/test.py b/tests/integration/test_parallel_replicas_custom_key/test.py index c11be610611..9222f417a94 100644 --- a/tests/integration/test_parallel_replicas_custom_key/test.py +++ b/tests/integration/test_parallel_replicas_custom_key/test.py @@ -68,7 +68,7 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter "SELECT count() FROM dist_table", settings={ "prefer_localhost_replica": 0, - "max_parallel_replicas": 3, + "max_parallel_replicas": 4, "parallel_replicas_mode": "custom_key", "parallel_replicas_custom_key": f"{{'test_table': '{custom_key}'}}", "parallel_replicas_custom_key_filter_type": filter_type, @@ -92,7 +92,7 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter def test_custom_key_different_table_names(start_cluster): - def run(table_source, table_name_for_custom_key): + def run(table_source, table_name_for_custom_key, should_use_virtual_shard=True): for node in nodes: node.rotate_logs() @@ -106,7 +106,7 @@ def test_custom_key_different_table_names(start_cluster): f"SELECT count() FROM {table_source}", settings={ "prefer_localhost_replica": 0, - "max_parallel_replicas": 3, + "max_parallel_replicas": 4, "parallel_replicas_mode": "custom_key", "parallel_replicas_custom_key": f"{{'{table_name_for_custom_key}': 'sipHash64(value)'}}", }, @@ -116,7 +116,7 @@ def test_custom_key_different_table_names(start_cluster): ) # we first transform all replicas into shards and then append for each shard filter - assert n1.contains_in_log( + assert not should_use_virtual_shard or n1.contains_in_log( "Single shard cluster used with custom_key, transforming replicas into virtual shards" ) @@ -133,3 +133,4 @@ def test_custom_key_different_table_names(start_cluster): "cluster('test_single_shard_multiple_replicas', default.test_table) as d", "test_table", ) + run("dist_table as d", "dist", should_use_virtual_shard=False) From eee7df814926fc323be455c348d0fd8d37e76efc Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Wed, 1 Feb 2023 13:33:32 +0000 Subject: [PATCH 038/342] WIP on group_by_use_nulls --- src/Analyzer/ColumnNode.h | 6 ++ src/Analyzer/FunctionNode.cpp | 3 +- src/Analyzer/FunctionNode.h | 13 +++- src/Analyzer/IQueryTreeNode.h | 5 ++ src/Analyzer/Passes/QueryAnalysisPass.cpp | 81 +++++++++++++++++++---- src/Interpreters/ActionsDAG.cpp | 32 ++++++++- src/Interpreters/ActionsDAG.h | 8 +++ src/Planner/PlannerActionsVisitor.cpp | 7 +- src/Planner/PlannerExpressionAnalysis.cpp | 5 +- 9 files changed, 139 insertions(+), 21 deletions(-) diff --git a/src/Analyzer/ColumnNode.h b/src/Analyzer/ColumnNode.h index e378bc5f3d0..79c0e23c86f 100644 --- a/src/Analyzer/ColumnNode.h +++ b/src/Analyzer/ColumnNode.h @@ -3,6 +3,7 @@ #include #include +#include namespace DB { @@ -117,6 +118,11 @@ public: return column.type; } + void convertToNullable() override + { + column.type = makeNullableSafe(column.type); + } + void dumpTreeImpl(WriteBuffer & buffer, FormatState & state, size_t indent) const override; protected: diff --git a/src/Analyzer/FunctionNode.cpp b/src/Analyzer/FunctionNode.cpp index 7961bfbae31..718dcf4bb58 100644 --- a/src/Analyzer/FunctionNode.cpp +++ b/src/Analyzer/FunctionNode.cpp @@ -99,7 +99,7 @@ void FunctionNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state buffer << ", function_type: " << function_type; if (function) - buffer << ", result_type: " + function->getResultType()->getName(); + buffer << ", result_type: " + getResultType()->getName(); const auto & parameters = getParameters(); if (!parameters.getNodes().empty()) @@ -177,6 +177,7 @@ QueryTreeNodePtr FunctionNode::cloneImpl() const */ result_function->function = function; result_function->kind = kind; + result_function->wrap_with_nullable = wrap_with_nullable; return result_function; } diff --git a/src/Analyzer/FunctionNode.h b/src/Analyzer/FunctionNode.h index 41751ec3f09..0311783b433 100644 --- a/src/Analyzer/FunctionNode.h +++ b/src/Analyzer/FunctionNode.h @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace DB @@ -170,7 +171,16 @@ public: throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Function node with name '{}' is not resolved", function_name); - return function->getResultType(); + auto type = function->getResultType(); + if (wrap_with_nullable) + return makeNullableSafe(type); + return type; + } + + void convertToNullable() override + { + chassert(kind == FunctionKind::ORDINARY); + wrap_with_nullable = true; } void dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const override; @@ -188,6 +198,7 @@ private: String function_name; FunctionKind kind = FunctionKind::UNKNOWN; IResolvedFunctionPtr function; + bool wrap_with_nullable = false; static constexpr size_t parameters_child_index = 0; static constexpr size_t arguments_child_index = 1; diff --git a/src/Analyzer/IQueryTreeNode.h b/src/Analyzer/IQueryTreeNode.h index 8aa834e60b7..3c8e73b9e31 100644 --- a/src/Analyzer/IQueryTreeNode.h +++ b/src/Analyzer/IQueryTreeNode.h @@ -90,6 +90,11 @@ public: throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Method getResultType is not supported for {} query node", getNodeTypeName()); } + virtual void convertToNullable() + { + throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Method convertToNullable is not supported for {} query node", getNodeTypeName()); + } + /** Is tree equal to other tree with node root. * * Aliases of query tree nodes are compared during isEqual call. diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 7d1f636754c..4c1d6d309d6 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -1,5 +1,6 @@ #include +#include "Common/logger_useful.h" #include #include @@ -446,6 +447,9 @@ public: alias_name_to_expressions[node_alias].push_back(node); } + if (const auto * function = node->as()) + LOG_DEBUG(&Poco::Logger::get("ExpressionsStack"), "Pushed function {} on stack", function->getFunctionName()); + expressions.emplace_back(node); } @@ -464,6 +468,9 @@ public: alias_name_to_expressions.erase(it); } + if (const auto * function = top_expression->as()) + LOG_DEBUG(&Poco::Logger::get("ExpressionsStack"), "Poped function {} on stack", function->getFunctionName()); + expressions.pop_back(); } @@ -482,6 +489,22 @@ public: return alias_name_to_expressions.contains(alias); } + bool hasAggregateFunction() const + { + const auto & factory = AggregateFunctionFactory::instance(); + for (const auto & node : expressions) + { + const auto * function = node->as(); + if (!function) + continue; + + if (factory.isAggregateFunctionName(function->getFunctionName())) + return true; + LOG_DEBUG(&Poco::Logger::get("ExpressionsStack"), "Function {} is being resolved, but is not aggregate", function->getFunctionName()); + } + return false; + } + QueryTreeNodePtr getExpressionWithAlias(const std::string & alias) const { auto expression_it = alias_name_to_expressions.find(alias); @@ -708,6 +731,8 @@ struct IdentifierResolveScope /// Table expression node to data std::unordered_map table_expression_node_to_data; + QueryTreeNodePtrWithHashSet nullable_group_by_keys; + /// Use identifier lookup to result cache bool use_identifier_lookup_to_result_cache = true; @@ -1221,7 +1246,6 @@ private: /// Results of scalar sub queries std::unordered_map> scalars; - }; /// Utility functions implementation @@ -3092,6 +3116,11 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook resolve_result.resolve_place = IdentifierResolvePlace::DATABASE_CATALOG; } + if (resolve_result.resolved_identifier + && scope.nullable_group_by_keys.contains(resolve_result.resolved_identifier) + && !scope.expressions_in_resolve_process_stack.hasAggregateFunction()) + resolve_result.resolved_identifier->convertToNullable(); + it->second = resolve_result; /** If identifier was not resolved, or during expression resolution identifier was explicitly added into non cached set, @@ -3099,7 +3128,8 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook */ if (!resolve_result.resolved_identifier || scope.non_cached_identifier_lookups_during_expression_resolve.contains(identifier_lookup) || - !scope.use_identifier_lookup_to_result_cache) + !scope.use_identifier_lookup_to_result_cache || + scope.context->getSettingsRef().group_by_use_nulls) scope.identifier_lookup_to_result.erase(it); return resolve_result; @@ -4627,6 +4657,8 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } function_node.resolveAsFunction(std::move(function_base)); + if (settings.group_by_use_nulls && scope.nullable_group_by_keys.contains(node)) + function_node.convertToNullable(); } catch (Exception & e) { @@ -5873,9 +5905,6 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier auto & query_node_typed = query_node->as(); const auto & settings = scope.context->getSettingsRef(); - if (settings.group_by_use_nulls) - throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "GROUP BY use nulls is not supported"); - bool is_rollup_or_cube = query_node_typed.isGroupByWithRollup() || query_node_typed.isGroupByWithCube(); if (query_node_typed.isGroupByWithGroupingSets() && query_node_typed.isGroupByWithTotals()) @@ -6011,16 +6040,11 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier resolveQueryJoinTreeNode(query_node_typed.getJoinTree(), scope, visitor); } - scope.use_identifier_lookup_to_result_cache = true; + if (!settings.group_by_use_nulls) + scope.use_identifier_lookup_to_result_cache = true; /// Resolve query node sections. - auto projection_columns = resolveProjectionExpressionNodeList(query_node_typed.getProjectionNode(), scope); - if (query_node_typed.getProjection().getNodes().empty()) - throw Exception(ErrorCodes::EMPTY_LIST_OF_COLUMNS_QUERIED, - "Empty list of columns in projection. In scope {}", - scope.scope_node->formatASTForErrorMessage()); - if (query_node_typed.hasWith()) resolveExpressionNodeList(query_node_typed.getWithNode(), scope, true /*allow_lambda_expression*/, false /*allow_table_expression*/); @@ -6041,6 +6065,17 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier resolveExpressionNodeList(grouping_sets_keys_list_node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); } + + for (const auto & grouping_set : query_node_typed.getGroupBy().getNodes()) + { + for (const auto & group_by_elem : grouping_set->as()->getNodes()) + scope.nullable_group_by_keys.insert(group_by_elem->clone()); + } + // for (const auto & grouping_set : query_node_typed.getGroupBy().getNodes()) + // { + // for (const auto & group_by_elem : grouping_set->as()->getNodes()) + // group_by_elem->convertToNullable(); + // } } else { @@ -6048,6 +6083,14 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier replaceNodesWithPositionalArguments(query_node_typed.getGroupByNode(), query_node_typed.getProjection().getNodes(), scope); resolveExpressionNodeList(query_node_typed.getGroupByNode(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); + + if (settings.group_by_use_nulls) + { + for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) + scope.nullable_group_by_keys.insert(group_by_elem->clone()); + // for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) + // group_by_elem->convertToNullable(); + } } } @@ -6100,6 +6143,12 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier validateLimitOffsetExpression(query_node_typed.getOffset(), "OFFSET", scope); } + auto projection_columns = resolveProjectionExpressionNodeList(query_node_typed.getProjectionNode(), scope); + if (query_node_typed.getProjection().getNodes().empty()) + throw Exception(ErrorCodes::EMPTY_LIST_OF_COLUMNS_QUERIED, + "Empty list of columns in projection. In scope {}", + scope.scope_node->formatASTForErrorMessage()); + /** Resolve nodes with duplicate aliases. * Table expressions cannot have duplicate aliases. * @@ -6234,7 +6283,9 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier if (grouping_set_key->as()) continue; - group_by_keys_nodes.push_back(grouping_set_key); + group_by_keys_nodes.push_back(grouping_set_key->clone()); + if (settings.group_by_use_nulls) + group_by_keys_nodes.back()->convertToNullable(); } } else @@ -6242,7 +6293,9 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier if (node->as()) continue; - group_by_keys_nodes.push_back(node); + group_by_keys_nodes.push_back(node->clone()); + if (settings.group_by_use_nulls) + group_by_keys_nodes.back()->convertToNullable(); } } diff --git a/src/Interpreters/ActionsDAG.cpp b/src/Interpreters/ActionsDAG.cpp index 5f1398fed39..4e7dfe2bef5 100644 --- a/src/Interpreters/ActionsDAG.cpp +++ b/src/Interpreters/ActionsDAG.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -199,6 +200,23 @@ const ActionsDAG::Node & ActionsDAG::addFunction( std::move(children), std::move(arguments), std::move(result_name), + function_base->getResultType(), + all_const); +} + +const ActionsDAG::Node & ActionsDAG::addFunction( + const FunctionNode & function, + NodeRawConstPtrs children, + std::string result_name) +{ + auto [arguments, all_const] = getFunctionArguments(children); + + return addFunctionImpl( + function.getFunction(), + std::move(children), + std::move(arguments), + std::move(result_name), + function.getResultType(), all_const); } @@ -214,6 +232,7 @@ const ActionsDAG::Node & ActionsDAG::addFunction( std::move(children), std::move(arguments), std::move(result_name), + function_base->getResultType(), all_const); } @@ -238,6 +257,7 @@ const ActionsDAG::Node & ActionsDAG::addFunctionImpl( NodeRawConstPtrs children, ColumnsWithTypeAndName arguments, std::string result_name, + DataTypePtr result_type, bool all_const) { size_t num_arguments = children.size(); @@ -247,7 +267,7 @@ const ActionsDAG::Node & ActionsDAG::addFunctionImpl( node.children = std::move(children); node.function_base = function_base; - node.result_type = node.function_base->getResultType(); + node.result_type = result_type; node.function = node.function_base->prepare(arguments); node.is_deterministic = node.function_base->isDeterministic(); @@ -2236,7 +2256,15 @@ ActionsDAGPtr ActionsDAG::buildFilterActionsDAG( for (const auto & child : node->children) function_children.push_back(node_to_result_node.find(child)->second); - result_node = &result_dag->addFunction(node->function_base, std::move(function_children), {}); + auto [arguments, all_const] = getFunctionArguments(function_children); + + result_node = &result_dag->addFunctionImpl( + node->function_base, + std::move(function_children), + std::move(arguments), + {}, + node->result_type, + all_const); break; } } diff --git a/src/Interpreters/ActionsDAG.h b/src/Interpreters/ActionsDAG.h index 40bc76fe057..0b240feea0e 100644 --- a/src/Interpreters/ActionsDAG.h +++ b/src/Interpreters/ActionsDAG.h @@ -23,6 +23,8 @@ using FunctionBasePtr = std::shared_ptr; class IFunctionOverloadResolver; using FunctionOverloadResolverPtr = std::shared_ptr; +class FunctionNode; + class IDataType; using DataTypePtr = std::shared_ptr; @@ -139,10 +141,15 @@ public: const FunctionOverloadResolverPtr & function, NodeRawConstPtrs children, std::string result_name); + const Node & addFunction( + const FunctionNode & function, + NodeRawConstPtrs children, + std::string result_name); const Node & addFunction( const FunctionBasePtr & function_base, NodeRawConstPtrs children, std::string result_name); + const Node & addCast(const Node & node_to_cast, const DataTypePtr & cast_type); /// Find first column by name in output nodes. This search is linear. @@ -357,6 +364,7 @@ private: NodeRawConstPtrs children, ColumnsWithTypeAndName arguments, std::string result_name, + DataTypePtr result_type, bool all_const); #if USE_EMBEDDED_COMPILER diff --git a/src/Planner/PlannerActionsVisitor.cpp b/src/Planner/PlannerActionsVisitor.cpp index 95edd93dd9f..13c88d47ca3 100644 --- a/src/Planner/PlannerActionsVisitor.cpp +++ b/src/Planner/PlannerActionsVisitor.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace DB { @@ -82,6 +83,7 @@ public: node_name, actions_dag->dumpNames()); + LOG_DEBUG(&Poco::Logger::get("ActionsScopeNode"), "Node: {} {}", it->second->result_name, it->second->result_type->getName()); return it->second; } @@ -122,7 +124,7 @@ public: } template - const ActionsDAG::Node * addFunctionIfNecessary(const std::string & node_name, ActionsDAG::NodeRawConstPtrs children, FunctionOrOverloadResolver function) + const ActionsDAG::Node * addFunctionIfNecessary(const std::string & node_name, ActionsDAG::NodeRawConstPtrs children, const FunctionOrOverloadResolver & function) { auto it = node_name_to_node.find(node_name); if (it != node_name_to_node.end()) @@ -225,6 +227,7 @@ PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::vi PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::visitColumn(const QueryTreeNodePtr & node) { auto column_node_name = calculateActionNodeName(node, *planner_context, node_to_node_name); + LOG_DEBUG(&Poco::Logger::get("PlannerActionsVisitorImpl"), "Processing column with name: {}", column_node_name); const auto & column_node = node->as(); Int64 actions_stack_size = static_cast(actions_stack.size() - 1); @@ -445,7 +448,7 @@ PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::vi } else { - actions_stack[level].addFunctionIfNecessary(function_node_name, children, function_node.getFunction()); + actions_stack[level].addFunctionIfNecessary(function_node_name, children, function_node); } size_t actions_stack_size = actions_stack.size(); diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index b1017c99c3e..3df6ef640c7 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -299,6 +299,7 @@ ProjectionAnalysisResult analyzeProjection(const QueryNode & query_node, { const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); const auto & projection_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + LOG_DEBUG(&Poco::Logger::get("PlannerExpressionAnalysis"), "Projection node: {}", query_node.getProjectionNode()->dumpTree()); auto projection_actions = buildActionsDAGFromExpressionNode(query_node.getProjectionNode(), projection_input, planner_context); auto projection_columns = query_node.getProjectionColumns(); @@ -320,6 +321,7 @@ ProjectionAnalysisResult analyzeProjection(const QueryNode & query_node, for (size_t i = 0; i < projection_outputs_size; ++i) { auto & projection_column = projection_columns[i]; + LOG_DEBUG(&Poco::Logger::get("PlannerExpressionAnalysis"), "Projection column {}: {} {}", i, projection_column.name, projection_column.type->getName()); const auto * projection_node = projection_actions_outputs[i]; const auto & projection_node_name = projection_node->result_name; @@ -436,7 +438,6 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo } auto window_analysis_result_optional = analyzeWindow(query_tree, join_tree_input_columns, planner_context, actions_chain); - auto projection_analysis_result = analyzeProjection(query_node, join_tree_input_columns, planner_context, actions_chain); std::optional sort_analysis_result_optional; if (query_node.hasOrderBy()) @@ -447,6 +448,8 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo if (query_node.hasLimitBy()) limit_by_analysis_result_optional = analyzeLimitBy(query_node, join_tree_input_columns, planner_context, actions_chain); + auto projection_analysis_result = analyzeProjection(query_node, join_tree_input_columns, planner_context, actions_chain); + const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); const auto & project_names_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; auto project_names_actions = std::make_shared(project_names_input); From f505b798da9282f7b1e2dfa8481bb299a5f365af Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Wed, 1 Feb 2023 17:18:15 +0000 Subject: [PATCH 039/342] Fix ActionDAG construction with group_by_use_nulls --- src/Planner/PlannerExpressionAnalysis.cpp | 80 +++++++++++++---------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index 3df6ef640c7..c7d38839c85 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -16,6 +16,8 @@ #include #include #include +#include "Common/tests/gtest_global_context.h" +#include "DataTypes/DataTypeNullable.h" namespace DB { @@ -33,12 +35,11 @@ namespace * It is client responsibility to update filter analysis result if filter column must be removed after chain is finalized. */ FilterAnalysisResult analyzeFilter(const QueryTreeNodePtr & filter_expression_node, - const ColumnsWithTypeAndName & join_tree_input_columns, + const ColumnsWithTypeAndName & current_output_columns, const PlannerContextPtr & planner_context, ActionsChain & actions_chain) { - const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & filter_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & filter_input = current_output_columns; FilterAnalysisResult result; @@ -52,8 +53,8 @@ FilterAnalysisResult analyzeFilter(const QueryTreeNodePtr & filter_expression_no /** Construct aggregation analysis result if query tree has GROUP BY or aggregates. * Actions before aggregation are added into actions chain, if result is not null optional. */ -std::optional analyzeAggregation(const QueryTreeNodePtr & query_tree, - const ColumnsWithTypeAndName & join_tree_input_columns, +std::pair, std::optional> analyzeAggregation(const QueryTreeNodePtr & query_tree, + const ColumnsWithTypeAndName & current_output_columns, const PlannerContextPtr & planner_context, ActionsChain & actions_chain) { @@ -62,15 +63,12 @@ std::optional analyzeAggregation(const QueryTreeNodeP auto aggregate_function_nodes = collectAggregateFunctionNodes(query_tree); auto aggregates_descriptions = extractAggregateDescriptions(aggregate_function_nodes, *planner_context); - ColumnsWithTypeAndName aggregates_columns; - aggregates_columns.reserve(aggregates_descriptions.size()); - for (auto & aggregate_description : aggregates_descriptions) - aggregates_columns.emplace_back(nullptr, aggregate_description.function->getResultType(), aggregate_description.column_name); + ColumnsWithTypeAndName aggregated_columns; + aggregated_columns.reserve(aggregates_descriptions.size()); Names aggregation_keys; - const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & group_by_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & group_by_input = current_output_columns; ActionsDAGPtr before_aggregation_actions = std::make_shared(group_by_input); before_aggregation_actions->getOutputs().clear(); @@ -83,6 +81,7 @@ std::optional analyzeAggregation(const QueryTreeNodeP PlannerActionsVisitor actions_visitor(planner_context); /// Add expressions from GROUP BY + bool group_by_use_nulls = planner_context->getQueryContext()->getSettingsRef().group_by_use_nulls; if (query_node.hasGroupBy()) { @@ -107,6 +106,8 @@ std::optional analyzeAggregation(const QueryTreeNodeP if (before_aggregation_actions_output_node_names.contains(expression_dag_node->result_name)) continue; + auto expression_type_after_aggregation = group_by_use_nulls ? makeNullableSafe(expression_dag_node->result_type) : expression_dag_node->result_type; + aggregated_columns.emplace_back(nullptr, expression_type_after_aggregation, expression_dag_node->result_name); aggregation_keys.push_back(expression_dag_node->result_name); before_aggregation_actions->getOutputs().push_back(expression_dag_node); before_aggregation_actions_output_node_names.insert(expression_dag_node->result_name); @@ -150,6 +151,8 @@ std::optional analyzeAggregation(const QueryTreeNodeP if (before_aggregation_actions_output_node_names.contains(expression_dag_node->result_name)) continue; + auto expression_type_after_aggregation = group_by_use_nulls ? makeNullableSafe(expression_dag_node->result_type) : expression_dag_node->result_type; + aggregated_columns.emplace_back(nullptr, expression_type_after_aggregation, expression_dag_node->result_name); aggregation_keys.push_back(expression_dag_node->result_name); before_aggregation_actions->getOutputs().push_back(expression_dag_node); before_aggregation_actions_output_node_names.insert(expression_dag_node->result_name); @@ -157,6 +160,9 @@ std::optional analyzeAggregation(const QueryTreeNodeP } } + for (auto & aggregate_description : aggregates_descriptions) + aggregated_columns.emplace_back(nullptr, aggregate_description.function->getResultType(), aggregate_description.column_name); + /// Add expressions from aggregate functions arguments for (auto & aggregate_function_node : aggregate_function_nodes) @@ -183,10 +189,10 @@ std::optional analyzeAggregation(const QueryTreeNodeP * With set number, which is used as an additional key at the stage of merging aggregating data. */ if (query_node.isGroupByWithRollup() || query_node.isGroupByWithCube() || query_node.isGroupByWithGroupingSets()) - aggregates_columns.emplace_back(nullptr, std::make_shared(), "__grouping_set"); + aggregated_columns.emplace_back(nullptr, std::make_shared(), "__grouping_set"); /// Only aggregation keys and aggregates are available for next steps after GROUP BY step - auto aggregate_step = std::make_unique(before_aggregation_actions, ActionsChainStep::AvailableOutputColumnsStrategy::OUTPUT_NODES, aggregates_columns); + auto aggregate_step = std::make_unique(before_aggregation_actions, ActionsChainStep::AvailableOutputColumnsStrategy::OUTPUT_NODES, aggregated_columns); actions_chain.addStep(std::move(aggregate_step)); AggregationAnalysisResult aggregation_analysis_result; @@ -196,14 +202,14 @@ std::optional analyzeAggregation(const QueryTreeNodeP aggregation_analysis_result.grouping_sets_parameters_list = std::move(grouping_sets_parameters_list); aggregation_analysis_result.group_by_with_constant_keys = group_by_with_constant_keys; - return aggregation_analysis_result; + return { aggregation_analysis_result, aggregated_columns }; } /** Construct window analysis result if query tree has window functions. * Actions before window functions are added into actions chain, if result is not null optional. */ std::optional analyzeWindow(const QueryTreeNodePtr & query_tree, - const ColumnsWithTypeAndName & join_tree_input_columns, + const ColumnsWithTypeAndName & current_output_columns, const PlannerContextPtr & planner_context, ActionsChain & actions_chain) { @@ -213,8 +219,7 @@ std::optional analyzeWindow(const QueryTreeNodePtr & query auto window_descriptions = extractWindowDescriptions(window_function_nodes, *planner_context); - const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & window_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & window_input = current_output_columns; PlannerActionsVisitor actions_visitor(planner_context); @@ -293,12 +298,11 @@ std::optional analyzeWindow(const QueryTreeNodePtr & query * It is client responsibility to update projection analysis result with project names actions after chain is finalized. */ ProjectionAnalysisResult analyzeProjection(const QueryNode & query_node, - const ColumnsWithTypeAndName & join_tree_input_columns, + const ColumnsWithTypeAndName & current_output_columns, const PlannerContextPtr & planner_context, ActionsChain & actions_chain) { - const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & projection_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & projection_input = current_output_columns; LOG_DEBUG(&Poco::Logger::get("PlannerExpressionAnalysis"), "Projection node: {}", query_node.getProjectionNode()->dumpTree()); auto projection_actions = buildActionsDAGFromExpressionNode(query_node.getProjectionNode(), projection_input, planner_context); @@ -344,12 +348,11 @@ ProjectionAnalysisResult analyzeProjection(const QueryNode & query_node, * Actions before sort are added into actions chain. */ SortAnalysisResult analyzeSort(const QueryNode & query_node, - const ColumnsWithTypeAndName & join_tree_input_columns, + const ColumnsWithTypeAndName & current_output_columns, const PlannerContextPtr & planner_context, ActionsChain & actions_chain) { - const auto *chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & order_by_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & order_by_input = current_output_columns; ActionsDAGPtr before_sort_actions = std::make_shared(order_by_input); auto & before_sort_actions_outputs = before_sort_actions->getOutputs(); @@ -388,12 +391,11 @@ SortAnalysisResult analyzeSort(const QueryNode & query_node, * Actions before limit by are added into actions chain. */ LimitByAnalysisResult analyzeLimitBy(const QueryNode & query_node, - const ColumnsWithTypeAndName & join_tree_input_columns, + const ColumnsWithTypeAndName & current_output_columns, const PlannerContextPtr & planner_context, ActionsChain & actions_chain) { - const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & limit_by_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & limit_by_input = current_output_columns; auto before_limit_by_actions = buildActionsDAGFromExpressionNode(query_node.getLimitByNode(), limit_by_input, planner_context); Names limit_by_column_names; @@ -420,35 +422,47 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo std::optional where_analysis_result_optional; std::optional where_action_step_index_optional; + ColumnsWithTypeAndName current_output_columns = join_tree_input_columns; + if (query_node.hasWhere()) { - where_analysis_result_optional = analyzeFilter(query_node.getWhere(), join_tree_input_columns, planner_context, actions_chain); + where_analysis_result_optional = analyzeFilter(query_node.getWhere(), current_output_columns, planner_context, actions_chain); where_action_step_index_optional = actions_chain.getLastStepIndex(); + current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); } - auto aggregation_analysis_result_optional = analyzeAggregation(query_tree, join_tree_input_columns, planner_context, actions_chain); + auto [aggregation_analysis_result_optional, aggregated_columns_optional] = analyzeAggregation(query_tree, current_output_columns, planner_context, actions_chain); + if (aggregated_columns_optional) + current_output_columns = std::move(*aggregated_columns_optional); std::optional having_analysis_result_optional; std::optional having_action_step_index_optional; if (query_node.hasHaving()) { - having_analysis_result_optional = analyzeFilter(query_node.getHaving(), join_tree_input_columns, planner_context, actions_chain); + having_analysis_result_optional = analyzeFilter(query_node.getHaving(), current_output_columns, planner_context, actions_chain); having_action_step_index_optional = actions_chain.getLastStepIndex(); + current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); } - auto window_analysis_result_optional = analyzeWindow(query_tree, join_tree_input_columns, planner_context, actions_chain); + auto window_analysis_result_optional = analyzeWindow(query_tree, current_output_columns, planner_context, actions_chain); std::optional sort_analysis_result_optional; if (query_node.hasOrderBy()) - sort_analysis_result_optional = analyzeSort(query_node, join_tree_input_columns, planner_context, actions_chain); + { + sort_analysis_result_optional = analyzeSort(query_node, current_output_columns, planner_context, actions_chain); + current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); + } std::optional limit_by_analysis_result_optional; if (query_node.hasLimitBy()) - limit_by_analysis_result_optional = analyzeLimitBy(query_node, join_tree_input_columns, planner_context, actions_chain); + { + limit_by_analysis_result_optional = analyzeLimitBy(query_node, current_output_columns, planner_context, actions_chain); + current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); + } - auto projection_analysis_result = analyzeProjection(query_node, join_tree_input_columns, planner_context, actions_chain); + auto projection_analysis_result = analyzeProjection(query_node, current_output_columns, planner_context, actions_chain); const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); const auto & project_names_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; From 7e40ab7658e1ad02acbd8ddf7067f4091a07338e Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Wed, 1 Feb 2023 18:04:19 +0000 Subject: [PATCH 040/342] Update output columns after WindowStep --- src/Planner/PlannerExpressionAnalysis.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index c7d38839c85..79feb501f37 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -446,6 +446,8 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo } auto window_analysis_result_optional = analyzeWindow(query_tree, current_output_columns, planner_context, actions_chain); + if (window_analysis_result_optional) + current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); std::optional sort_analysis_result_optional; if (query_node.hasOrderBy()) From d589864828ed31dcba034947375acf98a6897948 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Wed, 1 Feb 2023 18:12:13 +0000 Subject: [PATCH 041/342] Cleanup code --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 14 -------------- src/Planner/PlannerActionsVisitor.cpp | 3 --- src/Planner/PlannerExpressionAnalysis.cpp | 5 +---- 3 files changed, 1 insertion(+), 21 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 4c1d6d309d6..003944fa8b0 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -447,9 +447,6 @@ public: alias_name_to_expressions[node_alias].push_back(node); } - if (const auto * function = node->as()) - LOG_DEBUG(&Poco::Logger::get("ExpressionsStack"), "Pushed function {} on stack", function->getFunctionName()); - expressions.emplace_back(node); } @@ -468,9 +465,6 @@ public: alias_name_to_expressions.erase(it); } - if (const auto * function = top_expression->as()) - LOG_DEBUG(&Poco::Logger::get("ExpressionsStack"), "Poped function {} on stack", function->getFunctionName()); - expressions.pop_back(); } @@ -500,7 +494,6 @@ public: if (factory.isAggregateFunctionName(function->getFunctionName())) return true; - LOG_DEBUG(&Poco::Logger::get("ExpressionsStack"), "Function {} is being resolved, but is not aggregate", function->getFunctionName()); } return false; } @@ -6071,11 +6064,6 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier for (const auto & group_by_elem : grouping_set->as()->getNodes()) scope.nullable_group_by_keys.insert(group_by_elem->clone()); } - // for (const auto & grouping_set : query_node_typed.getGroupBy().getNodes()) - // { - // for (const auto & group_by_elem : grouping_set->as()->getNodes()) - // group_by_elem->convertToNullable(); - // } } else { @@ -6088,8 +6076,6 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier { for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) scope.nullable_group_by_keys.insert(group_by_elem->clone()); - // for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) - // group_by_elem->convertToNullable(); } } } diff --git a/src/Planner/PlannerActionsVisitor.cpp b/src/Planner/PlannerActionsVisitor.cpp index 13c88d47ca3..6bd245858fe 100644 --- a/src/Planner/PlannerActionsVisitor.cpp +++ b/src/Planner/PlannerActionsVisitor.cpp @@ -29,7 +29,6 @@ #include #include #include -#include namespace DB { @@ -83,7 +82,6 @@ public: node_name, actions_dag->dumpNames()); - LOG_DEBUG(&Poco::Logger::get("ActionsScopeNode"), "Node: {} {}", it->second->result_name, it->second->result_type->getName()); return it->second; } @@ -227,7 +225,6 @@ PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::vi PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::visitColumn(const QueryTreeNodePtr & node) { auto column_node_name = calculateActionNodeName(node, *planner_context, node_to_node_name); - LOG_DEBUG(&Poco::Logger::get("PlannerActionsVisitorImpl"), "Processing column with name: {}", column_node_name); const auto & column_node = node->as(); Int64 actions_stack_size = static_cast(actions_stack.size() - 1); diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index 79feb501f37..99815d89168 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -16,8 +17,6 @@ #include #include #include -#include "Common/tests/gtest_global_context.h" -#include "DataTypes/DataTypeNullable.h" namespace DB { @@ -303,7 +302,6 @@ ProjectionAnalysisResult analyzeProjection(const QueryNode & query_node, ActionsChain & actions_chain) { const auto & projection_input = current_output_columns; - LOG_DEBUG(&Poco::Logger::get("PlannerExpressionAnalysis"), "Projection node: {}", query_node.getProjectionNode()->dumpTree()); auto projection_actions = buildActionsDAGFromExpressionNode(query_node.getProjectionNode(), projection_input, planner_context); auto projection_columns = query_node.getProjectionColumns(); @@ -325,7 +323,6 @@ ProjectionAnalysisResult analyzeProjection(const QueryNode & query_node, for (size_t i = 0; i < projection_outputs_size; ++i) { auto & projection_column = projection_columns[i]; - LOG_DEBUG(&Poco::Logger::get("PlannerExpressionAnalysis"), "Projection column {}: {} {}", i, projection_column.name, projection_column.type->getName()); const auto * projection_node = projection_actions_outputs[i]; const auto & projection_node_name = projection_node->result_name; From 139a684952d42fd3be4289ca2ada8cfedb7a6591 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Fri, 3 Feb 2023 16:00:25 +0000 Subject: [PATCH 042/342] Move analyzeProjection call --- src/Planner/PlannerExpressionAnalysis.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index 99815d89168..d0104b0bf2a 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -419,7 +419,8 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo std::optional where_analysis_result_optional; std::optional where_action_step_index_optional; - ColumnsWithTypeAndName current_output_columns = join_tree_input_columns; + const auto * input_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); + ColumnsWithTypeAndName current_output_columns = input_columns ? *input_columns : join_tree_input_columns; if (query_node.hasWhere()) { @@ -446,6 +447,9 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo if (window_analysis_result_optional) current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); + auto projection_analysis_result = analyzeProjection(query_node, current_output_columns, planner_context, actions_chain); + current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); + std::optional sort_analysis_result_optional; if (query_node.hasOrderBy()) { @@ -461,10 +465,8 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo current_output_columns = actions_chain.getLastStepAvailableOutputColumns(); } - auto projection_analysis_result = analyzeProjection(query_node, current_output_columns, planner_context, actions_chain); - const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); - const auto & project_names_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + const auto & project_names_input = chain_available_output_columns ? *chain_available_output_columns : current_output_columns; auto project_names_actions = std::make_shared(project_names_input); project_names_actions->project(projection_analysis_result.projection_column_names_with_display_aliases); actions_chain.addStep(std::make_unique(project_names_actions)); From a1362b081f5fc4dd8e4fd8762c91800f5e1f0aff Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Fri, 3 Feb 2023 17:14:50 +0000 Subject: [PATCH 043/342] Add test --- ...2535_analyzer_group_by_use_nulls.reference | 256 ++++++++++++++++++ .../02535_analyzer_group_by_use_nulls.sql | 85 ++++++ 2 files changed, 341 insertions(+) create mode 100644 tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.reference create mode 100644 tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.sql diff --git a/tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.reference b/tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.reference new file mode 100644 index 00000000000..50755627996 --- /dev/null +++ b/tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.reference @@ -0,0 +1,256 @@ +-- { echoOn } +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; +0 0 0 +1 1 1 +2 0 2 +3 1 3 +4 0 4 +5 1 5 +6 0 6 +7 1 7 +8 0 8 +9 1 9 +\N \N 45 +set optimize_group_by_function_keys = 0; +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; +0 0 0 +0 \N 0 +1 1 1 +1 \N 1 +2 0 2 +2 \N 2 +3 1 3 +3 \N 3 +4 0 4 +4 \N 4 +5 1 5 +5 \N 5 +6 0 6 +6 \N 6 +7 1 7 +7 \N 7 +8 0 8 +8 \N 8 +9 1 9 +9 \N 9 +\N \N 45 +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=0; +0 0 0 +0 0 0 +0 0 45 +1 0 1 +1 1 1 +2 0 2 +2 0 2 +3 0 3 +3 1 3 +4 0 4 +4 0 4 +5 0 5 +5 1 5 +6 0 6 +6 0 6 +7 0 7 +7 1 7 +8 0 8 +8 0 8 +9 0 9 +9 1 9 +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY CUBE(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; +0 0 0 +0 \N 0 +1 1 1 +1 \N 1 +2 0 2 +2 \N 2 +3 1 3 +3 \N 3 +4 0 4 +4 \N 4 +5 1 5 +5 \N 5 +6 0 6 +6 \N 6 +7 1 7 +7 \N 7 +8 0 8 +8 \N 8 +9 1 9 +9 \N 9 +\N 0 20 +\N 1 25 +\N \N 45 +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY CUBE(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=0; +0 0 0 +0 0 0 +0 0 20 +0 0 45 +0 1 25 +1 0 1 +1 1 1 +2 0 2 +2 0 2 +3 0 3 +3 1 3 +4 0 4 +4 0 4 +5 0 5 +5 1 5 +6 0 6 +6 0 6 +7 0 7 +7 1 7 +8 0 8 +8 0 8 +9 0 9 +9 1 9 +SELECT + number, + number % 2, + sum(number) AS val +FROM numbers(10) +GROUP BY + GROUPING SETS ( + (number), + (number % 2) + ) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls = 1; +0 \N 0 +1 \N 1 +2 \N 2 +3 \N 3 +4 \N 4 +5 \N 5 +6 \N 6 +7 \N 7 +8 \N 8 +9 \N 9 +\N 0 20 +\N 1 25 +SELECT + number, + number % 2, + sum(number) AS val +FROM numbers(10) +GROUP BY + GROUPING SETS ( + (number), + (number % 2) + ) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls = 0; +0 0 0 +0 0 20 +0 1 25 +1 0 1 +2 0 2 +3 0 3 +4 0 4 +5 0 5 +6 0 6 +7 0 7 +8 0 8 +9 0 9 +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) WITH TOTALS +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; +0 0 0 +0 \N 0 +1 1 1 +1 \N 1 +2 0 2 +2 \N 2 +3 1 3 +3 \N 3 +4 0 4 +4 \N 4 +5 1 5 +5 \N 5 +6 0 6 +6 \N 6 +7 1 7 +7 \N 7 +8 0 8 +8 \N 8 +9 1 9 +9 \N 9 +\N \N 45 + +0 0 45 +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY CUBE(number, number % 2) WITH TOTALS +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; +0 0 0 +0 \N 0 +1 1 1 +1 \N 1 +2 0 2 +2 \N 2 +3 1 3 +3 \N 3 +4 0 4 +4 \N 4 +5 1 5 +5 \N 5 +6 0 6 +6 \N 6 +7 1 7 +7 \N 7 +8 0 8 +8 \N 8 +9 1 9 +9 \N 9 +\N 0 20 +\N 1 25 +\N \N 45 + +0 0 45 +SELECT + number, + number % 2, + sum(number) AS val +FROM numbers(10) +GROUP BY + GROUPING SETS ( + (number), + (number % 2) + ) +ORDER BY 1, tuple(val) +SETTINGS group_by_use_nulls = 1, max_bytes_before_external_sort=10; +0 \N 0 +1 \N 1 +2 \N 2 +3 \N 3 +4 \N 4 +5 \N 5 +6 \N 6 +7 \N 7 +8 \N 8 +9 \N 9 +\N 0 20 +\N 1 25 diff --git a/tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.sql b/tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.sql new file mode 100644 index 00000000000..a4d4f2f8bc9 --- /dev/null +++ b/tests/queries/0_stateless/02535_analyzer_group_by_use_nulls.sql @@ -0,0 +1,85 @@ +SET allow_experimental_analyzer=1; + +-- { echoOn } +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; + +set optimize_group_by_function_keys = 0; + +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; + +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=0; + +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY CUBE(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; + +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY CUBE(number, number % 2) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=0; + +SELECT + number, + number % 2, + sum(number) AS val +FROM numbers(10) +GROUP BY + GROUPING SETS ( + (number), + (number % 2) + ) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls = 1; + +SELECT + number, + number % 2, + sum(number) AS val +FROM numbers(10) +GROUP BY + GROUPING SETS ( + (number), + (number % 2) + ) +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls = 0; + +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY ROLLUP(number, number % 2) WITH TOTALS +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; + +SELECT number, number % 2, sum(number) AS val +FROM numbers(10) +GROUP BY CUBE(number, number % 2) WITH TOTALS +ORDER BY (number, number % 2, val) +SETTINGS group_by_use_nulls=1; + +SELECT + number, + number % 2, + sum(number) AS val +FROM numbers(10) +GROUP BY + GROUPING SETS ( + (number), + (number % 2) + ) +ORDER BY 1, tuple(val) +SETTINGS group_by_use_nulls = 1, max_bytes_before_external_sort=10; From 3ea3b3545d8c37e20ce5d5674038807030656590 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Fri, 3 Feb 2023 18:02:50 +0000 Subject: [PATCH 044/342] Add statefull test --- .../1_stateful/00173_group_by_use_nulls.sql | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/queries/1_stateful/00173_group_by_use_nulls.sql b/tests/queries/1_stateful/00173_group_by_use_nulls.sql index 7acacc4e579..f110ebb0db4 100644 --- a/tests/queries/1_stateful/00173_group_by_use_nulls.sql +++ b/tests/queries/1_stateful/00173_group_by_use_nulls.sql @@ -8,3 +8,17 @@ ORDER BY CounterID ASC LIMIT 10 SETTINGS group_by_use_nulls = 1; + +-- { echoOn } +set allow_experimental_analyzer = 1; + +SELECT + CounterID AS k, + quantileBFloat16(0.5)(ResolutionWidth) +FROM remote('127.0.0.{1,2}', test, hits) +GROUP BY k +ORDER BY + count() DESC, + CounterID ASC +LIMIT 10 +SETTINGS group_by_use_nulls = 1; From aca4f08bf5b2aac5fae6cc897cd0be4b9498b65a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 6 Feb 2023 14:33:45 +0000 Subject: [PATCH 045/342] Correctly calculate size after snapshot load --- src/Coordination/CoordinationSettings.cpp | 2 +- src/Coordination/FourLetterCommand.cpp | 9 +++++++ src/Coordination/FourLetterCommand.h | 13 ++++++++++ src/Coordination/KeeperDispatcher.h | 5 ++++ src/Coordination/KeeperServer.cpp | 5 ++++ src/Coordination/KeeperServer.h | 2 ++ src/Coordination/KeeperSnapshotManager.cpp | 28 +++++++++++++--------- src/Coordination/KeeperStateMachine.cpp | 8 +++++++ src/Coordination/KeeperStateMachine.h | 1 + src/Coordination/KeeperStorage.cpp | 20 ++++++++++++++-- src/Coordination/KeeperStorage.h | 5 +++- src/Coordination/SnapshotableHashTable.h | 13 ++++++++++ 12 files changed, 96 insertions(+), 15 deletions(-) diff --git a/src/Coordination/CoordinationSettings.cpp b/src/Coordination/CoordinationSettings.cpp index e665ccb89c7..f5c79d3be7a 100644 --- a/src/Coordination/CoordinationSettings.cpp +++ b/src/Coordination/CoordinationSettings.cpp @@ -36,7 +36,7 @@ void CoordinationSettings::loadFromConfig(const String & config_elem, const Poco } -const String KeeperConfigurationAndSettings::DEFAULT_FOUR_LETTER_WORD_CMD = "conf,cons,crst,envi,ruok,srst,srvr,stat,wchs,dirs,mntr,isro,rcvr,apiv,csnp,lgif,rqld"; +const String KeeperConfigurationAndSettings::DEFAULT_FOUR_LETTER_WORD_CMD = "conf,cons,crst,envi,ruok,srst,srvr,stat,wchs,dirs,mntr,isro,rcvr,apiv,csnp,lgif,rqld,rclc"; KeeperConfigurationAndSettings::KeeperConfigurationAndSettings() : server_id(NOT_EXIST) diff --git a/src/Coordination/FourLetterCommand.cpp b/src/Coordination/FourLetterCommand.cpp index 3e0e5acee0c..6157daad1cd 100644 --- a/src/Coordination/FourLetterCommand.cpp +++ b/src/Coordination/FourLetterCommand.cpp @@ -145,6 +145,9 @@ void FourLetterCommandFactory::registerCommands(KeeperDispatcher & keeper_dispat FourLetterCommandPtr request_leader_command = std::make_shared(keeper_dispatcher); factory.registerCommand(request_leader_command); + FourLetterCommandPtr recalculate_command = std::make_shared(keeper_dispatcher); + factory.registerCommand(recalculate_command); + factory.initializeAllowList(keeper_dispatcher); factory.setInitialize(true); } @@ -515,4 +518,10 @@ String RequestLeaderCommand::run() return keeper_dispatcher.requestLeader() ? "Sent leadership request to leader." : "Failed to send leadership request to leader."; } +String RecalculateCommand::run() +{ + keeper_dispatcher.recalculateStorageStats(); + return "ok"; +} + } diff --git a/src/Coordination/FourLetterCommand.h b/src/Coordination/FourLetterCommand.h index 8a8aacf7a3a..e1fe0333081 100644 --- a/src/Coordination/FourLetterCommand.h +++ b/src/Coordination/FourLetterCommand.h @@ -377,4 +377,17 @@ struct RequestLeaderCommand : public IFourLetterCommand ~RequestLeaderCommand() override = default; }; +/// Request to be leader. +struct RecalculateCommand : public IFourLetterCommand +{ + explicit RecalculateCommand(KeeperDispatcher & keeper_dispatcher_) + : IFourLetterCommand(keeper_dispatcher_) + { + } + + String name() override { return "rclc"; } + String run() override; + ~RecalculateCommand() override = default; +}; + } diff --git a/src/Coordination/KeeperDispatcher.h b/src/Coordination/KeeperDispatcher.h index ff902d8e036..90965d0934e 100644 --- a/src/Coordination/KeeperDispatcher.h +++ b/src/Coordination/KeeperDispatcher.h @@ -225,6 +225,11 @@ public: { return server->requestLeader(); } + + void recalculateStorageStats() + { + return server->recalculateStorageStats(); + } }; } diff --git a/src/Coordination/KeeperServer.cpp b/src/Coordination/KeeperServer.cpp index b823fbc697c..05f97313edd 100644 --- a/src/Coordination/KeeperServer.cpp +++ b/src/Coordination/KeeperServer.cpp @@ -947,4 +947,9 @@ bool KeeperServer::requestLeader() return isLeader() || raft_instance->request_leadership(); } +void KeeperServer::recalculateStorageStats() +{ + state_machine->recalculateStorageStats(); +} + } diff --git a/src/Coordination/KeeperServer.h b/src/Coordination/KeeperServer.h index feadf3bb7ce..bcff81f66f2 100644 --- a/src/Coordination/KeeperServer.h +++ b/src/Coordination/KeeperServer.h @@ -137,6 +137,8 @@ public: KeeperLogInfo getKeeperLogInfo(); bool requestLeader(); + + void recalculateStorageStats(); }; } diff --git a/src/Coordination/KeeperSnapshotManager.cpp b/src/Coordination/KeeperSnapshotManager.cpp index a3f7dbc2c6a..8b80db3e520 100644 --- a/src/Coordination/KeeperSnapshotManager.cpp +++ b/src/Coordination/KeeperSnapshotManager.cpp @@ -361,19 +361,25 @@ void KeeperStorageSnapshot::deserialize(SnapshotDeserializationResult & deserial "If you still want to ignore it, you can set 'keeper_server.ignore_system_path_on_startup' to true", error_msg); } - else if (match_result == EXACT && !is_node_empty(node)) + else if (match_result == EXACT) { - if (keeper_context->ignore_system_path_on_startup || keeper_context->server_state != KeeperContext::Phase::INIT) + if (!is_node_empty(node)) { - LOG_ERROR(&Poco::Logger::get("KeeperSnapshotManager"), "{}. Ignoring it", error_msg); - node = KeeperStorage::Node{}; + if (keeper_context->ignore_system_path_on_startup || keeper_context->server_state != KeeperContext::Phase::INIT) + { + LOG_ERROR(&Poco::Logger::get("KeeperSnapshotManager"), "{}. Ignoring it", error_msg); + node = KeeperStorage::Node{}; + } + else + throw Exception( + ErrorCodes::LOGICAL_ERROR, + "{}. Ignoring it can lead to data loss. " + "If you still want to ignore it, you can set 'keeper_server.ignore_system_path_on_startup' to true", + error_msg); } - else - throw Exception( - ErrorCodes::LOGICAL_ERROR, - "{}. Ignoring it can lead to data loss. " - "If you still want to ignore it, you can set 'keeper_server.ignore_system_path_on_startup' to true", - error_msg); + + // we always ignore the written size for this node + node.recalculateSize(); } storage.container.insertOrReplace(path, node); @@ -390,7 +396,7 @@ void KeeperStorageSnapshot::deserialize(SnapshotDeserializationResult & deserial { auto parent_path = parentPath(itr.key); storage.container.updateValue( - parent_path, [path = itr.key](KeeperStorage::Node & value) { value.addChild(getBaseName(path)); }); + parent_path, [version, path = itr.key](KeeperStorage::Node & value) { value.addChild(getBaseName(path), /*update_size*/ version < SnapshotVersion::V4); }); } } diff --git a/src/Coordination/KeeperStateMachine.cpp b/src/Coordination/KeeperStateMachine.cpp index e591f87c6f1..42ff59f45d9 100644 --- a/src/Coordination/KeeperStateMachine.cpp +++ b/src/Coordination/KeeperStateMachine.cpp @@ -640,4 +640,12 @@ ClusterConfigPtr KeeperStateMachine::getClusterConfig() const return nullptr; } +void KeeperStateMachine::recalculateStorageStats() +{ + std::lock_guard lock(storage_and_responses_lock); + LOG_INFO(log, "Recalculating storage stats"); + storage->recalculateStats(); + LOG_INFO(log, "Done recalculating storage stats"); +} + } diff --git a/src/Coordination/KeeperStateMachine.h b/src/Coordination/KeeperStateMachine.h index ffc7fce1cfe..d8181532f09 100644 --- a/src/Coordination/KeeperStateMachine.h +++ b/src/Coordination/KeeperStateMachine.h @@ -103,6 +103,7 @@ public: uint64_t getKeyArenaSize() const; uint64_t getLatestSnapshotBufSize() const; + void recalculateStorageStats(); private: /// In our state machine we always have a single snapshot which is stored /// in memory in compressed (serialized) format. diff --git a/src/Coordination/KeeperStorage.cpp b/src/Coordination/KeeperStorage.cpp index 0e4d631938c..b1f3b44b1e1 100644 --- a/src/Coordination/KeeperStorage.cpp +++ b/src/Coordination/KeeperStorage.cpp @@ -201,9 +201,10 @@ void KeeperStorage::Node::setData(String new_data) data = std::move(new_data); } -void KeeperStorage::Node::addChild(StringRef child_path) +void KeeperStorage::Node::addChild(StringRef child_path, bool update_size) { - size_bytes += sizeof child_path; + if (update_size) [[likely]] + size_bytes += sizeof child_path; children.insert(child_path); } @@ -234,6 +235,16 @@ void KeeperStorage::Node::shallowCopy(const KeeperStorage::Node & other) cached_digest = other.cached_digest; } +void KeeperStorage::Node::recalculateSize() +{ + size_bytes = sizeof(Node); + + for (const auto child_path : children) + size_bytes += sizeof child_path; + + size_bytes += data.size(); +} + KeeperStorage::KeeperStorage( int64_t tick_time_ms, const String & superdigest_, const KeeperContextPtr & keeper_context_, const bool initialize_system_nodes) : session_expiry_queue(tick_time_ms), keeper_context(keeper_context_), superdigest(superdigest_) @@ -2407,5 +2418,10 @@ uint64_t KeeperStorage::getTotalEphemeralNodesCount() const return ret; } +void KeeperStorage::recalculateStats() +{ + container.recalculateDataSize(); +} + } diff --git a/src/Coordination/KeeperStorage.h b/src/Coordination/KeeperStorage.h index a40cca8e778..be528072df4 100644 --- a/src/Coordination/KeeperStorage.h +++ b/src/Coordination/KeeperStorage.h @@ -47,7 +47,7 @@ public: const auto & getData() const noexcept { return data; } - void addChild(StringRef child_path); + void addChild(StringRef child_path, bool update_size = true); void removeChild(StringRef child_path); @@ -64,6 +64,8 @@ public: // (e.g. we don't need to copy list of children) void shallowCopy(const Node & other); + void recalculateSize(); + private: String data; ChildrenSet children{}; @@ -466,6 +468,7 @@ public: void dumpWatchesByPath(WriteBufferFromOwnString & buf) const; void dumpSessionsAndEphemerals(WriteBufferFromOwnString & buf) const; + void recalculateStats(); private: void removeDigest(const Node & node, std::string_view path); void addDigest(const Node & node, std::string_view path); diff --git a/src/Coordination/SnapshotableHashTable.h b/src/Coordination/SnapshotableHashTable.h index 27572ab86c7..97c7e933334 100644 --- a/src/Coordination/SnapshotableHashTable.h +++ b/src/Coordination/SnapshotableHashTable.h @@ -8,6 +8,8 @@ #include #include +#include + namespace DB { @@ -319,6 +321,17 @@ public: return approximate_data_size; } + void recalculateDataSize() + { + approximate_data_size = 0; + for (auto & node : list) + { + node.value.recalculateSize(); + approximate_data_size += node.key.size; + approximate_data_size += node.value.sizeInBytes(); + } + } + uint64_t keyArenaSize() const { return arena.size(); From 9cde46acff502b8ba8972c9c3ccbc13b904b6563 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 6 Feb 2023 16:09:56 +0000 Subject: [PATCH 046/342] Correctly calculate size on update --- src/Coordination/SnapshotableHashTable.h | 15 +-- .../test_keeper_mntr_data_size/__init__.py | 0 .../configs/enable_keeper.xml | 29 ++++++ .../test_keeper_mntr_data_size/test.py | 93 +++++++++++++++++++ 4 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 tests/integration/test_keeper_mntr_data_size/__init__.py create mode 100644 tests/integration/test_keeper_mntr_data_size/configs/enable_keeper.xml create mode 100644 tests/integration/test_keeper_mntr_data_size/test.py diff --git a/src/Coordination/SnapshotableHashTable.h b/src/Coordination/SnapshotableHashTable.h index 97c7e933334..5b01ca255f8 100644 --- a/src/Coordination/SnapshotableHashTable.h +++ b/src/Coordination/SnapshotableHashTable.h @@ -8,8 +8,6 @@ #include #include -#include - namespace DB { @@ -66,7 +64,7 @@ private: /// value_size: size of value to add /// old_value_size: size of value to minus /// old_value_size=0 means there is no old value with the same key. - void updateDataSize(OperationType op_type, uint64_t key_size, uint64_t value_size, uint64_t old_value_size) + void updateDataSize(OperationType op_type, uint64_t key_size, uint64_t value_size, uint64_t old_value_size, bool remove_old = true) { switch (op_type) { @@ -96,14 +94,14 @@ private: case UPDATE_VALUE: approximate_data_size += key_size; approximate_data_size += value_size; - if (!snapshot_mode) + if (remove_old) { approximate_data_size -= key_size; approximate_data_size -= old_value_size; } break; case ERASE: - if (!snapshot_mode) + if (remove_old) { approximate_data_size -= key_size; approximate_data_size -= old_value_size; @@ -179,7 +177,7 @@ public: list_itr->value = value; } } - updateDataSize(INSERT_OR_REPLACE, key.size(), value.sizeInBytes(), old_value_size); + updateDataSize(INSERT_OR_REPLACE, key.size(), value.sizeInBytes(), old_value_size, !snapshot_mode); } bool erase(const std::string & key) @@ -224,6 +222,7 @@ public: const_iterator ret; + bool remove_old_size = true; if (snapshot_mode) { /// We in snapshot mode but updating some node which is already more @@ -239,6 +238,8 @@ public: auto itr = list.insert(list.end(), std::move(elem_copy)); it->getMapped() = itr; ret = itr; + + remove_old_size = false; } else { @@ -252,7 +253,7 @@ public: ret = list_itr; } - updateDataSize(UPDATE_VALUE, key.size, ret->value.sizeInBytes(), old_value_size); + updateDataSize(UPDATE_VALUE, key.size, ret->value.sizeInBytes(), old_value_size, remove_old_size); return ret; } diff --git a/tests/integration/test_keeper_mntr_data_size/__init__.py b/tests/integration/test_keeper_mntr_data_size/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/integration/test_keeper_mntr_data_size/configs/enable_keeper.xml b/tests/integration/test_keeper_mntr_data_size/configs/enable_keeper.xml new file mode 100644 index 00000000000..a3217b34501 --- /dev/null +++ b/tests/integration/test_keeper_mntr_data_size/configs/enable_keeper.xml @@ -0,0 +1,29 @@ + + + 9181 + 1 + /var/lib/clickhouse/coordination/log + /var/lib/clickhouse/coordination/snapshots + + + 10 + 5 + 5000 + 10000 + trace + + + 0 + 0 + 0 + + + + + 1 + localhost + 9234 + + + + diff --git a/tests/integration/test_keeper_mntr_data_size/test.py b/tests/integration/test_keeper_mntr_data_size/test.py new file mode 100644 index 00000000000..8789ca0354c --- /dev/null +++ b/tests/integration/test_keeper_mntr_data_size/test.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +import pytest +from helpers.cluster import ClickHouseCluster +import helpers.keeper_utils as keeper_utils +import random +import string +import os +import time +from kazoo.client import KazooClient, KazooState + + +cluster = ClickHouseCluster(__file__) + +# clickhouse itself will use external zookeeper +node = cluster.add_instance( + "node", + main_configs=["configs/enable_keeper.xml"], + stay_alive=True, + with_zookeeper=True, +) + + +def random_string(length): + return "".join(random.choices(string.ascii_lowercase + string.digits, k=length)) + + +@pytest.fixture(scope="module") +def started_cluster(): + try: + cluster.start() + + yield cluster + + finally: + cluster.shutdown() + + +def get_connection_zk(nodename, timeout=30.0): + _fake_zk_instance = KazooClient( + hosts=cluster.get_instance_ip(nodename) + ":9181", timeout=timeout + ) + _fake_zk_instance.start() + return _fake_zk_instance + + +def restart_clickhouse(): + node.restart_clickhouse() + keeper_utils.wait_until_connected(cluster, node) + + +def test_mntr_data_size_after_restart(started_cluster): + try: + node_zk = None + node_zk = get_connection_zk("node") + + node_zk.create("/test_mntr_data_size", b"somevalue") + for i in range(100): + node_zk.create( + "/test_mntr_data_size/node" + str(i), random_string(123).encode() + ) + + def get_line_with_size(): + return next( + filter( + lambda line: "zk_approximate_data_size" in line, + keeper_utils.send_4lw_cmd(started_cluster, node, "mntr").split( + "\n" + ), + ), + None, + ) + + line_size_before = get_line_with_size() + assert line_size_before != None + + node_zk.stop() + node_zk.close() + node_zk = None + + restart_clickhouse() + + assert get_line_with_size() == line_size_before + + keeper_utils.send_4lw_cmd(started_cluster, node, "rclc") + assert get_line_with_size() == line_size_before + finally: + try: + if node_zk is not None: + node_zk.stop() + node_zk.close() + except: + pass From b11458f14241f81f22129a1476a9220010c2586e Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 7 Feb 2023 07:51:22 +0000 Subject: [PATCH 047/342] Fix erase --- src/Coordination/SnapshotableHashTable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Coordination/SnapshotableHashTable.h b/src/Coordination/SnapshotableHashTable.h index 5b01ca255f8..cfa3098b4a1 100644 --- a/src/Coordination/SnapshotableHashTable.h +++ b/src/Coordination/SnapshotableHashTable.h @@ -202,7 +202,7 @@ public: list.erase(list_itr); } - updateDataSize(ERASE, key.size(), 0, old_data_size); + updateDataSize(ERASE, key.size(), 0, old_data_size, !snapshot_mode); return true; } From fe48ab2b1d9006cdb8e0d937ad8d9a9370020c1c Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Tue, 7 Feb 2023 14:58:34 +0000 Subject: [PATCH 048/342] randomize setting 'ratio_of_defaults_for_sparse_serialization' --- tests/clickhouse-test | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/tests/clickhouse-test b/tests/clickhouse-test index 4361b64b62f..e84c9beed5d 100755 --- a/tests/clickhouse-test +++ b/tests/clickhouse-test @@ -511,7 +511,9 @@ class SettingsRandomizer: "merge_tree_coarse_index_granularity": lambda: random.randint(2, 32), "optimize_distinct_in_order": lambda: random.randint(0, 1), "optimize_sorting_by_input_stream_properties": lambda: random.randint(0, 1), - "enable_memory_bound_merging_of_aggregation_results": lambda: random.randint(0, 1), + "enable_memory_bound_merging_of_aggregation_results": lambda: random.randint( + 0, 1 + ), } @staticmethod @@ -524,10 +526,9 @@ class SettingsRandomizer: class MergeTreeSettingsRandomizer: settings = { - # Temporary disable due to large number of failures. TODO: fix. - # "ratio_of_defaults_for_sparse_serialization": threshold_generator( - # 0.1, 0.6, 0.0, 1.0 - # ), + "ratio_of_defaults_for_sparse_serialization": threshold_generator( + 0.3, 0.5, 0.0, 1.0 + ), "prefer_fetch_merged_part_size_threshold": threshold_generator( 0.2, 0.5, 1, 10 * 1024 * 1024 * 1024 ), @@ -1222,7 +1223,7 @@ class TestCase: ) result.check_if_need_retry(args, stdout, stderr, self.runs_count) # to avoid breaking CSV parser - result.description = result.description.replace('\0', '') + result.description = result.description.replace("\0", "") if result.status == TestStatus.FAIL: result.description = self.add_info_about_settings(result.description) @@ -1678,7 +1679,12 @@ def check_server_started(args): print("\nConnection timeout, will not retry") break except Exception as e: - print("\nUexpected exception, will not retry: ", type(e).__name__, ": ", str(e)) + print( + "\nUexpected exception, will not retry: ", + type(e).__name__, + ": ", + str(e), + ) break print("\nAll connection tries failed") @@ -1955,6 +1961,7 @@ def reportCoverage(args): ) ) + def reportLogStats(args): query = """ WITH From 94407682b4c360b47967a0b02d8ca9eb83767317 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Thu, 9 Feb 2023 18:06:15 +0000 Subject: [PATCH 049/342] fix function with sparse columns arguments --- src/Columns/ColumnAggregateFunction.h | 5 +++++ src/Columns/ColumnArray.cpp | 5 +++++ src/Columns/ColumnArray.h | 1 + src/Columns/ColumnCompressed.h | 1 + src/Columns/ColumnConst.h | 5 +++++ src/Columns/ColumnDecimal.h | 5 +++++ src/Columns/ColumnFixedString.h | 5 +++++ src/Columns/ColumnFunction.h | 5 +++++ src/Columns/ColumnLowCardinality.h | 5 +++++ src/Columns/ColumnMap.cpp | 5 +++++ src/Columns/ColumnMap.h | 1 + src/Columns/ColumnNullable.h | 5 +++++ src/Columns/ColumnObject.h | 1 + src/Columns/ColumnSparse.cpp | 13 +++++++++---- src/Columns/ColumnSparse.h | 2 +- src/Columns/ColumnString.h | 5 +++++ src/Columns/ColumnTuple.cpp | 5 +++++ src/Columns/ColumnTuple.h | 1 + src/Columns/ColumnUnique.h | 5 +++++ src/Columns/ColumnVector.h | 5 +++++ src/Columns/IColumn.h | 8 +++++++- src/Columns/IColumnDummy.h | 5 +++++ src/Columns/IColumnImpl.h | 10 ++++++++++ src/Functions/IFunction.cpp | 6 ++++-- 24 files changed, 106 insertions(+), 8 deletions(-) diff --git a/src/Columns/ColumnAggregateFunction.h b/src/Columns/ColumnAggregateFunction.h index 38040d65d3b..ae486d5d24e 100644 --- a/src/Columns/ColumnAggregateFunction.h +++ b/src/Columns/ColumnAggregateFunction.h @@ -223,6 +223,11 @@ public: throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getRatioOfDefaultRows is not supported for ColumnAggregateFunction"); } + UInt64 getNumberOfDefaultRows() const override + { + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getNumberOfDefaultRows is not supported for ColumnAggregateFunction"); + } + void getIndicesOfNonDefaultRows(Offsets &, size_t, size_t) const override { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getIndicesOfNonDefaultRows is not supported for ColumnAggregateFunction"); diff --git a/src/Columns/ColumnArray.cpp b/src/Columns/ColumnArray.cpp index 0d085a3275a..cbeebe52141 100644 --- a/src/Columns/ColumnArray.cpp +++ b/src/Columns/ColumnArray.cpp @@ -953,6 +953,11 @@ double ColumnArray::getRatioOfDefaultRows(double sample_ratio) const return getRatioOfDefaultRowsImpl(sample_ratio); } +UInt64 ColumnArray::getNumberOfDefaultRows() const +{ + return getNumberOfDefaultRowsImpl(); +} + void ColumnArray::getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const { return getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnArray.h b/src/Columns/ColumnArray.h index 44652fd0c4b..e60c388831d 100644 --- a/src/Columns/ColumnArray.h +++ b/src/Columns/ColumnArray.h @@ -173,6 +173,7 @@ public: } double getRatioOfDefaultRows(double sample_ratio) const override; + UInt64 getNumberOfDefaultRows() const override; void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override; diff --git a/src/Columns/ColumnCompressed.h b/src/Columns/ColumnCompressed.h index b258dbac878..d747f8ef5f4 100644 --- a/src/Columns/ColumnCompressed.h +++ b/src/Columns/ColumnCompressed.h @@ -117,6 +117,7 @@ public: void getExtremes(Field &, Field &) const override { throwMustBeDecompressed(); } size_t byteSizeAt(size_t) const override { throwMustBeDecompressed(); } double getRatioOfDefaultRows(double) const override { throwMustBeDecompressed(); } + UInt64 getNumberOfDefaultRows() const override { throwMustBeDecompressed(); } void getIndicesOfNonDefaultRows(Offsets &, size_t, size_t) const override { throwMustBeDecompressed(); } protected: diff --git a/src/Columns/ColumnConst.h b/src/Columns/ColumnConst.h index b86ed393e44..fcdcd2ce224 100644 --- a/src/Columns/ColumnConst.h +++ b/src/Columns/ColumnConst.h @@ -253,6 +253,11 @@ public: return data->isDefaultAt(0) ? 1.0 : 0.0; } + UInt64 getNumberOfDefaultRows() const override + { + return data->isDefaultAt(0) ? s : 0; + } + void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override { if (!data->isDefaultAt(0)) diff --git a/src/Columns/ColumnDecimal.h b/src/Columns/ColumnDecimal.h index d47164a178d..03e0b9be558 100644 --- a/src/Columns/ColumnDecimal.h +++ b/src/Columns/ColumnDecimal.h @@ -136,6 +136,11 @@ public: return this->template getRatioOfDefaultRowsImpl(sample_ratio); } + UInt64 getNumberOfDefaultRows() const override + { + return this->template getNumberOfDefaultRowsImpl(); + } + void getIndicesOfNonDefaultRows(IColumn::Offsets & indices, size_t from, size_t limit) const override { return this->template getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnFixedString.h b/src/Columns/ColumnFixedString.h index 7c2d9b1a155..39497e3403e 100644 --- a/src/Columns/ColumnFixedString.h +++ b/src/Columns/ColumnFixedString.h @@ -200,6 +200,11 @@ public: return getRatioOfDefaultRowsImpl(sample_ratio); } + UInt64 getNumberOfDefaultRows() const override + { + return getNumberOfDefaultRowsImpl(); + } + void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override { return getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnFunction.h b/src/Columns/ColumnFunction.h index 7b7ceb29a10..a1f6245c2bd 100644 --- a/src/Columns/ColumnFunction.h +++ b/src/Columns/ColumnFunction.h @@ -168,6 +168,11 @@ public: throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getRatioOfDefaultRows is not supported for {}", getName()); } + UInt64 getNumberOfDefaultRows() const override + { + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getNumberOfDefaultRows is not supported for {}", getName()); + } + void getIndicesOfNonDefaultRows(Offsets &, size_t, size_t) const override { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getIndicesOfNonDefaultRows is not supported for {}", getName()); diff --git a/src/Columns/ColumnLowCardinality.h b/src/Columns/ColumnLowCardinality.h index e895bc6b54e..1f2dbdc202e 100644 --- a/src/Columns/ColumnLowCardinality.h +++ b/src/Columns/ColumnLowCardinality.h @@ -199,6 +199,11 @@ public: return getIndexes().getRatioOfDefaultRows(sample_ratio); } + UInt64 getNumberOfDefaultRows() const override + { + return getIndexes().getNumberOfDefaultRows(); + } + void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override { return getIndexes().getIndicesOfNonDefaultRows(indices, from, limit); diff --git a/src/Columns/ColumnMap.cpp b/src/Columns/ColumnMap.cpp index 2b5f5f94ee8..b533b68b98d 100644 --- a/src/Columns/ColumnMap.cpp +++ b/src/Columns/ColumnMap.cpp @@ -296,6 +296,11 @@ double ColumnMap::getRatioOfDefaultRows(double sample_ratio) const return getRatioOfDefaultRowsImpl(sample_ratio); } +UInt64 ColumnMap::getNumberOfDefaultRows() const +{ + return getNumberOfDefaultRowsImpl(); +} + void ColumnMap::getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const { return getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnMap.h b/src/Columns/ColumnMap.h index db918c3db50..a11905fcaa0 100644 --- a/src/Columns/ColumnMap.h +++ b/src/Columns/ColumnMap.h @@ -92,6 +92,7 @@ public: void forEachSubcolumnRecursively(RecursiveColumnCallback callback) const override; bool structureEquals(const IColumn & rhs) const override; double getRatioOfDefaultRows(double sample_ratio) const override; + UInt64 getNumberOfDefaultRows() const override; void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override; void finalize() override { nested->finalize(); } bool isFinalized() const override { return nested->isFinalized(); } diff --git a/src/Columns/ColumnNullable.h b/src/Columns/ColumnNullable.h index 85bf095a9d1..df0088debc9 100644 --- a/src/Columns/ColumnNullable.h +++ b/src/Columns/ColumnNullable.h @@ -156,6 +156,11 @@ public: return getRatioOfDefaultRowsImpl(sample_ratio); } + UInt64 getNumberOfDefaultRows() const override + { + return getNumberOfDefaultRowsImpl(); + } + void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override { getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnObject.h b/src/Columns/ColumnObject.h index d09974fcc46..91920908542 100644 --- a/src/Columns/ColumnObject.h +++ b/src/Columns/ColumnObject.h @@ -254,6 +254,7 @@ public: bool hasEqualValues() const override { throwMustBeConcrete(); } size_t byteSizeAt(size_t) const override { throwMustBeConcrete(); } double getRatioOfDefaultRows(double) const override { throwMustBeConcrete(); } + UInt64 getNumberOfDefaultRows() const override { throwMustBeConcrete(); } void getIndicesOfNonDefaultRows(Offsets &, size_t, size_t) const override { throwMustBeConcrete(); } private: diff --git a/src/Columns/ColumnSparse.cpp b/src/Columns/ColumnSparse.cpp index 43802b6bbc8..9bc78ef115d 100644 --- a/src/Columns/ColumnSparse.cpp +++ b/src/Columns/ColumnSparse.cpp @@ -470,7 +470,7 @@ int ColumnSparse::compareAtWithCollation(size_t n, size_t m, const IColumn & rhs bool ColumnSparse::hasEqualValues() const { - size_t num_defaults = getNumberOfDefaults(); + size_t num_defaults = getNumberOfDefaultRows(); if (num_defaults == _size) return true; @@ -512,7 +512,7 @@ void ColumnSparse::getPermutationImpl(IColumn::PermutationSortDirection directio else values->getPermutation(direction, stability, limit + 1, null_direction_hint, perm); - size_t num_of_defaults = getNumberOfDefaults(); + size_t num_of_defaults = getNumberOfDefaultRows(); size_t row = 0; const auto & offsets_data = getOffsetsData(); @@ -677,7 +677,7 @@ void ColumnSparse::getExtremes(Field & min, Field & max) const return; } - if (getNumberOfDefaults() == 0) + if (getNumberOfDefaultRows() == 0) { size_t min_idx = 1; size_t max_idx = 1; @@ -709,7 +709,12 @@ void ColumnSparse::getIndicesOfNonDefaultRows(IColumn::Offsets & indices, size_t double ColumnSparse::getRatioOfDefaultRows(double) const { - return static_cast(getNumberOfDefaults()) / _size; + return static_cast(getNumberOfDefaultRows()) / _size; +} + +UInt64 ColumnSparse::getNumberOfDefaultRows() const +{ + return _size - offsets->size(); } MutableColumns ColumnSparse::scatter(ColumnIndex num_columns, const Selector & selector) const diff --git a/src/Columns/ColumnSparse.h b/src/Columns/ColumnSparse.h index ffe392a83c1..d4eb13bf208 100644 --- a/src/Columns/ColumnSparse.h +++ b/src/Columns/ColumnSparse.h @@ -132,6 +132,7 @@ public: void getIndicesOfNonDefaultRows(IColumn::Offsets & indices, size_t from, size_t limit) const override; double getRatioOfDefaultRows(double sample_ratio) const override; + UInt64 getNumberOfDefaultRows() const override; MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override; @@ -150,7 +151,6 @@ public: size_t sizeOfValueIfFixed() const override { return values->sizeOfValueIfFixed() + values->sizeOfValueIfFixed(); } bool isCollationSupported() const override { return values->isCollationSupported(); } - size_t getNumberOfDefaults() const { return _size - offsets->size(); } size_t getNumberOfTrailingDefaults() const { return offsets->empty() ? _size : _size - getOffsetsData().back() - 1; diff --git a/src/Columns/ColumnString.h b/src/Columns/ColumnString.h index aa251b1fda0..c9eed212616 100644 --- a/src/Columns/ColumnString.h +++ b/src/Columns/ColumnString.h @@ -273,6 +273,11 @@ public: return getRatioOfDefaultRowsImpl(sample_ratio); } + UInt64 getNumberOfDefaultRows() const override + { + return getNumberOfDefaultRowsImpl(); + } + void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override { return getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnTuple.cpp b/src/Columns/ColumnTuple.cpp index 903540c1859..369842c7281 100644 --- a/src/Columns/ColumnTuple.cpp +++ b/src/Columns/ColumnTuple.cpp @@ -565,6 +565,11 @@ double ColumnTuple::getRatioOfDefaultRows(double sample_ratio) const return getRatioOfDefaultRowsImpl(sample_ratio); } +UInt64 ColumnTuple::getNumberOfDefaultRows() const +{ + return getNumberOfDefaultRowsImpl(); +} + void ColumnTuple::getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const { return getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/ColumnTuple.h b/src/Columns/ColumnTuple.h index 25f6328b3fc..281fd94d893 100644 --- a/src/Columns/ColumnTuple.h +++ b/src/Columns/ColumnTuple.h @@ -102,6 +102,7 @@ public: bool isCollationSupported() const override; ColumnPtr compress() const override; double getRatioOfDefaultRows(double sample_ratio) const override; + UInt64 getNumberOfDefaultRows() const override; void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const override; void finalize() override; bool isFinalized() const override; diff --git a/src/Columns/ColumnUnique.h b/src/Columns/ColumnUnique.h index 8a95726d2be..377255d80c7 100644 --- a/src/Columns/ColumnUnique.h +++ b/src/Columns/ColumnUnique.h @@ -146,6 +146,11 @@ public: throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method 'getRatioOfDefaultRows' not implemented for ColumnUnique"); } + UInt64 getNumberOfDefaultRows() const override + { + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method 'getNumberOfDefaultRows' not implemented for ColumnUnique"); + } + void getIndicesOfNonDefaultRows(IColumn::Offsets &, size_t, size_t) const override { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method 'getIndicesOfNonDefaultRows' not implemented for ColumnUnique"); diff --git a/src/Columns/ColumnVector.h b/src/Columns/ColumnVector.h index ded66430160..bf790423d1d 100644 --- a/src/Columns/ColumnVector.h +++ b/src/Columns/ColumnVector.h @@ -358,6 +358,11 @@ public: return this->template getRatioOfDefaultRowsImpl(sample_ratio); } + UInt64 getNumberOfDefaultRows() const override + { + return this->template getNumberOfDefaultRowsImpl(); + } + void getIndicesOfNonDefaultRows(IColumn::Offsets & indices, size_t from, size_t limit) const override { return this->template getIndicesOfNonDefaultRowsImpl(indices, from, limit); diff --git a/src/Columns/IColumn.h b/src/Columns/IColumn.h index 53619c73e5b..d777fd7b240 100644 --- a/src/Columns/IColumn.h +++ b/src/Columns/IColumn.h @@ -441,10 +441,13 @@ public: throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method structureEquals is not supported for {}", getName()); } - /// Returns ration of values in column, that equal to default value of column. + /// Returns ratio of values in column, that are equal to default value of column. /// Checks only @sample_ratio ratio of rows. [[nodiscard]] virtual double getRatioOfDefaultRows(double sample_ratio = 1.0) const = 0; /// NOLINT + /// Returns number of values in column, that are equal to default value of column. + [[nodiscard]] virtual UInt64 getNumberOfDefaultRows() const = 0; + /// Returns indices of values in column, that not equal to default value of column. virtual void getIndicesOfNonDefaultRows(Offsets & indices, size_t from, size_t limit) const = 0; @@ -584,6 +587,9 @@ protected: template double getRatioOfDefaultRowsImpl(double sample_ratio) const; + template + UInt64 getNumberOfDefaultRowsImpl() const; + template void getIndicesOfNonDefaultRowsImpl(Offsets & indices, size_t from, size_t limit) const; diff --git a/src/Columns/IColumnDummy.h b/src/Columns/IColumnDummy.h index 0b00ebbdd1f..82d4c857b29 100644 --- a/src/Columns/IColumnDummy.h +++ b/src/Columns/IColumnDummy.h @@ -168,6 +168,11 @@ public: throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getRatioOfDefaultRows is not supported for {}", getName()); } + UInt64 getNumberOfDefaultRows() const override + { + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getNumberOfDefaultRows is not supported for {}", getName()); + } + void getIndicesOfNonDefaultRows(Offsets &, size_t, size_t) const override { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getIndicesOfNonDefaultRows is not supported for {}", getName()); diff --git a/src/Columns/IColumnImpl.h b/src/Columns/IColumnImpl.h index 8537a4c097c..e676373e211 100644 --- a/src/Columns/IColumnImpl.h +++ b/src/Columns/IColumnImpl.h @@ -194,6 +194,16 @@ double IColumn::getRatioOfDefaultRowsImpl(double sample_ratio) const return static_cast(res) / num_checked_rows; } +template +UInt64 IColumn::getNumberOfDefaultRowsImpl() const +{ + UInt64 res = 0; + size_t num_rows = size(); + for (size_t i = 0; i < num_rows; ++i) + res += static_cast(*this).isDefaultAt(i); + return res; +} + template void IColumn::getIndicesOfNonDefaultRowsImpl(Offsets & indices, size_t from, size_t limit) const { diff --git a/src/Functions/IFunction.cpp b/src/Functions/IFunction.cpp index 1c30dee0482..2163fa27c51 100644 --- a/src/Functions/IFunction.cpp +++ b/src/Functions/IFunction.cpp @@ -321,7 +321,7 @@ ColumnPtr IExecutableFunction::execute(const ColumnsWithTypeAndName & arguments, const auto * column_sparse = checkAndGetColumn(arguments[i].column.get()); /// In rare case, when sparse column doesn't have default values, /// it's more convenient to convert it to full before execution of function. - if (column_sparse && column_sparse->getNumberOfDefaults()) + if (column_sparse && column_sparse->getNumberOfDefaultRows()) { sparse_column_position = i; ++num_sparse_columns; @@ -359,7 +359,9 @@ ColumnPtr IExecutableFunction::execute(const ColumnsWithTypeAndName & arguments, return res->cloneResized(input_rows_count); /// If default of sparse column is changed after execution of function, convert to full column. - if (!result_type->supportsSparseSerialization() || !res->isDefaultAt(0)) + /// If there are any default in non-zero position after execution of function, convert to full column. + /// Currently there is no easy way to rebuild sparse column with new offsets. + if (!result_type->supportsSparseSerialization() || !res->isDefaultAt(0) || res->getNumberOfDefaultRows() != 1) { const auto & offsets_data = assert_cast &>(*sparse_offsets).getData(); return res->createWithOffsets(offsets_data, (*res)[0], input_rows_count, /*shift=*/ 1); From 1fce8e4eedf14f3fa8c1b9bc8ff90ef7a52a311f Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Mon, 13 Feb 2023 18:35:34 +0000 Subject: [PATCH 050/342] Fix alias resolve with group_by_use_nulls --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 33 +++++++++++++++-------- src/Planner/PlannerExpressionAnalysis.cpp | 3 ++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 003944fa8b0..2e7d0b2c6db 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -1,6 +1,5 @@ #include -#include "Common/logger_useful.h" #include #include @@ -194,7 +193,6 @@ namespace ErrorCodes * TODO: SELECT (compound_expression).*, (compound_expression).COLUMNS are not supported on parser level. * TODO: SELECT a.b.c.*, a.b.c.COLUMNS. Qualified matcher where identifier size is greater than 2 are not supported on parser level. * TODO: Support function identifier resolve from parent query scope, if lambda in parent scope does not capture any columns. - * TODO: Support group_by_use_nulls. * TODO: Scalar subqueries cache. */ @@ -676,7 +674,11 @@ struct IdentifierResolveScope if (auto * union_node = scope_node->as()) context = union_node->getContext(); else if (auto * query_node = scope_node->as()) + { context = query_node->getContext(); + group_by_use_nulls = context->getSettingsRef().group_by_use_nulls && + (query_node->isGroupByWithGroupingSets() || query_node->isGroupByWithRollup() || query_node->isGroupByWithCube()); + } } QueryTreeNodePtr scope_node; @@ -729,6 +731,9 @@ struct IdentifierResolveScope /// Use identifier lookup to result cache bool use_identifier_lookup_to_result_cache = true; + /// Apply nullability to aggregation keys + bool group_by_use_nulls = false; + /// Subquery depth size_t subquery_depth = 0; @@ -3122,7 +3127,7 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook if (!resolve_result.resolved_identifier || scope.non_cached_identifier_lookups_during_expression_resolve.contains(identifier_lookup) || !scope.use_identifier_lookup_to_result_cache || - scope.context->getSettingsRef().group_by_use_nulls) + scope.group_by_use_nulls) scope.identifier_lookup_to_result.erase(it); return resolve_result; @@ -4650,7 +4655,7 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } function_node.resolveAsFunction(std::move(function_base)); - if (settings.group_by_use_nulls && scope.nullable_group_by_keys.contains(node)) + if (scope.group_by_use_nulls && scope.nullable_group_by_keys.contains(node)) function_node.convertToNullable(); } catch (Exception & e) @@ -4795,8 +4800,8 @@ ProjectionNames QueryAnalyzer::resolveExpressionNode(QueryTreeNodePtr & node, Id node = tryResolveIdentifier({unresolved_identifier, IdentifierLookupContext::TABLE_EXPRESSION}, scope).resolved_identifier; /// If table identifier is resolved as CTE clone it and resolve - auto * subquery_node = node->as(); - auto * union_node = node->as(); + auto * subquery_node = node ? node->as() : nullptr; + auto * union_node = node ? node->as() : nullptr; bool resolved_as_cte = (subquery_node && subquery_node->isCTE()) || (union_node && union_node->isCTE()); if (resolved_as_cte) @@ -4900,6 +4905,12 @@ ProjectionNames QueryAnalyzer::resolveExpressionNode(QueryTreeNodePtr & node, Id if (result_projection_names.empty()) result_projection_names.push_back(column_node.getColumnName()); + if (scope.group_by_use_nulls && scope.nullable_group_by_keys.contains(node)) + { + node = node->clone(); + node->convertToNullable(); + } + break; } case QueryTreeNodeType::FUNCTION: @@ -4986,7 +4997,7 @@ ProjectionNames QueryAnalyzer::resolveExpressionNode(QueryTreeNodePtr & node, Id /** Update aliases after expression node was resolved. * Do not update node in alias table if we resolve it for duplicate alias. */ - if (!node_alias.empty() && use_alias_table) + if (!node_alias.empty() && use_alias_table && !scope.group_by_use_nulls) { auto it = scope.alias_name_to_expression_node.find(node_alias); if (it != scope.alias_name_to_expression_node.end()) @@ -6033,7 +6044,7 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier resolveQueryJoinTreeNode(query_node_typed.getJoinTree(), scope, visitor); } - if (!settings.group_by_use_nulls) + if (!scope.group_by_use_nulls) scope.use_identifier_lookup_to_result_cache = true; /// Resolve query node sections. @@ -6072,7 +6083,7 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier resolveExpressionNodeList(query_node_typed.getGroupByNode(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); - if (settings.group_by_use_nulls) + if (scope.group_by_use_nulls) { for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) scope.nullable_group_by_keys.insert(group_by_elem->clone()); @@ -6270,7 +6281,7 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier continue; group_by_keys_nodes.push_back(grouping_set_key->clone()); - if (settings.group_by_use_nulls) + if (scope.group_by_use_nulls) group_by_keys_nodes.back()->convertToNullable(); } } @@ -6280,7 +6291,7 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier continue; group_by_keys_nodes.push_back(node->clone()); - if (settings.group_by_use_nulls) + if (scope.group_by_use_nulls) group_by_keys_nodes.back()->convertToNullable(); } } diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index d0104b0bf2a..55a5da247f9 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -80,7 +80,8 @@ std::pair, std::optionalgetQueryContext()->getSettingsRef().group_by_use_nulls; + bool group_by_use_nulls = planner_context->getQueryContext()->getSettingsRef().group_by_use_nulls && + (query_node.isGroupByWithGroupingSets() || query_node.isGroupByWithRollup() || query_node.isGroupByWithCube()); if (query_node.hasGroupBy()) { From 8a3be450c391e020cbb4497b234243e8dbf5d47f Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Mon, 13 Feb 2023 18:38:07 +0000 Subject: [PATCH 051/342] Add test query --- tests/queries/1_stateful/00173_group_by_use_nulls.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/queries/1_stateful/00173_group_by_use_nulls.sql b/tests/queries/1_stateful/00173_group_by_use_nulls.sql index f110ebb0db4..8531e9efaf8 100644 --- a/tests/queries/1_stateful/00173_group_by_use_nulls.sql +++ b/tests/queries/1_stateful/00173_group_by_use_nulls.sql @@ -9,6 +9,17 @@ ORDER BY LIMIT 10 SETTINGS group_by_use_nulls = 1; +SELECT + CounterID AS k, + quantileBFloat16(0.5)(ResolutionWidth) +FROM test.hits +GROUP BY k +ORDER BY + count() DESC, + CounterID ASC +LIMIT 10 +SETTINGS group_by_use_nulls = 1 FORMAT Null; + -- { echoOn } set allow_experimental_analyzer = 1; From e460aa6272578f53f85a3cb3e108cf0b411abc7a Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Wed, 15 Feb 2023 16:00:51 +0000 Subject: [PATCH 052/342] Fix warning --- src/Planner/PlannerActionsVisitor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Planner/PlannerActionsVisitor.cpp b/src/Planner/PlannerActionsVisitor.cpp index 6bd245858fe..c9bae0b2c2a 100644 --- a/src/Planner/PlannerActionsVisitor.cpp +++ b/src/Planner/PlannerActionsVisitor.cpp @@ -327,7 +327,7 @@ PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::vi actions_stack.pop_back(); // TODO: Pass IFunctionBase here not FunctionCaptureOverloadResolver. - actions_stack[level].addFunctionIfNecessary(lambda_node_name, std::move(lambda_children), std::move(function_capture)); + actions_stack[level].addFunctionIfNecessary(lambda_node_name, std::move(lambda_children), function_capture); size_t actions_stack_size = actions_stack.size(); for (size_t i = level + 1; i < actions_stack_size; ++i) From 3730ea388f4e9de81dce547f309f24801d724d2b Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Wed, 15 Feb 2023 19:37:54 +0000 Subject: [PATCH 053/342] fix issues with sparse columns --- src/Client/ClientBase.cpp | 2 +- src/Columns/ColumnAggregateFunction.h | 4 ++-- src/Columns/IColumnImpl.h | 11 +++++------ src/Interpreters/Aggregator.cpp | 4 ++++ .../0_stateless/02432_s3_parallel_parts_cleanup.sql | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Client/ClientBase.cpp b/src/Client/ClientBase.cpp index bc8c43af8c6..6c6d22bc037 100644 --- a/src/Client/ClientBase.cpp +++ b/src/Client/ClientBase.cpp @@ -488,7 +488,7 @@ void ClientBase::onTotals(Block & block, ASTPtr parsed_query) void ClientBase::onExtremes(Block & block, ASTPtr parsed_query) { initOutputFormat(block, parsed_query); - output_format->setExtremes(block); + output_format->setExtremes(materializeBlock(block)); } diff --git a/src/Columns/ColumnAggregateFunction.h b/src/Columns/ColumnAggregateFunction.h index ae486d5d24e..f9ce45708c9 100644 --- a/src/Columns/ColumnAggregateFunction.h +++ b/src/Columns/ColumnAggregateFunction.h @@ -220,12 +220,12 @@ public: double getRatioOfDefaultRows(double) const override { - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getRatioOfDefaultRows is not supported for ColumnAggregateFunction"); + return 0.0; } UInt64 getNumberOfDefaultRows() const override { - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getNumberOfDefaultRows is not supported for ColumnAggregateFunction"); + return 0; } void getIndicesOfNonDefaultRows(Offsets &, size_t, size_t) const override diff --git a/src/Columns/IColumnImpl.h b/src/Columns/IColumnImpl.h index e676373e211..20aaefae39c 100644 --- a/src/Columns/IColumnImpl.h +++ b/src/Columns/IColumnImpl.h @@ -164,13 +164,9 @@ double IColumn::getRatioOfDefaultRowsImpl(double sample_ratio) const throw Exception(ErrorCodes::LOGICAL_ERROR, "Value of 'sample_ratio' must be in interval (0.0; 1.0], but got: {}", sample_ratio); - /// Randomize a little to avoid boundary effects. - std::uniform_int_distribution dist(1, static_cast(1.0 / sample_ratio)); - size_t num_rows = size(); - size_t num_sampled_rows = static_cast(num_rows * sample_ratio); - size_t num_checked_rows = dist(thread_local_rng); - num_sampled_rows = std::min(num_sampled_rows + dist(thread_local_rng), num_rows); + size_t num_sampled_rows = std::min(static_cast(num_rows * sample_ratio), num_rows); + size_t num_checked_rows = 0; size_t res = 0; if (num_sampled_rows == num_rows) @@ -191,6 +187,9 @@ double IColumn::getRatioOfDefaultRowsImpl(double sample_ratio) const } } + if (num_checked_rows == 0) + return 0.0; + return static_cast(res) / num_checked_rows; } diff --git a/src/Interpreters/Aggregator.cpp b/src/Interpreters/Aggregator.cpp index da4442f99d3..de172a6d7ac 100644 --- a/src/Interpreters/Aggregator.cpp +++ b/src/Interpreters/Aggregator.cpp @@ -1416,6 +1416,10 @@ void Aggregator::prepareAggregateInstructions( materialized_columns.push_back(columns.at(pos)->convertToFullColumnIfConst()); aggregate_columns[i][j] = materialized_columns.back().get(); + /// Sparse columns without defaults may be handled incorrectly. + if (aggregate_columns[i][j]->getNumberOfDefaultRows() == 0) + allow_sparse_arguments = false; + auto full_column = allow_sparse_arguments ? aggregate_columns[i][j]->getPtr() : recursiveRemoveSparse(aggregate_columns[i][j]->getPtr()); diff --git a/tests/queries/0_stateless/02432_s3_parallel_parts_cleanup.sql b/tests/queries/0_stateless/02432_s3_parallel_parts_cleanup.sql index 235a3335d9d..3688a649d5e 100644 --- a/tests/queries/0_stateless/02432_s3_parallel_parts_cleanup.sql +++ b/tests/queries/0_stateless/02432_s3_parallel_parts_cleanup.sql @@ -7,7 +7,7 @@ drop table if exists rmt2; create table rmt (n int, m int, k int) engine=ReplicatedMergeTree('/test/02432/{database}', '1') order by tuple() settings storage_policy = 's3_cache', allow_remote_fs_zero_copy_replication=1, max_part_removal_threads=10, concurrent_part_removal_threshold=1, cleanup_delay_period=1, cleanup_delay_period_random_add=1, - max_replicated_merges_in_queue=0, max_replicated_mutations_in_queue=0, min_bytes_for_compact_part=0, min_rows_for_compact_part=0; + max_replicated_merges_in_queue=0, max_replicated_mutations_in_queue=0, min_bytes_for_wide_part=0, min_rows_for_wide_part=0; insert into rmt(n, m) values (1, 42); insert into rmt(n, m) values (2, 42); @@ -37,7 +37,7 @@ select count(), sum(n), sum(m) from rmt; create table rmt2 (n int, m int, k String) engine=ReplicatedMergeTree('/test/02432/{database}', '2') order by tuple() settings storage_policy = 's3_cache', allow_remote_fs_zero_copy_replication=1, max_part_removal_threads=10, concurrent_part_removal_threshold=1, cleanup_delay_period=1, cleanup_delay_period_random_add=1, - min_bytes_for_compact_part=0, min_rows_for_compact_part=0, max_replicated_merges_in_queue=1, + min_bytes_for_wide_part=0, min_rows_for_wide_part=0, max_replicated_merges_in_queue=1, old_parts_lifetime=0; alter table rmt2 modify column k Nullable(String); From c24d68e7c9bb03d12d61fb5e64023daea26875c6 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Thu, 16 Feb 2023 21:53:53 +0000 Subject: [PATCH 054/342] fix tests with sparse columns --- src/Columns/IColumnImpl.h | 6 ++++-- .../0_stateless/01780_column_sparse_distinct.reference | 1 + .../queries/0_stateless/01780_column_sparse_distinct.sql | 2 +- .../0_stateless/02179_sparse_columns_detach.reference | 8 ++++---- tests/queries/0_stateless/02179_sparse_columns_detach.sql | 8 ++++---- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Columns/IColumnImpl.h b/src/Columns/IColumnImpl.h index 20aaefae39c..0eab9452813 100644 --- a/src/Columns/IColumnImpl.h +++ b/src/Columns/IColumnImpl.h @@ -164,12 +164,14 @@ double IColumn::getRatioOfDefaultRowsImpl(double sample_ratio) const throw Exception(ErrorCodes::LOGICAL_ERROR, "Value of 'sample_ratio' must be in interval (0.0; 1.0], but got: {}", sample_ratio); + static constexpr auto max_number_of_rows_for_full_search = 1000; + size_t num_rows = size(); size_t num_sampled_rows = std::min(static_cast(num_rows * sample_ratio), num_rows); size_t num_checked_rows = 0; size_t res = 0; - if (num_sampled_rows == num_rows) + if (num_sampled_rows == num_rows || num_rows <= max_number_of_rows_for_full_search) { for (size_t i = 0; i < num_rows; ++i) res += static_cast(*this).isDefaultAt(i); @@ -177,7 +179,7 @@ double IColumn::getRatioOfDefaultRowsImpl(double sample_ratio) const } else if (num_sampled_rows != 0) { - for (size_t i = num_checked_rows; i < num_rows; ++i) + for (size_t i = 0; i < num_rows; ++i) { if (num_checked_rows * num_rows <= i * num_sampled_rows) { diff --git a/tests/queries/0_stateless/01780_column_sparse_distinct.reference b/tests/queries/0_stateless/01780_column_sparse_distinct.reference index bb0cebc6540..beb45208e7b 100644 --- a/tests/queries/0_stateless/01780_column_sparse_distinct.reference +++ b/tests/queries/0_stateless/01780_column_sparse_distinct.reference @@ -5,3 +5,4 @@ all_2_2_0 v Sparse 2 3 4 +5 diff --git a/tests/queries/0_stateless/01780_column_sparse_distinct.sql b/tests/queries/0_stateless/01780_column_sparse_distinct.sql index 502ca7600d4..e98bada1aac 100644 --- a/tests/queries/0_stateless/01780_column_sparse_distinct.sql +++ b/tests/queries/0_stateless/01780_column_sparse_distinct.sql @@ -7,7 +7,7 @@ SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; SYSTEM STOP MERGES t_sparse_distinct; -INSERT INTO t_sparse_distinct SELECT number, number % 5 FROM numbers(100000); +INSERT INTO t_sparse_distinct SELECT number, number % 6 FROM numbers(100000); INSERT INTO t_sparse_distinct SELECT number, number % 100 = 0 FROM numbers(100000); SELECT name, column, serialization_kind diff --git a/tests/queries/0_stateless/02179_sparse_columns_detach.reference b/tests/queries/0_stateless/02179_sparse_columns_detach.reference index 2f9714f7a97..04a9b10c09f 100644 --- a/tests/queries/0_stateless/02179_sparse_columns_detach.reference +++ b/tests/queries/0_stateless/02179_sparse_columns_detach.reference @@ -1,12 +1,12 @@ -1000 +954 id Default s Sparse -1000 +954 id Default s Sparse -1000 +954 id Default s Sparse -1000 +954 id Default s Sparse diff --git a/tests/queries/0_stateless/02179_sparse_columns_detach.sql b/tests/queries/0_stateless/02179_sparse_columns_detach.sql index 4720e6720ba..2ae088fedb4 100644 --- a/tests/queries/0_stateless/02179_sparse_columns_detach.sql +++ b/tests/queries/0_stateless/02179_sparse_columns_detach.sql @@ -4,8 +4,8 @@ CREATE TABLE t_sparse_detach(id UInt64, s String) ENGINE = MergeTree ORDER BY id SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; -INSERT INTO t_sparse_detach SELECT number, number % 20 = 0 ? toString(number) : '' FROM numbers(10000); -INSERT INTO t_sparse_detach SELECT number, number % 20 = 0 ? toString(number) : '' FROM numbers(10000); +INSERT INTO t_sparse_detach SELECT number, number % 21 = 0 ? toString(number) : '' FROM numbers(10000); +INSERT INTO t_sparse_detach SELECT number, number % 21 = 0 ? toString(number) : '' FROM numbers(10000); OPTIMIZE TABLE t_sparse_detach FINAL; @@ -30,8 +30,8 @@ ALTER TABLE t_sparse_detach MODIFY SETTING vertical_merge_algorithm_min_rows_to_activate = 1, vertical_merge_algorithm_min_columns_to_activate = 1; -INSERT INTO t_sparse_detach SELECT number, number % 20 = 0 ? toString(number) : '' FROM numbers(10000); -INSERT INTO t_sparse_detach SELECT number, number % 20 = 0 ? toString(number) : '' FROM numbers(10000); +INSERT INTO t_sparse_detach SELECT number, number % 21 = 0 ? toString(number) : '' FROM numbers(10000); +INSERT INTO t_sparse_detach SELECT number, number % 21 = 0 ? toString(number) : '' FROM numbers(10000); OPTIMIZE TABLE t_sparse_detach FINAL; From 81f5f9708df0048010d42a2ddb334c6c1add22bb Mon Sep 17 00:00:00 2001 From: artem-yadr Date: Fri, 17 Feb 2023 01:39:23 +0300 Subject: [PATCH 055/342] Added support for MongoDB Replica Set URI with enumiration --- .../MongoDB/include/Poco/MongoDB/Connection.h | 278 +++++++++--------- base/poco/MongoDB/src/Connection.cpp | 196 ++++++++---- 2 files changed, 283 insertions(+), 191 deletions(-) diff --git a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h index de669aa90dd..319e1f93ba0 100644 --- a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h +++ b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h @@ -18,150 +18,154 @@ #define MongoDB_Connection_INCLUDED -#include "Poco/MongoDB/RequestMessage.h" -#include "Poco/MongoDB/ResponseMessage.h" -#include "Poco/Mutex.h" #include "Poco/Net/SocketAddress.h" #include "Poco/Net/StreamSocket.h" +#include "Poco/Mutex.h" +#include "Poco/MongoDB/RequestMessage.h" +#include "Poco/MongoDB/ResponseMessage.h" -namespace Poco +namespace Poco { +namespace MongoDB { + + +class MongoDB_API Connection + /// Represents a connection to a MongoDB server + /// using the MongoDB wire protocol. + /// + /// See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ + /// for more information on the wire protocol. { -namespace MongoDB +public: + typedef Poco::SharedPtr Ptr; + + class MongoDB_API SocketFactory + { + public: + SocketFactory(); + /// Creates the SocketFactory. + + virtual ~SocketFactory(); + /// Destroys the SocketFactory. + + virtual Poco::Net::StreamSocket createSocket(const std::string& host, int port, Poco::Timespan connectTimeout, bool secure); + /// Creates a Poco::Net::StreamSocket (if secure is false), or a + /// Poco::Net::SecureStreamSocket (if secure is true) connected to the + /// given host and port number. + /// + /// The default implementation will throw a Poco::NotImplementedException + /// if secure is true. + }; + + Connection(); + /// Creates an unconnected Connection. + /// + /// Use this when you want to connect later on. + + Connection(const std::string& hostAndPort); + /// Creates a Connection connected to the given MongoDB instance at host:port. + /// + /// The host and port must be separated with a colon. + + Connection(const std::string& uri, SocketFactory& socketFactory); + /// Creates a Connection connected to the given MongoDB instance at the + /// given URI. + /// + /// See the corresponding connect() method for more information. + + Connection(const std::string& host, int port); + /// Creates a Connection connected to the given MongoDB instance at host and port. + + Connection(const Poco::Net::SocketAddress& addrs); + /// Creates a Connection connected to the given MongoDB instance at the given address. + + Connection(const Poco::Net::StreamSocket& socket); + /// Creates a Connection connected to the given MongoDB instance using the given socket, + /// which must already be connected. + + virtual ~Connection(); + /// Destroys the Connection. + + Poco::Net::SocketAddress address() const; + /// Returns the address of the MongoDB server. + + std::string uri() const; + /// Returns the uri on which the connection was made. + + void connect(const std::string& hostAndPort); + /// Connects to the given MongoDB server. + /// + /// The host and port must be separated with a colon. + + void connect(const std::string& uri, SocketFactory& socketFactory); + /// Connects to the given MongoDB instance at the given URI. + /// + /// The URI must be in standard MongoDB connection string URI format: + /// + /// mongodb://:@hostname.com:/database-name?options + /// + /// The following options are supported: + /// + /// - ssl: If ssl=true is specified, a custom SocketFactory subclass creating + /// a SecureStreamSocket must be supplied. + /// - connectTimeoutMS: Socket connection timeout in milliseconds. + /// - socketTimeoutMS: Socket send/receive timeout in milliseconds. + /// - authMechanism: Authentication mechanism. Only "SCRAM-SHA-1" (default) + /// and "MONGODB-CR" are supported. + /// + /// Unknown options are silently ignored. + /// + /// Will also attempt to authenticate using the specified credentials, + /// using Database::authenticate(). + /// + /// Throws a Poco::NoPermissionException if authentication fails. + + void connect(const std::string& host, int port); + /// Connects to the given MongoDB server. + + void connect(const Poco::Net::SocketAddress& addrs); + /// Connects to the given MongoDB server. + + void connect(const Poco::Net::StreamSocket& socket); + /// Connects using an already connected socket. + + void disconnect(); + /// Disconnects from the MongoDB server. + + void sendRequest(RequestMessage& request); + /// Sends a request to the MongoDB server. + /// + /// Used for one-way requests without a response. + + void sendRequest(RequestMessage& request, ResponseMessage& response); + /// Sends a request to the MongoDB server and receives the response. + /// + /// Use this when a response is expected: only a "query" or "getmore" + /// request will return a response. + +protected: + void connect(); + +private: + Poco::Net::SocketAddress _address; + Poco::Net::StreamSocket _socket; + std::string _uri; +}; + + +// +// inlines +// +inline Net::SocketAddress Connection::address() const { - - - class MongoDB_API Connection - /// Represents a connection to a MongoDB server - /// using the MongoDB wire protocol. - /// - /// See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ - /// for more information on the wire protocol. - { - public: - typedef Poco::SharedPtr Ptr; - - class MongoDB_API SocketFactory - { - public: - SocketFactory(); - /// Creates the SocketFactory. - - virtual ~SocketFactory(); - /// Destroys the SocketFactory. - - virtual Poco::Net::StreamSocket createSocket(const std::string & host, int port, Poco::Timespan connectTimeout, bool secure); - /// Creates a Poco::Net::StreamSocket (if secure is false), or a - /// Poco::Net::SecureStreamSocket (if secure is true) connected to the - /// given host and port number. - /// - /// The default implementation will throw a Poco::NotImplementedException - /// if secure is true. - }; - - Connection(); - /// Creates an unconnected Connection. - /// - /// Use this when you want to connect later on. - - Connection(const std::string & hostAndPort); - /// Creates a Connection connected to the given MongoDB instance at host:port. - /// - /// The host and port must be separated with a colon. - - Connection(const std::string & uri, SocketFactory & socketFactory); - /// Creates a Connection connected to the given MongoDB instance at the - /// given URI. - /// - /// See the corresponding connect() method for more information. - - Connection(const std::string & host, int port); - /// Creates a Connection connected to the given MongoDB instance at host and port. - - Connection(const Poco::Net::SocketAddress & addrs); - /// Creates a Connection connected to the given MongoDB instance at the given address. - - Connection(const Poco::Net::StreamSocket & socket); - /// Creates a Connection connected to the given MongoDB instance using the given socket, - /// which must already be connected. - - virtual ~Connection(); - /// Destroys the Connection. - - Poco::Net::SocketAddress address() const; - /// Returns the address of the MongoDB server. - - void connect(const std::string & hostAndPort); - /// Connects to the given MongoDB server. - /// - /// The host and port must be separated with a colon. - - void connect(const std::string & uri, SocketFactory & socketFactory); - /// Connects to the given MongoDB instance at the given URI. - /// - /// The URI must be in standard MongoDB connection string URI format: - /// - /// mongodb://:@hostname.com:/database-name?options - /// - /// The following options are supported: - /// - /// - ssl: If ssl=true is specified, a custom SocketFactory subclass creating - /// a SecureStreamSocket must be supplied. - /// - connectTimeoutMS: Socket connection timeout in milliseconds. - /// - socketTimeoutMS: Socket send/receive timeout in milliseconds. - /// - authMechanism: Authentication mechanism. Only "SCRAM-SHA-1" (default) - /// and "MONGODB-CR" are supported. - /// - /// Unknown options are silently ignored. - /// - /// Will also attempt to authenticate using the specified credentials, - /// using Database::authenticate(). - /// - /// Throws a Poco::NoPermissionException if authentication fails. - - void connect(const std::string & host, int port); - /// Connects to the given MongoDB server. - - void connect(const Poco::Net::SocketAddress & addrs); - /// Connects to the given MongoDB server. - - void connect(const Poco::Net::StreamSocket & socket); - /// Connects using an already connected socket. - - void disconnect(); - /// Disconnects from the MongoDB server. - - void sendRequest(RequestMessage & request); - /// Sends a request to the MongoDB server. - /// - /// Used for one-way requests without a response. - - void sendRequest(RequestMessage & request, ResponseMessage & response); - /// Sends a request to the MongoDB server and receives the response. - /// - /// Use this when a response is expected: only a "query" or "getmore" - /// request will return a response. - - protected: - void connect(); - - private: - Poco::Net::SocketAddress _address; - Poco::Net::StreamSocket _socket; - }; - - - // - // inlines - // - inline Net::SocketAddress Connection::address() const - { - return _address; - } - - + return _address; } -} // namespace Poco::MongoDB +inline std::string Connection::uri() const +{ + return _uri; +} + +} } // namespace Poco::MongoDB #endif // MongoDB_Connection_INCLUDED diff --git a/base/poco/MongoDB/src/Connection.cpp b/base/poco/MongoDB/src/Connection.cpp index 56bb192cec2..411b59c774e 100644 --- a/base/poco/MongoDB/src/Connection.cpp +++ b/base/poco/MongoDB/src/Connection.cpp @@ -145,71 +145,159 @@ void Connection::connect(const Poco::Net::StreamSocket& socket) void Connection::connect(const std::string& uri, SocketFactory& socketFactory) { - Poco::URI theURI(uri); - if (theURI.getScheme() != "mongodb") throw Poco::UnknownURISchemeException(uri); + std::vector strAddresses; + std::string newURI; - std::string userInfo = theURI.getUserInfo(); - std::string host = theURI.getHost(); - Poco::UInt16 port = theURI.getPort(); - if (port == 0) port = 27017; + if (uri.find(',') != std::string::npos) + { + size_t pos; + size_t head = 0; + if ((pos = uri.find("@")) != std::string::npos) + { + head = pos + 1; + } + else if ((pos = uri.find("://")) != std::string::npos) + { + head = pos + 3; + } - std::string databaseName = theURI.getPath(); - if (!databaseName.empty() && databaseName[0] == '/') databaseName.erase(0, 1); - if (databaseName.empty()) databaseName = "admin"; + std::string tempstr; + std::string::const_iterator it = uri.begin(); + it += head; + size_t tail = head; + for (;it != uri.end() && *it != '?' && *it != '/'; ++it) + { + tempstr += *it; + tail++; + } - bool ssl = false; - Poco::Timespan connectTimeout; - Poco::Timespan socketTimeout; - std::string authMechanism = Database::AUTH_SCRAM_SHA1; + it = tempstr.begin(); + std::string token; + for (;it != tempstr.end(); ++it) + { + if (*it == ',') + { + newURI = uri.substr(0, head) + token + uri.substr(tail, uri.length()); + strAddresses.push_back(newURI); + token = ""; + } + else + { + token += *it; + } + } + newURI = uri.substr(0, head) + token + uri.substr(tail, uri.length()); + strAddresses.push_back(newURI); + } + else + { + strAddresses.push_back(uri); + } - Poco::URI::QueryParameters params = theURI.getQueryParameters(); - for (Poco::URI::QueryParameters::const_iterator it = params.begin(); it != params.end(); ++it) - { - if (it->first == "ssl") - { - ssl = (it->second == "true"); - } - else if (it->first == "connectTimeoutMS") - { - connectTimeout = static_cast(1000)*Poco::NumberParser::parse(it->second); - } - else if (it->first == "socketTimeoutMS") - { - socketTimeout = static_cast(1000)*Poco::NumberParser::parse(it->second); - } - else if (it->first == "authMechanism") - { - authMechanism = it->second; - } - } + newURI = strAddresses.front(); + Poco::URI theURI(newURI); + if (theURI.getScheme() != "mongodb") throw Poco::UnknownURISchemeException(uri); - connect(socketFactory.createSocket(host, port, connectTimeout, ssl)); + std::string userInfo = theURI.getUserInfo(); + std::string databaseName = theURI.getPath(); + if (!databaseName.empty() && databaseName[0] == '/') databaseName.erase(0, 1); + if (databaseName.empty()) databaseName = "admin"; - if (socketTimeout > 0) - { - _socket.setSendTimeout(socketTimeout); - _socket.setReceiveTimeout(socketTimeout); - } + bool ssl = false; + Poco::Timespan connectTimeout; + Poco::Timespan socketTimeout; + std::string authMechanism = Database::AUTH_SCRAM_SHA1; + std::string readPreference="primary"; - if (!userInfo.empty()) - { - std::string username; - std::string password; - std::string::size_type pos = userInfo.find(':'); - if (pos != std::string::npos) - { - username.assign(userInfo, 0, pos++); - password.assign(userInfo, pos, userInfo.size() - pos); - } - else username = userInfo; + Poco::URI::QueryParameters params = theURI.getQueryParameters(); + for (Poco::URI::QueryParameters::const_iterator it = params.begin(); it != params.end(); ++it) + { + if (it->first == "ssl") + { + ssl = (it->second == "true"); + } + else if (it->first == "connectTimeoutMS") + { + connectTimeout = static_cast(1000)*Poco::NumberParser::parse(it->second); + } + else if (it->first == "socketTimeoutMS") + { + socketTimeout = static_cast(1000)*Poco::NumberParser::parse(it->second); + } + else if (it->first == "authMechanism") + { + authMechanism = it->second; + } + else if (it->first == "readPreference") + { + readPreference= it->second; + } + } - Database database(databaseName); - if (!database.authenticate(*this, username, password, authMechanism)) - throw Poco::NoPermissionException(Poco::format("Access to MongoDB database %s denied for user %s", databaseName, username)); - } + for (std::vector::const_iterator it = strAddresses.cbegin();it != strAddresses.cend(); ++it) + { + newURI = *it; + Poco::URI theURI(newURI); + + std::string host = theURI.getHost(); + Poco::UInt16 port = theURI.getPort(); + if (port == 0) port = 27017; + + connect(socketFactory.createSocket(host, port, connectTimeout, ssl)); + _uri = newURI; + if (socketTimeout > 0) + { + _socket.setSendTimeout(socketTimeout); + _socket.setReceiveTimeout(socketTimeout); + } + if (strAddresses.size() > 1) + { + Poco::MongoDB::QueryRequest request("admin.$cmd"); + request.setNumberToReturn(1); + request.selector().add("isMaster", 1); + Poco::MongoDB::ResponseMessage response; + + sendRequest(request, response); + _uri = newURI; + if (!response.documents().empty()) + { + Poco::MongoDB::Document::Ptr doc = response.documents()[0]; + if (doc->get("ismaster") && readPreference == "primary") + { + break; + } + else if (!doc->get("ismaster") && readPreference == "secondary") + { + break; + } + else if (it + 1 == strAddresses.cend()) + { + throw Poco::URISyntaxException(uri); + } + } + } + } + if (!userInfo.empty()) + { + std::string username; + std::string password; + std::string::size_type pos = userInfo.find(':'); + if (pos != std::string::npos) + { + username.assign(userInfo, 0, pos++); + password.assign(userInfo, pos, userInfo.size() - pos); + } + else username = userInfo; + + Database database(databaseName); + + if (!database.authenticate(*this, username, password, authMechanism)) + throw Poco::NoPermissionException(Poco::format("Access to MongoDB database %s denied for user %s", databaseName, username)); + } } + void Connection::disconnect() { _socket.close(); From 4a2efc339bdc25ae5332b3f6fcd08a48e4530653 Mon Sep 17 00:00:00 2001 From: artem-yadr Date: Fri, 17 Feb 2023 01:47:36 +0300 Subject: [PATCH 056/342] Header fix --- .../MongoDB/include/Poco/MongoDB/Connection.h | 280 +++++++++--------- 1 file changed, 142 insertions(+), 138 deletions(-) diff --git a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h index 319e1f93ba0..699b9652eaf 100644 --- a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h +++ b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h @@ -18,154 +18,158 @@ #define MongoDB_Connection_INCLUDED -#include "Poco/Net/SocketAddress.h" -#include "Poco/Net/StreamSocket.h" -#include "Poco/Mutex.h" #include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/ResponseMessage.h" +#include "Poco/Mutex.h" +#include "Poco/Net/SocketAddress.h" +#include "Poco/Net/StreamSocket.h" -namespace Poco { -namespace MongoDB { - - -class MongoDB_API Connection - /// Represents a connection to a MongoDB server - /// using the MongoDB wire protocol. - /// - /// See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ - /// for more information on the wire protocol. +namespace Poco { -public: - typedef Poco::SharedPtr Ptr; - - class MongoDB_API SocketFactory - { - public: - SocketFactory(); - /// Creates the SocketFactory. - - virtual ~SocketFactory(); - /// Destroys the SocketFactory. - - virtual Poco::Net::StreamSocket createSocket(const std::string& host, int port, Poco::Timespan connectTimeout, bool secure); - /// Creates a Poco::Net::StreamSocket (if secure is false), or a - /// Poco::Net::SecureStreamSocket (if secure is true) connected to the - /// given host and port number. - /// - /// The default implementation will throw a Poco::NotImplementedException - /// if secure is true. - }; - - Connection(); - /// Creates an unconnected Connection. - /// - /// Use this when you want to connect later on. - - Connection(const std::string& hostAndPort); - /// Creates a Connection connected to the given MongoDB instance at host:port. - /// - /// The host and port must be separated with a colon. - - Connection(const std::string& uri, SocketFactory& socketFactory); - /// Creates a Connection connected to the given MongoDB instance at the - /// given URI. - /// - /// See the corresponding connect() method for more information. - - Connection(const std::string& host, int port); - /// Creates a Connection connected to the given MongoDB instance at host and port. - - Connection(const Poco::Net::SocketAddress& addrs); - /// Creates a Connection connected to the given MongoDB instance at the given address. - - Connection(const Poco::Net::StreamSocket& socket); - /// Creates a Connection connected to the given MongoDB instance using the given socket, - /// which must already be connected. - - virtual ~Connection(); - /// Destroys the Connection. - - Poco::Net::SocketAddress address() const; - /// Returns the address of the MongoDB server. - - std::string uri() const; - /// Returns the uri on which the connection was made. - - void connect(const std::string& hostAndPort); - /// Connects to the given MongoDB server. - /// - /// The host and port must be separated with a colon. - - void connect(const std::string& uri, SocketFactory& socketFactory); - /// Connects to the given MongoDB instance at the given URI. - /// - /// The URI must be in standard MongoDB connection string URI format: - /// - /// mongodb://:@hostname.com:/database-name?options - /// - /// The following options are supported: - /// - /// - ssl: If ssl=true is specified, a custom SocketFactory subclass creating - /// a SecureStreamSocket must be supplied. - /// - connectTimeoutMS: Socket connection timeout in milliseconds. - /// - socketTimeoutMS: Socket send/receive timeout in milliseconds. - /// - authMechanism: Authentication mechanism. Only "SCRAM-SHA-1" (default) - /// and "MONGODB-CR" are supported. - /// - /// Unknown options are silently ignored. - /// - /// Will also attempt to authenticate using the specified credentials, - /// using Database::authenticate(). - /// - /// Throws a Poco::NoPermissionException if authentication fails. - - void connect(const std::string& host, int port); - /// Connects to the given MongoDB server. - - void connect(const Poco::Net::SocketAddress& addrs); - /// Connects to the given MongoDB server. - - void connect(const Poco::Net::StreamSocket& socket); - /// Connects using an already connected socket. - - void disconnect(); - /// Disconnects from the MongoDB server. - - void sendRequest(RequestMessage& request); - /// Sends a request to the MongoDB server. - /// - /// Used for one-way requests without a response. - - void sendRequest(RequestMessage& request, ResponseMessage& response); - /// Sends a request to the MongoDB server and receives the response. - /// - /// Use this when a response is expected: only a "query" or "getmore" - /// request will return a response. - -protected: - void connect(); - -private: - Poco::Net::SocketAddress _address; - Poco::Net::StreamSocket _socket; - std::string _uri; -}; - - -// -// inlines -// -inline Net::SocketAddress Connection::address() const +namespace MongoDB { - return _address; -} -inline std::string Connection::uri() const + + + class MongoDB_API Connection + /// Represents a connection to a MongoDB server + /// using the MongoDB wire protocol. + /// + /// See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ + /// for more information on the wire protocol. + { + public: + typedef Poco::SharedPtr Ptr; + + class MongoDB_API SocketFactory + { + public: + SocketFactory(); + /// Creates the SocketFactory. + + virtual ~SocketFactory(); + /// Destroys the SocketFactory. + + virtual Poco::Net::StreamSocket createSocket(const std::string & host, int port, Poco::Timespan connectTimeout, bool secure); + /// Creates a Poco::Net::StreamSocket (if secure is false), or a + /// Poco::Net::SecureStreamSocket (if secure is true) connected to the + /// given host and port number. + /// + /// The default implementation will throw a Poco::NotImplementedException + /// if secure is true. + }; + + Connection(); + /// Creates an unconnected Connection. + /// + /// Use this when you want to connect later on. + + Connection(const std::string & hostAndPort); + /// Creates a Connection connected to the given MongoDB instance at host:port. + /// + /// The host and port must be separated with a colon. + + Connection(const std::string & uri, SocketFactory & socketFactory); + /// Creates a Connection connected to the given MongoDB instance at the + /// given URI. + /// + /// See the corresponding connect() method for more information. + + Connection(const std::string & host, int port); + /// Creates a Connection connected to the given MongoDB instance at host and port. + + Connection(const Poco::Net::SocketAddress & addrs); + /// Creates a Connection connected to the given MongoDB instance at the given address. + + Connection(const Poco::Net::StreamSocket & socket); + /// Creates a Connection connected to the given MongoDB instance using the given socket, + /// which must already be connected. + + virtual ~Connection(); + /// Destroys the Connection. + + Poco::Net::SocketAddress address() const; + /// Returns the address of the MongoDB server. + + std::string uri() const; + /// Returns the uri on which the connection was made. + + void connect(const std::string & hostAndPort); + /// Connects to the given MongoDB server. + /// + /// The host and port must be separated with a colon. + + void connect(const std::string & uri, SocketFactory & socketFactory); + /// Connects to the given MongoDB instance at the given URI. + /// + /// The URI must be in standard MongoDB connection string URI format: + /// + /// mongodb://:@hostname.com:/database-name?options + /// + /// The following options are supported: + /// + /// - ssl: If ssl=true is specified, a custom SocketFactory subclass creating + /// a SecureStreamSocket must be supplied. + /// - connectTimeoutMS: Socket connection timeout in milliseconds. + /// - socketTimeoutMS: Socket send/receive timeout in milliseconds. + /// - authMechanism: Authentication mechanism. Only "SCRAM-SHA-1" (default) + /// and "MONGODB-CR" are supported. + /// + /// Unknown options are silently ignored. + /// + /// Will also attempt to authenticate using the specified credentials, + /// using Database::authenticate(). + /// + /// Throws a Poco::NoPermissionException if authentication fails. + + void connect(const std::string & host, int port); + /// Connects to the given MongoDB server. + + void connect(const Poco::Net::SocketAddress & addrs); + /// Connects to the given MongoDB server. + + void connect(const Poco::Net::StreamSocket & socket); + /// Connects using an already connected socket. + + void disconnect(); + /// Disconnects from the MongoDB server. + + void sendRequest(RequestMessage & request); + /// Sends a request to the MongoDB server. + /// + /// Used for one-way requests without a response. + + void sendRequest(RequestMessage & request, ResponseMessage & response); + /// Sends a request to the MongoDB server and receives the response. + /// + /// Use this when a response is expected: only a "query" or "getmore" + /// request will return a response. + + protected: + void connect(); + + private: + Poco::Net::SocketAddress _address; + Poco::Net::StreamSocket _socket; + std::string _uri; + }; + + + // + // inlines + // + inline Net::SocketAddress Connection::address() const + { + return _address; + } + inline std::string Connection::uri() const { return _uri; } -} } // namespace Poco::MongoDB + +} +} // namespace Poco::MongoDB #endif // MongoDB_Connection_INCLUDED From 83dbde476b1a9554efa0d1edb6d93e9460964a17 Mon Sep 17 00:00:00 2001 From: artem-yadr Date: Fri, 17 Feb 2023 01:53:42 +0300 Subject: [PATCH 057/342] move brackets and delete newline --- base/poco/MongoDB/src/Connection.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/base/poco/MongoDB/src/Connection.cpp b/base/poco/MongoDB/src/Connection.cpp index 411b59c774e..c75211cf9d7 100644 --- a/base/poco/MongoDB/src/Connection.cpp +++ b/base/poco/MongoDB/src/Connection.cpp @@ -297,7 +297,6 @@ void Connection::connect(const std::string& uri, SocketFactory& socketFactory) } - void Connection::disconnect() { _socket.close(); From 4999d6cb1a0398328ddad048a90d53a0cab89ec7 Mon Sep 17 00:00:00 2001 From: artem-yadr <84010375+artem-yadr@users.noreply.github.com> Date: Fri, 17 Feb 2023 01:58:31 +0300 Subject: [PATCH 058/342] Update Connection.h --- base/poco/MongoDB/include/Poco/MongoDB/Connection.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h index 699b9652eaf..8fd9c7919e4 100644 --- a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h +++ b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h @@ -163,9 +163,9 @@ namespace MongoDB return _address; } inline std::string Connection::uri() const -{ + { return _uri; -} + } } From 08734d4dc0a0ef5f46838248138488e880f757f2 Mon Sep 17 00:00:00 2001 From: artem-yadr Date: Fri, 17 Feb 2023 14:56:21 +0300 Subject: [PATCH 059/342] poco changes are now used in MongoDBDictionarySource --- src/Dictionaries/MongoDBDictionarySource.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Dictionaries/MongoDBDictionarySource.cpp b/src/Dictionaries/MongoDBDictionarySource.cpp index bec566c29a6..f61efcab026 100644 --- a/src/Dictionaries/MongoDBDictionarySource.cpp +++ b/src/Dictionaries/MongoDBDictionarySource.cpp @@ -114,7 +114,11 @@ MongoDBDictionarySource::MongoDBDictionarySource( { if (!uri.empty()) { - Poco::URI poco_uri(uri); + // Connect with URI. + Poco::MongoDB::Connection::SocketFactory socket_factory; + connection->connect(uri, socket_factory); + + Poco::URI poco_uri(connection.uri()); // Parse database from URI. This is required for correctness -- the // cursor is created using database name and collection name, so we have @@ -134,10 +138,6 @@ MongoDBDictionarySource::MongoDBDictionarySource( { user.resize(separator); } - - // Connect with URI. - Poco::MongoDB::Connection::SocketFactory socket_factory; - connection->connect(uri, socket_factory); } else { From 6b6daf7ae8bdaa98081bb5c2bff259f02522d13f Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Fri, 17 Feb 2023 14:45:08 +0000 Subject: [PATCH 060/342] Automatic style fix --- tests/ci/workflow_approve_rerun_lambda/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ci/workflow_approve_rerun_lambda/app.py b/tests/ci/workflow_approve_rerun_lambda/app.py index b563a9786c4..fb14dfd2258 100644 --- a/tests/ci/workflow_approve_rerun_lambda/app.py +++ b/tests/ci/workflow_approve_rerun_lambda/app.py @@ -123,7 +123,7 @@ TRUSTED_CONTRIBUTORS = { "BoloniniD", # Seasoned contributor, HSE "tonickkozlov", # Cloudflare "tylerhannan", # ClickHouse Employee - "myrrc", # Mike Kot, DoubleCloud + "myrrc", # Mike Kot, DoubleCloud ] } From 046e2bb8eac0127f56fdaaedf54bee3cbb8f4fea Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Fri, 17 Feb 2023 16:50:20 +0000 Subject: [PATCH 061/342] fix issues with sparse columns --- src/Columns/ColumnSparse.cpp | 6 ++++-- .../01201_read_single_thread_in_order.sql | 2 +- .../01551_mergetree_read_in_order_spread.sql | 2 +- .../01710_projection_optimize_materialize.sql | 2 +- .../0_stateless/02008_materialize_column.sql | 16 ++++++++-------- .../0_stateless/02402_merge_engine_with_view.sql | 4 ++-- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Columns/ColumnSparse.cpp b/src/Columns/ColumnSparse.cpp index 9bc78ef115d..0e408fca467 100644 --- a/src/Columns/ColumnSparse.cpp +++ b/src/Columns/ColumnSparse.cpp @@ -450,11 +450,13 @@ void ColumnSparse::compareColumn(const IColumn & rhs, size_t rhs_row_num, { const auto & rhs_sparse = assert_cast(rhs); PaddedPODArray nested_result; - values->compareColumn(rhs_sparse.getValuesColumn(), rhs_sparse.getValueIndex(rhs_row_num), + values->compareColumn( + rhs_sparse.getValuesColumn(), + rhs_sparse.getValueIndex(rhs_row_num), nullptr, nested_result, direction, nan_direction_hint); const auto & offsets_data = getOffsetsData(); - compare_results.resize_fill(_size, nested_result[0]); + std::fill(compare_results.begin(), compare_results.end(), nested_result[0]); for (size_t i = 0; i < offsets_data.size(); ++i) compare_results[offsets_data[i]] = nested_result[i + 1]; } diff --git a/tests/queries/0_stateless/01201_read_single_thread_in_order.sql b/tests/queries/0_stateless/01201_read_single_thread_in_order.sql index bfe03192891..71437b2e04c 100644 --- a/tests/queries/0_stateless/01201_read_single_thread_in_order.sql +++ b/tests/queries/0_stateless/01201_read_single_thread_in_order.sql @@ -6,7 +6,7 @@ CREATE TABLE t ) ENGINE = MergeTree ORDER BY number -SETTINGS index_granularity = 128; +SETTINGS index_granularity = 128, ratio_of_defaults_for_sparse_serialization = 1.0; SET min_insert_block_size_rows = 0, min_insert_block_size_bytes = 0; INSERT INTO t SELECT number FROM numbers(10000000); diff --git a/tests/queries/0_stateless/01551_mergetree_read_in_order_spread.sql b/tests/queries/0_stateless/01551_mergetree_read_in_order_spread.sql index 1d21d861e20..41d4ff68824 100644 --- a/tests/queries/0_stateless/01551_mergetree_read_in_order_spread.sql +++ b/tests/queries/0_stateless/01551_mergetree_read_in_order_spread.sql @@ -1,4 +1,4 @@ --- Tags: no-s3-storage +-- Tags: no-s3-storage, no-random-merge-tree-settings DROP TABLE IF EXISTS data_01551; diff --git a/tests/queries/0_stateless/01710_projection_optimize_materialize.sql b/tests/queries/0_stateless/01710_projection_optimize_materialize.sql index d8251aabaf6..92d3ead828c 100644 --- a/tests/queries/0_stateless/01710_projection_optimize_materialize.sql +++ b/tests/queries/0_stateless/01710_projection_optimize_materialize.sql @@ -1,6 +1,6 @@ drop table if exists z; -create table z (pk Int64, d Date, id UInt64, c UInt64) Engine MergeTree partition by d order by pk ; +create table z (pk Int64, d Date, id UInt64, c UInt64) Engine MergeTree partition by d order by pk settings ratio_of_defaults_for_sparse_serialization = 1.0; insert into z select number, '2021-10-24', intDiv (number, 10000), 1 from numbers(1000000); optimize table z final; diff --git a/tests/queries/0_stateless/02008_materialize_column.sql b/tests/queries/0_stateless/02008_materialize_column.sql index 8a8eb2afe83..a78920d2525 100644 --- a/tests/queries/0_stateless/02008_materialize_column.sql +++ b/tests/queries/0_stateless/02008_materialize_column.sql @@ -8,33 +8,33 @@ INSERT INTO tmp SELECT * FROM system.numbers LIMIT 20; ALTER TABLE tmp MATERIALIZE COLUMN x; -- { serverError 36 } ALTER TABLE tmp ADD COLUMN s String DEFAULT toString(x); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(arraySort(groupArray(x))), groupArray(s) FROM tmp; ALTER TABLE tmp MODIFY COLUMN s String DEFAULT toString(x+1); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp MATERIALIZE COLUMN s; ALTER TABLE tmp MODIFY COLUMN s String DEFAULT toString(x+2); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp MATERIALIZE COLUMN s; ALTER TABLE tmp MODIFY COLUMN s String DEFAULT toString(x+3); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp DROP COLUMN s; ALTER TABLE tmp ADD COLUMN s String MATERIALIZED toString(x); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp MODIFY COLUMN s String MATERIALIZED toString(x+1); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp MATERIALIZE COLUMN s; ALTER TABLE tmp MODIFY COLUMN s String MATERIALIZED toString(x+2); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp MATERIALIZE COLUMN s; ALTER TABLE tmp MODIFY COLUMN s String MATERIALIZED toString(x+3); -SELECT groupArray(x), groupArray(s) FROM tmp; +SELECT arraySort(groupArray(x)), groupArray(s) FROM tmp; ALTER TABLE tmp DROP COLUMN s; DROP TABLE tmp; diff --git a/tests/queries/0_stateless/02402_merge_engine_with_view.sql b/tests/queries/0_stateless/02402_merge_engine_with_view.sql index 64822784845..ae9de1426e7 100644 --- a/tests/queries/0_stateless/02402_merge_engine_with_view.sql +++ b/tests/queries/0_stateless/02402_merge_engine_with_view.sql @@ -1,7 +1,7 @@ -- #40014 -CREATE TABLE m0 (id UInt64) ENGINE=MergeTree ORDER BY id SETTINGS index_granularity = 1; +CREATE TABLE m0 (id UInt64) ENGINE=MergeTree ORDER BY id SETTINGS index_granularity = 1, ratio_of_defaults_for_sparse_serialization = 1.0; INSERT INTO m0 SELECT number FROM numbers(10); -CREATE TABLE m1 (id UInt64, s String) ENGINE=MergeTree ORDER BY id SETTINGS index_granularity = 1; +CREATE TABLE m1 (id UInt64, s String) ENGINE=MergeTree ORDER BY id SETTINGS index_granularity = 1, ratio_of_defaults_for_sparse_serialization = 1.0; INSERT INTO m1 SELECT number, 'boo' FROM numbers(10); CREATE VIEW m1v AS SELECT id FROM m1; From 8da3594cd8463eb0974d79d3829f5ece93cc7477 Mon Sep 17 00:00:00 2001 From: avogar Date: Mon, 20 Feb 2023 17:42:56 +0000 Subject: [PATCH 062/342] Fix IPv4/IPv6 serialization/deserialization in binary formats --- src/Formats/CapnProtoUtils.cpp | 2 + src/Functions/array/FunctionArrayMapped.h | 1 + .../Formats/Impl/ArrowColumnToCHColumn.cpp | 37 ++++++++ .../Formats/Impl/AvroRowInputFormat.cpp | 11 +++ .../Formats/Impl/AvroRowOutputFormat.cpp | 14 +++ .../Impl/BSONEachRowRowInputFormat.cpp | 92 +++++++++++++------ .../Impl/BSONEachRowRowOutputFormat.cpp | 18 ++++ .../Formats/Impl/CHColumnToArrowColumn.cpp | 54 +++++++++++ .../Formats/Impl/CapnProtoRowInputFormat.cpp | 3 + .../Formats/Impl/CapnProtoRowOutputFormat.cpp | 5 + .../Formats/Impl/MsgPackRowInputFormat.cpp | 11 +++ .../Formats/Impl/MsgPackRowOutputFormat.cpp | 12 +++ .../Formats/Impl/ORCBlockOutputFormat.cpp | 15 +++ .../02566_ipv4_ipv6_binary_formats.reference | 18 ++++ .../02566_ipv4_ipv6_binary_formats.sh | 45 +++++++++ .../format_schemas/02566_ipv4_ipv6.capnp | 6 ++ 16 files changed, 316 insertions(+), 28 deletions(-) create mode 100644 tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference create mode 100755 tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh create mode 100644 tests/queries/0_stateless/format_schemas/02566_ipv4_ipv6.capnp diff --git a/src/Formats/CapnProtoUtils.cpp b/src/Formats/CapnProtoUtils.cpp index e5f619faff5..a557b762672 100644 --- a/src/Formats/CapnProtoUtils.cpp +++ b/src/Formats/CapnProtoUtils.cpp @@ -317,6 +317,7 @@ static bool checkCapnProtoType(const capnp::Type & capnp_type, const DataTypePtr case TypeIndex::UInt16: return capnp_type.isUInt16(); case TypeIndex::DateTime: [[fallthrough]]; + case TypeIndex::IPv4: [[fallthrough]]; case TypeIndex::UInt32: return capnp_type.isUInt32(); case TypeIndex::UInt64: @@ -355,6 +356,7 @@ static bool checkCapnProtoType(const capnp::Type & capnp_type, const DataTypePtr case TypeIndex::LowCardinality: return checkCapnProtoType(capnp_type, assert_cast(data_type.get())->getDictionaryType(), mode, error_message, column_name); case TypeIndex::FixedString: [[fallthrough]]; + case TypeIndex::IPv6: [[fallthrough]]; case TypeIndex::String: return capnp_type.isText() || capnp_type.isData(); default: diff --git a/src/Functions/array/FunctionArrayMapped.h b/src/Functions/array/FunctionArrayMapped.h index 5092698d01c..5d758826f6a 100644 --- a/src/Functions/array/FunctionArrayMapped.h +++ b/src/Functions/array/FunctionArrayMapped.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/src/Processors/Formats/Impl/ArrowColumnToCHColumn.cpp b/src/Processors/Formats/Impl/ArrowColumnToCHColumn.cpp index ef25b5a332f..b7a914427a2 100644 --- a/src/Processors/Formats/Impl/ArrowColumnToCHColumn.cpp +++ b/src/Processors/Formats/Impl/ArrowColumnToCHColumn.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -527,6 +528,38 @@ static std::shared_ptr getNestedArrowColumn(std::shared_ptr return std::make_shared(array_vector); } +static ColumnWithTypeAndName readIPv6ColumnFromBinaryData(std::shared_ptr & arrow_column, const String & column_name) +{ + size_t total_size = 0; + for (int chunk_i = 0, num_chunks = arrow_column->num_chunks(); chunk_i < num_chunks; ++chunk_i) + { + auto & chunk = dynamic_cast(*(arrow_column->chunk(chunk_i))); + const size_t chunk_length = chunk.length(); + + for (size_t i = 0; i != chunk_length; ++i) + { + /// If at least one value size is not 16 bytes, fallback to reading String column and further cast to IPv6. + if (chunk.value_length(i) != sizeof(IPv6)) + return readColumnWithStringData(arrow_column, column_name); + } + total_size += chunk_length; + } + + auto internal_type = std::make_shared(); + auto internal_column = internal_type->createColumn(); + auto & data = assert_cast(*internal_column).getData(); + data.reserve(total_size * sizeof(IPv6)); + + for (int chunk_i = 0, num_chunks = arrow_column->num_chunks(); chunk_i < num_chunks; ++chunk_i) + { + auto & chunk = dynamic_cast(*(arrow_column->chunk(chunk_i))); + std::shared_ptr buffer = chunk.value_data(); + const auto * raw_data = reinterpret_cast(buffer->data()); + data.insert_assume_reserved(raw_data, raw_data + chunk.length()); + } + return {std::move(internal_column), std::move(internal_type), column_name}; +} + static ColumnWithTypeAndName readColumnFromArrowColumn( std::shared_ptr & arrow_column, const std::string & column_name, @@ -558,7 +591,11 @@ static ColumnWithTypeAndName readColumnFromArrowColumn( { case arrow::Type::STRING: case arrow::Type::BINARY: + { + if (type_hint && isIPv6(type_hint)) + return readIPv6ColumnFromBinaryData(arrow_column, column_name); return readColumnWithStringData(arrow_column, column_name); + } case arrow::Type::FIXED_SIZE_BINARY: return readColumnWithFixedStringData(arrow_column, column_name); case arrow::Type::LARGE_BINARY: diff --git a/src/Processors/Formats/Impl/AvroRowInputFormat.cpp b/src/Processors/Formats/Impl/AvroRowInputFormat.cpp index eacd29e0db1..26f5a76776e 100644 --- a/src/Processors/Formats/Impl/AvroRowInputFormat.cpp +++ b/src/Processors/Formats/Impl/AvroRowInputFormat.cpp @@ -145,6 +145,9 @@ static void insertNumber(IColumn & column, WhichDataType type, T value) case TypeIndex::DateTime64: assert_cast &>(column).insertValue(static_cast(value)); break; + case TypeIndex::IPv4: + assert_cast(column).insertValue(IPv4(static_cast(value))); + break; default: throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Type is not compatible with Avro"); } @@ -386,6 +389,14 @@ AvroDeserializer::DeserializeFn AvroDeserializer::createDeserializeFn(avro::Node column.insertData(reinterpret_cast(tmp_fixed.data()), tmp_fixed.size()); }; } + else if (target.isIPv6() && fixed_size == sizeof(IPv6)) + { + return [tmp_fixed = std::vector(fixed_size)](IColumn & column, avro::Decoder & decoder) mutable + { + decoder.decodeFixed(tmp_fixed.size(), tmp_fixed); + column.insertData(reinterpret_cast(tmp_fixed.data()), tmp_fixed.size()); + }; + } break; } case avro::AVRO_SYMBOLIC: diff --git a/src/Processors/Formats/Impl/AvroRowOutputFormat.cpp b/src/Processors/Formats/Impl/AvroRowOutputFormat.cpp index 8483a91df62..1ca56a1c5cc 100644 --- a/src/Processors/Formats/Impl/AvroRowOutputFormat.cpp +++ b/src/Processors/Formats/Impl/AvroRowOutputFormat.cpp @@ -127,6 +127,11 @@ AvroSerializer::SchemaWithSerializeFn AvroSerializer::createSchemaWithSerializeF { encoder.encodeInt(assert_cast(column).getElement(row_num)); }}; + case TypeIndex::IPv4: + return {avro::IntSchema(), [](const IColumn & column, size_t row_num, avro::Encoder & encoder) + { + encoder.encodeInt(assert_cast(column).getElement(row_num)); + }}; case TypeIndex::Int32: return {avro::IntSchema(), [](const IColumn & column, size_t row_num, avro::Encoder & encoder) { @@ -205,6 +210,15 @@ AvroSerializer::SchemaWithSerializeFn AvroSerializer::createSchemaWithSerializeF encoder.encodeFixed(reinterpret_cast(s.data()), s.size()); }}; } + case TypeIndex::IPv6: + { + auto schema = avro::FixedSchema(sizeof(IPv6), "ipv6_" + toString(type_name_increment)); + return {schema, [](const IColumn & column, size_t row_num, avro::Encoder & encoder) + { + const std::string_view & s = assert_cast(column).getDataAt(row_num).toView(); + encoder.encodeFixed(reinterpret_cast(s.data()), s.size()); + }}; + } case TypeIndex::Enum8: { auto schema = avro::EnumSchema("enum8_" + toString(type_name_increment)); /// type names must be different for different types. diff --git a/src/Processors/Formats/Impl/BSONEachRowRowInputFormat.cpp b/src/Processors/Formats/Impl/BSONEachRowRowInputFormat.cpp index c4ffce2bc65..e0064c282b4 100644 --- a/src/Processors/Formats/Impl/BSONEachRowRowInputFormat.cpp +++ b/src/Processors/Formats/Impl/BSONEachRowRowInputFormat.cpp @@ -151,6 +151,17 @@ static void readAndInsertInteger(ReadBuffer & in, IColumn & column, const DataTy } } +static void readAndInsertIPv4(ReadBuffer & in, IColumn & column, BSONType bson_type) +{ + /// We expect BSON type Int32 as IPv4 value. + if (bson_type != BSONType::INT32) + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON Int32 into column with type IPv4"); + + UInt32 value; + readBinary(value, in); + assert_cast(column).insertValue(IPv4(value)); +} + template static void readAndInsertDouble(ReadBuffer & in, IColumn & column, const DataTypePtr & data_type, BSONType bson_type) { @@ -296,37 +307,52 @@ static void readAndInsertString(ReadBuffer & in, IColumn & column, BSONType bson } } +static void readAndInsertIPv6(ReadBuffer & in, IColumn & column, BSONType bson_type) +{ + if (bson_type != BSONType::BINARY) + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into IPv6 column", getBSONTypeName(bson_type)); + + auto size = readBSONSize(in); + auto subtype = getBSONBinarySubtype(readBSONType(in)); + if (subtype != BSONBinarySubtype::BINARY) + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON Binary subtype {} into IPv6 column", getBSONBinarySubtypeName(subtype)); + + if (size != sizeof(IPv6)) + throw Exception( + ErrorCodes::INCORRECT_DATA, + "Cannot parse value of type IPv6, size of binary data is not equal to the binary size of IPv6 value: {} != {}", + size, + sizeof(IPv6)); + + IPv6 value; + readBinary(value, in); + assert_cast(column).insertValue(value); +} + + static void readAndInsertUUID(ReadBuffer & in, IColumn & column, BSONType bson_type) { - if (bson_type == BSONType::BINARY) - { - auto size = readBSONSize(in); - auto subtype = getBSONBinarySubtype(readBSONType(in)); - if (subtype == BSONBinarySubtype::UUID || subtype == BSONBinarySubtype::UUID_OLD) - { - if (size != sizeof(UUID)) - throw Exception( - ErrorCodes::INCORRECT_DATA, - "Cannot parse value of type UUID, size of binary data is not equal to the binary size of UUID value: {} != {}", - size, - sizeof(UUID)); - - UUID value; - readBinary(value, in); - assert_cast(column).insertValue(value); - } - else - { - throw Exception( - ErrorCodes::ILLEGAL_COLUMN, - "Cannot insert BSON Binary subtype {} into UUID column", - getBSONBinarySubtypeName(subtype)); - } - } - else - { + if (bson_type != BSONType::BINARY) throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into UUID column", getBSONTypeName(bson_type)); - } + + auto size = readBSONSize(in); + auto subtype = getBSONBinarySubtype(readBSONType(in)); + if (subtype != BSONBinarySubtype::UUID && subtype != BSONBinarySubtype::UUID_OLD) + throw Exception( + ErrorCodes::ILLEGAL_COLUMN, + "Cannot insert BSON Binary subtype {} into UUID column", + getBSONBinarySubtypeName(subtype)); + + if (size != sizeof(UUID)) + throw Exception( + ErrorCodes::INCORRECT_DATA, + "Cannot parse value of type UUID, size of binary data is not equal to the binary size of UUID value: {} != {}", + size, + sizeof(UUID)); + + UUID value; + readBinary(value, in); + assert_cast(column).insertValue(value); } void BSONEachRowRowInputFormat::readArray(IColumn & column, const DataTypePtr & data_type, BSONType bson_type) @@ -591,6 +617,16 @@ bool BSONEachRowRowInputFormat::readField(IColumn & column, const DataTypePtr & readAndInsertString(*in, column, bson_type); return true; } + case TypeIndex::IPv4: + { + readAndInsertIPv4(*in, column, bson_type); + return true; + } + case TypeIndex::IPv6: + { + readAndInsertIPv6(*in, column, bson_type); + return true; + } case TypeIndex::UUID: { readAndInsertUUID(*in, column, bson_type); diff --git a/src/Processors/Formats/Impl/BSONEachRowRowOutputFormat.cpp b/src/Processors/Formats/Impl/BSONEachRowRowOutputFormat.cpp index 211021b0d78..95dd3079687 100644 --- a/src/Processors/Formats/Impl/BSONEachRowRowOutputFormat.cpp +++ b/src/Processors/Formats/Impl/BSONEachRowRowOutputFormat.cpp @@ -124,6 +124,7 @@ size_t BSONEachRowRowOutputFormat::countBSONFieldSize(const IColumn & column, co case TypeIndex::Date: [[fallthrough]]; case TypeIndex::Date32: [[fallthrough]]; case TypeIndex::Decimal32: [[fallthrough]]; + case TypeIndex::IPv4: [[fallthrough]]; case TypeIndex::Int32: { return size + sizeof(Int32); @@ -168,6 +169,10 @@ size_t BSONEachRowRowOutputFormat::countBSONFieldSize(const IColumn & column, co const auto & string_column = assert_cast(column); return size + sizeof(BSONSizeT) + string_column.getN() + 1; // Size of data + data + \0 or BSON subtype (in case of BSON binary) } + case TypeIndex::IPv6: + { + return size + sizeof(BSONSizeT) + 1 + sizeof(IPv6); // Size of data + BSON binary subtype + 16 bytes of value + } case TypeIndex::UUID: { return size + sizeof(BSONSizeT) + 1 + sizeof(UUID); // Size of data + BSON binary subtype + 16 bytes of value @@ -371,6 +376,19 @@ void BSONEachRowRowOutputFormat::serializeField(const IColumn & column, const Da writeBSONString(column, row_num, name, out, settings.bson.output_string_as_string); break; } + case TypeIndex::IPv4: + { + writeBSONNumber(BSONType::INT32, column, row_num, name, out); + break; + } + case TypeIndex::IPv6: + { + writeBSONTypeAndKeyName(BSONType::BINARY, name, out); + writeBSONSize(sizeof(IPv6), out); + writeBSONType(BSONBinarySubtype::BINARY, out); + writeBinary(assert_cast(column).getElement(row_num), out); + break; + } case TypeIndex::UUID: { writeBSONTypeAndKeyName(BSONType::BINARY, name, out); diff --git a/src/Processors/Formats/Impl/CHColumnToArrowColumn.cpp b/src/Processors/Formats/Impl/CHColumnToArrowColumn.cpp index aef54516627..8698b343eb3 100644 --- a/src/Processors/Formats/Impl/CHColumnToArrowColumn.cpp +++ b/src/Processors/Formats/Impl/CHColumnToArrowColumn.cpp @@ -434,6 +434,46 @@ namespace DB checkStatus(status, write_column->getName(), format_name); } + static void fillArrowArrayWithIPv6ColumnData( + ColumnPtr write_column, + const PaddedPODArray * null_bytemap, + const String & format_name, + arrow::ArrayBuilder* array_builder, + size_t start, + size_t end) + { + const auto & internal_column = assert_cast(*write_column); + const auto & internal_data = internal_column.getData(); + size_t fixed_length = sizeof(IPv6); + arrow::FixedSizeBinaryBuilder & builder = assert_cast(*array_builder); + arrow::Status status; + + PaddedPODArray arrow_null_bytemap = revertNullByteMap(null_bytemap, start, end); + const UInt8 * arrow_null_bytemap_raw_ptr = arrow_null_bytemap.empty() ? nullptr : arrow_null_bytemap.data(); + + const uint8_t * data_start = reinterpret_cast(internal_data.data()) + start * fixed_length; + status = builder.AppendValues(data_start, end - start, reinterpret_cast(arrow_null_bytemap_raw_ptr)); + checkStatus(status, write_column->getName(), format_name); + } + + static void fillArrowArrayWithIPv4ColumnData( + ColumnPtr write_column, + const PaddedPODArray * null_bytemap, + const String & format_name, + arrow::ArrayBuilder* array_builder, + size_t start, + size_t end) + { + const auto & internal_data = assert_cast(*write_column).getData(); + auto & builder = assert_cast(*array_builder); + arrow::Status status; + + PaddedPODArray arrow_null_bytemap = revertNullByteMap(null_bytemap, start, end); + const UInt8 * arrow_null_bytemap_raw_ptr = arrow_null_bytemap.empty() ? nullptr : arrow_null_bytemap.data(); + status = builder.AppendValues(&(internal_data.data() + start)->toUnderType(), end - start, reinterpret_cast(arrow_null_bytemap_raw_ptr)); + checkStatus(status, write_column->getName(), format_name); + } + static void fillArrowArrayWithDateColumnData( ColumnPtr write_column, const PaddedPODArray * null_bytemap, @@ -541,6 +581,14 @@ namespace DB else fillArrowArrayWithStringColumnData(column, null_bytemap, format_name, array_builder, start, end); } + else if (isIPv6(column_type)) + { + fillArrowArrayWithIPv6ColumnData(column, null_bytemap, format_name, array_builder, start, end); + } + else if (isIPv4(column_type)) + { + fillArrowArrayWithIPv4ColumnData(column, null_bytemap, format_name, array_builder, start, end); + } else if (isDate(column_type)) { fillArrowArrayWithDateColumnData(column, null_bytemap, format_name, array_builder, start, end); @@ -781,6 +829,12 @@ namespace DB if (isBool(column_type)) return arrow::boolean(); + if (isIPv6(column_type)) + return arrow::fixed_size_binary(sizeof(IPv6)); + + if (isIPv4(column_type)) + return arrow::uint32(); + const std::string type_name = column_type->getFamilyName(); if (const auto * arrow_type_it = std::find_if( internal_type_to_arrow_type.begin(), diff --git a/src/Processors/Formats/Impl/CapnProtoRowInputFormat.cpp b/src/Processors/Formats/Impl/CapnProtoRowInputFormat.cpp index 58ace9cfca5..9f37bcc3219 100644 --- a/src/Processors/Formats/Impl/CapnProtoRowInputFormat.cpp +++ b/src/Processors/Formats/Impl/CapnProtoRowInputFormat.cpp @@ -128,6 +128,9 @@ static void insertUnsignedInteger(IColumn & column, const DataTypePtr & column_t case TypeIndex::UInt64: assert_cast(column).insertValue(value); break; + case TypeIndex::IPv4: + assert_cast(column).insertValue(IPv4(static_cast(value))); + break; default: throw Exception(ErrorCodes::LOGICAL_ERROR, "Column type is not an unsigned integer."); } diff --git a/src/Processors/Formats/Impl/CapnProtoRowOutputFormat.cpp b/src/Processors/Formats/Impl/CapnProtoRowOutputFormat.cpp index dcbd5db5f9b..c0f61bbd586 100644 --- a/src/Processors/Formats/Impl/CapnProtoRowOutputFormat.cpp +++ b/src/Processors/Formats/Impl/CapnProtoRowOutputFormat.cpp @@ -111,7 +111,12 @@ static std::optional convertToDynamicValue( case capnp::DynamicValue::Type::INT: return capnp::DynamicValue::Reader(column->getInt(row_num)); case capnp::DynamicValue::Type::UINT: + { + /// IPv4 column doesn't support getUInt method. + if (isIPv4(data_type)) + return capnp::DynamicValue::Reader(assert_cast(column.get())->getElement(row_num)); return capnp::DynamicValue::Reader(column->getUInt(row_num)); + } case capnp::DynamicValue::Type::BOOL: return capnp::DynamicValue::Reader(column->getBool(row_num)); case capnp::DynamicValue::Type::FLOAT: diff --git a/src/Processors/Formats/Impl/MsgPackRowInputFormat.cpp b/src/Processors/Formats/Impl/MsgPackRowInputFormat.cpp index f337eedbb05..5605b87f3a9 100644 --- a/src/Processors/Formats/Impl/MsgPackRowInputFormat.cpp +++ b/src/Processors/Formats/Impl/MsgPackRowInputFormat.cpp @@ -162,6 +162,11 @@ static void insertInteger(IColumn & column, DataTypePtr type, UInt64 value) assert_cast(column).insertValue(value); break; } + case TypeIndex::IPv4: + { + assert_cast(column).insertValue(IPv4(static_cast(value))); + break; + } default: throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert MessagePack integer into column with type {}.", type->getName()); } @@ -190,6 +195,12 @@ static void insertString(IColumn & column, DataTypePtr type, const char * value, return; } + if (isIPv6(type) && bin) + { + assert_cast(column).insertData(value, size); + return; + } + if (!isStringOrFixedString(type)) throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert MessagePack string into column with type {}.", type->getName()); diff --git a/src/Processors/Formats/Impl/MsgPackRowOutputFormat.cpp b/src/Processors/Formats/Impl/MsgPackRowOutputFormat.cpp index a1ed45ec40f..07951d42bc6 100644 --- a/src/Processors/Formats/Impl/MsgPackRowOutputFormat.cpp +++ b/src/Processors/Formats/Impl/MsgPackRowOutputFormat.cpp @@ -56,6 +56,11 @@ void MsgPackRowOutputFormat::serializeField(const IColumn & column, DataTypePtr packer.pack_uint32(assert_cast(column).getElement(row_num)); return; } + case TypeIndex::IPv4: + { + packer.pack_uint32(assert_cast(column).getElement(row_num)); + return; + } case TypeIndex::UInt64: { packer.pack_uint64(assert_cast(column).getElement(row_num)); @@ -110,6 +115,13 @@ void MsgPackRowOutputFormat::serializeField(const IColumn & column, DataTypePtr packer.pack_bin_body(string.data(), static_cast(string.size())); return; } + case TypeIndex::IPv6: + { + const std::string_view & data = assert_cast(column).getDataAt(row_num).toView(); + packer.pack_bin(static_cast(data.size())); + packer.pack_bin_body(data.data(), static_cast(data.size())); + return; + } case TypeIndex::Array: { auto nested_type = assert_cast(*data_type).getNestedType(); diff --git a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp index 42c3e178436..86d9560beb9 100644 --- a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp @@ -75,6 +75,7 @@ std::unique_ptr ORCBlockOutputFormat::getORCType(const DataTypePtr & return orc::createPrimitiveType(orc::TypeKind::SHORT); } case TypeIndex::UInt32: [[fallthrough]]; + case TypeIndex::IPv4: [[fallthrough]]; case TypeIndex::Int32: { return orc::createPrimitiveType(orc::TypeKind::INT); @@ -109,6 +110,10 @@ std::unique_ptr ORCBlockOutputFormat::getORCType(const DataTypePtr & return orc::createPrimitiveType(orc::TypeKind::STRING); return orc::createPrimitiveType(orc::TypeKind::BINARY); } + case TypeIndex::IPv6: + { + return orc::createPrimitiveType(orc::TypeKind::BINARY); + } case TypeIndex::Nullable: { return getORCType(removeNullable(type)); @@ -309,6 +314,11 @@ void ORCBlockOutputFormat::writeColumn( writeNumbers(orc_column, column, null_bytemap, [](const UInt32 & value){ return value; }); break; } + case TypeIndex::IPv4: + { + writeNumbers(orc_column, column, null_bytemap, [](const IPv4 & value){ return value.toUnderType(); }); + break; + } case TypeIndex::Int64: { writeNumbers(orc_column, column, null_bytemap, [](const Int64 & value){ return value; }); @@ -339,6 +349,11 @@ void ORCBlockOutputFormat::writeColumn( writeStrings(orc_column, column, null_bytemap); break; } + case TypeIndex::IPv6: + { + writeStrings(orc_column, column, null_bytemap); + break; + } case TypeIndex::DateTime: { writeDateTimes( diff --git a/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference new file mode 100644 index 00000000000..e228d911715 --- /dev/null +++ b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference @@ -0,0 +1,18 @@ +CapnProto +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 +Avro +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 +Arrow +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 +Parquet +ipv6 Nullable(FixedString(16)) +ipv4 Nullable(Int64) +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 +ORC +ipv6 Nullable(String) +ipv4 Nullable(Int32) +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 +BSONEachRow +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 +MsgPack +2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 diff --git a/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh new file mode 100755 index 00000000000..e84c06027e8 --- /dev/null +++ b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Tags: no-fasttest, no-parallel + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +echo "CapnProto" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format CapnProto settings format_schema='format_schemas/02566_ipv4_ipv6:Message'" > 02566_ipv4_ipv6_data.capnp +${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.capnp, auto, 'ipv6 IPv6, ipv4 IPv4') settings format_schema='format_schemas/02566_ipv4_ipv6:Message'" +rm 02566_ipv4_ipv6_data.capnp + +echo "Avro" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format Avro" > 02566_ipv4_ipv6_data.avro +${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.avro, auto, 'ipv6 IPv6, ipv4 IPv4')" +rm 02566_ipv4_ipv6_data.avro + +echo "Arrow" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format Arrow" > 02566_ipv4_ipv6_data.arrow +${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.arrow, auto, 'ipv6 IPv6, ipv4 IPv4')" +rm 02566_ipv4_ipv6_data.arrow + +echo "Parquet" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format Parquet" > 02566_ipv4_ipv6_data.parquet +${CLICKHOUSE_LOCAL} -q "desc file(02566_ipv4_ipv6_data.parquet)" +${CLICKHOUSE_LOCAL} -q "select ipv6, toIPv4(ipv4) from file(02566_ipv4_ipv6_data.parquet, auto, 'ipv6 IPv6, ipv4 UInt32')" +rm 02566_ipv4_ipv6_data.parquet + +echo "ORC" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format ORC" > 02566_ipv4_ipv6_data.orc +${CLICKHOUSE_LOCAL} -q "desc file(02566_ipv4_ipv6_data.orc)" +${CLICKHOUSE_LOCAL} -q "select ipv6, toIPv4(ipv4) from file(02566_ipv4_ipv6_data.orc, auto, 'ipv6 IPv6, ipv4 UInt32')" +rm 02566_ipv4_ipv6_data.orc + +echo "BSONEachRow" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format BSONEachRow" > 02566_ipv4_ipv6_data.bson +${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.bson, auto, 'ipv6 IPv6, ipv4 IPv4')" +rm 02566_ipv4_ipv6_data.bson + +echo "MsgPack" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format MsgPack" > 02566_ipv4_ipv6_data.msgpack +${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.msgpack, auto, 'ipv6 IPv6, ipv4 IPv4')" +rm 02566_ipv4_ipv6_data.msgpack + + diff --git a/tests/queries/0_stateless/format_schemas/02566_ipv4_ipv6.capnp b/tests/queries/0_stateless/format_schemas/02566_ipv4_ipv6.capnp new file mode 100644 index 00000000000..f999043e2d2 --- /dev/null +++ b/tests/queries/0_stateless/format_schemas/02566_ipv4_ipv6.capnp @@ -0,0 +1,6 @@ +@0xb6ecde1cd54a101d; + +struct Message { + ipv4 @0 :UInt32; + ipv6 @1 :Data; +} From e37f6b545775783bac0a21e8e951c01586ab1778 Mon Sep 17 00:00:00 2001 From: avogar Date: Mon, 20 Feb 2023 19:50:25 +0000 Subject: [PATCH 063/342] Update docs --- docs/en/interfaces/formats.md | 209 ++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 98 deletions(-) diff --git a/docs/en/interfaces/formats.md b/docs/en/interfaces/formats.md index e94c6377ae9..f1935131491 100644 --- a/docs/en/interfaces/formats.md +++ b/docs/en/interfaces/formats.md @@ -1232,50 +1232,52 @@ Each row is formatted as a single document and each column is formatted as a sin For output it uses the following correspondence between ClickHouse types and BSON types: -| ClickHouse type | BSON Type | -|-----------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------| -| [Bool](/docs/en/sql-reference/data-types/boolean.md) | `\x08` boolean | -| [Int8/UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `\x10` int32 | -| [Int16UInt16](/docs/en/sql-reference/data-types/int-uint.md) | `\x10` int32 | -| [Int32](/docs/en/sql-reference/data-types/int-uint.md) | `\x10` int32 | -| [UInt32](/docs/en/sql-reference/data-types/int-uint.md) | `\x12` int64 | -| [Int64/UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `\x12` int64 | -| [Float32/Float64](/docs/en/sql-reference/data-types/float.md) | `\x01` double | -| [Date](/docs/en/sql-reference/data-types/date.md)/[Date32](/docs/en/sql-reference/data-types/date32.md) | `\x10` int32 | -| [DateTime](/docs/en/sql-reference/data-types/datetime.md) | `\x12` int64 | -| [DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | `\x09` datetime | -| [Decimal32](/docs/en/sql-reference/data-types/decimal.md) | `\x10` int32 | -| [Decimal64](/docs/en/sql-reference/data-types/decimal.md) | `\x12` int64 | -| [Decimal128](/docs/en/sql-reference/data-types/decimal.md) | `\x05` binary, `\x00` binary subtype, size = 16 | -| [Decimal256](/docs/en/sql-reference/data-types/decimal.md) | `\x05` binary, `\x00` binary subtype, size = 32 | -| [Int128/UInt128](/docs/en/sql-reference/data-types/int-uint.md) | `\x05` binary, `\x00` binary subtype, size = 16 | -| [Int256/UInt256](/docs/en/sql-reference/data-types/int-uint.md) | `\x05` binary, `\x00` binary subtype, size = 32 | +| ClickHouse type | BSON Type | +|-----------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------| +| [Bool](/docs/en/sql-reference/data-types/boolean.md) | `\x08` boolean | +| [Int8/UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `\x10` int32 | +| [Int16UInt16](/docs/en/sql-reference/data-types/int-uint.md) | `\x10` int32 | +| [Int32](/docs/en/sql-reference/data-types/int-uint.md) | `\x10` int32 | +| [UInt32](/docs/en/sql-reference/data-types/int-uint.md) | `\x12` int64 | +| [Int64/UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `\x12` int64 | +| [Float32/Float64](/docs/en/sql-reference/data-types/float.md) | `\x01` double | +| [Date](/docs/en/sql-reference/data-types/date.md)/[Date32](/docs/en/sql-reference/data-types/date32.md) | `\x10` int32 | +| [DateTime](/docs/en/sql-reference/data-types/datetime.md) | `\x12` int64 | +| [DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | `\x09` datetime | +| [Decimal32](/docs/en/sql-reference/data-types/decimal.md) | `\x10` int32 | +| [Decimal64](/docs/en/sql-reference/data-types/decimal.md) | `\x12` int64 | +| [Decimal128](/docs/en/sql-reference/data-types/decimal.md) | `\x05` binary, `\x00` binary subtype, size = 16 | +| [Decimal256](/docs/en/sql-reference/data-types/decimal.md) | `\x05` binary, `\x00` binary subtype, size = 32 | +| [Int128/UInt128](/docs/en/sql-reference/data-types/int-uint.md) | `\x05` binary, `\x00` binary subtype, size = 16 | +| [Int256/UInt256](/docs/en/sql-reference/data-types/int-uint.md) | `\x05` binary, `\x00` binary subtype, size = 32 | | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | `\x05` binary, `\x00` binary subtype or \x02 string if setting output_format_bson_string_as_string is enabled | -| [UUID](/docs/en/sql-reference/data-types/uuid.md) | `\x05` binary, `\x04` uuid subtype, size = 16 | -| [Array](/docs/en/sql-reference/data-types/array.md) | `\x04` array | -| [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `\x04` array | -| [Named Tuple](/docs/en/sql-reference/data-types/tuple.md) | `\x03` document | -| [Map](/docs/en/sql-reference/data-types/map.md) (with String keys) | `\x03` document | +| [UUID](/docs/en/sql-reference/data-types/uuid.md) | `\x05` binary, `\x04` uuid subtype, size = 16 | +| [Array](/docs/en/sql-reference/data-types/array.md) | `\x04` array | +| [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `\x04` array | +| [Named Tuple](/docs/en/sql-reference/data-types/tuple.md) | `\x03` document | +| [Map](/docs/en/sql-reference/data-types/map.md) (with String keys) | `\x03` document | +| [IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | `\x10` int32 | +| [IPv6](/docs/en/sql-reference/data-types/domains/ipv6.md) | `\x05` binary, `\x00` binary subtype | For input it uses the following correspondence between BSON types and ClickHouse types: -| BSON Type | ClickHouse Type | -|------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `\x01` double | [Float32/Float64](/docs/en/sql-reference/data-types/float.md) | -| `\x02` string | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | -| `\x03` document | [Map](/docs/en/sql-reference/data-types/map.md)/[Named Tuple](/docs/en/sql-reference/data-types/tuple.md) | -| `\x04` array | [Array](/docs/en/sql-reference/data-types/array.md)/[Tuple](/docs/en/sql-reference/data-types/tuple.md) | -| `\x05` binary, `\x00` binary subtype | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | -| `\x05` binary, `\x02` old binary subtype | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | -| `\x05` binary, `\x03` old uuid subtype | [UUID](/docs/en/sql-reference/data-types/uuid.md) | -| `\x05` binary, `\x04` uuid subtype | [UUID](/docs/en/sql-reference/data-types/uuid.md) | -| `\x07` ObjectId | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | -| `\x08` boolean | [Bool](/docs/en/sql-reference/data-types/boolean.md) | -| `\x09` datetime | [DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | -| `\x0A` null value | [NULL](/docs/en/sql-reference/data-types/nullable.md) | -| `\x0D` JavaScript code | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | -| `\x0E` symbol | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | -| `\x10` int32 | [Int32/UInt32](/docs/en/sql-reference/data-types/int-uint.md)/[Decimal32](/docs/en/sql-reference/data-types/decimal.md) | +| BSON Type | ClickHouse Type | +|------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `\x01` double | [Float32/Float64](/docs/en/sql-reference/data-types/float.md) | +| `\x02` string | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | +| `\x03` document | [Map](/docs/en/sql-reference/data-types/map.md)/[Named Tuple](/docs/en/sql-reference/data-types/tuple.md) | +| `\x04` array | [Array](/docs/en/sql-reference/data-types/array.md)/[Tuple](/docs/en/sql-reference/data-types/tuple.md) | +| `\x05` binary, `\x00` binary subtype | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md)/[IPv6](/docs/en/sql-reference/data-types/domains/ipv6.md) | +| `\x05` binary, `\x02` old binary subtype | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | +| `\x05` binary, `\x03` old uuid subtype | [UUID](/docs/en/sql-reference/data-types/uuid.md) | +| `\x05` binary, `\x04` uuid subtype | [UUID](/docs/en/sql-reference/data-types/uuid.md) | +| `\x07` ObjectId | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | +| `\x08` boolean | [Bool](/docs/en/sql-reference/data-types/boolean.md) | +| `\x09` datetime | [DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | +| `\x0A` null value | [NULL](/docs/en/sql-reference/data-types/nullable.md) | +| `\x0D` JavaScript code | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | +| `\x0E` symbol | [String](/docs/en/sql-reference/data-types/string.md)/[FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | +| `\x10` int32 | [Int32/UInt32](/docs/en/sql-reference/data-types/int-uint.md)/[Decimal32](/docs/en/sql-reference/data-types/decimal.md)/[IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | | `\x12` int64 | [Int64/UInt64](/docs/en/sql-reference/data-types/int-uint.md)/[Decimal64](/docs/en/sql-reference/data-types/decimal.md)/[DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | Other BSON types are not supported. Also, it performs conversion between different integer types (for example, you can insert BSON int32 value into ClickHouse UInt8). @@ -1608,23 +1610,25 @@ See also [Format Schema](#formatschema). The table below shows supported data types and how they match ClickHouse [data types](/docs/en/sql-reference/data-types/index.md) in `INSERT` and `SELECT` queries. -| CapnProto data type (`INSERT`) | ClickHouse data type | CapnProto data type (`SELECT`) | -|--------------------------------|-----------------------------------------------------------|--------------------------------| -| `UINT8`, `BOOL` | [UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `UINT8` | -| `INT8` | [Int8](/docs/en/sql-reference/data-types/int-uint.md) | `INT8` | -| `UINT16` | [UInt16](/docs/en/sql-reference/data-types/int-uint.md), [Date](/docs/en/sql-reference/data-types/date.md) | `UINT16` | -| `INT16` | [Int16](/docs/en/sql-reference/data-types/int-uint.md) | `INT16` | -| `UINT32` | [UInt32](/docs/en/sql-reference/data-types/int-uint.md), [DateTime](/docs/en/sql-reference/data-types/datetime.md) | `UINT32` | -| `INT32` | [Int32](/docs/en/sql-reference/data-types/int-uint.md) | `INT32` | -| `UINT64` | [UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `UINT64` | -| `INT64` | [Int64](/docs/en/sql-reference/data-types/int-uint.md), [DateTime64](/docs/en/sql-reference/data-types/datetime.md) | `INT64` | -| `FLOAT32` | [Float32](/docs/en/sql-reference/data-types/float.md) | `FLOAT32` | -| `FLOAT64` | [Float64](/docs/en/sql-reference/data-types/float.md) | `FLOAT64` | -| `TEXT, DATA` | [String](/docs/en/sql-reference/data-types/string.md), [FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | `TEXT, DATA` | -| `union(T, Void), union(Void, T)` | [Nullable(T)](/docs/en/sql-reference/data-types/date.md) | `union(T, Void), union(Void, T)` | -| `ENUM` | [Enum(8\|16)](/docs/en/sql-reference/data-types/enum.md) | `ENUM` | -| `LIST` | [Array](/docs/en/sql-reference/data-types/array.md) | `LIST` | -| `STRUCT` | [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `STRUCT` | +| CapnProto data type (`INSERT`) | ClickHouse data type | CapnProto data type (`SELECT`) | +|----------------------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------| +| `UINT8`, `BOOL` | [UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `UINT8` | +| `INT8` | [Int8](/docs/en/sql-reference/data-types/int-uint.md) | `INT8` | +| `UINT16` | [UInt16](/docs/en/sql-reference/data-types/int-uint.md), [Date](/docs/en/sql-reference/data-types/date.md) | `UINT16` | +| `INT16` | [Int16](/docs/en/sql-reference/data-types/int-uint.md) | `INT16` | +| `UINT32` | [UInt32](/docs/en/sql-reference/data-types/int-uint.md), [DateTime](/docs/en/sql-reference/data-types/datetime.md) | `UINT32` | +| `INT32` | [Int32](/docs/en/sql-reference/data-types/int-uint.md) | `INT32` | +| `UINT64` | [UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `UINT64` | +| `INT64` | [Int64](/docs/en/sql-reference/data-types/int-uint.md), [DateTime64](/docs/en/sql-reference/data-types/datetime.md) | `INT64` | +| `FLOAT32` | [Float32](/docs/en/sql-reference/data-types/float.md) | `FLOAT32` | +| `FLOAT64` | [Float64](/docs/en/sql-reference/data-types/float.md) | `FLOAT64` | +| `TEXT, DATA` | [String](/docs/en/sql-reference/data-types/string.md), [FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | `TEXT, DATA` | +| `union(T, Void), union(Void, T)` | [Nullable(T)](/docs/en/sql-reference/data-types/date.md) | `union(T, Void), union(Void, T)` | +| `ENUM` | [Enum(8\ |16)](/docs/en/sql-reference/data-types/enum.md) | `ENUM` | +| `LIST` | [Array](/docs/en/sql-reference/data-types/array.md) | `LIST` | +| `STRUCT` | [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `STRUCT` | +| `UINT32` | [IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | `UINT32` | +| `DATA` | [IPv6](/docs/en/sql-reference/data-types/domains/ipv6.md) | `DATA` | For working with `Enum` in CapnProto format use the [format_capn_proto_enum_comparising_mode](/docs/en/operations/settings/settings-formats.md/#format_capn_proto_enum_comparising_mode) setting. @@ -1804,21 +1808,23 @@ ClickHouse Avro format supports reading and writing [Avro data files](https://av The table below shows supported data types and how they match ClickHouse [data types](/docs/en/sql-reference/data-types/index.md) in `INSERT` and `SELECT` queries. -| Avro data type `INSERT` | ClickHouse data type | Avro data type `SELECT` | -|---------------------------------------------|----------------------------------------------------------------------------------------------------|------------------------------| -| `boolean`, `int`, `long`, `float`, `double` | [Int(8\|16\|32)](/docs/en/sql-reference/data-types/int-uint.md), [UInt(8\|16\|32)](/docs/en/sql-reference/data-types/int-uint.md) | `int` | -| `boolean`, `int`, `long`, `float`, `double` | [Int64](/docs/en/sql-reference/data-types/int-uint.md), [UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `long` | -| `boolean`, `int`, `long`, `float`, `double` | [Float32](/docs/en/sql-reference/data-types/float.md) | `float` | -| `boolean`, `int`, `long`, `float`, `double` | [Float64](/docs/en/sql-reference/data-types/float.md) | `double` | -| `bytes`, `string`, `fixed`, `enum` | [String](/docs/en/sql-reference/data-types/string.md) | `bytes` or `string` \* | -| `bytes`, `string`, `fixed` | [FixedString(N)](/docs/en/sql-reference/data-types/fixedstring.md) | `fixed(N)` | -| `enum` | [Enum(8\|16)](/docs/en/sql-reference/data-types/enum.md) | `enum` | -| `array(T)` | [Array(T)](/docs/en/sql-reference/data-types/array.md) | `array(T)` | -| `union(null, T)`, `union(T, null)` | [Nullable(T)](/docs/en/sql-reference/data-types/date.md) | `union(null, T)` | -| `null` | [Nullable(Nothing)](/docs/en/sql-reference/data-types/special-data-types/nothing.md) | `null` | -| `int (date)` \** | [Date](/docs/en/sql-reference/data-types/date.md) | `int (date)` \** | -| `long (timestamp-millis)` \** | [DateTime64(3)](/docs/en/sql-reference/data-types/datetime.md) | `long (timestamp-millis)` \* | -| `long (timestamp-micros)` \** | [DateTime64(6)](/docs/en/sql-reference/data-types/datetime.md) | `long (timestamp-micros)` \* | +| Avro data type `INSERT` | ClickHouse data type | Avro data type `SELECT` | +|---------------------------------------------|-----------------------------------------------------------------------------------------------------------------|-------------------------------------------------| +| `boolean`, `int`, `long`, `float`, `double` | [Int(8\ | 16\ |32)](/docs/en/sql-reference/data-types/int-uint.md), [UInt(8\|16\|32)](/docs/en/sql-reference/data-types/int-uint.md) | `int` | +| `boolean`, `int`, `long`, `float`, `double` | [Int64](/docs/en/sql-reference/data-types/int-uint.md), [UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `long` | +| `boolean`, `int`, `long`, `float`, `double` | [Float32](/docs/en/sql-reference/data-types/float.md) | `float` | +| `boolean`, `int`, `long`, `float`, `double` | [Float64](/docs/en/sql-reference/data-types/float.md) | `double` | +| `bytes`, `string`, `fixed`, `enum` | [String](/docs/en/sql-reference/data-types/string.md) | `bytes` or `string` \* | +| `bytes`, `string`, `fixed` | [FixedString(N)](/docs/en/sql-reference/data-types/fixedstring.md) | `fixed(N)` | +| `enum` | [Enum(8\ | 16)](/docs/en/sql-reference/data-types/enum.md) | `enum` | +| `array(T)` | [Array(T)](/docs/en/sql-reference/data-types/array.md) | `array(T)` | +| `union(null, T)`, `union(T, null)` | [Nullable(T)](/docs/en/sql-reference/data-types/date.md) | `union(null, T)` | +| `null` | [Nullable(Nothing)](/docs/en/sql-reference/data-types/special-data-types/nothing.md) | `null` | +| `int (date)` \** | [Date](/docs/en/sql-reference/data-types/date.md) | `int (date)` \** | +| `long (timestamp-millis)` \** | [DateTime64(3)](/docs/en/sql-reference/data-types/datetime.md) | `long (timestamp-millis)` \* | +| `long (timestamp-micros)` \** | [DateTime64(6)](/docs/en/sql-reference/data-types/datetime.md) | `long (timestamp-micros)` \* | +| `int` | [IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | `int` | +| `fixed(16)` | [IPv6](/docs/en/sql-reference/data-types/domains/ipv6.md) | `fixed(16)` | \* `bytes` is default, controlled by [output_format_avro_string_column_pattern](/docs/en/operations/settings/settings-formats.md/#output_format_avro_string_column_pattern) \** [Avro logical types](https://avro.apache.org/docs/current/spec.html#Logical+Types) @@ -1918,28 +1924,30 @@ Setting `format_avro_schema_registry_url` needs to be configured in `users.xml` The table below shows supported data types and how they match ClickHouse [data types](/docs/en/sql-reference/data-types/index.md) in `INSERT` and `SELECT` queries. -| Parquet data type (`INSERT`) | ClickHouse data type | Parquet data type (`SELECT`) | -|-----------------------------------------------|-----------------------------------------------------------------|------------------------------| -| `BOOL` | [Bool](/docs/en/sql-reference/data-types/boolean.md) | `BOOL` | -| `UINT8`, `BOOL` | [UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `UINT8` | -| `INT8` | [Int8](/docs/en/sql-reference/data-types/int-uint.md) | `INT8` | -| `UINT16` | [UInt16](/docs/en/sql-reference/data-types/int-uint.md) | `UINT16` | -| `INT16` | [Int16](/docs/en/sql-reference/data-types/int-uint.md) | `INT16` | -| `UINT32` | [UInt32](/docs/en/sql-reference/data-types/int-uint.md) | `UINT32` | -| `INT32` | [Int32](/docs/en/sql-reference/data-types/int-uint.md) | `INT32` | -| `UINT64` | [UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `UINT64` | -| `INT64` | [Int64](/docs/en/sql-reference/data-types/int-uint.md) | `INT64` | -| `FLOAT` | [Float32](/docs/en/sql-reference/data-types/float.md) | `FLOAT` | -| `DOUBLE` | [Float64](/docs/en/sql-reference/data-types/float.md) | `DOUBLE` | -| `DATE` | [Date32](/docs/en/sql-reference/data-types/date.md) | `DATE` | -| `TIME (ms)` | [DateTime](/docs/en/sql-reference/data-types/datetime.md) | `UINT32` | -| `TIMESTAMP`, `TIME (us, ns)` | [DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | `TIMESTAMP` | -| `STRING`, `BINARY` | [String](/docs/en/sql-reference/data-types/string.md) | `BINARY` | -| `STRING`, `BINARY`, `FIXED_LENGTH_BYTE_ARRAY` | [FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | `FIXED_LENGTH_BYTE_ARRAY` | -| `DECIMAL` | [Decimal](/docs/en/sql-reference/data-types/decimal.md) | `DECIMAL` | -| `LIST` | [Array](/docs/en/sql-reference/data-types/array.md) | `LIST` | -| `STRUCT` | [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `STRUCT` | -| `MAP` | [Map](/docs/en/sql-reference/data-types/map.md) | `MAP` | +| Parquet data type (`INSERT`) | ClickHouse data type | Parquet data type (`SELECT`) | +|----------------------------------------------------|-----------------------------------------------------------------|------------------------------| +| `BOOL` | [Bool](/docs/en/sql-reference/data-types/boolean.md) | `BOOL` | +| `UINT8`, `BOOL` | [UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `UINT8` | +| `INT8` | [Int8](/docs/en/sql-reference/data-types/int-uint.md) | `INT8` | +| `UINT16` | [UInt16](/docs/en/sql-reference/data-types/int-uint.md) | `UINT16` | +| `INT16` | [Int16](/docs/en/sql-reference/data-types/int-uint.md) | `INT16` | +| `UINT32` | [UInt32](/docs/en/sql-reference/data-types/int-uint.md) | `UINT32` | +| `INT32` | [Int32](/docs/en/sql-reference/data-types/int-uint.md) | `INT32` | +| `UINT64` | [UInt64](/docs/en/sql-reference/data-types/int-uint.md) | `UINT64` | +| `INT64` | [Int64](/docs/en/sql-reference/data-types/int-uint.md) | `INT64` | +| `FLOAT` | [Float32](/docs/en/sql-reference/data-types/float.md) | `FLOAT` | +| `DOUBLE` | [Float64](/docs/en/sql-reference/data-types/float.md) | `DOUBLE` | +| `DATE` | [Date32](/docs/en/sql-reference/data-types/date.md) | `DATE` | +| `TIME (ms)` | [DateTime](/docs/en/sql-reference/data-types/datetime.md) | `UINT32` | +| `TIMESTAMP`, `TIME (us, ns)` | [DateTime64](/docs/en/sql-reference/data-types/datetime64.md) | `TIMESTAMP` | +| `STRING`, `BINARY` | [String](/docs/en/sql-reference/data-types/string.md) | `BINARY` | +| `STRING`, `BINARY`, `FIXED_LENGTH_BYTE_ARRAY` | [FixedString](/docs/en/sql-reference/data-types/fixedstring.md) | `FIXED_LENGTH_BYTE_ARRAY` | +| `DECIMAL` | [Decimal](/docs/en/sql-reference/data-types/decimal.md) | `DECIMAL` | +| `LIST` | [Array](/docs/en/sql-reference/data-types/array.md) | `LIST` | +| `STRUCT` | [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `STRUCT` | +| `MAP` | [Map](/docs/en/sql-reference/data-types/map.md) | `MAP` | +| `UINT32` | [IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | `UINT32` | +| `FIXED_LENGTH_BYTE_ARRAY` | [IPv6](/docs/en/sql-reference/data-types/domains/ipv6.md) | `FIXED_LENGTH_BYTE_ARRAY` | Arrays can be nested and can have a value of the `Nullable` type as an argument. `Tuple` and `Map` types also can be nested. @@ -2006,6 +2014,8 @@ The table below shows supported data types and how they match ClickHouse [data t | `LIST` | [Array](/docs/en/sql-reference/data-types/array.md) | `LIST` | | `STRUCT` | [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `STRUCT` | | `MAP` | [Map](/docs/en/sql-reference/data-types/map.md) | `MAP` | +| `UINT32` | [IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | `UINT32` | +| `FIXED_SIZE_BINARY`, `BINARY` | [IPv6](/docs/en/sql-reference/data-types/domains/ipv6.md) | `FIXED_SIZE_BINARY` | Arrays can be nested and can have a value of the `Nullable` type as an argument. `Tuple` and `Map` types also can be nested. @@ -2053,8 +2063,8 @@ $ clickhouse-client --query="SELECT * FROM {some_table} FORMAT Arrow" > {filenam The table below shows supported data types and how they match ClickHouse [data types](/docs/en/sql-reference/data-types/index.md) in `INSERT` and `SELECT` queries. -| ORC data type (`INSERT`) | ClickHouse data type | ORC data type (`SELECT`) | -|---------------------------------------|---------------------------------------------------------|--------------------------| +| ORC data type (`INSERT`) | ClickHouse data type | ORC data type (`SELECT`) | +|---------------------------------------|---------------------------------------------------------------|--------------------------| | `Boolean` | [UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `Boolean` | | `Tinyint` | [Int8](/docs/en/sql-reference/data-types/int-uint.md) | `Tinyint` | | `Smallint` | [Int16](/docs/en/sql-reference/data-types/int-uint.md) | `Smallint` | @@ -2069,6 +2079,7 @@ The table below shows supported data types and how they match ClickHouse [data t | `List` | [Array](/docs/en/sql-reference/data-types/array.md) | `List` | | `Struct` | [Tuple](/docs/en/sql-reference/data-types/tuple.md) | `Struct` | | `Map` | [Map](/docs/en/sql-reference/data-types/map.md) | `Map` | +| `-` | [IPv4](/docs/en/sql-reference/data-types/int-uint.md) | `Int` | Other types are not supported. @@ -2263,8 +2274,8 @@ ClickHouse supports reading and writing [MessagePack](https://msgpack.org/) data ### Data Types Matching {#data-types-matching-msgpack} -| MessagePack data type (`INSERT`) | ClickHouse data type | MessagePack data type (`SELECT`) | -|--------------------------------------------------------------------|-----------------------------------------------------------|------------------------------------| +| MessagePack data type (`INSERT`) | ClickHouse data type | MessagePack data type (`SELECT`) | +|--------------------------------------------------------------------|-----------------------------------------------------------------|------------------------------------| | `uint N`, `positive fixint` | [UIntN](/docs/en/sql-reference/data-types/int-uint.md) | `uint N` | | `int N`, `negative fixint` | [IntN](/docs/en/sql-reference/data-types/int-uint.md) | `int N` | | `bool` | [UInt8](/docs/en/sql-reference/data-types/int-uint.md) | `uint 8` | @@ -2277,6 +2288,8 @@ ClickHouse supports reading and writing [MessagePack](https://msgpack.org/) data | `uint 64` | [DateTime64](/docs/en/sql-reference/data-types/datetime.md) | `uint 64` | | `fixarray`, `array 16`, `array 32` | [Array](/docs/en/sql-reference/data-types/array.md) | `fixarray`, `array 16`, `array 32` | | `fixmap`, `map 16`, `map 32` | [Map](/docs/en/sql-reference/data-types/map.md) | `fixmap`, `map 16`, `map 32` | +| `uint 32` | [IPv4](/docs/en/sql-reference/data-types/domains/ipv4.md) | `uint 32` | +| `bin 8` | [String](/docs/en/sql-reference/data-types/string.md) | `bin 8` | Example: From bf020298100a51ed57f10b0d1f9e2eceedd802bb Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 21 Feb 2023 12:19:58 +0000 Subject: [PATCH 064/342] Add pass for transforming or equality chain to single IN --- .../Passes/OrEqualityChainToInPass.cpp | 117 ++++++++++++++++++ src/Analyzer/Passes/OrEqualityChainToInPass.h | 18 +++ src/Analyzer/QueryTreePassManager.cpp | 4 +- 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 src/Analyzer/Passes/OrEqualityChainToInPass.cpp create mode 100644 src/Analyzer/Passes/OrEqualityChainToInPass.h diff --git a/src/Analyzer/Passes/OrEqualityChainToInPass.cpp b/src/Analyzer/Passes/OrEqualityChainToInPass.cpp new file mode 100644 index 00000000000..212e8252ce4 --- /dev/null +++ b/src/Analyzer/Passes/OrEqualityChainToInPass.cpp @@ -0,0 +1,117 @@ +#include + +#include + +#include +#include +#include +#include + +namespace DB +{ + +class OrEqualityChainToInVisitor : public InDepthQueryTreeVisitorWithContext +{ +public: + using Base = InDepthQueryTreeVisitorWithContext; + + explicit OrEqualityChainToInVisitor(ContextPtr context) + : Base(std::move(context)) + {} + + + void visitImpl(QueryTreeNodePtr & node) + { + auto * function_node = node->as(); + + if (!function_node || function_node->getFunctionName() != "or") + return; + + QueryTreeNodes or_operands; + + QueryTreeNodePtrWithHashMap node_to_equals_functions; + + for (const auto & argument : function_node->getArguments()) + { + auto * argument_function = argument->as(); + if (!argument_function) + { + or_operands.push_back(argument); + continue; + } + + /// collect all equality checks (x = value) + if (argument_function->getFunctionName() != "equals") + { + or_operands.push_back(argument); + continue; + } + + const auto & equals_arguments = argument_function->getArguments().getNodes(); + const auto & lhs = equals_arguments[0]; + + const auto * rhs_literal = equals_arguments[1]->as(); + if (!rhs_literal) + { + or_operands.push_back(argument); + continue; + } + + node_to_equals_functions[lhs].push_back(argument); + } + + auto in_function_resolver = FunctionFactory::instance().get("in", getContext()); + + for (auto & [lhs, equals_functions] : node_to_equals_functions) + { + const auto & settings = getSettings(); + if (equals_functions.size() < settings.optimize_min_equality_disjunction_chain_length && !lhs.node->getResultType()->lowCardinality()) + { + std::move(equals_functions.begin(), equals_functions.end(), std::back_inserter(or_operands)); + continue; + } + + Tuple args; + args.reserve(equals_functions.size()); + /// first we create tuple from RHS of equals functions + for (const auto & equals : equals_functions) + { + const auto * equals_function = equals->as(); + assert(equals_function && equals_function->getFunctionName() == "equals"); + const auto * rhs_literal = equals_function->getArguments().getNodes()[1]->as(); + assert(rhs_literal); + args.push_back(rhs_literal->getValue()); + } + + auto rhs_node = std::make_shared(std::move(args)); + + auto in_function = std::make_shared("in"); + + QueryTreeNodes in_arguments; + in_arguments.reserve(2); + in_arguments.push_back(lhs.node); + in_arguments.push_back(std::move(rhs_node)); + + in_function->getArguments().getNodes() = std::move(in_arguments); + in_function->resolveAsFunction(in_function_resolver); + + or_operands.push_back(std::move(in_function)); + } + + if (or_operands.size() == 1) + or_operands.push_back(std::make_shared(static_cast(0))); + + auto or_function_resolver = FunctionFactory::instance().get("or", getContext()); + function_node->getArguments().getNodes() = std::move(or_operands); + function_node->resolveAsFunction(or_function_resolver); + } +}; + + +void OrEqualityChainToInPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context) +{ + OrEqualityChainToInVisitor visitor(std::move(context)); + visitor.visit(query_tree_node); +} + +} diff --git a/src/Analyzer/Passes/OrEqualityChainToInPass.h b/src/Analyzer/Passes/OrEqualityChainToInPass.h new file mode 100644 index 00000000000..86c9c9efd57 --- /dev/null +++ b/src/Analyzer/Passes/OrEqualityChainToInPass.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace DB +{ + +class OrEqualityChainToInPass final : public IQueryTreePass +{ +public: + String getName() override { return "OrEqualityChainToIn"; } + + String getDescription() override { return "Transform all the 'or's with equality check to a single IN function"; } + + void run(QueryTreeNodePtr query_tree_node, ContextPtr context) override; +}; + +} diff --git a/src/Analyzer/QueryTreePassManager.cpp b/src/Analyzer/QueryTreePassManager.cpp index 588457f90f7..e4bc919e051 100644 --- a/src/Analyzer/QueryTreePassManager.cpp +++ b/src/Analyzer/QueryTreePassManager.cpp @@ -38,6 +38,7 @@ #include #include #include +#include namespace DB { @@ -145,7 +146,6 @@ private: /** ClickHouse query tree pass manager. * * TODO: Support _shard_num into shardNum() rewriting. - * TODO: Support logical expressions optimizer. * TODO: Support setting convert_query_to_cnf. * TODO: Support setting optimize_using_constraints. * TODO: Support setting optimize_substitute_columns. @@ -262,6 +262,8 @@ void addQueryTreePasses(QueryTreePassManager & manager) manager.addPass(std::make_unique()); + manager.addPass(std::make_unique()); + manager.addPass(std::make_unique()); } From bdad6a480ed157e0727d449de413a7dac2590dab Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 21 Feb 2023 12:56:09 +0000 Subject: [PATCH 065/342] Add tests --- src/Analyzer/Passes/OrEqualityChainToInPass.h | 20 + ...00621_regression_for_in_operator.reference | 44 ++ .../00621_regression_for_in_operator.sql | 4 + .../00736_disjunction_optimisation.reference | 426 ++++++++++++++++-- .../00736_disjunction_optimisation.sql | 24 + ...ssions_optimizer_low_cardinality.reference | 42 ++ ..._expressions_optimizer_low_cardinality.sql | 2 + 7 files changed, 526 insertions(+), 36 deletions(-) diff --git a/src/Analyzer/Passes/OrEqualityChainToInPass.h b/src/Analyzer/Passes/OrEqualityChainToInPass.h index 86c9c9efd57..2a9d6818a4f 100644 --- a/src/Analyzer/Passes/OrEqualityChainToInPass.h +++ b/src/Analyzer/Passes/OrEqualityChainToInPass.h @@ -5,6 +5,26 @@ namespace DB { +/** + * This pass replaces chains of equality functions inside an OR with a single IN operator. + * The replacement is done if: + * - rhs of the equality function is a literal + * - length of chain is at least 'optimize_min_equality_disjunction_chain_length' long OR lhs is LowCardinality + * + * E.g. (optimize_min_equality_disjunction_chain_length = 2) + * ------------------------------- + * SELECT * + * FROM table + * WHERE a = 1 OR b = 'test' OR a = 2; + * + * will be transformed into + * + * SELECT * + * FROM TABLE + * WHERE b = 'test' OR a IN (1, 2); + * ------------------------------- + */ + class OrEqualityChainToInPass final : public IQueryTreePass { public: diff --git a/tests/queries/0_stateless/00621_regression_for_in_operator.reference b/tests/queries/0_stateless/00621_regression_for_in_operator.reference index 90f0a70449a..c9eb7a08fc6 100644 --- a/tests/queries/0_stateless/00621_regression_for_in_operator.reference +++ b/tests/queries/0_stateless/00621_regression_for_in_operator.reference @@ -3,3 +3,47 @@ 2 2 2 +QUERY id: 0 + PROJECTION COLUMNS + count() UInt64 + PROJECTION + LIST id: 1, nodes: 1 + FUNCTION id: 2, function_name: count, function_type: aggregate, result_type: UInt64 + JOIN TREE + TABLE id: 3, table_name: default.regression_for_in_operator_view + WHERE + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 8, column_name: g, result_type: String, source_id: 3 + CONSTANT id: 9, constant_value: Tuple_(\'5\', \'6\'), constant_value_type: Tuple(String, String) + CONSTANT id: 10, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 +2 +2 +QUERY id: 0 + PROJECTION COLUMNS + count() UInt64 + PROJECTION + LIST id: 1, nodes: 1 + FUNCTION id: 2, function_name: count, function_type: aggregate, result_type: UInt64 + JOIN TREE + TABLE id: 3, table_name: default.regression_for_in_operator_view + WHERE + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 8, column_name: g, result_type: String, source_id: 3 + CONSTANT id: 9, constant_value: \'5\', constant_value_type: String + FUNCTION id: 10, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 11, nodes: 2 + COLUMN id: 8, column_name: g, result_type: String, source_id: 3 + CONSTANT id: 12, constant_value: \'6\', constant_value_type: String + SETTINGS allow_experimental_analyzer=1 diff --git a/tests/queries/0_stateless/00621_regression_for_in_operator.sql b/tests/queries/0_stateless/00621_regression_for_in_operator.sql index 273f930a90f..db1bcb4a39a 100644 --- a/tests/queries/0_stateless/00621_regression_for_in_operator.sql +++ b/tests/queries/0_stateless/00621_regression_for_in_operator.sql @@ -12,9 +12,13 @@ SELECT count() FROM regression_for_in_operator_view WHERE g IN ('5','6'); SET optimize_min_equality_disjunction_chain_length = 1; SELECT count() FROM regression_for_in_operator_view WHERE g = '5' OR g = '6'; +SELECT count() FROM regression_for_in_operator_view WHERE g = '5' OR g = '6' SETTINGS allow_experimental_analyzer = 1; +EXPLAIN QUERY TREE SELECT count() FROM regression_for_in_operator_view WHERE g = '5' OR g = '6' SETTINGS allow_experimental_analyzer = 1; SET optimize_min_equality_disjunction_chain_length = 3; SELECT count() FROM regression_for_in_operator_view WHERE g = '5' OR g = '6'; +SELECT count() FROM regression_for_in_operator_view WHERE g = '5' OR g = '6' SETTINGS allow_experimental_analyzer = 1; +EXPLAIN QUERY TREE SELECT count() FROM regression_for_in_operator_view WHERE g = '5' OR g = '6' SETTINGS allow_experimental_analyzer = 1; DROP TABLE regression_for_in_operator_view; DROP TABLE regression_for_in_operator; diff --git a/tests/queries/0_stateless/00736_disjunction_optimisation.reference b/tests/queries/0_stateless/00736_disjunction_optimisation.reference index afd698b425e..4210571842f 100644 --- a/tests/queries/0_stateless/00736_disjunction_optimisation.reference +++ b/tests/queries/0_stateless/00736_disjunction_optimisation.reference @@ -25,6 +25,97 @@ 3 21 3 22 3 23 +QUERY id: 0 + PROJECTION COLUMNS + k UInt64 + s UInt64 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + COLUMN id: 4, column_name: s, result_type: UInt64, source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.bug + WHERE + FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 6, nodes: 2 + FUNCTION id: 7, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 8, nodes: 2 + FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 10, nodes: 2 + COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 13, constant_value: UInt64_0, constant_value_type: UInt8 + FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 15, nodes: 2 + FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 17, nodes: 2 + COLUMN id: 18, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 19, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 20, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +QUERY id: 0 + PROJECTION COLUMNS + k UInt64 + s UInt64 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + COLUMN id: 4, column_name: s, result_type: UInt64, source_id: 3 + JOIN TREE + QUERY id: 3, is_subquery: 1 + PROJECTION COLUMNS + k UInt64 + s UInt64 + PROJECTION + LIST id: 5, nodes: 2 + COLUMN id: 6, column_name: k, result_type: UInt64, source_id: 7 + COLUMN id: 8, column_name: s, result_type: UInt64, source_id: 7 + JOIN TREE + TABLE id: 7, table_name: default.bug + WHERE + FUNCTION id: 9, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 10, nodes: 2 + FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 12, nodes: 2 + COLUMN id: 13, column_name: k, result_type: UInt64, source_id: 7 + CONSTANT id: 14, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 15, constant_value: UInt64_0, constant_value_type: UInt8 + WHERE + FUNCTION id: 16, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 17, nodes: 2 + FUNCTION id: 18, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 19, nodes: 2 + COLUMN id: 20, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 22, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 1 1 21 1 1 1 1 1 22 0 1 1 1 1 23 0 0 1 @@ -34,42 +125,6 @@ 3 1 21 1 1 1 3 1 22 0 1 1 3 1 23 0 0 1 -21 1 -22 1 -23 1 -21 1 -22 1 -23 1 -21 1 -22 1 -23 1 -1 21 -1 22 -1 23 -2 21 -2 22 -2 23 -3 21 -3 22 -3 23 -1 21 -1 22 -1 23 -2 21 -2 22 -2 23 -3 21 -3 22 -3 23 -1 21 -1 22 -1 23 -2 21 -2 22 -2 23 -3 21 -3 22 -3 23 1 1 21 1 1 1 1 1 22 0 1 1 1 1 23 0 0 1 @@ -79,6 +134,53 @@ 3 1 21 1 1 1 3 1 22 0 1 1 3 1 23 0 0 1 +QUERY id: 0 + PROJECTION COLUMNS + k UInt64 + or(equals(k, 1), equals(k, 2), equals(k, 3)) UInt8 + s UInt64 + equals(s, 21) UInt8 + or(equals(s, 21), equals(s, 22)) UInt8 + or(equals(s, 21), equals(s, 22), equals(s, 23)) UInt8 + PROJECTION + LIST id: 1, nodes: 6 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 12, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 13, constant_value: UInt64_21, constant_value_type: UInt8 + FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 15, nodes: 2 + FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 17, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 18, constant_value: Tuple_(UInt64_21, UInt64_22), constant_value_type: Tuple(UInt8, UInt8) + CONSTANT id: 19, constant_value: UInt64_0, constant_value_type: UInt8 + FUNCTION id: 20, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 21, nodes: 2 + FUNCTION id: 22, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 23, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 24, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 25, constant_value: UInt64_0, constant_value_type: UInt8 + JOIN TREE + TABLE id: 3, table_name: default.bug + SETTINGS allow_experimental_analyzer=1 21 1 22 1 23 1 @@ -88,3 +190,255 @@ 21 1 22 1 23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +QUERY id: 0 + PROJECTION COLUMNS + s UInt64 + or(equals(s, 21), equals(s, 22), equals(s, 23)) UInt8 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + JOIN TREE + TABLE id: 3, table_name: default.bug + SETTINGS allow_experimental_analyzer=1 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +QUERY id: 0 + PROJECTION COLUMNS + k UInt64 + s UInt64 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + COLUMN id: 4, column_name: s, result_type: UInt64, source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.bug + WHERE + FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 6, nodes: 2 + FUNCTION id: 7, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 8, nodes: 2 + FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 10, nodes: 2 + COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 13, constant_value: UInt64_0, constant_value_type: UInt8 + FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 15, nodes: 2 + FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 17, nodes: 2 + COLUMN id: 18, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 19, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 20, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +1 21 +1 22 +1 23 +2 21 +2 22 +2 23 +3 21 +3 22 +3 23 +QUERY id: 0 + PROJECTION COLUMNS + k UInt64 + s UInt64 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + COLUMN id: 4, column_name: s, result_type: UInt64, source_id: 3 + JOIN TREE + QUERY id: 3, is_subquery: 1 + PROJECTION COLUMNS + k UInt64 + s UInt64 + PROJECTION + LIST id: 5, nodes: 2 + COLUMN id: 6, column_name: k, result_type: UInt64, source_id: 7 + COLUMN id: 8, column_name: s, result_type: UInt64, source_id: 7 + JOIN TREE + TABLE id: 7, table_name: default.bug + WHERE + FUNCTION id: 9, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 10, nodes: 2 + FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 12, nodes: 2 + COLUMN id: 13, column_name: k, result_type: UInt64, source_id: 7 + CONSTANT id: 14, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 15, constant_value: UInt64_0, constant_value_type: UInt8 + WHERE + FUNCTION id: 16, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 17, nodes: 2 + FUNCTION id: 18, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 19, nodes: 2 + COLUMN id: 20, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 22, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 +1 1 21 1 1 1 +1 1 22 0 1 1 +1 1 23 0 0 1 +2 1 21 1 1 1 +2 1 22 0 1 1 +2 1 23 0 0 1 +3 1 21 1 1 1 +3 1 22 0 1 1 +3 1 23 0 0 1 +1 1 21 1 1 1 +1 1 22 0 1 1 +1 1 23 0 0 1 +2 1 21 1 1 1 +2 1 22 0 1 1 +2 1 23 0 0 1 +3 1 21 1 1 1 +3 1 22 0 1 1 +3 1 23 0 0 1 +QUERY id: 0 + PROJECTION COLUMNS + k UInt64 + or(equals(k, 1), equals(k, 2), equals(k, 3)) UInt8 + s UInt64 + equals(s, 21) UInt8 + or(equals(s, 21), equals(s, 22)) UInt8 + or(equals(s, 21), equals(s, 22), equals(s, 23)) UInt8 + PROJECTION + LIST id: 1, nodes: 6 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 12, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 13, constant_value: UInt64_21, constant_value_type: UInt8 + FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 15, nodes: 2 + FUNCTION id: 16, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 17, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 18, constant_value: UInt64_21, constant_value_type: UInt8 + FUNCTION id: 19, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 20, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 21, constant_value: UInt64_22, constant_value_type: UInt8 + FUNCTION id: 22, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 23, nodes: 2 + FUNCTION id: 24, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 25, nodes: 2 + COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 26, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 27, constant_value: UInt64_0, constant_value_type: UInt8 + JOIN TREE + TABLE id: 3, table_name: default.bug + SETTINGS allow_experimental_analyzer=1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +QUERY id: 0 + PROJECTION COLUMNS + s UInt64 + or(equals(s, 21), equals(s, 22), equals(s, 23)) UInt8 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + JOIN TREE + TABLE id: 3, table_name: default.bug + SETTINGS allow_experimental_analyzer=1 diff --git a/tests/queries/0_stateless/00736_disjunction_optimisation.sql b/tests/queries/0_stateless/00736_disjunction_optimisation.sql index 700221ef7f0..e52ed2ab45b 100644 --- a/tests/queries/0_stateless/00736_disjunction_optimisation.sql +++ b/tests/queries/0_stateless/00736_disjunction_optimisation.sql @@ -5,17 +5,41 @@ insert into bug values(1,21),(1,22),(1,23),(2,21),(2,22),(2,23),(3,21),(3,22),(3 set optimize_min_equality_disjunction_chain_length = 2; select * from bug; + select * from bug where (k =1 or k=2 or k =3) and (s=21 or s=22 or s=23); +select * from bug where (k =1 or k=2 or k =3) and (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1; +explain query tree select * from bug where (k =1 or k=2 or k =3) and (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1;; + select * from (select * from bug where k=1 or k=2 or k=3) where (s=21 or s=22 or s=23); +select * from (select * from bug where k=1 or k=2 or k=3) where (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1;; +explain query tree select * from (select * from bug where k=1 or k=2 or k=3) where (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1;; + select k, (k=1 or k=2 or k=3), s, (s=21), (s=21 or s=22), (s=21 or s=22 or s=23) from bug; +select k, (k=1 or k=2 or k=3), s, (s=21), (s=21 or s=22), (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; +explain query tree select k, (k=1 or k=2 or k=3), s, (s=21), (s=21 or s=22), (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; + select s, (s=21 or s=22 or s=23) from bug; +select s, (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; +explain query tree select s, (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; set optimize_min_equality_disjunction_chain_length = 3; select * from bug; + select * from bug where (k =1 or k=2 or k =3) and (s=21 or s=22 or s=23); +select * from bug where (k =1 or k=2 or k =3) and (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1; +explain query tree select * from bug where (k =1 or k=2 or k =3) and (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1;; + select * from (select * from bug where k=1 or k=2 or k=3) where (s=21 or s=22 or s=23); +select * from (select * from bug where k=1 or k=2 or k=3) where (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1;; +explain query tree select * from (select * from bug where k=1 or k=2 or k=3) where (s=21 or s=22 or s=23) SETTINGS allow_experimental_analyzer = 1;; + select k, (k=1 or k=2 or k=3), s, (s=21), (s=21 or s=22), (s=21 or s=22 or s=23) from bug; +select k, (k=1 or k=2 or k=3), s, (s=21), (s=21 or s=22), (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; +explain query tree select k, (k=1 or k=2 or k=3), s, (s=21), (s=21 or s=22), (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; + select s, (s=21 or s=22 or s=23) from bug; +select s, (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; +explain query tree select s, (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; DROP TABLE bug; diff --git a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference index dcfcac737c3..086c0a49b59 100644 --- a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference +++ b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference @@ -1,6 +1,48 @@ SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a IN (\'x\', \'y\') +QUERY id: 0 + PROJECTION COLUMNS + a LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 1 + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality + WHERE + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE (b = 0) OR (b = 1) +QUERY id: 0 + PROJECTION COLUMNS + a LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 1 + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality + WHERE + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 8, column_name: b, result_type: UInt32, source_id: 3 + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + FUNCTION id: 10, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 11, nodes: 2 + COLUMN id: 8, column_name: b, result_type: UInt32, source_id: 3 + CONSTANT id: 12, constant_value: UInt64_1, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 diff --git a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql index be355a05675..f5368679f60 100644 --- a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql +++ b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql @@ -4,7 +4,9 @@ CREATE TABLE t_logical_expressions_optimizer_low_cardinality (a LowCardinality(S -- LowCardinality case, ignore optimize_min_equality_disjunction_chain_length limit, optimzer applied EXPLAIN SYNTAX SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a = 'x' OR a = 'y'; +EXPLAIN QUERY TREE SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a = 'x' OR a = 'y' SETTINGS allow_experimental_analyzer = 1; -- Non-LowCardinality case, optimizer not applied for short chains EXPLAIN SYNTAX SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE b = 0 OR b = 1; +EXPLAIN QUERY TREE SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE b = 0 OR b = 1 SETTINGS allow_experimental_analyzer = 1; DROP TABLE t_logical_expressions_optimizer_low_cardinality; From b66a5bdcf0366b751ca5ad1ecbf00cf6e80ddd6a Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Tue, 21 Feb 2023 15:13:29 +0000 Subject: [PATCH 066/342] fix mutations with sparse columns --- src/DataTypes/DataTypeTuple.cpp | 14 +++ src/DataTypes/DataTypeTuple.h | 1 + src/DataTypes/IDataType.cpp | 9 ++ src/DataTypes/IDataType.h | 1 + .../Serializations/SerializationInfo.h | 1 + .../Serializations/SerializationInfoTuple.cpp | 13 +++ .../Serializations/SerializationInfoTuple.h | 2 + src/Storages/AlterCommands.cpp | 7 +- .../MergedColumnOnlyOutputStream.cpp | 1 - src/Storages/MergeTree/MutateTask.cpp | 48 +++++++++-- ...02662_sparse_columns_mutations_1.reference | 13 +++ .../02662_sparse_columns_mutations_1.sql | 49 +++++++++++ ...02662_sparse_columns_mutations_2.reference | 6 ++ .../02662_sparse_columns_mutations_2.sql | 33 +++++++ ...02662_sparse_columns_mutations_3.reference | 11 +++ .../02662_sparse_columns_mutations_3.sql | 85 +++++++++++++++++++ 16 files changed, 280 insertions(+), 14 deletions(-) create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_1.reference create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_1.sql create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_2.reference create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_2.sql create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_3.reference create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_3.sql diff --git a/src/DataTypes/DataTypeTuple.cpp b/src/DataTypes/DataTypeTuple.cpp index 768f87fe3d4..1e28af3ee54 100644 --- a/src/DataTypes/DataTypeTuple.cpp +++ b/src/DataTypes/DataTypeTuple.cpp @@ -312,6 +312,20 @@ MutableSerializationInfoPtr DataTypeTuple::createSerializationInfo(const Seriali return std::make_shared(std::move(infos), names, settings); } +MutableSerializationInfoPtr DataTypeTuple::cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const +{ + const auto & old_info_tuple = assert_cast(old_info); + assert(old_info_tuple.getNumElements() == elems.size()); + + MutableSerializationInfos infos; + infos.reserve(elems.size()); + for (size_t i = 0; i < elems.size(); ++i) + infos.push_back(elems[i]->cloneSerializationInfo(*old_info_tuple.getElementInfo(i), settings)); + + return std::make_shared(std::move(infos), names, settings); +} + + SerializationInfoPtr DataTypeTuple::getSerializationInfo(const IColumn & column) const { if (const auto * column_const = checkAndGetColumn(&column)) diff --git a/src/DataTypes/DataTypeTuple.h b/src/DataTypes/DataTypeTuple.h index 152f21015f5..9f3860f78db 100644 --- a/src/DataTypes/DataTypeTuple.h +++ b/src/DataTypes/DataTypeTuple.h @@ -57,6 +57,7 @@ public: SerializationPtr doGetDefaultSerialization() const override; SerializationPtr getSerialization(const SerializationInfo & info) const override; MutableSerializationInfoPtr createSerializationInfo(const SerializationInfo::Settings & settings) const override; + MutableSerializationInfoPtr cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const override; SerializationInfoPtr getSerializationInfo(const IColumn & column) const override; const DataTypePtr & getElement(size_t i) const { return elems[i]; } diff --git a/src/DataTypes/IDataType.cpp b/src/DataTypes/IDataType.cpp index e0612fbbf36..5cd0b6f659c 100644 --- a/src/DataTypes/IDataType.cpp +++ b/src/DataTypes/IDataType.cpp @@ -191,6 +191,15 @@ MutableSerializationInfoPtr IDataType::createSerializationInfo(const Serializati return std::make_shared(ISerialization::Kind::DEFAULT, settings); } +MutableSerializationInfoPtr IDataType::cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const +{ + auto new_kind = old_info.getKind(); + if (new_kind == ISerialization::Kind::SPARSE && !supportsSparseSerialization()) + new_kind = ISerialization::Kind::DEFAULT; + + return std::make_shared(new_kind, settings); +} + SerializationInfoPtr IDataType::getSerializationInfo(const IColumn & column) const { if (const auto * column_const = checkAndGetColumn(&column)) diff --git a/src/DataTypes/IDataType.h b/src/DataTypes/IDataType.h index bafe03dbc3a..32e3b53fc01 100644 --- a/src/DataTypes/IDataType.h +++ b/src/DataTypes/IDataType.h @@ -104,6 +104,7 @@ public: Names getSubcolumnNames() const; virtual MutableSerializationInfoPtr createSerializationInfo(const SerializationInfo::Settings & settings) const; + virtual MutableSerializationInfoPtr cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const; virtual SerializationInfoPtr getSerializationInfo(const IColumn & column) const; /// TODO: support more types. diff --git a/src/DataTypes/Serializations/SerializationInfo.h b/src/DataTypes/Serializations/SerializationInfo.h index a4a5685253f..4ae5cf8c193 100644 --- a/src/DataTypes/Serializations/SerializationInfo.h +++ b/src/DataTypes/Serializations/SerializationInfo.h @@ -51,6 +51,7 @@ public: virtual ~SerializationInfo() = default; virtual bool hasCustomSerialization() const { return kind != ISerialization::Kind::DEFAULT; } + virtual bool structureEquals(const SerializationInfo & rhs) const { return typeid(SerializationInfo) == typeid(rhs); } virtual void add(const IColumn & column); virtual void add(const SerializationInfo & other); diff --git a/src/DataTypes/Serializations/SerializationInfoTuple.cpp b/src/DataTypes/Serializations/SerializationInfoTuple.cpp index 6c326743e8a..1a9639a1566 100644 --- a/src/DataTypes/Serializations/SerializationInfoTuple.cpp +++ b/src/DataTypes/Serializations/SerializationInfoTuple.cpp @@ -28,6 +28,19 @@ bool SerializationInfoTuple::hasCustomSerialization() const return std::any_of(elems.begin(), elems.end(), [](const auto & elem) { return elem->hasCustomSerialization(); }); } +bool SerializationInfoTuple::structureEquals(const SerializationInfo & rhs) const +{ + const auto * rhs_tuple = typeid_cast(&rhs); + if (!rhs_tuple || elems.size() != rhs_tuple->elems.size()) + return false; + + for (size_t i = 0; i < elems.size(); ++i) + if (!elems[i]->structureEquals(*rhs_tuple->elems[i])) + return false; + + return true; +} + void SerializationInfoTuple::add(const IColumn & column) { SerializationInfo::add(column); diff --git a/src/DataTypes/Serializations/SerializationInfoTuple.h b/src/DataTypes/Serializations/SerializationInfoTuple.h index b01c629d2ff..ef288948b9a 100644 --- a/src/DataTypes/Serializations/SerializationInfoTuple.h +++ b/src/DataTypes/Serializations/SerializationInfoTuple.h @@ -11,6 +11,7 @@ public: SerializationInfoTuple(MutableSerializationInfos elems_, Names names_, const Settings & settings_); bool hasCustomSerialization() const override; + bool structureEquals(const SerializationInfo & rhs) const override; void add(const IColumn & column) override; void add(const SerializationInfo & other) override; @@ -25,6 +26,7 @@ public: Poco::JSON::Object toJSON() const override; void fromJSON(const Poco::JSON::Object & object) override; + size_t getNumElements() const { return elems.size(); } const MutableSerializationInfoPtr & getElementInfo(size_t i) const { return elems[i]; } ISerialization::Kind getElementKind(size_t i) const { return elems[i]->getKind(); } diff --git a/src/Storages/AlterCommands.cpp b/src/Storages/AlterCommands.cpp index da11a87eb4d..aff17465466 100644 --- a/src/Storages/AlterCommands.cpp +++ b/src/Storages/AlterCommands.cpp @@ -719,7 +719,7 @@ bool isMetadataOnlyConversion(const IDataType * from, const IDataType * to) { typeid(DataTypeUInt16), typeid(DataTypeDate) }, }; - /// Unwrap some nested and check for valid conevrsions + /// Unwrap some nested and check for valid conversions while (true) { /// types are equal, obviously pure metadata alter @@ -749,10 +749,9 @@ bool isMetadataOnlyConversion(const IDataType * from, const IDataType * to) const auto * nullable_from = typeid_cast(from); const auto * nullable_to = typeid_cast(to); - if (nullable_to) + if (nullable_from && nullable_to) { - /// Here we allow a conversion X -> Nullable(X) to make a metadata-only conversion. - from = nullable_from ? nullable_from->getNestedType().get() : from; + from = nullable_from->getNestedType().get(); to = nullable_to->getNestedType().get(); continue; } diff --git a/src/Storages/MergeTree/MergedColumnOnlyOutputStream.cpp b/src/Storages/MergeTree/MergedColumnOnlyOutputStream.cpp index 03829f1daf9..f6fc40884a1 100644 --- a/src/Storages/MergeTree/MergedColumnOnlyOutputStream.cpp +++ b/src/Storages/MergeTree/MergedColumnOnlyOutputStream.cpp @@ -86,7 +86,6 @@ MergedColumnOnlyOutputStream::fillChecksums( } new_part->setColumns(columns, serialization_infos); - return checksums; } diff --git a/src/Storages/MergeTree/MutateTask.cpp b/src/Storages/MergeTree/MutateTask.cpp index 4b9265488e7..8d95a76b5da 100644 --- a/src/Storages/MergeTree/MutateTask.cpp +++ b/src/Storages/MergeTree/MutateTask.cpp @@ -203,20 +203,50 @@ getColumnsForNewDataPart( } SerializationInfoByName new_serialization_infos; - for (const auto & [name, info] : serialization_infos) + for (const auto & [name, old_info] : serialization_infos) { if (removed_columns.contains(name)) continue; auto it = renamed_columns_from_to.find(name); - if (it != renamed_columns_from_to.end()) - new_serialization_infos.emplace(it->second, info); - else - new_serialization_infos.emplace(name, info); + auto new_name = it == renamed_columns_from_to.end() ? name : it->second; + + if (!updated_header.has(new_name)) + { + new_serialization_infos.emplace(new_name, old_info); + continue; + } + + auto old_type = part_columns.getPhysical(name); + auto new_type = updated_header.getByName(new_name).type; + + if (!new_type->supportsSparseSerialization()) + continue; + + SerializationInfo::Settings settings + { + .ratio_of_defaults_for_sparse = source_part->storage.getSettings()->ratio_of_defaults_for_sparse_serialization, + .choose_kind = false + }; + + auto new_info = new_type->createSerializationInfo(settings); + if (!old_info->structureEquals(*new_info)) + { + new_serialization_infos.emplace(new_name, std::move(new_info)); + continue; + } + + if (!old_info->hasCustomSerialization()) + { + new_serialization_infos.emplace(new_name, old_info); + continue; + } + + new_info = new_type->cloneSerializationInfo(*old_info, settings); + new_serialization_infos.emplace(new_name, std::move(new_info)); } - /// In compact parts we read all columns, because they all stored in a - /// single file + /// In compact parts we read all columns, because they all stored in a single file if (!isWidePart(source_part) || !isFullPartStorage(source_part->getDataPartStorage())) return {updated_header.getNamesAndTypesList(), new_serialization_infos}; @@ -1221,8 +1251,8 @@ private: skip_part_indices, ctx->compression_codec, ctx->txn, - false, - false, + /*reset_columns=*/ true, + /*blocks_are_granules_size=*/ false, ctx->context->getWriteSettings()); ctx->mutating_pipeline = QueryPipelineBuilder::getPipeline(std::move(builder)); diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_1.reference b/tests/queries/0_stateless/02662_sparse_columns_mutations_1.reference new file mode 100644 index 00000000000..3f5c8b6ed1f --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_1.reference @@ -0,0 +1,13 @@ +1_1_1_0 String Sparse +477 ['','foo'] +1_1_1_0_2 Nullable(String) Default +477 ['','foo'] +1_1_1_0_2 Nullable(String) Default +2_3_3_0 Nullable(String) Default +954 ['','foo'] +1_1_1_0_4 String Default +2_3_3_0_4 String Default +954 ['','foo'] +1_1_1_1_4 String Sparse +2_3_3_1_4 String Sparse +954 ['','foo'] diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_1.sql b/tests/queries/0_stateless/02662_sparse_columns_mutations_1.sql new file mode 100644 index 00000000000..3bf37e8e62b --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_1.sql @@ -0,0 +1,49 @@ +SET mutations_sync = 2; + +DROP TABLE IF EXISTS t_sparse_mutations_1; + +CREATE TABLE t_sparse_mutations_1 (key UInt8, id UInt64, s String) +ENGINE = MergeTree ORDER BY id PARTITION BY key +SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; + +INSERT INTO t_sparse_mutations_1 SELECT 1, number, if (number % 21 = 0, 'foo', '') FROM numbers (10000); + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_1' AND column = 's' AND active +ORDER BY name; + +SELECT countIf(s = 'foo'), arraySort(groupUniqArray(s)) FROM t_sparse_mutations_1; + +ALTER TABLE t_sparse_mutations_1 MODIFY COLUMN s Nullable(String); + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_1' AND column = 's' AND active +ORDER BY name; + +SELECT countIf(s = 'foo'), arraySort(groupUniqArray(s)) FROM t_sparse_mutations_1; + +INSERT INTO t_sparse_mutations_1 SELECT 2, number, if (number % 21 = 0, 'foo', '') FROM numbers (10000); + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_1' AND column = 's' AND active +ORDER BY name; + +SELECT countIf(s = 'foo'), arraySort(groupUniqArray(s)) FROM t_sparse_mutations_1; + +ALTER TABLE t_sparse_mutations_1 MODIFY COLUMN s String; + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_1' AND column = 's' AND active +ORDER BY name; + +SELECT countIf(s = 'foo'), arraySort(groupUniqArray(s)) FROM t_sparse_mutations_1; + +OPTIMIZE TABLE t_sparse_mutations_1 FINAL; + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_1' AND column = 's' AND active +ORDER BY name; + +SELECT countIf(s = 'foo'), arraySort(groupUniqArray(s)) FROM t_sparse_mutations_1; + +DROP TABLE t_sparse_mutations_1; diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_2.reference b/tests/queries/0_stateless/02662_sparse_columns_mutations_2.reference new file mode 100644 index 00000000000..64eb0119982 --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_2.reference @@ -0,0 +1,6 @@ +String Default +10000 49995000 +String Default +770 3848845 +String Sparse +770 3848845 diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_2.sql b/tests/queries/0_stateless/02662_sparse_columns_mutations_2.sql new file mode 100644 index 00000000000..561bd164200 --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_2.sql @@ -0,0 +1,33 @@ +SET mutations_sync = 2; + +DROP TABLE IF EXISTS t_sparse_mutations_2; + +CREATE TABLE t_sparse_mutations_2 (key UInt8, id UInt64, s String) +ENGINE = MergeTree ORDER BY id PARTITION BY key +SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; + +INSERT INTO t_sparse_mutations_2 SELECT 1, number, toString(number) FROM numbers (10000); + +SELECT type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_2' AND column = 's' AND active +ORDER BY name; + +SELECT count(), sum(s::UInt64) FROM t_sparse_mutations_2 WHERE s != ''; + +ALTER TABLE t_sparse_mutations_2 UPDATE s = '' WHERE id % 13 != 0; + +SELECT type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_2' AND column = 's' AND active +ORDER BY name; + +SELECT count(), sum(s::UInt64) FROM t_sparse_mutations_2 WHERE s != ''; + +OPTIMIZE TABLE t_sparse_mutations_2 FINAL; + +SELECT type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_2' AND column = 's' AND active +ORDER BY name; + +SELECT count(), sum(s::UInt64) FROM t_sparse_mutations_2 WHERE s != ''; + +DROP TABLE t_sparse_mutations_2; diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_3.reference b/tests/queries/0_stateless/02662_sparse_columns_mutations_3.reference new file mode 100644 index 00000000000..1501fd27fd5 --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_3.reference @@ -0,0 +1,11 @@ +String Default +Tuple(UInt64, UInt64, String, String, String) Default ['1','2','3','4','5'] ['UInt64','UInt64','String','String','String'] ['Default','Default','Default','Default','Default'] +10000 0 ['1'] ['0'] [''] +Tuple(UInt64, UInt64, String, String, String) Default ['1','2','3','4','5'] ['UInt64','UInt64','String','String','String'] ['Default','Sparse','Default','Default','Sparse'] +10000 0 ['1'] ['0'] [''] +Tuple(UInt64, UInt64, UInt64, UInt64, String) Default ['1','2','3','4','5'] ['UInt64','UInt64','UInt64','UInt64','String'] ['Default','Sparse','Default','Default','Sparse'] +10000 0 10000 0 [''] +Tuple(UInt64, UInt64, UInt64, UInt64, String) Default ['1','2','3','4','5'] ['UInt64','UInt64','UInt64','UInt64','String'] ['Default','Sparse','Default','Sparse','Sparse'] +10000 0 10000 0 [''] +Tuple(Nullable(UInt64), Nullable(UInt64), Nullable(UInt64), Nullable(UInt64), Nullable(String)) Default ['1','1.null','2','2.null','3','3.null','4','4.null','5','5.null'] ['Nullable(UInt64)','UInt8','Nullable(UInt64)','UInt8','Nullable(UInt64)','UInt8','Nullable(UInt64)','UInt8','Nullable(String)','UInt8'] ['Default','Default','Default','Default','Default','Default','Default','Default','Default','Default'] +10000 0 10000 0 [''] diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_3.sql b/tests/queries/0_stateless/02662_sparse_columns_mutations_3.sql new file mode 100644 index 00000000000..6e66336dcbc --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_3.sql @@ -0,0 +1,85 @@ +SET mutations_sync = 2; + +DROP TABLE IF EXISTS t_sparse_mutations_3; + +CREATE TABLE t_sparse_mutations_3 (key UInt8, id UInt64, s String) +ENGINE = MergeTree ORDER BY id PARTITION BY key +SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; + +INSERT INTO t_sparse_mutations_3 SELECT 1, number, toString(tuple(1, 0, '1', '0', '')) FROM numbers (10000); + +SELECT type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_3' AND column = 's' AND active +ORDER BY name; + +ALTER TABLE t_sparse_mutations_3 MODIFY COLUMN s Tuple(UInt64, UInt64, String, String, String); + +SELECT + type, + serialization_kind, + subcolumns.names, + subcolumns.types, + subcolumns.serializations +FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_3' AND column = 's' AND active +ORDER BY name; + +SELECT sum(s.1), sum(s.2), groupUniqArray(s.3), groupUniqArray(s.4), groupUniqArray(s.5) FROM t_sparse_mutations_3; + +OPTIMIZE TABLE t_sparse_mutations_3 FINAL; + +SELECT + type, + serialization_kind, + subcolumns.names, + subcolumns.types, + subcolumns.serializations +FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_3' AND column = 's' AND active +ORDER BY name; + +SELECT sum(s.1), sum(s.2), groupUniqArray(s.3), groupUniqArray(s.4), groupUniqArray(s.5) FROM t_sparse_mutations_3; + +ALTER TABLE t_sparse_mutations_3 MODIFY COLUMN s Tuple(UInt64, UInt64, UInt64, UInt64, String); + +SELECT + type, + serialization_kind, + subcolumns.names, + subcolumns.types, + subcolumns.serializations +FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_3' AND column = 's' AND active +ORDER BY name; + +SELECT sum(s.1), sum(s.2), sum(s.3), sum(s.4), groupUniqArray(s.5) FROM t_sparse_mutations_3; + +OPTIMIZE TABLE t_sparse_mutations_3 FINAL; + +SELECT + type, + serialization_kind, + subcolumns.names, + subcolumns.types, + subcolumns.serializations +FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_3' AND column = 's' AND active +ORDER BY name; + +SELECT sum(s.1), sum(s.2), sum(s.3), sum(s.4), groupUniqArray(s.5) FROM t_sparse_mutations_3; + +ALTER TABLE t_sparse_mutations_3 MODIFY COLUMN s Tuple(Nullable(UInt64), Nullable(UInt64), Nullable(UInt64), Nullable(UInt64), Nullable(String)); + +SELECT + type, + serialization_kind, + subcolumns.names, + subcolumns.types, + subcolumns.serializations +FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_3' AND column = 's' AND active +ORDER BY name; + +SELECT sum(s.1), sum(s.2), sum(s.3), sum(s.4), groupUniqArray(s.5) FROM t_sparse_mutations_3; + +DROP TABLE t_sparse_mutations_3; From a110e0f0225acdc2868423b08efc8b23fe1ad697 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 21 Feb 2023 15:58:56 +0100 Subject: [PATCH 067/342] Allow to ignore errors while pushing to MATERILIZED VIEW This can be useful in the following scenarious: - you want to duplicate the data to another table and you don't care about the errors - you want to duplicate system.*_log to another server, you are adding materialized view that will push to Distributed table, but you don't want to miss original blocks in the local system.*_log - you want to push some data to a 3d party service, using i.e. URL engine. Signed-off-by: Azat Khuzhin --- .../sql-reference/statements/create/view.md | 2 ++ src/Core/Settings.h | 1 + .../Transforms/buildPushingToViewsChain.cpp | 16 ++++++++-- ...materialized_views_ignore_errors.reference | 17 +++++++++++ ...02572_materialized_views_ignore_errors.sql | 30 +++++++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference create mode 100644 tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql diff --git a/docs/en/sql-reference/statements/create/view.md b/docs/en/sql-reference/statements/create/view.md index 527b31b36a4..35242ec7a93 100644 --- a/docs/en/sql-reference/statements/create/view.md +++ b/docs/en/sql-reference/statements/create/view.md @@ -66,6 +66,8 @@ A materialized view is implemented as follows: when inserting data to the table Materialized views in ClickHouse use **column names** instead of column order during insertion into destination table. If some column names are not present in the `SELECT` query result, ClickHouse uses a default value, even if the column is not [Nullable](../../data-types/nullable.md). A safe practice would be to add aliases for every column when using Materialized views. Materialized views in ClickHouse are implemented more like insert triggers. If there’s some aggregation in the view query, it’s applied only to the batch of freshly inserted data. Any changes to existing data of source table (like update, delete, drop partition, etc.) does not change the materialized view. + +By default if the will be an error during pushing to the materialized view the data will not be inserted into the table to which the materialized view is attached. You can change this by setting `materialized_views_ignore_errors=true` setting for your `INSERT` query. ::: If you specify `POPULATE`, the existing table data is inserted into the view when creating it, as if making a `CREATE TABLE ... AS SELECT ...` . Otherwise, the query contains only the data inserted in the table after creating the view. We **do not recommend** using `POPULATE`, since data inserted in the table during the view creation will not be inserted in it. diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 3002bcc8b49..ac9a169e2a8 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -508,6 +508,7 @@ class IColumn; M(Bool, allow_experimental_alter_materialized_view_structure, false, "Allow atomic alter on Materialized views. Work in progress.", 0) \ M(Bool, enable_early_constant_folding, true, "Enable query optimization where we analyze function and subqueries results and rewrite query if there're constants there", 0) \ M(Bool, deduplicate_blocks_in_dependent_materialized_views, false, "Should deduplicate blocks for materialized views if the block is not a duplicate for the table. Use true to always deduplicate in dependent tables.", 0) \ + M(Bool, materialized_views_ignore_errors, false, "Allows to ignore errors for MATERIALIZED VIEW, and deliver original block to the table regardless of MVs", 0) \ M(Bool, use_compact_format_in_distributed_parts_names, true, "Changes format of directories names for distributed table insert parts.", 0) \ M(Bool, validate_polygons, true, "Throw exception if polygon is invalid in function pointInPolygon (e.g. self-tangent, self-intersecting). If the setting is false, the function will accept invalid polygons but may silently return wrong result.", 0) \ M(UInt64, max_parser_depth, DBMS_DEFAULT_MAX_PARSER_DEPTH, "Maximum parser depth (recursion depth of recursive descend parser).", 0) \ diff --git a/src/Processors/Transforms/buildPushingToViewsChain.cpp b/src/Processors/Transforms/buildPushingToViewsChain.cpp index a58d70a8428..154986045d9 100644 --- a/src/Processors/Transforms/buildPushingToViewsChain.cpp +++ b/src/Processors/Transforms/buildPushingToViewsChain.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -173,7 +174,7 @@ class FinalizingViewsTransform final : public IProcessor static InputPorts initPorts(std::vector headers); public: - FinalizingViewsTransform(std::vector headers, ViewsDataPtr data); + FinalizingViewsTransform(std::vector headers, ViewsDataPtr data, bool materialized_views_ignore_errors_); String getName() const override { return "FinalizingViewsTransform"; } Status prepare() override; @@ -184,6 +185,7 @@ private: ViewsDataPtr views_data; std::vector statuses; std::exception_ptr any_exception; + bool materialized_views_ignore_errors; }; @@ -407,7 +409,7 @@ Chain buildPushingToViewsChain( headers.push_back(chain.getOutputHeader()); auto copying_data = std::make_shared(storage_header, views_data); - auto finalizing_views = std::make_shared(std::move(headers), views_data); + auto finalizing_views = std::make_shared(std::move(headers), views_data, settings.materialized_views_ignore_errors); auto out = copying_data->getOutputs().begin(); auto in = finalizing_views->getInputs().begin(); @@ -684,10 +686,11 @@ void PushingToWindowViewSink::consume(Chunk chunk) } -FinalizingViewsTransform::FinalizingViewsTransform(std::vector headers, ViewsDataPtr data) +FinalizingViewsTransform::FinalizingViewsTransform(std::vector headers, ViewsDataPtr data, bool materialized_views_ignore_errors_) : IProcessor(initPorts(std::move(headers)), {Block()}) , output(outputs.front()) , views_data(std::move(data)) + , materialized_views_ignore_errors(materialized_views_ignore_errors_) { statuses.resize(views_data->views.size()); } @@ -788,6 +791,13 @@ void FinalizingViewsTransform::work() auto & status = statuses[i]; ++i; + if (status.exception && materialized_views_ignore_errors) + { + auto exception = addStorageToException(status.exception, view.table_id); + tryLogException(exception, &Poco::Logger::get("PushingToViews"), "Cannot push to the storage, ignoring the error"); + continue; + } + if (status.exception) { if (!any_exception) diff --git a/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference new file mode 100644 index 00000000000..537865e10da --- /dev/null +++ b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference @@ -0,0 +1,17 @@ +-- { echoOn } +select * from data_02572 order by key; +insert into data_02572 values (1); -- { serverError UNKNOWN_TABLE } +select * from data_02572 order by key; +1 +insert into data_02572 settings materialized_views_ignore_errors=1 values (2); +select * from data_02572 order by key; +1 +2 +create table receiver_02572 as data_02572; +insert into data_02572 values (3); +select * from data_02572 order by key; +1 +2 +3 +select * from receiver_02572 order by key; +3 diff --git a/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql new file mode 100644 index 00000000000..41fa9e32d1c --- /dev/null +++ b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql @@ -0,0 +1,30 @@ +set prefer_localhost_replica=1; + +drop table if exists data_02572; +drop table if exists proxy_02572; +drop table if exists push_to_proxy_mv_02572; +drop table if exists receiver_02572; + +create table data_02572 (key Int) engine=Memory(); + +create table proxy_02572 (key Int) engine=Distributed('test_shard_localhost', currentDatabase(), 'receiver_02572'); +-- ensure that insert fails +insert into proxy_02572 values (1); -- { serverError UNKNOWN_TABLE } + +-- proxy data with MV +create materialized view push_to_proxy_mv_02572 to proxy_02572 as select * from data_02572; + +-- { echoOn } +select * from data_02572 order by key; + +insert into data_02572 values (1); -- { serverError UNKNOWN_TABLE } +select * from data_02572 order by key; + +insert into data_02572 settings materialized_views_ignore_errors=1 values (2); +select * from data_02572 order by key; + +create table receiver_02572 as data_02572; + +insert into data_02572 values (3); +select * from data_02572 order by key; +select * from receiver_02572 order by key; From 6391e2daecad874e29ae444b24f537a64758de0e Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 21 Feb 2023 16:27:30 +0100 Subject: [PATCH 068/342] Set materialized_views_ignore_errors unconditionally for system.*_log tables Signed-off-by: Azat Khuzhin --- docs/en/sql-reference/statements/create/view.md | 2 ++ src/Interpreters/SystemLog.cpp | 2 ++ ...s_materialized_views_ignore_errors.reference | 0 ...em_logs_materialized_views_ignore_errors.sql | 17 +++++++++++++++++ 4 files changed, 21 insertions(+) create mode 100644 tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference create mode 100644 tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql diff --git a/docs/en/sql-reference/statements/create/view.md b/docs/en/sql-reference/statements/create/view.md index 35242ec7a93..5d54662326a 100644 --- a/docs/en/sql-reference/statements/create/view.md +++ b/docs/en/sql-reference/statements/create/view.md @@ -68,6 +68,8 @@ Materialized views in ClickHouse use **column names** instead of column order du Materialized views in ClickHouse are implemented more like insert triggers. If there’s some aggregation in the view query, it’s applied only to the batch of freshly inserted data. Any changes to existing data of source table (like update, delete, drop partition, etc.) does not change the materialized view. By default if the will be an error during pushing to the materialized view the data will not be inserted into the table to which the materialized view is attached. You can change this by setting `materialized_views_ignore_errors=true` setting for your `INSERT` query. + +`materialized_views_ignore_errors` set to `true` by default for `system.*_log` tables. ::: If you specify `POPULATE`, the existing table data is inserted into the view when creating it, as if making a `CREATE TABLE ... AS SELECT ...` . Otherwise, the query contains only the data inserted in the table after creating the view. We **do not recommend** using `POPULATE`, since data inserted in the table during the view creation will not be inserted in it. diff --git a/src/Interpreters/SystemLog.cpp b/src/Interpreters/SystemLog.cpp index 9d8547abcf2..78513920236 100644 --- a/src/Interpreters/SystemLog.cpp +++ b/src/Interpreters/SystemLog.cpp @@ -426,6 +426,8 @@ void SystemLog::flushImpl(const std::vector & to_flush, // we need query context to do inserts to target table with MV containing subqueries or joins auto insert_context = Context::createCopy(context); insert_context->makeQueryContext(); + /// We always want to deliver the data to the original table regardless of the MVs + insert_context->setSetting("materialized_views_ignore_errors", true); InterpreterInsertQuery interpreter(query_ptr, insert_context); BlockIO io = interpreter.execute(); diff --git a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql new file mode 100644 index 00000000000..d26f86962c4 --- /dev/null +++ b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql @@ -0,0 +1,17 @@ +-- Tags: no-parallel +-- Tag no-parallel: due to attaching to system.query_log + +-- Attach MV to system.query_log and check that writing query_log will not fail + +drop table if exists log_proxy_02572; +drop table if exists push_to_logs_proxy_mv_02572; + +create table log_proxy_02572 as system.query_log engine=Distributed('test_shard_localhost', currentDatabase(), 'receiver_02572'); +create materialized view push_to_logs_proxy_mv_02572 to log_proxy_02572 as select * from system.query_log; + +set log_queries=1; +system flush logs; +system flush logs; + +drop table log_proxy_02572; +drop table push_to_logs_proxy_mv_02572; From 81435282ec2665ba189511a9427a7ccf6692e963 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 21 Feb 2023 17:04:58 +0100 Subject: [PATCH 069/342] Add an entry to system.query_views_log regardless of materialized_views_ignore_errors Signed-off-by: Azat Khuzhin --- .../Transforms/buildPushingToViewsChain.cpp | 15 ++++++--------- ...2_materialized_views_ignore_errors.reference | 17 ++++++++++++++--- .../02572_materialized_views_ignore_errors.sql | 16 +++++++++++++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Processors/Transforms/buildPushingToViewsChain.cpp b/src/Processors/Transforms/buildPushingToViewsChain.cpp index 154986045d9..13c4806590e 100644 --- a/src/Processors/Transforms/buildPushingToViewsChain.cpp +++ b/src/Processors/Transforms/buildPushingToViewsChain.cpp @@ -737,7 +737,7 @@ IProcessor::Status FinalizingViewsTransform::prepare() else statuses[i].exception = data.exception; - if (i == 0 && statuses[0].is_first) + if (i == 0 && statuses[0].is_first && !materialized_views_ignore_errors) { output.pushData(std::move(data)); return Status::PortFull; @@ -754,7 +754,7 @@ IProcessor::Status FinalizingViewsTransform::prepare() if (!statuses.empty()) return Status::Ready; - if (any_exception) + if (any_exception && !materialized_views_ignore_errors) output.pushException(any_exception); output.finish(); @@ -791,19 +791,16 @@ void FinalizingViewsTransform::work() auto & status = statuses[i]; ++i; - if (status.exception && materialized_views_ignore_errors) - { - auto exception = addStorageToException(status.exception, view.table_id); - tryLogException(exception, &Poco::Logger::get("PushingToViews"), "Cannot push to the storage, ignoring the error"); - continue; - } - if (status.exception) { if (!any_exception) any_exception = status.exception; view.setException(addStorageToException(status.exception, view.table_id)); + + /// Exception will be ignored, it is saved here for the system.query_views_log + if (materialized_views_ignore_errors) + tryLogException(view.exception, &Poco::Logger::get("PushingToViews"), "Cannot push to the storage, ignoring the error"); } else { diff --git a/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference index 537865e10da..fc2e6b78122 100644 --- a/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference +++ b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.reference @@ -1,10 +1,21 @@ -- { echoOn } select * from data_02572 order by key; -insert into data_02572 values (1); -- { serverError UNKNOWN_TABLE } -select * from data_02572 order by key; -1 insert into data_02572 settings materialized_views_ignore_errors=1 values (2); select * from data_02572 order by key; +2 +-- check system.query_views_log +system flush logs; +-- lower(status) to pass through clickhouse-test "exception" check +select lower(status::String), errorCodeToName(exception_code) +from system.query_views_log where + view_name = concatWithSeparator('.', currentDatabase(), 'push_to_proxy_mv_02572') and + view_target = concatWithSeparator('.', currentDatabase(), 'proxy_02572') + order by event_date, event_time +; +exceptionwhileprocessing UNKNOWN_TABLE +-- materialized_views_ignore_errors=0 +insert into data_02572 values (1); -- { serverError UNKNOWN_TABLE } +select * from data_02572 order by key; 1 2 create table receiver_02572 as data_02572; diff --git a/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql index 41fa9e32d1c..2d1f824b9b1 100644 --- a/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql +++ b/tests/queries/0_stateless/02572_materialized_views_ignore_errors.sql @@ -17,11 +17,21 @@ create materialized view push_to_proxy_mv_02572 to proxy_02572 as select * from -- { echoOn } select * from data_02572 order by key; -insert into data_02572 values (1); -- { serverError UNKNOWN_TABLE } -select * from data_02572 order by key; - insert into data_02572 settings materialized_views_ignore_errors=1 values (2); select * from data_02572 order by key; +-- check system.query_views_log +system flush logs; +-- lower(status) to pass through clickhouse-test "exception" check +select lower(status::String), errorCodeToName(exception_code) +from system.query_views_log where + view_name = concatWithSeparator('.', currentDatabase(), 'push_to_proxy_mv_02572') and + view_target = concatWithSeparator('.', currentDatabase(), 'proxy_02572') + order by event_date, event_time +; + +-- materialized_views_ignore_errors=0 +insert into data_02572 values (1); -- { serverError UNKNOWN_TABLE } +select * from data_02572 order by key; create table receiver_02572 as data_02572; From 83b2f6434107be0852b07b466faeb42f77e8496f Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 21 Feb 2023 17:23:26 +0100 Subject: [PATCH 070/342] Improve 02572_system_logs_materialized_views_ignore_errors test Signed-off-by: Azat Khuzhin --- ...tem_logs_materialized_views_ignore_errors.reference | 2 ++ ...72_system_logs_materialized_views_ignore_errors.sql | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference index e69de29bb2d..d5446e756a3 100644 --- a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference +++ b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.reference @@ -0,0 +1,2 @@ +10 querystart OK +10 queryfinish OK diff --git a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql index d26f86962c4..b7362ac1c33 100644 --- a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql +++ b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql @@ -3,15 +3,23 @@ -- Attach MV to system.query_log and check that writing query_log will not fail +set log_queries=1; + drop table if exists log_proxy_02572; drop table if exists push_to_logs_proxy_mv_02572; create table log_proxy_02572 as system.query_log engine=Distributed('test_shard_localhost', currentDatabase(), 'receiver_02572'); create materialized view push_to_logs_proxy_mv_02572 to log_proxy_02572 as select * from system.query_log; -set log_queries=1; +select 1 format Null; system flush logs; system flush logs; drop table log_proxy_02572; drop table push_to_logs_proxy_mv_02572; + +system flush logs; +-- lower() to pass through clickhouse-test "exception" check +select count(), lower(type::String), errorCodeToName(exception_code) + from system.query_log + where current_database = currentDatabase() group by 2, 3; From 4f8da66bb51c0e5f59674b2e2f86dfe49840fe08 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Tue, 21 Feb 2023 18:25:10 +0000 Subject: [PATCH 071/342] fix unit test with sparse columns --- src/Columns/ColumnSparse.cpp | 1 + src/Columns/tests/gtest_column_sparse.cpp | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Columns/ColumnSparse.cpp b/src/Columns/ColumnSparse.cpp index 0e408fca467..fbab61c879d 100644 --- a/src/Columns/ColumnSparse.cpp +++ b/src/Columns/ColumnSparse.cpp @@ -456,6 +456,7 @@ void ColumnSparse::compareColumn(const IColumn & rhs, size_t rhs_row_num, nullptr, nested_result, direction, nan_direction_hint); const auto & offsets_data = getOffsetsData(); + compare_results.resize(size()); std::fill(compare_results.begin(), compare_results.end(), nested_result[0]); for (size_t i = 0; i < offsets_data.size(); ++i) compare_results[offsets_data[i]] = nested_result[i + 1]; diff --git a/src/Columns/tests/gtest_column_sparse.cpp b/src/Columns/tests/gtest_column_sparse.cpp index b5b226c6862..6062ea51941 100644 --- a/src/Columns/tests/gtest_column_sparse.cpp +++ b/src/Columns/tests/gtest_column_sparse.cpp @@ -327,4 +327,3 @@ TEST(ColumnSparse, GetPermutation) } #undef DUMP_COLUMN -#undef DUMP_NON_DEFAULTS From 3a58951f6446b3aeca13cdc671322f459c4f77a7 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 21 Feb 2023 19:10:43 +0100 Subject: [PATCH 072/342] Improve documentation for materialized_views_ignore_errors Signed-off-by: Azat Khuzhin --- docs/en/sql-reference/statements/create/view.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/en/sql-reference/statements/create/view.md b/docs/en/sql-reference/statements/create/view.md index 5d54662326a..6e412c13291 100644 --- a/docs/en/sql-reference/statements/create/view.md +++ b/docs/en/sql-reference/statements/create/view.md @@ -67,9 +67,11 @@ Materialized views in ClickHouse use **column names** instead of column order du Materialized views in ClickHouse are implemented more like insert triggers. If there’s some aggregation in the view query, it’s applied only to the batch of freshly inserted data. Any changes to existing data of source table (like update, delete, drop partition, etc.) does not change the materialized view. -By default if the will be an error during pushing to the materialized view the data will not be inserted into the table to which the materialized view is attached. You can change this by setting `materialized_views_ignore_errors=true` setting for your `INSERT` query. +Materialized views in ClickHouse does not have deterministic behaviour in case of errors. This means that blocks that had been already written will be preserved in the destination table, but all blocks after error will not. -`materialized_views_ignore_errors` set to `true` by default for `system.*_log` tables. +By default if pushing to one of views fails, then the INSERT query will fail too, and some blocks may not be written to the destination table. This can be changed using `materialized_views_ignore_errors` setting (you should set it for `INSERT` query), if you will set `materialized_views_ignore_errors=true`, then any errors while pushing to views will be ignored and all blocks will be written to the destination table. + +Also note, that `materialized_views_ignore_errors` set to `true` by default for `system.*_log` tables. ::: If you specify `POPULATE`, the existing table data is inserted into the view when creating it, as if making a `CREATE TABLE ... AS SELECT ...` . Otherwise, the query contains only the data inserted in the table after creating the view. We **do not recommend** using `POPULATE`, since data inserted in the table during the view creation will not be inserted in it. From f5c232707d7e948776a67e761bcf65e19d08e05f Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Tue, 21 Feb 2023 20:04:40 +0100 Subject: [PATCH 073/342] cancel insert queries correctly --- .../PushingAsyncPipelineExecutor.cpp | 4 +- .../Executors/PushingPipelineExecutor.cpp | 4 +- src/Server/GRPCServer.cpp | 5 +- src/Server/TCPHandler.cpp | 15 ++++- src/Storages/StorageReplicatedMergeTree.cpp | 4 +- ...ert_cancellation_native_protocol.reference | 2 + ...434_insert_cancellation_native_protocol.sh | 65 +++++++++++++++++++ 7 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference create mode 100755 tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh diff --git a/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp b/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp index 54c1e7bf30f..20f47c6b54c 100644 --- a/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp +++ b/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp @@ -139,9 +139,11 @@ PushingAsyncPipelineExecutor::PushingAsyncPipelineExecutor(QueryPipeline & pipel PushingAsyncPipelineExecutor::~PushingAsyncPipelineExecutor() { + /// It must be finalized explicitly. Otherwise we cancel it assuming it's due to an exception. + chassert(finished || std::uncaught_exceptions()); try { - finish(); + cancel(); } catch (...) { diff --git a/src/Processors/Executors/PushingPipelineExecutor.cpp b/src/Processors/Executors/PushingPipelineExecutor.cpp index d9a14704cd0..ca60932ed6f 100644 --- a/src/Processors/Executors/PushingPipelineExecutor.cpp +++ b/src/Processors/Executors/PushingPipelineExecutor.cpp @@ -63,9 +63,11 @@ PushingPipelineExecutor::PushingPipelineExecutor(QueryPipeline & pipeline_) : pi PushingPipelineExecutor::~PushingPipelineExecutor() { + /// It must be finalized explicitly. Otherwise we cancel it assuming it's due to an exception. + chassert(finished || std::uncaught_exceptions()); try { - finish(); + cancel(); } catch (...) { diff --git a/src/Server/GRPCServer.cpp b/src/Server/GRPCServer.cpp index 595f5a8c2b7..9a4e43b2fde 100644 --- a/src/Server/GRPCServer.cpp +++ b/src/Server/GRPCServer.cpp @@ -984,7 +984,10 @@ namespace executor.push(block); } - executor.finish(); + if (isQueryCancelled()) + executor.cancel(); + else + executor.finish(); } void Call::initializePipeline(const Block & header) diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index f76c342fa9a..f1340764e00 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -109,6 +109,7 @@ namespace ErrorCodes extern const int UNEXPECTED_PACKET_FROM_CLIENT; extern const int UNKNOWN_PROTOCOL; extern const int AUTHENTICATION_FAILED; + extern const int QUERY_WAS_CANCELLED; } TCPHandler::TCPHandler(IServer & server_, TCPServer & tcp_server_, const Poco::Net::StreamSocket & socket_, bool parse_proxy_protocol_, std::string server_display_name_) @@ -658,6 +659,7 @@ bool TCPHandler::readDataNext() { LOG_INFO(log, "Client has dropped the connection, cancel the query."); state.is_connection_closed = true; + state.is_cancelled = true; break; } @@ -701,6 +703,9 @@ void TCPHandler::readData() while (readDataNext()) ; + + if (state.is_cancelled) + throw Exception(ErrorCodes::QUERY_WAS_CANCELLED, "Query was cancelled"); } @@ -711,6 +716,9 @@ void TCPHandler::skipData() while (readDataNext()) ; + + if (state.is_cancelled) + throw Exception(ErrorCodes::QUERY_WAS_CANCELLED, "Query was cancelled"); } @@ -747,7 +755,10 @@ void TCPHandler::processInsertQuery() while (readDataNext()) executor.push(std::move(state.block_for_insert)); - executor.finish(); + if (state.is_cancelled) + executor.cancel(); + else + executor.finish(); }; if (num_threads > 1) @@ -1314,6 +1325,8 @@ bool TCPHandler::receivePacket() std::this_thread::sleep_for(ms); } + state.is_cancelled = true; + return false; } diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index db99007cb4f..b8e889d6182 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -794,7 +794,7 @@ bool StorageReplicatedMergeTree::createTableIfNotExists(const StorageMetadataPtr auto code = zookeeper->tryMulti(ops, responses); if (code == Coordination::Error::ZNODEEXISTS) { - LOG_WARNING(log, "It looks like the table {} was created by another server at the same moment, will retry", zookeeper_path); + LOG_INFO(log, "It looks like the table {} was created by another server at the same moment, will retry", zookeeper_path); continue; } else if (code != Coordination::Error::ZOK) @@ -874,7 +874,7 @@ void StorageReplicatedMergeTree::createReplica(const StorageMetadataPtr & metada case Coordination::Error::ZNODEEXISTS: throw Exception(ErrorCodes::REPLICA_ALREADY_EXISTS, "Replica {} already exists", replica_path); case Coordination::Error::ZBADVERSION: - LOG_ERROR(log, "Retrying createReplica(), because some other replicas were created at the same time"); + LOG_INFO(log, "Retrying createReplica(), because some other replicas were created at the same time"); break; case Coordination::Error::ZNONODE: throw Exception(ErrorCodes::ALL_REPLICAS_LOST, "Table {} was suddenly removed", zookeeper_path); diff --git a/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference b/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference new file mode 100644 index 00000000000..e3038e03530 --- /dev/null +++ b/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference @@ -0,0 +1,2 @@ +5000000 +5000000 diff --git a/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh b/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh new file mode 100755 index 00000000000..4eb02c38c1a --- /dev/null +++ b/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2009 + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +export DATA_FILE="$CLICKHOUSE_TMP/deduptest.tsv" +export TEST_MARK="02434_insert_${CLICKHOUSE_DATABASE}_" + +$CLICKHOUSE_CLIENT -q 'select * from numbers(5000000) format TSV' > $DATA_FILE +$CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by A settings non_replicated_deduplication_window=1000;' +$CLICKHOUSE_CLIENT --max_block_size=100000 --min_chunk_bytes_for_parallel_parsing=10000 -q 'insert into dedup_test format TSV' < $DATA_FILE +$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' + +function thread_insert +{ + # supress "Killed" messages from bash + function wrap + { + $CLICKHOUSE_CLIENT --max_block_size=100000 --min_chunk_bytes_for_parallel_parsing=10000 --query_id="$ID" -q 'insert into dedup_test format TSV' < $DATA_FILE + } + export -f wrap + while true; do + export ID="$TEST_MARK$RANDOM" + bash -c wrap 2>&1| grep -Fav "Killed" + done +} + +function thread_select +{ + while true; do + $CLICKHOUSE_CLIENT -q "with (select count() from dedup_test) as c select throwIf(c != 5000000, 'Expected 5000000 rows, got ' || toString(c)) format Null" + sleep 0.$RANDOM; + done +} + +function thread_cancel +{ + while true; do + SIGNAL="INT" + if (( RANDOM % 2 )); then + SIGNAL="KILL" + fi + PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') + if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID" || echo "$PID"; fi + sleep 0.$RANDOM; + sleep 0.$RANDOM; + sleep 0.$RANDOM; + done +} + +export -f thread_insert; +export -f thread_select; +export -f thread_cancel; + +TIMEOUT=30 + +timeout $TIMEOUT bash -c thread_insert & +timeout $TIMEOUT bash -c thread_select & +timeout $TIMEOUT bash -c thread_cancel & + +wait + +$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' From 592af6d652b69c8e5b18e2a8578ac76cbfcffe71 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Wed, 22 Feb 2023 02:34:03 +0100 Subject: [PATCH 074/342] fix incomplete interst through http --- src/Common/StatusFile.cpp | 2 +- src/Core/MySQL/IMySQLReadPacket.cpp | 4 +- src/IO/LimitReadBuffer.cpp | 24 +++++-- src/IO/LimitReadBuffer.h | 8 ++- src/Interpreters/executeQuery.cpp | 2 +- src/Server/HTTP/HTTPServerRequest.cpp | 15 +++- src/Server/MySQLHandler.cpp | 2 +- src/Server/TCPHandler.cpp | 2 +- src/Storages/StorageLog.cpp | 2 +- .../02435_insert_cancellation_http.reference | 2 + .../02435_insert_cancellation_http.sh | 69 +++++++++++++++++++ 11 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 tests/queries/0_stateless/02435_insert_cancellation_http.reference create mode 100755 tests/queries/0_stateless/02435_insert_cancellation_http.sh diff --git a/src/Common/StatusFile.cpp b/src/Common/StatusFile.cpp index d90d50ff96d..e71056fcaa3 100644 --- a/src/Common/StatusFile.cpp +++ b/src/Common/StatusFile.cpp @@ -51,7 +51,7 @@ StatusFile::StatusFile(std::string path_, FillFunction fill_) std::string contents; { ReadBufferFromFile in(path, 1024); - LimitReadBuffer limit_in(in, 1024, false); + LimitReadBuffer limit_in(in, 1024, /* trow_exception */ false, /* exact_limit */ false); readStringUntilEOF(contents, limit_in); } diff --git a/src/Core/MySQL/IMySQLReadPacket.cpp b/src/Core/MySQL/IMySQLReadPacket.cpp index 74f309d0294..c99ae11688b 100644 --- a/src/Core/MySQL/IMySQLReadPacket.cpp +++ b/src/Core/MySQL/IMySQLReadPacket.cpp @@ -33,13 +33,13 @@ void IMySQLReadPacket::readPayloadWithUnpacked(ReadBuffer & in) void LimitedReadPacket::readPayload(ReadBuffer &in, uint8_t &sequence_id) { - LimitReadBuffer limited(in, 10000, true, "too long MySQL packet."); + LimitReadBuffer limited(in, 10000, /* trow_exception */ true, /* exact_limit */ false, "too long MySQL packet."); IMySQLReadPacket::readPayload(limited, sequence_id); } void LimitedReadPacket::readPayloadWithUnpacked(ReadBuffer & in) { - LimitReadBuffer limited(in, 10000, true, "too long MySQL packet."); + LimitReadBuffer limited(in, 10000, /* trow_exception */ true, /* exact_limit */ false, "too long MySQL packet."); IMySQLReadPacket::readPayloadWithUnpacked(limited); } diff --git a/src/IO/LimitReadBuffer.cpp b/src/IO/LimitReadBuffer.cpp index 6b3c383c753..96e2c359b58 100644 --- a/src/IO/LimitReadBuffer.cpp +++ b/src/IO/LimitReadBuffer.cpp @@ -9,6 +9,7 @@ namespace DB namespace ErrorCodes { extern const int LIMIT_EXCEEDED; + extern const int CANNOT_READ_ALL_DATA; } @@ -21,14 +22,22 @@ bool LimitReadBuffer::nextImpl() if (bytes >= limit) { + if (exact_limit && bytes == limit) + return false; + + if (exact_limit && bytes != limit) + throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected data, got {} bytes, expected {}", bytes, limit); + if (throw_exception) throw Exception(ErrorCodes::LIMIT_EXCEEDED, "Limit for LimitReadBuffer exceeded: {}", exception_message); - else - return false; + + return false; } if (!in->next()) { + if (exact_limit && bytes != limit) + throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected EOF, got {} of {} bytes", bytes, limit); /// Clearing the buffer with existing data. set(in->position(), 0); return false; @@ -43,12 +52,13 @@ bool LimitReadBuffer::nextImpl() } -LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, std::string exception_message_) +LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_) : ReadBuffer(in_ ? in_->position() : nullptr, 0) , in(in_) , owns_in(owns) , limit(limit_) , throw_exception(throw_exception_) + , exact_limit(exact_limit_) , exception_message(std::move(exception_message_)) { assert(in); @@ -61,14 +71,14 @@ LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, boo } -LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, std::string exception_message_) - : LimitReadBuffer(&in_, false, limit_, throw_exception_, exception_message_) +LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_) + : LimitReadBuffer(&in_, false, limit_, throw_exception_, exact_limit_, exception_message_) { } -LimitReadBuffer::LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, std::string exception_message_) - : LimitReadBuffer(in_.release(), true, limit_, throw_exception_, exception_message_) +LimitReadBuffer::LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_) + : LimitReadBuffer(in_.release(), true, limit_, throw_exception_, exact_limit_, exception_message_) { } diff --git a/src/IO/LimitReadBuffer.h b/src/IO/LimitReadBuffer.h index 92e5fbb0aa6..0f99bf2a08d 100644 --- a/src/IO/LimitReadBuffer.h +++ b/src/IO/LimitReadBuffer.h @@ -13,8 +13,9 @@ namespace DB class LimitReadBuffer : public ReadBuffer { public: - LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, std::string exception_message_ = {}); - LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, std::string exception_message_ = {}); + LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_ = {}); + LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, + std::string exception_message_ = {}); ~LimitReadBuffer() override; private: @@ -23,9 +24,10 @@ private: UInt64 limit; bool throw_exception; + bool exact_limit; std::string exception_message; - LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, std::string exception_message_); + LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_); bool nextImpl() override; }; diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 3bb46462353..aeaacf35051 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -1237,7 +1237,7 @@ void executeQuery( /// If not - copy enough data into 'parse_buf'. WriteBufferFromVector> out(parse_buf); - LimitReadBuffer limit(istr, max_query_size + 1, false); + LimitReadBuffer limit(istr, max_query_size + 1, /* trow_exception */ false, /* exact_limit */ false); copyData(limit, out); out.finalize(); diff --git a/src/Server/HTTP/HTTPServerRequest.cpp b/src/Server/HTTP/HTTPServerRequest.cpp index a82eb95aee1..d2e19d594dc 100644 --- a/src/Server/HTTP/HTTPServerRequest.cpp +++ b/src/Server/HTTP/HTTPServerRequest.cpp @@ -12,6 +12,8 @@ #include #include +#include + #if USE_SSL #include #include @@ -44,12 +46,23 @@ HTTPServerRequest::HTTPServerRequest(HTTPContextPtr context, HTTPServerResponse readRequest(*in); /// Try parse according to RFC7230 + /// If a client crashes, most systems will gracefully terminate the connection with FIN just like it's done on close(). + /// So we will get 0 from recv(...) and will not be able to understand that something went wrong (well, we probably + /// will get RST later on attempt to write to the socket that closed on the other side, but it will happen when the query is finished). + /// If we are extremely unlucky and data format is TSV, for example, then we may stop parsing exactly between rows + /// and decide that it's EOF (but it is not). It may break deduplication, because clients cannot control it + /// and retry with exactly the same (incomplete) set of rows. + /// That's why we have to check body size if it's provided. if (getChunkedTransferEncoding()) stream = std::make_unique(std::move(in), context->getMaxChunkSize()); else if (hasContentLength()) - stream = std::make_unique(std::move(in), getContentLength(), false); + stream = std::make_unique(std::move(in), getContentLength(), /* trow_exception */ true, /* exact_limit */ true); else if (getMethod() != HTTPRequest::HTTP_GET && getMethod() != HTTPRequest::HTTP_HEAD && getMethod() != HTTPRequest::HTTP_DELETE) + { stream = std::move(in); + LOG_WARNING(&Poco::Logger::get("HTTPServerRequest"), "Got an HTTP request with no content length, " + "it may be impossible to distinguish graceful EOF from abnormal connection loss"); + } else /// We have to distinguish empty buffer and nullptr. stream = std::make_unique(); diff --git a/src/Server/MySQLHandler.cpp b/src/Server/MySQLHandler.cpp index 3715d658730..279896da9fa 100644 --- a/src/Server/MySQLHandler.cpp +++ b/src/Server/MySQLHandler.cpp @@ -155,7 +155,7 @@ void MySQLHandler::run() payload.readStrict(command); // For commands which are executed without MemoryTracker. - LimitReadBuffer limited_payload(payload, 10000, true, "too long MySQL packet."); + LimitReadBuffer limited_payload(payload, 10000, /* trow_exception */ true, /* exact_limit */ false, "too long MySQL packet."); LOG_DEBUG(log, "Received command: {}. Connection id: {}.", static_cast(static_cast(command)), connection_id); diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index f1340764e00..3e89d1810c7 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -1057,7 +1057,7 @@ bool TCPHandler::receiveProxyHeader() /// Only PROXYv1 is supported. /// Validation of protocol is not fully performed. - LimitReadBuffer limit_in(*in, 107, true); /// Maximum length from the specs. + LimitReadBuffer limit_in(*in, 107, /* trow_exception */ true, /* exact_limit */ false); /// Maximum length from the specs. assertString("PROXY ", limit_in); diff --git a/src/Storages/StorageLog.cpp b/src/Storages/StorageLog.cpp index f2eca42ed0b..388faf1aeb5 100644 --- a/src/Storages/StorageLog.cpp +++ b/src/Storages/StorageLog.cpp @@ -118,7 +118,7 @@ private: if (limited_by_file_size) { - limited.emplace(*plain, file_size - offset, false); + limited.emplace(*plain, file_size - offset, /* trow_exception */ false, /* exact_limit */ false); compressed.emplace(*limited); } else diff --git a/tests/queries/0_stateless/02435_insert_cancellation_http.reference b/tests/queries/0_stateless/02435_insert_cancellation_http.reference new file mode 100644 index 00000000000..e3038e03530 --- /dev/null +++ b/tests/queries/0_stateless/02435_insert_cancellation_http.reference @@ -0,0 +1,2 @@ +5000000 +5000000 diff --git a/tests/queries/0_stateless/02435_insert_cancellation_http.sh b/tests/queries/0_stateless/02435_insert_cancellation_http.sh new file mode 100755 index 00000000000..653ec59f491 --- /dev/null +++ b/tests/queries/0_stateless/02435_insert_cancellation_http.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2009 + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +export DATA_FILE="$CLICKHOUSE_TMP/deduptest.tsv" +export TEST_MARK="02435_insert_${CLICKHOUSE_DATABASE}_" + +$CLICKHOUSE_CLIENT -q 'select * from numbers(5000000) format TSV' > $DATA_FILE +$CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by A settings non_replicated_deduplication_window=1000;' +$CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&max_insert_block_size=100000&input_format_parallel_parsing=0&query=insert+into+dedup_test+format+TSV" < $DATA_FILE +$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' + +function thread_insert +{ + # supress "Killed" messages from bash + function wrap + { + if (( RANDOM % 2 )); then + $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&query_id=$ID&max_insert_block_size=100000&input_format_parallel_parsing=0&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + else + $CLICKHOUSE_CURL -sS -X POST -H "Transfer-Encoding: chunked" --data-binary @- "$CLICKHOUSE_URL&query_id=$ID&max_insert_block_size=100000&input_format_parallel_parsing=0&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + fi + } + export -f wrap + while true; do + export ID="$TEST_MARK$RANDOM" + bash -c wrap 2>&1| grep -Fav "Killed" + done +} + +function thread_select +{ + while true; do + $CLICKHOUSE_CLIENT -q "with (select count() from dedup_test) as c select throwIf(c != 5000000, 'Expected 5000000 rows, got ' || toString(c)) format Null" + sleep 0.$RANDOM; + done +} + +function thread_cancel +{ + while true; do + SIGNAL="INT" + if (( RANDOM % 2 )); then + SIGNAL="KILL" + fi + PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') + if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID" || echo "$PID"; fi + sleep 0.$RANDOM; + sleep 0.$RANDOM; + sleep 0.$RANDOM; + done +} + +export -f thread_insert; +export -f thread_select; +export -f thread_cancel; + +TIMEOUT=30 + +timeout $TIMEOUT bash -c thread_insert & +timeout $TIMEOUT bash -c thread_select & +timeout $TIMEOUT bash -c thread_cancel & + +wait + +$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' From a3d5a76b37252e93f929f4737753f5fb9a156cc0 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 22 Feb 2023 08:07:16 +0100 Subject: [PATCH 075/342] Do not run 02572_system_logs_materialized_views_ignore_errors for Replicated CI: https://s3.amazonaws.com/clickhouse-test-reports/46658/3a58951f6446b3aeca13cdc671322f459c4f77a7/stateless_tests__release__databasereplicated__[1/4].html Signed-off-by: Azat Khuzhin --- .../02572_system_logs_materialized_views_ignore_errors.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql index b7362ac1c33..9568bc7af1a 100644 --- a/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql +++ b/tests/queries/0_stateless/02572_system_logs_materialized_views_ignore_errors.sql @@ -1,5 +1,6 @@ --- Tags: no-parallel +-- Tags: no-parallel, no-replicated-database -- Tag no-parallel: due to attaching to system.query_log +-- Tag no-replicated-database: Replicated database will has extra queries -- Attach MV to system.query_log and check that writing query_log will not fail From d0b50adffa98b6db67586098430d8f08f970e174 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 22 Feb 2023 11:00:14 +0100 Subject: [PATCH 076/342] Remove materialized_views_ignore_errors from FinalizingViewsTransform Signed-off-by: Azat Khuzhin --- .../Transforms/buildPushingToViewsChain.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Processors/Transforms/buildPushingToViewsChain.cpp b/src/Processors/Transforms/buildPushingToViewsChain.cpp index 13c4806590e..dce2c61457c 100644 --- a/src/Processors/Transforms/buildPushingToViewsChain.cpp +++ b/src/Processors/Transforms/buildPushingToViewsChain.cpp @@ -174,7 +174,7 @@ class FinalizingViewsTransform final : public IProcessor static InputPorts initPorts(std::vector headers); public: - FinalizingViewsTransform(std::vector headers, ViewsDataPtr data, bool materialized_views_ignore_errors_); + FinalizingViewsTransform(std::vector headers, ViewsDataPtr data); String getName() const override { return "FinalizingViewsTransform"; } Status prepare() override; @@ -185,7 +185,6 @@ private: ViewsDataPtr views_data; std::vector statuses; std::exception_ptr any_exception; - bool materialized_views_ignore_errors; }; @@ -409,7 +408,7 @@ Chain buildPushingToViewsChain( headers.push_back(chain.getOutputHeader()); auto copying_data = std::make_shared(storage_header, views_data); - auto finalizing_views = std::make_shared(std::move(headers), views_data, settings.materialized_views_ignore_errors); + auto finalizing_views = std::make_shared(std::move(headers), views_data); auto out = copying_data->getOutputs().begin(); auto in = finalizing_views->getInputs().begin(); @@ -686,11 +685,10 @@ void PushingToWindowViewSink::consume(Chunk chunk) } -FinalizingViewsTransform::FinalizingViewsTransform(std::vector headers, ViewsDataPtr data, bool materialized_views_ignore_errors_) +FinalizingViewsTransform::FinalizingViewsTransform(std::vector headers, ViewsDataPtr data) : IProcessor(initPorts(std::move(headers)), {Block()}) , output(outputs.front()) , views_data(std::move(data)) - , materialized_views_ignore_errors(materialized_views_ignore_errors_) { statuses.resize(views_data->views.size()); } @@ -712,6 +710,7 @@ IProcessor::Status FinalizingViewsTransform::prepare() if (!output.canPush()) return Status::PortFull; + bool materialized_views_ignore_errors = views_data->context->getSettingsRef().materialized_views_ignore_errors; size_t num_finished = 0; size_t pos = 0; for (auto & input : inputs) @@ -785,6 +784,8 @@ static std::exception_ptr addStorageToException(std::exception_ptr ptr, const St void FinalizingViewsTransform::work() { + bool materialized_views_ignore_errors = views_data->context->getSettingsRef().materialized_views_ignore_errors; + size_t i = 0; for (auto & view : views_data->views) { From e660c0838ca7ddf597f0a7d54b52e305b482a474 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Wed, 22 Feb 2023 17:54:35 +0100 Subject: [PATCH 077/342] fix multipart requests --- src/Common/StatusFile.cpp | 2 +- src/Core/ExternalTable.cpp | 6 ++-- src/Core/MySQL/IMySQLReadPacket.cpp | 4 +-- src/IO/LimitReadBuffer.cpp | 19 +++++++----- src/IO/LimitReadBuffer.h | 9 +++--- src/IO/examples/limit_read_buffer.cpp | 4 +-- src/IO/examples/limit_read_buffer2.cpp | 6 ++-- src/Interpreters/executeQuery.cpp | 2 +- src/Server/HTTP/HTMLForm.cpp | 16 ++++++++-- src/Server/HTTP/HTMLForm.h | 3 ++ src/Server/HTTP/HTTPServerRequest.cpp | 11 +++++-- src/Server/MySQLHandler.cpp | 2 +- src/Server/TCPHandler.cpp | 2 +- src/Storages/StorageLog.cpp | 2 +- .../02435_insert_cancellation_http.sh | 30 ++++++++++++------- 15 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/Common/StatusFile.cpp b/src/Common/StatusFile.cpp index e71056fcaa3..a9ffce7ddf8 100644 --- a/src/Common/StatusFile.cpp +++ b/src/Common/StatusFile.cpp @@ -51,7 +51,7 @@ StatusFile::StatusFile(std::string path_, FillFunction fill_) std::string contents; { ReadBufferFromFile in(path, 1024); - LimitReadBuffer limit_in(in, 1024, /* trow_exception */ false, /* exact_limit */ false); + LimitReadBuffer limit_in(in, 1024, /* trow_exception */ false, /* exact_limit */ {}); readStringUntilEOF(contents, limit_in); } diff --git a/src/Core/ExternalTable.cpp b/src/Core/ExternalTable.cpp index 217f8808185..36dcc677589 100644 --- a/src/Core/ExternalTable.cpp +++ b/src/Core/ExternalTable.cpp @@ -34,7 +34,7 @@ ExternalTableDataPtr BaseExternalTable::getData(ContextPtr context) { initReadBuffer(); initSampleBlock(); - auto input = context->getInputFormat(format, *read_buffer, sample_block, DEFAULT_BLOCK_SIZE); + auto input = context->getInputFormat(format, *read_buffer, sample_block, context->getSettingsRef().get("max_block_size").get()); auto data = std::make_unique(); data->pipe = std::make_unique(); @@ -135,7 +135,9 @@ void ExternalTablesHandler::handlePart(const Poco::Net::MessageHeader & header, if (settings.http_max_multipart_form_data_size) read_buffer = std::make_unique( stream, settings.http_max_multipart_form_data_size, - true, "the maximum size of multipart/form-data. This limit can be tuned by 'http_max_multipart_form_data_size' setting"); + /* trow_exception */ true, /* exact_limit */ std::optional(), + "the maximum size of multipart/form-data. " + "This limit can be tuned by 'http_max_multipart_form_data_size' setting"); else read_buffer = wrapReadBufferReference(stream); diff --git a/src/Core/MySQL/IMySQLReadPacket.cpp b/src/Core/MySQL/IMySQLReadPacket.cpp index c99ae11688b..39b2e5bbfb5 100644 --- a/src/Core/MySQL/IMySQLReadPacket.cpp +++ b/src/Core/MySQL/IMySQLReadPacket.cpp @@ -33,13 +33,13 @@ void IMySQLReadPacket::readPayloadWithUnpacked(ReadBuffer & in) void LimitedReadPacket::readPayload(ReadBuffer &in, uint8_t &sequence_id) { - LimitReadBuffer limited(in, 10000, /* trow_exception */ true, /* exact_limit */ false, "too long MySQL packet."); + LimitReadBuffer limited(in, 10000, /* trow_exception */ true, /* exact_limit */ {}, "too long MySQL packet."); IMySQLReadPacket::readPayload(limited, sequence_id); } void LimitedReadPacket::readPayloadWithUnpacked(ReadBuffer & in) { - LimitReadBuffer limited(in, 10000, /* trow_exception */ true, /* exact_limit */ false, "too long MySQL packet."); + LimitReadBuffer limited(in, 10000, /* trow_exception */ true, /* exact_limit */ {}, "too long MySQL packet."); IMySQLReadPacket::readPayloadWithUnpacked(limited); } diff --git a/src/IO/LimitReadBuffer.cpp b/src/IO/LimitReadBuffer.cpp index 96e2c359b58..e14112f8d19 100644 --- a/src/IO/LimitReadBuffer.cpp +++ b/src/IO/LimitReadBuffer.cpp @@ -22,11 +22,11 @@ bool LimitReadBuffer::nextImpl() if (bytes >= limit) { - if (exact_limit && bytes == limit) + if (exact_limit && bytes == *exact_limit) return false; - if (exact_limit && bytes != limit) - throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected data, got {} bytes, expected {}", bytes, limit); + if (exact_limit && bytes != *exact_limit) + throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected data, got {} bytes, expected {}", bytes, *exact_limit); if (throw_exception) throw Exception(ErrorCodes::LIMIT_EXCEEDED, "Limit for LimitReadBuffer exceeded: {}", exception_message); @@ -36,8 +36,8 @@ bool LimitReadBuffer::nextImpl() if (!in->next()) { - if (exact_limit && bytes != limit) - throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected EOF, got {} of {} bytes", bytes, limit); + if (exact_limit && bytes != *exact_limit) + throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected EOF, got {} of {} bytes", bytes, *exact_limit); /// Clearing the buffer with existing data. set(in->position(), 0); return false; @@ -52,7 +52,8 @@ bool LimitReadBuffer::nextImpl() } -LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_) +LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, + std::optional exact_limit_, std::string exception_message_) : ReadBuffer(in_ ? in_->position() : nullptr, 0) , in(in_) , owns_in(owns) @@ -71,13 +72,15 @@ LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, boo } -LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_) +LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, + std::optional exact_limit_, std::string exception_message_) : LimitReadBuffer(&in_, false, limit_, throw_exception_, exact_limit_, exception_message_) { } -LimitReadBuffer::LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_) +LimitReadBuffer::LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, + std::optional exact_limit_, std::string exception_message_) : LimitReadBuffer(in_.release(), true, limit_, throw_exception_, exact_limit_, exception_message_) { } diff --git a/src/IO/LimitReadBuffer.h b/src/IO/LimitReadBuffer.h index 0f99bf2a08d..15885c1d850 100644 --- a/src/IO/LimitReadBuffer.h +++ b/src/IO/LimitReadBuffer.h @@ -13,8 +13,9 @@ namespace DB class LimitReadBuffer : public ReadBuffer { public: - LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_ = {}); - LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, bool exact_limit_, + LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, + std::optional exact_limit_, std::string exception_message_ = {}); + LimitReadBuffer(std::unique_ptr in_, UInt64 limit_, bool throw_exception_, std::optional exact_limit_, std::string exception_message_ = {}); ~LimitReadBuffer() override; @@ -24,10 +25,10 @@ private: UInt64 limit; bool throw_exception; - bool exact_limit; + std::optional exact_limit; std::string exception_message; - LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, bool exact_limit_, std::string exception_message_); + LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, std::optional exact_limit_, std::string exception_message_); bool nextImpl() override; }; diff --git a/src/IO/examples/limit_read_buffer.cpp b/src/IO/examples/limit_read_buffer.cpp index 559c87353f0..9c57c175620 100644 --- a/src/IO/examples/limit_read_buffer.cpp +++ b/src/IO/examples/limit_read_buffer.cpp @@ -24,13 +24,13 @@ int main(int argc, char ** argv) writeCString("--- first ---\n", out); { - LimitReadBuffer limit_in(in, limit, false); + LimitReadBuffer limit_in(in, limit, /* trow_exception */ false, /* exact_limit */ {}); copyData(limit_in, out); } writeCString("\n--- second ---\n", out); { - LimitReadBuffer limit_in(in, limit, false); + LimitReadBuffer limit_in(in, limit, /* trow_exception */ false, /* exact_limit */ {}); copyData(limit_in, out); } diff --git a/src/IO/examples/limit_read_buffer2.cpp b/src/IO/examples/limit_read_buffer2.cpp index ac7c43d764c..a0369047d3a 100644 --- a/src/IO/examples/limit_read_buffer2.cpp +++ b/src/IO/examples/limit_read_buffer2.cpp @@ -27,7 +27,7 @@ try ReadBuffer in(src.data(), src.size(), 0); - LimitReadBuffer limit_in(in, 1, false); + LimitReadBuffer limit_in(in, 1, /* trow_exception */ false, /* exact_limit */ {}); { WriteBufferFromString out(dst); @@ -55,7 +55,7 @@ try char x; readChar(x, in); - LimitReadBuffer limit_in(in, 1, false); + LimitReadBuffer limit_in(in, 1, /* trow_exception */ false, /* exact_limit */ {}); copyData(limit_in, out); @@ -85,7 +85,7 @@ try ReadBuffer in(src.data(), src.size(), 0); { - LimitReadBuffer limit_in(in, 1, false); + LimitReadBuffer limit_in(in, 1, /* trow_exception */ false, /* exact_limit */ {}); char x; readChar(x, limit_in); diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index aeaacf35051..7fcdf1ff0dc 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -1237,7 +1237,7 @@ void executeQuery( /// If not - copy enough data into 'parse_buf'. WriteBufferFromVector> out(parse_buf); - LimitReadBuffer limit(istr, max_query_size + 1, /* trow_exception */ false, /* exact_limit */ false); + LimitReadBuffer limit(istr, max_query_size + 1, /* trow_exception */ false, /* exact_limit */ {}); copyData(limit, out); out.finalize(); diff --git a/src/Server/HTTP/HTMLForm.cpp b/src/Server/HTTP/HTMLForm.cpp index d9d897d20c4..1abf9e5b83e 100644 --- a/src/Server/HTTP/HTMLForm.cpp +++ b/src/Server/HTTP/HTMLForm.cpp @@ -20,6 +20,11 @@ namespace DB { +namespace ErrorCodes +{ + extern const int CANNOT_READ_ALL_DATA; +} + namespace { @@ -229,6 +234,11 @@ void HTMLForm::readMultipart(ReadBuffer & in_, PartHandler & handler) if (!in.skipToNextBoundary()) break; } + + /// It's important to check, because we could get "fake" EOF and incomplete request if a client suddenly died in the middle. + if (!in.isActualEOF()) + throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected EOF, " + "did not find the last boundary while parsing a multipart HTTP request"); } @@ -244,7 +254,8 @@ bool HTMLForm::MultipartReadBuffer::skipToNextBoundary() if (in.eof()) return false; - assert(boundary_hit); + chassert(boundary_hit); + chassert(!found_last_boundary); boundary_hit = false; @@ -255,7 +266,8 @@ bool HTMLForm::MultipartReadBuffer::skipToNextBoundary() { set(in.position(), 0); next(); /// We need to restrict our buffer to size of next available line. - return !startsWith(line, boundary + "--"); + found_last_boundary = startsWith(line, boundary + "--"); + return !found_last_boundary; } } diff --git a/src/Server/HTTP/HTMLForm.h b/src/Server/HTTP/HTMLForm.h index 16889b41d80..c75dafccaf0 100644 --- a/src/Server/HTTP/HTMLForm.h +++ b/src/Server/HTTP/HTMLForm.h @@ -108,10 +108,13 @@ public: /// Returns false if last boundary found. bool skipToNextBoundary(); + bool isActualEOF() const { return found_last_boundary; } + private: PeekableReadBuffer in; const std::string boundary; bool boundary_hit = true; + bool found_last_boundary = false; std::string readLine(bool append_crlf); diff --git a/src/Server/HTTP/HTTPServerRequest.cpp b/src/Server/HTTP/HTTPServerRequest.cpp index d2e19d594dc..c9ffa3a4c3b 100644 --- a/src/Server/HTTP/HTTPServerRequest.cpp +++ b/src/Server/HTTP/HTTPServerRequest.cpp @@ -56,12 +56,17 @@ HTTPServerRequest::HTTPServerRequest(HTTPContextPtr context, HTTPServerResponse if (getChunkedTransferEncoding()) stream = std::make_unique(std::move(in), context->getMaxChunkSize()); else if (hasContentLength()) - stream = std::make_unique(std::move(in), getContentLength(), /* trow_exception */ true, /* exact_limit */ true); + { + size_t content_length = getContentLength(); + stream = std::make_unique(std::move(in), content_length, + /* trow_exception */ true, /* exact_limit */ content_length); + } else if (getMethod() != HTTPRequest::HTTP_GET && getMethod() != HTTPRequest::HTTP_HEAD && getMethod() != HTTPRequest::HTTP_DELETE) { stream = std::move(in); - LOG_WARNING(&Poco::Logger::get("HTTPServerRequest"), "Got an HTTP request with no content length, " - "it may be impossible to distinguish graceful EOF from abnormal connection loss"); + if (!startsWith(getContentType(), "multipart/form-data")) + LOG_WARNING(&Poco::Logger::get("HTTPServerRequest"), "Got an HTTP request with no content length " + "and no chunked/multipart encoding, it may be impossible to distinguish graceful EOF from abnormal connection loss"); } else /// We have to distinguish empty buffer and nullptr. diff --git a/src/Server/MySQLHandler.cpp b/src/Server/MySQLHandler.cpp index 279896da9fa..4dcbca93e28 100644 --- a/src/Server/MySQLHandler.cpp +++ b/src/Server/MySQLHandler.cpp @@ -155,7 +155,7 @@ void MySQLHandler::run() payload.readStrict(command); // For commands which are executed without MemoryTracker. - LimitReadBuffer limited_payload(payload, 10000, /* trow_exception */ true, /* exact_limit */ false, "too long MySQL packet."); + LimitReadBuffer limited_payload(payload, 10000, /* trow_exception */ true, /* exact_limit */ {}, "too long MySQL packet."); LOG_DEBUG(log, "Received command: {}. Connection id: {}.", static_cast(static_cast(command)), connection_id); diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index 3e89d1810c7..bf5578f2610 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -1057,7 +1057,7 @@ bool TCPHandler::receiveProxyHeader() /// Only PROXYv1 is supported. /// Validation of protocol is not fully performed. - LimitReadBuffer limit_in(*in, 107, /* trow_exception */ true, /* exact_limit */ false); /// Maximum length from the specs. + LimitReadBuffer limit_in(*in, 107, /* trow_exception */ true, /* exact_limit */ {}); /// Maximum length from the specs. assertString("PROXY ", limit_in); diff --git a/src/Storages/StorageLog.cpp b/src/Storages/StorageLog.cpp index 388faf1aeb5..014a0ff2424 100644 --- a/src/Storages/StorageLog.cpp +++ b/src/Storages/StorageLog.cpp @@ -118,7 +118,7 @@ private: if (limited_by_file_size) { - limited.emplace(*plain, file_size - offset, /* trow_exception */ false, /* exact_limit */ false); + limited.emplace(*plain, file_size - offset, /* trow_exception */ false, /* exact_limit */ std::optional()); compressed.emplace(*limited); } else diff --git a/tests/queries/0_stateless/02435_insert_cancellation_http.sh b/tests/queries/0_stateless/02435_insert_cancellation_http.sh index 653ec59f491..e3a1645db63 100755 --- a/tests/queries/0_stateless/02435_insert_cancellation_http.sh +++ b/tests/queries/0_stateless/02435_insert_cancellation_http.sh @@ -10,24 +10,32 @@ export TEST_MARK="02435_insert_${CLICKHOUSE_DATABASE}_" $CLICKHOUSE_CLIENT -q 'select * from numbers(5000000) format TSV' > $DATA_FILE $CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by A settings non_replicated_deduplication_window=1000;' -$CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&max_insert_block_size=100000&input_format_parallel_parsing=0&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + +function insert_data +{ + SETTINGS="query_id=$ID&max_insert_block_size=100000&input_format_parallel_parsing=0" + TRASH_SETTINGS="query_id=$ID&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_insert_block_size=1100000&max_block_size=1100000&min_insert_block_size_bytes=0&min_insert_block_size_rows=1100000&max_insert_block_size=1100000" + TYPE=$(( RANDOM % 3 )) + if [[ "$TYPE" -eq 0 ]]; then + $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + elif [[ "$TYPE" -eq 1 ]]; then + $CLICKHOUSE_CURL -sS -X POST -H "Transfer-Encoding: chunked" --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + else + $CLICKHOUSE_CURL -sS -F 'file=@-' "$CLICKHOUSE_URL&$TRASH_SETTINGS&file_format=TSV&file_types=UInt64" -X POST --form-string 'query=insert into dedup_test select * from file' < $DATA_FILE + fi +} + +export -f insert_data + +insert_data $CLICKHOUSE_CLIENT -q 'select count() from dedup_test' function thread_insert { # supress "Killed" messages from bash - function wrap - { - if (( RANDOM % 2 )); then - $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&query_id=$ID&max_insert_block_size=100000&input_format_parallel_parsing=0&query=insert+into+dedup_test+format+TSV" < $DATA_FILE - else - $CLICKHOUSE_CURL -sS -X POST -H "Transfer-Encoding: chunked" --data-binary @- "$CLICKHOUSE_URL&query_id=$ID&max_insert_block_size=100000&input_format_parallel_parsing=0&query=insert+into+dedup_test+format+TSV" < $DATA_FILE - fi - } - export -f wrap while true; do export ID="$TEST_MARK$RANDOM" - bash -c wrap 2>&1| grep -Fav "Killed" + bash -c insert_data 2>&1| grep -Fav "Killed" done } From f49a67752ea252a189f073bb329396cdbea58b2f Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 23 Feb 2023 13:28:36 +0000 Subject: [PATCH 078/342] Support constant on LHS --- ...ass.cpp => LogicalExpressionOptimizer.cpp} | 53 ++++++++++--------- ...ass.h => LogicalExpressionOptimizerPass.h} | 6 +-- src/Analyzer/QueryTreePassManager.cpp | 4 +- .../00736_disjunction_optimisation.reference | 37 +++++++++++++ .../00736_disjunction_optimisation.sql | 4 ++ ...ssions_optimizer_low_cardinality.reference | 22 ++++++++ ..._expressions_optimizer_low_cardinality.sql | 2 + 7 files changed, 98 insertions(+), 30 deletions(-) rename src/Analyzer/Passes/{OrEqualityChainToInPass.cpp => LogicalExpressionOptimizer.cpp} (63%) rename src/Analyzer/Passes/{OrEqualityChainToInPass.h => LogicalExpressionOptimizerPass.h} (81%) diff --git a/src/Analyzer/Passes/OrEqualityChainToInPass.cpp b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp similarity index 63% rename from src/Analyzer/Passes/OrEqualityChainToInPass.cpp rename to src/Analyzer/Passes/LogicalExpressionOptimizer.cpp index 212e8252ce4..f9744cdc8d0 100644 --- a/src/Analyzer/Passes/OrEqualityChainToInPass.cpp +++ b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp @@ -1,4 +1,4 @@ -#include +#include #include @@ -10,12 +10,12 @@ namespace DB { -class OrEqualityChainToInVisitor : public InDepthQueryTreeVisitorWithContext +class LogicalExpressionOptimizerVisitor : public InDepthQueryTreeVisitorWithContext { public: - using Base = InDepthQueryTreeVisitorWithContext; + using Base = InDepthQueryTreeVisitorWithContext; - explicit OrEqualityChainToInVisitor(ContextPtr context) + explicit LogicalExpressionOptimizerVisitor(ContextPtr context) : Base(std::move(context)) {} @@ -34,38 +34,32 @@ public: for (const auto & argument : function_node->getArguments()) { auto * argument_function = argument->as(); - if (!argument_function) + if (!argument_function || argument_function->getFunctionName() != "equals") { or_operands.push_back(argument); continue; } /// collect all equality checks (x = value) - if (argument_function->getFunctionName() != "equals") - { - or_operands.push_back(argument); - continue; - } const auto & equals_arguments = argument_function->getArguments().getNodes(); const auto & lhs = equals_arguments[0]; + const auto & rhs = equals_arguments[1]; - const auto * rhs_literal = equals_arguments[1]->as(); - if (!rhs_literal) - { + if (lhs->as()) + node_to_equals_functions[rhs].push_back(argument); + else if (rhs->as()) + node_to_equals_functions[lhs].push_back(argument); + else or_operands.push_back(argument); - continue; - } - - node_to_equals_functions[lhs].push_back(argument); } auto in_function_resolver = FunctionFactory::instance().get("in", getContext()); - for (auto & [lhs, equals_functions] : node_to_equals_functions) + for (auto & [expression, equals_functions] : node_to_equals_functions) { const auto & settings = getSettings(); - if (equals_functions.size() < settings.optimize_min_equality_disjunction_chain_length && !lhs.node->getResultType()->lowCardinality()) + if (equals_functions.size() < settings.optimize_min_equality_disjunction_chain_length && !expression.node->getResultType()->lowCardinality()) { std::move(equals_functions.begin(), equals_functions.end(), std::back_inserter(or_operands)); continue; @@ -78,9 +72,18 @@ public: { const auto * equals_function = equals->as(); assert(equals_function && equals_function->getFunctionName() == "equals"); - const auto * rhs_literal = equals_function->getArguments().getNodes()[1]->as(); - assert(rhs_literal); - args.push_back(rhs_literal->getValue()); + + const auto & equals_arguments = equals_function->getArguments().getNodes(); + if (const auto * rhs_literal = equals_arguments[1]->as()) + { + args.push_back(rhs_literal->getValue()); + } + else + { + const auto * lhs_literal = equals_arguments[0]->as(); + assert(lhs_literal); + args.push_back(lhs_literal->getValue()); + } } auto rhs_node = std::make_shared(std::move(args)); @@ -89,7 +92,7 @@ public: QueryTreeNodes in_arguments; in_arguments.reserve(2); - in_arguments.push_back(lhs.node); + in_arguments.push_back(expression.node); in_arguments.push_back(std::move(rhs_node)); in_function->getArguments().getNodes() = std::move(in_arguments); @@ -108,9 +111,9 @@ public: }; -void OrEqualityChainToInPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context) +void LogicalExpressionOptimizerPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context) { - OrEqualityChainToInVisitor visitor(std::move(context)); + LogicalExpressionOptimizerVisitor visitor(std::move(context)); visitor.visit(query_tree_node); } diff --git a/src/Analyzer/Passes/OrEqualityChainToInPass.h b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h similarity index 81% rename from src/Analyzer/Passes/OrEqualityChainToInPass.h rename to src/Analyzer/Passes/LogicalExpressionOptimizerPass.h index 2a9d6818a4f..327ae3c3fee 100644 --- a/src/Analyzer/Passes/OrEqualityChainToInPass.h +++ b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h @@ -8,7 +8,7 @@ namespace DB /** * This pass replaces chains of equality functions inside an OR with a single IN operator. * The replacement is done if: - * - rhs of the equality function is a literal + * - rhs of the equality function is a constant * - length of chain is at least 'optimize_min_equality_disjunction_chain_length' long OR lhs is LowCardinality * * E.g. (optimize_min_equality_disjunction_chain_length = 2) @@ -25,10 +25,10 @@ namespace DB * ------------------------------- */ -class OrEqualityChainToInPass final : public IQueryTreePass +class LogicalExpressionOptimizerPass final : public IQueryTreePass { public: - String getName() override { return "OrEqualityChainToIn"; } + String getName() override { return "LogicalExpressionOptimizer"; } String getDescription() override { return "Transform all the 'or's with equality check to a single IN function"; } diff --git a/src/Analyzer/QueryTreePassManager.cpp b/src/Analyzer/QueryTreePassManager.cpp index dbbedfd1aaa..da419f0e5bd 100644 --- a/src/Analyzer/QueryTreePassManager.cpp +++ b/src/Analyzer/QueryTreePassManager.cpp @@ -39,7 +39,7 @@ #include #include #include -#include +#include namespace DB @@ -264,7 +264,7 @@ void addQueryTreePasses(QueryTreePassManager & manager) manager.addPass(std::make_unique()); - manager.addPass(std::make_unique()); + manager.addPass(std::make_unique()); manager.addPass(std::make_unique()); diff --git a/tests/queries/0_stateless/00736_disjunction_optimisation.reference b/tests/queries/0_stateless/00736_disjunction_optimisation.reference index 4210571842f..fa395dccdc8 100644 --- a/tests/queries/0_stateless/00736_disjunction_optimisation.reference +++ b/tests/queries/0_stateless/00736_disjunction_optimisation.reference @@ -442,3 +442,40 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +21 1 +22 1 +23 1 +QUERY id: 0 + PROJECTION COLUMNS + s UInt64 + or(equals(s, 21), equals(22, s), equals(23, s)) UInt8 + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + JOIN TREE + TABLE id: 3, table_name: default.bug + SETTINGS allow_experimental_analyzer=1 diff --git a/tests/queries/0_stateless/00736_disjunction_optimisation.sql b/tests/queries/0_stateless/00736_disjunction_optimisation.sql index e52ed2ab45b..e5bfc81f7ae 100644 --- a/tests/queries/0_stateless/00736_disjunction_optimisation.sql +++ b/tests/queries/0_stateless/00736_disjunction_optimisation.sql @@ -42,4 +42,8 @@ select s, (s=21 or s=22 or s=23) from bug; select s, (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; explain query tree select s, (s=21 or s=22 or s=23) from bug SETTINGS allow_experimental_analyzer = 1;; +select s, (s=21 or 22=s or 23=s) from bug; +select s, (s=21 or 22=s or 23=s) from bug SETTINGS allow_experimental_analyzer = 1;; +explain query tree select s, (s=21 or 22=s or 23=s) from bug SETTINGS allow_experimental_analyzer = 1;; + DROP TABLE bug; diff --git a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference index 086c0a49b59..617bdde183e 100644 --- a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference +++ b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference @@ -22,6 +22,28 @@ QUERY id: 0 SETTINGS allow_experimental_analyzer=1 SELECT a FROM t_logical_expressions_optimizer_low_cardinality +WHERE (a = \'x\') OR (\'y\' = a) +QUERY id: 0 + PROJECTION COLUMNS + a LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 1 + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality + WHERE + FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 5, nodes: 2 + FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 7, nodes: 2 + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) + CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + SETTINGS allow_experimental_analyzer=1 +SELECT a +FROM t_logical_expressions_optimizer_low_cardinality WHERE (b = 0) OR (b = 1) QUERY id: 0 PROJECTION COLUMNS diff --git a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql index f5368679f60..14f8ad830e7 100644 --- a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql +++ b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.sql @@ -5,6 +5,8 @@ CREATE TABLE t_logical_expressions_optimizer_low_cardinality (a LowCardinality(S -- LowCardinality case, ignore optimize_min_equality_disjunction_chain_length limit, optimzer applied EXPLAIN SYNTAX SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a = 'x' OR a = 'y'; EXPLAIN QUERY TREE SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a = 'x' OR a = 'y' SETTINGS allow_experimental_analyzer = 1; +EXPLAIN SYNTAX SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a = 'x' OR 'y' = a; +EXPLAIN QUERY TREE SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE a = 'x' OR 'y' = a SETTINGS allow_experimental_analyzer = 1; -- Non-LowCardinality case, optimizer not applied for short chains EXPLAIN SYNTAX SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE b = 0 OR b = 1; EXPLAIN QUERY TREE SELECT a FROM t_logical_expressions_optimizer_low_cardinality WHERE b = 0 OR b = 1 SETTINGS allow_experimental_analyzer = 1; From 88f322ace267dc1c194f85db5be1e94ba29c05c8 Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Thu, 23 Feb 2023 14:50:58 +0100 Subject: [PATCH 079/342] Try fix test --- tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh index e84c06027e8..d27a2f9fcbb 100755 --- a/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh +++ b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.sh @@ -6,8 +6,8 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) . "$CURDIR"/../shell_config.sh echo "CapnProto" -${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format CapnProto settings format_schema='format_schemas/02566_ipv4_ipv6:Message'" > 02566_ipv4_ipv6_data.capnp -${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.capnp, auto, 'ipv6 IPv6, ipv4 IPv4') settings format_schema='format_schemas/02566_ipv4_ipv6:Message'" +${CLICKHOUSE_LOCAL} -q "select '2001:db8:11a3:9d7:1f34:8a2e:7a0:765d'::IPv6 as ipv6, '127.0.0.1'::IPv4 as ipv4 format CapnProto settings format_schema='$CURDIR/format_schemas/02566_ipv4_ipv6:Message'" > 02566_ipv4_ipv6_data.capnp +${CLICKHOUSE_LOCAL} -q "select * from file(02566_ipv4_ipv6_data.capnp, auto, 'ipv6 IPv6, ipv4 IPv4') settings format_schema='$CURDIR/format_schemas/02566_ipv4_ipv6:Message'" rm 02566_ipv4_ipv6_data.capnp echo "Avro" From e37a3801ca4478bb88d21cc9d8eda029ba4dbc8a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 23 Feb 2023 16:04:51 +0000 Subject: [PATCH 080/342] Add new logical optimizations --- .../Passes/LogicalExpressionOptimizer.cpp | 148 ++++++++++++++++-- ...00621_regression_for_in_operator.reference | 4 +- .../00736_disjunction_optimisation.reference | 64 ++++---- ...ssions_optimizer_low_cardinality.reference | 8 +- ...imizer_removing_redundant_checks.reference | 0 ...al_optimizer_removing_redundant_checks.sql | 26 +++ 6 files changed, 202 insertions(+), 48 deletions(-) create mode 100644 tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference create mode 100644 tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.sql diff --git a/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp index f9744cdc8d0..c29d38c7865 100644 --- a/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp +++ b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp @@ -7,6 +7,8 @@ #include #include +#include + namespace DB { @@ -17,21 +19,115 @@ public: explicit LogicalExpressionOptimizerVisitor(ContextPtr context) : Base(std::move(context)) + , cast_function_resolver(FunctionFactory::instance().get("_CAST", getContext())) {} - void visitImpl(QueryTreeNodePtr & node) { auto * function_node = node->as(); - if (!function_node || function_node->getFunctionName() != "or") + if (!function_node) return; + if (function_node->getFunctionName() == "or") + { + tryReplaceOrEqualsChainWithIn(node); + return; + } + + if (function_node->getFunctionName() == "and") + { + tryReplaceAndEqualsChainsWithConstant(node); + return; + } + } +private: + void tryReplaceAndEqualsChainsWithConstant(QueryTreeNodePtr & node) + { + auto & function_node = node->as(); + assert(function_node.getFunctionName() == "and"); + + if (function_node.getResultType()->isNullable()) + return; + + QueryTreeNodes and_operands; + + QueryTreeNodePtrWithHashMap node_to_constants; + + for (const auto & argument : function_node.getArguments()) + { + auto * argument_function = argument->as(); + if (!argument_function || argument_function->getFunctionName() != "equals") + { + and_operands.push_back(argument); + continue; + } + + const auto & equals_arguments = argument_function->getArguments().getNodes(); + const auto & lhs = equals_arguments[0]; + const auto & rhs = equals_arguments[1]; + + const auto has_and_with_different_constant = [&](const QueryTreeNodePtr & expression, const ConstantNode * constant) + { + if (auto it = node_to_constants.find(expression); it != node_to_constants.end()) + { + if (!it->second->isEqual(*constant)) + return true; + } + else + { + node_to_constants.emplace(expression, constant); + and_operands.push_back(argument); + } + + return false; + }; + + bool collapse_to_false = false; + + if (const auto * lhs_literal = lhs->as()) + { + collapse_to_false = has_and_with_different_constant(rhs, lhs_literal); + } + else if (const auto * rhs_literal = rhs->as()) + { + collapse_to_false = has_and_with_different_constant(lhs, rhs_literal); + } + else + continue; + + if (collapse_to_false) + { + auto false_value = std::make_shared(0u, function_node.getResultType()); + auto false_node = std::make_shared(std::move(false_value)); + node = std::move(false_node); + return; + } + } + + if (and_operands.size() == 1) + { + assert(!function_node.getResultType()->isNullable()); + resolveAsCast(function_node, std::move(and_operands[0])); + return; + } + + auto and_function_resolver = FunctionFactory::instance().get("and", getContext()); + function_node.getArguments().getNodes() = std::move(and_operands); + function_node.resolveAsFunction(and_function_resolver); + } + + void tryReplaceOrEqualsChainWithIn(QueryTreeNodePtr & node) + { + auto & function_node = node->as(); + assert(function_node.getFunctionName() == "or"); + QueryTreeNodes or_operands; QueryTreeNodePtrWithHashMap node_to_equals_functions; + QueryTreeNodePtrWithHashMap node_to_constants; - for (const auto & argument : function_node->getArguments()) + for (const auto & argument : function_node.getArguments()) { auto * argument_function = argument->as(); if (!argument_function || argument_function->getFunctionName() != "equals") @@ -46,10 +142,20 @@ public: const auto & lhs = equals_arguments[0]; const auto & rhs = equals_arguments[1]; - if (lhs->as()) - node_to_equals_functions[rhs].push_back(argument); - else if (rhs->as()) - node_to_equals_functions[lhs].push_back(argument); + const auto add_equals_function_if_not_present = [&](const auto & expression_node, const ConstantNode * constant) + { + auto & constant_set = node_to_constants[expression_node]; + if (!constant_set.contains(constant)) + { + constant_set.insert(constant); + node_to_equals_functions[expression_node].push_back(argument); + } + }; + + if (const auto * lhs_literal = lhs->as()) + add_equals_function_if_not_present(rhs, lhs_literal); + else if (const auto * rhs_literal = rhs->as()) + add_equals_function_if_not_present(lhs, rhs_literal); else or_operands.push_back(argument); } @@ -102,12 +208,34 @@ public: } if (or_operands.size() == 1) - or_operands.push_back(std::make_shared(static_cast(0))); + { + assert(!function_node.getResultType()->isNullable()); + resolveAsCast(function_node, std::move(or_operands[0])); + return; + } auto or_function_resolver = FunctionFactory::instance().get("or", getContext()); - function_node->getArguments().getNodes() = std::move(or_operands); - function_node->resolveAsFunction(or_function_resolver); + function_node.getArguments().getNodes() = std::move(or_operands); + function_node.resolveAsFunction(or_function_resolver); } + + void resolveAsCast(FunctionNode & function_node, QueryTreeNodePtr operand) + { + std::string cast_type = function_node.getResultType()->getName(); + auto cast_type_constant_value = std::make_shared(std::move(cast_type), std::make_shared()); + auto cast_type_constant_node = std::make_shared(std::move(cast_type_constant_value)); + + QueryTreeNodes arguments; + arguments.reserve(2); + arguments.push_back(std::move(operand)); + arguments.push_back(std::move(cast_type_constant_node)); + + function_node.getArguments().getNodes() = std::move(arguments); + + function_node.resolveAsFunction(cast_function_resolver); + } + + const FunctionOverloadResolverPtr cast_function_resolver; }; diff --git a/tests/queries/0_stateless/00621_regression_for_in_operator.reference b/tests/queries/0_stateless/00621_regression_for_in_operator.reference index c9eb7a08fc6..54b345b65c0 100644 --- a/tests/queries/0_stateless/00621_regression_for_in_operator.reference +++ b/tests/queries/0_stateless/00621_regression_for_in_operator.reference @@ -12,7 +12,7 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.regression_for_in_operator_view WHERE - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -20,7 +20,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 8, column_name: g, result_type: String, source_id: 3 CONSTANT id: 9, constant_value: Tuple_(\'5\', \'6\'), constant_value_type: Tuple(String, String) - CONSTANT id: 10, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 10, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 2 2 diff --git a/tests/queries/0_stateless/00736_disjunction_optimisation.reference b/tests/queries/0_stateless/00736_disjunction_optimisation.reference index fa395dccdc8..4b899527913 100644 --- a/tests/queries/0_stateless/00736_disjunction_optimisation.reference +++ b/tests/queries/0_stateless/00736_disjunction_optimisation.reference @@ -39,7 +39,7 @@ QUERY id: 0 FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 6, nodes: 2 - FUNCTION id: 7, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 7, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 8, nodes: 2 FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 @@ -47,8 +47,8 @@ QUERY id: 0 LIST id: 10, nodes: 2 COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 3 CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 13, constant_value: UInt64_0, constant_value_type: UInt8 - FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + CONSTANT id: 13, constant_value: \'UInt8\', constant_value_type: String + FUNCTION id: 14, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 15, nodes: 2 FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 @@ -56,7 +56,7 @@ QUERY id: 0 LIST id: 17, nodes: 2 COLUMN id: 18, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 19, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 20, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 20, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 1 21 1 22 @@ -96,7 +96,7 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: default.bug WHERE - FUNCTION id: 9, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 9, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 10, nodes: 2 FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 @@ -104,9 +104,9 @@ QUERY id: 0 LIST id: 12, nodes: 2 COLUMN id: 13, column_name: k, result_type: UInt64, source_id: 7 CONSTANT id: 14, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 15, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 15, constant_value: \'UInt8\', constant_value_type: String WHERE - FUNCTION id: 16, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 16, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 17, nodes: 2 FUNCTION id: 18, function_name: in, function_type: ordinary, result_type: UInt8 @@ -114,7 +114,7 @@ QUERY id: 0 LIST id: 19, nodes: 2 COLUMN id: 20, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 22, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 22, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 1 1 21 1 1 1 1 1 22 0 1 1 @@ -145,7 +145,7 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 6 COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -153,14 +153,14 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 12, nodes: 2 COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 13, constant_value: UInt64_21, constant_value_type: UInt8 - FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 14, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 15, nodes: 2 FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 @@ -168,8 +168,8 @@ QUERY id: 0 LIST id: 17, nodes: 2 COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 18, constant_value: Tuple_(UInt64_21, UInt64_22), constant_value_type: Tuple(UInt8, UInt8) - CONSTANT id: 19, constant_value: UInt64_0, constant_value_type: UInt8 - FUNCTION id: 20, function_name: or, function_type: ordinary, result_type: UInt8 + CONSTANT id: 19, constant_value: \'UInt8\', constant_value_type: String + FUNCTION id: 20, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 21, nodes: 2 FUNCTION id: 22, function_name: in, function_type: ordinary, result_type: UInt8 @@ -177,7 +177,7 @@ QUERY id: 0 LIST id: 23, nodes: 2 COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 24, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 25, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 25, constant_value: \'UInt8\', constant_value_type: String JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -206,7 +206,7 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -214,7 +214,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -259,7 +259,7 @@ QUERY id: 0 FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 6, nodes: 2 - FUNCTION id: 7, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 7, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 8, nodes: 2 FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 @@ -267,8 +267,8 @@ QUERY id: 0 LIST id: 10, nodes: 2 COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 3 CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 13, constant_value: UInt64_0, constant_value_type: UInt8 - FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 + CONSTANT id: 13, constant_value: \'UInt8\', constant_value_type: String + FUNCTION id: 14, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 15, nodes: 2 FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 @@ -276,7 +276,7 @@ QUERY id: 0 LIST id: 17, nodes: 2 COLUMN id: 18, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 19, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 20, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 20, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 1 21 1 22 @@ -316,7 +316,7 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: default.bug WHERE - FUNCTION id: 9, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 9, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 10, nodes: 2 FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 @@ -324,9 +324,9 @@ QUERY id: 0 LIST id: 12, nodes: 2 COLUMN id: 13, column_name: k, result_type: UInt64, source_id: 7 CONSTANT id: 14, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 15, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 15, constant_value: \'UInt8\', constant_value_type: String WHERE - FUNCTION id: 16, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 16, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 17, nodes: 2 FUNCTION id: 18, function_name: in, function_type: ordinary, result_type: UInt8 @@ -334,7 +334,7 @@ QUERY id: 0 LIST id: 19, nodes: 2 COLUMN id: 20, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 22, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 22, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 1 1 21 1 1 1 1 1 22 0 1 1 @@ -365,7 +365,7 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 6 COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -373,7 +373,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: UInt8 ARGUMENTS @@ -393,7 +393,7 @@ QUERY id: 0 LIST id: 20, nodes: 2 COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 21, constant_value: UInt64_22, constant_value_type: UInt8 - FUNCTION id: 22, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 22, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 23, nodes: 2 FUNCTION id: 24, function_name: in, function_type: ordinary, result_type: UInt8 @@ -401,7 +401,7 @@ QUERY id: 0 LIST id: 25, nodes: 2 COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 26, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 27, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 27, constant_value: \'UInt8\', constant_value_type: String JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -430,7 +430,7 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -438,7 +438,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -467,7 +467,7 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -475,7 +475,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 diff --git a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference index 617bdde183e..22bd68e0ac1 100644 --- a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference +++ b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference @@ -10,7 +10,7 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality WHERE - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -18,7 +18,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 CONSTANT id: 8, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 SELECT a FROM t_logical_expressions_optimizer_low_cardinality @@ -32,7 +32,7 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality WHERE - FUNCTION id: 4, function_name: or, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 @@ -40,7 +40,7 @@ QUERY id: 0 LIST id: 7, nodes: 2 COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 CONSTANT id: 8, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) - CONSTANT id: 9, constant_value: UInt64_0, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String SETTINGS allow_experimental_analyzer=1 SELECT a FROM t_logical_expressions_optimizer_low_cardinality diff --git a/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference b/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.sql b/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.sql new file mode 100644 index 00000000000..f20ef412215 --- /dev/null +++ b/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.sql @@ -0,0 +1,26 @@ +SET allow_experimental_analyzer = 1; + +DROP TABLE IF EXISTS 02668_logical_optimizer; + +CREATE TABLE 02668_logical_optimizer +(a Int32, b LowCardinality(String)) +ENGINE=Memory; + +INSERT INTO 02668_logical_optimizer VALUES (1, 'test'), (2, 'test2'), (3, 'another'); + +SET optimize_min_equality_disjunction_chain_length = 2; + +SELECT * FROM 02668_logical_optimizer WHERE a = 1 OR 3 = a OR 1 = a; +EXPLAIN QUERY TREE SELECT * FROM 02668_logical_optimizer WHERE a = 1 OR 3 = a OR 1 = a; + +SELECT * FROM 02668_logical_optimizer WHERE a = 1 OR 1 = a; +EXPLAIN QUERY TREE SELECT * FROM 02668_logical_optimizer WHERE a = 1 OR 1 = a; + +SELECT * FROM 02668_logical_optimizer WHERE a = 1 AND 2 = a; +EXPLAIN QUERY TREE SELECT * FROM 02668_logical_optimizer WHERE a = 1 AND 2 = a; + +SELECT * FROM 02668_logical_optimizer WHERE 3 = a AND b = 'another' AND a = 3; +EXPLAIN QUERY TREE SELECT * FROM 02668_logical_optimizer WHERE a = 3 AND b = 'another' AND a = 3; + +SELECT * FROM 02668_logical_optimizer WHERE a = 2 AND 2 = a; +EXPLAIN QUERY TREE SELECT * FROM 02668_logical_optimizer WHERE a = 2 AND 2 = a; From d21a6a3ba40f2259aba17ff26acf930814a35f16 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 23 Feb 2023 20:42:46 +0100 Subject: [PATCH 081/342] fix --- .../PushingAsyncPipelineExecutor.cpp | 2 +- .../Executors/PushingPipelineExecutor.cpp | 2 +- ..._cancel_insert_when_client_dies.reference} | 1 + ...> 02434_cancel_insert_when_client_dies.sh} | 29 +++++++-- ...434_insert_cancellation_native_protocol.sh | 65 ------------------- .../02435_insert_cancellation_http.reference | 2 - utils/check-mysql-binlog/main.cpp | 3 +- 7 files changed, 27 insertions(+), 77 deletions(-) rename tests/queries/0_stateless/{02434_insert_cancellation_native_protocol.reference => 02434_cancel_insert_when_client_dies.reference} (88%) rename tests/queries/0_stateless/{02435_insert_cancellation_http.sh => 02434_cancel_insert_when_client_dies.sh} (59%) delete mode 100755 tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh delete mode 100644 tests/queries/0_stateless/02435_insert_cancellation_http.reference diff --git a/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp b/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp index 20f47c6b54c..70815bb8b3b 100644 --- a/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp +++ b/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp @@ -140,7 +140,7 @@ PushingAsyncPipelineExecutor::PushingAsyncPipelineExecutor(QueryPipeline & pipel PushingAsyncPipelineExecutor::~PushingAsyncPipelineExecutor() { /// It must be finalized explicitly. Otherwise we cancel it assuming it's due to an exception. - chassert(finished || std::uncaught_exceptions()); + chassert(finished || std::uncaught_exceptions() || std::current_exception()); try { cancel(); diff --git a/src/Processors/Executors/PushingPipelineExecutor.cpp b/src/Processors/Executors/PushingPipelineExecutor.cpp index ca60932ed6f..696932932df 100644 --- a/src/Processors/Executors/PushingPipelineExecutor.cpp +++ b/src/Processors/Executors/PushingPipelineExecutor.cpp @@ -64,7 +64,7 @@ PushingPipelineExecutor::PushingPipelineExecutor(QueryPipeline & pipeline_) : pi PushingPipelineExecutor::~PushingPipelineExecutor() { /// It must be finalized explicitly. Otherwise we cancel it assuming it's due to an exception. - chassert(finished || std::uncaught_exceptions()); + chassert(finished || std::uncaught_exceptions() || std::current_exception()); try { cancel(); diff --git a/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.reference similarity index 88% rename from tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference rename to tests/queries/0_stateless/02434_cancel_insert_when_client_dies.reference index e3038e03530..d2475419998 100644 --- a/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.reference +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.reference @@ -1,2 +1,3 @@ 5000000 5000000 +1 diff --git a/tests/queries/0_stateless/02435_insert_cancellation_http.sh b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh similarity index 59% rename from tests/queries/0_stateless/02435_insert_cancellation_http.sh rename to tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh index e3a1645db63..c2cf98f712f 100755 --- a/tests/queries/0_stateless/02435_insert_cancellation_http.sh +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# Tags: no-random-settings # shellcheck disable=SC2009 CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) @@ -6,19 +7,24 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) . "$CURDIR"/../shell_config.sh export DATA_FILE="$CLICKHOUSE_TMP/deduptest.tsv" -export TEST_MARK="02435_insert_${CLICKHOUSE_DATABASE}_" +export TEST_MARK="02434_insert_${CLICKHOUSE_DATABASE}_" $CLICKHOUSE_CLIENT -q 'select * from numbers(5000000) format TSV' > $DATA_FILE $CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by A settings non_replicated_deduplication_window=1000;' function insert_data { - SETTINGS="query_id=$ID&max_insert_block_size=100000&input_format_parallel_parsing=0" - TRASH_SETTINGS="query_id=$ID&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_insert_block_size=1100000&max_block_size=1100000&min_insert_block_size_bytes=0&min_insert_block_size_rows=1100000&max_insert_block_size=1100000" - TYPE=$(( RANDOM % 3 )) + SETTINGS="query_id=$ID&max_insert_block_size=110000&min_insert_block_size_rows=110000" + # max_block_size=10000, so external table will contain smaller blocks that will be squashed on insert-select (more chances to catch a bug on query cancellation) + TRASH_SETTINGS="query_id=$ID&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_insert_block_size=110000&max_block_size=10000&min_insert_block_size_bytes=0&min_insert_block_size_rows=110000&max_insert_block_size=110000" + TYPE=$(( RANDOM % 4 )) + if [[ "$TYPE" -eq 0 ]]; then - $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + # client will send 10000-rows blocks, server will squash them into 110000-rows blocks (more chances to catch a bug on query cancellation) + $CLICKHOUSE_CLIENT --max_block_size=10000 --max_insert_block_size=10000 --query_id="$ID" -q 'insert into dedup_test settings max_insert_block_size=110000, min_insert_block_size_rows=110000 format TSV' < $DATA_FILE elif [[ "$TYPE" -eq 1 ]]; then + $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + elif [[ "$TYPE" -eq 2 ]]; then $CLICKHOUSE_CURL -sS -X POST -H "Transfer-Encoding: chunked" --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE else $CLICKHOUSE_CURL -sS -F 'file=@-' "$CLICKHOUSE_URL&$TRASH_SETTINGS&file_format=TSV&file_types=UInt64" -X POST --form-string 'query=insert into dedup_test select * from file' < $DATA_FILE @@ -27,6 +33,7 @@ function insert_data export -f insert_data +ID="02434_insert_init_${CLICKHOUSE_DATABASE}_$RANDOM" insert_data $CLICKHOUSE_CLIENT -q 'select count() from dedup_test' @@ -66,12 +73,20 @@ export -f thread_insert; export -f thread_select; export -f thread_cancel; -TIMEOUT=30 +TIMEOUT=40 # 10 seconds for each TYPE timeout $TIMEOUT bash -c thread_insert & timeout $TIMEOUT bash -c thread_select & -timeout $TIMEOUT bash -c thread_cancel & +timeout $TIMEOUT bash -c thread_cancel 2> /dev/null & wait $CLICKHOUSE_CLIENT -q 'select count() from dedup_test' + +$CLICKHOUSE_CLIENT -q 'system flush logs' + +# We have to ignore stderr from thread_cancel, because our CI finds a bug in ps... +# So use this query to check that thread_cancel do something +$CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( + message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or + message like '%Connection reset by peer%')" diff --git a/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh b/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh deleted file mode 100755 index 4eb02c38c1a..00000000000 --- a/tests/queries/0_stateless/02434_insert_cancellation_native_protocol.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env bash -# shellcheck disable=SC2009 - -CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -# shellcheck source=../shell_config.sh -. "$CURDIR"/../shell_config.sh - -export DATA_FILE="$CLICKHOUSE_TMP/deduptest.tsv" -export TEST_MARK="02434_insert_${CLICKHOUSE_DATABASE}_" - -$CLICKHOUSE_CLIENT -q 'select * from numbers(5000000) format TSV' > $DATA_FILE -$CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by A settings non_replicated_deduplication_window=1000;' -$CLICKHOUSE_CLIENT --max_block_size=100000 --min_chunk_bytes_for_parallel_parsing=10000 -q 'insert into dedup_test format TSV' < $DATA_FILE -$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' - -function thread_insert -{ - # supress "Killed" messages from bash - function wrap - { - $CLICKHOUSE_CLIENT --max_block_size=100000 --min_chunk_bytes_for_parallel_parsing=10000 --query_id="$ID" -q 'insert into dedup_test format TSV' < $DATA_FILE - } - export -f wrap - while true; do - export ID="$TEST_MARK$RANDOM" - bash -c wrap 2>&1| grep -Fav "Killed" - done -} - -function thread_select -{ - while true; do - $CLICKHOUSE_CLIENT -q "with (select count() from dedup_test) as c select throwIf(c != 5000000, 'Expected 5000000 rows, got ' || toString(c)) format Null" - sleep 0.$RANDOM; - done -} - -function thread_cancel -{ - while true; do - SIGNAL="INT" - if (( RANDOM % 2 )); then - SIGNAL="KILL" - fi - PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') - if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID" || echo "$PID"; fi - sleep 0.$RANDOM; - sleep 0.$RANDOM; - sleep 0.$RANDOM; - done -} - -export -f thread_insert; -export -f thread_select; -export -f thread_cancel; - -TIMEOUT=30 - -timeout $TIMEOUT bash -c thread_insert & -timeout $TIMEOUT bash -c thread_select & -timeout $TIMEOUT bash -c thread_cancel & - -wait - -$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' diff --git a/tests/queries/0_stateless/02435_insert_cancellation_http.reference b/tests/queries/0_stateless/02435_insert_cancellation_http.reference deleted file mode 100644 index e3038e03530..00000000000 --- a/tests/queries/0_stateless/02435_insert_cancellation_http.reference +++ /dev/null @@ -1,2 +0,0 @@ -5000000 -5000000 diff --git a/utils/check-mysql-binlog/main.cpp b/utils/check-mysql-binlog/main.cpp index 7dd387ba5be..cf2a27e8aac 100644 --- a/utils/check-mysql-binlog/main.cpp +++ b/utils/check-mysql-binlog/main.cpp @@ -17,7 +17,8 @@ static DB::MySQLReplication::BinlogEventPtr parseSingleEventBody( std::shared_ptr & last_table_map_event, bool exist_checksum) { DB::MySQLReplication::BinlogEventPtr event; - DB::ReadBufferPtr limit_read_buffer = std::make_shared(payload, header.event_size - 19, false); + DB::ReadBufferPtr limit_read_buffer = std::make_shared(payload, header.event_size - 19, + /* trow_exception */ false, /* exact_limit */ {}); DB::ReadBufferPtr event_payload = std::make_shared(*limit_read_buffer, exist_checksum ? 4 : 0); switch (header.type) From 240e0070e5b4ea55b22a687fcad375131cbdad70 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 23 Feb 2023 23:07:35 +0100 Subject: [PATCH 082/342] fix --- src/Interpreters/AsynchronousInsertQueue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interpreters/AsynchronousInsertQueue.cpp b/src/Interpreters/AsynchronousInsertQueue.cpp index ac8101d4ca2..78b173de6dc 100644 --- a/src/Interpreters/AsynchronousInsertQueue.cpp +++ b/src/Interpreters/AsynchronousInsertQueue.cpp @@ -210,7 +210,7 @@ AsynchronousInsertQueue::push(ASTPtr query, ContextPtr query_context) /// to avoid buffering of huge amount of data in memory. auto read_buf = getReadBufferFromASTInsertQuery(query); - LimitReadBuffer limit_buf(*read_buf, settings.async_insert_max_data_size, false); + LimitReadBuffer limit_buf(*read_buf, settings.async_insert_max_data_size, /* trow_exception */ false, /* exact_limit */ {}); WriteBufferFromString write_buf(bytes); copyData(limit_buf, write_buf); From 5e5e802348af021c6dbeefd5a05e2ad98c1a21bf Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Thu, 23 Feb 2023 22:54:50 +0000 Subject: [PATCH 083/342] return back optimization for ALTER MODIFY Nullable --- src/Storages/MergeTree/MutateTask.cpp | 45 +++++++++++++++++-- .../02669_alter_modify_to_nullable.reference | 8 ++++ .../02669_alter_modify_to_nullable.sql | 31 +++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tests/queries/0_stateless/02669_alter_modify_to_nullable.reference create mode 100644 tests/queries/0_stateless/02669_alter_modify_to_nullable.sql diff --git a/src/Storages/MergeTree/MutateTask.cpp b/src/Storages/MergeTree/MutateTask.cpp index 8d95a76b5da..de623c58538 100644 --- a/src/Storages/MergeTree/MutateTask.cpp +++ b/src/Storages/MergeTree/MutateTask.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -1568,6 +1569,45 @@ bool MutateTask::execute() return false; } +static bool canSkipConversionToNullable(const MergeTreeDataPartPtr & part, const MutationCommand & command) +{ + if (command.type != MutationCommand::READ_COLUMN) + return false; + + auto part_column = part->tryGetColumn(command.column_name); + if (!part_column) + return false; + + /// For ALTER MODIFY COLUMN from 'Type' to 'Nullable(Type)' we can skip mutatation and + /// apply only metadata conversion. But it doesn't work for custom serialization. + const auto * to_nullable = typeid_cast(command.data_type.get()); + if (!to_nullable) + return false; + + if (!part_column->type->equals(*to_nullable->getNestedType())) + return false; + + auto serialization = part->getSerialization(command.column_name); + if (serialization->getKind() != ISerialization::Kind::DEFAULT) + return false; + + return true; +} + +static bool canSkipMutationCommandForPart(const MergeTreeDataPartPtr & part, const MutationCommand & command, const ContextPtr & context) +{ + if (command.partition) + { + auto command_partition_id = part->storage.getPartitionIDFromQuery(command.partition, context); + if (part->info.partition_id != command_partition_id) + return true; + } + + if (canSkipConversionToNullable(part, command)) + return true; + + return false; +} bool MutateTask::prepare() { @@ -1586,11 +1626,8 @@ bool MutateTask::prepare() context_for_reading->setSetting("force_primary_key", false); for (const auto & command : *ctx->commands) - { - if (command.partition == nullptr || ctx->source_part->info.partition_id == ctx->data->getPartitionIDFromQuery( - command.partition, context_for_reading)) + if (!canSkipMutationCommandForPart(ctx->source_part, command, context_for_reading)) ctx->commands_for_part.emplace_back(command); - } if (ctx->source_part->isStoredOnDisk() && !isStorageTouchedByMutations( *ctx->data, ctx->source_part, ctx->metadata_snapshot, ctx->commands_for_part, context_for_reading)) diff --git a/tests/queries/0_stateless/02669_alter_modify_to_nullable.reference b/tests/queries/0_stateless/02669_alter_modify_to_nullable.reference new file mode 100644 index 00000000000..aff80e1d699 --- /dev/null +++ b/tests/queries/0_stateless/02669_alter_modify_to_nullable.reference @@ -0,0 +1,8 @@ +1_1_1_0 String Default +2_2_2_0 String Sparse +20000 10435 ['','bar','foo'] +1_1_1_0_3 String Default +2_2_2_0_3 Nullable(String) Default +20000 10435 ['','bar','foo'] +1_1_1_0_3 0 +2_2_2_0_3 10000 diff --git a/tests/queries/0_stateless/02669_alter_modify_to_nullable.sql b/tests/queries/0_stateless/02669_alter_modify_to_nullable.sql new file mode 100644 index 00000000000..862280fd7cd --- /dev/null +++ b/tests/queries/0_stateless/02669_alter_modify_to_nullable.sql @@ -0,0 +1,31 @@ +DROP TABLE IF EXISTS t_modify_to_nullable; + +CREATE TABLE t_modify_to_nullable (key UInt64, id UInt64, s String) +ENGINE = MergeTree ORDER BY id PARTITION BY key +SETTINGS min_bytes_for_wide_part = 0, ratio_of_defaults_for_sparse_serialization = 0.9; + +INSERT INTO t_modify_to_nullable SELECT 1, number, 'foo' FROM numbers(10000); +INSERT INTO t_modify_to_nullable SELECT 2, number, if (number % 23 = 0, 'bar', '') FROM numbers(10000); + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_modify_to_nullable' AND column = 's' AND active +ORDER BY name; + +SELECT count(s), countIf(s != ''), arraySort(groupUniqArray(s)) FROM t_modify_to_nullable; + +SET mutations_sync = 2; +ALTER TABLE t_modify_to_nullable MODIFY COLUMN s Nullable(String); + +SELECT name, type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_modify_to_nullable' AND column = 's' AND active +ORDER BY name; + +SELECT count(s), countIf(s != ''), arraySort(groupUniqArray(s)) FROM t_modify_to_nullable; + +SYSTEM FLUSH LOGS; + +SELECT part_name, read_rows FROM system.part_log +WHERE database = currentDatabase() AND table = 't_modify_to_nullable' AND event_type = 'MutatePart' +ORDER BY part_name; + +DROP TABLE t_modify_to_nullable; From 4de9c9152940e334bb7a971b1bf6fc6bacb6499c Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Fri, 24 Feb 2023 01:48:06 +0100 Subject: [PATCH 084/342] add a test for transactions --- .../02434_cancel_insert_when_client_dies.sh | 2 +- ...02435_rollback_cancelled_queries.reference | 3 + .../02435_rollback_cancelled_queries.sh | 118 ++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/02435_rollback_cancelled_queries.reference create mode 100755 tests/queries/0_stateless/02435_rollback_cancelled_queries.sh diff --git a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh index c2cf98f712f..6cae90a3cc3 100755 --- a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh @@ -62,7 +62,7 @@ function thread_cancel SIGNAL="KILL" fi PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') - if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID" || echo "$PID"; fi + if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID"; fi sleep 0.$RANDOM; sleep 0.$RANDOM; sleep 0.$RANDOM; diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference b/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference new file mode 100644 index 00000000000..94c627bb5d8 --- /dev/null +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference @@ -0,0 +1,3 @@ +1000000 +0 1 +1 diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh new file mode 100755 index 00000000000..bc16c54cd1e --- /dev/null +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash +# Tags: no-random-settings, no-ordinary-database +# shellcheck disable=SC2009 + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +export DATA_FILE="$CLICKHOUSE_TMP/deduptest.tsv" +export TEST_MARK="02435_insert_${CLICKHOUSE_DATABASE}_" +export SESSION="02435_session_${CLICKHOUSE_DATABASE}" + +$CLICKHOUSE_CLIENT -q 'select * from numbers(1000000) format TSV' > $DATA_FILE +$CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by sin(A) partition by intDiv(A, 100000)' + +function insert_data +{ + IMPLICIT=$(( RANDOM % 2 )) + SESSION_ID="${SESSION}_$RANDOM$RANDOM$RANDOM" + TXN_SETTINGS="session_id=$SESSION_ID&throw_on_unsupported_query_inside_transaction=0" + BEGIN="" + COMMIT="" + SETTINGS="query_id=$ID&$TXN_SETTINGS&max_insert_block_size=110000&min_insert_block_size_rows=110000" + if [[ "$IMPLICIT" -eq 0 ]]; then + $CLICKHOUSE_CURL -sS -d 'begin transaction' "$CLICKHOUSE_URL&$TXN_SETTINGS" + BEGIN="begin transaction;" + COMMIT=$(echo -ne "\n\ncommit") + else + TXN_SETTINGS="$TXN_SETTINGS&implicit_transaction=1" + fi + + SETTINGS="query_id=$ID&$TXN_SETTINGS&max_insert_block_size=110000&min_insert_block_size_rows=110000" + # max_block_size=10000, so external table will contain smaller blocks that will be squashed on insert-select (more chances to catch a bug on query cancellation) + TRASH_SETTINGS="query_id=$ID&$TXN_SETTINGS&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_insert_block_size=110000&max_block_size=10000&min_insert_block_size_bytes=0&min_insert_block_size_rows=110000&max_insert_block_size=110000" + TYPE=$(( RANDOM % 6 )) + + if [[ "$TYPE" -eq 0 ]]; then + $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + elif [[ "$TYPE" -eq 1 ]]; then + $CLICKHOUSE_CURL -sS -X POST -H "Transfer-Encoding: chunked" --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + elif [[ "$TYPE" -eq 2 ]]; then + $CLICKHOUSE_CURL -sS -F 'file=@-' "$CLICKHOUSE_URL&$TRASH_SETTINGS&file_format=TSV&file_types=UInt64" -X POST --form-string 'query=insert into dedup_test select * from file' < $DATA_FILE + else + # client will send 1000-rows blocks, server will squash them into 110000-rows blocks (more chances to catch a bug on query cancellation) + $CLICKHOUSE_CLIENT --query_id="$ID" --throw_on_unsupported_query_inside_transaction=0 --implicit_transaction="$IMPLICIT" \ + --max_block_size=1000 --max_insert_block_size=1000 --multiquery -q \ + "${BEGIN}insert into dedup_test settings max_insert_block_size=110000, min_insert_block_size_rows=110000 format TSV$COMMIT" < $DATA_FILE \ + | grep -Fv "Transaction is not in RUNNING state" + fi + + if [[ "$IMPLICIT" -eq 0 ]]; then + $CLICKHOUSE_CURL -sS -d 'commit' "$CLICKHOUSE_URL&$TXN_SETTINGS" | grep -Faq "Transaction is not in RUNNING state" && $CLICKHOUSE_CURL -sS -d 'rollback' "$CLICKHOUSE_URL&$TXN_SETTINGS" + fi +} + +export -f insert_data + +ID="02435_insert_init_${CLICKHOUSE_DATABASE}_$RANDOM" +insert_data +$CLICKHOUSE_CLIENT -q 'select count() from dedup_test' + +function thread_insert +{ + # supress "Killed" messages from bash + while true; do + export ID="$TEST_MARK$RANDOM" + bash -c insert_data 2>&1| grep -Fav "Killed" + done +} + +function thread_select +{ + while true; do + $CLICKHOUSE_CLIENT --implicit_transaction=1 -q "with (select count() from dedup_test) as c select throwIf(c % 1000000 != 0, 'Expected 1000000 * N rows, got ' || toString(c)) format Null" + sleep 0.$RANDOM; + done +} + +function thread_cancel +{ + while true; do + SIGNAL="INT" + if (( RANDOM % 2 )); then + SIGNAL="KILL" + fi + PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') + if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID"; fi + sleep 0.$RANDOM; + done +} + +export -f thread_insert; +export -f thread_select; +export -f thread_cancel; + +TIMEOUT=20 # 5 seconds for each TYPE + +timeout $TIMEOUT bash -c thread_insert & +timeout $TIMEOUT bash -c thread_select & +timeout $TIMEOUT bash -c thread_cancel 2> /dev/null & + +wait +wait_for_queries_to_finish + +$CLICKHOUSE_CLIENT -q 'system flush logs' + +ID="02435_insert_last_${CLICKHOUSE_DATABASE}_$RANDOM" +insert_data + +$CLICKHOUSE_CLIENT --implicit_transaction=1 -q 'select count() % 1000000, count() > 0 from dedup_test' + +# We have to ignore stderr from thread_cancel, because our CI finds a bug in ps... +# So use this query to check that thread_cancel do something +$CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( + message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or + message like '%Connection reset by peer%')" + +$CLICKHOUSE_CLIENT --database_atomic_wait_for_drop_and_detach_synchronously=0 -q "drop table dedup_test" From 6f4c742408f77202c0740829d0d10d2d756ea9e9 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Fri, 24 Feb 2023 02:42:45 +0100 Subject: [PATCH 085/342] fix --- .../0_stateless/02435_rollback_cancelled_queries.reference | 2 +- .../queries/0_stateless/02435_rollback_cancelled_queries.sh | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference b/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference index 94c627bb5d8..2d32c17ec7c 100644 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.reference @@ -1,3 +1,3 @@ 1000000 -0 1 +0 1 diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh index bc16c54cd1e..18317960cdf 100755 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -42,7 +42,7 @@ function insert_data $CLICKHOUSE_CURL -sS -F 'file=@-' "$CLICKHOUSE_URL&$TRASH_SETTINGS&file_format=TSV&file_types=UInt64" -X POST --form-string 'query=insert into dedup_test select * from file' < $DATA_FILE else # client will send 1000-rows blocks, server will squash them into 110000-rows blocks (more chances to catch a bug on query cancellation) - $CLICKHOUSE_CLIENT --query_id="$ID" --throw_on_unsupported_query_inside_transaction=0 --implicit_transaction="$IMPLICIT" \ + $CLICKHOUSE_CLIENT --stacktrace --query_id="$ID" --throw_on_unsupported_query_inside_transaction=0 --implicit_transaction="$IMPLICIT" \ --max_block_size=1000 --max_insert_block_size=1000 --multiquery -q \ "${BEGIN}insert into dedup_test settings max_insert_block_size=110000, min_insert_block_size_rows=110000 format TSV$COMMIT" < $DATA_FILE \ | grep -Fv "Transaction is not in RUNNING state" @@ -107,7 +107,8 @@ $CLICKHOUSE_CLIENT -q 'system flush logs' ID="02435_insert_last_${CLICKHOUSE_DATABASE}_$RANDOM" insert_data -$CLICKHOUSE_CLIENT --implicit_transaction=1 -q 'select count() % 1000000, count() > 0 from dedup_test' +$CLICKHOUSE_CLIENT --implicit_transaction=1 -q 'select throwIf(count() % 1000000 != 0 or count() = 0) from dedup_test' \ + || $CLICKHOUSE_CLIENT -q "select name, rows, active, visible, creation_tid, creation_csn from system.parts where database=currentDatabase();" # We have to ignore stderr from thread_cancel, because our CI finds a bug in ps... # So use this query to check that thread_cancel do something From 7122ebab4d696b45add7f3d40b066e31a4093b18 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Fri, 24 Feb 2023 03:06:33 +0100 Subject: [PATCH 086/342] fix clickhouse-test --- tests/clickhouse-test | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/clickhouse-test b/tests/clickhouse-test index f5d1099c3f4..cfd2546bbdd 100755 --- a/tests/clickhouse-test +++ b/tests/clickhouse-test @@ -73,13 +73,7 @@ def stringhash(s): # First and last lines of the log def trim_for_log(s): - if not s: - return s - lines = s.splitlines() - if len(lines) > 100: - return "\n".join(lines[:50] + ["#" * 100] + lines[-50:]) - else: - return "\n".join(lines) + return s class HTTPError(Exception): From 8227a6e7bfbdaf0d9eb9f71be909dccf3013a3cc Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 24 Feb 2023 09:12:08 +0000 Subject: [PATCH 087/342] Fix tests --- .../Passes/LogicalExpressionOptimizer.cpp | 30 +-- .../Passes/LogicalExpressionOptimizerPass.h | 50 ++++- ...00621_regression_for_in_operator.reference | 10 +- .../00736_disjunction_optimisation.reference | 210 ++++++------------ ...ssions_optimizer_low_cardinality.reference | 20 +- ...imizer_removing_redundant_checks.reference | 89 ++++++++ 6 files changed, 221 insertions(+), 188 deletions(-) diff --git a/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp index c29d38c7865..85b44de74da 100644 --- a/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp +++ b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp @@ -19,7 +19,6 @@ public: explicit LogicalExpressionOptimizerVisitor(ContextPtr context) : Base(std::move(context)) - , cast_function_resolver(FunctionFactory::instance().get("_CAST", getContext())) {} void visitImpl(QueryTreeNodePtr & node) @@ -86,15 +85,11 @@ private: bool collapse_to_false = false; if (const auto * lhs_literal = lhs->as()) - { collapse_to_false = has_and_with_different_constant(rhs, lhs_literal); - } else if (const auto * rhs_literal = rhs->as()) - { collapse_to_false = has_and_with_different_constant(lhs, rhs_literal); - } else - continue; + and_operands.push_back(argument); if (collapse_to_false) { @@ -108,7 +103,7 @@ private: if (and_operands.size() == 1) { assert(!function_node.getResultType()->isNullable()); - resolveAsCast(function_node, std::move(and_operands[0])); + node = std::move(and_operands[0]); return; } @@ -210,7 +205,7 @@ private: if (or_operands.size() == 1) { assert(!function_node.getResultType()->isNullable()); - resolveAsCast(function_node, std::move(or_operands[0])); + node = std::move(or_operands[0]); return; } @@ -218,27 +213,8 @@ private: function_node.getArguments().getNodes() = std::move(or_operands); function_node.resolveAsFunction(or_function_resolver); } - - void resolveAsCast(FunctionNode & function_node, QueryTreeNodePtr operand) - { - std::string cast_type = function_node.getResultType()->getName(); - auto cast_type_constant_value = std::make_shared(std::move(cast_type), std::make_shared()); - auto cast_type_constant_node = std::make_shared(std::move(cast_type_constant_value)); - - QueryTreeNodes arguments; - arguments.reserve(2); - arguments.push_back(std::move(operand)); - arguments.push_back(std::move(cast_type_constant_node)); - - function_node.getArguments().getNodes() = std::move(arguments); - - function_node.resolveAsFunction(cast_function_resolver); - } - - const FunctionOverloadResolverPtr cast_function_resolver; }; - void LogicalExpressionOptimizerPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context) { LogicalExpressionOptimizerVisitor visitor(std::move(context)); diff --git a/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h index 327ae3c3fee..b436c94fe4c 100644 --- a/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h +++ b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h @@ -6,10 +6,12 @@ namespace DB { /** - * This pass replaces chains of equality functions inside an OR with a single IN operator. + * This pass tries to do optimizations on logical expression + * + * Replaces chains of equality functions inside an OR with a single IN operator. * The replacement is done if: - * - rhs of the equality function is a constant - * - length of chain is at least 'optimize_min_equality_disjunction_chain_length' long OR lhs is LowCardinality + * - one of the operands of the equality function is a constant + * - length of chain is at least 'optimize_min_equality_disjunction_chain_length' long OR the expression has type of LowCardinality * * E.g. (optimize_min_equality_disjunction_chain_length = 2) * ------------------------------- @@ -23,6 +25,48 @@ namespace DB * FROM TABLE * WHERE b = 'test' OR a IN (1, 2); * ------------------------------- + * + * Removes duplicate OR checks + * ------------------------------- + * SELECT * + * FROM table + * WHERE a = 1 OR b = 'test' OR a = 1; + * + * will be transformed into + * + * SELECT * + * FROM TABLE + * WHERE a = 1 OR b = 'test'; + * ------------------------------- + * + * Replaces AND chains with a single constant. + * The replacement is done if: + * - one of the operands of the equality function is a constant + * - constants are different for same expression + * ------------------------------- + * SELECT * + * FROM table + * WHERE a = 1 AND b = 'test' AND a = 2; + * + * will be transformed into + * + * SELECT * + * FROM TABLE + * WHERE 0; + * ------------------------------- + * + * Removes duplicate AND checks + * ------------------------------- + * SELECT * + * FROM table + * WHERE a = 1 AND b = 'test' AND a = 1; + * + * will be transformed into + * + * SELECT * + * FROM TABLE + * WHERE a = 1 AND b = 'test'; + * ------------------------------- */ class LogicalExpressionOptimizerPass final : public IQueryTreePass diff --git a/tests/queries/0_stateless/00621_regression_for_in_operator.reference b/tests/queries/0_stateless/00621_regression_for_in_operator.reference index 54b345b65c0..ab8bcf307eb 100644 --- a/tests/queries/0_stateless/00621_regression_for_in_operator.reference +++ b/tests/queries/0_stateless/00621_regression_for_in_operator.reference @@ -12,15 +12,11 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.regression_for_in_operator_view WHERE - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 8, column_name: g, result_type: String, source_id: 3 - CONSTANT id: 9, constant_value: Tuple_(\'5\', \'6\'), constant_value_type: Tuple(String, String) - CONSTANT id: 10, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 6, column_name: g, result_type: String, source_id: 3 + CONSTANT id: 7, constant_value: Tuple_(\'5\', \'6\'), constant_value_type: Tuple(String, String) SETTINGS allow_experimental_analyzer=1 2 2 diff --git a/tests/queries/0_stateless/00736_disjunction_optimisation.reference b/tests/queries/0_stateless/00736_disjunction_optimisation.reference index 4b899527913..84477a64057 100644 --- a/tests/queries/0_stateless/00736_disjunction_optimisation.reference +++ b/tests/queries/0_stateless/00736_disjunction_optimisation.reference @@ -39,24 +39,16 @@ QUERY id: 0 FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 6, nodes: 2 - FUNCTION id: 7, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 7, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 8, nodes: 2 - FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 10, nodes: 2 - COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 3 - CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 13, constant_value: \'UInt8\', constant_value_type: String - FUNCTION id: 14, function_name: _CAST, function_type: ordinary, result_type: UInt8 + COLUMN id: 9, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 10, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS - LIST id: 15, nodes: 2 - FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 17, nodes: 2 - COLUMN id: 18, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 19, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 20, constant_value: \'UInt8\', constant_value_type: String + LIST id: 12, nodes: 2 + COLUMN id: 13, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 14, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) SETTINGS allow_experimental_analyzer=1 1 21 1 22 @@ -96,25 +88,17 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: default.bug WHERE - FUNCTION id: 9, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 10, nodes: 2 - FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 12, nodes: 2 - COLUMN id: 13, column_name: k, result_type: UInt64, source_id: 7 - CONSTANT id: 14, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 15, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 7 + CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) WHERE - FUNCTION id: 16, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 13, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS - LIST id: 17, nodes: 2 - FUNCTION id: 18, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 19, nodes: 2 - COLUMN id: 20, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 22, constant_value: \'UInt8\', constant_value_type: String + LIST id: 14, nodes: 2 + COLUMN id: 15, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 16, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) SETTINGS allow_experimental_analyzer=1 1 1 21 1 1 1 1 1 22 0 1 1 @@ -145,39 +129,27 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 6 COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: UInt8 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 8, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 9, nodes: 2 + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 10, constant_value: UInt64_21, constant_value_type: UInt8 + FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 12, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 13, constant_value: UInt64_21, constant_value_type: UInt8 - FUNCTION id: 14, function_name: _CAST, function_type: ordinary, result_type: UInt8 + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 13, constant_value: Tuple_(UInt64_21, UInt64_22), constant_value_type: Tuple(UInt8, UInt8) + FUNCTION id: 14, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 15, nodes: 2 - FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 17, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 18, constant_value: Tuple_(UInt64_21, UInt64_22), constant_value_type: Tuple(UInt8, UInt8) - CONSTANT id: 19, constant_value: \'UInt8\', constant_value_type: String - FUNCTION id: 20, function_name: _CAST, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 21, nodes: 2 - FUNCTION id: 22, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 23, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 24, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 25, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 16, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -206,15 +178,11 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -259,24 +227,16 @@ QUERY id: 0 FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 6, nodes: 2 - FUNCTION id: 7, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 7, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 8, nodes: 2 - FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 10, nodes: 2 - COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 3 - CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 13, constant_value: \'UInt8\', constant_value_type: String - FUNCTION id: 14, function_name: _CAST, function_type: ordinary, result_type: UInt8 + COLUMN id: 9, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 10, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS - LIST id: 15, nodes: 2 - FUNCTION id: 16, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 17, nodes: 2 - COLUMN id: 18, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 19, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 20, constant_value: \'UInt8\', constant_value_type: String + LIST id: 12, nodes: 2 + COLUMN id: 13, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 14, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) SETTINGS allow_experimental_analyzer=1 1 21 1 22 @@ -316,25 +276,17 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: default.bug WHERE - FUNCTION id: 9, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 9, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 10, nodes: 2 - FUNCTION id: 11, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 12, nodes: 2 - COLUMN id: 13, column_name: k, result_type: UInt64, source_id: 7 - CONSTANT id: 14, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 15, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 11, column_name: k, result_type: UInt64, source_id: 7 + CONSTANT id: 12, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) WHERE - FUNCTION id: 16, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 13, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS - LIST id: 17, nodes: 2 - FUNCTION id: 18, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 19, nodes: 2 - COLUMN id: 20, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 22, constant_value: \'UInt8\', constant_value_type: String + LIST id: 14, nodes: 2 + COLUMN id: 15, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 16, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) SETTINGS allow_experimental_analyzer=1 1 1 21 1 1 1 1 1 22 0 1 1 @@ -365,43 +317,35 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 6 COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: UInt8 + COLUMN id: 2, column_name: k, result_type: UInt64, source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(UInt64_1, UInt64_2, UInt64_3), constant_value_type: Tuple(UInt8, UInt8, UInt8) + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + FUNCTION id: 8, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 9, nodes: 2 + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 10, constant_value: UInt64_21, constant_value_type: UInt8 + FUNCTION id: 11, function_name: or, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 12, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 13, constant_value: UInt64_21, constant_value_type: UInt8 - FUNCTION id: 14, function_name: or, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 15, nodes: 2 + FUNCTION id: 13, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 14, nodes: 2 + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 15, constant_value: UInt64_21, constant_value_type: UInt8 FUNCTION id: 16, function_name: equals, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 17, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 18, constant_value: UInt64_21, constant_value_type: UInt8 - FUNCTION id: 19, function_name: equals, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 20, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 21, constant_value: UInt64_22, constant_value_type: UInt8 - FUNCTION id: 22, function_name: _CAST, function_type: ordinary, result_type: UInt8 + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 18, constant_value: UInt64_22, constant_value_type: UInt8 + FUNCTION id: 19, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS - LIST id: 23, nodes: 2 - FUNCTION id: 24, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 25, nodes: 2 - COLUMN id: 10, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 26, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 27, constant_value: \'UInt8\', constant_value_type: String + LIST id: 20, nodes: 2 + COLUMN id: 7, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 21, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -430,15 +374,11 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 @@ -467,15 +407,11 @@ QUERY id: 0 PROJECTION LIST id: 1, nodes: 2 COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 2, column_name: s, result_type: UInt64, source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(UInt64_21, UInt64_22, UInt64_23), constant_value_type: Tuple(UInt8, UInt8, UInt8) JOIN TREE TABLE id: 3, table_name: default.bug SETTINGS allow_experimental_analyzer=1 diff --git a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference index 22bd68e0ac1..84589668d64 100644 --- a/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference +++ b/tests/queries/0_stateless/02477_logical_expressions_optimizer_low_cardinality.reference @@ -10,15 +10,11 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality WHERE - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) SETTINGS allow_experimental_analyzer=1 SELECT a FROM t_logical_expressions_optimizer_low_cardinality @@ -32,15 +28,11 @@ QUERY id: 0 JOIN TREE TABLE id: 3, table_name: default.t_logical_expressions_optimizer_low_cardinality WHERE - FUNCTION id: 4, function_name: _CAST, function_type: ordinary, result_type: UInt8 + FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8 ARGUMENTS LIST id: 5, nodes: 2 - FUNCTION id: 6, function_name: in, function_type: ordinary, result_type: UInt8 - ARGUMENTS - LIST id: 7, nodes: 2 - COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 - CONSTANT id: 8, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) - CONSTANT id: 9, constant_value: \'UInt8\', constant_value_type: String + COLUMN id: 2, column_name: a, result_type: LowCardinality(String), source_id: 3 + CONSTANT id: 6, constant_value: Tuple_(\'x\', \'y\'), constant_value_type: Tuple(String, String) SETTINGS allow_experimental_analyzer=1 SELECT a FROM t_logical_expressions_optimizer_low_cardinality diff --git a/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference b/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference index e69de29bb2d..d083e178586 100644 --- a/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference +++ b/tests/queries/0_stateless/02668_logical_optimizer_removing_redundant_checks.reference @@ -0,0 +1,89 @@ +1 test +3 another +QUERY id: 0 + PROJECTION COLUMNS + a Int32 + b LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: a, result_type: Int32, source_id: 3 + COLUMN id: 4, column_name: b, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.02668_logical_optimizer + WHERE + FUNCTION id: 5, function_name: in, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 6, nodes: 2 + COLUMN id: 7, column_name: a, result_type: Int32, source_id: 3 + CONSTANT id: 8, constant_value: Tuple_(UInt64_1, UInt64_3), constant_value_type: Tuple(UInt8, UInt8) +1 test +QUERY id: 0 + PROJECTION COLUMNS + a Int32 + b LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: a, result_type: Int32, source_id: 3 + COLUMN id: 4, column_name: b, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.02668_logical_optimizer + WHERE + FUNCTION id: 5, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 6, nodes: 2 + COLUMN id: 7, column_name: a, result_type: Int32, source_id: 3 + CONSTANT id: 8, constant_value: UInt64_1, constant_value_type: UInt8 +QUERY id: 0 + PROJECTION COLUMNS + a Int32 + b LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: a, result_type: Int32, source_id: 3 + COLUMN id: 4, column_name: b, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.02668_logical_optimizer + WHERE + CONSTANT id: 5, constant_value: UInt64_0, constant_value_type: UInt8 +3 another +QUERY id: 0 + PROJECTION COLUMNS + a Int32 + b LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: a, result_type: Int32, source_id: 3 + COLUMN id: 4, column_name: b, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.02668_logical_optimizer + WHERE + FUNCTION id: 5, function_name: and, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 6, nodes: 2 + FUNCTION id: 7, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 8, nodes: 2 + COLUMN id: 9, column_name: a, result_type: Int32, source_id: 3 + CONSTANT id: 10, constant_value: UInt64_3, constant_value_type: UInt8 + FUNCTION id: 11, function_name: equals, function_type: ordinary, result_type: LowCardinality(UInt8) + ARGUMENTS + LIST id: 12, nodes: 2 + COLUMN id: 13, column_name: b, result_type: LowCardinality(String), source_id: 3 + CONSTANT id: 14, constant_value: \'another\', constant_value_type: String +2 test2 +QUERY id: 0 + PROJECTION COLUMNS + a Int32 + b LowCardinality(String) + PROJECTION + LIST id: 1, nodes: 2 + COLUMN id: 2, column_name: a, result_type: Int32, source_id: 3 + COLUMN id: 4, column_name: b, result_type: LowCardinality(String), source_id: 3 + JOIN TREE + TABLE id: 3, table_name: default.02668_logical_optimizer + WHERE + FUNCTION id: 5, function_name: equals, function_type: ordinary, result_type: UInt8 + ARGUMENTS + LIST id: 6, nodes: 2 + COLUMN id: 7, column_name: a, result_type: Int32, source_id: 3 + CONSTANT id: 8, constant_value: UInt64_2, constant_value_type: UInt8 From a587bcbb8db253647949b6fb657ae9ba85bcdc9b Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 24 Feb 2023 12:14:51 +0000 Subject: [PATCH 088/342] Use initial range for fetching file size --- src/IO/ReadWriteBufferFromHTTP.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/IO/ReadWriteBufferFromHTTP.h b/src/IO/ReadWriteBufferFromHTTP.h index 5bc72c5ff62..dd8a3376607 100644 --- a/src/IO/ReadWriteBufferFromHTTP.h +++ b/src/IO/ReadWriteBufferFromHTTP.h @@ -118,6 +118,7 @@ namespace detail bool use_external_buffer; size_t offset_from_begin_pos = 0; + const Range initial_read_range; Range read_range; std::optional file_size; @@ -145,6 +146,7 @@ namespace detail size_t getOffset() const { return getRangeBegin() + offset_from_begin_pos; } + template std::istream * callImpl(Poco::URI uri_, Poco::Net::HTTPResponse & response, const std::string & method_) { // With empty path poco will send "POST HTTP/1.1" its bug. @@ -154,6 +156,12 @@ namespace detail Poco::Net::HTTPRequest request(method_, uri_.getPathAndQuery(), Poco::Net::HTTPRequest::HTTP_1_1); request.setHost(uri_.getHost()); // use original, not resolved host name in header + Range range; + if constexpr (use_initial_range) + range = Range{initial_read_range.begin.value_or(0), initial_read_range.end}; + else + range = Range{getOffset(), read_range.end}; + if (out_stream_callback) request.setChunkedTransferEncoding(true); @@ -163,10 +171,10 @@ namespace detail if (withPartialContent()) { String range_header_value; - if (read_range.end) - range_header_value = fmt::format("bytes={}-{}", getOffset(), *read_range.end); + if (range.end) + range_header_value = fmt::format("bytes={}-{}", *range.begin, *range.end); else - range_header_value = fmt::format("bytes={}-", getOffset()); + range_header_value = fmt::format("bytes={}-", *range.begin); LOG_TEST(log, "Adding header: Range: {}", range_header_value); request.set("Range", range_header_value); } @@ -206,7 +214,7 @@ namespace detail return *file_size; Poco::Net::HTTPResponse response; - getHeadResponse(response); + getHeadResponse(response); if (response.hasContentLength()) { @@ -235,13 +243,14 @@ namespace detail InitializeError initialization_error = InitializeError::NONE; private: + template void getHeadResponse(Poco::Net::HTTPResponse & response) { for (size_t i = 0; i < settings.http_max_tries; ++i) { try { - callWithRedirects(response, Poco::Net::HTTPRequest::HTTP_HEAD); + callWithRedirects(response, Poco::Net::HTTPRequest::HTTP_HEAD); break; } catch (const Poco::Exception & e) @@ -296,6 +305,7 @@ namespace detail , remote_host_filter {remote_host_filter_} , buffer_size {buffer_size_} , use_external_buffer {use_external_buffer_} + , initial_read_range(read_range_) , read_range(read_range_) , http_skip_not_found_url(http_skip_not_found_url_) , settings {settings_} @@ -359,9 +369,10 @@ namespace detail return location_uri; } + template void callWithRedirects(Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) { - call(response, method_, throw_on_all_errors); + call(response, method_, throw_on_all_errors); Poco::URI prev_uri = uri; while (isRedirect(response.getStatus())) @@ -373,15 +384,16 @@ namespace detail session->updateSession(uri_redirect); - istr = callImpl(uri_redirect, response, method); + istr = callImpl(uri_redirect, response, method); } } + template void call(Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) { try { - istr = callImpl(saved_uri_redirect ? *saved_uri_redirect : uri, response, method_); + istr = callImpl(saved_uri_redirect ? *saved_uri_redirect : uri, response, method_); } catch (...) { From c66d5bc687aa070ea9dcb6c497040754b601d78e Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 24 Feb 2023 12:51:36 +0000 Subject: [PATCH 089/342] better --- src/IO/ReadWriteBufferFromHTTP.h | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/IO/ReadWriteBufferFromHTTP.h b/src/IO/ReadWriteBufferFromHTTP.h index dd8a3376607..e4c48fab741 100644 --- a/src/IO/ReadWriteBufferFromHTTP.h +++ b/src/IO/ReadWriteBufferFromHTTP.h @@ -133,13 +133,13 @@ namespace detail ReadSettings settings; Poco::Logger * log; - bool withPartialContent() const + bool withPartialContent(const Range & range) const { /** * Add range header if we have some passed range (for disk web) * or if we want to retry GET request on purpose. */ - return read_range.begin || read_range.end || retry_with_range_header; + return range.begin || range.end || retry_with_range_header; } size_t getRangeBegin() const { return read_range.begin.value_or(0); } @@ -156,11 +156,6 @@ namespace detail Poco::Net::HTTPRequest request(method_, uri_.getPathAndQuery(), Poco::Net::HTTPRequest::HTTP_1_1); request.setHost(uri_.getHost()); // use original, not resolved host name in header - Range range; - if constexpr (use_initial_range) - range = Range{initial_read_range.begin.value_or(0), initial_read_range.end}; - else - range = Range{getOffset(), read_range.end}; if (out_stream_callback) request.setChunkedTransferEncoding(true); @@ -168,13 +163,25 @@ namespace detail for (auto & [header, value] : http_header_entries) request.set(header, value); - if (withPartialContent()) + std::optional range; + if constexpr (use_initial_range) + { + if (withPartialContent(initial_read_range)) + range = initial_read_range; + } + else + { + if (withPartialContent(read_range)) + range = Range{getOffset(), read_range.end}; + } + + if (range) { String range_header_value; - if (range.end) - range_header_value = fmt::format("bytes={}-{}", *range.begin, *range.end); + if (range->end) + range_header_value = fmt::format("bytes={}-{}", *range->begin, *range->end); else - range_header_value = fmt::format("bytes={}-", *range.begin); + range_header_value = fmt::format("bytes={}-", *range->begin); LOG_TEST(log, "Adding header: Range: {}", range_header_value); request.set("Range", range_header_value); } @@ -448,7 +455,7 @@ namespace detail if (response.hasContentLength()) LOG_DEBUG(log, "Received response with content length: {}", response.getContentLength()); - if (withPartialContent() && response.getStatus() != Poco::Net::HTTPResponse::HTTPStatus::HTTP_PARTIAL_CONTENT) + if (withPartialContent(read_range) && response.getStatus() != Poco::Net::HTTPResponse::HTTPStatus::HTTP_PARTIAL_CONTENT) { /// Having `200 OK` instead of `206 Partial Content` is acceptable in case we retried with range.begin == 0. if (read_range.begin && *read_range.begin != 0) From 12b38ece1d6924e0e7718eb57be41c2e83931cea Mon Sep 17 00:00:00 2001 From: avogar Date: Fri, 24 Feb 2023 13:07:09 +0000 Subject: [PATCH 090/342] Fix build --- src/Processors/Formats/Impl/AvroRowInputFormat.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Processors/Formats/Impl/AvroRowInputFormat.cpp b/src/Processors/Formats/Impl/AvroRowInputFormat.cpp index 1427e6098ed..cb851c4a1e9 100644 --- a/src/Processors/Formats/Impl/AvroRowInputFormat.cpp +++ b/src/Processors/Formats/Impl/AvroRowInputFormat.cpp @@ -433,6 +433,7 @@ AvroDeserializer::DeserializeFn AvroDeserializer::createDeserializeFn(avro::Node { decoder.decodeFixed(tmp_fixed.size(), tmp_fixed); column.insertData(reinterpret_cast(tmp_fixed.data()), tmp_fixed.size()); + return true; }; } break; From 5024f11adc2557b1fbf775b312b3d290bfea97fc Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Fri, 24 Feb 2023 17:18:45 +0000 Subject: [PATCH 091/342] fix order in serialization info --- src/DataTypes/Serializations/SerializationInfo.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/DataTypes/Serializations/SerializationInfo.h b/src/DataTypes/Serializations/SerializationInfo.h index 4ae5cf8c193..7f73d053f1b 100644 --- a/src/DataTypes/Serializations/SerializationInfo.h +++ b/src/DataTypes/Serializations/SerializationInfo.h @@ -86,7 +86,8 @@ using MutableSerializationInfoPtr = std::shared_ptr; using SerializationInfos = std::vector; using MutableSerializationInfos = std::vector; -class SerializationInfoByName : public std::unordered_map +/// The order is important because info is serialized to part metadata. +class SerializationInfoByName : public std::map { public: SerializationInfoByName() = default; From 3a947ecddc5a9f30e91ef74e4f54da97b8c91f9e Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Fri, 24 Feb 2023 19:17:44 +0000 Subject: [PATCH 092/342] do not allow const and non-deterministic secondary indexes --- src/Storages/IndicesDescription.cpp | 10 +------ src/Storages/MergeTree/MergeTreeData.cpp | 1 - src/Storages/MergeTree/MergeTreeIndices.cpp | 26 +++++++++++++++++++ .../02670_constant_skip_index.reference | 0 .../0_stateless/02670_constant_skip_index.sql | 25 ++++++++++++++++++ 5 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 tests/queries/0_stateless/02670_constant_skip_index.reference create mode 100644 tests/queries/0_stateless/02670_constant_skip_index.sql diff --git a/src/Storages/IndicesDescription.cpp b/src/Storages/IndicesDescription.cpp index 2e07aceeaa9..00a3636b605 100644 --- a/src/Storages/IndicesDescription.cpp +++ b/src/Storages/IndicesDescription.cpp @@ -94,15 +94,7 @@ IndexDescription IndexDescription::getIndexFromAST(const ASTPtr & definition_ast auto syntax = TreeRewriter(context).analyze(expr_list, columns.getAllPhysical()); result.expression = ExpressionAnalyzer(expr_list, syntax, context).getActions(true); - Block block_without_columns = result.expression->getSampleBlock(); - - for (size_t i = 0; i < block_without_columns.columns(); ++i) - { - const auto & column = block_without_columns.getByPosition(i); - result.column_names.emplace_back(column.name); - result.data_types.emplace_back(column.type); - result.sample_block.insert(ColumnWithTypeAndName(column.type->createColumn(), column.type, column.name)); - } + result.sample_block = result.expression->getSampleBlock(); const auto & definition_arguments = index_definition->type->arguments; if (definition_arguments) diff --git a/src/Storages/MergeTree/MergeTreeData.cpp b/src/Storages/MergeTree/MergeTreeData.cpp index 0d4e54453d7..f49ecba1385 100644 --- a/src/Storages/MergeTree/MergeTreeData.cpp +++ b/src/Storages/MergeTree/MergeTreeData.cpp @@ -524,7 +524,6 @@ void MergeTreeData::checkProperties( for (const auto & index : new_metadata.secondary_indices) { - MergeTreeIndexFactory::instance().validate(index, attach); if (indices_names.find(index.name) != indices_names.end()) diff --git a/src/Storages/MergeTree/MergeTreeIndices.cpp b/src/Storages/MergeTree/MergeTreeIndices.cpp index 2be9ecd8de3..6ae96d00171 100644 --- a/src/Storages/MergeTree/MergeTreeIndices.cpp +++ b/src/Storages/MergeTree/MergeTreeIndices.cpp @@ -35,6 +35,7 @@ MergeTreeIndexPtr MergeTreeIndexFactory::get( { auto it = creators.find(index.type); if (it == creators.end()) + { throw Exception(ErrorCodes::INCORRECT_QUERY, "Unknown Index type '{}'. Available index types: {}", index.type, std::accumulate(creators.cbegin(), creators.cend(), std::string{}, @@ -46,6 +47,7 @@ MergeTreeIndexPtr MergeTreeIndexFactory::get( return left + ", " + right.first; }) ); + } return it->second(index); } @@ -61,8 +63,31 @@ MergeTreeIndices MergeTreeIndexFactory::getMany(const std::vectorhasArrayJoin()) + throw Exception(ErrorCodes::INCORRECT_QUERY, "Secondary index '{}' cannot contain array joins", index.name); + + try + { + index.expression->assertDeterministic(); + } + catch (Exception & e) + { + e.addMessage(fmt::format("for secondary index '{}'", index.name)); + throw; + } + + for (const auto & elem : index.sample_block) + if (elem.column && (isColumnConst(*elem.column) || elem.column->isDummy())) + throw Exception(ErrorCodes::INCORRECT_QUERY, "Secondary index '{}' cannot contain constants", index.name); + } + auto it = validators.find(index.type); if (it == validators.end()) + { throw Exception(ErrorCodes::INCORRECT_QUERY, "Unknown Index type '{}'. Available index types: {}", index.type, std::accumulate( @@ -77,6 +102,7 @@ void MergeTreeIndexFactory::validate(const IndexDescription & index, bool attach return left + ", " + right.first; }) ); + } it->second(index, attach); } diff --git a/tests/queries/0_stateless/02670_constant_skip_index.reference b/tests/queries/0_stateless/02670_constant_skip_index.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02670_constant_skip_index.sql b/tests/queries/0_stateless/02670_constant_skip_index.sql new file mode 100644 index 00000000000..97dd2ab33c9 --- /dev/null +++ b/tests/queries/0_stateless/02670_constant_skip_index.sql @@ -0,0 +1,25 @@ + +DROP TABLE IF EXISTS t_constant_index; + +CREATE TABLE t_constant_index +( + id UInt64, + INDEX t_constant_index 'foo' TYPE set(2) GRANULARITY 1 +) ENGINE = MergeTree +ORDER BY id; -- { serverError INCORRECT_QUERY } + +CREATE TABLE t_constant_index +( + id UInt64, + INDEX t_constant_index id + rand() TYPE set(2) GRANULARITY 1 +) ENGINE = MergeTree +ORDER BY id; -- { serverError BAD_ARGUMENTS } + +CREATE TABLE t_constant_index +( + id UInt64, + INDEX t_constant_index id * 2 TYPE set(2) GRANULARITY 1 +) ENGINE = MergeTree +ORDER BY id; + +DROP TABLE t_constant_index; From cad1e0b7684a691116cef95f1c418df8ef4be740 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Sat, 25 Feb 2023 01:18:34 +0100 Subject: [PATCH 093/342] fix --- src/IO/WriteBufferFromPocoSocket.cpp | 11 +++++--- src/IO/WriteBufferFromPocoSocket.h | 1 + src/Interpreters/executeQuery.cpp | 15 ++++++----- src/QueryPipeline/BlockIO.cpp | 25 ++++++++++++++++++- src/QueryPipeline/BlockIO.h | 3 ++- src/Server/TCPHandler.cpp | 16 +++++++++--- src/Storages/Distributed/DistributedSink.cpp | 20 +++++++++++++++ src/Storages/Distributed/DistributedSink.h | 2 ++ .../test_insert_into_distributed/test.py | 2 +- .../02434_cancel_insert_when_client_dies.sh | 13 +++++++--- .../02435_rollback_cancelled_queries.sh | 2 +- utils/check-mysql-binlog/main.cpp | 2 +- 12 files changed, 89 insertions(+), 23 deletions(-) diff --git a/src/IO/WriteBufferFromPocoSocket.cpp b/src/IO/WriteBufferFromPocoSocket.cpp index e01245849ae..b15149c6f88 100644 --- a/src/IO/WriteBufferFromPocoSocket.cpp +++ b/src/IO/WriteBufferFromPocoSocket.cpp @@ -64,7 +64,8 @@ void WriteBufferFromPocoSocket::nextImpl() } catch (const Poco::Net::NetException & e) { - throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while writing to socket ({})", e.displayText(), peer_address.toString()); + throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while writing to socket ({} -> {})", e.displayText(), + our_address.toString(), peer_address.toString()); } catch (const Poco::TimeoutException &) { @@ -74,18 +75,20 @@ void WriteBufferFromPocoSocket::nextImpl() } catch (const Poco::IOException & e) { - throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while writing to socket ({})", e.displayText(), peer_address.toString()); + throw NetException(ErrorCodes::NETWORK_ERROR, "{}, while writing to socket ({} -> {})", e.displayText(), + our_address.toString(), peer_address.toString()); } if (res < 0) - throw NetException(ErrorCodes::CANNOT_WRITE_TO_SOCKET, "Cannot write to socket ({})", peer_address.toString()); + throw NetException(ErrorCodes::CANNOT_WRITE_TO_SOCKET, "Cannot write to socket ({} -> {})", + our_address.toString(), peer_address.toString()); bytes_written += res; } } WriteBufferFromPocoSocket::WriteBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size) - : BufferWithOwnMemory(buf_size), socket(socket_), peer_address(socket.peerAddress()) + : BufferWithOwnMemory(buf_size), socket(socket_), peer_address(socket.peerAddress()), our_address(socket.address()) { } diff --git a/src/IO/WriteBufferFromPocoSocket.h b/src/IO/WriteBufferFromPocoSocket.h index 295ca16ecaf..ffe1176c8cd 100644 --- a/src/IO/WriteBufferFromPocoSocket.h +++ b/src/IO/WriteBufferFromPocoSocket.h @@ -28,6 +28,7 @@ protected: * (getpeername will return an error). */ Poco::Net::SocketAddress peer_address; + Poco::Net::SocketAddress our_address; }; } diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index f3d83a37c59..5e1e4cb58fb 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -176,7 +176,7 @@ static void setExceptionStackTrace(QueryLogElement & elem) /// Log exception (with query info) into text log (not into system table). -static void logException(ContextPtr context, QueryLogElement & elem) +static void logException(ContextPtr context, QueryLogElement & elem, bool log_error = true) { String comment; if (!elem.log_comment.empty()) @@ -187,7 +187,7 @@ static void logException(ContextPtr context, QueryLogElement & elem) PreformattedMessage message; message.format_string = elem.exception_format_string; - if (elem.stack_trace.empty()) + if (elem.stack_trace.empty() || !log_error) message.text = fmt::format("{} (from {}){} (in query: {})", elem.exception, context->getClientInfo().current_address.toString(), comment, @@ -201,7 +201,10 @@ static void logException(ContextPtr context, QueryLogElement & elem) toOneLineQuery(elem.query), elem.stack_trace); - LOG_ERROR(&Poco::Logger::get("executeQuery"), message); + if (log_error) + LOG_ERROR(&Poco::Logger::get("executeQuery"), message); + else + LOG_INFO(&Poco::Logger::get("executeQuery"), message); } static void onExceptionBeforeStart( @@ -1101,7 +1104,7 @@ static std::tuple executeQueryImpl( quota(quota), status_info_to_query_log, implicit_txn_control, - query_span]() mutable + query_span](bool log_error) mutable { if (implicit_txn_control) { @@ -1139,9 +1142,9 @@ static std::tuple executeQueryImpl( elem.query_duration_ms = start_watch.elapsedMilliseconds(); } - if (current_settings.calculate_text_stack_trace) + if (current_settings.calculate_text_stack_trace && log_error) setExceptionStackTrace(elem); - logException(context, elem); + logException(context, elem, log_error); /// In case of exception we log internal queries also if (log_queries && elem.type >= log_queries_min_type && static_cast(elem.query_duration_ms) >= log_queries_min_query_duration_ms) diff --git a/src/QueryPipeline/BlockIO.cpp b/src/QueryPipeline/BlockIO.cpp index 9af7cd2b772..231c369707e 100644 --- a/src/QueryPipeline/BlockIO.cpp +++ b/src/QueryPipeline/BlockIO.cpp @@ -4,6 +4,10 @@ namespace DB { +namespace ErrorCodes +{ + extern const int QUERY_WAS_CANCELLED; +} void BlockIO::reset() { @@ -58,7 +62,26 @@ void BlockIO::onFinish() void BlockIO::onException() { if (exception_callback) - exception_callback(); + exception_callback(/* log_error */ true); + + pipeline.reset(); +} + +void BlockIO::onCancelOrConnectionLoss() +{ + /// Query was not finished gracefully, so we should call exception_callback + /// But we don't have a real exception + if (exception_callback) + { + try + { + throw Exception(ErrorCodes::QUERY_WAS_CANCELLED, "Query was cancelled or a client has unexpectedly dropped the connection"); + } + catch (...) + { + exception_callback(/* log_error */ false); + } + } pipeline.reset(); } diff --git a/src/QueryPipeline/BlockIO.h b/src/QueryPipeline/BlockIO.h index 4c8d29d0ba8..ff85a0d6772 100644 --- a/src/QueryPipeline/BlockIO.h +++ b/src/QueryPipeline/BlockIO.h @@ -26,13 +26,14 @@ struct BlockIO /// Callbacks for query logging could be set here. std::function finish_callback; - std::function exception_callback; + std::function exception_callback; /// When it is true, don't bother sending any non-empty blocks to the out stream bool null_format = false; void onFinish(); void onException(); + void onCancelOrConnectionLoss(); /// Set is_all_data_sent in system.processes for this query. void setAllDataSent() const; diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index 1f06f6a4ae9..9f53e418aec 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -416,17 +416,25 @@ void TCPHandler::runImpl() after_check_cancelled.restart(); after_send_progress.restart(); + auto finish_or_cancel = [this]() + { + if (state.is_cancelled) + state.io.onCancelOrConnectionLoss(); + else + state.io.onFinish(); + }; + if (state.io.pipeline.pushing()) { /// FIXME: check explicitly that insert query suggests to receive data via native protocol, state.need_receive_data_for_insert = true; processInsertQuery(); - state.io.onFinish(); + finish_or_cancel(); } else if (state.io.pipeline.pulling()) { processOrdinaryQueryWithProcessors(); - state.io.onFinish(); + finish_or_cancel(); } else if (state.io.pipeline.completed()) { @@ -455,7 +463,7 @@ void TCPHandler::runImpl() executor.execute(); } - state.io.onFinish(); + finish_or_cancel(); std::lock_guard lock(task_callback_mutex); @@ -469,7 +477,7 @@ void TCPHandler::runImpl() } else { - state.io.onFinish(); + finish_or_cancel(); } /// Do it before sending end of stream, to have a chance to show log message in client. diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index bac13ea37cf..b2bbd02f879 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -569,6 +569,26 @@ void DistributedSink::onFinish() } } +void DistributedSink::onCancel() +{ + if (pool && !pool->finished()) + { + try + { + pool->wait(); + } + catch (...) + { + tryLogCurrentException(storage.log); + } + } + + for (auto & shard_jobs : per_shard_jobs) + for (JobReplica & job : shard_jobs.replicas_jobs) + if (job.executor) + job.executor->cancel(); +} + IColumn::Selector DistributedSink::createSelector(const Block & source_block) const { diff --git a/src/Storages/Distributed/DistributedSink.h b/src/Storages/Distributed/DistributedSink.h index af0c64cbd78..325d5859289 100644 --- a/src/Storages/Distributed/DistributedSink.h +++ b/src/Storages/Distributed/DistributedSink.h @@ -54,6 +54,8 @@ public: void onFinish() override; private: + void onCancel() override; + IColumn::Selector createSelector(const Block & source_block) const; void writeAsync(const Block & block); diff --git a/tests/integration/test_insert_into_distributed/test.py b/tests/integration/test_insert_into_distributed/test.py index a52809f817c..3bee2149387 100644 --- a/tests/integration/test_insert_into_distributed/test.py +++ b/tests/integration/test_insert_into_distributed/test.py @@ -288,7 +288,7 @@ def test_inserts_single_replica_no_internal_replication(started_cluster): "prefer_localhost_replica": "0", }, ) - assert node2.query("SELECT count(*) FROM single_replicated").strip() == "1" + assert node2.query("SELECT count(*) FROM single_replicated").strip() == "0" finally: node2.query("TRUNCATE TABLE single_replicated") diff --git a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh index 6cae90a3cc3..2e653f2f1a3 100755 --- a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh @@ -11,20 +11,25 @@ export TEST_MARK="02434_insert_${CLICKHOUSE_DATABASE}_" $CLICKHOUSE_CLIENT -q 'select * from numbers(5000000) format TSV' > $DATA_FILE $CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order by A settings non_replicated_deduplication_window=1000;' +$CLICKHOUSE_CLIENT -q "create table dedup_dist(A Int64) Engine = Distributed('test_cluster_one_shard_two_replicas', currentDatabase(), dedup_test)" function insert_data { SETTINGS="query_id=$ID&max_insert_block_size=110000&min_insert_block_size_rows=110000" # max_block_size=10000, so external table will contain smaller blocks that will be squashed on insert-select (more chances to catch a bug on query cancellation) TRASH_SETTINGS="query_id=$ID&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_insert_block_size=110000&max_block_size=10000&min_insert_block_size_bytes=0&min_insert_block_size_rows=110000&max_insert_block_size=110000" - TYPE=$(( RANDOM % 4 )) + TYPE=$(( RANDOM % 5 )) if [[ "$TYPE" -eq 0 ]]; then # client will send 10000-rows blocks, server will squash them into 110000-rows blocks (more chances to catch a bug on query cancellation) - $CLICKHOUSE_CLIENT --max_block_size=10000 --max_insert_block_size=10000 --query_id="$ID" -q 'insert into dedup_test settings max_insert_block_size=110000, min_insert_block_size_rows=110000 format TSV' < $DATA_FILE + $CLICKHOUSE_CLIENT --max_block_size=10000 --max_insert_block_size=10000 --query_id="$ID" \ + -q 'insert into dedup_test settings max_insert_block_size=110000, min_insert_block_size_rows=110000 format TSV' < $DATA_FILE elif [[ "$TYPE" -eq 1 ]]; then - $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + $CLICKHOUSE_CLIENT --max_block_size=10000 --max_insert_block_size=10000 --query_id="$ID" --prefer_localhost_replica="$(( RANDOM % 2))" \ + -q 'insert into dedup_dist settings max_insert_block_size=110000, min_insert_block_size_rows=110000 format TSV' < $DATA_FILE elif [[ "$TYPE" -eq 2 ]]; then + $CLICKHOUSE_CURL -sS -X POST --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE + elif [[ "$TYPE" -eq 3 ]]; then $CLICKHOUSE_CURL -sS -X POST -H "Transfer-Encoding: chunked" --data-binary @- "$CLICKHOUSE_URL&$SETTINGS&query=insert+into+dedup_test+format+TSV" < $DATA_FILE else $CLICKHOUSE_CURL -sS -F 'file=@-' "$CLICKHOUSE_URL&$TRASH_SETTINGS&file_format=TSV&file_types=UInt64" -X POST --form-string 'query=insert into dedup_test select * from file' < $DATA_FILE @@ -73,7 +78,7 @@ export -f thread_insert; export -f thread_select; export -f thread_cancel; -TIMEOUT=40 # 10 seconds for each TYPE +TIMEOUT=40 timeout $TIMEOUT bash -c thread_insert & timeout $TIMEOUT bash -c thread_select & diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh index 18317960cdf..a69e526c1c8 100755 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -114,6 +114,6 @@ $CLICKHOUSE_CLIENT --implicit_transaction=1 -q 'select throwIf(count() % 1000000 # So use this query to check that thread_cancel do something $CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or - message like '%Connection reset by peer%')" + message like '%Connection reset by peer%' or message like '%Broken pipe, while writing to socket%')" $CLICKHOUSE_CLIENT --database_atomic_wait_for_drop_and_detach_synchronously=0 -q "drop table dedup_test" diff --git a/utils/check-mysql-binlog/main.cpp b/utils/check-mysql-binlog/main.cpp index cf2a27e8aac..68558340180 100644 --- a/utils/check-mysql-binlog/main.cpp +++ b/utils/check-mysql-binlog/main.cpp @@ -18,7 +18,7 @@ static DB::MySQLReplication::BinlogEventPtr parseSingleEventBody( { DB::MySQLReplication::BinlogEventPtr event; DB::ReadBufferPtr limit_read_buffer = std::make_shared(payload, header.event_size - 19, - /* trow_exception */ false, /* exact_limit */ {}); + /* trow_exception */ false, /* exact_limit */ std::nullopt); DB::ReadBufferPtr event_payload = std::make_shared(*limit_read_buffer, exist_checksum ? 4 : 0); switch (header.type) From 940035f1086a7e7c6bdb375304d9166024ebddd4 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Sat, 25 Feb 2023 01:31:38 +0100 Subject: [PATCH 094/342] fix --- .../queries/0_stateless/02434_cancel_insert_when_client_dies.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh index 2e653f2f1a3..2a17095b267 100755 --- a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh @@ -40,6 +40,7 @@ export -f insert_data ID="02434_insert_init_${CLICKHOUSE_DATABASE}_$RANDOM" insert_data +$CLICKHOUSE_CLIENT -q "system flush distributed dedup_dist" $CLICKHOUSE_CLIENT -q 'select count() from dedup_test' function thread_insert From 4b5d62d5bf34b534693b9f8f579ed87ecf25f1e5 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Sun, 26 Feb 2023 22:19:01 +0000 Subject: [PATCH 095/342] Analyzer: trivial count optimization --- src/Planner/PlannerJoinTree.cpp | 126 ++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 59b09f91888..36eb5c59ef5 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -39,6 +39,14 @@ #include #include #include +#include +#include +#include "Analyzer/AggregationUtils.h" +#include "Analyzer/FunctionNode.h" +#include +#include +#include +#include namespace DB { @@ -143,6 +151,119 @@ NameAndTypePair chooseSmallestColumnToReadFromStorage(const StoragePtr & storage return result; } +bool applyTrivialCountIfPossible( + QueryPlan & query_plan, + const TableNode & table_node, + const SelectQueryInfo & select_query_info, + const ContextPtr & query_context, + const Names & columns_names) +{ + const auto & settings = query_context->getSettingsRef(); + if (!settings.optimize_trivial_count_query) + return false; + + const auto & storage = table_node.getStorage(); + const auto & storage_snapshot = table_node.getStorageSnapshot(); + + bool can_apply_trivial_count = (settings.max_parallel_replicas <= 1) && // + !settings.allow_experimental_query_deduplication && // + !settings.empty_result_for_aggregation_by_empty_set && // + storage && // + storage->getName() != "MaterializedMySQL" && // + !storage->hasLightweightDeletedMask() && // + select_query_info.filter_asts.empty() && // ??? + select_query_info.has_aggregates; + if (!can_apply_trivial_count) + return false; + + QueryTreeNodes aggregates = collectAggregateFunctionNodes(select_query_info.query_tree); + if (aggregates.size() != 1) + return false; + + auto & main_query_node = select_query_info.query_tree->as(); + /// dump main query tree + { + WriteBufferFromOwnString buffer; + IQueryTreeNode::FormatState format_state; + main_query_node.dumpTreeImpl(buffer, format_state, 0); + + LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "main_query_node:\n{}", buffer.str()); + LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "Projection column:\n{}", main_query_node.getProjectionColumns().front().dump()); + } + + { + WriteBufferFromOwnString buffer; + buffer << columns_names; + LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "{}", buffer.str()); + } + + const auto * function_node = typeid_cast(aggregates.front().get()); + if (!function_node) + return false; + + if (!function_node->getAggregateFunction()) + return false; + + LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "Aggregation: {}", function_node->getFunctionName()); + + const auto * count_func = typeid_cast(function_node->getAggregateFunction().get()); + if (!count_func) + return false; + + /// get number of rows + std::optional num_rows{}; + // if (!query_tree. prewhere() && !query.where() && !context->getCurrentTransaction()) + if (!main_query_node.hasPrewhere() && !main_query_node.hasWhere()) + { + num_rows = storage->totalRows(settings); + } + // else // It's possible to optimize count() given only partition predicates + // { + // SelectQueryInfo temp_query_info; + // temp_query_info.query = query_ptr; + // temp_query_info.syntax_analyzer_result = syntax_analyzer_result; + // temp_query_info.prepared_sets = query_analyzer->getPreparedSets(); + // num_rows = storage->totalRowsByPartitionPredicate(temp_query_info, context); + // } + + if (!num_rows) + return false; + + LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "Number of rows: {}", num_rows.value()); + + /// set aggregation state + const AggregateFunctionCount & agg_count = *count_func; + std::vector state(agg_count.sizeOfData()); + AggregateDataPtr place = state.data(); + agg_count.create(place); + // SCOPE_EXIT_MEMORY_SAFE(agg_count.destroy(place)); + agg_count.set(place, num_rows.value()); + + auto column = ColumnAggregateFunction::create(function_node->getAggregateFunction()); + column->insertFrom(place); + + /// get count() argument type + DataTypes argument_types; + argument_types.reserve(columns_names.size()); + { + const Block source_header = storage_snapshot->getSampleBlockForColumns(columns_names); + for (const auto & column_name : columns_names) + argument_types.push_back(source_header.getByName(column_name).type); + } + + Block block_with_count{ + {std::move(column), + std::make_shared(function_node->getAggregateFunction(), argument_types, Array{}), + columns_names.front()}}; + + auto source = std::make_shared(block_with_count); + auto prepared_count = std::make_unique(Pipe(std::move(source))); + prepared_count->setStepDescription("Optimized trivial count"); + query_plan.addStep(std::move(prepared_count)); + + return true; +} + JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & table_expression, const SelectQueryInfo & select_query_info, const SelectQueryOptions & select_query_options, @@ -287,6 +408,11 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl table_expression_data.addColumn(additional_column_to_read, column_identifier); } + /// apply trivial_count optimization if possible + if (is_single_table_expression && table_node + && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info, planner_context->getQueryContext(), columns_names)) + return {std::move(query_plan), from_stage}; + bool need_rewrite_query_with_final = storage->needRewriteQueryWithFinal(columns_names); if (need_rewrite_query_with_final) { From 1e4d9e2421091468bddcce8f59191b87865a05c3 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Sun, 26 Feb 2023 22:37:06 +0000 Subject: [PATCH 096/342] try to fix --- src/Planner/PlannerJoinTree.cpp | 50 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 36eb5c59ef5..593bdd7dece 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -408,11 +408,6 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl table_expression_data.addColumn(additional_column_to_read, column_identifier); } - /// apply trivial_count optimization if possible - if (is_single_table_expression && table_node - && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info, planner_context->getQueryContext(), columns_names)) - return {std::move(query_plan), from_stage}; - bool need_rewrite_query_with_final = storage->needRewriteQueryWithFinal(columns_names); if (need_rewrite_query_with_final) { @@ -434,28 +429,39 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } } - storage->read(query_plan, columns_names, storage_snapshot, table_expression_query_info, query_context, from_stage, max_block_size, max_streams); + /// apply trivial_count optimization if possible + bool is_trivial_count_applied = (is_single_table_expression && table_node + && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info, planner_context->getQueryContext(), columns_names)); - if (query_plan.isInitialized()) + if (is_trivial_count_applied) { - /** Specify the number of threads only if it wasn't specified in storage. - * - * But in case of remote query and prefer_localhost_replica=1 (default) - * The inner local query (that is done in the same process, without - * network interaction), it will setMaxThreads earlier and distributed - * query will not update it. - */ - if (!query_plan.getMaxThreads() || is_remote) - query_plan.setMaxThreads(max_threads_execute_query); + from_stage = QueryProcessingStage::WithMergeableState; } else { - /// Create step which reads from empty source if storage has no data. - auto source_header = storage_snapshot->getSampleBlockForColumns(columns_names); - Pipe pipe(std::make_shared(source_header)); - auto read_from_pipe = std::make_unique(std::move(pipe)); - read_from_pipe->setStepDescription("Read from NullSource"); - query_plan.addStep(std::move(read_from_pipe)); + storage->read(query_plan, columns_names, storage_snapshot, table_expression_query_info, query_context, from_stage, max_block_size, max_streams); + + if (query_plan.isInitialized()) + { + /** Specify the number of threads only if it wasn't specified in storage. + * + * But in case of remote query and prefer_localhost_replica=1 (default) + * The inner local query (that is done in the same process, without + * network interaction), it will setMaxThreads earlier and distributed + * query will not update it. + */ + if (!query_plan.getMaxThreads() || is_remote) + query_plan.setMaxThreads(max_threads_execute_query); + } + else + { + /// Create step which reads from empty source if storage has no data. + auto source_header = storage_snapshot->getSampleBlockForColumns(columns_names); + Pipe pipe(std::make_shared(source_header)); + auto read_from_pipe = std::make_unique(std::move(pipe)); + read_from_pipe->setStepDescription("Read from NullSource"); + query_plan.addStep(std::move(read_from_pipe)); + } } } else if (query_node || union_node) From f9a324c47a50e67dfe1933677924e08f921a40e7 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Mon, 27 Feb 2023 09:48:24 +0000 Subject: [PATCH 097/342] Make diagnostic traces debug level --- src/Planner/PlannerJoinTree.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 593bdd7dece..b1e22b40d69 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -187,14 +187,14 @@ bool applyTrivialCountIfPossible( IQueryTreeNode::FormatState format_state; main_query_node.dumpTreeImpl(buffer, format_state, 0); - LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "main_query_node:\n{}", buffer.str()); - LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "Projection column:\n{}", main_query_node.getProjectionColumns().front().dump()); + LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "main_query_node:\n{}", buffer.str()); + LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "Projection column:\n{}", main_query_node.getProjectionColumns().front().dump()); } { WriteBufferFromOwnString buffer; buffer << columns_names; - LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "{}", buffer.str()); + LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "{}", buffer.str()); } const auto * function_node = typeid_cast(aggregates.front().get()); @@ -204,7 +204,7 @@ bool applyTrivialCountIfPossible( if (!function_node->getAggregateFunction()) return false; - LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "Aggregation: {}", function_node->getFunctionName()); + LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "Aggregation: {}", function_node->getFunctionName()); const auto * count_func = typeid_cast(function_node->getAggregateFunction().get()); if (!count_func) @@ -229,7 +229,7 @@ bool applyTrivialCountIfPossible( if (!num_rows) return false; - LOG_ERROR(&Poco::Logger::get(__PRETTY_FUNCTION__), "Number of rows: {}", num_rows.value()); + LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "Number of rows: {}", num_rows.value()); /// set aggregation state const AggregateFunctionCount & agg_count = *count_func; From cac9e96d0415f52d27a96c873c981f37bd8a2ed7 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Mon, 27 Feb 2023 10:56:59 +0000 Subject: [PATCH 098/342] Fix: do not apply if FINAL --- src/Planner/PlannerJoinTree.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index b1e22b40d69..9c005314421 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -162,6 +162,10 @@ bool applyTrivialCountIfPossible( if (!settings.optimize_trivial_count_query) return false; + /// can't apply if FINAL + if (select_query_info.table_expression_modifiers.has_value() && select_query_info.table_expression_modifiers.value().hasFinal()) + return false; + const auto & storage = table_node.getStorage(); const auto & storage_snapshot = table_node.getStorageSnapshot(); @@ -430,8 +434,8 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } /// apply trivial_count optimization if possible - bool is_trivial_count_applied = (is_single_table_expression && table_node - && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info, planner_context->getQueryContext(), columns_names)); + bool is_trivial_count_applied = is_single_table_expression && table_node + && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info, planner_context->getQueryContext(), columns_names); if (is_trivial_count_applied) { From 67099f2a65a78e8dc898510a4f700b36603fd067 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Mon, 27 Feb 2023 12:53:09 +0000 Subject: [PATCH 099/342] Tests --- .../02674_trivial_count_analyzer.reference | 48 +++++++++++++++++++ .../02674_trivial_count_analyzer.sql | 46 ++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tests/queries/0_stateless/02674_trivial_count_analyzer.reference create mode 100644 tests/queries/0_stateless/02674_trivial_count_analyzer.sql diff --git a/tests/queries/0_stateless/02674_trivial_count_analyzer.reference b/tests/queries/0_stateless/02674_trivial_count_analyzer.reference new file mode 100644 index 00000000000..2a94fd59d7b --- /dev/null +++ b/tests/queries/0_stateless/02674_trivial_count_analyzer.reference @@ -0,0 +1,48 @@ +-- { echoOn } +set allow_experimental_analyzer=1; +set optimize_trivial_count_query=1; +create table m3(a Int64, b UInt64) Engine=MergeTree order by tuple(); +select count() from m3; +0 +insert into m3 values (0,0); +insert into m3 values (-1,1); +select trimBoth(explain) from (explain select count() from m3) where explain like '%ReadFromPreparedSource (Optimized trivial count)%'; +ReadFromPreparedSource (Optimized trivial count) +select count() from m3; +2 +select count(*) from m3; +2 +select count(a) from m3; +2 +select count(b) from m3; +2 +select count() + 1 from m3; +3 +-- drop table m3; + +-- checking queries with FINAL +create table replacing_m3(a Int64, b UInt64) Engine=ReplacingMergeTree() order by (a, b); +SYSTEM STOP MERGES replacing_m3; +select count() from replacing_m3; +0 +insert into replacing_m3 values (0,0); +insert into replacing_m3 values (0,0); +insert into replacing_m3 values (-1,1); +insert into replacing_m3 values (-2,2); +select trimBoth(explain) from (explain select count() from replacing_m3) where explain like '%ReadFromPreparedSource (Optimized trivial count)%'; +ReadFromPreparedSource (Optimized trivial count) +select count() from replacing_m3; +4 +select count(*) from replacing_m3; +4 +select count(a) from replacing_m3; +4 +select count(b) from replacing_m3; +4 +set optimize_trivial_count_query=0; -- FIXME: wrong result for queries with FINAL +select count() from replacing_m3 FINAL; +3 +select count(a) from replacing_m3 FINAL; +3 +select count(b) from replacing_m3 FINAL; +3 diff --git a/tests/queries/0_stateless/02674_trivial_count_analyzer.sql b/tests/queries/0_stateless/02674_trivial_count_analyzer.sql new file mode 100644 index 00000000000..d4a686e6eff --- /dev/null +++ b/tests/queries/0_stateless/02674_trivial_count_analyzer.sql @@ -0,0 +1,46 @@ +drop table if exists m3; +drop table if exists replacing_m3; + +-- { echoOn } +set allow_experimental_analyzer=1; +set optimize_trivial_count_query=1; + +create table m3(a Int64, b UInt64) Engine=MergeTree order by tuple(); + +select count() from m3; + +insert into m3 values (0,0); +insert into m3 values (-1,1); + +select trimBoth(explain) from (explain select count() from m3) where explain like '%ReadFromPreparedSource (Optimized trivial count)%'; +select count() from m3; +select count(*) from m3; +select count(a) from m3; +select count(b) from m3; +select count() + 1 from m3; + +-- drop table m3; + +-- checking queries with FINAL +create table replacing_m3(a Int64, b UInt64) Engine=ReplacingMergeTree() order by (a, b); +SYSTEM STOP MERGES replacing_m3; + +select count() from replacing_m3; + +insert into replacing_m3 values (0,0); +insert into replacing_m3 values (0,0); +insert into replacing_m3 values (-1,1); +insert into replacing_m3 values (-2,2); + +select trimBoth(explain) from (explain select count() from replacing_m3) where explain like '%ReadFromPreparedSource (Optimized trivial count)%'; +select count() from replacing_m3; +select count(*) from replacing_m3; +select count(a) from replacing_m3; +select count(b) from replacing_m3; + +set optimize_trivial_count_query=0; -- FIXME: wrong result for queries with FINAL +select count() from replacing_m3 FINAL; +select count(a) from replacing_m3 FINAL; +select count(b) from replacing_m3 FINAL; + +-- drop table replacing_m3; From 7684153100907ad3987980a66b79824158d10dac Mon Sep 17 00:00:00 2001 From: artem-yadr <84010375+artem-yadr@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:18:08 +0300 Subject: [PATCH 100/342] Redeclaration error fix --- base/poco/MongoDB/src/Connection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/poco/MongoDB/src/Connection.cpp b/base/poco/MongoDB/src/Connection.cpp index c75211cf9d7..38c31d2250a 100644 --- a/base/poco/MongoDB/src/Connection.cpp +++ b/base/poco/MongoDB/src/Connection.cpp @@ -237,7 +237,7 @@ void Connection::connect(const std::string& uri, SocketFactory& socketFactory) for (std::vector::const_iterator it = strAddresses.cbegin();it != strAddresses.cend(); ++it) { newURI = *it; - Poco::URI theURI(newURI); + theURI = Poco::URI(newURI); std::string host = theURI.getHost(); Poco::UInt16 port = theURI.getPort(); From 385beda8888dcabc895d0538914b3ce747c7ea67 Mon Sep 17 00:00:00 2001 From: artem-yadr <84010375+artem-yadr@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:25:12 +0300 Subject: [PATCH 101/342] Update base/poco/MongoDB/include/Poco/MongoDB/Connection.h Co-authored-by: Kseniia Sumarokova <54203879+kssenii@users.noreply.github.com> --- base/poco/MongoDB/include/Poco/MongoDB/Connection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h index 8fd9c7919e4..5c5f6f76638 100644 --- a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h +++ b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h @@ -91,7 +91,7 @@ namespace MongoDB Poco::Net::SocketAddress address() const; /// Returns the address of the MongoDB server. - std::string uri() const; + const std::string & uri() const; /// Returns the uri on which the connection was made. void connect(const std::string & hostAndPort); From 0de2eb8a795908132ed2b6eaaa4bd09fb76e9f2d Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Mon, 27 Feb 2023 14:45:39 +0000 Subject: [PATCH 102/342] Update version_date.tsv and changelogs after v22.8.14.53-lts --- docs/changelogs/v22.8.14.53-lts.md | 40 ++++++++++++++++++++++++++++ utils/list-versions/version_date.tsv | 1 + 2 files changed, 41 insertions(+) create mode 100644 docs/changelogs/v22.8.14.53-lts.md diff --git a/docs/changelogs/v22.8.14.53-lts.md b/docs/changelogs/v22.8.14.53-lts.md new file mode 100644 index 00000000000..5978080fa3a --- /dev/null +++ b/docs/changelogs/v22.8.14.53-lts.md @@ -0,0 +1,40 @@ +--- +sidebar_position: 1 +sidebar_label: 2023 +--- + +# 2023 Changelog + +### ClickHouse release v22.8.14.53-lts (4ea67c40077) FIXME as compared to v22.8.13.20-lts (e4817946d18) + +#### Performance Improvement +* Backported in [#45845](https://github.com/ClickHouse/ClickHouse/issues/45845): Fixed performance of short `SELECT` queries that read from tables with large number of`Array`/`Map`/`Nested` columns. [#45630](https://github.com/ClickHouse/ClickHouse/pull/45630) ([Anton Popov](https://github.com/CurtizJ)). +* Backported in [#46374](https://github.com/ClickHouse/ClickHouse/issues/46374): Fix too big memory usage for vertical merges on non-remote disk. Respect `max_insert_delayed_streams_for_parallel_write` for the remote disk. [#46275](https://github.com/ClickHouse/ClickHouse/pull/46275) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). +* Backported in [#46358](https://github.com/ClickHouse/ClickHouse/issues/46358): Allow using Vertical merge algorithm with parts in Compact format. This will allow ClickHouse server to use much less memory for background operations. This closes [#46084](https://github.com/ClickHouse/ClickHouse/issues/46084). [#46282](https://github.com/ClickHouse/ClickHouse/pull/46282) ([Anton Popov](https://github.com/CurtizJ)). + +#### Build/Testing/Packaging Improvement +* Backported in [#46112](https://github.com/ClickHouse/ClickHouse/issues/46112): Remove the dependency on the `adduser` tool from the packages, because we don't use it. This fixes [#44934](https://github.com/ClickHouse/ClickHouse/issues/44934). [#45011](https://github.com/ClickHouse/ClickHouse/pull/45011) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46482](https://github.com/ClickHouse/ClickHouse/issues/46482): Get rid of unnecessary build for standalone clickhouse-keeper. [#46367](https://github.com/ClickHouse/ClickHouse/pull/46367) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#46505](https://github.com/ClickHouse/ClickHouse/issues/46505): Some time ago the ccache compression was changed to `zst`, but `gz` archives are downloaded by default. It fixes it by prioritizing zst archive. [#46490](https://github.com/ClickHouse/ClickHouse/pull/46490) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + +#### Bug Fix (user-visible misbehavior in official stable or prestable release) + +* Backported in [#45908](https://github.com/ClickHouse/ClickHouse/issues/45908): Fixed bug with non-parsable default value for EPHEMERAL column in table metadata. [#44026](https://github.com/ClickHouse/ClickHouse/pull/44026) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). +* Backported in [#46238](https://github.com/ClickHouse/ClickHouse/issues/46238): A couple of seg faults have been reported around `c-ares`. All of the recent stack traces observed fail on inserting into `std::unodered_set<>`. I believe I have found the root cause of this, it seems to be unprocessed queries. Prior to this PR, CH calls `poll` to wait on the file descriptors in the `c-ares` channel. According to the [poll docs](https://man7.org/linux/man-pages/man2/poll.2.html), a negative return value means an error has ocurred. Because of this, we would abort the execution and return failure. The problem is that `poll` will also return a negative value if a system interrupt occurs. A system interrupt does not mean the processing has failed or ended, but we would abort it anyways because we were checking for negative values. Once the execution is aborted, the whole stack is destroyed, which includes the `std::unordered_set` passed to the `void *` parameter of the c-ares callback. Once c-ares completed the request, the callback would be invoked and would access an invalid memory address causing a segfault. [#45629](https://github.com/ClickHouse/ClickHouse/pull/45629) ([Arthur Passos](https://github.com/arthurpassos)). +* Backported in [#45727](https://github.com/ClickHouse/ClickHouse/issues/45727): Fix key description when encountering duplicate primary keys. This can happen in projections. See [#45590](https://github.com/ClickHouse/ClickHouse/issues/45590) for details. [#45686](https://github.com/ClickHouse/ClickHouse/pull/45686) ([Amos Bird](https://github.com/amosbird)). +* Backported in [#46394](https://github.com/ClickHouse/ClickHouse/issues/46394): Fix `SYSTEM UNFREEZE` queries failing with the exception `CANNOT_PARSE_INPUT_ASSERTION_FAILED`. [#46325](https://github.com/ClickHouse/ClickHouse/pull/46325) ([Aleksei Filatov](https://github.com/aalexfvk)). +* Backported in [#46442](https://github.com/ClickHouse/ClickHouse/issues/46442): Fix possible `LOGICAL_ERROR` in asynchronous inserts with invalid data sent in format `VALUES`. [#46350](https://github.com/ClickHouse/ClickHouse/pull/46350) ([Anton Popov](https://github.com/CurtizJ)). +* Backported in [#46674](https://github.com/ClickHouse/ClickHouse/issues/46674): Fix an invalid processing of constant `LowCardinality` argument in function `arrayMap`. This bug could lead to a segfault in release, and logical error `Bad cast` in debug build. [#46569](https://github.com/ClickHouse/ClickHouse/pull/46569) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46879](https://github.com/ClickHouse/ClickHouse/issues/46879): Fix MSan report in the `maxIntersections` function. This closes [#43126](https://github.com/ClickHouse/ClickHouse/issues/43126). [#46847](https://github.com/ClickHouse/ClickHouse/pull/46847) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46871](https://github.com/ClickHouse/ClickHouse/issues/46871): Fix a bug in the `Map` data type. This closes [#46855](https://github.com/ClickHouse/ClickHouse/issues/46855). [#46856](https://github.com/ClickHouse/ClickHouse/pull/46856) ([Alexey Milovidov](https://github.com/alexey-milovidov)). + +#### NOT FOR CHANGELOG / INSIGNIFICANT + +* Another attempt to fix automerge, or at least to have debug footprint [#45476](https://github.com/ClickHouse/ClickHouse/pull/45476) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add check for running workflows to merge_pr.py [#45803](https://github.com/ClickHouse/ClickHouse/pull/45803) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Get rid of progress timestamps in release publishing [#45818](https://github.com/ClickHouse/ClickHouse/pull/45818) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add necessary dependency for sanitizers [#45959](https://github.com/ClickHouse/ClickHouse/pull/45959) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add helping logging to auto-merge script [#46080](https://github.com/ClickHouse/ClickHouse/pull/46080) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Fix write buffer destruction order for vertical merge. [#46205](https://github.com/ClickHouse/ClickHouse/pull/46205) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). +* Get rid of legacy DocsReleaseChecks [#46665](https://github.com/ClickHouse/ClickHouse/pull/46665) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + diff --git a/utils/list-versions/version_date.tsv b/utils/list-versions/version_date.tsv index 3814e94bf24..50937edb449 100644 --- a/utils/list-versions/version_date.tsv +++ b/utils/list-versions/version_date.tsv @@ -25,6 +25,7 @@ v22.9.4.32-stable 2022-10-26 v22.9.3.18-stable 2022-09-30 v22.9.2.7-stable 2022-09-23 v22.9.1.2603-stable 2022-09-22 +v22.8.14.53-lts 2023-02-27 v22.8.13.20-lts 2023-01-29 v22.8.12.45-lts 2023-01-10 v22.8.11.15-lts 2022-12-08 From 7b85b35534fd65fae459e5ce13b5328da7d6c6b0 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Mon, 27 Feb 2023 15:11:25 +0000 Subject: [PATCH 103/342] fix skip indexes --- src/Storages/IndicesDescription.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Storages/IndicesDescription.cpp b/src/Storages/IndicesDescription.cpp index 00a3636b605..591a9082f7a 100644 --- a/src/Storages/IndicesDescription.cpp +++ b/src/Storages/IndicesDescription.cpp @@ -96,6 +96,12 @@ IndexDescription IndexDescription::getIndexFromAST(const ASTPtr & definition_ast result.expression = ExpressionAnalyzer(expr_list, syntax, context).getActions(true); result.sample_block = result.expression->getSampleBlock(); + for (const auto & elem : result.sample_block) + { + result.column_names.push_back(elem.name); + result.data_types.push_back(elem.type); + } + const auto & definition_arguments = index_definition->type->arguments; if (definition_arguments) { From 2e921e3d6b4abcdf61ff4eec0405038f528175c0 Mon Sep 17 00:00:00 2001 From: avogar Date: Mon, 27 Feb 2023 16:00:19 +0000 Subject: [PATCH 104/342] Fix date and int inference from string in JSON --- src/Formats/SchemaInferenceUtils.cpp | 1 + src/Processors/Formats/ISchemaReader.h | 4 ++-- .../02674_date_int_string_json_inference.reference | 1 + .../0_stateless/02674_date_int_string_json_inference.sql | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02674_date_int_string_json_inference.reference create mode 100644 tests/queries/0_stateless/02674_date_int_string_json_inference.sql diff --git a/src/Formats/SchemaInferenceUtils.cpp b/src/Formats/SchemaInferenceUtils.cpp index 7a242a9f81c..00eb686385d 100644 --- a/src/Formats/SchemaInferenceUtils.cpp +++ b/src/Formats/SchemaInferenceUtils.cpp @@ -131,6 +131,7 @@ namespace type_indexes.erase(TypeIndex::Date); type_indexes.erase(TypeIndex::DateTime); + type_indexes.insert(TypeIndex::String); return; } diff --git a/src/Processors/Formats/ISchemaReader.h b/src/Processors/Formats/ISchemaReader.h index edc5c6068c3..330acc99369 100644 --- a/src/Processors/Formats/ISchemaReader.h +++ b/src/Processors/Formats/ISchemaReader.h @@ -170,10 +170,10 @@ void chooseResultColumnType( ErrorCodes::TYPE_MISMATCH, "Automatically defined type {} for column '{}' in row {} differs from type defined by previous rows: {}. " "You can specify the type for this column using setting schema_inference_hints", - type->getName(), + new_type->getName(), column_name, row, - new_type->getName()); + type->getName()); } } diff --git a/tests/queries/0_stateless/02674_date_int_string_json_inference.reference b/tests/queries/0_stateless/02674_date_int_string_json_inference.reference new file mode 100644 index 00000000000..2e89d6a15a4 --- /dev/null +++ b/tests/queries/0_stateless/02674_date_int_string_json_inference.reference @@ -0,0 +1 @@ +x Nullable(String) diff --git a/tests/queries/0_stateless/02674_date_int_string_json_inference.sql b/tests/queries/0_stateless/02674_date_int_string_json_inference.sql new file mode 100644 index 00000000000..21abf763cbf --- /dev/null +++ b/tests/queries/0_stateless/02674_date_int_string_json_inference.sql @@ -0,0 +1,2 @@ +desc format(JSONEachRow, '{"x" : "2020-01-01"}, {"x" : "1000"}') + From e01c9ff5ec080ca9537843e058ea9e0ccea42ac6 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Mon, 27 Feb 2023 17:37:29 +0000 Subject: [PATCH 105/342] Fixes: group by, final --- src/Planner/PlannerJoinTree.cpp | 34 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 9c005314421..8b7489d52c8 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -154,7 +154,7 @@ NameAndTypePair chooseSmallestColumnToReadFromStorage(const StoragePtr & storage bool applyTrivialCountIfPossible( QueryPlan & query_plan, const TableNode & table_node, - const SelectQueryInfo & select_query_info, + const QueryTreeNodePtr & query_tree, const ContextPtr & query_context, const Names & columns_names) { @@ -163,28 +163,26 @@ bool applyTrivialCountIfPossible( return false; /// can't apply if FINAL - if (select_query_info.table_expression_modifiers.has_value() && select_query_info.table_expression_modifiers.value().hasFinal()) + if (table_node.getTableExpressionModifiers().has_value() && table_node.getTableExpressionModifiers()->hasFinal()) + return false; + + auto & main_query_node = query_tree->as(); + if (main_query_node.hasGroupBy()) return false; const auto & storage = table_node.getStorage(); - const auto & storage_snapshot = table_node.getStorageSnapshot(); - - bool can_apply_trivial_count = (settings.max_parallel_replicas <= 1) && // - !settings.allow_experimental_query_deduplication && // - !settings.empty_result_for_aggregation_by_empty_set && // - storage && // - storage->getName() != "MaterializedMySQL" && // - !storage->hasLightweightDeletedMask() && // - select_query_info.filter_asts.empty() && // ??? - select_query_info.has_aggregates; - if (!can_apply_trivial_count) + if (!storage || storage->hasLightweightDeletedMask()) return false; - QueryTreeNodes aggregates = collectAggregateFunctionNodes(select_query_info.query_tree); + if (settings.max_parallel_replicas > 1 || // + settings.allow_experimental_query_deduplication || // + settings.empty_result_for_aggregation_by_empty_set) + return false; + + QueryTreeNodes aggregates = collectAggregateFunctionNodes(query_tree); if (aggregates.size() != 1) return false; - auto & main_query_node = select_query_info.query_tree->as(); /// dump main query tree { WriteBufferFromOwnString buffer; @@ -250,7 +248,7 @@ bool applyTrivialCountIfPossible( DataTypes argument_types; argument_types.reserve(columns_names.size()); { - const Block source_header = storage_snapshot->getSampleBlockForColumns(columns_names); + const Block source_header = table_node.getStorageSnapshot()->getSampleBlockForColumns(columns_names); for (const auto & column_name : columns_names) argument_types.push_back(source_header.getByName(column_name).type); } @@ -434,8 +432,8 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } /// apply trivial_count optimization if possible - bool is_trivial_count_applied = is_single_table_expression && table_node - && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info, planner_context->getQueryContext(), columns_names); + bool is_trivial_count_applied = is_single_table_expression && table_node && select_query_info.has_aggregates + && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info.query_tree, planner_context->getQueryContext(), columns_names); if (is_trivial_count_applied) { From d5469c0ab49106ad55aef68a8eaaec17d59d4081 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Tue, 28 Feb 2023 00:07:50 +0100 Subject: [PATCH 106/342] fix clickhouse-test --- tests/clickhouse-test | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/clickhouse-test b/tests/clickhouse-test index cfd2546bbdd..06a30317346 100755 --- a/tests/clickhouse-test +++ b/tests/clickhouse-test @@ -73,7 +73,14 @@ def stringhash(s): # First and last lines of the log def trim_for_log(s): - return s + if not s: + return s + lines = s.splitlines() + if len(lines) > 10000: + separator = "-" * 40 + str(len(lines) - 10000) + " lines are hidden" + "-" * 40 + return "\n".join(lines[:5000] + [] + [separator] + [] + lines[-5000:]) + else: + return "\n".join(lines) class HTTPError(Exception): From 5fb1f20882677f66ca5c50decd01dcaae4677cab Mon Sep 17 00:00:00 2001 From: AndyB Date: Mon, 27 Feb 2023 23:16:34 +0000 Subject: [PATCH 107/342] LOG_DEBUG over LOG_INFO --- src/Interpreters/executeQuery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 435401796a0..f3c103f6af2 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -995,7 +995,7 @@ static std::tuple executeQueryImpl( { double elapsed_seconds = static_cast(info.elapsed_microseconds) / 1000000.0; double rows_per_second = static_cast(elem.read_rows) / elapsed_seconds; - LOG_INFO( + LOG_DEBUG( &Poco::Logger::get("executeQuery"), "Read {} rows, {} in {} sec., {} rows/sec., {}/sec.", elem.read_rows, From 42c5634ae4af14602250e2938d28d70c064d6247 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Tue, 28 Feb 2023 11:35:36 +0000 Subject: [PATCH 108/342] Remove debug logging --- src/Planner/PlannerJoinTree.cpp | 39 ++++++++++----------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 8b7489d52c8..11944b4b71d 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -39,14 +39,14 @@ #include #include #include + #include -#include -#include "Analyzer/AggregationUtils.h" -#include "Analyzer/FunctionNode.h" -#include +#include +#include #include #include #include +#include namespace DB { @@ -183,22 +183,6 @@ bool applyTrivialCountIfPossible( if (aggregates.size() != 1) return false; - /// dump main query tree - { - WriteBufferFromOwnString buffer; - IQueryTreeNode::FormatState format_state; - main_query_node.dumpTreeImpl(buffer, format_state, 0); - - LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "main_query_node:\n{}", buffer.str()); - LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "Projection column:\n{}", main_query_node.getProjectionColumns().front().dump()); - } - - { - WriteBufferFromOwnString buffer; - buffer << columns_names; - LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "{}", buffer.str()); - } - const auto * function_node = typeid_cast(aggregates.front().get()); if (!function_node) return false; @@ -206,19 +190,22 @@ bool applyTrivialCountIfPossible( if (!function_node->getAggregateFunction()) return false; - LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "Aggregation: {}", function_node->getFunctionName()); - const auto * count_func = typeid_cast(function_node->getAggregateFunction().get()); if (!count_func) return false; /// get number of rows std::optional num_rows{}; - // if (!query_tree. prewhere() && !query.where() && !context->getCurrentTransaction()) - if (!main_query_node.hasPrewhere() && !main_query_node.hasWhere()) + /// Transaction check here is necessary because + /// MergeTree maintains total count for all parts in Active state and it simply returns that number for trivial select count() from table query. + /// But if we have current transaction, then we should return number of rows in current snapshot (that may include parts in Outdated state), + /// so we have to use totalRowsByPartitionPredicate() instead of totalRows even for trivial query + /// See https://github.com/ClickHouse/ClickHouse/pull/24258/files#r828182031 + if (!main_query_node.hasPrewhere() && !main_query_node.hasWhere() && !query_context->getCurrentTransaction()) { num_rows = storage->totalRows(settings); } + // TODO: // else // It's possible to optimize count() given only partition predicates // { // SelectQueryInfo temp_query_info; @@ -231,14 +218,12 @@ bool applyTrivialCountIfPossible( if (!num_rows) return false; - LOG_DEBUG(&Poco::Logger::get(__PRETTY_FUNCTION__), "Number of rows: {}", num_rows.value()); - /// set aggregation state const AggregateFunctionCount & agg_count = *count_func; std::vector state(agg_count.sizeOfData()); AggregateDataPtr place = state.data(); agg_count.create(place); - // SCOPE_EXIT_MEMORY_SAFE(agg_count.destroy(place)); + SCOPE_EXIT_MEMORY_SAFE(agg_count.destroy(place)); agg_count.set(place, num_rows.value()); auto column = ColumnAggregateFunction::create(function_node->getAggregateFunction()); From 919122948f73d391e938f4bffcdaebdad3a7a256 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 28 Feb 2023 14:22:44 +0000 Subject: [PATCH 109/342] More fixes and add tests --- src/IO/ReadWriteBufferFromHTTP.h | 135 ++++++++++-------- .../test_storage_url/configs/conf.xml | 4 + tests/integration/test_storage_url/test.py | 26 ++++ 3 files changed, 104 insertions(+), 61 deletions(-) diff --git a/src/IO/ReadWriteBufferFromHTTP.h b/src/IO/ReadWriteBufferFromHTTP.h index 58abf4a6317..edf7b3f5f95 100644 --- a/src/IO/ReadWriteBufferFromHTTP.h +++ b/src/IO/ReadWriteBufferFromHTTP.h @@ -49,22 +49,18 @@ namespace ErrorCodes extern const int UNKNOWN_FILE_SIZE; } -template -class UpdatableSessionBase +template +class UpdatableSession { -protected: - SessionPtr session; - UInt64 redirects{0}; - Poco::URI initial_uri; - ConnectionTimeouts timeouts; - UInt64 max_redirects; - public: - virtual void buildNewSession(const Poco::URI & uri) = 0; + using SessionPtr = typename TSessionFactory::SessionType; - explicit UpdatableSessionBase(const Poco::URI uri, const ConnectionTimeouts & timeouts_, UInt64 max_redirects_) - : initial_uri{uri}, timeouts{timeouts_}, max_redirects{max_redirects_} + explicit UpdatableSession(const Poco::URI & uri, UInt64 max_redirects_, std::shared_ptr session_factory_) + : max_redirects{max_redirects_} + , initial_uri(uri) + , session_factory(std::move(session_factory_)) { + session = session_factory->buildNewSession(uri); } SessionPtr getSession() { return session; } @@ -73,16 +69,21 @@ public: { ++redirects; if (redirects <= max_redirects) - { - buildNewSession(uri); - } + session = session_factory->buildNewSession(uri); else - { throw Exception(ErrorCodes::TOO_MANY_REDIRECTS, "Too many redirects while trying to access {}", initial_uri.toString()); - } } - virtual ~UpdatableSessionBase() = default; + std::shared_ptr> clone(const Poco::URI & uri) + { + return std::make_shared>(uri, max_redirects, session_factory); + } +private: + SessionPtr session; + UInt64 redirects{0}; + UInt64 max_redirects; + Poco::URI initial_uri; + std::shared_ptr session_factory; }; @@ -147,7 +148,7 @@ namespace detail size_t getOffset() const { return getRangeBegin() + offset_from_begin_pos; } template - std::istream * callImpl(Poco::URI uri_, Poco::Net::HTTPResponse & response, const std::string & method_) + std::istream * callImpl(UpdatableSessionPtr & current_session, Poco::URI uri_, Poco::Net::HTTPResponse & response, const std::string & method_) { // With empty path poco will send "POST HTTP/1.1" its bug. if (uri_.getPath().empty()) @@ -156,7 +157,6 @@ namespace detail Poco::Net::HTTPRequest request(method_, uri_.getPathAndQuery(), Poco::Net::HTTPRequest::HTTP_1_1); request.setHost(uri_.getHost()); // use original, not resolved host name in header - if (out_stream_callback) request.setChunkedTransferEncoding(true); @@ -191,8 +191,7 @@ namespace detail LOG_TRACE(log, "Sending request to {}", uri_.toString()); - auto sess = session->getSession(); - + auto sess = current_session->getSession(); try { auto & stream_out = sess->sendRequest(request); @@ -200,11 +199,13 @@ namespace detail if (out_stream_callback) out_stream_callback(stream_out); - istr = receiveResponse(*sess, request, response, true); + auto result_istr = receiveResponse(*sess, request, response, true); response.getCookies(cookies); - content_encoding = response.get("Content-Encoding", ""); - return istr; + if constexpr (!use_initial_range) + content_encoding = response.get("Content-Encoding", ""); + + return result_istr; } catch (const Poco::Exception & e) { @@ -362,7 +363,7 @@ namespace detail non_retriable_errors.begin(), non_retriable_errors.end(), [&](const auto status) { return http_status != status; }); } - Poco::URI getUriAfterRedirect(const Poco::URI & prev_uri, Poco::Net::HTTPResponse & response) + static Poco::URI getUriAfterRedirect(const Poco::URI & prev_uri, Poco::Net::HTTPResponse & response) { auto location = response.get("Location"); auto location_uri = Poco::URI(location); @@ -379,7 +380,14 @@ namespace detail template void callWithRedirects(Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) { - call(response, method_, throw_on_all_errors); + UpdatableSessionPtr current_session = nullptr; + + if (use_initial_range) + current_session = session->clone(uri); + else + current_session = session; + + call(current_session, response, method_, throw_on_all_errors); Poco::URI prev_uri = uri; while (isRedirect(response.getStatus())) @@ -389,18 +397,22 @@ namespace detail if (remote_host_filter) remote_host_filter->checkURL(uri_redirect); - session->updateSession(uri_redirect); + current_session->updateSession(uri_redirect); - istr = callImpl(uri_redirect, response, method); + auto result_istr = callImpl(current_session, uri_redirect, response, method); + if (!use_initial_range) + istr = result_istr; } } template - void call(Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) + void call(UpdatableSessionPtr & current_session, Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) { try { - istr = callImpl(saved_uri_redirect ? *saved_uri_redirect : uri, response, method_); + auto result_istr = callImpl(current_session, saved_uri_redirect ? *saved_uri_redirect : uri, response, method_); + if (!use_initial_range) + istr = result_istr; } catch (...) { @@ -436,7 +448,7 @@ namespace detail { Poco::Net::HTTPResponse response; - call(response, method); + call(session, response, method); if (initialization_error != InitializeError::NONE) return; @@ -448,7 +460,7 @@ namespace detail session->updateSession(uri_redirect); - istr = callImpl(uri_redirect, response, method); + istr = callImpl(session, uri_redirect, response, method); saved_uri_redirect = uri_redirect; } @@ -731,23 +743,24 @@ namespace detail }; } -class UpdatableSession : public UpdatableSessionBase +class SessionFactory { - using Parent = UpdatableSessionBase; - public: - UpdatableSession(const Poco::URI uri, const ConnectionTimeouts & timeouts_, const UInt64 max_redirects_) - : Parent(uri, timeouts_, max_redirects_) - { - session = makeHTTPSession(initial_uri, timeouts); - } + explicit SessionFactory(const ConnectionTimeouts & timeouts_) + : timeouts(timeouts_) + {} - void buildNewSession(const Poco::URI & uri) override { session = makeHTTPSession(uri, timeouts); } + using SessionType = HTTPSessionPtr; + + SessionType buildNewSession(const Poco::URI & uri) { return makeHTTPSession(uri, timeouts); } +private: + ConnectionTimeouts timeouts; }; -class ReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase> +class ReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase>> { - using Parent = detail::ReadWriteBufferFromHTTPBase>; + using SessionType = UpdatableSession; + using Parent = detail::ReadWriteBufferFromHTTPBase>; public: ReadWriteBufferFromHTTP( @@ -766,7 +779,7 @@ public: bool use_external_buffer_ = false, bool skip_not_found_url_ = false) : Parent( - std::make_shared(uri_, timeouts, max_redirects), + std::make_shared(uri_, max_redirects, std::make_shared(timeouts)), uri_, credentials_, method_, @@ -878,27 +891,27 @@ private: bool skip_not_found_url; }; -class UpdatablePooledSession : public UpdatableSessionBase +class PooledSessionFactory { - using Parent = UpdatableSessionBase; - -private: - size_t per_endpoint_pool_size; - public: - explicit UpdatablePooledSession( - const Poco::URI uri, const ConnectionTimeouts & timeouts_, const UInt64 max_redirects_, size_t per_endpoint_pool_size_) - : Parent(uri, timeouts_, max_redirects_), per_endpoint_pool_size{per_endpoint_pool_size_} - { - session = makePooledHTTPSession(initial_uri, timeouts, per_endpoint_pool_size); - } + explicit PooledSessionFactory( + const ConnectionTimeouts & timeouts_, size_t per_endpoint_pool_size_) + : timeouts(timeouts_) + , per_endpoint_pool_size(per_endpoint_pool_size_) + {} - void buildNewSession(const Poco::URI & uri) override { session = makePooledHTTPSession(uri, timeouts, per_endpoint_pool_size); } + using SessionType = PooledHTTPSessionPtr; + + SessionType buildNewSession(const Poco::URI & uri) { return makePooledHTTPSession(uri, timeouts, per_endpoint_pool_size); } +private: + ConnectionTimeouts timeouts; + size_t per_endpoint_pool_size; }; -class PooledReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase> +class PooledReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase>> { - using Parent = detail::ReadWriteBufferFromHTTPBase>; + using SessionType = UpdatableSession; + using Parent = detail::ReadWriteBufferFromHTTPBase>; public: explicit PooledReadWriteBufferFromHTTP( @@ -911,7 +924,7 @@ public: const UInt64 max_redirects = 0, size_t max_connections_per_endpoint = DEFAULT_COUNT_OF_HTTP_CONNECTIONS_PER_ENDPOINT) : Parent( - std::make_shared(uri_, timeouts_, max_redirects, max_connections_per_endpoint), + std::make_shared(uri_, max_redirects, std::make_shared(timeouts_, max_connections_per_endpoint)), uri_, credentials_, method_, diff --git a/tests/integration/test_storage_url/configs/conf.xml b/tests/integration/test_storage_url/configs/conf.xml index 790752c5c04..a9a78f9de71 100644 --- a/tests/integration/test_storage_url/configs/conf.xml +++ b/tests/integration/test_storage_url/configs/conf.xml @@ -6,5 +6,9 @@ TSV column1 UInt32, column2 UInt32, column3 UInt32 + + PUT + column1 UInt32, column2 UInt32, column3 UInt32 + diff --git a/tests/integration/test_storage_url/test.py b/tests/integration/test_storage_url/test.py index 5591e63400c..570f54f354a 100644 --- a/tests/integration/test_storage_url/test.py +++ b/tests/integration/test_storage_url/test.py @@ -75,3 +75,29 @@ def test_table_function_url_access_rights(): ["c3", "Nullable(Int64)"], ] ) + + +@pytest.mark.parametrize("file_format", ["Parquet", "CSV", "TSV", "JSONEachRow"]) +def test_file_formats(file_format): + url = f"http://nginx:80/{file_format}_file" + + values = ', '.join([f"({i}, {i + 1}, {i + 2})" for i in range(100)]) + node1.query( + f"insert into table function url(url_file, url = '{url}', format = '{file_format}') values", + stdin=values + ) + + for download_threads in [1, 4, 16]: + result = node1.query( + f""" +SELECT * +FROM url('{url}', '{file_format}') +LIMIT 10 +SETTINGS remote_read_min_bytes_for_seek = 1, max_read_buffer_size = 1, max_download_buffer_size = 1, max_download_threads = {download_threads} +""") + + expected_result = "" + for i in range(10): + expected_result += f"{i}\t{i + 1}\t{i + 2}\n" + + assert result == expected_result From 38d026ca6e627149eee5cd714be87a1b06a68b88 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Tue, 28 Feb 2023 14:29:24 +0000 Subject: [PATCH 110/342] Automatic style fix --- tests/integration/test_storage_url/test.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_storage_url/test.py b/tests/integration/test_storage_url/test.py index 570f54f354a..f3ed087d3d0 100644 --- a/tests/integration/test_storage_url/test.py +++ b/tests/integration/test_storage_url/test.py @@ -81,10 +81,10 @@ def test_table_function_url_access_rights(): def test_file_formats(file_format): url = f"http://nginx:80/{file_format}_file" - values = ', '.join([f"({i}, {i + 1}, {i + 2})" for i in range(100)]) + values = ", ".join([f"({i}, {i + 1}, {i + 2})" for i in range(100)]) node1.query( - f"insert into table function url(url_file, url = '{url}', format = '{file_format}') values", - stdin=values + f"insert into table function url(url_file, url = '{url}', format = '{file_format}') values", + stdin=values, ) for download_threads in [1, 4, 16]: @@ -94,7 +94,8 @@ SELECT * FROM url('{url}', '{file_format}') LIMIT 10 SETTINGS remote_read_min_bytes_for_seek = 1, max_read_buffer_size = 1, max_download_buffer_size = 1, max_download_threads = {download_threads} -""") +""" + ) expected_result = "" for i in range(10): From d953baa7e359bf768cabc4d7bab799eec8a56ce0 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Tue, 28 Feb 2023 16:12:51 +0000 Subject: [PATCH 111/342] Review improvements + fixup --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 60 +++++++++++------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 78291d29908..24e94f095e3 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -445,6 +445,12 @@ public: alias_name_to_expressions[node_alias].push_back(node); } + if (const auto * function = node->as()) + { + if (AggregateFunctionFactory::instance().isAggregateFunctionName(function->getFunctionName())) + ++aggregate_functions_counter; + } + expressions.emplace_back(node); } @@ -463,6 +469,12 @@ public: alias_name_to_expressions.erase(it); } + if (const auto * function = top_expression->as()) + { + if (AggregateFunctionFactory::instance().isAggregateFunctionName(function->getFunctionName())) + --aggregate_functions_counter; + } + expressions.pop_back(); } @@ -483,17 +495,7 @@ public: bool hasAggregateFunction() const { - const auto & factory = AggregateFunctionFactory::instance(); - for (const auto & node : expressions) - { - const auto * function = node->as(); - if (!function) - continue; - - if (factory.isAggregateFunctionName(function->getFunctionName())) - return true; - } - return false; + return aggregate_functions_counter > 0; } QueryTreeNodePtr getExpressionWithAlias(const std::string & alias) const @@ -542,6 +544,7 @@ public: private: QueryTreeNodes expressions; + size_t aggregate_functions_counter = 0; std::unordered_map alias_name_to_expressions; }; @@ -3114,11 +3117,6 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook resolve_result.resolve_place = IdentifierResolvePlace::DATABASE_CATALOG; } - if (resolve_result.resolved_identifier - && scope.nullable_group_by_keys.contains(resolve_result.resolved_identifier) - && !scope.expressions_in_resolve_process_stack.hasAggregateFunction()) - resolve_result.resolved_identifier->convertToNullable(); - it->second = resolve_result; /** If identifier was not resolved, or during expression resolution identifier was explicitly added into non cached set, @@ -3126,8 +3124,7 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook */ if (!resolve_result.resolved_identifier || scope.non_cached_identifier_lookups_during_expression_resolve.contains(identifier_lookup) || - !scope.use_identifier_lookup_to_result_cache || - scope.group_by_use_nulls) + !scope.use_identifier_lookup_to_result_cache) scope.identifier_lookup_to_result.erase(it); return resolve_result; @@ -4655,8 +4652,6 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } function_node.resolveAsFunction(std::move(function_base)); - if (scope.group_by_use_nulls && scope.nullable_group_by_keys.contains(node)) - function_node.convertToNullable(); } catch (Exception & e) { @@ -4905,12 +4900,6 @@ ProjectionNames QueryAnalyzer::resolveExpressionNode(QueryTreeNodePtr & node, Id if (result_projection_names.empty()) result_projection_names.push_back(column_node.getColumnName()); - if (scope.group_by_use_nulls && scope.nullable_group_by_keys.contains(node)) - { - node = node->clone(); - node->convertToNullable(); - } - break; } case QueryTreeNodeType::FUNCTION: @@ -4994,6 +4983,14 @@ ProjectionNames QueryAnalyzer::resolveExpressionNode(QueryTreeNodePtr & node, Id } } + if (node + && scope.nullable_group_by_keys.contains(node) + && !scope.expressions_in_resolve_process_stack.hasAggregateFunction()) + { + node = node->clone(); + node->convertToNullable(); + } + /** Update aliases after expression node was resolved. * Do not update node in alias table if we resolve it for duplicate alias. */ @@ -6074,10 +6071,13 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier resolveExpressionNodeList(grouping_sets_keys_list_node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); } - for (const auto & grouping_set : query_node_typed.getGroupBy().getNodes()) + if (scope.group_by_use_nulls) { - for (const auto & group_by_elem : grouping_set->as()->getNodes()) - scope.nullable_group_by_keys.insert(group_by_elem->clone()); + for (const auto & grouping_set : query_node_typed.getGroupBy().getNodes()) + { + for (const auto & group_by_elem : grouping_set->as()->getNodes()) + scope.nullable_group_by_keys.insert(group_by_elem); + } } } else @@ -6090,7 +6090,7 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier if (scope.group_by_use_nulls) { for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) - scope.nullable_group_by_keys.insert(group_by_elem->clone()); + scope.nullable_group_by_keys.insert(group_by_elem); } } } From f53b65a7cc2568d1c4b6850d1e57a88f94dc51ea Mon Sep 17 00:00:00 2001 From: artem-yadr <84010375+artem-yadr@users.noreply.github.com> Date: Tue, 28 Feb 2023 21:01:03 +0300 Subject: [PATCH 112/342] small fix --- base/poco/MongoDB/include/Poco/MongoDB/Connection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h index 5c5f6f76638..dcb813b75bc 100644 --- a/base/poco/MongoDB/include/Poco/MongoDB/Connection.h +++ b/base/poco/MongoDB/include/Poco/MongoDB/Connection.h @@ -162,7 +162,7 @@ namespace MongoDB { return _address; } - inline std::string Connection::uri() const + inline const std::string & Connection::uri() const { return _uri; } From 3f892e52abaf1791b6f1dbe27b3ed6ddad20e7f1 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 22 Jan 2023 13:20:38 +0100 Subject: [PATCH 113/342] Revert "Revert "Merge pull request #44922 from azat/dist/async-INSERT-metrics"" This is the revert of revert since there will be follow up patches to address the issues. This reverts commit a55798626a491139d84692a3c546f27cdde71665. --- src/Storages/Distributed/DirectoryMonitor.cpp | 298 +++++++++--------- src/Storages/Distributed/DirectoryMonitor.h | 27 +- src/Storages/Distributed/DistributedSink.cpp | 20 +- src/Storages/StorageDistributed.cpp | 14 +- src/Storages/StorageDistributed.h | 2 +- ..._INSERT_block_structure_mismatch.reference | 4 +- 6 files changed, 190 insertions(+), 175 deletions(-) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index cb6659e59ce..f2a3471d839 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -59,6 +59,7 @@ namespace ErrorCodes extern const int TOO_MANY_PARTITIONS; extern const int DISTRIBUTED_TOO_MANY_PENDING_BYTES; extern const int ARGUMENT_OUT_OF_BOUND; + extern const int LOGICAL_ERROR; } @@ -363,18 +364,22 @@ StorageDistributedDirectoryMonitor::StorageDistributedDirectoryMonitor( const std::string & relative_path_, ConnectionPoolPtr pool_, ActionBlocker & monitor_blocker_, - BackgroundSchedulePool & bg_pool) + BackgroundSchedulePool & bg_pool, + bool initialize_from_disk) : storage(storage_) , pool(std::move(pool_)) , disk(disk_) , relative_path(relative_path_) , path(fs::path(disk->getPath()) / relative_path / "") + , broken_relative_path(fs::path(relative_path) / "broken") + , broken_path(fs::path(path) / "broken" / "") , should_batch_inserts(storage.getDistributedSettingsRef().monitor_batch_inserts) , split_batch_on_failure(storage.getDistributedSettingsRef().monitor_split_batch_on_failure) , dir_fsync(storage.getDistributedSettingsRef().fsync_directories) , min_batched_block_size_rows(storage.getContext()->getSettingsRef().min_insert_block_size_rows) , min_batched_block_size_bytes(storage.getContext()->getSettingsRef().min_insert_block_size_bytes) , current_batch_file_path(path + "current_batch.txt") + , pending_files(std::numeric_limits::max()) , default_sleep_time(storage.getDistributedSettingsRef().monitor_sleep_time_ms.totalMilliseconds()) , sleep_time(default_sleep_time) , max_sleep_time(storage.getDistributedSettingsRef().monitor_max_sleep_time_ms.totalMilliseconds()) @@ -383,6 +388,11 @@ StorageDistributedDirectoryMonitor::StorageDistributedDirectoryMonitor( , metric_pending_files(CurrentMetrics::DistributedFilesToInsert, 0) , metric_broken_files(CurrentMetrics::BrokenDistributedFilesToInsert, 0) { + fs::create_directory(broken_path); + + if (initialize_from_disk) + initializeFilesFromDisk(); + task_handle = bg_pool.createTask(getLoggerName() + "/Bg", [this]{ run(); }); task_handle->activateAndSchedule(); } @@ -390,35 +400,29 @@ StorageDistributedDirectoryMonitor::StorageDistributedDirectoryMonitor( StorageDistributedDirectoryMonitor::~StorageDistributedDirectoryMonitor() { - if (!quit) + if (!pending_files.isFinished()) { - quit = true; + pending_files.clearAndFinish(); task_handle->deactivate(); } } void StorageDistributedDirectoryMonitor::flushAllData() { - if (quit) + if (pending_files.isFinished()) return; std::lock_guard lock{mutex}; - - const auto & files = getFiles(); - if (!files.empty()) - { - processFiles(files); - - /// Update counters. - getFiles(); - } + if (!hasPendingFiles()) + return; + processFiles(); } void StorageDistributedDirectoryMonitor::shutdownAndDropAllData() { - if (!quit) + if (!pending_files.isFinished()) { - quit = true; + pending_files.clearAndFinish(); task_handle->deactivate(); } @@ -432,19 +436,21 @@ void StorageDistributedDirectoryMonitor::run() std::lock_guard lock{mutex}; bool do_sleep = false; - while (!quit) + while (!pending_files.isFinished()) { do_sleep = true; - const auto & files = getFiles(); - if (files.empty()) + if (!hasPendingFiles()) break; if (!monitor_blocker.isCancelled()) { try { - do_sleep = !processFiles(files); + processFiles(); + /// No errors while processing existing files. + /// Let's see maybe there are more files to process. + do_sleep = false; std::lock_guard status_lock(status_mutex); status.last_exception = std::exception_ptr{}; @@ -469,9 +475,7 @@ void StorageDistributedDirectoryMonitor::run() } } else - { LOG_DEBUG(log, "Skipping send data over distributed table."); - } const auto now = std::chrono::system_clock::now(); if (now - last_decrease_time > decrease_error_count_period) @@ -486,10 +490,7 @@ void StorageDistributedDirectoryMonitor::run() break; } - /// Update counters. - getFiles(); - - if (!quit && do_sleep) + if (!pending_files.isFinished() && do_sleep) task_handle->scheduleAfter(sleep_time.count()); } @@ -567,41 +568,83 @@ ConnectionPoolPtr StorageDistributedDirectoryMonitor::createPool(const std::stri settings.distributed_replica_error_cap); } - -std::map StorageDistributedDirectoryMonitor::getFiles() +bool StorageDistributedDirectoryMonitor::hasPendingFiles() const { - std::map files; + return fs::exists(current_batch_file_path) || !current_batch_file.empty() || !pending_files.empty(); +} + +void StorageDistributedDirectoryMonitor::initializeFilesFromDisk() +{ + /// NOTE: This method does not requires to hold status_mutex, hence, no TSA + /// annotations in the header file. fs::directory_iterator end; - for (fs::directory_iterator it{path}; it != end; ++it) + + /// Initialize pending files { - const auto & file_path_str = it->path(); - if (!it->is_directory() && startsWith(fs::path(file_path_str).extension(), ".bin")) + size_t bytes_count = 0; + + for (fs::directory_iterator it{path}; it != end; ++it) { - files[parse(fs::path(file_path_str).stem())] = file_path_str; + const auto & file_path = it->path(); + const auto & base_name = file_path.stem().string(); + if (!it->is_directory() && startsWith(fs::path(file_path).extension(), ".bin") && parse(base_name)) + { + const std::string & file_path_str = file_path.string(); + if (!pending_files.push(file_path_str)) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot add pending file"); + bytes_count += fs::file_size(file_path); + } + else if (base_name != "tmp" && base_name != "broken") + { + /// It is OK to log current_batch.txt here too (useful for debugging). + LOG_WARNING(log, "Unexpected file {} in {}", file_path.string(), path); + } } + + LOG_TRACE(log, "Files set to {}", pending_files.size()); + LOG_TRACE(log, "Bytes set to {}", bytes_count); + + metric_pending_files.changeTo(pending_files.size()); + status.files_count = pending_files.size(); + status.bytes_count = bytes_count; } - return files; + /// Initialize broken files + { + size_t broken_bytes_count = 0; + size_t broken_files = 0; + + for (fs::directory_iterator it{broken_path}; it != end; ++it) + { + const auto & file_path = it->path(); + if (!it->is_directory() && startsWith(fs::path(file_path).extension(), ".bin") && parse(file_path.stem())) + broken_bytes_count += fs::file_size(file_path); + else + LOG_WARNING(log, "Unexpected file {} in {}", file_path.string(), broken_path); + } + + LOG_TRACE(log, "Broken files set to {}", broken_files); + LOG_TRACE(log, "Broken bytes set to {}", broken_bytes_count); + + metric_broken_files.changeTo(broken_files); + status.broken_files_count = broken_files; + status.broken_bytes_count = broken_bytes_count; + } } -bool StorageDistributedDirectoryMonitor::processFiles(const std::map & files) +void StorageDistributedDirectoryMonitor::processFiles() { if (should_batch_inserts) - { - processFilesWithBatching(files); - } + processFilesWithBatching(); else { - for (const auto & file : files) - { - if (quit) - return true; + /// Process unprocessed file. + if (!current_batch_file.empty()) + processFile(current_batch_file); - processFile(file.second); - } + while (pending_files.tryPop(current_batch_file)) + processFile(current_batch_file); } - - return true; } void StorageDistributedDirectoryMonitor::processFile(const std::string & file_path) @@ -648,7 +691,11 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa thread_trace_context->root_span.addAttribute(std::current_exception()); e.addMessage(fmt::format("While sending {}", file_path)); - maybeMarkAsBroken(file_path, e); + if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) + { + markAsBroken(file_path); + current_batch_file.clear(); + } throw; } catch (...) @@ -661,6 +708,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); markAsSend(file_path); + current_batch_file.clear(); LOG_TRACE(log, "Finished processing `{}` (took {} ms)", file_path, watch.elapsedMilliseconds()); } @@ -700,25 +748,19 @@ struct StorageDistributedDirectoryMonitor::BatchHeader struct StorageDistributedDirectoryMonitor::Batch { - /// File indexes for this batch. - std::vector file_indices; size_t total_rows = 0; size_t total_bytes = 0; bool recovered = false; StorageDistributedDirectoryMonitor & parent; - /// Information about all available indexes (not only for the current batch). - const std::map & file_index_to_path; + std::vector files; bool split_batch_on_failure = true; bool fsync = false; bool dir_fsync = false; - Batch( - StorageDistributedDirectoryMonitor & parent_, - const std::map & file_index_to_path_) + explicit Batch(StorageDistributedDirectoryMonitor & parent_) : parent(parent_) - , file_index_to_path(file_index_to_path_) , split_batch_on_failure(parent.split_batch_on_failure) , fsync(parent.storage.getDistributedSettingsRef().fsync_after_insert) , dir_fsync(parent.dir_fsync) @@ -733,7 +775,7 @@ struct StorageDistributedDirectoryMonitor::Batch void send() { - if (file_indices.empty()) + if (files.empty()) return; CurrentMetrics::Increment metric_increment{CurrentMetrics::DistributedSend}; @@ -776,7 +818,7 @@ struct StorageDistributedDirectoryMonitor::Batch } catch (const Exception & e) { - if (split_batch_on_failure && file_indices.size() > 1 && isSplittableErrorCode(e.code(), e.isRemoteException())) + if (split_batch_on_failure && files.size() > 1 && isSplittableErrorCode(e.code(), e.isRemoteException())) { tryLogCurrentException(parent.log, "Trying to split batch due to"); sendSeparateFiles(); @@ -796,49 +838,28 @@ struct StorageDistributedDirectoryMonitor::Batch } else { - std::vector files; - for (auto file_index_info : file_indices | boost::adaptors::indexed()) - { - if (file_index_info.index() > 8) - { - files.push_back("..."); - break; - } - - auto file_index = file_index_info.value(); - auto file_path = file_index_to_path.find(file_index); - if (file_path != file_index_to_path.end()) - files.push_back(file_path->second); - else - files.push_back(fmt::format("#{}.bin (deleted)", file_index)); - } - e.addMessage(fmt::format("While sending batch, size: {}, files: {}", file_indices.size(), fmt::join(files, "\n"))); - + e.addMessage(fmt::format("While sending a batch of {} files, files: {}", files.size(), fmt::join(files, "\n"))); throw; } } if (!batch_broken) { - LOG_TRACE(parent.log, "Sent a batch of {} files (took {} ms).", file_indices.size(), watch.elapsedMilliseconds()); + LOG_TRACE(parent.log, "Sent a batch of {} files (took {} ms).", files.size(), watch.elapsedMilliseconds()); auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, parent.disk, parent.relative_path); - for (UInt64 file_index : file_indices) - parent.markAsSend(file_index_to_path.at(file_index)); + for (const auto & file : files) + parent.markAsSend(file); } else if (!batch_marked_as_broken) { - LOG_ERROR(parent.log, "Marking a batch of {} files as broken.", file_indices.size()); + LOG_ERROR(parent.log, "Marking a batch of {} files as broken, files: {}", files.size(), fmt::join(files, "\n")); - for (UInt64 file_idx : file_indices) - { - auto file_path = file_index_to_path.find(file_idx); - if (file_path != file_index_to_path.end()) - parent.markAsBroken(file_path->second); - } + for (const auto & file : files) + parent.markAsBroken(file); } - file_indices.clear(); + files.clear(); total_rows = 0; total_bytes = 0; recovered = false; @@ -848,8 +869,11 @@ struct StorageDistributedDirectoryMonitor::Batch void writeText(WriteBuffer & out) { - for (UInt64 file_idx : file_indices) - out << file_idx << '\n'; + for (const auto & file : files) + { + UInt64 file_index = parse(fs::path(file).stem()); + out << file_index << '\n'; + } } void readText(ReadBuffer & in) @@ -858,8 +882,9 @@ struct StorageDistributedDirectoryMonitor::Batch { UInt64 idx; in >> idx >> "\n"; - file_indices.push_back(idx); + files.push_back(fmt::format("{}/{}.bin", parent.path, idx)); } + recovered = true; } @@ -871,14 +896,9 @@ private: IConnectionPool::Entry connection; - for (UInt64 file_idx : file_indices) + for (const auto & file : files) { - auto file_path = file_index_to_path.find(file_idx); - if (file_path == file_index_to_path.end()) - throw Exception(ErrorCodes::DISTRIBUTED_BROKEN_BATCH_INFO, - "Failed to send batch: file with index {} is absent", file_idx); - - ReadBufferFromFile in(file_path->second); + ReadBufferFromFile in(file); const auto & distributed_header = readDistributedHeader(in, parent.log); OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, @@ -892,7 +912,7 @@ private: compression_expected = connection->getCompression() == Protocol::Compression::Enable; LOG_DEBUG(parent.log, "Sending a batch of {} files to {} ({} rows, {} bytes).", - file_indices.size(), + files.size(), connection->getDescription(), formatReadableQuantity(total_rows), formatReadableSizeWithBinarySuffix(total_bytes)); @@ -913,19 +933,11 @@ private: { size_t broken_files = 0; - for (UInt64 file_idx : file_indices) + for (const auto & file : files) { - auto file_path = file_index_to_path.find(file_idx); - if (file_path == file_index_to_path.end()) - { - LOG_ERROR(parent.log, "Failed to send one file from batch: file with index {} is absent", file_idx); - ++broken_files; - continue; - } - try { - ReadBufferFromFile in(file_path->second); + ReadBufferFromFile in(file); const auto & distributed_header = readDistributedHeader(in, parent.log); // this function is called in a separated thread, so we set up the trace context from the file @@ -947,9 +959,11 @@ private: } catch (Exception & e) { - e.addMessage(fmt::format("While sending {}", file_path->second)); - parent.maybeMarkAsBroken(file_path->second, e); - ++broken_files; + if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) + { + parent.markAsBroken(file); + ++broken_files; + } } } @@ -1029,13 +1043,18 @@ std::shared_ptr StorageDistributedDirectoryMonitor::createSourceFromFil return std::make_shared(file_name); } -bool StorageDistributedDirectoryMonitor::addAndSchedule(size_t file_size, size_t ms) +bool StorageDistributedDirectoryMonitor::addAndSchedule(const std::string & file_path, size_t file_size, size_t ms) { - if (quit) + /// NOTE: It is better not to throw in this case, since the file is already + /// on disk (see DistributedSink), and it will be processed next time. + if (pending_files.isFinished()) return false; + if (!pending_files.push(file_path)) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot add pending file"); + { - std::lock_guard status_lock(status_mutex); + std::lock_guard lock(status_mutex); metric_pending_files.add(); status.bytes_count += file_size; ++status.files_count; @@ -1051,33 +1070,25 @@ StorageDistributedDirectoryMonitor::Status StorageDistributedDirectoryMonitor::g return current_status; } -void StorageDistributedDirectoryMonitor::processFilesWithBatching(const std::map & files) +void StorageDistributedDirectoryMonitor::processFilesWithBatching() { - std::unordered_set file_indices_to_skip; - + /// Possibly, we failed to send a batch on the previous iteration. Try to send exactly the same batch. if (fs::exists(current_batch_file_path)) { - /// Possibly, we failed to send a batch on the previous iteration. Try to send exactly the same batch. - Batch batch(*this, files); + Batch batch(*this); ReadBufferFromFile in{current_batch_file_path}; batch.readText(in); - file_indices_to_skip.insert(batch.file_indices.begin(), batch.file_indices.end()); batch.send(); + + auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); + fs::remove(current_batch_file_path); } std::unordered_map header_to_batch; - for (const auto & file : files) + std::string file_path; + while (pending_files.tryPop(file_path)) { - if (quit) - return; - - UInt64 file_idx = file.first; - const String & file_path = file.second; - - if (file_indices_to_skip.contains(file_idx)) - continue; - size_t total_rows = 0; size_t total_bytes = 0; Block header; @@ -1116,8 +1127,9 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching(const std::map } catch (const Exception & e) { - if (maybeMarkAsBroken(file_path, e)) + if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) { + markAsBroken(file_path); tryLogCurrentException(log, "File is marked broken due to"); continue; } @@ -1131,9 +1143,9 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching(const std::map std::move(distributed_header.client_info), std::move(header) ); - Batch & batch = header_to_batch.try_emplace(batch_header, *this, files).first->second; + Batch & batch = header_to_batch.try_emplace(batch_header, *this).first->second; - batch.file_indices.push_back(file_idx); + batch.files.push_back(file_path); batch.total_rows += total_rows; batch.total_bytes += total_bytes; @@ -1161,16 +1173,10 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching(const std::map void StorageDistributedDirectoryMonitor::markAsBroken(const std::string & file_path) { - const auto last_path_separator_pos = file_path.rfind('/'); - const auto & base_path = file_path.substr(0, last_path_separator_pos + 1); - const auto & file_name = file_path.substr(last_path_separator_pos + 1); - const String & broken_path = fs::path(base_path) / "broken/"; - const String & broken_file_path = fs::path(broken_path) / file_name; - - fs::create_directory(broken_path); + const String & broken_file_path = fs::path(broken_path) / fs::path(file_path).filename(); auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); - auto broken_dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, fs::path(relative_path) / "broken/"); + auto broken_dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, broken_relative_path); { std::lock_guard status_lock(status_mutex); @@ -1204,21 +1210,9 @@ void StorageDistributedDirectoryMonitor::markAsSend(const std::string & file_pat fs::remove(file_path); } -bool StorageDistributedDirectoryMonitor::maybeMarkAsBroken(const std::string & file_path, const Exception & e) -{ - /// Mark file as broken if necessary. - if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) - { - markAsBroken(file_path); - return true; - } - else - return false; -} - std::string StorageDistributedDirectoryMonitor::getLoggerName() const { - return storage.getStorageID().getFullTableName() + ".DirectoryMonitor"; + return storage.getStorageID().getFullTableName() + ".DirectoryMonitor." + disk->getName(); } void StorageDistributedDirectoryMonitor::updatePath(const std::string & new_relative_path) diff --git a/src/Storages/Distributed/DirectoryMonitor.h b/src/Storages/Distributed/DirectoryMonitor.h index 030d6acf6e2..313125e16bd 100644 --- a/src/Storages/Distributed/DirectoryMonitor.h +++ b/src/Storages/Distributed/DirectoryMonitor.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -38,7 +39,8 @@ public: const std::string & relative_path_, ConnectionPoolPtr pool_, ActionBlocker & monitor_blocker_, - BackgroundSchedulePool & bg_pool); + BackgroundSchedulePool & bg_pool, + bool initialize_from_disk); ~StorageDistributedDirectoryMonitor(); @@ -53,7 +55,7 @@ public: static std::shared_ptr createSourceFromFile(const String & file_name); /// For scheduling via DistributedSink. - bool addAndSchedule(size_t file_size, size_t ms); + bool addAndSchedule(const std::string & file_path, size_t file_size, size_t ms); struct InternalStatus { @@ -79,14 +81,15 @@ public: private: void run(); - std::map getFiles(); - bool processFiles(const std::map & files); + bool hasPendingFiles() const; + + void initializeFilesFromDisk(); + void processFiles(); void processFile(const std::string & file_path); - void processFilesWithBatching(const std::map & files); + void processFilesWithBatching(); void markAsBroken(const std::string & file_path); void markAsSend(const std::string & file_path); - bool maybeMarkAsBroken(const std::string & file_path, const Exception & e); std::string getLoggerName() const; @@ -96,25 +99,33 @@ private: DiskPtr disk; std::string relative_path; std::string path; + std::string broken_relative_path; + std::string broken_path; const bool should_batch_inserts = false; const bool split_batch_on_failure = true; const bool dir_fsync = false; const size_t min_batched_block_size_rows = 0; const size_t min_batched_block_size_bytes = 0; - String current_batch_file_path; + + /// This is pending data (due to some error) for should_batch_inserts==true + std::string current_batch_file_path; + /// This is pending data (due to some error) for should_batch_inserts==false + std::string current_batch_file; struct BatchHeader; struct Batch; std::mutex status_mutex; + InternalStatus status; + ConcurrentBoundedQueue pending_files; + const std::chrono::milliseconds default_sleep_time; std::chrono::milliseconds sleep_time; const std::chrono::milliseconds max_sleep_time; std::chrono::time_point last_decrease_time {std::chrono::system_clock::now()}; - std::atomic quit {false}; std::mutex mutex; Poco::Logger * log; ActionBlocker & monitor_blocker; diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index bac13ea37cf..edbb2acc923 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -724,6 +724,9 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const return guard; }; + std::vector bin_files; + bin_files.reserve(dir_names.size()); + auto it = dir_names.begin(); /// on first iteration write block to a temporary directory for subsequent /// hardlinking to ensure the inode is not freed until we're done @@ -802,8 +805,8 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const } // Create hardlink here to reuse increment number - const std::string block_file_path(fs::path(path) / file_name); - createHardLink(first_file_tmp_path, block_file_path); + bin_files.push_back(fs::path(path) / file_name); + createHardLink(first_file_tmp_path, bin_files.back()); auto dir_sync_guard = make_directory_sync_guard(*it); } ++it; @@ -814,8 +817,8 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const const std::string path(fs::path(disk_path) / (data_path + *it)); fs::create_directory(path); - const std::string block_file_path(fs::path(path) / (toString(storage.file_names_increment.get()) + ".bin")); - createHardLink(first_file_tmp_path, block_file_path); + bin_files.push_back(fs::path(path) / (toString(storage.file_names_increment.get()) + ".bin")); + createHardLink(first_file_tmp_path, bin_files.back()); auto dir_sync_guard = make_directory_sync_guard(*it); } @@ -826,10 +829,13 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const /// Notify auto sleep_ms = context->getSettingsRef().distributed_directory_monitor_sleep_time_ms; - for (const auto & dir_name : dir_names) + for (size_t i = 0; i < dir_names.size(); ++i) { - auto & directory_monitor = storage.requireDirectoryMonitor(disk, dir_name); - directory_monitor.addAndSchedule(file_size, sleep_ms.totalMilliseconds()); + const auto & dir_name = dir_names[i]; + const auto & bin_file = bin_files[i]; + + auto & directory_monitor = storage.requireDirectoryMonitor(disk, dir_name, /* startup= */ false); + directory_monitor.addAndSchedule(bin_file, file_size, sleep_ms.totalMilliseconds()); } } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 5516d6cadf0..a38beef983c 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -1208,12 +1208,15 @@ void StorageDistributed::createDirectoryMonitors(const DiskPtr & disk) const auto & dir_path = it->path(); if (std::filesystem::is_directory(dir_path)) { + /// Created by DistributedSink const auto & tmp_path = dir_path / "tmp"; - - /// "tmp" created by DistributedSink if (std::filesystem::is_directory(tmp_path) && std::filesystem::is_empty(tmp_path)) std::filesystem::remove(tmp_path); + const auto & broken_path = dir_path / "broken"; + if (std::filesystem::is_directory(broken_path) && std::filesystem::is_empty(broken_path)) + std::filesystem::remove(broken_path); + if (std::filesystem::is_empty(dir_path)) { LOG_DEBUG(log, "Removing {} (used for async INSERT into Distributed)", dir_path.string()); @@ -1222,14 +1225,14 @@ void StorageDistributed::createDirectoryMonitors(const DiskPtr & disk) } else { - requireDirectoryMonitor(disk, dir_path.filename().string()); + requireDirectoryMonitor(disk, dir_path.filename().string(), /* startup= */ true); } } } } -StorageDistributedDirectoryMonitor& StorageDistributed::requireDirectoryMonitor(const DiskPtr & disk, const std::string & name) +StorageDistributedDirectoryMonitor& StorageDistributed::requireDirectoryMonitor(const DiskPtr & disk, const std::string & name, bool startup) { const std::string & disk_path = disk->getPath(); const std::string key(disk_path + name); @@ -1243,7 +1246,8 @@ StorageDistributedDirectoryMonitor& StorageDistributed::requireDirectoryMonitor( *this, disk, relative_data_path + name, node_data.connection_pool, monitors_blocker, - getContext()->getDistributedSchedulePool()); + getContext()->getDistributedSchedulePool(), + /* initialize_from_disk= */ startup); } return *node_data.directory_monitor; } diff --git a/src/Storages/StorageDistributed.h b/src/Storages/StorageDistributed.h index 66fd7b77889..25a752fe795 100644 --- a/src/Storages/StorageDistributed.h +++ b/src/Storages/StorageDistributed.h @@ -166,7 +166,7 @@ private: /// create directory monitors for each existing subdirectory void createDirectoryMonitors(const DiskPtr & disk); /// ensure directory monitor thread and connectoin pool creation by disk and subdirectory name - StorageDistributedDirectoryMonitor & requireDirectoryMonitor(const DiskPtr & disk, const std::string & name); + StorageDistributedDirectoryMonitor & requireDirectoryMonitor(const DiskPtr & disk, const std::string & name, bool startup); /// Return list of metrics for all created monitors /// (note that monitors are created lazily, i.e. until at least one INSERT executed) diff --git a/tests/queries/0_stateless/01791_dist_INSERT_block_structure_mismatch.reference b/tests/queries/0_stateless/01791_dist_INSERT_block_structure_mismatch.reference index f3be69d3279..b0d8284faa5 100644 --- a/tests/queries/0_stateless/01791_dist_INSERT_block_structure_mismatch.reference +++ b/tests/queries/0_stateless/01791_dist_INSERT_block_structure_mismatch.reference @@ -1,7 +1,7 @@ DistributedSink: Structure does not match (remote: n Int8 Int8(size = 0), local: n UInt64 UInt64(size = 1)), implicit conversion will be done. DistributedSink: Structure does not match (remote: n Int8 Int8(size = 0), local: n UInt64 UInt64(size = 1)), implicit conversion will be done. - default.dist_01683.DirectoryMonitor: Structure does not match (remote: n Int8 Int8(size = 0), local: n UInt64 UInt64(size = 0)), implicit conversion will be done - default.dist_01683.DirectoryMonitor: Structure does not match (remote: n Int8 Int8(size = 0), local: n UInt64 UInt64(size = 0)), implicit conversion will be done + default.dist_01683.DirectoryMonitor.default: Structure does not match (remote: n Int8 Int8(size = 0), local: n UInt64 UInt64(size = 0)), implicit conversion will be done + default.dist_01683.DirectoryMonitor.default: Structure does not match (remote: n Int8 Int8(size = 0), local: n UInt64 UInt64(size = 0)), implicit conversion will be done 1 1 2 From 16646c0923338adfd20e2853de4a1b9373534d58 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 10:07:44 +0100 Subject: [PATCH 114/342] Rename DirectoryMonitor::current_batch_file to current_file Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 14 +++++++------- src/Storages/Distributed/DirectoryMonitor.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index f2a3471d839..e536f1e63d8 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -570,7 +570,7 @@ ConnectionPoolPtr StorageDistributedDirectoryMonitor::createPool(const std::stri bool StorageDistributedDirectoryMonitor::hasPendingFiles() const { - return fs::exists(current_batch_file_path) || !current_batch_file.empty() || !pending_files.empty(); + return fs::exists(current_batch_file_path) || !current_file.empty() || !pending_files.empty(); } void StorageDistributedDirectoryMonitor::initializeFilesFromDisk() @@ -639,11 +639,11 @@ void StorageDistributedDirectoryMonitor::processFiles() else { /// Process unprocessed file. - if (!current_batch_file.empty()) - processFile(current_batch_file); + if (!current_file.empty()) + processFile(current_file); - while (pending_files.tryPop(current_batch_file)) - processFile(current_batch_file); + while (pending_files.tryPop(current_file)) + processFile(current_file); } } @@ -694,7 +694,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) { markAsBroken(file_path); - current_batch_file.clear(); + current_file.clear(); } throw; } @@ -708,7 +708,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); markAsSend(file_path); - current_batch_file.clear(); + current_file.clear(); LOG_TRACE(log, "Finished processing `{}` (took {} ms)", file_path, watch.elapsedMilliseconds()); } diff --git a/src/Storages/Distributed/DirectoryMonitor.h b/src/Storages/Distributed/DirectoryMonitor.h index 313125e16bd..06843b77a4b 100644 --- a/src/Storages/Distributed/DirectoryMonitor.h +++ b/src/Storages/Distributed/DirectoryMonitor.h @@ -111,7 +111,7 @@ private: /// This is pending data (due to some error) for should_batch_inserts==true std::string current_batch_file_path; /// This is pending data (due to some error) for should_batch_inserts==false - std::string current_batch_file; + std::string current_file; struct BatchHeader; struct Batch; From ef1e642e059510ff182a6a9dc97d34fdb7c1a071 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 10:12:50 +0100 Subject: [PATCH 115/342] Add log message to StorageDistributedDirectoryMonitor::addAndSchedule() Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index e536f1e63d8..c04e54b6bba 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -1048,7 +1048,10 @@ bool StorageDistributedDirectoryMonitor::addAndSchedule(const std::string & file /// NOTE: It is better not to throw in this case, since the file is already /// on disk (see DistributedSink), and it will be processed next time. if (pending_files.isFinished()) + { + LOG_DEBUG(log, "File {} had not been scheduled, since the table had been detached", file_path); return false; + } if (!pending_files.push(file_path)) throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot add pending file"); From f0a2efa63014ff1349340e28647ab067698cf974 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 10:20:28 +0100 Subject: [PATCH 116/342] Always manipulate with absolute file paths in DirectoryMonitor Otherwise on batch restore we can get the difference in file paths. Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 16 ++++++++++------ src/Storages/Distributed/DirectoryMonitor.h | 3 ++- src/Storages/Distributed/DistributedSink.cpp | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index c04e54b6bba..eead8c8ea42 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -573,6 +573,12 @@ bool StorageDistributedDirectoryMonitor::hasPendingFiles() const return fs::exists(current_batch_file_path) || !current_file.empty() || !pending_files.empty(); } +void StorageDistributedDirectoryMonitor::addFile(const std::string & file_path) +{ + if (!pending_files.push(fs::absolute(file_path).string())) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot schedule a file '{}'", file_path); +} + void StorageDistributedDirectoryMonitor::initializeFilesFromDisk() { /// NOTE: This method does not requires to hold status_mutex, hence, no TSA @@ -591,8 +597,7 @@ void StorageDistributedDirectoryMonitor::initializeFilesFromDisk() if (!it->is_directory() && startsWith(fs::path(file_path).extension(), ".bin") && parse(base_name)) { const std::string & file_path_str = file_path.string(); - if (!pending_files.push(file_path_str)) - throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot add pending file"); + addFile(file_path_str); bytes_count += fs::file_size(file_path); } else if (base_name != "tmp" && base_name != "broken") @@ -882,7 +887,7 @@ struct StorageDistributedDirectoryMonitor::Batch { UInt64 idx; in >> idx >> "\n"; - files.push_back(fmt::format("{}/{}.bin", parent.path, idx)); + files.push_back(fs::absolute(fmt::format("{}/{}.bin", parent.path, idx)).string()); } recovered = true; @@ -1043,7 +1048,7 @@ std::shared_ptr StorageDistributedDirectoryMonitor::createSourceFromFil return std::make_shared(file_name); } -bool StorageDistributedDirectoryMonitor::addAndSchedule(const std::string & file_path, size_t file_size, size_t ms) +bool StorageDistributedDirectoryMonitor::addFileAndSchedule(const std::string & file_path, size_t file_size, size_t ms) { /// NOTE: It is better not to throw in this case, since the file is already /// on disk (see DistributedSink), and it will be processed next time. @@ -1053,8 +1058,7 @@ bool StorageDistributedDirectoryMonitor::addAndSchedule(const std::string & file return false; } - if (!pending_files.push(file_path)) - throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot add pending file"); + addFile(file_path); { std::lock_guard lock(status_mutex); diff --git a/src/Storages/Distributed/DirectoryMonitor.h b/src/Storages/Distributed/DirectoryMonitor.h index 06843b77a4b..9b1596d45e3 100644 --- a/src/Storages/Distributed/DirectoryMonitor.h +++ b/src/Storages/Distributed/DirectoryMonitor.h @@ -55,7 +55,7 @@ public: static std::shared_ptr createSourceFromFile(const String & file_name); /// For scheduling via DistributedSink. - bool addAndSchedule(const std::string & file_path, size_t file_size, size_t ms); + bool addFileAndSchedule(const std::string & file_path, size_t file_size, size_t ms); struct InternalStatus { @@ -83,6 +83,7 @@ private: bool hasPendingFiles() const; + void addFile(const std::string & file_path); void initializeFilesFromDisk(); void processFiles(); void processFile(const std::string & file_path); diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index edbb2acc923..c9c235596db 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -835,7 +835,7 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const const auto & bin_file = bin_files[i]; auto & directory_monitor = storage.requireDirectoryMonitor(disk, dir_name, /* startup= */ false); - directory_monitor.addAndSchedule(bin_file, file_size, sleep_ms.totalMilliseconds()); + directory_monitor.addFileAndSchedule(bin_file, file_size, sleep_ms.totalMilliseconds()); } } From 13a3e03f19b13f0210e93e28f433e3abee7b3fd9 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 10:27:59 +0100 Subject: [PATCH 117/342] Introduce StorageDistributedDirectoryMonitor::Batch::{de,}serialize() Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index eead8c8ea42..fe5018db821 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -793,24 +793,7 @@ struct StorageDistributedDirectoryMonitor::Batch /// we must try to re-send exactly the same batches. /// So we save contents of the current batch into the current_batch_file_path file /// and truncate it afterwards if all went well. - - /// Temporary file is required for atomicity. - String tmp_file{parent.current_batch_file_path + ".tmp"}; - - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, parent.disk, parent.relative_path); - if (fs::exists(tmp_file)) - LOG_ERROR(parent.log, "Temporary file {} exists. Unclean shutdown?", backQuote(tmp_file)); - - { - WriteBufferFromFile out{tmp_file, O_WRONLY | O_TRUNC | O_CREAT}; - writeText(out); - - out.finalize(); - if (fsync) - out.sync(); - } - - fs::rename(tmp_file, parent.current_batch_file_path); + serialize(); } bool batch_broken = false; @@ -872,6 +855,34 @@ struct StorageDistributedDirectoryMonitor::Batch fs::resize_file(parent.current_batch_file_path, 0); } + void serialize() + { + /// Temporary file is required for atomicity. + String tmp_file{parent.current_batch_file_path + ".tmp"}; + + auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, parent.disk, parent.relative_path); + if (fs::exists(tmp_file)) + LOG_ERROR(parent.log, "Temporary file {} exists. Unclean shutdown?", backQuote(tmp_file)); + + { + WriteBufferFromFile out{tmp_file, O_WRONLY | O_TRUNC | O_CREAT}; + writeText(out); + + out.finalize(); + if (fsync) + out.sync(); + } + + fs::rename(tmp_file, parent.current_batch_file_path); + } + + void deserialize() + { + ReadBufferFromFile in{parent.current_batch_file_path}; + readText(in); + } + +private: void writeText(WriteBuffer & out) { for (const auto & file : files) @@ -893,7 +904,6 @@ struct StorageDistributedDirectoryMonitor::Batch recovered = true; } -private: void sendBatch() { std::unique_ptr remote; @@ -1083,8 +1093,7 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() if (fs::exists(current_batch_file_path)) { Batch batch(*this); - ReadBufferFromFile in{current_batch_file_path}; - batch.readText(in); + batch.deserialize(); batch.send(); auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); From 0c19a75a1c3225d8ec6625e2ca37434245f63a4c Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 10:29:59 +0100 Subject: [PATCH 118/342] Add log message for batch restore Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index fe5018db821..c86661eb2ec 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -1092,6 +1092,8 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() /// Possibly, we failed to send a batch on the previous iteration. Try to send exactly the same batch. if (fs::exists(current_batch_file_path)) { + LOG_DEBUG(log, "Restoring the batch"); + Batch batch(*this); batch.deserialize(); batch.send(); From 22a39e29f760969607b728a9de671c18071cbfb4 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 10:41:38 +0100 Subject: [PATCH 119/342] Add a comment for StorageDistributedDirectoryMonitor::Batch::recovered Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index c86661eb2ec..06aa5219be0 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -755,6 +755,7 @@ struct StorageDistributedDirectoryMonitor::Batch { size_t total_rows = 0; size_t total_bytes = 0; + /// Does the batch had been created from the files in current_batch.txt? bool recovered = false; StorageDistributedDirectoryMonitor & parent; From 325a7b23050f3964704f87fabfacc92b0b737e9a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 16:53:51 +0100 Subject: [PATCH 120/342] Separate out DistributedHeader as DistributedAsyncInsertHeader Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 17 ++- .../DistributedAsyncInsertHeader.cpp | 109 ++++++++++++++++++ .../DistributedAsyncInsertHeader.h | 38 ++++++ 3 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 src/Storages/Distributed/DistributedAsyncInsertHeader.cpp create mode 100644 src/Storages/Distributed/DistributedAsyncInsertHeader.h diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index 06aa5219be0..469f2f90954 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -12,10 +12,9 @@ #include #include #include -#include +#include #include #include -#include #include #include #include @@ -277,7 +276,7 @@ namespace return nullptr; } - void writeAndConvert(RemoteInserter & remote, const DistributedHeader & distributed_header, ReadBufferFromFile & in) + void writeAndConvert(RemoteInserter & remote, const DistributedAsyncInsertHeader & distributed_header, ReadBufferFromFile & in) { CompressedReadBuffer decompressing_in(in); NativeReader block_in(decompressing_in, distributed_header.revision); @@ -296,7 +295,7 @@ namespace } void writeRemoteConvert( - const DistributedHeader & distributed_header, + const DistributedAsyncInsertHeader & distributed_header, RemoteInserter & remote, bool compression_expected, ReadBufferFromFile & in, @@ -662,7 +661,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa CurrentMetrics::Increment metric_increment{CurrentMetrics::DistributedSend}; ReadBufferFromFile in(file_path); - const auto & distributed_header = readDistributedHeader(in, log); + const auto & distributed_header = readDistributedAsyncInsertHeader(in, log); thread_trace_context = std::make_unique(__PRETTY_FUNCTION__, distributed_header.client_info.client_trace_context, @@ -915,7 +914,7 @@ private: for (const auto & file : files) { ReadBufferFromFile in(file); - const auto & distributed_header = readDistributedHeader(in, parent.log); + const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, distributed_header.client_info.client_trace_context, @@ -954,7 +953,7 @@ private: try { ReadBufferFromFile in(file); - const auto & distributed_header = readDistributedHeader(in, parent.log); + const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); // this function is called in a separated thread, so we set up the trace context from the file OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, @@ -1111,12 +1110,12 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() size_t total_rows = 0; size_t total_bytes = 0; Block header; - DistributedHeader distributed_header; + DistributedAsyncInsertHeader distributed_header; try { /// Determine metadata of the current file and check if it is not broken. ReadBufferFromFile in{file_path}; - distributed_header = readDistributedHeader(in, log); + distributed_header = readDistributedAsyncInsertHeader(in, log); if (distributed_header.rows) { diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp new file mode 100644 index 00000000000..19235c91cc6 --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int CANNOT_READ_ALL_DATA; + extern const int CHECKSUM_DOESNT_MATCH; +} + +DistributedAsyncInsertHeader readDistributedAsyncInsertHeader(ReadBufferFromFile & in, Poco::Logger * log) +{ + DistributedAsyncInsertHeader distributed_header; + + UInt64 query_size; + readVarUInt(query_size, in); + + if (query_size == DBMS_DISTRIBUTED_SIGNATURE_HEADER) + { + /// Read the header as a string. + String header_data; + readStringBinary(header_data, in); + + /// Check the checksum of the header. + CityHash_v1_0_2::uint128 expected_checksum; + readPODBinary(expected_checksum, in); + CityHash_v1_0_2::uint128 calculated_checksum = + CityHash_v1_0_2::CityHash128(header_data.data(), header_data.size()); + if (expected_checksum != calculated_checksum) + { + throw Exception(ErrorCodes::CHECKSUM_DOESNT_MATCH, + "Checksum of extra info doesn't match: corrupted data. Reference: {}{}. Actual: {}{}.", + getHexUIntLowercase(expected_checksum.first), getHexUIntLowercase(expected_checksum.second), + getHexUIntLowercase(calculated_checksum.first), getHexUIntLowercase(calculated_checksum.second)); + } + + /// Read the parts of the header. + ReadBufferFromString header_buf(header_data); + + readVarUInt(distributed_header.revision, header_buf); + if (DBMS_TCP_PROTOCOL_VERSION < distributed_header.revision) + { + LOG_WARNING(log, "ClickHouse shard version is older than ClickHouse initiator version. It may lack support for new features."); + } + + readStringBinary(distributed_header.insert_query, header_buf); + distributed_header.insert_settings.read(header_buf); + + if (header_buf.hasPendingData()) + distributed_header.client_info.read(header_buf, distributed_header.revision); + + if (header_buf.hasPendingData()) + { + readVarUInt(distributed_header.rows, header_buf); + readVarUInt(distributed_header.bytes, header_buf); + readStringBinary(distributed_header.block_header_string, header_buf); + } + + if (header_buf.hasPendingData()) + { + NativeReader header_block_in(header_buf, distributed_header.revision); + distributed_header.block_header = header_block_in.read(); + if (!distributed_header.block_header) + throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, + "Cannot read header from the {} batch. Data was written with protocol version {}, current version: {}", + in.getFileName(), distributed_header.revision, DBMS_TCP_PROTOCOL_VERSION); + } + + if (header_buf.hasPendingData()) + { + readVarUInt(distributed_header.shard_num, header_buf); + readStringBinary(distributed_header.cluster, header_buf); + readStringBinary(distributed_header.distributed_table, header_buf); + readStringBinary(distributed_header.remote_table, header_buf); + } + + /// Add handling new data here, for example: + /// + /// if (header_buf.hasPendingData()) + /// readVarUInt(my_new_data, header_buf); + /// + /// And note that it is safe, because we have checksum and size for header. + + return distributed_header; + } + + if (query_size == DBMS_DISTRIBUTED_SIGNATURE_HEADER_OLD_FORMAT) + { + distributed_header.insert_settings.read(in, SettingsWriteFormat::BINARY); + readStringBinary(distributed_header.insert_query, in); + return distributed_header; + } + + distributed_header.insert_query.resize(query_size); + in.readStrict(distributed_header.insert_query.data(), query_size); + + return distributed_header; +} + +} diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.h b/src/Storages/Distributed/DistributedAsyncInsertHeader.h new file mode 100644 index 00000000000..2c7a6477b6c --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace DB +{ + +class ReadBufferFromFile; + +/// Header for the binary files that are stored on disk for async INSERT into Distributed. +struct DistributedAsyncInsertHeader +{ + UInt64 revision = 0; + Settings insert_settings; + std::string insert_query; + ClientInfo client_info; + + /// .bin file cannot have zero rows/bytes. + size_t rows = 0; + size_t bytes = 0; + + UInt32 shard_num = 0; + std::string cluster; + std::string distributed_table; + std::string remote_table; + + /// dumpStructure() of the header -- obsolete + std::string block_header_string; + Block block_header; +}; + +DistributedAsyncInsertHeader readDistributedAsyncInsertHeader(ReadBufferFromFile & in, Poco::Logger * log); + +} From 33b13549ad6f39fabad4474ae6055458ce8a041f Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 17:01:41 +0100 Subject: [PATCH 121/342] Separate out DirectoryMonitorSource as DistributedAsyncInsertSource Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 70 ------------------- .../DistributedAsyncInsertSource.cpp | 62 ++++++++++++++++ .../DistributedAsyncInsertSource.h | 28 ++++++++ src/Storages/StorageFile.cpp | 7 +- 4 files changed, 93 insertions(+), 74 deletions(-) create mode 100644 src/Storages/Distributed/DistributedAsyncInsertSource.cpp create mode 100644 src/Storages/Distributed/DistributedAsyncInsertSource.h diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index 469f2f90954..c51e9c6e061 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -988,76 +988,6 @@ private: } }; -class DirectoryMonitorSource : public ISource -{ -public: - - struct Data - { - std::unique_ptr in; - std::unique_ptr decompressing_in; - std::unique_ptr block_in; - - Poco::Logger * log = nullptr; - - Block first_block; - - explicit Data(const String & file_name) - { - in = std::make_unique(file_name); - decompressing_in = std::make_unique(*in); - log = &Poco::Logger::get("DirectoryMonitorSource"); - - auto distributed_header = readDistributedHeader(*in, log); - block_in = std::make_unique(*decompressing_in, distributed_header.revision); - - first_block = block_in->read(); - } - - Data(Data &&) = default; - }; - - explicit DirectoryMonitorSource(const String & file_name) - : DirectoryMonitorSource(Data(file_name)) - { - } - - explicit DirectoryMonitorSource(Data data_) - : ISource(data_.first_block.cloneEmpty()) - , data(std::move(data_)) - { - } - - String getName() const override { return "DirectoryMonitorSource"; } - -protected: - Chunk generate() override - { - if (data.first_block) - { - size_t num_rows = data.first_block.rows(); - Chunk res(data.first_block.getColumns(), num_rows); - data.first_block.clear(); - return res; - } - - auto block = data.block_in->read(); - if (!block) - return {}; - - size_t num_rows = block.rows(); - return Chunk(block.getColumns(), num_rows); - } - -private: - Data data; -}; - -std::shared_ptr StorageDistributedDirectoryMonitor::createSourceFromFile(const String & file_name) -{ - return std::make_shared(file_name); -} - bool StorageDistributedDirectoryMonitor::addFileAndSchedule(const std::string & file_path, size_t file_size, size_t ms) { /// NOTE: It is better not to throw in this case, since the file is already diff --git a/src/Storages/Distributed/DistributedAsyncInsertSource.cpp b/src/Storages/Distributed/DistributedAsyncInsertSource.cpp new file mode 100644 index 00000000000..782cbf9c026 --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertSource.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include + +namespace DB +{ + +struct DistributedAsyncInsertSource::Data +{ + Poco::Logger * log = nullptr; + + ReadBufferFromFile in; + CompressedReadBuffer decompressing_in; + NativeReader block_in; + + Block first_block; + + explicit Data(const String & file_name) + : log(&Poco::Logger::get("DistributedAsyncInsertSource")) + , in(file_name) + , decompressing_in(in) + , block_in(decompressing_in, readDistributedAsyncInsertHeader(in, log).revision) + , first_block(block_in.read()) + { + } +}; + +DistributedAsyncInsertSource::DistributedAsyncInsertSource(const String & file_name) + : DistributedAsyncInsertSource(std::make_unique(file_name)) +{ +} + +DistributedAsyncInsertSource::DistributedAsyncInsertSource(std::unique_ptr data_) + : ISource(data_->first_block.cloneEmpty()) + , data(std::move(data_)) +{ +} + +DistributedAsyncInsertSource::~DistributedAsyncInsertSource() = default; + +Chunk DistributedAsyncInsertSource::generate() +{ + if (data->first_block) + { + size_t num_rows = data->first_block.rows(); + Chunk res(data->first_block.getColumns(), num_rows); + data->first_block.clear(); + return res; + } + + auto block = data->block_in.read(); + if (!block) + return {}; + + size_t num_rows = block.rows(); + return Chunk(block.getColumns(), num_rows); +} + +} diff --git a/src/Storages/Distributed/DistributedAsyncInsertSource.h b/src/Storages/Distributed/DistributedAsyncInsertSource.h new file mode 100644 index 00000000000..35f846151dc --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertSource.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +namespace DB +{ + +/// Source for the Distributed engine on-disk file for async INSERT. +class DistributedAsyncInsertSource : public ISource +{ + struct Data; + explicit DistributedAsyncInsertSource(std::unique_ptr data); + +public: + explicit DistributedAsyncInsertSource(const String & file_name); + ~DistributedAsyncInsertSource() override; + String getName() const override { return "DistributedAsyncInsertSource"; } + +protected: + Chunk generate() override; + +private: + std::unique_ptr data; +}; + +} diff --git a/src/Storages/StorageFile.cpp b/src/Storages/StorageFile.cpp index e2a2f84bc72..5fd5664b9e6 100644 --- a/src/Storages/StorageFile.cpp +++ b/src/Storages/StorageFile.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -368,8 +368,7 @@ ColumnsDescription StorageFile::getTableStructureFromFile( if (paths.empty()) throw Exception(ErrorCodes::INCORRECT_FILE_NAME, "Cannot get table structure from file, because no files match specified name"); - auto source = StorageDistributedDirectoryMonitor::createSourceFromFile(paths[0]); - return ColumnsDescription(source->getOutputs().front().getHeader().getNamesAndTypesList()); + return ColumnsDescription(DistributedAsyncInsertSource(paths[0]).getOutputs().front().getHeader().getNamesAndTypesList()); } if (paths.empty() && !FormatFactory::instance().checkIfFormatHasExternalSchemaReader(format)) @@ -597,7 +596,7 @@ public: /// Special case for distributed format. Defaults are not needed here. if (storage->format_name == "Distributed") { - pipeline = std::make_unique(StorageDistributedDirectoryMonitor::createSourceFromFile(current_path)); + pipeline = std::make_unique(std::make_shared(current_path)); reader = std::make_unique(*pipeline); continue; } From 1c4659b8e705b9a4c46929524bd392eaa6609f1d Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 18:12:42 +0100 Subject: [PATCH 122/342] Separate out Batch as DistributedAsyncInsertBatch (and also some helpers) Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DirectoryMonitor.cpp | 598 +++--------------- src/Storages/Distributed/DirectoryMonitor.h | 8 +- .../DistributedAsyncInsertBatch.cpp | 277 ++++++++ .../Distributed/DistributedAsyncInsertBatch.h | 44 ++ .../DistributedAsyncInsertHelpers.cpp | 124 ++++ .../DistributedAsyncInsertHelpers.h | 35 + src/Storages/StorageDistributed.h | 1 + 7 files changed, 559 insertions(+), 528 deletions(-) create mode 100644 src/Storages/Distributed/DistributedAsyncInsertBatch.cpp create mode 100644 src/Storages/Distributed/DistributedAsyncInsertBatch.h create mode 100644 src/Storages/Distributed/DistributedAsyncInsertHelpers.cpp create mode 100644 src/Storages/Distributed/DistributedAsyncInsertHelpers.h diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DirectoryMonitor.cpp index c51e9c6e061..d5091003cb3 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DirectoryMonitor.cpp @@ -1,6 +1,19 @@ +#include +#include +#include +#include +#include #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -21,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -41,13 +55,6 @@ namespace DB namespace ErrorCodes { - extern const int CANNOT_READ_ALL_DATA; - extern const int UNKNOWN_CODEC; - extern const int CANNOT_DECOMPRESS; - extern const int CHECKSUM_DOESNT_MATCH; - extern const int TOO_LARGE_SIZE_COMPRESSED; - extern const int ATTEMPT_TO_READ_AFTER_EOF; - extern const int EMPTY_DATA_PASSED; extern const int INCORRECT_FILE_NAME; extern const int MEMORY_LIMIT_EXCEEDED; extern const int DISTRIBUTED_BROKEN_BATCH_INFO; @@ -64,296 +71,64 @@ namespace ErrorCodes namespace { - constexpr const std::chrono::minutes decrease_error_count_period{5}; - template - ConnectionPoolPtrs createPoolsForAddresses(const std::string & name, PoolFactory && factory, const Cluster::ShardsInfo & shards_info, Poco::Logger * log) +template +ConnectionPoolPtrs createPoolsForAddresses(const std::string & name, PoolFactory && factory, const Cluster::ShardsInfo & shards_info, Poco::Logger * log) +{ + ConnectionPoolPtrs pools; + + auto make_connection = [&](const Cluster::Address & address) { - ConnectionPoolPtrs pools; - - auto make_connection = [&](const Cluster::Address & address) + try { - try - { - pools.emplace_back(factory(address)); - } - catch (const Exception & e) - { - if (e.code() == ErrorCodes::INCORRECT_FILE_NAME) - { - tryLogCurrentException(log); - return; - } - throw; - } - }; - - for (auto it = boost::make_split_iterator(name, boost::first_finder(",")); it != decltype(it){}; ++it) - { - const std::string & dirname = boost::copy_range(*it); - Cluster::Address address = Cluster::Address::fromFullString(dirname); - if (address.shard_index && dirname.ends_with("_all_replicas")) - { - if (address.shard_index > shards_info.size()) - { - LOG_ERROR(log, "No shard with shard_index={} ({})", address.shard_index, name); - continue; - } - - const auto & shard_info = shards_info[address.shard_index - 1]; - size_t replicas = shard_info.per_replica_pools.size(); - - for (size_t replica_index = 1; replica_index <= replicas; ++replica_index) - { - address.replica_index = static_cast(replica_index); - make_connection(address); - } - } - else - make_connection(address); + pools.emplace_back(factory(address)); } - - return pools; - } - - void assertChecksum(CityHash_v1_0_2::uint128 expected, CityHash_v1_0_2::uint128 calculated) - { - if (expected != calculated) + catch (const Exception & e) { - throw Exception(ErrorCodes::CHECKSUM_DOESNT_MATCH, - "Checksum of extra info doesn't match: corrupted data. Reference: {}{}. Actual: {}{}.", - getHexUIntLowercase(expected.first), getHexUIntLowercase(expected.second), - getHexUIntLowercase(calculated.first), getHexUIntLowercase(calculated.second)); + if (e.code() == ErrorCodes::INCORRECT_FILE_NAME) + { + tryLogCurrentException(log); + return; + } + throw; } - } - - struct DistributedHeader - { - UInt64 revision = 0; - Settings insert_settings; - std::string insert_query; - ClientInfo client_info; - - /// .bin file cannot have zero rows/bytes. - size_t rows = 0; - size_t bytes = 0; - - UInt32 shard_num = 0; - std::string cluster; - std::string distributed_table; - std::string remote_table; - - /// dumpStructure() of the header -- obsolete - std::string block_header_string; - Block block_header; }; - DistributedHeader readDistributedHeader(ReadBufferFromFile & in, Poco::Logger * log) + for (auto it = boost::make_split_iterator(name, boost::first_finder(",")); it != decltype(it){}; ++it) { - DistributedHeader distributed_header; - - UInt64 query_size; - readVarUInt(query_size, in); - - if (query_size == DBMS_DISTRIBUTED_SIGNATURE_HEADER) + const std::string & dirname = boost::copy_range(*it); + Cluster::Address address = Cluster::Address::fromFullString(dirname); + if (address.shard_index && dirname.ends_with("_all_replicas")) { - /// Read the header as a string. - String header_data; - readStringBinary(header_data, in); - - /// Check the checksum of the header. - CityHash_v1_0_2::uint128 checksum; - readPODBinary(checksum, in); - assertChecksum(checksum, CityHash_v1_0_2::CityHash128(header_data.data(), header_data.size())); - - /// Read the parts of the header. - ReadBufferFromString header_buf(header_data); - - readVarUInt(distributed_header.revision, header_buf); - if (DBMS_TCP_PROTOCOL_VERSION < distributed_header.revision) + if (address.shard_index > shards_info.size()) { - LOG_WARNING(log, "ClickHouse shard version is older than ClickHouse initiator version. It may lack support for new features."); + LOG_ERROR(log, "No shard with shard_index={} ({})", address.shard_index, name); + continue; } - readStringBinary(distributed_header.insert_query, header_buf); - distributed_header.insert_settings.read(header_buf); + const auto & shard_info = shards_info[address.shard_index - 1]; + size_t replicas = shard_info.per_replica_pools.size(); - if (header_buf.hasPendingData()) - distributed_header.client_info.read(header_buf, distributed_header.revision); - - if (header_buf.hasPendingData()) + for (size_t replica_index = 1; replica_index <= replicas; ++replica_index) { - readVarUInt(distributed_header.rows, header_buf); - readVarUInt(distributed_header.bytes, header_buf); - readStringBinary(distributed_header.block_header_string, header_buf); + address.replica_index = static_cast(replica_index); + make_connection(address); } - - if (header_buf.hasPendingData()) - { - NativeReader header_block_in(header_buf, distributed_header.revision); - distributed_header.block_header = header_block_in.read(); - if (!distributed_header.block_header) - throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, - "Cannot read header from the {} batch. Data was written with protocol version {}, current version: {}", - in.getFileName(), distributed_header.revision, DBMS_TCP_PROTOCOL_VERSION); - } - - if (header_buf.hasPendingData()) - { - readVarUInt(distributed_header.shard_num, header_buf); - readStringBinary(distributed_header.cluster, header_buf); - readStringBinary(distributed_header.distributed_table, header_buf); - readStringBinary(distributed_header.remote_table, header_buf); - } - - /// Add handling new data here, for example: - /// - /// if (header_buf.hasPendingData()) - /// readVarUInt(my_new_data, header_buf); - /// - /// And note that it is safe, because we have checksum and size for header. - - return distributed_header; } - - if (query_size == DBMS_DISTRIBUTED_SIGNATURE_HEADER_OLD_FORMAT) - { - distributed_header.insert_settings.read(in, SettingsWriteFormat::BINARY); - readStringBinary(distributed_header.insert_query, in); - return distributed_header; - } - - distributed_header.insert_query.resize(query_size); - in.readStrict(distributed_header.insert_query.data(), query_size); - - return distributed_header; + else + make_connection(address); } - /// 'remote_error' argument is used to decide whether some errors should be - /// ignored or not, in particular: - /// - /// - ATTEMPT_TO_READ_AFTER_EOF should not be ignored - /// if we receive it from remote (receiver), since: - /// - the sender will got ATTEMPT_TO_READ_AFTER_EOF when the client just go away, - /// i.e. server had been restarted - /// - since #18853 the file will be checked on the sender locally, and - /// if there is something wrong with the file itself, we will receive - /// ATTEMPT_TO_READ_AFTER_EOF not from the remote at first - /// and mark batch as broken. - bool isFileBrokenErrorCode(int code, bool remote_error) - { - return code == ErrorCodes::CHECKSUM_DOESNT_MATCH - || code == ErrorCodes::EMPTY_DATA_PASSED - || code == ErrorCodes::TOO_LARGE_SIZE_COMPRESSED - || code == ErrorCodes::CANNOT_READ_ALL_DATA - || code == ErrorCodes::UNKNOWN_CODEC - || code == ErrorCodes::CANNOT_DECOMPRESS - || code == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_INFO - || code == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_FILES - || (!remote_error && code == ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF); - } + return pools; +} - /// Can the batch be split and send files from batch one-by-one instead? - bool isSplittableErrorCode(int code, bool remote) - { - return code == ErrorCodes::MEMORY_LIMIT_EXCEEDED - /// FunctionRange::max_elements and similar - || code == ErrorCodes::ARGUMENT_OUT_OF_BOUND - || code == ErrorCodes::TOO_MANY_PARTS - || code == ErrorCodes::TOO_MANY_BYTES - || code == ErrorCodes::TOO_MANY_ROWS_OR_BYTES - || code == ErrorCodes::TOO_MANY_PARTITIONS - || code == ErrorCodes::DISTRIBUTED_TOO_MANY_PENDING_BYTES - || code == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_INFO - || isFileBrokenErrorCode(code, remote) - ; - } +uint64_t doubleToUInt64(double d) +{ + if (d >= static_cast(std::numeric_limits::max())) + return std::numeric_limits::max(); + return static_cast(d); +} - SyncGuardPtr getDirectorySyncGuard(bool dir_fsync, const DiskPtr & disk, const String & path) - { - if (dir_fsync) - return disk->getDirectorySyncGuard(path); - return nullptr; - } - - void writeAndConvert(RemoteInserter & remote, const DistributedAsyncInsertHeader & distributed_header, ReadBufferFromFile & in) - { - CompressedReadBuffer decompressing_in(in); - NativeReader block_in(decompressing_in, distributed_header.revision); - - while (Block block = block_in.read()) - { - auto converting_dag = ActionsDAG::makeConvertingActions( - block.cloneEmpty().getColumnsWithTypeAndName(), - remote.getHeader().getColumnsWithTypeAndName(), - ActionsDAG::MatchColumnsMode::Name); - - auto converting_actions = std::make_shared(std::move(converting_dag)); - converting_actions->execute(block); - remote.write(block); - } - } - - void writeRemoteConvert( - const DistributedAsyncInsertHeader & distributed_header, - RemoteInserter & remote, - bool compression_expected, - ReadBufferFromFile & in, - Poco::Logger * log) - { - if (!remote.getHeader()) - { - CheckingCompressedReadBuffer checking_in(in); - remote.writePrepared(checking_in); - return; - } - - /// This is old format, that does not have header for the block in the file header, - /// applying ConvertingTransform in this case is not a big overhead. - /// - /// Anyway we can get header only from the first block, which contain all rows anyway. - if (!distributed_header.block_header) - { - LOG_TRACE(log, "Processing batch {} with old format (no header)", in.getFileName()); - - writeAndConvert(remote, distributed_header, in); - return; - } - - if (!blocksHaveEqualStructure(distributed_header.block_header, remote.getHeader())) - { - LOG_WARNING(log, - "Structure does not match (remote: {}, local: {}), implicit conversion will be done", - remote.getHeader().dumpStructure(), distributed_header.block_header.dumpStructure()); - - writeAndConvert(remote, distributed_header, in); - return; - } - - /// If connection does not use compression, we have to uncompress the data. - if (!compression_expected) - { - writeAndConvert(remote, distributed_header, in); - return; - } - - if (distributed_header.revision != remote.getServerRevision()) - { - writeAndConvert(remote, distributed_header, in); - return; - } - - /// Otherwise write data as it was already prepared (more efficient path). - CheckingCompressedReadBuffer checking_in(in); - remote.writePrepared(checking_in); - } - - uint64_t doubleToUInt64(double d) - { - if (d >= static_cast(std::numeric_limits::max())) - return std::numeric_limits::max(); - return static_cast(d); - } } @@ -425,13 +200,15 @@ void StorageDistributedDirectoryMonitor::shutdownAndDropAllData() task_handle->deactivate(); } - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); + auto dir_sync_guard = getDirectorySyncGuard(relative_path); fs::remove_all(path); } void StorageDistributedDirectoryMonitor::run() { + constexpr const std::chrono::minutes decrease_error_count_period{5}; + std::lock_guard lock{mutex}; bool do_sleep = false; @@ -695,7 +472,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa thread_trace_context->root_span.addAttribute(std::current_exception()); e.addMessage(fmt::format("While sending {}", file_path)); - if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) + if (isDistributedSendBroken(e.code(), e.isRemoteException())) { markAsBroken(file_path); current_file.clear(); @@ -710,7 +487,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa throw; } - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); + auto dir_sync_guard = getDirectorySyncGuard(relative_path); markAsSend(file_path); current_file.clear(); LOG_TRACE(log, "Finished processing `{}` (took {} ms)", file_path, watch.elapsedMilliseconds()); @@ -750,244 +527,6 @@ struct StorageDistributedDirectoryMonitor::BatchHeader }; }; -struct StorageDistributedDirectoryMonitor::Batch -{ - size_t total_rows = 0; - size_t total_bytes = 0; - /// Does the batch had been created from the files in current_batch.txt? - bool recovered = false; - - StorageDistributedDirectoryMonitor & parent; - std::vector files; - - bool split_batch_on_failure = true; - bool fsync = false; - bool dir_fsync = false; - - explicit Batch(StorageDistributedDirectoryMonitor & parent_) - : parent(parent_) - , split_batch_on_failure(parent.split_batch_on_failure) - , fsync(parent.storage.getDistributedSettingsRef().fsync_after_insert) - , dir_fsync(parent.dir_fsync) - {} - - bool isEnoughSize() const - { - return (!parent.min_batched_block_size_rows && !parent.min_batched_block_size_bytes) - || (parent.min_batched_block_size_rows && total_rows >= parent.min_batched_block_size_rows) - || (parent.min_batched_block_size_bytes && total_bytes >= parent.min_batched_block_size_bytes); - } - - void send() - { - if (files.empty()) - return; - - CurrentMetrics::Increment metric_increment{CurrentMetrics::DistributedSend}; - - Stopwatch watch; - - if (!recovered) - { - /// For deduplication in Replicated tables to work, in case of error - /// we must try to re-send exactly the same batches. - /// So we save contents of the current batch into the current_batch_file_path file - /// and truncate it afterwards if all went well. - serialize(); - } - - bool batch_broken = false; - bool batch_marked_as_broken = false; - try - { - try - { - sendBatch(); - } - catch (const Exception & e) - { - if (split_batch_on_failure && files.size() > 1 && isSplittableErrorCode(e.code(), e.isRemoteException())) - { - tryLogCurrentException(parent.log, "Trying to split batch due to"); - sendSeparateFiles(); - } - else - throw; - } - } - catch (Exception & e) - { - if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) - { - tryLogCurrentException(parent.log, "Failed to send batch due to"); - batch_broken = true; - if (!e.isRemoteException() && e.code() == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_FILES) - batch_marked_as_broken = true; - } - else - { - e.addMessage(fmt::format("While sending a batch of {} files, files: {}", files.size(), fmt::join(files, "\n"))); - throw; - } - } - - if (!batch_broken) - { - LOG_TRACE(parent.log, "Sent a batch of {} files (took {} ms).", files.size(), watch.elapsedMilliseconds()); - - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, parent.disk, parent.relative_path); - for (const auto & file : files) - parent.markAsSend(file); - } - else if (!batch_marked_as_broken) - { - LOG_ERROR(parent.log, "Marking a batch of {} files as broken, files: {}", files.size(), fmt::join(files, "\n")); - - for (const auto & file : files) - parent.markAsBroken(file); - } - - files.clear(); - total_rows = 0; - total_bytes = 0; - recovered = false; - - fs::resize_file(parent.current_batch_file_path, 0); - } - - void serialize() - { - /// Temporary file is required for atomicity. - String tmp_file{parent.current_batch_file_path + ".tmp"}; - - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, parent.disk, parent.relative_path); - if (fs::exists(tmp_file)) - LOG_ERROR(parent.log, "Temporary file {} exists. Unclean shutdown?", backQuote(tmp_file)); - - { - WriteBufferFromFile out{tmp_file, O_WRONLY | O_TRUNC | O_CREAT}; - writeText(out); - - out.finalize(); - if (fsync) - out.sync(); - } - - fs::rename(tmp_file, parent.current_batch_file_path); - } - - void deserialize() - { - ReadBufferFromFile in{parent.current_batch_file_path}; - readText(in); - } - -private: - void writeText(WriteBuffer & out) - { - for (const auto & file : files) - { - UInt64 file_index = parse(fs::path(file).stem()); - out << file_index << '\n'; - } - } - - void readText(ReadBuffer & in) - { - while (!in.eof()) - { - UInt64 idx; - in >> idx >> "\n"; - files.push_back(fs::absolute(fmt::format("{}/{}.bin", parent.path, idx)).string()); - } - - recovered = true; - } - - void sendBatch() - { - std::unique_ptr remote; - bool compression_expected = false; - - IConnectionPool::Entry connection; - - for (const auto & file : files) - { - ReadBufferFromFile in(file); - const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); - - OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, - distributed_header.client_info.client_trace_context, - parent.storage.getContext()->getOpenTelemetrySpanLog()); - - if (!remote) - { - auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); - connection = parent.pool->get(timeouts); - compression_expected = connection->getCompression() == Protocol::Compression::Enable; - - LOG_DEBUG(parent.log, "Sending a batch of {} files to {} ({} rows, {} bytes).", - files.size(), - connection->getDescription(), - formatReadableQuantity(total_rows), - formatReadableSizeWithBinarySuffix(total_bytes)); - - remote = std::make_unique(*connection, timeouts, - distributed_header.insert_query, - distributed_header.insert_settings, - distributed_header.client_info); - } - writeRemoteConvert(distributed_header, *remote, compression_expected, in, parent.log); - } - - if (remote) - remote->onFinish(); - } - - void sendSeparateFiles() - { - size_t broken_files = 0; - - for (const auto & file : files) - { - try - { - ReadBufferFromFile in(file); - const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); - - // this function is called in a separated thread, so we set up the trace context from the file - OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, - distributed_header.client_info.client_trace_context, - parent.storage.getContext()->getOpenTelemetrySpanLog()); - - auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); - auto connection = parent.pool->get(timeouts); - bool compression_expected = connection->getCompression() == Protocol::Compression::Enable; - - RemoteInserter remote(*connection, timeouts, - distributed_header.insert_query, - distributed_header.insert_settings, - distributed_header.client_info); - - writeRemoteConvert(distributed_header, remote, compression_expected, in, parent.log); - remote.onFinish(); - } - catch (Exception & e) - { - if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) - { - parent.markAsBroken(file); - ++broken_files; - } - } - } - - if (broken_files) - throw Exception(ErrorCodes::DISTRIBUTED_BROKEN_BATCH_FILES, - "Failed to send {} files", broken_files); - } -}; - bool StorageDistributedDirectoryMonitor::addFileAndSchedule(const std::string & file_path, size_t file_size, size_t ms) { /// NOTE: It is better not to throw in this case, since the file is already @@ -1024,15 +563,15 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() { LOG_DEBUG(log, "Restoring the batch"); - Batch batch(*this); + DistributedAsyncInsertBatch batch(*this); batch.deserialize(); batch.send(); - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); + auto dir_sync_guard = getDirectorySyncGuard(relative_path); fs::remove(current_batch_file_path); } - std::unordered_map header_to_batch; + std::unordered_map header_to_batch; std::string file_path; while (pending_files.tryPop(file_path)) @@ -1075,7 +614,7 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() } catch (const Exception & e) { - if (isFileBrokenErrorCode(e.code(), e.isRemoteException())) + if (isDistributedSendBroken(e.code(), e.isRemoteException())) { markAsBroken(file_path); tryLogCurrentException(log, "File is marked broken due to"); @@ -1091,7 +630,7 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() std::move(distributed_header.client_info), std::move(header) ); - Batch & batch = header_to_batch.try_emplace(batch_header, *this).first->second; + DistributedAsyncInsertBatch & batch = header_to_batch.try_emplace(batch_header, *this).first->second; batch.files.push_back(file_path); batch.total_rows += total_rows; @@ -1105,12 +644,12 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() for (auto & kv : header_to_batch) { - Batch & batch = kv.second; + DistributedAsyncInsertBatch & batch = kv.second; batch.send(); } { - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); + auto dir_sync_guard = getDirectorySyncGuard(relative_path); /// current_batch.txt will not exist if there was no send /// (this is the case when all batches that was pending has been marked as pending) @@ -1123,8 +662,8 @@ void StorageDistributedDirectoryMonitor::markAsBroken(const std::string & file_p { const String & broken_file_path = fs::path(broken_path) / fs::path(file_path).filename(); - auto dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, relative_path); - auto broken_dir_sync_guard = getDirectorySyncGuard(dir_fsync, disk, broken_relative_path); + auto dir_sync_guard = getDirectorySyncGuard(relative_path); + auto broken_dir_sync_guard = getDirectorySyncGuard(broken_relative_path); { std::lock_guard status_lock(status_mutex); @@ -1158,6 +697,13 @@ void StorageDistributedDirectoryMonitor::markAsSend(const std::string & file_pat fs::remove(file_path); } +SyncGuardPtr StorageDistributedDirectoryMonitor::getDirectorySyncGuard(const std::string & dir_path) +{ + if (dir_fsync) + return disk->getDirectorySyncGuard(dir_path); + return nullptr; +} + std::string StorageDistributedDirectoryMonitor::getLoggerName() const { return storage.getStorageID().getFullTableName() + ".DirectoryMonitor." + disk->getName(); diff --git a/src/Storages/Distributed/DirectoryMonitor.h b/src/Storages/Distributed/DirectoryMonitor.h index 9b1596d45e3..8515f5a16a1 100644 --- a/src/Storages/Distributed/DirectoryMonitor.h +++ b/src/Storages/Distributed/DirectoryMonitor.h @@ -3,11 +3,11 @@ #include #include #include - +#include +#include #include #include #include -#include namespace CurrentMetrics { class Increment; } @@ -32,6 +32,8 @@ class ISource; */ class StorageDistributedDirectoryMonitor { + friend class DistributedAsyncInsertBatch; + public: StorageDistributedDirectoryMonitor( StorageDistributed & storage_, @@ -92,6 +94,8 @@ private: void markAsBroken(const std::string & file_path); void markAsSend(const std::string & file_path); + SyncGuardPtr getDirectorySyncGuard(const std::string & path); + std::string getLoggerName() const; StorageDistributed & storage; diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp new file mode 100644 index 00000000000..5f9e78a72e2 --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp @@ -0,0 +1,277 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + +namespace fs = std::filesystem; + +} + +namespace CurrentMetrics +{ + extern const Metric DistributedSend; +} + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int MEMORY_LIMIT_EXCEEDED; + extern const int DISTRIBUTED_BROKEN_BATCH_INFO; + extern const int DISTRIBUTED_BROKEN_BATCH_FILES; + extern const int TOO_MANY_PARTS; + extern const int TOO_MANY_BYTES; + extern const int TOO_MANY_ROWS_OR_BYTES; + extern const int TOO_MANY_PARTITIONS; + extern const int DISTRIBUTED_TOO_MANY_PENDING_BYTES; + extern const int ARGUMENT_OUT_OF_BOUND; +} + +/// Can the batch be split and send files from batch one-by-one instead? +bool isSplittableErrorCode(int code, bool remote) +{ + return code == ErrorCodes::MEMORY_LIMIT_EXCEEDED + /// FunctionRange::max_elements and similar + || code == ErrorCodes::ARGUMENT_OUT_OF_BOUND + || code == ErrorCodes::TOO_MANY_PARTS + || code == ErrorCodes::TOO_MANY_BYTES + || code == ErrorCodes::TOO_MANY_ROWS_OR_BYTES + || code == ErrorCodes::TOO_MANY_PARTITIONS + || code == ErrorCodes::DISTRIBUTED_TOO_MANY_PENDING_BYTES + || code == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_INFO + || isDistributedSendBroken(code, remote) + ; +} + +DistributedAsyncInsertBatch::DistributedAsyncInsertBatch(StorageDistributedDirectoryMonitor & parent_) + : parent(parent_) + , split_batch_on_failure(parent.split_batch_on_failure) + , fsync(parent.storage.getDistributedSettingsRef().fsync_after_insert) + , dir_fsync(parent.dir_fsync) +{} + +bool DistributedAsyncInsertBatch::isEnoughSize() const +{ + return (!parent.min_batched_block_size_rows && !parent.min_batched_block_size_bytes) + || (parent.min_batched_block_size_rows && total_rows >= parent.min_batched_block_size_rows) + || (parent.min_batched_block_size_bytes && total_bytes >= parent.min_batched_block_size_bytes); +} + +void DistributedAsyncInsertBatch::send() +{ + if (files.empty()) + return; + + CurrentMetrics::Increment metric_increment{CurrentMetrics::DistributedSend}; + + Stopwatch watch; + + if (!recovered) + { + /// For deduplication in Replicated tables to work, in case of error + /// we must try to re-send exactly the same batches. + /// So we save contents of the current batch into the current_batch_file_path file + /// and truncate it afterwards if all went well. + serialize(); + } + + bool batch_broken = false; + bool batch_marked_as_broken = false; + try + { + try + { + sendBatch(); + } + catch (const Exception & e) + { + if (split_batch_on_failure && files.size() > 1 && isSplittableErrorCode(e.code(), e.isRemoteException())) + { + tryLogCurrentException(parent.log, "Trying to split batch due to"); + sendSeparateFiles(); + } + else + throw; + } + } + catch (Exception & e) + { + if (isDistributedSendBroken(e.code(), e.isRemoteException())) + { + tryLogCurrentException(parent.log, "Failed to send batch due to"); + batch_broken = true; + if (!e.isRemoteException() && e.code() == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_FILES) + batch_marked_as_broken = true; + } + else + { + e.addMessage(fmt::format("While sending a batch of {} files, files: {}", files.size(), fmt::join(files, "\n"))); + throw; + } + } + + if (!batch_broken) + { + LOG_TRACE(parent.log, "Sent a batch of {} files (took {} ms).", files.size(), watch.elapsedMilliseconds()); + + auto dir_sync_guard = parent.getDirectorySyncGuard(parent.relative_path); + for (const auto & file : files) + parent.markAsSend(file); + } + else if (!batch_marked_as_broken) + { + LOG_ERROR(parent.log, "Marking a batch of {} files as broken, files: {}", files.size(), fmt::join(files, "\n")); + + for (const auto & file : files) + parent.markAsBroken(file); + } + + files.clear(); + total_rows = 0; + total_bytes = 0; + recovered = false; + + fs::resize_file(parent.current_batch_file_path, 0); +} + +void DistributedAsyncInsertBatch::serialize() +{ + /// Temporary file is required for atomicity. + String tmp_file{parent.current_batch_file_path + ".tmp"}; + + auto dir_sync_guard = parent.getDirectorySyncGuard(parent.relative_path); + if (fs::exists(tmp_file)) + LOG_ERROR(parent.log, "Temporary file {} exists. Unclean shutdown?", backQuote(tmp_file)); + + { + WriteBufferFromFile out{tmp_file, O_WRONLY | O_TRUNC | O_CREAT}; + writeText(out); + + out.finalize(); + if (fsync) + out.sync(); + } + + fs::rename(tmp_file, parent.current_batch_file_path); +} + +void DistributedAsyncInsertBatch::deserialize() +{ + ReadBufferFromFile in{parent.current_batch_file_path}; + readText(in); +} + +void DistributedAsyncInsertBatch::writeText(WriteBuffer & out) +{ + for (const auto & file : files) + { + UInt64 file_index = parse(fs::path(file).stem()); + out << file_index << '\n'; + } +} + +void DistributedAsyncInsertBatch::readText(ReadBuffer & in) +{ + while (!in.eof()) + { + UInt64 idx; + in >> idx >> "\n"; + files.push_back(fs::absolute(fmt::format("{}/{}.bin", parent.path, idx)).string()); + } + + recovered = true; +} + +void DistributedAsyncInsertBatch::sendBatch() +{ + std::unique_ptr remote; + bool compression_expected = false; + + IConnectionPool::Entry connection; + + for (const auto & file : files) + { + ReadBufferFromFile in(file); + const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); + + OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, + distributed_header.client_info.client_trace_context, + parent.storage.getContext()->getOpenTelemetrySpanLog()); + + if (!remote) + { + auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); + connection = parent.pool->get(timeouts); + compression_expected = connection->getCompression() == Protocol::Compression::Enable; + + LOG_DEBUG(parent.log, "Sending a batch of {} files to {} ({} rows, {} bytes).", + files.size(), + connection->getDescription(), + formatReadableQuantity(total_rows), + formatReadableSizeWithBinarySuffix(total_bytes)); + + remote = std::make_unique(*connection, timeouts, + distributed_header.insert_query, + distributed_header.insert_settings, + distributed_header.client_info); + } + writeRemoteConvert(distributed_header, *remote, compression_expected, in, parent.log); + } + + if (remote) + remote->onFinish(); +} + +void DistributedAsyncInsertBatch::sendSeparateFiles() +{ + size_t broken_files = 0; + + for (const auto & file : files) + { + try + { + ReadBufferFromFile in(file); + const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); + + // This function is called in a separated thread, so we set up the trace context from the file + OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, + distributed_header.client_info.client_trace_context, + parent.storage.getContext()->getOpenTelemetrySpanLog()); + + auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); + auto connection = parent.pool->get(timeouts); + bool compression_expected = connection->getCompression() == Protocol::Compression::Enable; + + RemoteInserter remote(*connection, timeouts, + distributed_header.insert_query, + distributed_header.insert_settings, + distributed_header.client_info); + + writeRemoteConvert(distributed_header, remote, compression_expected, in, parent.log); + remote.onFinish(); + } + catch (Exception & e) + { + if (isDistributedSendBroken(e.code(), e.isRemoteException())) + { + parent.markAsBroken(file); + ++broken_files; + } + } + } + + if (broken_files) + throw Exception(ErrorCodes::DISTRIBUTED_BROKEN_BATCH_FILES, + "Failed to send {} files", broken_files); +} + +} diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.h b/src/Storages/Distributed/DistributedAsyncInsertBatch.h new file mode 100644 index 00000000000..8b3320155fa --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +namespace DB +{ + +class StorageDistributedDirectoryMonitor; +class WriteBuffer; +class ReadBuffer; + +class DistributedAsyncInsertBatch +{ +public: + explicit DistributedAsyncInsertBatch(StorageDistributedDirectoryMonitor & parent_); + + bool isEnoughSize() const; + void send(); + + void serialize(); + void deserialize(); + + size_t total_rows = 0; + size_t total_bytes = 0; + std::vector files; + +private: + void writeText(WriteBuffer & out); + void readText(ReadBuffer & in); + void sendBatch(); + void sendSeparateFiles(); + + StorageDistributedDirectoryMonitor & parent; + + /// Does the batch had been created from the files in current_batch.txt? + bool recovered = false; + + bool split_batch_on_failure = true; + bool fsync = false; + bool dir_fsync = false; +}; + +} diff --git a/src/Storages/Distributed/DistributedAsyncInsertHelpers.cpp b/src/Storages/Distributed/DistributedAsyncInsertHelpers.cpp new file mode 100644 index 00000000000..98073ba1e08 --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertHelpers.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int CANNOT_READ_ALL_DATA; + extern const int UNKNOWN_CODEC; + extern const int CANNOT_DECOMPRESS; + extern const int CHECKSUM_DOESNT_MATCH; + extern const int TOO_LARGE_SIZE_COMPRESSED; + extern const int ATTEMPT_TO_READ_AFTER_EOF; + extern const int EMPTY_DATA_PASSED; + extern const int DISTRIBUTED_BROKEN_BATCH_INFO; + extern const int DISTRIBUTED_BROKEN_BATCH_FILES; +} + +/// 'remote_error' argument is used to decide whether some errors should be +/// ignored or not, in particular: +/// +/// - ATTEMPT_TO_READ_AFTER_EOF should not be ignored +/// if we receive it from remote (receiver), since: +/// - the sender will got ATTEMPT_TO_READ_AFTER_EOF when the client just go away, +/// i.e. server had been restarted +/// - since #18853 the file will be checked on the sender locally, and +/// if there is something wrong with the file itself, we will receive +/// ATTEMPT_TO_READ_AFTER_EOF not from the remote at first +/// and mark batch as broken. +bool isDistributedSendBroken(int code, bool remote_error) +{ + return code == ErrorCodes::CHECKSUM_DOESNT_MATCH + || code == ErrorCodes::EMPTY_DATA_PASSED + || code == ErrorCodes::TOO_LARGE_SIZE_COMPRESSED + || code == ErrorCodes::CANNOT_READ_ALL_DATA + || code == ErrorCodes::UNKNOWN_CODEC + || code == ErrorCodes::CANNOT_DECOMPRESS + || code == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_INFO + || code == ErrorCodes::DISTRIBUTED_BROKEN_BATCH_FILES + || (!remote_error && code == ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF); +} + +void writeAndConvert(RemoteInserter & remote, const DistributedAsyncInsertHeader & distributed_header, ReadBufferFromFile & in) +{ + CompressedReadBuffer decompressing_in(in); + NativeReader block_in(decompressing_in, distributed_header.revision); + + while (Block block = block_in.read()) + { + auto converting_dag = ActionsDAG::makeConvertingActions( + block.cloneEmpty().getColumnsWithTypeAndName(), + remote.getHeader().getColumnsWithTypeAndName(), + ActionsDAG::MatchColumnsMode::Name); + + auto converting_actions = std::make_shared(std::move(converting_dag)); + converting_actions->execute(block); + remote.write(block); + } +} + +void writeRemoteConvert( + const DistributedAsyncInsertHeader & distributed_header, + RemoteInserter & remote, + bool compression_expected, + ReadBufferFromFile & in, + Poco::Logger * log) +{ + if (!remote.getHeader()) + { + CheckingCompressedReadBuffer checking_in(in); + remote.writePrepared(checking_in); + return; + } + + /// This is old format, that does not have header for the block in the file header, + /// applying ConvertingTransform in this case is not a big overhead. + /// + /// Anyway we can get header only from the first block, which contain all rows anyway. + if (!distributed_header.block_header) + { + LOG_TRACE(log, "Processing batch {} with old format (no header)", in.getFileName()); + + writeAndConvert(remote, distributed_header, in); + return; + } + + if (!blocksHaveEqualStructure(distributed_header.block_header, remote.getHeader())) + { + LOG_WARNING(log, + "Structure does not match (remote: {}, local: {}), implicit conversion will be done", + remote.getHeader().dumpStructure(), distributed_header.block_header.dumpStructure()); + + writeAndConvert(remote, distributed_header, in); + return; + } + + /// If connection does not use compression, we have to uncompress the data. + if (!compression_expected) + { + writeAndConvert(remote, distributed_header, in); + return; + } + + if (distributed_header.revision != remote.getServerRevision()) + { + writeAndConvert(remote, distributed_header, in); + return; + } + + /// Otherwise write data as it was already prepared (more efficient path). + CheckingCompressedReadBuffer checking_in(in); + remote.writePrepared(checking_in); +} + +} diff --git a/src/Storages/Distributed/DistributedAsyncInsertHelpers.h b/src/Storages/Distributed/DistributedAsyncInsertHelpers.h new file mode 100644 index 00000000000..9543450418c --- /dev/null +++ b/src/Storages/Distributed/DistributedAsyncInsertHelpers.h @@ -0,0 +1,35 @@ +#pragma once + +namespace Poco +{ +class Logger; +} + +namespace DB +{ + +struct DistributedAsyncInsertHeader; +class ReadBufferFromFile; +class RemoteInserter; + +/// 'remote_error' argument is used to decide whether some errors should be +/// ignored or not, in particular: +/// +/// - ATTEMPT_TO_READ_AFTER_EOF should not be ignored +/// if we receive it from remote (receiver), since: +/// - the sender will got ATTEMPT_TO_READ_AFTER_EOF when the client just go away, +/// i.e. server had been restarted +/// - since #18853 the file will be checked on the sender locally, and +/// if there is something wrong with the file itself, we will receive +/// ATTEMPT_TO_READ_AFTER_EOF not from the remote at first +/// and mark batch as broken. +bool isDistributedSendBroken(int code, bool remote_error); + +void writeRemoteConvert( + const DistributedAsyncInsertHeader & distributed_header, + RemoteInserter & remote, + bool compression_expected, + ReadBufferFromFile & in, + Poco::Logger * log); + +} diff --git a/src/Storages/StorageDistributed.h b/src/Storages/StorageDistributed.h index 25a752fe795..432aee047e8 100644 --- a/src/Storages/StorageDistributed.h +++ b/src/Storages/StorageDistributed.h @@ -38,6 +38,7 @@ using ExpressionActionsPtr = std::shared_ptr; class StorageDistributed final : public IStorage, WithContext { friend class DistributedSink; + friend class DistributedAsyncInsertBatch; friend class StorageDistributedDirectoryMonitor; friend class StorageSystemDistributionQueue; From b5434eac3b189a6c588dbc17fba43e180809ed07 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 18:55:14 +0100 Subject: [PATCH 123/342] Rename StorageDistributedDirectoryMonitor to DistributedAsyncInsertDirectoryQueue Since #44922 it is not a directory monitor anymore. v2: Remove unused error codes v3: Contains some header fixes due to conflicts with master Signed-off-by: Azat Khuzhin --- src/Loggers/OwnSplitChannel.cpp | 2 +- .../DistributedAsyncInsertBatch.cpp | 4 +- .../Distributed/DistributedAsyncInsertBatch.h | 6 +- ... DistributedAsyncInsertDirectoryQueue.cpp} | 66 +++++++------------ ...=> DistributedAsyncInsertDirectoryQueue.h} | 25 +++++-- src/Storages/Distributed/DistributedSink.cpp | 4 +- src/Storages/StorageDistributed.cpp | 22 +++---- src/Storages/StorageDistributed.h | 14 ++-- .../System/StorageSystemDistributionQueue.cpp | 3 +- src/TableFunctions/ITableFunctionFileLike.cpp | 1 - ...etry_insert_on_distributed_table.reference | 4 +- 11 files changed, 71 insertions(+), 80 deletions(-) rename src/Storages/Distributed/{DirectoryMonitor.cpp => DistributedAsyncInsertDirectoryQueue.cpp} (90%) rename src/Storages/Distributed/{DirectoryMonitor.h => DistributedAsyncInsertDirectoryQueue.h} (81%) diff --git a/src/Loggers/OwnSplitChannel.cpp b/src/Loggers/OwnSplitChannel.cpp index 7974d5212e1..7a7456823ed 100644 --- a/src/Loggers/OwnSplitChannel.cpp +++ b/src/Loggers/OwnSplitChannel.cpp @@ -56,7 +56,7 @@ void OwnSplitChannel::tryLogSplit(const Poco::Message & msg) /// breaking some functionality because of unexpected "File not /// found" (or similar) error. /// - /// For example StorageDistributedDirectoryMonitor will mark batch + /// For example DistributedAsyncInsertDirectoryQueue will mark batch /// as broken, some MergeTree code can also be affected. /// /// Also note, that we cannot log the exception here, since this diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp index 5f9e78a72e2..80350600caa 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -52,7 +52,7 @@ bool isSplittableErrorCode(int code, bool remote) ; } -DistributedAsyncInsertBatch::DistributedAsyncInsertBatch(StorageDistributedDirectoryMonitor & parent_) +DistributedAsyncInsertBatch::DistributedAsyncInsertBatch(DistributedAsyncInsertDirectoryQueue & parent_) : parent(parent_) , split_batch_on_failure(parent.split_batch_on_failure) , fsync(parent.storage.getDistributedSettingsRef().fsync_after_insert) diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.h b/src/Storages/Distributed/DistributedAsyncInsertBatch.h index 8b3320155fa..867a0de89fa 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertBatch.h +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.h @@ -6,14 +6,14 @@ namespace DB { -class StorageDistributedDirectoryMonitor; +class DistributedAsyncInsertDirectoryQueue; class WriteBuffer; class ReadBuffer; class DistributedAsyncInsertBatch { public: - explicit DistributedAsyncInsertBatch(StorageDistributedDirectoryMonitor & parent_); + explicit DistributedAsyncInsertBatch(DistributedAsyncInsertDirectoryQueue & parent_); bool isEnoughSize() const; void send(); @@ -31,7 +31,7 @@ private: void sendBatch(); void sendSeparateFiles(); - StorageDistributedDirectoryMonitor & parent; + DistributedAsyncInsertDirectoryQueue & parent; /// Does the batch had been created from the files in current_batch.txt? bool recovered = false; diff --git a/src/Storages/Distributed/DirectoryMonitor.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp similarity index 90% rename from src/Storages/Distributed/DirectoryMonitor.cpp rename to src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index d5091003cb3..2d25c1822d8 100644 --- a/src/Storages/Distributed/DirectoryMonitor.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -22,19 +21,9 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include #include #include #include @@ -56,15 +45,6 @@ namespace DB namespace ErrorCodes { extern const int INCORRECT_FILE_NAME; - extern const int MEMORY_LIMIT_EXCEEDED; - extern const int DISTRIBUTED_BROKEN_BATCH_INFO; - extern const int DISTRIBUTED_BROKEN_BATCH_FILES; - extern const int TOO_MANY_PARTS; - extern const int TOO_MANY_BYTES; - extern const int TOO_MANY_ROWS_OR_BYTES; - extern const int TOO_MANY_PARTITIONS; - extern const int DISTRIBUTED_TOO_MANY_PENDING_BYTES; - extern const int ARGUMENT_OUT_OF_BOUND; extern const int LOGICAL_ERROR; } @@ -132,7 +112,7 @@ uint64_t doubleToUInt64(double d) } -StorageDistributedDirectoryMonitor::StorageDistributedDirectoryMonitor( +DistributedAsyncInsertDirectoryQueue::DistributedAsyncInsertDirectoryQueue( StorageDistributed & storage_, const DiskPtr & disk_, const std::string & relative_path_, @@ -172,7 +152,7 @@ StorageDistributedDirectoryMonitor::StorageDistributedDirectoryMonitor( } -StorageDistributedDirectoryMonitor::~StorageDistributedDirectoryMonitor() +DistributedAsyncInsertDirectoryQueue::~DistributedAsyncInsertDirectoryQueue() { if (!pending_files.isFinished()) { @@ -181,7 +161,7 @@ StorageDistributedDirectoryMonitor::~StorageDistributedDirectoryMonitor() } } -void StorageDistributedDirectoryMonitor::flushAllData() +void DistributedAsyncInsertDirectoryQueue::flushAllData() { if (pending_files.isFinished()) return; @@ -192,7 +172,7 @@ void StorageDistributedDirectoryMonitor::flushAllData() processFiles(); } -void StorageDistributedDirectoryMonitor::shutdownAndDropAllData() +void DistributedAsyncInsertDirectoryQueue::shutdownAndDropAllData() { if (!pending_files.isFinished()) { @@ -205,7 +185,7 @@ void StorageDistributedDirectoryMonitor::shutdownAndDropAllData() } -void StorageDistributedDirectoryMonitor::run() +void DistributedAsyncInsertDirectoryQueue::run() { constexpr const std::chrono::minutes decrease_error_count_period{5}; @@ -271,7 +251,7 @@ void StorageDistributedDirectoryMonitor::run() } -ConnectionPoolPtr StorageDistributedDirectoryMonitor::createPool(const std::string & name, const StorageDistributed & storage) +ConnectionPoolPtr DistributedAsyncInsertDirectoryQueue::createPool(const std::string & name, const StorageDistributed & storage) { const auto pool_factory = [&storage, &name] (const Cluster::Address & address) -> ConnectionPoolPtr { @@ -344,18 +324,18 @@ ConnectionPoolPtr StorageDistributedDirectoryMonitor::createPool(const std::stri settings.distributed_replica_error_cap); } -bool StorageDistributedDirectoryMonitor::hasPendingFiles() const +bool DistributedAsyncInsertDirectoryQueue::hasPendingFiles() const { return fs::exists(current_batch_file_path) || !current_file.empty() || !pending_files.empty(); } -void StorageDistributedDirectoryMonitor::addFile(const std::string & file_path) +void DistributedAsyncInsertDirectoryQueue::addFile(const std::string & file_path) { if (!pending_files.push(fs::absolute(file_path).string())) throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot schedule a file '{}'", file_path); } -void StorageDistributedDirectoryMonitor::initializeFilesFromDisk() +void DistributedAsyncInsertDirectoryQueue::initializeFilesFromDisk() { /// NOTE: This method does not requires to hold status_mutex, hence, no TSA /// annotations in the header file. @@ -413,7 +393,7 @@ void StorageDistributedDirectoryMonitor::initializeFilesFromDisk() status.broken_bytes_count = broken_bytes_count; } } -void StorageDistributedDirectoryMonitor::processFiles() +void DistributedAsyncInsertDirectoryQueue::processFiles() { if (should_batch_inserts) processFilesWithBatching(); @@ -428,7 +408,7 @@ void StorageDistributedDirectoryMonitor::processFiles() } } -void StorageDistributedDirectoryMonitor::processFile(const std::string & file_path) +void DistributedAsyncInsertDirectoryQueue::processFile(const std::string & file_path) { OpenTelemetry::TracingContextHolderPtr thread_trace_context; @@ -493,7 +473,7 @@ void StorageDistributedDirectoryMonitor::processFile(const std::string & file_pa LOG_TRACE(log, "Finished processing `{}` (took {} ms)", file_path, watch.elapsedMilliseconds()); } -struct StorageDistributedDirectoryMonitor::BatchHeader +struct DistributedAsyncInsertDirectoryQueue::BatchHeader { Settings settings; String query; @@ -527,7 +507,7 @@ struct StorageDistributedDirectoryMonitor::BatchHeader }; }; -bool StorageDistributedDirectoryMonitor::addFileAndSchedule(const std::string & file_path, size_t file_size, size_t ms) +bool DistributedAsyncInsertDirectoryQueue::addFileAndSchedule(const std::string & file_path, size_t file_size, size_t ms) { /// NOTE: It is better not to throw in this case, since the file is already /// on disk (see DistributedSink), and it will be processed next time. @@ -549,14 +529,14 @@ bool StorageDistributedDirectoryMonitor::addFileAndSchedule(const std::string & return task_handle->scheduleAfter(ms, false); } -StorageDistributedDirectoryMonitor::Status StorageDistributedDirectoryMonitor::getStatus() +DistributedAsyncInsertDirectoryQueue::Status DistributedAsyncInsertDirectoryQueue::getStatus() { std::lock_guard status_lock(status_mutex); Status current_status{status, path, monitor_blocker.isCancelled()}; return current_status; } -void StorageDistributedDirectoryMonitor::processFilesWithBatching() +void DistributedAsyncInsertDirectoryQueue::processFilesWithBatching() { /// Possibly, we failed to send a batch on the previous iteration. Try to send exactly the same batch. if (fs::exists(current_batch_file_path)) @@ -658,7 +638,7 @@ void StorageDistributedDirectoryMonitor::processFilesWithBatching() } } -void StorageDistributedDirectoryMonitor::markAsBroken(const std::string & file_path) +void DistributedAsyncInsertDirectoryQueue::markAsBroken(const std::string & file_path) { const String & broken_file_path = fs::path(broken_path) / fs::path(file_path).filename(); @@ -683,7 +663,7 @@ void StorageDistributedDirectoryMonitor::markAsBroken(const std::string & file_p LOG_ERROR(log, "Renamed `{}` to `{}`", file_path, broken_file_path); } -void StorageDistributedDirectoryMonitor::markAsSend(const std::string & file_path) +void DistributedAsyncInsertDirectoryQueue::markAsSend(const std::string & file_path) { size_t file_size = fs::file_size(file_path); @@ -697,19 +677,19 @@ void StorageDistributedDirectoryMonitor::markAsSend(const std::string & file_pat fs::remove(file_path); } -SyncGuardPtr StorageDistributedDirectoryMonitor::getDirectorySyncGuard(const std::string & dir_path) +SyncGuardPtr DistributedAsyncInsertDirectoryQueue::getDirectorySyncGuard(const std::string & dir_path) { if (dir_fsync) return disk->getDirectorySyncGuard(dir_path); return nullptr; } -std::string StorageDistributedDirectoryMonitor::getLoggerName() const +std::string DistributedAsyncInsertDirectoryQueue::getLoggerName() const { return storage.getStorageID().getFullTableName() + ".DirectoryMonitor." + disk->getName(); } -void StorageDistributedDirectoryMonitor::updatePath(const std::string & new_relative_path) +void DistributedAsyncInsertDirectoryQueue::updatePath(const std::string & new_relative_path) { task_handle->deactivate(); std::lock_guard lock{mutex}; diff --git a/src/Storages/Distributed/DirectoryMonitor.h b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h similarity index 81% rename from src/Storages/Distributed/DirectoryMonitor.h rename to src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h index 8515f5a16a1..90e3d563b4b 100644 --- a/src/Storages/Distributed/DirectoryMonitor.h +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h @@ -27,15 +27,28 @@ using ProcessorPtr = std::shared_ptr; class ISource; -/** Details of StorageDistributed. - * This type is not designed for standalone use. - */ -class StorageDistributedDirectoryMonitor +/** Queue for async INSERT Into Distributed engine (insert_distributed_sync=0). + * + * Files are added from two places: + * - from filesystem at startup (StorageDistributed::startup()) + * - on INSERT via DistributedSink + * + * Later, in background, those files will be send to the remote nodes. + * + * The behaviour of this queue can be configured via the following settings: + * - distributed_directory_monitor_batch_inserts + * - distributed_directory_monitor_split_batch_on_failure + * - distributed_directory_monitor_sleep_time_ms + * - distributed_directory_monitor_max_sleep_time_ms + * NOTE: It worth to rename the settings too + * ("directory_monitor" in settings looks too internal). + */ +class DistributedAsyncInsertDirectoryQueue { friend class DistributedAsyncInsertBatch; public: - StorageDistributedDirectoryMonitor( + DistributedAsyncInsertDirectoryQueue( StorageDistributed & storage_, const DiskPtr & disk_, const std::string & relative_path_, @@ -44,7 +57,7 @@ public: BackgroundSchedulePool & bg_pool, bool initialize_from_disk); - ~StorageDistributedDirectoryMonitor(); + ~DistributedAsyncInsertDirectoryQueue(); static ConnectionPoolPtr createPool(const std::string & name, const StorageDistributed & storage); diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index c9c235596db..38aa26fbe0c 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -834,7 +834,7 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const const auto & dir_name = dir_names[i]; const auto & bin_file = bin_files[i]; - auto & directory_monitor = storage.requireDirectoryMonitor(disk, dir_name, /* startup= */ false); + auto & directory_monitor = storage.getDirectoryQueue(disk, dir_name, /* startup= */ false); directory_monitor.addFileAndSchedule(bin_file, file_size, sleep_ms.totalMilliseconds()); } } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index a38beef983c..d95c703216e 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -1094,7 +1094,7 @@ void StorageDistributed::initializeFromDisk() { pool.scheduleOrThrowOnError([&]() { - createDirectoryMonitors(disk); + initializeDirectoryQueuesForDisk(disk); }); } pool.wait(); @@ -1133,7 +1133,7 @@ void StorageDistributed::shutdown() void StorageDistributed::drop() { // Some INSERT in-between shutdown() and drop() can call - // requireDirectoryMonitor() again, so call shutdown() to clear them, but + // getDirectoryQueue() again, so call shutdown() to clear them, but // when the drop() (this function) executed none of INSERT is allowed in // parallel. // @@ -1196,7 +1196,7 @@ StoragePolicyPtr StorageDistributed::getStoragePolicy() const return storage_policy; } -void StorageDistributed::createDirectoryMonitors(const DiskPtr & disk) +void StorageDistributed::initializeDirectoryQueuesForDisk(const DiskPtr & disk) { const std::string path(disk->getPath() + relative_data_path); fs::create_directories(path); @@ -1225,14 +1225,14 @@ void StorageDistributed::createDirectoryMonitors(const DiskPtr & disk) } else { - requireDirectoryMonitor(disk, dir_path.filename().string(), /* startup= */ true); + getDirectoryQueue(disk, dir_path.filename().string(), /* startup= */ true); } } } } -StorageDistributedDirectoryMonitor& StorageDistributed::requireDirectoryMonitor(const DiskPtr & disk, const std::string & name, bool startup) +DistributedAsyncInsertDirectoryQueue & StorageDistributed::getDirectoryQueue(const DiskPtr & disk, const std::string & name, bool startup) { const std::string & disk_path = disk->getPath(); const std::string key(disk_path + name); @@ -1241,8 +1241,8 @@ StorageDistributedDirectoryMonitor& StorageDistributed::requireDirectoryMonitor( auto & node_data = cluster_nodes_data[key]; if (!node_data.directory_monitor) { - node_data.connection_pool = StorageDistributedDirectoryMonitor::createPool(name, *this); - node_data.directory_monitor = std::make_unique( + node_data.connection_pool = DistributedAsyncInsertDirectoryQueue::createPool(name, *this); + node_data.directory_monitor = std::make_unique( *this, disk, relative_data_path + name, node_data.connection_pool, monitors_blocker, @@ -1252,9 +1252,9 @@ StorageDistributedDirectoryMonitor& StorageDistributed::requireDirectoryMonitor( return *node_data.directory_monitor; } -std::vector StorageDistributed::getDirectoryMonitorsStatuses() const +std::vector StorageDistributed::getDirectoryQueueStatuses() const { - std::vector statuses; + std::vector statuses; std::lock_guard lock(cluster_nodes_mutex); statuses.reserve(cluster_nodes_data.size()); for (const auto & node : cluster_nodes_data) @@ -1265,7 +1265,7 @@ std::vector StorageDistributed::getD std::optional StorageDistributed::totalBytes(const Settings &) const { UInt64 total_bytes = 0; - for (const auto & status : getDirectoryMonitorsStatuses()) + for (const auto & status : getDirectoryQueueStatuses()) total_bytes += status.bytes_count; return total_bytes; } @@ -1426,7 +1426,7 @@ void StorageDistributed::flushClusterNodesAllData(ContextPtr local_context) /// Sync SYSTEM FLUSH DISTRIBUTED with TRUNCATE auto table_lock = lockForShare(local_context->getCurrentQueryId(), local_context->getSettingsRef().lock_acquire_timeout); - std::vector> directory_monitors; + std::vector> directory_monitors; { std::lock_guard lock(cluster_nodes_mutex); diff --git a/src/Storages/StorageDistributed.h b/src/Storages/StorageDistributed.h index 432aee047e8..df4c16f0f67 100644 --- a/src/Storages/StorageDistributed.h +++ b/src/Storages/StorageDistributed.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include @@ -39,7 +39,7 @@ class StorageDistributed final : public IStorage, WithContext { friend class DistributedSink; friend class DistributedAsyncInsertBatch; - friend class StorageDistributedDirectoryMonitor; + friend class DistributedAsyncInsertDirectoryQueue; friend class StorageSystemDistributionQueue; public: @@ -165,15 +165,15 @@ private: const String & getRelativeDataPath() const { return relative_data_path; } /// create directory monitors for each existing subdirectory - void createDirectoryMonitors(const DiskPtr & disk); - /// ensure directory monitor thread and connectoin pool creation by disk and subdirectory name - StorageDistributedDirectoryMonitor & requireDirectoryMonitor(const DiskPtr & disk, const std::string & name, bool startup); + void initializeDirectoryQueuesForDisk(const DiskPtr & disk); + /// ensure directory queue thread and connection pool created by disk and subdirectory name + DistributedAsyncInsertDirectoryQueue & getDirectoryQueue(const DiskPtr & disk, const std::string & name, bool startup); /// Return list of metrics for all created monitors /// (note that monitors are created lazily, i.e. until at least one INSERT executed) /// /// Used by StorageSystemDistributionQueue - std::vector getDirectoryMonitorsStatuses() const; + std::vector getDirectoryQueueStatuses() const; static IColumn::Selector createSelector(ClusterPtr cluster, const ColumnWithTypeAndName & result); /// Apply the following settings: @@ -248,7 +248,7 @@ private: struct ClusterNodeData { - std::shared_ptr directory_monitor; + std::shared_ptr directory_monitor; ConnectionPoolPtr connection_pool; }; std::unordered_map cluster_nodes_data; diff --git a/src/Storages/System/StorageSystemDistributionQueue.cpp b/src/Storages/System/StorageSystemDistributionQueue.cpp index 34cff7df65d..d57269f0638 100644 --- a/src/Storages/System/StorageSystemDistributionQueue.cpp +++ b/src/Storages/System/StorageSystemDistributionQueue.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -174,7 +173,7 @@ void StorageSystemDistributionQueue::fillData(MutableColumns & res_columns, Cont auto & distributed_table = dynamic_cast(*tables[database][table]); - for (const auto & status : distributed_table.getDirectoryMonitorsStatuses()) + for (const auto & status : distributed_table.getDirectoryQueueStatuses()) { size_t col_num = 0; res_columns[col_num++]->insert(database); diff --git a/src/TableFunctions/ITableFunctionFileLike.cpp b/src/TableFunctions/ITableFunctionFileLike.cpp index 8cbffc10e5a..bbaf2b68418 100644 --- a/src/TableFunctions/ITableFunctionFileLike.cpp +++ b/src/TableFunctions/ITableFunctionFileLike.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include diff --git a/tests/queries/0_stateless/02417_opentelemetry_insert_on_distributed_table.reference b/tests/queries/0_stateless/02417_opentelemetry_insert_on_distributed_table.reference index dde07d4540d..81a1f62ca29 100644 --- a/tests/queries/0_stateless/02417_opentelemetry_insert_on_distributed_table.reference +++ b/tests/queries/0_stateless/02417_opentelemetry_insert_on_distributed_table.reference @@ -1,7 +1,7 @@ {"operation_name":"void DB::DistributedSink::writeToLocal(const Cluster::ShardInfo &, const DB::Block &, size_t)","cluster":"test_cluster_two_shards_localhost","shard":"1","rows":"1","bytes":"8"} {"operation_name":"void DB::DistributedSink::writeToLocal(const Cluster::ShardInfo &, const DB::Block &, size_t)","cluster":"test_cluster_two_shards_localhost","shard":"2","rows":"1","bytes":"8"} -{"operation_name":"void DB::StorageDistributedDirectoryMonitor::processFile(const std::string &)","cluster":"test_cluster_two_shards_localhost","shard":"1","rows":"1","bytes":"8"} -{"operation_name":"void DB::StorageDistributedDirectoryMonitor::processFile(const std::string &)","cluster":"test_cluster_two_shards_localhost","shard":"2","rows":"1","bytes":"8"} +{"operation_name":"void DB::DistributedAsyncInsertDirectoryQueue::processFile(const std::string &)","cluster":"test_cluster_two_shards_localhost","shard":"1","rows":"1","bytes":"8"} +{"operation_name":"void DB::DistributedAsyncInsertDirectoryQueue::processFile(const std::string &)","cluster":"test_cluster_two_shards_localhost","shard":"2","rows":"1","bytes":"8"} {"operation_name":"auto DB::DistributedSink::runWritingJob(DB::DistributedSink::JobReplica &, const DB::Block &, size_t)::(anonymous class)::operator()() const","cluster":"test_cluster_two_shards_localhost","shard":"1","rows":"1","bytes":"8"} {"operation_name":"auto DB::DistributedSink::runWritingJob(DB::DistributedSink::JobReplica &, const DB::Block &, size_t)::(anonymous class)::operator()() const","cluster":"test_cluster_two_shards_localhost","shard":"2","rows":"1","bytes":"8"} {"operation_name":"auto DB::DistributedSink::runWritingJob(DB::DistributedSink::JobReplica &, const DB::Block &, size_t)::(anonymous class)::operator()() const","cluster":"test_cluster_two_shards_localhost","shard":"1","rows":"1","bytes":"8"} From e10fb142fd124455b12d0b359c9866f0349ef26c Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 20:37:12 +0100 Subject: [PATCH 124/342] Fix race for distributed sends from disk Before it was initialized from disk only on startup, but if some INSERT can create the object before, then, it will lead to the situation when it will not be initialized. Signed-off-by: Azat Khuzhin --- .../DistributedAsyncInsertDirectoryQueue.cpp | 6 +-- .../DistributedAsyncInsertDirectoryQueue.h | 3 +- src/Storages/Distributed/DistributedSink.cpp | 38 +++++++++---------- src/Storages/StorageDistributed.cpp | 7 ++-- src/Storages/StorageDistributed.h | 8 +++- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 2d25c1822d8..3993e9065d5 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -118,8 +118,7 @@ DistributedAsyncInsertDirectoryQueue::DistributedAsyncInsertDirectoryQueue( const std::string & relative_path_, ConnectionPoolPtr pool_, ActionBlocker & monitor_blocker_, - BackgroundSchedulePool & bg_pool, - bool initialize_from_disk) + BackgroundSchedulePool & bg_pool) : storage(storage_) , pool(std::move(pool_)) , disk(disk_) @@ -144,8 +143,7 @@ DistributedAsyncInsertDirectoryQueue::DistributedAsyncInsertDirectoryQueue( { fs::create_directory(broken_path); - if (initialize_from_disk) - initializeFilesFromDisk(); + initializeFilesFromDisk(); task_handle = bg_pool.createTask(getLoggerName() + "/Bg", [this]{ run(); }); task_handle->activateAndSchedule(); diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h index 90e3d563b4b..de8bb813824 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.h @@ -54,8 +54,7 @@ public: const std::string & relative_path_, ConnectionPoolPtr pool_, ActionBlocker & monitor_blocker_, - BackgroundSchedulePool & bg_pool, - bool initialize_from_disk); + BackgroundSchedulePool & bg_pool); ~DistributedAsyncInsertDirectoryQueue(); diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index 38aa26fbe0c..3c4e493c34a 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -724,8 +724,8 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const return guard; }; - std::vector bin_files; - bin_files.reserve(dir_names.size()); + auto sleep_ms = context->getSettingsRef().distributed_directory_monitor_sleep_time_ms.totalMilliseconds(); + size_t file_size; auto it = dir_names.begin(); /// on first iteration write block to a temporary directory for subsequent @@ -804,10 +804,16 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const out.sync(); } + file_size = fs::file_size(first_file_tmp_path); + // Create hardlink here to reuse increment number - bin_files.push_back(fs::path(path) / file_name); - createHardLink(first_file_tmp_path, bin_files.back()); - auto dir_sync_guard = make_directory_sync_guard(*it); + auto bin_file = (fs::path(path) / file_name).string(); + auto & directory_queue = storage.getDirectoryQueue(disk, *it); + { + createHardLink(first_file_tmp_path, bin_file); + auto dir_sync_guard = make_directory_sync_guard(*it); + } + directory_queue.addFileAndSchedule(bin_file, file_size, sleep_ms); } ++it; @@ -817,26 +823,18 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const const std::string path(fs::path(disk_path) / (data_path + *it)); fs::create_directory(path); - bin_files.push_back(fs::path(path) / (toString(storage.file_names_increment.get()) + ".bin")); - createHardLink(first_file_tmp_path, bin_files.back()); - auto dir_sync_guard = make_directory_sync_guard(*it); + auto bin_file = (fs::path(path) / (toString(storage.file_names_increment.get()) + ".bin")).string(); + auto & directory_monitor = storage.getDirectoryQueue(disk, *it); + { + createHardLink(first_file_tmp_path, bin_file); + auto dir_sync_guard = make_directory_sync_guard(*it); + } + directory_monitor.addFileAndSchedule(bin_file, file_size, sleep_ms); } - auto file_size = fs::file_size(first_file_tmp_path); /// remove the temporary file, enabling the OS to reclaim inode after all threads /// have removed their corresponding files fs::remove(first_file_tmp_path); - - /// Notify - auto sleep_ms = context->getSettingsRef().distributed_directory_monitor_sleep_time_ms; - for (size_t i = 0; i < dir_names.size(); ++i) - { - const auto & dir_name = dir_names[i]; - const auto & bin_file = bin_files[i]; - - auto & directory_monitor = storage.getDirectoryQueue(disk, dir_name, /* startup= */ false); - directory_monitor.addFileAndSchedule(bin_file, file_size, sleep_ms.totalMilliseconds()); - } } } diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index d95c703216e..9f674ce3bed 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -1225,14 +1225,14 @@ void StorageDistributed::initializeDirectoryQueuesForDisk(const DiskPtr & disk) } else { - getDirectoryQueue(disk, dir_path.filename().string(), /* startup= */ true); + getDirectoryQueue(disk, dir_path.filename().string()); } } } } -DistributedAsyncInsertDirectoryQueue & StorageDistributed::getDirectoryQueue(const DiskPtr & disk, const std::string & name, bool startup) +DistributedAsyncInsertDirectoryQueue & StorageDistributed::getDirectoryQueue(const DiskPtr & disk, const std::string & name) { const std::string & disk_path = disk->getPath(); const std::string key(disk_path + name); @@ -1246,8 +1246,7 @@ DistributedAsyncInsertDirectoryQueue & StorageDistributed::getDirectoryQueue(con *this, disk, relative_data_path + name, node_data.connection_pool, monitors_blocker, - getContext()->getDistributedSchedulePool(), - /* initialize_from_disk= */ startup); + getContext()->getDistributedSchedulePool()); } return *node_data.directory_monitor; } diff --git a/src/Storages/StorageDistributed.h b/src/Storages/StorageDistributed.h index df4c16f0f67..3a7fae44708 100644 --- a/src/Storages/StorageDistributed.h +++ b/src/Storages/StorageDistributed.h @@ -166,8 +166,12 @@ private: /// create directory monitors for each existing subdirectory void initializeDirectoryQueuesForDisk(const DiskPtr & disk); - /// ensure directory queue thread and connection pool created by disk and subdirectory name - DistributedAsyncInsertDirectoryQueue & getDirectoryQueue(const DiskPtr & disk, const std::string & name, bool startup); + + /// Get directory queue thread and connection pool created by disk and subdirectory name + /// + /// Used for the INSERT into Distributed in case of insert_distributed_sync==1, from DistributedSink. + DistributedAsyncInsertDirectoryQueue & getDirectoryQueue(const DiskPtr & disk, const std::string & name); + /// Return list of metrics for all created monitors /// (note that monitors are created lazily, i.e. until at least one INSERT executed) From e83699a8d3b72de7ab5ca3295dcabc59492a7fc4 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 20:51:04 +0100 Subject: [PATCH 125/342] Improve comment for DistributedAsyncInsertDirectoryQueue::initializeFilesFromDisk() Signed-off-by: Azat Khuzhin --- .../Distributed/DistributedAsyncInsertDirectoryQueue.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 3993e9065d5..6530ebc578a 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -335,8 +335,9 @@ void DistributedAsyncInsertDirectoryQueue::addFile(const std::string & file_path void DistributedAsyncInsertDirectoryQueue::initializeFilesFromDisk() { - /// NOTE: This method does not requires to hold status_mutex, hence, no TSA - /// annotations in the header file. + /// NOTE: This method does not requires to hold status_mutex (because this + /// object is not in the list that the caller may iterate over), hence, no + /// TSA annotations in the header file. fs::directory_iterator end; From a76d7b22c1ff11c775986cef17d91c500ca9e7ce Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 20:51:16 +0100 Subject: [PATCH 126/342] Use existing public methods of StorageDistributed in DistributedSink Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DistributedSink.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index 3c4e493c34a..14febb1a2b4 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -340,9 +340,9 @@ DistributedSink::runWritingJob(JobReplica & job, const Block & current_block, si size_t rows = shard_block.rows(); span.addAttribute("clickhouse.shard_num", shard_info.shard_num); - span.addAttribute("clickhouse.cluster", this->storage.cluster_name); - span.addAttribute("clickhouse.distributed", this->storage.getStorageID().getFullNameNotQuoted()); - span.addAttribute("clickhouse.remote", [this]() { return storage.remote_database + "." + storage.remote_table; }); + span.addAttribute("clickhouse.cluster", storage.cluster_name); + span.addAttribute("clickhouse.distributed", storage.getStorageID().getFullNameNotQuoted()); + span.addAttribute("clickhouse.remote", [this]() { return storage.getRemoteDatabaseName() + "." + storage.getRemoteTableName(); }); span.addAttribute("clickhouse.rows", rows); span.addAttribute("clickhouse.bytes", [&shard_block]() { return toString(shard_block.bytes()); }); @@ -476,7 +476,7 @@ void DistributedSink::writeSync(const Block & block) span.addAttribute("clickhouse.start_shard", start); span.addAttribute("clickhouse.end_shard", end); - span.addAttribute("db.statement", this->query_string); + span.addAttribute("db.statement", query_string); if (num_shards > 1) { @@ -659,9 +659,9 @@ void DistributedSink::writeToLocal(const Cluster::ShardInfo & shard_info, const { OpenTelemetry::SpanHolder span(__PRETTY_FUNCTION__); span.addAttribute("clickhouse.shard_num", shard_info.shard_num); - span.addAttribute("clickhouse.cluster", this->storage.cluster_name); - span.addAttribute("clickhouse.distributed", this->storage.getStorageID().getFullNameNotQuoted()); - span.addAttribute("clickhouse.remote", [this]() { return storage.remote_database + "." + storage.remote_table; }); + span.addAttribute("clickhouse.cluster", storage.cluster_name); + span.addAttribute("clickhouse.distributed", storage.getStorageID().getFullNameNotQuoted()); + span.addAttribute("clickhouse.remote", [this]() { return storage.getRemoteDatabaseName() + "." + storage.getRemoteTableName(); }); span.addAttribute("clickhouse.rows", [&block]() { return toString(block.rows()); }); span.addAttribute("clickhouse.bytes", [&block]() { return toString(block.bytes()); }); @@ -782,9 +782,9 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const } writeVarUInt(shard_info.shard_num, header_buf); - writeStringBinary(this->storage.cluster_name, header_buf); - writeStringBinary(this->storage.getStorageID().getFullNameNotQuoted(), header_buf); - writeStringBinary(this->storage.remote_database + "." + this->storage.remote_table, header_buf); + writeStringBinary(storage.cluster_name, header_buf); + writeStringBinary(storage.getStorageID().getFullNameNotQuoted(), header_buf); + writeStringBinary(storage.getRemoteDatabaseName() + "." + storage.getRemoteTableName(), header_buf); /// Add new fields here, for example: /// writeVarUInt(my_new_data, header_buf); From 00115c6615c311faffc40d24476c1ef51d40312c Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 20:54:49 +0100 Subject: [PATCH 127/342] Rename readDistributedAsyncInsertHeader() Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DistributedAsyncInsertBatch.cpp | 4 ++-- .../Distributed/DistributedAsyncInsertDirectoryQueue.cpp | 4 ++-- src/Storages/Distributed/DistributedAsyncInsertHeader.cpp | 2 +- src/Storages/Distributed/DistributedAsyncInsertHeader.h | 4 ++-- src/Storages/Distributed/DistributedAsyncInsertSource.cpp | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp index 80350600caa..948b4186ec6 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp @@ -201,7 +201,7 @@ void DistributedAsyncInsertBatch::sendBatch() for (const auto & file : files) { ReadBufferFromFile in(file); - const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); + const auto & distributed_header = DistributedAsyncInsertHeader::read(in, parent.log); OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, distributed_header.client_info.client_trace_context, @@ -240,7 +240,7 @@ void DistributedAsyncInsertBatch::sendSeparateFiles() try { ReadBufferFromFile in(file); - const auto & distributed_header = readDistributedAsyncInsertHeader(in, parent.log); + const auto & distributed_header = DistributedAsyncInsertHeader::read(in, parent.log); // This function is called in a separated thread, so we set up the trace context from the file OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 6530ebc578a..b58096182ec 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -417,7 +417,7 @@ void DistributedAsyncInsertDirectoryQueue::processFile(const std::string & file_ CurrentMetrics::Increment metric_increment{CurrentMetrics::DistributedSend}; ReadBufferFromFile in(file_path); - const auto & distributed_header = readDistributedAsyncInsertHeader(in, log); + const auto & distributed_header = DistributedAsyncInsertHeader::read(in, log); thread_trace_context = std::make_unique(__PRETTY_FUNCTION__, distributed_header.client_info.client_trace_context, @@ -563,7 +563,7 @@ void DistributedAsyncInsertDirectoryQueue::processFilesWithBatching() { /// Determine metadata of the current file and check if it is not broken. ReadBufferFromFile in{file_path}; - distributed_header = readDistributedAsyncInsertHeader(in, log); + distributed_header = DistributedAsyncInsertHeader::read(in, log); if (distributed_header.rows) { diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp index 19235c91cc6..9584d1eebdf 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp @@ -17,7 +17,7 @@ namespace ErrorCodes extern const int CHECKSUM_DOESNT_MATCH; } -DistributedAsyncInsertHeader readDistributedAsyncInsertHeader(ReadBufferFromFile & in, Poco::Logger * log) +DistributedAsyncInsertHeader DistributedAsyncInsertHeader::read(ReadBufferFromFile & in, Poco::Logger * log) { DistributedAsyncInsertHeader distributed_header; diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.h b/src/Storages/Distributed/DistributedAsyncInsertHeader.h index 2c7a6477b6c..dc048107a47 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertHeader.h +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.h @@ -31,8 +31,8 @@ struct DistributedAsyncInsertHeader /// dumpStructure() of the header -- obsolete std::string block_header_string; Block block_header; + + static DistributedAsyncInsertHeader read(ReadBufferFromFile & in, Poco::Logger * log); }; -DistributedAsyncInsertHeader readDistributedAsyncInsertHeader(ReadBufferFromFile & in, Poco::Logger * log); - } diff --git a/src/Storages/Distributed/DistributedAsyncInsertSource.cpp b/src/Storages/Distributed/DistributedAsyncInsertSource.cpp index 782cbf9c026..7992636ac11 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertSource.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertSource.cpp @@ -22,7 +22,7 @@ struct DistributedAsyncInsertSource::Data : log(&Poco::Logger::get("DistributedAsyncInsertSource")) , in(file_name) , decompressing_in(in) - , block_in(decompressing_in, readDistributedAsyncInsertHeader(in, log).revision) + , block_in(decompressing_in, DistributedAsyncInsertHeader::read(in, log).revision) , first_block(block_in.read()) { } From 263c042c6aa6103e5be221fe36c83be593056ac6 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 21:01:54 +0100 Subject: [PATCH 128/342] Fix opentelemetry for distributed batch sends Signed-off-by: Azat Khuzhin --- .../DistributedAsyncInsertBatch.cpp | 79 ++++++++++++------- .../DistributedAsyncInsertDirectoryQueue.cpp | 12 +-- .../DistributedAsyncInsertHeader.cpp | 16 ++++ .../DistributedAsyncInsertHeader.h | 7 ++ 4 files changed, 77 insertions(+), 37 deletions(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp index 948b4186ec6..10bf9b8d66e 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp @@ -198,37 +198,59 @@ void DistributedAsyncInsertBatch::sendBatch() IConnectionPool::Entry connection; - for (const auto & file : files) + /// Since the batch is sent as a whole (in case of failure, the whole batch + /// will be repeated), we need to mark the whole batch as failed in case of + /// error). + std::vector tracing_contexts; + UInt64 batch_start_time = clock_gettime_ns(); + + try { - ReadBufferFromFile in(file); - const auto & distributed_header = DistributedAsyncInsertHeader::read(in, parent.log); - - OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, - distributed_header.client_info.client_trace_context, - parent.storage.getContext()->getOpenTelemetrySpanLog()); - - if (!remote) + for (const auto & file : files) { - auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); - connection = parent.pool->get(timeouts); - compression_expected = connection->getCompression() == Protocol::Compression::Enable; + ReadBufferFromFile in(file); + const auto & distributed_header = DistributedAsyncInsertHeader::read(in, parent.log); - LOG_DEBUG(parent.log, "Sending a batch of {} files to {} ({} rows, {} bytes).", - files.size(), - connection->getDescription(), - formatReadableQuantity(total_rows), - formatReadableSizeWithBinarySuffix(total_bytes)); + tracing_contexts.emplace_back(distributed_header.createTracingContextHolder( + parent.storage.getContext()->getOpenTelemetrySpanLog())); + tracing_contexts.back()->root_span.addAttribute("clickhouse.distributed_batch_start_time", batch_start_time); - remote = std::make_unique(*connection, timeouts, - distributed_header.insert_query, - distributed_header.insert_settings, - distributed_header.client_info); + if (!remote) + { + auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); + connection = parent.pool->get(timeouts); + compression_expected = connection->getCompression() == Protocol::Compression::Enable; + + LOG_DEBUG(parent.log, "Sending a batch of {} files to {} ({} rows, {} bytes).", + files.size(), + connection->getDescription(), + formatReadableQuantity(total_rows), + formatReadableSizeWithBinarySuffix(total_bytes)); + + remote = std::make_unique(*connection, timeouts, + distributed_header.insert_query, + distributed_header.insert_settings, + distributed_header.client_info); + } + writeRemoteConvert(distributed_header, *remote, compression_expected, in, parent.log); } - writeRemoteConvert(distributed_header, *remote, compression_expected, in, parent.log); - } - if (remote) - remote->onFinish(); + if (remote) + remote->onFinish(); + } + catch (...) + { + try + { + for (auto & tracing_context : tracing_contexts) + tracing_context->root_span.addAttribute(std::current_exception()); + } + catch (...) + { + tryLogCurrentException(parent.log, "Cannot append exception to tracing context"); + } + throw; + } } void DistributedAsyncInsertBatch::sendSeparateFiles() @@ -237,14 +259,15 @@ void DistributedAsyncInsertBatch::sendSeparateFiles() for (const auto & file : files) { + OpenTelemetry::TracingContextHolderPtr trace_context; + try { ReadBufferFromFile in(file); const auto & distributed_header = DistributedAsyncInsertHeader::read(in, parent.log); // This function is called in a separated thread, so we set up the trace context from the file - OpenTelemetry::TracingContextHolder thread_trace_context(__PRETTY_FUNCTION__, - distributed_header.client_info.client_trace_context, + trace_context = distributed_header.createTracingContextHolder( parent.storage.getContext()->getOpenTelemetrySpanLog()); auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); @@ -261,6 +284,8 @@ void DistributedAsyncInsertBatch::sendSeparateFiles() } catch (Exception & e) { + trace_context->root_span.addAttribute(std::current_exception()); + if (isDistributedSendBroken(e.code(), e.isRemoteException())) { parent.markAsBroken(file); diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index b58096182ec..31c621571ab 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -418,16 +418,8 @@ void DistributedAsyncInsertDirectoryQueue::processFile(const std::string & file_ ReadBufferFromFile in(file_path); const auto & distributed_header = DistributedAsyncInsertHeader::read(in, log); - - thread_trace_context = std::make_unique(__PRETTY_FUNCTION__, - distributed_header.client_info.client_trace_context, - this->storage.getContext()->getOpenTelemetrySpanLog()); - thread_trace_context->root_span.addAttribute("clickhouse.shard_num", distributed_header.shard_num); - thread_trace_context->root_span.addAttribute("clickhouse.cluster", distributed_header.cluster); - thread_trace_context->root_span.addAttribute("clickhouse.distributed", distributed_header.distributed_table); - thread_trace_context->root_span.addAttribute("clickhouse.remote", distributed_header.remote_table); - thread_trace_context->root_span.addAttribute("clickhouse.rows", distributed_header.rows); - thread_trace_context->root_span.addAttribute("clickhouse.bytes", distributed_header.bytes); + thread_trace_context = + distributed_header.createTracingContextHolder(storage.getContext()->getOpenTelemetrySpanLog()); auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); auto connection = pool->get(timeouts, &distributed_header.insert_settings); diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp index 9584d1eebdf..b5c16558eb2 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -106,4 +107,19 @@ DistributedAsyncInsertHeader DistributedAsyncInsertHeader::read(ReadBufferFromFi return distributed_header; } +OpenTelemetry::TracingContextHolderPtr DistributedAsyncInsertHeader::createTracingContextHolder(std::shared_ptr open_telemetry_span_log) const +{ + OpenTelemetry::TracingContextHolderPtr trace_context = std::make_unique( + __PRETTY_FUNCTION__, + client_info.client_trace_context, + std::move(open_telemetry_span_log)); + trace_context->root_span.addAttribute("clickhouse.shard_num", shard_num); + trace_context->root_span.addAttribute("clickhouse.cluster", cluster); + trace_context->root_span.addAttribute("clickhouse.distributed", distributed_table); + trace_context->root_span.addAttribute("clickhouse.remote", remote_table); + trace_context->root_span.addAttribute("clickhouse.rows", rows); + trace_context->root_span.addAttribute("clickhouse.bytes", bytes); + return trace_context; +} + } diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.h b/src/Storages/Distributed/DistributedAsyncInsertHeader.h index dc048107a47..dfc059d7083 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertHeader.h +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.h @@ -11,6 +11,12 @@ namespace DB class ReadBufferFromFile; +namespace OpenTelemetry +{ +struct TracingContextHolder; +using TracingContextHolderPtr = std::unique_ptr; +} + /// Header for the binary files that are stored on disk for async INSERT into Distributed. struct DistributedAsyncInsertHeader { @@ -33,6 +39,7 @@ struct DistributedAsyncInsertHeader Block block_header; static DistributedAsyncInsertHeader read(ReadBufferFromFile & in, Poco::Logger * log); + OpenTelemetry::TracingContextHolderPtr createTracingContextHolder(std::shared_ptr open_telemetry_span_log) const; }; } From 572dd76eae2b9e3090e07c388c6baa142a095a57 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 21:53:01 +0100 Subject: [PATCH 129/342] Extend ConcurrentBoundedQueue to deque Signed-off-by: Azat Khuzhin --- src/Common/ConcurrentBoundedQueue.h | 59 +++++++++++++++++++---------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/Common/ConcurrentBoundedQueue.h b/src/Common/ConcurrentBoundedQueue.h index fd4a2d5790b..665be69a28f 100644 --- a/src/Common/ConcurrentBoundedQueue.h +++ b/src/Common/ConcurrentBoundedQueue.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include @@ -18,7 +18,8 @@ template class ConcurrentBoundedQueue { private: - std::queue queue; + using Container = std::deque; + Container queue; mutable std::mutex queue_mutex; std::condition_variable push_condition; @@ -28,7 +29,7 @@ private: size_t max_fill = 0; - template + template bool emplaceImpl(std::optional timeout_milliseconds, Args &&...args) { { @@ -51,13 +52,17 @@ private: if (is_finished) return false; - queue.emplace(std::forward(args)...); + if constexpr (back) + queue.emplace_back(std::forward(args)...); + else + queue.emplace_front(std::forward(args)...); } pop_condition.notify_one(); return true; } + template bool popImpl(T & x, std::optional timeout_milliseconds) { { @@ -80,8 +85,16 @@ private: if (is_finished && queue.empty()) return false; - detail::moveOrCopyIfThrow(std::move(queue.front()), x); - queue.pop(); + if constexpr (front) + { + detail::moveOrCopyIfThrow(std::move(queue.front()), x); + queue.pop_front(); + } + else + { + detail::moveOrCopyIfThrow(std::move(queue.back()), x); + queue.pop_back(); + } } push_condition.notify_one(); @@ -94,6 +107,12 @@ public: : max_fill(max_fill_) {} + /// Returns false if queue is finished + [[nodiscard]] bool pushFront(const T & x) + { + return emplaceImpl(/* timeout_milliseconds= */ std::nullopt , x); + } + /// Returns false if queue is finished [[nodiscard]] bool push(const T & x) { @@ -109,37 +128,37 @@ public: template [[nodiscard]] bool emplace(Args &&... args) { - return emplaceImpl(std::nullopt /* timeout in milliseconds */, std::forward(args...)); - } - - /// Returns false if queue is finished and empty - [[nodiscard]] bool pop(T & x) - { - return popImpl(x, std::nullopt /*timeout in milliseconds*/); + return emplaceImpl(std::nullopt /* timeout in milliseconds */, std::forward(args...)); } /// Returns false if queue is finished or object was not pushed during timeout [[nodiscard]] bool tryPush(const T & x, UInt64 milliseconds = 0) { - return emplaceImpl(milliseconds, x); + return emplaceImpl(milliseconds, x); } [[nodiscard]] bool tryPush(T && x, UInt64 milliseconds = 0) { - return emplaceImpl(milliseconds, std::move(x)); + return emplaceImpl(milliseconds, std::move(x)); } /// Returns false if queue is finished or object was not emplaced during timeout template [[nodiscard]] bool tryEmplace(UInt64 milliseconds, Args &&... args) { - return emplaceImpl(milliseconds, std::forward(args...)); + return emplaceImpl(milliseconds, std::forward(args...)); + } + + /// Returns false if queue is finished and empty + [[nodiscard]] bool pop(T & x) + { + return popImpl(x, std::nullopt /*timeout in milliseconds*/); } /// Returns false if queue is (finished and empty) or (object was not popped during timeout) [[nodiscard]] bool tryPop(T & x, UInt64 milliseconds) { - return popImpl(x, milliseconds); + return popImpl(x, milliseconds); } /// Returns false if queue is empty. @@ -153,7 +172,7 @@ public: return false; detail::moveOrCopyIfThrow(std::move(queue.front()), x); - queue.pop(); + queue.pop_front(); } push_condition.notify_one(); @@ -222,7 +241,7 @@ public: if (is_finished) return; - std::queue empty_queue; + Container empty_queue; queue.swap(empty_queue); } @@ -235,7 +254,7 @@ public: { std::lock_guard lock(queue_mutex); - std::queue empty_queue; + Container empty_queue; queue.swap(empty_queue); is_finished = true; } From 752d27d663afd9b1b7ce384e9b9b84bf9f4e370e Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 22:00:57 +0100 Subject: [PATCH 130/342] Fix lossing files during distributed batch send v2: do not suppress exceptions in case of errors Signed-off-by: Azat Khuzhin --- .../DistributedAsyncInsertDirectoryQueue.cpp | 128 ++++++++++-------- 1 file changed, 73 insertions(+), 55 deletions(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 31c621571ab..32d1823b289 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -545,78 +545,96 @@ void DistributedAsyncInsertDirectoryQueue::processFilesWithBatching() std::unordered_map header_to_batch; std::string file_path; - while (pending_files.tryPop(file_path)) + + try { - size_t total_rows = 0; - size_t total_bytes = 0; - Block header; - DistributedAsyncInsertHeader distributed_header; - try + while (pending_files.tryPop(file_path)) { - /// Determine metadata of the current file and check if it is not broken. - ReadBufferFromFile in{file_path}; - distributed_header = DistributedAsyncInsertHeader::read(in, log); - - if (distributed_header.rows) + size_t total_rows = 0; + size_t total_bytes = 0; + Block header; + DistributedAsyncInsertHeader distributed_header; + try { - total_rows += distributed_header.rows; - total_bytes += distributed_header.bytes; - } + /// Determine metadata of the current file and check if it is not broken. + ReadBufferFromFile in{file_path}; + distributed_header = DistributedAsyncInsertHeader::read(in, log); - if (distributed_header.block_header) - header = distributed_header.block_header; - - if (!total_rows || !header) - { - LOG_DEBUG(log, "Processing batch {} with old format (no header/rows)", in.getFileName()); - - CompressedReadBuffer decompressing_in(in); - NativeReader block_in(decompressing_in, distributed_header.revision); - - while (Block block = block_in.read()) + if (distributed_header.rows) { - total_rows += block.rows(); - total_bytes += block.bytes(); + total_rows += distributed_header.rows; + total_bytes += distributed_header.bytes; + } - if (!header) - header = block.cloneEmpty(); + if (distributed_header.block_header) + header = distributed_header.block_header; + + if (!total_rows || !header) + { + LOG_DEBUG(log, "Processing batch {} with old format (no header/rows)", in.getFileName()); + + CompressedReadBuffer decompressing_in(in); + NativeReader block_in(decompressing_in, distributed_header.revision); + + while (Block block = block_in.read()) + { + total_rows += block.rows(); + total_bytes += block.bytes(); + + if (!header) + header = block.cloneEmpty(); + } } } - } - catch (const Exception & e) - { - if (isDistributedSendBroken(e.code(), e.isRemoteException())) + catch (const Exception & e) { - markAsBroken(file_path); - tryLogCurrentException(log, "File is marked broken due to"); - continue; + if (isDistributedSendBroken(e.code(), e.isRemoteException())) + { + markAsBroken(file_path); + tryLogCurrentException(log, "File is marked broken due to"); + continue; + } + else + throw; + } + + BatchHeader batch_header( + std::move(distributed_header.insert_settings), + std::move(distributed_header.insert_query), + std::move(distributed_header.client_info), + std::move(header) + ); + DistributedAsyncInsertBatch & batch = header_to_batch.try_emplace(batch_header, *this).first->second; + + batch.files.push_back(file_path); + batch.total_rows += total_rows; + batch.total_bytes += total_bytes; + + if (batch.isEnoughSize()) + { + batch.send(); } - else - throw; } - BatchHeader batch_header( - std::move(distributed_header.insert_settings), - std::move(distributed_header.insert_query), - std::move(distributed_header.client_info), - std::move(header) - ); - DistributedAsyncInsertBatch & batch = header_to_batch.try_emplace(batch_header, *this).first->second; - - batch.files.push_back(file_path); - batch.total_rows += total_rows; - batch.total_bytes += total_bytes; - - if (batch.isEnoughSize()) + for (auto & kv : header_to_batch) { + DistributedAsyncInsertBatch & batch = kv.second; batch.send(); } } - - for (auto & kv : header_to_batch) + catch (...) { - DistributedAsyncInsertBatch & batch = kv.second; - batch.send(); + /// Revert uncommitted files. + for (const auto & [_, batch] : header_to_batch) + { + for (const auto & file : batch.files) + { + if (!pending_files.pushFront(file)) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot re-schedule a file '{}'", file); + } + } + /// Rethrow exception + throw; } { From 16bfef3c8ae2f2af3f8a680ae6f4ed4e871ba6cd Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 21 Jan 2023 22:21:48 +0100 Subject: [PATCH 131/342] Fix processing current_batch.txt on init Signed-off-by: Azat Khuzhin --- .../Distributed/DistributedAsyncInsertDirectoryQueue.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 32d1823b289..97aaa8fba54 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -550,6 +550,12 @@ void DistributedAsyncInsertDirectoryQueue::processFilesWithBatching() { while (pending_files.tryPop(file_path)) { + if (!fs::exists(file_path)) + { + LOG_WARNING(log, "File {} does not exists, likely due to current_batch.txt processing", file_path); + continue; + } + size_t total_rows = 0; size_t total_bytes = 0; Block header; From 7063c20b3c4e288397096431c93118f75eea9ca5 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Fri, 17 Feb 2023 16:43:08 +0100 Subject: [PATCH 132/342] Change noisy "Skipping send data over distributed table." message to test Signed-off-by: Azat Khuzhin --- .../Distributed/DistributedAsyncInsertDirectoryQueue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 97aaa8fba54..0e31a759c4f 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -229,7 +229,7 @@ void DistributedAsyncInsertDirectoryQueue::run() } } else - LOG_DEBUG(log, "Skipping send data over distributed table."); + LOG_TEST(log, "Skipping send data over distributed table."); const auto now = std::chrono::system_clock::now(); if (now - last_decrease_time > decrease_error_count_period) From 591fca57f3fd1c072078f386b339157bc42d9ad7 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 18 Feb 2023 22:27:45 +0100 Subject: [PATCH 133/342] Fix function names for opentelemetry spans in StorageDistributed Fixes: 02417_opentelemetry_insert_on_distributed_table Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DistributedAsyncInsertBatch.cpp | 2 ++ .../Distributed/DistributedAsyncInsertDirectoryQueue.cpp | 5 +++-- src/Storages/Distributed/DistributedAsyncInsertHeader.cpp | 4 ++-- src/Storages/Distributed/DistributedAsyncInsertHeader.h | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp index 10bf9b8d66e..bf410eed6cc 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertBatch.cpp @@ -212,6 +212,7 @@ void DistributedAsyncInsertBatch::sendBatch() const auto & distributed_header = DistributedAsyncInsertHeader::read(in, parent.log); tracing_contexts.emplace_back(distributed_header.createTracingContextHolder( + __PRETTY_FUNCTION__, parent.storage.getContext()->getOpenTelemetrySpanLog())); tracing_contexts.back()->root_span.addAttribute("clickhouse.distributed_batch_start_time", batch_start_time); @@ -268,6 +269,7 @@ void DistributedAsyncInsertBatch::sendSeparateFiles() // This function is called in a separated thread, so we set up the trace context from the file trace_context = distributed_header.createTracingContextHolder( + __PRETTY_FUNCTION__, parent.storage.getContext()->getOpenTelemetrySpanLog()); auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index 0e31a759c4f..c6f675533c6 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -418,8 +418,9 @@ void DistributedAsyncInsertDirectoryQueue::processFile(const std::string & file_ ReadBufferFromFile in(file_path); const auto & distributed_header = DistributedAsyncInsertHeader::read(in, log); - thread_trace_context = - distributed_header.createTracingContextHolder(storage.getContext()->getOpenTelemetrySpanLog()); + thread_trace_context = distributed_header.createTracingContextHolder( + __PRETTY_FUNCTION__, + storage.getContext()->getOpenTelemetrySpanLog()); auto timeouts = ConnectionTimeouts::getTCPTimeoutsWithFailover(distributed_header.insert_settings); auto connection = pool->get(timeouts, &distributed_header.insert_settings); diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp index b5c16558eb2..018c1d863bb 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.cpp @@ -107,10 +107,10 @@ DistributedAsyncInsertHeader DistributedAsyncInsertHeader::read(ReadBufferFromFi return distributed_header; } -OpenTelemetry::TracingContextHolderPtr DistributedAsyncInsertHeader::createTracingContextHolder(std::shared_ptr open_telemetry_span_log) const +OpenTelemetry::TracingContextHolderPtr DistributedAsyncInsertHeader::createTracingContextHolder(const char * function, std::shared_ptr open_telemetry_span_log) const { OpenTelemetry::TracingContextHolderPtr trace_context = std::make_unique( - __PRETTY_FUNCTION__, + function, client_info.client_trace_context, std::move(open_telemetry_span_log)); trace_context->root_span.addAttribute("clickhouse.shard_num", shard_num); diff --git a/src/Storages/Distributed/DistributedAsyncInsertHeader.h b/src/Storages/Distributed/DistributedAsyncInsertHeader.h index dfc059d7083..a7330fa5ef1 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertHeader.h +++ b/src/Storages/Distributed/DistributedAsyncInsertHeader.h @@ -39,7 +39,7 @@ struct DistributedAsyncInsertHeader Block block_header; static DistributedAsyncInsertHeader read(ReadBufferFromFile & in, Poco::Logger * log); - OpenTelemetry::TracingContextHolderPtr createTracingContextHolder(std::shared_ptr open_telemetry_span_log) const; + OpenTelemetry::TracingContextHolderPtr createTracingContextHolder(const char * function, std::shared_ptr open_telemetry_span_log) const; }; } From d06a4b50d60e6ccb6e8907559abd0a8c5b27edae Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 28 Feb 2023 22:40:54 +0100 Subject: [PATCH 134/342] Latest review fixes (variable naming: s/monitor/queue) Signed-off-by: Azat Khuzhin --- src/Storages/Distributed/DistributedSink.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Storages/Distributed/DistributedSink.cpp b/src/Storages/Distributed/DistributedSink.cpp index 14febb1a2b4..1c372e87495 100644 --- a/src/Storages/Distributed/DistributedSink.cpp +++ b/src/Storages/Distributed/DistributedSink.cpp @@ -705,7 +705,7 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const CompressionCodecPtr compression_codec = CompressionCodecFactory::instance().get(compression_method, compression_level); /// tmp directory is used to ensure atomicity of transactions - /// and keep monitor thread out from reading incomplete data + /// and keep directory queue thread out from reading incomplete data std::string first_file_tmp_path; auto reservation = storage.getStoragePolicy()->reserveAndCheck(block.bytes()); @@ -824,12 +824,12 @@ void DistributedSink::writeToShard(const Cluster::ShardInfo & shard_info, const fs::create_directory(path); auto bin_file = (fs::path(path) / (toString(storage.file_names_increment.get()) + ".bin")).string(); - auto & directory_monitor = storage.getDirectoryQueue(disk, *it); + auto & directory_queue = storage.getDirectoryQueue(disk, *it); { createHardLink(first_file_tmp_path, bin_file); auto dir_sync_guard = make_directory_sync_guard(*it); } - directory_monitor.addFileAndSchedule(bin_file, file_size, sleep_ms); + directory_queue.addFileAndSchedule(bin_file, file_size, sleep_ms); } /// remove the temporary file, enabling the OS to reclaim inode after all threads From 99329d868232d9377d7f808763e951e6f15fd71c Mon Sep 17 00:00:00 2001 From: AVMusorin Date: Fri, 10 Feb 2023 13:55:04 +0100 Subject: [PATCH 135/342] Improve tests for Distributed INSERT Signed-off-by: Azat Khuzhin --- .../configs/overrides_1.xml | 2 - .../configs/overrides_2.xml | 2 - .../test.py | 113 +++++++++++------- .../02536_distributed_detach_table.reference | 2 + .../02536_distributed_detach_table.sql | 16 +++ ...ed_loosing_files_after_exception.reference | 16 +++ ...buted_loosing_files_after_exception.sql.j2 | 32 +++++ 7 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 tests/queries/0_stateless/02536_distributed_detach_table.reference create mode 100644 tests/queries/0_stateless/02536_distributed_detach_table.sql create mode 100644 tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.reference create mode 100644 tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.sql.j2 diff --git a/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_1.xml b/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_1.xml index 397e05e7a60..a79ce3de1fc 100644 --- a/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_1.xml +++ b/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_1.xml @@ -3,8 +3,6 @@ 0 - - 1 1 diff --git a/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_2.xml b/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_2.xml index 2ffd5beaf8d..8279fcdbe6d 100644 --- a/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_2.xml +++ b/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/configs/overrides_2.xml @@ -3,8 +3,6 @@ 0 - - 1 0 diff --git a/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/test.py b/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/test.py index a47268b06fd..faa38af6533 100644 --- a/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/test.py +++ b/tests/integration/test_distributed_directory_monitor_split_batch_on_failure/test.py @@ -18,61 +18,86 @@ node2 = cluster.add_instance( ) +def get_test_settings(): + settings = {"monitor_batch_inserts": [0, 1]} + return [(k, v) for k, values in settings.items() for v in values] + + +def drop_tables(): + tables = ["null_", "dist", "data", "mv", "dist_data"] + query = "\n".join([f"drop table if exists {table};" for table in tables]) + for _, node in cluster.instances.items(): + node.query(query) + + +def create_tables(**dist_settings): + drop_tables() + _settings_values = ",".join([f"{k}={v}" for k, v in dist_settings.items()]) + _settings = f"settings {_settings_values}" if _settings_values else "" + for _, node in cluster.instances.items(): + node.query( + f""" + create table null_ (key Int, value Int) engine=Null(); + create table dist as null_ engine=Distributed(test_cluster, currentDatabase(), null_, key) {_settings}; + create table data (key Int, uniq_values Int) engine=Memory(); + create materialized view mv to data as select key, uniqExact(value) uniq_values from null_ group by key; + system stop distributed sends dist; + + create table dist_data as data engine=Distributed(test_cluster, currentDatabase(), data); + """ + ) + + @pytest.fixture(scope="module") def started_cluster(): try: cluster.start() - - for _, node in cluster.instances.items(): - node.query( - """ - create table null_ (key Int, value Int) engine=Null(); - create table dist as null_ engine=Distributed(test_cluster, currentDatabase(), null_, key); - create table data (key Int, uniq_values Int) engine=Memory(); - create materialized view mv to data as select key, uniqExact(value) uniq_values from null_ group by key; - system stop distributed sends dist; - - create table dist_data as data engine=Distributed(test_cluster, currentDatabase(), data); - """ - ) - yield cluster finally: + drop_tables() cluster.shutdown() def test_distributed_directory_monitor_split_batch_on_failure_OFF(started_cluster): - for i in range(0, 100): - limit = 100e3 - node2.query( - f"insert into dist select number/100, number from system.numbers limit {limit} offset {limit*i}", - settings={ - # max_memory_usage is the limit for the batch on the remote node - # (local query should not be affected since 30MB is enough for 100K rows) - "max_memory_usage": "30Mi", - "max_untracked_memory": "0", - }, - ) - # "Received from" is mandatory, since the exception should be thrown on the remote node. - with pytest.raises( - QueryRuntimeException, - match=r"DB::Exception: Received from.*Memory limit \(for query\) exceeded: .*while pushing to view default\.mv", - ): + for setting, setting_value in get_test_settings(): + create_tables(**{setting: setting_value}) + for i in range(0, 100): + limit = 100e3 + node2.query( + f"insert into dist select number/100, number from system.numbers limit {limit} offset {limit*i}", + settings={ + # max_memory_usage is the limit for the batch on the remote node + # (local query should not be affected since 30MB is enough for 100K rows) + "max_memory_usage": "30Mi", + "max_untracked_memory": "0", + }, + ) + # "Received from" is mandatory, since the exception should be thrown on the remote node. + if setting == "monitor_batch_inserts" and setting_value == 1: + with pytest.raises( + QueryRuntimeException, + match=r"DB::Exception: Received from.*Memory limit \(for query\) exceeded: .*while pushing to view default\.mv", + ): + node2.query("system flush distributed dist") + assert int(node2.query("select count() from dist_data")) == 0 + continue node2.query("system flush distributed dist") - assert int(node2.query("select count() from dist_data")) == 0 + assert int(node2.query("select count() from dist_data")) == 100000 def test_distributed_directory_monitor_split_batch_on_failure_ON(started_cluster): - for i in range(0, 100): - limit = 100e3 - node1.query( - f"insert into dist select number/100, number from system.numbers limit {limit} offset {limit*i}", - settings={ - # max_memory_usage is the limit for the batch on the remote node - # (local query should not be affected since 30MB is enough for 100K rows) - "max_memory_usage": "30Mi", - "max_untracked_memory": "0", - }, - ) - node1.query("system flush distributed dist") - assert int(node1.query("select count() from dist_data")) == 100000 + for setting, setting_value in get_test_settings(): + create_tables(**{setting: setting_value}) + for i in range(0, 100): + limit = 100e3 + node1.query( + f"insert into dist select number/100, number from system.numbers limit {limit} offset {limit*i}", + settings={ + # max_memory_usage is the limit for the batch on the remote node + # (local query should not be affected since 30MB is enough for 100K rows) + "max_memory_usage": "30Mi", + "max_untracked_memory": "0", + }, + ) + node1.query("system flush distributed dist") + assert int(node1.query("select count() from dist_data")) == 100000 diff --git a/tests/queries/0_stateless/02536_distributed_detach_table.reference b/tests/queries/0_stateless/02536_distributed_detach_table.reference new file mode 100644 index 00000000000..f09bace4421 --- /dev/null +++ b/tests/queries/0_stateless/02536_distributed_detach_table.reference @@ -0,0 +1,2 @@ +0 0 +10 20 diff --git a/tests/queries/0_stateless/02536_distributed_detach_table.sql b/tests/queries/0_stateless/02536_distributed_detach_table.sql new file mode 100644 index 00000000000..92bee1ee544 --- /dev/null +++ b/tests/queries/0_stateless/02536_distributed_detach_table.sql @@ -0,0 +1,16 @@ +-- test detach distributed table with pending files +CREATE TABLE test_02536 (n Int8) ENGINE=MergeTree() ORDER BY tuple(); +CREATE TABLE test_dist_02536 (n Int8) ENGINE=Distributed(test_cluster_two_shards, currentDatabase(), test_02536, rand()); +SYSTEM STOP DISTRIBUTED SENDS test_dist_02536; + +INSERT INTO test_dist_02536 SELECT number FROM numbers(5) SETTINGS prefer_localhost_replica=0; +SELECT count(n), sum(n) FROM test_dist_02536; -- 0 0 + +DETACH TABLE test_dist_02536; +ATTACH TABLE test_dist_02536; + +SYSTEM FLUSH DISTRIBUTED test_dist_02536; + +SELECT count(n), sum(n) FROM test_dist_02536; -- 10 20 +DROP TABLE test_02536; +DROP TABLE test_dist_02536; diff --git a/tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.reference b/tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.reference new file mode 100644 index 00000000000..7793e91fcb6 --- /dev/null +++ b/tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.reference @@ -0,0 +1,16 @@ +monitor_batch_insert=0 +1 2 +1 0 +-- { echoOn } +SELECT sum(key), count(key) FROM dist; +2 2 +SELECT sum(key), count(key) FROM underlying; +2 2 +monitor_batch_insert=1 +1 2 +1 0 +-- { echoOn } +SELECT sum(key), count(key) FROM dist; +2 2 +SELECT sum(key), count(key) FROM underlying; +2 2 diff --git a/tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.sql.j2 b/tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.sql.j2 new file mode 100644 index 00000000000..4f8cf1ccffe --- /dev/null +++ b/tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.sql.j2 @@ -0,0 +1,32 @@ +{% for setting in [0, 1] %} +-- Testing that distributed table doesn't loose file after inserts which contain errors + +SELECT 'monitor_batch_insert={{ setting }}'; + +DROP TABLE IF EXISTS dist; +DROP TABLE IF EXISTS underlying; + +CREATE TABLE dist (key Int) ENGINE=Distributed(test_shard_localhost, currentDatabase(), underlying) SETTINGS monitor_batch_inserts={{ setting }}; +SYSTEM STOP DISTRIBUTED SENDS dist; + +INSERT INTO dist SETTINGS prefer_localhost_replica=0, max_threads=1 VALUES (1); +INSERT INTO dist SETTINGS prefer_localhost_replica=0, max_threads=2 VALUES (1); + +SYSTEM FLUSH DISTRIBUTED dist; -- { serverError UNKNOWN_TABLE } +-- check the second since after using queue it may got lost from it +SYSTEM FLUSH DISTRIBUTED dist; -- { serverError UNKNOWN_TABLE } + +SELECT is_blocked, data_files FROM system.distribution_queue WHERE database = currentDatabase() AND table = 'dist'; + +CREATE TABLE underlying (key Int) ENGINE=Memory(); +SYSTEM FLUSH DISTRIBUTED dist; + +-- all data should be flushed +SELECT is_blocked, data_files FROM system.distribution_queue WHERE database = currentDatabase() AND table = 'dist'; + +-- { echoOn } +SELECT sum(key), count(key) FROM dist; +SELECT sum(key), count(key) FROM underlying; +-- { echoOff } + +{% endfor %} From c797122dbe4fa97e726536cc5b6784e16a11dd44 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Wed, 1 Mar 2023 08:45:50 +0000 Subject: [PATCH 136/342] Better --- src/Coordination/KeeperStorage.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Coordination/KeeperStorage.cpp b/src/Coordination/KeeperStorage.cpp index b1f3b44b1e1..33b2a91d8bf 100644 --- a/src/Coordination/KeeperStorage.cpp +++ b/src/Coordination/KeeperStorage.cpp @@ -238,10 +238,7 @@ void KeeperStorage::Node::shallowCopy(const KeeperStorage::Node & other) void KeeperStorage::Node::recalculateSize() { size_bytes = sizeof(Node); - - for (const auto child_path : children) - size_bytes += sizeof child_path; - + size_bytes += children.size() * sizeof(decltype(children)::value_type); size_bytes += data.size(); } From e1352adced7eb8f5e5201fb12c1634903ade6a59 Mon Sep 17 00:00:00 2001 From: artem-yadr <84010375+artem-yadr@users.noreply.github.com> Date: Wed, 1 Mar 2023 12:50:03 +0300 Subject: [PATCH 137/342] Update MongoDBDictionarySource.cpp --- src/Dictionaries/MongoDBDictionarySource.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dictionaries/MongoDBDictionarySource.cpp b/src/Dictionaries/MongoDBDictionarySource.cpp index f61efcab026..a9555a94304 100644 --- a/src/Dictionaries/MongoDBDictionarySource.cpp +++ b/src/Dictionaries/MongoDBDictionarySource.cpp @@ -118,7 +118,7 @@ MongoDBDictionarySource::MongoDBDictionarySource( Poco::MongoDB::Connection::SocketFactory socket_factory; connection->connect(uri, socket_factory); - Poco::URI poco_uri(connection.uri()); + Poco::URI poco_uri(connection->uri()); // Parse database from URI. This is required for correctness -- the // cursor is created using database name and collection name, so we have From 67b271b1fa39719f69154d68ba660770b86e189e Mon Sep 17 00:00:00 2001 From: Alexey Korepanov Date: Wed, 1 Mar 2023 12:20:49 +0100 Subject: [PATCH 138/342] Keep indentation on Enter in the web UI --- programs/server/play.html | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/programs/server/play.html b/programs/server/play.html index c511d13cf91..d90293fbd1b 100644 --- a/programs/server/play.html +++ b/programs/server/play.html @@ -669,6 +669,29 @@ elem.selectionStart = selection_start + 4; elem.selectionEnd = selection_start + 4; + e.preventDefault(); + return false; + } else if (e.key === 'Enter') { + // If the user presses Enter, and the previous line starts with spaces, + // then we will insert the same number of spaces. + const elem = e.target; + if (elem.selectionStart !== elem.selectionEnd) { + // If there is a selection, then we will not insert spaces. + return; + } + const cursor_pos = elem.selectionStart; + + const elem_value = elem.value; + const text_before_cursor = elem_value.substring(0, cursor_pos); + const text_after_cursor = elem_value.substring(cursor_pos); + const prev_lines = text_before_cursor.split('\n'); + const prev_line = prev_lines.pop(); + const lead_spaces = prev_line.match(/^\s*/)[0]; + // Add leading spaces to the current line. + elem.value = text_before_cursor + '\n' + lead_spaces + text_after_cursor; + elem.selectionStart = cursor_pos + lead_spaces.length + 1; + elem.selectionEnd = elem.selectionStart; + e.preventDefault(); return false; } From 43dbd7bc2522ca174ef5065550e5a8031390af94 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Wed, 1 Mar 2023 12:37:34 +0100 Subject: [PATCH 139/342] Terminate long-running offline non-busy runners in EC2 --- tests/ci/ci_runners_metrics_lambda/app.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/ci/ci_runners_metrics_lambda/app.py b/tests/ci/ci_runners_metrics_lambda/app.py index 44cae9221f5..341e1b674ec 100644 --- a/tests/ci/ci_runners_metrics_lambda/app.py +++ b/tests/ci/ci_runners_metrics_lambda/app.py @@ -37,15 +37,17 @@ RunnerDescriptions = List[RunnerDescription] def get_dead_runners_in_ec2(runners: RunnerDescriptions) -> RunnerDescriptions: + """Returns instances that are offline/dead in EC2, or not found in EC2""" ids = { runner.name: runner for runner in runners # Only `i-deadbead123` are valid names for an instance ID - if runner.offline and not runner.busy and runner.name.startswith("i-") + if runner.name.startswith("i-") and runner.offline and not runner.busy } if not ids: return [] + # Delete all offline runners with wrong name result_to_delete = [ runner for runner in runners @@ -57,7 +59,7 @@ def get_dead_runners_in_ec2(runners: RunnerDescriptions) -> RunnerDescriptions: i = 0 inc = 100 - print("Checking ids", ids.keys()) + print("Checking ids: ", " ".join(ids.keys())) instances_statuses = [] while i < len(ids.keys()): try: @@ -106,6 +108,9 @@ def get_lost_ec2_instances(runners: RunnerDescriptions) -> List[dict]: Filters=[{"Name": "tag-key", "Values": ["github:runner-type"]}] )["Reservations"] lost_instances = [] + offline_runners = [ + runner.name for runner in runners if runner.offline and not runner.busy + ] # Here we refresh the runners to get the most recent state now = datetime.now().timestamp() @@ -126,6 +131,10 @@ def get_lost_ec2_instances(runners: RunnerDescriptions) -> List[dict]: ): continue + if instance["InstanceId"] in offline_runners: + lost_instances.append(instance) + continue + if instance["State"]["Name"] == "running" and ( not [ runner From f2e1d6d4025a3dcabf0f18828803d31252fc1bd9 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Wed, 1 Mar 2023 13:11:32 +0000 Subject: [PATCH 140/342] Update version_date.tsv and changelogs after v23.2.2.20-stable --- docker/keeper/Dockerfile | 2 +- docker/server/Dockerfile.alpine | 2 +- docker/server/Dockerfile.ubuntu | 2 +- docs/changelogs/v23.2.2.20-stable.md | 30 ++++++++++++++++++++++++++++ utils/list-versions/version_date.tsv | 3 +++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 docs/changelogs/v23.2.2.20-stable.md diff --git a/docker/keeper/Dockerfile b/docker/keeper/Dockerfile index 09395befdad..532f1531bb8 100644 --- a/docker/keeper/Dockerfile +++ b/docker/keeper/Dockerfile @@ -29,7 +29,7 @@ RUN arch=${TARGETARCH:-amd64} \ esac ARG REPOSITORY="https://s3.amazonaws.com/clickhouse-builds/22.4/31c367d3cd3aefd316778601ff6565119fe36682/package_release" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.2.20" ARG PACKAGES="clickhouse-keeper" # user/group precreated explicitly with fixed uid/gid on purpose. diff --git a/docker/server/Dockerfile.alpine b/docker/server/Dockerfile.alpine index 472f25eed2d..f56e177bbe9 100644 --- a/docker/server/Dockerfile.alpine +++ b/docker/server/Dockerfile.alpine @@ -33,7 +33,7 @@ RUN arch=${TARGETARCH:-amd64} \ # lts / testing / prestable / etc ARG REPO_CHANNEL="stable" ARG REPOSITORY="https://packages.clickhouse.com/tgz/${REPO_CHANNEL}" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.2.20" ARG PACKAGES="clickhouse-client clickhouse-server clickhouse-common-static" # user/group precreated explicitly with fixed uid/gid on purpose. diff --git a/docker/server/Dockerfile.ubuntu b/docker/server/Dockerfile.ubuntu index 5dbb244c298..1372288978d 100644 --- a/docker/server/Dockerfile.ubuntu +++ b/docker/server/Dockerfile.ubuntu @@ -22,7 +22,7 @@ RUN sed -i "s|http://archive.ubuntu.com|${apt_archive}|g" /etc/apt/sources.list ARG REPO_CHANNEL="stable" ARG REPOSITORY="deb https://packages.clickhouse.com/deb ${REPO_CHANNEL} main" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.2.20" ARG PACKAGES="clickhouse-client clickhouse-server clickhouse-common-static" # set non-empty deb_location_url url to create a docker image diff --git a/docs/changelogs/v23.2.2.20-stable.md b/docs/changelogs/v23.2.2.20-stable.md new file mode 100644 index 00000000000..60aeaa66cbf --- /dev/null +++ b/docs/changelogs/v23.2.2.20-stable.md @@ -0,0 +1,30 @@ +--- +sidebar_position: 1 +sidebar_label: 2023 +--- + +# 2023 Changelog + +### ClickHouse release v23.2.2.20-stable (f6c269c8df2) FIXME as compared to v23.2.1.2537-stable (52bf836e03a) + +#### Improvement +* Backported in [#46914](https://github.com/ClickHouse/ClickHouse/issues/46914): Allow PREWHERE for Merge with different DEFAULT expression for column. [#46831](https://github.com/ClickHouse/ClickHouse/pull/46831) ([Azat Khuzhin](https://github.com/azat)). +* Backported in [#47022](https://github.com/ClickHouse/ClickHouse/issues/47022): Allow IPv4 in range(). [#46995](https://github.com/ClickHouse/ClickHouse/pull/46995) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). + +#### Bug Fix +* Backported in [#46828](https://github.com/ClickHouse/ClickHouse/issues/46828): Combined PREWHERE column accumulated from multiple PREWHERE in some cases didn't contain 0's from previous steps. The fix is to apply final filter if we know that it wasn't applied from more than 1 last step. [#46785](https://github.com/ClickHouse/ClickHouse/pull/46785) ([Alexander Gololobov](https://github.com/davenger)). + +#### Build/Testing/Packaging Improvement +* Backported in [#47062](https://github.com/ClickHouse/ClickHouse/issues/47062): Fix error during server startup on old distros (e.g. Amazon Linux 2) and on ARM that glibc 2.28 symbols are not found. [#47008](https://github.com/ClickHouse/ClickHouse/pull/47008) ([Robert Schulze](https://github.com/rschu1ze)). + +#### Bug Fix (user-visible misbehavior in official stable or prestable release) + +* Backported in [#46895](https://github.com/ClickHouse/ClickHouse/issues/46895): Fixed a bug in automatic retries of `DROP TABLE` query with `ReplicatedMergeTree` tables and `Atomic` databases. In rare cases it could lead to `Can't get data for node /zk_path/log_pointer` and `The specified key does not exist` errors if ZooKeeper session expired during DROP and a new replicated table with the same path in ZooKeeper was created in parallel. [#46384](https://github.com/ClickHouse/ClickHouse/pull/46384) ([Alexander Tokmakov](https://github.com/tavplubix)). +* Backported in [#46865](https://github.com/ClickHouse/ClickHouse/issues/46865): Fix a bug in the `Map` data type. This closes [#46855](https://github.com/ClickHouse/ClickHouse/issues/46855). [#46856](https://github.com/ClickHouse/ClickHouse/pull/46856) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46958](https://github.com/ClickHouse/ClickHouse/issues/46958): Fix result of LIKE predicates which translate to substring searches and contain quoted non-LIKE metacharacters. [#46875](https://github.com/ClickHouse/ClickHouse/pull/46875) ([Robert Schulze](https://github.com/rschu1ze)). + +#### NOT FOR CHANGELOG / INSIGNIFICANT + +* More concise logging at trace level for PREWHERE steps [#46771](https://github.com/ClickHouse/ClickHouse/pull/46771) ([Alexander Gololobov](https://github.com/davenger)). +* Reduce updates of Mergeable Check [#46781](https://github.com/ClickHouse/ClickHouse/pull/46781) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + diff --git a/utils/list-versions/version_date.tsv b/utils/list-versions/version_date.tsv index 3814e94bf24..8a25ceb13cf 100644 --- a/utils/list-versions/version_date.tsv +++ b/utils/list-versions/version_date.tsv @@ -1,4 +1,6 @@ +v23.2.2.20-stable 2023-03-01 v23.2.1.2537-stable 2023-02-23 +v23.1.4.58-stable 2023-03-01 v23.1.3.5-stable 2023-02-03 v23.1.2.9-stable 2023-01-29 v23.1.1.3077-stable 2023-01-25 @@ -25,6 +27,7 @@ v22.9.4.32-stable 2022-10-26 v22.9.3.18-stable 2022-09-30 v22.9.2.7-stable 2022-09-23 v22.9.1.2603-stable 2022-09-22 +v22.8.14.53-lts 2023-02-27 v22.8.13.20-lts 2023-01-29 v22.8.12.45-lts 2023-01-10 v22.8.11.15-lts 2022-12-08 From a898dd556fb475020a6a7fcb1fdf58ac43d826f2 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Wed, 1 Mar 2023 13:12:22 +0000 Subject: [PATCH 141/342] Update version_date.tsv and changelogs after v23.1.4.58-stable --- docs/changelogs/v23.1.4.58-stable.md | 47 ++++++++++++++++++++++++++++ utils/list-versions/version_date.tsv | 2 ++ 2 files changed, 49 insertions(+) create mode 100644 docs/changelogs/v23.1.4.58-stable.md diff --git a/docs/changelogs/v23.1.4.58-stable.md b/docs/changelogs/v23.1.4.58-stable.md new file mode 100644 index 00000000000..d1ffe87f58e --- /dev/null +++ b/docs/changelogs/v23.1.4.58-stable.md @@ -0,0 +1,47 @@ +--- +sidebar_position: 1 +sidebar_label: 2023 +--- + +# 2023 Changelog + +### ClickHouse release v23.1.4.58-stable (9ed562163a5) FIXME as compared to v23.1.3.5-stable (548b494bcce) + +#### Performance Improvement +* Backported in [#46380](https://github.com/ClickHouse/ClickHouse/issues/46380): Fix too big memory usage for vertical merges on non-remote disk. Respect `max_insert_delayed_streams_for_parallel_write` for the remote disk. [#46275](https://github.com/ClickHouse/ClickHouse/pull/46275) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). + +#### Improvement +* Backported in [#46985](https://github.com/ClickHouse/ClickHouse/issues/46985): - Apply `ALTER TABLE table_name ON CLUSTER cluster MOVE PARTITION|PART partition_expr TO DISK|VOLUME 'disk_name'` to all replicas. Because `ALTER TABLE t MOVE` is not replicated. [#46402](https://github.com/ClickHouse/ClickHouse/pull/46402) ([lizhuoyu5](https://github.com/lzydmxy)). +* Backported in [#46778](https://github.com/ClickHouse/ClickHouse/issues/46778): Backward compatibility for T64 codec support for IPv4. [#46747](https://github.com/ClickHouse/ClickHouse/pull/46747) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). +* Backported in [#47020](https://github.com/ClickHouse/ClickHouse/issues/47020): Allow IPv4 in range(). [#46995](https://github.com/ClickHouse/ClickHouse/pull/46995) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). + +#### Build/Testing/Packaging Improvement +* Backported in [#46031](https://github.com/ClickHouse/ClickHouse/issues/46031): Add systemd.service file for clickhouse-keeper. Fixes [#44293](https://github.com/ClickHouse/ClickHouse/issues/44293). [#45568](https://github.com/ClickHouse/ClickHouse/pull/45568) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#46477](https://github.com/ClickHouse/ClickHouse/issues/46477): Get rid of unnecessary build for standalone clickhouse-keeper. [#46367](https://github.com/ClickHouse/ClickHouse/pull/46367) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#46511](https://github.com/ClickHouse/ClickHouse/issues/46511): Some time ago the ccache compression was changed to `zst`, but `gz` archives are downloaded by default. It fixes it by prioritizing zst archive. [#46490](https://github.com/ClickHouse/ClickHouse/pull/46490) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + +#### Bug Fix (user-visible misbehavior in official stable or prestable release) + +* Backported in [#46228](https://github.com/ClickHouse/ClickHouse/issues/46228): A couple of seg faults have been reported around `c-ares`. All of the recent stack traces observed fail on inserting into `std::unodered_set<>`. I believe I have found the root cause of this, it seems to be unprocessed queries. Prior to this PR, CH calls `poll` to wait on the file descriptors in the `c-ares` channel. According to the [poll docs](https://man7.org/linux/man-pages/man2/poll.2.html), a negative return value means an error has ocurred. Because of this, we would abort the execution and return failure. The problem is that `poll` will also return a negative value if a system interrupt occurs. A system interrupt does not mean the processing has failed or ended, but we would abort it anyways because we were checking for negative values. Once the execution is aborted, the whole stack is destroyed, which includes the `std::unordered_set` passed to the `void *` parameter of the c-ares callback. Once c-ares completed the request, the callback would be invoked and would access an invalid memory address causing a segfault. [#45629](https://github.com/ClickHouse/ClickHouse/pull/45629) ([Arthur Passos](https://github.com/arthurpassos)). +* Backported in [#46967](https://github.com/ClickHouse/ClickHouse/issues/46967): Backward compatibility - allow implicit narrowing conversion from UInt64 to IPv4 - required for "INSERT ... VALUES ..." expression. [#45865](https://github.com/ClickHouse/ClickHouse/pull/45865) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). +* Backported in [#46220](https://github.com/ClickHouse/ClickHouse/issues/46220): Fix reading of non existing nested columns with multiple level in compact parts. [#46045](https://github.com/ClickHouse/ClickHouse/pull/46045) ([Azat Khuzhin](https://github.com/azat)). +* Backported in [#46751](https://github.com/ClickHouse/ClickHouse/issues/46751): Follow-up fix for Replace domain IP types (IPv4, IPv6) with native https://github.com/ClickHouse/ClickHouse/pull/43221. [#46087](https://github.com/ClickHouse/ClickHouse/pull/46087) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). +* Backported in [#46448](https://github.com/ClickHouse/ClickHouse/issues/46448): Fix possible `LOGICAL_ERROR` in asynchronous inserts with invalid data sent in format `VALUES`. [#46350](https://github.com/ClickHouse/ClickHouse/pull/46350) ([Anton Popov](https://github.com/CurtizJ)). +* Backported in [#46680](https://github.com/ClickHouse/ClickHouse/issues/46680): Fix an invalid processing of constant `LowCardinality` argument in function `arrayMap`. This bug could lead to a segfault in release, and logical error `Bad cast` in debug build. [#46569](https://github.com/ClickHouse/ClickHouse/pull/46569) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46873](https://github.com/ClickHouse/ClickHouse/issues/46873): Fix a bug in the `Map` data type. This closes [#46855](https://github.com/ClickHouse/ClickHouse/issues/46855). [#46856](https://github.com/ClickHouse/ClickHouse/pull/46856) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46956](https://github.com/ClickHouse/ClickHouse/issues/46956): Fix result of LIKE predicates which translate to substring searches and contain quoted non-LIKE metacharacters. [#46875](https://github.com/ClickHouse/ClickHouse/pull/46875) ([Robert Schulze](https://github.com/rschu1ze)). + +#### NOT FOR CHANGELOG / INSIGNIFICANT + +* Another attempt to fix automerge, or at least to have debug footprint [#45476](https://github.com/ClickHouse/ClickHouse/pull/45476) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Support DELETE ON CLUSTER [#45786](https://github.com/ClickHouse/ClickHouse/pull/45786) ([Alexander Gololobov](https://github.com/davenger)). +* Add check for running workflows to merge_pr.py [#45803](https://github.com/ClickHouse/ClickHouse/pull/45803) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add necessary dependency for sanitizers [#45959](https://github.com/ClickHouse/ClickHouse/pull/45959) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add helping logging to auto-merge script [#46080](https://github.com/ClickHouse/ClickHouse/pull/46080) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Fix write buffer destruction order for vertical merge. [#46205](https://github.com/ClickHouse/ClickHouse/pull/46205) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). +* Wait for background tasks in ~UploadHelper [#46334](https://github.com/ClickHouse/ClickHouse/pull/46334) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). +* Improve install_check.py [#46458](https://github.com/ClickHouse/ClickHouse/pull/46458) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Fix dependencies for InstallPackagesTestAarch64 [#46597](https://github.com/ClickHouse/ClickHouse/pull/46597) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Get rid of legacy DocsReleaseChecks [#46665](https://github.com/ClickHouse/ClickHouse/pull/46665) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Reduce updates of Mergeable Check [#46781](https://github.com/ClickHouse/ClickHouse/pull/46781) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + diff --git a/utils/list-versions/version_date.tsv b/utils/list-versions/version_date.tsv index 3814e94bf24..6cae29d53a8 100644 --- a/utils/list-versions/version_date.tsv +++ b/utils/list-versions/version_date.tsv @@ -1,4 +1,5 @@ v23.2.1.2537-stable 2023-02-23 +v23.1.4.58-stable 2023-03-01 v23.1.3.5-stable 2023-02-03 v23.1.2.9-stable 2023-01-29 v23.1.1.3077-stable 2023-01-25 @@ -25,6 +26,7 @@ v22.9.4.32-stable 2022-10-26 v22.9.3.18-stable 2022-09-30 v22.9.2.7-stable 2022-09-23 v22.9.1.2603-stable 2022-09-22 +v22.8.14.53-lts 2023-02-27 v22.8.13.20-lts 2023-01-29 v22.8.12.45-lts 2023-01-10 v22.8.11.15-lts 2022-12-08 From e8847f2fb6be0c951cc4e7a13cec580c8da88989 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Wed, 1 Mar 2023 21:15:01 +0800 Subject: [PATCH 142/342] change as request --- .../functions/tuple-map-functions.md | 64 ++++++++++--------- src/Functions/map.cpp | 51 ++++++--------- .../0_stateless/01651_map_functions.reference | 2 + .../0_stateless/01651_map_functions.sql | 6 +- 4 files changed, 59 insertions(+), 64 deletions(-) diff --git a/docs/en/sql-reference/functions/tuple-map-functions.md b/docs/en/sql-reference/functions/tuple-map-functions.md index d59204a7482..28f2f930d90 100644 --- a/docs/en/sql-reference/functions/tuple-map-functions.md +++ b/docs/en/sql-reference/functions/tuple-map-functions.md @@ -66,6 +66,40 @@ Result: - [Map(key, value)](../../sql-reference/data-types/map.md) data type +## mapFromArrays + +mapFromArrays merges an array of keys and an array of values into a Map. + +**Syntax** + +```sql +mapFromArrays(keys, values) +``` + +Alias: `MAP_FROM_ARRAYS(keys, values)` + +**Parameters** +- `keys` — Given key array to create a map from. The nested type of array must be: [String](../../sql-reference/data-types/string.md), [Integer](../../sql-reference/data-types/int-uint.md), [LowCardinality](../../sql-reference/data-types/lowcardinality.md), [FixedString](../../sql-reference/data-types/fixedstring.md), [UUID](../../sql-reference/data-types/uuid.md), [Date](../../sql-reference/data-types/date.md), [DateTime](../../sql-reference/data-types/datetime.md), [Date32](../../sql-reference/data-types/date32.md), [Enum](../../sql-reference/data-types/enum.md) +- `values` - Given value array to create a map from. + +**Returned value** + +- A map whose keys and values are constructed from the key and value arrays + +**Example** + +Query: + +```sql +select mapFromArrays(['a', 'b', 'c'], [1, 2, 3]) +``` + +```text +┌─mapFromArrays(['a', 'b', 'c'], [1, 2, 3])─┐ +│ {'a':1,'b':2,'c':3} │ +└───────────────────────────────────────────┘ +``` + ## mapAdd Collect all the keys and sum corresponding values. @@ -431,36 +465,6 @@ Result: ``` -## mapFromArrays {#mapFromArrays} - -**Syntax** - -```sql -mapFromArrays(keys, values) -``` - -**Parameters** - -- `keys` — Given key array to create a map from. -- `values` - Given value array to create a map from. - -**Returned value** - -- A map whose keys and values are from input arrays respectively. - -**Example** - -Query: - -```sql -select mapFromArrays(['a', 'b', 'c'], [1, 2, 3]) -``` - -```text -┌─mapFromArrays(['a', 'b', 'c'], [1, 2, 3])─┐ -│ {'a':1,'b':2,'c':3} │ -└───────────────────────────────────────────┘ -``` ## mapApply diff --git a/src/Functions/map.cpp b/src/Functions/map.cpp index 78985bd18fc..549de200bea 100644 --- a/src/Functions/map.cpp +++ b/src/Functions/map.cpp @@ -26,7 +26,7 @@ namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; extern const int ILLEGAL_TYPE_OF_ARGUMENT; - extern const int SIZES_OF_ARRAYS_DOESNT_MATCH; + extern const int SIZES_OF_ARRAYS_DONT_MATCH; extern const int ILLEGAL_COLUMN; } @@ -149,26 +149,16 @@ public: } }; -// mapFromArrays(keys, values) is a function that allows you to make key-value pair from a pair of arrays +/// mapFromArrays(keys, values) is a function that allows you to make key-value pair from a pair of arrays class FunctionMapFromArrays : public IFunction { public: static constexpr auto name = "mapFromArrays"; - static FunctionPtr create(ContextPtr) - { - return std::make_shared(); - } + static FunctionPtr create(ContextPtr) { return std::make_shared(); } + String getName() const override { return name; } - String getName() const override - { - return name; - } - - size_t getNumberOfArguments() const override - { - return 2; - } + size_t getNumberOfArguments() const override { return 2; } bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } bool useDefaultImplementationForNulls() const override { return false; } @@ -177,25 +167,22 @@ public: DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override { if (arguments.size() != 2) - throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, - "Function {} requires 2 arguments, but {} given", getName(), arguments.size()); + throw Exception( + ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, + "Function {} requires 2 arguments, but {} given", + getName(), + arguments.size()); const auto * keys_type = checkAndGetDataType(arguments[0].get()); if (!keys_type) - throw Exception{"First argument for function " + getName() + " must be a Array", - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT}; + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "First argument for function {} must be an Array", getName()); const auto * values_type = checkAndGetDataType(arguments[1].get()); if (!values_type) - throw Exception{"Second argument for function " + getName() + " must be a Array", - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT}; + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Second argument for function {} must be an Array", getName()); - DataTypes tmp; - const auto & key_type = keys_type->getNestedType(); - const auto & value_type = values_type->getNestedType(); - tmp.emplace_back(key_type); - tmp.emplace_back(value_type); - return std::make_shared(tmp); + DataTypes key_value_types{keys_type->getNestedType(), values_type->getNestedType()}; + return std::make_shared(key_value_types); } ColumnPtr executeImpl( @@ -228,17 +215,15 @@ public: } if (!col_keys || !col_values) - throw Exception("Arguments of function " + getName() + " must be array.", ErrorCodes::ILLEGAL_COLUMN); + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Arguments of function {} must be array", getName()); if (!col_keys->hasEqualOffsets(*col_values)) - throw Exception("Array arguments for function " + getName() + " must have equal sizes", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH); + throw Exception(ErrorCodes::SIZES_OF_ARRAYS_DONT_MATCH, "Array arguments for function {} must have equal sizes", getName()); const auto & data_keys = col_keys->getDataPtr(); const auto & data_values = col_values->getDataPtr(); const auto & offsets = col_keys->getOffsetsPtr(); - auto nested_column = ColumnArray::create( - ColumnTuple::create(Columns{std::move(data_keys), std::move(data_values)}), - std::move(offsets)); + auto nested_column = ColumnArray::create(ColumnTuple::create(Columns{data_keys, data_values}), offsets); return ColumnMap::create(nested_column); } }; @@ -745,6 +730,8 @@ REGISTER_FUNCTION(Map) factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); + factory.registerAlias("MAP_FROM_ARRAYS", "mapFromArrays"); + } } diff --git a/tests/queries/0_stateless/01651_map_functions.reference b/tests/queries/0_stateless/01651_map_functions.reference index 284e27a1b0d..f7fd3503327 100644 --- a/tests/queries/0_stateless/01651_map_functions.reference +++ b/tests/queries/0_stateless/01651_map_functions.reference @@ -31,3 +31,5 @@ {'aa':4,'bb':5} {'aa':4,'bb':5} {'aa':4,'bb':5} +{'aa':4,'bb':5} +{'aa':4,'bb':5} diff --git a/tests/queries/0_stateless/01651_map_functions.sql b/tests/queries/0_stateless/01651_map_functions.sql index 57023d41470..4d2bb3a0812 100644 --- a/tests/queries/0_stateless/01651_map_functions.sql +++ b/tests/queries/0_stateless/01651_map_functions.sql @@ -31,7 +31,9 @@ select map( 'aa', 4, 'bb' , 5) as m, mapContains(m, 'aa'), mapContains(m, 'k'); select map(0, 0) as m, mapContains(m, number % 2) from numbers(2); select mapFromArrays(['aa', 'bb'], [4, 5]); -select mapFromArrays(['aa', 'bb'], materialize([4, 5])); -select mapFromArrays(['aa', 'bb'], materialize([4, 5])) from numbers(3); +select mapFromArrays(['aa', 'bb'], materialize([4, 5])) from numbers(2); +select mapFromArrays(materialize(['aa', 'bb']), [4, 5]) from numbers(2); +select mapFromArrays(materialize(['aa', 'bb']), materialize([4, 5])) from numbers(2); select mapFromArrays(['aa', 'bb'], [4, 5], [6, 7]); -- { serverError 42 } select mapFromArrays(['aa', 'bb'], [4, 5, 6]); -- { serverError 190 } +select mapFromArrays([[1,2], [3,4]], [4, 5, 6]); -- { serverError 36 } From 598efe19858a0ca3aee6be015a99ebdf92c5ca67 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Wed, 1 Mar 2023 21:17:05 +0800 Subject: [PATCH 143/342] change as request --- tests/queries/0_stateless/01651_map_functions.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/queries/0_stateless/01651_map_functions.sql b/tests/queries/0_stateless/01651_map_functions.sql index 4d2bb3a0812..f09739cc0d1 100644 --- a/tests/queries/0_stateless/01651_map_functions.sql +++ b/tests/queries/0_stateless/01651_map_functions.sql @@ -34,6 +34,8 @@ select mapFromArrays(['aa', 'bb'], [4, 5]); select mapFromArrays(['aa', 'bb'], materialize([4, 5])) from numbers(2); select mapFromArrays(materialize(['aa', 'bb']), [4, 5]) from numbers(2); select mapFromArrays(materialize(['aa', 'bb']), materialize([4, 5])) from numbers(2); +select mapFromArrays('aa', [4, 5]); -- { serverError 43 } +select mapFromArrays(['aa', 'bb'], 5); -- { serverError 43 } select mapFromArrays(['aa', 'bb'], [4, 5], [6, 7]); -- { serverError 42 } select mapFromArrays(['aa', 'bb'], [4, 5, 6]); -- { serverError 190 } select mapFromArrays([[1,2], [3,4]], [4, 5, 6]); -- { serverError 36 } From 119ea045acf5edffb8e141b6abd285a8bfc5cdb8 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Wed, 1 Mar 2023 21:19:39 +0800 Subject: [PATCH 144/342] change as request --- .../queries/0_stateless/01651_map_functions.sql | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/queries/0_stateless/01651_map_functions.sql b/tests/queries/0_stateless/01651_map_functions.sql index f09739cc0d1..848b6932fe0 100644 --- a/tests/queries/0_stateless/01651_map_functions.sql +++ b/tests/queries/0_stateless/01651_map_functions.sql @@ -8,7 +8,7 @@ select mapContains(a, 'name') from table_map; select mapContains(a, 'gender') from table_map; select mapContains(a, 'abc') from table_map; select mapContains(a, b) from table_map; -select mapContains(a, 10) from table_map; -- { serverError 386 } +select mapContains(a, 10) from table_map; -- { serverError NO_COMMON_TYPE } select mapKeys(a) from table_map; select mapFromArrays(c, d) from table_map; drop table if exists table_map; @@ -16,8 +16,8 @@ drop table if exists table_map; CREATE TABLE table_map (a Map(UInt8, Int), b UInt8, c UInt32, d Array(String), e Array(String)) engine = MergeTree order by tuple(); insert into table_map select map(number, number), number, number, [number, number, number], [number*2, number*3, number*4] from numbers(1000, 3); select mapContains(a, b), mapContains(a, c), mapContains(a, 233) from table_map; -select mapContains(a, 'aaa') from table_map; -- { serverError 386 } -select mapContains(b, 'aaa') from table_map; -- { serverError 43 } +select mapContains(a, 'aaa') from table_map; -- { serverError NO_COMMON_TYPE } +select mapContains(b, 'aaa') from table_map; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } select mapKeys(a) from table_map; select mapValues(a) from table_map; select mapFromArrays(d, e) from table_map; @@ -34,8 +34,8 @@ select mapFromArrays(['aa', 'bb'], [4, 5]); select mapFromArrays(['aa', 'bb'], materialize([4, 5])) from numbers(2); select mapFromArrays(materialize(['aa', 'bb']), [4, 5]) from numbers(2); select mapFromArrays(materialize(['aa', 'bb']), materialize([4, 5])) from numbers(2); -select mapFromArrays('aa', [4, 5]); -- { serverError 43 } -select mapFromArrays(['aa', 'bb'], 5); -- { serverError 43 } -select mapFromArrays(['aa', 'bb'], [4, 5], [6, 7]); -- { serverError 42 } -select mapFromArrays(['aa', 'bb'], [4, 5, 6]); -- { serverError 190 } -select mapFromArrays([[1,2], [3,4]], [4, 5, 6]); -- { serverError 36 } +select mapFromArrays('aa', [4, 5]); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +select mapFromArrays(['aa', 'bb'], 5); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +select mapFromArrays(['aa', 'bb'], [4, 5], [6, 7]); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +select mapFromArrays(['aa', 'bb'], [4, 5, 6]); -- { serverError SIZES_OF_ARRAYS_DONT_MATCH } +select mapFromArrays([[1,2], [3,4]], [4, 5, 6]); -- { serverError BAD_ARGUMENTS } From 50d033c563c58d5925a19eff01464616fe16ea7f Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Wed, 1 Mar 2023 13:48:23 +0000 Subject: [PATCH 145/342] Update version_date.tsv and changelogs after v22.12.4.76-stable --- docker/keeper/Dockerfile | 2 +- docker/server/Dockerfile.alpine | 2 +- docker/server/Dockerfile.ubuntu | 2 +- docs/changelogs/v22.12.4.76-stable.md | 55 +++++++++++++++++++++++++++ utils/list-versions/version_date.tsv | 4 ++ 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 docs/changelogs/v22.12.4.76-stable.md diff --git a/docker/keeper/Dockerfile b/docker/keeper/Dockerfile index 09395befdad..532f1531bb8 100644 --- a/docker/keeper/Dockerfile +++ b/docker/keeper/Dockerfile @@ -29,7 +29,7 @@ RUN arch=${TARGETARCH:-amd64} \ esac ARG REPOSITORY="https://s3.amazonaws.com/clickhouse-builds/22.4/31c367d3cd3aefd316778601ff6565119fe36682/package_release" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.2.20" ARG PACKAGES="clickhouse-keeper" # user/group precreated explicitly with fixed uid/gid on purpose. diff --git a/docker/server/Dockerfile.alpine b/docker/server/Dockerfile.alpine index 472f25eed2d..f56e177bbe9 100644 --- a/docker/server/Dockerfile.alpine +++ b/docker/server/Dockerfile.alpine @@ -33,7 +33,7 @@ RUN arch=${TARGETARCH:-amd64} \ # lts / testing / prestable / etc ARG REPO_CHANNEL="stable" ARG REPOSITORY="https://packages.clickhouse.com/tgz/${REPO_CHANNEL}" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.2.20" ARG PACKAGES="clickhouse-client clickhouse-server clickhouse-common-static" # user/group precreated explicitly with fixed uid/gid on purpose. diff --git a/docker/server/Dockerfile.ubuntu b/docker/server/Dockerfile.ubuntu index 5dbb244c298..1372288978d 100644 --- a/docker/server/Dockerfile.ubuntu +++ b/docker/server/Dockerfile.ubuntu @@ -22,7 +22,7 @@ RUN sed -i "s|http://archive.ubuntu.com|${apt_archive}|g" /etc/apt/sources.list ARG REPO_CHANNEL="stable" ARG REPOSITORY="deb https://packages.clickhouse.com/deb ${REPO_CHANNEL} main" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.2.20" ARG PACKAGES="clickhouse-client clickhouse-server clickhouse-common-static" # set non-empty deb_location_url url to create a docker image diff --git a/docs/changelogs/v22.12.4.76-stable.md b/docs/changelogs/v22.12.4.76-stable.md new file mode 100644 index 00000000000..79569ff841e --- /dev/null +++ b/docs/changelogs/v22.12.4.76-stable.md @@ -0,0 +1,55 @@ +--- +sidebar_position: 1 +sidebar_label: 2023 +--- + +# 2023 Changelog + +### ClickHouse release v22.12.4.76-stable (cb5772db805) FIXME as compared to v22.12.3.5-stable (893de538f02) + +#### Performance Improvement +* Backported in [#45704](https://github.com/ClickHouse/ClickHouse/issues/45704): Fixed performance of short `SELECT` queries that read from tables with large number of`Array`/`Map`/`Nested` columns. [#45630](https://github.com/ClickHouse/ClickHouse/pull/45630) ([Anton Popov](https://github.com/CurtizJ)). +* Backported in [#46378](https://github.com/ClickHouse/ClickHouse/issues/46378): Fix too big memory usage for vertical merges on non-remote disk. Respect `max_insert_delayed_streams_for_parallel_write` for the remote disk. [#46275](https://github.com/ClickHouse/ClickHouse/pull/46275) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). + +#### Bug Fix +* Backported in [#45672](https://github.com/ClickHouse/ClickHouse/issues/45672): Fix wiping sensitive info in logs. [#45603](https://github.com/ClickHouse/ClickHouse/pull/45603) ([Vitaly Baranov](https://github.com/vitlibar)). + +#### Build/Testing/Packaging Improvement +* Backported in [#45200](https://github.com/ClickHouse/ClickHouse/issues/45200): Fix zookeeper downloading, update the version, and optimize the image size. [#44853](https://github.com/ClickHouse/ClickHouse/pull/44853) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#46116](https://github.com/ClickHouse/ClickHouse/issues/46116): Remove the dependency on the `adduser` tool from the packages, because we don't use it. This fixes [#44934](https://github.com/ClickHouse/ClickHouse/issues/44934). [#45011](https://github.com/ClickHouse/ClickHouse/pull/45011) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46035](https://github.com/ClickHouse/ClickHouse/issues/46035): Add systemd.service file for clickhouse-keeper. Fixes [#44293](https://github.com/ClickHouse/ClickHouse/issues/44293). [#45568](https://github.com/ClickHouse/ClickHouse/pull/45568) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#46484](https://github.com/ClickHouse/ClickHouse/issues/46484): Get rid of unnecessary build for standalone clickhouse-keeper. [#46367](https://github.com/ClickHouse/ClickHouse/pull/46367) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#46509](https://github.com/ClickHouse/ClickHouse/issues/46509): Some time ago the ccache compression was changed to `zst`, but `gz` archives are downloaded by default. It fixes it by prioritizing zst archive. [#46490](https://github.com/ClickHouse/ClickHouse/pull/46490) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Backported in [#47058](https://github.com/ClickHouse/ClickHouse/issues/47058): Fix error during server startup on old distros (e.g. Amazon Linux 2) and on ARM that glibc 2.28 symbols are not found. [#47008](https://github.com/ClickHouse/ClickHouse/pull/47008) ([Robert Schulze](https://github.com/rschu1ze)). + +#### Bug Fix (user-visible misbehavior in official stable or prestable release) + +* Backported in [#45904](https://github.com/ClickHouse/ClickHouse/issues/45904): Fixed bug with non-parsable default value for EPHEMERAL column in table metadata. [#44026](https://github.com/ClickHouse/ClickHouse/pull/44026) ([Yakov Olkhovskiy](https://github.com/yakov-olkhovskiy)). +* Backported in [#45321](https://github.com/ClickHouse/ClickHouse/issues/45321): Fixed a bug in normalization of a `DEFAULT` expression in `CREATE TABLE` statement. The second argument of function `in` (or the right argument of operator `IN`) might be replaced with the result of its evaluation during CREATE query execution. Fixes [#44496](https://github.com/ClickHouse/ClickHouse/issues/44496). [#44547](https://github.com/ClickHouse/ClickHouse/pull/44547) ([Alexander Tokmakov](https://github.com/tavplubix)). +* Backported in [#45000](https://github.com/ClickHouse/ClickHouse/issues/45000): Another fix for `Cannot read all data` error which could happen while reading `LowCardinality` dictionary from remote fs. Fixes [#44709](https://github.com/ClickHouse/ClickHouse/issues/44709). [#44875](https://github.com/ClickHouse/ClickHouse/pull/44875) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). +* Backported in [#45553](https://github.com/ClickHouse/ClickHouse/issues/45553): Fix `SELECT ... FROM system.dictionaries` exception when there is a dictionary with a bad structure (e.g. incorrect type in xml config). [#45399](https://github.com/ClickHouse/ClickHouse/pull/45399) ([Aleksei Filatov](https://github.com/aalexfvk)). +* Backported in [#46226](https://github.com/ClickHouse/ClickHouse/issues/46226): A couple of seg faults have been reported around `c-ares`. All of the recent stack traces observed fail on inserting into `std::unodered_set<>`. I believe I have found the root cause of this, it seems to be unprocessed queries. Prior to this PR, CH calls `poll` to wait on the file descriptors in the `c-ares` channel. According to the [poll docs](https://man7.org/linux/man-pages/man2/poll.2.html), a negative return value means an error has ocurred. Because of this, we would abort the execution and return failure. The problem is that `poll` will also return a negative value if a system interrupt occurs. A system interrupt does not mean the processing has failed or ended, but we would abort it anyways because we were checking for negative values. Once the execution is aborted, the whole stack is destroyed, which includes the `std::unordered_set` passed to the `void *` parameter of the c-ares callback. Once c-ares completed the request, the callback would be invoked and would access an invalid memory address causing a segfault. [#45629](https://github.com/ClickHouse/ClickHouse/pull/45629) ([Arthur Passos](https://github.com/arthurpassos)). +* Backported in [#46218](https://github.com/ClickHouse/ClickHouse/issues/46218): Fix reading of non existing nested columns with multiple level in compact parts. [#46045](https://github.com/ClickHouse/ClickHouse/pull/46045) ([Azat Khuzhin](https://github.com/azat)). +* Backported in [#46446](https://github.com/ClickHouse/ClickHouse/issues/46446): Fix possible `LOGICAL_ERROR` in asynchronous inserts with invalid data sent in format `VALUES`. [#46350](https://github.com/ClickHouse/ClickHouse/pull/46350) ([Anton Popov](https://github.com/CurtizJ)). +* Backported in [#46678](https://github.com/ClickHouse/ClickHouse/issues/46678): Fix an invalid processing of constant `LowCardinality` argument in function `arrayMap`. This bug could lead to a segfault in release, and logical error `Bad cast` in debug build. [#46569](https://github.com/ClickHouse/ClickHouse/pull/46569) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46872](https://github.com/ClickHouse/ClickHouse/issues/46872): Fix a bug in the `Map` data type. This closes [#46855](https://github.com/ClickHouse/ClickHouse/issues/46855). [#46856](https://github.com/ClickHouse/ClickHouse/pull/46856) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#46954](https://github.com/ClickHouse/ClickHouse/issues/46954): Fix result of LIKE predicates which translate to substring searches and contain quoted non-LIKE metacharacters. [#46875](https://github.com/ClickHouse/ClickHouse/pull/46875) ([Robert Schulze](https://github.com/rschu1ze)). + +#### NOT FOR CHANGELOG / INSIGNIFICANT + +* Automatically merge green backport PRs and green approved PRs [#41110](https://github.com/ClickHouse/ClickHouse/pull/41110) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Improve release scripts [#45074](https://github.com/ClickHouse/ClickHouse/pull/45074) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Fix wrong approved_at, simplify conditions [#45302](https://github.com/ClickHouse/ClickHouse/pull/45302) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Get rid of artifactory in favor of r2 + ch-repos-manager [#45421](https://github.com/ClickHouse/ClickHouse/pull/45421) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Another attempt to fix automerge, or at least to have debug footprint [#45476](https://github.com/ClickHouse/ClickHouse/pull/45476) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Trim refs/tags/ from GITHUB_TAG in release workflow [#45636](https://github.com/ClickHouse/ClickHouse/pull/45636) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add check for running workflows to merge_pr.py [#45803](https://github.com/ClickHouse/ClickHouse/pull/45803) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Get rid of progress timestamps in release publishing [#45818](https://github.com/ClickHouse/ClickHouse/pull/45818) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add necessary dependency for sanitizers [#45959](https://github.com/ClickHouse/ClickHouse/pull/45959) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Add helping logging to auto-merge script [#46080](https://github.com/ClickHouse/ClickHouse/pull/46080) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Fix write buffer destruction order for vertical merge. [#46205](https://github.com/ClickHouse/ClickHouse/pull/46205) ([Nikolai Kochetov](https://github.com/KochetovNicolai)). +* Improve install_check.py [#46458](https://github.com/ClickHouse/ClickHouse/pull/46458) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Fix dependencies for InstallPackagesTestAarch64 [#46597](https://github.com/ClickHouse/ClickHouse/pull/46597) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Get rid of legacy DocsReleaseChecks [#46665](https://github.com/ClickHouse/ClickHouse/pull/46665) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* Reduce updates of Mergeable Check [#46781](https://github.com/ClickHouse/ClickHouse/pull/46781) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + diff --git a/utils/list-versions/version_date.tsv b/utils/list-versions/version_date.tsv index 3814e94bf24..4cc74097371 100644 --- a/utils/list-versions/version_date.tsv +++ b/utils/list-versions/version_date.tsv @@ -1,7 +1,10 @@ +v23.2.2.20-stable 2023-03-01 v23.2.1.2537-stable 2023-02-23 +v23.1.4.58-stable 2023-03-01 v23.1.3.5-stable 2023-02-03 v23.1.2.9-stable 2023-01-29 v23.1.1.3077-stable 2023-01-25 +v22.12.4.76-stable 2023-03-01 v22.12.3.5-stable 2023-01-10 v22.12.2.25-stable 2023-01-06 v22.12.1.1752-stable 2022-12-15 @@ -25,6 +28,7 @@ v22.9.4.32-stable 2022-10-26 v22.9.3.18-stable 2022-09-30 v22.9.2.7-stable 2022-09-23 v22.9.1.2603-stable 2022-09-22 +v22.8.14.53-lts 2023-02-27 v22.8.13.20-lts 2023-01-29 v22.8.12.45-lts 2023-01-10 v22.8.11.15-lts 2022-12-08 From b11ed5910727f549a68d1a1dba8819505a8949f2 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Wed, 1 Mar 2023 15:37:03 +0100 Subject: [PATCH 146/342] remove ps --- .../0_stateless/02434_cancel_insert_when_client_dies.sh | 7 +++---- .../0_stateless/02435_rollback_cancelled_queries.sh | 9 ++++----- tests/queries/shell_config.sh | 3 ++- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh index 2a17095b267..c4d7367ba14 100755 --- a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh @@ -67,7 +67,7 @@ function thread_cancel if (( RANDOM % 2 )); then SIGNAL="KILL" fi - PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') + PID=$(grep -Fa "$TEST_MARK" /proc/*/cmdline | grep -Fav grep | grep -Eoa "/proc/[0-9]*/cmdline:" | grep -Eo "[0-9]*" | head -1) if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID"; fi sleep 0.$RANDOM; sleep 0.$RANDOM; @@ -91,8 +91,7 @@ $CLICKHOUSE_CLIENT -q 'select count() from dedup_test' $CLICKHOUSE_CLIENT -q 'system flush logs' -# We have to ignore stderr from thread_cancel, because our CI finds a bug in ps... -# So use this query to check that thread_cancel do something +# Ensure that thread_cancel actually did something $CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or - message like '%Connection reset by peer%')" + message like '%Connection reset by peer%' or message like '%Broken pipe, while writing to socket%')" diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh index a69e526c1c8..b639cd5ef70 100755 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -83,7 +83,7 @@ function thread_cancel if (( RANDOM % 2 )); then SIGNAL="KILL" fi - PID=$(ps -ef | grep "$TEST_MARK" | grep -v grep | awk '{print $2}') + PID=$(grep -Fa "$TEST_MARK" /proc/*/cmdline | grep -Fav grep | grep -Eoa "/proc/[0-9]*/cmdline:" | grep -Eo "[0-9]*" | head -1) if [ ! -z "$PID" ]; then kill -s "$SIGNAL" "$PID"; fi sleep 0.$RANDOM; done @@ -93,14 +93,13 @@ export -f thread_insert; export -f thread_select; export -f thread_cancel; -TIMEOUT=20 # 5 seconds for each TYPE +TIMEOUT=20 timeout $TIMEOUT bash -c thread_insert & timeout $TIMEOUT bash -c thread_select & timeout $TIMEOUT bash -c thread_cancel 2> /dev/null & wait -wait_for_queries_to_finish $CLICKHOUSE_CLIENT -q 'system flush logs' @@ -110,10 +109,10 @@ insert_data $CLICKHOUSE_CLIENT --implicit_transaction=1 -q 'select throwIf(count() % 1000000 != 0 or count() = 0) from dedup_test' \ || $CLICKHOUSE_CLIENT -q "select name, rows, active, visible, creation_tid, creation_csn from system.parts where database=currentDatabase();" -# We have to ignore stderr from thread_cancel, because our CI finds a bug in ps... -# So use this query to check that thread_cancel do something +# Ensure that thread_cancel actually did something $CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or message like '%Connection reset by peer%' or message like '%Broken pipe, while writing to socket%')" +wait_for_queries_to_finish 30 $CLICKHOUSE_CLIENT --database_atomic_wait_for_drop_and_detach_synchronously=0 -q "drop table dedup_test" diff --git a/tests/queries/shell_config.sh b/tests/queries/shell_config.sh index 3006b74d3f9..ffa286a95cd 100644 --- a/tests/queries/shell_config.sh +++ b/tests/queries/shell_config.sh @@ -136,12 +136,13 @@ function clickhouse_client_removed_host_parameter() function wait_for_queries_to_finish() { + local max_tries="${1:-20}" # Wait for all queries to finish (query may still be running if thread is killed by timeout) num_tries=0 while [[ $($CLICKHOUSE_CLIENT -q "SELECT count() FROM system.processes WHERE current_database=currentDatabase() AND query NOT LIKE '%system.processes%'") -ne 0 ]]; do sleep 0.5; num_tries=$((num_tries+1)) - if [ $num_tries -eq 20 ]; then + if [ $num_tries -eq $max_tries ]; then $CLICKHOUSE_CLIENT -q "SELECT * FROM system.processes WHERE current_database=currentDatabase() AND query NOT LIKE '%system.processes%' FORMAT Vertical" break fi From 6ee65fa1dc3bef79cd6989a66999eca04fce32ee Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Wed, 1 Mar 2023 16:41:51 +0100 Subject: [PATCH 147/342] fix shellcheck --- .../0_stateless/01169_alter_partition_isolation_stress.sh | 1 + .../queries/0_stateless/01171_mv_select_insert_isolation_long.sh | 1 + tests/queries/0_stateless/01174_select_insert_isolation.sh | 1 + tests/queries/shell_config.sh | 1 + 4 files changed, 4 insertions(+) diff --git a/tests/queries/0_stateless/01169_alter_partition_isolation_stress.sh b/tests/queries/0_stateless/01169_alter_partition_isolation_stress.sh index a385fc81fe4..508ad05224c 100755 --- a/tests/queries/0_stateless/01169_alter_partition_isolation_stress.sh +++ b/tests/queries/0_stateless/01169_alter_partition_isolation_stress.sh @@ -2,6 +2,7 @@ # Tags: long, no-replicated-database, no-ordinary-database # shellcheck disable=SC2015 +# shellcheck disable=SC2119 CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh diff --git a/tests/queries/0_stateless/01171_mv_select_insert_isolation_long.sh b/tests/queries/0_stateless/01171_mv_select_insert_isolation_long.sh index 12b654f4215..199c2b5389f 100755 --- a/tests/queries/0_stateless/01171_mv_select_insert_isolation_long.sh +++ b/tests/queries/0_stateless/01171_mv_select_insert_isolation_long.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # Tags: long, no-parallel, no-ordinary-database # Test is too heavy, avoid parallel run in Flaky Check +# shellcheck disable=SC2119 CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh diff --git a/tests/queries/0_stateless/01174_select_insert_isolation.sh b/tests/queries/0_stateless/01174_select_insert_isolation.sh index dc5c1d7a722..29ccfbb1ccb 100755 --- a/tests/queries/0_stateless/01174_select_insert_isolation.sh +++ b/tests/queries/0_stateless/01174_select_insert_isolation.sh @@ -2,6 +2,7 @@ # Tags: long, no-ordinary-database # shellcheck disable=SC2015 +# shellcheck disable=SC2119 CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh diff --git a/tests/queries/shell_config.sh b/tests/queries/shell_config.sh index ffa286a95cd..ef70c82aefc 100644 --- a/tests/queries/shell_config.sh +++ b/tests/queries/shell_config.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2120 # Don't check for ODR violation, since we may test shared build with ASAN export ASAN_OPTIONS=detect_odr_violation=0 From 96e7454df7cd2fd7eaaec97084b679b48be8416b Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Wed, 1 Mar 2023 16:31:50 +0000 Subject: [PATCH 148/342] Fix test --- tests/queries/0_stateless/00965_logs_level_bugfix.reference | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/queries/0_stateless/00965_logs_level_bugfix.reference b/tests/queries/0_stateless/00965_logs_level_bugfix.reference index 52396b3fe79..affd41b780b 100644 --- a/tests/queries/0_stateless/00965_logs_level_bugfix.reference +++ b/tests/queries/0_stateless/00965_logs_level_bugfix.reference @@ -2,7 +2,6 @@ . . - . - From 9f109f0876c84c2f104ffbb96a7e82ed62328095 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Thu, 16 Feb 2023 11:34:36 +0100 Subject: [PATCH 149/342] Fix JOIN tests with invalid USING --- tests/queries/0_stateless/00057_join_aliases.sql | 4 ++-- .../00099_join_many_blocks_segfault.sql | 7 +++++-- .../00122_join_with_subquery_with_subquery.sql | 2 +- .../queries/0_stateless/00138_table_aliases.sql | 2 +- .../0_stateless/00162_shard_global_join.sql | 3 ++- .../00163_shard_join_with_empty_table.sql | 16 ++++++++-------- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tests/queries/0_stateless/00057_join_aliases.sql b/tests/queries/0_stateless/00057_join_aliases.sql index b994e26a747..6f283094396 100644 --- a/tests/queries/0_stateless/00057_join_aliases.sql +++ b/tests/queries/0_stateless/00057_join_aliases.sql @@ -1,6 +1,6 @@ SELECT * FROM ( - SELECT number, number / 2 AS n, j1, j2 - FROM system.numbers + SELECT number, n, j1, j2 + FROM (SELECT number, number / 2 AS n FROM system.numbers) js1 ANY LEFT JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 10) js2 USING n LIMIT 10 ) ORDER BY n diff --git a/tests/queries/0_stateless/00099_join_many_blocks_segfault.sql b/tests/queries/0_stateless/00099_join_many_blocks_segfault.sql index 7219bcecf55..31dec093e61 100644 --- a/tests/queries/0_stateless/00099_join_many_blocks_segfault.sql +++ b/tests/queries/0_stateless/00099_join_many_blocks_segfault.sql @@ -1,7 +1,10 @@ SELECT - 1 AS DomainID, + DomainID, Domain -FROM system.one +FROM +( + SELECT 1 AS DomainID FROM system.one +) js1 ANY LEFT JOIN ( SELECT diff --git a/tests/queries/0_stateless/00122_join_with_subquery_with_subquery.sql b/tests/queries/0_stateless/00122_join_with_subquery_with_subquery.sql index 0cb3de94a55..add31112579 100644 --- a/tests/queries/0_stateless/00122_join_with_subquery_with_subquery.sql +++ b/tests/queries/0_stateless/00122_join_with_subquery_with_subquery.sql @@ -1 +1 @@ -SELECT 1 AS k FROM system.one ANY LEFT JOIN (SELECT k FROM (SELECT 1 AS k, 2 AS x)) js2 USING k; +SELECT k FROM (SELECT 1 AS k FROM system.one) js1 ANY LEFT JOIN (SELECT k FROM (SELECT 1 AS k, 2 AS x)) js2 USING k; diff --git a/tests/queries/0_stateless/00138_table_aliases.sql b/tests/queries/0_stateless/00138_table_aliases.sql index 58fe2ac65e9..1d168250266 100644 --- a/tests/queries/0_stateless/00138_table_aliases.sql +++ b/tests/queries/0_stateless/00138_table_aliases.sql @@ -1,2 +1,2 @@ SELECT * FROM `system`.`one` AS `xxx`; -SELECT 1 AS k, s FROM `system`.`one` AS `xxx` ANY LEFT JOIN (SELECT 1 AS k, 'Hello' AS s) AS `yyy` USING k; +SELECT k, s FROM (SELECT 1 AS k FROM `system`.`one`) AS `xxx` ANY LEFT JOIN (SELECT 1 AS k, 'Hello' AS s) AS `yyy` USING k; diff --git a/tests/queries/0_stateless/00162_shard_global_join.sql b/tests/queries/0_stateless/00162_shard_global_join.sql index 040eb0e7c03..0bf80e70dda 100644 --- a/tests/queries/0_stateless/00162_shard_global_join.sql +++ b/tests/queries/0_stateless/00162_shard_global_join.sql @@ -1,3 +1,4 @@ -- Tags: shard -SELECT toFloat64(dummy + 2) AS n, j1, j2 FROM remote('127.0.0.{2,3}', system.one) jr1 GLOBAL ANY LEFT JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 10) jr2 USING n LIMIT 10; +SELECT n, j1, j2 FROM (SELECT toFloat64(dummy + 2) AS n FROM remote('127.0.0.{2,3}', system.one)) jr1 +GLOBAL ANY LEFT JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 10) jr2 USING n LIMIT 10; diff --git a/tests/queries/0_stateless/00163_shard_join_with_empty_table.sql b/tests/queries/0_stateless/00163_shard_join_with_empty_table.sql index 777b2443416..07242f7a8d4 100644 --- a/tests/queries/0_stateless/00163_shard_join_with_empty_table.sql +++ b/tests/queries/0_stateless/00163_shard_join_with_empty_table.sql @@ -4,29 +4,29 @@ SET any_join_distinct_right_table_keys = 1; SET joined_subquery_requires_alias = 0; SELECT * FROM ( - SELECT number, number / 2 AS n, j1, j2 - FROM remote('127.0.0.{2,3}', system.numbers) + SELECT number, n, j1, j2 + FROM (SELECT number, number / 2 AS n FROM remote('127.0.0.{2,3}', system.numbers)) ANY LEFT JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 0) USING n LIMIT 10 ) ORDER BY number; SELECT * FROM ( - SELECT dummy + 2 AS number, number / 2 AS n, j1, j2 - FROM remote('127.0.0.{2,3}', system.one) + SELECT number, n, j1, j2 + FROM (SELECT dummy + 2 AS number, number / 2 AS n FROM remote('127.0.0.{2,3}', system.one)) ANY INNER JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 0) USING n LIMIT 10 ) ORDER BY number; SELECT * FROM ( - SELECT number, number / 2 AS n, j1, j2 - FROM remote('127.0.0.{2,3}', system.numbers) + SELECT number, n, j1, j2 + FROM (SELECT number, number / 2 AS n FROM remote('127.0.0.{2,3}', system.numbers)) GLOBAL ANY LEFT JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 0) USING n LIMIT 10 ) ORDER BY number; SELECT * FROM ( - SELECT dummy + 2 AS number, number / 2 AS n, j1, j2 - FROM remote('127.0.0.{2,3}', system.one) + SELECT number, n, j1, j2 + FROM (SELECT dummy + 2 AS number, number / 2 AS n FROM remote('127.0.0.{2,3}', system.one)) GLOBAL ANY INNER JOIN (SELECT number / 3 AS n, number AS j1, 'Hello' AS j2 FROM system.numbers LIMIT 0) USING n LIMIT 10 ) ORDER BY number; From f1093716f0457c5427168f2a318bf6bb6e624d72 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 13:45:34 +0100 Subject: [PATCH 150/342] Analyzer MutationsInterpreter fix --- src/Interpreters/MutationsInterpreter.cpp | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Interpreters/MutationsInterpreter.cpp b/src/Interpreters/MutationsInterpreter.cpp index 034f327db91..0b52a1a51bc 100644 --- a/src/Interpreters/MutationsInterpreter.cpp +++ b/src/Interpreters/MutationsInterpreter.cpp @@ -172,6 +172,21 @@ ASTPtr prepareQueryAffectedAST(const std::vector & commands, co return select; } +QueryTreeNodePtr prepareQueryAffectedQueryTree(const std::vector & commands, const StoragePtr & storage, ContextPtr context) +{ + auto ast = prepareQueryAffectedAST(commands, storage, context); + auto query_tree = buildQueryTree(ast, context); + + auto & query_node = query_tree->as(); + query_node.getJoinTree() = std::make_shared(storage, context); + + QueryTreePassManager query_tree_pass_manager(context); + addQueryTreePasses(query_tree_pass_manager); + query_tree_pass_manager.run(query_tree); + + return query_tree; +} + ColumnDependencies getAllColumnDependencies(const StorageMetadataPtr & metadata_snapshot, const NameSet & updated_columns) { NameSet new_updated_columns = updated_columns; @@ -231,18 +246,15 @@ bool isStorageTouchedByMutations( std::optional interpreter_select_query; BlockIO io; - ASTPtr select_query = prepareQueryAffectedAST(commands, storage.shared_from_this(), context); - if (context->getSettingsRef().allow_experimental_analyzer) { - InterpreterSelectQueryAnalyzer interpreter(select_query, - context, - storage_from_part, - SelectQueryOptions().ignoreLimits().ignoreProjections()); + auto select_query_tree = prepareQueryAffectedQueryTree(commands, storage.shared_from_this(), context); + InterpreterSelectQueryAnalyzer interpreter(select_query_tree, context, SelectQueryOptions().ignoreLimits().ignoreProjections()); io = interpreter.execute(); } else { + ASTPtr select_query = prepareQueryAffectedAST(commands, storage.shared_from_this(), context); /// Interpreter must be alive, when we use result of execute() method. /// For some reason it may copy context and give it into ExpressionTransform /// after that we will use context from destroyed stack frame in our stream. From 0ee0324c2cf599838bfd28127eef3641d159d55b Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 18:52:39 +0100 Subject: [PATCH 151/342] Analyzer ARRAY JOIN conversion to AST fix --- src/Analyzer/ArrayJoinNode.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Analyzer/ArrayJoinNode.cpp b/src/Analyzer/ArrayJoinNode.cpp index 2157b5edf6f..490e227d617 100644 --- a/src/Analyzer/ArrayJoinNode.cpp +++ b/src/Analyzer/ArrayJoinNode.cpp @@ -5,8 +5,10 @@ #include #include +#include #include +#include namespace DB { @@ -52,8 +54,24 @@ ASTPtr ArrayJoinNode::toASTImpl() const auto array_join_ast = std::make_shared(); array_join_ast->kind = is_left ? ASTArrayJoin::Kind::Left : ASTArrayJoin::Kind::Inner; - const auto & join_expression_list_node = getJoinExpressionsNode(); - array_join_ast->children.push_back(join_expression_list_node->toAST()); + auto array_join_expressions_ast = std::make_shared(); + const auto & array_join_expressions = getJoinExpressions().getNodes(); + + for (const auto & array_join_expression : array_join_expressions) + { + ASTPtr array_join_expression_ast; + + auto * column_node = array_join_expression->as(); + if (column_node && column_node->getExpression()) + array_join_expression_ast = column_node->getExpression()->toAST(); + else + array_join_expression_ast = array_join_expression->toAST(); + + array_join_expression_ast->setAlias(array_join_expression->getAlias()); + array_join_expressions_ast->children.push_back(std::move(array_join_expression_ast)); + } + + array_join_ast->children.push_back(std::move(array_join_expressions_ast)); array_join_ast->expression_list = array_join_ast->children.back(); ASTPtr tables_in_select_query_ast = std::make_shared(); From 3fbb7f89e5a438ff357506663a0afeab8395a37a Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 21:14:22 +0100 Subject: [PATCH 152/342] Fixed tests --- .../00674_join_on_syntax.reference | 4 +-- .../0_stateless/00674_join_on_syntax.sql | 1 + .../0_stateless/00702_join_with_using.sql | 2 +- .../0_stateless/00722_inner_join.reference | 30 +++++++++---------- .../queries/0_stateless/00722_inner_join.sql | 2 ++ .../00818_inner_join_bug_3567.reference | 28 ++++++++--------- .../0_stateless/00818_inner_join_bug_3567.sql | 2 ++ ...full_join_wrong_columns_in_block.reference | 22 +++++++------- ...00819_full_join_wrong_columns_in_block.sql | 1 + .../00820_multiple_joins.reference | 4 +-- .../0_stateless/00820_multiple_joins.sql | 2 ++ ...le_joins_subquery_requires_alias.reference | 4 +-- ...multiple_joins_subquery_requires_alias.sql | 2 ++ .../00834_hints_for_type_function_typos.sh | 2 +- .../00835_if_generic_case.reference | 2 +- .../0_stateless/00835_if_generic_case.sql | 2 ++ .../01429_join_on_error_messages.sql | 6 ++-- .../01508_explain_header.reference | 2 +- .../01527_bad_aggregation_in_lambda.sql | 4 ++- 19 files changed, 69 insertions(+), 53 deletions(-) diff --git a/tests/queries/0_stateless/00674_join_on_syntax.reference b/tests/queries/0_stateless/00674_join_on_syntax.reference index 10c31e56fe8..7bad0431b8f 100644 --- a/tests/queries/0_stateless/00674_join_on_syntax.reference +++ b/tests/queries/0_stateless/00674_join_on_syntax.reference @@ -49,8 +49,8 @@ duplicate column names {"a1":1,"tab1_copy.a1":2} {"a1":1,"tab1_copy.a1":2} {"a1":1,"copy.a1":2} -{"a1":1,"copy.a1":2} -{"a1":1,"copy.a1":2} +{"a1":1,"tab1_copy.a1":2} +{"a1":1,"tab1_copy.a1":2} subquery 1 1 diff --git a/tests/queries/0_stateless/00674_join_on_syntax.sql b/tests/queries/0_stateless/00674_join_on_syntax.sql index cdca4e27a71..9ff26db1536 100644 --- a/tests/queries/0_stateless/00674_join_on_syntax.sql +++ b/tests/queries/0_stateless/00674_join_on_syntax.sql @@ -1,4 +1,5 @@ SET joined_subquery_requires_alias = 0; +SET allow_experimental_analyzer = 1; drop table if exists tab1; drop table if exists tab2; diff --git a/tests/queries/0_stateless/00702_join_with_using.sql b/tests/queries/0_stateless/00702_join_with_using.sql index 4f81e757dfc..462524e1922 100644 --- a/tests/queries/0_stateless/00702_join_with_using.sql +++ b/tests/queries/0_stateless/00702_join_with_using.sql @@ -7,7 +7,7 @@ CREATE TABLE using2(a UInt8, b UInt8) ENGINE=Memory; INSERT INTO using1 VALUES (1, 1) (2, 2) (3, 3); INSERT INTO using2 VALUES (4, 4) (2, 2) (3, 3); -SELECT * FROM using1 ALL LEFT JOIN (SELECT * FROM using2) js2 USING (a, a, a, b, b, b, a, a) ORDER BY a; +SELECT * FROM using1 ALL LEFT JOIN (SELECT * FROM using2) js2 USING (a, b) ORDER BY a; DROP TABLE using1; DROP TABLE using2; diff --git a/tests/queries/0_stateless/00722_inner_join.reference b/tests/queries/0_stateless/00722_inner_join.reference index b5e8a77a20d..86c07e6e84e 100644 --- a/tests/queries/0_stateless/00722_inner_join.reference +++ b/tests/queries/0_stateless/00722_inner_join.reference @@ -16,24 +16,24 @@ ┌─x──────┬─name─┐ │ system │ one │ └────────┴──────┘ -┌─database─┬─name─┐ -│ system │ one │ -└──────────┴──────┘ +┌─database─┬─t.name─┐ +│ system │ one │ +└──────────┴────────┘ ┌─db.x───┬─name─┐ │ system │ one │ └────────┴──────┘ -┌─db.name─┬─name─┐ -│ system │ one │ -└─────────┴──────┘ -┌─db.name─┬─name─┐ -│ system │ one │ -└─────────┴──────┘ -┌─database─┬─name─┐ -│ system │ one │ -└──────────┴──────┘ -┌─database─┬─name─┐ -│ system │ one │ -└──────────┴──────┘ +┌─db.name─┬─t.name─┐ +│ system │ one │ +└─────────┴────────┘ +┌─db.name─┬─t.name─┐ +│ system │ one │ +└─────────┴────────┘ +┌─t.database─┬─t.name─┐ +│ system │ one │ +└────────────┴────────┘ +┌─database─┬─t.name─┐ +│ system │ one │ +└──────────┴────────┘ 2 2 2 diff --git a/tests/queries/0_stateless/00722_inner_join.sql b/tests/queries/0_stateless/00722_inner_join.sql index eee164ed1e1..75ef40ff2b7 100644 --- a/tests/queries/0_stateless/00722_inner_join.sql +++ b/tests/queries/0_stateless/00722_inner_join.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS one; CREATE TABLE one(dummy UInt8) ENGINE = Memory; diff --git a/tests/queries/0_stateless/00818_inner_join_bug_3567.reference b/tests/queries/0_stateless/00818_inner_join_bug_3567.reference index 173e53ebc34..41e0d8ea43a 100644 --- a/tests/queries/0_stateless/00818_inner_join_bug_3567.reference +++ b/tests/queries/0_stateless/00818_inner_join_bug_3567.reference @@ -3,17 +3,17 @@ │ b │ 2018-01-01 │ │ c │ 2018-01-01 │ └───┴────────────┘ -┌─c─┬─a─┬──────────d─┬─a─┬──────────b─┐ -│ B │ b │ 2018-01-01 │ B │ 2018-01-01 │ -│ C │ c │ 2018-01-01 │ C │ 2018-01-01 │ -│ D │ d │ 2018-01-01 │ D │ 2018-01-01 │ -└───┴───┴────────────┴───┴────────────┘ -┌─a─┬──────────b─┬─c─┬──────────d─┐ -│ a │ 2018-01-01 │ │ 1970-01-01 │ -│ b │ 2018-01-01 │ B │ 2018-01-01 │ -│ c │ 2018-01-01 │ C │ 2018-01-01 │ -└───┴────────────┴───┴────────────┘ -┌─a─┬──────────b─┬─c─┬──────────d─┐ -│ b │ 2018-01-01 │ B │ 2018-01-01 │ -│ c │ 2018-01-01 │ C │ 2018-01-01 │ -└───┴────────────┴───┴────────────┘ +┌─c─┬─table2.a─┬──────────d─┬─a─┬──────────b─┐ +│ B │ b │ 2018-01-01 │ B │ 2018-01-01 │ +│ C │ c │ 2018-01-01 │ C │ 2018-01-01 │ +│ D │ d │ 2018-01-01 │ D │ 2018-01-01 │ +└───┴──────────┴────────────┴───┴────────────┘ +┌─a─┬──────────b─┬─c─┬──────────d─┬─c─┐ +│ a │ 2018-01-01 │ │ 1970-01-01 │ │ +│ b │ 2018-01-01 │ B │ 2018-01-01 │ B │ +│ c │ 2018-01-01 │ C │ 2018-01-01 │ C │ +└───┴────────────┴───┴────────────┴───┘ +┌─a─┬──────────b─┬─c─┬──────────d─┬─c─┐ +│ b │ 2018-01-01 │ B │ 2018-01-01 │ B │ +│ c │ 2018-01-01 │ C │ 2018-01-01 │ C │ +└───┴────────────┴───┴────────────┴───┘ diff --git a/tests/queries/0_stateless/00818_inner_join_bug_3567.sql b/tests/queries/0_stateless/00818_inner_join_bug_3567.sql index 1c851d40f47..cc0b63f9def 100644 --- a/tests/queries/0_stateless/00818_inner_join_bug_3567.sql +++ b/tests/queries/0_stateless/00818_inner_join_bug_3567.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS table1; DROP TABLE IF EXISTS table2; diff --git a/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.reference b/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.reference index afbe9855519..b6d689e3e74 100644 --- a/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.reference +++ b/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.reference @@ -1,11 +1,11 @@ -1 x x -1 x x -1 x x -1 x x -1 x x -1 x x -1 x x -1 x x -1 x x -1 x x -1 x x +1 x y +1 x y +1 x y +1 x y +1 x y +1 x y +1 x y +1 x y +1 x y +1 x y +1 x y diff --git a/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.sql b/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.sql index 14a7424e634..cdb9e57d17f 100644 --- a/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.sql +++ b/tests/queries/0_stateless/00819_full_join_wrong_columns_in_block.sql @@ -1,5 +1,6 @@ SET any_join_distinct_right_table_keys = 1; SET joined_subquery_requires_alias = 0; +SET allow_experimental_analyzer = 1; SELECT * FROM (SELECT 1 AS a, 'x' AS b) join (SELECT 1 as a, 'y' as b) using a; SELECT * FROM (SELECT 1 AS a, 'x' AS b) left join (SELECT 1 as a, 'y' as b) using a; diff --git a/tests/queries/0_stateless/00820_multiple_joins.reference b/tests/queries/0_stateless/00820_multiple_joins.reference index f9d9a664784..3d7054bacfc 100644 --- a/tests/queries/0_stateless/00820_multiple_joins.reference +++ b/tests/queries/0_stateless/00820_multiple_joins.reference @@ -32,13 +32,13 @@ 6 6 60 60 12 12 120 120 18 18 180 180 -┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬─t3.c─┐ +┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬────c─┐ │ 0 │ 0 │ 0 │ 0 │ 0 │ │ 6 │ 6 │ 60 │ 60 │ 600 │ │ 12 │ 12 │ 120 │ 120 │ 1200 │ │ 18 │ 18 │ 180 │ 180 │ 1800 │ └──────┴──────┴──────┴──────┴──────┘ -┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬─t3.c─┐ +┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬────c─┐ │ 0 │ 0 │ 0 │ 0 │ 0 │ │ 6 │ 6 │ 60 │ 60 │ 600 │ │ 12 │ 12 │ 120 │ 120 │ 1200 │ diff --git a/tests/queries/0_stateless/00820_multiple_joins.sql b/tests/queries/0_stateless/00820_multiple_joins.sql index af13e6f1d8b..5c7a7bebb0b 100644 --- a/tests/queries/0_stateless/00820_multiple_joins.sql +++ b/tests/queries/0_stateless/00820_multiple_joins.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS table1; DROP TABLE IF EXISTS table2; DROP TABLE IF EXISTS table3; diff --git a/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.reference b/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.reference index 6d317230813..11755d6bc8b 100644 --- a/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.reference +++ b/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.reference @@ -32,13 +32,13 @@ 6 6 60 60 12 12 120 120 18 18 180 180 -┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬─t3.c─┐ +┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬────c─┐ │ 0 │ 0 │ 0 │ 0 │ 0 │ │ 6 │ 6 │ 60 │ 60 │ 600 │ │ 12 │ 12 │ 120 │ 120 │ 1200 │ │ 18 │ 18 │ 180 │ 180 │ 1800 │ └──────┴──────┴──────┴──────┴──────┘ -┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬─t3.c─┐ +┌─t1.a─┬─t2.a─┬─t2.b─┬─t3.b─┬────c─┐ │ 0 │ 0 │ 0 │ 0 │ 0 │ │ 6 │ 6 │ 60 │ 60 │ 600 │ │ 12 │ 12 │ 120 │ 120 │ 1200 │ diff --git a/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.sql b/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.sql index 612e75e6d4d..3da2cad4eff 100644 --- a/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.sql +++ b/tests/queries/0_stateless/00820_multiple_joins_subquery_requires_alias.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS table1; DROP TABLE IF EXISTS table2; DROP TABLE IF EXISTS table3; diff --git a/tests/queries/0_stateless/00834_hints_for_type_function_typos.sh b/tests/queries/0_stateless/00834_hints_for_type_function_typos.sh index 41a35d908d1..b013993059f 100755 --- a/tests/queries/0_stateless/00834_hints_for_type_function_typos.sh +++ b/tests/queries/0_stateless/00834_hints_for_type_function_typos.sh @@ -10,7 +10,7 @@ $CLICKHOUSE_CLIENT -q "select c23ount(*) from system.functions;" 2>&1 | grep "Ma $CLICKHOUSE_CLIENT -q "select cunt(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['count'" &>/dev/null; $CLICKHOUSE_CLIENT -q "select positin(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['position'" &>/dev/null; $CLICKHOUSE_CLIENT -q "select POSITIO(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['position'" &>/dev/null; -$CLICKHOUSE_CLIENT -q "select fount(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['count'" | grep "Maybe you meant: \['round'" | grep "Or unknown aggregate function" &>/dev/null; +$CLICKHOUSE_CLIENT -q "select fount(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['count'" &>/dev/null; $CLICKHOUSE_CLIENT -q "select positin(*) from system.functions;" 2>&1 | grep -v "Or unknown aggregate function" &>/dev/null; $CLICKHOUSE_CLIENT -q "select pov(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['pow'\]" &>/dev/null; $CLICKHOUSE_CLIENT -q "select getColumnStructure('abc');" 2>&1 | grep "Maybe you meant: \['dumpColumnStructure'\]" &>/dev/null; diff --git a/tests/queries/0_stateless/00835_if_generic_case.reference b/tests/queries/0_stateless/00835_if_generic_case.reference index 45ee4651e17..84c036b17ca 100644 --- a/tests/queries/0_stateless/00835_if_generic_case.reference +++ b/tests/queries/0_stateless/00835_if_generic_case.reference @@ -2,7 +2,7 @@ 2000-01-01 00:00:00 2000-01-02 2000-01-02 00:00:00 2000-01-01 00:00:00 2000-01-02 2000-01-02 00:00:00 2000-01-01 00:00:00 2000-01-02 2000-01-02 00:00:00 -2000-01-01 00:00:00 2000-01-02 2000-01-02 +2000-01-01 00:00:00 2000-01-02 2000-01-02 00:00:00 2000-01-01 00:00:00 2000-01-02 2000-01-02 2000-01-01 00:00:00 2000-01-02 2000-01-02 2000-01-01 00:00:00 2000-01-02 2000-01-02 diff --git a/tests/queries/0_stateless/00835_if_generic_case.sql b/tests/queries/0_stateless/00835_if_generic_case.sql index 63baffcf17d..3d7f128f4c1 100644 --- a/tests/queries/0_stateless/00835_if_generic_case.sql +++ b/tests/queries/0_stateless/00835_if_generic_case.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + SELECT toDateTime('2000-01-01 00:00:00', 'Asia/Istanbul') AS x, toDate('2000-01-02') AS y, x > y ? x : y AS z; SELECT materialize(toDateTime('2000-01-01 00:00:00', 'Asia/Istanbul')) AS x, toDate('2000-01-02') AS y, x > y ? x : y AS z; SELECT toDateTime('2000-01-01 00:00:00', 'Asia/Istanbul') AS x, materialize(toDate('2000-01-02')) AS y, x > y ? x : y AS z; diff --git a/tests/queries/0_stateless/01429_join_on_error_messages.sql b/tests/queries/0_stateless/01429_join_on_error_messages.sql index cf9aac0e4da..9b8688c8415 100644 --- a/tests/queries/0_stateless/01429_join_on_error_messages.sql +++ b/tests/queries/0_stateless/01429_join_on_error_messages.sql @@ -1,8 +1,10 @@ +SET allow_experimental_analyzer = 1; + SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON (arrayJoin([1]) = B.b); -- { serverError 403 } SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON (A.a = arrayJoin([1])); -- { serverError 403 } -SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON equals(a); -- { serverError 62 } -SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON less(a); -- { serverError 62 } +SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON equals(a); -- { serverError 42 } +SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON less(a); -- { serverError 42 } SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON a = b AND a > b; -- { serverError 403 } SELECT 1 FROM (select 1 a) A JOIN (select 1 b) B ON a = b AND a < b; -- { serverError 403 } diff --git a/tests/queries/0_stateless/01508_explain_header.reference b/tests/queries/0_stateless/01508_explain_header.reference index 7510e67c643..206bd4afdab 100644 --- a/tests/queries/0_stateless/01508_explain_header.reference +++ b/tests/queries/0_stateless/01508_explain_header.reference @@ -1,4 +1,4 @@ -Expression ((Projection + Before ORDER BY)) +Expression ((Project names + (Projection + Change column names to column identifiers))) Header: x UInt8 ReadFromStorage (SystemOne) Header: dummy UInt8 diff --git a/tests/queries/0_stateless/01527_bad_aggregation_in_lambda.sql b/tests/queries/0_stateless/01527_bad_aggregation_in_lambda.sql index 10ffca22bd1..e16f6396a2c 100644 --- a/tests/queries/0_stateless/01527_bad_aggregation_in_lambda.sql +++ b/tests/queries/0_stateless/01527_bad_aggregation_in_lambda.sql @@ -1 +1,3 @@ -SELECT arrayMap(x -> x * sum(x), range(10)); -- { serverError 47 } +SET allow_experimental_analyzer = 1; + +SELECT arrayMap(x -> x * sum(x), range(10)); -- { serverError 10 } From 8665077b5ce8f4590b41848aef5698358d1fd95c Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 21:15:30 +0100 Subject: [PATCH 153/342] Planner add logging --- src/Planner/Planner.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Planner/Planner.cpp b/src/Planner/Planner.cpp index f0fe44e368f..fff539f59d6 100644 --- a/src/Planner/Planner.cpp +++ b/src/Planner/Planner.cpp @@ -1012,6 +1012,11 @@ void Planner::buildQueryPlanIfNeeded() if (query_plan.isInitialized()) return; + LOG_TRACE(&Poco::Logger::get("Planner"), "Query {} to stage {}{}", + query_tree->formatConvertedASTForErrorMessage(), + QueryProcessingStage::toString(select_query_options.to_stage), + select_query_options.only_analyze ? " only analyze" : ""); + if (query_tree->getNodeType() == QueryTreeNodeType::UNION) buildPlanForUnionNode(); else @@ -1200,6 +1205,12 @@ void Planner::buildPlanForQueryNode() query_plan = std::move(join_tree_query_plan.query_plan); } + LOG_TRACE(&Poco::Logger::get("Planner"), "Query {} from stage {} to stage {}{}", + query_tree->formatConvertedASTForErrorMessage(), + QueryProcessingStage::toString(from_stage), + QueryProcessingStage::toString(select_query_options.to_stage), + select_query_options.only_analyze ? " only analyze" : ""); + if (select_query_options.to_stage == QueryProcessingStage::FetchColumns) return; From 54b704254d0063050241016731dcb8d2b62da986 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 21:20:49 +0100 Subject: [PATCH 154/342] Analyzer support InterpreterCreateQuery --- src/Interpreters/InterpreterCreateQuery.cpp | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index 559fc3532f5..29b7a4db609 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -730,7 +731,21 @@ InterpreterCreateQuery::TableProperties InterpreterCreateQuery::getTableProperti } else if (create.select) { - Block as_select_sample = InterpreterSelectWithUnionQuery::getSampleBlock(create.select->clone(), getContext(), false /* is_subquery */, create.isParameterizedView()); + + Block as_select_sample; + + if (getContext()->getSettingsRef().allow_experimental_analyzer) + { + as_select_sample = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), getContext()); + } + else + { + as_select_sample = InterpreterSelectWithUnionQuery::getSampleBlock(create.select->clone(), + getContext(), + false /* is_subquery */, + create.isParameterizedView()); + } + properties.columns = ColumnsDescription(as_select_sample.getNamesAndTypesList()); } else if (create.as_table_function) @@ -1184,8 +1199,18 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create) getContext() )) { - Block input_block = InterpreterSelectWithUnionQuery( - create.select->clone(), getContext(), SelectQueryOptions().analyze()).getSampleBlock(); + Block input_block; + + if (getContext()->getSettingsRef().allow_experimental_analyzer) + { + input_block = InterpreterSelectQueryAnalyzer::getSampleBlock(create.select->clone(), getContext()); + } + else + { + input_block = InterpreterSelectWithUnionQuery(create.select->clone(), + getContext(), + SelectQueryOptions().analyze()).getSampleBlock(); + } Block output_block = to_table->getInMemoryMetadataPtr()->getSampleBlock(); From 1b946b314ecf8c710ac80e21236b3ee8c50af17d Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 21:31:21 +0100 Subject: [PATCH 155/342] Analyzer convert LIMIT OFFSET expressions --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 45d8154f39b..2cbdab81c73 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -1156,7 +1156,7 @@ private: static void replaceNodesWithPositionalArguments(QueryTreeNodePtr & node_list, const QueryTreeNodes & projection_nodes, IdentifierResolveScope & scope); - static void validateLimitOffsetExpression(QueryTreeNodePtr & expression_node, const String & expression_description, IdentifierResolveScope & scope); + static void convertLimitOffsetExpression(QueryTreeNodePtr & expression_node, const String & expression_description, IdentifierResolveScope & scope); static void validateTableExpressionModifiers(const QueryTreeNodePtr & table_expression_node, IdentifierResolveScope & scope); @@ -2091,7 +2091,7 @@ void QueryAnalyzer::replaceNodesWithPositionalArguments(QueryTreeNodePtr & node_ } } -void QueryAnalyzer::validateLimitOffsetExpression(QueryTreeNodePtr & expression_node, const String & expression_description, IdentifierResolveScope & scope) +void QueryAnalyzer::convertLimitOffsetExpression(QueryTreeNodePtr & expression_node, const String & expression_description, IdentifierResolveScope & scope) { const auto * limit_offset_constant_node = expression_node->as(); if (!limit_offset_constant_node || !isNativeNumber(removeNullable(limit_offset_constant_node->getResultType()))) @@ -2101,11 +2101,17 @@ void QueryAnalyzer::validateLimitOffsetExpression(QueryTreeNodePtr & expression_ expression_node->formatASTForErrorMessage(), scope.scope_node->formatASTForErrorMessage()); - Field converted = convertFieldToType(limit_offset_constant_node->getValue(), DataTypeUInt64()); - if (converted.isNull()) + Field converted_value = convertFieldToType(limit_offset_constant_node->getValue(), DataTypeUInt64()); + if (converted_value.isNull()) throw Exception(ErrorCodes::INVALID_LIMIT_EXPRESSION, "{} numeric constant expression is not representable as UInt64", expression_description); + + auto constant_value = std::make_shared(std::move(converted_value), std::make_shared()); + auto result_constant_node = std::make_shared(std::move(constant_value)); + result_constant_node->getSourceExpression() = limit_offset_constant_node->getSourceExpression(); + + expression_node = std::move(result_constant_node); } void QueryAnalyzer::validateTableExpressionModifiers(const QueryTreeNodePtr & table_expression_node, IdentifierResolveScope & scope) @@ -6473,13 +6479,13 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier if (query_node_typed.hasLimitByLimit()) { resolveExpressionNode(query_node_typed.getLimitByLimit(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); - validateLimitOffsetExpression(query_node_typed.getLimitByLimit(), "LIMIT BY LIMIT", scope); + convertLimitOffsetExpression(query_node_typed.getLimitByLimit(), "LIMIT BY LIMIT", scope); } if (query_node_typed.hasLimitByOffset()) { resolveExpressionNode(query_node_typed.getLimitByOffset(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); - validateLimitOffsetExpression(query_node_typed.getLimitByOffset(), "LIMIT BY OFFSET", scope); + convertLimitOffsetExpression(query_node_typed.getLimitByOffset(), "LIMIT BY OFFSET", scope); } if (query_node_typed.hasLimitBy()) @@ -6493,13 +6499,13 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier if (query_node_typed.hasLimit()) { resolveExpressionNode(query_node_typed.getLimit(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); - validateLimitOffsetExpression(query_node_typed.getLimit(), "LIMIT", scope); + convertLimitOffsetExpression(query_node_typed.getLimit(), "LIMIT", scope); } if (query_node_typed.hasOffset()) { resolveExpressionNode(query_node_typed.getOffset(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); - validateLimitOffsetExpression(query_node_typed.getOffset(), "OFFSET", scope); + convertLimitOffsetExpression(query_node_typed.getOffset(), "OFFSET", scope); } /** Resolve nodes with duplicate aliases. From 2672f24ced7b6a2c4754990524bab31f04a97187 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 19 Feb 2023 21:36:59 +0100 Subject: [PATCH 156/342] Analyzer cyclic aliases fix --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 50 +++++++++++++------ .../00818_alias_bug_4110.reference | 1 + .../0_stateless/00818_alias_bug_4110.sql | 10 ++-- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 2cbdab81c73..c97b167348b 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -361,6 +361,12 @@ struct IdentifierResolveResult } }; +struct IdentifierResolveState +{ + IdentifierResolveResult resolve_result; + bool cyclic_identifier_resolve = false; +}; + struct IdentifierResolveSettings { /// Allow to check join tree during identifier resolution @@ -687,7 +693,7 @@ struct IdentifierResolveScope ContextPtr context; /// Identifier lookup to result - std::unordered_map identifier_lookup_to_result; + std::unordered_map identifier_lookup_to_resolve_state; /// Lambda argument can be expression like constant, column, or it can be function std::unordered_map expression_argument_name_to_node; @@ -799,11 +805,11 @@ struct IdentifierResolveScope [[maybe_unused]] void dump(WriteBuffer & buffer) const { buffer << "Scope node " << scope_node->formatASTForErrorMessage() << '\n'; - buffer << "Identifier lookup to result " << identifier_lookup_to_result.size() << '\n'; - for (const auto & [identifier, result] : identifier_lookup_to_result) + buffer << "Identifier lookup to resolve state " << identifier_lookup_to_resolve_state.size() << '\n'; + for (const auto & [identifier, state] : identifier_lookup_to_resolve_state) { buffer << "Identifier " << identifier.dump() << " resolve result "; - result.dump(buffer); + state.resolve_result.dump(buffer); buffer << '\n'; } @@ -3316,21 +3322,28 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook IdentifierResolveScope & scope, IdentifierResolveSettings identifier_resolve_settings) { - auto it = scope.identifier_lookup_to_result.find(identifier_lookup); - if (it != scope.identifier_lookup_to_result.end()) + auto it = scope.identifier_lookup_to_resolve_state.find(identifier_lookup); + if (it != scope.identifier_lookup_to_resolve_state.end()) { - if (!it->second.resolved_identifier) + if (it->second.cyclic_identifier_resolve) throw Exception(ErrorCodes::CYCLIC_ALIASES, "Cyclic aliases for identifier '{}'. In scope {}", identifier_lookup.identifier.getFullName(), scope.scope_node->formatASTForErrorMessage()); - if (scope.use_identifier_lookup_to_result_cache && !scope.non_cached_identifier_lookups_during_expression_resolve.contains(identifier_lookup)) - return it->second; - } + if (!it->second.resolve_result.isResolved()) + it->second.cyclic_identifier_resolve = true; - auto [insert_it, _] = scope.identifier_lookup_to_result.insert({identifier_lookup, IdentifierResolveResult()}); - it = insert_it; + if (it->second.resolve_result.isResolved() && + scope.use_identifier_lookup_to_result_cache && + !scope.non_cached_identifier_lookups_during_expression_resolve.contains(identifier_lookup)) + return it->second.resolve_result; + } + else + { + auto [insert_it, _] = scope.identifier_lookup_to_resolve_state.insert({identifier_lookup, IdentifierResolveState()}); + it = insert_it; + } /// Resolve identifier from current scope @@ -3409,15 +3422,18 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifier(const IdentifierLook resolve_result.resolve_place = IdentifierResolvePlace::DATABASE_CATALOG; } - it->second = resolve_result; + bool was_cyclic_identifier_resolve = it->second.cyclic_identifier_resolve; + if (!was_cyclic_identifier_resolve) + it->second.resolve_result = resolve_result; + it->second.cyclic_identifier_resolve = false; /** If identifier was not resolved, or during expression resolution identifier was explicitly added into non cached set, * or identifier caching was disabled in resolve scope we remove identifier lookup result from identifier lookup to result table. */ - if (!resolve_result.resolved_identifier || + if (!was_cyclic_identifier_resolve && (!resolve_result.resolved_identifier || scope.non_cached_identifier_lookups_during_expression_resolve.contains(identifier_lookup) || - !scope.use_identifier_lookup_to_result_cache) - scope.identifier_lookup_to_result.erase(it); + !scope.use_identifier_lookup_to_result_cache)) + scope.identifier_lookup_to_resolve_state.erase(it); return resolve_result; } @@ -4987,6 +5003,8 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi */ ProjectionNames QueryAnalyzer::resolveExpressionNode(QueryTreeNodePtr & node, IdentifierResolveScope & scope, bool allow_lambda_expression, bool allow_table_expression) { + checkStackSize(); + auto resolved_expression_it = resolved_expressions.find(node); if (resolved_expression_it != resolved_expressions.end()) { diff --git a/tests/queries/0_stateless/00818_alias_bug_4110.reference b/tests/queries/0_stateless/00818_alias_bug_4110.reference index 210fc67db66..e6013d269c2 100644 --- a/tests/queries/0_stateless/00818_alias_bug_4110.reference +++ b/tests/queries/0_stateless/00818_alias_bug_4110.reference @@ -4,6 +4,7 @@ 11 12 12 11 10 10 +10 11 11 12 11 10 12 11 12 diff --git a/tests/queries/0_stateless/00818_alias_bug_4110.sql b/tests/queries/0_stateless/00818_alias_bug_4110.sql index df7e70cb275..9f3657221e4 100644 --- a/tests/queries/0_stateless/00818_alias_bug_4110.sql +++ b/tests/queries/0_stateless/00818_alias_bug_4110.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + select s.a as a, s.a + 1 as b from (select 10 as a) s; select s.a + 1 as a, s.a as b from (select 10 as a) s; select s.a + 1 as a, s.a + 1 as b from (select 10 as a) s; @@ -5,15 +7,15 @@ select s.a + 1 as b, s.a + 2 as a from (select 10 as a) s; select s.a + 2 as b, s.a + 1 as a from (select 10 as a) s; select a, a as a from (select 10 as a); -select s.a, a, a + 1 as a from (select 10 as a) as s; -- { serverError 352 } +select s.a, a, a + 1 as a from (select 10 as a) as s; select s.a + 2 as b, b - 1 as a from (select 10 as a) s; select s.a as a, s.a + 2 as b from (select 10 as a) s; select s.a + 1 as a, s.a + 2 as b from (select 10 as a) s; select a + 1 as a, a + 1 as b from (select 10 as a); select a + 1 as b, b + 1 as a from (select 10 as a); -- { serverError 174 } -select 10 as a, a + 1 as a; -- { serverError 179 } -with 10 as a select a as a; -- { serverError 179 } -with 10 as a select a + 1 as a; -- { serverError 179 } +select 10 as a, a + 1 as a; -- { serverError 47 } +with 10 as a select a as a; -- { serverError 47 } +with 10 as a select a + 1 as a; -- { serverError 47 } SELECT 0 as t FROM (SELECT 1 as t) as inn WHERE inn.t = 1; SELECT sum(value) as value FROM (SELECT 1 as value) as data WHERE data.value > 0; From 40fc36bb6f7a04524368aa445e78d9742e24195a Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Mon, 20 Feb 2023 19:26:39 +0100 Subject: [PATCH 157/342] Fixed tests --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 2 +- src/Planner/PlannerJoinTree.cpp | 3 +- .../00800_low_cardinality_join.sql | 3 +- .../00800_low_cardinality_merge_join.sql.j2 | 4 ++- .../00834_limit_with_constant_expressions.sql | 4 ++- .../01000_subquery_requires_alias.reference | 4 +-- .../01000_subquery_requires_alias.sql | 1 + .../01009_insert_select_data_loss.sql | 2 +- .../01010_pm_join_all_join_bug.sql | 4 ++- .../queries/0_stateless/01010_pmj_on_disk.sql | 12 +++---- .../01064_pm_all_join_const_and_nullable.sql | 32 +++++++++---------- .../0_stateless/01095_tpch_like_smoke.sql | 8 +++-- .../0_stateless/01120_join_constants.sql | 4 +-- .../01129_dict_get_join_lose_constness.sql | 4 +-- .../0_stateless/01223_dist_on_dist.sql | 4 +-- .../0_stateless/01232_untuple.reference | 2 +- tests/queries/0_stateless/01232_untuple.sql | 2 ++ .../0_stateless/01474_bad_global_join.sql | 4 ++- .../01671_merge_join_and_constants.reference | 10 +++--- .../01671_merge_join_and_constants.sql | 2 ++ .../01890_cross_join_explain_crash.reference | 1 + .../01890_cross_join_explain_crash.sql | 5 ++- ...obal_subqueries_subquery_or_table_name.sql | 4 ++- .../0_stateless/02116_tuple_element.sql | 6 ++-- .../02136_scalar_read_rows_json.reference | 2 +- .../02136_scalar_read_rows_json.sh | 2 +- .../02154_dictionary_get_http_json.reference | 8 ++--- .../02154_dictionary_get_http_json.sh | 1 + tests/queries/0_stateless/02163_operators.sql | 2 +- .../02234_clickhouse_local_test_mode.sh | 2 +- ..._column_matcher_and_column_transformer.sql | 4 ++- ...37_multiple_joins_original_names.reference | 1 + .../02337_multiple_joins_original_names.sql | 4 ++- ...497_if_transform_strings_to_enum.reference | 24 +++++++------- 34 files changed, 102 insertions(+), 75 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index c97b167348b..0e47aec7c20 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -3647,7 +3647,7 @@ QueryAnalyzer::QueryTreeNodesWithNames QueryAnalyzer::resolveQualifiedMatcher(Qu if (!table_expression_node) { - throw Exception(ErrorCodes::UNSUPPORTED_METHOD, + throw Exception(ErrorCodes::UNKNOWN_IDENTIFIER, "Qualified matcher {} does not find table. In scope {}", matcher_node->formatASTForErrorMessage(), scope.scope_node->formatASTForErrorMessage()); diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index f2259ca7e33..02c396542b1 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -533,7 +533,8 @@ JoinTreeQueryPlan buildQueryPlanForJoinNode(const QueryTreeNodePtr & join_table_ for (auto & output_node : cast_actions_dag->getOutputs()) { - if (planner_context->getGlobalPlannerContext()->hasColumnIdentifier(output_node->result_name)) + if (planner_context->getGlobalPlannerContext()->hasColumnIdentifier(output_node->result_name) && + output_node->result_type->canBeInsideNullable()) output_node = &cast_actions_dag->addFunction(to_nullable_function, {output_node}, output_node->result_name); } diff --git a/tests/queries/0_stateless/00800_low_cardinality_join.sql b/tests/queries/0_stateless/00800_low_cardinality_join.sql index 30fb40a56e9..9c1fd9b7ad3 100644 --- a/tests/queries/0_stateless/00800_low_cardinality_join.sql +++ b/tests/queries/0_stateless/00800_low_cardinality_join.sql @@ -1,3 +1,4 @@ +set allow_experimental_analyzer = 1; set joined_subquery_requires_alias = 0; select * from (select dummy as val from system.one) any left join (select dummy as val from system.one) using val; @@ -10,7 +11,7 @@ select * from (select toLowCardinality(toNullable(dummy)) as val from system.one select * from (select toLowCardinality(dummy) as val from system.one) any left join (select toLowCardinality(toNullable(dummy)) as val from system.one) using val; select * from (select toLowCardinality(toNullable(dummy)) as val from system.one) any left join (select toLowCardinality(toNullable(dummy)) as val from system.one) using val; select '-'; -select * from (select dummy as val from system.one) any left join (select dummy as val from system.one) on val + 0 = val * 1; -- { serverError 352 } +select * from (select dummy as val from system.one) any left join (select dummy as val from system.one) on val + 0 = val * 1; -- { serverError 403 } select * from (select dummy as val from system.one) any left join (select dummy as rval from system.one) on val + 0 = rval * 1; select * from (select toLowCardinality(dummy) as val from system.one) any left join (select dummy as rval from system.one) on val + 0 = rval * 1; select * from (select dummy as val from system.one) any left join (select toLowCardinality(dummy) as rval from system.one) on val + 0 = rval * 1; diff --git a/tests/queries/0_stateless/00800_low_cardinality_merge_join.sql.j2 b/tests/queries/0_stateless/00800_low_cardinality_merge_join.sql.j2 index 8b7856b7738..8e2037480c7 100644 --- a/tests/queries/0_stateless/00800_low_cardinality_merge_join.sql.j2 +++ b/tests/queries/0_stateless/00800_low_cardinality_merge_join.sql.j2 @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + {% for join_algorithm in ['partial_merge', 'full_sorting_merge', 'grace_hash'] -%} SET join_algorithm = '{{ join_algorithm }}'; @@ -12,7 +14,7 @@ SELECT * FROM (SELECT toLowCardinality(toNullable(dummy)) AS val FROM system.one SELECT * FROM (SELECT toLowCardinality(dummy) AS val FROM system.one) s1 ANY LEFT JOIN (SELECT toLowCardinality(toNullable(dummy)) AS val FROM system.one) s2 USING val ORDER BY val; SELECT * FROM (SELECT toLowCardinality(toNullable(dummy)) AS val FROM system.one) s1 ANY LEFT JOIN (SELECT toLowCardinality(toNullable(dummy)) AS val FROM system.one) s2 USING val ORDER BY val; SELECT '-'; -SELECT * FROM (SELECT dummy AS val FROM system.one) s1 ANY LEFT JOIN (SELECT dummy AS val FROM system.one) s2 ON val + 0 = val * 1 ORDER BY val; -- { serverError 352 } +SELECT * FROM (SELECT dummy AS val FROM system.one) s1 ANY LEFT JOIN (SELECT dummy AS val FROM system.one) s2 ON val + 0 = val * 1 ORDER BY val; -- { serverError 403 } SELECT * FROM (SELECT dummy AS val FROM system.one) s1 ANY LEFT JOIN (SELECT dummy AS rval FROM system.one) s2 ON val + 0 = rval * 1 ORDER BY val; SELECT * FROM (SELECT toLowCardinality(dummy) AS val FROM system.one) s1 ANY LEFT JOIN (SELECT dummy AS rval FROM system.one) s2 ON val + 0 = rval * 1 ORDER BY val; SELECT * FROM (SELECT dummy AS val FROM system.one) s1 ANY LEFT JOIN (SELECT toLowCardinality(dummy) AS rval FROM system.one) s2 ON val + 0 = rval * 1 ORDER BY val; diff --git a/tests/queries/0_stateless/00834_limit_with_constant_expressions.sql b/tests/queries/0_stateless/00834_limit_with_constant_expressions.sql index 12104145237..1d3429436e7 100644 --- a/tests/queries/0_stateless/00834_limit_with_constant_expressions.sql +++ b/tests/queries/0_stateless/00834_limit_with_constant_expressions.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + SELECT number FROM numbers(10) LIMIT 0 + 1; SELECT number FROM numbers(10) LIMIT 1 - 1; SELECT number FROM numbers(10) LIMIT 2 - 1; @@ -9,7 +11,7 @@ SELECT number FROM numbers(10) LIMIT now(); -- { serverError 440 } SELECT number FROM numbers(10) LIMIT today(); -- { serverError 440 } SELECT number FROM numbers(10) LIMIT toUInt8('1'); SELECT number FROM numbers(10) LIMIT toFloat32('1'); -SELECT number FROM numbers(10) LIMIT rand(); -- { serverError 36 } +SELECT number FROM numbers(10) LIMIT rand(); -- { serverError 440 } SELECT count() <= 1 FROM (SELECT number FROM numbers(10) LIMIT randConstant() % 2); diff --git a/tests/queries/0_stateless/01000_subquery_requires_alias.reference b/tests/queries/0_stateless/01000_subquery_requires_alias.reference index 8018af28430..b6ceb2a2a72 100644 --- a/tests/queries/0_stateless/01000_subquery_requires_alias.reference +++ b/tests/queries/0_stateless/01000_subquery_requires_alias.reference @@ -1,2 +1,2 @@ -1 2 3 -1 2 3 +2 1 3 +2 1 3 diff --git a/tests/queries/0_stateless/01000_subquery_requires_alias.sql b/tests/queries/0_stateless/01000_subquery_requires_alias.sql index d14d4637be8..27320fab933 100644 --- a/tests/queries/0_stateless/01000_subquery_requires_alias.sql +++ b/tests/queries/0_stateless/01000_subquery_requires_alias.sql @@ -1,3 +1,4 @@ +SET allow_experimental_analyzer = 1; SET joined_subquery_requires_alias = 1; SELECT * FROM (SELECT 1 as A, 2 as B) X diff --git a/tests/queries/0_stateless/01009_insert_select_data_loss.sql b/tests/queries/0_stateless/01009_insert_select_data_loss.sql index a153d7dbc30..cbf87c4d0f1 100644 --- a/tests/queries/0_stateless/01009_insert_select_data_loss.sql +++ b/tests/queries/0_stateless/01009_insert_select_data_loss.sql @@ -1,7 +1,7 @@ drop table if exists tab; create table tab (x UInt64) engine = MergeTree order by tuple(); -insert into tab select number as n from numbers(20) nums +insert into tab select n from (SELECT number AS n FROM numbers(20)) nums semi left join (select number * 10 as n from numbers(2)) js2 using(n) settings max_block_size = 5; select * from tab order by x; diff --git a/tests/queries/0_stateless/01010_pm_join_all_join_bug.sql b/tests/queries/0_stateless/01010_pm_join_all_join_bug.sql index 18a67f41194..d192b9a8b64 100644 --- a/tests/queries/0_stateless/01010_pm_join_all_join_bug.sql +++ b/tests/queries/0_stateless/01010_pm_join_all_join_bug.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS ints; CREATE TABLE ints (i64 Int64, i32 Int32) ENGINE = Memory; @@ -10,6 +12,6 @@ SELECT '-'; SELECT * FROM ints l INNER JOIN ints r USING i64 ORDER BY l.i32, r.i32; SELECT '-'; -SELECT count() FROM ( SELECT [1], count(1) ) AS t1 ALL RIGHT JOIN ( SELECT number AS s FROM numbers(2) ) AS t2 USING (s); -- { serverError NOT_FOUND_COLUMN_IN_BLOCK } +SELECT count() FROM ( SELECT [1], count(1) ) AS t1 ALL RIGHT JOIN ( SELECT number AS s FROM numbers(2) ) AS t2 USING (s); -- { serverError UNKNOWN_IDENTIFIER } DROP TABLE ints; diff --git a/tests/queries/0_stateless/01010_pmj_on_disk.sql b/tests/queries/0_stateless/01010_pmj_on_disk.sql index d4fb9184896..4925f78f82f 100644 --- a/tests/queries/0_stateless/01010_pmj_on_disk.sql +++ b/tests/queries/0_stateless/01010_pmj_on_disk.sql @@ -1,6 +1,6 @@ SET join_algorithm = 'hash'; -SELECT number as n, j FROM numbers(4) nums +SELECT n, j FROM (SELECT number as n FROM numbers(4)) nums ANY LEFT JOIN ( SELECT number * 2 AS n, number + 10 AS j FROM numbers(4000) @@ -10,7 +10,7 @@ ORDER BY n; SET max_rows_in_join = 1000; -SELECT number as n, j FROM numbers(4) nums +SELECT n, j FROM (SELECT number AS n FROM numbers(4)) nums ANY LEFT JOIN ( SELECT number * 2 AS n, number + 10 AS j FROM numbers(4000) @@ -20,7 +20,7 @@ ORDER BY n; -- { serverError 191 } SET join_algorithm = 'partial_merge'; -SELECT number as n, j FROM numbers(4) nums +SELECT n, j FROM (SELECT number as n FROM numbers(4)) nums ANY LEFT JOIN ( SELECT number * 2 AS n, number + 10 AS j FROM numbers(4000) @@ -30,7 +30,7 @@ ORDER BY n; SET partial_merge_join_optimizations = 1; -SELECT number as n, j FROM numbers(4) nums +SELECT n, j FROM (SELECT number AS n FROM numbers(4)) nums ANY LEFT JOIN ( SELECT number * 2 AS n, number + 10 AS j FROM numbers(4000) @@ -40,7 +40,7 @@ ORDER BY n; SET join_algorithm = 'auto'; -SELECT number as n, j FROM numbers(4) nums +SELECT n, j FROM (SELECT number AS n FROM numbers(4)) nums ANY LEFT JOIN ( SELECT number * 2 AS n, number + 10 AS j FROM numbers(4000) @@ -50,7 +50,7 @@ ORDER BY n; SET max_rows_in_join = '10'; -SELECT number as n, j FROM numbers(4) nums +SELECT n, j FROM (SELECT number AS n FROM numbers(4)) nums ANY LEFT JOIN ( SELECT number * 2 AS n, number + 10 AS j FROM numbers(4000) diff --git a/tests/queries/0_stateless/01064_pm_all_join_const_and_nullable.sql b/tests/queries/0_stateless/01064_pm_all_join_const_and_nullable.sql index 10306777f25..bc5e1d68290 100644 --- a/tests/queries/0_stateless/01064_pm_all_join_const_and_nullable.sql +++ b/tests/queries/0_stateless/01064_pm_all_join_const_and_nullable.sql @@ -1,87 +1,87 @@ SET join_algorithm = 'partial_merge'; SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(1) AS k FROM numbers(1)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(1) as k FROM numbers(1)) nums JOIN (SELECT 1 AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT 1 as k FROM numbers(1) nums +SELECT k FROM (SELECT 1 AS k FROM numbers(1)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT 1 as k FROM numbers(1) nums +SELECT k FROM (SELECT 1 as k FROM numbers(1)) nums JOIN (SELECT 1 AS k, number n FROM numbers(100000)) j USING k); SELECT 'first nullable'; SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(toNullable(1)) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(toNullable(1)) AS k FROM numbers(1)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(toNullable(1)) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(toNullable(1)) as k FROM numbers(1)) nums JOIN (SELECT 1 AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT toNullable(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT toNullable(1) as k FROM numbers(1)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT toNullable(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT toNullable(1) as k FROM numbers(1)) nums JOIN (SELECT 1 AS k, number n FROM numbers(100000)) j USING k); SELECT 'second nullable'; SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(1) as k FROM numbers(1)) nums JOIN (SELECT materialize(toNullable(1)) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(1) as k FROM numbers(1)) nums JOIN (SELECT toNullable(1) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT 1 as k FROM numbers(1) nums +SELECT k FROM (SELECT 1 as k FROM numbers(1)) nums JOIN (SELECT materialize(toNullable(1)) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT 1 as k FROM numbers(1) nums +SELECT k FROM (SELECT 1 as k FROM numbers(1)) nums JOIN (SELECT toNullable(1) AS k, number n FROM numbers(100000)) j USING k); SELECT 'both nullable'; SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(toNullable(1)) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(toNullable(1)) as k FROM numbers(1)) nums JOIN (SELECT materialize(toNullable(1)) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT materialize(toNullable(1)) as k FROM numbers(1) nums +SELECT k FROM (SELECT materialize(toNullable(1)) as k FROM numbers(1)) nums JOIN (SELECT toNullable(1) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT toNullable(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT toNullable(1) as k FROM numbers(1)) nums JOIN (SELECT materialize(toNullable(1)) AS k, number n FROM numbers(100000)) j USING k); SELECT count(1), uniqExact(1) FROM ( -SELECT toNullable(1) as k FROM numbers(1) nums +SELECT k FROM (SELECT toNullable(1) as k FROM numbers(1)) nums JOIN (SELECT toNullable(1) AS k, number n FROM numbers(100000)) j USING k); diff --git a/tests/queries/0_stateless/01095_tpch_like_smoke.sql b/tests/queries/0_stateless/01095_tpch_like_smoke.sql index 1ac9ec229f0..7ab57766605 100644 --- a/tests/queries/0_stateless/01095_tpch_like_smoke.sql +++ b/tests/queries/0_stateless/01095_tpch_like_smoke.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS part; DROP TABLE IF EXISTS supplier; DROP TABLE IF EXISTS partsupp; @@ -180,7 +182,7 @@ order by n_name, s_name, p_partkey -limit 100; -- { serverError 47 } +limit 100; -- { serverError 1 } select 3; select @@ -598,7 +600,7 @@ where lineitem where l_partkey = p_partkey - ); -- { serverError 47 } + ); -- { serverError 1 } select 18; select @@ -709,7 +711,7 @@ where and s_nationkey = n_nationkey and n_name = 'CANADA' order by - s_name; -- { serverError 47 } + s_name; -- { serverError 1 } select 21, 'fail: exists, not exists'; -- TODO -- select diff --git a/tests/queries/0_stateless/01120_join_constants.sql b/tests/queries/0_stateless/01120_join_constants.sql index fdf297f5934..d8c8b5757f8 100644 --- a/tests/queries/0_stateless/01120_join_constants.sql +++ b/tests/queries/0_stateless/01120_join_constants.sql @@ -1,7 +1,7 @@ SELECT t1.*, t2.*, - 'world', + 'world' AS constant, isConstant('world') FROM ( @@ -19,7 +19,7 @@ LEFT JOIN SELECT t1.*, t2.*, - 123, + 123 AS constant, isConstant('world') FROM ( diff --git a/tests/queries/0_stateless/01129_dict_get_join_lose_constness.sql b/tests/queries/0_stateless/01129_dict_get_join_lose_constness.sql index 4f7197dc5ce..fd3e12f7a15 100644 --- a/tests/queries/0_stateless/01129_dict_get_join_lose_constness.sql +++ b/tests/queries/0_stateless/01129_dict_get_join_lose_constness.sql @@ -13,9 +13,9 @@ SOURCE(CLICKHOUSE(HOST 'localhost' PORT tcpPort() USER 'default' PASSWORD '' TAB LIFETIME(0) LAYOUT(hashed()); -SELECT dictGetInt32('system.dict1', 'element_count', toUInt64(dict_key)) AS join_key, +SELECT join_key, toTimeZone(dictGetDateTime('system.dict1', 'loading_start_time', toUInt64(dict_key)), 'UTC') AS datetime -FROM (select 1 AS dict_key) js1 +FROM (select dictGetInt32('system.dict1', 'element_count', toUInt64(dict_key)) AS join_key, 1 AS dict_key) js1 LEFT JOIN (SELECT toInt32(2) AS join_key) js2 USING (join_key) WHERE now() >= datetime; diff --git a/tests/queries/0_stateless/01223_dist_on_dist.sql b/tests/queries/0_stateless/01223_dist_on_dist.sql index 65a240fd48b..4cd8ffd7a03 100644 --- a/tests/queries/0_stateless/01223_dist_on_dist.sql +++ b/tests/queries/0_stateless/01223_dist_on_dist.sql @@ -33,9 +33,9 @@ select 'cluster() GROUP BY ORDER BY'; select * from cluster(test_cluster_two_shards, currentDatabase(), dist_01223) group by key order by key; select 'LEFT JOIN'; -select toInt32(number) key, b.key from numbers(2) a left join (select distinct * from dist_01223) b using key order by b.key; +select a.key, b.key from (SELECT toInt32(number) key from numbers(2)) a left join (select distinct * from dist_01223) b using key order by b.key; select 'RIGHT JOIN'; -select toInt32(number) key, b.key from numbers(2) a right join (select distinct * from dist_01223) b using key order by b.key; +select a.key, b.key from (SELECT toInt32(number) key from numbers(2)) a right join (select distinct * from dist_01223) b using key order by b.key; -- more data for GROUP BY insert into data_01223 select number%3 from numbers(30); diff --git a/tests/queries/0_stateless/01232_untuple.reference b/tests/queries/0_stateless/01232_untuple.reference index 8e1f97d2585..7bb80bf618f 100644 --- a/tests/queries/0_stateless/01232_untuple.reference +++ b/tests/queries/0_stateless/01232_untuple.reference @@ -2,7 +2,7 @@ hello 1 3 world 9 9 (0,1) -key tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), 1) tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), 2) tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), 3) tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), 4) tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), 5) +key tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), \'1\') tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), \'2\') tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), \'3\') tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), \'4\') tupleElement(argMax(tuple(v1, v2, v3, v4, v5), v1), \'5\') 1 20 20 10 20 30 2 11 20 10 20 30 3 70 20 10 20 30 diff --git a/tests/queries/0_stateless/01232_untuple.sql b/tests/queries/0_stateless/01232_untuple.sql index 92150e92b29..ccefd13a772 100644 --- a/tests/queries/0_stateless/01232_untuple.sql +++ b/tests/queries/0_stateless/01232_untuple.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + select untuple((* except (b),)) from (select 1 a, 2 b, 3 c); select 'hello', untuple((* except (b),)), 'world' from (select 1 a, 2 b, 3 c); select argMax(untuple(x)) from (select (number, number + 1) as x from numbers(10)); diff --git a/tests/queries/0_stateless/01474_bad_global_join.sql b/tests/queries/0_stateless/01474_bad_global_join.sql index b65b4e804a8..2251c1797cf 100644 --- a/tests/queries/0_stateless/01474_bad_global_join.sql +++ b/tests/queries/0_stateless/01474_bad_global_join.sql @@ -1,5 +1,7 @@ -- Tags: global +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS local_table; DROP TABLE IF EXISTS dist_table; @@ -10,7 +12,7 @@ INSERT INTO local_table SELECT number AS id, toString(number) AS val FROM number CREATE TABLE dist_table AS local_table ENGINE = Distributed('test_cluster_two_shards_localhost', currentDatabase(), local_table); -SELECT uniq(d.val) FROM dist_table AS d GLOBAL LEFT JOIN numbers(100) AS t USING id; -- { serverError 284 } +SELECT uniq(d.val) FROM dist_table AS d GLOBAL LEFT JOIN numbers(100) AS t USING id; -- { serverError 47 } SELECT uniq(d.val) FROM dist_table AS d GLOBAL LEFT JOIN local_table AS t USING id; DROP TABLE local_table; diff --git a/tests/queries/0_stateless/01671_merge_join_and_constants.reference b/tests/queries/0_stateless/01671_merge_join_and_constants.reference index efd814df893..f82033ab3c7 100644 --- a/tests/queries/0_stateless/01671_merge_join_and_constants.reference +++ b/tests/queries/0_stateless/01671_merge_join_and_constants.reference @@ -1,6 +1,6 @@ -┌─a─┬──────────b─┬─c─┬──────────d─┬─t2.'0.10'─┐ -│ a │ 2018-01-01 │ │ 1970-01-01 │ │ -│ b │ 2018-01-01 │ B │ 2018-01-01 │ 0.10 │ -│ c │ 2018-01-01 │ C │ 2018-01-01 │ 0.10 │ -└───┴────────────┴───┴────────────┴───────────┘ +┌─a─┬──────────b─┬─c─┬──────────d─┬─'0.10'─┬─c─┐ +│ a │ 2018-01-01 │ │ 1970-01-01 │ │ │ +│ b │ 2018-01-01 │ B │ 2018-01-01 │ 0.10 │ B │ +│ c │ 2018-01-01 │ C │ 2018-01-01 │ 0.10 │ C │ +└───┴────────────┴───┴────────────┴────────┴───┘ \N \N \N \N 0 0 diff --git a/tests/queries/0_stateless/01671_merge_join_and_constants.sql b/tests/queries/0_stateless/01671_merge_join_and_constants.sql index e9a60f11875..5cabd6f7f06 100644 --- a/tests/queries/0_stateless/01671_merge_join_and_constants.sql +++ b/tests/queries/0_stateless/01671_merge_join_and_constants.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS table1; DROP TABLE IF EXISTS table2; diff --git a/tests/queries/0_stateless/01890_cross_join_explain_crash.reference b/tests/queries/0_stateless/01890_cross_join_explain_crash.reference index 76315843adb..c08ba9c71b3 100644 --- a/tests/queries/0_stateless/01890_cross_join_explain_crash.reference +++ b/tests/queries/0_stateless/01890_cross_join_explain_crash.reference @@ -1,3 +1,4 @@ +1 1 1 2 1 1 1 2 1 1 1 2 diff --git a/tests/queries/0_stateless/01890_cross_join_explain_crash.sql b/tests/queries/0_stateless/01890_cross_join_explain_crash.sql index 20a1956ea6b..bb2bc606870 100644 --- a/tests/queries/0_stateless/01890_cross_join_explain_crash.sql +++ b/tests/queries/0_stateless/01890_cross_join_explain_crash.sql @@ -1,8 +1,7 @@ +SET allow_experimental_analyzer = 1; SET joined_subquery_requires_alias = 0; -select * FROM (SELECT 1), (SELECT 1), (SELECT 1); -- { serverError 352 } --- This queries work by luck. --- Feel free to remove then if it is the only failed test. +select * FROM (SELECT 1), (SELECT 1), (SELECT 1); select * from (select 2), (select 1) as a, (select 1) as b; select * from (select 1) as a, (select 2), (select 1) as b; select * from (select 1) as a, (select 1) as b, (select 2); diff --git a/tests/queries/0_stateless/02002_global_subqueries_subquery_or_table_name.sql b/tests/queries/0_stateless/02002_global_subqueries_subquery_or_table_name.sql index 7d9c08c7cb6..75eff67970e 100644 --- a/tests/queries/0_stateless/02002_global_subqueries_subquery_or_table_name.sql +++ b/tests/queries/0_stateless/02002_global_subqueries_subquery_or_table_name.sql @@ -1,7 +1,9 @@ -- Tags: global +SET allow_experimental_analyzer = 1; + SELECT cityHash64(number GLOBAL IN (NULL, -2147483648, -9223372036854775808), nan, 1024, NULL, NULL, 1.000100016593933, NULL), (NULL, cityHash64(inf, -2147483648, NULL, NULL, 10.000100135803223), cityHash64(1.1754943508222875e-38, NULL, NULL, NULL), 2147483647) FROM cluster(test_cluster_two_shards_localhost, numbers((NULL, cityHash64(0., 65536, NULL, NULL, 10000000000., NULL), 0) GLOBAL IN (some_identifier), 65536)) -WHERE number GLOBAL IN [1025] --{serverError 284} +WHERE number GLOBAL IN [1025] --{serverError 36} diff --git a/tests/queries/0_stateless/02116_tuple_element.sql b/tests/queries/0_stateless/02116_tuple_element.sql index 4ce6e5cf136..b43c9133e6b 100644 --- a/tests/queries/0_stateless/02116_tuple_element.sql +++ b/tests/queries/0_stateless/02116_tuple_element.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS t_tuple_element; CREATE TABLE t_tuple_element(t1 Tuple(a UInt32, s String), t2 Tuple(UInt32, String)) ENGINE = Memory; @@ -16,7 +18,7 @@ EXPLAIN SYNTAX SELECT tupleElement(t1, 'a') FROM t_tuple_element; SELECT tupleElement(number, 1) FROM numbers(1); -- { serverError 43 } SELECT tupleElement(t1) FROM t_tuple_element; -- { serverError 42 } -SELECT tupleElement(t1, 'b') FROM t_tuple_element; -- { serverError 47 } +SELECT tupleElement(t1, 'b') FROM t_tuple_element; -- { serverError 10 } SELECT tupleElement(t1, 0) FROM t_tuple_element; -- { serverError 127 } SELECT tupleElement(t1, 3) FROM t_tuple_element; -- { serverError 127 } SELECT tupleElement(t1, materialize('a')) FROM t_tuple_element; -- { serverError 43 } @@ -28,7 +30,7 @@ SELECT tupleElement(t2, 1) FROM t_tuple_element; EXPLAIN SYNTAX SELECT tupleElement(t2, 1) FROM t_tuple_element; SELECT tupleElement(t2) FROM t_tuple_element; -- { serverError 42 } -SELECT tupleElement(t2, 'a') FROM t_tuple_element; -- { serverError 47 } +SELECT tupleElement(t2, 'a') FROM t_tuple_element; -- { serverError 10 } SELECT tupleElement(t2, 0) FROM t_tuple_element; -- { serverError 127 } SELECT tupleElement(t2, 3) FROM t_tuple_element; -- { serverError 127 } SELECT tupleElement(t2, materialize(1)) FROM t_tuple_element; -- { serverError 43 } diff --git a/tests/queries/0_stateless/02136_scalar_read_rows_json.reference b/tests/queries/0_stateless/02136_scalar_read_rows_json.reference index 49020a4432f..f8335125233 100644 --- a/tests/queries/0_stateless/02136_scalar_read_rows_json.reference +++ b/tests/queries/0_stateless/02136_scalar_read_rows_json.reference @@ -30,7 +30,7 @@ "meta": [ { - "type": "Tuple(UInt64, UInt64)" + "type": "Tuple(`max(number)` UInt64, `count(number)` UInt64)" } ], diff --git a/tests/queries/0_stateless/02136_scalar_read_rows_json.sh b/tests/queries/0_stateless/02136_scalar_read_rows_json.sh index 34b4b6909b5..1fe345d266d 100755 --- a/tests/queries/0_stateless/02136_scalar_read_rows_json.sh +++ b/tests/queries/0_stateless/02136_scalar_read_rows_json.sh @@ -7,4 +7,4 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) echo "#1" ${CLICKHOUSE_CLIENT} --query='SELECT count() FROM numbers(100) FORMAT JSON;' | grep -a -v "elapsed" echo "#2" -${CLICKHOUSE_CLIENT} --query='SELECT (SELECT max(number), count(number) FROM numbers(100000) as n) SETTINGS max_block_size = 65505 FORMAT JSON;' | grep -a -v "elapsed" | grep -v "_subquery" +${CLICKHOUSE_CLIENT} --query='SELECT (SELECT max(number), count(number) FROM numbers(100000) as n) SETTINGS max_block_size = 65505, allow_experimental_analyzer = 1 FORMAT JSON;' | grep -a -v "elapsed" | grep -v "_subquery" diff --git a/tests/queries/0_stateless/02154_dictionary_get_http_json.reference b/tests/queries/0_stateless/02154_dictionary_get_http_json.reference index 7106f551cd7..201d3b122e8 100644 --- a/tests/queries/0_stateless/02154_dictionary_get_http_json.reference +++ b/tests/queries/0_stateless/02154_dictionary_get_http_json.reference @@ -3,11 +3,11 @@ "meta": [ { - "name": "dictGet(02154_test_dictionary, 'value', toUInt64(0))", + "name": "dictGet('02154_test_dictionary', 'value', toUInt64(0))", "type": "String" }, { - "name": "dictGet(02154_test_dictionary, 'value', toUInt64(1))", + "name": "dictGet('02154_test_dictionary', 'value', toUInt64(1))", "type": "String" } ], @@ -15,8 +15,8 @@ "data": [ { - "dictGet(02154_test_dictionary, 'value', toUInt64(0))": "Value", - "dictGet(02154_test_dictionary, 'value', toUInt64(1))": "" + "dictGet('02154_test_dictionary', 'value', toUInt64(0))": "Value", + "dictGet('02154_test_dictionary', 'value', toUInt64(1))": "" } ], diff --git a/tests/queries/0_stateless/02154_dictionary_get_http_json.sh b/tests/queries/0_stateless/02154_dictionary_get_http_json.sh index a2bce866c76..fbaf67fff2f 100755 --- a/tests/queries/0_stateless/02154_dictionary_get_http_json.sh +++ b/tests/queries/0_stateless/02154_dictionary_get_http_json.sh @@ -32,6 +32,7 @@ $CLICKHOUSE_CLIENT -q """ echo """ SELECT dictGet(02154_test_dictionary, 'value', toUInt64(0)), dictGet(02154_test_dictionary, 'value', toUInt64(1)) + SETTINGS allow_experimental_analyzer = 1 FORMAT JSON """ | ${CLICKHOUSE_CURL} -sSg "${CLICKHOUSE_URL}&wait_end_of_query=1&output_format_write_statistics=0" -d @- diff --git a/tests/queries/0_stateless/02163_operators.sql b/tests/queries/0_stateless/02163_operators.sql index 4968e448ab2..b2414bb197e 100644 --- a/tests/queries/0_stateless/02163_operators.sql +++ b/tests/queries/0_stateless/02163_operators.sql @@ -1,2 +1,2 @@ WITH 2 AS `b.c`, [4, 5] AS a, 6 AS u, 3 AS v, 2 AS d, TRUE AS e, 1 AS f, 0 AS g, 2 AS h, 'Hello' AS i, 'World' AS j, TIMESTAMP '2022-02-02 02:02:02' AS w, [] AS k, (1, 2) AS l, 2 AS m, 3 AS n, [] AS o, [1] AS p, 1 AS q, q AS r, 1 AS s, 1 AS t -SELECT INTERVAL CASE CASE WHEN NOT -a[b.c] * u DIV v + d IS NOT NULL AND e OR f BETWEEN g AND h THEN i ELSE j END WHEN w THEN k END || [l, (m, n)] MINUTE IS NULL OR NOT o::Array(INT) = p <> q < r > s != t AS upyachka; +SELECT INTERVAL CASE CASE WHEN NOT -a[`b.c`] * u DIV v + d IS NOT NULL AND e OR f BETWEEN g AND h THEN i ELSE j END WHEN w THEN k END || [l, (m, n)] MINUTE IS NULL OR NOT o::Array(INT) = p <> q < r > s != t AS upyachka; diff --git a/tests/queries/0_stateless/02234_clickhouse_local_test_mode.sh b/tests/queries/0_stateless/02234_clickhouse_local_test_mode.sh index f736751726d..16200399099 100755 --- a/tests/queries/0_stateless/02234_clickhouse_local_test_mode.sh +++ b/tests/queries/0_stateless/02234_clickhouse_local_test_mode.sh @@ -5,5 +5,5 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) . "$CURDIR"/../shell_config.sh -$CLICKHOUSE_LOCAL --query="SELECT n" 2>&1 | grep -q "Code: 47. DB::Exception: Missing columns:" && echo 'OK' || echo 'FAIL' ||: +$CLICKHOUSE_LOCAL --query="SELECT n SETTINGS allow_experimental_analyzer = 1" 2>&1 | grep -q "Code: 47. DB::Exception:" && echo 'OK' || echo 'FAIL' ||: $CLICKHOUSE_LOCAL --query="SELECT n -- { serverError 47 }" diff --git a/tests/queries/0_stateless/02271_fix_column_matcher_and_column_transformer.sql b/tests/queries/0_stateless/02271_fix_column_matcher_and_column_transformer.sql index f0c0e2bae46..245b2cc97e3 100644 --- a/tests/queries/0_stateless/02271_fix_column_matcher_and_column_transformer.sql +++ b/tests/queries/0_stateless/02271_fix_column_matcher_and_column_transformer.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS github_events; CREATE TABLE github_events @@ -59,6 +61,6 @@ CREATE TABLE github_events ) ENGINE = MergeTree ORDER BY (event_type, repo_name, created_at); -with top_repos as ( select repo_name from github_events where event_type = 'WatchEvent' and toDate(created_at) = today() - 1 group by repo_name order by count() desc limit 100 union distinct select repo_name from github_events where event_type = 'WatchEvent' and toMonday(created_at) = toMonday(today() - interval 1 week) group by repo_name order by count() desc limit 100 union distinct select repo_name from github_events where event_type = 'WatchEvent' and toStartOfMonth(created_at) = toStartOfMonth(today()) - interval 1 month group by repo_name order by count() desc limit 100 union distinct select repo_name from github_events where event_type = 'WatchEvent' and toYear(created_at) = toYear(today()) - 1 group by repo_name order by count() desc limit 100 ), last_day as ( select repo_name, count() as count_last_day, rowNumberInAllBlocks() + 1 as position_last_day from github_events where repo_name in (select repo_name from top_repos) and toDate(created_at) = today() - 1 group by repo_name order by count_last_day desc ), last_week as ( select repo_name, count() as count_last_week, rowNumberInAllBlocks() + 1 as position_last_week from github_events where repo_name in (select repo_name from top_repos) and toMonday(created_at) = toMonday(today()) - interval 1 week group by repo_name order by count_last_week desc ), last_month as ( select repo_name, count() as count_last_month, rowNumberInAllBlocks() + 1 as position_last_month from github_events where repo_name in (select repo_name from top_repos) and toStartOfMonth(created_at) = toStartOfMonth(today()) - interval 1 month group by repo_name order by count_last_month desc ) select d.repo_name, columns(count) from last_day d join last_week w on d.repo_name = w.repo_name join last_month m on d.repo_name = m.repo_name FORMAT TabSeparatedWithNamesAndTypes; -- { serverError 47 } +with top_repos as ( select repo_name from github_events where event_type = 'WatchEvent' and toDate(created_at) = today() - 1 group by repo_name order by count() desc limit 100 union distinct select repo_name from github_events where event_type = 'WatchEvent' and toMonday(created_at) = toMonday(today() - interval 1 week) group by repo_name order by count() desc limit 100 union distinct select repo_name from github_events where event_type = 'WatchEvent' and toStartOfMonth(created_at) = toStartOfMonth(today()) - interval 1 month group by repo_name order by count() desc limit 100 union distinct select repo_name from github_events where event_type = 'WatchEvent' and toYear(created_at) = toYear(today()) - 1 group by repo_name order by count() desc limit 100 ), last_day as ( select repo_name, count() as count_last_day, rowNumberInAllBlocks() + 1 as position_last_day from github_events where repo_name in (select repo_name from top_repos) and toDate(created_at) = today() - 1 group by repo_name order by count_last_day desc ), last_week as ( select repo_name, count() as count_last_week, rowNumberInAllBlocks() + 1 as position_last_week from github_events where repo_name in (select repo_name from top_repos) and toMonday(created_at) = toMonday(today()) - interval 1 week group by repo_name order by count_last_week desc ), last_month as ( select repo_name, count() as count_last_month, rowNumberInAllBlocks() + 1 as position_last_month from github_events where repo_name in (select repo_name from top_repos) and toStartOfMonth(created_at) = toStartOfMonth(today()) - interval 1 month group by repo_name order by count_last_month desc ) select d.repo_name, columns(count) from last_day d join last_week w on d.repo_name = w.repo_name join last_month m on d.repo_name = m.repo_name; DROP TABLE github_events; diff --git a/tests/queries/0_stateless/02337_multiple_joins_original_names.reference b/tests/queries/0_stateless/02337_multiple_joins_original_names.reference index 6ed281c757a..e8183f05f5d 100644 --- a/tests/queries/0_stateless/02337_multiple_joins_original_names.reference +++ b/tests/queries/0_stateless/02337_multiple_joins_original_names.reference @@ -1,2 +1,3 @@ 1 1 +1 diff --git a/tests/queries/0_stateless/02337_multiple_joins_original_names.sql b/tests/queries/0_stateless/02337_multiple_joins_original_names.sql index afafee9f8eb..63bbfe6873d 100644 --- a/tests/queries/0_stateless/02337_multiple_joins_original_names.sql +++ b/tests/queries/0_stateless/02337_multiple_joins_original_names.sql @@ -1,11 +1,13 @@ -- https://github.com/ClickHouse/ClickHouse/issues/34697 +SET allow_experimental_analyzer = 1; + SELECT table1_id FROM ( SELECT first.table1_id FROM (SELECT number+1 as table1_id FROM numbers(1)) as first JOIN (SELECT number+1 as table2_id FROM numbers(1)) as second ON first.table1_id = second.table2_id JOIN (SELECT number+1 as table3_id FROM numbers(1)) as third ON first.table1_id = third.table3_id -); -- { serverError UNKNOWN_IDENTIFIER } +); SELECT table1_id FROM ( SELECT first.table1_id diff --git a/tests/queries/0_stateless/02497_if_transform_strings_to_enum.reference b/tests/queries/0_stateless/02497_if_transform_strings_to_enum.reference index c6265e195c4..f5284f38b86 100644 --- a/tests/queries/0_stateless/02497_if_transform_strings_to_enum.reference +++ b/tests/queries/0_stateless/02497_if_transform_strings_to_enum.reference @@ -37,7 +37,7 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: system.numbers LIMIT - CONSTANT id: 17, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 17, constant_value: UInt64_10, constant_value_type: UInt64 google google google @@ -80,7 +80,7 @@ QUERY id: 0 JOIN TREE TABLE id: 9, table_name: system.numbers LIMIT - CONSTANT id: 19, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 19, constant_value: UInt64_10, constant_value_type: UInt64 other1 other1 google1 @@ -124,7 +124,7 @@ QUERY id: 0 JOIN TREE TABLE id: 9, table_name: system.numbers LIMIT - CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt64 google1 google1 google1 @@ -171,7 +171,7 @@ QUERY id: 0 JOIN TREE TABLE id: 11, table_name: system.numbers LIMIT - CONSTANT id: 22, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 22, constant_value: UInt64_10, constant_value_type: UInt64 google google google @@ -225,7 +225,7 @@ QUERY id: 0 JOIN TREE TABLE id: 12, table_name: system.numbers LIMIT - CONSTANT id: 22, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 22, constant_value: UInt64_10, constant_value_type: UInt64 other other google @@ -276,7 +276,7 @@ QUERY id: 0 JOIN TREE TABLE id: 10, table_name: system.numbers LIMIT - CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt64 google google google google google google @@ -343,7 +343,7 @@ QUERY id: 0 JOIN TREE TABLE id: 9, table_name: system.numbers LIMIT - CONSTANT id: 19, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 19, constant_value: UInt64_10, constant_value_type: UInt64 other other other other google google @@ -404,7 +404,7 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: system.numbers LIMIT - CONSTANT id: 17, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 17, constant_value: UInt64_10, constant_value_type: UInt64 \N \N \N @@ -444,7 +444,7 @@ QUERY id: 0 JOIN TREE TABLE id: 11, table_name: system.numbers LIMIT - CONSTANT id: 12, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 12, constant_value: UInt64_10, constant_value_type: UInt64 \N \N \N @@ -473,7 +473,7 @@ QUERY id: 0 JOIN TREE TABLE id: 5, table_name: system.numbers LIMIT - CONSTANT id: 9, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: UInt64_10, constant_value_type: UInt64 other other google @@ -502,7 +502,7 @@ QUERY id: 0 JOIN TREE TABLE id: 5, table_name: system.numbers LIMIT - CONSTANT id: 9, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 9, constant_value: UInt64_10, constant_value_type: UInt64 google google google @@ -534,4 +534,4 @@ QUERY id: 0 JOIN TREE TABLE id: 7, table_name: system.numbers LIMIT - CONSTANT id: 11, constant_value: UInt64_10, constant_value_type: UInt8 + CONSTANT id: 11, constant_value: UInt64_10, constant_value_type: UInt64 From 6209d13ad535f24b079c6b2a9d29dcea0dbc242b Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 21 Feb 2023 21:50:24 +0100 Subject: [PATCH 158/342] Analyzer support _shardNum column to shardNum() function rewrite --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 21 +++++- .../Passes/ShardNumColumnToFunctionPass.cpp | 67 +++++++++++++++++++ .../Passes/ShardNumColumnToFunctionPass.h | 21 ++++++ src/Analyzer/QueryTreePassManager.cpp | 5 +- src/Functions/getScalar.cpp | 7 +- .../01018_Distributed__shard_num.reference | 2 +- .../01018_Distributed__shard_num.sql | 3 +- .../0_stateless/02163_shard_num.reference | 5 +- tests/queries/0_stateless/02163_shard_num.sql | 9 ++- 9 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 src/Analyzer/Passes/ShardNumColumnToFunctionPass.cpp create mode 100644 src/Analyzer/Passes/ShardNumColumnToFunctionPass.h diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 0e47aec7c20..147c0660bc2 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -4928,11 +4928,15 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi { auto function_base = function->build(argument_columns); + /// Do not constant fold get scalar functions + bool is_get_scalar_function = function_name == "__getScalar" || function_name == "shardNum" || + function_name == "shardCount"; + /** If function is suitable for constant folding try to convert it to constant. * Example: SELECT plus(1, 1); * Result: SELECT 2; */ - if (function_base->isSuitableForConstantFolding()) + if (function_base->isSuitableForConstantFolding() && !is_get_scalar_function) { auto result_type = function_base->getResultType(); auto executable_function = function_base->prepare(argument_columns); @@ -6247,6 +6251,21 @@ void QueryAnalyzer::resolveQueryJoinTreeNode(QueryTreeNodePtr & join_tree_node, { validateTableExpressionModifiers(join_tree_node, scope); initializeTableExpressionData(join_tree_node, scope); + + auto & query_node = scope.scope_node->as(); + auto & mutable_context = query_node.getMutableContext(); + + if (!mutable_context->isDistributed()) + { + bool is_distributed = false; + + if (auto * table_node = join_tree_node->as()) + is_distributed = table_node->getStorage()->isRemote(); + else if (auto * table_function_node = join_tree_node->as()) + is_distributed = table_function_node->getStorage()->isRemote(); + + mutable_context->setDistributed(is_distributed); + } } auto add_table_expression_alias_into_scope = [&](const QueryTreeNodePtr & table_expression_node) diff --git a/src/Analyzer/Passes/ShardNumColumnToFunctionPass.cpp b/src/Analyzer/Passes/ShardNumColumnToFunctionPass.cpp new file mode 100644 index 00000000000..b28816e8ff3 --- /dev/null +++ b/src/Analyzer/Passes/ShardNumColumnToFunctionPass.cpp @@ -0,0 +1,67 @@ +#include + +#include + +#include + +#include + +#include +#include +#include +#include +#include + +namespace DB +{ + +namespace +{ + +class ShardNumColumnToFunctionVisitor : public InDepthQueryTreeVisitorWithContext +{ +public: + using Base = InDepthQueryTreeVisitorWithContext; + using Base::Base; + + void visitImpl(QueryTreeNodePtr & node) const + { + auto * column_node = node->as(); + if (!column_node) + return; + + const auto & column = column_node->getColumn(); + if (column.name != "_shard_num") + return; + + auto column_source = column_node->getColumnSource(); + + auto * table_node = column_source->as(); + auto * table_function_node = column_source->as(); + if (!table_node && !table_function_node) + return; + + const auto & storage = table_node ? table_node->getStorage() : table_function_node->getStorage(); + if (!storage->isRemote()) + return; + + const auto & storage_snapshot = table_node ? table_node->getStorageSnapshot() : table_function_node->getStorageSnapshot(); + if (!storage->isVirtualColumn(column.name, storage_snapshot->getMetadataForQuery())) + return; + + auto function_node = std::make_shared("shardNum"); + auto function = FunctionFactory::instance().get(function_node->getFunctionName(), getContext()); + function_node->resolveAsFunction(function->build(function_node->getArgumentColumns())); + node = std::move(function_node); + } +}; + +} + +void ShardNumColumnToFunctionPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context) +{ + ShardNumColumnToFunctionVisitor visitor(context); + visitor.visit(query_tree_node); +} + +} diff --git a/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h b/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h new file mode 100644 index 00000000000..83b974954fa --- /dev/null +++ b/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +namespace DB +{ + +/** Rewrite _shard_num column into shardNum() function. + */ +class ShardNumColumnToFunctionPass final : public IQueryTreePass +{ +public: + String getName() override { return "ShardNumColumnToFunctionPass"; } + + String getDescription() override { return "Rewrite _shard_num column into shardNum() function"; } + + void run(QueryTreeNodePtr query_tree_node, ContextPtr context) override; + +}; + +} diff --git a/src/Analyzer/QueryTreePassManager.cpp b/src/Analyzer/QueryTreePassManager.cpp index 9ba18e27f73..dfac2f499dd 100644 --- a/src/Analyzer/QueryTreePassManager.cpp +++ b/src/Analyzer/QueryTreePassManager.cpp @@ -40,6 +40,7 @@ #include #include #include +#include namespace DB @@ -147,7 +148,6 @@ private: /** ClickHouse query tree pass manager. * - * TODO: Support _shard_num into shardNum() rewriting. * TODO: Support logical expressions optimizer. * TODO: Support setting convert_query_to_cnf. * TODO: Support setting optimize_using_constraints. @@ -266,10 +266,9 @@ void addQueryTreePasses(QueryTreePassManager & manager) manager.addPass(std::make_unique()); manager.addPass(std::make_unique()); - manager.addPass(std::make_unique()); - manager.addPass(std::make_unique()); + manager.addPass(std::make_unique()); } } diff --git a/src/Functions/getScalar.cpp b/src/Functions/getScalar.cpp index 1a26ac5f4da..d72c84b8528 100644 --- a/src/Functions/getScalar.cpp +++ b/src/Functions/getScalar.cpp @@ -121,7 +121,12 @@ public: ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override { - return ColumnConst::create(scalar.column, input_rows_count); + auto result = ColumnConst::create(scalar.column, input_rows_count); + + if (!isSuitableForConstantFolding()) + return result->convertToFullColumnIfConst(); + + return result; } private: diff --git a/tests/queries/0_stateless/01018_Distributed__shard_num.reference b/tests/queries/0_stateless/01018_Distributed__shard_num.reference index 46963a006ec..e50ae93402b 100644 --- a/tests/queries/0_stateless/01018_Distributed__shard_num.reference +++ b/tests/queries/0_stateless/01018_Distributed__shard_num.reference @@ -85,7 +85,7 @@ SELECT a._shard_num, a.key, b.host_name, b.host_address IN ('::1', '127.0.0.1'), FROM dist_1 a JOIN system.clusters b ON a._shard_num = b.shard_num -WHERE b.cluster = 'test_cluster_two_shards_localhost'; -- { serverError 47; } +WHERE b.cluster = 'test_cluster_two_shards_localhost'; -- { serverError 403 } SELECT 'dist_3'; dist_3 SELECT * FROM dist_3; diff --git a/tests/queries/0_stateless/01018_Distributed__shard_num.sql b/tests/queries/0_stateless/01018_Distributed__shard_num.sql index d3f4e1ac527..ce522227006 100644 --- a/tests/queries/0_stateless/01018_Distributed__shard_num.sql +++ b/tests/queries/0_stateless/01018_Distributed__shard_num.sql @@ -2,6 +2,7 @@ -- make the order static SET max_threads = 1; +SET allow_experimental_analyzer = 1; DROP TABLE IF EXISTS mem1; DROP TABLE IF EXISTS mem2; @@ -79,7 +80,7 @@ SELECT a._shard_num, a.key, b.host_name, b.host_address IN ('::1', '127.0.0.1'), FROM dist_1 a JOIN system.clusters b ON a._shard_num = b.shard_num -WHERE b.cluster = 'test_cluster_two_shards_localhost'; -- { serverError 47; } +WHERE b.cluster = 'test_cluster_two_shards_localhost'; -- { serverError 403 } SELECT 'dist_3'; SELECT * FROM dist_3; diff --git a/tests/queries/0_stateless/02163_shard_num.reference b/tests/queries/0_stateless/02163_shard_num.reference index a109d5d2b6b..f93803789a1 100644 --- a/tests/queries/0_stateless/02163_shard_num.reference +++ b/tests/queries/0_stateless/02163_shard_num.reference @@ -1,4 +1,5 @@ --- { echo } +-- { echoOn } + SELECT shardNum() AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) GROUP BY _shard_num; 2 1 1 1 @@ -14,4 +15,4 @@ SELECT _shard_num AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, SELECT a._shard_num AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) a GROUP BY shard_num; 2 1 1 1 -SELECT _shard_num FROM remote('127.1', system.one) AS a INNER JOIN (SELECT _shard_num FROM system.one) AS b USING (dummy); -- { serverError UNKNOWN_IDENTIFIER } +SELECT _shard_num FROM remote('127.1', system.one) AS a INNER JOIN (SELECT _shard_num FROM system.one) AS b USING (dummy); -- { serverError UNSUPPORTED_METHOD } diff --git a/tests/queries/0_stateless/02163_shard_num.sql b/tests/queries/0_stateless/02163_shard_num.sql index 27d40b3c976..65a2a8ca7c2 100644 --- a/tests/queries/0_stateless/02163_shard_num.sql +++ b/tests/queries/0_stateless/02163_shard_num.sql @@ -1,7 +1,12 @@ --- { echo } +SET allow_experimental_analyzer = 1; + +-- { echoOn } + SELECT shardNum() AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) GROUP BY _shard_num; SELECT shardNum() AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) GROUP BY shard_num; SELECT _shard_num AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) GROUP BY _shard_num; SELECT _shard_num AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) GROUP BY shard_num; SELECT a._shard_num AS shard_num, sum(1) as rows FROM remote('127.{1,2}', system, one) a GROUP BY shard_num; -SELECT _shard_num FROM remote('127.1', system.one) AS a INNER JOIN (SELECT _shard_num FROM system.one) AS b USING (dummy); -- { serverError UNKNOWN_IDENTIFIER } +SELECT _shard_num FROM remote('127.1', system.one) AS a INNER JOIN (SELECT _shard_num FROM system.one) AS b USING (dummy); -- { serverError UNSUPPORTED_METHOD } + +-- { echoOff } From bff55d5b2a493d1041f26edfacf0cde6ffa319e0 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 21 Feb 2023 21:51:48 +0100 Subject: [PATCH 159/342] Planner table expression data column order fix --- src/Planner/PlannerJoinTree.cpp | 2 +- src/Planner/TableExpressionData.h | 31 ++++---- .../02514_analyzer_drop_join_on.reference | 72 +++++++++---------- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 02c396542b1..a890780659c 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -259,7 +259,7 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl from_stage = storage->getQueryProcessingStage(query_context, select_query_options.to_stage, storage_snapshot, table_expression_query_info); - Names columns_names = table_expression_data.getColumnNames(); + auto columns_names = table_expression_data.getColumnNames(); /** The current user must have the SELECT privilege. * We do not check access rights for table functions because they have been already checked in ITableFunction::execute(). diff --git a/src/Planner/TableExpressionData.h b/src/Planner/TableExpressionData.h index 6b4a9b4748d..e828f128e38 100644 --- a/src/Planner/TableExpressionData.h +++ b/src/Planner/TableExpressionData.h @@ -63,9 +63,7 @@ public: if (hasColumn(column.name)) throw Exception(ErrorCodes::LOGICAL_ERROR, "Column with name {} already exists"); - column_name_to_column.emplace(column.name, column); - column_name_to_column_identifier.emplace(column.name, column_identifier); - column_identifier_to_column_name.emplace(column_identifier, column.name); + addColumnImpl(column, column_identifier); } /** Add column if it does not exists in table expression data. @@ -76,9 +74,7 @@ public: if (hasColumn(column.name)) return; - column_name_to_column.emplace(column.name, column); - column_name_to_column_identifier.emplace(column.name, column_identifier); - column_identifier_to_column_name.emplace(column_identifier, column.name); + addColumnImpl(column, column_identifier); } /// Add alias column name @@ -100,15 +96,9 @@ public: } /// Get column names - Names getColumnNames() const + const Names & getColumnNames() const { - Names result; - result.reserve(column_name_to_column.size()); - - for (const auto & [column_name, _] : column_name_to_column) - result.push_back(column_name); - - return result; + return column_names; } ColumnIdentifiers getColumnIdentifiers() const @@ -142,7 +132,6 @@ public: auto it = column_name_to_column.find(column_name); if (it == column_name_to_column.end()) { - auto column_names = getColumnNames(); throw Exception(ErrorCodes::LOGICAL_ERROR, "Column for column name {} does not exists. There are only column names: {}", column_name, @@ -172,7 +161,6 @@ public: auto it = column_name_to_column_identifier.find(column_name); if (it == column_name_to_column_identifier.end()) { - auto column_names = getColumnNames(); throw Exception(ErrorCodes::LOGICAL_ERROR, "Column identifier for column name {} does not exists. There are only column names: {}", column_name, @@ -240,6 +228,17 @@ public: } private: + void addColumnImpl(const NameAndTypePair & column, const ColumnIdentifier & column_identifier) + { + column_names.push_back(column.name); + column_name_to_column.emplace(column.name, column); + column_name_to_column_identifier.emplace(column.name, column_identifier); + column_identifier_to_column_name.emplace(column_identifier, column.name); + } + + /// Valid for table, table function, array join, query, union nodes + Names column_names; + /// Valid for table, table function, array join, query, union nodes ColumnNameToColumn column_name_to_column; diff --git a/tests/queries/0_stateless/02514_analyzer_drop_join_on.reference b/tests/queries/0_stateless/02514_analyzer_drop_join_on.reference index 0f6fa7e2e66..af6f07e3f95 100644 --- a/tests/queries/0_stateless/02514_analyzer_drop_join_on.reference +++ b/tests/queries/0_stateless/02514_analyzer_drop_join_on.reference @@ -24,15 +24,15 @@ Header: count() UInt64 Header: default.a.a2_4 String default.b.b1_0 UInt64 Join (JOIN FillRightFirst) - Header: default.a.a2_4 String - default.a.a1_1 UInt64 + Header: default.a.a1_1 UInt64 + default.a.a2_4 String default.b.b1_0 UInt64 Expression ((JOIN actions + Change column names to column identifiers)) - Header: default.a.a2_4 String - default.a.a1_1 UInt64 + Header: default.a.a1_1 UInt64 + default.a.a2_4 String ReadFromStorage (Memory) - Header: a2 String - a1 UInt64 + Header: a1 UInt64 + a2 String Expression ((JOIN actions + Change column names to column identifiers)) Header: default.b.b1_0 UInt64 ReadFromStorage (Memory) @@ -52,27 +52,27 @@ Expression ((Project names + (Projection + DROP unused columns after JOIN))) Header: a2 String d2 String Join (JOIN FillRightFirst) - Header: default.a.k_2 UInt64 - default.a.a2_0 String + Header: default.a.a2_0 String + default.a.k_2 UInt64 default.d.d2_1 String Expression (DROP unused columns after JOIN) - Header: default.a.k_2 UInt64 - default.a.a2_0 String + Header: default.a.a2_0 String + default.a.k_2 UInt64 Join (JOIN FillRightFirst) - Header: default.a.k_2 UInt64 - default.a.a2_0 String + Header: default.a.a2_0 String + default.a.k_2 UInt64 Expression (DROP unused columns after JOIN) - Header: default.a.k_2 UInt64 - default.a.a2_0 String + Header: default.a.a2_0 String + default.a.k_2 UInt64 Join (JOIN FillRightFirst) - Header: default.a.k_2 UInt64 - default.a.a2_0 String + Header: default.a.a2_0 String + default.a.k_2 UInt64 Expression (Change column names to column identifiers) - Header: default.a.k_2 UInt64 - default.a.a2_0 String + Header: default.a.a2_0 String + default.a.k_2 UInt64 ReadFromStorage (Memory) - Header: k UInt64 - a2 String + Header: a2 String + k UInt64 Expression (Change column names to column identifiers) Header: default.b.k_3 UInt64 ReadFromStorage (Memory) @@ -82,11 +82,11 @@ Header: a2 String ReadFromStorage (Memory) Header: k UInt64 Expression (Change column names to column identifiers) - Header: default.d.k_5 UInt64 - default.d.d2_1 String + Header: default.d.d2_1 String + default.d.k_5 UInt64 ReadFromStorage (Memory) - Header: k UInt64 - d2 String + Header: d2 String + k UInt64 EXPLAIN PLAN header = 1 SELECT b.bx FROM a JOIN (SELECT b1, b2 || 'x' AS bx FROM b ) AS b ON b.b1 = a.a1 @@ -123,28 +123,28 @@ Header: bx String b.bx_0 String b.b1_1 UInt64 Join (JOIN FillRightFirst) - Header: default.a.a2_6 String - default.a.a1_2 UInt64 + Header: default.a.a1_2 UInt64 + default.a.a2_6 String b.bx_0 String b.b1_1 UInt64 Expression ((JOIN actions + Change column names to column identifiers)) - Header: default.a.a2_6 String - default.a.a1_2 UInt64 + Header: default.a.a1_2 UInt64 + default.a.a2_6 String ReadFromStorage (Memory) - Header: a2 String - a1 UInt64 + Header: a1 UInt64 + a2 String Expression ((JOIN actions + (Change column names to column identifiers + (Project names + (Projection + Change column names to column identifiers))))) Header: b.b1_1 UInt64 b.bx_0 String ReadFromStorage (Memory) - Header: b2 String - b1 UInt64 + Header: b1 UInt64 + b2 String Expression ((JOIN actions + Change column names to column identifiers)) - Header: default.c.c2_5 String - default.c.c1_3 UInt64 + Header: default.c.c1_3 UInt64 + default.c.c2_5 String ReadFromStorage (Memory) - Header: c2 String - c1 UInt64 + Header: c1 UInt64 + c2 String Expression ((JOIN actions + (Change column names to column identifiers + (Project names + (Projection + Change column names to column identifiers))))) Header: d.d1_4 UInt64 ReadFromStorage (SystemNumbers) From 07e114d8f6e6655571bcfd6d1c379604e1553fd5 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Wed, 22 Feb 2023 15:03:16 +0100 Subject: [PATCH 160/342] Analyzer WITH FILL and INTERPOLATE fix --- src/Planner/Planner.cpp | 14 ++-- src/Planner/PlannerExpressionAnalysis.cpp | 69 +++++++++++++++++-- src/Planner/PlannerExpressionAnalysis.h | 1 + .../0_stateless/02233_interpolate_1.sql | 4 +- 4 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/Planner/Planner.cpp b/src/Planner/Planner.cpp index fff539f59d6..c5c9703ef6a 100644 --- a/src/Planner/Planner.cpp +++ b/src/Planner/Planner.cpp @@ -591,20 +591,24 @@ void addWithFillStepIfNeeded(QueryPlan & query_plan, if (query_node.hasInterpolate()) { auto interpolate_actions_dag = std::make_shared(); + auto query_plan_columns = query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(); + for (auto & query_plan_column : query_plan_columns) + { + query_plan_column.column = nullptr; + interpolate_actions_dag->addInput(query_plan_column); + } auto & interpolate_list_node = query_node.getInterpolate()->as(); auto & interpolate_list_nodes = interpolate_list_node.getNodes(); if (interpolate_list_nodes.empty()) { - auto query_plan_columns = query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(); - for (auto & query_plan_column : query_plan_columns) + for (const auto * input_node : interpolate_actions_dag->getInputs()) { - if (column_names_with_fill.contains(query_plan_column.name)) + if (column_names_with_fill.contains(input_node->result_name)) continue; - const auto * input_action_node = &interpolate_actions_dag->addInput(query_plan_column); - interpolate_actions_dag->getOutputs().push_back(input_action_node); + interpolate_actions_dag->getOutputs().push_back(input_node); } } else diff --git a/src/Planner/PlannerExpressionAnalysis.cpp b/src/Planner/PlannerExpressionAnalysis.cpp index a813d3b753a..0e97e69cd67 100644 --- a/src/Planner/PlannerExpressionAnalysis.cpp +++ b/src/Planner/PlannerExpressionAnalysis.cpp @@ -359,7 +359,7 @@ SortAnalysisResult analyzeSort(const QueryNode & query_node, before_sort_actions_outputs.clear(); PlannerActionsVisitor actions_visitor(planner_context); - + bool has_with_fill = false; std::unordered_set before_sort_actions_dag_output_node_names; /** We add only sort node sort expression in before ORDER BY actions DAG. @@ -370,6 +370,7 @@ SortAnalysisResult analyzeSort(const QueryNode & query_node, { auto & sort_node_typed = sort_node->as(); auto expression_dag_nodes = actions_visitor.visit(before_sort_actions, sort_node_typed.getExpression()); + has_with_fill |= sort_node_typed.withFill(); for (auto & action_dag_node : expression_dag_nodes) { @@ -381,10 +382,55 @@ SortAnalysisResult analyzeSort(const QueryNode & query_node, } } + if (has_with_fill) + { + for (auto & output_node : before_sort_actions_outputs) + output_node = &before_sort_actions->materializeNode(*output_node); + } + + /// We add only INPUT columns necessary for INTERPOLATE expression in before ORDER BY actions DAG + if (query_node.hasInterpolate()) + { + auto & interpolate_list_node = query_node.getInterpolate()->as(); + + PlannerActionsVisitor interpolate_actions_visitor(planner_context); + auto interpolate_actions_dag = std::make_shared(); + + for (auto & interpolate_node : interpolate_list_node.getNodes()) + { + auto & interpolate_node_typed = interpolate_node->as(); + interpolate_actions_visitor.visit(interpolate_actions_dag, interpolate_node_typed.getExpression()); + interpolate_actions_visitor.visit(interpolate_actions_dag, interpolate_node_typed.getInterpolateExpression()); + } + + std::unordered_map before_sort_actions_inputs_name_to_node; + for (const auto & node : before_sort_actions->getInputs()) + before_sort_actions_inputs_name_to_node.emplace(node->result_name, node); + + for (const auto & node : interpolate_actions_dag->getNodes()) + { + if (before_sort_actions_dag_output_node_names.contains(node.result_name) || + node.type != ActionsDAG::ActionType::INPUT) + continue; + + auto input_node_it = before_sort_actions_inputs_name_to_node.find(node.result_name); + if (input_node_it == before_sort_actions_inputs_name_to_node.end()) + { + auto input_column = ColumnWithTypeAndName{node.column, node.result_type, node.result_name}; + const auto * input_node = &before_sort_actions->addInput(std::move(input_column)); + auto [it, _] = before_sort_actions_inputs_name_to_node.emplace(node.result_name, input_node); + input_node_it = it; + } + + before_sort_actions_outputs.push_back(input_node_it->second); + before_sort_actions_dag_output_node_names.insert(node.result_name); + } + } + auto actions_step_before_sort = std::make_unique(before_sort_actions); actions_chain.addStep(std::move(actions_step_before_sort)); - return SortAnalysisResult{std::move(before_sort_actions)}; + return SortAnalysisResult{std::move(before_sort_actions), has_with_fill}; } /** Construct limit by analysis result. @@ -488,21 +534,30 @@ PlannerExpressionsAnalysisResult buildExpressionAnalysisResult(const QueryTreeNo const auto * chain_available_output_columns = actions_chain.getLastStepAvailableOutputColumnsOrNull(); auto project_names_input = chain_available_output_columns ? *chain_available_output_columns : join_tree_input_columns; + bool has_with_fill = sort_analysis_result_optional.has_value() && sort_analysis_result_optional->has_with_fill; - /** If there is DISTINCT we must preserve non constant projection output columns + /** If there is WITH FILL we must use non constant projection columns. + * + * Example: SELECT 1 AS value ORDER BY value ASC WITH FILL FROM 0 TO 5 STEP 1; + * + * If there is DISTINCT we must preserve non constant projection output columns * in project names actions, to prevent removing of unused expressions during chain finalize. * * Example: SELECT DISTINCT id, 1 AS value FROM test_table ORDER BY id; */ - if (query_node.isDistinct()) + if (has_with_fill || query_node.isDistinct()) { std::unordered_set projection_column_names; - for (auto & [column_name, _] : projection_analysis_result.projection_column_names_with_display_aliases) - projection_column_names.insert(column_name); + + if (query_node.isDistinct()) + { + for (auto & [column_name, _] : projection_analysis_result.projection_column_names_with_display_aliases) + projection_column_names.insert(column_name); + } for (auto & column : project_names_input) { - if (projection_column_names.contains(column.name)) + if (has_with_fill || projection_column_names.contains(column.name)) column.column = nullptr; } } diff --git a/src/Planner/PlannerExpressionAnalysis.h b/src/Planner/PlannerExpressionAnalysis.h index b6d6ed96e9d..792cfdec2ff 100644 --- a/src/Planner/PlannerExpressionAnalysis.h +++ b/src/Planner/PlannerExpressionAnalysis.h @@ -48,6 +48,7 @@ struct WindowAnalysisResult struct SortAnalysisResult { ActionsDAGPtr before_order_by_actions; + bool has_with_fill = false; }; struct LimitByAnalysisResult diff --git a/tests/queries/0_stateless/02233_interpolate_1.sql b/tests/queries/0_stateless/02233_interpolate_1.sql index 229c36e23fb..b08f4a23898 100644 --- a/tests/queries/0_stateless/02233_interpolate_1.sql +++ b/tests/queries/0_stateless/02233_interpolate_1.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + # Test WITH FILL without INTERPOLATE SELECT n, source, inter FROM ( SELECT toFloat32(number % 10) AS n, 'original' AS source, number as inter FROM numbers(10) WHERE number % 3 = 1 @@ -36,7 +38,7 @@ SELECT n, source, inter FROM ( # Test INTERPOLATE with inconsistent column - should produce error SELECT n, source, inter FROM ( SELECT toFloat32(number % 10) AS n, 'original' AS source, number as inter FROM numbers(10) WHERE number % 3 = 1 -) ORDER BY n WITH FILL FROM 0 TO 11.51 STEP 0.5 INTERPOLATE (inter AS source); -- { serverError 32 } +) ORDER BY n WITH FILL FROM 0 TO 11.51 STEP 0.5 INTERPOLATE (inter AS source); -- { serverError 6 } # Test INTERPOLATE with aliased column SELECT n, source, inter + 1 AS inter_p FROM ( From 51ee007e01e37625f2a15838849766676a641770 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Fri, 24 Feb 2023 13:46:09 +0100 Subject: [PATCH 161/342] Fixed tests --- .../Passes/CustomizeFunctionsPass.cpp | 63 ----------- .../Passes/NormalizeCountVariantsPass.cpp | 3 +- src/Analyzer/Passes/QueryAnalysisPass.cpp | 100 +++++++++++++----- src/Interpreters/InterpreterCreateQuery.cpp | 6 -- .../getHeaderForProcessingStage.cpp | 39 +------ src/Planner/Planner.cpp | 38 ++----- src/Planner/PlannerJoinTree.cpp | 63 +++++++---- src/Planner/Utils.cpp | 34 ++++++ src/Planner/Utils.h | 6 ++ .../optimizePrimaryKeyCondition.cpp | 17 +-- .../QueryPlan/ReadFromMergeTree.cpp | 4 +- src/Processors/QueryPlan/ReadFromMergeTree.h | 19 +--- .../QueryPlan/SourceStepWithFilter.h | 45 ++++++++ src/Storages/StorageDistributed.cpp | 42 +------- src/Storages/StorageDummy.cpp | 56 ++++++++++ src/Storages/StorageDummy.h | 74 +++++++++++++ src/Storages/StorageMerge.cpp | 8 +- src/Storages/StorageMerge.h | 15 +-- .../queries/0_stateless/00390_array_sort.sql | 4 +- .../00740_database_in_nested_view.sql | 1 + ...00936_function_result_with_operator_in.sql | 5 +- ...62_pm_all_join_with_block_continuation.sql | 26 ++--- ...2_multiple_joins_with_union_join.reference | 4 +- .../01852_multiple_joins_with_union_join.sql | 4 +- .../01936_quantiles_cannot_return_null.sql | 8 +- .../02339_analyzer_matcher_basic.reference | 27 +++-- .../02339_analyzer_matcher_basic.sql | 26 +++-- .../02378_analyzer_projection_names.reference | 2 +- 28 files changed, 425 insertions(+), 314 deletions(-) create mode 100644 src/Processors/QueryPlan/SourceStepWithFilter.h create mode 100644 src/Storages/StorageDummy.cpp create mode 100644 src/Storages/StorageDummy.h diff --git a/src/Analyzer/Passes/CustomizeFunctionsPass.cpp b/src/Analyzer/Passes/CustomizeFunctionsPass.cpp index 3333008a0c0..55307fd39e7 100644 --- a/src/Analyzer/Passes/CustomizeFunctionsPass.cpp +++ b/src/Analyzer/Passes/CustomizeFunctionsPass.cpp @@ -34,53 +34,6 @@ public: auto function_name = function_node->getFunctionName(); auto function_name_lowercase = Poco::toLower(function_name); - if (function_node->isAggregateFunction() || function_node->isWindowFunction()) - { - auto count_distinct_implementation_function_name = String(settings.count_distinct_implementation); - - /// Replace aggregateFunctionIfDistinct into aggregateFunctionDistinctIf to make execution more optimal - if (function_name_lowercase.ends_with("ifdistinct")) - { - size_t prefix_length = function_name_lowercase.size() - strlen("ifdistinct"); - auto updated_function_name = function_name_lowercase.substr(0, prefix_length) + "DistinctIf"; - resolveAggregateOrWindowFunctionNode(*function_node, updated_function_name); - function_name = function_node->getFunctionName(); - function_name_lowercase = Poco::toLower(function_name); - } - - /** Move -OrNull suffix ahead, this should execute after add -OrNull suffix. - * Used to rewrite aggregate functions with -OrNull suffix in some cases. - * Example: sumIfOrNull. - * Result: sumOrNullIf. - */ - if (function_name.ends_with("OrNull")) - { - auto function_properies = AggregateFunctionFactory::instance().tryGetProperties(function_name); - if (function_properies && !function_properies->returns_default_when_only_null) - { - size_t function_name_size = function_name.size(); - - static constexpr std::array suffixes_to_replace = {"MergeState", "Merge", "State", "If"}; - for (const auto & suffix : suffixes_to_replace) - { - auto suffix_string_value = String(suffix); - auto suffix_to_check = suffix_string_value + "OrNull"; - - if (!function_name.ends_with(suffix_to_check)) - continue; - - auto updated_function_name = function_name.substr(0, function_name_size - suffix_to_check.size()) + "OrNull" + suffix_string_value; - resolveAggregateOrWindowFunctionNode(*function_node, updated_function_name); - function_name = function_node->getFunctionName(); - function_name_lowercase = Poco::toLower(function_name); - break; - } - } - } - - return; - } - if (settings.transform_null_in) { auto function_result_type = function_node->getResultType(); @@ -106,22 +59,6 @@ public: } } - static inline void resolveAggregateOrWindowFunctionNode(FunctionNode & function_node, const String & aggregate_function_name) - { - auto function_aggregate_function = function_node.getAggregateFunction(); - - AggregateFunctionProperties properties; - auto aggregate_function = AggregateFunctionFactory::instance().get(aggregate_function_name, - function_aggregate_function->getArgumentTypes(), - function_aggregate_function->getParameters(), - properties); - - if (function_node.isAggregateFunction()) - function_node.resolveAsAggregateFunction(std::move(aggregate_function)); - else if (function_node.isWindowFunction()) - function_node.resolveAsWindowFunction(std::move(aggregate_function)); - } - inline void resolveOrdinaryFunctionNode(FunctionNode & function_node, const String & function_name) const { auto function = FunctionFactory::instance().get(function_name, getContext()); diff --git a/src/Analyzer/Passes/NormalizeCountVariantsPass.cpp b/src/Analyzer/Passes/NormalizeCountVariantsPass.cpp index 705018d108f..d36be98751c 100644 --- a/src/Analyzer/Passes/NormalizeCountVariantsPass.cpp +++ b/src/Analyzer/Passes/NormalizeCountVariantsPass.cpp @@ -46,8 +46,7 @@ public: } else if (function_node->getFunctionName() == "sum" && first_argument_constant_literal.getType() == Field::Types::UInt64 && - first_argument_constant_literal.get() == 1 && - !getSettings().aggregate_functions_null_for_empty) + first_argument_constant_literal.get() == 1) { resolveAsCountAggregateFunction(*function_node); function_node->getArguments().getNodes().clear(); diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 147c0660bc2..9d1a096e582 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -1168,9 +1168,11 @@ private: static void validateJoinTableExpressionWithoutAlias(const QueryTreeNodePtr & join_node, const QueryTreeNodePtr & table_expression_node, IdentifierResolveScope & scope); + static std::pair recursivelyCollectMaxOrdinaryExpressions(QueryTreeNodePtr & node, QueryTreeNodes & into); + static void expandGroupByAll(QueryNode & query_tree_node_typed); - static std::pair recursivelyCollectMaxOrdinaryExpressions(QueryTreeNodePtr & node, QueryTreeNodes & into); + static std::string rewriteAggregateFunctionNameIfNeeded(const std::string & aggregate_function_name, const ContextPtr & context); /// Resolve identifier functions @@ -2238,9 +2240,72 @@ void QueryAnalyzer::expandGroupByAll(QueryNode & query_tree_node_typed) for (auto & node : projection_list.getNodes()) recursivelyCollectMaxOrdinaryExpressions(node, group_by_nodes); - } +std::string QueryAnalyzer::rewriteAggregateFunctionNameIfNeeded(const std::string & aggregate_function_name, const ContextPtr & context) +{ + std::string result_aggregate_function_name = aggregate_function_name; + auto aggregate_function_name_lowercase = Poco::toLower(aggregate_function_name); + + const auto & settings = context->getSettingsRef(); + + if (aggregate_function_name_lowercase == "countdistinct") + { + result_aggregate_function_name = settings.count_distinct_implementation; + } + else if (aggregate_function_name_lowercase == "countdistinctif" || aggregate_function_name_lowercase == "countifdistinct") + { + result_aggregate_function_name = settings.count_distinct_implementation; + result_aggregate_function_name += "If"; + } + + /// Replace aggregateFunctionIfDistinct into aggregateFunctionDistinctIf to make execution more optimal + if (result_aggregate_function_name.ends_with("ifdistinct")) + { + size_t prefix_length = result_aggregate_function_name.size() - strlen("ifdistinct"); + result_aggregate_function_name = result_aggregate_function_name.substr(0, prefix_length) + "DistinctIf"; + } + + bool need_add_or_null = settings.aggregate_functions_null_for_empty && !result_aggregate_function_name.ends_with("OrNull"); + if (need_add_or_null) + { + auto properties = AggregateFunctionFactory::instance().tryGetProperties(result_aggregate_function_name); + if (!properties->returns_default_when_only_null) + result_aggregate_function_name += "OrNull"; + } + + /** Move -OrNull suffix ahead, this should execute after add -OrNull suffix. + * Used to rewrite aggregate functions with -OrNull suffix in some cases. + * Example: sumIfOrNull. + * Result: sumOrNullIf. + */ + if (result_aggregate_function_name.ends_with("OrNull")) + { + auto function_properies = AggregateFunctionFactory::instance().tryGetProperties(result_aggregate_function_name); + if (function_properies && !function_properies->returns_default_when_only_null) + { + size_t function_name_size = result_aggregate_function_name.size(); + + static constexpr std::array suffixes_to_replace = {"MergeState", "Merge", "State", "If"}; + for (const auto & suffix : suffixes_to_replace) + { + auto suffix_string_value = String(suffix); + auto suffix_to_check = suffix_string_value + "OrNull"; + + if (!result_aggregate_function_name.ends_with(suffix_to_check)) + continue; + + result_aggregate_function_name = result_aggregate_function_name.substr(0, function_name_size - suffix_to_check.size()); + result_aggregate_function_name += "OrNull"; + result_aggregate_function_name += suffix_string_value; + + break; + } + } + } + + return result_aggregate_function_name; +} /// Resolve identifier functions implementation @@ -3003,7 +3068,7 @@ QueryTreeNodePtr QueryAnalyzer::tryResolveIdentifierFromJoin(const IdentifierLoo { resolved_identifier = resolved_identifier->clone(); auto & resolved_column = resolved_identifier->as(); - resolved_column.setColumnType(makeNullableSafe(resolved_column.getColumnType())); + resolved_column.setColumnType(makeNullableOrLowCardinalityNullable(resolved_column.getColumnType())); } return resolved_identifier; @@ -3988,6 +4053,9 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I node = replace_expression->clone(); node_projection_names = resolveExpressionNode(node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); + if (node_projection_names.size() == 1) + node_projection_names[0] = column_name; + execute_replace_transformer = true; } @@ -4678,8 +4746,6 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } } - const auto & settings = scope.context->getSettingsRef(); - if (function_node.isWindowFunction()) { if (!AggregateFunctionFactory::instance().isAggregateFunctionName(function_name)) @@ -4694,12 +4760,10 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi "Window function '{}' does not support lambda arguments", function_name); - bool need_add_or_null = settings.aggregate_functions_null_for_empty && !function_name.ends_with("OrNull"); + std::string aggregate_function_name = rewriteAggregateFunctionNameIfNeeded(function_name, scope.context); AggregateFunctionProperties properties; - auto aggregate_function = need_add_or_null - ? AggregateFunctionFactory::instance().get(function_name + "OrNull", argument_types, parameters, properties) - : AggregateFunctionFactory::instance().get(function_name, argument_types, parameters, properties); + auto aggregate_function = AggregateFunctionFactory::instance().get(aggregate_function_name, argument_types, parameters, properties); function_node.resolveAsWindowFunction(std::move(aggregate_function)); @@ -4758,24 +4822,10 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi "Aggregate function '{}' does not support lambda arguments", function_name); - auto function_name_lowercase = Poco::toLower(function_name); - - if (function_name_lowercase == "countdistinct") - { - function_name = scope.context->getSettingsRef().count_distinct_implementation; - } - else if (function_name_lowercase == "countdistinctif" || function_name_lowercase == "countifdistinct") - { - function_name = scope.context->getSettingsRef().count_distinct_implementation; - function_name += "If"; - } - - bool need_add_or_null = settings.aggregate_functions_null_for_empty && !function_name.ends_with("OrNull"); - if (need_add_or_null) - function_name += "OrNull"; + std::string aggregate_function_name = rewriteAggregateFunctionNameIfNeeded(function_name, scope.context); AggregateFunctionProperties properties; - auto aggregate_function = AggregateFunctionFactory::instance().get(function_name, argument_types, parameters, properties); + auto aggregate_function = AggregateFunctionFactory::instance().get(aggregate_function_name, argument_types, parameters, properties); function_node.resolveAsAggregateFunction(std::move(aggregate_function)); diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index 29b7a4db609..edc6cc92fd7 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -1224,12 +1224,6 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create) output_columns.push_back(output_column->cloneEmpty()); } } - - ActionsDAG::makeConvertingActions( - input_columns, - output_columns, - ActionsDAG::MatchColumnsMode::Position - ); } } diff --git a/src/Interpreters/getHeaderForProcessingStage.cpp b/src/Interpreters/getHeaderForProcessingStage.cpp index ab10bd2024c..3f52b2f5099 100644 --- a/src/Interpreters/getHeaderForProcessingStage.cpp +++ b/src/Interpreters/getHeaderForProcessingStage.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -83,44 +84,6 @@ bool removeJoin(ASTSelectQuery & select, TreeRewriterResult & rewriter_result, C return true; } -class StorageDummy : public IStorage -{ -public: - StorageDummy(const StorageID & table_id_, const ColumnsDescription & columns_) - : IStorage(table_id_) - { - StorageInMemoryMetadata storage_metadata; - storage_metadata.setColumns(columns_); - setInMemoryMetadata(storage_metadata); - } - - std::string getName() const override { return "StorageDummy"; } - - bool supportsSampling() const override { return true; } - bool supportsFinal() const override { return true; } - bool supportsPrewhere() const override { return true; } - bool supportsSubcolumns() const override { return true; } - bool supportsDynamicSubcolumns() const override { return true; } - bool canMoveConditionsToPrewhere() const override { return false; } - - QueryProcessingStage::Enum - getQueryProcessingStage(ContextPtr, QueryProcessingStage::Enum, const StorageSnapshotPtr &, SelectQueryInfo &) const override - { - throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "StorageDummy does not support getQueryProcessingStage method"); - } - - Pipe read(const Names & /*column_names*/, - const StorageSnapshotPtr & /*storage_snapshot*/, - SelectQueryInfo & /*query_info*/, - ContextPtr /*context*/, - QueryProcessingStage::Enum /*processed_stage*/, - size_t /*max_block_size*/, - size_t /*num_streams*/) override - { - throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "StorageDummy does not support read method"); - } -}; - Block getHeaderForProcessingStage( const Names & column_names, const StorageSnapshotPtr & storage_snapshot, diff --git a/src/Planner/Planner.cpp b/src/Planner/Planner.cpp index c5c9703ef6a..49120c91db2 100644 --- a/src/Planner/Planner.cpp +++ b/src/Planner/Planner.cpp @@ -1178,36 +1178,14 @@ void Planner::buildPlanForQueryNode() collectTableExpressionData(query_tree, *planner_context); collectSets(query_tree, *planner_context); - QueryProcessingStage::Enum from_stage = QueryProcessingStage::FetchColumns; - - if (select_query_options.only_analyze) - { - Block join_tree_block; - - for (const auto & [_, table_expression_data] : planner_context->getTableExpressionNodeToData()) - { - for (const auto & [column_name, column] : table_expression_data.getColumnNameToColumn()) - { - const auto & column_identifier = table_expression_data.getColumnIdentifierOrThrow(column_name); - join_tree_block.insert(ColumnWithTypeAndName(column.type, column_identifier)); - } - } - - auto read_nothing_step = std::make_unique(join_tree_block); - read_nothing_step->setStepDescription("Read nothing"); - query_plan.addStep(std::move(read_nothing_step)); - } - else - { - auto top_level_identifiers = collectTopLevelColumnIdentifiers(query_tree, planner_context); - auto join_tree_query_plan = buildJoinTreeQueryPlan(query_tree, - select_query_info, - select_query_options, - top_level_identifiers, - planner_context); - from_stage = join_tree_query_plan.from_stage; - query_plan = std::move(join_tree_query_plan.query_plan); - } + auto top_level_identifiers = collectTopLevelColumnIdentifiers(query_tree, planner_context); + auto join_tree_query_plan = buildJoinTreeQueryPlan(query_tree, + select_query_info, + select_query_options, + top_level_identifiers, + planner_context); + auto from_stage = join_tree_query_plan.from_stage; + query_plan = std::move(join_tree_query_plan.query_plan); LOG_TRACE(&Poco::Logger::get("Planner"), "Query {} from stage {} to stage {}{}", query_tree->formatConvertedASTForErrorMessage(), diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index a890780659c..f8e4a3b7021 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include @@ -257,8 +257,6 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl else table_expression_query_info.table_expression_modifiers = table_function_node->getTableExpressionModifiers(); - from_stage = storage->getQueryProcessingStage(query_context, select_query_options.to_stage, storage_snapshot, table_expression_query_info); - auto columns_names = table_expression_data.getColumnNames(); /** The current user must have the SELECT privilege. @@ -308,7 +306,11 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } } - storage->read(query_plan, columns_names, storage_snapshot, table_expression_query_info, query_context, from_stage, max_block_size, max_streams); + if (!select_query_options.only_analyze) + { + from_stage = storage->getQueryProcessingStage(query_context, select_query_options.to_stage, storage_snapshot, table_expression_query_info); + storage->read(query_plan, columns_names, storage_snapshot, table_expression_query_info, query_context, from_stage, max_block_size, max_streams); + } if (query_plan.isInitialized()) { @@ -324,7 +326,7 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } else { - /// Create step which reads from empty source if storage has no data. + /// Create step which reads from empty source if storage has no data auto source_header = storage_snapshot->getSampleBlockForColumns(columns_names); Pipe pipe(std::make_shared(source_header)); auto read_from_pipe = std::make_unique(std::move(pipe)); @@ -334,22 +336,37 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } else if (query_node || union_node) { - if (table_expression_data.getColumnNames().empty()) + if (!select_query_options.only_analyze) { - const auto & projection_columns = query_node ? query_node->getProjectionColumns() : union_node->computeProjectionColumns(); - NamesAndTypesList projection_columns_list(projection_columns.begin(), projection_columns.end()); - auto additional_column_to_read = ExpressionActions::getSmallestColumn(projection_columns_list); + if (table_expression_data.getColumnNames().empty()) + { + const auto & projection_columns = query_node ? query_node->getProjectionColumns() : union_node->computeProjectionColumns(); + NamesAndTypesList projection_columns_list(projection_columns.begin(), projection_columns.end()); + auto additional_column_to_read = ExpressionActions::getSmallestColumn(projection_columns_list); - const auto & column_identifier = planner_context->getGlobalPlannerContext()->createColumnIdentifier(additional_column_to_read, table_expression); - table_expression_data.addColumn(additional_column_to_read, column_identifier); + const auto & column_identifier = planner_context->getGlobalPlannerContext()->createColumnIdentifier(additional_column_to_read, table_expression); + table_expression_data.addColumn(additional_column_to_read, column_identifier); + } + + auto subquery_options = select_query_options.subquery(); + Planner subquery_planner(table_expression, subquery_options, planner_context->getGlobalPlannerContext()); + /// Propagate storage limits to subquery + subquery_planner.addStorageLimits(*select_query_info.storage_limits); + subquery_planner.buildQueryPlanIfNeeded(); + query_plan = std::move(subquery_planner).extractQueryPlan(); } + else + { + auto projection_columns = query_node ? query_node->getProjectionColumns() : union_node->computeProjectionColumns(); + Block source_header; + for (auto & projection_column : projection_columns) + source_header.insert(ColumnWithTypeAndName(nullptr, projection_column.type, projection_column.name)); - auto subquery_options = select_query_options.subquery(); - Planner subquery_planner(table_expression, subquery_options, planner_context->getGlobalPlannerContext()); - /// Propagate storage limits to subquery - subquery_planner.addStorageLimits(*select_query_info.storage_limits); - subquery_planner.buildQueryPlanIfNeeded(); - query_plan = std::move(subquery_planner).extractQueryPlan(); + Pipe pipe(std::make_shared(source_header)); + auto read_from_pipe = std::make_unique(std::move(pipe)); + read_from_pipe->setStepDescription("Read from NullSource"); + query_plan.addStep(std::move(read_from_pipe)); + } } else { @@ -533,9 +550,15 @@ JoinTreeQueryPlan buildQueryPlanForJoinNode(const QueryTreeNodePtr & join_table_ for (auto & output_node : cast_actions_dag->getOutputs()) { - if (planner_context->getGlobalPlannerContext()->hasColumnIdentifier(output_node->result_name) && - output_node->result_type->canBeInsideNullable()) - output_node = &cast_actions_dag->addFunction(to_nullable_function, {output_node}, output_node->result_name); + if (planner_context->getGlobalPlannerContext()->hasColumnIdentifier(output_node->result_name)) + { + DataTypePtr type_to_check = output_node->result_type; + if (const auto * type_to_check_low_cardinality = typeid_cast(type_to_check.get())) + type_to_check = type_to_check_low_cardinality->getDictionaryType(); + + if (type_to_check->canBeInsideNullable()) + output_node = &cast_actions_dag->addFunction(to_nullable_function, {output_node}, output_node->result_name); + } } cast_actions_dag->projectInput(); diff --git a/src/Planner/Utils.cpp b/src/Planner/Utils.cpp index 6df024fee4c..5c5eadac55d 100644 --- a/src/Planner/Utils.cpp +++ b/src/Planner/Utils.cpp @@ -13,12 +13,17 @@ #include +#include + #include +#include #include #include #include #include +#include +#include #include #include @@ -357,4 +362,33 @@ std::optional tryExtractConstantFromConditionNode(const QueryTreeNodePtr & return predicate_value > 0; } +QueryTreeNodePtr replaceTablesAndTableFunctionsWithDummyTables(const QueryTreeNodePtr & query_node, + const ContextPtr & context, + ResultReplacementMap * result_replacement_map) +{ + auto & query_node_typed = query_node->as(); + auto table_expressions = extractTableExpressions(query_node_typed.getJoinTree()); + std::unordered_map replacement_map; + + for (auto & table_expression : table_expressions) + { + auto * table_node = table_expression->as(); + auto * table_function_node = table_expression->as(); + if (!table_node && !table_function_node) + continue; + + const auto & storage_snapshot = table_node ? table_node->getStorageSnapshot() : table_function_node->getStorageSnapshot(); + auto storage_dummy = std::make_shared(storage_snapshot->storage.getStorageID(), + storage_snapshot->metadata->getColumns()); + auto dummy_table_node = std::make_shared(std::move(storage_dummy), context); + + if (result_replacement_map) + result_replacement_map->emplace(table_expression, dummy_table_node); + + replacement_map.emplace(table_expression.get(), std::move(dummy_table_node)); + } + + return query_node->cloneAndReplace(replacement_map); +} + } diff --git a/src/Planner/Utils.h b/src/Planner/Utils.h index e304ae13eb5..0520bd67d26 100644 --- a/src/Planner/Utils.h +++ b/src/Planner/Utils.h @@ -66,4 +66,10 @@ QueryTreeNodePtr mergeConditionNodes(const QueryTreeNodes & condition_nodes, con /// Try extract boolean constant from condition node std::optional tryExtractConstantFromConditionNode(const QueryTreeNodePtr & condition_node); +/// Replace tables nodes and table function nodes with dummy table nodes +using ResultReplacementMap = std::unordered_map; +QueryTreeNodePtr replaceTablesAndTableFunctionsWithDummyTables(const QueryTreeNodePtr & query_node, + const ContextPtr & context, + ResultReplacementMap * result_replacement_map = nullptr); + } diff --git a/src/Processors/QueryPlan/Optimizations/optimizePrimaryKeyCondition.cpp b/src/Processors/QueryPlan/Optimizations/optimizePrimaryKeyCondition.cpp index cac4944bad7..753940339ea 100644 --- a/src/Processors/QueryPlan/Optimizations/optimizePrimaryKeyCondition.cpp +++ b/src/Processors/QueryPlan/Optimizations/optimizePrimaryKeyCondition.cpp @@ -1,8 +1,7 @@ #include #include #include -#include -#include +#include #include #include @@ -13,21 +12,15 @@ void optimizePrimaryKeyCondition(const Stack & stack) { const auto & frame = stack.back(); - auto * read_from_merge_tree = typeid_cast(frame.node->step.get()); - auto * read_from_merge = typeid_cast(frame.node->step.get()); - - if (!read_from_merge && !read_from_merge_tree) + auto * source_step_with_filter = dynamic_cast(frame.node->step.get()); + if (!source_step_with_filter) return; for (auto iter = stack.rbegin() + 1; iter != stack.rend(); ++iter) { if (auto * filter_step = typeid_cast(iter->node->step.get())) - { - if (read_from_merge_tree) - read_from_merge_tree->addFilter(filter_step->getExpression(), filter_step->getFilterColumnName()); - if (read_from_merge) - read_from_merge->addFilter(filter_step->getExpression(), filter_step->getFilterColumnName()); - } + source_step_with_filter->addFilter(filter_step->getExpression(), filter_step->getFilterColumnName()); + /// Note: actually, plan optimizations merge Filter and Expression steps. /// Ideally, chain should look like (Expression -> ...) -> (Filter -> ...) -> ReadFromStorage, /// So this is likely not needed. diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.cpp b/src/Processors/QueryPlan/ReadFromMergeTree.cpp index c70f6c6da30..fe279f3ab84 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.cpp +++ b/src/Processors/QueryPlan/ReadFromMergeTree.cpp @@ -148,7 +148,7 @@ ReadFromMergeTree::ReadFromMergeTree( Poco::Logger * log_, MergeTreeDataSelectAnalysisResultPtr analyzed_result_ptr_, bool enable_parallel_reading) - : ISourceStep(DataStream{.header = IMergeTreeSelectAlgorithm::transformHeader( + : SourceStepWithFilter(DataStream{.header = IMergeTreeSelectAlgorithm::transformHeader( storage_snapshot_->getSampleBlockForColumns(real_column_names_), getPrewhereInfoFromQueryInfo(query_info_), data_.getPartitionValueType(), @@ -1095,7 +1095,7 @@ MergeTreeDataSelectAnalysisResultPtr ReadFromMergeTree::selectRangesToRead(Merge return selectRangesToRead( std::move(parts), prewhere_info, - added_filter_nodes, + filter_nodes, storage_snapshot->metadata, storage_snapshot->getMetadataForQuery(), query_info, diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.h b/src/Processors/QueryPlan/ReadFromMergeTree.h index 4fd37fd6edf..9b28e903d21 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.h +++ b/src/Processors/QueryPlan/ReadFromMergeTree.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include #include #include @@ -29,7 +29,7 @@ using MergeTreeDataSelectAnalysisResultPtr = std::shared_ptrfindInOutputs(column_name)); - } - - void addFilterNodes(const ActionDAGNodes & filter_nodes) - { - for (const auto & node : filter_nodes.nodes) - added_filter_nodes.nodes.push_back(node); - } - StorageID getStorageID() const { return data.getStorageID(); } UInt64 getSelectedParts() const { return selected_parts; } UInt64 getSelectedRows() const { return selected_rows; } @@ -203,9 +191,6 @@ private: PrewhereInfoPtr prewhere_info; ExpressionActionsSettings actions_settings; - std::vector added_filter_dags; - ActionDAGNodes added_filter_nodes; - StorageSnapshotPtr storage_snapshot; StorageMetadataPtr metadata_for_reading; diff --git a/src/Processors/QueryPlan/SourceStepWithFilter.h b/src/Processors/QueryPlan/SourceStepWithFilter.h new file mode 100644 index 00000000000..a363451fff2 --- /dev/null +++ b/src/Processors/QueryPlan/SourceStepWithFilter.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include + +namespace DB +{ + +/** Source step that can use filters for more efficient pipeline initialization. + * Filters must be added before pipeline initialization. + */ +class SourceStepWithFilter : public ISourceStep +{ +public: + using Base = ISourceStep; + using Base::Base; + + const std::vector & getFilters() const + { + return filter_dags; + } + + const ActionDAGNodes & getFilterNodes() const + { + return filter_nodes; + } + + void addFilter(ActionsDAGPtr filter_dag, std::string column_name) + { + filter_nodes.nodes.push_back(&filter_dag->findInOutputs(column_name)); + filter_dags.push_back(std::move(filter_dag)); + } + + void addFilter(ActionsDAGPtr filter_dag, const ActionsDAG::Node * filter_node) + { + filter_nodes.nodes.push_back(filter_node); + filter_dags.push_back(std::move(filter_dag)); + } + +protected: + std::vector filter_dags; + ActionDAGNodes filter_nodes; +}; + +} diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 5516d6cadf0..286742586f2 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -133,7 +134,6 @@ namespace ErrorCodes extern const int DISTRIBUTED_TOO_MANY_PENDING_BYTES; extern const int ARGUMENT_OUT_OF_BOUND; extern const int TOO_LARGE_DISTRIBUTED_DEPTH; - extern const int UNSUPPORTED_METHOD; } namespace ActionLocks @@ -622,44 +622,6 @@ StorageSnapshotPtr StorageDistributed::getStorageSnapshotForQuery( namespace { -class StorageDistributedLocal : public IStorage -{ -public: - StorageDistributedLocal(const StorageID & table_id_, const ColumnsDescription & columns_) - : IStorage(table_id_) - { - StorageInMemoryMetadata storage_metadata; - storage_metadata.setColumns(columns_); - setInMemoryMetadata(storage_metadata); - } - - std::string getName() const override { return "StorageDistributedLocal"; } - - bool supportsSampling() const override { return true; } - bool supportsFinal() const override { return true; } - bool supportsPrewhere() const override { return true; } - bool supportsSubcolumns() const override { return true; } - bool supportsDynamicSubcolumns() const override { return true; } - bool canMoveConditionsToPrewhere() const override { return false; } - - QueryProcessingStage::Enum - getQueryProcessingStage(ContextPtr, QueryProcessingStage::Enum, const StorageSnapshotPtr &, SelectQueryInfo &) const override - { - throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "StorageDistributedLocal does not support getQueryProcessingStage method"); - } - - Pipe read(const Names & /*column_names*/, - const StorageSnapshotPtr & /*storage_snapshot*/, - SelectQueryInfo & /*query_info*/, - ContextPtr /*context*/, - QueryProcessingStage::Enum /*processed_stage*/, - size_t /*max_block_size*/, - size_t /*num_streams*/) override - { - throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "StorageDistributedLocal does not support read method"); - } -}; - QueryTreeNodePtr buildQueryTreeDistributedTableReplacedWithLocalTable(const SelectQueryInfo & query_info, const StorageSnapshotPtr & distributed_storage_snapshot, const StorageID & remote_storage_id, @@ -687,7 +649,7 @@ QueryTreeNodePtr buildQueryTreeDistributedTableReplacedWithLocalTable(const Sele auto resolved_remote_storage_id = query_context->resolveStorageID(remote_storage_id); auto storage = DatabaseCatalog::instance().tryGetTable(resolved_remote_storage_id, query_context); if (!storage) - storage = std::make_shared(resolved_remote_storage_id, distributed_storage_snapshot->metadata->getColumns()); + storage = std::make_shared(resolved_remote_storage_id, distributed_storage_snapshot->metadata->getColumns()); replacement_table_expression = std::make_shared(std::move(storage), query_context); } diff --git a/src/Storages/StorageDummy.cpp b/src/Storages/StorageDummy.cpp new file mode 100644 index 00000000000..e5f3b0b4d8e --- /dev/null +++ b/src/Storages/StorageDummy.cpp @@ -0,0 +1,56 @@ +#include + +#include +#include + +#include +#include + +namespace DB +{ + +StorageDummy::StorageDummy(const StorageID & table_id_, const ColumnsDescription & columns_) + : IStorage(table_id_) +{ + StorageInMemoryMetadata storage_metadata; + storage_metadata.setColumns(columns_); + setInMemoryMetadata(storage_metadata); +} + +QueryProcessingStage::Enum StorageDummy::getQueryProcessingStage( + ContextPtr, + QueryProcessingStage::Enum, + const StorageSnapshotPtr &, + SelectQueryInfo &) const +{ + return QueryProcessingStage::FetchColumns; +} + +void StorageDummy::read(QueryPlan & query_plan, + const Names & column_names, + const StorageSnapshotPtr & storage_snapshot, + SelectQueryInfo &, + ContextPtr, + QueryProcessingStage::Enum, + size_t, + size_t) +{ + query_plan.addStep(std::make_unique(*this, storage_snapshot, column_names)); +} + +ReadFromDummy::ReadFromDummy(const StorageDummy & storage_, + StorageSnapshotPtr storage_snapshot_, + Names column_names_) + : SourceStepWithFilter(DataStream{.header = storage_snapshot_->getSampleBlockForColumns(column_names_)}) + , storage(storage_) + , storage_snapshot(std::move(storage_snapshot_)) + , column_names(std::move(column_names_)) +{} + +void ReadFromDummy::initializePipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &) +{ + Pipe pipe(std::make_shared(getOutputStream().header)); + pipeline.init(std::move(pipe)); +} + +} diff --git a/src/Storages/StorageDummy.h b/src/Storages/StorageDummy.h new file mode 100644 index 00000000000..a7beef9d531 --- /dev/null +++ b/src/Storages/StorageDummy.h @@ -0,0 +1,74 @@ +#pragma once + +#include + +#include +#include + +namespace DB +{ + +class StorageDummy : public IStorage +{ +public: + StorageDummy(const StorageID & table_id_, const ColumnsDescription & columns_); + + std::string getName() const override { return "StorageDummy"; } + + bool supportsSampling() const override { return true; } + bool supportsFinal() const override { return true; } + bool supportsPrewhere() const override { return true; } + bool supportsSubcolumns() const override { return true; } + bool supportsDynamicSubcolumns() const override { return true; } + bool canMoveConditionsToPrewhere() const override { return false; } + + QueryProcessingStage::Enum getQueryProcessingStage( + ContextPtr local_context, + QueryProcessingStage::Enum to_stage, + const StorageSnapshotPtr & storage_snapshot, + SelectQueryInfo & query_info) const override; + + void read( + QueryPlan & query_plan, + const Names & column_names, + const StorageSnapshotPtr & storage_snapshot, + SelectQueryInfo & query_info, + ContextPtr local_context, + QueryProcessingStage::Enum processed_stage, + size_t max_block_size, + size_t num_streams) override; +}; + +class ReadFromDummy : public SourceStepWithFilter +{ +public: + explicit ReadFromDummy(const StorageDummy & storage_, + StorageSnapshotPtr storage_snapshot_, + Names column_names_); + + const StorageDummy & getStorage() const + { + return storage; + } + + const StorageSnapshotPtr & getStorageSnapshot() const + { + return storage_snapshot; + } + + const Names & getColumnNames() const + { + return column_names; + } + + String getName() const override { return "ReadFromDummy"; } + + void initializePipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings & settings) override; + +private: + const StorageDummy & storage; + StorageSnapshotPtr storage_snapshot; + Names column_names; +}; + +} diff --git a/src/Storages/StorageMerge.cpp b/src/Storages/StorageMerge.cpp index 0ca29e2826a..af5a32d47f7 100644 --- a/src/Storages/StorageMerge.cpp +++ b/src/Storages/StorageMerge.cpp @@ -384,7 +384,7 @@ ReadFromMerge::ReadFromMerge( const SelectQueryInfo & query_info_, ContextMutablePtr context_, QueryProcessingStage::Enum processed_stage) - : ISourceStep(DataStream{.header = common_header_}) + : SourceStepWithFilter(DataStream{.header = common_header_}) , required_max_block_size(max_block_size) , requested_num_streams(num_streams) , common_header(std::move(common_header_)) @@ -691,7 +691,11 @@ QueryPipelineBuilderPtr ReadFromMerge::createSources( return {}; if (auto * read_from_merge_tree = typeid_cast(plan.getRootNode()->step.get())) - read_from_merge_tree->addFilterNodes(added_filter_nodes); + { + size_t filters_dags_size = filter_dags.size(); + for (size_t i = 0; i < filters_dags_size; ++i) + read_from_merge_tree->addFilter(filter_dags[i], filter_nodes.nodes[i]); + } builder = plan.buildQueryPipeline( QueryPlanOptimizationSettings::fromContext(modified_context), diff --git a/src/Storages/StorageMerge.h b/src/Storages/StorageMerge.h index 03c1719dfef..4bc47375047 100644 --- a/src/Storages/StorageMerge.h +++ b/src/Storages/StorageMerge.h @@ -3,7 +3,7 @@ #include #include #include -#include +#include namespace DB @@ -115,7 +115,7 @@ private: friend class ReadFromMerge; }; -class ReadFromMerge final : public ISourceStep +class ReadFromMerge final : public SourceStepWithFilter { public: static constexpr auto name = "ReadFromMerge"; @@ -141,12 +141,6 @@ public: void initializePipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &) override; - void addFilter(ActionsDAGPtr expression, std::string column_name) - { - added_filter_dags.push_back(expression); - added_filter_nodes.nodes.push_back(&expression->findInOutputs(column_name)); - } - const StorageListWithLocks & getSelectedTables() const { return selected_tables; } /// Returns `false` if requested reading cannot be performed. @@ -169,11 +163,6 @@ private: ContextMutablePtr context; QueryProcessingStage::Enum common_processed_stage; - std::vector added_filter_dags; - ActionDAGNodes added_filter_nodes; - - std::string added_filter_column_name; - InputOrderInfoPtr order_info; struct AliasData diff --git a/tests/queries/0_stateless/00390_array_sort.sql b/tests/queries/0_stateless/00390_array_sort.sql index 8c2307eb3bd..1518761b166 100644 --- a/tests/queries/0_stateless/00390_array_sort.sql +++ b/tests/queries/0_stateless/00390_array_sort.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + SELECT [2, 1, 3] AS arr, arraySort(arr), arrayReverseSort(arr), arraySort(x -> -x, arr); SELECT materialize([2, 1, 3]) AS arr, arraySort(arr), arrayReverseSort(arr), arraySort(x -> -x, arr); @@ -51,4 +53,4 @@ SELECT arrayPartialSort(2, [1,2,3], [1]); -- { serverError ILLEGAL_TYPE_OF_ARGUM SELECT arrayPartialSort(2, [1,2,3], 3); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT arrayPartialSort(arraySort([1,2,3]), [1,2,3]); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT arrayMap(x -> range(x), [4, 1, 2, 3]) AS arr, 100 AS lim, arrayResize(arrayPartialSort(arrayPartialSort(lim, arr), arr), lim), arrayResize(arrayPartialReverseSort(lim, arr), lim), arrayResize(arrayPartialSort(x -> (-length(x)), lim, arr), lim); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } -SELECT arrayPartialReverseSort(arraySort((x, y) -> y, [NULL, NULL], [NULL, NULL]), arr), arrayMap(x -> toString(x), [257, -9223372036854775807, 2, -2147483648, 2147483648, NULL, 65536, -2147483648, 2, 65535]) AS arr, NULL, 100 AS lim, 65536, arrayResize(arrayPartialSort(x -> reverse(x), lim, arr), lim) GROUP BY [NULL, 1023, -2, NULL, 255, '0', NULL, 9223372036854775806] WITH ROLLUP; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT arrayPartialReverseSort(arraySort((x, y) -> y, [NULL, NULL], [NULL, NULL]), arr), arrayMap(x -> toString(x), [257, -9223372036854775807, 2, -2147483648, 2147483648, NULL, 65536, -2147483648, 2, 65535]) AS arr, NULL, 100 AS lim, 65536, arrayResize(arrayPartialSort(x -> reverse(x), lim, arr), lim) GROUP BY [NULL, 1023, -2, NULL, 255, '0', NULL, 9223372036854775806] WITH ROLLUP; -- { serverError NO_COMMON_TYPE } diff --git a/tests/queries/0_stateless/00740_database_in_nested_view.sql b/tests/queries/0_stateless/00740_database_in_nested_view.sql index 1890baafaa1..e2debe2859b 100644 --- a/tests/queries/0_stateless/00740_database_in_nested_view.sql +++ b/tests/queries/0_stateless/00740_database_in_nested_view.sql @@ -1,5 +1,6 @@ -- Tags: no-parallel +DROP DATABASE IF EXISTS test_00740; CREATE DATABASE test_00740; USE test_00740; diff --git a/tests/queries/0_stateless/00936_function_result_with_operator_in.sql b/tests/queries/0_stateless/00936_function_result_with_operator_in.sql index dfb19c1f3ec..9d976e5333d 100644 --- a/tests/queries/0_stateless/00936_function_result_with_operator_in.sql +++ b/tests/queries/0_stateless/00936_function_result_with_operator_in.sql @@ -1,3 +1,4 @@ +SET allow_experimental_analyzer = 1; SET force_primary_key = 1; DROP TABLE IF EXISTS samples; @@ -22,8 +23,8 @@ SELECT 'a' IN splitByChar('c', 'abcdef'); SELECT 'errors:'; -- non-constant expressions in the right side of IN -SELECT count() FROM samples WHERE 1 IN range(samples.value); -- { serverError 47 } -SELECT count() FROM samples WHERE 1 IN range(rand() % 1000); -- { serverError 36 } +SELECT count() FROM samples WHERE 1 IN range(samples.value); -- { serverError 1 } +SELECT count() FROM samples WHERE 1 IN range(rand() % 1000); -- { serverError 1 } -- index is not used SELECT count() FROM samples WHERE value IN range(3); -- { serverError 277 } diff --git a/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql b/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql index 15a28e92386..0732db73e38 100644 --- a/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql +++ b/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql @@ -2,32 +2,32 @@ SET max_memory_usage = 50000000; SET join_algorithm = 'partial_merge'; SELECT 'defaults'; - + SELECT count(1) FROM ( - SELECT materialize(1) as k, n FROM numbers(10) nums + SELECT k, n FROM (SELECT materialize(1) as k FROM numbers(10)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); SELECT count(1) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000) nums + SELECT k, n FROM (SELECT materialize(1) as k FROM numbers(1000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000000) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10)) j USING k); -- errors SET max_joined_block_size_rows = 0; - + SELECT count(1) FROM ( - SELECT materialize(1) as k, n FROM numbers(10) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(10)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); -- { serverError 241 } SELECT count(1) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); -- { serverError 241 } @@ -35,17 +35,17 @@ SELECT 'max_joined_block_size_rows = 2000'; SET max_joined_block_size_rows = 2000; SELECT count(1) FROM ( - SELECT materialize(1) as k, n FROM numbers(10) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(10)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000000) nums + SELECT k, n FROM (SELECT materialize(1) as k FROM numbers(1000000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10)) j USING k); @@ -53,16 +53,16 @@ SELECT 'max_rows_in_join = 1000'; SET max_rows_in_join = 1000; SELECT count(1) FROM ( - SELECT materialize(1) as k, n FROM numbers(10) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(10)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT materialize(1) as k, n FROM numbers(1000000) nums + SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000000)) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10)) j USING k); diff --git a/tests/queries/0_stateless/01852_multiple_joins_with_union_join.reference b/tests/queries/0_stateless/01852_multiple_joins_with_union_join.reference index eb37a198435..85e6138dc5d 100644 --- a/tests/queries/0_stateless/01852_multiple_joins_with_union_join.reference +++ b/tests/queries/0_stateless/01852_multiple_joins_with_union_join.reference @@ -1,2 +1,2 @@ -1 1 1 1 -1 1 1 1 +1 1 1 +1 1 1 diff --git a/tests/queries/0_stateless/01852_multiple_joins_with_union_join.sql b/tests/queries/0_stateless/01852_multiple_joins_with_union_join.sql index 39818fa51c2..8c6937eb581 100644 --- a/tests/queries/0_stateless/01852_multiple_joins_with_union_join.sql +++ b/tests/queries/0_stateless/01852_multiple_joins_with_union_join.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS v1; DROP TABLE IF EXISTS v2; @@ -17,7 +19,7 @@ JOIN v1 AS t2 USING (id) CROSS JOIN v2 AS n1 CROSS JOIN v2 AS n2; -SELECT * FROM v1 AS t1 JOIN v1 AS t2 USING (id) JOIN v1 AS t3 USING (value); -- { serverError NOT_IMPLEMENTED } +SELECT * FROM v1 AS t1 JOIN v1 AS t2 USING (id) JOIN v1 AS t3 USING (value); -- { serverError UNKNOWN_IDENTIFIER } DROP TABLE IF EXISTS v1; DROP TABLE IF EXISTS v2; diff --git a/tests/queries/0_stateless/01936_quantiles_cannot_return_null.sql b/tests/queries/0_stateless/01936_quantiles_cannot_return_null.sql index 81ac6224268..46fd8adf570 100644 --- a/tests/queries/0_stateless/01936_quantiles_cannot_return_null.sql +++ b/tests/queries/0_stateless/01936_quantiles_cannot_return_null.sql @@ -1,9 +1,9 @@ set aggregate_functions_null_for_empty=0; -SELECT quantiles(0.95)(x) FROM (SELECT 1 x WHERE 0); -SELECT quantiles(0.95)(number) FROM (SELECT number FROM numbers(10) WHERE number > 10); +SELECT quantiles(0.95)(x) FROM (SELECT 1 x WHERE 0); +SELECT quantiles(0.95)(number) FROM (SELECT number FROM numbers(10) WHERE number > 10); set aggregate_functions_null_for_empty=1; -SELECT quantiles(0.95)(x) FROM (SELECT 1 x WHERE 0); -SELECT quantiles(0.95)(number) FROM (SELECT number FROM numbers(10) WHERE number > 10); +SELECT quantiles(0.95)(x) FROM (SELECT 1 x WHERE 0); +SELECT quantiles(0.95)(number) FROM (SELECT number FROM numbers(10) WHERE number > 10); diff --git a/tests/queries/0_stateless/02339_analyzer_matcher_basic.reference b/tests/queries/0_stateless/02339_analyzer_matcher_basic.reference index 3ca0d303793..7e263647f98 100644 --- a/tests/queries/0_stateless/02339_analyzer_matcher_basic.reference +++ b/tests/queries/0_stateless/02339_analyzer_matcher_basic.reference @@ -38,6 +38,21 @@ id UInt64 value String 0 Value Database and table qualified matchers +-- +id UInt64 +value String +0 Value +-- +id UInt64 +0 +-- +id UInt64 +value String +0 Value +-- +id UInt64 +value String +0 Value APPLY transformer -- toString(id) String @@ -74,20 +89,20 @@ toString(value) String Value REPLACE transformer -- -5 UInt8 +id UInt8 value String 5 Value -- -5 UInt8 +id UInt8 value String 5 Value -- -5 UInt8 -6 UInt8 +id UInt8 +value UInt8 5 6 -- -5 UInt8 -6 UInt8 +id UInt8 +value UInt8 5 6 Combine EXCEPT, REPLACE, APPLY transformers -- diff --git a/tests/queries/0_stateless/02339_analyzer_matcher_basic.sql b/tests/queries/0_stateless/02339_analyzer_matcher_basic.sql index 9d7c486b28a..a09325fa43b 100644 --- a/tests/queries/0_stateless/02339_analyzer_matcher_basic.sql +++ b/tests/queries/0_stateless/02339_analyzer_matcher_basic.sql @@ -80,27 +80,25 @@ CREATE TABLE 02339_db.test_table INSERT INTO 02339_db.test_table VALUES (0, 'Value'); --- TODO: Qualified COLUMNS where identifier has more than 2 parts are not supported on parser level +SELECT '--'; --- SELECT '--'; +DESCRIBE (SELECT 02339_db.test_table.* FROM 02339_db.test_table); +SELECT 02339_db.test_table.* FROM 02339_db.test_table; --- DESCRIBE (SELECT 02339_db.test_table.* FROM 02339_db.test_table); --- SELECT 02339_db.test_table.* FROM 02339_db.test_table; +SELECT '--'; --- SELECT '--'; +DESCRIBE (SELECT 02339_db.test_table.COLUMNS(id) FROM 02339_db.test_table); +SELECT 02339_db.test_table.COLUMNS(id) FROM 02339_db.test_table; --- DESCRIBE (SELECT 02339_db.test_table.COLUMNS(id) FROM 02339_db.test_table); --- SELECT 02339_db.test_table.COLUMNS(id) FROM 02339_db.test_table; +SELECT '--'; --- SELECT '--'; +DESCRIBE (SELECT 02339_db.test_table.COLUMNS(id), 02339_db.test_table.COLUMNS(value) FROM 02339_db.test_table); +SELECT 02339_db.test_table.COLUMNS(id), 02339_db.test_table.COLUMNS(value) FROM 02339_db.test_table; --- DESCRIBE (SELECT 02339_db.test_table.COLUMNS(id), 02339_db.test_table.COLUMNS(value) FROM 02339_db.test_table); --- SELECT 02339_db.test_table.COLUMNS(id), 02339_db.test_table.COLUMNS(value) FROM 02339_db.test_table; +SELECT '--'; --- SELECT '--'; - --- DESCRIBE (SELECT 02339_db.test_table.COLUMNS('i'), 02339_db.test_table.COLUMNS('v') FROM 02339_db.test_table); --- SELECT 02339_db.test_table.COLUMNS('i'), 02339_db.test_table.COLUMNS('v') FROM 02339_db.test_table; +DESCRIBE (SELECT 02339_db.test_table.COLUMNS('i'), 02339_db.test_table.COLUMNS('v') FROM 02339_db.test_table); +SELECT 02339_db.test_table.COLUMNS('i'), 02339_db.test_table.COLUMNS('v') FROM 02339_db.test_table; DROP TABLE 02339_db.test_table; DROP DATABASE 02339_db; diff --git a/tests/queries/0_stateless/02378_analyzer_projection_names.reference b/tests/queries/0_stateless/02378_analyzer_projection_names.reference index 9e72fe0d100..a82d4d4c5d2 100644 --- a/tests/queries/0_stateless/02378_analyzer_projection_names.reference +++ b/tests/queries/0_stateless/02378_analyzer_projection_names.reference @@ -136,7 +136,7 @@ plus(id, id) UInt64 SELECT '--'; -- DESCRIBE (SELECT test_table.* REPLACE id + (id AS id_alias) AS id, id_alias FROM test_table); -plus(id, id_alias) UInt64 +id UInt64 value String id_alias UInt64 SELECT 'Matcher'; From 14e01566a099a6b7861a5cc69826cacdf7b238bc Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Fri, 24 Feb 2023 18:59:47 +0100 Subject: [PATCH 162/342] Fixed tests --- src/Analyzer/IQueryTreeNode.cpp | 7 +++++++ .../Passes/RewriteAggregateFunctionWithIfPass.cpp | 8 ++++++-- src/Interpreters/InterpreterCreateQuery.cpp | 6 ++++++ src/Planner/Planner.cpp | 3 ++- src/Planner/PlannerJoinTree.cpp | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Analyzer/IQueryTreeNode.cpp b/src/Analyzer/IQueryTreeNode.cpp index 6b9b8b53d81..ba361af0007 100644 --- a/src/Analyzer/IQueryTreeNode.cpp +++ b/src/Analyzer/IQueryTreeNode.cpp @@ -263,6 +263,13 @@ QueryTreeNodePtr IQueryTreeNode::cloneAndReplace(const ReplacementMap & replacem const auto [node_to_clone, place_for_cloned_node] = nodes_to_clone.back(); nodes_to_clone.pop_back(); + auto already_cloned_node_it = old_pointer_to_new_pointer.find(node_to_clone); + if (already_cloned_node_it != old_pointer_to_new_pointer.end()) + { + *place_for_cloned_node = already_cloned_node_it->second; + continue; + } + auto it = replacement_map.find(node_to_clone); auto node_clone = it != replacement_map.end() ? it->second : node_to_clone->cloneImpl(); *place_for_cloned_node = node_clone; diff --git a/src/Analyzer/Passes/RewriteAggregateFunctionWithIfPass.cpp b/src/Analyzer/Passes/RewriteAggregateFunctionWithIfPass.cpp index 4039e058b34..de264948d4c 100644 --- a/src/Analyzer/Passes/RewriteAggregateFunctionWithIfPass.cpp +++ b/src/Analyzer/Passes/RewriteAggregateFunctionWithIfPass.cpp @@ -17,7 +17,6 @@ namespace DB { - namespace { @@ -90,9 +89,14 @@ private: static inline void resolveAsAggregateFunctionWithIf(FunctionNode & function_node, const DataTypes & argument_types) { auto result_type = function_node.getResultType(); + + std::string suffix = "If"; + if (result_type->isNullable()) + suffix = "OrNullIf"; + AggregateFunctionProperties properties; auto aggregate_function = AggregateFunctionFactory::instance().get( - function_node.getFunctionName() + (result_type->isNullable() ? "IfOrNull" : "If"), + function_node.getFunctionName() + suffix, argument_types, function_node.getAggregateFunction()->getParameters(), properties); diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index edc6cc92fd7..29b7a4db609 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -1224,6 +1224,12 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create) output_columns.push_back(output_column->cloneEmpty()); } } + + ActionsDAG::makeConvertingActions( + input_columns, + output_columns, + ActionsDAG::MatchColumnsMode::Position + ); } } diff --git a/src/Planner/Planner.cpp b/src/Planner/Planner.cpp index 49120c91db2..a65ee60edfa 100644 --- a/src/Planner/Planner.cpp +++ b/src/Planner/Planner.cpp @@ -1440,7 +1440,8 @@ void Planner::buildPlanForQueryNode() } } - addBuildSubqueriesForSetsStepIfNeeded(query_plan, select_query_options, planner_context, result_actions_to_execute); + if (!select_query_options.only_analyze) + addBuildSubqueriesForSetsStepIfNeeded(query_plan, select_query_options, planner_context, result_actions_to_execute); } void Planner::addStorageLimits(const StorageLimitsList & limits) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index f8e4a3b7021..06424b041ba 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -360,7 +360,7 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl auto projection_columns = query_node ? query_node->getProjectionColumns() : union_node->computeProjectionColumns(); Block source_header; for (auto & projection_column : projection_columns) - source_header.insert(ColumnWithTypeAndName(nullptr, projection_column.type, projection_column.name)); + source_header.insert(ColumnWithTypeAndName(projection_column.type, projection_column.name)); Pipe pipe(std::make_shared(source_header)); auto read_from_pipe = std::make_unique(std::move(pipe)); From d39be3ac9ca3d0c6402867f000d6e938a22580b1 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sat, 25 Feb 2023 20:16:51 +0100 Subject: [PATCH 163/342] Fixed tests --- src/Analyzer/ColumnNode.cpp | 11 +- src/Analyzer/IdentifierNode.cpp | 10 +- src/Analyzer/Passes/CrossToInnerJoinPass.cpp | 35 +-- .../Passes/CustomizeFunctionsPass.cpp | 77 ------- src/Analyzer/Passes/CustomizeFunctionsPass.h | 25 --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 200 +++++++++++------- src/Analyzer/Passes/QueryAnalysisPass.h | 1 + src/Analyzer/QueryTreePassManager.cpp | 3 - src/Analyzer/TableFunctionNode.cpp | 9 +- src/Analyzer/TableNode.cpp | 21 +- src/Analyzer/TableNode.h | 13 ++ src/Analyzer/Utils.cpp | 28 +++ src/Analyzer/Utils.h | 11 + src/Functions/FunctionsExternalDictionaries.h | 25 +-- src/Functions/formatRow.cpp | 25 ++- .../ExternalDictionariesLoader.cpp | 5 + src/Interpreters/ExternalDictionariesLoader.h | 2 + src/Parsers/ExpressionElementParsers.cpp | 4 +- src/Planner/PlannerJoinTree.cpp | 21 +- src/Storages/StorageDistributed.cpp | 4 +- ..._no_aggregates_and_constant_keys.reference | 8 +- ..._shard_no_aggregates_and_constant_keys.sql | 3 +- .../00313_const_totals_extremes.reference | 12 +- .../00313_const_totals_extremes.sh | 12 +- ...01013_totals_without_aggregation.reference | 6 +- .../01013_totals_without_aggregation.sql | 2 + .../01115_join_with_dictionary.sql | 4 +- .../01470_columns_transformers.sql | 8 +- .../01603_decimal_mult_float.reference | 2 +- .../0_stateless/01603_decimal_mult_float.sql | 2 + .../0_stateless/01774_tuple_null_in.reference | 1 - .../0_stateless/01774_tuple_null_in.sql | 1 - ...hree_parts_identifiers_in_wrong_places.sql | 8 +- ..._with_statement_non_exist_column.reference | 1 - .../02354_with_statement_non_exist_column.sql | 2 - ..._group_by_without_key_and_totals.reference | 8 +- .../00081_group_by_without_key_and_totals.sql | 2 + 37 files changed, 323 insertions(+), 289 deletions(-) delete mode 100644 src/Analyzer/Passes/CustomizeFunctionsPass.cpp delete mode 100644 src/Analyzer/Passes/CustomizeFunctionsPass.h diff --git a/src/Analyzer/ColumnNode.cpp b/src/Analyzer/ColumnNode.cpp index b9939df37bb..c07d7bab717 100644 --- a/src/Analyzer/ColumnNode.cpp +++ b/src/Analyzer/ColumnNode.cpp @@ -110,8 +110,15 @@ ASTPtr ColumnNode::toASTImpl() const } else if (auto * table_node = column_source->as()) { - const auto & table_storage_id = table_node->getStorageID(); - column_identifier_parts = {table_storage_id.getDatabaseName(), table_storage_id.getTableName()}; + if (!table_node->getTemporaryTableName().empty()) + { + column_identifier_parts = { table_node->getTemporaryTableName() }; + } + else + { + const auto & table_storage_id = table_node->getStorageID(); + column_identifier_parts = { table_storage_id.getDatabaseName(), table_storage_id.getTableName() }; + } } } } diff --git a/src/Analyzer/IdentifierNode.cpp b/src/Analyzer/IdentifierNode.cpp index 4efc7f515ea..cb5d9609962 100644 --- a/src/Analyzer/IdentifierNode.cpp +++ b/src/Analyzer/IdentifierNode.cpp @@ -40,15 +40,7 @@ void IdentifierNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_sta bool IdentifierNode::isEqualImpl(const IQueryTreeNode & rhs) const { const auto & rhs_typed = assert_cast(rhs); - - if (table_expression_modifiers && rhs_typed.table_expression_modifiers && table_expression_modifiers != rhs_typed.table_expression_modifiers) - return false; - else if (table_expression_modifiers && !rhs_typed.table_expression_modifiers) - return false; - else if (!table_expression_modifiers && rhs_typed.table_expression_modifiers) - return false; - - return identifier == rhs_typed.identifier; + return identifier == rhs_typed.identifier && table_expression_modifiers == rhs_typed.table_expression_modifiers; } void IdentifierNode::updateTreeHashImpl(HashState & state) const diff --git a/src/Analyzer/Passes/CrossToInnerJoinPass.cpp b/src/Analyzer/Passes/CrossToInnerJoinPass.cpp index 4f62d6ef068..d4877d23f28 100644 --- a/src/Analyzer/Passes/CrossToInnerJoinPass.cpp +++ b/src/Analyzer/Passes/CrossToInnerJoinPass.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -152,25 +154,34 @@ public: QueryTreeNodes other_conditions; exctractJoinConditions(where_condition, equi_conditions, other_conditions); bool can_convert_cross_to_inner = false; - for (auto & cond : equi_conditions) + for (auto & condition : equi_conditions) { - auto left_src = getExpressionSource(getEquiArgument(cond, 0)); - auto right_src = getExpressionSource(getEquiArgument(cond, 1)); - if (left_src.second && right_src.second && left_src.first && right_src.first) - { - bool can_join_on = (findInTableExpression(left_src.first, left_table) && findInTableExpression(right_src.first, right_table)) - || (findInTableExpression(left_src.first, right_table) && findInTableExpression(right_src.first, left_table)); + const auto & lhs_equi_argument = getEquiArgument(condition, 0); + const auto & rhs_equi_argument = getEquiArgument(condition, 1); - if (can_join_on) + DataTypes key_types = {lhs_equi_argument->getResultType(), rhs_equi_argument->getResultType()}; + DataTypePtr common_key_type = tryGetLeastSupertype(key_types); + + /// If there is common key type, we can join on this condition + if (common_key_type) + { + auto left_src = getExpressionSource(lhs_equi_argument); + auto right_src = getExpressionSource(rhs_equi_argument); + + if (left_src.second && right_src.second && left_src.first && right_src.first) { - can_convert_cross_to_inner = true; - continue; + if ((findInTableExpression(left_src.first, left_table) && findInTableExpression(right_src.first, right_table)) || + (findInTableExpression(left_src.first, right_table) && findInTableExpression(right_src.first, left_table))) + { + can_convert_cross_to_inner = true; + continue; + } } } /// Can't join on this condition, move it to other conditions - other_conditions.push_back(cond); - cond = nullptr; + other_conditions.push_back(condition); + condition = nullptr; } if (!can_convert_cross_to_inner) diff --git a/src/Analyzer/Passes/CustomizeFunctionsPass.cpp b/src/Analyzer/Passes/CustomizeFunctionsPass.cpp deleted file mode 100644 index 55307fd39e7..00000000000 --- a/src/Analyzer/Passes/CustomizeFunctionsPass.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include - -#include -#include - -#include - -#include - -#include -#include - -namespace DB -{ - -namespace -{ - -class CustomizeFunctionsVisitor : public InDepthQueryTreeVisitorWithContext -{ -public: - using Base = InDepthQueryTreeVisitorWithContext; - using Base::Base; - - void visitImpl(QueryTreeNodePtr & node) const - { - auto * function_node = node->as(); - if (!function_node) - return; - - const auto & settings = getSettings(); - - /// After successful function replacement function name and function name lowercase must be recalculated - auto function_name = function_node->getFunctionName(); - auto function_name_lowercase = Poco::toLower(function_name); - - if (settings.transform_null_in) - { - auto function_result_type = function_node->getResultType(); - - static constexpr std::array, 4> in_function_to_replace_null_in_function_map = - {{ - {"in", "nullIn"}, - {"notin", "notNullIn"}, - {"globalin", "globalNullIn"}, - {"globalnotin", "globalNotNullIn"}, - }}; - - for (const auto & [in_function_name, in_function_name_to_replace] : in_function_to_replace_null_in_function_map) - { - if (function_name_lowercase == in_function_name) - { - resolveOrdinaryFunctionNode(*function_node, String(in_function_name_to_replace)); - function_name = function_node->getFunctionName(); - function_name_lowercase = Poco::toLower(function_name); - break; - } - } - } - } - - inline void resolveOrdinaryFunctionNode(FunctionNode & function_node, const String & function_name) const - { - auto function = FunctionFactory::instance().get(function_name, getContext()); - function_node.resolveAsFunction(function->build(function_node.getArgumentColumns())); - } -}; - -} - -void CustomizeFunctionsPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context) -{ - CustomizeFunctionsVisitor visitor(std::move(context)); - visitor.visit(query_tree_node); -} - -} diff --git a/src/Analyzer/Passes/CustomizeFunctionsPass.h b/src/Analyzer/Passes/CustomizeFunctionsPass.h deleted file mode 100644 index 7145099ca4c..00000000000 --- a/src/Analyzer/Passes/CustomizeFunctionsPass.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -namespace DB -{ - -/** Customize aggregate functions and `in` functions implementations. - * - * Example: SELECT countDistinct(); - * Result: SELECT countDistinctImplementation(); - * Function countDistinctImplementation is taken from settings.count_distinct_implementation. - */ -class CustomizeFunctionsPass final : public IQueryTreePass -{ -public: - String getName() override { return "CustomizeFunctions"; } - - String getDescription() override { return "Customize implementation of aggregate functions, and in functions."; } - - void run(QueryTreeNodePtr query_tree_node, ContextPtr context) override; - -}; - -} diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 9d1a096e582..01b9494bfc3 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -37,14 +37,16 @@ #include #include +#include +#include #include #include -#include #include -#include #include -#include +#include +#include +#include #include @@ -92,6 +94,7 @@ namespace ErrorCodes extern const int CYCLIC_ALIASES; extern const int INCORRECT_RESULT_OF_SCALAR_SUBQUERY; extern const int BAD_ARGUMENTS; + extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int MULTIPLE_EXPRESSIONS_FOR_ALIAS; extern const int TYPE_MISMATCH; extern const int AMBIGUOUS_IDENTIFIER; @@ -1971,26 +1974,10 @@ void QueryAnalyzer::evaluateScalarSubqueryIfNeeded(QueryTreeNodePtr & node, Iden auto constant_node = std::make_shared(std::move(constant_value), node); if (constant_node->getValue().isNull()) - { - std::string cast_type = constant_node->getResultType()->getName(); - std::string cast_function_name = "_CAST"; - - auto cast_type_constant_value = std::make_shared(std::move(cast_type), std::make_shared()); - auto cast_type_constant_node = std::make_shared(std::move(cast_type_constant_value)); - - auto cast_function_node = std::make_shared(cast_function_name); - cast_function_node->getArguments().getNodes().push_back(constant_node); - cast_function_node->getArguments().getNodes().push_back(std::move(cast_type_constant_node)); - - auto cast_function = FunctionFactory::instance().get(cast_function_name, context); - cast_function_node->resolveAsFunction(cast_function->build(cast_function_node->getArgumentColumns())); - - node = std::move(cast_function_node); - } + node = buildCastFunction(constant_node, constant_node->getResultType(), context); else - { node = std::move(constant_node); - } + return; } @@ -2333,6 +2320,8 @@ QueryTreeNodePtr QueryAnalyzer::tryResolveTableIdentifierFromDatabaseCatalog(con StorageID storage_id(database_name, table_name); storage_id = context->resolveStorageID(storage_id); + bool is_temporary_table = storage_id.getDatabaseName() == DatabaseCatalog::TEMPORARY_DATABASE; + auto storage = DatabaseCatalog::instance().tryGetTable(storage_id, context); if (!storage) return {}; @@ -2340,7 +2329,11 @@ QueryTreeNodePtr QueryAnalyzer::tryResolveTableIdentifierFromDatabaseCatalog(con auto storage_lock = storage->lockForShare(context->getInitialQueryId(), context->getSettingsRef().lock_acquire_timeout); auto storage_snapshot = storage->getStorageSnapshot(storage->getInMemoryMetadataPtr(), context); - return std::make_shared(std::move(storage), std::move(storage_lock), std::move(storage_snapshot)); + auto result = std::make_shared(std::move(storage), std::move(storage_lock), std::move(storage_snapshot)); + if (is_temporary_table) + result->setTemporaryTableName(table_name); + + return result; } /// Resolve identifier from compound expression @@ -3961,17 +3954,16 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I matched_expression_nodes_with_names = resolveUnqualifiedMatcher(matcher_node, scope); std::unordered_map> strict_transformer_to_used_column_names; - auto add_strict_transformer_column_name = [&](const IColumnTransformerNode * transformer, const std::string & column_name) + for (const auto & transformer : matcher_node_typed.getColumnTransformers().getNodes()) { - auto it = strict_transformer_to_used_column_names.find(transformer); - if (it == strict_transformer_to_used_column_names.end()) - { - auto [inserted_it, _] = strict_transformer_to_used_column_names.emplace(transformer, std::unordered_set()); - it = inserted_it; - } + auto * except_transformer = transformer->as(); + auto * replace_transformer = transformer->as(); - it->second.insert(column_name); - }; + if (except_transformer && except_transformer->isStrict()) + strict_transformer_to_used_column_names.emplace(except_transformer, std::unordered_set()); + else if (replace_transformer && replace_transformer->isStrict()) + strict_transformer_to_used_column_names.emplace(replace_transformer, std::unordered_set()); + } ListNodePtr list = std::make_shared(); ProjectionNames result_projection_names; @@ -4026,12 +4018,12 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I else if (auto * except_transformer = transformer->as()) { if (apply_transformer_was_used || replace_transformer_was_used) - break; + continue; if (except_transformer->isColumnMatching(column_name)) { if (except_transformer->isStrict()) - add_strict_transformer_column_name(except_transformer, column_name); + strict_transformer_to_used_column_names[except_transformer].insert(column_name); node = {}; break; @@ -4040,7 +4032,7 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I else if (auto * replace_transformer = transformer->as()) { if (apply_transformer_was_used || replace_transformer_was_used) - break; + continue; replace_transformer_was_used = true; @@ -4049,7 +4041,7 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I continue; if (replace_transformer->isStrict()) - add_strict_transformer_column_name(replace_transformer, column_name); + strict_transformer_to_used_column_names[replace_transformer].insert(column_name); node = replace_expression->clone(); node_projection_names = resolveExpressionNode(node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); @@ -4140,22 +4132,12 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I non_matched_column_names.push_back(column_name); } - WriteBufferFromOwnString non_matched_column_names_buffer; - size_t non_matched_column_names_size = non_matched_column_names.size(); - for (size_t i = 0; i < non_matched_column_names_size; ++i) - { - const auto & column_name = non_matched_column_names[i]; - - non_matched_column_names_buffer << column_name; - if (i + 1 != non_matched_column_names_size) - non_matched_column_names_buffer << ", "; - } - throw Exception(ErrorCodes::BAD_ARGUMENTS, - "Strict {} column transformer {} expects following column(s) {}", + "Strict {} column transformer {} expects following column(s) : {}. In scope {}", toString(strict_transformer_type), strict_transformer->formatASTForErrorMessage(), - non_matched_column_names_buffer.str()); + fmt::join(non_matched_column_names, ", "), + scope.scope_node->formatASTForErrorMessage()); } matcher_node = std::move(list); @@ -4449,17 +4431,26 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } bool is_special_function_in = false; - bool is_special_function_dict_get_or_join_get = false; + bool is_special_function_dict_get = false; + bool is_special_function_join_get = false; bool is_special_function_exists = false; if (!lambda_expression_untyped) { is_special_function_in = isNameOfInFunction(function_name); - is_special_function_dict_get_or_join_get = functionIsJoinGet(function_name) || functionIsDictGet(function_name); + is_special_function_dict_get = functionIsDictGet(function_name); + is_special_function_join_get = functionIsJoinGet(function_name); is_special_function_exists = function_name == "exists"; - /// Handle SELECT count(*) FROM test_table - if (Poco::toLower(function_name) == "count" && function_node_ptr->getArguments().getNodes().size() == 1) + auto function_name_lowercase = Poco::toLower(function_name); + + /** Special handling for count and countState functions. + * + * Example: SELECT count(*) FROM test_table + * Example: SELECT countState(*) FROM test_table; + */ + if (function_node_ptr->getArguments().getNodes().size() == 1 && + (function_name_lowercase == "count" || function_name_lowercase == "countstate")) { auto * matcher_node = function_node_ptr->getArguments().getNodes().front()->as(); if (matcher_node && matcher_node->isUnqualified()) @@ -4476,19 +4467,56 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi * Otherwise replace identifier with identifier full name constant. * Validation that dictionary exists or table exists will be performed during function `getReturnType` method call. */ - if (is_special_function_dict_get_or_join_get && + if ((is_special_function_dict_get || is_special_function_join_get) && !function_node_ptr->getArguments().getNodes().empty() && function_node_ptr->getArguments().getNodes()[0]->getNodeType() == QueryTreeNodeType::IDENTIFIER) { auto & first_argument = function_node_ptr->getArguments().getNodes()[0]; - auto & identifier_node = first_argument->as(); - IdentifierLookup identifier_lookup{identifier_node.getIdentifier(), IdentifierLookupContext::EXPRESSION}; + auto & first_argument_identifier = first_argument->as(); + auto identifier = first_argument_identifier.getIdentifier(); + + IdentifierLookup identifier_lookup{identifier, IdentifierLookupContext::EXPRESSION}; auto resolve_result = tryResolveIdentifier(identifier_lookup, scope); if (resolve_result.isResolved()) + { first_argument = std::move(resolve_result.resolved_identifier); + } else - first_argument = std::make_shared(identifier_node.getIdentifier().getFullName()); + { + size_t parts_size = identifier.getPartsSize(); + if (parts_size < 1 || parts_size > 2) + throw Exception(ErrorCodes::BAD_ARGUMENTS, + "Expected {} function first argument identifier to contain 1 or 2 parts. Actual '{}'. In scope {}", + function_name, + identifier.getFullName(), + scope.scope_node->formatASTForErrorMessage()); + + if (is_special_function_dict_get) + { + scope.context->getExternalDictionariesLoader().assertDictionaryStructureExists(identifier.getFullName(), scope.context); + } + else + { + auto table_node = tryResolveTableIdentifierFromDatabaseCatalog(identifier, scope.context); + if (!table_node) + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Function {} first argument expected table identifier '{}'. In scope {}", + function_name, + identifier.getFullName(), + scope.scope_node->formatASTForErrorMessage()); + + auto & table_node_typed = table_node->as(); + if (!std::dynamic_pointer_cast(table_node_typed.getStorage())) + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Function {} table '{}' should have engine StorageJoin. In scope {}", + function_name, + identifier.getFullName(), + scope.scope_node->formatASTForErrorMessage()); + } + + first_argument = std::make_shared(identifier.getFullName()); + } } /// Resolve function arguments @@ -4526,6 +4554,26 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi /// Replace right IN function argument if it is table or table function with subquery that read ordinary columns if (is_special_function_in) { + if (scope.context->getSettingsRef().transform_null_in) + { + static constexpr std::array, 4> in_function_to_replace_null_in_function_map = + {{ + {"in", "nullIn"}, + {"notIn", "notNullIn"}, + {"globalIn", "globalNullIn"}, + {"globalNotIn", "globalNotNullIn"}, + }}; + + for (const auto & [in_function_name, in_function_name_to_replace] : in_function_to_replace_null_in_function_map) + { + if (function_name == in_function_name) + { + function_name = in_function_name_to_replace; + break; + } + } + } + auto & function_in_arguments_nodes = function_node.getArguments().getNodes(); if (function_in_arguments_nodes.size() != 2) throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function '{}' expects 2 arguments", function_name); @@ -4586,6 +4634,7 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi auto & function_argument = function_arguments[function_argument_index]; ColumnWithTypeAndName argument_column; + argument_column.name = arguments_projection_names[function_argument_index]; /** If function argument is lambda, save lambda argument index and initialize argument type as DataTypeFunction * where function argument types are initialized with empty array of lambda arguments size. @@ -4627,7 +4676,7 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } /// Calculate function projection name - ProjectionNames result_projection_names = {calculateFunctionProjectionName(node, parameters_projection_names, arguments_projection_names)}; + ProjectionNames result_projection_names = { calculateFunctionProjectionName(node, parameters_projection_names, arguments_projection_names) }; /** Try to resolve function as * 1. Lambda function in current scope. Example: WITH (x -> x + 1) AS lambda SELECT lambda(1); @@ -4939,7 +4988,7 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } /// Recalculate function projection name after lambda resolution - result_projection_names = {calculateFunctionProjectionName(node, parameters_projection_names, arguments_projection_names)}; + result_projection_names = { calculateFunctionProjectionName(node, parameters_projection_names, arguments_projection_names) }; } /** Create SET column for special function IN to allow constant folding @@ -5765,10 +5814,19 @@ void QueryAnalyzer::initializeTableExpressionData(const QueryTreeNodePtr & table if (table_node) { - const auto & table_storage_id = table_node->getStorageID(); - table_expression_data.database_name = table_storage_id.database_name; - table_expression_data.table_name = table_storage_id.table_name; - table_expression_data.table_expression_name = table_storage_id.getFullNameNotQuoted(); + if (!table_node->getTemporaryTableName().empty()) + { + table_expression_data.table_name = table_node->getTemporaryTableName(); + table_expression_data.table_expression_name = table_node->getTemporaryTableName(); + } + else + { + const auto & table_storage_id = table_node->getStorageID(); + table_expression_data.database_name = table_storage_id.database_name; + table_expression_data.table_name = table_storage_id.table_name; + table_expression_data.table_expression_name = table_storage_id.getFullNameNotQuoted(); + } + table_expression_data.table_expression_description = "table"; } else if (query_node || union_node) @@ -5814,7 +5872,9 @@ void QueryAnalyzer::initializeTableExpressionData(const QueryTreeNodePtr & table if (column_default && column_default->kind == ColumnDefaultKind::Alias) { - auto column_node = std::make_shared(column_name_and_type, buildQueryTree(column_default->expression, scope.context), table_expression_node); + auto alias_expression = buildQueryTree(column_default->expression, scope.context); + alias_expression = buildCastFunction(alias_expression, column_name_and_type.type, scope.context, false /*resolve*/); + auto column_node = std::make_shared(column_name_and_type, std::move(alias_expression), table_expression_node); column_name_to_column_node.emplace(column_name_and_type.name, column_node); alias_columns_to_resolve.emplace_back(column_name_and_type.name, column_node); } @@ -6181,13 +6241,6 @@ void QueryAnalyzer::resolveJoin(QueryTreeNodePtr & join_node, IdentifierResolveS IdentifierLookup identifier_lookup{identifier_node->getIdentifier(), IdentifierLookupContext::EXPRESSION}; auto result_left_table_expression = tryResolveIdentifierFromJoinTreeNode(identifier_lookup, join_node_typed.getLeftTableExpression(), scope); - if (!result_left_table_expression && identifier_node->hasAlias()) - { - std::vector alias_identifier_parts = {identifier_node->getAlias()}; - IdentifierLookup alias_identifier_lookup{Identifier(std::move(alias_identifier_parts)), IdentifierLookupContext::EXPRESSION}; - result_left_table_expression = tryResolveIdentifierFromJoinTreeNode(alias_identifier_lookup, join_node_typed.getLeftTableExpression(), scope); - } - if (!result_left_table_expression) throw Exception(ErrorCodes::UNKNOWN_IDENTIFIER, "JOIN {} using identifier '{}' cannot be resolved from left table expression. In scope {}", @@ -6203,13 +6256,6 @@ void QueryAnalyzer::resolveJoin(QueryTreeNodePtr & join_node, IdentifierResolveS scope.scope_node->formatASTForErrorMessage()); auto result_right_table_expression = tryResolveIdentifierFromJoinTreeNode(identifier_lookup, join_node_typed.getRightTableExpression(), scope); - if (!result_right_table_expression && identifier_node->hasAlias()) - { - std::vector alias_identifier_parts = {identifier_node->getAlias()}; - IdentifierLookup alias_identifier_lookup{Identifier(std::move(alias_identifier_parts)), IdentifierLookupContext::EXPRESSION}; - result_right_table_expression = tryResolveIdentifierFromJoinTreeNode(alias_identifier_lookup, join_node_typed.getRightTableExpression(), scope); - } - if (!result_right_table_expression) throw Exception(ErrorCodes::UNKNOWN_IDENTIFIER, "JOIN {} using identifier '{}' cannot be resolved from right table expression. In scope {}", diff --git a/src/Analyzer/Passes/QueryAnalysisPass.h b/src/Analyzer/Passes/QueryAnalysisPass.h index 8b3a8a116e5..a4ee685812d 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.h +++ b/src/Analyzer/Passes/QueryAnalysisPass.h @@ -54,6 +54,7 @@ namespace DB * Replace `countDistinct` and `countIfDistinct` aggregate functions using setting count_distinct_implementation. * Add -OrNull suffix to aggregate functions if setting aggregate_functions_null_for_empty is true. * Function `exists` is converted into `in`. + * Functions `in`, `notIn`, `globalIn`, `globalNotIn` converted into `nullIn`, `notNullIn`, `globalNullIn`, `globalNotNullIn` if setting transform_null_in is true. * * For function `grouping` arguments are resolved, but it is planner responsibility to initialize it with concrete grouping function * based on group by kind and group by keys positions. diff --git a/src/Analyzer/QueryTreePassManager.cpp b/src/Analyzer/QueryTreePassManager.cpp index dfac2f499dd..2cecf4f81a2 100644 --- a/src/Analyzer/QueryTreePassManager.cpp +++ b/src/Analyzer/QueryTreePassManager.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -242,8 +241,6 @@ void addQueryTreePasses(QueryTreePassManager & manager) manager.addPass(std::make_unique()); manager.addPass(std::make_unique()); - manager.addPass(std::make_unique()); - manager.addPass(std::make_unique()); manager.addPass(std::make_unique()); manager.addPass(std::make_unique()); diff --git a/src/Analyzer/TableFunctionNode.cpp b/src/Analyzer/TableFunctionNode.cpp index c8cd05cf685..fb3a3af31e1 100644 --- a/src/Analyzer/TableFunctionNode.cpp +++ b/src/Analyzer/TableFunctionNode.cpp @@ -82,14 +82,7 @@ bool TableFunctionNode::isEqualImpl(const IQueryTreeNode & rhs) const if (storage && rhs_typed.storage) return storage_id == rhs_typed.storage_id; - if (table_expression_modifiers && rhs_typed.table_expression_modifiers && table_expression_modifiers != rhs_typed.table_expression_modifiers) - return false; - else if (table_expression_modifiers && !rhs_typed.table_expression_modifiers) - return false; - else if (!table_expression_modifiers && rhs_typed.table_expression_modifiers) - return false; - - return true; + return table_expression_modifiers == rhs_typed.table_expression_modifiers; } void TableFunctionNode::updateTreeHashImpl(HashState & state) const diff --git a/src/Analyzer/TableNode.cpp b/src/Analyzer/TableNode.cpp index 1018570c3d4..a746986be04 100644 --- a/src/Analyzer/TableNode.cpp +++ b/src/Analyzer/TableNode.cpp @@ -42,6 +42,9 @@ void TableNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, s buffer << ", table_name: " << storage_id.getFullNameNotQuoted(); + if (!temporary_table_name.empty()) + buffer << ", temporary_table_name: " << temporary_table_name; + if (table_expression_modifiers) { buffer << ", "; @@ -52,15 +55,8 @@ void TableNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, s bool TableNode::isEqualImpl(const IQueryTreeNode & rhs) const { const auto & rhs_typed = assert_cast(rhs); - - if (table_expression_modifiers && rhs_typed.table_expression_modifiers && table_expression_modifiers != rhs_typed.table_expression_modifiers) - return false; - else if (table_expression_modifiers && !rhs_typed.table_expression_modifiers) - return false; - else if (!table_expression_modifiers && rhs_typed.table_expression_modifiers) - return false; - - return storage_id == rhs_typed.storage_id; + return storage_id == rhs_typed.storage_id && table_expression_modifiers == rhs_typed.table_expression_modifiers && + temporary_table_name == rhs_typed.temporary_table_name; } void TableNode::updateTreeHashImpl(HashState & state) const @@ -69,6 +65,9 @@ void TableNode::updateTreeHashImpl(HashState & state) const state.update(full_name.size()); state.update(full_name); + state.update(temporary_table_name.size()); + state.update(temporary_table_name); + if (table_expression_modifiers) table_expression_modifiers->updateTreeHash(state); } @@ -77,12 +76,16 @@ QueryTreeNodePtr TableNode::cloneImpl() const { auto result_table_node = std::make_shared(storage, storage_id, storage_lock, storage_snapshot); result_table_node->table_expression_modifiers = table_expression_modifiers; + result_table_node->temporary_table_name = temporary_table_name; return result_table_node; } ASTPtr TableNode::toASTImpl() const { + if (!temporary_table_name.empty()) + return std::make_shared(temporary_table_name); + return std::make_shared(storage_id.getDatabaseName(), storage_id.getTableName()); } diff --git a/src/Analyzer/TableNode.h b/src/Analyzer/TableNode.h index 6d47f87c78b..1d5ec112ee0 100644 --- a/src/Analyzer/TableNode.h +++ b/src/Analyzer/TableNode.h @@ -56,6 +56,18 @@ public: return storage_lock; } + /// Get temporary table name + const std::string & getTemporaryTableName() const + { + return temporary_table_name; + } + + /// Set temporary table name + void setTemporaryTableName(std::string temporary_table_name_value) + { + temporary_table_name = std::move(temporary_table_name_value); + } + /// Return true if table node has table expression modifiers, false otherwise bool hasTableExpressionModifiers() const { @@ -102,6 +114,7 @@ private: TableLockHolder storage_lock; StorageSnapshotPtr storage_snapshot; std::optional table_expression_modifiers; + std::string temporary_table_name; static constexpr size_t children_size = 0; }; diff --git a/src/Analyzer/Utils.cpp b/src/Analyzer/Utils.cpp index e746a8ff570..c5a5c042cbc 100644 --- a/src/Analyzer/Utils.cpp +++ b/src/Analyzer/Utils.cpp @@ -5,13 +5,18 @@ #include #include +#include #include #include #include +#include + +#include #include #include +#include #include #include #include @@ -74,6 +79,29 @@ bool isNameOfInFunction(const std::string & function_name) return is_special_function_in; } +QueryTreeNodePtr buildCastFunction(const QueryTreeNodePtr & expression, + const DataTypePtr & type, + const ContextPtr & context, + bool resolve) +{ + std::string cast_type = type->getName(); + auto cast_type_constant_value = std::make_shared(std::move(cast_type), std::make_shared()); + auto cast_type_constant_node = std::make_shared(std::move(cast_type_constant_value)); + + std::string cast_function_name = "_CAST"; + auto cast_function_node = std::make_shared(cast_function_name); + cast_function_node->getArguments().getNodes().push_back(expression); + cast_function_node->getArguments().getNodes().push_back(std::move(cast_type_constant_node)); + + if (resolve) + { + auto cast_function = FunctionFactory::instance().get(cast_function_name, context); + cast_function_node->resolveAsFunction(cast_function->build(cast_function_node->getArgumentColumns())); + } + + return cast_function_node; +} + static ASTPtr convertIntoTableExpressionAST(const QueryTreeNodePtr & table_expression_node) { ASTPtr table_expression_node_ast; diff --git a/src/Analyzer/Utils.h b/src/Analyzer/Utils.h index 0f54b5cadd5..3e2d95c6012 100644 --- a/src/Analyzer/Utils.h +++ b/src/Analyzer/Utils.h @@ -2,6 +2,8 @@ #include +#include + namespace DB { @@ -11,6 +13,15 @@ bool isNodePartOfTree(const IQueryTreeNode * node, const IQueryTreeNode * root); /// Returns true if function name is name of IN function or its variations, false otherwise bool isNameOfInFunction(const std::string & function_name); +/** Build cast function that cast expression into type. + * If resolve = true, then result cast function is resolved during build, otherwise + * result cast function is not resolved during build. + */ +QueryTreeNodePtr buildCastFunction(const QueryTreeNodePtr & expression, + const DataTypePtr & type, + const ContextPtr & context, + bool resolve = true); + /** Add table expression in tables in select query children. * If table expression node is not of identifier node, table node, query node, table function node, join node or array join node type throws logical error exception. */ diff --git a/src/Functions/FunctionsExternalDictionaries.h b/src/Functions/FunctionsExternalDictionaries.h index 48ecc13da39..570147e4c86 100644 --- a/src/Functions/FunctionsExternalDictionaries.h +++ b/src/Functions/FunctionsExternalDictionaries.h @@ -675,27 +675,28 @@ private: return impl.isInjective(sample_columns); } - DataTypePtr getReturnTypeImpl(const DataTypes &) const override + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { - DataTypePtr result; + DataTypePtr result_type; if constexpr (IsDataTypeDecimal) - result = std::make_shared(DataType::maxPrecision(), 0); + result_type = std::make_shared(DataType::maxPrecision(), 0); else - result = std::make_shared(); + result_type = std::make_shared(); - return result; + auto return_type = impl.getReturnTypeImpl(arguments); + if (!return_type->equals(*result_type)) + throw Exception(ErrorCodes::TYPE_MISMATCH, "Function {} dictionary attribute has different type {} expected {}", + getName(), + return_type->getName(), + result_type->getName()); + + return result_type; } ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override { - auto return_type = impl.getReturnTypeImpl(arguments); - - if (!return_type->equals(*result_type)) - throw Exception(ErrorCodes::TYPE_MISMATCH, "Dictionary attribute has different type {} expected {}", - return_type->getName(), result_type->getName()); - - return impl.executeImpl(arguments, return_type, input_rows_count); + return impl.executeImpl(arguments, result_type, input_rows_count); } const FunctionDictGetNoType impl; diff --git a/src/Functions/formatRow.cpp b/src/Functions/formatRow.cpp index 53fb5d61472..12a5fc2cc27 100644 --- a/src/Functions/formatRow.cpp +++ b/src/Functions/formatRow.cpp @@ -35,7 +35,10 @@ class FunctionFormatRow : public IFunction public: static constexpr auto name = no_newline ? "formatRowNoNewline" : "formatRow"; - FunctionFormatRow(const String & format_name_, ContextPtr context_) : format_name(format_name_), context(context_) + FunctionFormatRow(String format_name_, Names arguments_column_names_, ContextPtr context_) + : format_name(std::move(format_name_)) + , arguments_column_names(std::move(arguments_column_names_)) + , context(std::move(context_)) { if (!FormatFactory::instance().getAllFormats().contains(format_name)) throw Exception(ErrorCodes::UNKNOWN_FORMAT, "Unknown format {}", format_name); @@ -55,9 +58,17 @@ public: WriteBufferFromVector buffer(vec); ColumnString::Offsets & offsets = col_str->getOffsets(); offsets.resize(input_rows_count); + Block arg_columns; - for (auto i = 1u; i < arguments.size(); ++i) - arg_columns.insert(arguments[i]); + + size_t arguments_size = arguments.size(); + for (size_t i = 1; i < arguments_size; ++i) + { + auto argument_column = arguments[i]; + argument_column.name = arguments_column_names[i]; + arg_columns.insert(std::move(argument_column)); + } + materializeBlockInplace(arg_columns); auto format_settings = getFormatSettings(context); auto out = FormatFactory::instance().getOutputFormat(format_name, buffer, arg_columns, context, format_settings); @@ -93,6 +104,7 @@ public: private: String format_name; + Names arguments_column_names; ContextPtr context; }; @@ -115,9 +127,14 @@ public: throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires at least two arguments: the format name and its output expression(s)", getName()); + Names arguments_column_names; + arguments_column_names.reserve(arguments.size()); + for (const auto & argument : arguments) + arguments_column_names.push_back(argument.name); + if (const auto * name_col = checkAndGetColumnConst(arguments.at(0).column.get())) return std::make_unique( - std::make_shared>(name_col->getValue(), context), + std::make_shared>(name_col->getValue(), std::move(arguments_column_names), context), collections::map(arguments, [](const auto & elem) { return elem.type; }), return_type); else diff --git a/src/Interpreters/ExternalDictionariesLoader.cpp b/src/Interpreters/ExternalDictionariesLoader.cpp index 2de0ff13e2b..080878c7d86 100644 --- a/src/Interpreters/ExternalDictionariesLoader.cpp +++ b/src/Interpreters/ExternalDictionariesLoader.cpp @@ -85,6 +85,11 @@ DictionaryStructure ExternalDictionariesLoader::getDictionaryStructure(const std return ExternalDictionariesLoader::getDictionaryStructure(*load_result.config); } +void ExternalDictionariesLoader::assertDictionaryStructureExists(const std::string & dictionary_name, ContextPtr query_context) const +{ + getDictionaryStructure(dictionary_name, query_context); +} + QualifiedTableName ExternalDictionariesLoader::qualifyDictionaryNameWithDatabase(const std::string & dictionary_name, ContextPtr query_context) const { auto qualified_name = QualifiedTableName::tryParseFromString(dictionary_name); diff --git a/src/Interpreters/ExternalDictionariesLoader.h b/src/Interpreters/ExternalDictionariesLoader.h index 421154a6d4f..659820d02ac 100644 --- a/src/Interpreters/ExternalDictionariesLoader.h +++ b/src/Interpreters/ExternalDictionariesLoader.h @@ -31,6 +31,8 @@ public: DictionaryStructure getDictionaryStructure(const std::string & dictionary_name, ContextPtr context) const; + void assertDictionaryStructureExists(const std::string & dictionary_name, ContextPtr context) const; + static DictionaryStructure getDictionaryStructure(const Poco::Util::AbstractConfiguration & config, const std::string & key_in_config = "dictionary"); static DictionaryStructure getDictionaryStructure(const ObjectConfig & config); diff --git a/src/Parsers/ExpressionElementParsers.cpp b/src/Parsers/ExpressionElementParsers.cpp index f2660e5a40c..72353a42a87 100644 --- a/src/Parsers/ExpressionElementParsers.cpp +++ b/src/Parsers/ExpressionElementParsers.cpp @@ -1474,7 +1474,7 @@ bool ParserColumnsTransformers::parseImpl(Pos & pos, ASTPtr & node, Expected & e auto opos = pos; if (ParserExpression().parse(pos, lambda, expected)) { - if (const auto * func = lambda->as(); func && func->name == "lambda") + if (auto * func = lambda->as(); func && func->name == "lambda") { if (func->arguments->children.size() != 2) throw Exception(ErrorCodes::SYNTAX_ERROR, "lambda requires two arguments"); @@ -1491,6 +1491,8 @@ bool ParserColumnsTransformers::parseImpl(Pos & pos, ASTPtr & node, Expected & e lambda_arg = *opt_arg_name; else throw Exception(ErrorCodes::SYNTAX_ERROR, "lambda argument declarations must be identifiers"); + + func->is_lambda_function = true; } else { diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 06424b041ba..4d6181e3686 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -404,15 +404,18 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl auto expected_header = planner.getQueryPlan().getCurrentDataStream().header; materializeBlockInplace(expected_header); - auto rename_actions_dag = ActionsDAG::makeConvertingActions( - query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(), - expected_header.getColumnsWithTypeAndName(), - ActionsDAG::MatchColumnsMode::Position, - true /*ignore_constant_values*/); - auto rename_step = std::make_unique(query_plan.getCurrentDataStream(), std::move(rename_actions_dag)); - std::string step_description = table_expression_data.isRemote() ? "Change remote column names to local column names" : "Change column names"; - rename_step->setStepDescription(std::move(step_description)); - query_plan.addStep(std::move(rename_step)); + if (!blocksHaveEqualStructure(query_plan.getCurrentDataStream().header, expected_header)) + { + auto rename_actions_dag = ActionsDAG::makeConvertingActions( + query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(), + expected_header.getColumnsWithTypeAndName(), + ActionsDAG::MatchColumnsMode::Position, + true /*ignore_constant_values*/); + auto rename_step = std::make_unique(query_plan.getCurrentDataStream(), std::move(rename_actions_dag)); + std::string step_description = table_expression_data.isRemote() ? "Change remote column names to local column names" : "Change column names"; + rename_step->setStepDescription(std::move(step_description)); + query_plan.addStep(std::move(rename_step)); + } } return {std::move(query_plan), from_stage}; diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index 286742586f2..5c0027b20f5 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -647,9 +647,7 @@ QueryTreeNodePtr buildQueryTreeDistributedTableReplacedWithLocalTable(const Sele else { auto resolved_remote_storage_id = query_context->resolveStorageID(remote_storage_id); - auto storage = DatabaseCatalog::instance().tryGetTable(resolved_remote_storage_id, query_context); - if (!storage) - storage = std::make_shared(resolved_remote_storage_id, distributed_storage_snapshot->metadata->getColumns()); + auto storage = std::make_shared(resolved_remote_storage_id, distributed_storage_snapshot->metadata->getColumns()); replacement_table_expression = std::make_shared(std::move(storage), query_context); } diff --git a/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.reference b/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.reference index fc77ed8a241..c5e3f5b9cc9 100644 --- a/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.reference +++ b/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.reference @@ -5,16 +5,16 @@ 11 40 -40 +0 41 -41 +0 2 42 -2 42 +2 0 43 -43 +0 11 11 diff --git a/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql b/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql index 93fa1f440b1..5cc06a41132 100644 --- a/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql +++ b/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql @@ -1,6 +1,7 @@ -- Tags: shard -set enable_positional_arguments=0; +set allow_experimental_analyzer = 1; +set enable_positional_arguments = 0; select 40 as z from (select * from system.numbers limit 3) group by z; select 41 as z from remote('127.0.0.{2,3}', system.one) group by z; diff --git a/tests/queries/0_stateless/00313_const_totals_extremes.reference b/tests/queries/0_stateless/00313_const_totals_extremes.reference index fcb39b8080c..fd10e1f4387 100644 --- a/tests/queries/0_stateless/00313_const_totals_extremes.reference +++ b/tests/queries/0_stateless/00313_const_totals_extremes.reference @@ -1,6 +1,6 @@ 1 1 -1 1 +0 1 1 1 1 1 @@ -27,7 +27,7 @@ "totals": { - "k": "1234567890123", + "k": "0", "count()": "1" }, @@ -65,7 +65,7 @@ [1.23, "1"] ], - "totals": [1.23, "1"], + "totals": [0, "1"], "extremes": { @@ -77,7 +77,7 @@ } 2010-01-01 1 -2010-01-01 1 +1970-01-01 1 2010-01-01 1 2010-01-01 1 @@ -104,7 +104,7 @@ "totals": { - "k": "2010-01-01 01:02:03", + "k": "1970-01-01 01:00:00", "count()": "1" }, @@ -142,7 +142,7 @@ [1.1, "1"] ], - "totals": [1.1, "1"], + "totals": [0, "1"], "extremes": { diff --git a/tests/queries/0_stateless/00313_const_totals_extremes.sh b/tests/queries/0_stateless/00313_const_totals_extremes.sh index 0c51d80abe8..5e880fe7aa0 100755 --- a/tests/queries/0_stateless/00313_const_totals_extremes.sh +++ b/tests/queries/0_stateless/00313_const_totals_extremes.sh @@ -4,10 +4,10 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh . "$CURDIR"/../shell_config.sh -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1 AS k, count() GROUP BY k WITH TOTALS"; -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1234567890123 AS k, count() GROUP BY k WITH TOTALS FORMAT JSON"; -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toFloat32(1.23) AS k, count() GROUP BY k WITH TOTALS FORMAT JSONCompact"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1 AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1234567890123 AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSON"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toFloat32(1.23) AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSONCompact"; -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDate('2010-01-01') AS k, count() GROUP BY k WITH TOTALS"; -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDateTime('2010-01-01 01:02:03') AS k, count() GROUP BY k WITH TOTALS FORMAT JSON"; -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1.1 AS k, count() GROUP BY k WITH TOTALS FORMAT JSONCompact"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDate('2010-01-01') AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDateTime('2010-01-01 01:02:03') AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSON"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1.1 AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSONCompact"; diff --git a/tests/queries/0_stateless/01013_totals_without_aggregation.reference b/tests/queries/0_stateless/01013_totals_without_aggregation.reference index 6dddf22a467..a1f5c1aa914 100644 --- a/tests/queries/0_stateless/01013_totals_without_aggregation.reference +++ b/tests/queries/0_stateless/01013_totals_without_aggregation.reference @@ -1,7 +1,7 @@ 11 -11 -12 +0 12 +0 13 -13 +0 diff --git a/tests/queries/0_stateless/01013_totals_without_aggregation.sql b/tests/queries/0_stateless/01013_totals_without_aggregation.sql index 584a8994767..291f95c6bd6 100644 --- a/tests/queries/0_stateless/01013_totals_without_aggregation.sql +++ b/tests/queries/0_stateless/01013_totals_without_aggregation.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + SELECT 11 AS n GROUP BY n WITH TOTALS; SELECT 12 AS n GROUP BY n WITH ROLLUP; SELECT 13 AS n GROUP BY n WITH CUBE; diff --git a/tests/queries/0_stateless/01115_join_with_dictionary.sql b/tests/queries/0_stateless/01115_join_with_dictionary.sql index cde1385eaae..83227aa555a 100644 --- a/tests/queries/0_stateless/01115_join_with_dictionary.sql +++ b/tests/queries/0_stateless/01115_join_with_dictionary.sql @@ -88,11 +88,11 @@ SELECT * FROM (SELECT number + 2 AS key FROM numbers(4)) s1 FULL JOIN dict_flat SELECT '-'; SELECT * FROM (SELECT number AS key FROM numbers(2)) s1 ANY INNER JOIN dict_flat d USING(key) ORDER BY s1.key; SELECT '-'; -SELECT * FROM (SELECT number AS key FROM numbers(2)) s1 ANY RIGHT JOIN dict_flat d USING(key) ORDER BY s1.key; +SELECT * FROM (SELECT number AS key FROM numbers(2)) s1 ANY RIGHT JOIN dict_flat d USING(key) ORDER BY key; SELECT '-'; SELECT * FROM (SELECT number AS key FROM numbers(2)) s1 SEMI RIGHT JOIN dict_flat d USING(key) ORDER BY s1.key; SELECT '-'; -SELECT * FROM (SELECT number AS key FROM numbers(2)) s1 ANTI RIGHT JOIN dict_flat d USING(key) ORDER BY s1.key; +SELECT * FROM (SELECT number AS key FROM numbers(2)) s1 ANTI RIGHT JOIN dict_flat d USING(key) ORDER BY key; SET join_use_nulls = 0; diff --git a/tests/queries/0_stateless/01470_columns_transformers.sql b/tests/queries/0_stateless/01470_columns_transformers.sql index 2da2f6e9c67..22c30ed36bf 100644 --- a/tests/queries/0_stateless/01470_columns_transformers.sql +++ b/tests/queries/0_stateless/01470_columns_transformers.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS columns_transformers; CREATE TABLE columns_transformers (i Int64, j Int16, k Int64) Engine=TinyLog; @@ -17,15 +19,15 @@ SELECT a.* APPLY(toDate) EXCEPT(i, j) APPLY(any) from columns_transformers a; SELECT * EXCEPT STRICT i from columns_transformers; SELECT * EXCEPT STRICT (i, j) from columns_transformers; SELECT * EXCEPT STRICT i, j1 from columns_transformers; -- { serverError 47 } -SELECT * EXCEPT STRICT(i, j1) from columns_transformers; -- { serverError 16 } +SELECT * EXCEPT STRICT(i, j1) from columns_transformers; -- { serverError 36 } SELECT * REPLACE STRICT i + 1 AS i from columns_transformers; -SELECT * REPLACE STRICT(i + 1 AS col) from columns_transformers; -- { serverError 16 } +SELECT * REPLACE STRICT(i + 1 AS col) from columns_transformers; -- { serverError 36 } SELECT * REPLACE(i + 1 AS i) APPLY(sum) from columns_transformers; SELECT columns_transformers.* REPLACE(j + 2 AS j, i + 1 AS i) APPLY(avg) from columns_transformers; SELECT columns_transformers.* REPLACE(j + 1 AS j, j + 2 AS j) APPLY(avg) from columns_transformers; -- { serverError 43 } -- REPLACE after APPLY will not match anything SELECT a.* APPLY(toDate) REPLACE(i + 1 AS i) APPLY(any) from columns_transformers a; -SELECT a.* APPLY(toDate) REPLACE STRICT(i + 1 AS i) APPLY(any) from columns_transformers a; -- { serverError 16 } +SELECT a.* APPLY(toDate) REPLACE STRICT(i + 1 AS i) APPLY(any) from columns_transformers a; -- { serverError 36 } EXPLAIN SYNTAX SELECT * APPLY(sum) from columns_transformers; EXPLAIN SYNTAX SELECT columns_transformers.* APPLY(avg) from columns_transformers; diff --git a/tests/queries/0_stateless/01603_decimal_mult_float.reference b/tests/queries/0_stateless/01603_decimal_mult_float.reference index c2917516e99..4c9d45423ee 100644 --- a/tests/queries/0_stateless/01603_decimal_mult_float.reference +++ b/tests/queries/0_stateless/01603_decimal_mult_float.reference @@ -3,7 +3,7 @@ 0.00012000000000000002 150.16500000000002 7.775900000000001 -56.62269 +56.622689999999984 598.8376688440277 299.41883695311844 0.7485470860550345 diff --git a/tests/queries/0_stateless/01603_decimal_mult_float.sql b/tests/queries/0_stateless/01603_decimal_mult_float.sql index 99e4b775da7..799ab91d332 100644 --- a/tests/queries/0_stateless/01603_decimal_mult_float.sql +++ b/tests/queries/0_stateless/01603_decimal_mult_float.sql @@ -1,3 +1,5 @@ +SET optimize_arithmetic_operations_in_aggregate_functions = 0; + SELECT toDecimal32(2, 2) * 1.2; SELECT toDecimal64(0.5, 2) * 20.33; SELECT 0.00001 * toDecimal32(12, 2); diff --git a/tests/queries/0_stateless/01774_tuple_null_in.reference b/tests/queries/0_stateless/01774_tuple_null_in.reference index aa47d0d46d4..573541ac970 100644 --- a/tests/queries/0_stateless/01774_tuple_null_in.reference +++ b/tests/queries/0_stateless/01774_tuple_null_in.reference @@ -1,2 +1 @@ 0 -0 diff --git a/tests/queries/0_stateless/01774_tuple_null_in.sql b/tests/queries/0_stateless/01774_tuple_null_in.sql index a9cc39e8840..f08e1a067e5 100644 --- a/tests/queries/0_stateless/01774_tuple_null_in.sql +++ b/tests/queries/0_stateless/01774_tuple_null_in.sql @@ -1,2 +1 @@ -SELECT (NULL, NULL) = (8, 0) OR (NULL, NULL) = (3, 2) OR (NULL, NULL) = (0, 0) OR (NULL, NULL) = (3, 1); SELECT (NULL, NULL) IN ((NULL, 0), (3, 1), (3, 2), (8, 0), (NULL, NULL)); diff --git a/tests/queries/0_stateless/01936_three_parts_identifiers_in_wrong_places.sql b/tests/queries/0_stateless/01936_three_parts_identifiers_in_wrong_places.sql index d2ca771edc5..83dd708c575 100644 --- a/tests/queries/0_stateless/01936_three_parts_identifiers_in_wrong_places.sql +++ b/tests/queries/0_stateless/01936_three_parts_identifiers_in_wrong_places.sql @@ -1,7 +1,9 @@ -SELECT dictGet(t.nest.a, concat(currentDatabase(), '.dict.dict'), 's', number) FROM numbers(5); -- { serverError 47 } +SET allow_experimental_analyzer = 1; -SELECT dictGetFloat64(t.b.s, 'database_for_dict.dict1', dictGetFloat64('Ta\0', toUInt64('databas\0_for_dict.dict1databas\0_for_dict.dict1', dictGetFloat64('', '', toUInt64(1048577), toDate(NULL)), NULL), toDate(dictGetFloat64(257, 'database_for_dict.dict1database_for_dict.dict1', '', toUInt64(NULL), 2, toDate(NULL)), '2019-05-2\0')), NULL, toUInt64(dictGetFloat64('', '', toUInt64(-9223372036854775808), toDate(NULL)), NULL)); -- { serverError 47 } +SELECT dictGet(t.nest.a, concat(currentDatabase(), '.dict.dict'), 's', number) FROM numbers(5); -- { serverError 36 } -SELECT NULL AND (2147483648 AND NULL) AND -2147483647, toUUID(((1048576 AND NULL) AND (2147483647 AND 257 AND NULL AND -2147483649) AND NULL) IN (test_01103.t1_distr.id), '00000000-e1fe-11e\0-bb8f\0853d60c00749'), stringToH3('89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff'); -- { serverError 47 } +SELECT dictGetFloat64(t.b.s, 'database_for_dict.dict1', dictGetFloat64('Ta\0', toUInt64('databas\0_for_dict.dict1databas\0_for_dict.dict1', dictGetFloat64('', '', toUInt64(1048577), toDate(NULL)), NULL), toDate(dictGetFloat64(257, 'database_for_dict.dict1database_for_dict.dict1', '', toUInt64(NULL), 2, toDate(NULL)), '2019-05-2\0')), NULL, toUInt64(dictGetFloat64('', '', toUInt64(-9223372036854775808), toDate(NULL)), NULL)); -- { serverError 36 } + +SELECT NULL AND (2147483648 AND NULL) AND -2147483647, toUUID(((1048576 AND NULL) AND (2147483647 AND 257 AND NULL AND -2147483649) AND NULL) IN (test_01103.t1_distr.id), '00000000-e1fe-11e\0-bb8f\0853d60c00749'), stringToH3('89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff89184926cc3ffff'); -- { serverError 36 } SELECT 'still alive'; diff --git a/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference b/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference index d00491fd7e5..e69de29bb2d 100644 --- a/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference +++ b/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference @@ -1 +0,0 @@ -1 diff --git a/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql b/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql index 1a989c1d952..869c335b621 100644 --- a/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql +++ b/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql @@ -1,5 +1,3 @@ -WITH x AS y SELECT 1; - DROP TEMPORARY TABLE IF EXISTS t1; DROP TEMPORARY TABLE IF EXISTS t2; diff --git a/tests/queries/1_stateful/00081_group_by_without_key_and_totals.reference b/tests/queries/1_stateful/00081_group_by_without_key_and_totals.reference index fc48d81d7bb..f7b0f272bd7 100644 --- a/tests/queries/1_stateful/00081_group_by_without_key_and_totals.reference +++ b/tests/queries/1_stateful/00081_group_by_without_key_and_totals.reference @@ -12,16 +12,16 @@ 523264 1 523264 -1 523264 +0 523264 1 523264 -1 523264 +0 523264 1 523264 -1 523264 +0 523264 1 523264 -1 523264 +0 523264 -1 86614 0 413533 1 9984 diff --git a/tests/queries/1_stateful/00081_group_by_without_key_and_totals.sql b/tests/queries/1_stateful/00081_group_by_without_key_and_totals.sql index d9653f48552..03e9e0feb40 100644 --- a/tests/queries/1_stateful/00081_group_by_without_key_and_totals.sql +++ b/tests/queries/1_stateful/00081_group_by_without_key_and_totals.sql @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + SELECT count() AS c FROM test.hits WHERE CounterID = 1704509 WITH TOTALS SETTINGS totals_mode = 'before_having', max_rows_to_group_by = 100000, group_by_overflow_mode = 'any'; SELECT count() AS c FROM test.hits WHERE CounterID = 1704509 WITH TOTALS SETTINGS totals_mode = 'after_having_inclusive', max_rows_to_group_by = 100000, group_by_overflow_mode = 'any'; SELECT count() AS c FROM test.hits WHERE CounterID = 1704509 WITH TOTALS SETTINGS totals_mode = 'after_having_exclusive', max_rows_to_group_by = 100000, group_by_overflow_mode = 'any'; From 47ece846bc29fc16625fadfc4b419d861db4b430 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 26 Feb 2023 14:45:10 +0100 Subject: [PATCH 164/342] Fixed tests --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 20 +++++++++---------- src/TableFunctions/TableFunctionMerge.cpp | 19 ++++++++++++++++++ src/TableFunctions/TableFunctionMerge.h | 1 + tests/queries/0_stateless/00203_full_join.sql | 10 +++++----- .../00744_join_not_found_column.sql | 20 +++++++++++++------ .../01165_lost_part_empty_partition.sql | 4 +++- .../01495_subqueries_in_with_statement_2.sql | 20 +++++++++---------- .../01720_join_implicit_cast.sql.j2 | 1 - .../01881_join_on_conditions_hash.sql.j2 | 7 ++++--- .../01881_join_on_conditions_merge.sql.j2 | 6 ++++-- .../0_stateless/02233_interpolate_1.sql | 2 +- 11 files changed, 69 insertions(+), 41 deletions(-) diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 01b9494bfc3..8a69eb348d6 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -4519,14 +4519,6 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi } } - /// Resolve function arguments - - bool allow_table_expressions = is_special_function_in || is_special_function_exists; - auto arguments_projection_names = resolveExpressionNodeList(function_node_ptr->getArgumentsNode(), - scope, - true /*allow_lambda_expression*/, - allow_table_expressions /*allow_table_expression*/); - if (is_special_function_exists) { /// Rewrite EXISTS (subquery) into 1 IN (SELECT 1 FROM (subquery) LIMIT 1). @@ -4535,20 +4527,26 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi auto constant_data_type = std::make_shared(); auto in_subquery = std::make_shared(Context::createCopy(scope.context)); + in_subquery->setIsSubquery(true); in_subquery->getProjection().getNodes().push_back(std::make_shared(1UL, constant_data_type)); in_subquery->getJoinTree() = exists_subquery_argument; in_subquery->getLimit() = std::make_shared(1UL, constant_data_type); - in_subquery->resolveProjectionColumns({NameAndTypePair("1", constant_data_type)}); - in_subquery->setIsSubquery(true); function_node_ptr = std::make_shared("in"); function_node_ptr->getArguments().getNodes() = {std::make_shared(1UL, constant_data_type), in_subquery}; node = function_node_ptr; function_name = "in"; - is_special_function_in = true; } + /// Resolve function arguments + + bool allow_table_expressions = is_special_function_in; + auto arguments_projection_names = resolveExpressionNodeList(function_node_ptr->getArgumentsNode(), + scope, + true /*allow_lambda_expression*/, + allow_table_expressions /*allow_table_expression*/); + auto & function_node = *function_node_ptr; /// Replace right IN function argument if it is table or table function with subquery that read ordinary columns diff --git a/src/TableFunctions/TableFunctionMerge.cpp b/src/TableFunctions/TableFunctionMerge.cpp index 742cc7f366b..066caa8170d 100644 --- a/src/TableFunctions/TableFunctionMerge.cpp +++ b/src/TableFunctions/TableFunctionMerge.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -36,6 +38,23 @@ namespace } } +std::vector TableFunctionMerge::skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr) const +{ + auto & table_function_node = query_node_table_function->as(); + auto & table_function_arguments_nodes = table_function_node.getArguments().getNodes(); + size_t table_function_arguments_size = table_function_arguments_nodes.size(); + + std::vector result; + + for (size_t i = 0; i < table_function_arguments_size; ++i) + { + auto * function_node = table_function_arguments_nodes[i]->as(); + if (function_node && function_node->getFunctionName() == "REGEXP") + result.push_back(i); + } + + return {0}; +} void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, ContextPtr context) { diff --git a/src/TableFunctions/TableFunctionMerge.h b/src/TableFunctions/TableFunctionMerge.h index 10221c8c72c..3439056deda 100644 --- a/src/TableFunctions/TableFunctionMerge.h +++ b/src/TableFunctions/TableFunctionMerge.h @@ -24,6 +24,7 @@ private: using DBToTableSetMap = std::map; const DBToTableSetMap & getSourceDatabasesAndTables(ContextPtr context) const; ColumnsDescription getActualTableStructure(ContextPtr context) const override; + std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; static TableSet getMatchedTablesWithAccess(const String & database_name, const String & table_regexp, const ContextPtr & context); diff --git a/tests/queries/0_stateless/00203_full_join.sql b/tests/queries/0_stateless/00203_full_join.sql index 9b07e9c84e7..43ce4c6da7d 100644 --- a/tests/queries/0_stateless/00203_full_join.sql +++ b/tests/queries/0_stateless/00203_full_join.sql @@ -17,17 +17,17 @@ DROP TABLE IF EXISTS t1_00203; DROP TABLE IF EXISTS t2_00203; CREATE TABLE t1_00203 (k1 UInt32, k2 UInt32, k3 UInt32, val_t1 String) ENGINE=TinyLog; -CREATE TABLE t2_00203 (val_t2 String, k3 UInt32, k2_alias UInt32, k1 UInt32) ENGINE=TinyLog; +CREATE TABLE t2_00203 (val_t2 String, k3 UInt32, k2 UInt32, k1 UInt32) ENGINE=TinyLog; INSERT INTO t1_00203 VALUES (1, 2, 3, 'aaa'), (2, 3, 4, 'bbb'); INSERT INTO t2_00203 VALUES ('ccc', 4, 3, 2), ('ddd', 7, 6, 5); -SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY FULL JOIN t2_00203 USING (k3, k1, k2 AS k2_alias) ORDER BY k1, k2, k3; -SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY RIGHT JOIN t2_00203 USING (k3, k1, k2 AS k2_alias) ORDER BY k1, k2, k3; +SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY FULL JOIN t2_00203 USING (k3, k1, k2) ORDER BY k1, k2, k3; +SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY RIGHT JOIN t2_00203 USING (k3, k1, k2) ORDER BY k1, k2, k3; SET any_join_distinct_right_table_keys = 0; -SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY FULL JOIN t2_00203 USING (k3, k1, k2 AS k2_alias) ORDER BY k1, k2, k3; -- { serverError 48 } -SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY RIGHT JOIN t2_00203 USING (k3, k1, k2 AS k2_alias) ORDER BY k1, k2, k3; +SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY FULL JOIN t2_00203 USING (k3, k1, k2) ORDER BY k1, k2, k3; -- { serverError 48 } +SELECT k1, k2, k3, val_t1, val_t2 FROM t1_00203 ANY RIGHT JOIN t2_00203 USING (k3, k1, k2) ORDER BY k1, k2, k3; DROP TABLE t1_00203; DROP TABLE t2_00203; diff --git a/tests/queries/0_stateless/00744_join_not_found_column.sql b/tests/queries/0_stateless/00744_join_not_found_column.sql index a271347dcd9..72969e7937d 100644 --- a/tests/queries/0_stateless/00744_join_not_found_column.sql +++ b/tests/queries/0_stateless/00744_join_not_found_column.sql @@ -1,3 +1,5 @@ +SET joined_subquery_requires_alias = 0; + DROP TEMPORARY TABLE IF EXISTS test_00744; CREATE TEMPORARY TABLE test_00744 ( @@ -11,27 +13,33 @@ FROM ( SELECT x, - 1 - FROM test_00744 + `1` + FROM + ( + SELECT x, 1 FROM test_00744 + ) ALL INNER JOIN ( SELECT count(), 1 FROM test_00744 - ) jss2 USING (1) + ) jss2 USING (`1`) LIMIT 10 ); SELECT x, - 1 -FROM test_00744 + `1` +FROM +( + SELECT x, 1 FROM test_00744 +) ALL INNER JOIN ( SELECT count(), 1 FROM test_00744 -) js2 USING (1) +) js2 USING (`1`) LIMIT 10; diff --git a/tests/queries/0_stateless/01165_lost_part_empty_partition.sql b/tests/queries/0_stateless/01165_lost_part_empty_partition.sql index 9279de1a5b3..acda7d68391 100644 --- a/tests/queries/0_stateless/01165_lost_part_empty_partition.sql +++ b/tests/queries/0_stateless/01165_lost_part_empty_partition.sql @@ -1,11 +1,13 @@ -- Tags: zookeeper +SET allow_experimental_analyzer = 1; + create table rmt1 (d DateTime, n int) engine=ReplicatedMergeTree('/test/01165/{database}/rmt', '1') order by n partition by toYYYYMMDD(d); create table rmt2 (d DateTime, n int) engine=ReplicatedMergeTree('/test/01165/{database}/rmt', '2') order by n partition by toYYYYMMDD(d); system stop replicated sends rmt1; insert into rmt1 values (now(), arrayJoin([1, 2])); -- { clientError 36 } -insert into rmt1(n) select * from system.numbers limit arrayJoin([1, 2]); -- { serverError 36 } +insert into rmt1(n) select * from system.numbers limit arrayJoin([1, 2]); -- { serverError 440 } insert into rmt1 values (now(), rand()); drop table rmt1; diff --git a/tests/queries/0_stateless/01495_subqueries_in_with_statement_2.sql b/tests/queries/0_stateless/01495_subqueries_in_with_statement_2.sql index 7ec4eeaa778..43dff687d84 100644 --- a/tests/queries/0_stateless/01495_subqueries_in_with_statement_2.sql +++ b/tests/queries/0_stateless/01495_subqueries_in_with_statement_2.sql @@ -1,29 +1,29 @@ -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)) SELECT * FROM x WHERE a in (SELECT a FROM y) ORDER BY a; -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)) SELECT * FROM x left JOIN y USING a ORDER BY a; -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)) SELECT * FROM x JOIN y USING a ORDER BY x.a; -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)), z AS (SELECT toUInt64(1) b) SELECT * FROM x JOIN y USING a WHERE a in (SELECT * FROM z); -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)), z AS (SELECT * FROM x WHERE a % 2), @@ -31,19 +31,17 @@ w AS (SELECT * FROM y WHERE a > 0) SELECT * FROM x JOIN y USING a WHERE a in (SELECT * FROM z) ORDER BY x.a; -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)), z AS (SELECT * FROM x WHERE a % 2), w AS (SELECT * FROM y WHERE a > 0) -SELECT max(a) FROM x JOIN y USING a WHERE a in (SELECT * FROM z) -HAVING a > (SELECT min(a) FROM w); +SELECT max(a) FROM x JOIN y USING a WHERE a in (SELECT * FROM z) AND a > (SELECT min(a) FROM w); -WITH +WITH x AS (SELECT number AS a FROM numbers(10)), y AS (SELECT number AS a FROM numbers(5)), z AS (SELECT * FROM x WHERE a % 2), w AS (SELECT * FROM y WHERE a > 0) -SELECT a FROM x JOIN y USING a WHERE a in (SELECT * FROM z) -HAVING a <= (SELECT max(a) FROM w) +SELECT a FROM x JOIN y USING a WHERE a in (SELECT * FROM z) AND a <= (SELECT max(a) FROM w) ORDER BY x.a; diff --git a/tests/queries/0_stateless/01720_join_implicit_cast.sql.j2 b/tests/queries/0_stateless/01720_join_implicit_cast.sql.j2 index f7439c8c56c..b08bcd3330e 100644 --- a/tests/queries/0_stateless/01720_join_implicit_cast.sql.j2 +++ b/tests/queries/0_stateless/01720_join_implicit_cast.sql.j2 @@ -47,7 +47,6 @@ SELECT any(toTypeName(a)) == 'Int32' AND any(toTypeName(b)) == 'Nullable(Int64)' SELECT any(toTypeName(a)) == 'Int32' AND any(toTypeName(b)) == 'Nullable(Int64)' FROM t_ab1 RIGHT JOIN t_ab2 USING (a, b); SELECT any(toTypeName(a)) == 'Int32' AND any(toTypeName(b)) == 'Nullable(Int64)' FROM t_ab1 INNER JOIN t_ab2 USING (a, b); -SELECT * FROM ( SELECT a, b as "_CAST(a, Int32)" FROM t_ab1 ) t_ab1 FULL JOIN t_ab2 ON (t_ab1.a == t_ab2.a); -- { serverError 44 } SELECT * FROM ( SELECT a, b as "_CAST(a, Int32)" FROM t_ab1 ) t_ab1 FULL JOIN t_ab2 USING (a) FORMAT Null; {% endfor %} diff --git a/tests/queries/0_stateless/01881_join_on_conditions_hash.sql.j2 b/tests/queries/0_stateless/01881_join_on_conditions_hash.sql.j2 index d2cc066a1b1..cda2b3e08ca 100644 --- a/tests/queries/0_stateless/01881_join_on_conditions_hash.sql.j2 +++ b/tests/queries/0_stateless/01881_join_on_conditions_hash.sql.j2 @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS t1; DROP TABLE IF EXISTS t2; DROP TABLE IF EXISTS t2_nullable; @@ -31,7 +33,6 @@ SELECT t1.key, t1.key2 FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.key == SELECT '--'; SELECT t1.key FROM t1 INNER ANY JOIN t2 ON t1.id == t2.id AND t2.key == t2.key2 AND t1.key == t1.key2; SELECT t1.key FROM t1 INNER ANY JOIN t2 ON t1.id == t2.id AND t2.key == t2.key2 AND t1.key == t1.key2 AND 0; -- { serverError INVALID_JOIN_ON_EXPRESSION } -SELECT t1.key FROM t1 INNER ANY JOIN t2 ON t1.id == t2.id AND t2.key == t2.key2 AND t1.key == t1.key2 AND 1; -- { serverError INVALID_JOIN_ON_EXPRESSION } SELECT '--'; SELECT '333' = t1.key FROM t1 INNER ANY JOIN t2 ON t1.id == t2.id AND t2.key == t2.key2 AND t1.key == t1.key2 AND t2.id > 2; @@ -71,8 +72,8 @@ SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.id; -- { serverError SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.id; -- { serverError 403 } SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.id + 2; -- { serverError 403 } SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.id + 2; -- { serverError 403 } -SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.key; -- { serverError 403 } -SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.key; -- { serverError 403 } +SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.key; -- { serverError 43 } +SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.key; -- { serverError 43 } SELECT * FROM t1 JOIN t2 ON t2.key == t2.key2 AND (t1.id == t2.id OR isNull(t2.key2)); -- { serverError 403 } SELECT * FROM t1 JOIN t2 ON t2.key == t2.key2 OR t1.id == t2.id; -- { serverError 403 } SELECT * FROM t1 JOIN t2 ON (t2.key == t2.key2 AND (t1.key == t1.key2 AND t1.key != 'XXX' OR t1.id == t2.id)) AND t1.id == t2.id; -- { serverError 403 } diff --git a/tests/queries/0_stateless/01881_join_on_conditions_merge.sql.j2 b/tests/queries/0_stateless/01881_join_on_conditions_merge.sql.j2 index 1704fedb92b..c1e9bdb4dc9 100644 --- a/tests/queries/0_stateless/01881_join_on_conditions_merge.sql.j2 +++ b/tests/queries/0_stateless/01881_join_on_conditions_merge.sql.j2 @@ -1,3 +1,5 @@ +SET allow_experimental_analyzer = 1; + DROP TABLE IF EXISTS t1; DROP TABLE IF EXISTS t2; DROP TABLE IF EXISTS t2_nullable; @@ -68,8 +70,8 @@ SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.id; -- { serverError SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.id; -- { serverError 403 } SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.id + 2; -- { serverError 403 } SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.id + 2; -- { serverError 403 } -SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.key; -- { serverError 403 } -SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.key; -- { serverError 403 } +SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t1.key; -- { serverError 43 } +SELECT * FROM t1 INNER ALL JOIN t2 ON t1.id == t2.id AND t2.key; -- { serverError 43 } SELECT * FROM t1 JOIN t2 ON t2.key == t2.key2 AND (t1.id == t2.id OR isNull(t2.key2)); -- { serverError 403 } SELECT * FROM t1 JOIN t2 ON t2.key == t2.key2 OR t1.id == t2.id; -- { serverError 403 } SELECT * FROM t1 JOIN t2 ON (t2.key == t2.key2 AND (t1.key == t1.key2 AND t1.key != 'XXX' OR t1.id == t2.id)) AND t1.id == t2.id; -- { serverError 403 } diff --git a/tests/queries/0_stateless/02233_interpolate_1.sql b/tests/queries/0_stateless/02233_interpolate_1.sql index b08f4a23898..1924ae1fcad 100644 --- a/tests/queries/0_stateless/02233_interpolate_1.sql +++ b/tests/queries/0_stateless/02233_interpolate_1.sql @@ -38,7 +38,7 @@ SELECT n, source, inter FROM ( # Test INTERPOLATE with inconsistent column - should produce error SELECT n, source, inter FROM ( SELECT toFloat32(number % 10) AS n, 'original' AS source, number as inter FROM numbers(10) WHERE number % 3 = 1 -) ORDER BY n WITH FILL FROM 0 TO 11.51 STEP 0.5 INTERPOLATE (inter AS source); -- { serverError 6 } +) ORDER BY n WITH FILL FROM 0 TO 11.51 STEP 0.5 INTERPOLATE (inter AS source); -- { serverError 32 } # Test INTERPOLATE with aliased column SELECT n, source, inter + 1 AS inter_p FROM ( From 6537029cccd57d51a9125f8d56e5d0ac3c7cc5b6 Mon Sep 17 00:00:00 2001 From: cmsxbc Date: Thu, 2 Mar 2023 02:15:09 +0800 Subject: [PATCH 165/342] fix: keeper systemd service file include invalid inline comment --- packages/clickhouse-keeper.service | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/clickhouse-keeper.service b/packages/clickhouse-keeper.service index 2809074c93a..e4ec5bf4ede 100644 --- a/packages/clickhouse-keeper.service +++ b/packages/clickhouse-keeper.service @@ -14,7 +14,8 @@ User=clickhouse Group=clickhouse Restart=always RestartSec=30 -RuntimeDirectory=%p # %p is resolved to the systemd unit name +# %p is resolved to the systemd unit name +RuntimeDirectory=%p ExecStart=/usr/bin/clickhouse-keeper --config=/etc/clickhouse-keeper/keeper_config.xml --pid-file=%t/%p/%p.pid # Minus means that this file is optional. EnvironmentFile=-/etc/default/%p From f3e3b916c913fee844ab9b564112289bb12130f9 Mon Sep 17 00:00:00 2001 From: kssenii Date: Wed, 1 Mar 2023 19:53:01 +0100 Subject: [PATCH 166/342] Allow nested custom disks --- src/Disks/getOrCreateDiskFromAST.cpp | 119 ++++++++++++------ src/Disks/getOrCreateDiskFromAST.h | 2 +- src/Parsers/FieldFromAST.cpp | 87 ++++++++----- src/Storages/MergeTree/MergeTreeSettings.cpp | 3 +- .../test_disk_configuration/test.py | 52 ++++++++ ...54_create_table_with_custom_disk.reference | 1 + .../02454_create_table_with_custom_disk.sql | 10 ++ 7 files changed, 204 insertions(+), 70 deletions(-) diff --git a/src/Disks/getOrCreateDiskFromAST.cpp b/src/Disks/getOrCreateDiskFromAST.cpp index 997bd2c853f..4479b532fb9 100644 --- a/src/Disks/getOrCreateDiskFromAST.cpp +++ b/src/Disks/getOrCreateDiskFromAST.cpp @@ -6,9 +6,14 @@ #include #include #include +#include +#include +#include #include #include #include +#include +#include namespace DB { @@ -18,48 +23,90 @@ namespace ErrorCodes extern const int BAD_ARGUMENTS; } -std::string getOrCreateDiskFromDiskAST(const ASTFunction & function, ContextPtr context) +namespace { - /// We need a unique name for a created custom disk, but it needs to be the same - /// after table is reattached or server is restarted, so take a hash of the disk - /// configuration serialized ast as a disk name suffix. - auto disk_setting_string = serializeAST(function, true); - auto disk_name = DiskSelector::TMP_INTERNAL_DISK_PREFIX - + toString(sipHash128(disk_setting_string.data(), disk_setting_string.size())); - - LOG_TRACE( - &Poco::Logger::get("getOrCreateDiskFromDiskAST"), - "Using disk name `{}` for custom disk {}", - disk_name, disk_setting_string); - - auto result_disk = context->getOrCreateDisk(disk_name, [&](const DisksMap & disks_map) -> DiskPtr { - const auto * function_args_expr = assert_cast(function.arguments.get()); - const auto & function_args = function_args_expr->children; - auto config = getDiskConfigurationFromAST(disk_name, function_args, context); - auto disk = DiskFactory::instance().create(disk_name, *config, disk_name, context, disks_map); - /// Mark that disk can be used without storage policy. - disk->markDiskAsCustom(); - return disk; - }); - - if (!result_disk->isRemote()) + std::string getOrCreateDiskFromDiskAST(const ASTFunction & function, ContextPtr context) { - static constexpr auto custom_disks_base_dir_in_config = "custom_local_disks_base_directory"; - auto disk_path_expected_prefix = context->getConfigRef().getString(custom_disks_base_dir_in_config, ""); + /// We need a unique name for a created custom disk, but it needs to be the same + /// after table is reattached or server is restarted, so take a hash of the disk + /// configuration serialized ast as a disk name suffix. + auto disk_setting_string = serializeAST(function, true); + auto disk_name = DiskSelector::TMP_INTERNAL_DISK_PREFIX + + toString(sipHash128(disk_setting_string.data(), disk_setting_string.size())); - if (disk_path_expected_prefix.empty()) - throw Exception( - ErrorCodes::BAD_ARGUMENTS, - "Base path for custom local disks must be defined in config file by `{}`", - custom_disks_base_dir_in_config); + LOG_TRACE( + &Poco::Logger::get("getOrCreateDiskFromDiskAST"), + "Using disk name `{}` for custom disk {}", + disk_name, disk_setting_string); - if (!pathStartsWith(result_disk->getPath(), disk_path_expected_prefix)) - throw Exception( - ErrorCodes::BAD_ARGUMENTS, - "Path of the custom local disk must be inside `{}` directory", - disk_path_expected_prefix); + auto result_disk = context->getOrCreateDisk(disk_name, [&](const DisksMap & disks_map) -> DiskPtr { + const auto * function_args_expr = assert_cast(function.arguments.get()); + const auto & function_args = function_args_expr->children; + auto config = getDiskConfigurationFromAST(disk_name, function_args, context); + auto disk = DiskFactory::instance().create(disk_name, *config, disk_name, context, disks_map); + /// Mark that disk can be used without storage policy. + disk->markDiskAsCustom(); + return disk; + }); + + if (!result_disk->isRemote()) + { + static constexpr auto custom_disks_base_dir_in_config = "custom_local_disks_base_directory"; + auto disk_path_expected_prefix = context->getConfigRef().getString(custom_disks_base_dir_in_config, ""); + + if (disk_path_expected_prefix.empty()) + throw Exception( + ErrorCodes::BAD_ARGUMENTS, + "Base path for custom local disks must be defined in config file by `{}`", + custom_disks_base_dir_in_config); + + if (!pathStartsWith(result_disk->getPath(), disk_path_expected_prefix)) + throw Exception( + ErrorCodes::BAD_ARGUMENTS, + "Path of the custom local disk must be inside `{}` directory", + disk_path_expected_prefix); + } + + return disk_name; } + class DiskConfigurationFlattener + { + public: + struct Data + { + ContextPtr context; + }; + + static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; } + + static void visit(ASTPtr & ast, Data & data) + { + if (isDiskFunction(ast)) + { + auto disk_name = getOrCreateDiskFromDiskAST(*ast->as(), data.context); + ast = std::make_shared(disk_name); + } + } + }; + + /// Visits children first. + using FlattenDiskConfigurationVisitor = InDepthNodeVisitor; +} + + +std::string getOrCreateDiskFromDiskAST(const ASTPtr & disk_function, ContextPtr context) +{ + if (!isDiskFunction(disk_function)) + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expected a disk function"); + + auto ast = disk_function->clone(); + + FlattenDiskConfigurationVisitor::Data data{context}; + FlattenDiskConfigurationVisitor{data}.visit(ast); + + auto disk_name = assert_cast(*ast).value.get(); + LOG_TRACE(&Poco::Logger::get("getOrCreateDiskFromDiskAST"), "Result disk name: {}", disk_name); return disk_name; } diff --git a/src/Disks/getOrCreateDiskFromAST.h b/src/Disks/getOrCreateDiskFromAST.h index 7c64707b0bd..0195f575278 100644 --- a/src/Disks/getOrCreateDiskFromAST.h +++ b/src/Disks/getOrCreateDiskFromAST.h @@ -13,6 +13,6 @@ class ASTFunction; * add it to DiskSelector by a unique (but always the same for given configuration) disk name * and return this name. */ -std::string getOrCreateDiskFromDiskAST(const ASTFunction & function, ContextPtr context); +std::string getOrCreateDiskFromDiskAST(const ASTPtr & disk_function, ContextPtr context); } diff --git a/src/Parsers/FieldFromAST.cpp b/src/Parsers/FieldFromAST.cpp index 3cd10c1cf80..cdfd9c627bc 100644 --- a/src/Parsers/FieldFromAST.cpp +++ b/src/Parsers/FieldFromAST.cpp @@ -5,7 +5,10 @@ #include #include #include +#include +#include #include +#include namespace DB @@ -31,42 +34,64 @@ bool FieldFromASTImpl::isSecret() const return isDiskFunction(ast); } +class DiskConfigurationHider +{ +public: + struct Data {}; + + static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; } + + static void visit(ASTPtr & ast, Data &) + { + if (isDiskFunction(ast)) + { + const auto & disk_function = assert_cast(*ast); + const auto * disk_function_args_expr = assert_cast(disk_function.arguments.get()); + const auto & disk_function_args = disk_function_args_expr->children; + + auto is_secret_arg = [](const std::string & arg_name) + { + /// We allow to not hide type of the disk, e.g. disk(type = s3, ...) + /// and also nested disk, e.g. disk(type = cache, disk = disk(type = s3, ...)) + return arg_name != "type" && arg_name != "disk"; + }; + + for (const auto & arg : disk_function_args) + { + auto * setting_function = arg->as(); + if (!setting_function || setting_function->name != "equals") + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected equals function"); + + auto * function_args_expr = assert_cast(setting_function->arguments.get()); + if (!function_args_expr) + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected arguments"); + + auto & function_args = function_args_expr->children; + if (function_args.empty()) + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected non zero number of arguments"); + + auto * key_identifier = function_args[0]->as(); + if (!key_identifier) + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected Identifier"); + + const std::string & key = key_identifier->name(); + if (is_secret_arg(key)) + function_args[1] = std::make_shared("[HIDDEN]"); + } + } + } +}; + +/// Visits children first. +using HideDiskConfigurationVisitor = InDepthNodeVisitor; + String FieldFromASTImpl::toString(bool show_secrets) const { if (!show_secrets && isDiskFunction(ast)) { auto hidden = ast->clone(); - const auto & disk_function = assert_cast(*hidden); - const auto * disk_function_args_expr = assert_cast(disk_function.arguments.get()); - const auto & disk_function_args = disk_function_args_expr->children; - - auto is_secret_arg = [](const std::string & arg_name) - { - return arg_name != "type"; - }; - - for (const auto & arg : disk_function_args) - { - auto * setting_function = arg->as(); - if (!setting_function || setting_function->name != "equals") - throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected equals function"); - - auto * function_args_expr = assert_cast(setting_function->arguments.get()); - if (!function_args_expr) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected arguments"); - - auto & function_args = function_args_expr->children; - if (function_args.empty()) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected non zero number of arguments"); - - auto * key_identifier = function_args[0]->as(); - if (!key_identifier) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad format: expected Identifier"); - - const std::string & key = key_identifier->name(); - if (is_secret_arg(key)) - function_args[1] = std::make_shared("[HIDDEN]"); - } + HideDiskConfigurationVisitor::Data data{}; + HideDiskConfigurationVisitor{data}.visit(hidden); return serializeAST(*hidden); } diff --git a/src/Storages/MergeTree/MergeTreeSettings.cpp b/src/Storages/MergeTree/MergeTreeSettings.cpp index e951b8f54cf..479e50fdebb 100644 --- a/src/Storages/MergeTree/MergeTreeSettings.cpp +++ b/src/Storages/MergeTree/MergeTreeSettings.cpp @@ -64,8 +64,7 @@ void MergeTreeSettings::loadFromQuery(ASTStorage & storage_def, ContextPtr conte auto ast = dynamic_cast(custom.getImpl()).ast; if (ast && isDiskFunction(ast)) { - const auto & ast_function = assert_cast(*ast); - auto disk_name = getOrCreateDiskFromDiskAST(ast_function, context); + auto disk_name = getOrCreateDiskFromDiskAST(ast, context); LOG_TRACE(&Poco::Logger::get("MergeTreeSettings"), "Created custom disk {}", disk_name); value = disk_name; } diff --git a/tests/integration/test_disk_configuration/test.py b/tests/integration/test_disk_configuration/test.py index 34f8bea219f..96cdb0213bc 100644 --- a/tests/integration/test_disk_configuration/test.py +++ b/tests/integration/test_disk_configuration/test.py @@ -295,6 +295,58 @@ def test_merge_tree_custom_disk_setting(start_cluster): ) +def test_merge_tree_nested_custom_disk_setting(start_cluster): + node = cluster.instances["node1"] + + minio = cluster.minio_client + for obj in list(minio.list_objects(cluster.minio_bucket, "data/", recursive=True)): + minio.remove_object(cluster.minio_bucket, obj.object_name) + assert ( + len(list(minio.list_objects(cluster.minio_bucket, "data/", recursive=True))) + == 0 + ) + + node.query( + """ + DROP TABLE IF EXISTS test; + CREATE TABLE test (a Int32) + ENGINE = MergeTree() order by tuple() + SETTINGS disk = disk( + type=cache, + max_size='1Gi', + path='/var/lib/clickhouse/custom_disk_cache/', + disk=disk( + type=s3, + endpoint='http://minio1:9001/root/data/', + access_key_id='minio', + secret_access_key='minio123')); + """ + ) + + node.query("INSERT INTO test SELECT number FROM numbers(100)") + node.query("SYSTEM DROP FILESYSTEM CACHE") + + # Check cache is filled + assert 0 == int(node.query("SELECT count() FROM system.filesystem_cache")) + assert 100 == int(node.query("SELECT count() FROM test")) + node.query("SELECT * FROM test") + assert 0 < int(node.query("SELECT count() FROM system.filesystem_cache")) + + # Check s3 is filled + assert ( + len(list(minio.list_objects(cluster.minio_bucket, "data/", recursive=True))) > 0 + ) + + node.restart_clickhouse() + + assert 100 == int(node.query("SELECT count() FROM test")) + + expected = """ + SETTINGS disk = disk(type = cache, max_size = \\'[HIDDEN]\\', path = \\'[HIDDEN]\\', disk = disk(type = s3, endpoint = \\'[HIDDEN]\\' + """ + assert expected.strip() in node.query(f"SHOW CREATE TABLE test").strip() + + def test_merge_tree_setting_override(start_cluster): node = cluster.instances["node3"] assert ( diff --git a/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference b/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference index 1d8610c59c9..a71d52b6f57 100644 --- a/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference +++ b/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference @@ -9,3 +9,4 @@ SETTINGS disk = disk(type = local, path = \'/var/lib/clickhouse/disks/local/\') CREATE TABLE default.test\n(\n `a` Int32\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS disk = disk(type = local, path = \'[HIDDEN]\'), index_granularity = 8192 a Int32 200 +CREATE TABLE default.test\n(\n `a` Int32\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS disk = disk(type = cache, max_size = \'[HIDDEN]\', path = \'[HIDDEN]\', disk = disk(type = local, path = \'[HIDDEN]\')), index_granularity = 8192 diff --git a/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql b/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql index 6cb1c0774aa..4909f91ccb1 100644 --- a/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql +++ b/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql @@ -26,3 +26,13 @@ DESCRIBE TABLE test; INSERT INTO test SELECT number FROM numbers(100); SELECT count() FROM test; + +DROP TABLE test; + +CREATE TABLE test (a Int32) +ENGINE = MergeTree() order by tuple() +SETTINGS disk = disk(type=cache, max_size='1Gi', path='/var/lib/clickhouse/custom_disk_cache/', disk=disk(type=local, path='/var/lib/clickhouse/disks/local/')); + +SHOW CREATE TABLE test; + +DROP TABLE test; From 13d50509c0b56fbef24132e4687122be633c6983 Mon Sep 17 00:00:00 2001 From: kssenii Date: Wed, 1 Mar 2023 20:16:15 +0100 Subject: [PATCH 167/342] Fix style check --- src/Disks/getOrCreateDiskFromAST.cpp | 1 - src/Parsers/FieldFromAST.cpp | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/Disks/getOrCreateDiskFromAST.cpp b/src/Disks/getOrCreateDiskFromAST.cpp index 4479b532fb9..9abc4a7d413 100644 --- a/src/Disks/getOrCreateDiskFromAST.cpp +++ b/src/Disks/getOrCreateDiskFromAST.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Parsers/FieldFromAST.cpp b/src/Parsers/FieldFromAST.cpp index cdfd9c627bc..b2251599441 100644 --- a/src/Parsers/FieldFromAST.cpp +++ b/src/Parsers/FieldFromAST.cpp @@ -5,8 +5,6 @@ #include #include #include -#include -#include #include #include From 9f6b154ccfeb498ba18373c5fe66c6e1080a48ea Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Wed, 1 Mar 2023 20:51:23 +0100 Subject: [PATCH 168/342] Insignificant improvement --- tests/ci/terminate_runner_lambda/app.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/ci/terminate_runner_lambda/app.py b/tests/ci/terminate_runner_lambda/app.py index ee9aadc488d..d72e6384ea2 100644 --- a/tests/ci/terminate_runner_lambda/app.py +++ b/tests/ci/terminate_runner_lambda/app.py @@ -64,19 +64,25 @@ def list_runners(access_token: str) -> RunnerDescriptions: "Authorization": f"token {access_token}", "Accept": "application/vnd.github.v3+json", } + per_page = 100 response = requests.get( - "https://api.github.com/orgs/ClickHouse/actions/runners?per_page=100", + f"https://api.github.com/orgs/ClickHouse/actions/runners?per_page={per_page}", headers=headers, ) response.raise_for_status() data = response.json() total_runners = data["total_count"] + print("Expected total runners", total_runners) runners = data["runners"] - total_pages = int(total_runners / 100 + 1) + # round to 0 for 0, 1 for 1..100, but to 2 for 101..200 + total_pages = (total_runners - 1) // per_page + 1 + + print("Total pages", total_pages) for i in range(2, total_pages + 1): response = requests.get( - f"https://api.github.com/orgs/ClickHouse/actions/runners?page={i}&per_page=100", + "https://api.github.com/orgs/ClickHouse/actions/runners" + f"?page={i}&per_page={per_page}", headers=headers, ) response.raise_for_status() @@ -95,6 +101,7 @@ def list_runners(access_token: str) -> RunnerDescriptions: busy=runner["busy"], ) result.append(desc) + return result From e17c0b6bf1e7cfe03e0d3f42a5398aee4ec18edb Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Wed, 1 Mar 2023 20:19:51 +0000 Subject: [PATCH 169/342] Review fixes --- src/Planner/PlannerJoinTree.cpp | 30 ++++++++----------- .../02674_trivial_count_analyzer.reference | 5 ++-- .../02674_trivial_count_analyzer.sql | 5 ++-- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 11944b4b71d..26fc2764a64 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -1,6 +1,7 @@ #include #include +#include #include @@ -19,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -27,6 +30,7 @@ #include #include #include +#include #include #include @@ -41,12 +45,8 @@ #include #include -#include -#include #include #include -#include -#include namespace DB { @@ -174,23 +174,17 @@ bool applyTrivialCountIfPossible( if (!storage || storage->hasLightweightDeletedMask()) return false; - if (settings.max_parallel_replicas > 1 || // - settings.allow_experimental_query_deduplication || // - settings.empty_result_for_aggregation_by_empty_set) + if (settings.max_parallel_replicas > 1 || settings.allow_experimental_query_deduplication + || settings.empty_result_for_aggregation_by_empty_set) return false; QueryTreeNodes aggregates = collectAggregateFunctionNodes(query_tree); if (aggregates.size() != 1) return false; - const auto * function_node = typeid_cast(aggregates.front().get()); - if (!function_node) - return false; - - if (!function_node->getAggregateFunction()) - return false; - - const auto * count_func = typeid_cast(function_node->getAggregateFunction().get()); + const auto & function_node = aggregates.front().get()->as(); + chassert(function_node.getAggregateFunction() != nullptr); + const auto * count_func = typeid_cast(function_node.getAggregateFunction().get()); if (!count_func) return false; @@ -226,7 +220,7 @@ bool applyTrivialCountIfPossible( SCOPE_EXIT_MEMORY_SAFE(agg_count.destroy(place)); agg_count.set(place, num_rows.value()); - auto column = ColumnAggregateFunction::create(function_node->getAggregateFunction()); + auto column = ColumnAggregateFunction::create(function_node.getAggregateFunction()); column->insertFrom(place); /// get count() argument type @@ -240,7 +234,7 @@ bool applyTrivialCountIfPossible( Block block_with_count{ {std::move(column), - std::make_shared(function_node->getAggregateFunction(), argument_types, Array{}), + std::make_shared(function_node.getAggregateFunction(), argument_types, Array{}), columns_names.front()}}; auto source = std::make_shared(block_with_count); @@ -416,7 +410,7 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } } - /// apply trivial_count optimization if possible + /// Apply trivial_count optimization if possible bool is_trivial_count_applied = is_single_table_expression && table_node && select_query_info.has_aggregates && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info.query_tree, planner_context->getQueryContext(), columns_names); diff --git a/tests/queries/0_stateless/02674_trivial_count_analyzer.reference b/tests/queries/0_stateless/02674_trivial_count_analyzer.reference index 2a94fd59d7b..05feadb58a0 100644 --- a/tests/queries/0_stateless/02674_trivial_count_analyzer.reference +++ b/tests/queries/0_stateless/02674_trivial_count_analyzer.reference @@ -18,8 +18,7 @@ select count(b) from m3; 2 select count() + 1 from m3; 3 --- drop table m3; - +drop table m3; -- checking queries with FINAL create table replacing_m3(a Int64, b UInt64) Engine=ReplacingMergeTree() order by (a, b); SYSTEM STOP MERGES replacing_m3; @@ -39,10 +38,10 @@ select count(a) from replacing_m3; 4 select count(b) from replacing_m3; 4 -set optimize_trivial_count_query=0; -- FIXME: wrong result for queries with FINAL select count() from replacing_m3 FINAL; 3 select count(a) from replacing_m3 FINAL; 3 select count(b) from replacing_m3 FINAL; 3 +drop table replacing_m3; diff --git a/tests/queries/0_stateless/02674_trivial_count_analyzer.sql b/tests/queries/0_stateless/02674_trivial_count_analyzer.sql index d4a686e6eff..988d1b9ba92 100644 --- a/tests/queries/0_stateless/02674_trivial_count_analyzer.sql +++ b/tests/queries/0_stateless/02674_trivial_count_analyzer.sql @@ -19,7 +19,7 @@ select count(a) from m3; select count(b) from m3; select count() + 1 from m3; --- drop table m3; +drop table m3; -- checking queries with FINAL create table replacing_m3(a Int64, b UInt64) Engine=ReplacingMergeTree() order by (a, b); @@ -38,9 +38,8 @@ select count(*) from replacing_m3; select count(a) from replacing_m3; select count(b) from replacing_m3; -set optimize_trivial_count_query=0; -- FIXME: wrong result for queries with FINAL select count() from replacing_m3 FINAL; select count(a) from replacing_m3 FINAL; select count(b) from replacing_m3 FINAL; --- drop table replacing_m3; +drop table replacing_m3; From 169398ec07d0db3d7812526db00aa1c3d4481fb2 Mon Sep 17 00:00:00 2001 From: Alexey Korepanov Date: Wed, 1 Mar 2023 21:57:51 +0100 Subject: [PATCH 170/342] Add check for empty leading spaces --- programs/server/play.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/programs/server/play.html b/programs/server/play.html index d90293fbd1b..8c3dd0c1f5b 100644 --- a/programs/server/play.html +++ b/programs/server/play.html @@ -687,6 +687,10 @@ const prev_lines = text_before_cursor.split('\n'); const prev_line = prev_lines.pop(); const lead_spaces = prev_line.match(/^\s*/)[0]; + if (!lead_spaces) { + return; + } + // Add leading spaces to the current line. elem.value = text_before_cursor + '\n' + lead_spaces + text_after_cursor; elem.selectionStart = cursor_pos + lead_spaces.length + 1; From 5ab5902f38906254af902d01cc71851f6cb78401 Mon Sep 17 00:00:00 2001 From: avogar Date: Wed, 1 Mar 2023 21:27:46 +0000 Subject: [PATCH 171/342] Allow control compression in Parquet/ORC/Arrow output formats, support more compression for input formats --- contrib/arrow-cmake/CMakeLists.txt | 20 +++++++++- contrib/orc | 2 +- src/Core/Settings.h | 3 ++ src/Core/SettingsEnums.cpp | 23 +++++++++++- src/Core/SettingsEnums.h | 6 +++ src/Formats/FormatFactory.cpp | 3 ++ src/Formats/FormatSettings.h | 29 +++++++++++++++ .../Formats/Impl/ArrowBlockOutputFormat.cpp | 26 ++++++++++++- .../Formats/Impl/ORCBlockOutputFormat.cpp | 32 +++++++++++++++- .../Formats/Impl/ParquetBlockOutputFormat.cpp | 37 +++++++++++++++++-- 10 files changed, 171 insertions(+), 10 deletions(-) diff --git a/contrib/arrow-cmake/CMakeLists.txt b/contrib/arrow-cmake/CMakeLists.txt index ae6f270a768..4181f916d63 100644 --- a/contrib/arrow-cmake/CMakeLists.txt +++ b/contrib/arrow-cmake/CMakeLists.txt @@ -115,6 +115,13 @@ configure_file("${ORC_SOURCE_SRC_DIR}/Adaptor.hh.in" "${ORC_BUILD_INCLUDE_DIR}/A # ARROW_ORC + adapters/orc/CMakefiles set(ORC_SRCS + "${CMAKE_CURRENT_BINARY_DIR}/orc_proto.pb.h" + "${ORC_SOURCE_SRC_DIR}/sargs/ExpressionTree.cc" + "${ORC_SOURCE_SRC_DIR}/sargs/Literal.cc" + "${ORC_SOURCE_SRC_DIR}/sargs/PredicateLeaf.cc" + "${ORC_SOURCE_SRC_DIR}/sargs/SargsApplier.cc" + "${ORC_SOURCE_SRC_DIR}/sargs/SearchArgument.cc" + "${ORC_SOURCE_SRC_DIR}/sargs/TruthValue.cc" "${ORC_SOURCE_SRC_DIR}/Exceptions.cc" "${ORC_SOURCE_SRC_DIR}/OrcFile.cc" "${ORC_SOURCE_SRC_DIR}/Reader.cc" @@ -129,13 +136,20 @@ set(ORC_SRCS "${ORC_SOURCE_SRC_DIR}/MemoryPool.cc" "${ORC_SOURCE_SRC_DIR}/RLE.cc" "${ORC_SOURCE_SRC_DIR}/RLEv1.cc" - "${ORC_SOURCE_SRC_DIR}/RLEv2.cc" + "${ORC_SOURCE_SRC_DIR}/RleDecoderV2.cc" + "${ORC_SOURCE_SRC_DIR}/RleEncoderV2.cc" + "${ORC_SOURCE_SRC_DIR}/RLEV2Util.cc" "${ORC_SOURCE_SRC_DIR}/Statistics.cc" "${ORC_SOURCE_SRC_DIR}/StripeStream.cc" "${ORC_SOURCE_SRC_DIR}/Timezone.cc" "${ORC_SOURCE_SRC_DIR}/TypeImpl.cc" "${ORC_SOURCE_SRC_DIR}/Vector.cc" "${ORC_SOURCE_SRC_DIR}/Writer.cc" + "${ORC_SOURCE_SRC_DIR}/Adaptor.cc" + "${ORC_SOURCE_SRC_DIR}/BloomFilter.cc" + "${ORC_SOURCE_SRC_DIR}/Murmur3.cc" + "${ORC_SOURCE_SRC_DIR}/BlockBuffer.cc" + "${ORC_SOURCE_SRC_DIR}/wrap/orc-proto-wrapper.cc" "${ORC_SOURCE_SRC_DIR}/io/InputStream.cc" "${ORC_SOURCE_SRC_DIR}/io/OutputStream.cc" "${ORC_ADDITION_SOURCE_DIR}/orc_proto.pb.cc" @@ -358,6 +372,9 @@ SET(ARROW_SRCS "${LIBRARY_DIR}/util/compression_zlib.cc" ${ARROW_SRCS}) add_definitions(-DARROW_WITH_ZSTD) SET(ARROW_SRCS "${LIBRARY_DIR}/util/compression_zstd.cc" ${ARROW_SRCS}) +add_definitions(-DARROW_WITH_BROTLI) +SET(ARROW_SRCS "${LIBRARY_DIR}/util/compression_brotli.cc" ${ARROW_SRCS}) + add_library(_arrow ${ARROW_SRCS}) @@ -372,6 +389,7 @@ target_link_libraries(_arrow PRIVATE ch_contrib::snappy ch_contrib::zlib ch_contrib::zstd + ch_contrib::brotli ) target_link_libraries(_arrow PUBLIC _orc) diff --git a/contrib/orc b/contrib/orc index f9a393ed243..c5d7755ba0b 160000 --- a/contrib/orc +++ b/contrib/orc @@ -1 +1 @@ -Subproject commit f9a393ed2433a60034795284f82d093b348f2102 +Subproject commit c5d7755ba0b9a95631c8daea4d094101f26ec761 diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 3908254b6f1..8d3e787f42c 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -857,6 +857,7 @@ class IColumn; M(Bool, output_format_parquet_string_as_string, false, "Use Parquet String type instead of Binary for String columns.", 0) \ M(Bool, output_format_parquet_fixed_string_as_fixed_byte_array, true, "Use Parquet FIXED_LENGTH_BYTE_ARRAY type instead of Binary for FixedString columns.", 0) \ M(ParquetVersion, output_format_parquet_version, "2.latest", "Parquet format version for output format. Supported versions: 1.0, 2.4, 2.6 and 2.latest (default)", 0) \ + M(ParquetCompression, output_format_parquet_compression_method, "snappy", "Compression method for Parquet output format. Supported codecs: snappy, lz4, brotli, zstd, gzip, none (uncompressed)", 0) \ M(String, output_format_avro_codec, "", "Compression codec used for output. Possible values: 'null', 'deflate', 'snappy'.", 0) \ M(UInt64, output_format_avro_sync_interval, 16 * 1024, "Sync interval in bytes.", 0) \ M(String, output_format_avro_string_column_pattern, "", "For Avro format: regexp of String columns to select as AVRO string.", 0) \ @@ -899,8 +900,10 @@ class IColumn; M(Bool, output_format_arrow_low_cardinality_as_dictionary, false, "Enable output LowCardinality type as Dictionary Arrow type", 0) \ M(Bool, output_format_arrow_string_as_string, false, "Use Arrow String type instead of Binary for String columns", 0) \ M(Bool, output_format_arrow_fixed_string_as_fixed_byte_array, true, "Use Arrow FIXED_SIZE_BINARY type instead of Binary for FixedString columns.", 0) \ + M(ArrowCompression, output_format_arrow_compression_method, "none", "Compression method for Arrow output format. Supported codecs: lz4_frame, zstd, none (uncompressed)", 0) \ \ M(Bool, output_format_orc_string_as_string, false, "Use ORC String type instead of Binary for String columns", 0) \ + M(ORCCompression, output_format_orc_compression_method, "none", "Compression method for ORC output format. Supported codecs: lz4, snappy, zlib, zstd, none (uncompressed)", 0) \ \ M(EnumComparingMode, format_capn_proto_enum_comparising_mode, FormatSettings::EnumComparingMode::BY_VALUES, "How to map ClickHouse Enum and CapnProto Enum", 0) \ \ diff --git a/src/Core/SettingsEnums.cpp b/src/Core/SettingsEnums.cpp index 9e1ab585bb0..91572aa1b3f 100644 --- a/src/Core/SettingsEnums.cpp +++ b/src/Core/SettingsEnums.cpp @@ -158,7 +158,7 @@ IMPLEMENT_SETTING_ENUM(EscapingRule, ErrorCodes::BAD_ARGUMENTS, {"XML", FormatSettings::EscapingRule::XML}, {"Raw", FormatSettings::EscapingRule::Raw}}) -IMPLEMENT_SETTING_ENUM(MsgPackUUIDRepresentation , ErrorCodes::BAD_ARGUMENTS, +IMPLEMENT_SETTING_ENUM(MsgPackUUIDRepresentation, ErrorCodes::BAD_ARGUMENTS, {{"bin", FormatSettings::MsgPackUUIDRepresentation::BIN}, {"str", FormatSettings::MsgPackUUIDRepresentation::STR}, {"ext", FormatSettings::MsgPackUUIDRepresentation::EXT}}) @@ -172,11 +172,30 @@ IMPLEMENT_SETTING_ENUM(LocalFSReadMethod, ErrorCodes::BAD_ARGUMENTS, {"pread", LocalFSReadMethod::pread}, {"read", LocalFSReadMethod::read}}) - IMPLEMENT_SETTING_ENUM_WITH_RENAME(ParquetVersion, ErrorCodes::BAD_ARGUMENTS, {{"1.0", FormatSettings::ParquetVersion::V1_0}, {"2.4", FormatSettings::ParquetVersion::V2_4}, {"2.6", FormatSettings::ParquetVersion::V2_6}, {"2.latest", FormatSettings::ParquetVersion::V2_LATEST}}) +IMPLEMENT_SETTING_ENUM(ParquetCompression, ErrorCodes::BAD_ARGUMENTS, + {{"none", FormatSettings::ParquetCompression::NONE}, + {"snappy", FormatSettings::ParquetCompression::SNAPPY}, + {"zstd", FormatSettings::ParquetCompression::ZSTD}, + {"gzip", FormatSettings::ParquetCompression::GZIP}, + {"lz4", FormatSettings::ParquetCompression::LZ4}, + {"brotli", FormatSettings::ParquetCompression::BROTLI}}) + +IMPLEMENT_SETTING_ENUM(ArrowCompression, ErrorCodes::BAD_ARGUMENTS, + {{"none", FormatSettings::ArrowCompression::NONE}, + {"lz4_frame", FormatSettings::ArrowCompression::LZ4_FRAME}, + {"zstd", FormatSettings::ArrowCompression::ZSTD}}) + +IMPLEMENT_SETTING_ENUM(ORCCompression, ErrorCodes::BAD_ARGUMENTS, + {{"none", FormatSettings::ORCCompression::NONE}, + {"snappy", FormatSettings::ORCCompression::SNAPPY}, + {"zstd", FormatSettings::ORCCompression::ZSTD}, + {"zlib", FormatSettings::ORCCompression::ZLIB}, + {"lz4", FormatSettings::ORCCompression::LZ4}}) + } diff --git a/src/Core/SettingsEnums.h b/src/Core/SettingsEnums.h index 139a04f3a5a..14e952bbd65 100644 --- a/src/Core/SettingsEnums.h +++ b/src/Core/SettingsEnums.h @@ -194,6 +194,12 @@ DECLARE_SETTING_ENUM_WITH_RENAME(EscapingRule, FormatSettings::EscapingRule) DECLARE_SETTING_ENUM_WITH_RENAME(MsgPackUUIDRepresentation, FormatSettings::MsgPackUUIDRepresentation) +DECLARE_SETTING_ENUM_WITH_RENAME(ParquetCompression, FormatSettings::ParquetCompression) + +DECLARE_SETTING_ENUM_WITH_RENAME(ArrowCompression, FormatSettings::ArrowCompression) + +DECLARE_SETTING_ENUM_WITH_RENAME(ORCCompression, FormatSettings::ORCCompression) + enum class Dialect { clickhouse, diff --git a/src/Formats/FormatFactory.cpp b/src/Formats/FormatFactory.cpp index aca3166a8c4..7f14810b260 100644 --- a/src/Formats/FormatFactory.cpp +++ b/src/Formats/FormatFactory.cpp @@ -118,6 +118,7 @@ FormatSettings getFormatSettings(ContextPtr context, const Settings & settings) format_settings.parquet.output_string_as_string = settings.output_format_parquet_string_as_string; format_settings.parquet.output_fixed_string_as_fixed_byte_array = settings.output_format_parquet_fixed_string_as_fixed_byte_array; format_settings.parquet.max_block_size = settings.input_format_parquet_max_block_size; + format_settings.parquet.output_compression_method = settings.output_format_parquet_compression_method; format_settings.pretty.charset = settings.output_format_pretty_grid_charset.toString() == "ASCII" ? FormatSettings::Pretty::Charset::ASCII : FormatSettings::Pretty::Charset::UTF8; format_settings.pretty.color = settings.output_format_pretty_color; format_settings.pretty.max_column_pad_width = settings.output_format_pretty_max_column_pad_width; @@ -158,6 +159,7 @@ FormatSettings getFormatSettings(ContextPtr context, const Settings & settings) format_settings.arrow.case_insensitive_column_matching = settings.input_format_arrow_case_insensitive_column_matching; format_settings.arrow.output_string_as_string = settings.output_format_arrow_string_as_string; format_settings.arrow.output_fixed_string_as_fixed_byte_array = settings.output_format_arrow_fixed_string_as_fixed_byte_array; + format_settings.arrow.output_compression_method = settings.output_format_arrow_compression_method; format_settings.orc.import_nested = settings.input_format_orc_import_nested; format_settings.orc.allow_missing_columns = settings.input_format_orc_allow_missing_columns; format_settings.orc.row_batch_size = settings.input_format_orc_row_batch_size; @@ -168,6 +170,7 @@ FormatSettings getFormatSettings(ContextPtr context, const Settings & settings) format_settings.orc.skip_columns_with_unsupported_types_in_schema_inference = settings.input_format_orc_skip_columns_with_unsupported_types_in_schema_inference; format_settings.orc.case_insensitive_column_matching = settings.input_format_orc_case_insensitive_column_matching; format_settings.orc.output_string_as_string = settings.output_format_orc_string_as_string; + format_settings.orc.output_compression_method = settings.output_format_orc_compression_method; format_settings.defaults_for_omitted_fields = settings.input_format_defaults_for_omitted_fields; format_settings.capn_proto.enum_comparing_mode = settings.format_capn_proto_enum_comparising_mode; format_settings.capn_proto.skip_fields_with_unsupported_types_in_schema_inference = settings.input_format_capn_proto_skip_fields_with_unsupported_types_in_schema_inference; diff --git a/src/Formats/FormatSettings.h b/src/Formats/FormatSettings.h index d1755a35c5f..88a5adbc8df 100644 --- a/src/Formats/FormatSettings.h +++ b/src/Formats/FormatSettings.h @@ -86,6 +86,13 @@ struct FormatSettings UInt64 max_parser_depth = DBMS_DEFAULT_MAX_PARSER_DEPTH; + enum class ArrowCompression + { + NONE, + LZ4_FRAME, + ZSTD + }; + struct { UInt64 row_group_size = 1000000; @@ -96,6 +103,7 @@ struct FormatSettings bool case_insensitive_column_matching = false; bool output_string_as_string = false; bool output_fixed_string_as_fixed_byte_array = true; + ArrowCompression output_compression_method = ArrowCompression::NONE; } arrow; struct @@ -183,6 +191,16 @@ struct FormatSettings V2_LATEST, }; + enum class ParquetCompression + { + NONE, + SNAPPY, + ZSTD, + LZ4, + GZIP, + BROTLI, + }; + struct { UInt64 row_group_size = 1000000; @@ -195,6 +213,7 @@ struct FormatSettings bool output_fixed_string_as_fixed_byte_array = true; UInt64 max_block_size = 8192; ParquetVersion output_version; + ParquetCompression output_compression_method = ParquetCompression::SNAPPY; } parquet; struct Pretty @@ -276,6 +295,15 @@ struct FormatSettings bool accurate_types_of_literals = true; } values; + enum class ORCCompression + { + NONE, + LZ4, + SNAPPY, + ZSTD, + ZLIB, + }; + struct { bool import_nested = false; @@ -285,6 +313,7 @@ struct FormatSettings bool case_insensitive_column_matching = false; std::unordered_set skip_stripes = {}; bool output_string_as_string = false; + ORCCompression output_compression_method = ORCCompression::NONE; } orc; /// For capnProto format we should determine how to diff --git a/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp index bf0e2448082..ec35c52e37c 100644 --- a/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp @@ -8,6 +8,7 @@ #include #include "ArrowBufferedStreams.h" #include "CHColumnToArrowColumn.h" +#include "config.h" namespace DB @@ -17,6 +18,25 @@ namespace ErrorCodes extern const int UNKNOWN_EXCEPTION; } +namespace +{ + +arrow::Compression::type getArrowCompression(FormatSettings::ArrowCompression method) +{ + switch (method) + { + case FormatSettings::ArrowCompression::NONE: + return arrow::Compression::type::UNCOMPRESSED; + case FormatSettings::ArrowCompression::ZSTD: + return arrow::Compression::type::ZSTD; + case FormatSettings::ArrowCompression::LZ4_FRAME: + return arrow::Compression::type::LZ4_FRAME; + } +} + +} + + ArrowBlockOutputFormat::ArrowBlockOutputFormat(WriteBuffer & out_, const Block & header_, bool stream_, const FormatSettings & format_settings_) : IOutputFormat(header_, out_) , stream{stream_} @@ -78,12 +98,14 @@ void ArrowBlockOutputFormat::prepareWriter(const std::shared_ptr { arrow_ostream = std::make_shared(out); arrow::Result> writer_status; + arrow::ipc::IpcWriteOptions options = arrow::ipc::IpcWriteOptions::Defaults(); + options.codec = *arrow::util::Codec::Create(getArrowCompression(format_settings.arrow.output_compression_method)); // TODO: should we use arrow::ipc::IpcOptions::alignment? if (stream) - writer_status = arrow::ipc::MakeStreamWriter(arrow_ostream.get(), schema); + writer_status = arrow::ipc::MakeStreamWriter(arrow_ostream.get(), schema, options); else - writer_status = arrow::ipc::MakeFileWriter(arrow_ostream.get(), schema); + writer_status = arrow::ipc::MakeFileWriter(arrow_ostream.get(), schema,options); if (!writer_status.ok()) throw Exception(ErrorCodes::UNKNOWN_EXCEPTION, diff --git a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp index 42c3e178436..ecb7c2fbc92 100644 --- a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp @@ -22,12 +22,42 @@ #include #include +#include "config.h" + namespace DB { namespace ErrorCodes { extern const int ILLEGAL_COLUMN; + extern const int NOT_IMPLEMENTED; +} + +namespace +{ + +orc::CompressionKind getORCCompression(FormatSettings::ORCCompression method) +{ + if (method == FormatSettings::ORCCompression::NONE) + return orc::CompressionKind::CompressionKind_NONE; + +#if USE_SNAPPY + if (method == FormatSettings::ORCCompression::SNAPPY) + return orc::CompressionKind::CompressionKind_SNAPPY; +#endif + + if (method == FormatSettings::ORCCompression::ZSTD) + return orc::CompressionKind::CompressionKind_ZSTD; + + if (method == FormatSettings::ORCCompression::LZ4) + return orc::CompressionKind::CompressionKind_LZ4; + + if (method == FormatSettings::ORCCompression::ZLIB) + return orc::CompressionKind::CompressionKind_ZLIB; + + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); +} + } ORCOutputStream::ORCOutputStream(WriteBuffer & out_) : out(out_) {} @@ -529,7 +559,7 @@ void ORCBlockOutputFormat::prepareWriter() { const Block & header = getPort(PortKind::Main).getHeader(); schema = orc::createStructType(); - options.setCompression(orc::CompressionKind::CompressionKind_NONE); + options.setCompression(getORCCompression(format_settings.orc.output_compression_method)); size_t columns_count = header.columns(); for (size_t i = 0; i != columns_count; ++i) schema->addStructField(header.safeGetByPosition(i).name, getORCType(recursiveRemoveLowCardinality(data_types[i]))); diff --git a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp index 18c81f8fd6a..742912df980 100644 --- a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp @@ -16,6 +16,9 @@ namespace ErrorCodes extern const int UNKNOWN_EXCEPTION; } +namespace +{ + static parquet::ParquetVersion::type getParquetVersion(const FormatSettings & settings) { switch (settings.parquet.output_version) @@ -31,6 +34,36 @@ static parquet::ParquetVersion::type getParquetVersion(const FormatSettings & se } } +parquet::Compression::type getParquetCompression(FormatSettings::ParquetCompression method) +{ + if (method == FormatSettings::ParquetCompression::NONE) + return parquet::Compression::type::UNCOMPRESSED; + +#if USE_SNAPPY + if (method == FormatSettings::ParquetCompression::SNAPPY) + return parquet::Compression::type::SNAPPY; +#endif + +#if USE_BROTLI + if (method == FormatSettings::ParquetCompression::BROTLI) + return parquet::Compression::type::BROTLI; +#endif + + if (method == FormatSettings::ParquetCompression::ZSTD) + return parquet::Compression::type::ZSTD; + + if (method == FormatSettings::ParquetCompression::LZ4) + return parquet::Compression::type::LZ4; + + if (method == FormatSettings::ParquetCompression::GZIP) + return parquet::Compression::type::GZIP; + + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); +} + + +} + ParquetBlockOutputFormat::ParquetBlockOutputFormat(WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_) : IOutputFormat(header_, out_), format_settings{format_settings_} { @@ -60,9 +93,7 @@ void ParquetBlockOutputFormat::consume(Chunk chunk) parquet::WriterProperties::Builder builder; builder.version(getParquetVersion(format_settings)); -#if USE_SNAPPY - builder.compression(parquet::Compression::SNAPPY); -#endif + builder.compression(getParquetCompression(format_settings.parquet.output_compression_method)); auto props = builder.build(); auto status = parquet::arrow::FileWriter::Open( *arrow_table->schema(), From 4f33e95c96af0b4cf9e1ad9ab3b92d1cfebf72e4 Mon Sep 17 00:00:00 2001 From: avogar Date: Wed, 1 Mar 2023 21:28:37 +0000 Subject: [PATCH 172/342] Add tests --- ...1_parquet_arrow_orc_compressions.reference | 14 +++++++++++ .../02581_parquet_arrow_orc_compressions.sh | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.reference create mode 100755 tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh diff --git a/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.reference b/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.reference new file mode 100644 index 00000000000..492b12dba56 --- /dev/null +++ b/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.reference @@ -0,0 +1,14 @@ +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 diff --git a/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh b/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh new file mode 100755 index 00000000000..10850e17bea --- /dev/null +++ b/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Parquet settings output_format_parquet_compression_method='none'" | $CLICKHOUSE_LOCAL --input-format=Parquet -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Parquet settings output_format_parquet_compression_method='lz4'" | $CLICKHOUSE_LOCAL --input-format=Parquet -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Parquet settings output_format_parquet_compression_method='snappy'" | $CLICKHOUSE_LOCAL --input-format=Parquet -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Parquet settings output_format_parquet_compression_method='zstd'" | $CLICKHOUSE_LOCAL --input-format=Parquet -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Parquet settings output_format_parquet_compression_method='brotli'" | $CLICKHOUSE_LOCAL --input-format=Parquet -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Parquet settings output_format_parquet_compression_method='gzip'" | $CLICKHOUSE_LOCAL --input-format=Parquet -q "select count() from table" + +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format ORC settings output_format_orc_compression_method='none'" | $CLICKHOUSE_LOCAL --input-format=ORC -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format ORC settings output_format_orc_compression_method='lz4'" | $CLICKHOUSE_LOCAL --input-format=ORC -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format ORC settings output_format_orc_compression_method='zstd'" | $CLICKHOUSE_LOCAL --input-format=ORC -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format ORC settings output_format_orc_compression_method='zlib'" | $CLICKHOUSE_LOCAL --input-format=ORC -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format ORC settings output_format_orc_compression_method='snappy'" | $CLICKHOUSE_LOCAL --input-format=ORC -q "select count() from table" + + +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Arrow settings output_format_arrow_compression_method='none'" | $CLICKHOUSE_LOCAL --input-format=Arrow -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Arrow settings output_format_arrow_compression_method='lz4_frame'" | $CLICKHOUSE_LOCAL --input-format=Arrow -q "select count() from table" +$CLICKHOUSE_LOCAL -q "select * from numbers(10) format Arrow settings output_format_arrow_compression_method='zstd'" | $CLICKHOUSE_LOCAL --input-format=Arrow -q "select count() from table" + From 657afa849e8ab3140ebe3ce21ae7f05ef249a82a Mon Sep 17 00:00:00 2001 From: AndyB Date: Wed, 1 Mar 2023 23:35:09 +0000 Subject: [PATCH 173/342] logger level in test --- tests/integration/test_log_levels_update/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_log_levels_update/test.py b/tests/integration/test_log_levels_update/test.py index 176733cd7cb..842c7914eaa 100644 --- a/tests/integration/test_log_levels_update/test.py +++ b/tests/integration/test_log_levels_update/test.py @@ -10,7 +10,7 @@ node = cluster.add_instance( config = """ - information + debug /var/log/clickhouse-server/clickhouse-server.log """ From 0a3a60b7a1ae5fcae0cc9354f22ff1fd781cc657 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 2 Mar 2023 00:56:11 +0100 Subject: [PATCH 174/342] Additionally search for inactive runners in the whole ASG --- tests/ci/terminate_runner_lambda/app.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/ci/terminate_runner_lambda/app.py b/tests/ci/terminate_runner_lambda/app.py index d72e6384ea2..61daa6af02a 100644 --- a/tests/ci/terminate_runner_lambda/app.py +++ b/tests/ci/terminate_runner_lambda/app.py @@ -156,8 +156,10 @@ def main( # so they will be cleaned out by ci_runners_metrics_lambda eventually instances_to_kill = [] + total_to_kill = 0 for zone, num_to_kill in to_kill_by_zone.items(): candidates = instances_by_zone[zone] + total_to_kill += num_to_kill if num_to_kill > len(candidates): raise Exception( f"Required to kill {num_to_kill}, but have only {len(candidates)} candidates in AV {zone}" @@ -195,8 +197,25 @@ def main( instances_to_kill += [runner.name for runner in delete_for_av] - print("Got instances to kill: ", ", ".join(instances_to_kill)) + if len(instances_to_kill) < total_to_kill: + print(f"Check other hosts from the same ASG {event['AutoScalingGroupName']}") + client = boto3.client("autoscaling") + as_groups = client.describe_auto_scaling_groups( + AutoScalingGroupNames=[event["AutoScalingGroupName"]] + ) + assert len(as_groups["AutoScalingGroups"]) == 1 + asg = as_groups["AutoScalingGroups"][0] + for instance in asg["Instances"]: + for runner in runners: + if runner.name == instance["InstanceId"] and not runner.busy: + print(f"Runner {runner.name} is not busy and can be deleted") + instances_to_kill.append(runner.name) + if total_to_kill <= len(instances_to_kill): + print("Got enough instances to kill") + break + + print("Got instances to kill: ", ", ".join(instances_to_kill)) response = {"InstanceIDs": instances_to_kill} print(response) return response From 036eaad9190f73f5fca4aee5e67a5293a6b64fdc Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 2 Mar 2023 01:29:40 +0100 Subject: [PATCH 175/342] Reduce lambda time by caching access_token --- tests/ci/terminate_runner_lambda/app.py | 58 +++++++++++++++++-------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/tests/ci/terminate_runner_lambda/app.py b/tests/ci/terminate_runner_lambda/app.py index 61daa6af02a..c9192417575 100644 --- a/tests/ci/terminate_runner_lambda/app.py +++ b/tests/ci/terminate_runner_lambda/app.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 import argparse -import sys import json +import sys import time from collections import namedtuple +from dataclasses import dataclass from typing import Any, Dict, List, Tuple import boto3 # type: ignore @@ -53,6 +54,32 @@ def get_access_token(jwt_token: str, installation_id: int) -> str: return data["token"] # type: ignore +@dataclass +class CachedToken: + time: int + value: str + + +cached_token = CachedToken(0, "") + + +def get_cached_access_token() -> str: + if time.time() - 500 < cached_token.time: + return cached_token.value + private_key, app_id = get_key_and_app_from_aws() + payload = { + "iat": int(time.time()) - 60, + "exp": int(time.time()) + (10 * 60), + "iss": app_id, + } + + encoded_jwt = jwt.encode(payload, private_key, algorithm="RS256") + installation_id = get_installation_id(encoded_jwt) + cached_token.time = int(time.time()) + cached_token.value = get_access_token(encoded_jwt, installation_id) + return cached_token.value + + RunnerDescription = namedtuple( "RunnerDescription", ["id", "name", "tags", "offline", "busy"] ) @@ -132,23 +159,11 @@ def get_candidates_to_be_killed(event_data: dict) -> Dict[str, List[str]]: return instances_by_zone -def main( - github_secret_key: str, github_app_id: int, event: dict -) -> Dict[str, List[str]]: +def main(access_token: str, event: dict) -> Dict[str, List[str]]: print("Got event", json.dumps(event, sort_keys=True, indent=4)) to_kill_by_zone = how_many_instances_to_kill(event) instances_by_zone = get_candidates_to_be_killed(event) - payload = { - "iat": int(time.time()) - 60, - "exp": int(time.time()) + (10 * 60), - "iss": github_app_id, - } - - encoded_jwt = jwt.encode(payload, github_secret_key, algorithm="RS256") - installation_id = get_installation_id(encoded_jwt) - access_token = get_access_token(encoded_jwt, installation_id) - runners = list_runners(access_token) # We used to delete potential hosts to terminate from GitHub runners pool, # but the documentation states: @@ -222,8 +237,7 @@ def main( def handler(event: dict, context: Any) -> Dict[str, List[str]]: - private_key, app_id = get_key_and_app_from_aws() - return main(private_key, app_id, event) + return main(get_cached_access_token(), event) if __name__ == "__main__": @@ -300,4 +314,14 @@ if __name__ == "__main__": "Cause": "SCALE_IN", } - main(private_key, args.app_id, sample_event) + payload = { + "iat": int(time.time()) - 60, + "exp": int(time.time()) + (10 * 60), + "iss": args.app_id, + } + + encoded_jwt = jwt.encode(payload, private_key, algorithm="RS256") + installation_id = get_installation_id(encoded_jwt) + access_token = get_access_token(encoded_jwt, args.app_id) + + main(access_token, sample_event) From 566a0e166fe0f51b21e11cb3977ac4f0e5aac6eb Mon Sep 17 00:00:00 2001 From: Yakov Olkhovskiy Date: Thu, 2 Mar 2023 00:42:02 +0000 Subject: [PATCH 176/342] preserve uid gid if running with sudo --- .../decompressor.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/utils/self-extracting-executable/decompressor.cpp b/utils/self-extracting-executable/decompressor.cpp index 37fbd043814..5f2a769dcdb 100644 --- a/utils/self-extracting-executable/decompressor.cpp +++ b/utils/self-extracting-executable/decompressor.cpp @@ -168,6 +168,24 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n return 0; } +bool getSudoIDs(uid_t &sudo_uid, uid_t &sudo_gid) +{ + sudo_uid = 0; + sudo_gid = 0; + + if (getuid() || geteuid() || getenv("SUDO_USER") == nullptr || getenv("SUDO_UID") == nullptr || getenv("SUDO_GID") == nullptr) + return false; + + char * str_end; + long id = strtol(getenv("SUDO_UID"), &str_end, 10); + if (*str_end == 0) + sudo_uid = static_cast(id); + id = strtol(getenv("SUDO_GID"), &str_end, 10); + if (*str_end == 0) + sudo_gid = static_cast(id); + + return true; +} /// Read data about files and decomrpess them. int decompressFiles(int input_fd, char * path, char * name, bool & have_compressed_analoge, bool & has_exec, char * decompressed_suffix, uint64_t * decompressed_umask) @@ -220,6 +238,10 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress return 1; } + uid_t sudo_uid = 0; + uid_t sudo_gid = 0; + getSudoIDs(sudo_uid, sudo_gid); + FileData file_info; /// Decompress files with appropriate file names for (size_t i = 0; i < le64toh(metadata.number_of_files); ++i) @@ -319,6 +341,9 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress perror("fsync"); if (0 != close(output_fd)) perror("close"); + + if (sudo_uid && sudo_gid) + chown(file_name, sudo_uid, sudo_gid); } if (0 != munmap(input, info_in.st_size)) @@ -532,6 +557,9 @@ int main(int/* argc*/, char* argv[]) return 1; } + if (uid_t sudo_uid = 0, sudo_gid = 0; getSudoIDs(sudo_uid, sudo_gid)) + chown(static_cast(self), sudo_uid, sudo_gid); + if (has_exec) { #if !defined(OS_DARWIN) && !defined(OS_FREEBSD) From 088869b654f70f5f32d1edafac90bceeeafde5b7 Mon Sep 17 00:00:00 2001 From: SupunKavinda Date: Thu, 2 Mar 2023 01:47:00 +0100 Subject: [PATCH 177/342] adds hyvor/clickhouse-php to docs --- docs/en/interfaces/third-party/client-libraries.md | 1 + docs/ru/interfaces/third-party/client-libraries.md | 1 + docs/zh/interfaces/third-party/client-libraries.md | 1 + 3 files changed, 3 insertions(+) diff --git a/docs/en/interfaces/third-party/client-libraries.md b/docs/en/interfaces/third-party/client-libraries.md index 4114e47e982..0e065cb7179 100644 --- a/docs/en/interfaces/third-party/client-libraries.md +++ b/docs/en/interfaces/third-party/client-libraries.md @@ -26,6 +26,7 @@ ClickHouse Inc does **not** maintain the libraries listed below and hasn’t don - [one-ck](https://github.com/lizhichao/one-ck) - [glushkovds/phpclickhouse-laravel](https://packagist.org/packages/glushkovds/phpclickhouse-laravel) - [kolya7k ClickHouse PHP extension](https://github.com//kolya7k/clickhouse-php) + - [hyvor/clickhouse-php](https://github.com/hyvor/clickhouse-php) - Go - [clickhouse](https://github.com/kshvakov/clickhouse/) - [go-clickhouse](https://github.com/roistat/go-clickhouse) diff --git a/docs/ru/interfaces/third-party/client-libraries.md b/docs/ru/interfaces/third-party/client-libraries.md index 6c63fb4c730..f55bbe2a47d 100644 --- a/docs/ru/interfaces/third-party/client-libraries.md +++ b/docs/ru/interfaces/third-party/client-libraries.md @@ -24,6 +24,7 @@ sidebar_label: "Клиентские библиотеки от сторонни - [SeasClick C++ client](https://github.com/SeasX/SeasClick) - [glushkovds/phpclickhouse-laravel](https://packagist.org/packages/glushkovds/phpclickhouse-laravel) - [kolya7k ClickHouse PHP extension](https://github.com//kolya7k/clickhouse-php) + - [hyvor/clickhouse-php](https://github.com/hyvor/clickhouse-php) - Go - [clickhouse](https://github.com/kshvakov/clickhouse/) - [go-clickhouse](https://github.com/roistat/go-clickhouse) diff --git a/docs/zh/interfaces/third-party/client-libraries.md b/docs/zh/interfaces/third-party/client-libraries.md index 1d46c19ce8b..4ee74f1d2c3 100644 --- a/docs/zh/interfaces/third-party/client-libraries.md +++ b/docs/zh/interfaces/third-party/client-libraries.md @@ -24,6 +24,7 @@ Yandex**没有**维护下面列出的库,也没有做过任何广泛的测试 - [SeasClick C++ client](https://github.com/SeasX/SeasClick) - [one-ck](https://github.com/lizhichao/one-ck) - [glushkovds/phpclickhouse-laravel](https://packagist.org/packages/glushkovds/phpclickhouse-laravel) + - [hyvor/clickhouse-php](https://github.com/hyvor/clickhouse-php) - Go - [clickhouse](https://github.com/kshvakov/clickhouse/) - [go-clickhouse](https://github.com/roistat/go-clickhouse) From ad4a44df52b6b04ac5977d12aa35b099a792133c Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 2 Mar 2023 02:59:27 +0100 Subject: [PATCH 178/342] fix --- src/Interpreters/Session.cpp | 32 ++++++++++++++++++- src/Interpreters/Session.h | 3 ++ src/Server/HTTPHandler.cpp | 14 +++++++- .../02435_rollback_cancelled_queries.sh | 12 +++---- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/Interpreters/Session.cpp b/src/Interpreters/Session.cpp index 7411050aa2d..70d4c0e6ae0 100644 --- a/src/Interpreters/Session.cpp +++ b/src/Interpreters/Session.cpp @@ -140,6 +140,23 @@ public: scheduleCloseSession(session, lock); } + void closeSession(const UUID & user_id, const String & session_id) + { + std::unique_lock lock(mutex); + Key key{user_id, session_id}; + auto it = sessions.find(key); + if (it == sessions.end()) + { + LOG_INFO(log, "Session {} not found for user {}, probably it's already closed", session_id, user_id); + return; + } + + if (!it->second.unique()) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot close session {} with refcount {}", session_id, it->second.use_count()); + + sessions.erase(it); + } + private: class SessionKeyHash { @@ -408,7 +425,7 @@ ContextMutablePtr Session::makeSessionContext(const String & session_name_, std: std::shared_ptr new_named_session; bool new_named_session_created = false; std::tie(new_named_session, new_named_session_created) - = NamedSessionsStorage::instance().acquireSession(global_context, user_id.value_or(UUID{}), session_name_, timeout_, session_check_); + = NamedSessionsStorage::instance().acquireSession(global_context, *user_id, session_name_, timeout_, session_check_); auto new_session_context = new_named_session->context; new_session_context->makeSessionContext(); @@ -533,5 +550,18 @@ void Session::releaseSessionID() named_session = nullptr; } +void Session::closeSession(const String & session_id) +{ + if (!user_id) /// User was not authenticated + return; + + /// named_session may be not set due to an early exception + if (!named_session) + return; + + releaseSessionID(); + NamedSessionsStorage::instance().closeSession(*user_id, session_id); +} + } diff --git a/src/Interpreters/Session.h b/src/Interpreters/Session.h index 0f17c378915..443867806d6 100644 --- a/src/Interpreters/Session.h +++ b/src/Interpreters/Session.h @@ -77,6 +77,9 @@ public: /// Releases the currently used session ID so it becomes available for reuse by another session. void releaseSessionID(); + /// Closes and removes session + void closeSession(const String & session_id); + private: std::shared_ptr getSessionLog() const; ContextMutablePtr makeQueryContextImpl(const ClientInfo * client_info_to_copy, ClientInfo * client_info_to_move) const; diff --git a/src/Server/HTTPHandler.cpp b/src/Server/HTTPHandler.cpp index 702743ef1f0..f468167f782 100644 --- a/src/Server/HTTPHandler.cpp +++ b/src/Server/HTTPHandler.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -678,7 +679,7 @@ void HTTPHandler::processQuery( std::unique_ptr in; static const NameSet reserved_param_names{"compress", "decompress", "user", "password", "quota_key", "query_id", "stacktrace", - "buffer_size", "wait_end_of_query", "session_id", "session_timeout", "session_check", "client_protocol_version"}; + "buffer_size", "wait_end_of_query", "session_id", "session_timeout", "session_check", "client_protocol_version", "close_session"}; Names reserved_param_suffixes; @@ -957,6 +958,14 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse /// In case of exception, send stack trace to client. bool with_stacktrace = false; + /// Close http session (if any) after processing the request + bool close_session = false; + String session_id; + + SCOPE_EXIT_SAFE({ + if (close_session && !session_id.empty()) + session->closeSession(session_id); + }); OpenTelemetry::TracingContextHolderPtr thread_trace_context; SCOPE_EXIT({ @@ -1006,6 +1015,9 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse HTMLForm params(default_settings, request); with_stacktrace = params.getParsed("stacktrace", false); + close_session = params.getParsed("close_session", false); + if (close_session) + session_id = params.get("session_id"); /// FIXME: maybe this check is already unnecessary. /// Workaround. Poco does not detect 411 Length Required case. diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh index b639cd5ef70..7c7ef037e02 100755 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -16,22 +16,20 @@ $CLICKHOUSE_CLIENT -q 'create table dedup_test(A Int64) Engine = MergeTree order function insert_data { IMPLICIT=$(( RANDOM % 2 )) - SESSION_ID="${SESSION}_$RANDOM$RANDOM$RANDOM" - TXN_SETTINGS="session_id=$SESSION_ID&throw_on_unsupported_query_inside_transaction=0" + SESSION_ID="${SESSION}_$RANDOM.$RANDOM.$RANDOM" + TXN_SETTINGS="session_id=$SESSION_ID&throw_on_unsupported_query_inside_transaction=0&implicit_transaction=$IMPLICIT" BEGIN="" COMMIT="" SETTINGS="query_id=$ID&$TXN_SETTINGS&max_insert_block_size=110000&min_insert_block_size_rows=110000" if [[ "$IMPLICIT" -eq 0 ]]; then $CLICKHOUSE_CURL -sS -d 'begin transaction' "$CLICKHOUSE_URL&$TXN_SETTINGS" + SETTINGS="$SETTINGS&session_check=1" BEGIN="begin transaction;" COMMIT=$(echo -ne "\n\ncommit") - else - TXN_SETTINGS="$TXN_SETTINGS&implicit_transaction=1" fi - SETTINGS="query_id=$ID&$TXN_SETTINGS&max_insert_block_size=110000&min_insert_block_size_rows=110000" # max_block_size=10000, so external table will contain smaller blocks that will be squashed on insert-select (more chances to catch a bug on query cancellation) - TRASH_SETTINGS="query_id=$ID&$TXN_SETTINGS&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_insert_block_size=110000&max_block_size=10000&min_insert_block_size_bytes=0&min_insert_block_size_rows=110000&max_insert_block_size=110000" + TRASH_SETTINGS="$SETTINGS&input_format_parallel_parsing=0&max_threads=1&max_insert_threads=1&max_block_size=10000&min_insert_block_size_bytes=0" TYPE=$(( RANDOM % 6 )) if [[ "$TYPE" -eq 0 ]]; then @@ -49,7 +47,7 @@ function insert_data fi if [[ "$IMPLICIT" -eq 0 ]]; then - $CLICKHOUSE_CURL -sS -d 'commit' "$CLICKHOUSE_URL&$TXN_SETTINGS" | grep -Faq "Transaction is not in RUNNING state" && $CLICKHOUSE_CURL -sS -d 'rollback' "$CLICKHOUSE_URL&$TXN_SETTINGS" + $CLICKHOUSE_CURL -sS -d 'commit' "$CLICKHOUSE_URL&$TXN_SETTINGS&close_session=1" | grep -Faq "Transaction is not in RUNNING state" && $CLICKHOUSE_CURL -sS -d 'rollback' "$CLICKHOUSE_URL&$TXN_SETTINGS" fi } From 11fbb6076b5616287ef635c5eef63dec6919ca82 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Thu, 2 Mar 2023 14:20:35 +0800 Subject: [PATCH 179/342] fix fast test --- .../02415_all_new_functions_must_be_documented.reference | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/02415_all_new_functions_must_be_documented.reference b/tests/queries/0_stateless/02415_all_new_functions_must_be_documented.reference index e41249af54c..571a3c3afb5 100644 --- a/tests/queries/0_stateless/02415_all_new_functions_must_be_documented.reference +++ b/tests/queries/0_stateless/02415_all_new_functions_must_be_documented.reference @@ -421,6 +421,7 @@ mapContains mapContainsKeyLike mapExtractKeyLike mapFilter +mapFromArrays mapKeys mapPopulateSeries mapSubtract From a6cf2cdab8e59e8457def1a891fb3a3443918ca3 Mon Sep 17 00:00:00 2001 From: avogar Date: Thu, 2 Mar 2023 10:36:07 +0000 Subject: [PATCH 180/342] Fix style, add docs --- docs/en/interfaces/formats.md | 3 +++ .../en/operations/settings/settings-formats.md | 18 ++++++++++++++++++ .../Formats/Impl/ArrowBlockOutputFormat.cpp | 2 -- .../Formats/Impl/ORCBlockOutputFormat.cpp | 2 -- .../Formats/Impl/ParquetBlockOutputFormat.cpp | 2 +- .../02581_parquet_arrow_orc_compressions.sh | 1 + 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/docs/en/interfaces/formats.md b/docs/en/interfaces/formats.md index b2b2c6d5b1e..1b32de9723f 100644 --- a/docs/en/interfaces/formats.md +++ b/docs/en/interfaces/formats.md @@ -1973,6 +1973,7 @@ To exchange data with Hadoop, you can use [HDFS table engine](/docs/en/engines/t - [input_format_parquet_skip_columns_with_unsupported_types_in_schema_inference](/docs/en/operations/settings/settings-formats.md/#input_format_parquet_skip_columns_with_unsupported_types_in_schema_inference) - allow skipping columns with unsupported types while schema inference for Parquet format. Default value - `false`. - [output_format_parquet_fixed_string_as_fixed_byte_array](/docs/en/operations/settings/settings-formats.md/#output_format_parquet_fixed_string_as_fixed_byte_array) - use Parquet FIXED_LENGTH_BYTE_ARRAY type instead of Binary/String for FixedString columns. Default value - `true`. - [output_format_parquet_version](/docs/en/operations/settings/settings-formats.md/#output_format_parquet_version) - The version of Parquet format used in output format. Default value - `2.latest`. +- [output_format_parquet_compression_method](/docs/en/operations/settings/settings-formats.md/#output_format_parquet_compression_method) - compression method used in output Parquet format. Default value - `snappy`. ## Arrow {#data-format-arrow} @@ -2041,6 +2042,7 @@ $ clickhouse-client --query="SELECT * FROM {some_table} FORMAT Arrow" > {filenam - [input_format_arrow_allow_missing_columns](/docs/en/operations/settings/settings-formats.md/#input_format_arrow_allow_missing_columns) - allow missing columns while reading Arrow data. Default value - `false`. - [input_format_arrow_skip_columns_with_unsupported_types_in_schema_inference](/docs/en/operations/settings/settings-formats.md/#input_format_arrow_skip_columns_with_unsupported_types_in_schema_inference) - allow skipping columns with unsupported types while schema inference for Arrow format. Default value - `false`. - [output_format_arrow_fixed_string_as_fixed_byte_array](/docs/en/operations/settings/settings-formats.md/#output_format_arrow_fixed_string_as_fixed_byte_array) - use Arrow FIXED_SIZE_BINARY type instead of Binary/String for FixedString columns. Default value - `true`. +- [output_format_arrow_compression_method](/docs/en/operations/settings/settings-formats.md/#output_format_arrow_compression_method) - compression method used in output Arrow format. Default value - `none`. ## ArrowStream {#data-format-arrow-stream} @@ -2096,6 +2098,7 @@ $ clickhouse-client --query="SELECT * FROM {some_table} FORMAT ORC" > {filename. ### Arrow format settings {#parquet-format-settings} - [output_format_arrow_string_as_string](/docs/en/operations/settings/settings-formats.md/#output_format_arrow_string_as_string) - use Arrow String type instead of Binary for String columns. Default value - `false`. +- [output_format_orc_compression_method](/docs/en/operations/settings/settings-formats.md/#output_format_orc_compression_method) - compression method used in output ORC format. Default value - `none`. - [input_format_arrow_import_nested](/docs/en/operations/settings/settings-formats.md/#input_format_arrow_import_nested) - allow inserting array of structs into Nested table in Arrow input format. Default value - `false`. - [input_format_arrow_case_insensitive_column_matching](/docs/en/operations/settings/settings-formats.md/#input_format_arrow_case_insensitive_column_matching) - ignore case when matching Arrow columns with ClickHouse columns. Default value - `false`. - [input_format_arrow_allow_missing_columns](/docs/en/operations/settings/settings-formats.md/#input_format_arrow_allow_missing_columns) - allow missing columns while reading Arrow data. Default value - `false`. diff --git a/docs/en/operations/settings/settings-formats.md b/docs/en/operations/settings/settings-formats.md index 3580d83f704..919ebaf562f 100644 --- a/docs/en/operations/settings/settings-formats.md +++ b/docs/en/operations/settings/settings-formats.md @@ -1014,6 +1014,12 @@ Use Arrow FIXED_SIZE_BINARY type instead of Binary/String for FixedString column Enabled by default. +### output_format_arrow_compression_method {#output_format_arrow_compression_method} + +Compression method used in output Arrow format. Supported codecs: `lz4_frame`, `zstd`, `none` (uncompressed) + +Default value: `none`. + ## ORC format settings {#orc-format-settings} ### input_format_orc_import_nested {#input_format_orc_import_nested} @@ -1057,6 +1063,12 @@ Use ORC String type instead of Binary for String columns. Disabled by default. +### output_format_orc_compression_method {#output_format_orc_compression_method} + +Compression method used in output ORC format. Supported codecs: `lz4`, `snappy`, `zlib`, `zstd`, `none` (uncompressed) + +Default value: `none`. + ## Parquet format settings {#parquet-format-settings} ### input_format_parquet_import_nested {#input_format_parquet_import_nested} @@ -1112,6 +1124,12 @@ The version of Parquet format used in output format. Supported versions: `1.0`, Default value: `2.latest`. +### output_format_parquet_compression_method {#output_format_parquet_compression_method} + +Compression method used in output Parquet format. Supported codecs: `snappy`, `lz4`, `brotli`, `zstd`, `gzip`, `none` (uncompressed) + +Default value: `snappy`. + ## Hive format settings {#hive-format-settings} ### input_format_hive_text_fields_delimiter {#input_format_hive_text_fields_delimiter} diff --git a/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp index ec35c52e37c..c85c0342c8c 100644 --- a/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ArrowBlockOutputFormat.cpp @@ -8,7 +8,6 @@ #include #include "ArrowBufferedStreams.h" #include "CHColumnToArrowColumn.h" -#include "config.h" namespace DB @@ -36,7 +35,6 @@ arrow::Compression::type getArrowCompression(FormatSettings::ArrowCompression me } - ArrowBlockOutputFormat::ArrowBlockOutputFormat(WriteBuffer & out_, const Block & header_, bool stream_, const FormatSettings & format_settings_) : IOutputFormat(header_, out_) , stream{stream_} diff --git a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp index ecb7c2fbc92..39cacde94ed 100644 --- a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp @@ -22,8 +22,6 @@ #include #include -#include "config.h" - namespace DB { diff --git a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp index 742912df980..cedd8a9c54c 100644 --- a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp @@ -14,6 +14,7 @@ namespace DB namespace ErrorCodes { extern const int UNKNOWN_EXCEPTION; + extern const int NOT_IMPLEMENTED; } namespace @@ -61,7 +62,6 @@ parquet::Compression::type getParquetCompression(FormatSettings::ParquetCompress throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); } - } ParquetBlockOutputFormat::ParquetBlockOutputFormat(WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_) diff --git a/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh b/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh index 10850e17bea..89b5147f026 100755 --- a/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh +++ b/tests/queries/0_stateless/02581_parquet_arrow_orc_compressions.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# Tags: no-fasttest CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh From 5ddc9a9eedd1bd26881142e5ce5aacf020ed7983 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Wed, 1 Mar 2023 10:37:47 +0000 Subject: [PATCH 181/342] Add binary compatibility check for Aarch64 --- .github/workflows/backport_branches.yml | 40 +++++++++++-- .github/workflows/master.yml | 40 +++++++++++-- .github/workflows/pull_request.yml | 40 +++++++++++-- .github/workflows/release_branches.yml | 40 +++++++++++-- tests/ci/ci_config.py | 5 +- tests/ci/compatibility_check.py | 80 ++++++++++++++++++------- 6 files changed, 207 insertions(+), 38 deletions(-) diff --git a/.github/workflows/backport_branches.yml b/.github/workflows/backport_branches.yml index 110c06631c7..b14a32127c4 100644 --- a/.github/workflows/backport_branches.yml +++ b/.github/workflows/backport_branches.yml @@ -79,7 +79,7 @@ jobs: with: name: changed_images path: ${{ runner.temp }}/changed_images.json - CompatibilityCheck: + CompatibilityCheckX86: needs: [BuilderDebRelease] runs-on: [self-hosted, style-checker] steps: @@ -98,12 +98,43 @@ jobs: uses: actions/download-artifact@v3 with: path: ${{ env.REPORTS_PATH }} - - name: CompatibilityCheck + - name: CompatibilityCheckX86 run: | sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + - name: Cleanup + if: always() + run: | + docker ps --quiet | xargs --no-run-if-empty docker kill ||: + docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||: + sudo rm -fr "$TEMP_PATH" + CompatibilityCheckAarch64: + needs: [BuilderDebAarch64] + runs-on: [self-hosted, style-checker] + steps: + - name: Set envs + run: | + cat >> "$GITHUB_ENV" << 'EOF' + TEMP_PATH=${{runner.temp}}/compatibility_check + REPO_COPY=${{runner.temp}}/compatibility_check/ClickHouse + REPORTS_PATH=${{runner.temp}}/reports_dir + EOF + - name: Check out repository code + uses: ClickHouse/checkout@v1 + with: + clear-repository: true + - name: Download json reports + uses: actions/download-artifact@v3 + with: + path: ${{ env.REPORTS_PATH }} + - name: CompatibilityCheckAarch64 + run: | + sudo rm -fr "$TEMP_PATH" + mkdir -p "$TEMP_PATH" + cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (aarch64)" --check-glibc - name: Cleanup if: always() run: | @@ -741,7 +772,8 @@ jobs: - FunctionalStatefulTestDebug - StressTestTsan - IntegrationTestsRelease - - CompatibilityCheck + - CompatibilityCheckX86 + - CompatibilityCheckAarch64 runs-on: [self-hosted, style-checker] steps: - name: Check out repository code diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 7e045992dee..e224d6cf5c3 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -110,7 +110,7 @@ jobs: docker ps --quiet | xargs --no-run-if-empty docker kill ||: docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||: sudo rm -fr "$TEMP_PATH" - CompatibilityCheck: + CompatibilityCheckX86: needs: [BuilderDebRelease] runs-on: [self-hosted, style-checker] steps: @@ -129,12 +129,43 @@ jobs: uses: actions/download-artifact@v3 with: path: ${{ env.REPORTS_PATH }} - - name: CompatibilityCheck + - name: CompatibilityCheckX86 run: | sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + - name: Cleanup + if: always() + run: | + docker ps --quiet | xargs --no-run-if-empty docker kill ||: + docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||: + sudo rm -fr "$TEMP_PATH" + CompatibilityCheckAarch64: + needs: [BuilderDebAarch64] + runs-on: [self-hosted, style-checker] + steps: + - name: Set envs + run: | + cat >> "$GITHUB_ENV" << 'EOF' + TEMP_PATH=${{runner.temp}}/compatibility_check + REPO_COPY=${{runner.temp}}/compatibility_check/ClickHouse + REPORTS_PATH=${{runner.temp}}/reports_dir + EOF + - name: Check out repository code + uses: ClickHouse/checkout@v1 + with: + clear-repository: true + - name: Download json reports + uses: actions/download-artifact@v3 + with: + path: ${{ env.REPORTS_PATH }} + - name: CompatibilityCheckAarch64 + run: | + sudo rm -fr "$TEMP_PATH" + mkdir -p "$TEMP_PATH" + cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (aarch64)" --check-glibc - name: Cleanup if: always() run: | @@ -3124,7 +3155,8 @@ jobs: - PerformanceComparisonX86-1 - PerformanceComparisonX86-2 - PerformanceComparisonX86-3 - - CompatibilityCheck + - CompatibilityCheckX86 + - CompatibilityCheckAarch64 - ASTFuzzerTestDebug - ASTFuzzerTestAsan - ASTFuzzerTestTsan diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 7d410f833c5..ae6cb1d3a83 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -174,7 +174,7 @@ jobs: docker ps --quiet | xargs --no-run-if-empty docker kill ||: docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||: sudo rm -fr "$TEMP_PATH" "$CACHES_PATH" - CompatibilityCheck: + CompatibilityCheckX86: needs: [BuilderDebRelease] runs-on: [self-hosted, style-checker] steps: @@ -193,12 +193,43 @@ jobs: uses: actions/download-artifact@v3 with: path: ${{ env.REPORTS_PATH }} - - name: CompatibilityCheck + - name: CompatibilityCheckX86 run: | sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + - name: Cleanup + if: always() + run: | + docker ps --quiet | xargs --no-run-if-empty docker kill ||: + docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||: + sudo rm -fr "$TEMP_PATH" + CompatibilityCheckAarch64: + needs: [BuilderDebAarch64] + runs-on: [self-hosted, style-checker] + steps: + - name: Set envs + run: | + cat >> "$GITHUB_ENV" << 'EOF' + TEMP_PATH=${{runner.temp}}/compatibility_check + REPO_COPY=${{runner.temp}}/compatibility_check/ClickHouse + REPORTS_PATH=${{runner.temp}}/reports_dir + EOF + - name: Check out repository code + uses: ClickHouse/checkout@v1 + with: + clear-repository: true + - name: Download json reports + uses: actions/download-artifact@v3 + with: + path: ${{ env.REPORTS_PATH }} + - name: CompatibilityCheckAarch64 + run: | + sudo rm -fr "$TEMP_PATH" + mkdir -p "$TEMP_PATH" + cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (aarch64)" --check-glibc - name: Cleanup if: always() run: | @@ -4792,7 +4823,8 @@ jobs: - UnitTestsMsan - UnitTestsUBsan - UnitTestsReleaseClang - - CompatibilityCheck + - CompatibilityCheckX86 + - CompatibilityCheckAarch64 - IntegrationTestsFlakyCheck - SQLancerTestRelease - SQLancerTestDebug diff --git a/.github/workflows/release_branches.yml b/.github/workflows/release_branches.yml index 4d2a99c2106..229532efdab 100644 --- a/.github/workflows/release_branches.yml +++ b/.github/workflows/release_branches.yml @@ -71,7 +71,7 @@ jobs: with: name: changed_images path: ${{ runner.temp }}/changed_images.json - CompatibilityCheck: + CompatibilityCheckX86: needs: [BuilderDebRelease] runs-on: [self-hosted, style-checker] steps: @@ -90,12 +90,43 @@ jobs: uses: actions/download-artifact@v3 with: path: ${{ env.REPORTS_PATH }} - - name: CompatibilityCheck + - name: CompatibilityCheckX86 run: | sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + - name: Cleanup + if: always() + run: | + docker ps --quiet | xargs --no-run-if-empty docker kill ||: + docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||: + sudo rm -fr "$TEMP_PATH" + CompatibilityCheckAarch64: + needs: [BuilderDebAarch64] + runs-on: [self-hosted, style-checker] + steps: + - name: Set envs + run: | + cat >> "$GITHUB_ENV" << 'EOF' + TEMP_PATH=${{runner.temp}}/compatibility_check + REPO_COPY=${{runner.temp}}/compatibility_check/ClickHouse + REPORTS_PATH=${{runner.temp}}/reports_dir + EOF + - name: Check out repository code + uses: ClickHouse/checkout@v1 + with: + clear-repository: true + - name: Download json reports + uses: actions/download-artifact@v3 + with: + path: ${{ env.REPORTS_PATH }} + - name: CompatibilityCheckAarch64 + run: | + sudo rm -fr "$TEMP_PATH" + mkdir -p "$TEMP_PATH" + cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (aarch64)" --check-glibc - name: Cleanup if: always() run: | @@ -1947,7 +1978,8 @@ jobs: - IntegrationTestsTsan1 - IntegrationTestsTsan2 - IntegrationTestsTsan3 - - CompatibilityCheck + - CompatibilityCheckX86 + - CompatibilityCheckAarch64 runs-on: [self-hosted, style-checker] steps: - name: Check out repository code diff --git a/tests/ci/ci_config.py b/tests/ci/ci_config.py index 08cd2d466d0..9df198430d2 100644 --- a/tests/ci/ci_config.py +++ b/tests/ci/ci_config.py @@ -316,9 +316,12 @@ CI_CONFIG = { "Integration tests flaky check (asan)": { "required_build": "package_asan", }, - "Compatibility check": { + "Compatibility check (x86)": { "required_build": "package_release", }, + "Compatibility check (aarch64)": { + "required_build": "package_aarch64", + }, "Unit tests (release-clang)": { "required_build": "binary_release", }, diff --git a/tests/ci/compatibility_check.py b/tests/ci/compatibility_check.py index 0bdcf1ba3b4..9f3c1a1ca8b 100644 --- a/tests/ci/compatibility_check.py +++ b/tests/ci/compatibility_check.py @@ -2,6 +2,7 @@ from distutils.version import StrictVersion from typing import List, Tuple +import argparse import logging import os import subprocess @@ -28,9 +29,7 @@ from upload_result_helper import upload_results IMAGE_UBUNTU = "clickhouse/test-old-ubuntu" IMAGE_CENTOS = "clickhouse/test-old-centos" -MAX_GLIBC_VERSION = "2.4" DOWNLOAD_RETRIES_COUNT = 5 -CHECK_NAME = "Compatibility check" def process_os_check(log_path: str) -> TestResult: @@ -43,7 +42,7 @@ def process_os_check(log_path: str) -> TestResult: return TestResult(name, "OK") -def process_glibc_check(log_path: str) -> TestResults: +def process_glibc_check(log_path: str, max_glibc_version: str) -> TestResults: test_results = [] # type: TestResults with open(log_path, "r") as log: for line in log: @@ -53,7 +52,7 @@ def process_glibc_check(log_path: str) -> TestResults: _, version = symbol_with_glibc.split("@GLIBC_") if version == "PRIVATE": test_results.append(TestResult(symbol_with_glibc, "FAIL")) - elif StrictVersion(version) > MAX_GLIBC_VERSION: + elif StrictVersion(version) > max_glibc_version: test_results.append(TestResult(symbol_with_glibc, "FAIL")) if not test_results: test_results.append(TestResult("glibc check", "OK")) @@ -61,10 +60,10 @@ def process_glibc_check(log_path: str) -> TestResults: def process_result( - result_folder: str, server_log_folder: str + result_folder: str, server_log_folder: str, max_glibc_version: str ) -> Tuple[str, str, TestResults, List[str]]: glibc_log_path = os.path.join(result_folder, "glibc.log") - test_results = process_glibc_check(glibc_log_path) + test_results = process_glibc_check(glibc_log_path, max_glibc_version) status = "success" description = "Compatibility check passed" @@ -101,13 +100,18 @@ def process_result( return status, description, test_results, result_logs -def get_run_commands( - build_path, result_folder, server_log_folder, image_centos, image_ubuntu -): +def get_run_commands_glibc(build_path, result_folder): return [ f"readelf -s --wide {build_path}/usr/bin/clickhouse | grep '@GLIBC_' > {result_folder}/glibc.log", f"readelf -s --wide {build_path}/usr/bin/clickhouse-odbc-bridge | grep '@GLIBC_' >> {result_folder}/glibc.log", f"readelf -s --wide {build_path}/usr/bin/clickhouse-library-bridge | grep '@GLIBC_' >> {result_folder}/glibc.log", + ] + + +def get_run_commands_distributions( + build_path, result_folder, server_log_folder, image_centos, image_ubuntu +): + return [ f"docker run --network=host --volume={build_path}/usr/bin/clickhouse:/clickhouse " f"--volume={build_path}/etc/clickhouse-server:/config " f"--volume={server_log_folder}:/var/log/clickhouse-server {image_ubuntu} > {result_folder}/ubuntu:12.04", @@ -117,9 +121,21 @@ def get_run_commands( ] +def parse_args(): + parser = argparse.ArgumentParser("Check compatibility with old distributions") + parser.add_argument("--check-name", required=True) + parser.add_argument("--check-glibc", action="store_true") + parser.add_argument( + "--check-distributions", action="store_true" + ) # currently hardcoded to x86, don't enable for ARM + return parser.parse_args() + + def main(): logging.basicConfig(level=logging.INFO) + args = parse_args() + stopwatch = Stopwatch() temp_path = TEMP_PATH @@ -129,13 +145,11 @@ def main(): gh = Github(get_best_robot_token(), per_page=100) - rerun_helper = RerunHelper(gh, pr_info, CHECK_NAME) + rerun_helper = RerunHelper(gh, pr_info, args.check_name) if rerun_helper.is_already_finished_by_status(): logging.info("Check is already finished according to github status, exiting") sys.exit(0) - docker_images = get_images_with_versions(reports_path, [IMAGE_CENTOS, IMAGE_UBUNTU]) - packages_path = os.path.join(temp_path, "packages") if not os.path.exists(packages_path): os.makedirs(packages_path) @@ -145,7 +159,7 @@ def main(): "clickhouse-common-static_" in url or "clickhouse-server_" in url ) - download_builds_filter(CHECK_NAME, reports_path, packages_path, url_filter) + download_builds_filter(args.check_name, reports_path, packages_path, url_filter) for f in os.listdir(packages_path): if ".deb" in f: @@ -162,9 +176,24 @@ def main(): if not os.path.exists(result_path): os.makedirs(result_path) - run_commands = get_run_commands( - packages_path, result_path, server_log_path, docker_images[0], docker_images[1] - ) + run_commands = [] + + if args.check_glibc: + check_glibc_commands = get_run_commands_glibc(packages_path, result_path) + run_commands.extend(check_glibc_commands) + + if args.check_distributions: + docker_images = get_images_with_versions( + reports_path, [IMAGE_CENTOS, IMAGE_UBUNTU] + ) + check_distributions_commands = get_run_commands_distributions( + packages_path, + result_path, + server_log_path, + docker_images[0], + docker_images[1], + ) + run_commands.extend(check_distributions_commands) state = "success" for run_command in run_commands: @@ -177,13 +206,22 @@ def main(): subprocess.check_call(f"sudo chown -R ubuntu:ubuntu {temp_path}", shell=True) + # See https://sourceware.org/glibc/wiki/Glibc%20Timeline + max_glibc_version = "" + if "x86" in args.check_name: + max_glibc_version = "2.4" + elif "aarch64" in args.check_nam: + max_glibc_version = "2.18" # because of build with newer sysroot? + else: + raise Exception("Can't determine max glibc version") + s3_helper = S3Helper() state, description, test_results, additional_logs = process_result( - result_path, server_log_path + result_path, server_log_path, max_glibc_version ) ch_helper = ClickHouseHelper() - mark_flaky_tests(ch_helper, CHECK_NAME, test_results) + mark_flaky_tests(ch_helper, args.check_name, test_results) report_url = upload_results( s3_helper, @@ -191,10 +229,10 @@ def main(): pr_info.sha, test_results, additional_logs, - CHECK_NAME, + args.check_name, ) print(f"::notice ::Report url: {report_url}") - post_commit_status(gh, pr_info.sha, CHECK_NAME, description, state, report_url) + post_commit_status(gh, pr_info.sha, args.check_name, description, state, report_url) prepared_events = prepare_tests_results_for_clickhouse( pr_info, @@ -203,7 +241,7 @@ def main(): stopwatch.duration_seconds, stopwatch.start_time_str, report_url, - CHECK_NAME, + args.check_name, ) ch_helper.insert_events_into(db="default", table="checks", events=prepared_events) From 33cafc6e891a692af3d1315706afb8cea95804e2 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Thu, 2 Mar 2023 11:22:32 +0000 Subject: [PATCH 182/342] Automatic style fix --- tests/ci/compatibility_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ci/compatibility_check.py b/tests/ci/compatibility_check.py index 9f3c1a1ca8b..398f3df3deb 100644 --- a/tests/ci/compatibility_check.py +++ b/tests/ci/compatibility_check.py @@ -211,7 +211,7 @@ def main(): if "x86" in args.check_name: max_glibc_version = "2.4" elif "aarch64" in args.check_nam: - max_glibc_version = "2.18" # because of build with newer sysroot? + max_glibc_version = "2.18" # because of build with newer sysroot? else: raise Exception("Can't determine max glibc version") From cdc8596f81cd4322fda3a188ffcbb9a4065e8882 Mon Sep 17 00:00:00 2001 From: KevinyhZou Date: Thu, 2 Mar 2023 20:08:39 +0800 Subject: [PATCH 183/342] enable int type in repeat function --- .../functions/string-functions.md | 2 +- src/Functions/repeat.cpp | 29 +++++++++++++------ .../01013_repeat_function.reference | 8 ++--- .../0_stateless/01013_repeat_function.sql | 8 ++--- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/docs/en/sql-reference/functions/string-functions.md b/docs/en/sql-reference/functions/string-functions.md index 845be6e04c7..f3c5b20f886 100644 --- a/docs/en/sql-reference/functions/string-functions.md +++ b/docs/en/sql-reference/functions/string-functions.md @@ -330,7 +330,7 @@ repeat(s, n) **Arguments** - `s` — The string to repeat. [String](../../sql-reference/data-types/string.md). -- `n` — The number of times to repeat the string. [UInt](../../sql-reference/data-types/int-uint.md). +- `n` — The number of times to repeat the string. [UInt or Int](../../sql-reference/data-types/int-uint.md). **Returned value** diff --git a/src/Functions/repeat.cpp b/src/Functions/repeat.cpp index dcd05f373fc..0c323c39969 100644 --- a/src/Functions/repeat.cpp +++ b/src/Functions/repeat.cpp @@ -39,13 +39,15 @@ struct RepeatImpl size, max_string_size); } + template static void vectorStrConstRepeat( const ColumnString::Chars & data, const ColumnString::Offsets & offsets, ColumnString::Chars & res_data, ColumnString::Offsets & res_offsets, - UInt64 repeat_time) + T repeat_time) { + repeat_time = repeat_time < 0 ? 0 : repeat_time; checkRepeatTime(repeat_time); UInt64 data_size = 0; @@ -77,7 +79,8 @@ struct RepeatImpl res_offsets.assign(offsets); for (UInt64 i = 0; i < col_num.size(); ++i) { - size_t repeated_size = (offsets[i] - offsets[i - 1] - 1) * col_num[i] + 1; + T repeat_time = col_num[i] < 0 ? 0 : col_num[i]; + size_t repeated_size = (offsets[i] - offsets[i - 1] - 1) * repeat_time + 1; checkStringSize(repeated_size); data_size += repeated_size; res_offsets[i] = data_size; @@ -86,7 +89,7 @@ struct RepeatImpl for (UInt64 i = 0; i < col_num.size(); ++i) { - T repeat_time = col_num[i]; + T repeat_time = col_num[i] < 0 ? 0 : col_num[i]; checkRepeatTime(repeat_time); process(data.data() + offsets[i - 1], res_data.data() + res_offsets[i - 1], offsets[i] - offsets[i - 1], repeat_time); } @@ -105,7 +108,8 @@ struct RepeatImpl UInt64 col_size = col_num.size(); for (UInt64 i = 0; i < col_size; ++i) { - size_t repeated_size = str_size * col_num[i] + 1; + T repeat_time = col_num[i] < 0 ? 0 : col_num[i]; + size_t repeated_size = str_size * repeat_time + 1; checkStringSize(repeated_size); data_size += repeated_size; res_offsets[i] = data_size; @@ -113,7 +117,7 @@ struct RepeatImpl res_data.resize(data_size); for (UInt64 i = 0; i < col_size; ++i) { - T repeat_time = col_num[i]; + T repeat_time = col_num[i] < 0 ? 0 : col_num[i]; checkRepeatTime(repeat_time); process( reinterpret_cast(const_cast(copy_str.data())), @@ -168,7 +172,8 @@ class FunctionRepeat : public IFunction template static bool castType(const IDataType * type, F && f) { - return castTypeToEither(type, std::forward(f)); + return castTypeToEither(type, std::forward(f)); } public: @@ -186,7 +191,7 @@ public: if (!isString(arguments[0])) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}", arguments[0]->getName(), getName()); - if (!isUnsignedInteger(arguments[1])) + if (!isInteger(arguments[1])) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}", arguments[1]->getName(), getName()); return arguments[0]; @@ -204,9 +209,15 @@ public: { if (const ColumnConst * scale_column_num = checkAndGetColumn(numcolumn.get())) { - UInt64 repeat_time = scale_column_num->getValue(); auto col_res = ColumnString::create(); - RepeatImpl::vectorStrConstRepeat(col->getChars(), col->getOffsets(), col_res->getChars(), col_res->getOffsets(), repeat_time); + castType(arguments[1].type.get(), [&](const auto & type) + { + using DataType = std::decay_t; + using T = typename DataType::FieldType; + T repeat_time = scale_column_num->getValue(); + RepeatImpl::vectorStrConstRepeat(col->getChars(), col->getOffsets(), col_res->getChars(), col_res->getOffsets(), repeat_time); + return true; + }); return col_res; } else if (castType(arguments[1].type.get(), [&](const auto & type) diff --git a/tests/queries/0_stateless/01013_repeat_function.reference b/tests/queries/0_stateless/01013_repeat_function.reference index 46bb248a99a..ea0dadd524f 100644 --- a/tests/queries/0_stateless/01013_repeat_function.reference +++ b/tests/queries/0_stateless/01013_repeat_function.reference @@ -1,7 +1,7 @@ abcabcabcabcabcabcabcabcabcabc abcabcabc -sdfggsdfgg -xywq + + abcabcabcabcabcabcabcabcabcabcabcabc sdfggsdfggsdfggsdfggsdfggsdfggsdfggsdfggsdfggsdfgg @@ -20,8 +20,8 @@ sdfggsdfggsdfggsdfggsdfggsdfggsdfggsdfggsdfggsdfgg xywqxywqxywqxywqxywqxywqxywqxywqxywqxywq plkfplkfplkfplkfplkfplkfplkfplkfplkfplkf abcabcabc -abcabc -abc + + abcabcabcabcabcabcabcabcabcabcabcabc abcabcabcabcabcabcabcabcabcabc diff --git a/tests/queries/0_stateless/01013_repeat_function.sql b/tests/queries/0_stateless/01013_repeat_function.sql index 85b0c16b4ab..b29cc032f28 100644 --- a/tests/queries/0_stateless/01013_repeat_function.sql +++ b/tests/queries/0_stateless/01013_repeat_function.sql @@ -3,20 +3,20 @@ DROP TABLE IF EXISTS defaults; CREATE TABLE defaults ( strings String, - u8 UInt8, + i8 Int8, u16 UInt16, u32 UInt32, u64 UInt64 )ENGINE = Memory(); -INSERT INTO defaults values ('abc', 3, 12, 4, 56) ('sdfgg', 2, 10, 21, 200) ('xywq', 1, 4, 9, 5) ('plkf', 0, 5, 7,77); +INSERT INTO defaults values ('abc', 3, 12, 4, 56) ('sdfgg', -2, 10, 21, 200) ('xywq', -1, 4, 9, 5) ('plkf', 0, 5, 7,77); -SELECT repeat(strings, u8) FROM defaults; +SELECT repeat(strings, i8) FROM defaults; SELECT repeat(strings, u16) FROM defaults; SELECT repeat(strings, u32) from defaults; SELECT repeat(strings, u64) FROM defaults; SELECT repeat(strings, 10) FROM defaults; -SELECT repeat('abc', u8) FROM defaults; +SELECT repeat('abc', i8) FROM defaults; SELECT repeat('abc', u16) FROM defaults; SELECT repeat('abc', u32) FROM defaults; SELECT repeat('abc', u64) FROM defaults; From eab29f4a4e38c15a6b7845208c02fb3dcb5e3210 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Thu, 2 Mar 2023 13:09:18 +0100 Subject: [PATCH 184/342] Fixed tests --- src/Functions/FunctionsExternalDictionaries.h | 21 ++++++++------- .../00313_const_totals_extremes.reference | 4 +-- .../00313_const_totals_extremes.sh | 2 +- ...62_pm_all_join_with_block_continuation.sql | 26 +++++++++---------- .../0_stateless/01508_explain_header.sql | 2 ++ 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/Functions/FunctionsExternalDictionaries.h b/src/Functions/FunctionsExternalDictionaries.h index 570147e4c86..d3be5b1200b 100644 --- a/src/Functions/FunctionsExternalDictionaries.h +++ b/src/Functions/FunctionsExternalDictionaries.h @@ -675,28 +675,29 @@ private: return impl.isInjective(sample_columns); } - DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override + DataTypePtr getReturnTypeImpl(const DataTypes &) const override { - DataTypePtr result_type; + DataTypePtr result; if constexpr (IsDataTypeDecimal) - result_type = std::make_shared(DataType::maxPrecision(), 0); + result = std::make_shared(DataType::maxPrecision(), 0); else - result_type = std::make_shared(); + result = std::make_shared(); + return result; + } + + ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override + { auto return_type = impl.getReturnTypeImpl(arguments); + if (!return_type->equals(*result_type)) throw Exception(ErrorCodes::TYPE_MISMATCH, "Function {} dictionary attribute has different type {} expected {}", getName(), return_type->getName(), result_type->getName()); - return result_type; - } - - ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override - { - return impl.executeImpl(arguments, result_type, input_rows_count); + return impl.executeImpl(arguments, return_type, input_rows_count); } const FunctionDictGetNoType impl; diff --git a/tests/queries/0_stateless/00313_const_totals_extremes.reference b/tests/queries/0_stateless/00313_const_totals_extremes.reference index fd10e1f4387..b5abbfa3c4e 100644 --- a/tests/queries/0_stateless/00313_const_totals_extremes.reference +++ b/tests/queries/0_stateless/00313_const_totals_extremes.reference @@ -86,7 +86,7 @@ [ { "name": "k", - "type": "DateTime" + "type": "DateTime('UTC')" }, { "name": "count()", @@ -104,7 +104,7 @@ "totals": { - "k": "1970-01-01 01:00:00", + "k": "1970-01-01 00:00:00", "count()": "1" }, diff --git a/tests/queries/0_stateless/00313_const_totals_extremes.sh b/tests/queries/0_stateless/00313_const_totals_extremes.sh index 5e880fe7aa0..539a19817e2 100755 --- a/tests/queries/0_stateless/00313_const_totals_extremes.sh +++ b/tests/queries/0_stateless/00313_const_totals_extremes.sh @@ -9,5 +9,5 @@ ${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statist ${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toFloat32(1.23) AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSONCompact"; ${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDate('2010-01-01') AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1"; -${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDateTime('2010-01-01 01:02:03') AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSON"; +${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT toDateTime('2010-01-01 01:02:03', 'UTC') AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSON"; ${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&extremes=1&output_format_write_statistics=0" -d "SELECT 1.1 AS k, count() GROUP BY k WITH TOTALS SETTINGS allow_experimental_analyzer = 1 FORMAT JSONCompact"; diff --git a/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql b/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql index 0732db73e38..15a28e92386 100644 --- a/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql +++ b/tests/queries/0_stateless/01062_pm_all_join_with_block_continuation.sql @@ -2,32 +2,32 @@ SET max_memory_usage = 50000000; SET join_algorithm = 'partial_merge'; SELECT 'defaults'; - + SELECT count(1) FROM ( - SELECT k, n FROM (SELECT materialize(1) as k FROM numbers(10)) nums + SELECT materialize(1) as k, n FROM numbers(10) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); SELECT count(1) FROM ( - SELECT k, n FROM (SELECT materialize(1) as k FROM numbers(1000)) nums + SELECT materialize(1) as k, n FROM numbers(1000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000000)) nums + SELECT materialize(1) as k, n FROM numbers(1000000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10)) j USING k); -- errors SET max_joined_block_size_rows = 0; - + SELECT count(1) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(10)) nums + SELECT materialize(1) as k, n FROM numbers(10) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); -- { serverError 241 } SELECT count(1) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000)) nums + SELECT materialize(1) as k, n FROM numbers(1000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); -- { serverError 241 } @@ -35,17 +35,17 @@ SELECT 'max_joined_block_size_rows = 2000'; SET max_joined_block_size_rows = 2000; SELECT count(1) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(10)) nums + SELECT materialize(1) as k, n FROM numbers(10) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000)) nums + SELECT materialize(1) as k, n FROM numbers(1000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT k, n FROM (SELECT materialize(1) as k FROM numbers(1000000)) nums + SELECT materialize(1) as k, n FROM numbers(1000000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10)) j USING k); @@ -53,16 +53,16 @@ SELECT 'max_rows_in_join = 1000'; SET max_rows_in_join = 1000; SELECT count(1) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(10)) nums + SELECT materialize(1) as k, n FROM numbers(10) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(1000000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000)) nums + SELECT materialize(1) as k, n FROM numbers(1000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10000)) j USING k); SELECT count(1), uniqExact(n) FROM ( - SELECT k, n FROM (SELECT materialize(1) AS k FROM numbers(1000000)) nums + SELECT materialize(1) as k, n FROM numbers(1000000) nums JOIN (SELECT materialize(1) AS k, number n FROM numbers(10)) j USING k); diff --git a/tests/queries/0_stateless/01508_explain_header.sql b/tests/queries/0_stateless/01508_explain_header.sql index fb274c84d8a..a9f876068aa 100644 --- a/tests/queries/0_stateless/01508_explain_header.sql +++ b/tests/queries/0_stateless/01508_explain_header.sql @@ -1 +1,3 @@ +SET allow_experimental_analyzer = 1; + explain header = 1 select 1 as x; From f9e80b4d416e385e0135166f9a86bc756999b90a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 16:36:35 +0400 Subject: [PATCH 185/342] Typo in the `/docs/ru/sql-reference/statements/select/group-by.md` --- docs/ru/sql-reference/statements/select/group-by.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ru/sql-reference/statements/select/group-by.md b/docs/ru/sql-reference/statements/select/group-by.md index 450543cb7a2..ea4f357d33c 100644 --- a/docs/ru/sql-reference/statements/select/group-by.md +++ b/docs/ru/sql-reference/statements/select/group-by.md @@ -108,7 +108,7 @@ SELECT year, month, day, count(*) FROM t GROUP BY year, month, day WITH ROLLUP; ## Модификатор WITH CUBE {#with-cube-modifier} -Модификатор `WITH CUBE` применятеся для расчета подытогов по всем комбинациям группировки ключевых выражений в списке `GROUP BY`. +Модификатор `WITH CUBE` применяется для расчета подытогов по всем комбинациям группировки ключевых выражений в списке `GROUP BY`. Строки с подытогами добавляются в конец результирующей таблицы. В колонках, по которым выполняется группировка, указывается значение `0` или пустая строка. From d55b800fc6b40699b5608c46de4658879d32d374 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Thu, 2 Mar 2023 13:43:03 +0100 Subject: [PATCH 186/342] Fixed code review issues --- src/Analyzer/ColumnTransformers.cpp | 2 +- src/Analyzer/Passes/QueryAnalysisPass.cpp | 10 +++++++-- src/Planner/Planner.cpp | 1 + src/Planner/PlannerJoinTree.cpp | 26 +++++++++++------------ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Analyzer/ColumnTransformers.cpp b/src/Analyzer/ColumnTransformers.cpp index cc037fb2a99..ee336a0e7f3 100644 --- a/src/Analyzer/ColumnTransformers.cpp +++ b/src/Analyzer/ColumnTransformers.cpp @@ -348,7 +348,7 @@ ASTPtr ReplaceColumnTransformerNode::toASTImpl() const auto replacement_ast = std::make_shared(); replacement_ast->name = replacements_names[i]; replacement_ast->children.push_back(replacement_expressions_nodes[i]->toAST()); - ast_replace_transformer->children.push_back(replacement_ast); + ast_replace_transformer->children.push_back(std::move(replacement_ast)); } return ast_replace_transformer; diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 8a69eb348d6..f8645f2b756 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -4045,6 +4045,12 @@ ProjectionNames QueryAnalyzer::resolveMatcher(QueryTreeNodePtr & matcher_node, I node = replace_expression->clone(); node_projection_names = resolveExpressionNode(node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/); + + /** If replace expression resolved as single node, we want to use replace column name as result projection name, instead + * of using replace expression projection name. + * + * Example: SELECT * REPLACE id + 5 AS id FROM test_table; + */ if (node_projection_names.size() == 1) node_projection_names[0] = column_name; @@ -5026,14 +5032,14 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi auto function_base = function->build(argument_columns); /// Do not constant fold get scalar functions - bool is_get_scalar_function = function_name == "__getScalar" || function_name == "shardNum" || + bool disable_constant_folding = function_name == "__getScalar" || function_name == "shardNum" || function_name == "shardCount"; /** If function is suitable for constant folding try to convert it to constant. * Example: SELECT plus(1, 1); * Result: SELECT 2; */ - if (function_base->isSuitableForConstantFolding() && !is_get_scalar_function) + if (function_base->isSuitableForConstantFolding() && !disable_constant_folding) { auto result_type = function_base->getResultType(); auto executable_function = function_base->prepare(argument_columns); diff --git a/src/Planner/Planner.cpp b/src/Planner/Planner.cpp index a65ee60edfa..2ce470d9ecf 100644 --- a/src/Planner/Planner.cpp +++ b/src/Planner/Planner.cpp @@ -594,6 +594,7 @@ void addWithFillStepIfNeeded(QueryPlan & query_plan, auto query_plan_columns = query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(); for (auto & query_plan_column : query_plan_columns) { + /// INTERPOLATE actions dag input columns must be non constant query_plan_column.column = nullptr; interpolate_actions_dag->addInput(query_plan_column); } diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 4d6181e3686..d6235208063 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -336,7 +336,19 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } else if (query_node || union_node) { - if (!select_query_options.only_analyze) + if (select_query_options.only_analyze) + { + auto projection_columns = query_node ? query_node->getProjectionColumns() : union_node->computeProjectionColumns(); + Block source_header; + for (auto & projection_column : projection_columns) + source_header.insert(ColumnWithTypeAndName(projection_column.type, projection_column.name)); + + Pipe pipe(std::make_shared(source_header)); + auto read_from_pipe = std::make_unique(std::move(pipe)); + read_from_pipe->setStepDescription("Read from NullSource"); + query_plan.addStep(std::move(read_from_pipe)); + } + else { if (table_expression_data.getColumnNames().empty()) { @@ -355,18 +367,6 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl subquery_planner.buildQueryPlanIfNeeded(); query_plan = std::move(subquery_planner).extractQueryPlan(); } - else - { - auto projection_columns = query_node ? query_node->getProjectionColumns() : union_node->computeProjectionColumns(); - Block source_header; - for (auto & projection_column : projection_columns) - source_header.insert(ColumnWithTypeAndName(projection_column.type, projection_column.name)); - - Pipe pipe(std::make_shared(source_header)); - auto read_from_pipe = std::make_unique(std::move(pipe)); - read_from_pipe->setStepDescription("Read from NullSource"); - query_plan.addStep(std::move(read_from_pipe)); - } } else { From 6aee72391847c86ca6007443828b6b4000daf645 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 16:50:28 +0400 Subject: [PATCH 187/342] Typos in the page `/docs/ru/sql-reference/statements/select/join.md` --- docs/ru/sql-reference/statements/select/join.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/ru/sql-reference/statements/select/join.md b/docs/ru/sql-reference/statements/select/join.md index 6853a8c8c90..6be438f8c43 100644 --- a/docs/ru/sql-reference/statements/select/join.md +++ b/docs/ru/sql-reference/statements/select/join.md @@ -16,7 +16,7 @@ FROM (ON )|(USING ) ... ``` -Выражения из секции `ON` и столбцы из секции `USING` называется «ключами соединения». Если не указано иное, при присоединение создаётся [Декартово произведение](https://en.wikipedia.org/wiki/Cartesian_product) из строк с совпадающими значениями ключей соединения, что может привести к получению результатов с гораздо большим количеством строк, чем исходные таблицы. +Выражения из секции `ON` и столбцы из секции `USING` называются «ключами соединения». Если не указано иное, при присоединение создаётся [Декартово произведение](https://en.wikipedia.org/wiki/Cartesian_product) из строк с совпадающими значениями ключей соединения, что может привести к получению результатов с гораздо большим количеством строк, чем исходные таблицы. ## Поддерживаемые типы соединения {#select-join-types} @@ -28,7 +28,7 @@ FROM - `FULL OUTER JOIN`, не совпадающие строки из обеих таблиц возвращаются в дополнение к совпадающим строкам. - `CROSS JOIN`, производит декартово произведение таблиц целиком, ключи соединения не указываются. -Без указания типа `JOIN` подразумевается `INNER`. Ключевое слово `OUTER` можно опускать. Альтернативным синтаксисом для `CROSS JOIN` является ли указание нескольких таблиц, разделённых запятыми, в [секции FROM](from.md). +Без указания типа `JOIN` подразумевается `INNER`. Ключевое слово `OUTER` можно опускать. Альтернативным синтаксисом для `CROSS JOIN` является указание нескольких таблиц, разделённых запятыми, в [секции FROM](from.md). Дополнительные типы соединений, доступные в ClickHouse: @@ -62,7 +62,7 @@ FROM Строки объединяются только тогда, когда всё составное условие выполнено. Если оно не выполнено, то строки могут попасть в результат в зависимости от типа `JOIN`. Обратите внимание, что если то же самое условие поместить в секцию `WHERE`, то строки, для которых оно не выполняется, никогда не попаду в результат. -Оператор `OR` внутри секции `ON` работает, используя алгоритм хеш-соединения — на каждый агрумент `OR` с ключами соединений для `JOIN` создается отдельная хеш-таблица, поэтому потребление памяти и время выполнения запроса растет линейно при увеличении количества выражений `OR` секции `ON`. +Оператор `OR` внутри секции `ON` работает, используя алгоритм хеш-соединения — на каждый аргумент `OR` с ключами соединений для `JOIN` создается отдельная хеш-таблица, поэтому потребление памяти и время выполнения запроса растет линейно при увеличении количества выражений `OR` секции `ON`. :::note "Примечание" Если в условии использованы столбцы из разных таблиц, то пока поддерживается только оператор равенства (`=`). @@ -280,7 +280,7 @@ SELECT a, b, toTypeName(a), toTypeName(b) FROM t_1 FULL JOIN t_2 USING (a, b); Каждый раз для выполнения запроса с одинаковым `JOIN`, подзапрос выполняется заново — результат не кэшируется. Это можно избежать, используя специальный движок таблиц [Join](../../../engines/table-engines/special/join.md), представляющий собой подготовленное множество для соединения, которое всегда находится в оперативке. -В некоторых случаях это более эффективно использовать [IN](../../operators/in.md) вместо `JOIN`. +В некоторых случаях более эффективно использовать [IN](../../operators/in.md) вместо `JOIN`. Если `JOIN` необходим для соединения с таблицами измерений (dimension tables - сравнительно небольшие таблицы, которые содержат свойства измерений - например, имена для рекламных кампаний), то использование `JOIN` может быть не очень удобным из-за громоздкости синтаксиса, а также из-за того, что правая таблица читается заново при каждом запросе. Специально для таких случаев существует функциональность «Внешние словари», которую следует использовать вместо `JOIN`. Дополнительные сведения смотрите в разделе «Внешние словари». From bafd9773bc310b65f4a1f94201dcb72b193fb70e Mon Sep 17 00:00:00 2001 From: alex filatov <42028484+phil-88@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:53:09 +0300 Subject: [PATCH 188/342] fix Unknown library method 'extDict_libClone' We have an issue when using external dictionary. Occasionally library bridge called with extDict_libClone and fails with Unknown library method 'extDict_libClone'. And it looks like it is because of at some point `else if (method == "extDict_libNew")` was changed to if (lib_new) with no handling for extDict_libClone inside this new if else statement and reporing an error that extDict_libClone is an unknown method. So there is a two-line fix to handle extDict_libClone properly. Error logs that we have: ``` 2022.12.16 14:17:44.285088 [ 393573 ] {} ExternalDictionaries: Could not update cache dictionary 'dict.vhash_s', next update is scheduled at 2022-12-16 14:18:00: Code: 86. DB::Exception: Received error from remote server /extdict_request?version=1&dictionary_id=be2b2cd1-ba57-4658-8d1b-35ef40ab005b&method=extDict_libClone&from_dictionary_id=c3537142-eaa9-4deb-9b65-47eb8ea1dee6. HTTP status code: 500 Internal Server Error, body: Unknown library method 'extDict_libClone' 2022.12.16 14:17:44.387049 [ 399133 ] {} ExternalDictionaries: Could not update cache dictionary 'dict.vhash_s', next update is scheduled at 2022-12-16 14:17:51: Code: 86. DB::Exception: Received error from remote server /extdict_request?version=1&dictionary_id=0df866ac-6c94-4974-a76c-3940522091b9&method=extDict_libClone&from_dictionary_id=c3537142-eaa9-4deb-9b65-47eb8ea1dee6. HTTP status code: 500 Internal Server Error, body: Unknown library method 'extDict_libClone' 2022.12.16 14:17:44.488468 [ 397769 ] {} ExternalDictionaries: Could not update cache dictionary 'dict.vhash_s', next update is scheduled at 2022-12-16 14:19:38: Code: 86. DB::Exception: Received error from remote server /extdict_request?version=1&dictionary_id=2d8af321-b669-4526-982b-42c0fabf0e8d&method=extDict_libClone&from_dictionary_id=c3537142-eaa9-4deb-9b65-47eb8ea1dee6. HTTP status code: 500 Internal Server Error, body: Unknown library method 'extDict_libClone' 2022.12.16 14:17:44.489935 [ 398226 ] {datamarts_v_dwh_node0032-241534:0x552da2_1_11} executeQuery: Code: 510. DB::Exception: Update failed for dictionary 'dict.vhash_s': Code: 510. DB::Exception: Update failed for dictionary dict.vhash_s : Code: 86. DB::Exception: Received error from remote server /extdict_request?version=1&dictionary_id=be2b2cd1-ba57-4658-8d1b-35ef40ab005b&method=extDict_libClone&from_dictionary_id=c3537142-eaa9-4deb-9b65-47eb8ea1dee6. HTTP status code: 500 Internal Server Error, body: Unknown library method 'extDict_libClone' ``` --- programs/library-bridge/LibraryBridgeHandlers.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/programs/library-bridge/LibraryBridgeHandlers.cpp b/programs/library-bridge/LibraryBridgeHandlers.cpp index ab81472be88..9642dd7ee63 100644 --- a/programs/library-bridge/LibraryBridgeHandlers.cpp +++ b/programs/library-bridge/LibraryBridgeHandlers.cpp @@ -158,6 +158,8 @@ void ExternalDictionaryLibraryBridgeRequestHandler::handleRequest(HTTPServerRequ if (cloned) { writeStringBinary("1", out); + out.finalize(); + return; } else { From 4a746f32a54a9f7b10bb029532c8b2369db9a5b0 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 17:12:15 +0400 Subject: [PATCH 189/342] Typos in the page `/docs/ru/sql-reference/statements/select/order-by.md` --- docs/ru/sql-reference/statements/select/order-by.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/ru/sql-reference/statements/select/order-by.md b/docs/ru/sql-reference/statements/select/order-by.md index 40efb862250..a3390501f2c 100644 --- a/docs/ru/sql-reference/statements/select/order-by.md +++ b/docs/ru/sql-reference/statements/select/order-by.md @@ -67,7 +67,7 @@ sidebar_label: ORDER BY ## Примеры с использованием сравнения {#collation-examples} -Пример с значениями типа [String](../../../sql-reference/data-types/string.md): +Пример со значениями типа [String](../../../sql-reference/data-types/string.md): Входная таблица: @@ -241,13 +241,13 @@ SELECT * FROM collate_test ORDER BY s ASC COLLATE 'en'; └───┴─────────┘ ``` -## Деталь реализации {#implementation-details} +## Детали реализации {#implementation-details} Если кроме `ORDER BY` указан также не слишком большой [LIMIT](limit.md), то расходуется меньше оперативки. Иначе расходуется количество памяти, пропорциональное количеству данных для сортировки. При распределённой обработке запроса, если отсутствует [GROUP BY](group-by.md), сортировка частично делается на удалённых серверах, а на сервере-инициаторе запроса производится слияние результатов. Таким образом, при распределённой сортировке, может сортироваться объём данных, превышающий размер памяти на одном сервере. Существует возможность выполнять сортировку во внешней памяти (с созданием временных файлов на диске), если оперативной памяти не хватает. Для этого предназначена настройка `max_bytes_before_external_sort`. Если она выставлена в 0 (по умолчанию), то внешняя сортировка выключена. Если она включена, то при достижении объёмом данных для сортировки указанного количества байт, накопленные данные будут отсортированы и сброшены во временный файл. После того, как все данные будут прочитаны, будет произведено слияние всех сортированных файлов и выдача результата. Файлы записываются в директорию `/var/lib/clickhouse/tmp/` (по умолчанию, может быть изменено с помощью параметра `tmp_path`) в конфиге. -На выполнение запроса может расходоваться больше памяти, чем `max_bytes_before_external_sort`. Поэтому, значение этой настройки должно быть существенно меньше, чем `max_memory_usage`. Для примера, если на вашем сервере 128 GB оперативки, и вам нужно выполнить один запрос, то выставите `max_memory_usage` в 100 GB, а `max_bytes_before_external_sort` в 80 GB. +На выполнение запроса может расходоваться больше памяти, чем `max_bytes_before_external_sort`. Поэтому значение этой настройки должно быть существенно меньше, чем `max_memory_usage`. Для примера, если на вашем сервере 128 GB оперативки, и вам нужно выполнить один запрос, то выставьте `max_memory_usage` в 100 GB, а `max_bytes_before_external_sort` в 80 GB. Внешняя сортировка работает существенно менее эффективно, чем сортировка в оперативке. @@ -366,9 +366,9 @@ ORDER BY └────────────┴────────────┴──────────┘ ``` -Поле `d1` не заполняется и использует значение по умолчанию. Поскольку у нас нет повторяющихся значений для `d2`, мы не можем правильно рассчитать последователность заполнения для `d1`. +Поле `d1` не заполняется и использует значение по умолчанию. Поскольку у нас нет повторяющихся значений для `d2`, мы не можем правильно рассчитать последовательность заполнения для `d1`. -Cледующий запрос (с измененым порядком в ORDER BY): +Cледующий запрос (с измененным порядком в ORDER BY): ```sql SELECT toDate((number * 10) * 86400) AS d1, From f57bb3d71ebe16ad542bc5e5ea6b6722cd873690 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 17:14:52 +0400 Subject: [PATCH 190/342] Typo in the page `/docs/ru/sql-reference/statements/select/prewhere.md` --- docs/ru/sql-reference/statements/select/prewhere.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ru/sql-reference/statements/select/prewhere.md b/docs/ru/sql-reference/statements/select/prewhere.md index e37aec814ec..092370d4b3a 100644 --- a/docs/ru/sql-reference/statements/select/prewhere.md +++ b/docs/ru/sql-reference/statements/select/prewhere.md @@ -13,7 +13,7 @@ Prewhere — это оптимизация для более эффективн `PREWHERE` имеет смысл использовать, если есть условия фильтрации, которые использует меньшинство столбцов из тех, что есть в запросе, но достаточно сильно фильтрует данные. Таким образом, сокращается количество читаемых данных. -В запросе может быть одновременно указаны и `PREWHERE`, и `WHERE`. В этом случае `PREWHERE` предшествует `WHERE`. +В запросе могут быть одновременно указаны и `PREWHERE`, и `WHERE`. В этом случае `PREWHERE` предшествует `WHERE`. Если значение параметра [optimize_move_to_prewhere](../../../operations/settings/settings.md#optimize_move_to_prewhere) равно 0, эвристика по автоматическому перемещению части выражений из `WHERE` к `PREWHERE` отключается. From 00d28893e8e7f1020f2fb08935db014b7f52da82 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 17:20:25 +0400 Subject: [PATCH 191/342] Typos in the page `/docs/ru/sql-reference/statements/select/sample.md` --- docs/ru/sql-reference/statements/select/sample.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ru/sql-reference/statements/select/sample.md b/docs/ru/sql-reference/statements/select/sample.md index e63479d6c27..decef52d06f 100644 --- a/docs/ru/sql-reference/statements/select/sample.md +++ b/docs/ru/sql-reference/statements/select/sample.md @@ -10,7 +10,7 @@ sidebar_label: SAMPLE Сэмплирование имеет смысл, когда: 1. Точность результата не важна, например, для оценочных расчетов. -2. Возможности аппаратной части не позволяют соответствовать строгим критериям. Например, время ответа должно быть \<100 мс. При этом точность расчета имеет более низкий приоритет. +2. Возможности аппаратной части не позволяют соответствовать строгим критериям. Например, время ответа должно быть <100 мс. При этом точность расчета имеет более низкий приоритет. 3. Точность результата участвует в бизнес-модели сервиса. Например, пользователи с бесплатной подпиской на сервис могут получать отчеты с меньшей точностью, чем пользователи с премиум подпиской. :::note "Внимание" From 39aff35ac3f77c8f99b279e5224911dbb9d6f861 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 17:23:02 +0400 Subject: [PATCH 192/342] Typos in the page `/docs/ru/sql-reference/statements/select/union.md` --- docs/ru/sql-reference/statements/select/union.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ru/sql-reference/statements/select/union.md b/docs/ru/sql-reference/statements/select/union.md index 4dad63c2c7c..05d20100452 100644 --- a/docs/ru/sql-reference/statements/select/union.md +++ b/docs/ru/sql-reference/statements/select/union.md @@ -26,7 +26,7 @@ SELECT CounterID, 2 AS table, sum(Sign) AS c Результирующие столбцы сопоставляются по их индексу (порядку внутри `SELECT`). Если имена столбцов не совпадают, то имена для конечного результата берутся из первого запроса. -При объединении выполняет приведение типов. Например, если два запроса имеют одно и то же поле с не-`Nullable` и `Nullable` совместимыми типами, полученные в результате `UNION` данные будут иметь `Nullable` тип. +При объединении выполняется приведение типов. Например, если два запроса имеют одно и то же поле с не-`Nullable` и `Nullable` совместимыми типами, полученные в результате `UNION` данные будут иметь `Nullable` тип. Запросы, которые являются частью `UNION`, могут быть заключены в круглые скобки. [ORDER BY](order-by.md) и [LIMIT](limit.md) применяются к отдельным запросам, а не к конечному результату. Если вам нужно применить преобразование к конечному результату, вы можете разместить все объединенные с помощью `UNION` запросы в подзапрос в секции [FROM](from.md). From d42a95a6c5ada969e69030c07e9ae1ee01b17e42 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 17:25:49 +0400 Subject: [PATCH 193/342] Type in the page `/docs/ru/sql-reference/statements/select/with.md` --- docs/ru/sql-reference/statements/select/with.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ru/sql-reference/statements/select/with.md b/docs/ru/sql-reference/statements/select/with.md index 58f380b0a85..c4121a6569a 100644 --- a/docs/ru/sql-reference/statements/select/with.md +++ b/docs/ru/sql-reference/statements/select/with.md @@ -5,7 +5,7 @@ sidebar_label: WITH # Секция WITH {#with-clause} -Clickhouse поддерживает [Общие табличные выражения](https://ru.wikipedia.org/wiki/Иерархические_и_рекурсивные_запросы_в_SQL), то есть позволяет использовать результаты выражений из секции `WITH` в остальной части `SELECT` запроса. Именованные подзапросы могут быть включены в текущий и дочерний контекст запроса в тех местах, где разрешены табличные объекты. Рекурсия предотвращается путем скрытия общего табличного выражения текущего уровня из выражения `WITH`. +ClickHouse поддерживает [Общие табличные выражения](https://ru.wikipedia.org/wiki/Иерархические_и_рекурсивные_запросы_в_SQL), то есть позволяет использовать результаты выражений из секции `WITH` в остальной части `SELECT` запроса. Именованные подзапросы могут быть включены в текущий и дочерний контекст запроса в тех местах, где разрешены табличные объекты. Рекурсия предотвращается путем скрытия общего табличного выражения текущего уровня из выражения `WITH`. ## Синтаксис From 740aeaba1fc18ad8667a0bbf7ffefca12b5fc9e4 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 2 Mar 2023 13:36:47 +0000 Subject: [PATCH 194/342] Apply some CTAD --- src/Common/CancelToken.cpp | 8 +++---- src/Common/ConcurrentBoundedQueue.h | 14 ++++++------ src/Common/DateLUT.cpp | 2 +- src/Common/StatusInfo.h | 4 ++-- src/Common/TLDListsHolder.cpp | 4 ++-- src/Common/mysqlxx/Pool.cpp | 6 ++--- src/Common/mysqlxx/PoolFactory.cpp | 4 ++-- src/Common/mysqlxx/PoolWithFailover.cpp | 2 +- src/Coordination/InMemoryLogStore.cpp | 22 +++++++++---------- src/Coordination/KeeperStateMachine.cpp | 2 +- src/Coordination/SummingStateMachine.cpp | 10 ++++----- src/Databases/MySQL/DatabaseMySQL.cpp | 18 +++++++-------- .../PostgreSQL/DatabasePostgreSQL.cpp | 18 +++++++-------- src/Databases/SQLite/DatabaseSQLite.cpp | 10 ++++----- src/Interpreters/Context.cpp | 2 +- .../ExternalLoaderXMLConfigRepository.cpp | 4 ++-- src/Interpreters/GraceHashJoin.cpp | 2 +- src/Interpreters/JIT/CHJIT.cpp | 6 ++--- src/Loggers/OwnSplitChannel.cpp | 4 ++-- .../Impl/ParallelFormattingOutputFormat.cpp | 8 +++---- .../Formats/Impl/ParallelParsingInputFormat.h | 2 +- .../CreateSetAndFilterOnTheFlyStep.cpp | 2 +- src/Processors/Sources/ShellCommandSource.cpp | 4 ++-- src/Server/PrometheusMetricsWriter.cpp | 2 +- .../FileLog/FileLogDirectoryWatcher.cpp | 16 +++++++------- src/Storages/FileLog/StorageFileLog.cpp | 4 ++-- src/Storages/Hive/StorageHive.cpp | 4 ++-- .../MergeTree/ReplicatedMergeTreeQueue.cpp | 6 ++--- src/Storages/StorageDictionary.cpp | 8 +++---- src/Storages/StorageMergeTree.cpp | 4 ++-- 30 files changed, 101 insertions(+), 101 deletions(-) diff --git a/src/Common/CancelToken.cpp b/src/Common/CancelToken.cpp index 53f71fee144..077d60c7efa 100644 --- a/src/Common/CancelToken.cpp +++ b/src/Common/CancelToken.cpp @@ -35,26 +35,26 @@ namespace void CancelToken::Registry::insert(CancelToken * token) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); threads[token->thread_id] = token; } void CancelToken::Registry::remove(CancelToken * token) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); threads.erase(token->thread_id); } void CancelToken::Registry::signal(UInt64 tid) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); if (auto it = threads.find(tid); it != threads.end()) it->second->signalImpl(); } void CancelToken::Registry::signal(UInt64 tid, int code, const String & message) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); if (auto it = threads.find(tid); it != threads.end()) it->second->signalImpl(code, message); } diff --git a/src/Common/ConcurrentBoundedQueue.h b/src/Common/ConcurrentBoundedQueue.h index fd4a2d5790b..d3abf86c817 100644 --- a/src/Common/ConcurrentBoundedQueue.h +++ b/src/Common/ConcurrentBoundedQueue.h @@ -163,14 +163,14 @@ public: /// Returns size of queue size_t size() const { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); return queue.size(); } /// Returns if queue is empty bool empty() const { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); return queue.empty(); } @@ -184,7 +184,7 @@ public: bool was_finished_before = false; { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); if (is_finished) return true; @@ -202,14 +202,14 @@ public: /// Returns if queue is finished bool isFinished() const { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); return is_finished; } /// Returns if queue is finished and empty bool isFinishedAndEmpty() const { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); return is_finished && queue.empty(); } @@ -217,7 +217,7 @@ public: void clear() { { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); if (is_finished) return; @@ -233,7 +233,7 @@ public: void clearAndFinish() { { - std::lock_guard lock(queue_mutex); + std::lock_guard lock(queue_mutex); std::queue empty_queue; queue.swap(empty_queue); diff --git a/src/Common/DateLUT.cpp b/src/Common/DateLUT.cpp index ae6127670e5..a31ece00c0b 100644 --- a/src/Common/DateLUT.cpp +++ b/src/Common/DateLUT.cpp @@ -149,7 +149,7 @@ DateLUT::DateLUT() const DateLUTImpl & DateLUT::getImplementation(const std::string & time_zone) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); auto it = impls.emplace(time_zone, nullptr).first; if (!it->second) diff --git a/src/Common/StatusInfo.h b/src/Common/StatusInfo.h index 99d6a6f23f5..9aa185cd0c3 100644 --- a/src/Common/StatusInfo.h +++ b/src/Common/StatusInfo.h @@ -26,13 +26,13 @@ namespace CurrentStatusInfo inline void set(Status status, Key key, Int8 value) { - std::lock_guard lock(locks[status]); + std::lock_guard lock(locks[status]); values[status][key] = value; } inline void unset(Status status, Key key) { - std::lock_guard lock(locks[status]); + std::lock_guard lock(locks[status]); values[status].erase(key); } } diff --git a/src/Common/TLDListsHolder.cpp b/src/Common/TLDListsHolder.cpp index c493a88010b..623b88f83a5 100644 --- a/src/Common/TLDListsHolder.cpp +++ b/src/Common/TLDListsHolder.cpp @@ -109,14 +109,14 @@ size_t TLDListsHolder::parseAndAddTldList(const std::string & name, const std::s } size_t tld_list_size = tld_list.size(); - std::lock_guard lock(tld_lists_map_mutex); + std::lock_guard lock(tld_lists_map_mutex); tld_lists_map.insert(std::make_pair(name, std::move(tld_list))); return tld_list_size; } const TLDList & TLDListsHolder::getTldList(const std::string & name) { - std::lock_guard lock(tld_lists_map_mutex); + std::lock_guard lock(tld_lists_map_mutex); auto it = tld_lists_map.find(name); if (it == tld_lists_map.end()) throw Exception(ErrorCodes::TLD_LIST_NOT_FOUND, "TLD list {} does not exist", name); diff --git a/src/Common/mysqlxx/Pool.cpp b/src/Common/mysqlxx/Pool.cpp index bee62a0af2e..ba74700d4bc 100644 --- a/src/Common/mysqlxx/Pool.cpp +++ b/src/Common/mysqlxx/Pool.cpp @@ -130,7 +130,7 @@ Pool::Pool(const Poco::Util::AbstractConfiguration & cfg, const std::string & co Pool::~Pool() { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); for (auto & connection : connections) delete static_cast(connection); @@ -187,7 +187,7 @@ Pool::Entry Pool::get(uint64_t wait_timeout) Pool::Entry Pool::tryGet() { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); initialize(); @@ -229,7 +229,7 @@ void Pool::removeConnection(Connection* connection) { logger.trace("(%s): Removing connection.", getDescription()); - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); if (connection) { if (connection->ref_count > 0) diff --git a/src/Common/mysqlxx/PoolFactory.cpp b/src/Common/mysqlxx/PoolFactory.cpp index 1d2f9fc74c7..5fae934a400 100644 --- a/src/Common/mysqlxx/PoolFactory.cpp +++ b/src/Common/mysqlxx/PoolFactory.cpp @@ -76,7 +76,7 @@ PoolWithFailover PoolFactory::get(const Poco::Util::AbstractConfiguration & conf const std::string & config_name, unsigned default_connections, unsigned max_connections, size_t max_tries) { - std::lock_guard lock(impl->mutex); + std::lock_guard lock(impl->mutex); if (auto entry = impl->pools.find(config_name); entry != impl->pools.end()) { return *(entry->second); @@ -106,7 +106,7 @@ PoolWithFailover PoolFactory::get(const Poco::Util::AbstractConfiguration & conf void PoolFactory::reset() { - std::lock_guard lock(impl->mutex); + std::lock_guard lock(impl->mutex); impl->pools.clear(); impl->pools_by_ids.clear(); } diff --git a/src/Common/mysqlxx/PoolWithFailover.cpp b/src/Common/mysqlxx/PoolWithFailover.cpp index 0ed231cdf8d..cd9b9e6ec31 100644 --- a/src/Common/mysqlxx/PoolWithFailover.cpp +++ b/src/Common/mysqlxx/PoolWithFailover.cpp @@ -123,7 +123,7 @@ PoolWithFailover::PoolWithFailover(const PoolWithFailover & other) PoolWithFailover::Entry PoolWithFailover::get() { Poco::Util::Application & app = Poco::Util::Application::instance(); - std::lock_guard locker(mutex); + std::lock_guard locker(mutex); /// If we cannot connect to some replica due to pool overflow, than we will wait and connect. PoolPtr * full_pool = nullptr; diff --git a/src/Coordination/InMemoryLogStore.cpp b/src/Coordination/InMemoryLogStore.cpp index b7bccdf588e..ca240584a54 100644 --- a/src/Coordination/InMemoryLogStore.cpp +++ b/src/Coordination/InMemoryLogStore.cpp @@ -27,7 +27,7 @@ uint64_t InMemoryLogStore::start_index() const uint64_t InMemoryLogStore::next_slot() const { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); // Exclude the dummy entry. return start_idx + logs.size() - 1; } @@ -35,7 +35,7 @@ uint64_t InMemoryLogStore::next_slot() const nuraft::ptr InMemoryLogStore::last_entry() const { uint64_t next_idx = next_slot(); - std::lock_guard lock(logs_lock); + std::lock_guard lock(logs_lock); auto entry = logs.find(next_idx - 1); if (entry == logs.end()) entry = logs.find(0); @@ -47,7 +47,7 @@ uint64_t InMemoryLogStore::append(nuraft::ptr & entry) { ptr clone = makeClone(entry); - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); uint64_t idx = start_idx + logs.size() - 1; logs[idx] = clone; return idx; @@ -58,7 +58,7 @@ void InMemoryLogStore::write_at(uint64_t index, nuraft::ptr & nuraft::ptr clone = makeClone(entry); // Discard all logs equal to or greater than `index. - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); auto itr = logs.lower_bound(index); while (itr != logs.end()) itr = logs.erase(itr); @@ -76,7 +76,7 @@ nuraft::ptr>> InMemoryLogStore::log_e { nuraft::ptr src = nullptr; { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); auto entry = logs.find(i); if (entry == logs.end()) { @@ -94,7 +94,7 @@ nuraft::ptr InMemoryLogStore::entry_at(uint64_t index) { nuraft::ptr src = nullptr; { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); auto entry = logs.find(index); if (entry == logs.end()) entry = logs.find(0); @@ -107,7 +107,7 @@ uint64_t InMemoryLogStore::term_at(uint64_t index) { uint64_t term = 0; { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); auto entry = logs.find(index); if (entry == logs.end()) entry = logs.find(0); @@ -125,7 +125,7 @@ nuraft::ptr InMemoryLogStore::pack(uint64_t index, Int32 cnt) { ptr le = nullptr; { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); le = logs[ii]; } assert(le.get()); @@ -162,13 +162,13 @@ void InMemoryLogStore::apply_pack(uint64_t index, nuraft::buffer & pack) nuraft::ptr le = nuraft::log_entry::deserialize(*buf_local); { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); logs[cur_idx] = le; } } { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); auto entry = logs.upper_bound(0); if (entry != logs.end()) start_idx = entry->first; @@ -179,7 +179,7 @@ void InMemoryLogStore::apply_pack(uint64_t index, nuraft::buffer & pack) bool InMemoryLogStore::compact(uint64_t last_log_index) { - std::lock_guard l(logs_lock); + std::lock_guard l(logs_lock); for (uint64_t ii = start_idx; ii <= last_log_index; ++ii) { auto entry = logs.find(ii); diff --git a/src/Coordination/KeeperStateMachine.cpp b/src/Coordination/KeeperStateMachine.cpp index 68f6cfde01e..ccee058ba5c 100644 --- a/src/Coordination/KeeperStateMachine.cpp +++ b/src/Coordination/KeeperStateMachine.cpp @@ -341,7 +341,7 @@ void KeeperStateMachine::rollbackRequest(const KeeperStorage::RequestForSession nuraft::ptr KeeperStateMachine::last_snapshot() { /// Just return the latest snapshot. - std::lock_guard lock(snapshots_lock); + std::lock_guard lock(snapshots_lock); return latest_snapshot_meta; } diff --git a/src/Coordination/SummingStateMachine.cpp b/src/Coordination/SummingStateMachine.cpp index 4151b727744..4c21f1d8658 100644 --- a/src/Coordination/SummingStateMachine.cpp +++ b/src/Coordination/SummingStateMachine.cpp @@ -37,7 +37,7 @@ nuraft::ptr SummingStateMachine::commit(const uint64_t log_idx, bool SummingStateMachine::apply_snapshot(nuraft::snapshot & s) { - std::lock_guard ll(snapshots_lock); + std::lock_guard ll(snapshots_lock); auto entry = snapshots.find(s.get_last_log_idx()); if (entry == snapshots.end()) return false; @@ -50,7 +50,7 @@ bool SummingStateMachine::apply_snapshot(nuraft::snapshot & s) nuraft::ptr SummingStateMachine::last_snapshot() { // Just return the latest snapshot. - std::lock_guard ll(snapshots_lock); + std::lock_guard ll(snapshots_lock); auto entry = snapshots.rbegin(); if (entry == snapshots.rend()) return nullptr; @@ -100,7 +100,7 @@ void SummingStateMachine::save_logical_snp_obj( nuraft::buffer_serializer bs(data); int64_t local_value = static_cast(bs.get_u64()); - std::lock_guard ll(snapshots_lock); + std::lock_guard ll(snapshots_lock); auto entry = snapshots.find(s.get_last_log_idx()); assert(entry != snapshots.end()); entry->second->value = local_value; @@ -118,7 +118,7 @@ int SummingStateMachine::read_logical_snp_obj( { nuraft::ptr ctx = nullptr; { - std::lock_guard ll(snapshots_lock); + std::lock_guard ll(snapshots_lock); auto entry = snapshots.find(s.get_last_log_idx()); if (entry == snapshots.end()) { @@ -155,7 +155,7 @@ void SummingStateMachine::create_snapshot( nuraft::async_result::handler_type & when_done) { { - std::lock_guard ll(snapshots_lock); + std::lock_guard ll(snapshots_lock); createSnapshotInternal(s); } nuraft::ptr except(nullptr); diff --git a/src/Databases/MySQL/DatabaseMySQL.cpp b/src/Databases/MySQL/DatabaseMySQL.cpp index 85944319999..cfb64ba6959 100644 --- a/src/Databases/MySQL/DatabaseMySQL.cpp +++ b/src/Databases/MySQL/DatabaseMySQL.cpp @@ -82,7 +82,7 @@ DatabaseMySQL::DatabaseMySQL( bool DatabaseMySQL::empty() const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); fetchTablesIntoLocalCache(getContext()); @@ -99,7 +99,7 @@ bool DatabaseMySQL::empty() const DatabaseTablesIteratorPtr DatabaseMySQL::getTablesIterator(ContextPtr local_context, const FilterByNameFunction & filter_by_table_name) const { Tables tables; - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); fetchTablesIntoLocalCache(local_context); @@ -117,7 +117,7 @@ bool DatabaseMySQL::isTableExist(const String & name, ContextPtr local_context) StoragePtr DatabaseMySQL::tryGetTable(const String & mysql_table_name, ContextPtr local_context) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); fetchTablesIntoLocalCache(local_context); @@ -129,7 +129,7 @@ StoragePtr DatabaseMySQL::tryGetTable(const String & mysql_table_name, ContextPt ASTPtr DatabaseMySQL::getCreateTableQueryImpl(const String & table_name, ContextPtr local_context, bool throw_on_error) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); fetchTablesIntoLocalCache(local_context); @@ -175,7 +175,7 @@ ASTPtr DatabaseMySQL::getCreateTableQueryImpl(const String & table_name, Context time_t DatabaseMySQL::getObjectMetadataModificationTime(const String & table_name) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); fetchTablesIntoLocalCache(getContext()); @@ -360,7 +360,7 @@ void DatabaseMySQL::cleanOutdatedTables() void DatabaseMySQL::attachTable(ContextPtr /* context_ */, const String & table_name, const StoragePtr & storage, const String &) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; if (!local_tables_cache.contains(table_name)) throw Exception(ErrorCodes::UNKNOWN_TABLE, "Cannot attach table {}.{} because it does not exist.", @@ -383,7 +383,7 @@ void DatabaseMySQL::attachTable(ContextPtr /* context_ */, const String & table_ StoragePtr DatabaseMySQL::detachTable(ContextPtr /* context */, const String & table_name) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; if (remove_or_detach_tables.contains(table_name)) throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {}.{} is dropped", @@ -405,7 +405,7 @@ String DatabaseMySQL::getMetadataPath() const void DatabaseMySQL::loadStoredObjects(ContextMutablePtr, LoadingStrictnessLevel /*mode*/, bool /* skip_startup_tables */) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; fs::directory_iterator iter(getMetadataPath()); for (fs::directory_iterator end; iter != end; ++iter) @@ -421,7 +421,7 @@ void DatabaseMySQL::loadStoredObjects(ContextMutablePtr, LoadingStrictnessLevel void DatabaseMySQL::detachTablePermanently(ContextPtr, const String & table_name) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; fs::path remove_flag = fs::path(getMetadataPath()) / (escapeForFileName(table_name) + suffix); diff --git a/src/Databases/PostgreSQL/DatabasePostgreSQL.cpp b/src/Databases/PostgreSQL/DatabasePostgreSQL.cpp index f2d57968baa..8c267ea929b 100644 --- a/src/Databases/PostgreSQL/DatabasePostgreSQL.cpp +++ b/src/Databases/PostgreSQL/DatabasePostgreSQL.cpp @@ -76,7 +76,7 @@ String DatabasePostgreSQL::formatTableName(const String & table_name, bool quote bool DatabasePostgreSQL::empty() const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); auto connection_holder = pool->get(); auto tables_list = fetchPostgreSQLTablesList(connection_holder->get(), configuration.schema); @@ -91,7 +91,7 @@ bool DatabasePostgreSQL::empty() const DatabaseTablesIteratorPtr DatabasePostgreSQL::getTablesIterator(ContextPtr local_context, const FilterByNameFunction & /* filter_by_table_name */) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); Tables tables; /// Do not allow to throw here, because this might be, for example, a query to system.tables. @@ -154,7 +154,7 @@ bool DatabasePostgreSQL::checkPostgresTable(const String & table_name) const bool DatabasePostgreSQL::isTableExist(const String & table_name, ContextPtr /* context */) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); if (detached_or_dropped.contains(table_name)) return false; @@ -165,7 +165,7 @@ bool DatabasePostgreSQL::isTableExist(const String & table_name, ContextPtr /* c StoragePtr DatabasePostgreSQL::tryGetTable(const String & table_name, ContextPtr local_context) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); if (!detached_or_dropped.contains(table_name)) return fetchTable(table_name, local_context, false); @@ -210,7 +210,7 @@ StoragePtr DatabasePostgreSQL::fetchTable(const String & table_name, ContextPtr, void DatabasePostgreSQL::attachTable(ContextPtr /* context_ */, const String & table_name, const StoragePtr & storage, const String &) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; if (!checkPostgresTable(table_name)) throw Exception(ErrorCodes::UNKNOWN_TABLE, @@ -235,7 +235,7 @@ void DatabasePostgreSQL::attachTable(ContextPtr /* context_ */, const String & t StoragePtr DatabasePostgreSQL::detachTable(ContextPtr /* context_ */, const String & table_name) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; if (detached_or_dropped.contains(table_name)) throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Cannot detach table {}. It is already dropped/detached", getTableNameForLogs(table_name)); @@ -266,7 +266,7 @@ void DatabasePostgreSQL::createTable(ContextPtr local_context, const String & ta void DatabasePostgreSQL::dropTable(ContextPtr, const String & table_name, bool /* sync */) { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; if (!checkPostgresTable(table_name)) throw Exception(ErrorCodes::UNKNOWN_TABLE, "Cannot drop table {} because it does not exist", getTableNameForLogs(table_name)); @@ -293,7 +293,7 @@ void DatabasePostgreSQL::drop(ContextPtr /*context*/) void DatabasePostgreSQL::loadStoredObjects(ContextMutablePtr /* context */, LoadingStrictnessLevel /*mode*/, bool /* skip_startup_tables */) { { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; fs::directory_iterator iter(getMetadataPath()); /// Check for previously dropped tables @@ -314,7 +314,7 @@ void DatabasePostgreSQL::loadStoredObjects(ContextMutablePtr /* context */, Load void DatabasePostgreSQL::removeOutdatedTables() { - std::lock_guard lock{mutex}; + std::lock_guard lock{mutex}; auto connection_holder = pool->get(); auto actual_tables = fetchPostgreSQLTablesList(connection_holder->get(), configuration.schema); diff --git a/src/Databases/SQLite/DatabaseSQLite.cpp b/src/Databases/SQLite/DatabaseSQLite.cpp index 653e22487f0..1cba9d1dc26 100644 --- a/src/Databases/SQLite/DatabaseSQLite.cpp +++ b/src/Databases/SQLite/DatabaseSQLite.cpp @@ -40,14 +40,14 @@ DatabaseSQLite::DatabaseSQLite( bool DatabaseSQLite::empty() const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); return fetchTablesList().empty(); } DatabaseTablesIteratorPtr DatabaseSQLite::getTablesIterator(ContextPtr local_context, const IDatabase::FilterByNameFunction &) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); Tables tables; auto table_names = fetchTablesList(); @@ -120,14 +120,14 @@ bool DatabaseSQLite::checkSQLiteTable(const String & table_name) const bool DatabaseSQLite::isTableExist(const String & table_name, ContextPtr) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); return checkSQLiteTable(table_name); } StoragePtr DatabaseSQLite::tryGetTable(const String & table_name, ContextPtr local_context) const { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); return fetchTable(table_name, local_context, false); } @@ -175,7 +175,7 @@ ASTPtr DatabaseSQLite::getCreateTableQueryImpl(const String & table_name, Contex { StoragePtr storage; { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); storage = fetchTable(table_name, local_context, false); } if (!storage) diff --git a/src/Interpreters/Context.cpp b/src/Interpreters/Context.cpp index 33505e41789..d1b09707bca 100644 --- a/src/Interpreters/Context.cpp +++ b/src/Interpreters/Context.cpp @@ -1289,7 +1289,7 @@ void Context::addQueryAccessInfo( if (isGlobalContext()) throw Exception(ErrorCodes::LOGICAL_ERROR, "Global context cannot have query access info"); - std::lock_guard lock(query_access_info.mutex); + std::lock_guard lock(query_access_info.mutex); query_access_info.databases.emplace(quoted_database_name); query_access_info.tables.emplace(full_quoted_table_name); for (const auto & column_name : column_names) diff --git a/src/Interpreters/ExternalLoaderXMLConfigRepository.cpp b/src/Interpreters/ExternalLoaderXMLConfigRepository.cpp index ba4d9beffe3..8b9584f1242 100644 --- a/src/Interpreters/ExternalLoaderXMLConfigRepository.cpp +++ b/src/Interpreters/ExternalLoaderXMLConfigRepository.cpp @@ -34,7 +34,7 @@ std::set ExternalLoaderXMLConfigRepository::getAllLoadablesDefiniti std::unordered_set patterns_copy; { - std::lock_guard lock(patterns_mutex); + std::lock_guard lock(patterns_mutex); patterns_copy = patterns; } @@ -71,7 +71,7 @@ std::set ExternalLoaderXMLConfigRepository::getAllLoadablesDefiniti void ExternalLoaderXMLConfigRepository::updatePatterns(const std::unordered_set & patterns_) { - std::lock_guard lock(patterns_mutex); + std::lock_guard lock(patterns_mutex); if (patterns == patterns_) return; diff --git a/src/Interpreters/GraceHashJoin.cpp b/src/Interpreters/GraceHashJoin.cpp index 51d4c7d1f4b..79a825a752f 100644 --- a/src/Interpreters/GraceHashJoin.cpp +++ b/src/Interpreters/GraceHashJoin.cpp @@ -52,7 +52,7 @@ namespace Block read() { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); if (eof) return {}; diff --git a/src/Interpreters/JIT/CHJIT.cpp b/src/Interpreters/JIT/CHJIT.cpp index 96c02ad539e..63fe4f44f5f 100644 --- a/src/Interpreters/JIT/CHJIT.cpp +++ b/src/Interpreters/JIT/CHJIT.cpp @@ -352,7 +352,7 @@ CHJIT::~CHJIT() = default; CHJIT::CompiledModule CHJIT::compileModule(std::function compile_function) { - std::lock_guard lock(jit_lock); + std::lock_guard lock(jit_lock); auto module = createModuleForCompilation(); compile_function(*module); @@ -426,7 +426,7 @@ CHJIT::CompiledModule CHJIT::compileModule(std::unique_ptr module) void CHJIT::deleteCompiledModule(const CHJIT::CompiledModule & module) { - std::lock_guard lock(jit_lock); + std::lock_guard lock(jit_lock); auto module_it = module_identifier_to_memory_manager.find(module.identifier); if (module_it == module_identifier_to_memory_manager.end()) @@ -438,7 +438,7 @@ void CHJIT::deleteCompiledModule(const CHJIT::CompiledModule & module) void CHJIT::registerExternalSymbol(const std::string & symbol_name, void * address) { - std::lock_guard lock(jit_lock); + std::lock_guard lock(jit_lock); symbol_resolver->registerSymbol(symbol_name, address); } diff --git a/src/Loggers/OwnSplitChannel.cpp b/src/Loggers/OwnSplitChannel.cpp index 7974d5212e1..047f6caffcd 100644 --- a/src/Loggers/OwnSplitChannel.cpp +++ b/src/Loggers/OwnSplitChannel.cpp @@ -137,7 +137,7 @@ void OwnSplitChannel::logSplit(const Poco::Message & msg) std::shared_ptr text_log_locked{}; { - std::lock_guard lock(text_log_mutex); + std::lock_guard lock(text_log_mutex); text_log_locked = text_log.lock(); } if (text_log_locked) @@ -155,7 +155,7 @@ void OwnSplitChannel::addChannel(Poco::AutoPtr channel, const std #ifndef WITHOUT_TEXT_LOG void OwnSplitChannel::addTextLog(std::shared_ptr log, int max_priority) { - std::lock_guard lock(text_log_mutex); + std::lock_guard lock(text_log_mutex); text_log = log; text_log_max_priority.store(max_priority, std::memory_order_relaxed); } diff --git a/src/Processors/Formats/Impl/ParallelFormattingOutputFormat.cpp b/src/Processors/Formats/Impl/ParallelFormattingOutputFormat.cpp index db4bb422cb1..35d4dd9ddcd 100644 --- a/src/Processors/Formats/Impl/ParallelFormattingOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ParallelFormattingOutputFormat.cpp @@ -15,7 +15,7 @@ namespace DB collector_finished.wait(); { - std::lock_guard lock(collector_thread_mutex); + std::lock_guard lock(collector_thread_mutex); if (collector_thread.joinable()) collector_thread.join(); } @@ -80,7 +80,7 @@ namespace DB } { - std::lock_guard lock(collector_thread_mutex); + std::lock_guard lock(collector_thread_mutex); if (collector_thread.joinable()) collector_thread.join(); } @@ -137,7 +137,7 @@ namespace DB { /// Notify other threads. - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); unit.status = READY_TO_INSERT; writer_condvar.notify_all(); } @@ -227,7 +227,7 @@ namespace DB unit.actual_memory_size = out_buffer.getActualSize(); { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); unit.status = READY_TO_READ; collector_condvar.notify_all(); } diff --git a/src/Processors/Formats/Impl/ParallelParsingInputFormat.h b/src/Processors/Formats/Impl/ParallelParsingInputFormat.h index 252b6ec3f81..03fb2d650dc 100644 --- a/src/Processors/Formats/Impl/ParallelParsingInputFormat.h +++ b/src/Processors/Formats/Impl/ParallelParsingInputFormat.h @@ -292,7 +292,7 @@ private: { /// Additionally notify condvars - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); segmentator_condvar.notify_all(); reader_condvar.notify_all(); } diff --git a/src/Processors/QueryPlan/CreateSetAndFilterOnTheFlyStep.cpp b/src/Processors/QueryPlan/CreateSetAndFilterOnTheFlyStep.cpp index 86b64d1519c..53dcec9ef0a 100644 --- a/src/Processors/QueryPlan/CreateSetAndFilterOnTheFlyStep.cpp +++ b/src/Processors/QueryPlan/CreateSetAndFilterOnTheFlyStep.cpp @@ -62,7 +62,7 @@ public: { assert(!rhs_ports.first->isConnected() && !rhs_ports.second->isConnected()); - std::lock_guard lock(mux); + std::lock_guard lock(mux); if (input_port || output_port) { assert(input_port && output_port); diff --git a/src/Processors/Sources/ShellCommandSource.cpp b/src/Processors/Sources/ShellCommandSource.cpp index 4983fa047dc..ed8f9b41e78 100644 --- a/src/Processors/Sources/ShellCommandSource.cpp +++ b/src/Processors/Sources/ShellCommandSource.cpp @@ -274,7 +274,7 @@ namespace } catch (...) { - std::lock_guard lock(send_data_lock); + std::lock_guard lock(send_data_lock); exception_during_send_data = std::current_exception(); } }); @@ -387,7 +387,7 @@ namespace void rethrowExceptionDuringSendDataIfNeeded() { - std::lock_guard lock(send_data_lock); + std::lock_guard lock(send_data_lock); if (exception_during_send_data) { command_is_invalid = true; diff --git a/src/Server/PrometheusMetricsWriter.cpp b/src/Server/PrometheusMetricsWriter.cpp index 843d1e64463..abf2a2c0b6b 100644 --- a/src/Server/PrometheusMetricsWriter.cpp +++ b/src/Server/PrometheusMetricsWriter.cpp @@ -125,7 +125,7 @@ void PrometheusMetricsWriter::write(WriteBuffer & wb) const { for (size_t i = 0, end = CurrentStatusInfo::end(); i < end; ++i) { - std::lock_guard lock(CurrentStatusInfo::locks[static_cast(i)]); + std::lock_guard lock(CurrentStatusInfo::locks[static_cast(i)]); std::string metric_name{CurrentStatusInfo::getName(static_cast(i))}; std::string metric_doc{CurrentStatusInfo::getDocumentation(static_cast(i))}; diff --git a/src/Storages/FileLog/FileLogDirectoryWatcher.cpp b/src/Storages/FileLog/FileLogDirectoryWatcher.cpp index 192721f9f3c..3c5342a1e83 100644 --- a/src/Storages/FileLog/FileLogDirectoryWatcher.cpp +++ b/src/Storages/FileLog/FileLogDirectoryWatcher.cpp @@ -12,7 +12,7 @@ FileLogDirectoryWatcher::FileLogDirectoryWatcher(const std::string & path_, Stor FileLogDirectoryWatcher::Events FileLogDirectoryWatcher::getEventsAndReset() { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); Events res; res.swap(events); return res; @@ -20,7 +20,7 @@ FileLogDirectoryWatcher::Events FileLogDirectoryWatcher::getEventsAndReset() FileLogDirectoryWatcher::Error FileLogDirectoryWatcher::getErrorAndReset() { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); Error old_error = error; error = {}; return old_error; @@ -33,7 +33,7 @@ const std::string & FileLogDirectoryWatcher::getPath() const void FileLogDirectoryWatcher::onItemAdded(DirectoryWatcherBase::DirectoryEvent ev) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); EventInfo info{ev.event, "onItemAdded"}; std::string event_path = ev.path; @@ -51,7 +51,7 @@ void FileLogDirectoryWatcher::onItemAdded(DirectoryWatcherBase::DirectoryEvent e void FileLogDirectoryWatcher::onItemRemoved(DirectoryWatcherBase::DirectoryEvent ev) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); EventInfo info{ev.event, "onItemRemoved"}; std::string event_path = ev.path; @@ -74,7 +74,7 @@ void FileLogDirectoryWatcher::onItemRemoved(DirectoryWatcherBase::DirectoryEvent /// because it is equal to just record and handle one MODIY event void FileLogDirectoryWatcher::onItemModified(DirectoryWatcherBase::DirectoryEvent ev) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); auto event_path = ev.path; EventInfo info{ev.event, "onItemModified"}; @@ -97,7 +97,7 @@ void FileLogDirectoryWatcher::onItemModified(DirectoryWatcherBase::DirectoryEven void FileLogDirectoryWatcher::onItemMovedFrom(DirectoryWatcherBase::DirectoryEvent ev) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); EventInfo info{ev.event, "onItemMovedFrom"}; std::string event_path = ev.path; @@ -114,7 +114,7 @@ void FileLogDirectoryWatcher::onItemMovedFrom(DirectoryWatcherBase::DirectoryEve void FileLogDirectoryWatcher::onItemMovedTo(DirectoryWatcherBase::DirectoryEvent ev) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); EventInfo info{ev.event, "onItemMovedTo"}; std::string event_path = ev.path; @@ -131,7 +131,7 @@ void FileLogDirectoryWatcher::onItemMovedTo(DirectoryWatcherBase::DirectoryEvent void FileLogDirectoryWatcher::onError(Exception e) { - std::lock_guard lock(mutex); + std::lock_guard lock(mutex); LOG_ERROR(log, "Error happened during watching directory: {}", error.error_msg); error.has_error = true; error.error_msg = e.message(); diff --git a/src/Storages/FileLog/StorageFileLog.cpp b/src/Storages/FileLog/StorageFileLog.cpp index 7838db881e9..dae6f6a7ca9 100644 --- a/src/Storages/FileLog/StorageFileLog.cpp +++ b/src/Storages/FileLog/StorageFileLog.cpp @@ -312,7 +312,7 @@ Pipe StorageFileLog::read( if (mv_attached) throw Exception(ErrorCodes::QUERY_NOT_ALLOWED, "Cannot read from StorageFileLog with attached materialized views"); - std::lock_guard lock(file_infos_mutex); + std::lock_guard lock(file_infos_mutex); if (running_streams) { throw Exception(ErrorCodes::CANNOT_SELECT, "Another select query is running on this table, need to wait it finish."); @@ -659,7 +659,7 @@ void StorageFileLog::threadFunc() bool StorageFileLog::streamToViews() { - std::lock_guard lock(file_infos_mutex); + std::lock_guard lock(file_infos_mutex); if (running_streams) { LOG_INFO(log, "Another select query is running on this table, need to wait it finish."); diff --git a/src/Storages/Hive/StorageHive.cpp b/src/Storages/Hive/StorageHive.cpp index 13477a46bb5..85e6341eb5a 100644 --- a/src/Storages/Hive/StorageHive.cpp +++ b/src/Storages/Hive/StorageHive.cpp @@ -856,7 +856,7 @@ HiveFiles StorageHive::collectHiveFiles( = collectHiveFilesFromPartition(partition, query_info, hive_table_metadata, fs, context_, prune_level); if (!hive_files_in_partition.empty()) { - std::lock_guard lock(hive_files_mutex); + std::lock_guard lock(hive_files_mutex); hit_parttions_num += 1; if (hive_max_query_partitions > 0 && hit_parttions_num > hive_max_query_partitions) { @@ -882,7 +882,7 @@ HiveFiles StorageHive::collectHiveFiles( auto hive_file = getHiveFileIfNeeded(file_info, {}, query_info, hive_table_metadata, context_, prune_level); if (hive_file) { - std::lock_guard lock(hive_files_mutex); + std::lock_guard lock(hive_files_mutex); hive_files.push_back(hive_file); } }); diff --git a/src/Storages/MergeTree/ReplicatedMergeTreeQueue.cpp b/src/Storages/MergeTree/ReplicatedMergeTreeQueue.cpp index 125529e7b94..20dbaa73812 100644 --- a/src/Storages/MergeTree/ReplicatedMergeTreeQueue.cpp +++ b/src/Storages/MergeTree/ReplicatedMergeTreeQueue.cpp @@ -2316,7 +2316,7 @@ bool ReplicatedMergeTreeMergePredicate::canMergeSinglePart( return false; } - std::lock_guard lock(queue.state_mutex); + std::lock_guard lock(queue.state_mutex); /// We look for containing parts in queue.virtual_parts (and not in prev_virtual_parts) because queue.virtual_parts is newer /// and it is guaranteed that it will contain all merges assigned before this object is constructed. @@ -2334,7 +2334,7 @@ bool ReplicatedMergeTreeMergePredicate::canMergeSinglePart( bool ReplicatedMergeTreeMergePredicate::partParticipatesInReplaceRange(const MergeTreeData::DataPartPtr & part, String * out_reason) const { - std::lock_guard lock(queue.state_mutex); + std::lock_guard lock(queue.state_mutex); for (const auto & entry : queue.queue) { if (entry->type != ReplicatedMergeTreeLogEntry::REPLACE_RANGE) @@ -2457,7 +2457,7 @@ bool ReplicatedMergeTreeMergePredicate::isGoingToBeDropped(const MergeTreePartIn String ReplicatedMergeTreeMergePredicate::getCoveringVirtualPart(const String & part_name) const { - std::lock_guard lock(queue.state_mutex); + std::lock_guard lock(queue.state_mutex); return queue.virtual_parts.getContainingPart(MergeTreePartInfo::fromPartName(part_name, queue.format_version)); } diff --git a/src/Storages/StorageDictionary.cpp b/src/Storages/StorageDictionary.cpp index 5d1f08771be..b0b8aba38c7 100644 --- a/src/Storages/StorageDictionary.cpp +++ b/src/Storages/StorageDictionary.cpp @@ -208,13 +208,13 @@ void StorageDictionary::removeDictionaryConfigurationFromRepository() Poco::Timestamp StorageDictionary::getUpdateTime() const { - std::lock_guard lock(dictionary_config_mutex); + std::lock_guard lock(dictionary_config_mutex); return update_time; } LoadablesConfigurationPtr StorageDictionary::getConfiguration() const { - std::lock_guard lock(dictionary_config_mutex); + std::lock_guard lock(dictionary_config_mutex); return configuration; } @@ -234,7 +234,7 @@ void StorageDictionary::renameInMemory(const StorageID & new_table_id) assert(old_table_id.uuid == new_table_id.uuid || move_to_atomic || move_to_ordinary); { - std::lock_guard lock(dictionary_config_mutex); + std::lock_guard lock(dictionary_config_mutex); configuration->setString("dictionary.database", new_table_id.database_name); configuration->setString("dictionary.name", new_table_id.table_name); @@ -301,7 +301,7 @@ void StorageDictionary::alter(const AlterCommands & params, ContextPtr alter_con dictionary_non_const->setDictionaryComment(new_comment); } - std::lock_guard lock(dictionary_config_mutex); + std::lock_guard lock(dictionary_config_mutex); configuration->setString("dictionary.comment", new_comment); } diff --git a/src/Storages/StorageMergeTree.cpp b/src/Storages/StorageMergeTree.cpp index 8210ef93172..aadd7b8c20a 100644 --- a/src/Storages/StorageMergeTree.cpp +++ b/src/Storages/StorageMergeTree.cpp @@ -732,7 +732,7 @@ CancellationCode StorageMergeTree::killMutation(const String & mutation_id) to_kill->removeFile(); LOG_TRACE(log, "Cancelled part mutations and removed mutation file {}", mutation_id); { - std::lock_guard lock(mutation_wait_mutex); + std::lock_guard lock(mutation_wait_mutex); mutation_wait_event.notify_all(); } @@ -1306,7 +1306,7 @@ size_t StorageMergeTree::clearOldMutations(bool truncate) std::vector mutations_to_delete; { - std::lock_guard lock(currently_processing_in_background_mutex); + std::lock_guard lock(currently_processing_in_background_mutex); if (current_mutations_by_version.size() <= finished_mutations_to_keep) return 0; From 10610cbdf475d47af8f301bac415c436404c16dd Mon Sep 17 00:00:00 2001 From: kssenii Date: Thu, 2 Mar 2023 14:46:20 +0100 Subject: [PATCH 195/342] Fix test --- src/Disks/getOrCreateDiskFromAST.cpp | 5 ----- src/Parsers/FieldFromAST.cpp | 6 +++--- tests/queries/0_stateless/02344_show_caches.reference | 1 + 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Disks/getOrCreateDiskFromAST.cpp b/src/Disks/getOrCreateDiskFromAST.cpp index 9abc4a7d413..637acff7b95 100644 --- a/src/Disks/getOrCreateDiskFromAST.cpp +++ b/src/Disks/getOrCreateDiskFromAST.cpp @@ -33,11 +33,6 @@ namespace auto disk_name = DiskSelector::TMP_INTERNAL_DISK_PREFIX + toString(sipHash128(disk_setting_string.data(), disk_setting_string.size())); - LOG_TRACE( - &Poco::Logger::get("getOrCreateDiskFromDiskAST"), - "Using disk name `{}` for custom disk {}", - disk_name, disk_setting_string); - auto result_disk = context->getOrCreateDisk(disk_name, [&](const DisksMap & disks_map) -> DiskPtr { const auto * function_args_expr = assert_cast(function.arguments.get()); const auto & function_args = function_args_expr->children; diff --git a/src/Parsers/FieldFromAST.cpp b/src/Parsers/FieldFromAST.cpp index b2251599441..a81bf45a8be 100644 --- a/src/Parsers/FieldFromAST.cpp +++ b/src/Parsers/FieldFromAST.cpp @@ -32,7 +32,7 @@ bool FieldFromASTImpl::isSecret() const return isDiskFunction(ast); } -class DiskConfigurationHider +class DiskConfigurationMasker { public: struct Data {}; @@ -50,7 +50,7 @@ public: auto is_secret_arg = [](const std::string & arg_name) { /// We allow to not hide type of the disk, e.g. disk(type = s3, ...) - /// and also nested disk, e.g. disk(type = cache, disk = disk(type = s3, ...)) + /// and also nested disk, e.g. disk = 'disk_name' return arg_name != "type" && arg_name != "disk"; }; @@ -81,7 +81,7 @@ public: }; /// Visits children first. -using HideDiskConfigurationVisitor = InDepthNodeVisitor; +using HideDiskConfigurationVisitor = InDepthNodeVisitor; String FieldFromASTImpl::toString(bool show_secrets) const { diff --git a/tests/queries/0_stateless/02344_show_caches.reference b/tests/queries/0_stateless/02344_show_caches.reference index 2ee4f902ba1..b321319a309 100644 --- a/tests/queries/0_stateless/02344_show_caches.reference +++ b/tests/queries/0_stateless/02344_show_caches.reference @@ -10,5 +10,6 @@ local_cache s3_cache_6 s3_cache_small local_cache_2 +__tmp_internal_324081342946782869538999598488311137423 local_cache_3 s3_cache_multi_2 From 29f0f4e74517e43eb2d9d8b1d76be08efaa0ba6a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 2 Mar 2023 17:54:16 +0400 Subject: [PATCH 196/342] Typos in the page `/docs/ru/sql-reference/statements/create/table.md` --- docs/ru/sql-reference/statements/create/table.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/ru/sql-reference/statements/create/table.md b/docs/ru/sql-reference/statements/create/table.md index c29d575cac1..7a930b529ed 100644 --- a/docs/ru/sql-reference/statements/create/table.md +++ b/docs/ru/sql-reference/statements/create/table.md @@ -301,7 +301,7 @@ ClickHouse поддерживает временные таблицы со сл - Временные таблицы исчезают после завершения сессии, в том числе при обрыве соединения. - Временная таблица использует только модуль памяти. - Невозможно указать базу данных для временной таблицы. Она создается вне баз данных. -- Невозможно создать временную таблицу распределнным DDL запросом на всех серверах кластера (с опцией `ON CLUSTER`): такая таблица существует только в рамках существующей сессии. +- Невозможно создать временную таблицу распределённым DDL запросом на всех серверах кластера (с опцией `ON CLUSTER`): такая таблица существует только в рамках существующей сессии. - Если временная таблица имеет то же имя, что и некоторая другая, то, при упоминании в запросе без указания БД, будет использована временная таблица. - При распределённой обработке запроса, используемые в запросе временные таблицы, передаются на удалённые серверы. @@ -344,7 +344,9 @@ REPLACE TABLE myOldTable SELECT * FROM myOldTable WHERE CounterID <12345; ### Синтаксис +```sql {CREATE [OR REPLACE]|REPLACE} TABLE [db.]table_name +``` Для данного запроса можно использовать любые варианты синтаксиса запроса `CREATE`. Запрос `REPLACE` для несуществующей таблицы вызовет ошибку. From 9722d8a4030f3aca302ae495cd6a2750e8eb5248 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 2 Mar 2023 14:17:48 +0000 Subject: [PATCH 197/342] Rename variable --- src/IO/ReadWriteBufferFromHTTP.h | 70 +++++++++++++++++++------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/IO/ReadWriteBufferFromHTTP.h b/src/IO/ReadWriteBufferFromHTTP.h index edf7b3f5f95..5c5285c5260 100644 --- a/src/IO/ReadWriteBufferFromHTTP.h +++ b/src/IO/ReadWriteBufferFromHTTP.h @@ -147,7 +147,7 @@ namespace detail size_t getOffset() const { return getRangeBegin() + offset_from_begin_pos; } - template + template std::istream * callImpl(UpdatableSessionPtr & current_session, Poco::URI uri_, Poco::Net::HTTPResponse & response, const std::string & method_) { // With empty path poco will send "POST HTTP/1.1" its bug. @@ -164,7 +164,7 @@ namespace detail request.set(header, value); std::optional range; - if constexpr (use_initial_range) + if constexpr (for_object_info) { if (withPartialContent(initial_read_range)) range = initial_read_range; @@ -202,7 +202,9 @@ namespace detail auto result_istr = receiveResponse(*sess, request, response, true); response.getCookies(cookies); - if constexpr (!use_initial_range) + /// we can fetch object info while the request is being processed + /// and we don't want to override any context used by it + if constexpr (!for_object_info) content_encoding = response.get("Content-Encoding", ""); return result_istr; @@ -222,7 +224,7 @@ namespace detail return *file_size; Poco::Net::HTTPResponse response; - getHeadResponse(response); + getHeadResponse(response); if (response.hasContentLength()) { @@ -251,14 +253,13 @@ namespace detail InitializeError initialization_error = InitializeError::NONE; private: - template void getHeadResponse(Poco::Net::HTTPResponse & response) { for (size_t i = 0; i < settings.http_max_tries; ++i) { try { - callWithRedirects(response, Poco::Net::HTTPRequest::HTTP_HEAD); + callWithRedirects(response, Poco::Net::HTTPRequest::HTTP_HEAD); break; } catch (const Poco::Exception & e) @@ -377,17 +378,19 @@ namespace detail return location_uri; } - template + template void callWithRedirects(Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) { UpdatableSessionPtr current_session = nullptr; - if (use_initial_range) + /// we can fetch object info while the request is being processed + /// and we don't want to override any context used by it + if constexpr (for_object_info) current_session = session->clone(uri); else current_session = session; - call(current_session, response, method_, throw_on_all_errors); + call(current_session, response, method_, throw_on_all_errors); Poco::URI prev_uri = uri; while (isRedirect(response.getStatus())) @@ -399,42 +402,53 @@ namespace detail current_session->updateSession(uri_redirect); - auto result_istr = callImpl(current_session, uri_redirect, response, method); - if (!use_initial_range) + /// we can fetch object info while the request is being processed + /// and we don't want to override any context used by it + auto result_istr = callImpl(current_session, uri_redirect, response, method); + if constexpr (!for_object_info) istr = result_istr; } } - template + template void call(UpdatableSessionPtr & current_session, Poco::Net::HTTPResponse & response, const String & method_, bool throw_on_all_errors = false) { try { - auto result_istr = callImpl(current_session, saved_uri_redirect ? *saved_uri_redirect : uri, response, method_); - if (!use_initial_range) + /// we can fetch object info while the request is being processed + /// and we don't want to override any context used by it + auto result_istr = callImpl(current_session, saved_uri_redirect ? *saved_uri_redirect : uri, response, method_); + if constexpr (!for_object_info) istr = result_istr; } catch (...) { - if (throw_on_all_errors) + /// we can fetch object info while the request is being processed + /// and we don't want to override any context used by it + if constexpr (for_object_info) { throw; } - - auto http_status = response.getStatus(); - - if (http_status == Poco::Net::HTTPResponse::HTTPStatus::HTTP_NOT_FOUND && http_skip_not_found_url) - { - initialization_error = InitializeError::SKIP_NOT_FOUND_URL; - } - else if (!isRetriableError(http_status)) - { - initialization_error = InitializeError::NON_RETRYABLE_ERROR; - exception = std::current_exception(); - } else { - throw; + if (throw_on_all_errors) + throw; + + auto http_status = response.getStatus(); + + if (http_status == Poco::Net::HTTPResponse::HTTPStatus::HTTP_NOT_FOUND && http_skip_not_found_url) + { + initialization_error = InitializeError::SKIP_NOT_FOUND_URL; + } + else if (!isRetriableError(http_status)) + { + initialization_error = InitializeError::NON_RETRYABLE_ERROR; + exception = std::current_exception(); + } + else + { + throw; + } } } } From cf245538b91923a97c617a997e2bc68d01b51d75 Mon Sep 17 00:00:00 2001 From: AndyB Date: Thu, 2 Mar 2023 14:31:38 +0000 Subject: [PATCH 198/342] checker fix --- tests/integration/test_log_levels_update/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_log_levels_update/test.py b/tests/integration/test_log_levels_update/test.py index 842c7914eaa..4b83b6431fc 100644 --- a/tests/integration/test_log_levels_update/test.py +++ b/tests/integration/test_log_levels_update/test.py @@ -63,4 +63,4 @@ def test_log_levels_update(start_cluster): log = get_log(node) assert len(log) > 0 - assert not re.search("(|)", log) + assert not re.search("", log) From fdf8a418f517eeb739aa22e6afb0d7392a212a17 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Wed, 1 Mar 2023 20:37:20 +0000 Subject: [PATCH 199/342] better interface --- src/DataTypes/DataTypeTuple.cpp | 14 -------------- src/DataTypes/DataTypeTuple.h | 1 - src/DataTypes/IDataType.cpp | 9 --------- src/DataTypes/IDataType.h | 1 - src/DataTypes/Serializations/SerializationInfo.cpp | 9 +++++++++ src/DataTypes/Serializations/SerializationInfo.h | 2 ++ .../Serializations/SerializationInfoTuple.cpp | 14 ++++++++++++++ .../Serializations/SerializationInfoTuple.h | 2 +- src/Storages/MergeTree/MutateTask.cpp | 2 +- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/DataTypes/DataTypeTuple.cpp b/src/DataTypes/DataTypeTuple.cpp index 1e28af3ee54..768f87fe3d4 100644 --- a/src/DataTypes/DataTypeTuple.cpp +++ b/src/DataTypes/DataTypeTuple.cpp @@ -312,20 +312,6 @@ MutableSerializationInfoPtr DataTypeTuple::createSerializationInfo(const Seriali return std::make_shared(std::move(infos), names, settings); } -MutableSerializationInfoPtr DataTypeTuple::cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const -{ - const auto & old_info_tuple = assert_cast(old_info); - assert(old_info_tuple.getNumElements() == elems.size()); - - MutableSerializationInfos infos; - infos.reserve(elems.size()); - for (size_t i = 0; i < elems.size(); ++i) - infos.push_back(elems[i]->cloneSerializationInfo(*old_info_tuple.getElementInfo(i), settings)); - - return std::make_shared(std::move(infos), names, settings); -} - - SerializationInfoPtr DataTypeTuple::getSerializationInfo(const IColumn & column) const { if (const auto * column_const = checkAndGetColumn(&column)) diff --git a/src/DataTypes/DataTypeTuple.h b/src/DataTypes/DataTypeTuple.h index 9f3860f78db..152f21015f5 100644 --- a/src/DataTypes/DataTypeTuple.h +++ b/src/DataTypes/DataTypeTuple.h @@ -57,7 +57,6 @@ public: SerializationPtr doGetDefaultSerialization() const override; SerializationPtr getSerialization(const SerializationInfo & info) const override; MutableSerializationInfoPtr createSerializationInfo(const SerializationInfo::Settings & settings) const override; - MutableSerializationInfoPtr cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const override; SerializationInfoPtr getSerializationInfo(const IColumn & column) const override; const DataTypePtr & getElement(size_t i) const { return elems[i]; } diff --git a/src/DataTypes/IDataType.cpp b/src/DataTypes/IDataType.cpp index 5cd0b6f659c..e0612fbbf36 100644 --- a/src/DataTypes/IDataType.cpp +++ b/src/DataTypes/IDataType.cpp @@ -191,15 +191,6 @@ MutableSerializationInfoPtr IDataType::createSerializationInfo(const Serializati return std::make_shared(ISerialization::Kind::DEFAULT, settings); } -MutableSerializationInfoPtr IDataType::cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const -{ - auto new_kind = old_info.getKind(); - if (new_kind == ISerialization::Kind::SPARSE && !supportsSparseSerialization()) - new_kind = ISerialization::Kind::DEFAULT; - - return std::make_shared(new_kind, settings); -} - SerializationInfoPtr IDataType::getSerializationInfo(const IColumn & column) const { if (const auto * column_const = checkAndGetColumn(&column)) diff --git a/src/DataTypes/IDataType.h b/src/DataTypes/IDataType.h index 5bd12f84d04..e5bdbeca69e 100644 --- a/src/DataTypes/IDataType.h +++ b/src/DataTypes/IDataType.h @@ -104,7 +104,6 @@ public: Names getSubcolumnNames() const; virtual MutableSerializationInfoPtr createSerializationInfo(const SerializationInfo::Settings & settings) const; - virtual MutableSerializationInfoPtr cloneSerializationInfo(const SerializationInfo & old_info, const SerializationInfo::Settings & settings) const; virtual SerializationInfoPtr getSerializationInfo(const IColumn & column) const; /// TODO: support more types. diff --git a/src/DataTypes/Serializations/SerializationInfo.cpp b/src/DataTypes/Serializations/SerializationInfo.cpp index 313fa1fa235..af3330d867d 100644 --- a/src/DataTypes/Serializations/SerializationInfo.cpp +++ b/src/DataTypes/Serializations/SerializationInfo.cpp @@ -97,6 +97,15 @@ MutableSerializationInfoPtr SerializationInfo::clone() const return std::make_shared(kind, settings, data); } +std::shared_ptr SerializationInfo::createWithType(const IDataType & type, const Settings & new_settings) const +{ + auto new_kind = kind; + if (new_kind == ISerialization::Kind::SPARSE && !type.supportsSparseSerialization()) + new_kind = ISerialization::Kind::DEFAULT; + + return std::make_shared(new_kind, new_settings); +} + void SerializationInfo::serialializeKindBinary(WriteBuffer & out) const { writeBinary(static_cast(kind), out); diff --git a/src/DataTypes/Serializations/SerializationInfo.h b/src/DataTypes/Serializations/SerializationInfo.h index 7f73d053f1b..560156980db 100644 --- a/src/DataTypes/Serializations/SerializationInfo.h +++ b/src/DataTypes/Serializations/SerializationInfo.h @@ -8,6 +8,7 @@ namespace DB { +class ReadBuffer; class ReadBuffer; class WriteBuffer; class NamesAndTypesList; @@ -59,6 +60,7 @@ public: virtual void replaceData(const SerializationInfo & other); virtual std::shared_ptr clone() const; + virtual std::shared_ptr createWithType(const IDataType & type, const Settings & new_settings) const; virtual void serialializeKindBinary(WriteBuffer & out) const; virtual void deserializeFromKindsBinary(ReadBuffer & in); diff --git a/src/DataTypes/Serializations/SerializationInfoTuple.cpp b/src/DataTypes/Serializations/SerializationInfoTuple.cpp index 1a9639a1566..5724dd7a5c3 100644 --- a/src/DataTypes/Serializations/SerializationInfoTuple.cpp +++ b/src/DataTypes/Serializations/SerializationInfoTuple.cpp @@ -97,6 +97,20 @@ MutableSerializationInfoPtr SerializationInfoTuple::clone() const return std::make_shared(std::move(elems_cloned), names, settings); } +MutableSerializationInfoPtr SerializationInfoTuple::createWithType(const IDataType & type, const Settings & new_settings) const +{ + const auto & type_tuple = assert_cast(type); + const auto & tuple_elements = type_tuple.getElements(); + assert(elems.size() == tuple_elements.size()); + + MutableSerializationInfos infos; + infos.reserve(elems.size()); + for (size_t i = 0; i < elems.size(); ++i) + infos.push_back(elems[i]->createWithType(*tuple_elements[i], new_settings)); + + return std::make_shared(std::move(infos), names, new_settings); +} + void SerializationInfoTuple::serialializeKindBinary(WriteBuffer & out) const { SerializationInfo::serialializeKindBinary(out); diff --git a/src/DataTypes/Serializations/SerializationInfoTuple.h b/src/DataTypes/Serializations/SerializationInfoTuple.h index ef288948b9a..4bcd14d34cb 100644 --- a/src/DataTypes/Serializations/SerializationInfoTuple.h +++ b/src/DataTypes/Serializations/SerializationInfoTuple.h @@ -19,6 +19,7 @@ public: void replaceData(const SerializationInfo & other) override; MutableSerializationInfoPtr clone() const override; + MutableSerializationInfoPtr createWithType(const IDataType & type, const Settings & new_settings) const override; void serialializeKindBinary(WriteBuffer & out) const override; void deserializeFromKindsBinary(ReadBuffer & in) override; @@ -26,7 +27,6 @@ public: Poco::JSON::Object toJSON() const override; void fromJSON(const Poco::JSON::Object & object) override; - size_t getNumElements() const { return elems.size(); } const MutableSerializationInfoPtr & getElementInfo(size_t i) const { return elems[i]; } ISerialization::Kind getElementKind(size_t i) const { return elems[i]->getKind(); } diff --git a/src/Storages/MergeTree/MutateTask.cpp b/src/Storages/MergeTree/MutateTask.cpp index 9420b7bebaf..1239befdd67 100644 --- a/src/Storages/MergeTree/MutateTask.cpp +++ b/src/Storages/MergeTree/MutateTask.cpp @@ -244,7 +244,7 @@ getColumnsForNewDataPart( continue; } - new_info = new_type->cloneSerializationInfo(*old_info, settings); + new_info = old_info->createWithType(*new_type, settings); new_serialization_infos.emplace(new_name, std::move(new_info)); } From df41a83ddb1f83618ce1ad666545cf6138444df3 Mon Sep 17 00:00:00 2001 From: Yakov Olkhovskiy Date: Thu, 2 Mar 2023 14:49:49 +0000 Subject: [PATCH 200/342] set uid gid to file's original --- .../decompressor.cpp | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/utils/self-extracting-executable/decompressor.cpp b/utils/self-extracting-executable/decompressor.cpp index 5f2a769dcdb..39fe552dfac 100644 --- a/utils/self-extracting-executable/decompressor.cpp +++ b/utils/self-extracting-executable/decompressor.cpp @@ -168,23 +168,9 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n return 0; } -bool getSudoIDs(uid_t &sudo_uid, uid_t &sudo_gid) +bool isSudo() { - sudo_uid = 0; - sudo_gid = 0; - - if (getuid() || geteuid() || getenv("SUDO_USER") == nullptr || getenv("SUDO_UID") == nullptr || getenv("SUDO_GID") == nullptr) - return false; - - char * str_end; - long id = strtol(getenv("SUDO_UID"), &str_end, 10); - if (*str_end == 0) - sudo_uid = static_cast(id); - id = strtol(getenv("SUDO_GID"), &str_end, 10); - if (*str_end == 0) - sudo_gid = static_cast(id); - - return true; + return getuid() == 0 && geteuid() == 0 && getenv("SUDO_USER") && getenv("SUDO_UID") && getenv("SUDO_GID"); } /// Read data about files and decomrpess them. @@ -238,9 +224,7 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress return 1; } - uid_t sudo_uid = 0; - uid_t sudo_gid = 0; - getSudoIDs(sudo_uid, sudo_gid); + bool is_sudo = isSudo(); FileData file_info; /// Decompress files with appropriate file names @@ -342,8 +326,8 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress if (0 != close(output_fd)) perror("close"); - if (sudo_uid && sudo_gid) - chown(file_name, sudo_uid, sudo_gid); + if (is_sudo) + chown(file_name, info_in.st_uid, info_in.st_gid); } if (0 != munmap(input, info_in.st_size)) @@ -557,8 +541,8 @@ int main(int/* argc*/, char* argv[]) return 1; } - if (uid_t sudo_uid = 0, sudo_gid = 0; getSudoIDs(sudo_uid, sudo_gid)) - chown(static_cast(self), sudo_uid, sudo_gid); + if (isSudo()) + chown(static_cast(self), input_info.st_uid, input_info.st_gid); if (has_exec) { From 95292ce5c79a8386ec6736e1b3fd934c3a38e02a Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Thu, 2 Mar 2023 16:26:46 +0000 Subject: [PATCH 201/342] Make 01710_projections more stable. --- tests/queries/0_stateless/01710_projections.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/01710_projections.sql b/tests/queries/0_stateless/01710_projections.sql index cbabd3ec598..146b46aed38 100644 --- a/tests/queries/0_stateless/01710_projections.sql +++ b/tests/queries/0_stateless/01710_projections.sql @@ -1,6 +1,6 @@ drop table if exists projection_test; -create table projection_test (`sum(block_count)` UInt64, domain_alias UInt64 alias length(domain), datetime DateTime, domain LowCardinality(String), x_id String, y_id String, block_count Int64, retry_count Int64, duration Int64, kbytes Int64, buffer_time Int64, first_time Int64, total_bytes Nullable(UInt64), valid_bytes Nullable(UInt64), completed_bytes Nullable(UInt64), fixed_bytes Nullable(UInt64), force_bytes Nullable(UInt64), projection p (select toStartOfMinute(datetime) dt_m, countIf(first_time = 0) / count(), avg((kbytes * 8) / duration), count(), sum(block_count) / sum(duration), avg(block_count / duration), sum(buffer_time) / sum(duration), avg(buffer_time / duration), sum(valid_bytes) / sum(total_bytes), sum(completed_bytes) / sum(total_bytes), sum(fixed_bytes) / sum(total_bytes), sum(force_bytes) / sum(total_bytes), sum(valid_bytes) / sum(total_bytes), sum(retry_count) / sum(duration), avg(retry_count / duration), countIf(block_count > 0) / count(), countIf(first_time = 0) / count(), uniqHLL12(x_id), uniqHLL12(y_id) group by dt_m, domain)) engine MergeTree partition by toDate(datetime) order by (toStartOfTenMinutes(datetime), domain); +create table projection_test (`sum(block_count)` UInt64, domain_alias UInt64 alias length(domain), datetime DateTime, domain LowCardinality(String), x_id String, y_id String, block_count Int64, retry_count Int64, duration Int64, kbytes Int64, buffer_time Int64, first_time Int64, total_bytes Nullable(UInt64), valid_bytes Nullable(UInt64), completed_bytes Nullable(UInt64), fixed_bytes Nullable(UInt64), force_bytes Nullable(UInt64), projection p (select toStartOfMinute(datetime) dt_m, countIf(first_time = 0) / count(), avg((kbytes * 8) / duration), count(), sum(block_count) / sum(duration), avg(block_count / duration), sum(buffer_time) / sum(duration), avg(buffer_time / duration), sum(valid_bytes) / sum(total_bytes), sum(completed_bytes) / sum(total_bytes), sum(fixed_bytes) / sum(total_bytes), sum(force_bytes) / sum(total_bytes), sum(valid_bytes) / sum(total_bytes), sum(retry_count) / sum(duration), avg(retry_count / duration), countIf(block_count > 0) / count(), countIf(first_time = 0) / count(), uniqHLL12(x_id), uniqHLL12(y_id) group by dt_m, domain)) engine MergeTree partition by toDate(datetime) order by (toStartOfTenMinutes(datetime), domain) settings index_granularity_bytes = 10000000; insert into projection_test with rowNumberInAllBlocks() as id select 1, toDateTime('2020-10-24 00:00:00') + (id / 20), toString(id % 100), * from generateRandom('x_id String, y_id String, block_count Int64, retry_count Int64, duration Int64, kbytes Int64, buffer_time Int64, first_time Int64, total_bytes Nullable(UInt64), valid_bytes Nullable(UInt64), completed_bytes Nullable(UInt64), fixed_bytes Nullable(UInt64), force_bytes Nullable(UInt64)', 10, 10, 1) limit 1000 settings max_threads = 1; From 48fc545e68fc86cb903a2272f1a62640429bfde2 Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Thu, 2 Mar 2023 17:27:57 +0100 Subject: [PATCH 202/342] Update 01710_projections.sql --- tests/queries/0_stateless/01710_projections.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/01710_projections.sql b/tests/queries/0_stateless/01710_projections.sql index 146b46aed38..5097a88c8fa 100644 --- a/tests/queries/0_stateless/01710_projections.sql +++ b/tests/queries/0_stateless/01710_projections.sql @@ -1,6 +1,6 @@ drop table if exists projection_test; -create table projection_test (`sum(block_count)` UInt64, domain_alias UInt64 alias length(domain), datetime DateTime, domain LowCardinality(String), x_id String, y_id String, block_count Int64, retry_count Int64, duration Int64, kbytes Int64, buffer_time Int64, first_time Int64, total_bytes Nullable(UInt64), valid_bytes Nullable(UInt64), completed_bytes Nullable(UInt64), fixed_bytes Nullable(UInt64), force_bytes Nullable(UInt64), projection p (select toStartOfMinute(datetime) dt_m, countIf(first_time = 0) / count(), avg((kbytes * 8) / duration), count(), sum(block_count) / sum(duration), avg(block_count / duration), sum(buffer_time) / sum(duration), avg(buffer_time / duration), sum(valid_bytes) / sum(total_bytes), sum(completed_bytes) / sum(total_bytes), sum(fixed_bytes) / sum(total_bytes), sum(force_bytes) / sum(total_bytes), sum(valid_bytes) / sum(total_bytes), sum(retry_count) / sum(duration), avg(retry_count / duration), countIf(block_count > 0) / count(), countIf(first_time = 0) / count(), uniqHLL12(x_id), uniqHLL12(y_id) group by dt_m, domain)) engine MergeTree partition by toDate(datetime) order by (toStartOfTenMinutes(datetime), domain) settings index_granularity_bytes = 10000000; +create table projection_test (`sum(block_count)` UInt64, domain_alias UInt64 alias length(domain), datetime DateTime, domain LowCardinality(String), x_id String, y_id String, block_count Int64, retry_count Int64, duration Int64, kbytes Int64, buffer_time Int64, first_time Int64, total_bytes Nullable(UInt64), valid_bytes Nullable(UInt64), completed_bytes Nullable(UInt64), fixed_bytes Nullable(UInt64), force_bytes Nullable(UInt64), projection p (select toStartOfMinute(datetime) dt_m, countIf(first_time = 0) / count(), avg((kbytes * 8) / duration), count(), sum(block_count) / sum(duration), avg(block_count / duration), sum(buffer_time) / sum(duration), avg(buffer_time / duration), sum(valid_bytes) / sum(total_bytes), sum(completed_bytes) / sum(total_bytes), sum(fixed_bytes) / sum(total_bytes), sum(force_bytes) / sum(total_bytes), sum(valid_bytes) / sum(total_bytes), sum(retry_count) / sum(duration), avg(retry_count / duration), countIf(block_count > 0) / count(), countIf(first_time = 0) / count(), uniqHLL12(x_id), uniqHLL12(y_id) group by dt_m, domain)) engine MergeTree partition by toDate(datetime) order by (toStartOfTenMinutes(datetime), domain) settings index_granularity_bytes = 10000000; insert into projection_test with rowNumberInAllBlocks() as id select 1, toDateTime('2020-10-24 00:00:00') + (id / 20), toString(id % 100), * from generateRandom('x_id String, y_id String, block_count Int64, retry_count Int64, duration Int64, kbytes Int64, buffer_time Int64, first_time Int64, total_bytes Nullable(UInt64), valid_bytes Nullable(UInt64), completed_bytes Nullable(UInt64), fixed_bytes Nullable(UInt64), force_bytes Nullable(UInt64)', 10, 10, 1) limit 1000 settings max_threads = 1; From 73a6c920d3dd595d6771e6801e91c5fa0bbf9446 Mon Sep 17 00:00:00 2001 From: Dan Roscigno Date: Thu, 2 Mar 2023 11:31:22 -0500 Subject: [PATCH 203/342] Update date-time-functions.md closes #42758 --- .../functions/date-time-functions.md | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/en/sql-reference/functions/date-time-functions.md b/docs/en/sql-reference/functions/date-time-functions.md index f6af8abcbaf..a6043d2507e 100644 --- a/docs/en/sql-reference/functions/date-time-functions.md +++ b/docs/en/sql-reference/functions/date-time-functions.md @@ -1126,15 +1126,48 @@ Rounds the time to the half hour. ## toYYYYMM -Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 100 + MM). +Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 100 + MM). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. + +### example +```sql +SELECT + toYYYYMM(now(), 'US/Eastern') +``` +```response +┌─toYYYYMM(now(), 'US/Eastern')─┐ +│ 202303 │ +└───────────────────────────────┘ +``` ## toYYYYMMDD -Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 10000 + MM \* 100 + DD). +Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 10000 + MM \* 100 + DD). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. + +### example +```sql +SELECT + toYYYYMMDD(now(), 'US/Eastern') +``` +```response +┌─toYYYYMMDD(now(), 'US/Eastern')─┐ +│ 20230302 │ +└─────────────────────────────────┘ +``` ## toYYYYMMDDhhmmss -Converts a date or date with time to a UInt64 number containing the year and month number (YYYY \* 10000000000 + MM \* 100000000 + DD \* 1000000 + hh \* 10000 + mm \* 100 + ss). +Converts a date or date with time to a UInt64 number containing the year and month number (YYYY \* 10000000000 + MM \* 100000000 + DD \* 1000000 + hh \* 10000 + mm \* 100 + ss). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. + +### example +```sql +SELECT + toYYYYMMDDhhmmss(now(), 'US/Eastern') +``` +```response +┌─toYYYYMMDDhhmmss(now(), 'US/Eastern')─┐ +│ 20230302112209 │ +└───────────────────────────────────────┘ +``` ## addYears, addMonths, addWeeks, addDays, addHours, addMinutes, addSeconds, addQuarters From 63eeec1785c7fa6553e93f5cbe0d94ce0647d292 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Thu, 2 Mar 2023 17:44:48 +0100 Subject: [PATCH 204/342] Mark 01771_bloom_filter_not_has as no-parallel and long This test can take long enough time in debug build > 10min, and because clickhouse-test does not wait the test in this case and simply call DROP DATABASE, this will eventually lead to error during DROP since the table is still in use. CI: https://s3.amazonaws.com/clickhouse-test-reports/45491/99329d868232d9377d7f808763e951e6f15fd71c/stateless_tests__debug__%5B5/5%5D.html Signed-off-by: Azat Khuzhin --- tests/queries/0_stateless/01771_bloom_filter_not_has.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/01771_bloom_filter_not_has.sql b/tests/queries/0_stateless/01771_bloom_filter_not_has.sql index ab0e3d308f9..f945cbde56b 100644 --- a/tests/queries/0_stateless/01771_bloom_filter_not_has.sql +++ b/tests/queries/0_stateless/01771_bloom_filter_not_has.sql @@ -1,3 +1,4 @@ +-- Tags: no-parallel, long DROP TABLE IF EXISTS bloom_filter_null_array; CREATE TABLE bloom_filter_null_array (v Array(Int32), INDEX idx v TYPE bloom_filter GRANULARITY 3) ENGINE = MergeTree() ORDER BY v; INSERT INTO bloom_filter_null_array SELECT [number] FROM numbers(10000000); From d378ae9c7698685cb187deff8e7e490058d5ccd5 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 2 Mar 2023 17:05:11 +0000 Subject: [PATCH 205/342] Docs: Switch two rows for consistency --- docs/en/sql-reference/functions/date-time-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/functions/date-time-functions.md b/docs/en/sql-reference/functions/date-time-functions.md index f6af8abcbaf..49b0bc25edd 100644 --- a/docs/en/sql-reference/functions/date-time-functions.md +++ b/docs/en/sql-reference/functions/date-time-functions.md @@ -1231,8 +1231,8 @@ Using replacement fields, you can define a pattern for the resulting string. “ | %e | day of the month, space-padded (1-31) |   2 | | %f | fractional second from the fractional part of DateTime64 | 1234560 | | %F | short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2018-01-02 | -| %G | four-digit year format for ISO week number, calculated from the week-based year [defined by the ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Week_dates) standard, normally useful only with %V | 2018 | | %g | two-digit year format, aligned to ISO 8601, abbreviated from four-digit notation | 18 | +| %G | four-digit year format for ISO week number, calculated from the week-based year [defined by the ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Week_dates) standard, normally useful only with %V | 2018 | | %h | hour in 12h format (01-12) | 09 | | %H | hour in 24h format (00-23) | 22 | | %i | minute (00-59) | 33 | From 14351922e3f250c4c92383462f701fcaef198054 Mon Sep 17 00:00:00 2001 From: avogar Date: Thu, 2 Mar 2023 17:20:29 +0000 Subject: [PATCH 206/342] Fix possible deadlock in QueryStatus --- src/Interpreters/ProcessList.cpp | 58 ++++++++++++++++++++++++++++---- src/Interpreters/ProcessList.h | 16 ++++++++- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/Interpreters/ProcessList.cpp b/src/Interpreters/ProcessList.cpp index 49d7989ac5e..52674dc1c77 100644 --- a/src/Interpreters/ProcessList.cpp +++ b/src/Interpreters/ProcessList.cpp @@ -373,6 +373,19 @@ QueryStatus::~QueryStatus() } } +void QueryStatus::ExecutorHolder::cancel() +{ + std::lock_guard lock(mutex); + if (executor) + executor->cancel(); +} + +void QueryStatus::ExecutorHolder::remove() +{ + std::lock_guard lock(mutex); + executor = nullptr; +} + CancellationCode QueryStatus::cancelQuery(bool) { if (is_killed.load()) @@ -380,9 +393,37 @@ CancellationCode QueryStatus::cancelQuery(bool) is_killed.store(true); - std::lock_guard lock(executors_mutex); - for (auto * e : executors) - e->cancel(); + std::unique_lock lock(executors_mutex); + + /// Track all cancelled executors. + std::unordered_set cancelled; + /// We cancel executors from the left to the right, so if the last executor + /// was cancelled, then all executors were cancelled. + while (!cancelled.contains(executors.back().get())) + { + size_t size = executors.size(); + /// We should create a copy of executor holder, because it can be + /// removed from vector in removePipelineExecutor from another thread + /// and reference will be invalid. + for (auto e : executors) + { + if (cancelled.contains(e.get())) + continue; + /// We should call cancel() with unlocked executors_mutex, because + /// cancel() can try to lock some internal mutex that is already locked by query executing + /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, + /// which will lead to deadlock. + lock.unlock(); + e->cancel(); + lock.lock(); + cancelled.insert(e.get()); + /// While executors_mutex was unlocked, removePipelineExecutor could be called and + /// the size of executors could have changed. In this case we should start iterating + /// over it again to avoid using invalid iterators. + if (executors.size() != size) + break; + } + } return CancellationCode::CancelSent; } @@ -396,15 +437,18 @@ void QueryStatus::addPipelineExecutor(PipelineExecutor * e) throw Exception(ErrorCodes::QUERY_WAS_CANCELLED, "Query was cancelled"); std::lock_guard lock(executors_mutex); - assert(std::find(executors.begin(), executors.end(), e) == executors.end()); - executors.push_back(e); + assert(std::find_if(executors.begin(), executors.end(), [e](const ExecutorHolderPtr & x){ return x->executor == e; }) == executors.end()); + executors.push_back(std::make_shared(e)); } void QueryStatus::removePipelineExecutor(PipelineExecutor * e) { std::lock_guard lock(executors_mutex); - assert(std::find(executors.begin(), executors.end(), e) != executors.end()); - std::erase_if(executors, [e](PipelineExecutor * x) { return x == e; }); + auto it = std::find_if(executors.begin(), executors.end(), [e](const ExecutorHolderPtr & x){ return x->executor == e; }); + assert(it != executors.end()); + /// Invalidate executor pointer inside holder. + (*it)->remove(); + executors.erase(it); } bool QueryStatus::checkTimeLimit() diff --git a/src/Interpreters/ProcessList.h b/src/Interpreters/ProcessList.h index d5c136ab62a..30bfde4e218 100644 --- a/src/Interpreters/ProcessList.h +++ b/src/Interpreters/ProcessList.h @@ -119,8 +119,22 @@ protected: mutable std::mutex executors_mutex; + struct ExecutorHolder + { + ExecutorHolder(PipelineExecutor * e) : executor(e) {} + + void cancel(); + + void remove(); + + PipelineExecutor * executor; + std::mutex mutex; + }; + + using ExecutorHolderPtr = std::shared_ptr; + /// Array of PipelineExecutors to be cancelled when a cancelQuery is received - std::vector executors; + std::vector executors; enum QueryStreamsStatus { From fc8ea01c2b2a198d2170f8c4f9ca775f5d3a37b4 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 2 Mar 2023 18:23:31 +0100 Subject: [PATCH 207/342] use unique names and paths in test_replicated_database --- .../test_replicated_database/test.py | 249 +++++++++--------- 1 file changed, 126 insertions(+), 123 deletions(-) diff --git a/tests/integration/test_replicated_database/test.py b/tests/integration/test_replicated_database/test.py index ead9a762b1b..c71c76244ce 100644 --- a/tests/integration/test_replicated_database/test.py +++ b/tests/integration/test_replicated_database/test.py @@ -80,15 +80,15 @@ def started_cluster(): def test_create_replicated_table(started_cluster): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica' || '1');" + "CREATE DATABASE create_replicated_table ENGINE = Replicated('/test/create_replicated_table', 'shard1', 'replica' || '1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE create_replicated_table ENGINE = Replicated('/test/create_replicated_table', 'shard1', 'replica2');" ) assert ( "Explicit zookeeper_path and replica_name are specified" in main_node.query_and_get_error( - "CREATE TABLE testdb.replicated_table (d Date, k UInt64, i32 Int32) " + "CREATE TABLE create_replicated_table.replicated_table (d Date, k UInt64, i32 Int32) " "ENGINE=ReplicatedMergeTree('/test/tmp', 'r') ORDER BY k PARTITION BY toYYYYMM(d);" ) ) @@ -96,7 +96,7 @@ def test_create_replicated_table(started_cluster): assert ( "Explicit zookeeper_path and replica_name are specified" in main_node.query_and_get_error( - "CREATE TABLE testdb.replicated_table (d Date, k UInt64, i32 Int32) " + "CREATE TABLE create_replicated_table.replicated_table (d Date, k UInt64, i32 Int32) " "ENGINE=ReplicatedMergeTree('/test/tmp', 'r') ORDER BY k PARTITION BY toYYYYMM(d);" ) ) @@ -104,39 +104,39 @@ def test_create_replicated_table(started_cluster): assert ( "This syntax for *MergeTree engine is deprecated" in main_node.query_and_get_error( - "CREATE TABLE testdb.replicated_table (d Date, k UInt64, i32 Int32) " + "CREATE TABLE create_replicated_table.replicated_table (d Date, k UInt64, i32 Int32) " "ENGINE=ReplicatedMergeTree('/test/tmp/{shard}', '{replica}', d, k, 8192);" ) ) main_node.query( - "CREATE TABLE testdb.replicated_table (d Date, k UInt64, i32 Int32) ENGINE=ReplicatedMergeTree ORDER BY k PARTITION BY toYYYYMM(d);" + "CREATE TABLE create_replicated_table.replicated_table (d Date, k UInt64, i32 Int32) ENGINE=ReplicatedMergeTree ORDER BY k PARTITION BY toYYYYMM(d);" ) expected = ( - "CREATE TABLE testdb.replicated_table\\n(\\n `d` Date,\\n `k` UInt64,\\n `i32` Int32\\n)\\n" + "CREATE TABLE create_replicated_table.replicated_table\\n(\\n `d` Date,\\n `k` UInt64,\\n `i32` Int32\\n)\\n" "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\n" "PARTITION BY toYYYYMM(d)\\nORDER BY k\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, dummy_node], "testdb.replicated_table", expected) + assert_create_query([main_node, dummy_node], "create_replicated_table.replicated_table", expected) # assert without replacing uuid - assert main_node.query("show create testdb.replicated_table") == dummy_node.query( - "show create testdb.replicated_table" + assert main_node.query("show create create_replicated_table.replicated_table") == dummy_node.query( + "show create create_replicated_table.replicated_table" ) - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE create_replicated_table SYNC") + dummy_node.query("DROP DATABASE create_replicated_table SYNC") @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_simple_alter_table(started_cluster, engine): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE test_simple_alter_table ENGINE = Replicated('/test/simple_alter_table', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE test_simple_alter_table ENGINE = Replicated('/test/simple_alter_table', 'shard1', 'replica2');" ) # test_simple_alter_table - name = "testdb.alter_test_{}".format(engine) + name = "test_simple_alter_table.alter_test_{}".format(engine) main_node.query( "CREATE TABLE {} " "(CounterID UInt32, StartDate Date, UserID UInt32, VisitID UInt32, NestedColumn Nested(A UInt8, S String), ToDrop UInt32) " @@ -184,10 +184,10 @@ def test_simple_alter_table(started_cluster, engine): # test_create_replica_after_delay competing_node.query( - "CREATE DATABASE IF NOT EXISTS testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica3');" + "CREATE DATABASE IF NOT EXISTS test_simple_alter_table ENGINE = Replicated('/test/simple_alter_table', 'shard1', 'replica3');" ) - name = "testdb.alter_test_{}".format(engine) + name = "test_simple_alter_table.alter_test_{}".format(engine) main_node.query("ALTER TABLE {} ADD COLUMN Added3 UInt32;".format(name)) main_node.query("ALTER TABLE {} DROP COLUMN AddedNested1;".format(name)) main_node.query("ALTER TABLE {} RENAME COLUMN Added1 TO AddedNested1;".format(name)) @@ -207,21 +207,21 @@ def test_simple_alter_table(started_cluster, engine): ) assert_create_query([main_node, dummy_node, competing_node], name, expected) - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") - competing_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE test_simple_alter_table SYNC") + dummy_node.query("DROP DATABASE test_simple_alter_table SYNC") + competing_node.query("DROP DATABASE test_simple_alter_table SYNC") @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_delete_from_table(started_cluster, engine): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE delete_from_table ENGINE = Replicated('/test/simple_alter_table', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard2', 'replica1');" + "CREATE DATABASE delete_from_table ENGINE = Replicated('/test/simple_alter_table', 'shard2', 'replica1');" ) - name = "testdb.delete_test_{}".format(engine) + name = "delete_from_table.delete_test_{}".format(engine) main_node.query( "CREATE TABLE {} " "(id UInt64, value String) " @@ -238,7 +238,7 @@ def test_delete_from_table(started_cluster, engine): table_for_select = name if not "Replicated" in engine: - table_for_select = "cluster('testdb', {})".format(name) + table_for_select = "cluster('delete_from_table', {})".format(name) for node in [main_node, dummy_node]: assert_eq_with_retry( node, @@ -246,8 +246,8 @@ def test_delete_from_table(started_cluster, engine): expected, ) - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE delete_from_table SYNC") + dummy_node.query("DROP DATABASE delete_from_table SYNC") def get_table_uuid(database, name): @@ -276,17 +276,17 @@ def fixture_attachable_part(started_cluster): @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_alter_attach(started_cluster, attachable_part, engine): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alter_attach ENGINE = Replicated('/test/alter_attach', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE alter_attach ENGINE = Replicated('/test/alter_attach', 'shard1', 'replica2');" ) name = "alter_attach_test_{}".format(engine) main_node.query( - f"CREATE TABLE testdb.{name} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" + f"CREATE TABLE alter_attach.{name} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" ) - table_uuid = get_table_uuid("testdb", name) + table_uuid = get_table_uuid("alter_attach", name) # Provide and attach a part to the main node shutil.copytree( attachable_part, @@ -295,113 +295,113 @@ def test_alter_attach(started_cluster, attachable_part, engine): f"database/store/{table_uuid[:3]}/{table_uuid}/detached/all_1_1_0", ), ) - main_node.query(f"ALTER TABLE testdb.{name} ATTACH PART 'all_1_1_0'") + main_node.query(f"ALTER TABLE alter_attach.{name} ATTACH PART 'all_1_1_0'") # On the main node, data is attached - assert main_node.query(f"SELECT CounterID FROM testdb.{name}") == "123\n" + assert main_node.query(f"SELECT CounterID FROM alter_attach.{name}") == "123\n" # On the other node, data is replicated only if using a Replicated table engine if engine == "ReplicatedMergeTree": - assert dummy_node.query(f"SELECT CounterID FROM testdb.{name}") == "123\n" + assert dummy_node.query(f"SELECT CounterID FROM alter_attach.{name}") == "123\n" else: - assert dummy_node.query(f"SELECT CounterID FROM testdb.{name}") == "" - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + assert dummy_node.query(f"SELECT CounterID FROM alter_attach.{name}") == "" + main_node.query("DROP DATABASE alter_attach SYNC") + dummy_node.query("DROP DATABASE alter_attach SYNC") @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_alter_drop_part(started_cluster, engine): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alter_drop_part ENGINE = Replicated('/test/alter_drop_part', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE alter_drop_part ENGINE = Replicated('/test/alter_drop_part', 'shard1', 'replica2');" ) table = f"alter_drop_{engine}" part_name = "all_0_0_0" if engine == "ReplicatedMergeTree" else "all_1_1_0" main_node.query( - f"CREATE TABLE testdb.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" + f"CREATE TABLE alter_drop_part.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" ) - main_node.query(f"INSERT INTO testdb.{table} VALUES (123)") + main_node.query(f"INSERT INTO alter_drop_part.{table} VALUES (123)") if engine == "MergeTree": - dummy_node.query(f"INSERT INTO testdb.{table} VALUES (456)") - main_node.query(f"ALTER TABLE testdb.{table} DROP PART '{part_name}'") - assert main_node.query(f"SELECT CounterID FROM testdb.{table}") == "" + dummy_node.query(f"INSERT INTO alter_drop_part.{table} VALUES (456)") + main_node.query(f"ALTER TABLE alter_drop_part.{table} DROP PART '{part_name}'") + assert main_node.query(f"SELECT CounterID FROM alter_drop_part.{table}") == "" if engine == "ReplicatedMergeTree": # The DROP operation is still replicated at the table engine level - assert dummy_node.query(f"SELECT CounterID FROM testdb.{table}") == "" + assert dummy_node.query(f"SELECT CounterID FROM alter_drop_part.{table}") == "" else: - assert dummy_node.query(f"SELECT CounterID FROM testdb.{table}") == "456\n" - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + assert dummy_node.query(f"SELECT CounterID FROM alter_drop_part.{table}") == "456\n" + main_node.query("DROP DATABASE alter_drop_part SYNC") + dummy_node.query("DROP DATABASE alter_drop_part SYNC") @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_alter_detach_part(started_cluster, engine): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alter_detach_part ENGINE = Replicated('/test/alter_detach_part', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE alter_detach_part ENGINE = Replicated('/test/alter_detach_part', 'shard1', 'replica2');" ) table = f"alter_detach_{engine}" part_name = "all_0_0_0" if engine == "ReplicatedMergeTree" else "all_1_1_0" main_node.query( - f"CREATE TABLE testdb.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" + f"CREATE TABLE alter_detach_part.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" ) - main_node.query(f"INSERT INTO testdb.{table} VALUES (123)") + main_node.query(f"INSERT INTO alter_detach_part.{table} VALUES (123)") if engine == "MergeTree": - dummy_node.query(f"INSERT INTO testdb.{table} VALUES (456)") - main_node.query(f"ALTER TABLE testdb.{table} DETACH PART '{part_name}'") - detached_parts_query = f"SELECT name FROM system.detached_parts WHERE database='testdb' AND table='{table}'" + dummy_node.query(f"INSERT INTO alter_detach_part.{table} VALUES (456)") + main_node.query(f"ALTER TABLE alter_detach_part.{table} DETACH PART '{part_name}'") + detached_parts_query = f"SELECT name FROM system.detached_parts WHERE database='alter_detach_part' AND table='{table}'" assert main_node.query(detached_parts_query) == f"{part_name}\n" if engine == "ReplicatedMergeTree": # The detach operation is still replicated at the table engine level assert dummy_node.query(detached_parts_query) == f"{part_name}\n" else: assert dummy_node.query(detached_parts_query) == "" - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE alter_detach_part SYNC") + dummy_node.query("DROP DATABASE alter_detach_part SYNC") @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_alter_drop_detached_part(started_cluster, engine): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alter_drop_detached_part ENGINE = Replicated('/test/alter_drop_detached_part', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE alter_drop_detached_part ENGINE = Replicated('/test/alter_drop_detached_part', 'shard1', 'replica2');" ) table = f"alter_drop_detached_{engine}" part_name = "all_0_0_0" if engine == "ReplicatedMergeTree" else "all_1_1_0" main_node.query( - f"CREATE TABLE testdb.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" + f"CREATE TABLE alter_drop_detached_part.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" ) - main_node.query(f"INSERT INTO testdb.{table} VALUES (123)") - main_node.query(f"ALTER TABLE testdb.{table} DETACH PART '{part_name}'") + main_node.query(f"INSERT INTO alter_drop_detached_part.{table} VALUES (123)") + main_node.query(f"ALTER TABLE alter_drop_detached_part.{table} DETACH PART '{part_name}'") if engine == "MergeTree": - dummy_node.query(f"INSERT INTO testdb.{table} VALUES (456)") - dummy_node.query(f"ALTER TABLE testdb.{table} DETACH PART '{part_name}'") - main_node.query(f"ALTER TABLE testdb.{table} DROP DETACHED PART '{part_name}'") - detached_parts_query = f"SELECT name FROM system.detached_parts WHERE database='testdb' AND table='{table}'" + dummy_node.query(f"INSERT INTO alter_drop_detached_part.{table} VALUES (456)") + dummy_node.query(f"ALTER TABLE alter_drop_detached_part.{table} DETACH PART '{part_name}'") + main_node.query(f"ALTER TABLE alter_drop_detached_part.{table} DROP DETACHED PART '{part_name}'") + detached_parts_query = f"SELECT name FROM system.detached_parts WHERE database='alter_drop_detached_part' AND table='{table}'" assert main_node.query(detached_parts_query) == "" assert dummy_node.query(detached_parts_query) == f"{part_name}\n" - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE alter_drop_detached_part SYNC") + dummy_node.query("DROP DATABASE alter_drop_detached_part SYNC") @pytest.mark.parametrize("engine", ["MergeTree", "ReplicatedMergeTree"]) def test_alter_drop_partition(started_cluster, engine): main_node.query( - "CREATE DATABASE alter_drop_partition ENGINE = Replicated('/clickhouse/databases/test_alter_drop_partition', 'shard1', 'replica1');" + "CREATE DATABASE alter_drop_partition ENGINE = Replicated('/test/alter_drop_partition', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE alter_drop_partition ENGINE = Replicated('/clickhouse/databases/test_alter_drop_partition', 'shard1', 'replica2');" + "CREATE DATABASE alter_drop_partition ENGINE = Replicated('/test/alter_drop_partition', 'shard1', 'replica2');" ) snapshotting_node.query( - "CREATE DATABASE alter_drop_partition ENGINE = Replicated('/clickhouse/databases/test_alter_drop_partition', 'shard2', 'replica1');" + "CREATE DATABASE alter_drop_partition ENGINE = Replicated('/test/alter_drop_partition', 'shard2', 'replica1');" ) table = f"alter_drop_partition.alter_drop_{engine}" @@ -430,52 +430,52 @@ def test_alter_drop_partition(started_cluster, engine): def test_alter_fetch(started_cluster): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alter_fetch ENGINE = Replicated('/test/alter_fetch', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE alter_fetch ENGINE = Replicated('/test/alter_fetch', 'shard1', 'replica2');" ) main_node.query( - "CREATE TABLE testdb.fetch_source (CounterID UInt32) ENGINE = ReplicatedMergeTree ORDER BY (CounterID)" + "CREATE TABLE alter_fetch.fetch_source (CounterID UInt32) ENGINE = ReplicatedMergeTree ORDER BY (CounterID)" ) main_node.query( - "CREATE TABLE testdb.fetch_target (CounterID UInt32) ENGINE = ReplicatedMergeTree ORDER BY (CounterID)" + "CREATE TABLE alter_fetch.fetch_target (CounterID UInt32) ENGINE = ReplicatedMergeTree ORDER BY (CounterID)" ) - main_node.query("INSERT INTO testdb.fetch_source VALUES (123)") - table_uuid = get_table_uuid("testdb", "fetch_source") + main_node.query("INSERT INTO alter_fetch.fetch_source VALUES (123)") + table_uuid = get_table_uuid("alter_fetch", "fetch_source") main_node.query( - f"ALTER TABLE testdb.fetch_target FETCH PART 'all_0_0_0' FROM '/clickhouse/tables/{table_uuid}/{{shard}}' " + f"ALTER TABLE alter_fetch.fetch_target FETCH PART 'all_0_0_0' FROM '/clickhouse/tables/{table_uuid}/{{shard}}' " ) - detached_parts_query = "SELECT name FROM system.detached_parts WHERE database='testdb' AND table='fetch_target'" + detached_parts_query = "SELECT name FROM system.detached_parts WHERE database='alter_fetch' AND table='fetch_target'" assert main_node.query(detached_parts_query) == "all_0_0_0\n" assert dummy_node.query(detached_parts_query) == "" - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE alter_fetch SYNC") + dummy_node.query("DROP DATABASE alter_fetch SYNC") def test_alters_from_different_replicas(started_cluster): main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard1', 'replica2');" ) # test_alters_from_different_replicas competing_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica3');" + "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard1', 'replica3');" ) main_node.query( - "CREATE TABLE testdb.concurrent_test " + "CREATE TABLE alters_from_different_replicas.concurrent_test " "(CounterID UInt32, StartDate Date, UserID UInt32, VisitID UInt32, NestedColumn Nested(A UInt8, S String), ToDrop UInt32) " "ENGINE = MergeTree PARTITION BY toYYYYMM(StartDate) ORDER BY (CounterID, StartDate, intHash32(UserID), VisitID);" ) main_node.query( - "CREATE TABLE testdb.dist AS testdb.concurrent_test ENGINE = Distributed(testdb, testdb, concurrent_test, CounterID)" + "CREATE TABLE alters_from_different_replicas.dist AS alters_from_different_replicas.concurrent_test ENGINE = Distributed(alters_from_different_replicas, alters_from_different_replicas, concurrent_test, CounterID)" ) dummy_node.stop_clickhouse(kill=True) @@ -484,7 +484,7 @@ def test_alters_from_different_replicas(started_cluster): assert ( "There are 1 unfinished hosts (0 of them are currently active)" in competing_node.query_and_get_error( - "ALTER TABLE testdb.concurrent_test ADD COLUMN Added0 UInt32;", + "ALTER TABLE alters_from_different_replicas.concurrent_test ADD COLUMN Added0 UInt32;", settings=settings, ) ) @@ -493,7 +493,7 @@ def test_alters_from_different_replicas(started_cluster): "distributed_ddl_output_mode": "null_status_on_timeout", } assert "shard1\treplica2\tQUEUED\t" in main_node.query( - "ALTER TABLE testdb.concurrent_test ADD COLUMN Added2 UInt32;", + "ALTER TABLE alters_from_different_replicas.concurrent_test ADD COLUMN Added2 UInt32;", settings=settings, ) settings = { @@ -501,22 +501,22 @@ def test_alters_from_different_replicas(started_cluster): "distributed_ddl_output_mode": "never_throw", } assert "shard1\treplica2\tQUEUED\t" in competing_node.query( - "ALTER TABLE testdb.concurrent_test ADD COLUMN Added1 UInt32 AFTER Added0;", + "ALTER TABLE alters_from_different_replicas.concurrent_test ADD COLUMN Added1 UInt32 AFTER Added0;", settings=settings, ) dummy_node.start_clickhouse() main_node.query( - "ALTER TABLE testdb.concurrent_test ADD COLUMN AddedNested1 Nested(A UInt32, B UInt64) AFTER Added2;" + "ALTER TABLE alters_from_different_replicas.concurrent_test ADD COLUMN AddedNested1 Nested(A UInt32, B UInt64) AFTER Added2;" ) competing_node.query( - "ALTER TABLE testdb.concurrent_test ADD COLUMN AddedNested1.C Array(String) AFTER AddedNested1.B;" + "ALTER TABLE alters_from_different_replicas.concurrent_test ADD COLUMN AddedNested1.C Array(String) AFTER AddedNested1.B;" ) main_node.query( - "ALTER TABLE testdb.concurrent_test ADD COLUMN AddedNested2 Nested(A UInt32, B UInt64) AFTER AddedNested1;" + "ALTER TABLE alters_from_different_replicas.concurrent_test ADD COLUMN AddedNested2 Nested(A UInt32, B UInt64) AFTER AddedNested1;" ) expected = ( - "CREATE TABLE testdb.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" + "CREATE TABLE alters_from_different_replicas.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" " `VisitID` UInt32,\\n `NestedColumn.A` Array(UInt8),\\n `NestedColumn.S` Array(String),\\n `ToDrop` UInt32,\\n" " `Added0` UInt32,\\n `Added1` UInt32,\\n `Added2` UInt32,\\n `AddedNested1.A` Array(UInt32),\\n" " `AddedNested1.B` Array(UInt64),\\n `AddedNested1.C` Array(String),\\n `AddedNested2.A` Array(UInt32),\\n" @@ -524,51 +524,51 @@ def test_alters_from_different_replicas(started_cluster): "ENGINE = MergeTree\\nPARTITION BY toYYYYMM(StartDate)\\nORDER BY (CounterID, StartDate, intHash32(UserID), VisitID)\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, competing_node], "testdb.concurrent_test", expected) + assert_create_query([main_node, competing_node], "alters_from_different_replicas.concurrent_test", expected) # test_create_replica_after_delay - main_node.query("DROP TABLE testdb.concurrent_test SYNC") + main_node.query("DROP TABLE alters_from_different_replicas.concurrent_test SYNC") main_node.query( - "CREATE TABLE testdb.concurrent_test " + "CREATE TABLE alters_from_different_replicas.concurrent_test " "(CounterID UInt32, StartDate Date, UserID UInt32, VisitID UInt32, NestedColumn Nested(A UInt8, S String), ToDrop UInt32) " "ENGINE = ReplicatedMergeTree ORDER BY CounterID;" ) expected = ( - "CREATE TABLE testdb.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" + "CREATE TABLE alters_from_different_replicas.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" " `VisitID` UInt32,\\n `NestedColumn.A` Array(UInt8),\\n `NestedColumn.S` Array(String),\\n `ToDrop` UInt32\\n)\\n" "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\nORDER BY CounterID\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, competing_node], "testdb.concurrent_test", expected) + assert_create_query([main_node, competing_node], "alters_from_different_replicas.concurrent_test", expected) main_node.query( - "INSERT INTO testdb.dist (CounterID, StartDate, UserID) SELECT number, addDays(toDate('2020-02-02'), number), intHash32(number) FROM numbers(10)" + "INSERT INTO alters_from_different_replicas.dist (CounterID, StartDate, UserID) SELECT number, addDays(toDate('2020-02-02'), number), intHash32(number) FROM numbers(10)" ) # test_replica_restart main_node.restart_clickhouse() expected = ( - "CREATE TABLE testdb.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" + "CREATE TABLE alters_from_different_replicas.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" " `VisitID` UInt32,\\n `NestedColumn.A` Array(UInt8),\\n `NestedColumn.S` Array(String),\\n `ToDrop` UInt32\\n)\\n" "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\nORDER BY CounterID\\nSETTINGS index_granularity = 8192" ) # test_snapshot_and_snapshot_recover snapshotting_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard2', 'replica1');" + "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard2', 'replica1');" ) snapshot_recovering_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard2', 'replica2');" + "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard2', 'replica2');" ) - assert_create_query(all_nodes, "testdb.concurrent_test", expected) + assert_create_query(all_nodes, "alters_from_different_replicas.concurrent_test", expected) - main_node.query("SYSTEM FLUSH DISTRIBUTED testdb.dist") + main_node.query("SYSTEM FLUSH DISTRIBUTED alters_from_different_replicas.dist") main_node.query( - "ALTER TABLE testdb.concurrent_test UPDATE StartDate = addYears(StartDate, 1) WHERE 1" + "ALTER TABLE alters_from_different_replicas.concurrent_test UPDATE StartDate = addYears(StartDate, 1) WHERE 1" ) - res = main_node.query("ALTER TABLE testdb.concurrent_test DELETE WHERE UserID % 2") + res = main_node.query("ALTER TABLE alters_from_different_replicas.concurrent_test DELETE WHERE UserID % 2") assert ( "shard1\treplica1\tOK" in res and "shard1\treplica2\tOK" in res @@ -585,28 +585,28 @@ def test_alters_from_different_replicas(started_cluster): ) assert ( main_node.query( - "SELECT shard_num, replica_num, host_name FROM system.clusters WHERE cluster='testdb'" + "SELECT shard_num, replica_num, host_name FROM system.clusters WHERE cluster='alters_from_different_replicas'" ) == expected ) # test_drop_and_create_replica - main_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE alters_from_different_replicas SYNC") main_node.query( - "CREATE DATABASE testdb ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard1', 'replica1');" ) expected = ( - "CREATE TABLE testdb.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" + "CREATE TABLE alters_from_different_replicas.concurrent_test\\n(\\n `CounterID` UInt32,\\n `StartDate` Date,\\n `UserID` UInt32,\\n" " `VisitID` UInt32,\\n `NestedColumn.A` Array(UInt8),\\n `NestedColumn.S` Array(String),\\n `ToDrop` UInt32\\n)\\n" "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\nORDER BY CounterID\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, competing_node], "testdb.concurrent_test", expected) - assert_create_query(all_nodes, "testdb.concurrent_test", expected) + assert_create_query([main_node, competing_node], "alters_from_different_replicas.concurrent_test", expected) + assert_create_query(all_nodes, "alters_from_different_replicas.concurrent_test", expected) for node in all_nodes: - node.query("SYSTEM SYNC REPLICA testdb.concurrent_test") + node.query("SYSTEM SYNC REPLICA alters_from_different_replicas.concurrent_test") expected = ( "0\t2021-02-02\t4249604106\n" @@ -618,14 +618,14 @@ def test_alters_from_different_replicas(started_cluster): assert_eq_with_retry( dummy_node, - "SELECT CounterID, StartDate, UserID FROM testdb.dist ORDER BY CounterID", + "SELECT CounterID, StartDate, UserID FROM alters_from_different_replicas.dist ORDER BY CounterID", expected, ) - main_node.query("DROP DATABASE testdb SYNC") - dummy_node.query("DROP DATABASE testdb SYNC") - competing_node.query("DROP DATABASE testdb SYNC") - snapshotting_node.query("DROP DATABASE testdb SYNC") - snapshot_recovering_node.query("DROP DATABASE testdb SYNC") + main_node.query("DROP DATABASE alters_from_different_replicas SYNC") + dummy_node.query("DROP DATABASE alters_from_different_replicas SYNC") + competing_node.query("DROP DATABASE alters_from_different_replicas SYNC") + snapshotting_node.query("DROP DATABASE alters_from_different_replicas SYNC") + snapshot_recovering_node.query("DROP DATABASE alters_from_different_replicas SYNC") def create_some_tables(db): @@ -1063,10 +1063,10 @@ def test_server_uuid(started_cluster): def test_sync_replica(started_cluster): main_node.query( - "CREATE DATABASE test_sync_database ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica1');" + "CREATE DATABASE test_sync_database ENGINE = Replicated('/test/sync_replica', 'shard1', 'replica1');" ) dummy_node.query( - "CREATE DATABASE test_sync_database ENGINE = Replicated('/clickhouse/databases/test1', 'shard1', 'replica2');" + "CREATE DATABASE test_sync_database ENGINE = Replicated('/test/sync_replica', 'shard1', 'replica2');" ) number_of_tables = 1000 @@ -1113,17 +1113,20 @@ def test_sync_replica(started_cluster): ) lp1 = main_node.query( - "select value from system.zookeeper where path='/clickhouse/databases/test1/replicas/shard1|replica1' and name='log_ptr'" + "select value from system.zookeeper where path='/test/sync_replica/replicas/shard1|replica1' and name='log_ptr'" ) lp2 = main_node.query( - "select value from system.zookeeper where path='/clickhouse/databases/test1/replicas/shard1|replica2' and name='log_ptr'" + "select value from system.zookeeper where path='/test/sync_replica/replicas/shard1|replica2' and name='log_ptr'" ) max_lp = main_node.query( - "select value from system.zookeeper where path='/clickhouse/databases/test1/' and name='max_log_ptr'" + "select value from system.zookeeper where path='/test/sync_replica/' and name='max_log_ptr'" ) assert lp1 == max_lp assert lp2 == max_lp + main_node.query("DROP DATABASE test_sync_database SYNC") + dummy_node.query("DROP DATABASE test_sync_database SYNC") + def test_force_synchronous_settings(started_cluster): main_node.query( From 4457ebab48ab60d8cae6838a40e73f6e72c8b08e Mon Sep 17 00:00:00 2001 From: Dan Roscigno Date: Thu, 2 Mar 2023 12:31:56 -0500 Subject: [PATCH 208/342] Update docs/en/sql-reference/functions/date-time-functions.md Co-authored-by: Ilya Yatsishin <2159081+qoega@users.noreply.github.com> --- docs/en/sql-reference/functions/date-time-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/functions/date-time-functions.md b/docs/en/sql-reference/functions/date-time-functions.md index a6043d2507e..9dd6ef63ec1 100644 --- a/docs/en/sql-reference/functions/date-time-functions.md +++ b/docs/en/sql-reference/functions/date-time-functions.md @@ -1156,7 +1156,7 @@ SELECT ## toYYYYMMDDhhmmss -Converts a date or date with time to a UInt64 number containing the year and month number (YYYY \* 10000000000 + MM \* 100000000 + DD \* 1000000 + hh \* 10000 + mm \* 100 + ss). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. +Converts a date or date with time to a UInt64 number containing the year and month number (YYYY \* 10000000000 + MM \* 100000000 + DD \* 1000000 + hh \* 10000 + mm \* 100 + ss). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. ### example ```sql From fdcbec4fee7df1bb5a51249cef96964b5245c822 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Thu, 2 Mar 2023 17:35:56 +0000 Subject: [PATCH 209/342] Automatic style fix --- .../test_replicated_database/test.py | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/tests/integration/test_replicated_database/test.py b/tests/integration/test_replicated_database/test.py index c71c76244ce..2ab2fe499ff 100644 --- a/tests/integration/test_replicated_database/test.py +++ b/tests/integration/test_replicated_database/test.py @@ -118,11 +118,13 @@ def test_create_replicated_table(started_cluster): "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\n" "PARTITION BY toYYYYMM(d)\\nORDER BY k\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, dummy_node], "create_replicated_table.replicated_table", expected) - # assert without replacing uuid - assert main_node.query("show create create_replicated_table.replicated_table") == dummy_node.query( - "show create create_replicated_table.replicated_table" + assert_create_query( + [main_node, dummy_node], "create_replicated_table.replicated_table", expected ) + # assert without replacing uuid + assert main_node.query( + "show create create_replicated_table.replicated_table" + ) == dummy_node.query("show create create_replicated_table.replicated_table") main_node.query("DROP DATABASE create_replicated_table SYNC") dummy_node.query("DROP DATABASE create_replicated_table SYNC") @@ -330,7 +332,10 @@ def test_alter_drop_part(started_cluster, engine): # The DROP operation is still replicated at the table engine level assert dummy_node.query(f"SELECT CounterID FROM alter_drop_part.{table}") == "" else: - assert dummy_node.query(f"SELECT CounterID FROM alter_drop_part.{table}") == "456\n" + assert ( + dummy_node.query(f"SELECT CounterID FROM alter_drop_part.{table}") + == "456\n" + ) main_node.query("DROP DATABASE alter_drop_part SYNC") dummy_node.query("DROP DATABASE alter_drop_part SYNC") @@ -379,11 +384,17 @@ def test_alter_drop_detached_part(started_cluster, engine): f"CREATE TABLE alter_drop_detached_part.{table} (CounterID UInt32) ENGINE = {engine} ORDER BY (CounterID)" ) main_node.query(f"INSERT INTO alter_drop_detached_part.{table} VALUES (123)") - main_node.query(f"ALTER TABLE alter_drop_detached_part.{table} DETACH PART '{part_name}'") + main_node.query( + f"ALTER TABLE alter_drop_detached_part.{table} DETACH PART '{part_name}'" + ) if engine == "MergeTree": dummy_node.query(f"INSERT INTO alter_drop_detached_part.{table} VALUES (456)") - dummy_node.query(f"ALTER TABLE alter_drop_detached_part.{table} DETACH PART '{part_name}'") - main_node.query(f"ALTER TABLE alter_drop_detached_part.{table} DROP DETACHED PART '{part_name}'") + dummy_node.query( + f"ALTER TABLE alter_drop_detached_part.{table} DETACH PART '{part_name}'" + ) + main_node.query( + f"ALTER TABLE alter_drop_detached_part.{table} DROP DETACHED PART '{part_name}'" + ) detached_parts_query = f"SELECT name FROM system.detached_parts WHERE database='alter_drop_detached_part' AND table='{table}'" assert main_node.query(detached_parts_query) == "" assert dummy_node.query(detached_parts_query) == f"{part_name}\n" @@ -524,7 +535,11 @@ def test_alters_from_different_replicas(started_cluster): "ENGINE = MergeTree\\nPARTITION BY toYYYYMM(StartDate)\\nORDER BY (CounterID, StartDate, intHash32(UserID), VisitID)\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, competing_node], "alters_from_different_replicas.concurrent_test", expected) + assert_create_query( + [main_node, competing_node], + "alters_from_different_replicas.concurrent_test", + expected, + ) # test_create_replica_after_delay main_node.query("DROP TABLE alters_from_different_replicas.concurrent_test SYNC") @@ -540,7 +555,11 @@ def test_alters_from_different_replicas(started_cluster): "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\nORDER BY CounterID\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, competing_node], "alters_from_different_replicas.concurrent_test", expected) + assert_create_query( + [main_node, competing_node], + "alters_from_different_replicas.concurrent_test", + expected, + ) main_node.query( "INSERT INTO alters_from_different_replicas.dist (CounterID, StartDate, UserID) SELECT number, addDays(toDate('2020-02-02'), number), intHash32(number) FROM numbers(10)" @@ -562,13 +581,17 @@ def test_alters_from_different_replicas(started_cluster): snapshot_recovering_node.query( "CREATE DATABASE alters_from_different_replicas ENGINE = Replicated('/test/alters_from_different_replicas', 'shard2', 'replica2');" ) - assert_create_query(all_nodes, "alters_from_different_replicas.concurrent_test", expected) + assert_create_query( + all_nodes, "alters_from_different_replicas.concurrent_test", expected + ) main_node.query("SYSTEM FLUSH DISTRIBUTED alters_from_different_replicas.dist") main_node.query( "ALTER TABLE alters_from_different_replicas.concurrent_test UPDATE StartDate = addYears(StartDate, 1) WHERE 1" ) - res = main_node.query("ALTER TABLE alters_from_different_replicas.concurrent_test DELETE WHERE UserID % 2") + res = main_node.query( + "ALTER TABLE alters_from_different_replicas.concurrent_test DELETE WHERE UserID % 2" + ) assert ( "shard1\treplica1\tOK" in res and "shard1\treplica2\tOK" in res @@ -602,8 +625,14 @@ def test_alters_from_different_replicas(started_cluster): "ENGINE = ReplicatedMergeTree(\\'/clickhouse/tables/{uuid}/{shard}\\', \\'{replica}\\')\\nORDER BY CounterID\\nSETTINGS index_granularity = 8192" ) - assert_create_query([main_node, competing_node], "alters_from_different_replicas.concurrent_test", expected) - assert_create_query(all_nodes, "alters_from_different_replicas.concurrent_test", expected) + assert_create_query( + [main_node, competing_node], + "alters_from_different_replicas.concurrent_test", + expected, + ) + assert_create_query( + all_nodes, "alters_from_different_replicas.concurrent_test", expected + ) for node in all_nodes: node.query("SYSTEM SYNC REPLICA alters_from_different_replicas.concurrent_test") From 167e4903a8f5b175d587efaed15c154ee0837991 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Thu, 2 Mar 2023 18:45:59 +0100 Subject: [PATCH 210/342] Fix concrete columns PREWHERE support This is the fix for the IStorage::supportedPrewhereColumns() API. Signed-off-by: Azat Khuzhin --- src/Interpreters/InterpreterSelectQuery.cpp | 3 +-- .../MergeTree/MergeTreeWhereOptimizer.cpp | 16 ++++++++++++++++ src/Storages/MergeTree/MergeTreeWhereOptimizer.h | 3 +++ ...rge_prewhere_different_default_kind.reference | 9 +++++---- ...575_merge_prewhere_different_default_kind.sql | 11 +++++++---- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 318ea5fdf42..a4eed606de1 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -593,8 +593,6 @@ InterpreterSelectQuery::InterpreterSelectQuery( Names queried_columns = syntax_analyzer_result->requiredSourceColumns(); const auto & supported_prewhere_columns = storage->supportedPrewhereColumns(); - if (supported_prewhere_columns.has_value()) - std::erase_if(queried_columns, [&](const auto & name) { return !supported_prewhere_columns->contains(name); }); MergeTreeWhereOptimizer{ current_info, @@ -602,6 +600,7 @@ InterpreterSelectQuery::InterpreterSelectQuery( std::move(column_compressed_sizes), metadata_snapshot, queried_columns, + supported_prewhere_columns, log}; } } diff --git a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp index 3a866cc8934..fdddc29048b 100644 --- a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp +++ b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp @@ -32,10 +32,12 @@ MergeTreeWhereOptimizer::MergeTreeWhereOptimizer( std::unordered_map column_sizes_, const StorageMetadataPtr & metadata_snapshot, const Names & queried_columns_, + const std::optional & supported_columns_, Poco::Logger * log_) : table_columns{collections::map( metadata_snapshot->getColumns().getAllPhysical(), [](const NameAndTypePair & col) { return col.name; })} , queried_columns{queried_columns_} + , supported_columns{supported_columns_} , sorting_key_names{NameSet( metadata_snapshot->getSortingKey().column_names.begin(), metadata_snapshot->getSortingKey().column_names.end())} , block_with_constants{KeyCondition::getBlockWithConstants(query_info.query->clone(), query_info.syntax_analyzer_result, context)} @@ -195,6 +197,8 @@ void MergeTreeWhereOptimizer::analyzeImpl(Conditions & res, const ASTPtr & node, && (!is_final || isExpressionOverSortingKey(node)) /// Only table columns are considered. Not array joined columns. NOTE We're assuming that aliases was expanded. && isSubsetOfTableColumns(cond.identifiers) + /// Some identifiers can unable to support PREWHERE (usually because of different types in Merge engine) + && identifiersSupportsPrewhere(cond.identifiers) /// Do not move conditions involving all queried columns. && cond.identifiers.size() < queried_columns.size(); @@ -321,6 +325,18 @@ UInt64 MergeTreeWhereOptimizer::getIdentifiersColumnSize(const NameSet & identif return size; } +bool MergeTreeWhereOptimizer::identifiersSupportsPrewhere(const NameSet & identifiers) const +{ + if (!supported_columns.has_value()) + return true; + + for (const auto & identifier : identifiers) + if (!supported_columns->contains(identifier)) + return false; + + return true; +} + bool MergeTreeWhereOptimizer::isExpressionOverSortingKey(const ASTPtr & ast) const { if (const auto * func = ast->as()) diff --git a/src/Storages/MergeTree/MergeTreeWhereOptimizer.h b/src/Storages/MergeTree/MergeTreeWhereOptimizer.h index f37255bdbee..8953923542e 100644 --- a/src/Storages/MergeTree/MergeTreeWhereOptimizer.h +++ b/src/Storages/MergeTree/MergeTreeWhereOptimizer.h @@ -39,6 +39,7 @@ public: std::unordered_map column_sizes_, const StorageMetadataPtr & metadata_snapshot, const Names & queried_columns_, + const std::optional & supported_columns_, Poco::Logger * log_); private: @@ -82,6 +83,7 @@ private: void optimizeArbitrary(ASTSelectQuery & select) const; UInt64 getIdentifiersColumnSize(const NameSet & identifiers) const; + bool identifiersSupportsPrewhere(const NameSet & identifiers) const; bool isExpressionOverSortingKey(const ASTPtr & ast) const; @@ -105,6 +107,7 @@ private: const StringSet table_columns; const Names queried_columns; + const std::optional supported_columns; const NameSet sorting_key_names; const Block block_with_constants; Poco::Logger * log; diff --git a/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.reference b/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.reference index 32db2512eab..c17e235ddad 100644 --- a/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.reference +++ b/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.reference @@ -1,12 +1,13 @@ -- { echoOn } -- for pure PREWHERE it is not addressed yet. SELECT * FROM m PREWHERE a = 'OK'; -OK 0 +OK 1970-01-01 0 SELECT * FROM m PREWHERE f = 0; -- { serverError ILLEGAL_PREWHERE } SELECT * FROM m WHERE f = 0 SETTINGS optimize_move_to_prewhere=0; -OK 0 +OK 1970-01-01 0 SELECT * FROM m WHERE f = 0 SETTINGS optimize_move_to_prewhere=1; -OK 0 +OK 1970-01-01 0 -- { echoOn } SELECT * FROM m WHERE f = 0 SETTINGS optimize_move_to_prewhere=1; -OK 0 +OK 1970-01-01 0 +OK 1970-01-01 0 diff --git a/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.sql b/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.sql index 0f1d582a26e..88c7923a570 100644 --- a/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.sql +++ b/tests/queries/0_stateless/02575_merge_prewhere_different_default_kind.sql @@ -6,20 +6,22 @@ DROP TABLE IF EXISTS t2; CREATE TABLE m ( - `a` String, - `f` UInt8 + a String, + date Date, + f UInt8 ) ENGINE = Merge(currentDatabase(), '^(t1|t2)$'); CREATE TABLE t1 ( a String, + date Date, f UInt8 ALIAS 0 ) ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 8192; -INSERT INTO t1 VALUES ('OK'); +INSERT INTO t1 (a) VALUES ('OK'); -- { echoOn } -- for pure PREWHERE it is not addressed yet. @@ -32,12 +34,13 @@ SELECT * FROM m WHERE f = 0 SETTINGS optimize_move_to_prewhere=1; CREATE TABLE t2 ( a String, + date Date, f UInt8, ) ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 8192; -INSERT INTO t2 VALUES ('OK', 1); +INSERT INTO t2 (a) VALUES ('OK'); -- { echoOn } SELECT * FROM m WHERE f = 0 SETTINGS optimize_move_to_prewhere=1; From a0a40fb0575582ade22514dd9d95f20db887ae8b Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 2 Mar 2023 17:51:43 +0000 Subject: [PATCH 211/342] nam --> name --- tests/ci/compatibility_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ci/compatibility_check.py b/tests/ci/compatibility_check.py index 398f3df3deb..688fe883c1e 100644 --- a/tests/ci/compatibility_check.py +++ b/tests/ci/compatibility_check.py @@ -210,7 +210,7 @@ def main(): max_glibc_version = "" if "x86" in args.check_name: max_glibc_version = "2.4" - elif "aarch64" in args.check_nam: + elif "aarch64" in args.check_name: max_glibc_version = "2.18" # because of build with newer sysroot? else: raise Exception("Can't determine max glibc version") From 2db7df61537e1eb25f356d7d7625f2907721d257 Mon Sep 17 00:00:00 2001 From: Dan Roscigno Date: Thu, 2 Mar 2023 12:55:04 -0500 Subject: [PATCH 212/342] Update docs/en/sql-reference/functions/date-time-functions.md Co-authored-by: Ilya Yatsishin <2159081+qoega@users.noreply.github.com> --- docs/en/sql-reference/functions/date-time-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/functions/date-time-functions.md b/docs/en/sql-reference/functions/date-time-functions.md index 41e50977bba..1b75ceaa9b4 100644 --- a/docs/en/sql-reference/functions/date-time-functions.md +++ b/docs/en/sql-reference/functions/date-time-functions.md @@ -1126,7 +1126,7 @@ Rounds the time to the half hour. ## toYYYYMM -Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 100 + MM). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. +Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 100 + MM). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. ### example ```sql From 9cb70723d1afd51b57e8010720dd3f47e88e23bc Mon Sep 17 00:00:00 2001 From: Dan Roscigno Date: Thu, 2 Mar 2023 12:55:16 -0500 Subject: [PATCH 213/342] Update docs/en/sql-reference/functions/date-time-functions.md Co-authored-by: Ilya Yatsishin <2159081+qoega@users.noreply.github.com> --- docs/en/sql-reference/functions/date-time-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/functions/date-time-functions.md b/docs/en/sql-reference/functions/date-time-functions.md index 1b75ceaa9b4..0892f0d7ef7 100644 --- a/docs/en/sql-reference/functions/date-time-functions.md +++ b/docs/en/sql-reference/functions/date-time-functions.md @@ -1141,7 +1141,7 @@ SELECT ## toYYYYMMDD -Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 10000 + MM \* 100 + DD). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. +Converts a date or date with time to a UInt32 number containing the year and month number (YYYY \* 10000 + MM \* 100 + DD). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. ### example ```sql From fbabba78c905cca5b1c24ae4fdd2503f5870a146 Mon Sep 17 00:00:00 2001 From: Dan Roscigno Date: Thu, 2 Mar 2023 12:55:23 -0500 Subject: [PATCH 214/342] Update docs/en/sql-reference/functions/date-time-functions.md Co-authored-by: Ilya Yatsishin <2159081+qoega@users.noreply.github.com> --- docs/en/sql-reference/functions/date-time-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/functions/date-time-functions.md b/docs/en/sql-reference/functions/date-time-functions.md index 0892f0d7ef7..ef0475027dd 100644 --- a/docs/en/sql-reference/functions/date-time-functions.md +++ b/docs/en/sql-reference/functions/date-time-functions.md @@ -1156,7 +1156,7 @@ SELECT ## toYYYYMMDDhhmmss -Converts a date or date with time to a UInt64 number containing the year and month number (YYYY \* 10000000000 + MM \* 100000000 + DD \* 1000000 + hh \* 10000 + mm \* 100 + ss). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. +Converts a date or date with time to a UInt64 number containing the year and month number (YYYY \* 10000000000 + MM \* 100000000 + DD \* 1000000 + hh \* 10000 + mm \* 100 + ss). Accepts a second optional timezone argument. If provided, the timezone must be a string constant. ### example ```sql From a6c2473518bf78d4ccfa2f06f2621a09d3f4d092 Mon Sep 17 00:00:00 2001 From: Sema Checherinda Date: Wed, 1 Mar 2023 18:16:03 +0100 Subject: [PATCH 215/342] do flushUntrackedMemory when context swith --- src/Common/CurrentThread.cpp | 3 +-- src/Common/ThreadStatus.cpp | 6 ++++++ src/Common/ThreadStatus.h | 2 ++ src/Interpreters/ThreadStatusExt.cpp | 4 +++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Common/CurrentThread.cpp b/src/Common/CurrentThread.cpp index 526e28c043d..90483171304 100644 --- a/src/Common/CurrentThread.cpp +++ b/src/Common/CurrentThread.cpp @@ -129,8 +129,7 @@ void CurrentThread::flushUntrackedMemory() if (current_thread->untracked_memory == 0) return; - current_thread->memory_tracker.adjustWithUntrackedMemory(current_thread->untracked_memory); - current_thread->untracked_memory = 0; + current_thread->flushUntrackedMemory(); } } diff --git a/src/Common/ThreadStatus.cpp b/src/Common/ThreadStatus.cpp index 46c171b5cb6..da723f5c89d 100644 --- a/src/Common/ThreadStatus.cpp +++ b/src/Common/ThreadStatus.cpp @@ -144,6 +144,12 @@ ThreadStatus::ThreadStatus() #endif } +void ThreadStatus::flushUntrackedMemory() +{ + memory_tracker.adjustWithUntrackedMemory(untracked_memory); + untracked_memory = 0; +} + ThreadStatus::~ThreadStatus() { memory_tracker.adjustWithUntrackedMemory(untracked_memory); diff --git a/src/Common/ThreadStatus.h b/src/Common/ThreadStatus.h index 20550a63312..e620413c8eb 100644 --- a/src/Common/ThreadStatus.h +++ b/src/Common/ThreadStatus.h @@ -290,6 +290,8 @@ public: void logToQueryViewsLog(const ViewRuntimeData & vinfo); + void flushUntrackedMemory(); + protected: void applyQuerySettings(); diff --git a/src/Interpreters/ThreadStatusExt.cpp b/src/Interpreters/ThreadStatusExt.cpp index 84400fc3711..b22748e84ae 100644 --- a/src/Interpreters/ThreadStatusExt.cpp +++ b/src/Interpreters/ThreadStatusExt.cpp @@ -352,8 +352,10 @@ void ThreadStatus::detachQuery(bool exit_if_already_detached, bool thread_exits) thread_group->threads.erase(this); } performance_counters.setParent(&ProfileEvents::global_counters); - memory_tracker.reset(); + flushUntrackedMemory(); + + memory_tracker.reset(); memory_tracker.setParent(thread_group->memory_tracker.getParent()); query_id.clear(); From 7a9f4aab8ed435008e35b2cb73f634da50fb0630 Mon Sep 17 00:00:00 2001 From: Sema Checherinda Date: Thu, 2 Mar 2023 19:16:42 +0100 Subject: [PATCH 216/342] do not call adjustWithUntrackedMemory when untracked_memory is 0 --- src/Common/CurrentThread.cpp | 3 --- src/Common/ThreadStatus.cpp | 5 ++++- src/Storages/MergeTree/MergeList.cpp | 7 +++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Common/CurrentThread.cpp b/src/Common/CurrentThread.cpp index 90483171304..c10d5dbc68c 100644 --- a/src/Common/CurrentThread.cpp +++ b/src/Common/CurrentThread.cpp @@ -126,9 +126,6 @@ void CurrentThread::flushUntrackedMemory() { if (unlikely(!current_thread)) return; - if (current_thread->untracked_memory == 0) - return; - current_thread->flushUntrackedMemory(); } diff --git a/src/Common/ThreadStatus.cpp b/src/Common/ThreadStatus.cpp index da723f5c89d..407343aa268 100644 --- a/src/Common/ThreadStatus.cpp +++ b/src/Common/ThreadStatus.cpp @@ -146,13 +146,16 @@ ThreadStatus::ThreadStatus() void ThreadStatus::flushUntrackedMemory() { + if (untracked_memory == 0) + return; + memory_tracker.adjustWithUntrackedMemory(untracked_memory); untracked_memory = 0; } ThreadStatus::~ThreadStatus() { - memory_tracker.adjustWithUntrackedMemory(untracked_memory); + flushUntrackedMemory(); if (thread_group) { diff --git a/src/Storages/MergeTree/MergeList.cpp b/src/Storages/MergeTree/MergeList.cpp index 76d69cc6b7d..fa1887a02e6 100644 --- a/src/Storages/MergeTree/MergeList.cpp +++ b/src/Storages/MergeTree/MergeList.cpp @@ -144,8 +144,11 @@ MergeInfo MergeListElement::getInfo() const MergeListElement::~MergeListElement() { - CurrentThread::getMemoryTracker()->adjustWithUntrackedMemory(untracked_memory); - untracked_memory = 0; + if (untracked_memory != 0) + { + CurrentThread::getMemoryTracker()->adjustWithUntrackedMemory(untracked_memory); + untracked_memory = 0; + } } From edd238273c4d6d0a6462c6f2bc0e7d7656169bf1 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 2 Mar 2023 19:51:05 +0100 Subject: [PATCH 217/342] fix another bug in client --- src/Client/ClientBase.cpp | 4 ++-- src/Server/TCPHandler.cpp | 3 +++ .../0_stateless/02232_dist_insert_send_logs_level_hung.sh | 2 +- .../0_stateless/02434_cancel_insert_when_client_dies.sh | 3 ++- .../queries/0_stateless/02435_rollback_cancelled_queries.sh | 5 +++-- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Client/ClientBase.cpp b/src/Client/ClientBase.cpp index 96aff9aa304..53eb5080130 100644 --- a/src/Client/ClientBase.cpp +++ b/src/Client/ClientBase.cpp @@ -1360,7 +1360,7 @@ void ClientBase::sendData(Block & sample, const ColumnsDescription & columns_des throw; } - if (have_data_in_stdin) + if (have_data_in_stdin && !cancelled) sendDataFromStdin(sample, columns_description_for_query, parsed_query); } else if (parsed_insert_query->data) @@ -1370,7 +1370,7 @@ void ClientBase::sendData(Block & sample, const ColumnsDescription & columns_des try { sendDataFrom(data_in, sample, columns_description_for_query, parsed_query, have_data_in_stdin); - if (have_data_in_stdin) + if (have_data_in_stdin && !cancelled) sendDataFromStdin(sample, columns_description_for_query, parsed_query); } catch (Exception & e) diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index a1ef7a98992..768f16a09e0 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -1335,6 +1335,7 @@ bool TCPHandler::receivePacket() std::this_thread::sleep_for(ms); } + LOG_INFO(log, "Received 'Cancel' packet from the client, canceling the query"); state.is_cancelled = true; return false; @@ -1378,6 +1379,7 @@ String TCPHandler::receiveReadTaskResponseAssumeLocked() { if (packet_type == Protocol::Client::Cancel) { + LOG_INFO(log, "Received 'Cancel' packet from the client, canceling the read task"); state.is_cancelled = true; /// For testing connection collector. if (unlikely(sleep_in_receive_cancel.totalMilliseconds())) @@ -1411,6 +1413,7 @@ std::optional TCPHandler::receivePartitionMergeTreeReadTas { if (packet_type == Protocol::Client::Cancel) { + LOG_INFO(log, "Received 'Cancel' packet from the client, canceling the MergeTree read task"); state.is_cancelled = true; /// For testing connection collector. if (unlikely(sleep_in_receive_cancel.totalMilliseconds())) diff --git a/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh b/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh index 322e7e73991..5ed94148bc1 100755 --- a/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh +++ b/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh @@ -49,7 +49,7 @@ insert_client_opts=( timeout 250s $CLICKHOUSE_CLIENT "${client_opts[@]}" "${insert_client_opts[@]}" -q "insert into function remote('127.2', currentDatabase(), in_02232) select * from numbers(1e6)" # Kill underlying query of remote() to make KILL faster -timeout 30s $CLICKHOUSE_CLIENT "${client_opts[@]}" -q "KILL QUERY WHERE Settings['log_comment'] = '$CLICKHOUSE_LOG_COMMENT' SYNC" --format Null +timeout 60s $CLICKHOUSE_CLIENT "${client_opts[@]}" -q "KILL QUERY WHERE Settings['log_comment'] = '$CLICKHOUSE_LOG_COMMENT' SYNC" --format Null echo $? $CLICKHOUSE_CLIENT "${client_opts[@]}" -nm -q " diff --git a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh index c4d7367ba14..85aa992d98c 100755 --- a/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh +++ b/tests/queries/0_stateless/02434_cancel_insert_when_client_dies.sh @@ -93,5 +93,6 @@ $CLICKHOUSE_CLIENT -q 'system flush logs' # Ensure that thread_cancel actually did something $CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( - message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or + message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes', + 'Query was cancelled or a client has unexpectedly dropped the connection') or message like '%Connection reset by peer%' or message like '%Broken pipe, while writing to socket%')" diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh index 7c7ef037e02..408743d395b 100755 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -47,7 +47,7 @@ function insert_data fi if [[ "$IMPLICIT" -eq 0 ]]; then - $CLICKHOUSE_CURL -sS -d 'commit' "$CLICKHOUSE_URL&$TXN_SETTINGS&close_session=1" | grep -Faq "Transaction is not in RUNNING state" && $CLICKHOUSE_CURL -sS -d 'rollback' "$CLICKHOUSE_URL&$TXN_SETTINGS" + $CLICKHOUSE_CURL -sS -d 'commit' "$CLICKHOUSE_URL&$TXN_SETTINGS&close_session=1" fi } @@ -109,7 +109,8 @@ $CLICKHOUSE_CLIENT --implicit_transaction=1 -q 'select throwIf(count() % 1000000 # Ensure that thread_cancel actually did something $CLICKHOUSE_CLIENT -q "select count() > 0 from system.text_log where event_date >= yesterday() and query_id like '$TEST_MARK%' and ( - message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes') or + message_format_string in ('Unexpected end of file while reading chunk header of HTTP chunked data', 'Unexpected EOF, got {} of {} bytes', + 'Query was cancelled or a client has unexpectedly dropped the connection') or message like '%Connection reset by peer%' or message like '%Broken pipe, while writing to socket%')" wait_for_queries_to_finish 30 From 547319eb371a7853a5703fcb780aec4e894dfc89 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Thu, 2 Mar 2023 19:53:47 +0100 Subject: [PATCH 218/342] fix --- tests/queries/0_stateless/02435_rollback_cancelled_queries.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh index 408743d395b..e4aec6503a4 100755 --- a/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh +++ b/tests/queries/0_stateless/02435_rollback_cancelled_queries.sh @@ -62,7 +62,7 @@ function thread_insert # supress "Killed" messages from bash while true; do export ID="$TEST_MARK$RANDOM" - bash -c insert_data 2>&1| grep -Fav "Killed" + bash -c insert_data 2>&1| grep -Fav "Killed" | grep -Fav "SESSION_IS_LOCKED" | grep -Fav "SESSION_NOT_FOUND" done } From e91cdb4f8df2f9340856a71b255d5b11b095d98e Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Thu, 2 Mar 2023 19:03:17 +0000 Subject: [PATCH 219/342] Add stupid retries in clickhouse-test health check. --- tests/clickhouse-test | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/clickhouse-test b/tests/clickhouse-test index 124eed01723..7da125bcbfa 100755 --- a/tests/clickhouse-test +++ b/tests/clickhouse-test @@ -94,7 +94,7 @@ class HTTPError(Exception): # Helpers to execute queries via HTTP interface. def clickhouse_execute_http( - base_args, query, timeout=30, settings=None, default_format=None, max_http_retries=5 + base_args, query, timeout=30, settings=None, default_format=None, max_http_retries=5, retry_error_codes=False ): if args.secure: client = http.client.HTTPSConnection( @@ -131,7 +131,8 @@ def clickhouse_execute_http( ) res = client.getresponse() data = res.read() - break + if res.status == 200 or (not retry_error_codes): + break except Exception as ex: if i == max_http_retries - 1: raise ex @@ -143,8 +144,8 @@ def clickhouse_execute_http( return data -def clickhouse_execute(base_args, query, timeout=30, settings=None, max_http_retries=5): - return clickhouse_execute_http(base_args, query, timeout, settings, max_http_retries=max_http_retries).strip() +def clickhouse_execute(base_args, query, timeout=30, settings=None, max_http_retries=5, retry_error_codes=False): + return clickhouse_execute_http(base_args, query, timeout, settings, max_http_retries=max_http_retries, retry_error_codes=retry_error_codes).strip() def clickhouse_execute_json(base_args, query, timeout=60, settings=None, max_http_retries=5): @@ -1078,7 +1079,7 @@ class TestCase: @staticmethod def send_test_name_failed(suite: str, case: str): pid = os.getpid() - clickhouse_execute(args, f"SELECT 'Running test {suite}/{case} from pid={pid}'") + clickhouse_execute(args, f"SELECT 'Running test {suite}/{case} from pid={pid}'", retry_error_codes=True) def run_single_test( self, server_logs_level, client_options From c848052c7cb00e776aaec7087f735c28ac8b878e Mon Sep 17 00:00:00 2001 From: avogar Date: Thu, 2 Mar 2023 19:25:50 +0000 Subject: [PATCH 220/342] Add test, rename some tests to fix order --- ...reference => 02581_width_bucket.reference} | 0 ...idth_bucket.sql => 02581_width_bucket.sql} | 0 ...join_subquery_empty_column_list.reference} | 0 ...lyzer_join_subquery_empty_column_list.sql} | 0 ..._async_reading_with_small_limit.reference} | 0 ... 02582_async_reading_with_small_limit.sql} | 0 ...rence => 02583_map_literal_cast.reference} | 0 ...al_cast.sql => 02583_map_literal_cast.sql} | 0 ...4.reference => 02584_range_ipv4.reference} | 0 ...74_range_ipv4.sql => 02584_range_ipv4.sql} | 0 .../02585_query_status_deadlock.reference | 0 .../02585_query_status_deadlock.sh | 24 +++++++++++++++++++ 12 files changed, 24 insertions(+) rename tests/queries/0_stateless/{25337_width_bucket.reference => 02581_width_bucket.reference} (100%) rename tests/queries/0_stateless/{25337_width_bucket.sql => 02581_width_bucket.sql} (100%) rename tests/queries/0_stateless/{25339_analyzer_join_subquery_empty_column_list.reference => 02582_analyzer_join_subquery_empty_column_list.reference} (100%) rename tests/queries/0_stateless/{25339_analyzer_join_subquery_empty_column_list.sql => 02582_analyzer_join_subquery_empty_column_list.sql} (100%) rename tests/queries/0_stateless/{02664_async_reading_with_small_limit.reference => 02582_async_reading_with_small_limit.reference} (100%) rename tests/queries/0_stateless/{02664_async_reading_with_small_limit.sql => 02582_async_reading_with_small_limit.sql} (100%) rename tests/queries/0_stateless/{02670_map_literal_cast.reference => 02583_map_literal_cast.reference} (100%) rename tests/queries/0_stateless/{02670_map_literal_cast.sql => 02583_map_literal_cast.sql} (100%) rename tests/queries/0_stateless/{02674_range_ipv4.reference => 02584_range_ipv4.reference} (100%) rename tests/queries/0_stateless/{02674_range_ipv4.sql => 02584_range_ipv4.sql} (100%) create mode 100644 tests/queries/0_stateless/02585_query_status_deadlock.reference create mode 100755 tests/queries/0_stateless/02585_query_status_deadlock.sh diff --git a/tests/queries/0_stateless/25337_width_bucket.reference b/tests/queries/0_stateless/02581_width_bucket.reference similarity index 100% rename from tests/queries/0_stateless/25337_width_bucket.reference rename to tests/queries/0_stateless/02581_width_bucket.reference diff --git a/tests/queries/0_stateless/25337_width_bucket.sql b/tests/queries/0_stateless/02581_width_bucket.sql similarity index 100% rename from tests/queries/0_stateless/25337_width_bucket.sql rename to tests/queries/0_stateless/02581_width_bucket.sql diff --git a/tests/queries/0_stateless/25339_analyzer_join_subquery_empty_column_list.reference b/tests/queries/0_stateless/02582_analyzer_join_subquery_empty_column_list.reference similarity index 100% rename from tests/queries/0_stateless/25339_analyzer_join_subquery_empty_column_list.reference rename to tests/queries/0_stateless/02582_analyzer_join_subquery_empty_column_list.reference diff --git a/tests/queries/0_stateless/25339_analyzer_join_subquery_empty_column_list.sql b/tests/queries/0_stateless/02582_analyzer_join_subquery_empty_column_list.sql similarity index 100% rename from tests/queries/0_stateless/25339_analyzer_join_subquery_empty_column_list.sql rename to tests/queries/0_stateless/02582_analyzer_join_subquery_empty_column_list.sql diff --git a/tests/queries/0_stateless/02664_async_reading_with_small_limit.reference b/tests/queries/0_stateless/02582_async_reading_with_small_limit.reference similarity index 100% rename from tests/queries/0_stateless/02664_async_reading_with_small_limit.reference rename to tests/queries/0_stateless/02582_async_reading_with_small_limit.reference diff --git a/tests/queries/0_stateless/02664_async_reading_with_small_limit.sql b/tests/queries/0_stateless/02582_async_reading_with_small_limit.sql similarity index 100% rename from tests/queries/0_stateless/02664_async_reading_with_small_limit.sql rename to tests/queries/0_stateless/02582_async_reading_with_small_limit.sql diff --git a/tests/queries/0_stateless/02670_map_literal_cast.reference b/tests/queries/0_stateless/02583_map_literal_cast.reference similarity index 100% rename from tests/queries/0_stateless/02670_map_literal_cast.reference rename to tests/queries/0_stateless/02583_map_literal_cast.reference diff --git a/tests/queries/0_stateless/02670_map_literal_cast.sql b/tests/queries/0_stateless/02583_map_literal_cast.sql similarity index 100% rename from tests/queries/0_stateless/02670_map_literal_cast.sql rename to tests/queries/0_stateless/02583_map_literal_cast.sql diff --git a/tests/queries/0_stateless/02674_range_ipv4.reference b/tests/queries/0_stateless/02584_range_ipv4.reference similarity index 100% rename from tests/queries/0_stateless/02674_range_ipv4.reference rename to tests/queries/0_stateless/02584_range_ipv4.reference diff --git a/tests/queries/0_stateless/02674_range_ipv4.sql b/tests/queries/0_stateless/02584_range_ipv4.sql similarity index 100% rename from tests/queries/0_stateless/02674_range_ipv4.sql rename to tests/queries/0_stateless/02584_range_ipv4.sql diff --git a/tests/queries/0_stateless/02585_query_status_deadlock.reference b/tests/queries/0_stateless/02585_query_status_deadlock.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02585_query_status_deadlock.sh b/tests/queries/0_stateless/02585_query_status_deadlock.sh new file mode 100755 index 00000000000..92dd05ef46c --- /dev/null +++ b/tests/queries/0_stateless/02585_query_status_deadlock.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +QUERY_ID="${CLICKHOUSE_DATABASE}_test_02585_query_to_kill_id_1" + +$CLICKHOUSE_CLIENT --query_id="$QUERY_ID" -n -q " +create temporary table tmp as select * from numbers(500000000); +select * from remote('127.0.0.2', 'system.numbers_mt') where number in (select * from tmp);" &> /dev/null & + + +while true +do + res=$($CLICKHOUSE_CLIENT -q "select query, event_time from system.query_log where query_id = '$QUERY_ID' and query like 'select%' limit 1") + if [ -n "$res" ]; then + break + fi + sleep 1 +done + +$CLICKHOUSE_CLIENT -q "kill query where query_id = '$QUERY_ID' sync" &> /dev/null + From 81b30021db077be76e9244ea92e5ec87a223a515 Mon Sep 17 00:00:00 2001 From: Salvatore Mesoraca Date: Thu, 2 Mar 2023 20:48:13 +0100 Subject: [PATCH 221/342] ActionsDAG: do not change result of and() during optimization - part 2 (#47028) --- src/Interpreters/ActionsDAG.cpp | 6 +++--- .../02674_and_consistency.reference | 4 ++++ .../0_stateless/02674_and_consistency.sql | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/02674_and_consistency.reference create mode 100644 tests/queries/0_stateless/02674_and_consistency.sql diff --git a/src/Interpreters/ActionsDAG.cpp b/src/Interpreters/ActionsDAG.cpp index bfb010b6105..daba4c1608d 100644 --- a/src/Interpreters/ActionsDAG.cpp +++ b/src/Interpreters/ActionsDAG.cpp @@ -1946,7 +1946,7 @@ ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown( } auto conjunction = getConjunctionNodes(predicate, allowed_nodes); - if (conjunction.rejected.size() == 1 && WhichDataType{conjunction.rejected.front()->result_type}.isFloat()) + if (conjunction.rejected.size() == 1 && WhichDataType{removeNullable(conjunction.rejected.front()->result_type)}.isFloat()) return nullptr; auto actions = cloneActionsForConjunction(conjunction.allowed, all_inputs); @@ -2014,7 +2014,7 @@ ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown( node.children.swap(new_children); *predicate = std::move(node); } - else if (!WhichDataType{new_children.front()->result_type}.isFloat()) + else if (!WhichDataType{removeNullable(new_children.front()->result_type)}.isFloat()) { /// If type is different, cast column. /// This case is possible, cause AND can use any numeric type as argument. @@ -2045,7 +2045,7 @@ ActionsDAGPtr ActionsDAG::cloneActionsForFilterPushDown( /// remove the AND. /// Just update children and rebuild it. predicate->children.swap(new_children); - if (WhichDataType{predicate->children.front()->result_type}.isFloat()) + if (WhichDataType{removeNullable(predicate->children.front()->result_type)}.isFloat()) { Node node; node.type = ActionType::COLUMN; diff --git a/tests/queries/0_stateless/02674_and_consistency.reference b/tests/queries/0_stateless/02674_and_consistency.reference new file mode 100644 index 00000000000..e74ab1928c5 --- /dev/null +++ b/tests/queries/0_stateless/02674_and_consistency.reference @@ -0,0 +1,4 @@ +10 +#45218 +10 += diff --git a/tests/queries/0_stateless/02674_and_consistency.sql b/tests/queries/0_stateless/02674_and_consistency.sql new file mode 100644 index 00000000000..5988832ba68 --- /dev/null +++ b/tests/queries/0_stateless/02674_and_consistency.sql @@ -0,0 +1,21 @@ +SELECT SUM(number) +FROM +( + SELECT 10 AS number +) +GROUP BY number +HAVING 1 AND sin(SUMOrNull(number)) +SETTINGS enable_optimize_predicate_expression = 0; + +select '#45218'; + +SELECT SUM(number) +FROM +( + SELECT 10 AS number +) +GROUP BY cos(min2(number, number) % number) - number +HAVING ((-sign(-233841197)) IS NOT NULL) AND sin(lcm(SUM(number), SUM(number)) >= ('372497213' IS NOT NULL)) +SETTINGS aggregate_functions_null_for_empty = 1, enable_optimize_predicate_expression = 0; + +select '='; From d4c5ab9dcdcbbbbd4b0d3754385369147f2ede1a Mon Sep 17 00:00:00 2001 From: LiuNeng <1398775315@qq.com> Date: Fri, 3 Mar 2023 04:01:52 +0800 Subject: [PATCH 222/342] Optimize one nullable key aggregate performance (#45772) --- src/Common/ColumnsHashing.h | 82 +++++++++++------ src/Common/ColumnsHashingImpl.h | 49 +++++++++- src/Common/HashTable/TwoLevelStringHashMap.h | 2 +- src/Interpreters/Aggregator.cpp | 61 +++++++++--- src/Interpreters/Aggregator.h | 92 ++++++++++++++++--- src/Interpreters/JIT/compileFunction.cpp | 8 +- src/Interpreters/JIT/compileFunction.h | 2 +- tests/performance/groupby_onekey_nullable.xml | 49 ++++++++++ 8 files changed, 285 insertions(+), 60 deletions(-) create mode 100644 tests/performance/groupby_onekey_nullable.xml diff --git a/src/Common/ColumnsHashing.h b/src/Common/ColumnsHashing.h index 50db3977519..5ac6ca15812 100644 --- a/src/Common/ColumnsHashing.h +++ b/src/Common/ColumnsHashing.h @@ -16,7 +16,6 @@ #include #include - namespace DB { namespace ErrorCodes @@ -29,26 +28,42 @@ namespace ColumnsHashing /// For the case when there is one numeric key. /// UInt8/16/32/64 for any type with corresponding bit width. -template +template struct HashMethodOneNumber - : public columns_hashing_impl::HashMethodBase, Value, Mapped, use_cache, need_offset> + : public columns_hashing_impl::HashMethodBase, Value, Mapped, use_cache, need_offset, nullable> { - using Self = HashMethodOneNumber; - using Base = columns_hashing_impl::HashMethodBase; + using Self = HashMethodOneNumber; + using Base = columns_hashing_impl::HashMethodBase; static constexpr bool has_cheap_key_calculation = true; const char * vec; /// If the keys of a fixed length then key_sizes contains their lengths, empty otherwise. - HashMethodOneNumber(const ColumnRawPtrs & key_columns, const Sizes & /*key_sizes*/, const HashMethodContextPtr &) + HashMethodOneNumber(const ColumnRawPtrs & key_columns, const Sizes & /*key_sizes*/, const HashMethodContextPtr &) : Base(key_columns[0]) { - vec = key_columns[0]->getRawData().data(); + if constexpr (nullable) + { + const auto * null_column = checkAndGetColumn(key_columns[0]); + vec = null_column->getNestedColumnPtr()->getRawData().data(); + } + else + { + vec = key_columns[0]->getRawData().data(); + } } - explicit HashMethodOneNumber(const IColumn * column) + explicit HashMethodOneNumber(const IColumn * column) : Base(column) { - vec = column->getRawData().data(); + if constexpr (nullable) + { + const auto * null_column = checkAndGetColumn(column); + vec = null_column->getNestedColumnPtr()->getRawData().data(); + } + else + { + vec = column->getRawData().data(); + } } /// Creates context. Method is called once and result context is used in all threads. @@ -73,22 +88,30 @@ struct HashMethodOneNumber /// For the case when there is one string key. -template +template struct HashMethodString - : public columns_hashing_impl::HashMethodBase, Value, Mapped, use_cache, need_offset> + : public columns_hashing_impl::HashMethodBase, Value, Mapped, use_cache, need_offset, nullable> { - using Self = HashMethodString; - using Base = columns_hashing_impl::HashMethodBase; + using Self = HashMethodString; + using Base = columns_hashing_impl::HashMethodBase; static constexpr bool has_cheap_key_calculation = false; const IColumn::Offset * offsets; const UInt8 * chars; - HashMethodString(const ColumnRawPtrs & key_columns, const Sizes & /*key_sizes*/, const HashMethodContextPtr &) + HashMethodString(const ColumnRawPtrs & key_columns, const Sizes & /*key_sizes*/, const HashMethodContextPtr &) : Base(key_columns[0]) { - const IColumn & column = *key_columns[0]; - const ColumnString & column_string = assert_cast(column); + const IColumn * column; + if constexpr (nullable) + { + column = checkAndGetColumn(key_columns[0])->getNestedColumnPtr().get(); + } + else + { + column = key_columns[0]; + } + const ColumnString & column_string = assert_cast(*column); offsets = column_string.getOffsets().data(); chars = column_string.getChars().data(); } @@ -108,28 +131,35 @@ struct HashMethodString } protected: - friend class columns_hashing_impl::HashMethodBase; + friend class columns_hashing_impl::HashMethodBase; }; /// For the case when there is one fixed-length string key. -template +template struct HashMethodFixedString - : public columns_hashing_impl:: - HashMethodBase, Value, Mapped, use_cache, need_offset> + : public columns_hashing_impl::HashMethodBase, Value, Mapped, use_cache, need_offset, nullable> { - using Self = HashMethodFixedString; - using Base = columns_hashing_impl::HashMethodBase; + using Self = HashMethodFixedString; + using Base = columns_hashing_impl::HashMethodBase; static constexpr bool has_cheap_key_calculation = false; size_t n; const ColumnFixedString::Chars * chars; - HashMethodFixedString(const ColumnRawPtrs & key_columns, const Sizes & /*key_sizes*/, const HashMethodContextPtr &) + HashMethodFixedString(const ColumnRawPtrs & key_columns, const Sizes & /*key_sizes*/, const HashMethodContextPtr &) : Base(key_columns[0]) { - const IColumn & column = *key_columns[0]; - const ColumnFixedString & column_string = assert_cast(column); + const IColumn * column; + if constexpr (nullable) + { + column = checkAndGetColumn(key_columns[0])->getNestedColumnPtr().get(); + } + else + { + column = key_columns[0]; + } + const ColumnFixedString & column_string = assert_cast(*column); n = column_string.getN(); chars = &column_string.getChars(); } @@ -149,7 +179,7 @@ struct HashMethodFixedString } protected: - friend class columns_hashing_impl::HashMethodBase; + friend class columns_hashing_impl::HashMethodBase; }; diff --git a/src/Common/ColumnsHashingImpl.h b/src/Common/ColumnsHashingImpl.h index 03e7b0436e9..3240510ea9b 100644 --- a/src/Common/ColumnsHashingImpl.h +++ b/src/Common/ColumnsHashingImpl.h @@ -6,7 +6,6 @@ #include #include - namespace DB { namespace ErrorCodes @@ -140,7 +139,7 @@ public: FindResultImpl(bool found_, size_t off) : FindResultImplBase(found_), FindResultImplOffsetBase(off) {} }; -template +template class HashMethodBase { public: @@ -154,6 +153,19 @@ public: template ALWAYS_INLINE EmplaceResult emplaceKey(Data & data, size_t row, Arena & pool) { + if constexpr (nullable) + { + if (isNullAt(row)) + { + bool has_null_key = data.hasNullKeyData(); + data.hasNullKeyData() = true; + + if constexpr (has_mapped) + return EmplaceResult(data.getNullKeyData(), data.getNullKeyData(), !has_null_key); + else + return EmplaceResult(!has_null_key); + } + } auto key_holder = static_cast(*this).getKeyHolder(row, pool); return emplaceImpl(key_holder, data); } @@ -161,6 +173,16 @@ public: template ALWAYS_INLINE FindResult findKey(Data & data, size_t row, Arena & pool) { + if constexpr (nullable) + { + if (isNullAt(row)) + { + if constexpr (has_mapped) + return FindResult(&data.getNullKeyData(), data.hasNullKeyData(), 0); + else + return FindResult(data.hasNullKeyData(), 0); + } + } auto key_holder = static_cast(*this).getKeyHolder(row, pool); return findKeyImpl(keyHolderGetKey(key_holder), data); } @@ -172,10 +194,25 @@ public: return data.hash(keyHolderGetKey(key_holder)); } + ALWAYS_INLINE bool isNullAt(size_t row) const + { + if constexpr (nullable) + { + return null_map->getBool(row); + } + else + { + return false; + } + } + protected: Cache cache; + const IColumn * null_map = nullptr; + bool has_null_data = false; - HashMethodBase() + /// column argument only for nullable column + explicit HashMethodBase(const IColumn * column = nullptr) { if constexpr (consecutive_keys_optimization) { @@ -188,6 +225,11 @@ protected: else cache.value = Value(); } + if constexpr (nullable) + { + + null_map = &checkAndGetColumn(column)->getNullMapColumn(); + } } template @@ -293,7 +335,6 @@ protected: } }; - template struct MappedCache : public PaddedPODArray {}; diff --git a/src/Common/HashTable/TwoLevelStringHashMap.h b/src/Common/HashTable/TwoLevelStringHashMap.h index 6bd8f74dbd6..31b98a952c8 100644 --- a/src/Common/HashTable/TwoLevelStringHashMap.h +++ b/src/Common/HashTable/TwoLevelStringHashMap.h @@ -9,7 +9,7 @@ class TwoLevelStringHashMap : public TwoLevelStringHashTable, StringHashMap>; + using Base = TwoLevelStringHashTable, ImplTable>; using LookupResult = typename Base::LookupResult; using Base::Base; diff --git a/src/Interpreters/Aggregator.cpp b/src/Interpreters/Aggregator.cpp index 55e49eb8d19..229cb818735 100644 --- a/src/Interpreters/Aggregator.cpp +++ b/src/Interpreters/Aggregator.cpp @@ -775,6 +775,31 @@ AggregatedDataVariants::Type Aggregator::chooseAggregationMethod() if (has_nullable_key) { + /// Optimization for one key + if (params.keys_size == 1 && !has_low_cardinality) + { + if (types_removed_nullable[0]->isValueRepresentedByNumber()) + { + size_t size_of_field = types_removed_nullable[0]->getSizeOfValueInMemory(); + if (size_of_field == 1) + return AggregatedDataVariants::Type::nullable_key8; + if (size_of_field == 2) + return AggregatedDataVariants::Type::nullable_key16; + if (size_of_field == 4) + return AggregatedDataVariants::Type::nullable_key32; + if (size_of_field == 8) + return AggregatedDataVariants::Type::nullable_key64; + } + if (isFixedString(types_removed_nullable[0])) + { + return AggregatedDataVariants::Type::nullable_key_fixed_string; + } + if (isString(types_removed_nullable[0])) + { + return AggregatedDataVariants::Type::nullable_key_string; + } + } + if (params.keys_size == num_fixed_contiguous_keys && !has_low_cardinality) { /// Pack if possible all the keys along with information about which key values are nulls @@ -1889,7 +1914,7 @@ inline void Aggregator::insertAggregatesIntoColumns(Mapped & mapped, MutableColu template -Block Aggregator::insertResultsIntoColumns(PaddedPODArray & places, OutputBlockColumns && out_cols, Arena * arena) const +Block Aggregator::insertResultsIntoColumns(PaddedPODArray & places, OutputBlockColumns && out_cols, Arena * arena, bool has_null_key_data [[maybe_unused]]) const { std::exception_ptr exception; size_t aggregate_functions_destroy_index = 0; @@ -1912,8 +1937,12 @@ Block Aggregator::insertResultsIntoColumns(PaddedPODArray & pl continue; auto & final_aggregate_column = out_cols.final_aggregate_columns[i]; - final_aggregate_column = final_aggregate_column->cloneResized(places.size()); - columns_data.emplace_back(getColumnData(final_aggregate_column.get())); + /** + * In convertToBlockImplFinal, additional data with a key of null may be written, + * and additional memory for null data needs to be allocated when using the compiled function + */ + final_aggregate_column = final_aggregate_column->cloneResized(places.size() + (has_null_key_data ? 1 : 0)); + columns_data.emplace_back(getColumnData(final_aggregate_column.get(), (has_null_key_data ? 1 : 0))); } auto insert_aggregates_into_columns_function = compiled_functions.insert_aggregates_into_columns_function; @@ -1982,15 +2011,21 @@ Aggregator::convertToBlockImplFinal(Method & method, Table & data, Arena * arena std::optional out_cols; std::optional shuffled_key_sizes; PaddedPODArray places; + bool has_null_key_data = false; auto init_out_cols = [&]() { out_cols = prepareOutputBlockColumns(params, aggregate_functions, getHeader(final), aggregates_pools, final, max_block_size); - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) { + /** + * When one_key_nullable_optimization is enabled, null data will be written to the key column and result column in advance. + * And in insertResultsIntoColumns need to allocate memory for null data. + */ if (data.hasNullKeyData()) { + has_null_key_data = Method::one_key_nullable_optimization; out_cols->key_columns[0]->insertDefault(); insertAggregatesIntoColumns(data.getNullKeyData(), out_cols->final_aggregate_columns, arena); data.hasNullKeyData() = false; @@ -2022,7 +2057,7 @@ Aggregator::convertToBlockImplFinal(Method & method, Table & data, Arena * arena { if (places.size() >= max_block_size) { - res.emplace_back(insertResultsIntoColumns(places, std::move(out_cols.value()), arena)); + res.emplace_back(insertResultsIntoColumns(places, std::move(out_cols.value()), arena, has_null_key_data)); places.clear(); out_cols.reset(); } @@ -2031,12 +2066,12 @@ Aggregator::convertToBlockImplFinal(Method & method, Table & data, Arena * arena if constexpr (return_single_block) { - return insertResultsIntoColumns(places, std::move(out_cols.value()), arena); + return insertResultsIntoColumns(places, std::move(out_cols.value()), arena, has_null_key_data); } else { if (out_cols.has_value()) - res.emplace_back(insertResultsIntoColumns(places, std::move(out_cols.value()), arena)); + res.emplace_back(insertResultsIntoColumns(places, std::move(out_cols.value()), arena, has_null_key_data)); return res; } } @@ -2056,7 +2091,7 @@ Aggregator::convertToBlockImplNotFinal(Method & method, Table & data, Arenas & a { out_cols = prepareOutputBlockColumns(params, aggregate_functions, getHeader(final), aggregates_pools, final, max_block_size); - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) { if (data.hasNullKeyData()) { @@ -2397,7 +2432,7 @@ void NO_INLINE Aggregator::mergeDataNullKey( Table & table_src, Arena * arena) const { - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) { if (table_src.hasNullKeyData()) { @@ -2429,7 +2464,7 @@ void NO_INLINE Aggregator::mergeDataNullKey( template void NO_INLINE Aggregator::mergeDataImpl(Table & table_dst, Table & table_src, Arena * arena) const { - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) mergeDataNullKey(table_dst, table_src, arena); auto merge = [&](AggregateDataPtr & __restrict dst, AggregateDataPtr & __restrict src, bool inserted) @@ -2490,7 +2525,7 @@ void NO_INLINE Aggregator::mergeDataNoMoreKeysImpl( Arena * arena) const { /// Note : will create data for NULL key if not exist - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) mergeDataNullKey(table_dst, table_src, arena); table_src.mergeToViaFind(table_dst, [&](AggregateDataPtr dst, AggregateDataPtr & src, bool found) @@ -2518,7 +2553,7 @@ void NO_INLINE Aggregator::mergeDataOnlyExistingKeysImpl( Arena * arena) const { /// Note : will create data for NULL key if not exist - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) mergeDataNullKey(table_dst, table_src, arena); table_src.mergeToViaFind(table_dst, @@ -3194,7 +3229,7 @@ void NO_INLINE Aggregator::convertBlockToTwoLevelImpl( /// For every row. for (size_t i = 0; i < rows; ++i) { - if constexpr (Method::low_cardinality_optimization) + if constexpr (Method::low_cardinality_optimization || Method::one_key_nullable_optimization) { if (state.isNullAt(i)) { diff --git a/src/Interpreters/Aggregator.h b/src/Interpreters/Aggregator.h index 6e776fb9fa8..e2f76f31466 100644 --- a/src/Interpreters/Aggregator.h +++ b/src/Interpreters/Aggregator.h @@ -159,10 +159,17 @@ using StringHashTableWithNullKey = AggregationDataWithNullKey; using AggregatedDataWithNullableUInt16Key = AggregationDataWithNullKey; +using AggregatedDataWithNullableUInt32Key = AggregationDataWithNullKey; + using AggregatedDataWithNullableUInt64Key = AggregationDataWithNullKey; using AggregatedDataWithNullableStringKey = AggregationDataWithNullKey; +using AggregatedDataWithNullableShortStringKey = AggregationDataWithNullKey; + +using AggregatedDataWithNullableUInt32KeyTwoLevel = AggregationDataWithNullKeyTwoLevel< + TwoLevelHashMap, + TwoLevelHashTableGrower<>, HashTableAllocator, HashTableWithNullKey>>; using AggregatedDataWithNullableUInt64KeyTwoLevel = AggregationDataWithNullKeyTwoLevel< TwoLevelHashMap, TwoLevelHashTableGrower<>, HashTableAllocator, HashTableWithNullKey>>; @@ -174,11 +181,10 @@ using AggregatedDataWithNullableStringKeyTwoLevel = AggregationDataWithNullKeyTw TwoLevelHashMapWithSavedHash, TwoLevelHashTableGrower<>, HashTableAllocator, HashTableWithNullKey>>; - /// For the case where there is one numeric key. /// FieldType is UInt8/16/32/64 for any type with corresponding bit width. template + bool consecutive_keys_optimization = true, bool nullable = false> struct AggregationMethodOneNumber { using Data = TData; @@ -198,10 +204,11 @@ struct AggregationMethodOneNumber /// To use one `Method` in different threads, use different `State`. using State = ColumnsHashing::HashMethodOneNumber; + Mapped, FieldType, consecutive_keys_optimization, false, nullable>; /// Use optimization for low cardinality. static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = nullable; /// Shuffle key columns before `insertKeyIntoColumns` call if needed. std::optional shuffleKeyColumns(std::vector &, const Sizes &) { return {}; } @@ -209,9 +216,20 @@ struct AggregationMethodOneNumber // Insert the key from the hash table into columns. static void insertKeyIntoColumns(const Key & key, std::vector & key_columns, const Sizes & /*key_sizes*/) { + ColumnVectorHelper * column; + if constexpr (nullable) + { + ColumnNullable & nullable_col = assert_cast(*key_columns[0]); + ColumnUInt8 * null_map = assert_cast(&nullable_col.getNullMapColumn()); + null_map->insertDefault(); + column = static_cast(&nullable_col.getNestedColumn()); + } + else + { + column = static_cast(key_columns[0]); + } static_assert(sizeof(FieldType) <= sizeof(Key)); const auto * key_holder = reinterpret_cast(&key); - auto * column = static_cast(key_columns[0]); if constexpr (sizeof(FieldType) < sizeof(Key) && std::endian::native == std::endian::big) column->insertRawData(key_holder + (sizeof(Key) - sizeof(FieldType))); else @@ -242,6 +260,7 @@ struct AggregationMethodString using State = ColumnsHashing::HashMethodString; static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = false; std::optional shuffleKeyColumns(std::vector &, const Sizes &) { return {}; } @@ -253,7 +272,7 @@ struct AggregationMethodString /// Same as above but without cache -template +template struct AggregationMethodStringNoCache { using Data = TData; @@ -271,15 +290,23 @@ struct AggregationMethodStringNoCache { } - using State = ColumnsHashing::HashMethodString; + using State = ColumnsHashing::HashMethodString; static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = nullable; std::optional shuffleKeyColumns(std::vector &, const Sizes &) { return {}; } static void insertKeyIntoColumns(StringRef key, std::vector & key_columns, const Sizes &) { - static_cast(key_columns[0])->insertData(key.data, key.size); + if constexpr (nullable) + { + static_cast(key_columns[0])->insertData(key.data, key.size); + } + else + { + static_cast(key_columns[0])->insertData(key.data, key.size); + } } }; @@ -306,6 +333,7 @@ struct AggregationMethodFixedString using State = ColumnsHashing::HashMethodFixedString; static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = false; std::optional shuffleKeyColumns(std::vector &, const Sizes &) { return {}; } @@ -316,7 +344,7 @@ struct AggregationMethodFixedString }; /// Same as above but without cache -template +template struct AggregationMethodFixedStringNoCache { using Data = TData; @@ -334,15 +362,23 @@ struct AggregationMethodFixedStringNoCache { } - using State = ColumnsHashing::HashMethodFixedString; + using State = ColumnsHashing::HashMethodFixedString; static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = nullable; std::optional shuffleKeyColumns(std::vector &, const Sizes &) { return {}; } static void insertKeyIntoColumns(StringRef key, std::vector & key_columns, const Sizes &) { - static_cast(key_columns[0])->insertData(key.data, key.size); + if constexpr (nullable) + { + static_cast(key_columns[0])->insertData(key.data, key.size); + } + else + { + static_cast(key_columns[0])->insertData(key.data, key.size); + } } }; @@ -418,6 +454,7 @@ struct AggregationMethodKeysFixed use_cache>; static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = false; std::optional shuffleKeyColumns(std::vector & key_columns, const Sizes & key_sizes) { @@ -505,6 +542,7 @@ struct AggregationMethodSerialized using State = ColumnsHashing::HashMethodSerialized; static const bool low_cardinality_optimization = false; + static const bool one_key_nullable_optimization = false; std::optional shuffleKeyColumns(std::vector &, const Sizes &) { return {}; } @@ -588,6 +626,18 @@ struct AggregatedDataVariants : private boost::noncopyable std::unique_ptr> serialized_hash64; /// Support for nullable keys. + std::unique_ptr> nullable_key8; + std::unique_ptr> nullable_key16; + std::unique_ptr> nullable_key32; + std::unique_ptr> nullable_key64; + std::unique_ptr> nullable_key32_two_level; + std::unique_ptr> nullable_key64_two_level; + + std::unique_ptr> nullable_key_string; + std::unique_ptr> nullable_key_fixed_string; + std::unique_ptr> nullable_key_string_two_level; + std::unique_ptr> nullable_key_fixed_string_two_level; + std::unique_ptr> nullable_keys128; std::unique_ptr> nullable_keys256; std::unique_ptr> nullable_keys128_two_level; @@ -640,6 +690,16 @@ struct AggregatedDataVariants : private boost::noncopyable M(keys128_hash64, false) \ M(keys256_hash64, false) \ M(serialized_hash64, false) \ + M(nullable_key8, false) \ + M(nullable_key16, false) \ + M(nullable_key32, false) \ + M(nullable_key64, false) \ + M(nullable_key32_two_level, true) \ + M(nullable_key64_two_level, true) \ + M(nullable_key_string, false) \ + M(nullable_key_fixed_string, false) \ + M(nullable_key_string_two_level, true) \ + M(nullable_key_fixed_string_two_level, true) \ M(nullable_keys128, false) \ M(nullable_keys256, false) \ M(nullable_keys128_two_level, true) \ @@ -754,6 +814,10 @@ struct AggregatedDataVariants : private boost::noncopyable M(keys128) \ M(keys256) \ M(serialized) \ + M(nullable_key32) \ + M(nullable_key64) \ + M(nullable_key_string) \ + M(nullable_key_fixed_string) \ M(nullable_keys128) \ M(nullable_keys256) \ M(low_cardinality_key32) \ @@ -767,6 +831,8 @@ struct AggregatedDataVariants : private boost::noncopyable #define APPLY_FOR_VARIANTS_NOT_CONVERTIBLE_TO_TWO_LEVEL(M) \ M(key8) \ M(key16) \ + M(nullable_key8) \ + M(nullable_key16) \ M(keys16) \ M(key64_hash64) \ M(key_string_hash64)\ @@ -810,6 +876,10 @@ struct AggregatedDataVariants : private boost::noncopyable M(keys128_two_level) \ M(keys256_two_level) \ M(serialized_two_level) \ + M(nullable_key32_two_level) \ + M(nullable_key64_two_level) \ + M(nullable_key_string_two_level) \ + M(nullable_key_fixed_string_two_level) \ M(nullable_keys128_two_level) \ M(nullable_keys256_two_level) \ M(low_cardinality_key32_two_level) \ @@ -1290,7 +1360,7 @@ private: Arena * arena) const; template - Block insertResultsIntoColumns(PaddedPODArray & places, OutputBlockColumns && out_cols, Arena * arena) const; + Block insertResultsIntoColumns(PaddedPODArray & places, OutputBlockColumns && out_cols, Arena * arena, bool has_null_key_data) const; template ConvertToBlockRes diff --git a/src/Interpreters/JIT/compileFunction.cpp b/src/Interpreters/JIT/compileFunction.cpp index 8bf0eb25b60..a7233433861 100644 --- a/src/Interpreters/JIT/compileFunction.cpp +++ b/src/Interpreters/JIT/compileFunction.cpp @@ -41,7 +41,7 @@ namespace ErrorCodes extern const int LOGICAL_ERROR; } -ColumnData getColumnData(const IColumn * column) +ColumnData getColumnData(const IColumn * column, size_t skip_rows) { const bool is_const = isColumnConst(*column); @@ -52,11 +52,11 @@ ColumnData getColumnData(const IColumn * column) if (const auto * nullable = typeid_cast(column)) { - result.null_data = nullable->getNullMapColumn().getRawData().data(); + result.null_data = nullable->getNullMapColumn().getDataAt(skip_rows).data; column = &nullable->getNestedColumn(); } - - result.data = column->getRawData().data(); + /// skip null key data for one nullable key optimization + result.data = column->getDataAt(skip_rows).data; return result; } diff --git a/src/Interpreters/JIT/compileFunction.h b/src/Interpreters/JIT/compileFunction.h index 0e0a1106698..fe5abe1988c 100644 --- a/src/Interpreters/JIT/compileFunction.h +++ b/src/Interpreters/JIT/compileFunction.h @@ -26,7 +26,7 @@ struct ColumnData /** Returns ColumnData for column. * If constant column is passed, LOGICAL_ERROR will be thrown. */ -ColumnData getColumnData(const IColumn * column); +ColumnData getColumnData(const IColumn * column, size_t skip_rows = 0); using ColumnDataRowsOffset = size_t; using ColumnDataRowsSize = size_t; diff --git a/tests/performance/groupby_onekey_nullable.xml b/tests/performance/groupby_onekey_nullable.xml new file mode 100644 index 00000000000..2c03cb0861f --- /dev/null +++ b/tests/performance/groupby_onekey_nullable.xml @@ -0,0 +1,49 @@ + + + 8 + 0 + + + + + key_type + + key_string + key_string_two_level + key_fixed_string + key_int64 + key_int64_two_level + key_int32 + key_int32_two_level + Key_int16 + key_int8 + + + + + CREATE TABLE t_nullable + ( + key_string Nullable(String), + key_string_two_level Nullable(String), + key_fixed_string Nullable(FixedString(3)), + key_int64 Nullable(Int64), + key_int64_two_level Nullable(Int64), + key_int32 Nullable(Int32), + key_int32_two_level Nullable(Int32), + Key_int16 Nullable(Int16), + key_int8 Nullable(Int8), + i1 Nullable(Int64), + i2 Nullable(Int64), + ) + ENGINE = MergeTree + ORDER BY tuple() + + insert into t_nullable select ['aa','bb','cc','dd'][number % 10000 + 1], ['aa','bb','cc','dd'][number % 200000 + 1], ['aa','bb','cc','dd'][number % 4 + 1], toInt64(number%10000), toInt64(number%200000), toInt32(number%10000), toInt32(number%200000), toInt16(number%10000), toInt8(number%100), number%6000+1, number%5000+2 from numbers_mt(20000000) + insert into t_nullable select ['aa','bb','cc','dd'][number % 10000 + 1], ['aa','bb','cc','dd'][number % 200000 + 1], ['aa','bb','cc','dd'][number % 4 + 1], toInt64(number%10000), toInt64(number%200000), toInt32(number%10000), toInt32(number%200000), toInt16(number%10000), toInt8(number%100), number%6000+1, number%5000+2 from numbers_mt(20000000) + insert into t_nullable select ['aa','bb','cc','dd'][number % 10000 + 1], ['aa','bb','cc','dd'][number % 200000 + 1], ['aa','bb','cc','dd'][number % 4 + 1], toInt64(number%10000), toInt64(number%200000), toInt32(number%10000), toInt32(number%200000), toInt16(number%10000), toInt8(number%100), number%6000+1, number%5000+2 from numbers_mt(20000000) + insert into t_nullable select ['aa','bb','cc','dd'][number % 10000 + 1], ['aa','bb','cc','dd'][number % 200000 + 1], ['aa','bb','cc','dd'][number % 4 + 1], toInt64(number%10000), toInt64(number%200000), toInt32(number%10000), toInt32(number%200000), toInt16(number%10000), toInt8(number%100), number%6000+1, number%5000+2 from numbers_mt(20000000) + OPTIMIZE TABLE t_nullable FINAL + select min(i1), min(i2) from t_nullable group by {key_type} format Null + + drop table if exists t_nullable + \ No newline at end of file From 93861e04ae941d2b20b9b6f79b73e19bacec74ab Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 2 Mar 2023 20:48:25 +0000 Subject: [PATCH 223/342] Fix fix fix --- tests/ci/compatibility_check.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/ci/compatibility_check.py b/tests/ci/compatibility_check.py index 688fe883c1e..a06f4baf359 100644 --- a/tests/ci/compatibility_check.py +++ b/tests/ci/compatibility_check.py @@ -60,18 +60,20 @@ def process_glibc_check(log_path: str, max_glibc_version: str) -> TestResults: def process_result( - result_folder: str, server_log_folder: str, max_glibc_version: str + result_folder: str, server_log_folder: str, check_glibc: bool, check_distributions: bool, max_glibc_version: str ) -> Tuple[str, str, TestResults, List[str]]: glibc_log_path = os.path.join(result_folder, "glibc.log") test_results = process_glibc_check(glibc_log_path, max_glibc_version) status = "success" description = "Compatibility check passed" - if len(test_results) > 1 or test_results[0].status != "OK": - status = "failure" - description = "glibc check failed" - if status == "success": + if check_glibc: + if len(test_results) > 1 or test_results[0].status != "OK": + status = "failure" + description = "glibc check failed" + + if status == "success" and check_distributions: for operating_system in ("ubuntu:12.04", "centos:5"): test_result = process_os_check( os.path.join(result_folder, operating_system) @@ -217,7 +219,7 @@ def main(): s3_helper = S3Helper() state, description, test_results, additional_logs = process_result( - result_path, server_log_path, max_glibc_version + result_path, server_log_path, args.check_glibc, args.check_distributions, max_glibc_version ) ch_helper = ClickHouseHelper() From 6eb2877e532aeb083357dcd4a0d7477e4de357b8 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Thu, 2 Mar 2023 20:54:45 +0000 Subject: [PATCH 224/342] Automatic style fix --- tests/ci/compatibility_check.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/ci/compatibility_check.py b/tests/ci/compatibility_check.py index a06f4baf359..8841bedbb18 100644 --- a/tests/ci/compatibility_check.py +++ b/tests/ci/compatibility_check.py @@ -60,7 +60,11 @@ def process_glibc_check(log_path: str, max_glibc_version: str) -> TestResults: def process_result( - result_folder: str, server_log_folder: str, check_glibc: bool, check_distributions: bool, max_glibc_version: str + result_folder: str, + server_log_folder: str, + check_glibc: bool, + check_distributions: bool, + max_glibc_version: str, ) -> Tuple[str, str, TestResults, List[str]]: glibc_log_path = os.path.join(result_folder, "glibc.log") test_results = process_glibc_check(glibc_log_path, max_glibc_version) @@ -219,7 +223,11 @@ def main(): s3_helper = S3Helper() state, description, test_results, additional_logs = process_result( - result_path, server_log_path, args.check_glibc, args.check_distributions, max_glibc_version + result_path, + server_log_path, + args.check_glibc, + args.check_distributions, + max_glibc_version, ) ch_helper = ClickHouseHelper() From fc227d94bcaf9cd4d3583b1cb5c91c177b150cb8 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 2 Mar 2023 21:20:26 +0000 Subject: [PATCH 225/342] 02346_full_text_search.sql: Add result separators to simplify analysis --- .../02346_full_text_search.reference | 9 +++++++++ .../0_stateless/02346_full_text_search.sql | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/queries/0_stateless/02346_full_text_search.reference b/tests/queries/0_stateless/02346_full_text_search.reference index f1e21e511d0..9cd09110608 100644 --- a/tests/queries/0_stateless/02346_full_text_search.reference +++ b/tests/queries/0_stateless/02346_full_text_search.reference @@ -1,3 +1,4 @@ +Test inverted(2) af inverted 1 101 Alick a01 @@ -10,6 +11,7 @@ af inverted 113 Click b03 118 Click b08 1 +Test inverted() af inverted 101 Alick a01 106 Alick a06 @@ -21,9 +23,11 @@ af inverted 101 Alick a01 111 Alick b01 1 +Test on array columns af inverted 3 ['Click a03','Click b03'] 1 +Test on map columns af inverted 103 {'Click':'Click a03'} 108 {'Click':'Click a08'} @@ -32,20 +36,25 @@ af inverted 1 103 {'Click':'Click a03'} 1 +Test inverted(2) on a column with two parts af inverted 101 Alick a01 111 Alick b01 201 rick c01 1 +Test inverted(2) on UTF-8 data af inverted 102 clickhouse你好 1 +Test max_digestion_size_per_segment af inverted BC614E,05397FB1,6969696969898240,CF3304 1 +Test density==1 af inverted 1 1 +Test density==0.1 af inverted 1 1 diff --git a/tests/queries/0_stateless/02346_full_text_search.sql b/tests/queries/0_stateless/02346_full_text_search.sql index 2b10800e78f..ed086861f1f 100644 --- a/tests/queries/0_stateless/02346_full_text_search.sql +++ b/tests/queries/0_stateless/02346_full_text_search.sql @@ -2,7 +2,7 @@ SET allow_experimental_inverted_index = 1; SET log_queries = 1; ---------------------------------------------------- --- Test inverted(2) +SELECT 'Test inverted(2)'; DROP TABLE IF EXISTS tab; @@ -58,7 +58,7 @@ SELECT read_rows==8 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test inverted() +SELECT 'Test inverted()'; DROP TABLE IF EXISTS tab_x; @@ -111,7 +111,7 @@ SELECT read_rows==4 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test on array columns +SELECT 'Test on array columns'; DROP TABLE IF EXISTS tab; @@ -138,7 +138,7 @@ SELECT read_rows==2 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test on map columns +SELECT 'Test on map columns'; DROP TABLE IF EXISTS tab; @@ -178,7 +178,8 @@ SELECT read_rows==8 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test inverted(2) on a column with two parts +SELECT 'Test inverted(2) on a column with two parts'; + DROP TABLE IF EXISTS tab; @@ -206,7 +207,7 @@ SELECT read_rows==6 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test inverted(2) on UTF-8 data +SELECT 'Test inverted(2) on UTF-8 data'; DROP TABLE IF EXISTS tab; @@ -234,7 +235,7 @@ SELECT read_rows==2 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test max_digestion_size_per_segment +SELECT 'Test max_digestion_size_per_segment'; DROP TABLE IF EXISTS tab; @@ -265,7 +266,7 @@ SELECT read_rows==256 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test density==1 +SELECT 'Test density==1'; DROP TABLE IF EXISTS tab; @@ -294,7 +295,7 @@ SELECT read_rows==0 from system.query_log LIMIT 1; ---------------------------------------------------- --- Test density==0.1 +SELECT 'Test density==0.1'; DROP TABLE IF EXISTS tab; From 9c01c6cc252627d109809696bfa3acb584ef3979 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Thu, 2 Mar 2023 22:30:52 +0100 Subject: [PATCH 226/342] Add Cloud --- programs/server/play.html | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/programs/server/play.html b/programs/server/play.html index c511d13cf91..e4fe719ddcb 100644 --- a/programs/server/play.html +++ b/programs/server/play.html @@ -398,15 +398,30 @@ fill: var(--logo-color); } + #cloud-logo + { + color: var(--background-color); + text-shadow: 0rem 0rem 2rem var(--logo-color); + font-size: 10vw; + display: block; + } + #logo:hover { fill: var(--logo-color-active); + color: var(--logo-color-active); + } + + #cloud-logo:hover + { + filter: brightness(150%); } #logo-container { text-align: center; margin-top: 5em; + line-height: 0.75; } #chart @@ -487,6 +502,7 @@ +

From 6c68e5c4c61c991802ef0c2ebb0b77573e45813d Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 3 Mar 2023 01:04:10 +0300 Subject: [PATCH 227/342] Revert "Give users option of overwriting" --- docs/_includes/install/universal.sh | 33 +++++++++++------------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/docs/_includes/install/universal.sh b/docs/_includes/install/universal.sh index 403aab6f4e6..de34897a6f6 100755 --- a/docs/_includes/install/universal.sh +++ b/docs/_includes/install/universal.sh @@ -60,21 +60,12 @@ fi clickhouse_download_filename_prefix="clickhouse" clickhouse="$clickhouse_download_filename_prefix" -if [ -f "$clickhouse" ] -then - read -p "ClickHouse binary ${clickhouse} already exists. Overwrite? [y/N] " answer - if [ "$answer" = "y" -o "$answer" = "Y" ] - then - rm -f "$clickhouse" - else - i=0 - while [ -f "$clickhouse" ] - do - clickhouse="${clickhouse_download_filename_prefix}.${i}" - i=$(($i+1)) - done - fi -fi +i=0 +while [ -f "$clickhouse" ] +do + clickhouse="${clickhouse_download_filename_prefix}.${i}" + i=$(($i+1)) +done URL="https://builds.clickhouse.com/master/${DIR}/clickhouse" echo @@ -85,9 +76,9 @@ echo echo "Successfully downloaded the ClickHouse binary, you can run it as: ./${clickhouse}" -#if [ "${OS}" = "Linux" ] -#then - #echo - #echo "You can also install it: - #sudo ./${clickhouse} install" -#fi +if [ "${OS}" = "Linux" ] +then + echo + echo "You can also install it: + sudo ./${clickhouse} install" +fi From 1f4141a35d314a7c5cf663cbb6b80229c9dc2220 Mon Sep 17 00:00:00 2001 From: DanRoscigno Date: Sat, 25 Feb 2023 11:05:18 -0500 Subject: [PATCH 228/342] Give users option of overwriting --- docs/_includes/install/universal.sh | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/docs/_includes/install/universal.sh b/docs/_includes/install/universal.sh index de34897a6f6..1699be138c8 100755 --- a/docs/_includes/install/universal.sh +++ b/docs/_includes/install/universal.sh @@ -60,12 +60,21 @@ fi clickhouse_download_filename_prefix="clickhouse" clickhouse="$clickhouse_download_filename_prefix" -i=0 -while [ -f "$clickhouse" ] -do - clickhouse="${clickhouse_download_filename_prefix}.${i}" - i=$(($i+1)) -done +if [ -f "$clickhouse" ] +then + read -p "ClickHouse binary ${clickhouse} already exists. Overwrite? [y/N] " answer + if [ "$answer" = "y" -o "$answer" = "Y" ] + then + rm -f "$clickhouse" + else + i=0 + while [ -f "$clickhouse" ] + do + clickhouse="${clickhouse_download_filename_prefix}.${i}" + i=$(($i+1)) + done + fi +fi URL="https://builds.clickhouse.com/master/${DIR}/clickhouse" echo From 619b28206034120c7eb51b75bc6c919e8feed795 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Fri, 3 Mar 2023 01:22:04 +0100 Subject: [PATCH 229/342] more correct handling of fatal errors --- src/Common/CurrentThread.cpp | 4 ++-- src/Common/CurrentThread.h | 4 ++-- src/Common/ThreadStatus.cpp | 12 +++++++----- src/Daemon/BaseDaemon.cpp | 6 +++--- src/Interpreters/ThreadStatusExt.cpp | 7 +++++-- src/Server/GRPCServer.cpp | 3 +-- src/Server/TCPHandler.cpp | 13 +++++-------- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Common/CurrentThread.cpp b/src/Common/CurrentThread.cpp index 526e28c043d..cf4bd97490e 100644 --- a/src/Common/CurrentThread.cpp +++ b/src/Common/CurrentThread.cpp @@ -67,8 +67,8 @@ void CurrentThread::attachInternalTextLogsQueue(const std::shared_ptr callback) { - if (unlikely(!current_thread)) - return; + /// It does not make sense to set a callback for sending logs to a client if there's no thread status + chassert(current_thread); current_thread->setFatalErrorCallback(callback); } diff --git a/src/Common/CurrentThread.h b/src/Common/CurrentThread.h index f36b92e319d..ffc00c77504 100644 --- a/src/Common/CurrentThread.h +++ b/src/Common/CurrentThread.h @@ -100,8 +100,8 @@ public: /// Initializes query with current thread as master thread in constructor, and detaches it in destructor struct QueryScope : private boost::noncopyable { - explicit QueryScope(ContextMutablePtr query_context); - explicit QueryScope(ContextPtr query_context); + explicit QueryScope(ContextMutablePtr query_context, std::function fatal_error_callback = {}); + explicit QueryScope(ContextPtr query_context, std::function fatal_error_callback = {}); ~QueryScope(); void logPeakMemoryUsage(); diff --git a/src/Common/ThreadStatus.cpp b/src/Common/ThreadStatus.cpp index 46c171b5cb6..18b33cd412a 100644 --- a/src/Common/ThreadStatus.cpp +++ b/src/Common/ThreadStatus.cpp @@ -226,17 +226,19 @@ void ThreadStatus::attachInternalProfileEventsQueue(const InternalProfileEventsQ void ThreadStatus::setFatalErrorCallback(std::function callback) { - fatal_error_callback = std::move(callback); - - if (!thread_group) - return; - + /// It does not make sense to set a callback for sending logs to a client if there's no thread group + chassert(thread_group); std::lock_guard lock(thread_group->mutex); + fatal_error_callback = std::move(callback); thread_group->fatal_error_callback = fatal_error_callback; } void ThreadStatus::onFatalError() { + /// No thread group - no callback + if (!thread_group) + return; + std::lock_guard lock(thread_group->mutex); if (fatal_error_callback) fatal_error_callback(); diff --git a/src/Daemon/BaseDaemon.cpp b/src/Daemon/BaseDaemon.cpp index 60179fd5317..fbfa9e68774 100644 --- a/src/Daemon/BaseDaemon.cpp +++ b/src/Daemon/BaseDaemon.cpp @@ -134,7 +134,7 @@ static void terminateRequestedSignalHandler(int sig, siginfo_t *, void *) } -static std::atomic fatal_error_printed{false}; +static std::atomic_flag fatal_error_printed; /** Handler for "fault" or diagnostic signals. Send data about fault to separate thread to write into log. */ @@ -165,7 +165,7 @@ static void signalHandler(int sig, siginfo_t * info, void * context) for (size_t i = 0; i < 300; ++i) { /// We will synchronize with the thread printing the messages with an atomic variable to finish earlier. - if (fatal_error_printed) + if (fatal_error_printed.test()) break; /// This coarse method of synchronization is perfectly ok for fatal signals. @@ -421,7 +421,7 @@ private: if (thread_ptr) thread_ptr->onFatalError(); - fatal_error_printed = true; + fatal_error_printed.test_and_set(); } }; diff --git a/src/Interpreters/ThreadStatusExt.cpp b/src/Interpreters/ThreadStatusExt.cpp index 84400fc3711..b42f2671a5e 100644 --- a/src/Interpreters/ThreadStatusExt.cpp +++ b/src/Interpreters/ThreadStatusExt.cpp @@ -550,15 +550,16 @@ void CurrentThread::detachQueryIfNotDetached() } -CurrentThread::QueryScope::QueryScope(ContextMutablePtr query_context) +CurrentThread::QueryScope::QueryScope(ContextMutablePtr query_context, std::function fatal_error_callback) { CurrentThread::initializeQuery(); CurrentThread::attachQueryContext(query_context); if (!query_context->hasQueryContext()) query_context->makeQueryContext(); + setFatalErrorCallback(fatal_error_callback); } -CurrentThread::QueryScope::QueryScope(ContextPtr query_context) +CurrentThread::QueryScope::QueryScope(ContextPtr query_context, std::function fatal_error_callback) { if (!query_context->hasQueryContext()) throw Exception( @@ -566,6 +567,7 @@ CurrentThread::QueryScope::QueryScope(ContextPtr query_context) CurrentThread::initializeQuery(); CurrentThread::attachQueryContext(query_context); + setFatalErrorCallback(fatal_error_callback); } void CurrentThread::QueryScope::logPeakMemoryUsage() @@ -585,6 +587,7 @@ CurrentThread::QueryScope::~QueryScope() if (log_peak_memory_usage_in_destructor) logPeakMemoryUsage(); + setFatalErrorCallback({}); CurrentThread::detachQueryIfNotDetached(); } catch (...) diff --git a/src/Server/GRPCServer.cpp b/src/Server/GRPCServer.cpp index 595f5a8c2b7..533c3d0959b 100644 --- a/src/Server/GRPCServer.cpp +++ b/src/Server/GRPCServer.cpp @@ -836,7 +836,7 @@ namespace query_context->applySettingsChanges(settings_changes); query_context->setCurrentQueryId(query_info.query_id()); - query_scope.emplace(query_context); + query_scope.emplace(query_context, /* fatal_error_callback */ [this]{ onFatalError(); }); /// Set up tracing context for this query on current thread thread_trace_context = std::make_unique("GRPCServer", @@ -854,7 +854,6 @@ namespace logs_queue->max_priority = Poco::Logger::parseLevel(client_logs_level.toString()); logs_queue->setSourceRegexp(settings.send_logs_source_regexp); CurrentThread::attachInternalTextLogsQueue(logs_queue, client_logs_level); - CurrentThread::setFatalErrorCallback([this]{ onFatalError(); }); } /// Set the current database if specified. diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index a307b472a64..cac7160b070 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -277,7 +277,11 @@ void TCPHandler::runImpl() query_context->getSettingsRef(), query_context->getOpenTelemetrySpanLog()); - query_scope.emplace(query_context); + query_scope.emplace(query_context, /* fatal_error_callback */ [this] + { + std::lock_guard lock(fatal_error_mutex); + sendLogs(); + }); /// If query received, then settings in query_context has been updated. /// So it's better to update the connection settings for flexibility. @@ -298,11 +302,6 @@ void TCPHandler::runImpl() state.logs_queue->max_priority = Poco::Logger::parseLevel(client_logs_level.toString()); state.logs_queue->setSourceRegexp(query_context->getSettingsRef().send_logs_source_regexp); CurrentThread::attachInternalTextLogsQueue(state.logs_queue, client_logs_level); - CurrentThread::setFatalErrorCallback([this] - { - std::lock_guard lock(fatal_error_mutex); - sendLogs(); - }); } if (client_tcp_protocol_version >= DBMS_MIN_PROTOCOL_VERSION_WITH_INCREMENTAL_PROFILE_EVENTS) { @@ -611,8 +610,6 @@ void TCPHandler::runImpl() /// It is important to destroy query context here. We do not want it to live arbitrarily longer than the query. query_context.reset(); - CurrentThread::setFatalErrorCallback({}); - if (is_interserver_mode) { /// We don't really have session in interserver mode, new one is created for each query. It's better to reset it now. From 29e396cc30e219815e574105bb170505443a09bb Mon Sep 17 00:00:00 2001 From: houbaron Date: Fri, 3 Mar 2023 11:57:03 +0800 Subject: [PATCH 230/342] Update skipping-indexes.md --- docs/zh/guides/improving-query-performance/skipping-indexes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/guides/improving-query-performance/skipping-indexes.md b/docs/zh/guides/improving-query-performance/skipping-indexes.md index 2f9ce09d77f..f5889898c2c 100644 --- a/docs/zh/guides/improving-query-performance/skipping-indexes.md +++ b/docs/zh/guides/improving-query-performance/skipping-indexes.md @@ -1,6 +1,6 @@ --- slug: /zh/guides/improving-query-performance/skipping-indexes -sidebar_label: Data Skipping Indexes +sidebar_label: 跳数索引 sidebar_position: 2 --- From 1f7a0c70f0442feb1ea3ac1593a4d699893de6bc Mon Sep 17 00:00:00 2001 From: houbaron Date: Fri, 3 Mar 2023 12:00:42 +0800 Subject: [PATCH 231/342] Update sparse-primary-indexes.md --- .../improving-query-performance/sparse-primary-indexes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/guides/improving-query-performance/sparse-primary-indexes.md b/docs/zh/guides/improving-query-performance/sparse-primary-indexes.md index e773a02fbc3..18b23a79f86 100644 --- a/docs/zh/guides/improving-query-performance/sparse-primary-indexes.md +++ b/docs/zh/guides/improving-query-performance/sparse-primary-indexes.md @@ -1,6 +1,6 @@ --- slug: /zh/guides/improving-query-performance/sparse-primary-indexes -sidebar_label: Sparse Primary Indexes +sidebar_label: 主键稀疏索引 sidebar_position: 20 --- From df8d36470830f0f140c01a3d13c203b8eb7eeddf Mon Sep 17 00:00:00 2001 From: houbaron Date: Fri, 3 Mar 2023 14:24:36 +0800 Subject: [PATCH 232/342] Update backup.md --- docs/zh/operations/backup.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/zh/operations/backup.md b/docs/zh/operations/backup.md index e0c5174a9a4..04bb09c60a6 100644 --- a/docs/zh/operations/backup.md +++ b/docs/zh/operations/backup.md @@ -12,8 +12,9 @@ sidebar_label: "\u6570\u636E\u5907\u4EFD" 不同公司有不同的可用资源和业务需求,因此不存在一个通用的解决方案可以应对各种情况下的ClickHouse备份和恢复。 适用于 1GB 数据的方案可能并不适用于几十 PB 数据的情况。 有多种具备各自优缺点的可能方法,将在下面对其进行讨论。最好使用几种方法而不是仅仅使用一种方法来弥补它们的各种缺点。。 -!!! note "注" - 需要注意的是,如果您备份了某些内容并且从未尝试过还原它,那么当您实际需要它时可能无法正常恢复(或者至少需要的时间比业务能够容忍的时间更长)。 因此,无论您选择哪种备份方法,请确保自动还原过程,并定期在备用ClickHouse群集上演练。 +:::node +需要注意的是,如果您备份了某些内容并且从未尝试过还原它,那么当您实际需要它时可能无法正常恢复(或者至少需要的时间比业务能够容忍的时间更长)。 因此,无论您选择哪种备份方法,请确保自动还原过程,并定期在备用ClickHouse群集上演练。 +::: ## 将源数据复制到其它地方 {#duplicating-source-data-somewhere-else} From 9aef18abadda4f9395e1bcb2c732571a22a9bc0c Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Fri, 3 Mar 2023 08:35:21 +0000 Subject: [PATCH 233/342] Doc update for mapFromArrays() --- .../functions/tuple-map-functions.md | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/en/sql-reference/functions/tuple-map-functions.md b/docs/en/sql-reference/functions/tuple-map-functions.md index 28f2f930d90..34c865e7752 100644 --- a/docs/en/sql-reference/functions/tuple-map-functions.md +++ b/docs/en/sql-reference/functions/tuple-map-functions.md @@ -68,7 +68,9 @@ Result: ## mapFromArrays -mapFromArrays merges an array of keys and an array of values into a Map. +Merges an [Array](../../sql-reference/data-types/array.md) of keys and an [Array](../../sql-reference/data-types/array.md) of values into a [Map(key, value)](../../sql-reference/data-types/map.md). + +The function is a more convenient alternative to `CAST((key_array, value_array), 'Map(key_type, value_type)')`. For example, instead of writing `CAST((['aa', 'bb'], [4, 5]), 'Map(String, UInt32)')`, you can write `mapFromArrays(['aa', 'bb'], [4, 5])`. **Syntax** @@ -78,7 +80,7 @@ mapFromArrays(keys, values) Alias: `MAP_FROM_ARRAYS(keys, values)` -**Parameters** +**Arguments** - `keys` — Given key array to create a map from. The nested type of array must be: [String](../../sql-reference/data-types/string.md), [Integer](../../sql-reference/data-types/int-uint.md), [LowCardinality](../../sql-reference/data-types/lowcardinality.md), [FixedString](../../sql-reference/data-types/fixedstring.md), [UUID](../../sql-reference/data-types/uuid.md), [Date](../../sql-reference/data-types/date.md), [DateTime](../../sql-reference/data-types/datetime.md), [Date32](../../sql-reference/data-types/date32.md), [Enum](../../sql-reference/data-types/enum.md) - `values` - Given value array to create a map from. @@ -269,7 +271,7 @@ Determines whether the `map` contains the `key` parameter. mapContains(map, key) ``` -**Parameters** +**Arguments** - `map` — Map. [Map](../../sql-reference/data-types/map.md). - `key` — Key. Type matches the type of keys of `map` parameter. @@ -314,7 +316,7 @@ Can be optimized by enabling the [optimize_functions_to_subcolumns](../../operat mapKeys(map) ``` -**Parameters** +**Arguments** - `map` — Map. [Map](../../sql-reference/data-types/map.md). @@ -357,7 +359,7 @@ Can be optimized by enabling the [optimize_functions_to_subcolumns](../../operat mapValues(map) ``` -**Parameters** +**Arguments** - `map` — Map. [Map](../../sql-reference/data-types/map.md). @@ -396,7 +398,7 @@ Result: mapContainsKeyLike(map, pattern) ``` -**Parameters** +**Arguments** - `map` — Map. [Map](../../sql-reference/data-types/map.md). - `pattern` - String pattern to match. @@ -434,7 +436,7 @@ Result: mapExtractKeyLike(map, pattern) ``` -**Parameters** +**Arguments** - `map` — Map. [Map](../../sql-reference/data-types/map.md). - `pattern` - String pattern to match. @@ -463,8 +465,6 @@ Result: │ {} │ └────────────────────────────┘ ``` - - ## mapApply @@ -474,7 +474,7 @@ Result: mapApply(func, map) ``` -**Parameters** +**Arguments** - `func` - [Lambda function](../../sql-reference/functions/index.md#higher-order-functions---operator-and-lambdaparams-expr-function). - `map` — [Map](../../sql-reference/data-types/map.md). @@ -514,7 +514,7 @@ Result: mapFilter(func, map) ``` -**Parameters** +**Arguments** - `func` - [Lambda function](../../sql-reference/functions/index.md#higher-order-functions---operator-and-lambdaparams-expr-function). - `map` — [Map](../../sql-reference/data-types/map.md). @@ -556,7 +556,7 @@ Result: mapUpdate(map1, map2) ``` -**Parameters** +**Arguments** - `map1` [Map](../../sql-reference/data-types/map.md). - `map2` [Map](../../sql-reference/data-types/map.md). From a1d99c602b50877d322b5efa411c641317f9a7c2 Mon Sep 17 00:00:00 2001 From: Sergei Trifonov Date: Fri, 3 Mar 2023 10:52:45 +0100 Subject: [PATCH 234/342] Update docs/zh/operations/backup.md --- docs/zh/operations/backup.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/operations/backup.md b/docs/zh/operations/backup.md index 04bb09c60a6..6d491f9c2f7 100644 --- a/docs/zh/operations/backup.md +++ b/docs/zh/operations/backup.md @@ -12,7 +12,7 @@ sidebar_label: "\u6570\u636E\u5907\u4EFD" 不同公司有不同的可用资源和业务需求,因此不存在一个通用的解决方案可以应对各种情况下的ClickHouse备份和恢复。 适用于 1GB 数据的方案可能并不适用于几十 PB 数据的情况。 有多种具备各自优缺点的可能方法,将在下面对其进行讨论。最好使用几种方法而不是仅仅使用一种方法来弥补它们的各种缺点。。 -:::node +:::note 需要注意的是,如果您备份了某些内容并且从未尝试过还原它,那么当您实际需要它时可能无法正常恢复(或者至少需要的时间比业务能够容忍的时间更长)。 因此,无论您选择哪种备份方法,请确保自动还原过程,并定期在备用ClickHouse群集上演练。 ::: From 38d157a850769ff88a040b1f802258f8a9d532c8 Mon Sep 17 00:00:00 2001 From: vdimir Date: Thu, 2 Mar 2023 17:50:26 +0000 Subject: [PATCH 235/342] Fix race in grace hash join with limit --- src/Interpreters/GraceHashJoin.cpp | 4 +++- src/Interpreters/GraceHashJoin.h | 2 +- .../25340_grace_hash_limit_race.reference | 0 .../0_stateless/25340_grace_hash_limit_race.sql | 15 +++++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/25340_grace_hash_limit_race.reference create mode 100644 tests/queries/0_stateless/25340_grace_hash_limit_race.sql diff --git a/src/Interpreters/GraceHashJoin.cpp b/src/Interpreters/GraceHashJoin.cpp index 79a825a752f..1b62939ac7b 100644 --- a/src/Interpreters/GraceHashJoin.cpp +++ b/src/Interpreters/GraceHashJoin.cpp @@ -410,6 +410,8 @@ void GraceHashJoin::initialize(const Block & sample_block) void GraceHashJoin::joinBlock(Block & block, std::shared_ptr & not_processed) { + std::shared_lock current_bucket_lock(current_bucket_mutex); + if (block.rows() == 0) { hash_join->joinBlock(block, not_processed); @@ -549,7 +551,7 @@ public: IBlocksStreamPtr GraceHashJoin::getDelayedBlocks() { - std::lock_guard current_bucket_lock(current_bucket_mutex); + std::unique_lock current_bucket_lock(current_bucket_mutex); if (current_bucket == nullptr) return nullptr; diff --git a/src/Interpreters/GraceHashJoin.h b/src/Interpreters/GraceHashJoin.h index 4f7694e2f07..3a0b45370ea 100644 --- a/src/Interpreters/GraceHashJoin.h +++ b/src/Interpreters/GraceHashJoin.h @@ -139,7 +139,7 @@ private: mutable SharedMutex rehash_mutex; FileBucket * current_bucket = nullptr; - mutable std::mutex current_bucket_mutex; + mutable SharedMutex current_bucket_mutex; InMemoryJoinPtr hash_join; Block hash_join_sample_block; diff --git a/tests/queries/0_stateless/25340_grace_hash_limit_race.reference b/tests/queries/0_stateless/25340_grace_hash_limit_race.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/25340_grace_hash_limit_race.sql b/tests/queries/0_stateless/25340_grace_hash_limit_race.sql new file mode 100644 index 00000000000..322c808c94d --- /dev/null +++ b/tests/queries/0_stateless/25340_grace_hash_limit_race.sql @@ -0,0 +1,15 @@ +DROP TABLE IF EXISTS test_grace_hash; + +CREATE TABLE test_grace_hash (id UInt32, value UInt64) ENGINE = MergeTree ORDER BY id; + +INSERT INTO test_grace_hash SELECT number, number % 100 = 0 FROM numbers(100000); + +SET join_algorithm = 'grace_hash'; + +SELECT * FROM ( + SELECT f.id FROM test_grace_hash AS f + LEFT JOIN test_grace_hash AS d + ON f.id = d.id + LIMIT 1000 +) FORMAT Null; + From f06910da98cfc4aafe3bc86876975a86ee3cc760 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 3 Mar 2023 00:47:57 +0300 Subject: [PATCH 236/342] Update 25340_grace_hash_limit_race.sql --- tests/queries/0_stateless/25340_grace_hash_limit_race.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/25340_grace_hash_limit_race.sql b/tests/queries/0_stateless/25340_grace_hash_limit_race.sql index 322c808c94d..b12dd7bdbba 100644 --- a/tests/queries/0_stateless/25340_grace_hash_limit_race.sql +++ b/tests/queries/0_stateless/25340_grace_hash_limit_race.sql @@ -13,3 +13,4 @@ SELECT * FROM ( LIMIT 1000 ) FORMAT Null; +DROP TABLE test_grace_hash; From db2ffc109ccae0118679896cf7c0e9a1f90aad35 Mon Sep 17 00:00:00 2001 From: vdimir Date: Fri, 3 Mar 2023 10:17:34 +0000 Subject: [PATCH 237/342] Add comment to GrachHashJoin::current_bucket_mutex --- src/Interpreters/GraceHashJoin.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Interpreters/GraceHashJoin.h b/src/Interpreters/GraceHashJoin.h index 3a0b45370ea..0d6c4741b95 100644 --- a/src/Interpreters/GraceHashJoin.h +++ b/src/Interpreters/GraceHashJoin.h @@ -139,6 +139,12 @@ private: mutable SharedMutex rehash_mutex; FileBucket * current_bucket = nullptr; + + /* Function `getDelayedBlocks` should be a critical section. + * Also some `joinBlock` calls may be in progress and we need to wait for them to finish, + * because they may may use `hash_join`, but `getDelayedBlocks` switches it to another bucket. + * So, `joinBlock` acquires shared lock and getDelayedBlocks acquires exclusive lock. + */ mutable SharedMutex current_bucket_mutex; InMemoryJoinPtr hash_join; From b96682ce32ef307c66172e70fb1f7c5093691e0c Mon Sep 17 00:00:00 2001 From: serxa Date: Fri, 3 Mar 2023 10:35:07 +0000 Subject: [PATCH 238/342] Fix markup in docs --- .../zh/engines/database-engines/replicated.md | 27 +++++++++--------- .../mergetree-family/mergetree.md | 14 ++++++---- .../table-engines/special/distributed.md | 28 ++++++++++--------- docs/zh/getting-started/tutorial.md | 5 ++-- docs/zh/interfaces/formats.md | 12 ++++---- docs/zh/interfaces/http.md | 10 ++++--- docs/zh/interfaces/third-party/index.md | 3 +- docs/zh/operations/access-rights.md | 6 ++-- docs/zh/operations/backup.md | 5 ++-- .../settings.md | 15 ++++++---- .../operations/settings/settings-profiles.md | 5 ++-- docs/zh/operations/settings/settings-users.md | 5 ++-- docs/zh/operations/settings/settings.md | 5 ++-- docs/zh/operations/system-tables/parts.md | 5 ++-- docs/zh/operations/system-tables/query_log.md | 5 ++-- .../aggregate-functions/reference/corr.md | 5 ++-- .../aggregate-functions/reference/covarpop.md | 5 ++-- .../reference/covarsamp.md | 5 ++-- .../reference/quantiletiming.md | 10 ++++--- .../reference/quantiletimingweighted.md | 10 ++++--- .../reference/stddevpop.md | 5 ++-- .../reference/stddevsamp.md | 5 ++-- .../reference/uniqcombined.md | 5 ++-- .../aggregate-functions/reference/varpop.md | 5 ++-- .../aggregate-functions/reference/varsamp.md | 5 ++-- docs/zh/sql-reference/ansi.md | 5 ++-- .../data-types/simpleaggregatefunction.md | 8 +++--- .../functions/string-search-functions.md | 25 ++++++++++------- .../sql-reference/statements/alter/delete.md | 5 ++-- .../sql-reference/statements/alter/index.md | 5 ++-- .../statements/alter/order-by.md | 5 ++-- .../sql-reference/statements/alter/setting.md | 5 ++-- .../sql-reference/statements/alter/update.md | 5 ++-- docs/zh/sql-reference/statements/exchange.md | 3 +- docs/zh/sql-reference/statements/rename.md | 3 +- .../statements/select/group-by.md | 5 ++-- .../sql-reference/statements/select/join.md | 10 ++++--- .../statements/select/limit-by.md | 5 ++-- .../sql-reference/statements/select/sample.md | 5 ++-- .../sql-reference/statements/select/where.md | 7 +++-- docs/zh/sql-reference/statements/system.md | 7 ++--- .../zh/sql-reference/table-functions/mysql.md | 5 ++-- 42 files changed, 186 insertions(+), 137 deletions(-) diff --git a/docs/zh/engines/database-engines/replicated.md b/docs/zh/engines/database-engines/replicated.md index df5872e9b19..843d7a3edd5 100644 --- a/docs/zh/engines/database-engines/replicated.md +++ b/docs/zh/engines/database-engines/replicated.md @@ -19,7 +19,6 @@ CREATE DATABASE testdb ENGINE = Replicated('zoo_path', 'shard_name', 'replica_na - `shard_name` — 分片的名字。数据库副本按`shard_name`分组到分片中。 - `replica_name` — 副本的名字。同一分片的所有副本的副本名称必须不同。 -!!! note "警告" 对于[ReplicatedMergeTree](../table-engines/mergetree-family/replication.md#table_engines-replication)表,如果没有提供参数,则使用默认参数:`/clickhouse/tables/{uuid}/{shard}`和`{replica}`。这些可以在服务器设置[default_replica_path](../../operations/server-configuration-parameters/settings.md#default_replica_path)和[default_replica_name](../../operations/server-configuration-parameters/settings.md#default_replica_name)中更改。宏`{uuid}`被展开到表的uuid, `{shard}`和`{replica}`被展开到服务器配置的值,而不是数据库引擎参数。但是在将来,可以使用Replicated数据库的`shard_name`和`replica_name`。 ## 使用方式 {#specifics-and-recommendations} @@ -52,8 +51,8 @@ CREATE TABLE r.rmt (n UInt64) ENGINE=ReplicatedMergeTree ORDER BY n; ``` ``` text -┌─────hosts────────────┬──status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐ -│ shard1|replica1 │ 0 │ │ 2 │ 0 │ +┌─────hosts────────────┬──status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐ +│ shard1|replica1 │ 0 │ │ 2 │ 0 │ │ shard1|other_replica │ 0 │ │ 1 │ 0 │ │ other_shard|r1 │ 0 │ │ 0 │ 0 │ └──────────────────────┴─────────┴───────┴─────────────────────┴──────────────────┘ @@ -62,13 +61,13 @@ CREATE TABLE r.rmt (n UInt64) ENGINE=ReplicatedMergeTree ORDER BY n; 显示系统表: ``` sql -SELECT cluster, shard_num, replica_num, host_name, host_address, port, is_local +SELECT cluster, shard_num, replica_num, host_name, host_address, port, is_local FROM system.clusters WHERE cluster='r'; ``` ``` text -┌─cluster─┬─shard_num─┬─replica_num─┬─host_name─┬─host_address─┬─port─┬─is_local─┐ -│ r │ 1 │ 1 │ node3 │ 127.0.0.1 │ 9002 │ 0 │ +┌─cluster─┬─shard_num─┬─replica_num─┬─host_name─┬─host_address─┬─port─┬─is_local─┐ +│ r │ 1 │ 1 │ node3 │ 127.0.0.1 │ 9002 │ 0 │ │ r │ 2 │ 1 │ node2 │ 127.0.0.1 │ 9001 │ 0 │ │ r │ 2 │ 2 │ node1 │ 127.0.0.1 │ 9000 │ 1 │ └─────────┴───────────┴─────────────┴───────────┴──────────────┴──────┴──────────┘ @@ -83,9 +82,9 @@ node1 :) SELECT materialize(hostName()) AS host, groupArray(n) FROM r.d GROUP BY ``` ``` text -┌─hosts─┬─groupArray(n)─┐ -│ node1 │ [1,3,5,7,9] │ -│ node2 │ [0,2,4,6,8] │ +┌─hosts─┬─groupArray(n)─┐ +│ node1 │ [1,3,5,7,9] │ +│ node2 │ [0,2,4,6,8] │ └───────┴───────────────┘ ``` @@ -98,8 +97,8 @@ node4 :) CREATE DATABASE r ENGINE=Replicated('some/path/r','other_shard','r2'); 集群配置如下所示: ``` text -┌─cluster─┬─shard_num─┬─replica_num─┬─host_name─┬─host_address─┬─port─┬─is_local─┐ -│ r │ 1 │ 1 │ node3 │ 127.0.0.1 │ 9002 │ 0 │ +┌─cluster─┬─shard_num─┬─replica_num─┬─host_name─┬─host_address─┬─port─┬─is_local─┐ +│ r │ 1 │ 1 │ node3 │ 127.0.0.1 │ 9002 │ 0 │ │ r │ 1 │ 2 │ node4 │ 127.0.0.1 │ 9003 │ 0 │ │ r │ 2 │ 1 │ node2 │ 127.0.0.1 │ 9001 │ 0 │ │ r │ 2 │ 2 │ node1 │ 127.0.0.1 │ 9000 │ 1 │ @@ -113,8 +112,8 @@ node2 :) SELECT materialize(hostName()) AS host, groupArray(n) FROM r.d GROUP BY ``` ```text -┌─hosts─┬─groupArray(n)─┐ -│ node2 │ [1,3,5,7,9] │ -│ node4 │ [0,2,4,6,8] │ +┌─hosts─┬─groupArray(n)─┐ +│ node2 │ [1,3,5,7,9] │ +│ node4 │ [0,2,4,6,8] │ └───────┴───────────────┘ ``` diff --git a/docs/zh/engines/table-engines/mergetree-family/mergetree.md b/docs/zh/engines/table-engines/mergetree-family/mergetree.md index 40aa764e2d3..1fcf64fcd25 100644 --- a/docs/zh/engines/table-engines/mergetree-family/mergetree.md +++ b/docs/zh/engines/table-engines/mergetree-family/mergetree.md @@ -1,6 +1,6 @@ ---- -slug: /zh/engines/table-engines/mergetree-family/mergetree ---- +--- +slug: /zh/engines/table-engines/mergetree-family/mergetree +--- # MergeTree {#table_engines-mergetree} Clickhouse 中最强大的表引擎当属 `MergeTree` (合并树)引擎及该系列(`*MergeTree`)中的其他引擎。 @@ -25,8 +25,9 @@ Clickhouse 中最强大的表引擎当属 `MergeTree` (合并树)引擎及 需要的话,您可以给表设置一个采样方法。 -!!! note "注意" - [合并](../special/merge.md#merge) 引擎并不属于 `*MergeTree` 系列。 +:::info +[合并](../special/merge.md#merge) 引擎并不属于 `*MergeTree` 系列。 +::: ## 建表 {#table_engine-mergetree-creating-a-table} @@ -364,7 +365,7 @@ WHERE 子句中的条件可以包含对某列数据进行运算的函数表达 常量参数小于 ngram 大小的函数不能使用 `ngrambf_v1` 进行查询优化。 -!!! note "注意" +:::note 布隆过滤器可能会包含不符合条件的匹配,所以 `ngrambf_v1`, `tokenbf_v1` 和 `bloom_filter` 索引不能用于结果返回为假的函数,例如: - 可以用来优化的场景 @@ -379,6 +380,7 @@ WHERE 子句中的条件可以包含对某列数据进行运算的函数表达 - `NOT s = 1` - `s != 1` - `NOT startsWith(s, 'test')` +::: ## 并发数据访问 {#concurrent-data-access} diff --git a/docs/zh/engines/table-engines/special/distributed.md b/docs/zh/engines/table-engines/special/distributed.md index c832e9e19ba..3c8a7a833d0 100644 --- a/docs/zh/engines/table-engines/special/distributed.md +++ b/docs/zh/engines/table-engines/special/distributed.md @@ -45,7 +45,7 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster] AS [db2.]name2 - [insert_distributed_sync](../../../operations/settings/settings.md#insert_distributed_sync) 设置 - [MergeTree](../../../engines/table-engines/mergetree-family/mergetree.md#table_engine-mergetree-multiple-volumes) 查看示例 - + **分布式设置** - `fsync_after_insert` - 对异步插入到分布式的文件数据执行`fsync`。确保操作系统将所有插入的数据刷新到启动节点**磁盘上的一个文件**中。 @@ -66,19 +66,20 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster] AS [db2.]name2 - `monitor_max_sleep_time_ms` - 等同于 [distributed_directory_monitor_max_sleep_time_ms](../../../operations/settings/settings.md#distributed_directory_monitor_max_sleep_time_ms) -!!! note "备注" +::note +**稳定性设置** (`fsync_...`): - **稳定性设置** (`fsync_...`): +- 只影响异步插入(例如:`insert_distributed_sync=false`), 当数据首先存储在启动节点磁盘上,然后再异步发送到shard。 +— 可能会显著降低`insert`的性能 +- 影响将存储在分布式表文件夹中的数据写入 **接受您插入的节点** 。如果你需要保证写入数据到底层的MergeTree表中,请参阅 `system.merge_tree_settings` 中的持久性设置(`...fsync...`) - - 只影响异步插入(例如:`insert_distributed_sync=false`), 当数据首先存储在启动节点磁盘上,然后再异步发送到shard。 - — 可能会显著降低`insert`的性能 - - 影响将存储在分布式表文件夹中的数据写入 **接受您插入的节点** 。如果你需要保证写入数据到底层的MergeTree表中,请参阅 `system.merge_tree_settings` 中的持久性设置(`...fsync...`) +**插入限制设置** (`..._insert`) 请见: - **插入限制设置** (`..._insert`) 请见: +- [insert_distributed_sync](../../../operations/settings/settings.md#insert_distributed_sync) 设置 +- [prefer_localhost_replica](../../../operations/settings/settings.md#settings-prefer-localhost-replica) 设置 +- `bytes_to_throw_insert` 在 `bytes_to_delay_insert` 之前处理,所以你不应该设置它的值小于 `bytes_to_delay_insert` +::: - - [insert_distributed_sync](../../../operations/settings/settings.md#insert_distributed_sync) 设置 - - [prefer_localhost_replica](../../../operations/settings/settings.md#settings-prefer-localhost-replica) 设置 - - `bytes_to_throw_insert` 在 `bytes_to_delay_insert` 之前处理,所以你不应该设置它的值小于 `bytes_to_delay_insert` **示例** ``` sql @@ -214,7 +215,7 @@ SELECT 查询会被发送到所有分片,并且无论数据在分片中如何 ## 读取数据 {#distributed-reading-data} - + 当查询一个`Distributed`表时,`SELECT`查询被发送到所有的分片,不管数据是如何分布在分片上的(它们可以完全随机分布)。当您添加一个新分片时,您不必将旧数据传输到它。相反,您可以使用更重的权重向其写入新数据——数据的分布会稍微不均匀,但查询将正确有效地工作。 当启用`max_parallel_replicas`选项时,查询处理将在单个分片中的所有副本之间并行化。更多信息,请参见[max_parallel_replicas](../../../operations/settings/settings.md#settings-max_parallel_replicas)。 @@ -225,8 +226,9 @@ SELECT 查询会被发送到所有分片,并且无论数据在分片中如何 - `_shard_num` — 表`system.clusters` 中的 `shard_num` 值 . 数据类型: [UInt32](../../../sql-reference/data-types/int-uint.md). -!!! note "备注" - 因为 [remote](../../../sql-reference/table-functions/remote.md) 和 [cluster](../../../sql-reference/table-functions/cluster.mdx) 表方法内部创建了分布式表, `_shard_num` 对他们都有效. +:::note +因为 [remote](../../../sql-reference/table-functions/remote.md) 和 [cluster](../../../sql-reference/table-functions/cluster.mdx) 表方法内部创建了分布式表, `_shard_num` 对他们都有效. +::: **详见** - [虚拟列](../../../engines/table-engines/index.md#table_engines-virtual_columns) 描述 diff --git a/docs/zh/getting-started/tutorial.md b/docs/zh/getting-started/tutorial.md index bef3ecee35f..989cf5f57d8 100644 --- a/docs/zh/getting-started/tutorial.md +++ b/docs/zh/getting-started/tutorial.md @@ -617,8 +617,9 @@ INSERT INTO tutorial.hits_all SELECT * FROM tutorial.hits_v1; 启用本机复制[Zookeeper](http://zookeeper.apache.org/)是必需的。 ClickHouse负责所有副本的数据一致性,并在失败后自动运行恢复过程。建议将ZooKeeper集群部署在单独的服务器上(其中没有其他进程,包括运行的ClickHouse)。 -!!! note "注意" - ZooKeeper不是一个严格的要求:在某些简单的情况下,您可以通过将数据写入应用程序代码中的所有副本来复制数据。 这种方法是**不**建议的,在这种情况下,ClickHouse将无法保证所有副本上的数据一致性。 因此需要由您的应用来保证这一点。 +:::note +ZooKeeper不是一个严格的要求:在某些简单的情况下,您可以通过将数据写入应用程序代码中的所有副本来复制数据。 这种方法是**不**建议的,在这种情况下,ClickHouse将无法保证所有副本上的数据一致性。 因此需要由您的应用来保证这一点。 +::: ZooKeeper位置在配置文件中指定: diff --git a/docs/zh/interfaces/formats.md b/docs/zh/interfaces/formats.md index 852b327366b..fd3cf743818 100644 --- a/docs/zh/interfaces/formats.md +++ b/docs/zh/interfaces/formats.md @@ -685,8 +685,9 @@ CREATE TABLE IF NOT EXISTS example_table - 如果`input_format_defaults_for_omitted_fields = 0`, 那么`x`和`a`的默认值等于`0`(作为`UInt32`数据类型的默认值)。 - 如果`input_format_defaults_for_omitted_fields = 1`, 那么`x`的默认值为`0`,但`a`的默认值为`x * 2`。 -!!! note "注意" +:::warning 当使用`input_format_defaults_for_omitted_fields = 1`插入数据时,与使用`input_format_defaults_for_omitted_fields = 0`相比,ClickHouse消耗更多的计算资源。 +::: ### Selecting Data {#selecting-data} @@ -708,8 +709,9 @@ CREATE TABLE IF NOT EXISTS example_table 与[JSON](#json)格式不同,没有替换无效的UTF-8序列。值以与`JSON`相同的方式转义。 -!!! note "提示" +:::info 字符串中可以输出任意一组字节。如果您确信表中的数据可以被格式化为JSON而不会丢失任何信息,那么就使用`JSONEachRow`格式。 +::: ### Nested Structures {#jsoneachrow-nested} @@ -1216,9 +1218,9 @@ SET format_avro_schema_registry_url = 'http://schema-registry'; SELECT * FROM topic1_stream; ``` -!!! note "警告" - 设置 `format_avro_schema_registry_url` 需要写入配置文件`users.xml`以在Clickhouse重启后,该设置仍为您的设定值。您也可以在使用Kafka引擎的时候指定该设置。 - +:::warning +设置 `format_avro_schema_registry_url` 需要写入配置文件`users.xml`以在Clickhouse重启后,该设置仍为您的设定值。您也可以在使用Kafka引擎的时候指定该设置。 +::: ## Parquet {#data-format-parquet} diff --git a/docs/zh/interfaces/http.md b/docs/zh/interfaces/http.md index 11754ed3e2f..e0c12193a6a 100644 --- a/docs/zh/interfaces/http.md +++ b/docs/zh/interfaces/http.md @@ -188,8 +188,9 @@ $ curl -vsS "http://localhost:8123/?enable_http_compression=1" -d 'SELECT number $ echo "SELECT 1" | gzip -c | curl -sS --data-binary @- -H 'Content-Encoding: gzip' 'http://localhost:8123/' ``` -!!! note "警告" - 一些HTTP客户端可能会在默认情况下从服务器解压数据(使用`gzip`和`deflate`),即使您未正确地使用了压缩设置,您也可能会得到解压数据。 +:::warning +一些HTTP客户端可能会在默认情况下从服务器解压数据(使用`gzip`和`deflate`),即使您未正确地使用了压缩设置,您也可能会得到解压数据。 +::: 您可以使用`database`URL参数或`X-ClickHouse-Database`头来指定默认数据库。 @@ -447,8 +448,9 @@ $ curl -H 'XXX:TEST_HEADER_VALUE' -H 'PARAMS_XXX:max_threads' 'http://localhost: max_final_threads 2 ``` -!!! note "警告" - 在一个`predefined_query_handler`中,只支持insert类型的一个`查询`。 +:::warning +在一个`predefined_query_handler`中,只支持insert类型的一个`查询`。 +::: ### 动态查询 {#dynamic_query_handler} diff --git a/docs/zh/interfaces/third-party/index.md b/docs/zh/interfaces/third-party/index.md index 0c6b194d04a..64c0b30a9c0 100644 --- a/docs/zh/interfaces/third-party/index.md +++ b/docs/zh/interfaces/third-party/index.md @@ -13,5 +13,6 @@ sidebar_position: 24 - [GUI](../../interfaces/third-party/gui.md) - [Proxies](../../interfaces/third-party/proxy.md) -!!! note "注意" +:::note 支持通用API的通用工具[ODBC](../../interfaces/odbc.md)或[JDBC](../../interfaces/jdbc.md),通常也适用于ClickHouse,但这里没有列出,因为它们实在太多了。 +::: diff --git a/docs/zh/operations/access-rights.md b/docs/zh/operations/access-rights.md index 713cf991f47..8d640ef32c0 100644 --- a/docs/zh/operations/access-rights.md +++ b/docs/zh/operations/access-rights.md @@ -24,9 +24,9 @@ ClickHouse权限实体包括: 我们建议你使用SQL工作流的方式。当然配置的方式也可以同时起作用, 所以如果你正在用服务端配置的方式来管理权限和账户,你可以平滑的切换到SQL驱动的工作流方式。 -!!! note "警告" - 你无法同时使用两个配置的方式来管理同一个权限实体。 - +:::warning +你无法同时使用两个配置的方式来管理同一个权限实体。 +::: ## 用法 {#access-control-usage} diff --git a/docs/zh/operations/backup.md b/docs/zh/operations/backup.md index e0c5174a9a4..6d491f9c2f7 100644 --- a/docs/zh/operations/backup.md +++ b/docs/zh/operations/backup.md @@ -12,8 +12,9 @@ sidebar_label: "\u6570\u636E\u5907\u4EFD" 不同公司有不同的可用资源和业务需求,因此不存在一个通用的解决方案可以应对各种情况下的ClickHouse备份和恢复。 适用于 1GB 数据的方案可能并不适用于几十 PB 数据的情况。 有多种具备各自优缺点的可能方法,将在下面对其进行讨论。最好使用几种方法而不是仅仅使用一种方法来弥补它们的各种缺点。。 -!!! note "注" - 需要注意的是,如果您备份了某些内容并且从未尝试过还原它,那么当您实际需要它时可能无法正常恢复(或者至少需要的时间比业务能够容忍的时间更长)。 因此,无论您选择哪种备份方法,请确保自动还原过程,并定期在备用ClickHouse群集上演练。 +:::note +需要注意的是,如果您备份了某些内容并且从未尝试过还原它,那么当您实际需要它时可能无法正常恢复(或者至少需要的时间比业务能够容忍的时间更长)。 因此,无论您选择哪种备份方法,请确保自动还原过程,并定期在备用ClickHouse群集上演练。 +::: ## 将源数据复制到其它地方 {#duplicating-source-data-somewhere-else} diff --git a/docs/zh/operations/server-configuration-parameters/settings.md b/docs/zh/operations/server-configuration-parameters/settings.md index 89ae411b9e0..2fd04b1260a 100644 --- a/docs/zh/operations/server-configuration-parameters/settings.md +++ b/docs/zh/operations/server-configuration-parameters/settings.md @@ -528,8 +528,9 @@ SSL客户端/服务器配置。 包含数据的目录的路径。 -!!! note "注" - 尾部斜杠是强制性的。 +:::note +尾部斜杠是强制性的。 +::: **示例** @@ -714,8 +715,9 @@ TCP端口,用于与客户端进行安全通信。 使用它与 [OpenSSL](#serv 用于处理大型查询的临时数据的路径。 -!!! note "注" - 尾部斜杠是强制性的。 +:::note +尾部斜杠是强制性的。 +::: **示例** @@ -728,11 +730,12 @@ TCP端口,用于与客户端进行安全通信。 使用它与 [OpenSSL](#serv 从政策 [`storage_configuration`](../../engines/table-engines/mergetree-family/mergetree.md#table_engine-mergetree-multiple-volumes) 存储临时文件。 如果没有设置 [`tmp_path`](#server-settings-tmp_path) 被使用,否则被忽略。 -!!! note "注" - - `move_factor` 被忽略 +:::note +- `move_factor` 被忽略 - `keep_free_space_bytes` 被忽略 - `max_data_part_size_bytes` 被忽略 -您必须在该政策中只有一个卷 +::: ## uncompressed_cache_size {#server-settings-uncompressed_cache_size} diff --git a/docs/zh/operations/settings/settings-profiles.md b/docs/zh/operations/settings/settings-profiles.md index 1ad394950bf..5051276607f 100644 --- a/docs/zh/operations/settings/settings-profiles.md +++ b/docs/zh/operations/settings/settings-profiles.md @@ -8,8 +8,9 @@ sidebar_label: "\u8BBE\u7F6E\u914D\u7F6E" 设置配置是设置的集合,并按照相同的名称进行分组。 -!!! note "信息" - ClickHouse 还支持用 [SQL驱动的工作流](../../operations/access-rights.md#access-control) 管理设置配置。我们建议使用它。 +:::info +ClickHouse 还支持用 [SQL驱动的工作流](../../operations/access-rights.md#access-control) 管理设置配置。我们建议使用它。 +::: 设置配置可以任意命名。你可以为不同的用户指定相同的设置配置。您可以在设置配置中写入的最重要的内容是 `readonly=1`,这将确保只读访问。 diff --git a/docs/zh/operations/settings/settings-users.md b/docs/zh/operations/settings/settings-users.md index de4aa27df69..3fb97bbddb2 100644 --- a/docs/zh/operations/settings/settings-users.md +++ b/docs/zh/operations/settings/settings-users.md @@ -10,8 +10,9 @@ sidebar_label: "\u7528\u6237\u8BBE\u7F6E" `user.xml` 中的 `users` 配置段包含了用户配置 -!!! note "提示" - ClickHouse还支持 [SQL驱动的工作流](../access-rights.md#access-control) 用于管理用户。 我们建议使用它。 +:::note +ClickHouse还支持 [SQL驱动的工作流](../access-rights.md#access-control) 用于管理用户。 我们建议使用它。 +::: `users` 配置段的结构: diff --git a/docs/zh/operations/settings/settings.md b/docs/zh/operations/settings/settings.md index 4107a499463..457b208602f 100644 --- a/docs/zh/operations/settings/settings.md +++ b/docs/zh/operations/settings/settings.md @@ -266,8 +266,9 @@ INSERT INTO test VALUES (lower('Hello')), (lower('world')), (lower('INSERT')), ( 执行时 `INSERT` 查询时,将省略的输入列值替换为相应列的默认值。 此选项仅适用于 [JSONEachRow](../../interfaces/formats.md#jsoneachrow), [CSV](../../interfaces/formats.md#csv) 和 [TabSeparated](../../interfaces/formats.md#tabseparated) 格式。 -!!! note "注" - 启用此选项后,扩展表元数据将从服务器发送到客户端。 它会消耗服务器上的额外计算资源,并可能降低性能。 +:::note +启用此选项后,扩展表元数据将从服务器发送到客户端。 它会消耗服务器上的额外计算资源,并可能降低性能。 +::: 可能的值: diff --git a/docs/zh/operations/system-tables/parts.md b/docs/zh/operations/system-tables/parts.md index 0bd728f543f..0ebac3944ff 100644 --- a/docs/zh/operations/system-tables/parts.md +++ b/docs/zh/operations/system-tables/parts.md @@ -99,8 +99,9 @@ slug: /zh/operations/system-tables/parts - `move_ttl_info.expression` ([Array](../../sql-reference/data-types/array.md)([String](../../sql-reference/data-types/string.md))) — 表达式的数组。 每个表达式定义一个 [TTL MOVE 规则](../../engines/table-engines/mergetree-family/mergetree.md#table_engine-mergetree-ttl). - !!! note "警告" - 保留 `move_ttl_info.expression` 数组主要是为了向后兼容,现在检查 `TTL MOVE` 规则最简单的方法是使用 `move_ttl_info.min` 和 `move_ttl_info.max` 字段。 +:::warning +保留 `move_ttl_info.expression` 数组主要是为了向后兼容,现在检查 `TTL MOVE` 规则最简单的方法是使用 `move_ttl_info.min` 和 `move_ttl_info.max` 字段。 +::: - `move_ttl_info.min` ([Array](../../sql-reference/data-types/array.md)([DateTime](../../sql-reference/data-types/datetime.md))) — 日期值和时间值的数组。数组中的每个元素都描述了一个 [TTL MOVE rule](../../engines/table-engines/mergetree-family/mergetree.md#table_engine-mergetree-ttl) 的最小键值。 diff --git a/docs/zh/operations/system-tables/query_log.md b/docs/zh/operations/system-tables/query_log.md index 93e5771d4b5..7149282dfcc 100644 --- a/docs/zh/operations/system-tables/query_log.md +++ b/docs/zh/operations/system-tables/query_log.md @@ -8,8 +8,9 @@ machine_translated_rev: 5decc73b5dc60054f19087d3690c4eb99446a6c3 包含已执行查询的相关信息,例如:开始时间、处理持续时间、错误消息。 -!!! note "注" - 此表不包含以下内容的摄取数据 `INSERT` 查询。 +:::note +此表不包含以下内容的摄取数据 `INSERT` 查询。 +::: 您可以更改query_log的设置,在服务器配置的 [query_log](../../operations/server-configuration-parameters/settings.md#server_configuration_parameters-query-log) 部分。 diff --git a/docs/zh/sql-reference/aggregate-functions/reference/corr.md b/docs/zh/sql-reference/aggregate-functions/reference/corr.md index 01a89e428ab..48b5bf904f5 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/corr.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/corr.md @@ -12,5 +12,6 @@ sidebar_position: 107 计算Pearson相关系数: `Σ((x - x̅)(y - y̅)) / sqrt(Σ((x - x̅)^2) * Σ((y - y̅)^2))`。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `corrStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `corrStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +::: diff --git a/docs/zh/sql-reference/aggregate-functions/reference/covarpop.md b/docs/zh/sql-reference/aggregate-functions/reference/covarpop.md index 93bfee15684..e98270dc896 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/covarpop.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/covarpop.md @@ -12,5 +12,6 @@ covarPop(x, y) 计算 `Σ((x - x̅)(y - y̅)) / n` 的值。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `covarPopStable` 函数。 它的工作速度较慢,但提供了较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `covarPopStable` 函数。 它的工作速度较慢,但提供了较低的计算错误。 +::: diff --git a/docs/zh/sql-reference/aggregate-functions/reference/covarsamp.md b/docs/zh/sql-reference/aggregate-functions/reference/covarsamp.md index 7c8565211b1..1b8dfc4a60f 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/covarsamp.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/covarsamp.md @@ -14,5 +14,6 @@ covarSamp(x, y) 返回Float64。 当 `n <= 1`, 返回 +∞。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `covarSampStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `covarSampStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +::: diff --git a/docs/zh/sql-reference/aggregate-functions/reference/quantiletiming.md b/docs/zh/sql-reference/aggregate-functions/reference/quantiletiming.md index 5e14ce6a11c..af7ad77c717 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/quantiletiming.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/quantiletiming.md @@ -37,8 +37,9 @@ quantileTiming(level)(expr) 否则,计算结果将四舍五入到16毫秒的最接近倍数。 -!!! note "注" - 对于计算页面加载时间分位数, 此函数比[quantile](../../../sql-reference/aggregate-functions/reference/quantile.md#quantile)更有效和准确。 +:::note +对于计算页面加载时间分位数, 此函数比[quantile](../../../sql-reference/aggregate-functions/reference/quantile.md#quantile)更有效和准确。 +::: **返回值** @@ -46,8 +47,9 @@ quantileTiming(level)(expr) 类型: `Float32`。 -!!! note "注" - 如果没有值传递给函数(当使用 `quantileTimingIf`), [NaN](../../../sql-reference/data-types/float.md#data_type-float-nan-inf)被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 参见 [ORDER BY clause](../../../sql-reference/statements/select/order-by.md#select-order-by) 对于 `NaN` 值排序注意事项。 +:::note +如果没有值传递给函数(当使用 `quantileTimingIf`), [NaN](../../../sql-reference/data-types/float.md#data_type-float-nan-inf)被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 参见 [ORDER BY clause](../../../sql-reference/statements/select/order-by.md#select-order-by) 对于 `NaN` 值排序注意事项。 +::: **示例** diff --git a/docs/zh/sql-reference/aggregate-functions/reference/quantiletimingweighted.md b/docs/zh/sql-reference/aggregate-functions/reference/quantiletimingweighted.md index 2c28583343a..b520a0f96af 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/quantiletimingweighted.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/quantiletimingweighted.md @@ -39,8 +39,9 @@ quantileTimingWeighted(level)(expr, weight) 否则,计算结果将四舍五入到16毫秒的最接近倍数。 -!!! note "注" - 对于计算页面加载时间分位数, 此函数比[quantile](../../../sql-reference/aggregate-functions/reference/quantile.md#quantile)更有效和准确。 +:::note +对于计算页面加载时间分位数, 此函数比[quantile](../../../sql-reference/aggregate-functions/reference/quantile.md#quantile)更有效和准确。 +::: **返回值** @@ -48,8 +49,9 @@ quantileTimingWeighted(level)(expr, weight) 类型: `Float32`。 -!!! note "注" - 如果没有值传递给函数(当使用 `quantileTimingIf`), [NaN](../../../sql-reference/data-types/float.md#data_type-float-nan-inf)被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 参见 [ORDER BY clause](../../../sql-reference/statements/select/order-by.md#select-order-by) 对于 `NaN` 值排序注意事项。 +:::note +如果没有值传递给函数(当使用 `quantileTimingIf`), [NaN](../../../sql-reference/data-types/float.md#data_type-float-nan-inf)被返回。 这样做的目的是将这些案例与导致零的案例区分开来。 参见 [ORDER BY clause](../../../sql-reference/statements/select/order-by.md#select-order-by) 对于 `NaN` 值排序注意事项。 +::: **示例** diff --git a/docs/zh/sql-reference/aggregate-functions/reference/stddevpop.md b/docs/zh/sql-reference/aggregate-functions/reference/stddevpop.md index ea82e21e46f..a113084cdee 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/stddevpop.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/stddevpop.md @@ -7,5 +7,6 @@ sidebar_position: 30 结果等于 [varPop](../../../sql-reference/aggregate-functions/reference/varpop.md)的平方根。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `stddevPopStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `stddevPopStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +::: diff --git a/docs/zh/sql-reference/aggregate-functions/reference/stddevsamp.md b/docs/zh/sql-reference/aggregate-functions/reference/stddevsamp.md index efeafb71072..d242f4e3401 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/stddevsamp.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/stddevsamp.md @@ -7,5 +7,6 @@ sidebar_position: 31 结果等于 [varSamp] (../../../sql-reference/aggregate-functions/reference/varsamp.md)的平方根。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `stddevSampStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `stddevSampStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +::: \ No newline at end of file diff --git a/docs/zh/sql-reference/aggregate-functions/reference/uniqcombined.md b/docs/zh/sql-reference/aggregate-functions/reference/uniqcombined.md index edc790ec00f..7b4a78d662e 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/uniqcombined.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/uniqcombined.md @@ -36,8 +36,9 @@ uniqCombined(HLL_precision)(x[, ...]) - 确定性地提供结果(它不依赖于查询处理顺序)。 -!!! note "注" - 由于它对非 `String` 类型使用32位哈希,对于基数显著大于`UINT_MAX` ,结果将有非常高的误差(误差将在几百亿不同值之后迅速提高), 因此这种情况,你应该使用 [uniqCombined64](../../../sql-reference/aggregate-functions/reference/uniqcombined64.md#agg_function-uniqcombined64) +:::note +由于它对非 `String` 类型使用32位哈希,对于基数显著大于`UINT_MAX` ,结果将有非常高的误差(误差将在几百亿不同值之后迅速提高), 因此这种情况,你应该使用 [uniqCombined64](../../../sql-reference/aggregate-functions/reference/uniqcombined64.md#agg_function-uniqcombined64) +::: 相比于 [uniq](../../../sql-reference/aggregate-functions/reference/uniq.md#agg_function-uniq) 函数, 该 `uniqCombined`: diff --git a/docs/zh/sql-reference/aggregate-functions/reference/varpop.md b/docs/zh/sql-reference/aggregate-functions/reference/varpop.md index eb17955210b..6d6b0acc615 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/varpop.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/varpop.md @@ -9,5 +9,6 @@ sidebar_position: 32 换句话说,计算一组数据的离差。 返回 `Float64`。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `varPopStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `varPopStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +::: diff --git a/docs/zh/sql-reference/aggregate-functions/reference/varsamp.md b/docs/zh/sql-reference/aggregate-functions/reference/varsamp.md index 9b9d0ced92d..508e35445a7 100644 --- a/docs/zh/sql-reference/aggregate-functions/reference/varsamp.md +++ b/docs/zh/sql-reference/aggregate-functions/reference/varsamp.md @@ -11,5 +11,6 @@ sidebar_position: 33 返回 `Float64`。 当 `n <= 1`,返回 `+∞`。 -!!! note "注" - 该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `varSampStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +:::note +该函数使用数值不稳定的算法。 如果你需要 [数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability) 在计算中,使用 `varSampStable` 函数。 它的工作速度较慢,但提供较低的计算错误。 +::: diff --git a/docs/zh/sql-reference/ansi.md b/docs/zh/sql-reference/ansi.md index cdccee0084f..74f13256eba 100644 --- a/docs/zh/sql-reference/ansi.md +++ b/docs/zh/sql-reference/ansi.md @@ -6,8 +6,9 @@ sidebar_label: "ANSI\u517C\u5BB9\u6027" # ClickHouse SQL方言 与ANSI SQL的兼容性{#ansi-sql-compatibility-of-clickhouse-sql-dialect} -!!! note "注" - 本文参考Annex G所著的[ISO/IEC CD 9075-2:2011](https://www.iso.org/obp/ui/#iso:std:iso-iec:9075:-2:ed-4:v1:en:sec:8)标准. +:::note +本文参考Annex G所著的[ISO/IEC CD 9075-2:2011](https://www.iso.org/obp/ui/#iso:std:iso-iec:9075:-2:ed-4:v1:en:sec:8)标准. +::: ## 行为差异 {#differences-in-behaviour} diff --git a/docs/zh/sql-reference/data-types/simpleaggregatefunction.md b/docs/zh/sql-reference/data-types/simpleaggregatefunction.md index b26994a775e..601cb602a78 100644 --- a/docs/zh/sql-reference/data-types/simpleaggregatefunction.md +++ b/docs/zh/sql-reference/data-types/simpleaggregatefunction.md @@ -25,10 +25,10 @@ slug: /zh/sql-reference/data-types/simpleaggregatefunction - [`argMax`](../../sql-reference/aggregate-functions/reference/argmax.md) -!!! note "注" - `SimpleAggregateFunction(func, Type)` 的值外观和存储方式于 `Type` 相同, 所以你不需要应用带有 `-Merge`/`-State` 后缀的函数。 - - `SimpleAggregateFunction` 的性能优于具有相同聚合函数的 `AggregateFunction` 。 +:::note +`SimpleAggregateFunction(func, Type)` 的值外观和存储方式于 `Type` 相同, 所以你不需要应用带有 `-Merge`/`-State` 后缀的函数。 +`SimpleAggregateFunction` 的性能优于具有相同聚合函数的 `AggregateFunction` 。 +::: **参数** diff --git a/docs/zh/sql-reference/functions/string-search-functions.md b/docs/zh/sql-reference/functions/string-search-functions.md index 756ac7c16c7..e4167127424 100644 --- a/docs/zh/sql-reference/functions/string-search-functions.md +++ b/docs/zh/sql-reference/functions/string-search-functions.md @@ -42,8 +42,9 @@ slug: /zh/sql-reference/functions/string-search-functions 对于不区分大小写的搜索或/和UTF-8格式,使用函数`multiSearchAnyCaseInsensitive,multiSearchAnyUTF8,multiSearchAnyCaseInsensitiveUTF8`。 -!!! note "注意" - 在所有`multiSearch*`函数中,由于实现规范,needles的数量应小于28。 +:::note +在所有`multiSearch*`函数中,由于实现规范,needles的数量应小于28。 +::: ## 匹配(大海捞针,模式) {#matchhaystack-pattern} @@ -60,8 +61,9 @@ slug: /zh/sql-reference/functions/string-search-functions 与`match`相同,但如果所有正则表达式都不匹配,则返回0;如果任何模式匹配,则返回1。它使用[超扫描](https://github.com/intel/hyperscan)库。对于在字符串中搜索子字符串的模式,最好使用«multisearchany»,因为它更高效。 -!!! note "注意" - 任何`haystack`字符串的长度必须小于232\字节,否则抛出异常。这种限制是因为hyperscan API而产生的。 +:::note +任何`haystack`字符串的长度必须小于232\字节,否则抛出异常。这种限制是因为hyperscan API而产生的。 +::: ## multiMatchAnyIndex(大海捞针,\[模式1,模式2, …, patternn\]) {#multimatchanyindexhaystack-pattern1-pattern2-patternn} @@ -75,11 +77,13 @@ slug: /zh/sql-reference/functions/string-search-functions 与`multiFuzzyMatchAny`相同,但返回匹配项的匹配能容的索引位置。 -!!! note "注意" - `multiFuzzyMatch*`函数不支持UTF-8正则表达式,由于hyperscan限制,这些表达式被按字节解析。 +:::note +`multiFuzzyMatch*`函数不支持UTF-8正则表达式,由于hyperscan限制,这些表达式被按字节解析。 +::: -!!! note "注意" - 如要关闭所有hyperscan函数的使用,请设置`SET allow_hyperscan = 0;`。 +:::note +如要关闭所有hyperscan函数的使用,请设置`SET allow_hyperscan = 0;`。 +::: ## 提取(大海捞针,图案) {#extracthaystack-pattern} @@ -119,5 +123,6 @@ slug: /zh/sql-reference/functions/string-search-functions 对于不区分大小写的搜索或/和UTF-8格式,使用函数`ngramSearchCaseInsensitive,ngramSearchUTF8,ngramSearchCaseInsensitiveUTF8`。 -!!! note "注意" - 对于UTF-8,我们使用3-gram。所有这些都不是完全公平的n-gram距离。我们使用2字节哈希来散列n-gram,然后计算这些哈希表之间的(非)对称差异 - 可能会发生冲突。对于UTF-8不区分大小写的格式,我们不使用公平的`tolower`函数 - 我们将每个Unicode字符字节的第5位(从零开始)和字节的第一位归零 - 这适用于拉丁语,主要用于所有西里尔字母。 +:::note +对于UTF-8,我们使用3-gram。所有这些都不是完全公平的n-gram距离。我们使用2字节哈希来散列n-gram,然后计算这些哈希表之间的(非)对称差异 - 可能会发生冲突。对于UTF-8不区分大小写的格式,我们不使用公平的`tolower`函数 - 我们将每个Unicode字符字节的第5位(从零开始)和字节的第一位归零 - 这适用于拉丁语,主要用于所有西里尔字母。 +::: diff --git a/docs/zh/sql-reference/statements/alter/delete.md b/docs/zh/sql-reference/statements/alter/delete.md index 85d3d3077a7..5eb77c35a93 100644 --- a/docs/zh/sql-reference/statements/alter/delete.md +++ b/docs/zh/sql-reference/statements/alter/delete.md @@ -12,8 +12,9 @@ ALTER TABLE [db.]table [ON CLUSTER cluster] DELETE WHERE filter_expr 删除匹配指定过滤表达式的数据。实现为[突变](../../../sql-reference/statements/alter/index.md#mutations). -!!! note "备注" - `ALTER TABLE`前缀使得这个语法不同于大多数其他支持SQL的系统。它的目的是表示,与OLTP数据库中的类似查询不同,这是一个不为经常使用而设计的繁重操作。 +:::note +`ALTER TABLE`前缀使得这个语法不同于大多数其他支持SQL的系统。它的目的是表示,与OLTP数据库中的类似查询不同,这是一个不为经常使用而设计的繁重操作。 +::: `filter_expr` 的类型必须是`UInt8`。该查询删除表中该表达式接受非零值的行。 diff --git a/docs/zh/sql-reference/statements/alter/index.md b/docs/zh/sql-reference/statements/alter/index.md index b0f0fc21cbe..8320b207725 100644 --- a/docs/zh/sql-reference/statements/alter/index.md +++ b/docs/zh/sql-reference/statements/alter/index.md @@ -17,8 +17,9 @@ sidebar_label: ALTER - [CONSTRAINT](../../../sql-reference/statements/alter/constraint.md) - [TTL](../../../sql-reference/statements/alter/ttl.md) -!!! note "备注" - 大多数 `ALTER TABLE` 查询只支持[\*MergeTree](../../../engines/table-engines/mergetree-family/index.md)表,以及[Merge](../../../engines/table-engines/special/merge.md)和[Distributed](../../../engines/table-engines/special/distributed.md)。 +:::note +大多数 `ALTER TABLE` 查询只支持[\*MergeTree](../../../engines/table-engines/mergetree-family/index.md)表,以及[Merge](../../../engines/table-engines/special/merge.md)和[Distributed](../../../engines/table-engines/special/distributed.md)。 +::: 这些 `ALTER` 语句操作视图: diff --git a/docs/zh/sql-reference/statements/alter/order-by.md b/docs/zh/sql-reference/statements/alter/order-by.md index e70a8b59c85..e50c4e6e805 100644 --- a/docs/zh/sql-reference/statements/alter/order-by.md +++ b/docs/zh/sql-reference/statements/alter/order-by.md @@ -14,5 +14,6 @@ ALTER TABLE [db].name [ON CLUSTER cluster] MODIFY ORDER BY new_expression 从某种意义上说,该命令是轻量级的,它只更改元数据。要保持数据部分行按排序键表达式排序的属性,您不能向排序键添加包含现有列的表达式(仅在相同的`ALTER`查询中由`ADD COLUMN`命令添加的列,没有默认的列值)。 -!!! note "备注" - 它只适用于[`MergeTree`](../../../engines/table-engines/mergetree-family/mergetree.md)表族(包括[replicated](../../../engines/table-engines/mergetree-family/replication.md)表)。 +:::note +它只适用于[`MergeTree`](../../../engines/table-engines/mergetree-family/mergetree.md)表族(包括[replicated](../../../engines/table-engines/mergetree-family/replication.md)表)。 +::: diff --git a/docs/zh/sql-reference/statements/alter/setting.md b/docs/zh/sql-reference/statements/alter/setting.md index e2d597554e7..2e1e97db331 100644 --- a/docs/zh/sql-reference/statements/alter/setting.md +++ b/docs/zh/sql-reference/statements/alter/setting.md @@ -14,8 +14,9 @@ sidebar_label: SETTING ALTER TABLE [db].name [ON CLUSTER cluster] MODIFY|RESET SETTING ... ``` -!!! note "注意" - 这些查询只能应用于 [MergeTree](../../../engines/table-engines/mergetree-family/mergetree.md) 表。 +:::note +这些查询只能应用于 [MergeTree](../../../engines/table-engines/mergetree-family/mergetree.md) 表。 +::: ## 修改设置 {#alter_modify_setting} diff --git a/docs/zh/sql-reference/statements/alter/update.md b/docs/zh/sql-reference/statements/alter/update.md index 522eb0a705b..97b2b43d889 100644 --- a/docs/zh/sql-reference/statements/alter/update.md +++ b/docs/zh/sql-reference/statements/alter/update.md @@ -12,8 +12,9 @@ ALTER TABLE [db.]table UPDATE column1 = expr1 [, ...] WHERE filter_expr 操作与指定过滤表达式相匹配的数据。作为一个[变更 mutation](../../../sql-reference/statements/alter/index.md#mutations)来实现. -!!! note "Note" - `ALTER TABLE` 的前缀使这个语法与其他大多数支持SQL的系统不同。它的目的是表明,与OLTP数据库中的类似查询不同,这是一个繁重的操作,不是为频繁使用而设计。 +:::note +`ALTER TABLE` 的前缀使这个语法与其他大多数支持SQL的系统不同。它的目的是表明,与OLTP数据库中的类似查询不同,这是一个繁重的操作,不是为频繁使用而设计。 +::: `filter_expr`必须是`UInt8`类型。这个查询将指定列的值更新为行中相应表达式的值,对于这些行,`filter_expr`取值为非零。使用`CAST`操作符将数值映射到列的类型上。不支持更新用于计算主键或分区键的列。 diff --git a/docs/zh/sql-reference/statements/exchange.md b/docs/zh/sql-reference/statements/exchange.md index e6ac1dbf1dc..47cefa0d2e6 100644 --- a/docs/zh/sql-reference/statements/exchange.md +++ b/docs/zh/sql-reference/statements/exchange.md @@ -9,8 +9,9 @@ sidebar_label: EXCHANGE 以原子方式交换两个表或字典的名称。 此任务也可以通过使用[RENAME](./rename.md)来完成,但在这种情况下操作不是原子的。 -!!! note "注意" +:::note `EXCHANGE`仅支持[Atomic](../../engines/database-engines/atomic.md)数据库引擎. +::: **语法** diff --git a/docs/zh/sql-reference/statements/rename.md b/docs/zh/sql-reference/statements/rename.md index c26dce306cc..156306fbd3e 100644 --- a/docs/zh/sql-reference/statements/rename.md +++ b/docs/zh/sql-reference/statements/rename.md @@ -9,8 +9,9 @@ sidebar_label: RENAME 重命名数据库、表或字典。 可以在单个查询中重命名多个实体。 请注意,具有多个实体的`RENAME`查询是非原子操作。 要以原子方式交换实体名称,请使用[EXCHANGE](./exchange.md)语法. -!!! note "注意" +:::note `RENAME`仅支持[Atomic](../../engines/database-engines/atomic.md)数据库引擎. +::: **语法** diff --git a/docs/zh/sql-reference/statements/select/group-by.md b/docs/zh/sql-reference/statements/select/group-by.md index 31c1649bc30..29c72ce7e45 100644 --- a/docs/zh/sql-reference/statements/select/group-by.md +++ b/docs/zh/sql-reference/statements/select/group-by.md @@ -11,8 +11,9 @@ sidebar_label: GROUP BY - 在所有的表达式在 [SELECT](../../../sql-reference/statements/select/index.md), [HAVING](../../../sql-reference/statements/select/having),和 [ORDER BY](../../../sql-reference/statements/select/order-by.md) 子句中 **必须** 基于键表达式进行计算 **或** 上 [聚合函数](../../../sql-reference/aggregate-functions/index.md) 在非键表达式(包括纯列)上。 换句话说,从表中选择的每个列必须用于键表达式或聚合函数内,但不能同时使用。 - 聚合结果 `SELECT` 查询将包含尽可能多的行,因为有唯一值 “grouping key” 在源表中。 通常这会显着减少行数,通常是数量级,但不一定:如果所有行数保持不变 “grouping key” 值是不同的。 -!!! note "注" - 还有一种额外的方法可以在表上运行聚合。 如果查询仅在聚合函数中包含表列,则 `GROUP BY` 可以省略,并且通过一个空的键集合来假定聚合。 这样的查询总是只返回一行。 +:::note +还有一种额外的方法可以在表上运行聚合。 如果查询仅在聚合函数中包含表列,则 `GROUP BY` 可以省略,并且通过一个空的键集合来假定聚合。 这样的查询总是只返回一行。 +::: ## 空处理 {#null-processing} diff --git a/docs/zh/sql-reference/statements/select/join.md b/docs/zh/sql-reference/statements/select/join.md index 08290a02de5..a2686aa5e53 100644 --- a/docs/zh/sql-reference/statements/select/join.md +++ b/docs/zh/sql-reference/statements/select/join.md @@ -39,8 +39,9 @@ ClickHouse中提供的其他联接类型: ## 严格 {#join-settings} -!!! note "注" - 可以使用以下方式复盖默认的严格性值 [join_default_strictness](../../../operations/settings/settings.md#settings-join_default_strictness) 设置。 +:::note +可以使用以下方式复盖默认的严格性值 [join_default_strictness](../../../operations/settings/settings.md#settings-join_default_strictness) 设置。 +::: Also the behavior of ClickHouse server for `ANY JOIN` operations depends on the [any_join_distinct_right_table_keys](../../../operations/settings/settings.md#any_join_distinct_right_table_keys) setting. @@ -91,8 +92,9 @@ USING (equi_column1, ... equi_columnN, asof_column) `ASOF JOIN`会从 `table_2` 中的用户事件时间戳找出和 `table_1` 中用户事件时间戳中最近的一个时间戳,来满足最接近匹配的条件。如果有得话,则相等的时间戳值是最接近的值。在此例中,`user_id` 列可用于条件匹配,`ev_time` 列可用于最接近匹配。在此例中,`event_1_1` 可以 JOIN `event_2_1`,`event_1_2` 可以JOIN `event_2_3`,但是 `event_2_2` 不能被JOIN。 -!!! note "注" - `ASOF JOIN`在 [JOIN](../../../engines/table-engines/special/join.md) 表引擎中 **不受** 支持。 +:::note +`ASOF JOIN`在 [JOIN](../../../engines/table-engines/special/join.md) 表引擎中 **不受** 支持。 +::: ## 分布式联接 {#global-join} diff --git a/docs/zh/sql-reference/statements/select/limit-by.md b/docs/zh/sql-reference/statements/select/limit-by.md index 50e3505b7fb..68b88bf8d7a 100644 --- a/docs/zh/sql-reference/statements/select/limit-by.md +++ b/docs/zh/sql-reference/statements/select/limit-by.md @@ -14,8 +14,9 @@ ClickHouse支持以下语法变体: 处理查询时,ClickHouse首先选择经由排序键排序过后的数据。排序键可以显式地使用[ORDER BY](order-by.md#select-order-by)从句指定,或隐式地使用表引擎使用的排序键(数据的顺序仅在使用[ORDER BY](order-by.md#select-order-by)时才可以保证,否则由于多线程处理,数据顺序会随机化)。然后ClickHouse执行`LIMIT n BY expressions`从句,将每一行按 `expressions` 的值进行分组,并对每一分组返回前`n`行。如果指定了`OFFSET`,那么对于每一分组,ClickHouse会跳过前`offset_value`行,接着返回前`n`行。如果`offset_value`大于某一分组的行数,ClickHouse会从分组返回0行。 -!!! note "注" - `LIMIT BY`与[LIMIT](../../../sql-reference/statements/select/limit.md)没有关系。它们可以在同一个查询中使用。 +:::note +`LIMIT BY`与[LIMIT](../../../sql-reference/statements/select/limit.md)没有关系。它们可以在同一个查询中使用。 +::: ## 例 {#examples} diff --git a/docs/zh/sql-reference/statements/select/sample.md b/docs/zh/sql-reference/statements/select/sample.md index f701bd3b805..0993958b029 100644 --- a/docs/zh/sql-reference/statements/select/sample.md +++ b/docs/zh/sql-reference/statements/select/sample.md @@ -15,8 +15,9 @@ sidebar_label: SAMPLE - 当您的原始数据不准确时,所以近似不会明显降低质量。 - 业务需求的目标是近似结果(为了成本效益,或者向高级用户推销确切结果)。 -!!! note "注" - 您只能使用采样中的表 [MergeTree](../../../engines/table-engines/mergetree-family/mergetree.md) 族,并且只有在表创建过程中指定了采样表达式(请参阅 [MergeTree引擎](../../../engines/table-engines/mergetree-family/mergetree.md#table_engine-mergetree-creating-a-table)). +:::note +您只能使用采样中的表 [MergeTree](../../../engines/table-engines/mergetree-family/mergetree.md) 族,并且只有在表创建过程中指定了采样表达式(请参阅 [MergeTree引擎](../../../engines/table-engines/mergetree-family/mergetree.md#table_engine-mergetree-creating-a-table)). +::: 下面列出了数据采样的功能: diff --git a/docs/zh/sql-reference/statements/select/where.md b/docs/zh/sql-reference/statements/select/where.md index fe165e8ad16..6c7183840c7 100644 --- a/docs/zh/sql-reference/statements/select/where.md +++ b/docs/zh/sql-reference/statements/select/where.md @@ -11,9 +11,10 @@ sidebar_label: WHERE 如果基础表引擎支持,`WHERE`表达式会使用索引和分区进行剪枝。 -!!! note "注" - 有一个叫做过滤优化 [prewhere](../../../sql-reference/statements/select/prewhere.md) 的东西. - +:::note +有一个叫做过滤优化 [prewhere](../../../sql-reference/statements/select/prewhere.md) 的东西. +::: + 如果需要测试一个 [NULL](../../../sql-reference/syntax.md#null-literal) 值,请使用 [IS NULL](../../operators/index.md#operator-is-null) and [IS NOT NULL](../../operators/index.md#is-not-null) 运算符或 [isNull](../../../sql-reference/functions/functions-for-nulls.md#isnull) 和 [isNotNull](../../../sql-reference/functions/functions-for-nulls.md#isnotnull) 函数。否则带有 NULL 的表达式永远不会通过。 **示例** diff --git a/docs/zh/sql-reference/statements/system.md b/docs/zh/sql-reference/statements/system.md index d8d60c28af5..1942d6fb79a 100644 --- a/docs/zh/sql-reference/statements/system.md +++ b/docs/zh/sql-reference/statements/system.md @@ -124,10 +124,9 @@ ClickHouse可以管理 [MergeTree](../../engines/table-engines/mergetree-family/ SYSTEM STOP MERGES [[db.]merge_tree_family_table_name] ``` - -!!! note "Note" - `DETACH / ATTACH` 表操作会在后台进行表的merge操作,甚至当所有MergeTree表的合并操作已经停止的情况下。 - +:::note +`DETACH / ATTACH` 表操作会在后台进行表的merge操作,甚至当所有MergeTree表的合并操作已经停止的情况下。 +::: ### START MERGES {#query_language-system-start-merges} diff --git a/docs/zh/sql-reference/table-functions/mysql.md b/docs/zh/sql-reference/table-functions/mysql.md index 6c9753b9b12..4efee2e616b 100644 --- a/docs/zh/sql-reference/table-functions/mysql.md +++ b/docs/zh/sql-reference/table-functions/mysql.md @@ -49,8 +49,9 @@ SELECT name FROM mysql(`mysql1:3306|mysql2:3306|mysql3:3306`, 'mysql_database', 与原始MySQL表具有相同列的表对象。 -!!! note "注意" - 在`INSERT`查询中为了区分`mysql(...)`与带有列名列表的表名的表函数,你必须使用关键字`FUNCTION`或`TABLE FUNCTION`。查看如下示例。 +:::note +在`INSERT`查询中为了区分`mysql(...)`与带有列名列表的表名的表函数,你必须使用关键字`FUNCTION`或`TABLE FUNCTION`。查看如下示例。 +::: ## 用法示例 {#usage-example} From 380e076e842fee30e4bd6b9729a36edbe623e61f Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Fri, 3 Mar 2023 11:19:05 +0000 Subject: [PATCH 239/342] x86 --> amd64 --- .github/workflows/backport_branches.yml | 2 +- .github/workflows/master.yml | 2 +- .github/workflows/pull_request.yml | 2 +- .github/workflows/release_branches.yml | 2 +- tests/ci/ci_config.py | 2 +- tests/ci/compatibility_check.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/backport_branches.yml b/.github/workflows/backport_branches.yml index b14a32127c4..a324d20abc9 100644 --- a/.github/workflows/backport_branches.yml +++ b/.github/workflows/backport_branches.yml @@ -103,7 +103,7 @@ jobs: sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (amd64)" --check-glibc --check-distributions - name: Cleanup if: always() run: | diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index e224d6cf5c3..b70fe256833 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -134,7 +134,7 @@ jobs: sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (amd64)" --check-glibc --check-distributions - name: Cleanup if: always() run: | diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ae6cb1d3a83..ff98739db00 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -198,7 +198,7 @@ jobs: sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (amd64)" --check-glibc --check-distributions - name: Cleanup if: always() run: | diff --git a/.github/workflows/release_branches.yml b/.github/workflows/release_branches.yml index 229532efdab..74ec1163cc9 100644 --- a/.github/workflows/release_branches.yml +++ b/.github/workflows/release_branches.yml @@ -95,7 +95,7 @@ jobs: sudo rm -fr "$TEMP_PATH" mkdir -p "$TEMP_PATH" cp -r "$GITHUB_WORKSPACE" "$TEMP_PATH" - cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (x86)" --check-glibc --check-distributions + cd "$REPO_COPY/tests/ci" && python3 compatibility_check.py --check-name "Compatibility check (amd64)" --check-glibc --check-distributions - name: Cleanup if: always() run: | diff --git a/tests/ci/ci_config.py b/tests/ci/ci_config.py index 9df198430d2..2f35b337cb3 100644 --- a/tests/ci/ci_config.py +++ b/tests/ci/ci_config.py @@ -316,7 +316,7 @@ CI_CONFIG = { "Integration tests flaky check (asan)": { "required_build": "package_asan", }, - "Compatibility check (x86)": { + "Compatibility check (amd64)": { "required_build": "package_release", }, "Compatibility check (aarch64)": { diff --git a/tests/ci/compatibility_check.py b/tests/ci/compatibility_check.py index 8841bedbb18..432e9ec7c01 100644 --- a/tests/ci/compatibility_check.py +++ b/tests/ci/compatibility_check.py @@ -214,7 +214,7 @@ def main(): # See https://sourceware.org/glibc/wiki/Glibc%20Timeline max_glibc_version = "" - if "x86" in args.check_name: + if "amd64" in args.check_name: max_glibc_version = "2.4" elif "aarch64" in args.check_name: max_glibc_version = "2.18" # because of build with newer sysroot? From d34f00f4370fd548a739bda02c4d1902c464e945 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Fri, 3 Mar 2023 15:02:05 +0300 Subject: [PATCH 240/342] Update run.sh --- docker/test/upgrade/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/test/upgrade/run.sh b/docker/test/upgrade/run.sh index df32e2833e7..ce8a56c777e 100644 --- a/docker/test/upgrade/run.sh +++ b/docker/test/upgrade/run.sh @@ -162,7 +162,7 @@ rg -Fav -e "Code: 236. DB::Exception: Cancelled merging parts" \ -e "Cannot flush" \ -e "Container already exists" \ /var/log/clickhouse-server/clickhouse-server.upgrade.log | zgrep -Fa "" > /test_output/upgrade_error_messages.txt \ - && echo -e "Error message in clickhouse-server.log (see upgrade_error_messages.txt)$FAIL$(head_escaped /test_output/bc_check_error_messages.txt)" \ + && echo -e "Error message in clickhouse-server.log (see upgrade_error_messages.txt)$FAIL$(head_escaped /test_output/upgrade_error_messages.txt)" \ >> /test_output/test_results.tsv \ || echo -e "No Error messages after server upgrade$OK" >> /test_output/test_results.tsv From 56f02374f6dbbb2195279fdf9415a3275a5697c7 Mon Sep 17 00:00:00 2001 From: kssenii Date: Fri, 3 Mar 2023 12:14:05 +0100 Subject: [PATCH 241/342] Remove stateless test because it makes unfixably 02344_show_caches flaky --- tests/queries/0_stateless/02344_show_caches.reference | 1 - .../02454_create_table_with_custom_disk.reference | 1 - .../02454_create_table_with_custom_disk.sql | 10 ---------- 3 files changed, 12 deletions(-) diff --git a/tests/queries/0_stateless/02344_show_caches.reference b/tests/queries/0_stateless/02344_show_caches.reference index b321319a309..2ee4f902ba1 100644 --- a/tests/queries/0_stateless/02344_show_caches.reference +++ b/tests/queries/0_stateless/02344_show_caches.reference @@ -10,6 +10,5 @@ local_cache s3_cache_6 s3_cache_small local_cache_2 -__tmp_internal_324081342946782869538999598488311137423 local_cache_3 s3_cache_multi_2 diff --git a/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference b/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference index a71d52b6f57..1d8610c59c9 100644 --- a/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference +++ b/tests/queries/0_stateless/02454_create_table_with_custom_disk.reference @@ -9,4 +9,3 @@ SETTINGS disk = disk(type = local, path = \'/var/lib/clickhouse/disks/local/\') CREATE TABLE default.test\n(\n `a` Int32\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS disk = disk(type = local, path = \'[HIDDEN]\'), index_granularity = 8192 a Int32 200 -CREATE TABLE default.test\n(\n `a` Int32\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS disk = disk(type = cache, max_size = \'[HIDDEN]\', path = \'[HIDDEN]\', disk = disk(type = local, path = \'[HIDDEN]\')), index_granularity = 8192 diff --git a/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql b/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql index 4909f91ccb1..6cb1c0774aa 100644 --- a/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql +++ b/tests/queries/0_stateless/02454_create_table_with_custom_disk.sql @@ -26,13 +26,3 @@ DESCRIBE TABLE test; INSERT INTO test SELECT number FROM numbers(100); SELECT count() FROM test; - -DROP TABLE test; - -CREATE TABLE test (a Int32) -ENGINE = MergeTree() order by tuple() -SETTINGS disk = disk(type=cache, max_size='1Gi', path='/var/lib/clickhouse/custom_disk_cache/', disk=disk(type=local, path='/var/lib/clickhouse/disks/local/')); - -SHOW CREATE TABLE test; - -DROP TABLE test; From bc8b34f74ba8176020ba65c6d8aed44f8f53afdd Mon Sep 17 00:00:00 2001 From: Sema Checherinda Date: Fri, 3 Mar 2023 13:16:46 +0100 Subject: [PATCH 242/342] adjust test test_seekable_formats --- tests/integration/test_storage_s3/test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/integration/test_storage_s3/test.py b/tests/integration/test_storage_s3/test.py index 54944b56919..8b20727a7b5 100644 --- a/tests/integration/test_storage_s3/test.py +++ b/tests/integration/test_storage_s3/test.py @@ -1056,13 +1056,13 @@ def test_seekable_formats(started_cluster): table_function = f"s3(s3_orc, structure='a Int32, b String', format='ORC')" exec_query_with_retry( instance, - f"insert into table function {table_function} SELECT number, randomString(100) FROM numbers(1000000) settings s3_truncate_on_insert=1", + f"insert into table function {table_function} SELECT number, randomString(100) FROM numbers(1500000) settings s3_truncate_on_insert=1", ) result = instance.query( - f"SELECT count() FROM {table_function} SETTINGS max_memory_usage='50M'" + f"SELECT count() FROM {table_function} SETTINGS max_memory_usage='60M'" ) - assert int(result) == 1000000 + assert int(result) == 1500000 instance.query(f"SELECT * FROM {table_function} FORMAT Null") @@ -1073,7 +1073,7 @@ def test_seekable_formats(started_cluster): result = result.strip() assert result.endswith("MiB") result = result[: result.index(".")] - assert int(result) > 80 + assert int(result) > 150 def test_seekable_formats_url(started_cluster): @@ -1083,23 +1083,23 @@ def test_seekable_formats_url(started_cluster): table_function = f"s3(s3_parquet, structure='a Int32, b String', format='Parquet')" exec_query_with_retry( instance, - f"insert into table function {table_function} SELECT number, randomString(100) FROM numbers(1000000) settings s3_truncate_on_insert=1", + f"insert into table function {table_function} SELECT number, randomString(100) FROM numbers(1500000) settings s3_truncate_on_insert=1", ) result = instance.query(f"SELECT count() FROM {table_function}") - assert int(result) == 1000000 + assert int(result) == 1500000 table_function = f"s3(s3_orc, structure='a Int32, b String', format='ORC')" exec_query_with_retry( instance, - f"insert into table function {table_function} SELECT number, randomString(100) FROM numbers(1000000) settings s3_truncate_on_insert=1", + f"insert into table function {table_function} SELECT number, randomString(100) FROM numbers(1500000) settings s3_truncate_on_insert=1", ) table_function = f"url('http://{started_cluster.minio_host}:{started_cluster.minio_port}/{bucket}/test_parquet', 'Parquet', 'a Int32, b String')" result = instance.query( - f"SELECT count() FROM {table_function} SETTINGS max_memory_usage='50M'" + f"SELECT count() FROM {table_function} SETTINGS max_memory_usage='60M'" ) - assert int(result) == 1000000 + assert int(result) == 1500000 def test_empty_file(started_cluster): From e2278ca70c8945b17dce8ff9698905ca33f6f847 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Wed, 1 Mar 2023 20:59:56 +0100 Subject: [PATCH 243/342] Add code for autoscaling lambda --- tests/ci/autoscale_runners_lambda/app.py | 226 ++++++++++++++++++ .../build_and_deploy_archive.sh | 1 + .../autoscale_runners_lambda/requirements.txt | 1 + 3 files changed, 228 insertions(+) create mode 100644 tests/ci/autoscale_runners_lambda/app.py create mode 120000 tests/ci/autoscale_runners_lambda/build_and_deploy_archive.sh create mode 100644 tests/ci/autoscale_runners_lambda/requirements.txt diff --git a/tests/ci/autoscale_runners_lambda/app.py b/tests/ci/autoscale_runners_lambda/app.py new file mode 100644 index 00000000000..2777d5cf9c1 --- /dev/null +++ b/tests/ci/autoscale_runners_lambda/app.py @@ -0,0 +1,226 @@ +#!/usr/bin/env python3 + +"""The lambda to decrease/increase ASG desired capacity based on current queue""" + +import json +import logging +import time +from dataclasses import dataclass +from pprint import pformat +from typing import Any, List, Literal, Optional + +import boto3 # type: ignore +import requests # type: ignore + +RUNNER_TYPE_LABELS = [ + "builder", + "func-tester", + "func-tester-aarch64", + "fuzzer-unit-tester", + "stress-tester", + "style-checker", + "style-checker-aarch64", +] +QUEUE_QUERY = f"""SELECT + last_status AS status, + toUInt32(count()) AS length, + labels +FROM +( + SELECT + arraySort(groupArray(status))[-1] AS last_status, + labels, + id, + html_url + FROM default.workflow_jobs + WHERE has(labels, 'self-hosted') + AND hasAny({RUNNER_TYPE_LABELS}, labels) + AND started_at > now() - INTERVAL 2 DAY + GROUP BY ALL + HAVING last_status IN ('in_progress', 'queued') +) +GROUP BY ALL +ORDER BY labels, last_status""" + + +@dataclass +class Queue: + status: Literal["in_progress", "queued"] + lentgh: int + label: str + + +### VENDORING +def get_parameter_from_ssm(name, decrypt=True, client=None): + if not client: + client = boto3.client("ssm", region_name="us-east-1") + return client.get_parameter(Name=name, WithDecryption=decrypt)["Parameter"]["Value"] + + +class CHException(Exception): + pass + + +class ClickHouseHelper: + def __init__( + self, + url: Optional[str] = None, + user: Optional[str] = None, + password: Optional[str] = None, + ): + self.url = url + self.auth = {} + if user: + self.auth["X-ClickHouse-User"] = user + if password: + self.auth["X-ClickHouse-Key"] = password + + def _select_and_get_json_each_row(self, db, query): + params = { + "database": db, + "query": query, + "default_format": "JSONEachRow", + } + for i in range(5): + response = None + try: + response = requests.get(self.url, params=params, headers=self.auth) + response.raise_for_status() + return response.text + except Exception as ex: + logging.warning("Cannot fetch data with exception %s", str(ex)) + if response: + logging.warning("Reponse text %s", response.text) + time.sleep(0.1 * i) + + raise CHException("Cannot fetch data from clickhouse") + + def select_json_each_row(self, db, query): + text = self._select_and_get_json_each_row(db, query) + result = [] + for line in text.split("\n"): + if line: + result.append(json.loads(line)) + return result + + +CH_CLIENT = ClickHouseHelper(get_parameter_from_ssm("clickhouse-test-stat-url"), "play") + + +def set_capacity( + runner_type: str, queues: List[Queue], client: Any, dry_run: bool = True +) -> None: + assert len(queues) in (1, 2) + assert all(q.label == runner_type for q in queues) + as_groups = client.describe_auto_scaling_groups( + Filters=[ + {"Name": "tag-key", "Values": ["github:runner-type"]}, + {"Name": "tag-value", "Values": [runner_type]}, + ] + )["AutoScalingGroups"] + assert len(as_groups) == 1 + asg = as_groups[0] + running = 0 + queued = 0 + for q in queues: + if q.status == "in_progress": + running = q.lentgh + continue + if q.status == "queued": + queued = q.lentgh + continue + raise ValueError("Queue status is not in ['in_progress', 'queued']") + + capacity_reserve = max(0, asg["DesiredCapacity"] - running) + stop = False + if queued: + # This part is about scaling up + # First, let's check if there's enough runners to cover the queue + stop = stop or (asg["DesiredCapacity"] - running - queued) > 0 + + stop = stop or asg["MaxSize"] <= asg["DesiredCapacity"] + # Let's calculate a new desired capacity. Here the scale is used to not + # scale up and down too quickly + desired_capacity = asg["DesiredCapacity"] + ((queued - capacity_reserve) // 5) + desired_capacity = max(desired_capacity, asg["MinSize"]) + desired_capacity = min(desired_capacity, asg["MaxSize"]) + # Finally, should the capacity be even changed + stop = stop or asg["DesiredCapacity"] == desired_capacity + if stop: + return + logging.info( + "The ASG %s capacity will be increased to %s, current capacity=%s, " + "maximum capacity=%s, running jobs=%s, queue size=%s", + asg["AutoScalingGroupName"], + desired_capacity, + asg["DesiredCapacity"], + asg["MaxSize"], + running, + queued, + ) + if not dry_run: + client.set_desired_capacity( + AutoScalingGroupName=asg["AutoScalingGroupName"], + DesiredCapacity=desired_capacity, + ) + return + + # Now we will calculate if we need to scale down + stop = stop or asg["DesiredCapacity"] <= asg["MinSize"] + stop = stop or asg["DesiredCapacity"] <= running + # Scale down quicker than scale up + desired_capacity = asg["DesiredCapacity"] - (capacity_reserve // 3) + desired_capacity = max(desired_capacity, asg["MinSize"]) + desired_capacity = min(desired_capacity, asg["MaxSize"]) + stop = stop or asg["DesiredCapacity"] == desired_capacity + if stop: + return + + logging.info( + "The ASG %s capacity will be decreased to %s, current capacity=%s, " + "minimum capacity=%s, running jobs=%s, queue size=%s", + asg["AutoScalingGroupName"], + desired_capacity, + asg["DesiredCapacity"], + asg["MinSize"], + running, + queued, + ) + if not dry_run: + client.set_desired_capacity( + AutoScalingGroupName=asg["AutoScalingGroupName"], + DesiredCapacity=desired_capacity, + ) + + +def main(dry_run: bool = True) -> None: + logging.getLogger().setLevel(logging.INFO) + asg_client = boto3.client("autoscaling") + try: + global CH_CLIENT + queues = CH_CLIENT.select_json_each_row("default", QUEUE_QUERY) + except CHException as ex: + logging.exception( + "Got an exception on insert, tryuing to update the client " + "credentials and repeat", + exc_info=ex, + ) + CH_CLIENT = ClickHouseHelper( + get_parameter_from_ssm("clickhouse-test-stat-url"), "play" + ) + queues = CH_CLIENT.select_json_each_row("default", QUEUE_QUERY) + + logging.info("Received queue data:\n%s", pformat(queues, width=120)) + for runner_type in RUNNER_TYPE_LABELS: + runner_queues = [ + Queue(queue["status"], queue["length"], runner_type) + for queue in queues + if runner_type in queue["labels"] + ] + set_capacity(runner_type, runner_queues, asg_client, dry_run) + + +def handler(event: dict, context: Any) -> None: + _ = event + _ = context + return main(False) diff --git a/tests/ci/autoscale_runners_lambda/build_and_deploy_archive.sh b/tests/ci/autoscale_runners_lambda/build_and_deploy_archive.sh new file mode 120000 index 00000000000..96ba3fa024e --- /dev/null +++ b/tests/ci/autoscale_runners_lambda/build_and_deploy_archive.sh @@ -0,0 +1 @@ +../team_keys_lambda/build_and_deploy_archive.sh \ No newline at end of file diff --git a/tests/ci/autoscale_runners_lambda/requirements.txt b/tests/ci/autoscale_runners_lambda/requirements.txt new file mode 100644 index 00000000000..f2293605cf1 --- /dev/null +++ b/tests/ci/autoscale_runners_lambda/requirements.txt @@ -0,0 +1 @@ +requests From 0483de0e04af22ae9a51b1feceef86810efd2cda Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 2 Mar 2023 13:53:56 +0100 Subject: [PATCH 244/342] Reorder `status` in default.workflow_jobs --- tests/ci/workflow_jobs_lambda/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ci/workflow_jobs_lambda/app.py b/tests/ci/workflow_jobs_lambda/app.py index f6589576806..9436e01ad53 100644 --- a/tests/ci/workflow_jobs_lambda/app.py +++ b/tests/ci/workflow_jobs_lambda/app.py @@ -213,7 +213,7 @@ def send_event_workflow_job(workflow_job: WorkflowJob) -> None: # `head_sha` String, # `url` String, # `html_url` String, - # `status` Enum8('queued' = 1, 'in_progress' = 2, 'completed' = 3, 'waiting' = 4), + # `status` Enum8('waiting' = 1, 'queued' = 2, 'in_progress' = 3, 'completed' = 4), # `conclusion` LowCardinality(String), # `started_at` DateTime, # `completed_at` DateTime, From a5869e25f644551b00c034bb25efa56d4ed111d3 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 2 Mar 2023 20:21:40 +0100 Subject: [PATCH 245/342] Add tests for autoscale_runners_lambda --- tests/ci/autoscale_runners_lambda_test.py | 133 ++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 tests/ci/autoscale_runners_lambda_test.py diff --git a/tests/ci/autoscale_runners_lambda_test.py b/tests/ci/autoscale_runners_lambda_test.py new file mode 100644 index 00000000000..e6ac157ee4a --- /dev/null +++ b/tests/ci/autoscale_runners_lambda_test.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python + +import unittest +from dataclasses import dataclass +from typing import Any, List + +from autoscale_runners_lambda.app import set_capacity, Queue + + +@dataclass +class TestCase: + name: str + min_size: int + desired_capacity: int + max_size: int + queues: List[Queue] + expected_capacity: int + + +class TestSetCapacity(unittest.TestCase): + class FakeClient: + def __init__(self): + self._expected_data = {} # type: dict + self._expected_capacity = -1 + + @property + def expected_data(self) -> dict: + """a one-time property""" + data, self._expected_data = self._expected_data, {} + return data + + @expected_data.setter + def expected_data(self, value: dict) -> None: + self._expected_data = value + + @property + def expected_capacity(self) -> int: + """one-time property""" + capacity, self._expected_capacity = self._expected_capacity, -1 + return capacity + + def describe_auto_scaling_groups(self, **kwargs: Any) -> dict: + _ = kwargs + return self.expected_data + + def set_desired_capacity(self, **kwargs: Any) -> None: + self._expected_capacity = kwargs["DesiredCapacity"] + + def data_helper( + self, name: str, min_size: int, desired_capacity: int, max_size: int + ) -> None: + self.expected_data = { + "AutoScalingGroups": [ + { + "AutoScalingGroupName": name, + "DesiredCapacity": desired_capacity, + "MinSize": min_size, + "MaxSize": max_size, + } + ] + } + + def setUp(self): + self.client = self.FakeClient() + + def test_normal_cases(self): + test_cases = ( + # Do not change capacity + TestCase("noqueue", 1, 13, 20, [Queue("in_progress", 155, "noqueue")], -1), + TestCase("w/reserve", 1, 13, 20, [Queue("queued", 5, "w/reserve")], -1), + TestCase("w/reserve", 1, 13, 20, [Queue("queued", 17, "w/reserve")], -1), + TestCase("w/reserve", 1, 23, 20, [Queue("queued", 17, "w/reserve")], -1), + TestCase("less-min", 10, 3, 20, [Queue("queued", 1, "less-min")], -1), + # Increase capacity + TestCase("increase", 1, 13, 20, [Queue("queued", 23, "increase")], 15), + TestCase("increase", 1, 13, 20, [Queue("queued", 18, "increase")], 14), + TestCase("increase", 1, 13, 20, [Queue("queued", 183, "increase")], 20), + TestCase( + "increase-w/o reserve", + 1, + 13, + 20, + [ + Queue("in_progress", 11, "increase-w/o reserve"), + Queue("queued", 12, "increase-w/o reserve"), + ], + 15, + ), + TestCase("lower-min", 10, 5, 20, [Queue("queued", 5, "lower-min")], 10), + # Decrease capacity + TestCase("decrease", 1, 13, 20, [Queue("in_progress", 3, "decrease")], 10), + TestCase("decrease", 1, 13, 20, [Queue("in_progress", 5, "decrease")], 11), + ) + for t in test_cases: + self.client.data_helper(t.name, t.min_size, t.desired_capacity, t.max_size) + set_capacity(t.name, t.queues, self.client, False) + self.assertEqual(t.expected_capacity, self.client.expected_capacity, t.name) + + def test_exceptions(self): + test_cases = ( + ( + TestCase( + "different names", + 1, + 1, + 1, + [Queue("queued", 5, "another name")], + -1, + ), + AssertionError, + ), + (TestCase("wrong queue len", 1, 1, 1, [], -1), AssertionError), + ( + TestCase( + "wrong queue", 1, 1, 1, [Queue("wrong", 1, "wrong queue")], -1 # type: ignore + ), + ValueError, + ), + ) + for t, error in test_cases: + with self.assertRaises(error): + self.client.data_helper( + t.name, t.min_size, t.desired_capacity, t.max_size + ) + set_capacity(t.name, t.queues, self.client, False) + + with self.assertRaises(AssertionError): + self.client.expected_data = {"AutoScalingGroups": [1, 2]} + set_capacity( + "wrong number of ASGs", + [Queue("queued", 1, "wrong number of ASGs")], + self.client, + ) From 4e9c2462648c265646cd79055837a73ecd6b7631 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 2 Mar 2023 20:28:27 +0100 Subject: [PATCH 246/342] Check CI python code always --- .github/workflows/pull_request.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 7d410f833c5..1efa6147c9a 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -37,7 +37,6 @@ jobs: cd "$GITHUB_WORKSPACE/tests/ci" python3 run_check.py PythonUnitTests: - needs: CheckLabels runs-on: [self-hosted, style-checker] steps: - name: Check out repository code From c1774cabd01cea086c96547e7fffd35a3a2282b2 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Thu, 2 Mar 2023 20:29:34 +0100 Subject: [PATCH 247/342] Do not install GitPython on runners, it is preinstalled --- .github/workflows/cherry_pick.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/cherry_pick.yml b/.github/workflows/cherry_pick.yml index 065e584182b..8d1e2055978 100644 --- a/.github/workflows/cherry_pick.yml +++ b/.github/workflows/cherry_pick.yml @@ -35,7 +35,6 @@ jobs: fetch-depth: 0 - name: Cherry pick run: | - sudo pip install GitPython cd "$GITHUB_WORKSPACE/tests/ci" python3 cherry_pick.py - name: Cleanup From 6a746beff8586ebbe496089b8da4dff044b5657a Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Fri, 3 Mar 2023 09:53:21 +0100 Subject: [PATCH 248/342] Tune style-checker scaling up and down by modifiers --- tests/ci/autoscale_runners_lambda/app.py | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/ci/autoscale_runners_lambda/app.py b/tests/ci/autoscale_runners_lambda/app.py index 2777d5cf9c1..25c6c2eb9a8 100644 --- a/tests/ci/autoscale_runners_lambda/app.py +++ b/tests/ci/autoscale_runners_lambda/app.py @@ -7,7 +7,7 @@ import logging import time from dataclasses import dataclass from pprint import pformat -from typing import Any, List, Literal, Optional +from typing import Any, List, Literal, Optional, Tuple import boto3 # type: ignore import requests # type: ignore @@ -50,6 +50,19 @@ class Queue: label: str +def get_scales(runner_type: str) -> Tuple[int, int]: + "returns the multipliers for scaling down and up ASG by types" + # Scaling down is quicker on the lack of running jobs than scaling up on + # queue + scale_down = 3 + scale_up = 5 + if runner_type == "style-checker": + # the style checkers have so many noise, so it scales up too quickly + scale_down = 2 + scale_up = 10 + return scale_down, scale_up + + ### VENDORING def get_parameter_from_ssm(name, decrypt=True, client=None): if not client: @@ -131,6 +144,7 @@ def set_capacity( continue raise ValueError("Queue status is not in ['in_progress', 'queued']") + scale_down, scale_up = get_scales(runner_type) capacity_reserve = max(0, asg["DesiredCapacity"] - running) stop = False if queued: @@ -139,9 +153,10 @@ def set_capacity( stop = stop or (asg["DesiredCapacity"] - running - queued) > 0 stop = stop or asg["MaxSize"] <= asg["DesiredCapacity"] - # Let's calculate a new desired capacity. Here the scale is used to not - # scale up and down too quickly - desired_capacity = asg["DesiredCapacity"] + ((queued - capacity_reserve) // 5) + # Let's calculate a new desired capacity + desired_capacity = asg["DesiredCapacity"] + ( + (queued - capacity_reserve) // scale_up + ) desired_capacity = max(desired_capacity, asg["MinSize"]) desired_capacity = min(desired_capacity, asg["MaxSize"]) # Finally, should the capacity be even changed @@ -168,8 +183,7 @@ def set_capacity( # Now we will calculate if we need to scale down stop = stop or asg["DesiredCapacity"] <= asg["MinSize"] stop = stop or asg["DesiredCapacity"] <= running - # Scale down quicker than scale up - desired_capacity = asg["DesiredCapacity"] - (capacity_reserve // 3) + desired_capacity = asg["DesiredCapacity"] - (capacity_reserve // scale_down) desired_capacity = max(desired_capacity, asg["MinSize"]) desired_capacity = min(desired_capacity, asg["MaxSize"]) stop = stop or asg["DesiredCapacity"] == desired_capacity From a5764643604dff138150ad7719b7c0781ea75593 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Fri, 3 Mar 2023 12:01:05 +0100 Subject: [PATCH 249/342] Reduce statisctics interval, process empty response for runners --- tests/ci/autoscale_runners_lambda/app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ci/autoscale_runners_lambda/app.py b/tests/ci/autoscale_runners_lambda/app.py index 25c6c2eb9a8..ca173be2eab 100644 --- a/tests/ci/autoscale_runners_lambda/app.py +++ b/tests/ci/autoscale_runners_lambda/app.py @@ -35,7 +35,7 @@ FROM FROM default.workflow_jobs WHERE has(labels, 'self-hosted') AND hasAny({RUNNER_TYPE_LABELS}, labels) - AND started_at > now() - INTERVAL 2 DAY + AND started_at > now() - INTERVAL 1 DAY GROUP BY ALL HAVING last_status IN ('in_progress', 'queued') ) @@ -231,6 +231,7 @@ def main(dry_run: bool = True) -> None: for queue in queues if runner_type in queue["labels"] ] + runner_queues = runner_queues or [Queue("in_progress", 0, runner_type)] set_capacity(runner_type, runner_queues, asg_client, dry_run) From 602933ce1edd9f02ec35b47ef428a49685fa123c Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Fri, 3 Mar 2023 12:09:09 +0100 Subject: [PATCH 250/342] Make smarter scaler based on reserve/deficit --- tests/ci/autoscale_runners_lambda/app.py | 19 +++++++++---------- tests/ci/autoscale_runners_lambda_test.py | 5 ++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/ci/autoscale_runners_lambda/app.py b/tests/ci/autoscale_runners_lambda/app.py index ca173be2eab..596e675ee24 100644 --- a/tests/ci/autoscale_runners_lambda/app.py +++ b/tests/ci/autoscale_runners_lambda/app.py @@ -145,18 +145,18 @@ def set_capacity( raise ValueError("Queue status is not in ['in_progress', 'queued']") scale_down, scale_up = get_scales(runner_type) - capacity_reserve = max(0, asg["DesiredCapacity"] - running) + # How much nodes are free (positive) or need to be added (negative) + capacity_reserve = asg["DesiredCapacity"] - running - queued stop = False - if queued: + if capacity_reserve < 0: # This part is about scaling up - # First, let's check if there's enough runners to cover the queue - stop = stop or (asg["DesiredCapacity"] - running - queued) > 0 - + capacity_deficit = -capacity_reserve + # It looks that we are still OK, since no queued jobs exist + stop = stop or queued == 0 + # Are we already at the capacity limits stop = stop or asg["MaxSize"] <= asg["DesiredCapacity"] # Let's calculate a new desired capacity - desired_capacity = asg["DesiredCapacity"] + ( - (queued - capacity_reserve) // scale_up - ) + desired_capacity = asg["DesiredCapacity"] + (capacity_deficit // scale_up) desired_capacity = max(desired_capacity, asg["MinSize"]) desired_capacity = min(desired_capacity, asg["MaxSize"]) # Finally, should the capacity be even changed @@ -181,8 +181,7 @@ def set_capacity( return # Now we will calculate if we need to scale down - stop = stop or asg["DesiredCapacity"] <= asg["MinSize"] - stop = stop or asg["DesiredCapacity"] <= running + stop = stop or asg["DesiredCapacity"] == asg["MinSize"] desired_capacity = asg["DesiredCapacity"] - (capacity_reserve // scale_down) desired_capacity = max(desired_capacity, asg["MinSize"]) desired_capacity = min(desired_capacity, asg["MaxSize"]) diff --git a/tests/ci/autoscale_runners_lambda_test.py b/tests/ci/autoscale_runners_lambda_test.py index e6ac157ee4a..7efa0004745 100644 --- a/tests/ci/autoscale_runners_lambda_test.py +++ b/tests/ci/autoscale_runners_lambda_test.py @@ -67,10 +67,7 @@ class TestSetCapacity(unittest.TestCase): test_cases = ( # Do not change capacity TestCase("noqueue", 1, 13, 20, [Queue("in_progress", 155, "noqueue")], -1), - TestCase("w/reserve", 1, 13, 20, [Queue("queued", 5, "w/reserve")], -1), TestCase("w/reserve", 1, 13, 20, [Queue("queued", 17, "w/reserve")], -1), - TestCase("w/reserve", 1, 23, 20, [Queue("queued", 17, "w/reserve")], -1), - TestCase("less-min", 10, 3, 20, [Queue("queued", 1, "less-min")], -1), # Increase capacity TestCase("increase", 1, 13, 20, [Queue("queued", 23, "increase")], 15), TestCase("increase", 1, 13, 20, [Queue("queued", 18, "increase")], 14), @@ -88,6 +85,8 @@ class TestSetCapacity(unittest.TestCase): ), TestCase("lower-min", 10, 5, 20, [Queue("queued", 5, "lower-min")], 10), # Decrease capacity + TestCase("w/reserve", 1, 13, 20, [Queue("queued", 5, "w/reserve")], 11), + TestCase("w/reserve", 1, 23, 20, [Queue("queued", 17, "w/reserve")], 20), TestCase("decrease", 1, 13, 20, [Queue("in_progress", 3, "decrease")], 10), TestCase("decrease", 1, 13, 20, [Queue("in_progress", 5, "decrease")], 11), ) From 1bbf5acd47b86e19f6767619d372466bca28bec2 Mon Sep 17 00:00:00 2001 From: Konstantin Bogdanov Date: Fri, 3 Mar 2023 15:55:52 +0300 Subject: [PATCH 251/342] Pass headers from StorageURL to WriteBufferFromHTTP (#46996) * Pass headers from StorageURL to WriteBufferFromHTTP * Add a test * Lint * `time.sleep(1)` * Start echo server earlier * Add proper handling for mock server start * Automatic style fix --------- Co-authored-by: robot-clickhouse --- src/IO/WriteBufferFromHTTP.cpp | 4 ++ src/IO/WriteBufferFromHTTP.h | 2 + src/Storages/StorageURL.cpp | 11 +++- src/Storages/StorageURL.h | 1 + .../test_storage_url_http_headers/__init__.py | 0 .../http_headers_echo_server.py | 31 +++++++++ .../test_storage_url_http_headers/test.py | 66 +++++++++++++++++++ 7 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 tests/integration/test_storage_url_http_headers/__init__.py create mode 100644 tests/integration/test_storage_url_http_headers/http_headers_echo_server.py create mode 100644 tests/integration/test_storage_url_http_headers/test.py diff --git a/src/IO/WriteBufferFromHTTP.cpp b/src/IO/WriteBufferFromHTTP.cpp index f7456ad6b6c..355c42a23c9 100644 --- a/src/IO/WriteBufferFromHTTP.cpp +++ b/src/IO/WriteBufferFromHTTP.cpp @@ -11,6 +11,7 @@ WriteBufferFromHTTP::WriteBufferFromHTTP( const std::string & method, const std::string & content_type, const std::string & content_encoding, + const HTTPHeaderEntries & additional_headers, const ConnectionTimeouts & timeouts, size_t buffer_size_) : WriteBufferFromOStream(buffer_size_) @@ -28,6 +29,9 @@ WriteBufferFromHTTP::WriteBufferFromHTTP( if (!content_encoding.empty()) request.set("Content-Encoding", content_encoding); + for (const auto & header: additional_headers) + request.add(header.name, header.value); + LOG_TRACE((&Poco::Logger::get("WriteBufferToHTTP")), "Sending request to {}", uri.toString()); ostr = &session->sendRequest(request); diff --git a/src/IO/WriteBufferFromHTTP.h b/src/IO/WriteBufferFromHTTP.h index 6966bc8a5c5..ce5020dfa78 100644 --- a/src/IO/WriteBufferFromHTTP.h +++ b/src/IO/WriteBufferFromHTTP.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ public: const std::string & method = Poco::Net::HTTPRequest::HTTP_POST, // POST or PUT only const std::string & content_type = "", const std::string & content_encoding = "", + const HTTPHeaderEntries & additional_headers = {}, const ConnectionTimeouts & timeouts = {}, size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE); diff --git a/src/Storages/StorageURL.cpp b/src/Storages/StorageURL.cpp index 152dda8f360..c0ddb0bc48a 100644 --- a/src/Storages/StorageURL.cpp +++ b/src/Storages/StorageURL.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -458,6 +459,7 @@ StorageURLSink::StorageURLSink( ContextPtr context, const ConnectionTimeouts & timeouts, const CompressionMethod compression_method, + const HTTPHeaderEntries & headers, const String & http_method) : SinkToStorage(sample_block) { @@ -465,7 +467,7 @@ StorageURLSink::StorageURLSink( std::string content_encoding = toContentEncodingName(compression_method); write_buf = wrapWriteBufferWithCompressionMethod( - std::make_unique(Poco::URI(uri), http_method, content_type, content_encoding, timeouts), + std::make_unique(Poco::URI(uri), http_method, content_type, content_encoding, headers, timeouts), compression_method, 3); writer = FormatFactory::instance().getOutputFormat(format, *write_buf, sample_block, context, format_settings); @@ -530,6 +532,7 @@ public: ContextPtr context_, const ConnectionTimeouts & timeouts_, const CompressionMethod compression_method_, + const HTTPHeaderEntries & headers_, const String & http_method_) : PartitionedSink(partition_by, context_, sample_block_) , uri(uri_) @@ -539,6 +542,7 @@ public: , context(context_) , timeouts(timeouts_) , compression_method(compression_method_) + , headers(headers_) , http_method(http_method_) { } @@ -548,7 +552,7 @@ public: auto partition_path = PartitionedSink::replaceWildcards(uri, partition_id); context->getRemoteHostFilter().checkURL(Poco::URI(partition_path)); return std::make_shared( - partition_path, format, format_settings, sample_block, context, timeouts, compression_method, http_method); + partition_path, format, format_settings, sample_block, context, timeouts, compression_method, headers, http_method); } private: @@ -560,6 +564,7 @@ private: const ConnectionTimeouts timeouts; const CompressionMethod compression_method; + const HTTPHeaderEntries headers; const String http_method; }; @@ -821,6 +826,7 @@ SinkToStoragePtr IStorageURLBase::write(const ASTPtr & query, const StorageMetad context, getHTTPTimeouts(context), compression_method, + headers, http_method); } else @@ -833,6 +839,7 @@ SinkToStoragePtr IStorageURLBase::write(const ASTPtr & query, const StorageMetad context, getHTTPTimeouts(context), compression_method, + headers, http_method); } } diff --git a/src/Storages/StorageURL.h b/src/Storages/StorageURL.h index acf49f3cb71..1cfffc3e73a 100644 --- a/src/Storages/StorageURL.h +++ b/src/Storages/StorageURL.h @@ -137,6 +137,7 @@ public: ContextPtr context, const ConnectionTimeouts & timeouts, CompressionMethod compression_method, + const HTTPHeaderEntries & headers = {}, const String & method = Poco::Net::HTTPRequest::HTTP_POST); std::string getName() const override { return "StorageURLSink"; } diff --git a/tests/integration/test_storage_url_http_headers/__init__.py b/tests/integration/test_storage_url_http_headers/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/integration/test_storage_url_http_headers/http_headers_echo_server.py b/tests/integration/test_storage_url_http_headers/http_headers_echo_server.py new file mode 100644 index 00000000000..b1a3f6777b1 --- /dev/null +++ b/tests/integration/test_storage_url_http_headers/http_headers_echo_server.py @@ -0,0 +1,31 @@ +import http.server + +RESULT_PATH = "/headers.txt" + + +class RequestHandler(http.server.BaseHTTPRequestHandler): + def log_message(self, *args): + with open(RESULT_PATH, "w") as f: + f.write(self.headers.as_string()) + + def do_POST(self): + self.rfile.read1() + self.send_response(200) + self.end_headers() + self.wfile.write(b'{"status":"ok"}') + + +if __name__ == "__main__": + with open(RESULT_PATH, "w") as f: + f.write("") + httpd = http.server.HTTPServer( + ( + "localhost", + 8000, + ), + RequestHandler, + ) + try: + httpd.serve_forever() + finally: + httpd.server_close() diff --git a/tests/integration/test_storage_url_http_headers/test.py b/tests/integration/test_storage_url_http_headers/test.py new file mode 100644 index 00000000000..3bbf5ec81c9 --- /dev/null +++ b/tests/integration/test_storage_url_http_headers/test.py @@ -0,0 +1,66 @@ +import pytest +import os +import time + +from . import http_headers_echo_server + +from helpers.cluster import ClickHouseCluster + +cluster = ClickHouseCluster(__file__) +server = cluster.add_instance("node") + + +def run_echo_server(): + script_dir = os.path.dirname(os.path.realpath(__file__)) + + server.copy_file_to_container( + os.path.join(script_dir, "http_headers_echo_server.py"), + "/http_headers_echo_server.py", + ) + + server.exec_in_container( + [ + "bash", + "-c", + "python3 /http_headers_echo_server.py > /http_headers_echo.server.log 2>&1", + ], + detach=True, + user="root", + ) + + for _ in range(0, 10): + ping_response = server.exec_in_container( + ["curl", "-s", f"http://localhost:8000/"], + nothrow=True, + ) + + if "html" in ping_response: + return + + print(ping_response) + + raise Exception("Echo server is not responding") + + +@pytest.fixture(scope="module") +def started_cluster(): + try: + cluster.start() + run_echo_server() + yield cluster + finally: + cluster.shutdown() + + +def test_storage_url_http_headers(started_cluster): + query = "INSERT INTO TABLE FUNCTION url('http://localhost:8000/', JSON, 'a UInt64', headers('X-My-Custom-Header'='test-header')) VALUES (1)" + + server.query(query) + + result = server.exec_in_container( + ["cat", http_headers_echo_server.RESULT_PATH], user="root" + ) + + print(result) + + assert "X-My-Custom-Header: test-header" in result From 6c9225063674f01285f98a05382e9a80aee74f97 Mon Sep 17 00:00:00 2001 From: DanRoscigno Date: Fri, 3 Mar 2023 08:21:02 -0500 Subject: [PATCH 252/342] add compression types for file function --- docs/en/sql-reference/table-functions/file.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/en/sql-reference/table-functions/file.md b/docs/en/sql-reference/table-functions/file.md index d2ef66dde73..594c328c3ff 100644 --- a/docs/en/sql-reference/table-functions/file.md +++ b/docs/en/sql-reference/table-functions/file.md @@ -6,21 +6,22 @@ sidebar_label: file # file -Creates a table from a file. This table function is similar to [url](../../sql-reference/table-functions/url.md) and [hdfs](../../sql-reference/table-functions/hdfs.md) ones. +Creates a table from a file. This table function is similar to [url](/docs/en/sql-reference/table-functions/url.md) and [hdfs](/docs/en/sql-reference/table-functions/hdfs.md) ones. -`file` function can be used in `SELECT` and `INSERT` queries on data in [File](../../engines/table-engines/special/file.md) tables. +`file` function can be used in `SELECT` and `INSERT` queries on data in [File](/docs/en/engines/table-engines/special/file.md) tables. **Syntax** ``` sql -file(path [,format] [,structure]) +file(path [,format] [,structure] [,compression]) ``` **Parameters** -- `path` — The relative path to the file from [user_files_path](../../operations/server-configuration-parameters/settings.md#server_configuration_parameters-user_files_path). Path to file support following globs in read-only mode: `*`, `?`, `{abc,def}` and `{N..M}` where `N`, `M` — numbers, `'abc', 'def'` — strings. -- `format` — The [format](../../interfaces/formats.md#formats) of the file. +- `path` — The relative path to the file from [user_files_path](/docs/en/operations/server-configuration-parameters/settings.md#server_configuration_parameters-user_files_path). Path to file support following globs in read-only mode: `*`, `?`, `{abc,def}` and `{N..M}` where `N`, `M` — numbers, `'abc', 'def'` — strings. +- `format` — The [format](/docs/en/interfaces/formats.md#formats) of the file. - `structure` — Structure of the table. Format: `'column1_name column1_type, column2_name column2_type, ...'`. +- `compression` — The existing compression type when used in a `SELECT` query, or the desired compression type when used in an `INSERT` query. The supported compression types are `gz`, `br`, `xz`, `zst`, `lz4`, and `bz2`. **Returned value** @@ -53,7 +54,7 @@ SELECT * FROM file('test.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 U └─────────┴─────────┴─────────┘ ``` -Getting the first 10 lines of a table that contains 3 columns of [UInt32](../../sql-reference/data-types/int-uint.md) type from a CSV file: +Getting the first 10 lines of a table that contains 3 columns of [UInt32](/docs/en/sql-reference/data-types/int-uint.md) type from a CSV file: ``` sql SELECT * FROM file('test.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32') LIMIT 10; @@ -143,4 +144,4 @@ SELECT count(*) FROM file('big_dir/**/file002', 'CSV', 'name String, value UInt3 **See Also** -- [Virtual columns](../../engines/table-engines/index.md#table_engines-virtual_columns) +- [Virtual columns](/docs/en/engines/table-engines/index.md#table_engines-virtual_columns) From 221ac4a1e57fa124f81a677a3b8630518bcbe4bd Mon Sep 17 00:00:00 2001 From: avogar Date: Fri, 3 Mar 2023 13:23:44 +0000 Subject: [PATCH 253/342] Better --- src/Interpreters/ProcessList.cpp | 49 ++++++++++++-------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/src/Interpreters/ProcessList.cpp b/src/Interpreters/ProcessList.cpp index 52674dc1c77..6f5553f58fd 100644 --- a/src/Interpreters/ProcessList.cpp +++ b/src/Interpreters/ProcessList.cpp @@ -362,7 +362,9 @@ QueryStatus::QueryStatus( QueryStatus::~QueryStatus() { - assert(executors.empty()); + /// Check that all executors were invalidated. + for (const auto & e : executors) + assert(!e->executor); if (auto * memory_tracker = getMemoryTracker()) { @@ -394,35 +396,21 @@ CancellationCode QueryStatus::cancelQuery(bool) is_killed.store(true); std::unique_lock lock(executors_mutex); - - /// Track all cancelled executors. - std::unordered_set cancelled; - /// We cancel executors from the left to the right, so if the last executor - /// was cancelled, then all executors were cancelled. - while (!cancelled.contains(executors.back().get())) + for (const auto & e : executors) { - size_t size = executors.size(); - /// We should create a copy of executor holder, because it can be - /// removed from vector in removePipelineExecutor from another thread - /// and reference will be invalid. - for (auto e : executors) - { - if (cancelled.contains(e.get())) - continue; - /// We should call cancel() with unlocked executors_mutex, because - /// cancel() can try to lock some internal mutex that is already locked by query executing - /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, - /// which will lead to deadlock. - lock.unlock(); - e->cancel(); - lock.lock(); - cancelled.insert(e.get()); - /// While executors_mutex was unlocked, removePipelineExecutor could be called and - /// the size of executors could have changed. In this case we should start iterating - /// over it again to avoid using invalid iterators. - if (executors.size() != size) - break; - } + /// We should call cancel() with unlocked executors_mutex, because + /// cancel() can try to lock some internal mutex that is already locked by query executing + /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, + /// which will lead to deadlock. + /// Note that the size and the content of executors cannot be changed while + /// executors_mutex is unlocked, because: + /// 1) We don't allow adding new executors while cancelling query in addPipelineExecutor + /// 2) We don't actually remove executor holder from executors in removePipelineExecutor, + /// just mark that executor is invalid. + /// So, it's safe to continue iteration over executors after subsequent mutex locking. + lock.unlock(); + e->cancel(); + lock.lock(); } return CancellationCode::CancelSent; @@ -446,9 +434,8 @@ void QueryStatus::removePipelineExecutor(PipelineExecutor * e) std::lock_guard lock(executors_mutex); auto it = std::find_if(executors.begin(), executors.end(), [e](const ExecutorHolderPtr & x){ return x->executor == e; }); assert(it != executors.end()); - /// Invalidate executor pointer inside holder. + /// Invalidate executor pointer inside holder, but don't remove holder from the executors (to avoid race with cancelQuery) (*it)->remove(); - executors.erase(it); } bool QueryStatus::checkTimeLimit() From e44e5281182c2cda7aef51bdabff529d337d1f3a Mon Sep 17 00:00:00 2001 From: Suzy Wang Date: Fri, 3 Mar 2023 06:20:18 -0800 Subject: [PATCH 254/342] remove unnecessary reverse memcpy --- src/Common/formatIPv6.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Common/formatIPv6.h b/src/Common/formatIPv6.h index bc8f70f047c..7b88f93750b 100644 --- a/src/Common/formatIPv6.h +++ b/src/Common/formatIPv6.h @@ -82,11 +82,7 @@ inline bool parseIPv4(T * &src, EOFfunction eof, unsigned char * dst, int32_t fi break; } - if constexpr (std::endian::native == std::endian::little) - memcpy(dst, &result, sizeof(result)); - else - reverseMemcpy(dst, &result, sizeof(result)); - + memcpy(dst, &result, sizeof(result)); return true; } From a8ceab136635a9238a431dea5d651c9731a10e6a Mon Sep 17 00:00:00 2001 From: Yakov Olkhovskiy <99031427+yakov-olkhovskiy@users.noreply.github.com> Date: Fri, 3 Mar 2023 09:24:58 -0500 Subject: [PATCH 255/342] NOLINT for getenv --- utils/self-extracting-executable/decompressor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/self-extracting-executable/decompressor.cpp b/utils/self-extracting-executable/decompressor.cpp index 39fe552dfac..8db0d95e70d 100644 --- a/utils/self-extracting-executable/decompressor.cpp +++ b/utils/self-extracting-executable/decompressor.cpp @@ -170,7 +170,7 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n bool isSudo() { - return getuid() == 0 && geteuid() == 0 && getenv("SUDO_USER") && getenv("SUDO_UID") && getenv("SUDO_GID"); + return getuid() == 0 && geteuid() == 0 && getenv("SUDO_USER") && getenv("SUDO_UID") && getenv("SUDO_GID"); // NOLINT(concurrency-mt-unsafe) } /// Read data about files and decomrpess them. From 737cf8e14934f7b4ce4869241fb67e461143bc45 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Fri, 3 Mar 2023 15:14:49 +0000 Subject: [PATCH 256/342] Better --- src/Client/HedgedConnections.cpp | 4 +- src/Client/MultiplexedConnections.cpp | 4 +- src/Core/Settings.h | 3 +- src/Core/SettingsEnums.cpp | 5 - src/Core/SettingsEnums.h | 9 - src/Interpreters/Cluster.cpp | 2 +- src/Interpreters/Context.cpp | 23 ++- src/Interpreters/Context.h | 9 + src/Interpreters/InterpreterSelectQuery.cpp | 45 +++-- .../getCustomKeyFilterForParallelReplicas.cpp | 49 ++--- .../getCustomKeyFilterForParallelReplicas.h | 7 +- .../MergeTree/MergeTreeDataSelectExecutor.cpp | 5 +- src/Storages/SelectQueryInfo.h | 2 + src/Storages/StorageDistributed.cpp | 55 +----- .../test_parallel_replicas_custom_key/test.py | 74 ++------ ...4_shard_distributed_with_many_replicas.sql | 2 +- ...here_max_parallel_replicas_distributed.sql | 5 +- .../01034_sample_final_distributed.sql | 5 +- .../01557_max_parallel_replicas_no_sample.sql | 3 +- .../02221_parallel_replicas_bug.sh | 2 +- ...arallel_reading_from_replicas_benchmark.sh | 1 - .../02404_memory_bound_merging.sql | 3 +- ...max_parallel_replicas_custom_key.reference | 175 +++++++++++------- .../02535_max_parallel_replicas_custom_key.sh | 12 +- 24 files changed, 222 insertions(+), 282 deletions(-) diff --git a/src/Client/HedgedConnections.cpp b/src/Client/HedgedConnections.cpp index b1fa6886e84..b97f9454fa5 100644 --- a/src/Client/HedgedConnections.cpp +++ b/src/Client/HedgedConnections.cpp @@ -175,9 +175,7 @@ void HedgedConnections::sendQuery( modified_settings.group_by_two_level_threshold_bytes = 0; } - const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 - && (settings.parallel_replicas_mode != ParallelReplicasMode::READ_TASKS - || !settings.allow_experimental_parallel_reading_from_replicas); + const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 && !settings.allow_experimental_parallel_reading_from_replicas; if (offset_states.size() > 1 && enable_sample_offset_parallel_processing) { diff --git a/src/Client/MultiplexedConnections.cpp b/src/Client/MultiplexedConnections.cpp index ec6788105ca..cc260353339 100644 --- a/src/Client/MultiplexedConnections.cpp +++ b/src/Client/MultiplexedConnections.cpp @@ -141,9 +141,7 @@ void MultiplexedConnections::sendQuery( } } - const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 - && (settings.parallel_replicas_mode != ParallelReplicasMode::READ_TASKS - || !settings.allow_experimental_parallel_reading_from_replicas); + const bool enable_sample_offset_parallel_processing = settings.max_parallel_replicas > 1 && !settings.allow_experimental_parallel_reading_from_replicas; size_t num_replicas = replica_states.size(); if (num_replicas > 1) diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 83aa6104996..797f57f1c04 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -147,9 +147,8 @@ class IColumn; M(UInt64, max_parallel_replicas, 1, "The maximum number of replicas of each shard used when the query is executed. For consistency (to get different parts of the same partition), this option only works for the specified sampling key. The lag of the replicas is not controlled.", 0) \ M(UInt64, parallel_replicas_count, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the number of parallel replicas participating in query processing.", 0) \ M(UInt64, parallel_replica_offset, 0, "This is internal setting that should not be used directly and represents an implementation detail of the 'parallel replicas' mode. This setting will be automatically set up by the initiator server for distributed queries to the index of the replica participating in query processing among parallel replicas.", 0) \ - M(Map, parallel_replicas_custom_key, "", "Custom key for parallel replicas using modulo operation on the key for assigning work to replicas.", 0) \ + M(String, parallel_replicas_custom_key, "", "Custom key assigning work to replicas when parallel replicas are used.", 0) \ M(ParallelReplicasCustomKeyFilterType, parallel_replicas_custom_key_filter_type, ParallelReplicasCustomKeyFilterType::DEFAULT, "Type of filter to use with custom key for parallel replicas. default - use modulo operation on the custom key, range - use range filter on custom key using all possible values for the value type of custom key.", 0) \ - M(ParallelReplicasMode, parallel_replicas_mode, ParallelReplicasMode::SAMPLE_KEY, "How to process query using multiple replicas.", 0) \ \ M(String, cluster_for_parallel_replicas, "default", "Cluster for a shard in which current server is located", 0) \ M(Bool, allow_experimental_parallel_reading_from_replicas, false, "If true, ClickHouse will send a SELECT query to all replicas of a table. It will work for any kind on MergeTree table.", 0) \ diff --git a/src/Core/SettingsEnums.cpp b/src/Core/SettingsEnums.cpp index d5bd6cb2ae6..b04757ecaa2 100644 --- a/src/Core/SettingsEnums.cpp +++ b/src/Core/SettingsEnums.cpp @@ -167,11 +167,6 @@ IMPLEMENT_SETTING_ENUM(Dialect, ErrorCodes::BAD_ARGUMENTS, {{"clickhouse", Dialect::clickhouse}, {"kusto", Dialect::kusto}}) -IMPLEMENT_SETTING_ENUM(ParallelReplicasMode, ErrorCodes::BAD_ARGUMENTS, - {{"sample_key", ParallelReplicasMode::SAMPLE_KEY}, - {"custom_key", ParallelReplicasMode::CUSTOM_KEY}, - {"read_tasks", ParallelReplicasMode::READ_TASKS}}) - IMPLEMENT_SETTING_ENUM(ParallelReplicasCustomKeyFilterType, ErrorCodes::BAD_ARGUMENTS, {{"default", ParallelReplicasCustomKeyFilterType::DEFAULT}, {"range", ParallelReplicasCustomKeyFilterType::RANGE}}) diff --git a/src/Core/SettingsEnums.h b/src/Core/SettingsEnums.h index 5bb36cec60d..ae9456cc6d7 100644 --- a/src/Core/SettingsEnums.h +++ b/src/Core/SettingsEnums.h @@ -203,15 +203,6 @@ enum class Dialect DECLARE_SETTING_ENUM(Dialect) -enum class ParallelReplicasMode : uint8_t -{ - SAMPLE_KEY, - CUSTOM_KEY, - READ_TASKS, -}; - -DECLARE_SETTING_ENUM(ParallelReplicasMode) - enum class ParallelReplicasCustomKeyFilterType : uint8_t { DEFAULT, diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index 1975fa29686..b419dacd523 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -752,7 +752,7 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti auto shuffled_replicas = replicas; // shuffle replicas so we don't always pick the same subset shuffleReplicas(shuffled_replicas, settings); - create_shards_from_replicas(std::span{shuffled_replicas.begin(), shuffled_replicas.begin() + max_replicas_from_shard}); + create_shards_from_replicas(std::span{shuffled_replicas.begin(), max_replicas_from_shard}); } } diff --git a/src/Interpreters/Context.cpp b/src/Interpreters/Context.cpp index 08a3cf206c5..90f9c3bbac4 100644 --- a/src/Interpreters/Context.cpp +++ b/src/Interpreters/Context.cpp @@ -4056,23 +4056,34 @@ std::shared_ptr Context::getAsyncReadCounters() const return async_read_counters; } +Context::ParallelReplicasMode Context::getParallelReplicasMode() const +{ + const auto & settings = getSettingsRef(); + + using enum Context::ParallelReplicasMode; + if (!settings.parallel_replicas_custom_key.value.empty()) + return CUSTOM_KEY; + + if (settings.allow_experimental_parallel_reading_from_replicas + && !settings.use_hedged_requests) + return READ_TASKS; + + return SAMPLE_KEY; +} + bool Context::canUseParallelReplicasOnInitiator() const { const auto & settings = getSettingsRef(); - return settings.allow_experimental_parallel_reading_from_replicas - && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS + return getParallelReplicasMode() == ParallelReplicasMode::READ_TASKS && settings.max_parallel_replicas > 1 - && !settings.use_hedged_requests && !getClientInfo().collaborate_with_initiator; } bool Context::canUseParallelReplicasOnFollower() const { const auto & settings = getSettingsRef(); - return settings.allow_experimental_parallel_reading_from_replicas - && settings.parallel_replicas_mode == ParallelReplicasMode::READ_TASKS + return getParallelReplicasMode() == ParallelReplicasMode::READ_TASKS && settings.max_parallel_replicas > 1 - && !settings.use_hedged_requests && getClientInfo().collaborate_with_initiator; } diff --git a/src/Interpreters/Context.h b/src/Interpreters/Context.h index 19bb6868331..67594a41459 100644 --- a/src/Interpreters/Context.h +++ b/src/Interpreters/Context.h @@ -1123,6 +1123,15 @@ public: bool canUseParallelReplicasOnInitiator() const; bool canUseParallelReplicasOnFollower() const; + enum class ParallelReplicasMode : uint8_t + { + SAMPLE_KEY, + CUSTOM_KEY, + READ_TASKS, + }; + + ParallelReplicasMode getParallelReplicasMode() const; + private: std::unique_lock getLock() const; diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index e84df90c449..c53734d1e9b 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -517,29 +517,38 @@ InterpreterSelectQuery::InterpreterSelectQuery( settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); ASTPtr parallel_replicas_custom_filter_ast = nullptr; - if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY && !joined_tables.tablesWithColumns().empty()) + if (context->getParallelReplicasMode() == Context::ParallelReplicasMode::CUSTOM_KEY && !joined_tables.tablesWithColumns().empty()) { - if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, joined_tables.tablesWithColumns().front().table, *context)) + if (settings.parallel_replicas_count > 1) { - LOG_INFO(log, "Processing query on a replica using custom_key"); - if (!storage) - throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); + if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, *context)) + { + LOG_INFO(log, "Processing query on a replica using custom_key"); + if (!storage) + throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); - parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica( - settings.parallel_replicas_count, - settings.parallel_replica_offset, - std::move(custom_key_ast), - settings.parallel_replicas_custom_key_filter_type, - *storage, - context); + parallel_replicas_custom_filter_ast = getCustomKeyFilterForParallelReplica( + settings.parallel_replicas_count, + settings.parallel_replica_offset, + std::move(custom_key_ast), + settings.parallel_replicas_custom_key_filter_type, + *storage, + context); + } + else if (settings.parallel_replica_offset > 0) + { + LOG_DEBUG( + log, + "Will use no data on this replica because parallel replicas processing with custom_key has been requested" + " (setting 'max_parallel_replicas') but the table does not have custom_key defined for it or it's invalid (settings `parallel_replicas_custom_key`)"); + parallel_replicas_custom_filter_ast = std::make_shared(false); + } } - else if (settings.parallel_replica_offset > 0) + else if (auto * distributed = dynamic_cast(storage.get()); + distributed && canUseCustomKey(settings, *distributed->getCluster(), *context)) { - LOG_DEBUG( - log, - "Will use no data on this replica because parallel replicas processing with custom_key has been requested" - " (setting 'max_parallel_replicas') but the table does not have custom_key defined for it (settings `parallel_replicas_custom_key`)"); - parallel_replicas_custom_filter_ast = std::make_shared(false); + query_info.use_custom_key = true; + context->setSetting("distributed_group_by_no_merge", 2); } } diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp index 229668ceff4..2a32d450497 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.cpp @@ -20,6 +20,12 @@ namespace ErrorCodes extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; } +bool canUseCustomKey(const Settings & settings, const Cluster & cluster, const Context & context) +{ + return settings.max_parallel_replicas > 1 && context.getParallelReplicasMode() == Context::ParallelReplicasMode::CUSTOM_KEY + && cluster.getShardCount() == 1 && cluster.getShardsInfo()[0].getAllNodeCount() > 1; +} + ASTPtr getCustomKeyFilterForParallelReplica( size_t replicas_count, size_t replica_num, @@ -115,43 +121,14 @@ ASTPtr getCustomKeyFilterForParallelReplica( return makeASTFunction("and", std::move(lower_function), std::move(upper_function)); } -ASTPtr parseCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context) +ASTPtr parseCustomKeyForTable(const String & custom_key, const Context & context) { - for (size_t i = 0; i < custom_keys.size(); ++i) - { - const auto & tuple = custom_keys[i].safeGet(); - auto & table = tuple.at(0).safeGet(); - auto & filter = tuple.at(1).safeGet(); - - if (table == target.alias || - (table == target.table && context.getCurrentDatabase() == target.database) || - (table == target.database + '.' + target.table)) - { - /// Try to parse expression - ParserExpression parser; - const auto & settings = context.getSettingsRef(); - return parseQuery( - parser, filter.data(), filter.data() + filter.size(), - "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); - } - } - - return nullptr; -} - -bool containsCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context) -{ - for (size_t i = 0; i < custom_keys.size(); ++i) - { - const auto & tuple = custom_keys[i].safeGet(); - auto & table = tuple.at(0).safeGet(); - - if (table == target.alias || - (table == target.table && context.getCurrentDatabase() == target.database) || - (table == target.database + '.' + target.table)) - return true; - } - return false; + /// Try to parse expression + ParserExpression parser; + const auto & settings = context.getSettingsRef(); + return parseQuery( + parser, custom_key.data(), custom_key.data() + custom_key.size(), + "parallel replicas custom key", settings.max_query_size, settings.max_parser_depth); } } diff --git a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h index fc515132487..543f1889b32 100644 --- a/src/Interpreters/getCustomKeyFilterForParallelReplicas.h +++ b/src/Interpreters/getCustomKeyFilterForParallelReplicas.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -9,6 +10,8 @@ namespace DB { +bool canUseCustomKey(const Settings & settings, const Cluster & cluster, const Context & context); + /// Get AST for filter created from custom_key /// replica_num is the number of the replica for which we are generating filter starting from 0 ASTPtr getCustomKeyFilterForParallelReplica( @@ -19,8 +22,6 @@ ASTPtr getCustomKeyFilterForParallelReplica( const IStorage & storage, const ContextPtr & context); -ASTPtr parseCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context); - -bool containsCustomKeyForTable(const Map & custom_keys, const DatabaseAndTableWithAlias & target, const Context & context); +ASTPtr parseCustomKeyForTable(const String & custom_keys, const Context & context); } diff --git a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index 7e914486326..07da66e4378 100644 --- a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -588,9 +588,10 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( * It is also important that the entire universe can be covered using SAMPLE 0.1 OFFSET 0, ... OFFSET 0.9 and similar decimals. */ + auto parallel_replicas_mode = context->getParallelReplicasMode(); /// Parallel replicas has been requested but there is no way to sample data. /// Select all data from first replica and no data from other replicas. - if (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY + if (settings.parallel_replicas_count > 1 && parallel_replicas_mode == Context::ParallelReplicasMode::SAMPLE_KEY && !data.supportsSampling() && settings.parallel_replica_offset > 0) { LOG_DEBUG( @@ -602,7 +603,7 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling( } sampling.use_sampling = relative_sample_size > 0 - || (settings.parallel_replicas_count > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::SAMPLE_KEY + || (settings.parallel_replicas_count > 1 && parallel_replicas_mode == Context::ParallelReplicasMode::SAMPLE_KEY && data.supportsSampling()); bool no_data = false; /// There is nothing left after sampling. diff --git a/src/Storages/SelectQueryInfo.h b/src/Storages/SelectQueryInfo.h index 7a3452f87d4..e3996950e79 100644 --- a/src/Storages/SelectQueryInfo.h +++ b/src/Storages/SelectQueryInfo.h @@ -207,6 +207,8 @@ struct SelectQueryInfo /// /// Configured in StorageDistributed::getQueryProcessingStage() ClusterPtr optimized_cluster; + /// should we use custom key with the cluster + bool use_custom_key = false; mutable ParallelReplicasReadingCoordinatorPtr coordinator; diff --git a/src/Storages/StorageDistributed.cpp b/src/Storages/StorageDistributed.cpp index b088a008b46..259caaf394a 100644 --- a/src/Storages/StorageDistributed.cpp +++ b/src/Storages/StorageDistributed.cpp @@ -264,12 +264,6 @@ size_t getClusterQueriedNodes(const Settings & settings, const ClusterPtr & clus return (num_remote_shards + num_local_shards) * settings.max_parallel_replicas; } -bool canUseCustomKey(const Settings & settings, const Cluster & cluster) -{ - return settings.max_parallel_replicas > 1 && settings.parallel_replicas_mode == ParallelReplicasMode::CUSTOM_KEY - && cluster.getShardCount() == 1 && cluster.getShardsInfo()[0].getAllNodeCount() > 1; -} - } /// For destruction of std::unique_ptr of type that is incomplete in class definition. @@ -412,36 +406,9 @@ QueryProcessingStage::Enum StorageDistributed::getQueryProcessingStage( size_t nodes = getClusterQueriedNodes(settings, cluster); - const auto use_virtual_shards = [&] - { - if (!canUseCustomKey(settings, *cluster)) - return false; - - auto distributed_table = DatabaseAndTableWithAlias( - *getTableExpression(query_info.query->as(), 0), local_context->getCurrentDatabase()); - - if (containsCustomKeyForTable(settings.parallel_replicas_custom_key, distributed_table, *local_context)) - { - LOG_INFO(log, "Found custom_key for {}", distributed_table.getQualifiedNamePrefix(false)); - return true; - } - - DatabaseAndTableWithAlias remote_table_info; - remote_table_info.database = remote_database; - remote_table_info.table = remote_table; - if (containsCustomKeyForTable(settings.parallel_replicas_custom_key, remote_table_info, *local_context)) - { - LOG_INFO(log, "Found custom_key for {}", remote_table_info.getQualifiedNamePrefix(false)); - return true; - } - - return false; - }; - - if (use_virtual_shards()) + if (query_info.use_custom_key) { LOG_INFO(log, "Single shard cluster used with custom_key, transforming replicas into virtual shards"); - query_info.cluster = cluster->getClusterWithReplicasAsShards(settings, settings.max_parallel_replicas); } else @@ -816,25 +783,9 @@ void StorageDistributed::read( auto settings = local_context->getSettingsRef(); ClusterProxy::AdditionalShardFilterGenerator additional_shard_filter_generator; - if (canUseCustomKey(settings, *getCluster())) + if (query_info.use_custom_key) { - const auto get_custom_key_ast = [&]() -> ASTPtr - { - auto distributed_table = DatabaseAndTableWithAlias( - *getTableExpression(query_info.query->as(), 0), local_context->getCurrentDatabase()); - if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, distributed_table, *local_context)) - return custom_key_ast; - - DatabaseAndTableWithAlias remote_table_info; - remote_table_info.database = remote_database; - remote_table_info.table = remote_table; - if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, remote_table_info, *local_context)) - return custom_key_ast; - - return nullptr; - }; - - if (auto custom_key_ast = get_custom_key_ast()) + if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, *local_context)) { if (query_info.getCluster()->getShardCount() == 1) { diff --git a/tests/integration/test_parallel_replicas_custom_key/test.py b/tests/integration/test_parallel_replicas_custom_key/test.py index 9222f417a94..342abdcb088 100644 --- a/tests/integration/test_parallel_replicas_custom_key/test.py +++ b/tests/integration/test_parallel_replicas_custom_key/test.py @@ -44,11 +44,11 @@ def create_tables(cluster): def insert_data(cluster, row_num): create_tables(cluster) n1 = nodes[0] - n1.query(f"INSERT INTO dist_table SELECT number, number FROM numbers({row_num})") + n1.query(f"INSERT INTO dist_table SELECT number % 4, number FROM numbers({row_num})") n1.query("SYSTEM FLUSH DISTRIBUTED dist_table") -@pytest.mark.parametrize("custom_key", ["sipHash64(value)", "key"]) +@pytest.mark.parametrize("custom_key", ["sipHash64(key)", "key"]) @pytest.mark.parametrize("filter_type", ["default", "range"]) @pytest.mark.parametrize( "cluster", @@ -61,21 +61,22 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter row_num = 1000 insert_data(cluster, row_num) + expected_result = "" + for i in range(4): + expected_result += f"{i}\t250\n" + n1 = nodes[0] assert ( - int( - n1.query( - "SELECT count() FROM dist_table", - settings={ - "prefer_localhost_replica": 0, - "max_parallel_replicas": 4, - "parallel_replicas_mode": "custom_key", - "parallel_replicas_custom_key": f"{{'test_table': '{custom_key}'}}", - "parallel_replicas_custom_key_filter_type": filter_type, - }, - ) + n1.query( + "SELECT key, count() FROM dist_table GROUP BY key ORDER BY key", + settings={ + "prefer_localhost_replica": 0, + "max_parallel_replicas": 4, + "parallel_replicas_custom_key": custom_key, + "parallel_replicas_custom_key_filter_type": filter_type, + }, ) - == row_num + == expected_result ) if cluster == "test_multiple_shards_multiple_replicas": @@ -89,48 +90,3 @@ def test_parallel_replicas_custom_key(start_cluster, cluster, custom_key, filter assert n1.contains_in_log( "Single shard cluster used with custom_key, transforming replicas into virtual shards" ) - - -def test_custom_key_different_table_names(start_cluster): - def run(table_source, table_name_for_custom_key, should_use_virtual_shard=True): - for node in nodes: - node.rotate_logs() - - row_num = 1000 - insert_data("test_single_shard_multiple_replicas", row_num) - - n1 = nodes[0] - assert ( - int( - n1.query( - f"SELECT count() FROM {table_source}", - settings={ - "prefer_localhost_replica": 0, - "max_parallel_replicas": 4, - "parallel_replicas_mode": "custom_key", - "parallel_replicas_custom_key": f"{{'{table_name_for_custom_key}': 'sipHash64(value)'}}", - }, - ) - ) - == row_num - ) - - # we first transform all replicas into shards and then append for each shard filter - assert not should_use_virtual_shard or n1.contains_in_log( - "Single shard cluster used with custom_key, transforming replicas into virtual shards" - ) - - run("dist_table", "dist_table") - run("dist_table as d", "d") - run("dist_table as d", "dist_table") - run("dist_table as d", "test_table") - run( - "cluster('test_single_shard_multiple_replicas', default.test_table)", - "test_table", - ) - run("cluster('test_single_shard_multiple_replicas', default.test_table) as d", "d") - run( - "cluster('test_single_shard_multiple_replicas', default.test_table) as d", - "test_table", - ) - run("dist_table as d", "dist", should_use_virtual_shard=False) diff --git a/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql b/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql index 795551e5dfa..901b818cbc0 100644 --- a/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql +++ b/tests/queries/0_stateless/00124_shard_distributed_with_many_replicas.sql @@ -1,7 +1,7 @@ -- Tags: replica, distributed +SET allow_experimental_parallel_reading_from_replicas = 0; SET max_parallel_replicas = 2; -SET parallel_replicas_mode = 'sample_key'; DROP TABLE IF EXISTS report; set allow_deprecated_syntax_for_merge_tree=1; diff --git a/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql b/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql index 96d52d7e60e..86c84427297 100644 --- a/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql +++ b/tests/queries/0_stateless/01034_prewhere_max_parallel_replicas_distributed.sql @@ -1,5 +1,7 @@ -- Tags: replica, distributed +set allow_experimental_parallel_reading_from_replicas=0; + drop table if exists test_max_parallel_replicas_lr; -- If you wonder why the table is named with "_lr" suffix in this test. @@ -9,9 +11,6 @@ CREATE TABLE test_max_parallel_replicas_lr (timestamp UInt64) ENGINE = MergeTree INSERT INTO test_max_parallel_replicas_lr select number as timestamp from system.numbers limit 100; SET max_parallel_replicas = 2; -SET parallel_replicas_mode = 'sample_key'; -SET allow_experimental_parallel_reading_from_replicas = 0; - select count() FROM remote('127.0.0.{2|3}', currentDatabase(), test_max_parallel_replicas_lr) PREWHERE timestamp > 0; drop table test_max_parallel_replicas_lr; diff --git a/tests/queries/0_stateless/01034_sample_final_distributed.sql b/tests/queries/0_stateless/01034_sample_final_distributed.sql index 1b1832f7a0d..a81fef645db 100644 --- a/tests/queries/0_stateless/01034_sample_final_distributed.sql +++ b/tests/queries/0_stateless/01034_sample_final_distributed.sql @@ -1,5 +1,7 @@ -- Tags: distributed +set allow_experimental_parallel_reading_from_replicas = 0; + drop table if exists sample_final; create table sample_final (CounterID UInt32, EventDate Date, EventTime DateTime, UserID UInt64, Sign Int8) engine = CollapsingMergeTree(Sign) order by (CounterID, EventDate, intHash32(UserID), EventTime) sample by intHash32(UserID); insert into sample_final select number / (8192 * 4), toDate('2019-01-01'), toDateTime('2019-01-01 00:00:01') + number, number / (8192 * 2), number % 3 = 1 ? -1 : 1 from numbers(1000000); @@ -13,10 +15,7 @@ select count() from sample_final sample 1/2; select 'count sample final'; select count() from sample_final final sample 1/2; select 'count final max_parallel_replicas'; - -set allow_experimental_parallel_reading_from_replicas = 0; set max_parallel_replicas=2; -set parallel_replicas_mode='sample_key'; select count() from remote('127.0.0.{2|3}', currentDatabase(), sample_final) final; drop table if exists sample_final; diff --git a/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql b/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql index c44c335700f..04777f5b31c 100644 --- a/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql +++ b/tests/queries/0_stateless/01557_max_parallel_replicas_no_sample.sql @@ -1,10 +1,11 @@ -- Tags: replica +SET allow_experimental_parallel_reading_from_replicas=0; + DROP TABLE IF EXISTS t; CREATE TABLE t (x String) ENGINE = MergeTree ORDER BY x; INSERT INTO t VALUES ('Hello'); -SET parallel_replicas_mode = 'sample_key'; SET max_parallel_replicas = 3; SELECT * FROM remote('127.0.0.{2|3|4}', currentDatabase(), t); diff --git a/tests/queries/0_stateless/02221_parallel_replicas_bug.sh b/tests/queries/0_stateless/02221_parallel_replicas_bug.sh index 1a703b20d2a..cce32bf8272 100755 --- a/tests/queries/0_stateless/02221_parallel_replicas_bug.sh +++ b/tests/queries/0_stateless/02221_parallel_replicas_bug.sh @@ -4,4 +4,4 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) . "$CURDIR"/../shell_config.sh -${CLICKHOUSE_CLIENT} --allow_experimental_parallel_reading_from_replicas=1 --parallel_replicas_mode='read_tasks' -nm < "$CURDIR"/01099_parallel_distributed_insert_select.sql > /dev/null +${CLICKHOUSE_CLIENT} --allow_experimental_parallel_reading_from_replicas=1 -nm < "$CURDIR"/01099_parallel_distributed_insert_select.sql > /dev/null diff --git a/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh b/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh index 283c51cf0a2..2a163746e20 100755 --- a/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh +++ b/tests/queries/0_stateless/02226_parallel_reading_from_replicas_benchmark.sh @@ -15,7 +15,6 @@ as select * from numbers(1); # Logical error: 'Coordinator for parallel reading from replicas is not initialized'. opts=( --allow_experimental_parallel_reading_from_replicas 1 - --parallel_replicas_mode 'read_tasks' --max_parallel_replicas 3 --iterations 1 diff --git a/tests/queries/0_stateless/02404_memory_bound_merging.sql b/tests/queries/0_stateless/02404_memory_bound_merging.sql index 6e6bc5a3b6c..b6299de9aae 100644 --- a/tests/queries/0_stateless/02404_memory_bound_merging.sql +++ b/tests/queries/0_stateless/02404_memory_bound_merging.sql @@ -1,4 +1,4 @@ --- Tags: no-parallel, long, no-random-merge-tree-settings +-- Tags: no-parallel, no-random-merge-tree-settings drop table if exists pr_t; drop table if exists dist_t_different_dbs; @@ -61,7 +61,6 @@ create table pr_t(a UInt64, b UInt64) engine=MergeTree order by a; insert into pr_t select number % 1000, number % 1000 from numbers_mt(1e6); set allow_experimental_parallel_reading_from_replicas = 1; -set parallel_replicas_mode = 'read_tasks'; set max_parallel_replicas = 3; set use_hedged_requests = 0; set cluster_for_parallel_replicas = 'test_cluster_one_shard_three_replicas_localhost'; diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference index 21ed26b7579..9e4e24235aa 100644 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference @@ -23,103 +23,150 @@ filter_type='range' max_replicas=2 prefer_localhost_replica=1 Hello filter_type='range' max_replicas=3 prefer_localhost_replica=1 Hello -query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x)' +query='SELECT y, count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key) GROUP BY y ORDER BY y' with custom_key='y' filter_type='default' max_replicas=1 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=2 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=3 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=1 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=2 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=3 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=1 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=2 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=3 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=1 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=2 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=3 prefer_localhost_replica=1 -1000 -query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='y' +0 334 +1 333 +2 333 +query='SELECT y, count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key) GROUP BY y ORDER BY y' with custom_key='cityHash64(y)' filter_type='default' max_replicas=1 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=2 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=3 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=1 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=2 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=3 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=1 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=2 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=3 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=1 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=2 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=3 prefer_localhost_replica=1 -1000 -query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x) + y' +0 334 +1 333 +2 333 +query='SELECT y, count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key) GROUP BY y ORDER BY y' with custom_key='cityHash64(y) + 1' filter_type='default' max_replicas=1 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=2 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=3 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=1 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=2 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=3 prefer_localhost_replica=0 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=1 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=2 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='default' max_replicas=3 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=1 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=2 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 filter_type='range' max_replicas=3 prefer_localhost_replica=1 -1000 -query='SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)' with custom_key='cityHash64(x) + 1' -filter_type='default' max_replicas=1 prefer_localhost_replica=0 -1000 -filter_type='default' max_replicas=2 prefer_localhost_replica=0 -1000 -filter_type='default' max_replicas=3 prefer_localhost_replica=0 -1000 -filter_type='range' max_replicas=1 prefer_localhost_replica=0 -1000 -filter_type='range' max_replicas=2 prefer_localhost_replica=0 -1000 -filter_type='range' max_replicas=3 prefer_localhost_replica=0 -1000 -filter_type='default' max_replicas=1 prefer_localhost_replica=1 -1000 -filter_type='default' max_replicas=2 prefer_localhost_replica=1 -1000 -filter_type='default' max_replicas=3 prefer_localhost_replica=1 -1000 -filter_type='range' max_replicas=1 prefer_localhost_replica=1 -1000 -filter_type='range' max_replicas=2 prefer_localhost_replica=1 -1000 -filter_type='range' max_replicas=3 prefer_localhost_replica=1 -1000 +0 334 +1 333 +2 333 diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh index b6e5c853dc2..314251be874 100755 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh @@ -12,8 +12,7 @@ function run_with_custom_key { for max_replicas in {1..3}; do echo "filter_type='$filter_type' max_replicas=$max_replicas prefer_localhost_replica=$prefer_localhost_replica" query="$1 SETTINGS max_parallel_replicas=$max_replicas\ - , parallel_replicas_mode='custom_key'\ - , parallel_replicas_custom_key={'02535_custom_key': '$2'}\ + , parallel_replicas_custom_key='$2'\ , parallel_replicas_custom_key_filter_type='$filter_type'\ , prefer_localhost_replica=$prefer_localhost_replica" $CLICKHOUSE_CLIENT --query="$query" @@ -32,15 +31,14 @@ run_with_custom_key "SELECT * FROM cluster(test_cluster_one_shard_three_replicas $CLICKHOUSE_CLIENT --query="DROP TABLE 02535_custom_key" $CLICKHOUSE_CLIENT --query="CREATE TABLE 02535_custom_key (x String, y Int32) ENGINE = MergeTree ORDER BY cityHash64(x)" -$CLICKHOUSE_CLIENT --query="INSERT INTO 02535_custom_key SELECT toString(number), number FROM numbers(1000)" +$CLICKHOUSE_CLIENT --query="INSERT INTO 02535_custom_key SELECT toString(number), number % 3 FROM numbers(1000)" function run_count_with_custom_key { - run_with_custom_key "SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key)" "$1" + run_with_custom_key "SELECT y, count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key) GROUP BY y ORDER BY y" "$1" } -run_count_with_custom_key "cityHash64(x)" run_count_with_custom_key "y" -run_count_with_custom_key "cityHash64(x) + y" -run_count_with_custom_key "cityHash64(x) + 1" +run_count_with_custom_key "cityHash64(y)" +run_count_with_custom_key "cityHash64(y) + 1" $CLICKHOUSE_CLIENT --query="DROP TABLE 02535_custom_key" From 46667d43d88d2b30acdae30b9316df385ec20369 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Fri, 3 Mar 2023 15:25:25 +0000 Subject: [PATCH 257/342] Automatic style fix --- tests/integration/test_parallel_replicas_custom_key/test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_parallel_replicas_custom_key/test.py b/tests/integration/test_parallel_replicas_custom_key/test.py index 342abdcb088..baac2661506 100644 --- a/tests/integration/test_parallel_replicas_custom_key/test.py +++ b/tests/integration/test_parallel_replicas_custom_key/test.py @@ -44,7 +44,9 @@ def create_tables(cluster): def insert_data(cluster, row_num): create_tables(cluster) n1 = nodes[0] - n1.query(f"INSERT INTO dist_table SELECT number % 4, number FROM numbers({row_num})") + n1.query( + f"INSERT INTO dist_table SELECT number % 4, number FROM numbers({row_num})" + ) n1.query("SYSTEM FLUSH DISTRIBUTED dist_table") From 0f2ae721411afc2b3150bde3a4a12c5426118f6f Mon Sep 17 00:00:00 2001 From: AVMusorin Date: Thu, 2 Mar 2023 13:51:21 +0100 Subject: [PATCH 258/342] prohibit DEFAULT/EPHEMERAL/ALIAS in KafkaEngine --- .../table-engines/integrations/kafka.md | 4 ++ .../sql-reference/statements/create/table.md | 4 ++ src/Storages/ColumnsDescription.cpp | 9 ++++ src/Storages/ColumnsDescription.h | 3 ++ src/Storages/Kafka/StorageKafka.cpp | 5 ++ tests/integration/test_storage_kafka/test.py | 50 +++++++++++++++++++ 6 files changed, 75 insertions(+) diff --git a/docs/en/engines/table-engines/integrations/kafka.md b/docs/en/engines/table-engines/integrations/kafka.md index ef422632d3e..e6134043b8e 100644 --- a/docs/en/engines/table-engines/integrations/kafka.md +++ b/docs/en/engines/table-engines/integrations/kafka.md @@ -125,6 +125,10 @@ Groups are flexible and synced on the cluster. For instance, if you have 10 topi 2. Create a table with the desired structure. 3. Create a materialized view that converts data from the engine and puts it into a previously created table. +:::info +Kafka Engine doesn't support columns with default value of type `DEFAULT/MATERIALIZED/EPHEMERAL/ALIAS`. If you need columns with any default type, they can be added at `MATERIALIZED VIEW` level. +::: + When the `MATERIALIZED VIEW` joins the engine, it starts collecting data in the background. This allows you to continually receive messages from Kafka and convert them to the required format using `SELECT`. One kafka table can have as many materialized views as you like, they do not read data from the kafka table directly, but receive new records (in blocks), this way you can write to several tables with different detail level (with grouping - aggregation and without). diff --git a/docs/en/sql-reference/statements/create/table.md b/docs/en/sql-reference/statements/create/table.md index 9e66afba613..ba495b0eed5 100644 --- a/docs/en/sql-reference/statements/create/table.md +++ b/docs/en/sql-reference/statements/create/table.md @@ -121,6 +121,10 @@ If the data type and default expression are defined explicitly, this expression Default expressions may be defined as an arbitrary expression from table constants and columns. When creating and changing the table structure, it checks that expressions do not contain loops. For INSERT, it checks that expressions are resolvable – that all columns they can be calculated from have been passed. +:::info +Kafka Engine doesn't support columns with default value of type `DEFAULT/MATERIALIZED/EPHEMERAL/ALIAS`. If you need columns with any default type, they can be added at `MATERIALIZED VIEW` level, see [Kafka Engine](../../../engines/table-engines/integrations/kafka.md#description). +::: + ### DEFAULT `DEFAULT expr` diff --git a/src/Storages/ColumnsDescription.cpp b/src/Storages/ColumnsDescription.cpp index d401840eec7..fa39e304925 100644 --- a/src/Storages/ColumnsDescription.cpp +++ b/src/Storages/ColumnsDescription.cpp @@ -383,6 +383,15 @@ NamesAndTypesList ColumnsDescription::getEphemeral() const return ret; } +NamesAndTypesList ColumnsDescription::getWithDefaultExpression() const +{ + NamesAndTypesList ret; + for (const auto & col : columns) + if (col.default_desc.expression) + ret.emplace_back(col.name, col.type); + return ret; +} + NamesAndTypesList ColumnsDescription::getAll() const { NamesAndTypesList ret; diff --git a/src/Storages/ColumnsDescription.h b/src/Storages/ColumnsDescription.h index 4f874f4b850..36109392ab6 100644 --- a/src/Storages/ColumnsDescription.h +++ b/src/Storages/ColumnsDescription.h @@ -132,6 +132,9 @@ public: NamesAndTypesList getInsertable() const; /// ordinary + ephemeral NamesAndTypesList getAliases() const; NamesAndTypesList getEphemeral() const; + // Columns with preset default expression. + // For example from `CREATE TABLE` statement + NamesAndTypesList getWithDefaultExpression() const; NamesAndTypesList getAllPhysical() const; /// ordinary + materialized. NamesAndTypesList getAll() const; /// ordinary + materialized + aliases + ephemeral /// Returns .size0/.null/... diff --git a/src/Storages/Kafka/StorageKafka.cpp b/src/Storages/Kafka/StorageKafka.cpp index 7b97273d8af..2afdc0dda8a 100644 --- a/src/Storages/Kafka/StorageKafka.cpp +++ b/src/Storages/Kafka/StorageKafka.cpp @@ -959,6 +959,11 @@ void registerStorageKafka(StorageFactory & factory) { throw Exception(ErrorCodes::BAD_ARGUMENTS, "kafka_poll_max_batch_size can not be lower than 1"); } + if (args.columns.getOrdinary() != args.columns.getAll() || !args.columns.getWithDefaultExpression().empty()) + { + throw Exception(ErrorCodes::BAD_ARGUMENTS, "KafkaEngine doesn't support DEFAULT/MATERIALIZED/EPHEMERAL/ALIAS expressions for columns. " + "See https://clickhouse.com/docs/en/engines/table-engines/integrations/kafka/#configuration"); + } return std::make_shared(args.table_id, args.getContext(), args.columns, std::move(kafka_settings), collection_name); }; diff --git a/tests/integration/test_storage_kafka/test.py b/tests/integration/test_storage_kafka/test.py index 9f617369859..51952ac1eb7 100644 --- a/tests/integration/test_storage_kafka/test.py +++ b/tests/integration/test_storage_kafka/test.py @@ -285,6 +285,56 @@ def avro_confluent_message(schema_registry_client, value): # Tests +def test_kafka_prohibited_column_types(kafka_cluster): + def assert_returned_exception(e): + assert e.value.returncode == 36 + assert ( + "KafkaEngine doesn't support DEFAULT/MATERIALIZED/EPHEMERAL/ALIAS expressions for columns." + in str(e.value) + ) + + # check column with DEFAULT expression + with pytest.raises(QueryRuntimeException) as exception: + instance.query( + """ + CREATE TABLE test.kafka (a Int, b Int DEFAULT 0) + ENGINE = Kafka('{kafka_broker}:19092', '{kafka_topic_new}', '{kafka_group_name_new}', '{kafka_format_json_each_row}', '\\n') + """ + ) + assert_returned_exception(exception) + + # check EPHEMERAL + with pytest.raises(QueryRuntimeException) as exception: + instance.query( + """ + CREATE TABLE test.kafka (a Int, b Int EPHEMERAL) + ENGINE = Kafka('{kafka_broker}:19092', '{kafka_topic_new}', '{kafka_group_name_new}', '{kafka_format_json_each_row}', '\\n') + """ + ) + assert_returned_exception(exception) + + # check ALIAS + with pytest.raises(QueryRuntimeException) as exception: + instance.query( + """ + CREATE TABLE test.kafka (a Int, b String Alias toString(a)) + ENGINE = Kafka('{kafka_broker}:19092', '{kafka_topic_new}', '{kafka_group_name_new}', '{kafka_format_json_each_row}', '\\n') + """ + ) + assert_returned_exception(exception) + + # check MATERIALIZED + # check ALIAS + with pytest.raises(QueryRuntimeException) as exception: + instance.query( + """ + CREATE TABLE test.kafka (a Int, b String MATERIALIZED toString(a)) + ENGINE = Kafka('{kafka_broker}:19092', '{kafka_topic_new}', '{kafka_group_name_new}', '{kafka_format_json_each_row}', '\\n') + """ + ) + assert_returned_exception(exception) + + def test_kafka_settings_old_syntax(kafka_cluster): assert TSV( instance.query( From 5403360924ef984e8e8d7cb517f71ab2e6ea0f5a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Fri, 3 Mar 2023 16:25:42 +0100 Subject: [PATCH 259/342] Add real client (initiator server) address into the logs for interserver mode It is useful to understand what is going on, in some obscure cases, for instance if someone will copy configuration from the production to some docker env, and then you will see docker's private network addresses in the logs. Signed-off-by: Azat Khuzhin --- src/Server/TCPHandler.cpp | 29 +++++++++++++++++++---------- src/Server/TCPHandler.h | 2 ++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index a307b472a64..f2b4284613a 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -1208,14 +1208,7 @@ void TCPHandler::receiveHello() session = makeSession(); auto & client_info = session->getClientInfo(); - - /// Extract the last entry from comma separated list of forwarded_for addresses. - /// Only the last proxy can be trusted (if any). - String forwarded_address = client_info.getLastForwardedFor(); - if (!forwarded_address.empty() && server.config().getBool("auth_use_forwarded_address", false)) - session->authenticate(user, password, Poco::Net::SocketAddress(forwarded_address, socket().peerAddress().port())); - else - session->authenticate(user, password, socket().peerAddress()); + session->authenticate(user, password, getClientAddress(client_info)); } void TCPHandler::receiveAddendum() @@ -1501,11 +1494,16 @@ void TCPHandler::receiveQuery() /// so we should not rely on that. However, in this particular case we got client_info from other clickhouse-server, so it's ok. if (client_info.initial_user.empty()) { - LOG_DEBUG(log, "User (no user, interserver mode)"); + LOG_DEBUG(log, "User (no user, interserver mode) (client: {})", getClientAddress(client_info).toString()); } else { - LOG_DEBUG(log, "User (initial, interserver mode): {}", client_info.initial_user); + LOG_DEBUG(log, "User (initial, interserver mode): {} (client: {})", client_info.initial_user, getClientAddress(client_info).toString()); + /// In case of inter-server mode authorization is done with the + /// initial address of the client, not the real address from which + /// the query was come, since the real address is the address of + /// the initiator server, while we are interested in client's + /// address. session->authenticate(AlwaysAllowCredentials{client_info.initial_user}, client_info.initial_address); } #else @@ -1991,4 +1989,15 @@ void TCPHandler::run() } } +Poco::Net::SocketAddress TCPHandler::getClientAddress(const ClientInfo & client_info) +{ + /// Extract the last entry from comma separated list of forwarded_for addresses. + /// Only the last proxy can be trusted (if any). + String forwarded_address = client_info.getLastForwardedFor(); + if (!forwarded_address.empty() && server.config().getBool("auth_use_forwarded_address", false)) + return Poco::Net::SocketAddress(forwarded_address, socket().peerAddress().port()); + else + return socket().peerAddress(); +} + } diff --git a/src/Server/TCPHandler.h b/src/Server/TCPHandler.h index f06b0b060b3..e3673b213d5 100644 --- a/src/Server/TCPHandler.h +++ b/src/Server/TCPHandler.h @@ -273,6 +273,8 @@ private: /// This function is called from different threads. void updateProgress(const Progress & value); + + Poco::Net::SocketAddress getClientAddress(const ClientInfo & client_info); }; } From b5771016f6d53f460b635f10538df60d575dac23 Mon Sep 17 00:00:00 2001 From: vdimir Date: Fri, 3 Mar 2023 15:46:41 +0000 Subject: [PATCH 260/342] Fix 01019_alter_materialized_view_consistent --- .../01019_alter_materialized_view_consistent.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh b/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh index e90085f4e8e..496215ba492 100755 --- a/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh +++ b/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh @@ -50,12 +50,19 @@ function insert_thread() { function alter_thread() { trap 'exit' INT - ALTER[0]="ALTER TABLE mv MODIFY QUERY SELECT v == 1 as test, v as case FROM src_a;" - ALTER[1]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" + # Generate random ALTERs, but make sure that at least one of them is for each source table. + for i in {0..5}; do + ALTER[$i]="ALTER TABLE mv MODIFY QUERY SELECT v == 1 as test, v as case FROM src_a;" + done + ALTER[$RANDOM % 3]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" + ALTER[$RANDOM % 6]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" + ALTER[$RANDOM % 6]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" + i=0 while true; do - $CLICKHOUSE_CLIENT --allow_experimental_alter_materialized_view_structure=1 \ - -q "${ALTER[$RANDOM % 2]}" + $CLICKHOUSE_CLIENT --allow_experimental_alter_materialized_view_structure=1 -q "${ALTER[$i % 6]}" + ((i=i+1)) + sleep "0.0$RANDOM" is_done=$($CLICKHOUSE_CLIENT -q "SELECT countIf(case = 1) > 0 AND countIf(case = 2) > 0 FROM mv;") From a0f5756751a6cec5c03a56a5f1552113ffb57cfd Mon Sep 17 00:00:00 2001 From: vdimir Date: Fri, 3 Mar 2023 16:28:41 +0000 Subject: [PATCH 261/342] Fix DelayedJoinedBlocksWorkerTransform for grace hash join --- src/Interpreters/GraceHashJoin.cpp | 5 +--- src/Interpreters/GraceHashJoin.h | 8 ++---- .../Transforms/JoiningTransform.cpp | 27 +++++++++++++++++-- .../25340_grace_hash_limit_race.reference | 1 + .../25340_grace_hash_limit_race.sql | 4 +-- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Interpreters/GraceHashJoin.cpp b/src/Interpreters/GraceHashJoin.cpp index 1b62939ac7b..7795061072c 100644 --- a/src/Interpreters/GraceHashJoin.cpp +++ b/src/Interpreters/GraceHashJoin.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -410,8 +409,6 @@ void GraceHashJoin::initialize(const Block & sample_block) void GraceHashJoin::joinBlock(Block & block, std::shared_ptr & not_processed) { - std::shared_lock current_bucket_lock(current_bucket_mutex); - if (block.rows() == 0) { hash_join->joinBlock(block, not_processed); @@ -551,7 +548,7 @@ public: IBlocksStreamPtr GraceHashJoin::getDelayedBlocks() { - std::unique_lock current_bucket_lock(current_bucket_mutex); + std::lock_guard current_bucket_lock(current_bucket_mutex); if (current_bucket == nullptr) return nullptr; diff --git a/src/Interpreters/GraceHashJoin.h b/src/Interpreters/GraceHashJoin.h index 0d6c4741b95..cbc0f2f3266 100644 --- a/src/Interpreters/GraceHashJoin.h +++ b/src/Interpreters/GraceHashJoin.h @@ -140,12 +140,8 @@ private: FileBucket * current_bucket = nullptr; - /* Function `getDelayedBlocks` should be a critical section. - * Also some `joinBlock` calls may be in progress and we need to wait for them to finish, - * because they may may use `hash_join`, but `getDelayedBlocks` switches it to another bucket. - * So, `joinBlock` acquires shared lock and getDelayedBlocks acquires exclusive lock. - */ - mutable SharedMutex current_bucket_mutex; + /// Function `getDelayedBlocks` is a critical section, we process only one bucket at a time. + mutable std::mutex current_bucket_mutex; InMemoryJoinPtr hash_join; Block hash_join_sample_block; diff --git a/src/Processors/Transforms/JoiningTransform.cpp b/src/Processors/Transforms/JoiningTransform.cpp index c28a84e9d5d..bf80de67b42 100644 --- a/src/Processors/Transforms/JoiningTransform.cpp +++ b/src/Processors/Transforms/JoiningTransform.cpp @@ -318,12 +318,22 @@ DelayedJoinedBlocksWorkerTransform::DelayedJoinedBlocksWorkerTransform(Block out IProcessor::Status DelayedJoinedBlocksWorkerTransform::prepare() { + auto & output = outputs.front(); + auto & input = inputs.front(); + + if (output.isFinished()) + { + input.close(); + return Status::Finished; + } + + if (!output.canPush()) + return Status::PortFull; + if (inputs.size() != 1 && outputs.size() != 1) throw Exception(ErrorCodes::LOGICAL_ERROR, "DelayedJoinedBlocksWorkerTransform must have exactly one input port"); - auto & output = outputs.front(); - auto & input = inputs.front(); if (output_chunk) { @@ -397,6 +407,9 @@ DelayedJoinedBlocksTransform::DelayedJoinedBlocksTransform(size_t num_streams, J void DelayedJoinedBlocksTransform::work() { + if (finished) + return; + delayed_blocks = join->getDelayedBlocks(); finished = finished || delayed_blocks == nullptr; } @@ -406,6 +419,14 @@ IProcessor::Status DelayedJoinedBlocksTransform::prepare() { for (auto & output : outputs) { + if (output.isFinished()) + { + /// If at least one output is finished, then we have read all data from buckets. + /// Some workers can still can busy with joining the last chunk of data in memory, + /// but after that they also will finish when they will try to get next chunk. + finished = true; + continue; + } if (!output.canPush()) return Status::PortFull; } @@ -414,6 +435,8 @@ IProcessor::Status DelayedJoinedBlocksTransform::prepare() { for (auto & output : outputs) { + if (output.isFinished()) + continue; Chunk chunk; chunk.setChunkInfo(std::make_shared()); output.push(std::move(chunk)); diff --git a/tests/queries/0_stateless/25340_grace_hash_limit_race.reference b/tests/queries/0_stateless/25340_grace_hash_limit_race.reference index e69de29bb2d..83b33d238da 100644 --- a/tests/queries/0_stateless/25340_grace_hash_limit_race.reference +++ b/tests/queries/0_stateless/25340_grace_hash_limit_race.reference @@ -0,0 +1 @@ +1000 diff --git a/tests/queries/0_stateless/25340_grace_hash_limit_race.sql b/tests/queries/0_stateless/25340_grace_hash_limit_race.sql index b12dd7bdbba..55262ab2455 100644 --- a/tests/queries/0_stateless/25340_grace_hash_limit_race.sql +++ b/tests/queries/0_stateless/25340_grace_hash_limit_race.sql @@ -6,11 +6,11 @@ INSERT INTO test_grace_hash SELECT number, number % 100 = 0 FROM numbers(100000) SET join_algorithm = 'grace_hash'; -SELECT * FROM ( +SELECT count() FROM ( SELECT f.id FROM test_grace_hash AS f LEFT JOIN test_grace_hash AS d ON f.id = d.id LIMIT 1000 -) FORMAT Null; +); DROP TABLE test_grace_hash; From 9389cc29da406533ff589fde058a2f22fc1e9aa6 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Fri, 3 Mar 2023 16:48:47 +0000 Subject: [PATCH 262/342] better mutations of sparse columns --- .../Serializations/SerializationInfo.cpp | 32 +++++++++++++++++-- .../Serializations/SerializationInfo.h | 6 +++- .../Serializations/SerializationInfoTuple.cpp | 18 ++++++++--- .../Serializations/SerializationInfoTuple.h | 6 +++- src/Storages/MergeTree/MutateTask.cpp | 16 +++------- ...02662_sparse_columns_mutations_4.reference | 2 ++ .../02662_sparse_columns_mutations_4.sql | 21 ++++++++++++ ...02662_sparse_columns_mutations_5.reference | 2 ++ .../02662_sparse_columns_mutations_5.sql | 21 ++++++++++++ 9 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_4.reference create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_4.sql create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_5.reference create mode 100644 tests/queries/0_stateless/02662_sparse_columns_mutations_5.sql diff --git a/src/DataTypes/Serializations/SerializationInfo.cpp b/src/DataTypes/Serializations/SerializationInfo.cpp index af3330d867d..4e5790ad58d 100644 --- a/src/DataTypes/Serializations/SerializationInfo.cpp +++ b/src/DataTypes/Serializations/SerializationInfo.cpp @@ -97,11 +97,37 @@ MutableSerializationInfoPtr SerializationInfo::clone() const return std::make_shared(kind, settings, data); } -std::shared_ptr SerializationInfo::createWithType(const IDataType & type, const Settings & new_settings) const +/// Returns true if all rows with default values of type 'lhs' +/// are mapped to default values of type 'rhs' after conversion. +static bool preserveDefaultsAfterConversion(const IDataType & lhs, const IDataType & rhs) +{ + if (lhs.equals(rhs)) + return true; + + bool lhs_is_columned_as_numeric = isColumnedAsNumber(lhs) || isColumnedAsDecimal(lhs); + bool rhs_is_columned_as_numeric = isColumnedAsNumber(rhs) || isColumnedAsDecimal(rhs); + + if (lhs_is_columned_as_numeric && rhs_is_columned_as_numeric) + return true; + + if (isStringOrFixedString(lhs) && isStringOrFixedString(rhs)) + return true; + + return false; +} + +std::shared_ptr SerializationInfo::createWithType( + const IDataType & old_type, + const IDataType & new_type, + const Settings & new_settings) const { auto new_kind = kind; - if (new_kind == ISerialization::Kind::SPARSE && !type.supportsSparseSerialization()) - new_kind = ISerialization::Kind::DEFAULT; + if (new_kind == ISerialization::Kind::SPARSE) + { + if (!new_type.supportsSparseSerialization() + || !preserveDefaultsAfterConversion(old_type, new_type)) + new_kind = ISerialization::Kind::DEFAULT; + } return std::make_shared(new_kind, new_settings); } diff --git a/src/DataTypes/Serializations/SerializationInfo.h b/src/DataTypes/Serializations/SerializationInfo.h index 560156980db..5b802b379e1 100644 --- a/src/DataTypes/Serializations/SerializationInfo.h +++ b/src/DataTypes/Serializations/SerializationInfo.h @@ -60,7 +60,11 @@ public: virtual void replaceData(const SerializationInfo & other); virtual std::shared_ptr clone() const; - virtual std::shared_ptr createWithType(const IDataType & type, const Settings & new_settings) const; + + virtual std::shared_ptr createWithType( + const IDataType & old_type, + const IDataType & new_type, + const Settings & new_settings) const; virtual void serialializeKindBinary(WriteBuffer & out) const; virtual void deserializeFromKindsBinary(ReadBuffer & in); diff --git a/src/DataTypes/Serializations/SerializationInfoTuple.cpp b/src/DataTypes/Serializations/SerializationInfoTuple.cpp index 5724dd7a5c3..d36668f03b6 100644 --- a/src/DataTypes/Serializations/SerializationInfoTuple.cpp +++ b/src/DataTypes/Serializations/SerializationInfoTuple.cpp @@ -97,16 +97,24 @@ MutableSerializationInfoPtr SerializationInfoTuple::clone() const return std::make_shared(std::move(elems_cloned), names, settings); } -MutableSerializationInfoPtr SerializationInfoTuple::createWithType(const IDataType & type, const Settings & new_settings) const +MutableSerializationInfoPtr SerializationInfoTuple::createWithType( + const IDataType & old_type, + const IDataType & new_type, + const Settings & new_settings) const { - const auto & type_tuple = assert_cast(type); - const auto & tuple_elements = type_tuple.getElements(); - assert(elems.size() == tuple_elements.size()); + const auto & old_tuple = assert_cast(old_type); + const auto & new_tuple = assert_cast(new_type); + + const auto & old_elements = old_tuple.getElements(); + const auto & new_elements = new_tuple.getElements(); + + assert(elems.size() == old_elements.size()); + assert(elems.size() == new_elements.size()); MutableSerializationInfos infos; infos.reserve(elems.size()); for (size_t i = 0; i < elems.size(); ++i) - infos.push_back(elems[i]->createWithType(*tuple_elements[i], new_settings)); + infos.push_back(elems[i]->createWithType(*old_elements[i], *new_elements[i], new_settings)); return std::make_shared(std::move(infos), names, new_settings); } diff --git a/src/DataTypes/Serializations/SerializationInfoTuple.h b/src/DataTypes/Serializations/SerializationInfoTuple.h index 4bcd14d34cb..a9f3bdb6c6e 100644 --- a/src/DataTypes/Serializations/SerializationInfoTuple.h +++ b/src/DataTypes/Serializations/SerializationInfoTuple.h @@ -19,7 +19,11 @@ public: void replaceData(const SerializationInfo & other) override; MutableSerializationInfoPtr clone() const override; - MutableSerializationInfoPtr createWithType(const IDataType & type, const Settings & new_settings) const override; + + MutableSerializationInfoPtr createWithType( + const IDataType & old_type, + const IDataType & new_type, + const Settings & new_settings) const override; void serialializeKindBinary(WriteBuffer & out) const override; void deserializeFromKindsBinary(ReadBuffer & in) override; diff --git a/src/Storages/MergeTree/MutateTask.cpp b/src/Storages/MergeTree/MutateTask.cpp index 1239befdd67..94715822e52 100644 --- a/src/Storages/MergeTree/MutateTask.cpp +++ b/src/Storages/MergeTree/MutateTask.cpp @@ -219,18 +219,18 @@ getColumnsForNewDataPart( continue; } - auto old_type = part_columns.getPhysical(name); + auto old_type = part_columns.getPhysical(name).type; auto new_type = updated_header.getByName(new_name).type; - if (!new_type->supportsSparseSerialization()) - continue; - SerializationInfo::Settings settings { .ratio_of_defaults_for_sparse = source_part->storage.getSettings()->ratio_of_defaults_for_sparse_serialization, .choose_kind = false }; + if (!new_type->supportsSparseSerialization() || settings.isAlwaysDefault()) + continue; + auto new_info = new_type->createSerializationInfo(settings); if (!old_info->structureEquals(*new_info)) { @@ -238,13 +238,7 @@ getColumnsForNewDataPart( continue; } - if (!old_info->hasCustomSerialization()) - { - new_serialization_infos.emplace(new_name, old_info); - continue; - } - - new_info = old_info->createWithType(*new_type, settings); + new_info = old_info->createWithType(*old_type, *new_type, settings); new_serialization_infos.emplace(new_name, std::move(new_info)); } diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_4.reference b/tests/queries/0_stateless/02662_sparse_columns_mutations_4.reference new file mode 100644 index 00000000000..2e24ab44f9a --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_4.reference @@ -0,0 +1,2 @@ +UInt64 Sparse +String Default diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_4.sql b/tests/queries/0_stateless/02662_sparse_columns_mutations_4.sql new file mode 100644 index 00000000000..039af658489 --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_4.sql @@ -0,0 +1,21 @@ +SET mutations_sync = 2; + +DROP TABLE IF EXISTS t_sparse_mutations_4; + +CREATE TABLE t_sparse_mutations_4 (k UInt64, v UInt64) +ENGINE = MergeTree ORDER BY k +SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; + +INSERT INTO t_sparse_mutations_4 SELECT number, 0 FROM numbers(10000); + +SELECT type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_4' AND column = 'v' AND active +ORDER BY name; + +ALTER TABLE t_sparse_mutations_4 MODIFY COLUMN v String; + +SELECT type, serialization_kind FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_4' AND column = 'v' AND active +ORDER BY name; + +DROP TABLE t_sparse_mutations_4; diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_5.reference b/tests/queries/0_stateless/02662_sparse_columns_mutations_5.reference new file mode 100644 index 00000000000..698d61cbb24 --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_5.reference @@ -0,0 +1,2 @@ +Tuple(UInt64, UInt64) Default ['1','2'] ['UInt64','UInt64'] ['Sparse','Sparse'] +Tuple(UInt64, String) Default ['1','2'] ['UInt64','String'] ['Sparse','Default'] diff --git a/tests/queries/0_stateless/02662_sparse_columns_mutations_5.sql b/tests/queries/0_stateless/02662_sparse_columns_mutations_5.sql new file mode 100644 index 00000000000..79bac836bdc --- /dev/null +++ b/tests/queries/0_stateless/02662_sparse_columns_mutations_5.sql @@ -0,0 +1,21 @@ +SET mutations_sync = 2; + +DROP TABLE IF EXISTS t_sparse_mutations_5; + +CREATE TABLE t_sparse_mutations_5 (k UInt64, t Tuple(UInt64, UInt64)) +ENGINE = MergeTree ORDER BY k +SETTINGS ratio_of_defaults_for_sparse_serialization = 0.9; + +INSERT INTO t_sparse_mutations_5 SELECT number, (0, 0) FROM numbers(10000); + +SELECT type, serialization_kind, subcolumns.names, subcolumns.types, subcolumns.serializations FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_5' AND column = 't' AND active +ORDER BY name; + +ALTER TABLE t_sparse_mutations_5 MODIFY COLUMN t Tuple(UInt64, String); + +SELECT type, serialization_kind, subcolumns.names, subcolumns.types, subcolumns.serializations FROM system.parts_columns +WHERE database = currentDatabase() AND table = 't_sparse_mutations_5' AND column = 't' AND active +ORDER BY name; + +DROP TABLE t_sparse_mutations_5; From bbbe529e249adb3847f45571b83086fc805c6071 Mon Sep 17 00:00:00 2001 From: HarryLeeIBM Date: Fri, 3 Mar 2023 09:29:35 -0800 Subject: [PATCH 263/342] Fix farmhash for s390x --- contrib/libfarmhash/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contrib/libfarmhash/CMakeLists.txt b/contrib/libfarmhash/CMakeLists.txt index a0533a93f17..436bc3d0108 100644 --- a/contrib/libfarmhash/CMakeLists.txt +++ b/contrib/libfarmhash/CMakeLists.txt @@ -6,6 +6,10 @@ if (MSVC) target_compile_definitions (_farmhash PRIVATE FARMHASH_NO_BUILTIN_EXPECT=1) endif () +if (ARCH_S390X) + add_compile_definitions(WORDS_BIGENDIAN) +endif () + target_include_directories (_farmhash BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) add_library(ch_contrib::farmhash ALIAS _farmhash) From 6c929737eb4d5c62377927889988d5e5dda9ba25 Mon Sep 17 00:00:00 2001 From: Dave Lahn Date: Fri, 3 Mar 2023 18:42:10 +0000 Subject: [PATCH 264/342] Add missing policy name element The S3 cache example had a list of policies, but was missing the policy name. This caused this configuration to fail. --- docs/en/operations/storing-data.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/en/operations/storing-data.md b/docs/en/operations/storing-data.md index 203fe4e42d2..cba666f73da 100644 --- a/docs/en/operations/storing-data.md +++ b/docs/en/operations/storing-data.md @@ -135,11 +135,13 @@ Example of configuration for versions later or equal to 22.8: - -
- cache -
-
+ + +
+ cache +
+
+
``` From 7997df5739e509132a596ec4782b39e6e389970c Mon Sep 17 00:00:00 2001 From: Kseniia Sumarokova <54203879+kssenii@users.noreply.github.com> Date: Fri, 3 Mar 2023 19:50:21 +0100 Subject: [PATCH 265/342] Update storing-data.md --- docs/en/operations/storing-data.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/en/operations/storing-data.md b/docs/en/operations/storing-data.md index cba666f73da..3f9a0f67187 100644 --- a/docs/en/operations/storing-data.md +++ b/docs/en/operations/storing-data.md @@ -161,11 +161,13 @@ Example of configuration for versions earlier than 22.8:
- -
- s3 -
-
+ + +
+ s3 +
+
+
``` From 5c4da5aa4a08ea2fe1cfa279fc1ec8c4e6e7f68f Mon Sep 17 00:00:00 2001 From: Nikita Mikhaylov Date: Fri, 3 Mar 2023 20:05:42 +0100 Subject: [PATCH 266/342] Use separate thread pool for IO operations for backups (#47174) --- programs/server/Server.cpp | 6 ++++++ src/Backups/BackupIO_S3.cpp | 8 ++++---- src/Core/ServerSettings.h | 4 +++- src/IO/BackupIOThreadPool.cpp | 34 ++++++++++++++++++++++++++++++++++ src/IO/BackupsIOThreadPool.h | 20 ++++++++++++++++++++ 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/IO/BackupIOThreadPool.cpp create mode 100644 src/IO/BackupsIOThreadPool.h diff --git a/programs/server/Server.cpp b/programs/server/Server.cpp index 1486a51c710..57d361886d2 100644 --- a/programs/server/Server.cpp +++ b/programs/server/Server.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -773,6 +774,11 @@ try server_settings.max_io_thread_pool_free_size, server_settings.io_thread_pool_queue_size); + BackupsIOThreadPool::initialize( + server_settings.max_backups_io_thread_pool_size, + server_settings.max_backups_io_thread_pool_free_size, + server_settings.backups_io_thread_pool_queue_size); + /// Initialize global local cache for remote filesystem. if (config().has("local_cache_for_remote_fs")) { diff --git a/src/Backups/BackupIO_S3.cpp b/src/Backups/BackupIO_S3.cpp index a303a0bc2d5..2f315e8d488 100644 --- a/src/Backups/BackupIO_S3.cpp +++ b/src/Backups/BackupIO_S3.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include @@ -167,7 +167,7 @@ void BackupWriterS3::copyFileNative(DiskPtr src_disk, const String & src_file_na std::string src_bucket = object_storage->getObjectsNamespace(); auto file_path = fs::path(s3_uri.key) / dest_file_name; copyS3File(client, src_bucket, objects[0].absolute_path, src_offset, src_size, s3_uri.bucket, file_path, request_settings, {}, - threadPoolCallbackRunner(IOThreadPool::get(), "BackupWriterS3")); + threadPoolCallbackRunner(BackupsIOThreadPool::get(), "BackupWriterS3")); } } @@ -175,7 +175,7 @@ void BackupWriterS3::copyDataToFile( const CreateReadBufferFunction & create_read_buffer, UInt64 offset, UInt64 size, const String & dest_file_name) { copyDataToS3File(create_read_buffer, offset, size, client, s3_uri.bucket, fs::path(s3_uri.key) / dest_file_name, request_settings, {}, - threadPoolCallbackRunner(IOThreadPool::get(), "BackupWriterS3")); + threadPoolCallbackRunner(BackupsIOThreadPool::get(), "BackupWriterS3")); } BackupWriterS3::~BackupWriterS3() = default; @@ -222,7 +222,7 @@ std::unique_ptr BackupWriterS3::writeFile(const String & file_name) request_settings, std::nullopt, DBMS_DEFAULT_BUFFER_SIZE, - threadPoolCallbackRunner(IOThreadPool::get(), "BackupWriterS3")); + threadPoolCallbackRunner(BackupsIOThreadPool::get(), "BackupWriterS3")); } void BackupWriterS3::removeFile(const String & file_name) diff --git a/src/Core/ServerSettings.h b/src/Core/ServerSettings.h index 1e884266c17..abc5b4d14d8 100644 --- a/src/Core/ServerSettings.h +++ b/src/Core/ServerSettings.h @@ -21,6 +21,9 @@ namespace DB M(UInt64, max_io_thread_pool_size, 100, "The maximum number of threads that would be used for IO operations", 0) \ M(UInt64, max_io_thread_pool_free_size, 0, "Max free size for IO thread pool.", 0) \ M(UInt64, io_thread_pool_queue_size, 10000, "Queue size for IO thread pool.", 0) \ + M(UInt64, max_backups_io_thread_pool_size, 1000, "The maximum number of threads that would be used for IO operations for BACKUP queries", 0) \ + M(UInt64, max_backups_io_thread_pool_free_size, 0, "Max free size for backups IO thread pool.", 0) \ + M(UInt64, backups_io_thread_pool_queue_size, 0, "Queue size for backups IO thread pool.", 0) \ M(Int32, max_connections, 1024, "Max server connections.", 0) \ M(UInt32, asynchronous_metrics_update_period_s, 1, "Period in seconds for updating asynchronous metrics.", 0) \ M(UInt32, asynchronous_heavy_metrics_update_period_s, 120, "Period in seconds for updating asynchronous metrics.", 0) \ @@ -75,4 +78,3 @@ struct ServerSettings : public BaseSettings }; } - diff --git a/src/IO/BackupIOThreadPool.cpp b/src/IO/BackupIOThreadPool.cpp new file mode 100644 index 00000000000..067fc54b1ae --- /dev/null +++ b/src/IO/BackupIOThreadPool.cpp @@ -0,0 +1,34 @@ +#include +#include "Core/Field.h" + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int LOGICAL_ERROR; +} + +std::unique_ptr BackupsIOThreadPool::instance; + +void BackupsIOThreadPool::initialize(size_t max_threads, size_t max_free_threads, size_t queue_size) +{ + if (instance) + { + throw Exception(ErrorCodes::LOGICAL_ERROR, "The BackupsIO thread pool is initialized twice"); + } + + instance = std::make_unique(max_threads, max_free_threads, queue_size, false /*shutdown_on_exception*/); +} + +ThreadPool & BackupsIOThreadPool::get() +{ + if (!instance) + { + throw Exception(ErrorCodes::LOGICAL_ERROR, "The BackupsIO thread pool is not initialized"); + } + + return *instance; +} + +} diff --git a/src/IO/BackupsIOThreadPool.h b/src/IO/BackupsIOThreadPool.h new file mode 100644 index 00000000000..f5aae5741a8 --- /dev/null +++ b/src/IO/BackupsIOThreadPool.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +namespace DB +{ + +/* + * ThreadPool used for the Backup IO. + */ +class BackupsIOThreadPool +{ + static std::unique_ptr instance; + +public: + static void initialize(size_t max_threads, size_t max_free_threads, size_t queue_size); + static ThreadPool & get(); +}; + +} From ea8ae193d3ae40215493e450e34dc4a188a981b4 Mon Sep 17 00:00:00 2001 From: liding1992 Date: Thu, 2 Mar 2023 17:07:10 +0800 Subject: [PATCH 267/342] Fix the problem that the 'ReplicatedMergeTree' table failed to insert two similar data when the 'part_type' is configured as 'InMemory' mode. --- src/Columns/ColumnString.h | 4 ++-- ...cated_merge_tree_insert_zookeeper_long.reference | 1 + ..._replicated_merge_tree_insert_zookeeper_long.sql | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.reference create mode 100644 tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql diff --git a/src/Columns/ColumnString.h b/src/Columns/ColumnString.h index aa251b1fda0..fc2ac2d2385 100644 --- a/src/Columns/ColumnString.h +++ b/src/Columns/ColumnString.h @@ -187,8 +187,8 @@ public: void updateHashFast(SipHash & hash) const override { - hash.update(reinterpret_cast(offsets.data()), size() * sizeof(offsets[0])); - hash.update(reinterpret_cast(chars.data()), size() * sizeof(chars[0])); + hash.update(reinterpret_cast(offsets.data()), offsets.size() * sizeof(offsets[0])); + hash.update(reinterpret_cast(chars.data()), chars.size() * sizeof(chars[0])); } void insertRangeFrom(const IColumn & src, size_t start, size_t length) override; diff --git a/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.reference b/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.reference new file mode 100644 index 00000000000..0cfbf08886f --- /dev/null +++ b/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.reference @@ -0,0 +1 @@ +2 diff --git a/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql b/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql new file mode 100644 index 00000000000..df07d2e2faf --- /dev/null +++ b/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql @@ -0,0 +1,13 @@ +DROP TABLE IF EXISTS inmemory_test; + +CREATE TABLE inmemory_test (d Date, id String) +ENGINE=ReplicatedMergeTree('/clickhouse/tables/{database}/inmemory_test', 'r1') +PARTITION BY toYYYYMMDD(d) ORDER BY (d, id) +SETTINGS min_rows_for_compact_part = 10, index_granularity = 8192; + +INSERT INTO inmemory_test(d, id) VALUES('2023-01-01', 'abcdefghijklmnopqrstuvwxyz'); +INSERT INTO inmemory_test(d, id) VALUES('2023-01-01', 'a1234567890123456789012345'); + +SELECT COUNT(1) FROM inmemory_test; + +DROP TABLE inmemory_test; From b1e8b74b9cd81f6c8296b63515f91c893530b353 Mon Sep 17 00:00:00 2001 From: Sema Checherinda <104093494+CheSema@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:54:41 +0100 Subject: [PATCH 268/342] Update 02675_replicated_merge_tree_insert_zookeeper_long.sql --- .../02675_replicated_merge_tree_insert_zookeeper_long.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql b/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql index df07d2e2faf..194ea9bfcc1 100644 --- a/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql +++ b/tests/queries/0_stateless/02675_replicated_merge_tree_insert_zookeeper_long.sql @@ -1,3 +1,5 @@ +-- Tags: no-s3-storage + DROP TABLE IF EXISTS inmemory_test; CREATE TABLE inmemory_test (d Date, id String) From a70789c0b3ecf5384f0cd22fb214b24c9f6d5038 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sat, 4 Mar 2023 09:15:33 +0100 Subject: [PATCH 269/342] Whitespace --- src/Backups/BackupCoordinationRemote.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Backups/BackupCoordinationRemote.cpp b/src/Backups/BackupCoordinationRemote.cpp index 8c696057755..2ca06b76dc1 100644 --- a/src/Backups/BackupCoordinationRemote.cpp +++ b/src/Backups/BackupCoordinationRemote.cpp @@ -604,7 +604,7 @@ bool BackupCoordinationRemote::hasConcurrentBackups(const std::atomic &) return false; auto zk = getZooKeeper(); - std::string backup_stage_path = zookeeper_path +"/stage"; + std::string backup_stage_path = zookeeper_path + "/stage"; if (!zk->exists(root_zookeeper_path)) zk->createAncestors(root_zookeeper_path); From 694f5eb0a830bd96a8f95e51a042607e418df330 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sat, 4 Mar 2023 12:10:27 +0300 Subject: [PATCH 270/342] Update ProcessList.cpp --- src/Interpreters/ProcessList.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Interpreters/ProcessList.cpp b/src/Interpreters/ProcessList.cpp index 6f5553f58fd..37cb9ee599f 100644 --- a/src/Interpreters/ProcessList.cpp +++ b/src/Interpreters/ProcessList.cpp @@ -362,9 +362,11 @@ QueryStatus::QueryStatus( QueryStatus::~QueryStatus() { +#if !defined(NDEBUG) /// Check that all executors were invalidated. for (const auto & e : executors) assert(!e->executor); +#endif if (auto * memory_tracker = getMemoryTracker()) { From 849fac672ade1bfc668d785ff3875b5a9ad09156 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sat, 4 Mar 2023 16:31:18 +0300 Subject: [PATCH 271/342] Update 02585_query_status_deadlock.sh --- tests/queries/0_stateless/02585_query_status_deadlock.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/02585_query_status_deadlock.sh b/tests/queries/0_stateless/02585_query_status_deadlock.sh index 92dd05ef46c..227ecb1c1b2 100755 --- a/tests/queries/0_stateless/02585_query_status_deadlock.sh +++ b/tests/queries/0_stateless/02585_query_status_deadlock.sh @@ -10,6 +10,7 @@ $CLICKHOUSE_CLIENT --query_id="$QUERY_ID" -n -q " create temporary table tmp as select * from numbers(500000000); select * from remote('127.0.0.2', 'system.numbers_mt') where number in (select * from tmp);" &> /dev/null & +$CLICKHOUSE_CLIENT -q "SYSTEM FLUSH LOGS" while true do From 11930ad68503c30f8c6a91e5894c205e364b9414 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Sat, 4 Mar 2023 17:27:47 +0000 Subject: [PATCH 272/342] Docs: Update index support of has(), hasAny(), hasAll() --- .../mergetree-family/mergetree.md | 45 ++++++++++--------- .../MergeTree/MergeTreeIndexFullText.cpp | 4 +- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/docs/en/engines/table-engines/mergetree-family/mergetree.md b/docs/en/engines/table-engines/mergetree-family/mergetree.md index 7c0416d3ea4..fc8060077b0 100644 --- a/docs/en/engines/table-engines/mergetree-family/mergetree.md +++ b/docs/en/engines/table-engines/mergetree-family/mergetree.md @@ -450,29 +450,32 @@ INDEX sample_index3 (lower(str), str) TYPE ngrambf_v1(3, 256, 2, 0) GRANULARITY Conditions in the `WHERE` clause contains calls of the functions that operate with columns. If the column is a part of an index, ClickHouse tries to use this index when performing the functions. ClickHouse supports different subsets of functions for using indexes. -The `set` index can be used with all functions. Function subsets for other indexes are shown in the table below. +Indexes of type `set` can be utilized by all functions. The other index types are supported as follows: | Function (operator) / Index | primary key | minmax | ngrambf_v1 | tokenbf_v1 | bloom_filter | -|------------------------------------------------------------------------------------------------------------|-------------|--------|-------------|-------------|---------------| -| [equals (=, ==)](/docs/en/sql-reference/functions/comparison-functions.md/#function-equals) | ✔ | ✔ | ✔ | ✔ | ✔ | -| [notEquals(!=, <>)](/docs/en/sql-reference/functions/comparison-functions.md/#function-notequals) | ✔ | ✔ | ✔ | ✔ | ✔ | -| [like](/docs/en/sql-reference/functions/string-search-functions.md/#function-like) | ✔ | ✔ | ✔ | ✔ | ✗ | -| [notLike](/docs/en/sql-reference/functions/string-search-functions.md/#function-notlike) | ✔ | ✔ | ✔ | ✔ | ✗ | -| [startsWith](/docs/en/sql-reference/functions/string-functions.md/#startswith) | ✔ | ✔ | ✔ | ✔ | ✗ | -| [endsWith](/docs/en/sql-reference/functions/string-functions.md/#endswith) | ✗ | ✗ | ✔ | ✔ | ✗ | -| [multiSearchAny](/docs/en/sql-reference/functions/string-search-functions.md/#function-multisearchany) | ✗ | ✗ | ✔ | ✗ | ✗ | -| [in](/docs/en/sql-reference/functions/in-functions#in-functions) | ✔ | ✔ | ✔ | ✔ | ✔ | -| [notIn](/docs/en/sql-reference/functions/in-functions#in-functions) | ✔ | ✔ | ✔ | ✔ | ✔ | -| [less (<)](/docs/en/sql-reference/functions/comparison-functions.md/#function-less) | ✔ | ✔ | ✗ | ✗ | ✗ | -| [greater (>)](/docs/en/sql-reference/functions/comparison-functions.md/#function-greater) | ✔ | ✔ | ✗ | ✗ | ✗ | -| [lessOrEquals (<=)](/docs/en/sql-reference/functions/comparison-functions.md/#function-lessorequals) | ✔ | ✔ | ✗ | ✗ | ✗ | -| [greaterOrEquals (>=)](/docs/en/sql-reference/functions/comparison-functions.md/#function-greaterorequals) | ✔ | ✔ | ✗ | ✗ | ✗ | -| [empty](/docs/en/sql-reference/functions/array-functions#function-empty) | ✔ | ✔ | ✗ | ✗ | ✗ | -| [notEmpty](/docs/en/sql-reference/functions/array-functions#function-notempty) | ✔ | ✔ | ✗ | ✗ | ✗ | -| hasToken | ✗ | ✗ | ✗ | ✔ | ✗ | -| hasTokenOrNull | ✗ | ✗ | ✗ | ✔ | ✗ | -| hasTokenCaseInsensitive | ✗ | ✗ | ✗ | ✔ | ✗ | -| hasTokenCaseInsensitiveOrNull | ✗ | ✗ | ✗ | ✔ | ✗ | +|------------------------------------------------------------------------------------------------------------|-------------|--------|------------|------------|--------------| +| [equals (=, ==)](/docs/en/sql-reference/functions/comparison-functions.md/#function-equals) | ✔ | ✔ | ✔ | ✔ | ✔ | +| [notEquals(!=, <>)](/docs/en/sql-reference/functions/comparison-functions.md/#function-notequals) | ✔ | ✔ | ✔ | ✔ | ✔ | +| [like](/docs/en/sql-reference/functions/string-search-functions.md/#function-like) | ✔ | ✔ | ✔ | ✔ | ✗ | +| [notLike](/docs/en/sql-reference/functions/string-search-functions.md/#function-notlike) | ✔ | ✔ | ✔ | ✔ | ✗ | +| [startsWith](/docs/en/sql-reference/functions/string-functions.md/#startswith) | ✔ | ✔ | ✔ | ✔ | ✗ | +| [endsWith](/docs/en/sql-reference/functions/string-functions.md/#endswith) | ✗ | ✗ | ✔ | ✔ | ✗ | +| [multiSearchAny](/docs/en/sql-reference/functions/string-search-functions.md/#function-multisearchany) | ✗ | ✗ | ✔ | ✗ | ✗ | +| [in](/docs/en/sql-reference/functions/in-functions#in-functions) | ✔ | ✔ | ✔ | ✔ | ✔ | +| [notIn](/docs/en/sql-reference/functions/in-functions#in-functions) | ✔ | ✔ | ✔ | ✔ | ✔ | +| [less (<)](/docs/en/sql-reference/functions/comparison-functions.md/#function-less) | ✔ | ✔ | ✗ | ✗ | ✗ | +| [greater (>)](/docs/en/sql-reference/functions/comparison-functions.md/#function-greater) | ✔ | ✔ | ✗ | ✗ | ✗ | +| [lessOrEquals (<=)](/docs/en/sql-reference/functions/comparison-functions.md/#function-lessorequals) | ✔ | ✔ | ✗ | ✗ | ✗ | +| [greaterOrEquals (>=)](/docs/en/sql-reference/functions/comparison-functions.md/#function-greaterorequals) | ✔ | ✔ | ✗ | ✗ | ✗ | +| [empty](/docs/en/sql-reference/functions/array-functions#function-empty) | ✔ | ✔ | ✗ | ✗ | ✗ | +| [notEmpty](/docs/en/sql-reference/functions/array-functions#function-notempty) | ✔ | ✔ | ✗ | ✗ | ✗ | +| [has](/docs/en/sql-reference/functions/array-functions#function-has) | ✗ | ✗ | ✔ | ✔ | ✔ | +| [hasAny](/docs/en/sql-reference/functions/array-functions#function-hasAny) | ✗ | ✗ | ✗ | ✗ | ✔ | +| [hasAll](/docs/en/sql-reference/functions/array-functions#function-hasAll) | ✗ | ✗ | ✗ | ✗ | ✔ | +| hasToken | ✗ | ✗ | ✗ | ✔ | ✗ | +| hasTokenOrNull | ✗ | ✗ | ✗ | ✔ | ✗ | +| hasTokenCaseInsensitive | ✗ | ✗ | ✗ | ✔ | ✗ | +| hasTokenCaseInsensitiveOrNull | ✗ | ✗ | ✗ | ✔ | ✗ | Functions with a constant argument that is less than ngram size can’t be used by `ngrambf_v1` for query optimization. diff --git a/src/Storages/MergeTree/MergeTreeIndexFullText.cpp b/src/Storages/MergeTree/MergeTreeIndexFullText.cpp index 80c4c201c3f..fa1bd36f863 100644 --- a/src/Storages/MergeTree/MergeTreeIndexFullText.cpp +++ b/src/Storages/MergeTree/MergeTreeIndexFullText.cpp @@ -748,9 +748,7 @@ void bloomFilterIndexValidator(const IndexDescription & index, bool /*attach*/) if (!data_type.isString() && !data_type.isFixedString()) throw Exception(ErrorCodes::INCORRECT_QUERY, - "Bloom filter index can be used only with `String`, `FixedString`, " - "`LowCardinality(String)`, `LowCardinality(FixedString)` column " - "or Array with `String` or `FixedString` values column."); + "Ngram and token bloom filter indexes can only be used with column types `String`, `FixedString`, `LowCardinality(String)`, `LowCardinality(FixedString)`, `Array(String)` or `Array(FixedString)`"); } if (index.type == NgramTokenExtractor::getName()) From 08ce7b031e95a6c61e965e07deb26c23174e7a95 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Sat, 4 Mar 2023 16:48:04 +0100 Subject: [PATCH 273/342] release shared ptrs after finishing a transaction --- src/Interpreters/MergeTreeTransaction.cpp | 14 ++++++++++++++ src/Interpreters/MergeTreeTransaction.h | 4 ++++ src/Interpreters/TransactionLog.cpp | 6 ++++-- src/Interpreters/TransactionLog.h | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Interpreters/MergeTreeTransaction.cpp b/src/Interpreters/MergeTreeTransaction.cpp index f16ece46530..50ecb061752 100644 --- a/src/Interpreters/MergeTreeTransaction.cpp +++ b/src/Interpreters/MergeTreeTransaction.cpp @@ -315,6 +315,15 @@ bool MergeTreeTransaction::rollback() noexcept return true; } +void MergeTreeTransaction::afterFinalize() +{ + std::lock_guard lock{mutex}; + /// Release shared pointers just in case + storages.clear(); + mutations.clear(); + finalized = true; +} + void MergeTreeTransaction::onException() { TransactionLog::instance().rollbackTransaction(shared_from_this()); @@ -331,6 +340,11 @@ String MergeTreeTransaction::dumpDescription() const } std::lock_guard lock{mutex}; + if (finalized) + { + res += ", cannot dump detailed description, transaction is finalized"; + return res; + } res += fmt::format(", affects {} tables:", storages.size()); diff --git a/src/Interpreters/MergeTreeTransaction.h b/src/Interpreters/MergeTreeTransaction.h index f2d8d29d244..e5a80e03e18 100644 --- a/src/Interpreters/MergeTreeTransaction.h +++ b/src/Interpreters/MergeTreeTransaction.h @@ -65,6 +65,8 @@ private: scope_guard beforeCommit(); void afterCommit(CSN assigned_csn) noexcept; bool rollback() noexcept; + void afterFinalize(); + void checkIsNotCancelled() const; mutable std::mutex mutex; @@ -74,6 +76,8 @@ private: std::atomic snapshot; const std::list::iterator snapshot_in_use_it; + bool finalized TSA_GUARDED_BY(mutex) = false; + /// Lists of changes made by transaction std::unordered_set storages TSA_GUARDED_BY(mutex); DataPartsVector creating_parts TSA_GUARDED_BY(mutex); diff --git a/src/Interpreters/TransactionLog.cpp b/src/Interpreters/TransactionLog.cpp index 827957b8749..6257e617d4a 100644 --- a/src/Interpreters/TransactionLog.cpp +++ b/src/Interpreters/TransactionLog.cpp @@ -350,7 +350,7 @@ void TransactionLog::tryFinalizeUnknownStateTransactions() /// CSNs must be already loaded, only need to check if the corresponding mapping exists. if (auto csn = getCSN(txn->tid)) { - finalizeCommittedTransaction(txn, csn, state_guard); + finalizeCommittedTransaction(txn.get(), csn, state_guard); } else { @@ -431,7 +431,7 @@ CSN TransactionLog::commitTransaction(const MergeTreeTransactionPtr & txn, bool /// The only thing we can do is to postpone its finalization. { std::lock_guard lock{running_list_mutex}; - unknown_state_list.emplace_back(txn.get(), std::move(state_guard)); + unknown_state_list.emplace_back(txn, std::move(state_guard)); } log_updated_event->set(); if (throw_on_unknown_status) @@ -487,6 +487,7 @@ CSN TransactionLog::finalizeCommittedTransaction(MergeTreeTransaction * txn, CSN } } + txn->afterFinalize(); return allocated_csn; } @@ -523,6 +524,7 @@ void TransactionLog::rollbackTransaction(const MergeTreeTransactionPtr & txn) no } tryWriteEventToSystemLog(log, global_context, TransactionsInfoLogElement::ROLLBACK, txn->tid); + txn->afterFinalize(); } MergeTreeTransactionPtr TransactionLog::tryGetRunningTransaction(const TIDHash & tid) diff --git a/src/Interpreters/TransactionLog.h b/src/Interpreters/TransactionLog.h index 64d02ad3ac5..6e8777d8519 100644 --- a/src/Interpreters/TransactionLog.h +++ b/src/Interpreters/TransactionLog.h @@ -177,7 +177,7 @@ private: /// Transactions that are currently processed TransactionsList running_list TSA_GUARDED_BY(running_list_mutex); /// If we lost connection on attempt to create csn- node then we don't know transaction's state. - using UnknownStateList = std::vector>; + using UnknownStateList = std::vector>; UnknownStateList unknown_state_list TSA_GUARDED_BY(running_list_mutex); UnknownStateList unknown_state_list_loaded TSA_GUARDED_BY(running_list_mutex); /// Ordered list of snapshots that are currently used by some transactions. Needed for background cleanup. From b8d33f4d38b688ddcaffc372a50148db84eb6d25 Mon Sep 17 00:00:00 2001 From: kssenii Date: Sun, 5 Mar 2023 13:32:04 +0100 Subject: [PATCH 274/342] Fix test --- .../test_disk_configuration/test.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/integration/test_disk_configuration/test.py b/tests/integration/test_disk_configuration/test.py index 96cdb0213bc..6ebe994dc68 100644 --- a/tests/integration/test_disk_configuration/test.py +++ b/tests/integration/test_disk_configuration/test.py @@ -294,6 +294,12 @@ def test_merge_tree_custom_disk_setting(start_cluster): ).strip() ) + node1.query(f"DROP TABLE {TABLE_NAME} SYNC") + node1.query(f"DROP TABLE {TABLE_NAME}_2 SYNC") + node1.query(f"DROP TABLE {TABLE_NAME}_3 SYNC") + node1.query(f"DROP TABLE {TABLE_NAME}_4 SYNC") + node2.query(f"DROP TABLE {TABLE_NAME}_4 SYNC") + def test_merge_tree_nested_custom_disk_setting(start_cluster): node = cluster.instances["node1"] @@ -307,9 +313,9 @@ def test_merge_tree_nested_custom_disk_setting(start_cluster): ) node.query( - """ - DROP TABLE IF EXISTS test; - CREATE TABLE test (a Int32) + f""" + DROP TABLE IF EXISTS {TABLE_NAME} SYNC; + CREATE TABLE {TABLE_NAME} (a Int32) ENGINE = MergeTree() order by tuple() SETTINGS disk = disk( type=cache, @@ -323,13 +329,13 @@ def test_merge_tree_nested_custom_disk_setting(start_cluster): """ ) - node.query("INSERT INTO test SELECT number FROM numbers(100)") + node.query(f"INSERT INTO {TABLE_NAME} SELECT number FROM numbers(100)") node.query("SYSTEM DROP FILESYSTEM CACHE") # Check cache is filled assert 0 == int(node.query("SELECT count() FROM system.filesystem_cache")) - assert 100 == int(node.query("SELECT count() FROM test")) - node.query("SELECT * FROM test") + assert 100 == int(node.query(f"SELECT count() FROM {TABLE_NAME}")) + node.query(f"SELECT * FROM {TABLE_NAME}") assert 0 < int(node.query("SELECT count() FROM system.filesystem_cache")) # Check s3 is filled @@ -339,12 +345,13 @@ def test_merge_tree_nested_custom_disk_setting(start_cluster): node.restart_clickhouse() - assert 100 == int(node.query("SELECT count() FROM test")) + assert 100 == int(node.query(f"SELECT count() FROM {TABLE_NAME}")) expected = """ SETTINGS disk = disk(type = cache, max_size = \\'[HIDDEN]\\', path = \\'[HIDDEN]\\', disk = disk(type = s3, endpoint = \\'[HIDDEN]\\' """ - assert expected.strip() in node.query(f"SHOW CREATE TABLE test").strip() + assert expected.strip() in node.query(f"SHOW CREATE TABLE {TABLE_NAME}").strip() + node.query(f"DROP TABLE {TABLE_NAME} SYNC") def test_merge_tree_setting_override(start_cluster): @@ -419,3 +426,4 @@ def test_merge_tree_setting_override(start_cluster): assert ( len(list(minio.list_objects(cluster.minio_bucket, "data/", recursive=True))) > 0 ) + node.query(f"DROP TABLE {TABLE_NAME} SYNC") From 099013831a8f50fbb0f9cce97bde5db90251fceb Mon Sep 17 00:00:00 2001 From: Nikita Mikhaylov Date: Sun, 5 Mar 2023 16:15:03 +0100 Subject: [PATCH 275/342] Added batching for reads and retries for the most heavy function in backups (#47243) --- src/Backups/BackupCoordinationRemote.cpp | 150 +++++++++++++++++++++-- src/Backups/BackupCoordinationRemote.h | 18 ++- src/Backups/BackupsWorker.cpp | 27 +++- src/Backups/IBackupCoordination.h | 20 ++- src/Core/Settings.h | 4 + 5 files changed, 199 insertions(+), 20 deletions(-) diff --git a/src/Backups/BackupCoordinationRemote.cpp b/src/Backups/BackupCoordinationRemote.cpp index 2ca06b76dc1..029a27d2712 100644 --- a/src/Backups/BackupCoordinationRemote.cpp +++ b/src/Backups/BackupCoordinationRemote.cpp @@ -166,13 +166,25 @@ namespace } BackupCoordinationRemote::BackupCoordinationRemote( - const String & root_zookeeper_path_, const String & backup_uuid_, zkutil::GetZooKeeper get_zookeeper_, bool is_internal_) - : root_zookeeper_path(root_zookeeper_path_) + const BackupKeeperSettings & keeper_settings_, + const String & root_zookeeper_path_, + const String & backup_uuid_, + zkutil::GetZooKeeper get_zookeeper_, + bool is_internal_) + : keeper_settings(keeper_settings_) + , root_zookeeper_path(root_zookeeper_path_) , zookeeper_path(root_zookeeper_path_ + "/backup-" + backup_uuid_) , backup_uuid(backup_uuid_) , get_zookeeper(get_zookeeper_) , is_internal(is_internal_) { + zookeeper_retries_info = ZooKeeperRetriesInfo( + "BackupCoordinationRemote", + &Poco::Logger::get("BackupCoordinationRemote"), + keeper_settings.keeper_max_retries, + keeper_settings.keeper_retry_initial_backoff_ms, + keeper_settings.keeper_retry_max_backoff_ms); + createRootNodes(); stage_sync.emplace( zookeeper_path + "/stage", [this] { return getZooKeeper(); }, &Poco::Logger::get("BackupCoordination")); @@ -486,19 +498,131 @@ void BackupCoordinationRemote::updateFileInfo(const FileInfo & file_info) std::vector BackupCoordinationRemote::getAllFileInfos() const { - auto zk = getZooKeeper(); - std::vector file_infos; - Strings escaped_names = zk->getChildren(zookeeper_path + "/file_names"); - for (const String & escaped_name : escaped_names) + /// There could be tons of files inside /file_names or /file_infos + /// Thus we use MultiRead requests for processing them + /// We also use [Zoo]Keeper retries and it should be safe, because + /// this function is called at the end after the actual copying is finished. + + auto split_vector = [](Strings && vec, size_t max_batch_size) -> std::vector { - String size_and_checksum = zk->get(zookeeper_path + "/file_names/" + escaped_name); - UInt64 size = deserializeSizeAndChecksum(size_and_checksum).first; - FileInfo file_info; - if (size) /// we don't keep FileInfos for empty files - file_info = deserializeFileInfo(zk->get(zookeeper_path + "/file_infos/" + size_and_checksum)); - file_info.file_name = unescapeForFileName(escaped_name); - file_infos.emplace_back(std::move(file_info)); + std::vector result; + size_t left_border = 0; + + auto move_to_result = [&](auto && begin, auto && end) + { + auto batch = Strings(); + batch.reserve(max_batch_size); + std::move(begin, end, std::back_inserter(batch)); + result.push_back(std::move(batch)); + }; + + if (max_batch_size == 0) + { + move_to_result(vec.begin(), vec.end()); + return result; + } + + for (size_t pos = 0; pos < vec.size(); ++pos) + { + if (pos >= left_border + max_batch_size) + { + move_to_result(vec.begin() + left_border, vec.begin() + pos); + left_border = pos; + } + } + + if (vec.begin() + left_border != vec.end()) + move_to_result(vec.begin() + left_border, vec.end()); + + return result; + }; + + std::vector batched_escaped_names; + { + ZooKeeperRetriesControl retries_ctl("getAllFileInfos::getChildren", zookeeper_retries_info); + retries_ctl.retryLoop([&]() + { + auto zk = getZooKeeper(); + batched_escaped_names = split_vector(zk->getChildren(zookeeper_path + "/file_names"), keeper_settings.batch_size_for_keeper_multiread); + }); } + + std::vector file_infos; + file_infos.reserve(batched_escaped_names.size()); + + for (auto & batch : batched_escaped_names) + { + std::optional sizes_and_checksums; + { + Strings file_names_paths; + file_names_paths.reserve(batch.size()); + for (const String & escaped_name : batch) + file_names_paths.emplace_back(zookeeper_path + "/file_names/" + escaped_name); + + + ZooKeeperRetriesControl retries_ctl("getAllFileInfos::getSizesAndChecksums", zookeeper_retries_info); + retries_ctl.retryLoop([&]() + { + auto zk = getZooKeeper(); + sizes_and_checksums = zk->get(file_names_paths); + }); + } + + Strings non_empty_file_names; + Strings non_empty_file_infos_paths; + std::vector non_empty_files_infos; + + /// Process all files and understand whether there are some empty files + /// Save non empty file names for further batch processing + { + std::vector empty_files_infos; + for (size_t i = 0; i < batch.size(); ++i) + { + auto file_name = batch[i]; + if (sizes_and_checksums.value()[i].error != Coordination::Error::ZOK) + throw zkutil::KeeperException(sizes_and_checksums.value()[i].error); + auto size_and_checksum = sizes_and_checksums.value()[i].data; + auto size = deserializeSizeAndChecksum(size_and_checksum).first; + + if (size) + { + /// Save it later for batch processing + non_empty_file_names.emplace_back(file_name); + non_empty_file_infos_paths.emplace_back(zookeeper_path + "/file_infos/" + size_and_checksum); + continue; + } + + /// File is empty + FileInfo empty_file_info; + empty_file_info.file_name = unescapeForFileName(file_name); + empty_files_infos.emplace_back(std::move(empty_file_info)); + } + + std::move(empty_files_infos.begin(), empty_files_infos.end(), std::back_inserter(file_infos)); + } + + std::optional non_empty_file_infos_serialized; + ZooKeeperRetriesControl retries_ctl("getAllFileInfos::getFileInfos", zookeeper_retries_info); + retries_ctl.retryLoop([&]() + { + auto zk = getZooKeeper(); + non_empty_file_infos_serialized = zk->get(non_empty_file_infos_paths); + }); + + /// Process non empty files + for (size_t i = 0; i < non_empty_file_names.size(); ++i) + { + FileInfo file_info; + if (non_empty_file_infos_serialized.value()[i].error != Coordination::Error::ZOK) + throw zkutil::KeeperException(non_empty_file_infos_serialized.value()[i].error); + file_info = deserializeFileInfo(non_empty_file_infos_serialized.value()[i].data); + file_info.file_name = unescapeForFileName(non_empty_file_names[i]); + non_empty_files_infos.emplace_back(std::move(file_info)); + } + + std::move(non_empty_files_infos.begin(), non_empty_files_infos.end(), std::back_inserter(file_infos)); + } + return file_infos; } diff --git a/src/Backups/BackupCoordinationRemote.h b/src/Backups/BackupCoordinationRemote.h index c7260bcd237..23c76f5be47 100644 --- a/src/Backups/BackupCoordinationRemote.h +++ b/src/Backups/BackupCoordinationRemote.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace DB @@ -16,7 +17,20 @@ constexpr size_t MAX_ZOOKEEPER_ATTEMPTS = 10; class BackupCoordinationRemote : public IBackupCoordination { public: - BackupCoordinationRemote(const String & root_zookeeper_path_, const String & backup_uuid_, zkutil::GetZooKeeper get_zookeeper_, bool is_internal_); + struct BackupKeeperSettings + { + UInt64 keeper_max_retries; + UInt64 keeper_retry_initial_backoff_ms; + UInt64 keeper_retry_max_backoff_ms; + UInt64 batch_size_for_keeper_multiread; + }; + + BackupCoordinationRemote( + const BackupKeeperSettings & keeper_settings_, + const String & root_zookeeper_path_, + const String & backup_uuid_, + zkutil::GetZooKeeper get_zookeeper_, + bool is_internal_); ~BackupCoordinationRemote() override; void setStage(const String & current_host, const String & new_stage, const String & message) override; @@ -68,12 +82,14 @@ private: void prepareReplicatedTables() const; void prepareReplicatedAccess() const; + const BackupKeeperSettings keeper_settings; const String root_zookeeper_path; const String zookeeper_path; const String backup_uuid; const zkutil::GetZooKeeper get_zookeeper; const bool is_internal; + mutable ZooKeeperRetriesInfo zookeeper_retries_info; std::optional stage_sync; mutable std::mutex mutex; diff --git a/src/Backups/BackupsWorker.cpp b/src/Backups/BackupsWorker.cpp index 865151cc9ec..bdcff249e7d 100644 --- a/src/Backups/BackupsWorker.cpp +++ b/src/Backups/BackupsWorker.cpp @@ -38,12 +38,14 @@ namespace Stage = BackupCoordinationStage; namespace { - std::shared_ptr makeBackupCoordination(const String & root_zk_path, const String & backup_uuid, const ContextPtr & context, bool is_internal_backup) + std::shared_ptr makeBackupCoordination(std::optional keeper_settings, String & root_zk_path, const String & backup_uuid, const ContextPtr & context, bool is_internal_backup) { if (!root_zk_path.empty()) { + if (!keeper_settings.has_value()) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Parameter keeper_settings is empty while root_zk_path is not. This is bug"); auto get_zookeeper = [global_context = context->getGlobalContext()] { return global_context->getZooKeeper(); }; - return std::make_shared(root_zk_path, backup_uuid, get_zookeeper, is_internal_backup); + return std::make_shared(*keeper_settings, root_zk_path, backup_uuid, get_zookeeper, is_internal_backup); } else { @@ -169,7 +171,15 @@ OperationID BackupsWorker::startMakingBackup(const ASTPtr & query, const Context /// if it's not created here. However to handle errors better it's better to make a coordination here because this way /// if an exception will be thrown in startMakingBackup() other hosts will know about that. root_zk_path = context->getConfigRef().getString("backups.zookeeper_path", "/clickhouse/backups"); - backup_coordination = makeBackupCoordination(root_zk_path, toString(*backup_settings.backup_uuid), context, backup_settings.internal); + + BackupCoordinationRemote::BackupKeeperSettings keeper_settings + { + .keeper_max_retries = context->getSettingsRef().backup_keeper_max_retries, + .keeper_retry_initial_backoff_ms = context->getSettingsRef().backup_keeper_retry_initial_backoff_ms, + .keeper_retry_max_backoff_ms = context->getSettingsRef().backup_keeper_retry_max_backoff_ms, + .batch_size_for_keeper_multiread = context->getSettingsRef().backup_batch_size_for_keeper_multiread, + }; + backup_coordination = makeBackupCoordination(keeper_settings, root_zk_path, toString(*backup_settings.backup_uuid), context, backup_settings.internal); } auto backup_info = BackupInfo::fromAST(*backup_query->backup_name); @@ -265,10 +275,17 @@ void BackupsWorker::doBackup( context->checkAccess(required_access); String root_zk_path; - + std::optional keeper_settings; ClusterPtr cluster; if (on_cluster) { + keeper_settings = BackupCoordinationRemote::BackupKeeperSettings + { + .keeper_max_retries = context->getSettingsRef().backup_keeper_max_retries, + .keeper_retry_initial_backoff_ms = context->getSettingsRef().backup_keeper_retry_initial_backoff_ms, + .keeper_retry_max_backoff_ms = context->getSettingsRef().backup_keeper_retry_max_backoff_ms, + .batch_size_for_keeper_multiread = context->getSettingsRef().backup_batch_size_for_keeper_multiread, + }; root_zk_path = context->getConfigRef().getString("backups.zookeeper_path", "/clickhouse/backups"); backup_query->cluster = context->getMacros()->expand(backup_query->cluster); cluster = context->getCluster(backup_query->cluster); @@ -277,7 +294,7 @@ void BackupsWorker::doBackup( /// Make a backup coordination. if (!backup_coordination) - backup_coordination = makeBackupCoordination(root_zk_path, toString(*backup_settings.backup_uuid), context, backup_settings.internal); + backup_coordination = makeBackupCoordination(keeper_settings, root_zk_path, toString(*backup_settings.backup_uuid), context, backup_settings.internal); if (!allow_concurrent_backups && backup_coordination->hasConcurrentBackups(std::ref(num_active_backups))) throw Exception(ErrorCodes::CONCURRENT_ACCESS_NOT_SUPPORTED, "Concurrent backups not supported, turn on setting 'allow_concurrent_backups'"); diff --git a/src/Backups/IBackupCoordination.h b/src/Backups/IBackupCoordination.h index f5fa01a1530..b75d856b50f 100644 --- a/src/Backups/IBackupCoordination.h +++ b/src/Backups/IBackupCoordination.h @@ -1,7 +1,9 @@ #pragma once -#include #include +#include +#include +#include namespace DB @@ -85,6 +87,22 @@ public: /// Position in the archive. UInt64 pos_in_archive = static_cast(-1); + + /// Note: this format doesn't allow to parse data back + /// It is useful only for debugging purposes + [[ maybe_unused ]] String describe() + { + String result; + result += fmt::format("file_name: {};\n", file_name); + result += fmt::format("size: {};\n", size); + result += fmt::format("checksum: {};\n", getHexUIntLowercase(checksum)); + result += fmt::format("base_size: {};\n", base_size); + result += fmt::format("base_checksum: {};\n", getHexUIntLowercase(checksum)); + result += fmt::format("data_file_name: {};\n", data_file_name); + result += fmt::format("archive_suffix: {};\n", archive_suffix); + result += fmt::format("pos_in_archive: {};\n", pos_in_archive); + return result; + } }; /// Adds file information. diff --git a/src/Core/Settings.h b/src/Core/Settings.h index a65f2ccb60f..aeb6b5b1130 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -414,6 +414,10 @@ class IColumn; \ M(UInt64, backup_threads, 16, "The maximum number of threads to execute BACKUP requests.", 0) \ M(UInt64, restore_threads, 16, "The maximum number of threads to execute RESTORE requests.", 0) \ + M(UInt64, backup_keeper_max_retries, 20, "Max retries for keeper operations during backup", 0) \ + M(UInt64, backup_keeper_retry_initial_backoff_ms, 100, "Initial backoff timeout for [Zoo]Keeper operations during backup", 0) \ + M(UInt64, backup_keeper_retry_max_backoff_ms, 5000, "Max backoff timeout for [Zoo]Keeper operations during backup", 0) \ + M(UInt64, backup_batch_size_for_keeper_multiread, 10000, "Maximum size of batch for multiread request to [Zoo]Keeper during backup", 0) \ \ M(Bool, log_profile_events, true, "Log query performance statistics into the query_log, query_thread_log and query_views_log.", 0) \ M(Bool, log_query_settings, true, "Log query settings into the query_log.", 0) \ From c38d820d0f9b71156a01a1a20d16b8260c000c03 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Sun, 5 Mar 2023 16:45:17 +0000 Subject: [PATCH 276/342] Add 4LW for cleaning resource --- programs/keeper/CMakeLists.txt | 1 + src/Coordination/CoordinationSettings.cpp | 2 +- src/Coordination/FourLetterCommand.cpp | 9 ++++++++ src/Coordination/FourLetterCommand.h | 13 ++++++++++- src/Coordination/KeeperDispatcher.cpp | 27 ++++++++++++++++++++++- src/Coordination/KeeperDispatcher.h | 2 ++ 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/programs/keeper/CMakeLists.txt b/programs/keeper/CMakeLists.txt index 9b01e6920a4..761335fb707 100644 --- a/programs/keeper/CMakeLists.txt +++ b/programs/keeper/CMakeLists.txt @@ -128,6 +128,7 @@ if (BUILD_STANDALONE_KEEPER) ch_contrib::lz4 ch_contrib::zstd ch_contrib::cityhash + ch_contrib::jemalloc common ch_contrib::double_conversion ch_contrib::dragonbox_to_chars pcg_random diff --git a/src/Coordination/CoordinationSettings.cpp b/src/Coordination/CoordinationSettings.cpp index f5c79d3be7a..5e1ac1e2d7f 100644 --- a/src/Coordination/CoordinationSettings.cpp +++ b/src/Coordination/CoordinationSettings.cpp @@ -36,7 +36,7 @@ void CoordinationSettings::loadFromConfig(const String & config_elem, const Poco } -const String KeeperConfigurationAndSettings::DEFAULT_FOUR_LETTER_WORD_CMD = "conf,cons,crst,envi,ruok,srst,srvr,stat,wchs,dirs,mntr,isro,rcvr,apiv,csnp,lgif,rqld,rclc"; +const String KeeperConfigurationAndSettings::DEFAULT_FOUR_LETTER_WORD_CMD = "conf,cons,crst,envi,ruok,srst,srvr,stat,wchs,dirs,mntr,isro,rcvr,apiv,csnp,lgif,rqld,rclc,clrs"; KeeperConfigurationAndSettings::KeeperConfigurationAndSettings() : server_id(NOT_EXIST) diff --git a/src/Coordination/FourLetterCommand.cpp b/src/Coordination/FourLetterCommand.cpp index 6157daad1cd..8a7fdb82fb7 100644 --- a/src/Coordination/FourLetterCommand.cpp +++ b/src/Coordination/FourLetterCommand.cpp @@ -148,6 +148,9 @@ void FourLetterCommandFactory::registerCommands(KeeperDispatcher & keeper_dispat FourLetterCommandPtr recalculate_command = std::make_shared(keeper_dispatcher); factory.registerCommand(recalculate_command); + FourLetterCommandPtr clean_resources_command = std::make_shared(keeper_dispatcher); + factory.registerCommand(clean_resources_command); + factory.initializeAllowList(keeper_dispatcher); factory.setInitialize(true); } @@ -524,4 +527,10 @@ String RecalculateCommand::run() return "ok"; } +String CleanResourcesCommand::run() +{ + keeper_dispatcher.cleanResources(); + return "ok"; +} + } diff --git a/src/Coordination/FourLetterCommand.h b/src/Coordination/FourLetterCommand.h index e1fe0333081..c1a91303c05 100644 --- a/src/Coordination/FourLetterCommand.h +++ b/src/Coordination/FourLetterCommand.h @@ -377,7 +377,6 @@ struct RequestLeaderCommand : public IFourLetterCommand ~RequestLeaderCommand() override = default; }; -/// Request to be leader. struct RecalculateCommand : public IFourLetterCommand { explicit RecalculateCommand(KeeperDispatcher & keeper_dispatcher_) @@ -390,4 +389,16 @@ struct RecalculateCommand : public IFourLetterCommand ~RecalculateCommand() override = default; }; +struct CleanResourcesCommand : public IFourLetterCommand +{ + explicit CleanResourcesCommand(KeeperDispatcher & keeper_dispatcher_) + : IFourLetterCommand(keeper_dispatcher_) + { + } + + String name() override { return "clrs"; } + String run() override; + ~CleanResourcesCommand() override = default; +}; + } diff --git a/src/Coordination/KeeperDispatcher.cpp b/src/Coordination/KeeperDispatcher.cpp index 06c693e45be..a6d16334924 100644 --- a/src/Coordination/KeeperDispatcher.cpp +++ b/src/Coordination/KeeperDispatcher.cpp @@ -9,7 +9,7 @@ #include #include #include - +#include #include #include @@ -17,12 +17,26 @@ #include #include +#if USE_JEMALLOC +# include + +#define STRINGIFY_HELPER(x) #x +#define STRINGIFY(x) STRINGIFY_HELPER(x) + +#endif + namespace CurrentMetrics { extern const Metric KeeperAliveConnections; extern const Metric KeeperOutstandingRequets; } +namespace ProfileEvents +{ + extern const Event MemoryAllocatorPurge; + extern const Event MemoryAllocatorPurgeTimeMicroseconds; +} + namespace fs = std::filesystem; namespace DB @@ -753,4 +767,15 @@ Keeper4LWInfo KeeperDispatcher::getKeeper4LWInfo() const return result; } +void KeeperDispatcher::cleanResources() +{ +#if USE_JEMALLOC + LOG_TRACE(&Poco::Logger::get("KeeperDispatcher"), "Purging unused memory"); + Stopwatch watch; + mallctl("arena." STRINGIFY(MALLCTL_ARENAS_ALL) ".purge", nullptr, nullptr, nullptr, 0); + ProfileEvents::increment(ProfileEvents::MemoryAllocatorPurge); + ProfileEvents::increment(ProfileEvents::MemoryAllocatorPurgeTimeMicroseconds, watch.elapsedMicroseconds()); +#endif +} + } diff --git a/src/Coordination/KeeperDispatcher.h b/src/Coordination/KeeperDispatcher.h index 90965d0934e..9371d2fbbac 100644 --- a/src/Coordination/KeeperDispatcher.h +++ b/src/Coordination/KeeperDispatcher.h @@ -230,6 +230,8 @@ public: { return server->recalculateStorageStats(); } + + static void cleanResources(); }; } From 3a184880c6488a5027416315ec97ee06a93f1917 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Sun, 5 Mar 2023 17:18:53 +0000 Subject: [PATCH 277/342] Add test for clrs --- .../test_keeper_four_word_command/test.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/integration/test_keeper_four_word_command/test.py b/tests/integration/test_keeper_four_word_command/test.py index 04f6800b92b..d3fcfcc3014 100644 --- a/tests/integration/test_keeper_four_word_command/test.py +++ b/tests/integration/test_keeper_four_word_command/test.py @@ -679,3 +679,41 @@ def test_cmd_rqld(started_cluster): + " does not become leader after 30s, maybe there is something wrong." ) assert keeper_utils.is_leader(cluster, node) + + +def test_cmd_clrs(started_cluster): + def get_memory_purges(): + return node1.query( + "SELECT value FROM system.events WHERE event = 'MemoryAllocatorPurge' SETTINGS system_events_show_zero_values = 1" + ) + + zk = None + try: + wait_nodes() + + zk = get_fake_zk(node1.name, timeout=30.0) + + paths = [f"/clrs_{i}" for i in range(10000)] + + # we only count the events because we cannot reliably test memory usage of Keeper + # but let's create and delete nodes so the first purge needs to release some memory + create_transaction = zk.transaction() + for path in paths: + create_transaction.create(path) + create_transaction.commit() + + delete_transaction = zk.transaction() + for path in paths: + delete_transaction.delete(path) + delete_transaction.commit() + + # repeat multiple times to make sure MemoryAllocatorPurge isn't increased because of other reasons + for _ in range(5): + prev_purges = int(get_memory_purges()) + keeper_utils.send_4lw_cmd(cluster, node1, cmd="clrs") + current_purges = int(get_memory_purges()) + assert current_purges > prev_purges + prev_purges = current_purges + + finally: + destroy_zk_client(zk) From 4f85b733f13f0b3780705e55f4e36dc910d9021b Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 5 Mar 2023 20:19:33 +0300 Subject: [PATCH 278/342] Use string concatenation for XML serialization (#47251) --- src/Backups/BackupImpl.cpp | 66 ++++++++++++++++++++------------------ src/IO/Operators.h | 8 +++++ 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/Backups/BackupImpl.cpp b/src/Backups/BackupImpl.cpp index e4c85bec496..b5f48a1a277 100644 --- a/src/Backups/BackupImpl.cpp +++ b/src/Backups/BackupImpl.cpp @@ -16,11 +16,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include #include @@ -317,11 +317,19 @@ void BackupImpl::writeBackupMetadata() { assert(!is_internal_backup); - Poco::AutoPtr config{new Poco::Util::XMLConfiguration()}; - config->setInt("version", CURRENT_BACKUP_VERSION); - config->setBool("deduplicate_files", deduplicate_files); - config->setString("timestamp", toString(LocalDateTime{timestamp})); - config->setString("uuid", toString(*uuid)); + checkLockFile(true); + + std::unique_ptr out; + if (use_archives) + out = getArchiveWriter("")->writeFile(".backup"); + else + out = writer->writeFile(".backup"); + + *out << ""; + *out << "" << CURRENT_BACKUP_VERSION << ""; + *out << "" << deduplicate_files << ""; + *out << "" << toString(LocalDateTime{timestamp}) << ""; + *out << "" << toString(*uuid) << ""; auto all_file_infos = coordination->getAllFileInfos(); @@ -336,8 +344,8 @@ void BackupImpl::writeBackupMetadata() if (base_backup_in_use) { - config->setString("base_backup", base_backup_info->toString()); - config->setString("base_backup_uuid", toString(*base_backup_uuid)); + *out << "" << xml << base_backup_info->toString() << ""; + *out << "" << toString(*base_backup_uuid) << ""; } } @@ -346,31 +354,32 @@ void BackupImpl::writeBackupMetadata() num_entries = 0; size_of_entries = 0; - for (size_t i = 0; i != all_file_infos.size(); ++i) + *out << ""; + for (const auto & info : all_file_infos) { - const auto & info = all_file_infos[i]; - String prefix = i ? "contents.file[" + std::to_string(i) + "]." : "contents.file."; - config->setString(prefix + "name", info.file_name); - config->setUInt64(prefix + "size", info.size); + *out << ""; + + *out << "" << xml << info.file_name << ""; + *out << "" << info.size << ""; if (info.size) { - config->setString(prefix + "checksum", hexChecksum(info.checksum)); + *out << "" << hexChecksum(info.checksum) << ""; if (info.base_size) { - config->setBool(prefix + "use_base", true); + *out << "true"; if (info.base_size != info.size) { - config->setUInt64(prefix + "base_size", info.base_size); - config->setString(prefix + "base_checksum", hexChecksum(info.base_checksum)); + *out << "" << info.base_size << ""; + *out << "" << hexChecksum(info.base_checksum) << ""; } } if (!info.data_file_name.empty() && (info.data_file_name != info.file_name)) - config->setString(prefix + "data_file", info.data_file_name); + *out << "" << xml << info.data_file_name << ""; if (!info.archive_suffix.empty()) - config->setString(prefix + "archive_suffix", info.archive_suffix); + *out << "" << xml << info.archive_suffix << ""; if (info.pos_in_archive != static_cast(-1)) - config->setUInt64(prefix + "pos_in_archive", info.pos_in_archive); + *out << "" << info.pos_in_archive << ""; } total_size += info.size; @@ -380,23 +389,16 @@ void BackupImpl::writeBackupMetadata() ++num_entries; size_of_entries += info.size - info.base_size; } + + *out << ""; } + *out << ""; - std::ostringstream stream; // STYLE_CHECK_ALLOW_STD_STRING_STREAM - config->save(stream); - String str = stream.str(); + *out << ""; - checkLockFile(true); - - std::unique_ptr out; - if (use_archives) - out = getArchiveWriter("")->writeFile(".backup"); - else - out = writer->writeFile(".backup"); - out->write(str.data(), str.size()); out->finalize(); - uncompressed_size = size_of_entries + str.size(); + uncompressed_size = size_of_entries + out->count(); } diff --git a/src/IO/Operators.h b/src/IO/Operators.h index 06ff20c43e8..185745e8415 100644 --- a/src/IO/Operators.h +++ b/src/IO/Operators.h @@ -30,11 +30,13 @@ enum EscapeManip { escape }; /// For strings - escape special c enum QuoteManip { quote }; /// For strings, dates, datetimes - enclose in single quotes with escaping. In the rest, as usual. enum DoubleQuoteManip { double_quote }; /// For strings, dates, datetimes - enclose in double quotes with escaping. In the rest, as usual. enum BinaryManip { binary }; /// Output in binary format. +enum XMLManip { xml }; /// Output strings with XML escaping. struct EscapeManipWriteBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; struct QuoteManipWriteBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; struct DoubleQuoteManipWriteBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; struct BinaryManipWriteBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; +struct XMLManipWriteBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; struct EscapeManipReadBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; struct QuoteManipReadBuffer : std::reference_wrapper { using std::reference_wrapper::reference_wrapper; }; @@ -48,11 +50,13 @@ inline EscapeManipWriteBuffer operator<< (WriteBuffer & buf, EscapeManip) inline QuoteManipWriteBuffer operator<< (WriteBuffer & buf, QuoteManip) { return buf; } inline DoubleQuoteManipWriteBuffer operator<< (WriteBuffer & buf, DoubleQuoteManip) { return buf; } inline BinaryManipWriteBuffer operator<< (WriteBuffer & buf, BinaryManip) { return buf; } +inline XMLManipWriteBuffer operator<< (WriteBuffer & buf, XMLManip) { return buf; } template WriteBuffer & operator<< (EscapeManipWriteBuffer buf, const T & x) { writeText(x, buf.get()); return buf; } template WriteBuffer & operator<< (QuoteManipWriteBuffer buf, const T & x) { writeQuoted(x, buf.get()); return buf; } template WriteBuffer & operator<< (DoubleQuoteManipWriteBuffer buf, const T & x) { writeDoubleQuoted(x, buf.get()); return buf; } template WriteBuffer & operator<< (BinaryManipWriteBuffer buf, const T & x) { writeBinary(x, buf.get()); return buf; } +template WriteBuffer & operator<< (XMLManipWriteBuffer buf, const T & x) { writeText(x, buf.get()); return buf; } inline WriteBuffer & operator<< (EscapeManipWriteBuffer buf, const String & x) { writeEscapedString(x, buf); return buf; } inline WriteBuffer & operator<< (EscapeManipWriteBuffer buf, std::string_view x) { writeEscapedString(x, buf); return buf; } @@ -63,6 +67,10 @@ inline WriteBuffer & operator<< (QuoteManipWriteBuffer buf, const char * x inline WriteBuffer & operator<< (DoubleQuoteManipWriteBuffer buf, const char * x) { writeAnyQuotedString<'"'>(x, x + strlen(x), buf.get()); return buf; } inline WriteBuffer & operator<< (BinaryManipWriteBuffer buf, const char * x) { writeStringBinary(x, buf.get()); return buf; } +inline WriteBuffer & operator<< (XMLManipWriteBuffer buf, std::string_view x) { writeXMLStringForTextElementOrAttributeValue(x, buf); return buf; } +inline WriteBuffer & operator<< (XMLManipWriteBuffer buf, StringRef x) { writeXMLStringForTextElementOrAttributeValue(x.toView(), buf); return buf; } +inline WriteBuffer & operator<< (XMLManipWriteBuffer buf, const char * x) { writeXMLStringForTextElementOrAttributeValue(std::string_view(x), buf); return buf; } + /// The manipulator calls the WriteBuffer method `next` - this makes the buffer reset. For nested buffers, the reset is not recursive. enum FlushManip { flush }; From d4f1ac60bc2fb3a8ffb20491705c3ffa823e7b62 Mon Sep 17 00:00:00 2001 From: Vladimir C Date: Mon, 6 Mar 2023 10:30:51 +0100 Subject: [PATCH 279/342] Update comment in alter_materialized_view_consistent --- .../0_stateless/01019_alter_materialized_view_consistent.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh b/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh index 496215ba492..3a2eac1f38f 100755 --- a/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh +++ b/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh @@ -54,6 +54,7 @@ function alter_thread() { for i in {0..5}; do ALTER[$i]="ALTER TABLE mv MODIFY QUERY SELECT v == 1 as test, v as case FROM src_a;" done + # Insert 3 ALTERs to src_b, one in the first half of the array and two in arbitrary positions. ALTER[$RANDOM % 3]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" ALTER[$RANDOM % 6]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" ALTER[$RANDOM % 6]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;" From 48f644a95859af19efa3999369214755e3f81edb Mon Sep 17 00:00:00 2001 From: Vladimir C Date: Mon, 6 Mar 2023 10:38:21 +0100 Subject: [PATCH 280/342] Fix style in JoiningTransform.cpp --- src/Processors/Transforms/JoiningTransform.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Processors/Transforms/JoiningTransform.cpp b/src/Processors/Transforms/JoiningTransform.cpp index bf80de67b42..120ff51cad1 100644 --- a/src/Processors/Transforms/JoiningTransform.cpp +++ b/src/Processors/Transforms/JoiningTransform.cpp @@ -333,8 +333,6 @@ IProcessor::Status DelayedJoinedBlocksWorkerTransform::prepare() if (inputs.size() != 1 && outputs.size() != 1) throw Exception(ErrorCodes::LOGICAL_ERROR, "DelayedJoinedBlocksWorkerTransform must have exactly one input port"); - - if (output_chunk) { input.setNotNeeded(); @@ -414,7 +412,6 @@ void DelayedJoinedBlocksTransform::work() finished = finished || delayed_blocks == nullptr; } - IProcessor::Status DelayedJoinedBlocksTransform::prepare() { for (auto & output : outputs) From 56e8547f7e1573a1f60188e6cc7223327c2c3755 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Mon, 6 Mar 2023 10:47:32 +0000 Subject: [PATCH 281/342] Do not apply the optimization when plan is built only for analysis --- src/Planner/PlannerJoinTree.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index 06dc5b70bc1..6f818e2c8f7 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -410,13 +410,12 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(const QueryTreeNodePtr & tabl } /// Apply trivial_count optimization if possible - bool is_trivial_count_applied = is_single_table_expression && table_node && select_query_info.has_aggregates + bool is_trivial_count_applied = !select_query_options.only_analyze && is_single_table_expression && table_node && select_query_info.has_aggregates && applyTrivialCountIfPossible(query_plan, *table_node, select_query_info.query_tree, planner_context->getQueryContext(), columns_names); if (is_trivial_count_applied) { - if (!select_query_options.only_analyze) - from_stage = QueryProcessingStage::WithMergeableState; + from_stage = QueryProcessingStage::WithMergeableState; } else { From cb3dd3c2009ac2c5ab9e397dbf91675472f0611c Mon Sep 17 00:00:00 2001 From: kssenii Date: Mon, 6 Mar 2023 13:06:31 +0100 Subject: [PATCH 282/342] Fix flaky test --- tests/integration/test_filesystem_layout/test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/test_filesystem_layout/test.py b/tests/integration/test_filesystem_layout/test.py index 898bbc40eb9..2be478f95d0 100644 --- a/tests/integration/test_filesystem_layout/test.py +++ b/tests/integration/test_filesystem_layout/test.py @@ -44,8 +44,6 @@ def test_file_path_escaping(started_cluster): ] ) - -def test_file_path_escaping_atomic_db(started_cluster): node.query("CREATE DATABASE IF NOT EXISTS `test 2` ENGINE = Atomic") node.query( """ From aa776d00fec38fc80c4ed584fe46e9d0a82ac3fd Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:24:36 +0100 Subject: [PATCH 283/342] Use executors snapshot --- src/Interpreters/ProcessList.cpp | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Interpreters/ProcessList.cpp b/src/Interpreters/ProcessList.cpp index 37cb9ee599f..bf452775d27 100644 --- a/src/Interpreters/ProcessList.cpp +++ b/src/Interpreters/ProcessList.cpp @@ -397,23 +397,26 @@ CancellationCode QueryStatus::cancelQuery(bool) is_killed.store(true); - std::unique_lock lock(executors_mutex); - for (const auto & e : executors) + std::vector executors_snapshot; + { - /// We should call cancel() with unlocked executors_mutex, because - /// cancel() can try to lock some internal mutex that is already locked by query executing - /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, - /// which will lead to deadlock. - /// Note that the size and the content of executors cannot be changed while - /// executors_mutex is unlocked, because: - /// 1) We don't allow adding new executors while cancelling query in addPipelineExecutor - /// 2) We don't actually remove executor holder from executors in removePipelineExecutor, - /// just mark that executor is invalid. - /// So, it's safe to continue iteration over executors after subsequent mutex locking. - lock.unlock(); - e->cancel(); - lock.lock(); + /// Create a snapshot of executors under a mutex. + std::lock_guard lock(executors_mutex); + executors_snapshot = executors; } + + /// We should call cancel() for each executor with unlocked executors_mutex, because + /// cancel() can try to lock some internal mutex that is already locked by query executing + /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, + /// which will lead to deadlock. + /// Note that the size and the content of executors cannot be changed while + /// executors_mutex is unlocked, because: + /// 1) We don't allow adding new executors while cancelling query in addPipelineExecutor + /// 2) We don't actually remove executor holder from executors in removePipelineExecutor, + /// just mark that executor is invalid. + /// So, it's ok to use a snapshot created above under a mutex, it won't be any differ from actual executors. + for (const auto & e : executors_shapshot) + e->cancel(); return CancellationCode::CancelSent; } From 9117c7491dde2fbe4e01a90f44cd433f5521c9cc Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 6 Mar 2023 12:55:02 +0000 Subject: [PATCH 284/342] Join threads if exception happened in constructor --- src/Common/ZooKeeper/ZooKeeperImpl.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Common/ZooKeeper/ZooKeeperImpl.cpp b/src/Common/ZooKeeper/ZooKeeperImpl.cpp index 1fbdd857379..b637bdea835 100644 --- a/src/Common/ZooKeeper/ZooKeeperImpl.cpp +++ b/src/Common/ZooKeeper/ZooKeeperImpl.cpp @@ -358,12 +358,27 @@ ZooKeeper::ZooKeeper( if (!args.auth_scheme.empty()) sendAuth(args.auth_scheme, args.identity); - send_thread = ThreadFromGlobalPool([this] { sendThread(); }); - receive_thread = ThreadFromGlobalPool([this] { receiveThread(); }); + try + { + send_thread = ThreadFromGlobalPool([this] { sendThread(); }); + receive_thread = ThreadFromGlobalPool([this] { receiveThread(); }); - initApiVersion(); + initApiVersion(); - ProfileEvents::increment(ProfileEvents::ZooKeeperInit); + ProfileEvents::increment(ProfileEvents::ZooKeeperInit); + } + catch (...) + { + tryLogCurrentException(log, "Failed to connect to ZooKeeper"); + + if (send_thread.joinable()) + send_thread.join(); + + if (receive_thread.joinable()) + receive_thread.join(); + + throw; + } } From 12525f768cd2375d6605a4cf9e2ebdcdda16dfd0 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 6 Mar 2023 14:18:01 +0100 Subject: [PATCH 285/342] Add default constructor for `MultiReadResponse` (#47254) * Add default constructor for MultiReadResponse * Remove optional * Fix style --- src/Backups/BackupCoordinationRemote.cpp | 18 +++++++-------- src/Common/ZooKeeper/ZooKeeper.h | 28 +++++++++++++++++++++--- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/Backups/BackupCoordinationRemote.cpp b/src/Backups/BackupCoordinationRemote.cpp index 029a27d2712..c0fb4d5e066 100644 --- a/src/Backups/BackupCoordinationRemote.cpp +++ b/src/Backups/BackupCoordinationRemote.cpp @@ -552,7 +552,7 @@ std::vector BackupCoordinationRemote::getAllFileInfos() const for (auto & batch : batched_escaped_names) { - std::optional sizes_and_checksums; + zkutil::ZooKeeper::MultiGetResponse sizes_and_checksums; { Strings file_names_paths; file_names_paths.reserve(batch.size()); @@ -561,7 +561,7 @@ std::vector BackupCoordinationRemote::getAllFileInfos() const ZooKeeperRetriesControl retries_ctl("getAllFileInfos::getSizesAndChecksums", zookeeper_retries_info); - retries_ctl.retryLoop([&]() + retries_ctl.retryLoop([&] { auto zk = getZooKeeper(); sizes_and_checksums = zk->get(file_names_paths); @@ -579,9 +579,9 @@ std::vector BackupCoordinationRemote::getAllFileInfos() const for (size_t i = 0; i < batch.size(); ++i) { auto file_name = batch[i]; - if (sizes_and_checksums.value()[i].error != Coordination::Error::ZOK) - throw zkutil::KeeperException(sizes_and_checksums.value()[i].error); - auto size_and_checksum = sizes_and_checksums.value()[i].data; + if (sizes_and_checksums[i].error != Coordination::Error::ZOK) + throw zkutil::KeeperException(sizes_and_checksums[i].error); + const auto & size_and_checksum = sizes_and_checksums[i].data; auto size = deserializeSizeAndChecksum(size_and_checksum).first; if (size) @@ -601,7 +601,7 @@ std::vector BackupCoordinationRemote::getAllFileInfos() const std::move(empty_files_infos.begin(), empty_files_infos.end(), std::back_inserter(file_infos)); } - std::optional non_empty_file_infos_serialized; + zkutil::ZooKeeper::MultiGetResponse non_empty_file_infos_serialized; ZooKeeperRetriesControl retries_ctl("getAllFileInfos::getFileInfos", zookeeper_retries_info); retries_ctl.retryLoop([&]() { @@ -613,9 +613,9 @@ std::vector BackupCoordinationRemote::getAllFileInfos() const for (size_t i = 0; i < non_empty_file_names.size(); ++i) { FileInfo file_info; - if (non_empty_file_infos_serialized.value()[i].error != Coordination::Error::ZOK) - throw zkutil::KeeperException(non_empty_file_infos_serialized.value()[i].error); - file_info = deserializeFileInfo(non_empty_file_infos_serialized.value()[i].data); + if (non_empty_file_infos_serialized[i].error != Coordination::Error::ZOK) + throw zkutil::KeeperException(non_empty_file_infos_serialized[i].error); + file_info = deserializeFileInfo(non_empty_file_infos_serialized[i].data); file_info.file_name = unescapeForFileName(non_empty_file_names[i]); non_empty_files_infos.emplace_back(std::move(file_info)); } diff --git a/src/Common/ZooKeeper/ZooKeeper.h b/src/Common/ZooKeeper/ZooKeeper.h index 9de8241cfbe..d20d036f04e 100644 --- a/src/Common/ZooKeeper/ZooKeeper.h +++ b/src/Common/ZooKeeper/ZooKeeper.h @@ -33,6 +33,12 @@ namespace CurrentMetrics namespace DB { class ZooKeeperLog; + +namespace ErrorCodes +{ + extern const int LOGICAL_ERROR; +} + } namespace zkutil @@ -79,13 +85,23 @@ concept ZooKeeperResponse = std::derived_from; template struct MultiReadResponses { + MultiReadResponses() = default; + template explicit MultiReadResponses(TResponses responses_) : responses(std::move(responses_)) {} size_t size() const { - return std::visit([](auto && resp) { return resp.size(); }, responses); + return std::visit( + [&](const TResponses & resp) -> size_t + { + if constexpr (std::same_as) + throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "No responses set for MultiRead"); + else + return resp.size(); + }, + responses); } ResponseType & operator[](size_t index) @@ -94,8 +110,10 @@ struct MultiReadResponses [&](TResponses & resp) -> ResponseType & { if constexpr (std::same_as) + { return dynamic_cast(*resp[index]); - else + } + else if constexpr (std::same_as) { if constexpr (try_multi) { @@ -107,6 +125,10 @@ struct MultiReadResponses } return resp[index]; } + else + { + throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "No responses set for MultiRead"); + } }, responses); } @@ -137,7 +159,7 @@ private: size_t size() const { return future_responses.size(); } }; - std::variant responses; + std::variant responses; }; /// ZooKeeper session. The interface is substantially different from the usual libzookeeper API. From c13638904af33b71d76e4eae0b91b894588879b9 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Mon, 6 Mar 2023 14:38:54 +0100 Subject: [PATCH 286/342] Adjust the aggregation interval --- tests/ci/autoscale_runners_lambda/app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/ci/autoscale_runners_lambda/app.py b/tests/ci/autoscale_runners_lambda/app.py index 596e675ee24..7e3af3f6779 100644 --- a/tests/ci/autoscale_runners_lambda/app.py +++ b/tests/ci/autoscale_runners_lambda/app.py @@ -21,6 +21,11 @@ RUNNER_TYPE_LABELS = [ "style-checker", "style-checker-aarch64", ] + +# 4 HOUR - is a balance to get the most precise values +# - Our longest possible running check is around 5h on the worst scenario +# - The long queue won't be wiped out and replaced, so the measurmenet is fine +# - If the data is spoiled by something, we are from the bills perspective QUEUE_QUERY = f"""SELECT last_status AS status, toUInt32(count()) AS length, @@ -35,7 +40,7 @@ FROM FROM default.workflow_jobs WHERE has(labels, 'self-hosted') AND hasAny({RUNNER_TYPE_LABELS}, labels) - AND started_at > now() - INTERVAL 1 DAY + AND started_at > now() - INTERVAL 4 HOUR GROUP BY ALL HAVING last_status IN ('in_progress', 'queued') ) From e4751f95e1860b52652f2c16d930ab6280d346c6 Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Mon, 6 Mar 2023 14:56:05 +0100 Subject: [PATCH 287/342] Fix typo --- src/Interpreters/ProcessList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interpreters/ProcessList.cpp b/src/Interpreters/ProcessList.cpp index bf452775d27..a26844ae73c 100644 --- a/src/Interpreters/ProcessList.cpp +++ b/src/Interpreters/ProcessList.cpp @@ -415,7 +415,7 @@ CancellationCode QueryStatus::cancelQuery(bool) /// 2) We don't actually remove executor holder from executors in removePipelineExecutor, /// just mark that executor is invalid. /// So, it's ok to use a snapshot created above under a mutex, it won't be any differ from actual executors. - for (const auto & e : executors_shapshot) + for (const auto & e : executors_snapshot) e->cancel(); return CancellationCode::CancelSent; From 9e3794f7d73b983a7cf4afb1bad9f01b650bfb71 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Mon, 6 Mar 2023 14:36:53 +0000 Subject: [PATCH 288/342] Skip for sanitizer builds --- tests/integration/test_keeper_four_word_command/test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/test_keeper_four_word_command/test.py b/tests/integration/test_keeper_four_word_command/test.py index d3fcfcc3014..412780c8f0f 100644 --- a/tests/integration/test_keeper_four_word_command/test.py +++ b/tests/integration/test_keeper_four_word_command/test.py @@ -682,6 +682,9 @@ def test_cmd_rqld(started_cluster): def test_cmd_clrs(started_cluster): + if node1.is_built_with_sanitizer(): + return + def get_memory_purges(): return node1.query( "SELECT value FROM system.events WHERE event = 'MemoryAllocatorPurge' SETTINGS system_events_show_zero_values = 1" From d8e5fb519557041dc03c64026775329720da3d2a Mon Sep 17 00:00:00 2001 From: avogar Date: Mon, 6 Mar 2023 15:12:15 +0000 Subject: [PATCH 289/342] Use switch, use lz4 by default --- src/Core/Settings.h | 6 ++-- src/Core/SettingsChangesHistory.h | 5 ++- .../Formats/Impl/ORCBlockOutputFormat.cpp | 32 ++++++++--------- .../Formats/Impl/ParquetBlockOutputFormat.cpp | 36 +++++++++---------- 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 8eaecff9708..ae5d5326031 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -858,7 +858,7 @@ class IColumn; M(Bool, output_format_parquet_string_as_string, false, "Use Parquet String type instead of Binary for String columns.", 0) \ M(Bool, output_format_parquet_fixed_string_as_fixed_byte_array, true, "Use Parquet FIXED_LENGTH_BYTE_ARRAY type instead of Binary for FixedString columns.", 0) \ M(ParquetVersion, output_format_parquet_version, "2.latest", "Parquet format version for output format. Supported versions: 1.0, 2.4, 2.6 and 2.latest (default)", 0) \ - M(ParquetCompression, output_format_parquet_compression_method, "snappy", "Compression method for Parquet output format. Supported codecs: snappy, lz4, brotli, zstd, gzip, none (uncompressed)", 0) \ + M(ParquetCompression, output_format_parquet_compression_method, "lz4", "Compression method for Parquet output format. Supported codecs: snappy, lz4, brotli, zstd, gzip, none (uncompressed)", 0) \ M(String, output_format_avro_codec, "", "Compression codec used for output. Possible values: 'null', 'deflate', 'snappy'.", 0) \ M(UInt64, output_format_avro_sync_interval, 16 * 1024, "Sync interval in bytes.", 0) \ M(String, output_format_avro_string_column_pattern, "", "For Avro format: regexp of String columns to select as AVRO string.", 0) \ @@ -901,10 +901,10 @@ class IColumn; M(Bool, output_format_arrow_low_cardinality_as_dictionary, false, "Enable output LowCardinality type as Dictionary Arrow type", 0) \ M(Bool, output_format_arrow_string_as_string, false, "Use Arrow String type instead of Binary for String columns", 0) \ M(Bool, output_format_arrow_fixed_string_as_fixed_byte_array, true, "Use Arrow FIXED_SIZE_BINARY type instead of Binary for FixedString columns.", 0) \ - M(ArrowCompression, output_format_arrow_compression_method, "none", "Compression method for Arrow output format. Supported codecs: lz4_frame, zstd, none (uncompressed)", 0) \ + M(ArrowCompression, output_format_arrow_compression_method, "lz4_frame", "Compression method for Arrow output format. Supported codecs: lz4_frame, zstd, none (uncompressed)", 0) \ \ M(Bool, output_format_orc_string_as_string, false, "Use ORC String type instead of Binary for String columns", 0) \ - M(ORCCompression, output_format_orc_compression_method, "none", "Compression method for ORC output format. Supported codecs: lz4, snappy, zlib, zstd, none (uncompressed)", 0) \ + M(ORCCompression, output_format_orc_compression_method, "lz4", "Compression method for ORC output format. Supported codecs: lz4, snappy, zlib, zstd, none (uncompressed)", 0) \ \ M(EnumComparingMode, format_capn_proto_enum_comparising_mode, FormatSettings::EnumComparingMode::BY_VALUES, "How to map ClickHouse Enum and CapnProto Enum", 0) \ \ diff --git a/src/Core/SettingsChangesHistory.h b/src/Core/SettingsChangesHistory.h index 04f328bb665..91b3dff3141 100644 --- a/src/Core/SettingsChangesHistory.h +++ b/src/Core/SettingsChangesHistory.h @@ -81,7 +81,10 @@ namespace SettingsChangesHistory static std::map settings_changes_history = { {"23.3", {{"output_format_parquet_version", "1.0", "2.latest", "Use latest Parquet format version for output format"}, - {"input_format_json_ignore_unknown_keys_in_named_tuple", false, true, "Improve parsing JSON objects as named tuples"}}}, + {"input_format_json_ignore_unknown_keys_in_named_tuple", false, true, "Improve parsing JSON objects as named tuples"}, + {"output_format_arrow_compression_method", "none", "lz4_frame", "Use lz4 compression in Arrow output format by default"}, + {"output_format_parquet_compression_method", "snappy", "lz4", "Use lz4 compression in Parquet output format by default"}, + {"output_format_orc_compression_method", "none", "lz4_frame", "Use lz4 compression in ORC output format by default"}}}, {"23.2", {{"output_format_parquet_fixed_string_as_fixed_byte_array", false, true, "Use Parquet FIXED_LENGTH_BYTE_ARRAY type for FixedString by default"}, {"output_format_arrow_fixed_string_as_fixed_byte_array", false, true, "Use Arrow FIXED_SIZE_BINARY type for FixedString by default"}, {"query_plan_remove_redundant_distinct", false, true, "Remove redundant Distinct step in query plan"}, diff --git a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp index 39cacde94ed..4264e9da4d6 100644 --- a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp @@ -23,7 +23,7 @@ #include namespace DB -{ +{7 namespace ErrorCodes { @@ -36,24 +36,22 @@ namespace orc::CompressionKind getORCCompression(FormatSettings::ORCCompression method) { - if (method == FormatSettings::ORCCompression::NONE) - return orc::CompressionKind::CompressionKind_NONE; - + switch (method) + { + case FormatSettings::ORCCompression::NONE: + return orc::CompressionKind::CompressionKind_NONE; + case FormatSettings::ORCCompression::SNAPPY: #if USE_SNAPPY - if (method == FormatSettings::ORCCompression::SNAPPY) - return orc::CompressionKind::CompressionKind_SNAPPY; + return orc::CompressionKind::CompressionKind_SNAPPY; #endif - - if (method == FormatSettings::ORCCompression::ZSTD) - return orc::CompressionKind::CompressionKind_ZSTD; - - if (method == FormatSettings::ORCCompression::LZ4) - return orc::CompressionKind::CompressionKind_LZ4; - - if (method == FormatSettings::ORCCompression::ZLIB) - return orc::CompressionKind::CompressionKind_ZLIB; - - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Snappy compression method is not supported"); + case FormatSettings::ORCCompression::ZSTD: + return orc::CompressionKind::CompressionKind_ZSTD; + case FormatSettings::ORCCompression::LZ4: + return orc::CompressionKind::CompressionKind_LZ4; + case FormatSettings::ORCCompression::ZLIB: + return orc::CompressionKind::CompressionKind_ZLIB; + } } } diff --git a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp index cedd8a9c54c..3695bb9d110 100644 --- a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp @@ -37,29 +37,27 @@ static parquet::ParquetVersion::type getParquetVersion(const FormatSettings & se parquet::Compression::type getParquetCompression(FormatSettings::ParquetCompression method) { - if (method == FormatSettings::ParquetCompression::NONE) - return parquet::Compression::type::UNCOMPRESSED; - + switch (method) + { + case FormatSettings::ParquetCompression::NONE: + return parquet::Compression::type::UNCOMPRESSED; + case FormatSettings::ParquetCompression::SNAPPY: #if USE_SNAPPY - if (method == FormatSettings::ParquetCompression::SNAPPY) - return parquet::Compression::type::SNAPPY; + return parquet::Compression::type::SNAPPY; #endif - + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Snappy compression method is not supported"); + case FormatSettings::ParquetCompression::BROTLI: #if USE_BROTLI - if (method == FormatSettings::ParquetCompression::BROTLI) - return parquet::Compression::type::BROTLI; + return parquet::Compression::type::BROTLI; #endif - - if (method == FormatSettings::ParquetCompression::ZSTD) - return parquet::Compression::type::ZSTD; - - if (method == FormatSettings::ParquetCompression::LZ4) - return parquet::Compression::type::LZ4; - - if (method == FormatSettings::ParquetCompression::GZIP) - return parquet::Compression::type::GZIP; - - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Brotli compression method is not supported"); + case FormatSettings::ParquetCompression::ZSTD: + return parquet::Compression::type::ZSTD; + case FormatSettings::ParquetCompression::LZ4: + return parquet::Compression::type::LZ4; + case FormatSettings::ParquetCompression::GZIP: + return parquet::Compression::type::GZIP; + } } } From 1b0d0c61082574733c7281a5511184dc98a6a59d Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Mon, 6 Mar 2023 15:29:13 +0000 Subject: [PATCH 290/342] fix skip indexes --- src/Storages/IndicesDescription.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Storages/IndicesDescription.cpp b/src/Storages/IndicesDescription.cpp index 591a9082f7a..a93ac248c98 100644 --- a/src/Storages/IndicesDescription.cpp +++ b/src/Storages/IndicesDescription.cpp @@ -96,8 +96,11 @@ IndexDescription IndexDescription::getIndexFromAST(const ASTPtr & definition_ast result.expression = ExpressionAnalyzer(expr_list, syntax, context).getActions(true); result.sample_block = result.expression->getSampleBlock(); - for (const auto & elem : result.sample_block) + for (auto & elem : result.sample_block) { + if (!elem.column) + elem.column = elem.type->createColumn(); + result.column_names.push_back(elem.name); result.data_types.push_back(elem.type); } From 3076e929802c56fa9e38770f56a02e0622233e02 Mon Sep 17 00:00:00 2001 From: Vladimir C Date: Mon, 6 Mar 2023 16:56:02 +0100 Subject: [PATCH 291/342] Apply suggestions from code review Co-authored-by: Igor Nikonov <954088+devcrafter@users.noreply.github.com> --- src/Interpreters/GraceHashJoin.h | 1 - src/Processors/Transforms/JoiningTransform.cpp | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Interpreters/GraceHashJoin.h b/src/Interpreters/GraceHashJoin.h index cbc0f2f3266..b8d83f4cad0 100644 --- a/src/Interpreters/GraceHashJoin.h +++ b/src/Interpreters/GraceHashJoin.h @@ -140,7 +140,6 @@ private: FileBucket * current_bucket = nullptr; - /// Function `getDelayedBlocks` is a critical section, we process only one bucket at a time. mutable std::mutex current_bucket_mutex; InMemoryJoinPtr hash_join; diff --git a/src/Processors/Transforms/JoiningTransform.cpp b/src/Processors/Transforms/JoiningTransform.cpp index 120ff51cad1..bba8ec6fa16 100644 --- a/src/Processors/Transforms/JoiningTransform.cpp +++ b/src/Processors/Transforms/JoiningTransform.cpp @@ -328,7 +328,10 @@ IProcessor::Status DelayedJoinedBlocksWorkerTransform::prepare() } if (!output.canPush()) + { + input.setNotNeeded(); return Status::PortFull; + } if (inputs.size() != 1 && outputs.size() != 1) throw Exception(ErrorCodes::LOGICAL_ERROR, "DelayedJoinedBlocksWorkerTransform must have exactly one input port"); @@ -419,7 +422,7 @@ IProcessor::Status DelayedJoinedBlocksTransform::prepare() if (output.isFinished()) { /// If at least one output is finished, then we have read all data from buckets. - /// Some workers can still can busy with joining the last chunk of data in memory, + /// Some workers can still be busy with joining the last chunk of data in memory, /// but after that they also will finish when they will try to get next chunk. finished = true; continue; From 4bc443f3c757a7c3c3e61b9699b4cc57c468c178 Mon Sep 17 00:00:00 2001 From: Mike Kot Date: Fri, 3 Mar 2023 01:27:54 +0300 Subject: [PATCH 292/342] initial solution --- base/base/TypeList.h | 3 +- base/base/TypePair.h | 4 - src/Common/StackTrace.cpp | 516 ++++++++---------- src/Common/StackTrace.h | 13 +- src/Daemon/BaseDaemon.cpp | 2 +- src/DataTypes/DataTypeLowCardinality.cpp | 2 +- src/Functions/FunctionsEmbeddedDictionaries.h | 38 +- src/Functions/array/arrayIntersect.cpp | 8 +- src/Interpreters/CrashLog.cpp | 2 +- 9 files changed, 270 insertions(+), 318 deletions(-) delete mode 100644 base/base/TypePair.h diff --git a/base/base/TypeList.h b/base/base/TypeList.h index 244403b1c6b..310f0c0c586 100644 --- a/base/base/TypeList.h +++ b/base/base/TypeList.h @@ -4,7 +4,6 @@ #include #include #include "defines.h" -#include "TypePair.h" /// General-purpose typelist. Easy on compilation times as it does not use recursion. template @@ -28,7 +27,7 @@ namespace TypeListUtils /// In some contexts it's more handy to use functions in constexpr Root changeRoot(TypeList) { return {}; } template - constexpr void forEach(TypeList, F && f) { (std::forward(f)(Id{}), ...); } + constexpr void forEach(TypeList, F && f) { (std::forward(f)(TypeList{}), ...); } } template diff --git a/base/base/TypePair.h b/base/base/TypePair.h deleted file mode 100644 index 8c2f380618c..00000000000 --- a/base/base/TypePair.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -template struct TypePair {}; -template struct Id {}; diff --git a/src/Common/StackTrace.cpp b/src/Common/StackTrace.cpp index 0d47d3dcb92..eb6696e8c05 100644 --- a/src/Common/StackTrace.cpp +++ b/src/Common/StackTrace.cpp @@ -1,18 +1,21 @@ -#include +#include "StackTrace.h" + +#include +#include +#include #include #include -#include #include -#include +#include #include -#include #include +#include #include #include #include -#include +#include #include "config.h" @@ -20,24 +23,23 @@ # include #endif - namespace { - /// Currently this variable is set up once on server startup. - /// But we use atomic just in case, so it is possible to be modified at runtime. - std::atomic show_addresses = true; +/// Currently this variable is set up once on server startup. +/// But we use atomic just in case, so it is possible to be modified at runtime. +std::atomic show_addresses = true; - bool shouldShowAddress(const void * addr) - { - /// If the address is less than 4096, most likely it is a nullptr dereference with offset, - /// and showing this offset is secure nevertheless. - /// NOTE: 4096 is the page size on x86 and it can be different on other systems, - /// but for the purpose of this branch, it does not matter. - if (reinterpret_cast(addr) < 4096) - return true; +bool shouldShowAddress(const void * addr) +{ + /// If the address is less than 4096, most likely it is a nullptr dereference with offset, + /// and showing this offset is secure nevertheless. + /// NOTE: 4096 is the page size on x86 and it can be different on other systems, + /// but for the purpose of this branch, it does not matter. + if (reinterpret_cast(addr) < 4096) + return true; - return show_addresses.load(std::memory_order_relaxed); - } + return show_addresses.load(std::memory_order_relaxed); +} } void StackTrace::setShowAddresses(bool show) @@ -45,155 +47,129 @@ void StackTrace::setShowAddresses(bool show) show_addresses.store(show, std::memory_order_relaxed); } +std::string SigsegvErrorString(const siginfo_t & info, [[maybe_unused]] const ucontext_t & context) +{ + using namespace std::string_literals; + std::string address + = info.si_addr == nullptr ? "NULL pointer"s : (shouldShowAddress(info.si_addr) ? fmt::format("{}", info.si_addr) : ""s); + + const std::string_view access = +#if defined(__x86_64__) && !defined(OS_FREEBSD) && !defined(OS_DARWIN) && !defined(__arm__) && !defined(__powerpc__) + (context.uc_mcontext.gregs[REG_ERR] & 0x02) ? "write" : "read"; +#else + ""; +#endif + + std::string_view message; + + switch (info.si_code) + { + case SEGV_ACCERR: + message = "Attempted access has violated the permissions assigned to the memory area"; + break; + case SEGV_MAPERR: + message = "Address not mapped to object"; + break; + default: + message = "Unknown si_code"; + break; + } + + return fmt::format("Address: {}. Access: {}. {}.", std::move(address), access, message); +} + +constexpr std::string_view SigbusErrorString(int si_code) +{ + switch (si_code) + { + case BUS_ADRALN: + return "Invalid address alignment."; + case BUS_ADRERR: + return "Non-existent physical address."; + case BUS_OBJERR: + return "Object specific hardware error."; + + // Linux specific +#if defined(BUS_MCEERR_AR) + case BUS_MCEERR_AR: + return "Hardware memory error: action required."; +#endif +#if defined(BUS_MCEERR_AO) + case BUS_MCEERR_AO: + return "Hardware memory error: action optional."; +#endif + default: + return "Unknown si_code."; + } +} + +constexpr std::string_view SigfpeErrorString(int si_code) +{ + switch (si_code) + { + case FPE_INTDIV: + return "Integer divide by zero."; + case FPE_INTOVF: + return "Integer overflow."; + case FPE_FLTDIV: + return "Floating point divide by zero."; + case FPE_FLTOVF: + return "Floating point overflow."; + case FPE_FLTUND: + return "Floating point underflow."; + case FPE_FLTRES: + return "Floating point inexact result."; + case FPE_FLTINV: + return "Floating point invalid operation."; + case FPE_FLTSUB: + return "Subscript out of range."; + default: + return "Unknown si_code."; + } +} + +constexpr std::string_view SigillErrorString(int si_code) +{ + switch (si_code) + { + case ILL_ILLOPC: + return "Illegal opcode."; + case ILL_ILLOPN: + return "Illegal operand."; + case ILL_ILLADR: + return "Illegal addressing mode."; + case ILL_ILLTRP: + return "Illegal trap."; + case ILL_PRVOPC: + return "Privileged opcode."; + case ILL_PRVREG: + return "Privileged register."; + case ILL_COPROC: + return "Coprocessor error."; + case ILL_BADSTK: + return "Internal stack error."; + default: + return "Unknown si_code."; + } +} std::string signalToErrorMessage(int sig, const siginfo_t & info, [[maybe_unused]] const ucontext_t & context) { - std::stringstream error; // STYLE_CHECK_ALLOW_STD_STRING_STREAM - error.exceptions(std::ios::failbit); switch (sig) { case SIGSEGV: - { - /// Print info about address and reason. - if (nullptr == info.si_addr) - error << "Address: NULL pointer."; - else if (shouldShowAddress(info.si_addr)) - error << "Address: " << info.si_addr; - -#if defined(__x86_64__) && !defined(OS_FREEBSD) && !defined(OS_DARWIN) && !defined(__arm__) && !defined(__powerpc__) - auto err_mask = context.uc_mcontext.gregs[REG_ERR]; - if ((err_mask & 0x02)) - error << " Access: write."; - else - error << " Access: read."; -#endif - - switch (info.si_code) - { - case SEGV_ACCERR: - error << " Attempted access has violated the permissions assigned to the memory area."; - break; - case SEGV_MAPERR: - error << " Address not mapped to object."; - break; - default: - error << " Unknown si_code."; - break; - } - break; - } - + return SigsegvErrorString(info, context); case SIGBUS: - { - switch (info.si_code) - { - case BUS_ADRALN: - error << "Invalid address alignment."; - break; - case BUS_ADRERR: - error << "Non-existent physical address."; - break; - case BUS_OBJERR: - error << "Object specific hardware error."; - break; - - // Linux specific -#if defined(BUS_MCEERR_AR) - case BUS_MCEERR_AR: - error << "Hardware memory error: action required."; - break; -#endif -#if defined(BUS_MCEERR_AO) - case BUS_MCEERR_AO: - error << "Hardware memory error: action optional."; - break; -#endif - - default: - error << "Unknown si_code."; - break; - } - break; - } - + return std::string{SigbusErrorString(info.si_code)}; case SIGILL: - { - switch (info.si_code) - { - case ILL_ILLOPC: - error << "Illegal opcode."; - break; - case ILL_ILLOPN: - error << "Illegal operand."; - break; - case ILL_ILLADR: - error << "Illegal addressing mode."; - break; - case ILL_ILLTRP: - error << "Illegal trap."; - break; - case ILL_PRVOPC: - error << "Privileged opcode."; - break; - case ILL_PRVREG: - error << "Privileged register."; - break; - case ILL_COPROC: - error << "Coprocessor error."; - break; - case ILL_BADSTK: - error << "Internal stack error."; - break; - default: - error << "Unknown si_code."; - break; - } - break; - } - + return std::string{SigillErrorString(info.si_code)}; case SIGFPE: - { - switch (info.si_code) - { - case FPE_INTDIV: - error << "Integer divide by zero."; - break; - case FPE_INTOVF: - error << "Integer overflow."; - break; - case FPE_FLTDIV: - error << "Floating point divide by zero."; - break; - case FPE_FLTOVF: - error << "Floating point overflow."; - break; - case FPE_FLTUND: - error << "Floating point underflow."; - break; - case FPE_FLTRES: - error << "Floating point inexact result."; - break; - case FPE_FLTINV: - error << "Floating point invalid operation."; - break; - case FPE_FLTSUB: - error << "Subscript out of range."; - break; - default: - error << "Unknown si_code."; - break; - } - break; - } - + return std::string{SigfpeErrorString(info.si_code)}; case SIGTSTP: - { - error << "This is a signal used for debugging purposes by the user."; - break; - } + return "This is a signal used for debugging purposes by the user."; + default: + return ""; } - - return error.str(); } static void * getCallerAddress(const ucontext_t & context) @@ -207,10 +183,8 @@ static void * getCallerAddress(const ucontext_t & context) # else return reinterpret_cast(context.uc_mcontext.gregs[REG_RIP]); # endif - #elif defined(OS_DARWIN) && defined(__aarch64__) return reinterpret_cast(context.uc_mcontext->__ss.__pc); - #elif defined(OS_FREEBSD) && defined(__aarch64__) return reinterpret_cast(context.uc_mcontext.mc_gpregs.gp_elr); #elif defined(__aarch64__) @@ -228,20 +202,17 @@ static void * getCallerAddress(const ucontext_t & context) #endif } +// FIXME: looks like this is used only for Sentry but duplicates the whole algo, maybe replace? void StackTrace::symbolize( - const StackTrace::FramePointers & frame_pointers, [[maybe_unused]] size_t offset, - size_t size, StackTrace::Frames & frames) + const StackTrace::FramePointers & frame_pointers, [[maybe_unused]] size_t offset, size_t size, StackTrace::Frames & frames) { #if defined(__ELF__) && !defined(OS_FREEBSD) - auto symbol_index_ptr = DB::SymbolIndex::instance(); const DB::SymbolIndex & symbol_index = *symbol_index_ptr; std::unordered_map dwarfs; for (size_t i = 0; i < offset; ++i) - { frames[i].virtual_addr = frame_pointers[i]; - } for (size_t i = offset; i < size; ++i) { @@ -254,7 +225,7 @@ void StackTrace::symbolize( if (object) { current_frame.object = object->name; - if (std::filesystem::exists(current_frame.object.value())) + if (std::error_code ec; std::filesystem::exists(current_frame.object.value(), ec) && !ec) { auto dwarf_it = dwarfs.try_emplace(object->name, object->elf).first; @@ -269,34 +240,19 @@ void StackTrace::symbolize( } } else - { current_frame.object = "?"; - } - const auto * symbol = symbol_index.findSymbol(current_frame.virtual_addr); - if (symbol) - { - int status = 0; - current_frame.symbol = demangle(symbol->name, status); - } + if (const auto * symbol = symbol_index.findSymbol(current_frame.virtual_addr)) + current_frame.symbol = demangle(symbol->name); else - { current_frame.symbol = "?"; - } } #else for (size_t i = 0; i < size; ++i) - { frames[i].virtual_addr = frame_pointers[i]; - } #endif } -StackTrace::StackTrace() -{ - tryCapture(); -} - StackTrace::StackTrace(const ucontext_t & signal_context) { tryCapture(); @@ -325,81 +281,97 @@ StackTrace::StackTrace(const ucontext_t & signal_context) } } -StackTrace::StackTrace(NoCapture) -{ -} - void StackTrace::tryCapture() { - size = 0; #if USE_UNWIND size = unw_backtrace(frame_pointers.data(), capacity); __msan_unpoison(frame_pointers.data(), size * sizeof(frame_pointers[0])); +#else + size = 0; #endif } -size_t StackTrace::getSize() const +// Clickhouse uses bundled libc++ so type names will be same on every system thus it's save to hardcode them +constexpr std::pair replacements[] + = {{"::__1", ""}, {"std::basic_string, std::allocator>", "String"}}; + +String collapseNames(String && haystack) { - return size; + // TODO: surely there is a written version already for better in place search&replace + for (auto [needle, to] : replacements) + { + size_t pos = 0; + while ((pos = haystack.find(needle, pos)) != std::string::npos) + { + haystack.replace(pos, needle.length(), to); + pos += to.length(); + } + } + + return haystack; } -size_t StackTrace::getOffset() const +struct StackTraceRefTriple { - return offset; + const StackTrace::FramePointers & pointers; + size_t offset; + size_t size; +}; + +struct StackTraceTriple +{ + StackTrace::FramePointers pointers; + size_t offset; + size_t size; +}; + +template +concept MaybeRef = std::is_same_v || std::is_same_v; + +constexpr bool operator<(const MaybeRef auto & left, const MaybeRef auto & right) +{ + return std::tuple{left.pointers, left.size, left.offset} < std::tuple{right.pointers, right.size, right.offset}; } -const StackTrace::FramePointers & StackTrace::getFramePointers() const +static void +toStringEveryLineImpl([[maybe_unused]] bool fatal, const StackTraceRefTriple & stack_trace, Fn auto && callback) { - return frame_pointers; -} - -static void toStringEveryLineImpl( - [[maybe_unused]] bool fatal, - const StackTrace::FramePointers & frame_pointers, - size_t offset, - size_t size, - std::function callback) -{ - if (size == 0) + if (stack_trace.size == 0) return callback(""); #if defined(__ELF__) && !defined(OS_FREEBSD) - auto symbol_index_ptr = DB::SymbolIndex::instance(); - const DB::SymbolIndex & symbol_index = *symbol_index_ptr; - std::unordered_map dwarfs; - - std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM + std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM out.exceptions(std::ios::failbit); - for (size_t i = offset; i < size; ++i) + using enum DB::Dwarf::LocationInfoMode; + const auto mode = fatal ? FULL_WITH_INLINE : FAST; + + auto symbol_index_ptr = DB::SymbolIndex::instance(); + const DB::SymbolIndex & symbol_index = *symbol_index_ptr; + std::unordered_map dwarfs; + + for (size_t i = stack_trace.offset; i < stack_trace.size; ++i) { std::vector inline_frames; - const void * virtual_addr = frame_pointers[i]; + const void * virtual_addr = stack_trace.pointers[i]; const auto * object = symbol_index.findObject(virtual_addr); uintptr_t virtual_offset = object ? uintptr_t(object->address_begin) : 0; const void * physical_addr = reinterpret_cast(uintptr_t(virtual_addr) - virtual_offset); out << i << ". "; - if (object) + if (std::error_code ec; object && std::filesystem::exists(object->name, ec) && !ec) { - if (std::filesystem::exists(object->name)) - { - auto dwarf_it = dwarfs.try_emplace(object->name, object->elf).first; + auto dwarf_it = dwarfs.try_emplace(object->name, object->elf).first; - DB::Dwarf::LocationInfo location; - auto mode = fatal ? DB::Dwarf::LocationInfoMode::FULL_WITH_INLINE : DB::Dwarf::LocationInfoMode::FAST; - if (dwarf_it->second.findAddress(uintptr_t(physical_addr), location, mode, inline_frames)) - out << location.file.toString() << ":" << location.line << ": "; - } + DB::Dwarf::LocationInfo location; + + if (dwarf_it->second.findAddress(uintptr_t(physical_addr), location, mode, inline_frames)) + out << location.file.toString() << ":" << location.line << ": "; } - const auto * symbol = symbol_index.findSymbol(virtual_addr); - if (symbol) - { - int status = 0; - out << demangle(symbol->name, status); - } + if (const auto * const symbol = symbol_index.findSymbol(virtual_addr)) + out << collapseNames(demangle(symbol->name)); else out << "?"; @@ -411,64 +383,31 @@ static void toStringEveryLineImpl( for (size_t j = 0; j < inline_frames.size(); ++j) { const auto & frame = inline_frames[j]; - int status = 0; - callback(fmt::format("{}.{}. inlined from {}:{}: {}", - i, j+1, frame.location.file.toString(), frame.location.line, demangle(frame.name, status))); + callback(fmt::format( + "{}.{}. inlined from {}:{}: {}", + i, + j + 1, + frame.location.file.toString(), + frame.location.line, + collapseNames(demangle(frame.name)))); } callback(out.str()); out.str({}); } #else - std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM - out.exceptions(std::ios::failbit); - - for (size_t i = offset; i < size; ++i) - { - const void * addr = frame_pointers[i]; - if (shouldShowAddress(addr)) - { - out << i << ". " << addr; - - callback(out.str()); - out.str({}); - } - } + for (size_t i = stack_trace.offset; i < stack_trace.size; ++i) + if (const void * const addr = stack_trace.pointers[i]; shouldShowAddress(addr)) + callback(fmt::format("{}. {}", i, addr)); #endif } -static std::string toStringImpl(const StackTrace::FramePointers & frame_pointers, size_t offset, size_t size) +void StackTrace::toStringEveryLine(std::function callback) const { - std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM - out.exceptions(std::ios::failbit); - toStringEveryLineImpl(false, frame_pointers, offset, size, [&](const std::string & str) { out << str << '\n'; }); - return out.str(); + toStringEveryLineImpl(true, {frame_pointers, offset, size}, std::move(callback)); } -void StackTrace::toStringEveryLine(std::function callback) const -{ - toStringEveryLineImpl(true, frame_pointers, offset, size, std::move(callback)); -} - - -std::string StackTrace::toString() const -{ - return toStringStatic(frame_pointers, offset, size); -} - -std::string StackTrace::toString(void ** frame_pointers_, size_t offset, size_t size) -{ - __msan_unpoison(frame_pointers_, size * sizeof(*frame_pointers_)); - - StackTrace::FramePointers frame_pointers_copy{}; - for (size_t i = 0; i < size; ++i) - frame_pointers_copy[i] = frame_pointers_[i]; - - return toStringStatic(frame_pointers_copy, offset, size); -} - -using StackTraceRepresentation = std::tuple; -using StackTraceCache = std::map; +using StackTraceCache = std::map>; static StackTraceCache & cacheInstance() { @@ -478,21 +417,40 @@ static StackTraceCache & cacheInstance() static std::mutex stacktrace_cache_mutex; -std::string StackTrace::toStringStatic(const StackTrace::FramePointers & frame_pointers, size_t offset, size_t size) +String toStringCached(const StackTrace::FramePointers & pointers, size_t offset, size_t size) { /// Calculation of stack trace text is extremely slow. /// We use simple cache because otherwise the server could be overloaded by trash queries. /// Note that this cache can grow unconditionally, but practically it should be small. std::lock_guard lock{stacktrace_cache_mutex}; - StackTraceRepresentation key{frame_pointers, offset, size}; - auto & cache = cacheInstance(); - if (cache.contains(key)) - return cache[key]; + StackTraceCache & cache = cacheInstance(); - auto result = toStringImpl(frame_pointers, offset, size); - cache[key] = result; - return result; + if (auto it = cache.find(StackTraceRefTriple{pointers, offset, size}); it != cache.end()) + return it->second; + else + { + std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM + out.exceptions(std::ios::failbit); + toStringEveryLineImpl(false, {pointers, offset, size}, [&](std::string_view str) { out << str << '\n'; }); + + return cache.emplace(StackTraceTriple{pointers, offset, size}, out.str()).first->second; + } +} + +std::string StackTrace::toString() const +{ + return toStringCached(frame_pointers, offset, size); +} + +std::string StackTrace::toString(void ** frame_pointers_raw, size_t offset, size_t size) +{ + __msan_unpoison(frame_pointers_raw, size * sizeof(*frame_pointers_raw)); + + StackTrace::FramePointers frame_pointers; + std::copy_n(frame_pointers_raw, size, frame_pointers.begin()); + + return toStringCached(frame_pointers, offset, size); } void StackTrace::dropCache() diff --git a/src/Common/StackTrace.h b/src/Common/StackTrace.h index f07c05107ee..3940c880c5b 100644 --- a/src/Common/StackTrace.h +++ b/src/Common/StackTrace.h @@ -46,26 +46,25 @@ public: using Frames = std::array; /// Tries to capture stack trace - StackTrace(); + inline StackTrace() { tryCapture(); } /// Tries to capture stack trace. Fallbacks on parsing caller address from /// signal context if no stack trace could be captured explicit StackTrace(const ucontext_t & signal_context); /// Creates empty object for deferred initialization - explicit StackTrace(NoCapture); + explicit inline StackTrace(NoCapture) {} - size_t getSize() const; - size_t getOffset() const; - const FramePointers & getFramePointers() const; + constexpr size_t getSize() const { return size; } + constexpr size_t getOffset() const { return offset; } + const FramePointers & getFramePointers() const { return frame_pointers; } std::string toString() const; static std::string toString(void ** frame_pointers, size_t offset, size_t size); - static std::string toStringStatic(const FramePointers & frame_pointers, size_t offset, size_t size); static void dropCache(); static void symbolize(const FramePointers & frame_pointers, size_t offset, size_t size, StackTrace::Frames & frames); - void toStringEveryLine(std::function callback) const; + void toStringEveryLine(std::function callback) const; /// Displaying the addresses can be disabled for security reasons. /// If you turn off addresses, it will be more secure, but we will be unable to help you with debugging. diff --git a/src/Daemon/BaseDaemon.cpp b/src/Daemon/BaseDaemon.cpp index fbfa9e68774..18c4c0d97a0 100644 --- a/src/Daemon/BaseDaemon.cpp +++ b/src/Daemon/BaseDaemon.cpp @@ -374,7 +374,7 @@ private: } /// Write symbolized stack trace line by line for better grep-ability. - stack_trace.toStringEveryLine([&](const std::string & s) { LOG_FATAL(log, fmt::runtime(s)); }); + stack_trace.toStringEveryLine([&](std::string_view s) { LOG_FATAL(log, fmt::runtime(s)); }); #if defined(OS_LINUX) /// Write information about binary checksum. It can be difficult to calculate, so do it only after printing stack trace. diff --git a/src/DataTypes/DataTypeLowCardinality.cpp b/src/DataTypes/DataTypeLowCardinality.cpp index 5e3a1cd3a0e..8293455cabc 100644 --- a/src/DataTypes/DataTypeLowCardinality.cpp +++ b/src/DataTypes/DataTypeLowCardinality.cpp @@ -55,7 +55,7 @@ namespace } template - void operator()(Id) + void operator()(TypeList) { if (typeid_cast *>(&keys_type)) column = creator(static_cast *>(nullptr)); diff --git a/src/Functions/FunctionsEmbeddedDictionaries.h b/src/Functions/FunctionsEmbeddedDictionaries.h index e54ab0277d5..af040c6ab93 100644 --- a/src/Functions/FunctionsEmbeddedDictionaries.h +++ b/src/Functions/FunctionsEmbeddedDictionaries.h @@ -166,12 +166,12 @@ public: if (arguments[0]->getName() != TypeName) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {} (must be {})", - arguments[0]->getName(), getName(), String(TypeName)); + arguments[0]->getName(), getName(), TypeName); - if (arguments.size() == 2 && arguments[1]->getName() != TypeName) + if (arguments.size() == 2 && arguments[1]->getName() != "String") throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of the second ('point of view') argument of function {} (must be {})", - arguments[1]->getName(), getName(), String(TypeName)); + "Illegal type {} of the second ('point of view') argument of function {} (must be String)", + arguments[1]->getName(), getName()); return arguments[0]; } @@ -257,16 +257,16 @@ public: if (arguments[0]->getName() != TypeName) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of first argument of function {} (must be {})", - arguments[0]->getName(), getName(), String(TypeName)); + arguments[0]->getName(), getName(), TypeName); if (arguments[1]->getName() != TypeName) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of second argument of function {} (must be {})", - arguments[1]->getName(), getName(), String(TypeName)); + arguments[1]->getName(), getName(), TypeName); - if (arguments.size() == 3 && arguments[2]->getName() != TypeName) + if (arguments.size() == 3 && arguments[2]->getName() != "String") throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of the third ('point of view') argument of function {} (must be {})", - arguments[2]->getName(), getName(), String(TypeName)); + "Illegal type {} of the third ('point of view') argument of function {} (must be String)", + arguments[2]->getName(), getName()); return std::make_shared(); } @@ -390,12 +390,12 @@ public: if (arguments[0]->getName() != TypeName) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {} (must be {})", - arguments[0]->getName(), getName(), String(TypeName)); + arguments[0]->getName(), getName(), TypeName); - if (arguments.size() == 2 && arguments[1]->getName() != TypeName) + if (arguments.size() == 2 && arguments[1]->getName() != "String") throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of the second ('point of view') argument of function {} (must be {})", - arguments[1]->getName(), getName(), String(TypeName)); + "Illegal type {} of the second ('point of view') argument of function {} (must be String)", + arguments[1]->getName(), getName()); return std::make_shared(arguments[0]); } @@ -591,15 +591,15 @@ public: "Number of arguments for function {} doesn't match: passed {}, should be 1 or 2.", getName(), arguments.size()); - if (arguments[0]->getName() != TypeName) + if (arguments[0]->getName() != "UInt32") throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of the first argument of function {} (must be {})", - arguments[0]->getName(), getName(), String(TypeName)); + "Illegal type {} of the first argument of function {} (must be UInt32)", + arguments[0]->getName(), getName()); - if (arguments.size() == 2 && arguments[1]->getName() != TypeName) + if (arguments.size() == 2 && arguments[1]->getName() != "String") throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of the second argument of function {} (must be {})", - arguments[0]->getName(), getName(), String(TypeName)); + "Illegal type {} of the second argument of function {} (must be String)", + arguments[0]->getName(), getName()); return std::make_shared(); } diff --git a/src/Functions/array/arrayIntersect.cpp b/src/Functions/array/arrayIntersect.cpp index 1a718595a3b..c6f0a5afa62 100644 --- a/src/Functions/array/arrayIntersect.cpp +++ b/src/Functions/array/arrayIntersect.cpp @@ -107,7 +107,7 @@ private: : arrays(arrays_), data_type(data_type_), result(result_) {} template - void operator()(Id); + void operator()(TypeList); }; struct DecimalExecutor @@ -120,7 +120,7 @@ private: : arrays(arrays_), data_type(data_type_), result(result_) {} template - void operator()(Id); + void operator()(TypeList); }; }; @@ -446,7 +446,7 @@ ColumnPtr FunctionArrayIntersect::executeImpl(const ColumnsWithTypeAndName & arg } template -void FunctionArrayIntersect::NumberExecutor::operator()(Id) +void FunctionArrayIntersect::NumberExecutor::operator()(TypeList) { using Container = ClearableHashMapWithStackMemory, INITIAL_SIZE_DEGREE>; @@ -456,7 +456,7 @@ void FunctionArrayIntersect::NumberExecutor::operator()(Id) } template -void FunctionArrayIntersect::DecimalExecutor::operator()(Id) +void FunctionArrayIntersect::DecimalExecutor::operator()(TypeList) { using Container = ClearableHashMapWithStackMemory, INITIAL_SIZE_DEGREE>; diff --git a/src/Interpreters/CrashLog.cpp b/src/Interpreters/CrashLog.cpp index 47a9d62fba6..f1f0ffb6f60 100644 --- a/src/Interpreters/CrashLog.cpp +++ b/src/Interpreters/CrashLog.cpp @@ -80,7 +80,7 @@ void collectCrashLog(Int32 signal, UInt64 thread_id, const String & query_id, co for (size_t i = stack_trace_offset; i < stack_trace_size; ++i) trace.push_back(reinterpret_cast(stack_trace.getFramePointers()[i])); - stack_trace.toStringEveryLine([&trace_full](const std::string & line) { trace_full.push_back(line); }); + stack_trace.toStringEveryLine([&trace_full](std::string_view line) { trace_full.push_back(line); }); CrashLogElement element{static_cast(time / 1000000000), time, signal, thread_id, query_id, trace, trace_full}; crash_log_owned->add(element); From 07a9e486e6aafa184d7a3ea244db0758137128c7 Mon Sep 17 00:00:00 2001 From: Mike Kot Date: Sat, 4 Mar 2023 21:37:28 +0300 Subject: [PATCH 293/342] memset for frame pointers' --- src/Common/StackTrace.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Common/StackTrace.cpp b/src/Common/StackTrace.cpp index eb6696e8c05..0aebaf9801c 100644 --- a/src/Common/StackTrace.cpp +++ b/src/Common/StackTrace.cpp @@ -425,14 +425,15 @@ String toStringCached(const StackTrace::FramePointers & pointers, size_t offset, std::lock_guard lock{stacktrace_cache_mutex}; StackTraceCache & cache = cacheInstance(); + const StackTraceRefTriple key{pointers, offset, size}; - if (auto it = cache.find(StackTraceRefTriple{pointers, offset, size}); it != cache.end()) + if (auto it = cache.find(key); it != cache.end()) return it->second; else { - std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM + std::ostringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM out.exceptions(std::ios::failbit); - toStringEveryLineImpl(false, {pointers, offset, size}, [&](std::string_view str) { out << str << '\n'; }); + toStringEveryLineImpl(false, key, [&](std::string_view str) { out << str << '\n'; }); return cache.emplace(StackTraceTriple{pointers, offset, size}, out.str()).first->second; } @@ -447,7 +448,7 @@ std::string StackTrace::toString(void ** frame_pointers_raw, size_t offset, size { __msan_unpoison(frame_pointers_raw, size * sizeof(*frame_pointers_raw)); - StackTrace::FramePointers frame_pointers; + StackTrace::FramePointers frame_pointers{}; std::copy_n(frame_pointers_raw, size, frame_pointers.begin()); return toStringCached(frame_pointers, offset, size); From e50fd6e1ef67eaf7c69e173b9a4486c90e598f1f Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Mon, 6 Mar 2023 16:35:17 +0000 Subject: [PATCH 294/342] Update version_date.tsv and changelogs after v23.2.3.17-stable --- docker/keeper/Dockerfile | 2 +- docker/server/Dockerfile.alpine | 2 +- docker/server/Dockerfile.ubuntu | 2 +- docs/changelogs/v23.2.3.17-stable.md | 23 +++++++++++++++++++++++ utils/list-versions/version_date.tsv | 5 +++++ 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 docs/changelogs/v23.2.3.17-stable.md diff --git a/docker/keeper/Dockerfile b/docker/keeper/Dockerfile index 09395befdad..500249b5bd6 100644 --- a/docker/keeper/Dockerfile +++ b/docker/keeper/Dockerfile @@ -29,7 +29,7 @@ RUN arch=${TARGETARCH:-amd64} \ esac ARG REPOSITORY="https://s3.amazonaws.com/clickhouse-builds/22.4/31c367d3cd3aefd316778601ff6565119fe36682/package_release" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.3.17" ARG PACKAGES="clickhouse-keeper" # user/group precreated explicitly with fixed uid/gid on purpose. diff --git a/docker/server/Dockerfile.alpine b/docker/server/Dockerfile.alpine index 472f25eed2d..5f613eea4d0 100644 --- a/docker/server/Dockerfile.alpine +++ b/docker/server/Dockerfile.alpine @@ -33,7 +33,7 @@ RUN arch=${TARGETARCH:-amd64} \ # lts / testing / prestable / etc ARG REPO_CHANNEL="stable" ARG REPOSITORY="https://packages.clickhouse.com/tgz/${REPO_CHANNEL}" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.3.17" ARG PACKAGES="clickhouse-client clickhouse-server clickhouse-common-static" # user/group precreated explicitly with fixed uid/gid on purpose. diff --git a/docker/server/Dockerfile.ubuntu b/docker/server/Dockerfile.ubuntu index 5dbb244c298..3e99bca8bc2 100644 --- a/docker/server/Dockerfile.ubuntu +++ b/docker/server/Dockerfile.ubuntu @@ -22,7 +22,7 @@ RUN sed -i "s|http://archive.ubuntu.com|${apt_archive}|g" /etc/apt/sources.list ARG REPO_CHANNEL="stable" ARG REPOSITORY="deb https://packages.clickhouse.com/deb ${REPO_CHANNEL} main" -ARG VERSION="23.2.1.2537" +ARG VERSION="23.2.3.17" ARG PACKAGES="clickhouse-client clickhouse-server clickhouse-common-static" # set non-empty deb_location_url url to create a docker image diff --git a/docs/changelogs/v23.2.3.17-stable.md b/docs/changelogs/v23.2.3.17-stable.md new file mode 100644 index 00000000000..fb2c4e394dc --- /dev/null +++ b/docs/changelogs/v23.2.3.17-stable.md @@ -0,0 +1,23 @@ +--- +sidebar_position: 1 +sidebar_label: 2023 +--- + +# 2023 Changelog + +### ClickHouse release v23.2.3.17-stable (dec18bf7281) FIXME as compared to v23.2.2.20-stable (f6c269c8df2) + +#### Bug Fix (user-visible misbehavior in official stable or prestable release) + +* Backported in [#46907](https://github.com/ClickHouse/ClickHouse/issues/46907): - Fix incorrect alias recursion in QueryNormalizer. [#46609](https://github.com/ClickHouse/ClickHouse/pull/46609) ([Raúl Marín](https://github.com/Algunenano)). +* Backported in [#47091](https://github.com/ClickHouse/ClickHouse/issues/47091): - Fix arithmetic operations in aggregate optimization with `min` and `max`. [#46705](https://github.com/ClickHouse/ClickHouse/pull/46705) ([Duc Canh Le](https://github.com/canhld94)). +* Backported in [#46885](https://github.com/ClickHouse/ClickHouse/issues/46885): Fix MSan report in the `maxIntersections` function. This closes [#43126](https://github.com/ClickHouse/ClickHouse/issues/43126). [#46847](https://github.com/ClickHouse/ClickHouse/pull/46847) ([Alexey Milovidov](https://github.com/alexey-milovidov)). +* Backported in [#47067](https://github.com/ClickHouse/ClickHouse/issues/47067): Fix typo in systemd service, which causes the systemd service start to fail. [#47051](https://github.com/ClickHouse/ClickHouse/pull/47051) ([Palash Goel](https://github.com/palash-goel)). +* Backported in [#47259](https://github.com/ClickHouse/ClickHouse/issues/47259): Fix concrete columns PREWHERE support. [#47154](https://github.com/ClickHouse/ClickHouse/pull/47154) ([Azat Khuzhin](https://github.com/azat)). + +#### NOT FOR CHANGELOG / INSIGNIFICANT + +* Use /etc/default/clickhouse in systemd too [#47003](https://github.com/ClickHouse/ClickHouse/pull/47003) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). +* do flushUntrackedMemory when context switches [#47102](https://github.com/ClickHouse/ClickHouse/pull/47102) ([Sema Checherinda](https://github.com/CheSema)). +* Update typing for a new PyGithub version [#47123](https://github.com/ClickHouse/ClickHouse/pull/47123) ([Mikhail f. Shiryaev](https://github.com/Felixoid)). + diff --git a/utils/list-versions/version_date.tsv b/utils/list-versions/version_date.tsv index 3814e94bf24..34bc3f646fc 100644 --- a/utils/list-versions/version_date.tsv +++ b/utils/list-versions/version_date.tsv @@ -1,7 +1,11 @@ +v23.2.3.17-stable 2023-03-06 +v23.2.2.20-stable 2023-03-01 v23.2.1.2537-stable 2023-02-23 +v23.1.4.58-stable 2023-03-01 v23.1.3.5-stable 2023-02-03 v23.1.2.9-stable 2023-01-29 v23.1.1.3077-stable 2023-01-25 +v22.12.4.76-stable 2023-03-01 v22.12.3.5-stable 2023-01-10 v22.12.2.25-stable 2023-01-06 v22.12.1.1752-stable 2022-12-15 @@ -25,6 +29,7 @@ v22.9.4.32-stable 2022-10-26 v22.9.3.18-stable 2022-09-30 v22.9.2.7-stable 2022-09-23 v22.9.1.2603-stable 2022-09-22 +v22.8.14.53-lts 2023-02-27 v22.8.13.20-lts 2023-01-29 v22.8.12.45-lts 2023-01-10 v22.8.11.15-lts 2022-12-08 From df34ab240d19d68226b230413d0ae023d98686f7 Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Mon, 6 Mar 2023 18:03:17 +0100 Subject: [PATCH 295/342] Don't use switch --- .../Formats/Impl/ORCBlockOutputFormat.cpp | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp index 4264e9da4d6..7fae2c8f480 100644 --- a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp @@ -36,22 +36,24 @@ namespace orc::CompressionKind getORCCompression(FormatSettings::ORCCompression method) { - switch (method) - { - case FormatSettings::ORCCompression::NONE: - return orc::CompressionKind::CompressionKind_NONE; - case FormatSettings::ORCCompression::SNAPPY: + if (method == FormatSettings::ORCCompression::NONE) + return orc::CompressionKind::CompressionKind_NONE; + #if USE_SNAPPY - return orc::CompressionKind::CompressionKind_SNAPPY; + if (method == FormatSettings::ORCCompression::SNAPPY) + return orc::CompressionKind::CompressionKind_SNAPPY; #endif - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Snappy compression method is not supported"); - case FormatSettings::ORCCompression::ZSTD: - return orc::CompressionKind::CompressionKind_ZSTD; - case FormatSettings::ORCCompression::LZ4: - return orc::CompressionKind::CompressionKind_LZ4; - case FormatSettings::ORCCompression::ZLIB: - return orc::CompressionKind::CompressionKind_ZLIB; - } + + if (method == FormatSettings::ORCCompression::ZSTD) + return orc::CompressionKind::CompressionKind_ZSTD; + + if (method == FormatSettings::ORCCompression::LZ4) + return orc::CompressionKind::CompressionKind_LZ4; + + if (method == FormatSettings::ORCCompression::ZLIB) + return orc::CompressionKind::CompressionKind_ZLIB; + + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); } } From fed645a4945efab6aa9a2b98ac2861de483be158 Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Mon, 6 Mar 2023 18:03:54 +0100 Subject: [PATCH 296/342] Don't use switch --- .../Formats/Impl/ParquetBlockOutputFormat.cpp | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp index 3695bb9d110..cedd8a9c54c 100644 --- a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp @@ -37,27 +37,29 @@ static parquet::ParquetVersion::type getParquetVersion(const FormatSettings & se parquet::Compression::type getParquetCompression(FormatSettings::ParquetCompression method) { - switch (method) - { - case FormatSettings::ParquetCompression::NONE: - return parquet::Compression::type::UNCOMPRESSED; - case FormatSettings::ParquetCompression::SNAPPY: + if (method == FormatSettings::ParquetCompression::NONE) + return parquet::Compression::type::UNCOMPRESSED; + #if USE_SNAPPY - return parquet::Compression::type::SNAPPY; + if (method == FormatSettings::ParquetCompression::SNAPPY) + return parquet::Compression::type::SNAPPY; #endif - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Snappy compression method is not supported"); - case FormatSettings::ParquetCompression::BROTLI: + #if USE_BROTLI - return parquet::Compression::type::BROTLI; + if (method == FormatSettings::ParquetCompression::BROTLI) + return parquet::Compression::type::BROTLI; #endif - throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Brotli compression method is not supported"); - case FormatSettings::ParquetCompression::ZSTD: - return parquet::Compression::type::ZSTD; - case FormatSettings::ParquetCompression::LZ4: - return parquet::Compression::type::LZ4; - case FormatSettings::ParquetCompression::GZIP: - return parquet::Compression::type::GZIP; - } + + if (method == FormatSettings::ParquetCompression::ZSTD) + return parquet::Compression::type::ZSTD; + + if (method == FormatSettings::ParquetCompression::LZ4) + return parquet::Compression::type::LZ4; + + if (method == FormatSettings::ParquetCompression::GZIP) + return parquet::Compression::type::GZIP; + + throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method"); } } From b5dffe7417527bb11ef5979133e092ec1ca222db Mon Sep 17 00:00:00 2001 From: alesapin Date: Mon, 6 Mar 2023 19:57:09 +0100 Subject: [PATCH 297/342] Fix bug in zero copy replica which can lead to dataloss --- src/Storages/StorageReplicatedMergeTree.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index 54ae8aa5a7b..9e79a715610 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -8434,7 +8434,11 @@ std::pair StorageReplicatedMergeTree::unlockSharedDataByID( } else if (error_code == Coordination::Error::ZNONODE) { - LOG_TRACE(logger, "Node with parent zookeeper lock {} for part {} doesn't exist (part was unlocked before)", zookeeper_part_uniq_node, part_name); + /// We don't know what to do, because this part can be mutation part + /// with hardlinked columns. Since we don't have this information (about blobs not to remove) + /// we refuce to remove blobs. + LOG_WARNING(logger, "Node with parent zookeeper lock {} for part {} doesn't exist (part was unlocked before), refuse to remove blobs", zookeeper_part_uniq_node, part_name); + return std::make_pair{false, {}}; } else { From 0b46de317b8b6217b9e920f494509b656f102c21 Mon Sep 17 00:00:00 2001 From: Denny Crane Date: Mon, 6 Mar 2023 16:33:28 -0400 Subject: [PATCH 298/342] Update ip-address-functions.md --- docs/en/sql-reference/functions/ip-address-functions.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/en/sql-reference/functions/ip-address-functions.md b/docs/en/sql-reference/functions/ip-address-functions.md index 3843ca0fc36..cbb93ee15d7 100644 --- a/docs/en/sql-reference/functions/ip-address-functions.md +++ b/docs/en/sql-reference/functions/ip-address-functions.md @@ -286,6 +286,14 @@ Same as `toIPv4`, but if the IPv4 address has an invalid format, it returns 0. Same as `toIPv4`, but if the IPv4 address has an invalid format, it returns null. +## toIPv6OrDefault(string) + +Same as `toIPv6`, but if the IPv6 address has an invalid format, it returns 0. + +## toIPv6OrNull(string) + +Same as `toIPv6`, but if the IPv6 address has an invalid format, it returns null. + ## toIPv6 Converts a string form of IPv6 address to [IPv6](../../sql-reference/data-types/domains/ipv6.md) type. If the IPv6 address has an invalid format, returns an empty value. From d8029b7b2d092c218668826abcfdfc1f3c2f6061 Mon Sep 17 00:00:00 2001 From: Denny Crane Date: Mon, 6 Mar 2023 16:36:04 -0400 Subject: [PATCH 299/342] Update ip-address-functions.md --- docs/en/sql-reference/functions/ip-address-functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/sql-reference/functions/ip-address-functions.md b/docs/en/sql-reference/functions/ip-address-functions.md index cbb93ee15d7..47058a28d12 100644 --- a/docs/en/sql-reference/functions/ip-address-functions.md +++ b/docs/en/sql-reference/functions/ip-address-functions.md @@ -280,7 +280,7 @@ SELECT ## toIPv4OrDefault(string) -Same as `toIPv4`, but if the IPv4 address has an invalid format, it returns 0. +Same as `toIPv4`, but if the IPv4 address has an invalid format, it returns `0.0.0.0` (0 IPv4). ## toIPv4OrNull(string) @@ -288,7 +288,7 @@ Same as `toIPv4`, but if the IPv4 address has an invalid format, it returns null ## toIPv6OrDefault(string) -Same as `toIPv6`, but if the IPv6 address has an invalid format, it returns 0. +Same as `toIPv6`, but if the IPv6 address has an invalid format, it returns `::` (0 IPv6). ## toIPv6OrNull(string) From 93ba0f4bdf45384bcbcf1296226275fe4a5ea7e6 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 7 Mar 2023 01:30:44 +0300 Subject: [PATCH 300/342] Update StackTrace.cpp --- src/Common/StackTrace.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/StackTrace.cpp b/src/Common/StackTrace.cpp index 0aebaf9801c..3c0bcd2f808 100644 --- a/src/Common/StackTrace.cpp +++ b/src/Common/StackTrace.cpp @@ -291,7 +291,7 @@ void StackTrace::tryCapture() #endif } -// Clickhouse uses bundled libc++ so type names will be same on every system thus it's save to hardcode them +/// ClickHouse uses bundled libc++ so type names will be the same on every system thus it's safe to hardcode them constexpr std::pair replacements[] = {{"::__1", ""}, {"std::basic_string, std::allocator>", "String"}}; From 990af0fb7977ab41b291df5c563c062ab26557e6 Mon Sep 17 00:00:00 2001 From: Nikolay Degterinsky <43110995+evillique@users.noreply.github.com> Date: Mon, 6 Mar 2023 23:44:41 +0100 Subject: [PATCH 301/342] Fix typo --- src/Databases/DatabaseAtomic.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Databases/DatabaseAtomic.cpp b/src/Databases/DatabaseAtomic.cpp index 34c4fd3d5d8..eaeeb793b23 100644 --- a/src/Databases/DatabaseAtomic.cpp +++ b/src/Databases/DatabaseAtomic.cpp @@ -273,7 +273,7 @@ void DatabaseAtomic::renameTable(ContextPtr local_context, const String & table_ else renameNoReplace(old_metadata_path, new_metadata_path); - /// After metadata was successfully moved, the following methods should not throw (if them do, it's a logical error) + /// After metadata was successfully moved, the following methods should not throw (if they do, it's a logical error) table_data_path = detach(*this, table_name, table->storesDataOnDisk()); if (exchange) other_table_data_path = detach(other_db, to_table_name, other_table->storesDataOnDisk()); From aacd05e34bcc6abd44eb67613b68551b18fd4d99 Mon Sep 17 00:00:00 2001 From: Yakov Olkhovskiy <99031427+yakov-olkhovskiy@users.noreply.github.com> Date: Mon, 6 Mar 2023 21:24:04 -0500 Subject: [PATCH 302/342] bugfix + review suggestion --- .../self-extracting-executable/decompressor.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/utils/self-extracting-executable/decompressor.cpp b/utils/self-extracting-executable/decompressor.cpp index 8db0d95e70d..1374c269dc1 100644 --- a/utils/self-extracting-executable/decompressor.cpp +++ b/utils/self-extracting-executable/decompressor.cpp @@ -170,7 +170,7 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n bool isSudo() { - return getuid() == 0 && geteuid() == 0 && getenv("SUDO_USER") && getenv("SUDO_UID") && getenv("SUDO_GID"); // NOLINT(concurrency-mt-unsafe) + return geteuid() == 0 && getenv("SUDO_USER") && getenv("SUDO_UID") && getenv("SUDO_GID"); // NOLINT(concurrency-mt-unsafe) } /// Read data about files and decomrpess them. @@ -423,6 +423,13 @@ int main(int/* argc*/, char* argv[]) else name = file_path; + struct stat input_info; + if (0 != stat(self, &input_info)) + { + perror("stat"); + return 1; + } + #if !defined(OS_DARWIN) && !defined(OS_FREEBSD) /// get inode of this executable uint64_t inode = getInode(self); @@ -450,13 +457,6 @@ int main(int/* argc*/, char* argv[]) return 1; } - struct stat input_info; - if (0 != stat(self, &input_info)) - { - perror("stat"); - return 1; - } - /// inconsistency in WSL1 Ubuntu - inode reported in /proc/self/maps is a 64bit to /// 32bit conversion of input_info.st_ino if (input_info.st_ino & 0xFFFFFFFF00000000 && !(inode & 0xFFFFFFFF00000000)) From 946dff2d04e7f7af8d3541ed7de11f195b521b6c Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Tue, 7 Mar 2023 10:51:06 +0100 Subject: [PATCH 303/342] Fix test 02566_ipv4_ipv6_binary_formats --- .../0_stateless/02566_ipv4_ipv6_binary_formats.reference | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference index e228d911715..a3d8a33f757 100644 --- a/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference +++ b/tests/queries/0_stateless/02566_ipv4_ipv6_binary_formats.reference @@ -6,7 +6,7 @@ Arrow 2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 Parquet ipv6 Nullable(FixedString(16)) -ipv4 Nullable(Int64) +ipv4 Nullable(UInt32) 2001:db8:11a3:9d7:1f34:8a2e:7a0:765d 127.0.0.1 ORC ipv6 Nullable(String) From 0d778e693bbeb90d7a8d15c9724684e1b3766913 Mon Sep 17 00:00:00 2001 From: Kseniia Sumarokova <54203879+kssenii@users.noreply.github.com> Date: Tue, 7 Mar 2023 11:31:43 +0100 Subject: [PATCH 304/342] Update docs/en/sql-reference/statements/create/view.md --- docs/en/sql-reference/statements/create/view.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/statements/create/view.md b/docs/en/sql-reference/statements/create/view.md index 6e412c13291..a9e7e34dcbb 100644 --- a/docs/en/sql-reference/statements/create/view.md +++ b/docs/en/sql-reference/statements/create/view.md @@ -67,7 +67,7 @@ Materialized views in ClickHouse use **column names** instead of column order du Materialized views in ClickHouse are implemented more like insert triggers. If there’s some aggregation in the view query, it’s applied only to the batch of freshly inserted data. Any changes to existing data of source table (like update, delete, drop partition, etc.) does not change the materialized view. -Materialized views in ClickHouse does not have deterministic behaviour in case of errors. This means that blocks that had been already written will be preserved in the destination table, but all blocks after error will not. +Materialized views in ClickHouse do not have deterministic behaviour in case of errors. This means that blocks that had been already written will be preserved in the destination table, but all blocks after error will not. By default if pushing to one of views fails, then the INSERT query will fail too, and some blocks may not be written to the destination table. This can be changed using `materialized_views_ignore_errors` setting (you should set it for `INSERT` query), if you will set `materialized_views_ignore_errors=true`, then any errors while pushing to views will be ignored and all blocks will be written to the destination table. From 20925406ba0b1d51b338dd386e28a2fef5633573 Mon Sep 17 00:00:00 2001 From: Sergei Trifonov Date: Tue, 7 Mar 2023 11:48:28 +0100 Subject: [PATCH 305/342] Docs for `BackupsIOThreadPool` 1/2 --- docs/en/development/architecture.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/development/architecture.md b/docs/en/development/architecture.md index 50b338844df..5c867c2c6b9 100644 --- a/docs/en/development/architecture.md +++ b/docs/en/development/architecture.md @@ -172,7 +172,7 @@ Global thread pool is `GlobalThreadPool` singleton class. To allocate thread fro Global pool is universal and all pools described below are implemented on top of it. This can be thought of as a hierarchy of pools. Any specialized pool takes its threads from the global pool using `ThreadPool` class. So the main purpose of any specialized pool is to apply limit on the number of simultaneous jobs and do job scheduling. If there are more jobs scheduled than threads in a pool, `ThreadPool` accumulates jobs in a queue with priorities. Each job has an integer priority. Default priority is zero. All jobs with higher priority values are started before any job with lower priority value. But there is no difference between already executing jobs, thus priority matters only when the pool in overloaded. -IO thread pool is implemented as a plain `ThreadPool` accessible via `IOThreadPool::get()` method. It is configured in the same way as global pool with `max_io_thread_pool_size`, `max_io_thread_pool_free_size` and `io_thread_pool_queue_size` settings. The main purpose of IO thread pool is to avoid exhaustion of the global pool with IO jobs, which could prevent queries from fully utilizing CPU. +IO thread pool is implemented as a plain `ThreadPool` accessible via `IOThreadPool::get()` method. It is configured in the same way as global pool with `max_io_thread_pool_size`, `max_io_thread_pool_free_size` and `io_thread_pool_queue_size` settings. The main purpose of IO thread pool is to avoid exhaustion of the global pool with IO jobs, which could prevent queries from fully utilizing CPU. Backup to S3 does significant amount of IO operations and to avoid impact on interactive queries there is separate `BackupsIOThreadPool` configured with `max_backups_io_thread_pool_size`, `max_backups_io_thread_pool_free_size` and `backups_io_thread_pool_queue_size` settings. For periodic task execution there is `BackgroundSchedulePool` class. You can register tasks using `BackgroundSchedulePool::TaskHolder` objects and the pool ensures that no task runs two jobs at the same time. It also allows you to postpone task execution to a specific instant in the future or temporarily deactivate task. Global `Context` provides a few instances of this class for different purposes. For general purpose tasks `Context::getSchedulePool()` is used. From ace08ad6b26d8f1d5a48667bca270056549e95c4 Mon Sep 17 00:00:00 2001 From: Sergei Trifonov Date: Tue, 7 Mar 2023 11:49:38 +0100 Subject: [PATCH 306/342] Update docs/en/development/architecture.md --- docs/en/development/architecture.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/development/architecture.md b/docs/en/development/architecture.md index 5c867c2c6b9..dd46b294ddd 100644 --- a/docs/en/development/architecture.md +++ b/docs/en/development/architecture.md @@ -172,7 +172,7 @@ Global thread pool is `GlobalThreadPool` singleton class. To allocate thread fro Global pool is universal and all pools described below are implemented on top of it. This can be thought of as a hierarchy of pools. Any specialized pool takes its threads from the global pool using `ThreadPool` class. So the main purpose of any specialized pool is to apply limit on the number of simultaneous jobs and do job scheduling. If there are more jobs scheduled than threads in a pool, `ThreadPool` accumulates jobs in a queue with priorities. Each job has an integer priority. Default priority is zero. All jobs with higher priority values are started before any job with lower priority value. But there is no difference between already executing jobs, thus priority matters only when the pool in overloaded. -IO thread pool is implemented as a plain `ThreadPool` accessible via `IOThreadPool::get()` method. It is configured in the same way as global pool with `max_io_thread_pool_size`, `max_io_thread_pool_free_size` and `io_thread_pool_queue_size` settings. The main purpose of IO thread pool is to avoid exhaustion of the global pool with IO jobs, which could prevent queries from fully utilizing CPU. Backup to S3 does significant amount of IO operations and to avoid impact on interactive queries there is separate `BackupsIOThreadPool` configured with `max_backups_io_thread_pool_size`, `max_backups_io_thread_pool_free_size` and `backups_io_thread_pool_queue_size` settings. +IO thread pool is implemented as a plain `ThreadPool` accessible via `IOThreadPool::get()` method. It is configured in the same way as global pool with `max_io_thread_pool_size`, `max_io_thread_pool_free_size` and `io_thread_pool_queue_size` settings. The main purpose of IO thread pool is to avoid exhaustion of the global pool with IO jobs, which could prevent queries from fully utilizing CPU. Backup to S3 does significant amount of IO operations and to avoid impact on interactive queries there is a separate `BackupsIOThreadPool` configured with `max_backups_io_thread_pool_size`, `max_backups_io_thread_pool_free_size` and `backups_io_thread_pool_queue_size` settings. For periodic task execution there is `BackgroundSchedulePool` class. You can register tasks using `BackgroundSchedulePool::TaskHolder` objects and the pool ensures that no task runs two jobs at the same time. It also allows you to postpone task execution to a specific instant in the future or temporarily deactivate task. Global `Context` provides a few instances of this class for different purposes. For general purpose tasks `Context::getSchedulePool()` is used. From 2ae0bceb2b427e0e1a1d8ae3e6d30107448fad3e Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 7 Mar 2023 10:56:11 +0000 Subject: [PATCH 307/342] Address PR comments --- docs/en/operations/settings/settings.md | 53 +++++++-------------- docs/en/sql-reference/operators/in.md | 6 +-- src/Interpreters/Cluster.cpp | 26 ++++++---- src/Interpreters/InterpreterSelectQuery.cpp | 12 ++--- 4 files changed, 43 insertions(+), 54 deletions(-) diff --git a/docs/en/operations/settings/settings.md b/docs/en/operations/settings/settings.md index 809fdb17d99..94dcf159ca9 100644 --- a/docs/en/operations/settings/settings.md +++ b/docs/en/operations/settings/settings.md @@ -1248,8 +1248,8 @@ Possible values: Default value: 1. :::warning -Disable this setting if you use [max_parallel_replicas](#settings-max_parallel_replicas) with [parallel_replicas_mode](#settings-parallel_replicas_mode) set to `sample_key` or `read_tasks`. -If [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `custom_key`, disable this setting only if it's used on a cluster with multiple shards containing multiple replicas. +Disable this setting if you use [max_parallel_replicas](#settings-max_parallel_replicas) without [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key). +If [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key) is set, disable this setting only if it's used on a cluster with multiple shards containing multiple replicas. If it's used on a cluster with a single shard and multiple replicas, disabling this setting will have negative effects. ::: @@ -1275,11 +1275,14 @@ Default value: `1`. **Additional Info** -This options will produce different results depending on the value of [parallel_replicas_mode](#settings-parallel_replicas_mode). +This options will produce different results depending on the settings used. -### `sample_key` +:::warning +This setting will produce incorrect results when joins or subqueries are involved, and all tables don't meet certain requirements. See [Distributed Subqueries and max_parallel_replicas](../../sql-reference/operators/in.md/#max_parallel_replica-subqueries) for more details. +::: + +### Parallel processing using `SAMPLE` key -If [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `sample_key`, this setting is useful for replicated tables with a sampling key. A query may be processed faster if it is executed on several servers in parallel. But the query performance may degrade in the following cases: - The position of the sampling key in the partitioning key does not allow efficient range scans. @@ -1287,45 +1290,21 @@ A query may be processed faster if it is executed on several servers in parallel - The sampling key is an expression that is expensive to calculate. - The cluster latency distribution has a long tail, so that querying more servers increases the query overall latency. -### `custom_key` +### Parallel processing using [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key) -If [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `custom_key`, this setting is useful for any replicated table. -A query may be processed faster if it is executed on several servers in parallel but it depends on the used [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key) -and [parallel_replicas_custom_key_filter_type](#settings-parallel_replicas_custom_key_filter_type). - -Use `default` for [parallel_replicas_custom_key_filter_type](#settings-parallel_replicas_custom_key_filter_type) unless the data is split across the entire integer space (e.g. column contains hash values), -then `range` should be used. -Simple expressions using primary keys are preferred. - -If the `custom_key` mode is used on a cluster that consists of a single shard with multiple replicas, those replicas will be converted into virtual shards. -Otherwise, it will behave same as `sample_key` mode, it will use multiple replicas of each shard. - -:::warning -This setting will produce incorrect results when joins or subqueries are involved, and all tables don't meet certain requirements. See [Distributed Subqueries and max_parallel_replicas](../../sql-reference/operators/in.md/#max_parallel_replica-subqueries) for more details. -::: - -## parallel_replicas_mode {#settings-parallel_replicas_mode} - -Mode of splitting work between replicas. - -Possible values: - -- `sample_key` — Use `SAMPLE` key defined in the `SAMPLE BY` clause to split the work between replicas. -- `custom_key` — Define an arbitrary integer expression to use for splitting work between replicas. -- `read_tasks` — Split tasks for reading physical parts between replicas. - -Default value: `sample_key`. +This setting is useful for any replicated table. ## parallel_replicas_custom_key {#settings-parallel_replicas_custom_key} -Map of arbitrary integer expression that can be used to split work between replicas for a specific table. -If it's used with `cluster` function, the key can be name of the local table defined inside the `cluster` function. -If it's used with `Distributed` engine, the key can be name of the distributed table, alias or the local table for which the `Distributed` engine is created. +An arbitrary integer expression that can be used to split work between replicas for a specific table. The value can be any integer expression. +A query may be processed faster if it is executed on several servers in parallel but it depends on the used [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key) +and [parallel_replicas_custom_key_filter_type](#settings-parallel_replicas_custom_key_filter_type). -Used only if `parallel_replicas_mode` is set to `custom_key`. +Simple expressions using primary keys are preferred. -Default value: `{}`. +If the setting is used on a cluster that consists of a single shard with multiple replicas, those replicas will be converted into virtual shards. +Otherwise, it will behave same as for `SAMPLE` key, it will use multiple replicas of each shard. ## parallel_replicas_custom_key_filter_type {#settings-parallel_replicas_custom_key_filter_type} diff --git a/docs/en/sql-reference/operators/in.md b/docs/en/sql-reference/operators/in.md index e1e4118524a..0599a50c0a4 100644 --- a/docs/en/sql-reference/operators/in.md +++ b/docs/en/sql-reference/operators/in.md @@ -235,10 +235,10 @@ If `some_predicate` is not selective enough, it will return large amount of data When [max_parallel_replicas](#settings-max_parallel_replicas) is greater than 1, distributed queries are further transformed. -For example, if [parallel_replicas_mode](#settings-parallel_replicas_mode) is set to `sample_key`, the following: +For example, the following: ```sql SELECT CounterID, count() FROM distributed_table_1 WHERE UserID IN (SELECT UserID FROM local_table_2 WHERE CounterID < 100) -SETTINGS max_parallel_replicas=3, parallel_replicas_mode='sample_key' +SETTINGS max_parallel_replicas=3 ``` is transformed on each server into @@ -256,4 +256,4 @@ Therefore adding the [max_parallel_replicas](#settings-max_parallel_replicas) se One workaround if local_table_2 does not meet the requirements, is to use `GLOBAL IN` or `GLOBAL JOIN`. -If a table doesn't have a sampling key, more flexible options for [parallel_replicas_mode](#settings-parallel_replicas_mode) can be used that can produce different and more optimal behaviour. +If a table doesn't have a sampling key, more flexible options for [parallel_replicas_custom_key](#settings-parallel_replicas_custom_key) can be used that can produce different and more optimal behaviour. diff --git a/src/Interpreters/Cluster.cpp b/src/Interpreters/Cluster.cpp index b419dacd523..0add0e427f9 100644 --- a/src/Interpreters/Cluster.cpp +++ b/src/Interpreters/Cluster.cpp @@ -671,7 +671,7 @@ std::unique_ptr Cluster::getClusterWithMultipleShards(const std::vector namespace { -void shuffleReplicas(auto & replicas, const Settings & settings) +void shuffleReplicas(std::vector & replicas, const Settings & settings, size_t replicas_needed) { std::random_device rd; std::mt19937 gen{rd()}; @@ -679,15 +679,25 @@ void shuffleReplicas(auto & replicas, const Settings & settings) if (settings.prefer_localhost_replica) { // force for local replica to always be included - auto local_replica = std::find_if(replicas.begin(), replicas.end(), [](const auto & replica) { return replica.is_local; }); - if (local_replica != replicas.end()) - { - if (local_replica != replicas.begin()) - std::swap(*replicas.begin(), *local_replica); + auto first_non_local_replica = std::partition(replicas.begin(), replicas.end(), [](const auto & replica) { return replica.is_local; }); + size_t local_replicas_count = first_non_local_replica - replicas.begin(); - std::shuffle(replicas.begin() + 1, replicas.end(), gen); + if (local_replicas_count == replicas_needed) + { + /// we have exact amount of local replicas as needed, no need to do anything return; } + + if (local_replicas_count > replicas_needed) + { + /// we can use only local replicas, shuffle them + std::shuffle(replicas.begin(), first_non_local_replica, gen); + return; + } + + /// shuffle just non local replicas + std::shuffle(first_non_local_replica, replicas.end(), gen); + return; } std::shuffle(replicas.begin(), replicas.end(), gen); @@ -751,7 +761,7 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti { auto shuffled_replicas = replicas; // shuffle replicas so we don't always pick the same subset - shuffleReplicas(shuffled_replicas, settings); + shuffleReplicas(shuffled_replicas, settings, max_replicas_from_shard); create_shards_from_replicas(std::span{shuffled_replicas.begin(), max_replicas_from_shard}); } } diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index d016922e89e..33d362556aa 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -523,7 +523,7 @@ InterpreterSelectQuery::InterpreterSelectQuery( { if (auto custom_key_ast = parseCustomKeyForTable(settings.parallel_replicas_custom_key, *context)) { - LOG_INFO(log, "Processing query on a replica using custom_key"); + LOG_TRACE(log, "Processing query on a replica using custom_key '{}'", settings.parallel_replicas_custom_key.value); if (!storage) throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Storage is unknown when trying to parse custom key for parallel replica"); @@ -537,11 +537,11 @@ InterpreterSelectQuery::InterpreterSelectQuery( } else if (settings.parallel_replica_offset > 0) { - LOG_DEBUG( - log, - "Will use no data on this replica because parallel replicas processing with custom_key has been requested" - " (setting 'max_parallel_replicas') but the table does not have custom_key defined for it or it's invalid (settings `parallel_replicas_custom_key`)"); - parallel_replicas_custom_filter_ast = std::make_shared(false); + throw Exception( + ErrorCodes::BAD_ARGUMENTS, + "Parallel replicas processing with custom_key has been requested " + "(setting 'max_parallel_replicas') but the table does not have custom_key defined for it " + "or it's invalid (settings `parallel_replicas_custom_key`)"); } } else if (auto * distributed = dynamic_cast(storage.get()); From 3dcdf4cd4ef72b3a8043e7533c1119efbbbfa090 Mon Sep 17 00:00:00 2001 From: Sergei Trifonov Date: Tue, 7 Mar 2023 12:12:29 +0100 Subject: [PATCH 308/342] Docs for `BackupsIOThreadPool` 2/2 --- .../settings.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/docs/en/operations/server-configuration-parameters/settings.md b/docs/en/operations/server-configuration-parameters/settings.md index 17d03dfa4ec..92a8270ac0f 100644 --- a/docs/en/operations/server-configuration-parameters/settings.md +++ b/docs/en/operations/server-configuration-parameters/settings.md @@ -967,6 +967,7 @@ The maximum number of jobs that can be scheduled on the Global Thread pool. Incr Possible values: - Positive integer. +- 0 — No limit. Default value: `10000`. @@ -976,6 +977,69 @@ Default value: `10000`. 12000 ``` +## max_io_thread_pool_size {#max-io-thread-pool-size} + +ClickHouse uses threads from the IO Thread pool to do some IO operations (e.g. to interact with S3). `max_io_thread_pool_size` limits the maximum number of threads in the pool. + +Possible values: + +- Positive integer. + +Default value: `100`. + +## max_io_thread_pool_free_size {#max-io-thread-pool-free-size} + +If the number of **idle** threads in the IO Thread pool is greater than `max_io_thread_pool_free_size`, then ClickHouse releases resources occupied by some threads and the pool size is decreased. Threads can be created again if necessary. + +Possible values: + +- Positive integer. + +Default value: `0`. + +## io_thread_pool_queue_size {#io-thread-pool-queue-size} + +The maximum number of jobs that can be scheduled on the IO Thread pool. + +Possible values: + +- Positive integer. +- 0 — No limit. + +Default value: `10000`. + +## max_backups_io_thread_pool_size {#max-backups-io-thread-pool-size} + +ClickHouse uses threads from the Backups IO Thread pool to do S3 backup IO operations. `max_backups_io_thread_pool_size` limits the maximum number of threads in the pool. + +Possible values: + +- Positive integer. + +Default value: `1000`. + +## max_backups_io_thread_pool_free_size {#max-backups-io-thread-pool-free-size} + +If the number of **idle** threads in the Backups IO Thread pool is greater than `max_backup_io_thread_pool_free_size`, then ClickHouse releases resources occupied by some threads and the pool size is decreased. Threads can be created again if necessary. + +Possible values: + +- Positive integer. +- Zero. + +Default value: `0`. + +## backups_io_thread_pool_queue_size {#backups-io-thread-pool-queue-size} + +The maximum number of jobs that can be scheduled on the Backups IO Thread pool. It is recommended to keep this queue unlimited due to the current S3 backup logic. + +Possible values: + +- Positive integer. +- 0 — No limit. + +Default value: `0`. + ## background_pool_size {#background_pool_size} Sets the number of threads performing background merges and mutations for tables with MergeTree engines. This setting is also could be applied at server startup from the `default` profile configuration for backward compatibility at the ClickHouse server start. You can only increase the number of threads at runtime. To lower the number of threads you have to restart the server. By adjusting this setting, you manage CPU and disk load. Smaller pool size utilizes less CPU and disk resources, but background processes advance slower which might eventually impact query performance. From f408f750ebc9a4f09224de2e027be2b0f84e2e0a Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 7 Mar 2023 11:24:46 +0000 Subject: [PATCH 309/342] Disable parallel replicas for joins --- src/Interpreters/InterpreterSelectQuery.cpp | 3 ++- .../02535_max_parallel_replicas_custom_key.reference | 1 + .../0_stateless/02535_max_parallel_replicas_custom_key.sh | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 33d362556aa..2f579244b9a 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -455,10 +455,11 @@ InterpreterSelectQuery::InterpreterSelectQuery( } } - if (joined_tables.tablesCount() > 1 && settings.allow_experimental_parallel_reading_from_replicas) + if (joined_tables.tablesCount() > 1 && (!settings.parallel_replicas_custom_key.value.empty() || settings.allow_experimental_parallel_reading_from_replicas)) { LOG_WARNING(log, "Joins are not supported with parallel replicas. Query will be executed without using them."); context->setSetting("allow_experimental_parallel_reading_from_replicas", false); + context->setSetting("parallel_replicas_custom_key", String{""}); } /// Rewrite JOINs diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference index 9e4e24235aa..8d0f56ba185 100644 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.reference @@ -170,3 +170,4 @@ filter_type='range' max_replicas=3 prefer_localhost_replica=1 0 334 1 333 2 333 +1 diff --git a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh index 314251be874..3035a191c8f 100755 --- a/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh +++ b/tests/queries/0_stateless/02535_max_parallel_replicas_custom_key.sh @@ -41,4 +41,6 @@ run_count_with_custom_key "y" run_count_with_custom_key "cityHash64(y)" run_count_with_custom_key "cityHash64(y) + 1" +$CLICKHOUSE_CLIENT --query="SELECT count() FROM cluster(test_cluster_one_shard_three_replicas_localhost, currentDatabase(), 02535_custom_key) as t1 JOIN 02535_custom_key USING y" --parallel_replicas_custom_key="y" --send_logs_level="trace" 2>&1 | grep -Fac "Joins are not supported with parallel replicas" + $CLICKHOUSE_CLIENT --query="DROP TABLE 02535_custom_key" From 1db6b9414e8b71f27bd1040f696d780f020ca1e0 Mon Sep 17 00:00:00 2001 From: alesapin Date: Tue, 7 Mar 2023 12:39:17 +0100 Subject: [PATCH 310/342] Update src/Storages/StorageReplicatedMergeTree.cpp Co-authored-by: Ilya Yatsishin <2159081+qoega@users.noreply.github.com> --- src/Storages/StorageReplicatedMergeTree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index 9e79a715610..61eedc18736 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -8436,7 +8436,7 @@ std::pair StorageReplicatedMergeTree::unlockSharedDataByID( { /// We don't know what to do, because this part can be mutation part /// with hardlinked columns. Since we don't have this information (about blobs not to remove) - /// we refuce to remove blobs. + /// we refuse to remove blobs. LOG_WARNING(logger, "Node with parent zookeeper lock {} for part {} doesn't exist (part was unlocked before), refuse to remove blobs", zookeeper_part_uniq_node, part_name); return std::make_pair{false, {}}; } From c10cb436f41979e92679752d74dc5129637f932c Mon Sep 17 00:00:00 2001 From: alesapin Date: Tue, 7 Mar 2023 12:42:42 +0100 Subject: [PATCH 311/342] Fix build --- src/Storages/StorageReplicatedMergeTree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index 61eedc18736..6c6ff30fd04 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -8438,7 +8438,7 @@ std::pair StorageReplicatedMergeTree::unlockSharedDataByID( /// with hardlinked columns. Since we don't have this information (about blobs not to remove) /// we refuse to remove blobs. LOG_WARNING(logger, "Node with parent zookeeper lock {} for part {} doesn't exist (part was unlocked before), refuse to remove blobs", zookeeper_part_uniq_node, part_name); - return std::make_pair{false, {}}; + return {false, {}}; } else { From ced97cefbb5dcbdb74782543281c71ccba387efc Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Tue, 7 Mar 2023 09:36:41 +0000 Subject: [PATCH 312/342] Address review comments --- .../Passes/LogicalExpressionOptimizer.cpp | 21 +++++++++++++++---- .../Passes/LogicalExpressionOptimizerPass.h | 12 +++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp index 85b44de74da..73585a4cd23 100644 --- a/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp +++ b/src/Analyzer/Passes/LogicalExpressionOptimizer.cpp @@ -102,7 +102,12 @@ private: if (and_operands.size() == 1) { - assert(!function_node.getResultType()->isNullable()); + /// AND operator can have UInt8 or bool as its type. + /// bool is used if a bool constant is at least one operand. + /// Because we reduce the number of operands here by eliminating the same equality checks, + /// the only situation we can end up here is we had AND check where all the equality checks are the same so we know the type is UInt8. + /// Otherwise, we will have > 1 operands and we don't have to do anything. + assert(!function_node.getResultType()->isNullable() && and_operands[0]->getResultType()->equals(*function_node.getResultType())); node = std::move(and_operands[0]); return; } @@ -204,9 +209,17 @@ private: if (or_operands.size() == 1) { - assert(!function_node.getResultType()->isNullable()); - node = std::move(or_operands[0]); - return; + /// if the result type of operand is the same as the result type of OR + /// we can replace OR with the operand + if (or_operands[0]->getResultType()->equals(*function_node.getResultType())) + { + assert(!function_node.getResultType()->isNullable()); + node = std::move(or_operands[0]); + return; + } + + /// otherwise add a stub 0 to make OR correct + or_operands.push_back(std::make_shared(static_cast(0))); } auto or_function_resolver = FunctionFactory::instance().get("or", getContext()); diff --git a/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h index b436c94fe4c..05c10ddc685 100644 --- a/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h +++ b/src/Analyzer/Passes/LogicalExpressionOptimizerPass.h @@ -6,9 +6,9 @@ namespace DB { /** - * This pass tries to do optimizations on logical expression + * This pass tries to do optimizations on logical expression: * - * Replaces chains of equality functions inside an OR with a single IN operator. + * 1. Replaces chains of equality functions inside an OR with a single IN operator. * The replacement is done if: * - one of the operands of the equality function is a constant * - length of chain is at least 'optimize_min_equality_disjunction_chain_length' long OR the expression has type of LowCardinality @@ -26,7 +26,7 @@ namespace DB * WHERE b = 'test' OR a IN (1, 2); * ------------------------------- * - * Removes duplicate OR checks + * 2. Removes duplicate OR checks * ------------------------------- * SELECT * * FROM table @@ -39,7 +39,7 @@ namespace DB * WHERE a = 1 OR b = 'test'; * ------------------------------- * - * Replaces AND chains with a single constant. + * 3. Replaces AND chains with a single constant. * The replacement is done if: * - one of the operands of the equality function is a constant * - constants are different for same expression @@ -55,7 +55,7 @@ namespace DB * WHERE 0; * ------------------------------- * - * Removes duplicate AND checks + * 4. Removes duplicate AND checks * ------------------------------- * SELECT * * FROM table @@ -74,7 +74,7 @@ class LogicalExpressionOptimizerPass final : public IQueryTreePass public: String getName() override { return "LogicalExpressionOptimizer"; } - String getDescription() override { return "Transform all the 'or's with equality check to a single IN function"; } + String getDescription() override { return "Transform equality chain to a single IN function or a constant if possible"; } void run(QueryTreeNodePtr query_tree_node, ContextPtr context) override; }; From 7ce20f5cd2f702df04b2ccbc58117cee9cd974fb Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Tue, 7 Mar 2023 00:53:37 +0100 Subject: [PATCH 313/342] fix tests --- src/Interpreters/InterpreterKillQueryQuery.cpp | 4 ++++ .../Executors/PushingAsyncPipelineExecutor.cpp | 2 +- tests/integration/test_grpc_protocol/test.py | 2 -- .../02232_dist_insert_send_logs_level_hung.sh | 11 ++++++++++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Interpreters/InterpreterKillQueryQuery.cpp b/src/Interpreters/InterpreterKillQueryQuery.cpp index 40698386ccb..3330159aff5 100644 --- a/src/Interpreters/InterpreterKillQueryQuery.cpp +++ b/src/Interpreters/InterpreterKillQueryQuery.cpp @@ -161,6 +161,8 @@ public: if (curr_process.processed) continue; + LOG_DEBUG(&Poco::Logger::get("KillQuery"), "Will kill query {} (synchronously)", curr_process.query_id); + auto code = process_list.sendCancelToQuery(curr_process.query_id, curr_process.user, true); if (code != CancellationCode::QueryIsNotInitializedYet && code != CancellationCode::CancelSent) @@ -226,6 +228,8 @@ BlockIO InterpreterKillQueryQuery::execute() MutableColumns res_columns = header.cloneEmptyColumns(); for (const auto & query_desc : queries_to_stop) { + if (!query.test) + LOG_DEBUG(&Poco::Logger::get("KillQuery"), "Will kill query {} (asynchronously)", query_desc.query_id); auto code = (query.test) ? CancellationCode::Unknown : process_list.sendCancelToQuery(query_desc.query_id, query_desc.user, true); insertResultRow(query_desc.source_num, code, processes_block, header, res_columns); } diff --git a/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp b/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp index 70815bb8b3b..4478f1548a4 100644 --- a/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp +++ b/src/Processors/Executors/PushingAsyncPipelineExecutor.cpp @@ -187,7 +187,7 @@ void PushingAsyncPipelineExecutor::push(Chunk chunk) if (!is_pushed) throw Exception(ErrorCodes::LOGICAL_ERROR, - "Pipeline for PushingPipelineExecutor was finished before all data was inserted"); + "Pipeline for PushingAsyncPipelineExecutor was finished before all data was inserted"); } void PushingAsyncPipelineExecutor::push(Block block) diff --git a/tests/integration/test_grpc_protocol/test.py b/tests/integration/test_grpc_protocol/test.py index a1bc0d42a46..137d585f7d1 100644 --- a/tests/integration/test_grpc_protocol/test.py +++ b/tests/integration/test_grpc_protocol/test.py @@ -594,8 +594,6 @@ def test_cancel_while_processing_input(): stub = clickhouse_grpc_pb2_grpc.ClickHouseStub(main_channel) result = stub.ExecuteQueryWithStreamInput(send_query_info()) assert result.cancelled == True - assert result.progress.written_rows == 6 - assert query("SELECT a FROM t ORDER BY a") == "1\n2\n3\n4\n5\n6\n" def test_cancel_while_generating_output(): diff --git a/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh b/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh index 5ed94148bc1..734cef06214 100755 --- a/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh +++ b/tests/queries/0_stateless/02232_dist_insert_send_logs_level_hung.sh @@ -49,7 +49,16 @@ insert_client_opts=( timeout 250s $CLICKHOUSE_CLIENT "${client_opts[@]}" "${insert_client_opts[@]}" -q "insert into function remote('127.2', currentDatabase(), in_02232) select * from numbers(1e6)" # Kill underlying query of remote() to make KILL faster -timeout 60s $CLICKHOUSE_CLIENT "${client_opts[@]}" -q "KILL QUERY WHERE Settings['log_comment'] = '$CLICKHOUSE_LOG_COMMENT' SYNC" --format Null +# This test is reproducing very interesting bahaviour. +# The block size is 1, so the secondary query creates InterpreterSelectQuery for each row due to pushing to the MV. +# It works extremely slow, and the initial query produces new blocks and writes them to the socket much faster +# then the secondary query can read and process them. Therefore, it fills network buffers in the kernel. +# Once a buffer in the kernel is full, send(...) blocks until the secondary query will finish processing data +# that it already has in ReadBufferFromPocoSocket and call recv. +# Or until the kernel will decide to resize the buffer (seems like it has non-trivial rules for that). +# Anyway, it may look like the initial query got stuck, but actually it did not. +# Moreover, the initial query cannot be killed at that point, so KILL QUERY ... SYNC will get "stuck" as well. +timeout 30s $CLICKHOUSE_CLIENT "${client_opts[@]}" -q "KILL QUERY WHERE query like '%INSERT INTO $CLICKHOUSE_DATABASE.in_02232%' SYNC" --format Null echo $? $CLICKHOUSE_CLIENT "${client_opts[@]}" -nm -q " From 0da310c8dcf51331bc435f7cf49c69e84145286e Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Tue, 7 Mar 2023 15:30:40 +0100 Subject: [PATCH 314/342] Update ProcessList.cpp --- src/Interpreters/ProcessList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interpreters/ProcessList.cpp b/src/Interpreters/ProcessList.cpp index a26844ae73c..b792ea538ae 100644 --- a/src/Interpreters/ProcessList.cpp +++ b/src/Interpreters/ProcessList.cpp @@ -404,7 +404,7 @@ CancellationCode QueryStatus::cancelQuery(bool) std::lock_guard lock(executors_mutex); executors_snapshot = executors; } - + /// We should call cancel() for each executor with unlocked executors_mutex, because /// cancel() can try to lock some internal mutex that is already locked by query executing /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, From 27e4b09f944ae450e394b2886dd557ac64ae141b Mon Sep 17 00:00:00 2001 From: vdimir Date: Tue, 7 Mar 2023 15:13:13 +0000 Subject: [PATCH 315/342] Fix 02570_fallback_from_async_insert --- tests/queries/0_stateless/02570_fallback_from_async_insert.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/02570_fallback_from_async_insert.sh b/tests/queries/0_stateless/02570_fallback_from_async_insert.sh index 9c158d6241b..d7c8944b89d 100755 --- a/tests/queries/0_stateless/02570_fallback_from_async_insert.sh +++ b/tests/queries/0_stateless/02570_fallback_from_async_insert.sh @@ -47,6 +47,7 @@ $CLICKHOUSE_CLIENT --query "SYSTEM FLUSH LOGS" $CLICKHOUSE_CLIENT --query " SELECT 'id_' || splitByChar('_', query_id)[1] AS id FROM system.text_log WHERE query_id LIKE '%$query_id_suffix' AND message LIKE '%$message%' + ORDER BY id " $CLICKHOUSE_CLIENT --query "DROP TABLE IF EXISTS t_async_insert_fallback" From d9611dde0b12d2c7c5b5c633f028d7c625ae4d78 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Tue, 7 Mar 2023 15:23:27 +0000 Subject: [PATCH 316/342] fix test --- .../01705_normalize_create_alter_function_names.reference | 4 ++-- .../01705_normalize_create_alter_function_names.sql | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/queries/0_stateless/01705_normalize_create_alter_function_names.reference b/tests/queries/0_stateless/01705_normalize_create_alter_function_names.reference index b6f5fe99ca1..b5b93c34c00 100644 --- a/tests/queries/0_stateless/01705_normalize_create_alter_function_names.reference +++ b/tests/queries/0_stateless/01705_normalize_create_alter_function_names.reference @@ -1,2 +1,2 @@ -CREATE TABLE default.x\n(\n `i` Int32,\n INDEX mm rand() TYPE minmax GRANULARITY 1,\n INDEX nn rand() TYPE minmax GRANULARITY 1,\n PROJECTION p\n (\n SELECT max(i)\n ),\n PROJECTION p2\n (\n SELECT min(i)\n )\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/tables/default/x\', \'r\')\nORDER BY i\nSETTINGS index_granularity = 8192 -metadata format version: 1\ndate column: \nsampling expression: \nindex granularity: 8192\nmode: 0\nsign column: \nprimary key: i\ndata format version: 1\npartition key: \nindices: mm rand() TYPE minmax GRANULARITY 1, nn rand() TYPE minmax GRANULARITY 1\nprojections: p (SELECT max(i)), p2 (SELECT min(i))\ngranularity bytes: 10485760\n +CREATE TABLE default.x\n(\n `i` Int32,\n INDEX mm log2(i) TYPE minmax GRANULARITY 1,\n INDEX nn log2(i) TYPE minmax GRANULARITY 1,\n PROJECTION p\n (\n SELECT max(i)\n ),\n PROJECTION p2\n (\n SELECT min(i)\n )\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/tables/default/x\', \'r\')\nORDER BY i\nSETTINGS index_granularity = 8192 +metadata format version: 1\ndate column: \nsampling expression: \nindex granularity: 8192\nmode: 0\nsign column: \nprimary key: i\ndata format version: 1\npartition key: \nindices: mm log2(i) TYPE minmax GRANULARITY 1, nn log2(i) TYPE minmax GRANULARITY 1\nprojections: p (SELECT max(i)), p2 (SELECT min(i))\ngranularity bytes: 10485760\n diff --git a/tests/queries/0_stateless/01705_normalize_create_alter_function_names.sql b/tests/queries/0_stateless/01705_normalize_create_alter_function_names.sql index 683bd271405..be0f7e8b710 100644 --- a/tests/queries/0_stateless/01705_normalize_create_alter_function_names.sql +++ b/tests/queries/0_stateless/01705_normalize_create_alter_function_names.sql @@ -2,9 +2,9 @@ drop table if exists x; -create table x(i int, index mm RAND() type minmax granularity 1, projection p (select MAX(i))) engine ReplicatedMergeTree('/clickhouse/tables/{database}/x', 'r') order by i; +create table x(i int, index mm LOG2(i) type minmax granularity 1, projection p (select MAX(i))) engine ReplicatedMergeTree('/clickhouse/tables/{database}/x', 'r') order by i; -alter table x add index nn RAND() type minmax granularity 1, add projection p2 (select MIN(i)); +alter table x add index nn LOG2(i) type minmax granularity 1, add projection p2 (select MIN(i)); show create x; From af23c8bed07f665c0cf074b64bb10d7ad49cbb66 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Mon, 6 Mar 2023 17:46:46 +0100 Subject: [PATCH 317/342] Get the release logs w/o delay from curl --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2ef05fe989b..4b4c7e6c70e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,7 +15,7 @@ jobs: - name: Deploy packages and assets run: | GITHUB_TAG="${GITHUB_REF#refs/tags/}" - curl --silent --data '' \ + curl --silent --data '' --no-buffer \ '${{ secrets.PACKAGES_RELEASE_URL }}/release/'"${GITHUB_TAG}"'?binary=binary_darwin&binary=binary_darwin_aarch64&sync=true' ############################################################################################ ##################################### Docker images ####################################### From c63be68a8fd9a18c881df656d56f17ea899280fc Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Tue, 7 Mar 2023 13:35:00 +0100 Subject: [PATCH 318/342] Use explicit docker tags and paths --- .github/workflows/backport_branches.yml | 3 ++- .github/workflows/master.yml | 3 ++- .github/workflows/pull_request.yml | 3 ++- .github/workflows/release.yml | 3 ++- .github/workflows/release_branches.yml | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/backport_branches.yml b/.github/workflows/backport_branches.yml index a324d20abc9..7cdf11fec0f 100644 --- a/.github/workflows/backport_branches.yml +++ b/.github/workflows/backport_branches.yml @@ -452,7 +452,8 @@ jobs: - name: Check docker clickhouse/clickhouse-server building run: | cd "$GITHUB_WORKSPACE/tests/ci" - python3 docker_server.py --release-type head --no-push + python3 docker_server.py --release-type head --no-push \ + --image-repo clickhouse/clickhouse-server --image-path docker/server python3 docker_server.py --release-type head --no-push --no-ubuntu \ --image-repo clickhouse/clickhouse-keeper --image-path docker/keeper - name: Cleanup diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index b70fe256833..7c5e477ab60 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -860,7 +860,8 @@ jobs: - name: Check docker clickhouse/clickhouse-server building run: | cd "$GITHUB_WORKSPACE/tests/ci" - python3 docker_server.py --release-type head + python3 docker_server.py --release-type head \ + --image-repo clickhouse/clickhouse-server --image-path docker/server python3 docker_server.py --release-type head --no-ubuntu \ --image-repo clickhouse/clickhouse-keeper --image-path docker/keeper - name: Cleanup diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ff98739db00..e7303c7b69f 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -917,7 +917,8 @@ jobs: - name: Check docker clickhouse/clickhouse-server building run: | cd "$GITHUB_WORKSPACE/tests/ci" - python3 docker_server.py --release-type head --no-push + python3 docker_server.py --release-type head --no-push \ + --image-repo clickhouse/clickhouse-server --image-path docker/server python3 docker_server.py --release-type head --no-push --no-ubuntu \ --image-repo clickhouse/clickhouse-keeper --image-path docker/keeper - name: Cleanup diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4b4c7e6c70e..e69d189c80e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -31,7 +31,8 @@ jobs: - name: Check docker clickhouse/clickhouse-server building run: | cd "$GITHUB_WORKSPACE/tests/ci" - python3 docker_server.py --release-type auto --version "${{ github.ref }}" + python3 docker_server.py --release-type auto --version "${{ github.ref }}" \ + --image-repo clickhouse/clickhouse-server --image-path docker/server python3 docker_server.py --release-type auto --version "${{ github.ref }}" --no-ubuntu \ --image-repo clickhouse/clickhouse-keeper --image-path docker/keeper - name: Cleanup diff --git a/.github/workflows/release_branches.yml b/.github/workflows/release_branches.yml index 74ec1163cc9..e56a1fb58fc 100644 --- a/.github/workflows/release_branches.yml +++ b/.github/workflows/release_branches.yml @@ -525,7 +525,8 @@ jobs: - name: Check docker clickhouse/clickhouse-server building run: | cd "$GITHUB_WORKSPACE/tests/ci" - python3 docker_server.py --release-type head --no-push + python3 docker_server.py --release-type head --no-push \ + --image-repo clickhouse/clickhouse-server --image-path docker/server python3 docker_server.py --release-type head --no-push --no-ubuntu \ --image-repo clickhouse/clickhouse-keeper --image-path docker/keeper - name: Cleanup From dedd242f350d9ddfb8a43eac36103f4841b57941 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Tue, 7 Mar 2023 13:42:18 +0100 Subject: [PATCH 319/342] Add a manual trigger for release workflow --- .github/workflows/release.yml | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e69d189c80e..73246af6dfc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,14 +7,27 @@ on: # yamllint disable-line rule:truthy release: types: - published + workflow_dispatch: + inputs: + tag: + description: 'Release tag' + required: true + type: string jobs: ReleasePublish: runs-on: [self-hosted, style-checker] steps: + - name: Set tag from input + if: github.event_name == 'workflow_dispatch' + run: | + echo "GITHUB_TAG=${{ github.event.inputs.tag }}" >> "$GITHUB_ENV" + - name: Set tag from REF + if: github.event_name == 'release' + run: | + echo "GITHUB_TAG=${GITHUB_REF#refs/tags/}" >> "$GITHUB_ENV" - name: Deploy packages and assets run: | - GITHUB_TAG="${GITHUB_REF#refs/tags/}" curl --silent --data '' --no-buffer \ '${{ secrets.PACKAGES_RELEASE_URL }}/release/'"${GITHUB_TAG}"'?binary=binary_darwin&binary=binary_darwin_aarch64&sync=true' ############################################################################################ @@ -23,17 +36,26 @@ jobs: DockerServerImages: runs-on: [self-hosted, style-checker] steps: + - name: Set tag from input + if: github.event_name == 'workflow_dispatch' + run: | + echo "GITHUB_TAG=${{ github.event.inputs.tag }}" >> "$GITHUB_ENV" + - name: Set tag from REF + if: github.event_name == 'release' + run: | + echo "GITHUB_TAG=${GITHUB_REF#refs/tags/}" >> "$GITHUB_ENV" - name: Check out repository code uses: ClickHouse/checkout@v1 with: clear-repository: true fetch-depth: 0 # otherwise we will have no version info + ref: ${{ env.GITHUB_TAG }} - name: Check docker clickhouse/clickhouse-server building run: | cd "$GITHUB_WORKSPACE/tests/ci" - python3 docker_server.py --release-type auto --version "${{ github.ref }}" \ + python3 docker_server.py --release-type auto --version "$GITHUB_TAG" \ --image-repo clickhouse/clickhouse-server --image-path docker/server - python3 docker_server.py --release-type auto --version "${{ github.ref }}" --no-ubuntu \ + python3 docker_server.py --release-type auto --version "$GITHUB_TAG" --no-ubuntu \ --image-repo clickhouse/clickhouse-keeper --image-path docker/keeper - name: Cleanup if: always() From 86410a7a3f8a9241413f8a36d3bda04e003e17da Mon Sep 17 00:00:00 2001 From: vdimir Date: Tue, 7 Mar 2023 16:16:51 +0000 Subject: [PATCH 320/342] Fix MergeTreeTransaction::isReadOnly --- src/Interpreters/MergeTreeTransaction.cpp | 7 +++++++ src/Interpreters/MergeTreeTransaction.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/src/Interpreters/MergeTreeTransaction.cpp b/src/Interpreters/MergeTreeTransaction.cpp index 50ecb061752..bfdda354c9b 100644 --- a/src/Interpreters/MergeTreeTransaction.cpp +++ b/src/Interpreters/MergeTreeTransaction.cpp @@ -168,6 +168,8 @@ void MergeTreeTransaction::addMutation(const StoragePtr & table, const String & bool MergeTreeTransaction::isReadOnly() const { std::lock_guard lock{mutex}; + if (finalized) + return is_read_only; chassert((creating_parts.empty() && removing_parts.empty() && mutations.empty()) == storages.empty()); return storages.empty(); } @@ -318,6 +320,11 @@ bool MergeTreeTransaction::rollback() noexcept void MergeTreeTransaction::afterFinalize() { std::lock_guard lock{mutex}; + chassert((creating_parts.empty() && removing_parts.empty() && mutations.empty()) == storages.empty()); + + /// Remember if it was read-only transaction before we clear storages + is_read_only = storages.empty(); + /// Release shared pointers just in case storages.clear(); mutations.clear(); diff --git a/src/Interpreters/MergeTreeTransaction.h b/src/Interpreters/MergeTreeTransaction.h index e5a80e03e18..4ca36cf64ad 100644 --- a/src/Interpreters/MergeTreeTransaction.h +++ b/src/Interpreters/MergeTreeTransaction.h @@ -78,6 +78,9 @@ private: bool finalized TSA_GUARDED_BY(mutex) = false; + /// Indicates if transaction was read-only before `afterFinalize` + bool is_read_only TSA_GUARDED_BY(mutex) = false; + /// Lists of changes made by transaction std::unordered_set storages TSA_GUARDED_BY(mutex); DataPartsVector creating_parts TSA_GUARDED_BY(mutex); From 4e79df448f151a471d049c6938c06e2e690e9904 Mon Sep 17 00:00:00 2001 From: vdimir Date: Tue, 7 Mar 2023 16:17:35 +0000 Subject: [PATCH 321/342] Catch exceptions in LiveViewPeriodicRefreshTask --- src/Storages/LiveView/StorageLiveView.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Storages/LiveView/StorageLiveView.cpp b/src/Storages/LiveView/StorageLiveView.cpp index 547becf3837..3467628d081 100644 --- a/src/Storages/LiveView/StorageLiveView.cpp +++ b/src/Storages/LiveView/StorageLiveView.cpp @@ -241,7 +241,18 @@ StorageLiveView::StorageLiveView( blocks_metadata_ptr = std::make_shared(); active_ptr = std::make_shared(true); - periodic_refresh_task = getContext()->getSchedulePool().createTask("LiveViewPeriodicRefreshTask", [this]{ periodicRefreshTaskFunc(); }); + periodic_refresh_task = getContext()->getSchedulePool().createTask("LiveViewPeriodicRefreshTask", + [this] + { + try + { + periodicRefreshTaskFunc(); + } + catch (...) + { + tryLogCurrentException(log, "Exception in LiveView periodic refresh task in BackgroundSchedulePool"); + } + }); periodic_refresh_task->deactivate(); } From ba0cc954415275380c93f51c145b6ed479388aa3 Mon Sep 17 00:00:00 2001 From: Kruglov Pavel <48961922+Avogar@users.noreply.github.com> Date: Tue, 7 Mar 2023 20:04:52 +0100 Subject: [PATCH 322/342] Fix typo --- src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp index 7fae2c8f480..39cacde94ed 100644 --- a/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ORCBlockOutputFormat.cpp @@ -23,7 +23,7 @@ #include namespace DB -{7 +{ namespace ErrorCodes { From ff8fc0634ac6c27b74b7182e3307212b8b25f5bd Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Tue, 7 Mar 2023 19:50:42 +0000 Subject: [PATCH 323/342] Docs: Small cleanups after Kafka fix #47138 --- docs/en/engines/table-engines/integrations/kafka.md | 12 ++++++------ docs/en/sql-reference/statements/create/table.md | 8 ++------ src/Storages/ColumnsDescription.h | 4 +--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/docs/en/engines/table-engines/integrations/kafka.md b/docs/en/engines/table-engines/integrations/kafka.md index e6134043b8e..255ba06f056 100644 --- a/docs/en/engines/table-engines/integrations/kafka.md +++ b/docs/en/engines/table-engines/integrations/kafka.md @@ -19,8 +19,8 @@ Kafka lets you: ``` sql CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster] ( - name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1], - name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2], + name1 [type1], + name2 [type2], ... ) ENGINE = Kafka() SETTINGS @@ -113,6 +113,10 @@ Kafka(kafka_broker_list, kafka_topic_list, kafka_group_name, kafka_format +:::info +The Kafka table engine doesn't support columns with [default value](../../../sql-reference/statements/create/table.md#default_value). If you need columns with default value, you can add them at materialized view level (see below). +::: + ## Description {#description} The delivered messages are tracked automatically, so each message in a group is only counted once. If you want to get the data twice, then create a copy of the table with another group name. @@ -125,10 +129,6 @@ Groups are flexible and synced on the cluster. For instance, if you have 10 topi 2. Create a table with the desired structure. 3. Create a materialized view that converts data from the engine and puts it into a previously created table. -:::info -Kafka Engine doesn't support columns with default value of type `DEFAULT/MATERIALIZED/EPHEMERAL/ALIAS`. If you need columns with any default type, they can be added at `MATERIALIZED VIEW` level. -::: - When the `MATERIALIZED VIEW` joins the engine, it starts collecting data in the background. This allows you to continually receive messages from Kafka and convert them to the required format using `SELECT`. One kafka table can have as many materialized views as you like, they do not read data from the kafka table directly, but receive new records (in blocks), this way you can write to several tables with different detail level (with grouping - aggregation and without). diff --git a/docs/en/sql-reference/statements/create/table.md b/docs/en/sql-reference/statements/create/table.md index b3d5060e13c..50e74920e4b 100644 --- a/docs/en/sql-reference/statements/create/table.md +++ b/docs/en/sql-reference/statements/create/table.md @@ -110,7 +110,7 @@ If the type is not `Nullable` and if `NULL` is specified, it will be treated as See also [data_type_default_nullable](../../../operations/settings/settings.md#data_type_default_nullable) setting. -## Default Values +## Default Values {#default_values} The column description can specify an expression for a default value, in one of the following ways: `DEFAULT expr`, `MATERIALIZED expr`, `ALIAS expr`. @@ -124,10 +124,6 @@ If the data type and default expression are defined explicitly, this expression Default expressions may be defined as an arbitrary expression from table constants and columns. When creating and changing the table structure, it checks that expressions do not contain loops. For INSERT, it checks that expressions are resolvable – that all columns they can be calculated from have been passed. -:::info -Kafka Engine doesn't support columns with default value of type `DEFAULT/MATERIALIZED/EPHEMERAL/ALIAS`. If you need columns with any default type, they can be added at `MATERIALIZED VIEW` level, see [Kafka Engine](../../../engines/table-engines/integrations/kafka.md#description). -::: - ### DEFAULT `DEFAULT expr` @@ -580,7 +576,7 @@ SELECT * FROM base.t1; You can add a comment to the table when you creating it. :::note -The comment is supported for all table engines except [Kafka](../../../engines/table-engines/integrations/kafka.md), [RabbitMQ](../../../engines/table-engines/integrations/rabbitmq.md) and [EmbeddedRocksDB](../../../engines/table-engines/integrations/embedded-rocksdb.md). +The comment clause is supported by all table engines except [Kafka](../../../engines/table-engines/integrations/kafka.md), [RabbitMQ](../../../engines/table-engines/integrations/rabbitmq.md) and [EmbeddedRocksDB](../../../engines/table-engines/integrations/embedded-rocksdb.md). ::: diff --git a/src/Storages/ColumnsDescription.h b/src/Storages/ColumnsDescription.h index 36109392ab6..5551fdea2e3 100644 --- a/src/Storages/ColumnsDescription.h +++ b/src/Storages/ColumnsDescription.h @@ -132,9 +132,7 @@ public: NamesAndTypesList getInsertable() const; /// ordinary + ephemeral NamesAndTypesList getAliases() const; NamesAndTypesList getEphemeral() const; - // Columns with preset default expression. - // For example from `CREATE TABLE` statement - NamesAndTypesList getWithDefaultExpression() const; + NamesAndTypesList getWithDefaultExpression() const; // columns with default expression, for example set by `CREATE TABLE` statement NamesAndTypesList getAllPhysical() const; /// ordinary + materialized. NamesAndTypesList getAll() const; /// ordinary + materialized + aliases + ephemeral /// Returns .size0/.null/... From 9792b93a1171f8fc95479c616b236141c70af667 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Thu, 23 Feb 2023 13:48:36 +0100 Subject: [PATCH 324/342] Fix changing a role which is already expired in RoleCache. --- src/Access/RoleCache.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Access/RoleCache.cpp b/src/Access/RoleCache.cpp index 308b771243e..7a8c13636d2 100644 --- a/src/Access/RoleCache.cpp +++ b/src/Access/RoleCache.cpp @@ -163,11 +163,15 @@ void RoleCache::roleChanged(const UUID & role_id, const RolePtr & changed_role) std::lock_guard lock{mutex}; auto role_from_cache = cache.get(role_id); - if (!role_from_cache) - return; - role_from_cache->first = changed_role; - cache.update(role_id, role_from_cache); - collectEnabledRoles(¬ifications); + if (role_from_cache) + { + /// We update the role stored in a cache entry only if that entry has not expired yet. + role_from_cache->first = changed_role; + cache.update(role_id, role_from_cache); + } + + /// An enabled role for some users has been changed, we need to recalculate the access rights. + collectEnabledRoles(¬ifications); /// collectEnabledRoles() must be called with the `mutex` locked. } @@ -177,8 +181,12 @@ void RoleCache::roleRemoved(const UUID & role_id) scope_guard notifications; std::lock_guard lock{mutex}; + + /// If a cache entry with the role has expired already, that remove() will do nothing. cache.remove(role_id); - collectEnabledRoles(¬ifications); + + /// An enabled role for some users has been removed, we need to recalculate the access rights. + collectEnabledRoles(¬ifications); /// collectEnabledRoles() must be called with the `mutex` locked. } } From f8eb1fad91f0ea389e1a6f7b61270b07aa606f76 Mon Sep 17 00:00:00 2001 From: Ilya Yatsishin <2159081+qoega@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:06:14 +0100 Subject: [PATCH 325/342] Disable parallel format in health check --- tests/clickhouse-test | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/clickhouse-test b/tests/clickhouse-test index e30e709f363..c1103da1552 100755 --- a/tests/clickhouse-test +++ b/tests/clickhouse-test @@ -118,6 +118,7 @@ def clickhouse_execute_http( "http_connection_timeout": timeout, "http_receive_timeout": timeout, "http_send_timeout": timeout, + "output_format_parallel_formatting": 0, } if settings is not None: params.update(settings) From 9920a52c51d177bd18ffb9a5788b29c8af9a5ac1 Mon Sep 17 00:00:00 2001 From: Mike Kot Date: Mon, 6 Mar 2023 17:53:58 +0300 Subject: [PATCH 326/342] use std::lerp, constexpr hex.h --- base/base/hex.h | 214 ++++++++++++++++++ base/base/interpolate.h | 13 ++ programs/install/Install.cpp | 2 +- src/Backups/BackupCoordinationRemote.cpp | 2 +- src/Backups/BackupImpl.cpp | 2 +- src/Backups/IBackupCoordination.h | 2 +- src/Columns/tests/gtest_weak_hash_32.cpp | 2 +- src/Common/BinStringDecodeHelper.h | 2 +- src/Common/OpenTelemetryTraceContext.cpp | 2 +- src/Common/SymbolIndex.cpp | 2 +- src/Common/escapeForFileName.cpp | 2 +- src/Common/formatIPv6.cpp | 2 +- src/Common/formatIPv6.h | 2 +- src/Common/getHashOfLoadedBinary.cpp | 2 +- src/Common/getMappedArea.cpp | 2 +- src/Common/hex.cpp | 92 -------- src/Common/hex.h | 145 ------------ src/Common/interpolate.h | 20 -- src/Compression/CompressedReadBufferBase.cpp | 2 +- src/Compression/CompressionCodecMultiple.cpp | 2 +- src/Coordination/KeeperDispatcher.cpp | 2 +- src/Coordination/KeeperStorage.cpp | 2 +- src/Daemon/SentryWriter.cpp | 2 +- .../IO/CachedOnDiskReadBufferFromFile.cpp | 2 +- src/Disks/IO/ReadBufferFromRemoteFSGather.cpp | 2 +- src/Formats/BSONTypes.cpp | 2 +- src/Formats/verbosePrintString.cpp | 2 +- src/Functions/FunctionsCodingIP.cpp | 2 +- src/Functions/FunctionsCodingUUID.cpp | 2 +- src/Functions/URL/decodeURLComponent.cpp | 2 +- src/Functions/bitShiftRight.cpp | 2 +- src/Functions/decodeXMLComponent.cpp | 2 +- src/IO/HTTPChunkedReadBuffer.cpp | 2 +- src/IO/ReadHelpers.cpp | 2 +- src/IO/WriteHelpers.cpp | 2 +- src/IO/tests/gtest_hadoop_snappy_decoder.cpp | 2 +- src/Interpreters/Cache/FileCacheKey.h | 2 +- src/Interpreters/Cache/FileSegment.cpp | 2 +- src/Interpreters/InterpreterCreateQuery.cpp | 2 +- src/Interpreters/OpenTelemetrySpanLog.cpp | 2 +- .../tests/gtest_lru_file_cache.cpp | 2 +- src/Parsers/Access/ParserCreateUserQuery.cpp | 2 +- src/Server/KeeperTCPHandler.cpp | 2 +- .../Cache/ExternalDataSourceCache.cpp | 2 +- .../DistributedAsyncInsertDirectoryQueue.cpp | 2 +- src/Storages/LiveView/LiveViewSink.h | 2 +- src/Storages/LiveView/StorageLiveView.cpp | 2 +- .../MergeTree/MergeTreeDataMergerMutator.cpp | 2 +- .../MergeTree/MergeTreeDataPartChecksum.cpp | 2 +- src/Storages/MergeTree/MergeTreePartition.cpp | 2 +- .../PartMetadataManagerWithCache.cpp | 2 +- .../MergeTree/SimpleMergeSelector.cpp | 10 +- .../MaterializedPostgreSQLConsumer.cpp | 2 +- src/Storages/StorageReplicatedMergeTree.cpp | 2 +- src/Storages/System/StorageSystemParts.cpp | 2 +- .../System/StorageSystemProjectionParts.cpp | 2 +- utils/checksum-for-compressed-block/main.cpp | 2 +- utils/wikistat-loader/main.cpp | 2 +- 58 files changed, 284 insertions(+), 314 deletions(-) create mode 100644 base/base/hex.h create mode 100644 base/base/interpolate.h delete mode 100644 src/Common/hex.cpp delete mode 100644 src/Common/hex.h delete mode 100644 src/Common/interpolate.h diff --git a/base/base/hex.h b/base/base/hex.h new file mode 100644 index 00000000000..e0c57f9dd42 --- /dev/null +++ b/base/base/hex.h @@ -0,0 +1,214 @@ +#pragma once + +#include +#include "types.h" + +/// Maps 0..15 to 0..9A..F or 0..9a..f correspondingly. + +constexpr inline std::string_view hex_digit_to_char_uppercase_table = "0123456789ABCDEF"; +constexpr inline std::string_view hex_digit_to_char_lowercase_table = "0123456789abcdef"; + +constexpr char hexDigitUppercase(unsigned char c) +{ + return hex_digit_to_char_uppercase_table[c]; +} +constexpr char hexDigitLowercase(unsigned char c) +{ + return hex_digit_to_char_lowercase_table[c]; +} + +/// Maps 0..255 to 00..FF or 00..ff correspondingly + +constexpr inline std::string_view hex_byte_to_char_uppercase_table = // + "000102030405060708090A0B0C0D0E0F" + "101112131415161718191A1B1C1D1E1F" + "202122232425262728292A2B2C2D2E2F" + "303132333435363738393A3B3C3D3E3F" + "404142434445464748494A4B4C4D4E4F" + "505152535455565758595A5B5C5D5E5F" + "606162636465666768696A6B6C6D6E6F" + "707172737475767778797A7B7C7D7E7F" + "808182838485868788898A8B8C8D8E8F" + "909192939495969798999A9B9C9D9E9F" + "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF" + "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF" + "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF" + "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF" + "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF" + "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"; + +constexpr inline std::string_view hex_byte_to_char_lowercase_table = // + "000102030405060708090a0b0c0d0e0f" + "101112131415161718191a1b1c1d1e1f" + "202122232425262728292a2b2c2d2e2f" + "303132333435363738393a3b3c3d3e3f" + "404142434445464748494a4b4c4d4e4f" + "505152535455565758595a5b5c5d5e5f" + "606162636465666768696a6b6c6d6e6f" + "707172737475767778797a7b7c7d7e7f" + "808182838485868788898a8b8c8d8e8f" + "909192939495969798999a9b9c9d9e9f" + "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" + "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" + "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" + "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" + "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" + "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; + +inline void writeHexByteUppercase(UInt8 byte, void * out) +{ + memcpy(out, &hex_byte_to_char_uppercase_table[static_cast(byte) * 2], 2); +} + +inline void writeHexByteLowercase(UInt8 byte, void * out) +{ + memcpy(out, &hex_byte_to_char_lowercase_table[static_cast(byte) * 2], 2); +} + +constexpr inline std::string_view bin_byte_to_char_table = // + "0000000000000001000000100000001100000100000001010000011000000111" + "0000100000001001000010100000101100001100000011010000111000001111" + "0001000000010001000100100001001100010100000101010001011000010111" + "0001100000011001000110100001101100011100000111010001111000011111" + "0010000000100001001000100010001100100100001001010010011000100111" + "0010100000101001001010100010101100101100001011010010111000101111" + "0011000000110001001100100011001100110100001101010011011000110111" + "0011100000111001001110100011101100111100001111010011111000111111" + "0100000001000001010000100100001101000100010001010100011001000111" + "0100100001001001010010100100101101001100010011010100111001001111" + "0101000001010001010100100101001101010100010101010101011001010111" + "0101100001011001010110100101101101011100010111010101111001011111" + "0110000001100001011000100110001101100100011001010110011001100111" + "0110100001101001011010100110101101101100011011010110111001101111" + "0111000001110001011100100111001101110100011101010111011001110111" + "0111100001111001011110100111101101111100011111010111111001111111" + "1000000010000001100000101000001110000100100001011000011010000111" + "1000100010001001100010101000101110001100100011011000111010001111" + "1001000010010001100100101001001110010100100101011001011010010111" + "1001100010011001100110101001101110011100100111011001111010011111" + "1010000010100001101000101010001110100100101001011010011010100111" + "1010100010101001101010101010101110101100101011011010111010101111" + "1011000010110001101100101011001110110100101101011011011010110111" + "1011100010111001101110101011101110111100101111011011111010111111" + "1100000011000001110000101100001111000100110001011100011011000111" + "1100100011001001110010101100101111001100110011011100111011001111" + "1101000011010001110100101101001111010100110101011101011011010111" + "1101100011011001110110101101101111011100110111011101111011011111" + "1110000011100001111000101110001111100100111001011110011011100111" + "1110100011101001111010101110101111101100111011011110111011101111" + "1111000011110001111100101111001111110100111101011111011011110111" + "1111100011111001111110101111101111111100111111011111111011111111"; + +inline void writeBinByte(UInt8 byte, void * out) +{ + memcpy(out, &bin_byte_to_char_table[static_cast(byte) * 8], 8); +} + +/// Produces hex representation of an unsigned int with leading zeros (for checksums) +template +inline void writeHexUIntImpl(TUInt uint_, char * out, std::string_view table) +{ + union + { + TUInt value; + UInt8 uint8[sizeof(TUInt)]; + }; + + value = uint_; + + for (size_t i = 0; i < sizeof(TUInt); ++i) + { + if constexpr (std::endian::native == std::endian::little) + memcpy(out + i * 2, &table[static_cast(uint8[sizeof(TUInt) - 1 - i]) * 2], 2); + else + memcpy(out + i * 2, &table[static_cast(uint8[i]) * 2], 2); + } +} + +template +inline void writeHexUIntUppercase(TUInt uint_, char * out) +{ + writeHexUIntImpl(uint_, out, hex_byte_to_char_uppercase_table); +} + +template +inline void writeHexUIntLowercase(TUInt uint_, char * out) +{ + writeHexUIntImpl(uint_, out, hex_byte_to_char_lowercase_table); +} + +template +std::string getHexUIntUppercase(TUInt uint_) +{ + std::string res(sizeof(TUInt) * 2, '\0'); + writeHexUIntUppercase(uint_, res.data()); + return res; +} + +template +std::string getHexUIntLowercase(TUInt uint_) +{ + std::string res(sizeof(TUInt) * 2, '\0'); + writeHexUIntLowercase(uint_, res.data()); + return res; +} + +/// Maps 0..9, A..F, a..f to 0..15. Other chars are mapped to implementation specific value. + +constexpr inline std::string_view hex_char_to_digit_table + = {"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" //0-9 + "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" //A-Z + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" //a-z + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", + 256}; + +constexpr UInt8 unhex(char c) +{ + return hex_char_to_digit_table[static_cast(c)]; +} + +constexpr UInt8 unhex2(const char * data) +{ + return static_cast(unhex(data[0])) * 0x10 + static_cast(unhex(data[1])); +} + +constexpr UInt16 unhex4(const char * data) +{ + return static_cast(unhex(data[0])) * 0x1000 + static_cast(unhex(data[1])) * 0x100 + + static_cast(unhex(data[2])) * 0x10 + static_cast(unhex(data[3])); +} + +template +constexpr TUInt unhexUInt(const char * data) +{ + TUInt res = 0; + if constexpr ((sizeof(TUInt) <= 8) || ((sizeof(TUInt) % 8) != 0)) + { + for (size_t i = 0; i < sizeof(TUInt) * 2; ++i, ++data) + { + res <<= 4; + res += unhex(*data); + } + } + else + { + for (size_t i = 0; i < sizeof(TUInt) / 8; ++i, data += 16) + { + res <<= 64; + res += unhexUInt(data); + } + } + return res; +} diff --git a/base/base/interpolate.h b/base/base/interpolate.h new file mode 100644 index 00000000000..1d4fc0b6257 --- /dev/null +++ b/base/base/interpolate.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include + +/** Linear interpolation in logarithmic coordinates. + * Exponential interpolation is related to linear interpolation + * exactly in same way as geometric mean is related to arithmetic mean. + */ +constexpr double interpolateExponential(double min, double max, double ratio) +{ + assert(min > 0 && ratio >= 0 && ratio <= 1); + return min * std::pow(max / min, ratio); +} diff --git a/programs/install/Install.cpp b/programs/install/Install.cpp index d568012bb26..80f3b0bbc63 100644 --- a/programs/install/Install.cpp +++ b/programs/install/Install.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Backups/BackupCoordinationRemote.cpp b/src/Backups/BackupCoordinationRemote.cpp index c0fb4d5e066..8e43676f59c 100644 --- a/src/Backups/BackupCoordinationRemote.cpp +++ b/src/Backups/BackupCoordinationRemote.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include diff --git a/src/Backups/BackupImpl.cpp b/src/Backups/BackupImpl.cpp index b5f48a1a277..fb8abee814a 100644 --- a/src/Backups/BackupImpl.cpp +++ b/src/Backups/BackupImpl.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Backups/IBackupCoordination.h b/src/Backups/IBackupCoordination.h index b75d856b50f..588a20d9eeb 100644 --- a/src/Backups/IBackupCoordination.h +++ b/src/Backups/IBackupCoordination.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include diff --git a/src/Columns/tests/gtest_weak_hash_32.cpp b/src/Columns/tests/gtest_weak_hash_32.cpp index 5755cc3af72..cbf47790b9f 100644 --- a/src/Columns/tests/gtest_weak_hash_32.cpp +++ b/src/Columns/tests/gtest_weak_hash_32.cpp @@ -14,7 +14,7 @@ #include #include -#include +#include #include #include diff --git a/src/Common/BinStringDecodeHelper.h b/src/Common/BinStringDecodeHelper.h index 513a4196b6f..df3e014cfad 100644 --- a/src/Common/BinStringDecodeHelper.h +++ b/src/Common/BinStringDecodeHelper.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace DB { diff --git a/src/Common/OpenTelemetryTraceContext.cpp b/src/Common/OpenTelemetryTraceContext.cpp index b62822ceda2..df4ee6a34bf 100644 --- a/src/Common/OpenTelemetryTraceContext.cpp +++ b/src/Common/OpenTelemetryTraceContext.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/Common/SymbolIndex.cpp b/src/Common/SymbolIndex.cpp index 6f31009b1d2..f1cace5017c 100644 --- a/src/Common/SymbolIndex.cpp +++ b/src/Common/SymbolIndex.cpp @@ -1,7 +1,7 @@ #if defined(__ELF__) && !defined(OS_FREEBSD) #include -#include +#include #include #include diff --git a/src/Common/escapeForFileName.cpp b/src/Common/escapeForFileName.cpp index bcca04706dc..a1f9bff28d0 100644 --- a/src/Common/escapeForFileName.cpp +++ b/src/Common/escapeForFileName.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/src/Common/formatIPv6.cpp b/src/Common/formatIPv6.cpp index 7c027a23b4d..86e33beb7c3 100644 --- a/src/Common/formatIPv6.cpp +++ b/src/Common/formatIPv6.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/src/Common/formatIPv6.h b/src/Common/formatIPv6.h index 7b88f93750b..be4dfc7391e 100644 --- a/src/Common/formatIPv6.h +++ b/src/Common/formatIPv6.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include constexpr size_t IPV4_BINARY_LENGTH = 4; diff --git a/src/Common/getHashOfLoadedBinary.cpp b/src/Common/getHashOfLoadedBinary.cpp index da053750036..cc0ad0d2143 100644 --- a/src/Common/getHashOfLoadedBinary.cpp +++ b/src/Common/getHashOfLoadedBinary.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include static int callback(dl_phdr_info * info, size_t, void * data) diff --git a/src/Common/getMappedArea.cpp b/src/Common/getMappedArea.cpp index 573d3194f3d..4f40c604c6a 100644 --- a/src/Common/getMappedArea.cpp +++ b/src/Common/getMappedArea.cpp @@ -4,7 +4,7 @@ #if defined(OS_LINUX) #include -#include +#include #include #include diff --git a/src/Common/hex.cpp b/src/Common/hex.cpp deleted file mode 100644 index e8f9b981062..00000000000 --- a/src/Common/hex.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include - -const char * const hex_digit_to_char_uppercase_table = "0123456789ABCDEF"; -const char * const hex_digit_to_char_lowercase_table = "0123456789abcdef"; - -const char * const hex_byte_to_char_uppercase_table = - "000102030405060708090A0B0C0D0E0F" - "101112131415161718191A1B1C1D1E1F" - "202122232425262728292A2B2C2D2E2F" - "303132333435363738393A3B3C3D3E3F" - "404142434445464748494A4B4C4D4E4F" - "505152535455565758595A5B5C5D5E5F" - "606162636465666768696A6B6C6D6E6F" - "707172737475767778797A7B7C7D7E7F" - "808182838485868788898A8B8C8D8E8F" - "909192939495969798999A9B9C9D9E9F" - "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF" - "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF" - "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF" - "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF" - "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF" - "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"; - -const char * const hex_byte_to_char_lowercase_table = - "000102030405060708090a0b0c0d0e0f" - "101112131415161718191a1b1c1d1e1f" - "202122232425262728292a2b2c2d2e2f" - "303132333435363738393a3b3c3d3e3f" - "404142434445464748494a4b4c4d4e4f" - "505152535455565758595a5b5c5d5e5f" - "606162636465666768696a6b6c6d6e6f" - "707172737475767778797a7b7c7d7e7f" - "808182838485868788898a8b8c8d8e8f" - "909192939495969798999a9b9c9d9e9f" - "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" - "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" - "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" - "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" - "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" - "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; - -const char * const hex_char_to_digit_table = - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" //0-9 - "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" //A-Z - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" //a-z - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" - "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; - -const char * const bin_byte_to_char_table = - "0000000000000001000000100000001100000100000001010000011000000111" - "0000100000001001000010100000101100001100000011010000111000001111" - "0001000000010001000100100001001100010100000101010001011000010111" - "0001100000011001000110100001101100011100000111010001111000011111" - "0010000000100001001000100010001100100100001001010010011000100111" - "0010100000101001001010100010101100101100001011010010111000101111" - "0011000000110001001100100011001100110100001101010011011000110111" - "0011100000111001001110100011101100111100001111010011111000111111" - "0100000001000001010000100100001101000100010001010100011001000111" - "0100100001001001010010100100101101001100010011010100111001001111" - "0101000001010001010100100101001101010100010101010101011001010111" - "0101100001011001010110100101101101011100010111010101111001011111" - "0110000001100001011000100110001101100100011001010110011001100111" - "0110100001101001011010100110101101101100011011010110111001101111" - "0111000001110001011100100111001101110100011101010111011001110111" - "0111100001111001011110100111101101111100011111010111111001111111" - "1000000010000001100000101000001110000100100001011000011010000111" - "1000100010001001100010101000101110001100100011011000111010001111" - "1001000010010001100100101001001110010100100101011001011010010111" - "1001100010011001100110101001101110011100100111011001111010011111" - "1010000010100001101000101010001110100100101001011010011010100111" - "1010100010101001101010101010101110101100101011011010111010101111" - "1011000010110001101100101011001110110100101101011011011010110111" - "1011100010111001101110101011101110111100101111011011111010111111" - "1100000011000001110000101100001111000100110001011100011011000111" - "1100100011001001110010101100101111001100110011011100111011001111" - "1101000011010001110100101101001111010100110101011101011011010111" - "1101100011011001110110101101101111011100110111011101111011011111" - "1110000011100001111000101110001111100100111001011110011011100111" - "1110100011101001111010101110101111101100111011011110111011101111" - "1111000011110001111100101111001111110100111101011111011011110111" - "1111100011111001111110101111101111111100111111011111111011111111"; diff --git a/src/Common/hex.h b/src/Common/hex.h deleted file mode 100644 index 062a6c27f76..00000000000 --- a/src/Common/hex.h +++ /dev/null @@ -1,145 +0,0 @@ -#pragma once -#include - - -/// Maps 0..15 to 0..9A..F or 0..9a..f correspondingly. - -extern const char * const hex_digit_to_char_uppercase_table; -extern const char * const hex_digit_to_char_lowercase_table; - -inline char hexDigitUppercase(unsigned char c) -{ - return hex_digit_to_char_uppercase_table[c]; -} - -inline char hexDigitLowercase(unsigned char c) -{ - return hex_digit_to_char_lowercase_table[c]; -} - - -#include -#include - -#include - - -/// Maps 0..255 to 00..FF or 00..ff correspondingly - -extern const char * const hex_byte_to_char_uppercase_table; -extern const char * const hex_byte_to_char_lowercase_table; - -inline void writeHexByteUppercase(UInt8 byte, void * out) -{ - memcpy(out, &hex_byte_to_char_uppercase_table[static_cast(byte) * 2], 2); -} - -inline void writeHexByteLowercase(UInt8 byte, void * out) -{ - memcpy(out, &hex_byte_to_char_lowercase_table[static_cast(byte) * 2], 2); -} - -extern const char * const bin_byte_to_char_table; - -inline void writeBinByte(UInt8 byte, void * out) -{ - memcpy(out, &bin_byte_to_char_table[static_cast(byte) * 8], 8); -} - -/// Produces hex representation of an unsigned int with leading zeros (for checksums) -template -inline void writeHexUIntImpl(TUInt uint_, char * out, const char * const table) -{ - union - { - TUInt value; - UInt8 uint8[sizeof(TUInt)]; - }; - - value = uint_; - - for (size_t i = 0; i < sizeof(TUInt); ++i) - { - if constexpr (std::endian::native == std::endian::little) - memcpy(out + i * 2, &table[static_cast(uint8[sizeof(TUInt) - 1 - i]) * 2], 2); - else - memcpy(out + i * 2, &table[static_cast(uint8[i]) * 2], 2); - } -} - -template -inline void writeHexUIntUppercase(TUInt uint_, char * out) -{ - writeHexUIntImpl(uint_, out, hex_byte_to_char_uppercase_table); -} - -template -inline void writeHexUIntLowercase(TUInt uint_, char * out) -{ - writeHexUIntImpl(uint_, out, hex_byte_to_char_lowercase_table); -} - -template -std::string getHexUIntUppercase(TUInt uint_) -{ - std::string res(sizeof(TUInt) * 2, '\0'); - writeHexUIntUppercase(uint_, res.data()); - return res; -} - -template -std::string getHexUIntLowercase(TUInt uint_) -{ - std::string res(sizeof(TUInt) * 2, '\0'); - writeHexUIntLowercase(uint_, res.data()); - return res; -} - - -/// Maps 0..9, A..F, a..f to 0..15. Other chars are mapped to implementation specific value. - -extern const char * const hex_char_to_digit_table; - -inline UInt8 unhex(char c) -{ - return hex_char_to_digit_table[static_cast(c)]; -} - -inline UInt8 unhex2(const char * data) -{ - return - static_cast(unhex(data[0])) * 0x10 - + static_cast(unhex(data[1])); -} - -inline UInt16 unhex4(const char * data) -{ - return - static_cast(unhex(data[0])) * 0x1000 - + static_cast(unhex(data[1])) * 0x100 - + static_cast(unhex(data[2])) * 0x10 - + static_cast(unhex(data[3])); -} - -template -TUInt unhexUInt(const char * data) -{ - TUInt res = 0; - if constexpr ((sizeof(TUInt) <= 8) || ((sizeof(TUInt) % 8) != 0)) - { - for (size_t i = 0; i < sizeof(TUInt) * 2; ++i, ++data) - { - res <<= 4; - res += unhex(*data); - } - } - else - { - for (size_t i = 0; i < sizeof(TUInt) / 8; ++i, data += 16) - { - res <<= 64; - res += unhexUInt(data); - } - } - return res; -} diff --git a/src/Common/interpolate.h b/src/Common/interpolate.h deleted file mode 100644 index 05900563b80..00000000000 --- a/src/Common/interpolate.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - - -inline double interpolateLinear(double min, double max, double ratio) -{ - return min + (max - min) * ratio; -} - - -/** It is linear interpolation in logarithmic coordinates. - * Exponential interpolation is related to linear interpolation - * exactly in same way as geometric mean is related to arithmetic mean. - * 'min' must be greater than zero, 'ratio' must be from 0 to 1. - */ -inline double interpolateExponential(double min, double max, double ratio) -{ - return min * std::pow(max / min, ratio); -} diff --git a/src/Compression/CompressedReadBufferBase.cpp b/src/Compression/CompressedReadBufferBase.cpp index ab856cc9801..3111f649b26 100644 --- a/src/Compression/CompressedReadBufferBase.cpp +++ b/src/Compression/CompressedReadBufferBase.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Compression/CompressionCodecMultiple.cpp b/src/Compression/CompressionCodecMultiple.cpp index 5203e349317..dba67749e4d 100644 --- a/src/Compression/CompressionCodecMultiple.cpp +++ b/src/Compression/CompressionCodecMultiple.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include namespace DB diff --git a/src/Coordination/KeeperDispatcher.cpp b/src/Coordination/KeeperDispatcher.cpp index a6d16334924..2aa11dd9eed 100644 --- a/src/Coordination/KeeperDispatcher.cpp +++ b/src/Coordination/KeeperDispatcher.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/Coordination/KeeperStorage.cpp b/src/Coordination/KeeperStorage.cpp index 33b2a91d8bf..41a6af54204 100644 --- a/src/Coordination/KeeperStorage.cpp +++ b/src/Coordination/KeeperStorage.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Daemon/SentryWriter.cpp b/src/Daemon/SentryWriter.cpp index 9f4f18e64d1..3c62e54b117 100644 --- a/src/Daemon/SentryWriter.cpp +++ b/src/Daemon/SentryWriter.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include "config.h" #include "config_version.h" diff --git a/src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp b/src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp index 44a719c82bb..72346787cfb 100644 --- a/src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp +++ b/src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/Disks/IO/ReadBufferFromRemoteFSGather.cpp b/src/Disks/IO/ReadBufferFromRemoteFSGather.cpp index 43b6544acb0..8450e740ab5 100644 --- a/src/Disks/IO/ReadBufferFromRemoteFSGather.cpp +++ b/src/Disks/IO/ReadBufferFromRemoteFSGather.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/Formats/BSONTypes.cpp b/src/Formats/BSONTypes.cpp index 813c155325a..88396fd2aba 100644 --- a/src/Formats/BSONTypes.cpp +++ b/src/Formats/BSONTypes.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace DB { diff --git a/src/Formats/verbosePrintString.cpp b/src/Formats/verbosePrintString.cpp index 2f3e09ed75f..5c6111c2929 100644 --- a/src/Formats/verbosePrintString.cpp +++ b/src/Formats/verbosePrintString.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include diff --git a/src/Functions/FunctionsCodingIP.cpp b/src/Functions/FunctionsCodingIP.cpp index a941092b7d6..4784368db9b 100644 --- a/src/Functions/FunctionsCodingIP.cpp +++ b/src/Functions/FunctionsCodingIP.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/Functions/FunctionsCodingUUID.cpp b/src/Functions/FunctionsCodingUUID.cpp index dade406c801..dd9170e44ad 100644 --- a/src/Functions/FunctionsCodingUUID.cpp +++ b/src/Functions/FunctionsCodingUUID.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Functions/URL/decodeURLComponent.cpp b/src/Functions/URL/decodeURLComponent.cpp index 9e516e73e3c..7d98ccd63a0 100644 --- a/src/Functions/URL/decodeURLComponent.cpp +++ b/src/Functions/URL/decodeURLComponent.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/src/Functions/bitShiftRight.cpp b/src/Functions/bitShiftRight.cpp index 108847f13ed..21a0f7584aa 100644 --- a/src/Functions/bitShiftRight.cpp +++ b/src/Functions/bitShiftRight.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace DB { diff --git a/src/Functions/decodeXMLComponent.cpp b/src/Functions/decodeXMLComponent.cpp index 8b84bb1194e..a25e67e0e37 100644 --- a/src/Functions/decodeXMLComponent.cpp +++ b/src/Functions/decodeXMLComponent.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include diff --git a/src/IO/HTTPChunkedReadBuffer.cpp b/src/IO/HTTPChunkedReadBuffer.cpp index b9c42088c41..65ccad4aab7 100644 --- a/src/IO/HTTPChunkedReadBuffer.cpp +++ b/src/IO/HTTPChunkedReadBuffer.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include diff --git a/src/IO/ReadHelpers.cpp b/src/IO/ReadHelpers.cpp index 86a2b9c650e..e14b3ae9129 100644 --- a/src/IO/ReadHelpers.cpp +++ b/src/IO/ReadHelpers.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/src/IO/WriteHelpers.cpp b/src/IO/WriteHelpers.cpp index caeea0a82a3..a0eceddc6f6 100644 --- a/src/IO/WriteHelpers.cpp +++ b/src/IO/WriteHelpers.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include namespace DB diff --git a/src/IO/tests/gtest_hadoop_snappy_decoder.cpp b/src/IO/tests/gtest_hadoop_snappy_decoder.cpp index 4db0deac08e..2847c730735 100644 --- a/src/IO/tests/gtest_hadoop_snappy_decoder.cpp +++ b/src/IO/tests/gtest_hadoop_snappy_decoder.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include using namespace DB; TEST(HadoopSnappyDecoder, repeatNeedMoreInput) { diff --git a/src/Interpreters/Cache/FileCacheKey.h b/src/Interpreters/Cache/FileCacheKey.h index fed4c7f47e0..67e1466e2d4 100644 --- a/src/Interpreters/Cache/FileCacheKey.h +++ b/src/Interpreters/Cache/FileCacheKey.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include namespace DB diff --git a/src/Interpreters/Cache/FileSegment.cpp b/src/Interpreters/Cache/FileSegment.cpp index f4d7b2612a5..bd4554c6532 100644 --- a/src/Interpreters/Cache/FileSegment.cpp +++ b/src/Interpreters/Cache/FileSegment.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index 29b7a4db609..c352280b7ed 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/Interpreters/OpenTelemetrySpanLog.cpp b/src/Interpreters/OpenTelemetrySpanLog.cpp index 57d5c11ad97..63b8ae406a6 100644 --- a/src/Interpreters/OpenTelemetrySpanLog.cpp +++ b/src/Interpreters/OpenTelemetrySpanLog.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/src/Interpreters/tests/gtest_lru_file_cache.cpp b/src/Interpreters/tests/gtest_lru_file_cache.cpp index 93faafb5cea..62aef2441d6 100644 --- a/src/Interpreters/tests/gtest_lru_file_cache.cpp +++ b/src/Interpreters/tests/gtest_lru_file_cache.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Parsers/Access/ParserCreateUserQuery.cpp b/src/Parsers/Access/ParserCreateUserQuery.cpp index de83c5760c1..c1d0691d305 100644 --- a/src/Parsers/Access/ParserCreateUserQuery.cpp +++ b/src/Parsers/Access/ParserCreateUserQuery.cpp @@ -17,7 +17,7 @@ #include #include #include "config.h" -#include +#include #if USE_SSL # include # include diff --git a/src/Server/KeeperTCPHandler.cpp b/src/Server/KeeperTCPHandler.cpp index 0853c6ee62b..f9e11062906 100644 --- a/src/Server/KeeperTCPHandler.cpp +++ b/src/Server/KeeperTCPHandler.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #ifdef POCO_HAVE_FD_EPOLL diff --git a/src/Storages/Cache/ExternalDataSourceCache.cpp b/src/Storages/Cache/ExternalDataSourceCache.cpp index 56b2e661836..1fc68a2d774 100644 --- a/src/Storages/Cache/ExternalDataSourceCache.cpp +++ b/src/Storages/Cache/ExternalDataSourceCache.cpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp index c6f675533c6..b47028b883a 100644 --- a/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp +++ b/src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Storages/LiveView/LiveViewSink.h b/src/Storages/LiveView/LiveViewSink.h index 1d90e35618f..e163400f2af 100644 --- a/src/Storages/LiveView/LiveViewSink.h +++ b/src/Storages/LiveView/LiveViewSink.h @@ -3,7 +3,7 @@ #include #include #include -#include +#include namespace DB diff --git a/src/Storages/LiveView/StorageLiveView.cpp b/src/Storages/LiveView/StorageLiveView.cpp index 547becf3837..b24f5fb2c63 100644 --- a/src/Storages/LiveView/StorageLiveView.cpp +++ b/src/Storages/LiveView/StorageLiveView.cpp @@ -27,7 +27,7 @@ limitations under the License. */ #include #include #include -#include +#include #include #include diff --git a/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp b/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp index 0d123623f05..cf009a10c27 100644 --- a/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp +++ b/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/Storages/MergeTree/MergeTreeDataPartChecksum.cpp b/src/Storages/MergeTree/MergeTreeDataPartChecksum.cpp index 8f4d066baa3..7a0b1d03e79 100644 --- a/src/Storages/MergeTree/MergeTreeDataPartChecksum.cpp +++ b/src/Storages/MergeTree/MergeTreeDataPartChecksum.cpp @@ -1,6 +1,6 @@ #include "MergeTreeDataPartChecksum.h" #include -#include +#include #include #include #include diff --git a/src/Storages/MergeTree/MergeTreePartition.cpp b/src/Storages/MergeTree/MergeTreePartition.cpp index 3a6908ef32d..3b28012e7d6 100644 --- a/src/Storages/MergeTree/MergeTreePartition.cpp +++ b/src/Storages/MergeTree/MergeTreePartition.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include diff --git a/src/Storages/MergeTree/PartMetadataManagerWithCache.cpp b/src/Storages/MergeTree/PartMetadataManagerWithCache.cpp index b6260d5edb6..3a53cf25745 100644 --- a/src/Storages/MergeTree/PartMetadataManagerWithCache.cpp +++ b/src/Storages/MergeTree/PartMetadataManagerWithCache.cpp @@ -1,7 +1,7 @@ #include "PartMetadataManagerWithCache.h" #if USE_ROCKSDB -#include +#include #include #include #include diff --git a/src/Storages/MergeTree/SimpleMergeSelector.cpp b/src/Storages/MergeTree/SimpleMergeSelector.cpp index 15291622a2a..af3373fd175 100644 --- a/src/Storages/MergeTree/SimpleMergeSelector.cpp +++ b/src/Storages/MergeTree/SimpleMergeSelector.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -28,7 +28,7 @@ struct Estimator { double difference = std::abs(log2(static_cast(sum_size) / size_prev_at_left)); if (difference < settings.heuristic_to_align_parts_max_absolute_difference_in_powers_of_two) - current_score *= interpolateLinear(settings.heuristic_to_align_parts_max_score_adjustment, 1, + current_score *= std::lerp(settings.heuristic_to_align_parts_max_score_adjustment, 1, difference / settings.heuristic_to_align_parts_max_absolute_difference_in_powers_of_two); } @@ -115,8 +115,8 @@ bool allow( // std::cerr << "size_normalized: " << size_normalized << "\n"; /// Calculate boundaries for age - double min_age_to_lower_base = interpolateLinear(settings.min_age_to_lower_base_at_min_size, settings.min_age_to_lower_base_at_max_size, size_normalized); - double max_age_to_lower_base = interpolateLinear(settings.max_age_to_lower_base_at_min_size, settings.max_age_to_lower_base_at_max_size, size_normalized); + double min_age_to_lower_base = std::lerp(settings.min_age_to_lower_base_at_min_size, settings.min_age_to_lower_base_at_max_size, size_normalized); + double max_age_to_lower_base = std::lerp(settings.max_age_to_lower_base_at_min_size, settings.max_age_to_lower_base_at_max_size, size_normalized); // std::cerr << "min_age_to_lower_base: " << min_age_to_lower_base << "\n"; // std::cerr << "max_age_to_lower_base: " << max_age_to_lower_base << "\n"; @@ -137,7 +137,7 @@ bool allow( // std::cerr << "combined_ratio: " << combined_ratio << "\n"; - double lowered_base = interpolateLinear(settings.base, 2.0, combined_ratio); + double lowered_base = std::lerp(settings.base, 2.0, combined_ratio); // std::cerr << "------- lowered_base: " << lowered_base << "\n"; diff --git a/src/Storages/PostgreSQL/MaterializedPostgreSQLConsumer.cpp b/src/Storages/PostgreSQL/MaterializedPostgreSQLConsumer.cpp index 9c6eeceb605..d048c94ac75 100644 --- a/src/Storages/PostgreSQL/MaterializedPostgreSQLConsumer.cpp +++ b/src/Storages/PostgreSQL/MaterializedPostgreSQLConsumer.cpp @@ -2,7 +2,7 @@ #include "StorageMaterializedPostgreSQL.h" #include -#include +#include #include #include #include diff --git a/src/Storages/StorageReplicatedMergeTree.cpp b/src/Storages/StorageReplicatedMergeTree.cpp index 54ae8aa5a7b..4c0aced0b48 100644 --- a/src/Storages/StorageReplicatedMergeTree.cpp +++ b/src/Storages/StorageReplicatedMergeTree.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/Storages/System/StorageSystemParts.cpp b/src/Storages/System/StorageSystemParts.cpp index f6854e7d5d0..86ecb336b51 100644 --- a/src/Storages/System/StorageSystemParts.cpp +++ b/src/Storages/System/StorageSystemParts.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/Storages/System/StorageSystemProjectionParts.cpp b/src/Storages/System/StorageSystemProjectionParts.cpp index 37c62ba5eb0..d2c6c3ef287 100644 --- a/src/Storages/System/StorageSystemProjectionParts.cpp +++ b/src/Storages/System/StorageSystemProjectionParts.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include namespace DB { diff --git a/utils/checksum-for-compressed-block/main.cpp b/utils/checksum-for-compressed-block/main.cpp index 27a2154340e..4f9923e7638 100644 --- a/utils/checksum-for-compressed-block/main.cpp +++ b/utils/checksum-for-compressed-block/main.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include /** A tool to easily prove if "Checksum doesn't match: corrupted data" diff --git a/utils/wikistat-loader/main.cpp b/utils/wikistat-loader/main.cpp index 31ade014c74..493f1df05da 100644 --- a/utils/wikistat-loader/main.cpp +++ b/utils/wikistat-loader/main.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include #include From 429696b575a1eaa4d2206a7b0cd7d925060cc6c6 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Tue, 7 Mar 2023 22:57:47 +0000 Subject: [PATCH 327/342] Update reference file --- .../00173_group_by_use_nulls.reference | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/queries/1_stateful/00173_group_by_use_nulls.reference b/tests/queries/1_stateful/00173_group_by_use_nulls.reference index 02723bf14dd..e82b996ad3c 100644 --- a/tests/queries/1_stateful/00173_group_by_use_nulls.reference +++ b/tests/queries/1_stateful/00173_group_by_use_nulls.reference @@ -8,3 +8,25 @@ 59183 1336 33010362 1336 800784 1336 +-- { echoOn } +set allow_experimental_analyzer = 1; +SELECT + CounterID AS k, + quantileBFloat16(0.5)(ResolutionWidth) +FROM remote('127.0.0.{1,2}', test, hits) +GROUP BY k +ORDER BY + count() DESC, + CounterID ASC +LIMIT 10 +SETTINGS group_by_use_nulls = 1; +1704509 1384 +732797 1336 +598875 1384 +792887 1336 +3807842 1336 +25703952 1336 +716829 1384 +59183 1336 +33010362 1336 +800784 1336 From f5cf039190d547f52d718b2a16403ce8eecad8ab Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Tue, 7 Mar 2023 21:27:27 +0100 Subject: [PATCH 328/342] check if a symlink exists more carefully --- src/Common/filesystemHelpers.cpp | 8 ++++++++ src/Common/filesystemHelpers.h | 1 + src/Databases/DatabaseAtomic.cpp | 3 +++ 3 files changed, 12 insertions(+) diff --git a/src/Common/filesystemHelpers.cpp b/src/Common/filesystemHelpers.cpp index 6e1b5573bef..eabc7bdafbb 100644 --- a/src/Common/filesystemHelpers.cpp +++ b/src/Common/filesystemHelpers.cpp @@ -383,6 +383,14 @@ bool isSymlink(const fs::path & path) return fs::is_symlink(path); /// STYLE_CHECK_ALLOW_STD_FS_SYMLINK } +bool isSymlinkNoThrow(const fs::path & path) +{ + std::error_code dummy; + if (path.filename().empty()) + return fs::is_symlink(path.parent_path(), dummy); /// STYLE_CHECK_ALLOW_STD_FS_SYMLINK + return fs::is_symlink(path, dummy); /// STYLE_CHECK_ALLOW_STD_FS_SYMLINK +} + fs::path readSymlink(const fs::path & path) { /// See the comment for isSymlink diff --git a/src/Common/filesystemHelpers.h b/src/Common/filesystemHelpers.h index 14ee5f54322..8591cd6cf92 100644 --- a/src/Common/filesystemHelpers.h +++ b/src/Common/filesystemHelpers.h @@ -95,6 +95,7 @@ void setModificationTime(const std::string & path, time_t time); time_t getChangeTime(const std::string & path); bool isSymlink(const fs::path & path); +bool isSymlinkNoThrow(const fs::path & path); fs::path readSymlink(const fs::path & path); } diff --git a/src/Databases/DatabaseAtomic.cpp b/src/Databases/DatabaseAtomic.cpp index 34c4fd3d5d8..d8e746f01f9 100644 --- a/src/Databases/DatabaseAtomic.cpp +++ b/src/Databases/DatabaseAtomic.cpp @@ -509,6 +509,9 @@ void DatabaseAtomic::tryCreateMetadataSymlink() { try { + /// fs::exists could return false for broken symlink + if (FS::isSymlinkNoThrow(metadata_symlink)) + fs::remove(metadata_symlink); fs::create_directory_symlink(metadata_path, path_to_metadata_symlink); } catch (...) From 51edcf232cf2962d51d57f808ab6cea42e28327d Mon Sep 17 00:00:00 2001 From: Nikolay Degterinsky Date: Wed, 8 Mar 2023 00:52:58 +0000 Subject: [PATCH 329/342] Add support for big int types to runningDifference() function --- src/Functions/runningDifference.h | 10 +++++++- .../queries/0_stateless/00534_filimonov.data | 8 +++++++ .../00653_running_difference.reference | 24 +++++++++++++++++++ .../0_stateless/00653_running_difference.sql | 8 +++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Functions/runningDifference.h b/src/Functions/runningDifference.h index 154370d4cd9..f1ec4f9e523 100644 --- a/src/Functions/runningDifference.h +++ b/src/Functions/runningDifference.h @@ -70,7 +70,7 @@ private: if (!has_prev_value) { - dst[i] = is_first_line_zero ? 0 : src[i]; + dst[i] = is_first_line_zero ? static_cast(0) : static_cast(src[i]); prev = src[i]; has_prev_value = true; } @@ -102,6 +102,10 @@ private: f(UInt32()); else if (which.isUInt64()) f(UInt64()); + else if (which.isUInt128()) + f(UInt128()); + else if (which.isUInt256()) + f(UInt256()); else if (which.isInt8()) f(Int8()); else if (which.isInt16()) @@ -110,6 +114,10 @@ private: f(Int32()); else if (which.isInt64()) f(Int64()); + else if (which.isInt128()) + f(Int128()); + else if (which.isInt256()) + f(Int256()); else if (which.isFloat32()) f(Float32()); else if (which.isFloat64()) diff --git a/tests/queries/0_stateless/00534_filimonov.data b/tests/queries/0_stateless/00534_filimonov.data index 911a8e4d1f3..eb4500877e5 100644 --- a/tests/queries/0_stateless/00534_filimonov.data +++ b/tests/queries/0_stateless/00534_filimonov.data @@ -276,10 +276,14 @@ SELECT runningDifference(CAST( 0 AS Nullable(Int8))); SELECT runningDifference(CAST( 0 AS Nullable(Int16))); SELECT runningDifference(CAST( 0 AS Nullable(Int32))); SELECT runningDifference(CAST( 0 AS Nullable(Int64))); +SELECT runningDifference(CAST( 0 AS Nullable(Int128))); +SELECT runningDifference(CAST( 0 AS Nullable(Int256))); SELECT runningDifference(CAST( 0 AS Nullable(UInt8))); SELECT runningDifference(CAST( 0 AS Nullable(UInt16))); SELECT runningDifference(CAST( 0 AS Nullable(UInt32))); SELECT runningDifference(CAST( 0 AS Nullable(UInt64))); +SELECT runningDifference(CAST( 0 AS Nullable(UInt128))); +SELECT runningDifference(CAST( 0 AS Nullable(UInt256))); SELECT runningDifference(CAST( 0 AS Nullable(Float32))); SELECT runningDifference(CAST( 0 AS Nullable(Float64))); SELECT runningDifference(CAST( 0 AS Nullable(Date))); @@ -288,10 +292,14 @@ SELECT runningDifference(CAST(NULL AS Nullable(Int8))); SELECT runningDifference(CAST(NULL AS Nullable(Int16))); SELECT runningDifference(CAST(NULL AS Nullable(Int32))); SELECT runningDifference(CAST(NULL AS Nullable(Int64))); +SELECT runningDifference(CAST(NULL AS Nullable(Int128))); +SELECT runningDifference(CAST(NULL AS Nullable(Int256))); SELECT runningDifference(CAST(NULL AS Nullable(UInt8))); SELECT runningDifference(CAST(NULL AS Nullable(UInt16))); SELECT runningDifference(CAST(NULL AS Nullable(UInt32))); SELECT runningDifference(CAST(NULL AS Nullable(UInt64))); +SELECT runningDifference(CAST(NULL AS Nullable(UInt128))); +SELECT runningDifference(CAST(NULL AS Nullable(UInt256)); SELECT runningDifference(CAST(NULL AS Nullable(Float32))); SELECT runningDifference(CAST(NULL AS Nullable(Float64))); SELECT runningDifference(CAST(NULL AS Nullable(Date))); diff --git a/tests/queries/0_stateless/00653_running_difference.reference b/tests/queries/0_stateless/00653_running_difference.reference index e2833e0bb3e..624ce92ce0f 100644 --- a/tests/queries/0_stateless/00653_running_difference.reference +++ b/tests/queries/0_stateless/00653_running_difference.reference @@ -19,6 +19,30 @@ \N \N 2 +- +0 +1 +4 +5 +170141183460469231731687303715884105717 +- +0 +1 +4 +5 +170141183460469231731687303715884105718 +- +0 +1 +4 +5 +170141183460469231731687303715884105717 +- +0 +1 +4 +5 +170141183460469231731687303715884105718 --Date Difference-- \N \N diff --git a/tests/queries/0_stateless/00653_running_difference.sql b/tests/queries/0_stateless/00653_running_difference.sql index f2b4a7300b2..1f18cfc42a7 100644 --- a/tests/queries/0_stateless/00653_running_difference.sql +++ b/tests/queries/0_stateless/00653_running_difference.sql @@ -5,6 +5,14 @@ select '-'; select runningDifference(x) from (select arrayJoin([Null, 1]) as x); select '-'; select runningDifference(x) from (select arrayJoin([Null, Null, 1, 3, Null, Null, 5]) as x); +select '-'; +select runningDifference(x) from (select arrayJoin([0, 1, 5, 10, 170141183460469231731687303715884105727]::Array(UInt128)) as x); +select '-'; +select runningDifference(x) from (select arrayJoin([0, 1, 5, 10, 170141183460469231731687303715884105728]::Array(UInt256)) as x); +select '-'; +select runningDifference(x) from (select arrayJoin([0, 1, 5, 10, 170141183460469231731687303715884105727]::Array(Int128)) as x); +select '-'; +select runningDifference(x) from (select arrayJoin([0, 1, 5, 10, 170141183460469231731687303715884105728]::Array(Int256)) as x); select '--Date Difference--'; select runningDifference(x) from (select arrayJoin([Null, Null, toDate('1970-1-1'), toDate('1970-12-31'), Null, Null, toDate('2010-8-9')]) as x); select '-'; From 927efc0f2c0e164a2884c0d2ec087fa7dfbae7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5B=EB=8D=B0=EC=9D=B4=ED=84=B0=ED=94=8C=EB=9E=AB=ED=8F=BC?= =?UTF-8?q?=ED=8C=80=5D=20=EC=9D=B4=ED=98=B8=EC=84=A0?= Date: Wed, 8 Mar 2023 14:01:38 +0900 Subject: [PATCH 330/342] Fix git clone command. Insert space in front of shallow option. --- docs/en/development/developer-instruction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/development/developer-instruction.md b/docs/en/development/developer-instruction.md index b46cc10f99d..ace5ab79bb4 100644 --- a/docs/en/development/developer-instruction.md +++ b/docs/en/development/developer-instruction.md @@ -67,7 +67,7 @@ It generally means that the SSH keys for connecting to GitHub are missing. These You can also clone the repository via https protocol: - git clone --recursive--shallow-submodules https://github.com/ClickHouse/ClickHouse.git + git clone --recursive --shallow-submodules https://github.com/ClickHouse/ClickHouse.git This, however, will not let you send your changes to the server. You can still use it temporarily and add the SSH keys later replacing the remote address of the repository with `git remote` command. From ce809e61fb18eb6b05105ffe2fef03124996ba75 Mon Sep 17 00:00:00 2001 From: Hosun Lee Date: Wed, 8 Mar 2023 14:09:18 +0900 Subject: [PATCH 331/342] Revert commit. --- docs/en/development/developer-instruction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/development/developer-instruction.md b/docs/en/development/developer-instruction.md index ace5ab79bb4..94834f41999 100644 --- a/docs/en/development/developer-instruction.md +++ b/docs/en/development/developer-instruction.md @@ -39,7 +39,7 @@ Next, you need to download the source files onto your working machine. This is c In the command line terminal run: - git clone --recursive --shallow-submodules git@github.com:your_github_username/ClickHouse.git + git clone --recursive--shallow-submodules git@github.com:your_github_username/ClickHouse.git cd ClickHouse Note: please, substitute *your_github_username* with what is appropriate! From aae82a73aa7ae838cdbecda5f1a305186de2fd47 Mon Sep 17 00:00:00 2001 From: Hosun Lee Date: Wed, 8 Mar 2023 14:10:12 +0900 Subject: [PATCH 332/342] Fix git clone command. Insert space in front of shallow option. --- docs/en/development/developer-instruction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/development/developer-instruction.md b/docs/en/development/developer-instruction.md index 94834f41999..ace5ab79bb4 100644 --- a/docs/en/development/developer-instruction.md +++ b/docs/en/development/developer-instruction.md @@ -39,7 +39,7 @@ Next, you need to download the source files onto your working machine. This is c In the command line terminal run: - git clone --recursive--shallow-submodules git@github.com:your_github_username/ClickHouse.git + git clone --recursive --shallow-submodules git@github.com:your_github_username/ClickHouse.git cd ClickHouse Note: please, substitute *your_github_username* with what is appropriate! From 31b7e6edc64689d1d682506530a426c8f06ae1b6 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Tue, 7 Mar 2023 21:21:59 +0100 Subject: [PATCH 333/342] Keep subscriptions for all enabled roles. --- src/Access/EnabledRoles.h | 5 +-- src/Access/RoleCache.cpp | 71 +++++++++++++++++++++++++-------------- src/Access/RoleCache.h | 24 ++++++++++--- 3 files changed, 67 insertions(+), 33 deletions(-) diff --git a/src/Access/EnabledRoles.h b/src/Access/EnabledRoles.h index e0d773db343..5de76abe409 100644 --- a/src/Access/EnabledRoles.h +++ b/src/Access/EnabledRoles.h @@ -44,10 +44,11 @@ private: friend class RoleCache; explicit EnabledRoles(const Params & params_); - void setRolesInfo(const std::shared_ptr & info_, scope_guard * notifications); - const Params params; + /// Called by RoleCache to store `EnabledRolesInfo` in this `EnabledRoles` after the calculation is done. + void setRolesInfo(const std::shared_ptr & info_, scope_guard * notifications); + std::shared_ptr info; mutable std::mutex info_mutex; diff --git a/src/Access/RoleCache.cpp b/src/Access/RoleCache.cpp index 7a8c13636d2..bfc6200929d 100644 --- a/src/Access/RoleCache.cpp +++ b/src/Access/RoleCache.cpp @@ -57,7 +57,9 @@ namespace RoleCache::RoleCache(const AccessControl & access_control_) - : access_control(access_control_), cache(600000 /* 10 minutes */) {} + : access_control(access_control_), cache(600000 /* 10 minutes */) +{ +} RoleCache::~RoleCache() = default; @@ -70,18 +72,18 @@ RoleCache::getEnabledRoles(const std::vector & roles, const std::vectorsecond.lock(); - if (from_cache) - return from_cache; - enabled_roles.erase(it); + if (auto enabled_roles = it->second.enabled_roles.lock()) + return enabled_roles; + enabled_roles_by_params.erase(it); } auto res = std::shared_ptr(new EnabledRoles(params)); - collectEnabledRoles(*res, nullptr); - enabled_roles.emplace(std::move(params), res); + SubscriptionsOnRoles subscriptions_on_roles; + collectEnabledRoles(*res, subscriptions_on_roles, nullptr); + enabled_roles_by_params.emplace(std::move(params), EnabledRolesWithSubscriptions{res, std::move(subscriptions_on_roles)}); return res; } @@ -90,21 +92,23 @@ void RoleCache::collectEnabledRoles(scope_guard * notifications) { /// `mutex` is already locked. - for (auto i = enabled_roles.begin(), e = enabled_roles.end(); i != e;) + for (auto i = enabled_roles_by_params.begin(), e = enabled_roles_by_params.end(); i != e;) { - auto elem = i->second.lock(); - if (!elem) - i = enabled_roles.erase(i); + auto & item = i->second; + if (auto enabled_roles = item.enabled_roles.lock()) + { + collectEnabledRoles(*enabled_roles, item.subscriptions_on_roles, notifications); + ++i; + } else { - collectEnabledRoles(*elem, notifications); - ++i; + i = enabled_roles_by_params.erase(i); } } } -void RoleCache::collectEnabledRoles(EnabledRoles & enabled, scope_guard * notifications) +void RoleCache::collectEnabledRoles(EnabledRoles & enabled_roles, SubscriptionsOnRoles & subscriptions_on_roles, scope_guard * notifications) { /// `mutex` is already locked. @@ -112,43 +116,57 @@ void RoleCache::collectEnabledRoles(EnabledRoles & enabled, scope_guard * notifi auto new_info = std::make_shared(); boost::container::flat_set skip_ids; - auto get_role_function = [this](const UUID & id) { return getRole(id); }; + /// We need to collect and keep not only enabled roles but also subscriptions for them to be able to recalculate EnabledRolesInfo when some of the roles change. + SubscriptionsOnRoles new_subscriptions_on_roles; + new_subscriptions_on_roles.reserve(subscriptions_on_roles.size()); - for (const auto & current_role : enabled.params.current_roles) + auto get_role_function = [this, &subscriptions_on_roles](const UUID & id) TSA_NO_THREAD_SAFETY_ANALYSIS { return getRole(id, subscriptions_on_roles); }; + + for (const auto & current_role : enabled_roles.params.current_roles) collectRoles(*new_info, skip_ids, get_role_function, current_role, true, false); - for (const auto & current_role : enabled.params.current_roles_with_admin_option) + for (const auto & current_role : enabled_roles.params.current_roles_with_admin_option) collectRoles(*new_info, skip_ids, get_role_function, current_role, true, true); + /// Remove duplicates from `subscriptions_on_roles`. + std::sort(new_subscriptions_on_roles.begin(), new_subscriptions_on_roles.end()); + new_subscriptions_on_roles.erase(std::unique(new_subscriptions_on_roles.begin(), new_subscriptions_on_roles.end()), new_subscriptions_on_roles.end()); + subscriptions_on_roles = std::move(new_subscriptions_on_roles); + /// Collect data from the collected roles. - enabled.setRolesInfo(new_info, notifications); + enabled_roles.setRolesInfo(new_info, notifications); } -RolePtr RoleCache::getRole(const UUID & role_id) +RolePtr RoleCache::getRole(const UUID & role_id, SubscriptionsOnRoles & subscriptions_on_roles) { /// `mutex` is already locked. auto role_from_cache = cache.get(role_id); if (role_from_cache) + { + subscriptions_on_roles.emplace_back(role_from_cache->second); return role_from_cache->first; + } - auto subscription = access_control.subscribeForChanges(role_id, - [this, role_id](const UUID &, const AccessEntityPtr & entity) + auto on_role_changed_or_removed = [this, role_id](const UUID &, const AccessEntityPtr & entity) { auto changed_role = entity ? typeid_cast(entity) : nullptr; if (changed_role) roleChanged(role_id, changed_role); else roleRemoved(role_id); - }); + }; + + auto subscription_on_role = std::make_shared(access_control.subscribeForChanges(role_id, on_role_changed_or_removed)); auto role = access_control.tryRead(role_id); if (role) { - auto cache_value = Poco::SharedPtr>( - new std::pair{role, std::move(subscription)}); + auto cache_value = Poco::SharedPtr>>( + new std::pair>{role, subscription_on_role}); cache.add(role_id, cache_value); + subscriptions_on_roles.emplace_back(subscription_on_role); return role; } @@ -162,6 +180,7 @@ void RoleCache::roleChanged(const UUID & role_id, const RolePtr & changed_role) scope_guard notifications; std::lock_guard lock{mutex}; + auto role_from_cache = cache.get(role_id); if (role_from_cache) { diff --git a/src/Access/RoleCache.h b/src/Access/RoleCache.h index 51c415d4d1d..24f19cb9d94 100644 --- a/src/Access/RoleCache.h +++ b/src/Access/RoleCache.h @@ -24,15 +24,29 @@ public: const std::vector & current_roles_with_admin_option); private: - void collectEnabledRoles(scope_guard * notifications); - void collectEnabledRoles(EnabledRoles & enabled, scope_guard * notifications); - RolePtr getRole(const UUID & role_id); + using SubscriptionsOnRoles = std::vector>; + + void collectEnabledRoles(scope_guard * notifications) TSA_REQUIRES(mutex); + void collectEnabledRoles(EnabledRoles & enabled_roles, SubscriptionsOnRoles & subscriptions_on_roles, scope_guard * notifications) TSA_REQUIRES(mutex); + RolePtr getRole(const UUID & role_id, SubscriptionsOnRoles & subscriptions_on_roles) TSA_REQUIRES(mutex); void roleChanged(const UUID & role_id, const RolePtr & changed_role); void roleRemoved(const UUID & role_id); const AccessControl & access_control; - Poco::AccessExpireCache> cache; - std::map> enabled_roles; + + Poco::AccessExpireCache>> TSA_GUARDED_BY(mutex) cache; + + struct EnabledRolesWithSubscriptions + { + std::weak_ptr enabled_roles; + + /// We need to keep subscriptions for all enabled roles to be able to recalculate EnabledRolesInfo when some of the roles change. + /// `cache` also keeps subscriptions but that's not enough because values can be purged from the `cache` anytime. + SubscriptionsOnRoles subscriptions_on_roles; + }; + + std::map TSA_GUARDED_BY(mutex) enabled_roles_by_params; + mutable std::mutex mutex; }; From b2b8b7f872591714e10a702c64fd58aebd0e6457 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Fri, 3 Mar 2023 19:05:38 +0100 Subject: [PATCH 334/342] Fix RewriteArrayExistsToHasPass --- src/Analyzer/Passes/ArrayExistsToHasPass.cpp | 130 ++++++++++-------- src/Analyzer/Passes/ArrayExistsToHasPass.h | 12 +- src/Analyzer/Passes/AutoFinalOnQueryPass.cpp | 3 +- .../Passes/ShardNumColumnToFunctionPass.h | 3 + 4 files changed, 87 insertions(+), 61 deletions(-) diff --git a/src/Analyzer/Passes/ArrayExistsToHasPass.cpp b/src/Analyzer/Passes/ArrayExistsToHasPass.cpp index b4b8b5b4579..c0f958588f1 100644 --- a/src/Analyzer/Passes/ArrayExistsToHasPass.cpp +++ b/src/Analyzer/Passes/ArrayExistsToHasPass.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -8,71 +10,85 @@ #include #include -#include "ArrayExistsToHasPass.h" - namespace DB { + namespace { - class RewriteArrayExistsToHasVisitor : public InDepthQueryTreeVisitorWithContext + +class RewriteArrayExistsToHasVisitor : public InDepthQueryTreeVisitorWithContext +{ +public: + using Base = InDepthQueryTreeVisitorWithContext; + using Base::Base; + + void visitImpl(QueryTreeNodePtr & node) { - public: - using Base = InDepthQueryTreeVisitorWithContext; - using Base::Base; + if (!getSettings().optimize_rewrite_array_exists_to_has) + return; - void visitImpl(QueryTreeNodePtr & node) + auto * array_exists_function_node = node->as(); + if (!array_exists_function_node || array_exists_function_node->getFunctionName() != "arrayExists") + return; + + auto & array_exists_function_arguments_nodes = array_exists_function_node->getArguments().getNodes(); + if (array_exists_function_arguments_nodes.size() != 2) + return; + + /// lambda function must be like: x -> x = elem + auto * lambda_node = array_exists_function_arguments_nodes[0]->as(); + if (!lambda_node) + return; + + auto & lambda_arguments_nodes = lambda_node->getArguments().getNodes(); + if (lambda_arguments_nodes.size() != 1) + return; + + const auto & lambda_argument_column_node = lambda_arguments_nodes[0]; + if (lambda_argument_column_node->getNodeType() != QueryTreeNodeType::COLUMN) + return; + + auto * filter_node = lambda_node->getExpression()->as(); + if (!filter_node || filter_node->getFunctionName() != "equals") + return; + + const auto & filter_arguments_nodes = filter_node->getArguments().getNodes(); + if (filter_arguments_nodes.size() != 2) + return; + + const auto & filter_lhs_argument_node = filter_arguments_nodes[0]; + auto filter_lhs_argument_node_type = filter_lhs_argument_node->getNodeType(); + + const auto & filter_rhs_argument_node = filter_arguments_nodes[1]; + auto filter_rhs_argument_node_type = filter_rhs_argument_node->getNodeType(); + + QueryTreeNodePtr has_constant_element_argument; + + if (filter_lhs_argument_node_type == QueryTreeNodeType::COLUMN && + filter_rhs_argument_node_type == QueryTreeNodeType::CONSTANT && + filter_lhs_argument_node->isEqual(*lambda_argument_column_node)) { - if (!getSettings().optimize_rewrite_array_exists_to_has) - return; - - auto * function_node = node->as(); - if (!function_node || function_node->getFunctionName() != "arrayExists") - return; - - auto & function_arguments_nodes = function_node->getArguments().getNodes(); - if (function_arguments_nodes.size() != 2) - return; - - /// lambda function must be like: x -> x = elem - auto * lambda_node = function_arguments_nodes[0]->as(); - if (!lambda_node) - return; - - auto & lambda_arguments_nodes = lambda_node->getArguments().getNodes(); - if (lambda_arguments_nodes.size() != 1) - return; - auto * column_node = lambda_arguments_nodes[0]->as(); - - auto * filter_node = lambda_node->getExpression()->as(); - if (!filter_node || filter_node->getFunctionName() != "equals") - return; - - auto filter_arguments_nodes = filter_node->getArguments().getNodes(); - if (filter_arguments_nodes.size() != 2) - return; - - ColumnNode * filter_column_node = nullptr; - if (filter_arguments_nodes[1]->as() && (filter_column_node = filter_arguments_nodes[0]->as()) - && filter_column_node->getColumnName() == column_node->getColumnName()) - { - /// Rewrite arrayExists(x -> x = elem, arr) -> has(arr, elem) - function_arguments_nodes[0] = std::move(function_arguments_nodes[1]); - function_arguments_nodes[1] = std::move(filter_arguments_nodes[1]); - function_node->resolveAsFunction( - FunctionFactory::instance().get("has", getContext())->build(function_node->getArgumentColumns())); - } - else if ( - filter_arguments_nodes[0]->as() && (filter_column_node = filter_arguments_nodes[1]->as()) - && filter_column_node->getColumnName() == column_node->getColumnName()) - { - /// Rewrite arrayExists(x -> elem = x, arr) -> has(arr, elem) - function_arguments_nodes[0] = std::move(function_arguments_nodes[1]); - function_arguments_nodes[1] = std::move(filter_arguments_nodes[0]); - function_node->resolveAsFunction( - FunctionFactory::instance().get("has", getContext())->build(function_node->getArgumentColumns())); - } + /// Rewrite arrayExists(x -> x = elem, arr) -> has(arr, elem) + has_constant_element_argument = filter_rhs_argument_node; } - }; + else if (filter_lhs_argument_node_type == QueryTreeNodeType::CONSTANT && + filter_rhs_argument_node_type == QueryTreeNodeType::COLUMN && + filter_rhs_argument_node->isEqual(*lambda_argument_column_node)) + { + /// Rewrite arrayExists(x -> elem = x, arr) -> has(arr, elem) + has_constant_element_argument = filter_lhs_argument_node; + } + else + { + return; + } + + auto has_function = FunctionFactory::instance().get("has", getContext()); + array_exists_function_arguments_nodes[0] = std::move(array_exists_function_arguments_nodes[1]); + array_exists_function_arguments_nodes[1] = std::move(has_constant_element_argument); + array_exists_function_node->resolveAsFunction(has_function->build(array_exists_function_node->getArgumentColumns())); + } +}; } diff --git a/src/Analyzer/Passes/ArrayExistsToHasPass.h b/src/Analyzer/Passes/ArrayExistsToHasPass.h index 7d9d1cf3d68..8f4623116e3 100644 --- a/src/Analyzer/Passes/ArrayExistsToHasPass.h +++ b/src/Analyzer/Passes/ArrayExistsToHasPass.h @@ -4,8 +4,15 @@ namespace DB { -/// Rewrite possible 'arrayExists(func, arr)' to 'has(arr, elem)' to improve performance -/// arrayExists(x -> x = 1, arr) -> has(arr, 1) + +/** Rewrite possible 'arrayExists(func, arr)' to 'has(arr, elem)' to improve performance. + * + * Example: SELECT arrayExists(x -> x = 1, arr); + * Result: SELECT has(arr, 1); + * + * Example: SELECT arrayExists(x -> 1 = x, arr); + * Result: SELECT has(arr, 1); + */ class RewriteArrayExistsToHasPass final : public IQueryTreePass { public: @@ -15,4 +22,5 @@ public: void run(QueryTreeNodePtr query_tree_node, ContextPtr context) override; }; + } diff --git a/src/Analyzer/Passes/AutoFinalOnQueryPass.cpp b/src/Analyzer/Passes/AutoFinalOnQueryPass.cpp index fdf818681d7..fa5fc0e75a8 100644 --- a/src/Analyzer/Passes/AutoFinalOnQueryPass.cpp +++ b/src/Analyzer/Passes/AutoFinalOnQueryPass.cpp @@ -22,8 +22,7 @@ public: void visitImpl(QueryTreeNodePtr & node) { - const auto & context = getContext(); - if (!context->getSettingsRef().final) + if (!getSettings().final) return; const auto * query_node = node->as(); diff --git a/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h b/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h index 83b974954fa..71a038bcf39 100644 --- a/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h +++ b/src/Analyzer/Passes/ShardNumColumnToFunctionPass.h @@ -6,6 +6,9 @@ namespace DB { /** Rewrite _shard_num column into shardNum() function. + * + * Example: SELECT _shard_num FROM distributed_table; + * Result: SELECT shardNum() FROM distributed_table; */ class ShardNumColumnToFunctionPass final : public IQueryTreePass { From 20c0476d2fb9cc4964b8075fdc8507d6f3c467b3 Mon Sep 17 00:00:00 2001 From: Sergei Trifonov Date: Wed, 8 Mar 2023 10:51:16 +0100 Subject: [PATCH 335/342] Update docs/en/operations/server-configuration-parameters/settings.md Co-authored-by: pufit --- docs/en/operations/server-configuration-parameters/settings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/operations/server-configuration-parameters/settings.md b/docs/en/operations/server-configuration-parameters/settings.md index 92a8270ac0f..9d8f3ba8355 100644 --- a/docs/en/operations/server-configuration-parameters/settings.md +++ b/docs/en/operations/server-configuration-parameters/settings.md @@ -989,7 +989,7 @@ Default value: `100`. ## max_io_thread_pool_free_size {#max-io-thread-pool-free-size} -If the number of **idle** threads in the IO Thread pool is greater than `max_io_thread_pool_free_size`, then ClickHouse releases resources occupied by some threads and the pool size is decreased. Threads can be created again if necessary. +If the number of **idle** threads in the IO Thread pool exceeds `max_io_thread_pool_free_size`, ClickHouse will release resources occupied by idling threads and decrease the pool size. Threads can be created again if necessary. Possible values: From 880b34fded1b8a3cee8b690309b11c9903af091a Mon Sep 17 00:00:00 2001 From: Sergei Trifonov Date: Wed, 8 Mar 2023 10:51:34 +0100 Subject: [PATCH 336/342] Update docs/en/operations/server-configuration-parameters/settings.md Co-authored-by: pufit --- docs/en/operations/server-configuration-parameters/settings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/operations/server-configuration-parameters/settings.md b/docs/en/operations/server-configuration-parameters/settings.md index 9d8f3ba8355..3fe815bc79a 100644 --- a/docs/en/operations/server-configuration-parameters/settings.md +++ b/docs/en/operations/server-configuration-parameters/settings.md @@ -1020,7 +1020,7 @@ Default value: `1000`. ## max_backups_io_thread_pool_free_size {#max-backups-io-thread-pool-free-size} -If the number of **idle** threads in the Backups IO Thread pool is greater than `max_backup_io_thread_pool_free_size`, then ClickHouse releases resources occupied by some threads and the pool size is decreased. Threads can be created again if necessary. +If the number of **idle** threads in the Backups IO Thread pool exceeds `max_backup_io_thread_pool_free_size`, ClickHouse will release resources occupied by idling threads and decrease the pool size. Threads can be created again if necessary. Possible values: From b6f3b81403dd8051be2fc511f4ec87023ef1aca2 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Mon, 6 Mar 2023 12:50:09 +0100 Subject: [PATCH 337/342] Query plan filter push down optimization filled JOIN fix --- src/Processors/QueryPlan/JoinStep.h | 2 ++ .../QueryPlan/Optimizations/filterPushDown.cpp | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Processors/QueryPlan/JoinStep.h b/src/Processors/QueryPlan/JoinStep.h index a814d541574..e7185f36588 100644 --- a/src/Processors/QueryPlan/JoinStep.h +++ b/src/Processors/QueryPlan/JoinStep.h @@ -49,6 +49,8 @@ public: String getName() const override { return "FilledJoin"; } void transformPipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &) override; + const JoinPtr & getJoin() const { return join; } + private: void updateOutputStream() override; diff --git a/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp b/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp index d466c52725f..37bc894339f 100644 --- a/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp +++ b/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp @@ -314,11 +314,14 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes if (auto updated_steps = simplePushDownOverStep(parent_node, nodes, child)) return updated_steps; - if (auto * join = typeid_cast(child.get())) + auto * join = typeid_cast(child.get()); + auto * filled_join = typeid_cast(child.get()); + + if (join || filled_join) { auto join_push_down = [&](JoinKind kind) -> size_t { - const auto & table_join = join->getJoin()->getTableJoin(); + const auto & table_join = join ? join->getJoin()->getTableJoin() : filled_join->getJoin()->getTableJoin(); /// Only inner and left(/right) join are supported. Other types may generate default values for left table keys. /// So, if we push down a condition like `key != 0`, not all rows may be filtered. @@ -326,8 +329,8 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes return 0; bool is_left = kind == JoinKind::Left; - const auto & input_header = is_left ? join->getInputStreams().front().header : join->getInputStreams().back().header; - const auto & res_header = join->getOutputStream().header; + const auto & input_header = is_left ? child->getInputStreams().front().header : child->getInputStreams().back().header; + const auto & res_header = child->getOutputStream().header; Names allowed_keys; const auto & source_columns = input_header.getNames(); for (const auto & name : source_columns) @@ -372,7 +375,7 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes return updated_steps; /// For full sorting merge join we push down both to the left and right tables, because left and right streams are not independent. - if (join->allowPushDownToRight()) + if (join && join->allowPushDownToRight()) { if (size_t updated_steps = join_push_down(JoinKind::Right)) return updated_steps; From 3aed5a4ab455ce1c6600a5a011381432e5543ba4 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 7 Mar 2023 12:35:17 +0100 Subject: [PATCH 338/342] Added tests --- ...dicate_push_down_filled_join_fix.reference | 33 +++++++++++++++++++ ...75_predicate_push_down_filled_join_fix.sql | 26 +++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.reference create mode 100644 tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.sql diff --git a/tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.reference b/tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.reference new file mode 100644 index 00000000000..ecdb62c5cb5 --- /dev/null +++ b/tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.reference @@ -0,0 +1,33 @@ +Expression ((Project names + (Projection + ))) +Header: t1.id UInt64 + t1.value String + t2.value String +Actions: INPUT : 0 -> t1.id_0 UInt64 : 0 + INPUT : 1 -> t1.value_1 String : 1 + INPUT : 2 -> t2.value_2 String : 2 + ALIAS t1.id_0 :: 0 -> t1.id UInt64 : 3 + ALIAS t1.value_1 :: 1 -> t1.value String : 0 + ALIAS t2.value_2 :: 2 -> t2.value String : 1 +Positions: 3 0 1 + FilledJoin (Filled JOIN) + Header: t1.id_0 UInt64 + t1.value_1 String + t2.value_2 String + Filter (( + (JOIN actions + Change column names to column identifiers))) + Header: t1.id_0 UInt64 + t1.value_1 String + Filter column: equals(t1.id_0, 0_UInt8) (removed) + Actions: INPUT : 0 -> id UInt64 : 0 + INPUT : 1 -> value String : 1 + COLUMN Const(UInt8) -> 0_UInt8 UInt8 : 2 + ALIAS id :: 0 -> t1.id_0 UInt64 : 3 + ALIAS value :: 1 -> t1.value_1 String : 0 + FUNCTION equals(t1.id_0 : 3, 0_UInt8 :: 2) -> equals(t1.id_0, 0_UInt8) UInt8 : 1 + Positions: 1 3 0 + ReadFromMergeTree (default.test_table) + Header: id UInt64 + value String + ReadType: Default + Parts: 1 + Granules: 1 +0 Value JoinValue diff --git a/tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.sql b/tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.sql new file mode 100644 index 00000000000..78cb423216b --- /dev/null +++ b/tests/queries/0_stateless/02675_predicate_push_down_filled_join_fix.sql @@ -0,0 +1,26 @@ +SET allow_experimental_analyzer = 1; + +DROP TABLE IF EXISTS test_table; +CREATE TABLE test_table +( + id UInt64, + value String +) ENGINE=MergeTree ORDER BY id; + +INSERT INTO test_table VALUES (0, 'Value'); + +DROP TABLE IF EXISTS test_table_join; +CREATE TABLE test_table_join +( + id UInt64, + value String +) ENGINE = Join(All, inner, id); + +INSERT INTO test_table_join VALUES (0, 'JoinValue'); + +EXPLAIN header = 1, actions = 1 SELECT t1.id, t1.value, t2.value FROM test_table AS t1 INNER JOIN test_table_join AS t2 ON t1.id = t2.id WHERE t1.id = 0; + +SELECT t1.id, t1.value, t2.value FROM test_table AS t1 INNER JOIN test_table_join AS t2 ON t1.id = t2.id WHERE t1.id = 0; + +DROP TABLE test_table_join; +DROP TABLE test_table; From 79ef95bc0c2c2e983cdbb90afc75177aa8597430 Mon Sep 17 00:00:00 2001 From: avogar Date: Wed, 8 Mar 2023 12:54:22 +0000 Subject: [PATCH 339/342] Fix tests and builds --- .../Formats/Impl/ParquetBlockOutputFormat.cpp | 2 +- .../01308_orc_output_format_arrays.reference | Bin 567 -> 36 bytes .../01308_orc_output_format_arrays.sh | 2 +- .../queries/0_stateless/02426_orc_bug.reference | Bin 312 -> 36 bytes tests/queries/0_stateless/02426_orc_bug.sh | 9 +++++++++ tests/queries/0_stateless/02426_orc_bug.sql | 3 --- 6 files changed, 11 insertions(+), 5 deletions(-) create mode 100755 tests/queries/0_stateless/02426_orc_bug.sh delete mode 100644 tests/queries/0_stateless/02426_orc_bug.sql diff --git a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp index cedd8a9c54c..759f773a574 100644 --- a/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/ParquetBlockOutputFormat.cpp @@ -20,7 +20,7 @@ namespace ErrorCodes namespace { -static parquet::ParquetVersion::type getParquetVersion(const FormatSettings & settings) +parquet::ParquetVersion::type getParquetVersion(const FormatSettings & settings) { switch (settings.parquet.output_version) { diff --git a/tests/queries/0_stateless/01308_orc_output_format_arrays.reference b/tests/queries/0_stateless/01308_orc_output_format_arrays.reference index 1f9646ac112132378f512bb4e3a610f6019698e1..7feea7cec35600166ea51a6950459c05eb6c7d88 100644 GIT binary patch literal 36 rcmXpqH%qZFO-ZpdOEI=INij9EFfubvv`jTIH%c@%Pfk%#(B%REtfL6? literal 567 zcmaKou}Z{15Qb-Fv+J_CUfj1(^yJ73Txpxm?(hk$eF1A9K)8pp z5ER#OZ*wFHHuLYd|I9G_i{;#AZq?1nOH zi@h-PB)k2_Z@x78PeSsIycPNP4-!J}r2;h0Sd#;1L6P@^5+4Osk&~#JS^h!#DJTY| z99adXz?c*3z$*GG`bp%3P4IQ>*K8yV+53Mj)#Y{L&fPY*Z5^i6IX{^Uqp&J}r7{({ zSCX4ST<`_rgrW2m5pNorkz6v0~6|4#a#fA`ch4R^#kxNzslh121M bv&Wk&w9hE%uGh(gAgwNH^YEtBVmaq;55+tO diff --git a/tests/queries/0_stateless/01308_orc_output_format_arrays.sh b/tests/queries/0_stateless/01308_orc_output_format_arrays.sh index 1d9aea353b6..498854874cf 100755 --- a/tests/queries/0_stateless/01308_orc_output_format_arrays.sh +++ b/tests/queries/0_stateless/01308_orc_output_format_arrays.sh @@ -11,7 +11,7 @@ $CLICKHOUSE_CLIENT --query="CREATE TABLE orc (array1 Array(Int32), array2 Array( $CLICKHOUSE_CLIENT --query="INSERT INTO orc VALUES ([1,2,3,4,5], [[1,2], [3,4], [5]]), ([42], [[42, 42], [42]])"; -$CLICKHOUSE_CLIENT --query="SELECT * FROM orc FORMAT ORC"; +$CLICKHOUSE_CLIENT --query="SELECT * FROM orc FORMAT ORC SETTINGS output_format_orc_compression_method='none'" | md5sum; $CLICKHOUSE_CLIENT --query="DROP TABLE orc"; diff --git a/tests/queries/0_stateless/02426_orc_bug.reference b/tests/queries/0_stateless/02426_orc_bug.reference index e5ad2b492892318e8fa83954efdecbb2bf287a91..baa88da215881130acc288c7ce2a15bd34f45e90 100644 GIT binary patch literal 36 rcmcCawlFudut-WXGfYc0H%zs(G)*xwOi4*gGB-9%F*jFG(B%REwulK^ literal 312 zcmeYda^~XZVrF1q5MtqA3Si*ke3ufJp+V>i>cN2mUcI{%`ot z$i>FNCcq%U3#J(*_`x)jggB690E#n#X+|*3B)}-a4CVt30n&^h!+_QT)ghBo% Date: Wed, 8 Mar 2023 08:26:07 -0500 Subject: [PATCH 340/342] geteuid is enough --- utils/self-extracting-executable/decompressor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/self-extracting-executable/decompressor.cpp b/utils/self-extracting-executable/decompressor.cpp index 1374c269dc1..d41b9b1ebe1 100644 --- a/utils/self-extracting-executable/decompressor.cpp +++ b/utils/self-extracting-executable/decompressor.cpp @@ -170,7 +170,7 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n bool isSudo() { - return geteuid() == 0 && getenv("SUDO_USER") && getenv("SUDO_UID") && getenv("SUDO_GID"); // NOLINT(concurrency-mt-unsafe) + return geteuid() == 0; } /// Read data about files and decomrpess them. From 95beedc8a4bb3fc8bd33318b825d3718bdff945c Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Wed, 8 Mar 2023 15:56:13 +0000 Subject: [PATCH 341/342] Fix 02343_group_by_use_nulls test --- tests/queries/0_stateless/02343_group_by_use_nulls.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/queries/0_stateless/02343_group_by_use_nulls.sql b/tests/queries/0_stateless/02343_group_by_use_nulls.sql index a979a78be0d..e1d4021a943 100644 --- a/tests/queries/0_stateless/02343_group_by_use_nulls.sql +++ b/tests/queries/0_stateless/02343_group_by_use_nulls.sql @@ -1,3 +1,4 @@ +set optimize_group_by_function_keys=0; -- { echoOn } SELECT number, number % 2, sum(number) AS val FROM numbers(10) From 52767ea186da08f8a48f5ee2941c1a86d20b0521 Mon Sep 17 00:00:00 2001 From: Alexander Tokmakov Date: Wed, 8 Mar 2023 19:25:08 +0300 Subject: [PATCH 342/342] Update 02368_cancel_write_into_hdfs.sh --- tests/queries/0_stateless/02368_cancel_write_into_hdfs.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02368_cancel_write_into_hdfs.sh b/tests/queries/0_stateless/02368_cancel_write_into_hdfs.sh index 8262cd7eab5..65d0b3f434f 100755 --- a/tests/queries/0_stateless/02368_cancel_write_into_hdfs.sh +++ b/tests/queries/0_stateless/02368_cancel_write_into_hdfs.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash -# Tags: no-fasttest, no-stress +# Tags: no-fasttest, no-asan, no-tsan, no-msan, no-ubsan, no-debug +# FIXME https://github.com/ClickHouse/ClickHouse/issues/47207 CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=../shell_config.sh