2018-10-30 16:31:21 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2018-08-22 06:42:37 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2018-10-26 15:13:02 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2018-10-29 19:04:28 +00:00
|
|
|
#include <Parsers/ASTSubquery.h>
|
2018-09-27 19:25:18 +00:00
|
|
|
|
2018-08-22 06:42:37 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
DatabaseAndTableWithAlias::DatabaseAndTableWithAlias(const ASTIdentifier & identifier, const String & current_database)
|
2018-08-22 06:42:37 +00:00
|
|
|
{
|
2018-10-30 16:31:21 +00:00
|
|
|
alias = identifier.tryGetAlias();
|
|
|
|
|
2019-01-25 15:42:24 +00:00
|
|
|
std::tie(database, table) = IdentifierSemantic::extractDatabaseAndTable(identifier);
|
|
|
|
if (database.empty())
|
|
|
|
database = current_database;
|
2018-10-30 16:31:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 18:15:04 +00:00
|
|
|
DatabaseAndTableWithAlias::DatabaseAndTableWithAlias(const ASTPtr & node, const String & current_database)
|
|
|
|
{
|
|
|
|
const auto * identifier = typeid_cast<const ASTIdentifier *>(node.get());
|
|
|
|
if (!identifier)
|
|
|
|
throw Exception("Logical error: identifier expected", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
*this = DatabaseAndTableWithAlias(*identifier, current_database);
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
DatabaseAndTableWithAlias::DatabaseAndTableWithAlias(const ASTTableExpression & table_expression, const String & current_database)
|
|
|
|
{
|
|
|
|
if (table_expression.database_and_table_name)
|
2019-01-14 18:15:04 +00:00
|
|
|
*this = DatabaseAndTableWithAlias(table_expression.database_and_table_name, current_database);
|
2018-10-30 16:31:21 +00:00
|
|
|
else if (table_expression.table_function)
|
|
|
|
alias = table_expression.table_function->tryGetAlias();
|
|
|
|
else if (table_expression.subquery)
|
|
|
|
alias = table_expression.subquery->tryGetAlias();
|
|
|
|
else
|
|
|
|
throw Exception("Logical error: no known elements in ASTTableExpression", ErrorCodes::LOGICAL_ERROR);
|
2018-08-22 06:42:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:08:43 +00:00
|
|
|
bool DatabaseAndTableWithAlias::satisfies(const DatabaseAndTableWithAlias & db_table, bool table_may_be_an_alias)
|
|
|
|
{
|
|
|
|
/// table.*, alias.* or database.table.*
|
|
|
|
|
|
|
|
if (database.empty())
|
|
|
|
{
|
|
|
|
if (!db_table.table.empty() && table == db_table.table)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!db_table.alias.empty())
|
|
|
|
return (alias == db_table.alias) || (table_may_be_an_alias && table == db_table.alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
return database == db_table.database && table == db_table.table;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:42:37 +00:00
|
|
|
String DatabaseAndTableWithAlias::getQualifiedNamePrefix() const
|
|
|
|
{
|
2018-10-01 14:18:47 +00:00
|
|
|
if (alias.empty() && table.empty())
|
|
|
|
return "";
|
2019-01-25 15:42:24 +00:00
|
|
|
return (!alias.empty() ? alias : table) + '.';
|
2018-08-22 06:42:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
std::vector<const ASTTableExpression *> getSelectTablesExpression(const ASTSelectQuery & select_query)
|
2018-10-26 15:13:02 +00:00
|
|
|
{
|
2018-10-30 16:31:21 +00:00
|
|
|
if (!select_query.tables)
|
2018-10-26 15:13:02 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<const ASTTableExpression *> tables_expression;
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
for (const auto & child : select_query.tables->children)
|
2018-10-26 15:13:02 +00:00
|
|
|
{
|
|
|
|
ASTTablesInSelectQueryElement * tables_element = static_cast<ASTTablesInSelectQueryElement *>(child.get());
|
|
|
|
|
|
|
|
if (tables_element->table_expression)
|
|
|
|
tables_expression.emplace_back(static_cast<const ASTTableExpression *>(tables_element->table_expression.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tables_expression;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:04:28 +00:00
|
|
|
static const ASTTableExpression * getTableExpression(const ASTSelectQuery & select, size_t table_number)
|
|
|
|
{
|
|
|
|
if (!select.tables)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ASTTablesInSelectQuery & tables_in_select_query = static_cast<ASTTablesInSelectQuery &>(*select.tables);
|
|
|
|
if (tables_in_select_query.children.size() <= table_number)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ASTTablesInSelectQueryElement & tables_element =
|
|
|
|
static_cast<ASTTablesInSelectQueryElement &>(*tables_in_select_query.children[table_number]);
|
2018-10-30 16:31:21 +00:00
|
|
|
|
2018-10-29 19:04:28 +00:00
|
|
|
if (!tables_element.table_expression)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return static_cast<const ASTTableExpression *>(tables_element.table_expression.get());
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
std::vector<DatabaseAndTableWithAlias> getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database)
|
|
|
|
{
|
|
|
|
std::vector<const ASTTableExpression *> tables_expression = getSelectTablesExpression(select_query);
|
|
|
|
|
|
|
|
std::vector<DatabaseAndTableWithAlias> database_and_table_with_aliases;
|
|
|
|
database_and_table_with_aliases.reserve(tables_expression.size());
|
|
|
|
|
|
|
|
for (const auto & table_expression : tables_expression)
|
|
|
|
database_and_table_with_aliases.emplace_back(DatabaseAndTableWithAlias(*table_expression, current_database));
|
|
|
|
|
|
|
|
return database_and_table_with_aliases;
|
|
|
|
}
|
|
|
|
|
2018-11-01 11:32:44 +00:00
|
|
|
std::optional<DatabaseAndTableWithAlias> getDatabaseAndTable(const ASTSelectQuery & select, size_t table_number)
|
2018-10-29 19:04:28 +00:00
|
|
|
{
|
|
|
|
const ASTTableExpression * table_expression = getTableExpression(select, table_number);
|
|
|
|
if (!table_expression)
|
2018-10-30 16:31:21 +00:00
|
|
|
return {};
|
2018-10-29 19:04:28 +00:00
|
|
|
|
|
|
|
ASTPtr database_and_table_name = table_expression->database_and_table_name;
|
2019-01-14 18:15:04 +00:00
|
|
|
if (!database_and_table_name || !isIdentifier(database_and_table_name))
|
2018-10-30 16:31:21 +00:00
|
|
|
return {};
|
2018-10-29 19:04:28 +00:00
|
|
|
|
2019-01-14 18:15:04 +00:00
|
|
|
return DatabaseAndTableWithAlias(database_and_table_name);
|
2018-10-29 19:04:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 18:29:54 +00:00
|
|
|
ASTPtr extractTableExpression(const ASTSelectQuery & select, size_t table_number)
|
2018-10-29 19:04:28 +00:00
|
|
|
{
|
2019-01-15 18:29:54 +00:00
|
|
|
if (const ASTTableExpression * table_expression = getTableExpression(select, table_number))
|
2018-10-29 19:04:28 +00:00
|
|
|
{
|
|
|
|
if (table_expression->database_and_table_name)
|
2019-01-15 18:29:54 +00:00
|
|
|
return table_expression->database_and_table_name;
|
2018-10-29 19:04:28 +00:00
|
|
|
|
|
|
|
if (table_expression->table_function)
|
|
|
|
return table_expression->table_function;
|
|
|
|
|
|
|
|
if (table_expression->subquery)
|
|
|
|
return static_cast<const ASTSubquery *>(table_expression->subquery.get())->children[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-24 03:32:20 +00:00
|
|
|
}
|