From f253719eac18119859c5def7163b4aa9a0e9cddd Mon Sep 17 00:00:00 2001 From: VadimPE Date: Wed, 29 Aug 2018 18:15:42 +0300 Subject: [PATCH] CLICKHOUSE-3934 add SettingsJoinStrictness --- dbms/src/Common/ErrorCodes.cpp | 3 +- dbms/src/Interpreters/ExpressionAnalyzer.cpp | 6 +-- dbms/src/Interpreters/Settings.h | 2 +- dbms/src/Interpreters/SettingsCommon.cpp | 48 ++++++++++++++++++++ dbms/src/Interpreters/SettingsCommon.h | 31 +++++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) diff --git a/dbms/src/Common/ErrorCodes.cpp b/dbms/src/Common/ErrorCodes.cpp index 719775701d..513a26987e 100644 --- a/dbms/src/Common/ErrorCodes.cpp +++ b/dbms/src/Common/ErrorCodes.cpp @@ -391,7 +391,8 @@ namespace ErrorCodes extern const int CANNOT_READLINE = 414; extern const int ALL_REPLICAS_LOST = 415; extern const int REPLICA_STATUS_CHANGED = 416; - extern const int EXPECT_ALL_OR_ANY = 417; + extern const int EXPECTED_ALL_OR_ANY = 417; + extern const int UNKNOWN_JOIN_STRICTNESS = 418; extern const int KEEPER_EXCEPTION = 999; extern const int POCO_EXCEPTION = 1000; diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index 5806e16750..7f7edaebaa 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -96,7 +96,7 @@ namespace ErrorCodes extern const int CONDITIONAL_TREE_PARENT_NOT_FOUND; extern const int TYPE_MISMATCH; extern const int INVALID_JOIN_ON_EXPRESSION; - extern const int EXPECT_ALL_OR_ANY; + extern const int EXPECTED_ALL_OR_ANY; } @@ -2492,10 +2492,8 @@ bool ExpressionAnalyzer::appendJoin(ExpressionActionsChain & chain, bool only_ty join_params.strictness = ASTTableJoin::Strictness::Any; else if (settings.join_default_strictness.toString() == "ALL") join_params.strictness = ASTTableJoin::Strictness::All; - else if (settings.join_default_strictness.toString() != "") - throw Exception("Unexcepted join_default_strictness = " + settings.join_default_strictness.toString() + ". It can be an empty string, ANY or ALL"); else - throw Exception("Expected ANY or ALL in JOIN section, because setting (join_default_strictness) is empty", DB::ErrorCodes::EXPECT_ALL_OR_ANY); + throw Exception("Expected ANY or ALL in JOIN section, because setting (join_default_strictness) is empty", DB::ErrorCodes::EXPECTED_ALL_OR_ANY); } const auto & table_to_join = static_cast(*join_element.table_expression); diff --git a/dbms/src/Interpreters/Settings.h b/dbms/src/Interpreters/Settings.h index 5cc90694c5..1bacccc292 100644 --- a/dbms/src/Interpreters/Settings.h +++ b/dbms/src/Interpreters/Settings.h @@ -173,7 +173,7 @@ struct Settings \ M(SettingBool, join_use_nulls, 0, "Use NULLs for non-joined rows of outer JOINs. If false, use default value of corresponding columns data type.") \ \ - M(SettingString, join_default_strictness, "", "Set default strictness in JOIN query. If empty, query without strictness will throw exception") \ + M(SettingJoinStrictness, join_default_strictness, JoinStrictness::Unspecified, "Set default strictness in JOIN query. If empty, query without strictness will throw exception") \ \ M(SettingUInt64, preferred_block_size_bytes, 1000000, "") \ \ diff --git a/dbms/src/Interpreters/SettingsCommon.cpp b/dbms/src/Interpreters/SettingsCommon.cpp index dfc79fd86c..ccf4cade6a 100644 --- a/dbms/src/Interpreters/SettingsCommon.cpp +++ b/dbms/src/Interpreters/SettingsCommon.cpp @@ -22,6 +22,7 @@ namespace ErrorCodes extern const int UNKNOWN_COMPRESSION_METHOD; extern const int UNKNOWN_DISTRIBUTED_PRODUCT_MODE; extern const int UNKNOWN_GLOBAL_SUBQUERIES_METHOD; + extern const int UNKNOWN_JOIN_STRICTNESS; extern const int SIZE_OF_FIXED_STRING_DOESNT_MATCH; extern const int BAD_ARGUMENTS; } @@ -288,6 +289,53 @@ void SettingLoadBalancing::write(WriteBuffer & buf) const } +JoinStrictness SettingJoinStrictness::getJoinStrictness(const String & s) +{ + if (s == "") return JoinStrictness::Unspecified; + if (s == "ALL") return JoinStrictness::ALL; + if (s == "ANY") return JoinStrictness::ANY; + + throw Exception("Unknown join strictness mode: '" + s + "', must be one of '', 'ALL', 'ANY'", + ErrorCodes::UNKNOWN_JOIN_STRICTNESS); +} + +String SettingJoinStrictness::toString() const +{ + const char * strings[] = {"", "ALL", "ANY"}; + if (value < JoinStrictness::Unspecified || value > JoinStrictness::ANY) + throw Exception("Unknown join strictness mode", ErrorCodes::UNKNOWN_JOIN_STRICTNESS); + return strings[static_cast(value)]; +} + +void SettingJoinStrictness::set(JoinStrictness x) +{ + value = x; + changed = true; +} + +void SettingJoinStrictness::set(const Field & x) +{ + set(safeGet(x)); +} + +void SettingJoinStrictness::set(const String & x) +{ + set(getJoinStrictness(x)); +} + +void SettingJoinStrictness::set(ReadBuffer & buf) +{ + String x; + readBinary(x, buf); + set(x); +} + +void SettingJoinStrictness::write(WriteBuffer & buf) const +{ + writeBinary(toString(), buf); +} + + TotalsMode SettingTotalsMode::getTotalsMode(const String & s) { if (s == "before_having") return TotalsMode::BEFORE_HAVING; diff --git a/dbms/src/Interpreters/SettingsCommon.h b/dbms/src/Interpreters/SettingsCommon.h index fc441ea30c..667912d01b 100644 --- a/dbms/src/Interpreters/SettingsCommon.h +++ b/dbms/src/Interpreters/SettingsCommon.h @@ -191,6 +191,37 @@ struct SettingLoadBalancing }; +enum class JoinStrictness +{ + Unspecified = 0, /// Query JOIN without strictness will throw Exception. + ALL, /// Query JOIN without strictness -> ALL JOIN ... + ANY, /// Query JOIN without strictness -> ANY JOIN ... +}; + + +struct SettingJoinStrictness +{ + JoinStrictness value; + bool changed = false; + + SettingJoinStrictness(JoinStrictness x) : value(x) {} + + operator JoinStrictness() const { return value; } + SettingJoinStrictness & operator= (JoinStrictness x) { set(x); return *this; } + + static JoinStrictness getJoinStrictness(const String & s); + + String toString() const; + + void set(JoinStrictness x); + void set(const Field & x); + void set(const String & x); + void set(ReadBuffer & buf); + + void write(WriteBuffer & buf) const; +}; + + /// Which rows should be included in TOTALS. enum class TotalsMode { -- GitLab