From 1dce20e5daf60cbb0777b17b9e20a0c6b192345c Mon Sep 17 00:00:00 2001 From: vdimir Date: Sat, 14 Nov 2020 16:44:45 +0300 Subject: [PATCH] Default host, port and user parameters for source(clickhouse(...)) --- .../ClickHouseDictionarySource.cpp | 18 +++++++++++----- src/Dictionaries/ClickHouseDictionarySource.h | 2 +- .../01018_ddl_dictionaries_select.reference | 2 ++ .../01018_ddl_dictionaries_select.sql | 21 +++++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Dictionaries/ClickHouseDictionarySource.cpp b/src/Dictionaries/ClickHouseDictionarySource.cpp index 8199b16a94b..72cdb4a78e3 100644 --- a/src/Dictionaries/ClickHouseDictionarySource.cpp +++ b/src/Dictionaries/ClickHouseDictionarySource.cpp @@ -17,6 +17,14 @@ namespace DB { +namespace +{ + inline static UInt16 getPortFromContext(const Context & context, bool secure) + { + return secure ? context.getTCPPortSecure().value_or(0) : context.getTCPPort(); + } +} + namespace ErrorCodes { } @@ -59,10 +67,10 @@ ClickHouseDictionarySource::ClickHouseDictionarySource( const std::string & default_database) : update_time{std::chrono::system_clock::from_time_t(0)} , dict_struct{dict_struct_} - , host{config.getString(config_prefix + ".host")} - , port(config.getInt(config_prefix + ".port")) , secure(config.getBool(config_prefix + ".secure", false)) - , user{config.getString(config_prefix + ".user", "")} + , host{config.getString(config_prefix + ".host", "localhost")} + , port(config.getInt(config_prefix + ".port", getPortFromContext(context_, secure))) + , user{config.getString(config_prefix + ".user", "default")} , password{config.getString(config_prefix + ".password", "")} , db{config.getString(config_prefix + ".db", default_database)} , table{config.getString(config_prefix + ".table")} @@ -72,7 +80,7 @@ ClickHouseDictionarySource::ClickHouseDictionarySource( , query_builder{dict_struct, db, "", table, where, IdentifierQuotingStyle::Backticks} , sample_block{sample_block_} , context(context_) - , is_local{isLocalAddress({host, port}, secure ? context.getTCPPortSecure().value_or(0) : context.getTCPPort())} + , is_local{isLocalAddress({host, port}, getPortFromContext(context_, secure))} , pool{is_local ? nullptr : createPool(host, port, secure, db, user, password)} , load_all_query{query_builder.composeLoadAllQuery()} { @@ -92,9 +100,9 @@ ClickHouseDictionarySource::ClickHouseDictionarySource( ClickHouseDictionarySource::ClickHouseDictionarySource(const ClickHouseDictionarySource & other) : update_time{other.update_time} , dict_struct{other.dict_struct} + , secure{other.secure} , host{other.host} , port{other.port} - , secure{other.secure} , user{other.user} , password{other.password} , db{other.db} diff --git a/src/Dictionaries/ClickHouseDictionarySource.h b/src/Dictionaries/ClickHouseDictionarySource.h index 8017d458b7e..9ef77d061fd 100644 --- a/src/Dictionaries/ClickHouseDictionarySource.h +++ b/src/Dictionaries/ClickHouseDictionarySource.h @@ -61,9 +61,9 @@ private: std::chrono::time_point update_time; const DictionaryStructure dict_struct; + const bool secure; const std::string host; const UInt16 port; - const bool secure; const std::string user; const std::string password; const std::string db; diff --git a/tests/queries/0_stateless/01018_ddl_dictionaries_select.reference b/tests/queries/0_stateless/01018_ddl_dictionaries_select.reference index 28ee2a8690b..f69302fb90f 100644 --- a/tests/queries/0_stateless/01018_ddl_dictionaries_select.reference +++ b/tests/queries/0_stateless/01018_ddl_dictionaries_select.reference @@ -7,6 +7,8 @@ 17 11 11 +17 +11 7 11 6 diff --git a/tests/queries/0_stateless/01018_ddl_dictionaries_select.sql b/tests/queries/0_stateless/01018_ddl_dictionaries_select.sql index 4b548a913ea..78789388a89 100644 --- a/tests/queries/0_stateless/01018_ddl_dictionaries_select.sql +++ b/tests/queries/0_stateless/01018_ddl_dictionaries_select.sql @@ -51,6 +51,27 @@ DROP DICTIONARY database_for_dict.dict1; SELECT dictGetUInt8('database_for_dict.dict1', 'second_column', toUInt64(11)); -- {serverError 36} +-- SOURCE(CLICKHOUSE(...)) uses default params if not specified +DROP DICTIONARY IF EXISTS database_for_dict.dict1; + +CREATE DICTIONARY database_for_dict.dict1 +( + key_column UInt64 DEFAULT 0, + second_column UInt8 DEFAULT 1, + third_column String DEFAULT 'qqq', + fourth_column Float64 DEFAULT 42.0 +) +PRIMARY KEY key_column +SOURCE(CLICKHOUSE(TABLE 'table_for_dict' PASSWORD '' DB 'database_for_dict')) +LIFETIME(MIN 1 MAX 10) +LAYOUT(FLAT()); + +SELECT dictGetUInt8('database_for_dict.dict1', 'second_column', toUInt64(11)); + +SELECT count(distinct(dictGetUInt8('database_for_dict.dict1', 'second_column', toUInt64(number)))) from numbers(100); + +DROP DICTIONARY database_for_dict.dict1; + CREATE DICTIONARY database_for_dict.dict1 ( key_column UInt64 DEFAULT 0,