ClickHouse/src/Interpreters/StorageID.cpp

115 lines
3.3 KiB
C++
Raw Normal View History

2020-03-13 10:30:55 +00:00
#include <Interpreters/StorageID.h>
2020-02-17 19:28:25 +00:00
#include <Parsers/ASTQueryWithTableAndOutput.h>
2020-03-13 10:30:55 +00:00
#include <Parsers/ASTIdentifier.h>
2020-02-17 19:28:25 +00:00
#include <Common/quoteString.h>
#include <IO/WriteHelpers.h>
2020-07-14 19:19:17 +00:00
#include <IO/ReadHelpers.h>
2020-03-04 20:29:52 +00:00
#include <Interpreters/DatabaseAndTableWithAlias.h>
2020-07-14 19:19:17 +00:00
#include <Poco/Util/AbstractConfiguration.h>
2020-02-17 19:28:25 +00:00
namespace DB
{
2020-03-11 19:10:55 +00:00
namespace ErrorCodes
{
2020-03-13 15:41:36 +00:00
extern const int LOGICAL_ERROR;
2020-03-11 19:10:55 +00:00
extern const int UNKNOWN_DATABASE;
}
2020-03-13 10:30:55 +00:00
StorageID::StorageID(const ASTQueryWithTableAndOutput & query)
2020-02-17 19:28:25 +00:00
{
2020-03-13 10:30:55 +00:00
database_name = query.database;
2020-02-17 19:28:25 +00:00
table_name = query.table;
uuid = query.uuid;
assertNotEmpty();
}
2020-03-13 10:30:55 +00:00
StorageID::StorageID(const ASTIdentifier & table_identifier_node)
{
DatabaseAndTableWithAlias database_table(table_identifier_node);
database_name = database_table.database;
table_name = database_table.table;
uuid = database_table.uuid;
assertNotEmpty();
}
StorageID::StorageID(const ASTPtr & node)
{
2020-04-22 06:01:33 +00:00
if (const auto * identifier = dynamic_cast<const ASTIdentifier *>(node.get()))
2020-03-13 10:30:55 +00:00
*this = StorageID(*identifier);
2020-04-22 06:01:33 +00:00
else if (const auto * simple_query = dynamic_cast<const ASTQueryWithTableAndOutput *>(node.get()))
2020-03-13 10:30:55 +00:00
*this = StorageID(*simple_query);
else
throw Exception("Unexpected AST", ErrorCodes::LOGICAL_ERROR);
}
String StorageID::getTableName() const
{
assertNotEmpty();
return table_name;
}
2020-03-06 20:38:19 +00:00
String StorageID::getDatabaseName() const
{
assertNotEmpty();
if (database_name.empty())
throw Exception("Database name is empty", ErrorCodes::UNKNOWN_DATABASE);
return database_name;
}
2020-02-17 19:28:25 +00:00
String StorageID::getNameForLogs() const
{
assertNotEmpty();
return (database_name.empty() ? "" : backQuoteIfNeed(database_name) + ".") + backQuoteIfNeed(table_name)
+ (hasUUID() ? " (" + toString(uuid) + ")" : "");
2020-02-17 19:28:25 +00:00
}
bool StorageID::operator<(const StorageID & rhs) const
{
assertNotEmpty();
/// It's needed for ViewDependencies
if (!hasUUID() && !rhs.hasUUID())
/// If both IDs don't have UUID, compare them like pair of strings
return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
else if (hasUUID() && rhs.hasUUID())
/// If both IDs have UUID, compare UUIDs and ignore database and table name
return uuid < rhs.uuid;
else
/// All IDs without UUID are less, then all IDs with UUID
return !hasUUID();
}
2020-03-06 20:38:19 +00:00
String StorageID::getFullTableName() const
{
return backQuoteIfNeed(getDatabaseName()) + "." + backQuoteIfNeed(table_name);
}
2020-07-15 19:25:31 +00:00
String StorageID::getFullNameNotQuoted() const
{
return getDatabaseName() + "." + table_name;
}
2020-07-14 19:19:17 +00:00
StorageID StorageID::fromDictionaryConfig(const Poco::Util::AbstractConfiguration & config,
const String & config_prefix)
{
StorageID res = StorageID::createEmpty();
res.database_name = config.getString(config_prefix + ".database", "");
res.table_name = config.getString(config_prefix + ".name");
const String uuid_str = config.getString(config_prefix + ".uuid", "");
if (!uuid_str.empty())
res.uuid = parseFromString<UUID>(uuid_str);
return res;
}
2020-07-15 19:25:31 +00:00
String StorageID::getInternalDictionaryName() const
{
assertNotEmpty();
if (hasUUID())
return toString(uuid);
if (database_name.empty())
return table_name;
return database_name + "." + table_name;
}
2020-02-17 19:28:25 +00:00
}