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>
|
2019-12-12 15:28:24 +00:00
|
|
|
#include <Interpreters/getTableExpressions.h>
|
|
|
|
|
2018-08-22 06:42:37 +00:00
|
|
|
#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
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2018-08-22 06:42:37 +00:00
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
DatabaseAndTableWithAlias::DatabaseAndTableWithAlias(const ASTTableIdentifier & identifier, const String & current_database)
|
2018-08-22 06:42:37 +00:00
|
|
|
{
|
2018-10-30 16:31:21 +00:00
|
|
|
alias = identifier.tryGetAlias();
|
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
auto table_id = identifier.getTableId();
|
2020-03-12 18:04:29 +00:00
|
|
|
std::tie(database, table, uuid) = std::tie(table_id.database_name, table_id.table_name, table_id.uuid);
|
2019-01-25 15:42:24 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-10-26 15:49:00 +00:00
|
|
|
const auto * identifier = node->as<ASTTableIdentifier>();
|
2019-01-14 18:15:04 +00:00
|
|
|
if (!identifier)
|
2020-10-26 15:49:00 +00:00
|
|
|
throw Exception("Logical error: table identifier expected", ErrorCodes::LOGICAL_ERROR);
|
2019-01-14 18:15:04 +00:00
|
|
|
|
|
|
|
*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)
|
2021-01-02 04:51:13 +00:00
|
|
|
{
|
|
|
|
const auto & cte_name = table_expression.subquery->as<const ASTSubquery &>().cte_name;
|
|
|
|
if (!cte_name.empty())
|
|
|
|
{
|
|
|
|
database = current_database;
|
|
|
|
table = cte_name;
|
|
|
|
}
|
2018-10-30 16:31:21 +00:00
|
|
|
alias = table_expression.subquery->tryGetAlias();
|
2021-01-02 04:51:13 +00:00
|
|
|
}
|
2018-10-30 16:31:21 +00:00
|
|
|
else
|
|
|
|
throw Exception("Logical error: no known elements in ASTTableExpression", ErrorCodes::LOGICAL_ERROR);
|
2018-08-22 06:42:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 06:01:33 +00:00
|
|
|
bool DatabaseAndTableWithAlias::satisfies(const DatabaseAndTableWithAlias & db_table, bool table_may_be_an_alias) const
|
2019-01-16 12:08:43 +00:00
|
|
|
{
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:40:58 +00:00
|
|
|
String DatabaseAndTableWithAlias::getQualifiedNamePrefix(bool with_dot) const
|
2018-08-22 06:42:37 +00:00
|
|
|
{
|
2018-10-01 14:18:47 +00:00
|
|
|
if (alias.empty() && table.empty())
|
|
|
|
return "";
|
2019-03-04 19:40:58 +00:00
|
|
|
return (!alias.empty() ? alias : table) + (with_dot ? "." : "");
|
2018-08-22 06:42:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:21 +00:00
|
|
|
std::vector<DatabaseAndTableWithAlias> getDatabaseAndTables(const ASTSelectQuery & select_query, const String & current_database)
|
|
|
|
{
|
2019-12-12 15:28:24 +00:00
|
|
|
std::vector<const ASTTableExpression *> tables_expression = getTableExpressions(select_query);
|
2018-10-30 16:31:21 +00:00
|
|
|
|
|
|
|
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;
|
2020-10-26 15:49:00 +00:00
|
|
|
if (!database_and_table_name || !database_and_table_name->as<ASTTableIdentifier>())
|
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
|
|
|
}
|
|
|
|
|
2018-08-24 03:32:20 +00:00
|
|
|
}
|