ClickHouse/src/Interpreters/StorageID.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.2 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
{
database_name = query.getDatabase();
table_name = query.getTable();
2020-02-17 19:28:25 +00:00
uuid = query.uuid;
assertNotEmpty();
}
2020-10-26 15:49:00 +00:00
StorageID::StorageID(const ASTTableIdentifier & table_identifier_node)
2020-03-13 10:30:55 +00:00
{
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-10-26 15:49:00 +00:00
if (const auto * identifier = node->as<ASTTableIdentifier>())
2020-03-13 10:30:55 +00:00
*this = StorageID(*identifier);
2020-11-18 14:48:24 +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(ErrorCodes::LOGICAL_ERROR, "Unexpected AST");
2020-03-13 10:30:55 +00:00
}
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(ErrorCodes::UNKNOWN_DATABASE, "Database name is empty");
2020-03-06 20:38:19 +00:00
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
}
/// NOTE: This implementation doesn't allow to implement a good "operator <".
/// Because "a != b" must be equivalent to "(a < b) || (b < a)", and we can't make "operator <" to meet that.
2021-06-06 22:55:37 +00:00
bool StorageID::operator==(const StorageID & rhs) const
{
assertNotEmpty();
if (hasUUID() && rhs.hasUUID())
return uuid == rhs.uuid;
else
return std::tie(database_name, table_name) == std::tie(rhs.database_name, rhs.table_name);
}
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;
}
2021-10-13 20:47:28 +00:00
String StorageID::getShortName() const
2020-07-15 19:25:31 +00:00
{
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
}