diff --git a/src/Parsers/ASTIdentifier.cpp b/src/Parsers/ASTIdentifier.cpp index 58052b1419b..5724ea7dc04 100644 --- a/src/Parsers/ASTIdentifier.cpp +++ b/src/Parsers/ASTIdentifier.cpp @@ -201,6 +201,39 @@ String ASTTableIdentifier::getDatabaseName() const else return {}; } +ASTPtr ASTTableIdentifier::getTable() const +{ + if (name_parts.size() == 2) + { + if (!name_parts[1].empty()) + return std::make_shared(name_parts[1]); + + if (name_parts[0].empty()) + return std::make_shared("", children[1]->clone()); + else + return std::make_shared("", children[0]->clone()); + } + else + { + if (name_parts[0].empty()) + return std::make_shared("", children[0]->clone()); + else + return std::make_shared(name_parts[0]); + } +} + +ASTPtr ASTTableIdentifier::getDatabase() const +{ + if (name_parts.size() == 2) + { + if (name_parts[0].empty()) + return std::make_shared("", children[0]->clone()); + else + return std::make_shared(name_parts[0]); + } + else return {}; +} + void ASTTableIdentifier::resetTable(const String & database_name, const String & table_name) { auto identifier = std::make_shared(database_name, table_name); diff --git a/src/Parsers/ASTIdentifier.h b/src/Parsers/ASTIdentifier.h index 323280e07bc..048b32e438d 100644 --- a/src/Parsers/ASTIdentifier.h +++ b/src/Parsers/ASTIdentifier.h @@ -61,6 +61,7 @@ protected: private: using ASTWithAlias::children; /// ASTIdentifier is child free + friend class ASTTableIdentifier; friend class ReplaceQueryParameterVisitor; friend struct IdentifierSemantic; friend void setIdentifierSpecial(ASTPtr & ast); @@ -83,6 +84,9 @@ public: StorageID getTableId() const; String getDatabaseName() const; + ASTPtr getTable() const; + ASTPtr getDatabase() const; + // FIXME: used only when it's needed to rewrite distributed table name to real remote table name. void resetTable(const String & database_name, const String & table_name); // TODO(ilezhankin): get rid of this diff --git a/src/Parsers/ASTQueryWithOnCluster.h b/src/Parsers/ASTQueryWithOnCluster.h index 0e08c961d24..de665fa4f53 100644 --- a/src/Parsers/ASTQueryWithOnCluster.h +++ b/src/Parsers/ASTQueryWithOnCluster.h @@ -45,18 +45,6 @@ protected: return query_ptr; } - template - static ASTPtr removeOnClusterSystem(ASTPtr query_ptr, const std::string & new_database) - { - T & query = static_cast(*query_ptr); - - query.cluster.clear(); - if (query.database.empty()) - query.database = new_database; - - return query_ptr; - } - template static ASTPtr removeOnCluster(ASTPtr query_ptr) { diff --git a/src/Parsers/ASTQueryWithTableAndOutput.cpp b/src/Parsers/ASTQueryWithTableAndOutput.cpp index 9529ede0818..8aad26d8add 100644 --- a/src/Parsers/ASTQueryWithTableAndOutput.cpp +++ b/src/Parsers/ASTQueryWithTableAndOutput.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,6 +7,49 @@ namespace DB { +String ASTQueryWithTableAndOutput::getDatabase() const +{ + String name; + tryGetIdentifierNameInto(database, name); + return name; +} + +String ASTQueryWithTableAndOutput::getTable() const +{ + String name; + tryGetIdentifierNameInto(table, name); + return name; +} + +void ASTQueryWithTableAndOutput::setDatabase(const String & name) +{ + if (name.empty()) + database.reset(); + else + database = std::make_shared(name); +} + +void ASTQueryWithTableAndOutput::setTable(const String & name) +{ + if (name.empty()) + table.reset(); + else + table = std::make_shared(name); +} + +void ASTQueryWithTableAndOutput::cloneTableOptions(ASTQueryWithTableAndOutput & cloned) const +{ + if (database) + { + cloned.database = database->clone(); + cloned.children.push_back(cloned.database); + } + if (table) + { + cloned.table = table->clone(); + cloned.children.push_back(cloned.table); + } +} void ASTQueryWithTableAndOutput::formatHelper(const FormatSettings & settings, const char * name) const { settings.ostr << (settings.hilite ? hilite_keyword : "") << name << " " << (settings.hilite ? hilite_none : ""); diff --git a/src/Parsers/ASTQueryWithTableAndOutput.h b/src/Parsers/ASTQueryWithTableAndOutput.h index cad3e5355c3..a01a9059dea 100644 --- a/src/Parsers/ASTQueryWithTableAndOutput.h +++ b/src/Parsers/ASTQueryWithTableAndOutput.h @@ -2,7 +2,6 @@ #include #include -#include #include @@ -21,50 +20,13 @@ public: UUID uuid = UUIDHelpers::Nil; bool temporary{false}; + String getDatabase() const; + String getTable() const; - String getDatabase() const - { - String name; - tryGetIdentifierNameInto(database, name); - return name; - } + void setDatabase(const String & name); + void setTable(const String & name); - String getTable() const - { - String name; - tryGetIdentifierNameInto(table, name); - return name; - } - - void setDatabase(const String & name) - { - if (name.empty()) - database.reset(); - else - database = std::make_shared(name); - } - - void setTable(const String & name) - { - if (name.empty()) - table.reset(); - else - table = std::make_shared(name); - } - - void cloneTableOptions(ASTQueryWithTableAndOutput & cloned) const - { - if (database) - { - cloned.database = database->clone(); - cloned.children.push_back(cloned.database); - } - if (table) - { - cloned.table = table->clone(); - cloned.children.push_back(cloned.table); - } - } + void cloneTableOptions(ASTQueryWithTableAndOutput & cloned) const; protected: void formatHelper(const FormatSettings & settings, const char * name) const;