diff --git a/dbms/src/Interpreters/AnalyzedJoin.cpp b/dbms/src/Interpreters/AnalyzedJoin.cpp index a11ea893f54..772fcad3eaf 100644 --- a/dbms/src/Interpreters/AnalyzedJoin.cpp +++ b/dbms/src/Interpreters/AnalyzedJoin.cpp @@ -1,16 +1,11 @@ #include -#include -#include #include #include #include -#include -#include #include #include -#include #include @@ -225,31 +220,6 @@ bool AnalyzedJoin::sameJoin(const AnalyzedJoin * x, const AnalyzedJoin * y) && x->columns_added_by_join == y->columns_added_by_join; } -NamesAndTypesList getNamesAndTypeListFromTableExpression(const ASTTableExpression & table_expression, const Context & context) -{ - NamesAndTypesList names_and_type_list; - if (table_expression.subquery) - { - const auto & subquery = table_expression.subquery->children.at(0); - names_and_type_list = InterpreterSelectWithUnionQuery::getSampleBlock(subquery, context).getNamesAndTypesList(); - } - else if (table_expression.table_function) - { - const auto table_function = table_expression.table_function; - auto query_context = const_cast(&context.getQueryContext()); - const auto & function_storage = query_context->executeTableFunction(table_function); - names_and_type_list = function_storage->getSampleBlockNonMaterialized().getNamesAndTypesList(); - } - else if (table_expression.database_and_table_name) - { - DatabaseAndTableWithAlias database_table(table_expression.database_and_table_name); - const auto & table = context.getTable(database_table.database, database_table.table); - names_and_type_list = table->getSampleBlockNonMaterialized().getNamesAndTypesList(); - } - - return names_and_type_list; -} - JoinPtr makeJoin(std::shared_ptr table_join, const Block & right_sample_block) { bool is_left_or_inner = isLeft(table_join->kind()) || isInner(table_join->kind()); diff --git a/dbms/src/Interpreters/AnalyzedJoin.h b/dbms/src/Interpreters/AnalyzedJoin.h index a7052c90a1d..f015dcddfbb 100644 --- a/dbms/src/Interpreters/AnalyzedJoin.h +++ b/dbms/src/Interpreters/AnalyzedJoin.h @@ -123,9 +123,6 @@ public: friend JoinPtr makeJoin(std::shared_ptr table_join, const Block & right_sample_block); }; -struct ASTTableExpression; -NamesAndTypesList getNamesAndTypeListFromTableExpression(const ASTTableExpression & table_expression, const Context & context); - bool isMergeJoin(const JoinPtr &); } diff --git a/dbms/src/Interpreters/DatabaseAndTableWithAlias.cpp b/dbms/src/Interpreters/DatabaseAndTableWithAlias.cpp index ec29fbf40c1..caddbee3091 100644 --- a/dbms/src/Interpreters/DatabaseAndTableWithAlias.cpp +++ b/dbms/src/Interpreters/DatabaseAndTableWithAlias.cpp @@ -1,6 +1,8 @@ #include #include #include +#include + #include #include @@ -14,9 +16,6 @@ namespace DB NameSet removeDuplicateColumns(NamesAndTypesList & columns); -struct ASTTableExpression; -NamesAndTypesList getNamesAndTypeListFromTableExpression(const ASTTableExpression & table_expression, const Context & context); - DatabaseAndTableWithAlias::DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database) { alias = identifier.tryGetAlias(); @@ -70,44 +69,9 @@ String DatabaseAndTableWithAlias::getQualifiedNamePrefix(bool with_dot) const return (!alias.empty() ? alias : table) + (with_dot ? "." : ""); } -std::vector getSelectTablesExpression(const ASTSelectQuery & select_query) -{ - if (!select_query.tables()) - return {}; - - std::vector tables_expression; - - for (const auto & child : select_query.tables()->children) - { - const auto * tables_element = child->as(); - - if (tables_element->table_expression) - tables_expression.emplace_back(tables_element->table_expression->as()); - } - - return tables_expression; -} - -static const ASTTableExpression * getTableExpression(const ASTSelectQuery & select, size_t table_number) -{ - if (!select.tables()) - return {}; - - const auto & tables_in_select_query = select.tables()->as(); - if (tables_in_select_query.children.size() <= table_number) - return {}; - - const auto & tables_element = tables_in_select_query.children[table_number]->as(); - - if (!tables_element.table_expression) - return {}; - - return tables_element.table_expression->as(); -} - std::vector getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database) { - std::vector tables_expression = getSelectTablesExpression(select_query); + std::vector tables_expression = getTableExpressions(select_query); std::vector database_and_table_with_aliases; database_and_table_with_aliases.reserve(tables_expression.size()); @@ -131,43 +95,4 @@ std::optional getDatabaseAndTable(const ASTSelectQuer return DatabaseAndTableWithAlias(database_and_table_name); } -ASTPtr extractTableExpression(const ASTSelectQuery & select, size_t table_number) -{ - if (const ASTTableExpression * table_expression = getTableExpression(select, table_number)) - { - if (table_expression->database_and_table_name) - return table_expression->database_and_table_name; - - if (table_expression->table_function) - return table_expression->table_function; - - if (table_expression->subquery) - return table_expression->subquery->children[0]; - } - - return nullptr; -} - -std::vector getDatabaseAndTablesWithColumnNames(const ASTSelectQuery & select_query, const Context & context) -{ - std::vector tables_with_columns; - - if (select_query.tables() && !select_query.tables()->children.empty()) - { - String current_database = context.getCurrentDatabase(); - - for (const ASTTableExpression * table_expression : getSelectTablesExpression(select_query)) - { - DatabaseAndTableWithAlias table_name(*table_expression, current_database); - - NamesAndTypesList names_and_types = getNamesAndTypeListFromTableExpression(*table_expression, context); - removeDuplicateColumns(names_and_types); - - tables_with_columns.emplace_back(std::move(table_name), names_and_types.getNames()); - } - } - - return tables_with_columns; -} - } diff --git a/dbms/src/Interpreters/DatabaseAndTableWithAlias.h b/dbms/src/Interpreters/DatabaseAndTableWithAlias.h index 22b03a5ed44..532b2e528df 100644 --- a/dbms/src/Interpreters/DatabaseAndTableWithAlias.h +++ b/dbms/src/Interpreters/DatabaseAndTableWithAlias.h @@ -41,9 +41,4 @@ using TableWithColumnNames = std::pair; std::vector getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database); std::optional getDatabaseAndTable(const ASTSelectQuery & select, size_t table_number); -std::vector getDatabaseAndTablesWithColumnNames(const ASTSelectQuery & select_query, const Context & context); - -std::vector getSelectTablesExpression(const ASTSelectQuery & select_query); -ASTPtr extractTableExpression(const ASTSelectQuery & select, size_t table_number); - } diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index d4565f6894d..9fc98a3eb1f 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp b/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp index fba978df9c2..7e8174b7955 100644 --- a/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp +++ b/dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -28,8 +29,6 @@ namespace ErrorCodes extern const int UNKNOWN_IDENTIFIER; } -NamesAndTypesList getNamesAndTypeListFromTableExpression(const ASTTableExpression & table_expression, const Context & context); - namespace { @@ -56,7 +55,7 @@ public: } String table_name = DatabaseAndTableWithAlias(*expr, context.getCurrentDatabase()).getQualifiedNamePrefix(false); - NamesAndTypesList columns = getNamesAndTypeListFromTableExpression(*expr, context); + NamesAndTypesList columns = getColumnsFromTableExpression(*expr, context); tables_order.push_back(table_name); table_columns.emplace(std::move(table_name), std::move(columns)); } diff --git a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp index f68ac0bc708..7457aa1bef7 100644 --- a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp +++ b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -359,7 +360,7 @@ PredicateExpressionsOptimizer::SubqueriesProjectionColumns PredicateExpressionsO { SubqueriesProjectionColumns projection_columns; - for (const auto & table_expression : getSelectTablesExpression(*ast_select)) + for (const auto & table_expression : getTableExpressions(*ast_select)) if (table_expression->subquery) getSubqueryProjectionColumns(table_expression->subquery, projection_columns); @@ -442,7 +443,7 @@ ASTs PredicateExpressionsOptimizer::evaluateAsterisk(ASTSelectQuery * select_que if (!select_query->tables() || select_query->tables()->children.empty()) return {}; - std::vector tables_expression = getSelectTablesExpression(*select_query); + std::vector tables_expression = getTableExpressions(*select_query); if (const auto * qualified_asterisk = asterisk->as()) { diff --git a/dbms/src/Interpreters/SyntaxAnalyzer.cpp b/dbms/src/Interpreters/SyntaxAnalyzer.cpp index 0c9355c924e..7c14fc06190 100644 --- a/dbms/src/Interpreters/SyntaxAnalyzer.cpp +++ b/dbms/src/Interpreters/SyntaxAnalyzer.cpp @@ -20,6 +20,7 @@ #include #include #include /// getSmallestColumn() +#include #include #include @@ -134,7 +135,7 @@ std::vector getTablesWithColumns(const ASTSelectQuery & se { const auto & joined_expression = table_join_node->table_expression->as(); - columns_from_joined_table = getNamesAndTypeListFromTableExpression(joined_expression, context); + columns_from_joined_table = getColumnsFromTableExpression(joined_expression, context); joined_table.first = DatabaseAndTableWithAlias(joined_expression, context.getCurrentDatabase()); for (const auto & column : columns_from_joined_table) diff --git a/dbms/src/Interpreters/getTableExpressions.cpp b/dbms/src/Interpreters/getTableExpressions.cpp new file mode 100644 index 00000000000..237b1c00c9b --- /dev/null +++ b/dbms/src/Interpreters/getTableExpressions.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include + +namespace DB +{ + +std::vector getTableExpressions(const ASTSelectQuery & select_query) +{ + if (!select_query.tables()) + return {}; + + std::vector tables_expression; + + for (const auto & child : select_query.tables()->children) + { + const auto * tables_element = child->as(); + + if (tables_element->table_expression) + tables_expression.emplace_back(tables_element->table_expression->as()); + } + + return tables_expression; +} + +const ASTTableExpression * getTableExpression(const ASTSelectQuery & select, size_t table_number) +{ + if (!select.tables()) + return {}; + + const auto & tables_in_select_query = select.tables()->as(); + if (tables_in_select_query.children.size() <= table_number) + return {}; + + const auto & tables_element = tables_in_select_query.children[table_number]->as(); + + if (!tables_element.table_expression) + return {}; + + return tables_element.table_expression->as(); +} + +ASTPtr extractTableExpression(const ASTSelectQuery & select, size_t table_number) +{ + if (const ASTTableExpression * table_expression = getTableExpression(select, table_number)) + { + if (table_expression->database_and_table_name) + return table_expression->database_and_table_name; + + if (table_expression->table_function) + return table_expression->table_function; + + if (table_expression->subquery) + return table_expression->subquery->children[0]; + } + + return nullptr; +} + +NamesAndTypesList getColumnsFromTableExpression(const ASTTableExpression & table_expression, const Context & context) +{ + NamesAndTypesList names_and_type_list; + if (table_expression.subquery) + { + const auto & subquery = table_expression.subquery->children.at(0); + names_and_type_list = InterpreterSelectWithUnionQuery::getSampleBlock(subquery, context).getNamesAndTypesList(); + } + else if (table_expression.table_function) + { + const auto table_function = table_expression.table_function; + auto query_context = const_cast(&context.getQueryContext()); + const auto & function_storage = query_context->executeTableFunction(table_function); + names_and_type_list = function_storage->getSampleBlockNonMaterialized().getNamesAndTypesList(); + } + else if (table_expression.database_and_table_name) + { + DatabaseAndTableWithAlias database_table(table_expression.database_and_table_name); + const auto & table = context.getTable(database_table.database, database_table.table); + names_and_type_list = table->getSampleBlockNonMaterialized().getNamesAndTypesList(); + } + + return names_and_type_list; +} + +std::vector getDatabaseAndTablesWithColumnNames(const ASTSelectQuery & select_query, const Context & context) +{ + std::vector tables_with_columns; + + if (select_query.tables() && !select_query.tables()->children.empty()) + { + String current_database = context.getCurrentDatabase(); + + for (const ASTTableExpression * table_expression : getTableExpressions(select_query)) + { + DatabaseAndTableWithAlias table_name(*table_expression, current_database); + + NamesAndTypesList names_and_types = getColumnsFromTableExpression(*table_expression, context); + removeDuplicateColumns(names_and_types); + + tables_with_columns.emplace_back(std::move(table_name), names_and_types.getNames()); + } + } + + return tables_with_columns; +} + +} diff --git a/dbms/src/Interpreters/getTableExpressions.h b/dbms/src/Interpreters/getTableExpressions.h new file mode 100644 index 00000000000..60d94f2c844 --- /dev/null +++ b/dbms/src/Interpreters/getTableExpressions.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace DB +{ + +struct ASTTableExpression; +class ASTSelectQuery; +class Context; + +std::vector getTableExpressions(const ASTSelectQuery & select_query); +const ASTTableExpression * getTableExpression(const ASTSelectQuery & select, size_t table_number); +ASTPtr extractTableExpression(const ASTSelectQuery & select, size_t table_number); + +NamesAndTypesList getColumnsFromTableExpression(const ASTTableExpression & table_expression, const Context & context); +std::vector getDatabaseAndTablesWithColumnNames(const ASTSelectQuery & select_query, const Context & context); + +} diff --git a/dbms/src/Storages/LiveView/StorageLiveView.cpp b/dbms/src/Storages/LiveView/StorageLiveView.cpp index 06340dfac38..6118ef26bba 100644 --- a/dbms/src/Storages/LiveView/StorageLiveView.cpp +++ b/dbms/src/Storages/LiveView/StorageLiveView.cpp @@ -37,7 +37,7 @@ limitations under the License. */ #include #include #include -#include +#include #include namespace DB diff --git a/dbms/src/Storages/StorageMaterializedView.cpp b/dbms/src/Storages/StorageMaterializedView.cpp index 24a6461bdab..f4021284bc8 100644 --- a/dbms/src/Storages/StorageMaterializedView.cpp +++ b/dbms/src/Storages/StorageMaterializedView.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include