2019-01-14 18:15:04 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2019-08-08 20:02:30 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromOStream.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2020-03-13 10:30:55 +00:00
|
|
|
#include <Interpreters/StorageID.h>
|
2015-11-08 01:29:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-08-08 20:02:30 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNEXPECTED_AST_STRUCTURE;
|
2020-04-07 14:05:51 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2019-08-08 20:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-13 15:18:02 +00:00
|
|
|
ASTPtr ASTIdentifier::clone() const
|
|
|
|
{
|
|
|
|
auto ret = std::make_shared<ASTIdentifier>(*this);
|
|
|
|
ret->semantic = std::make_shared<IdentifierSemanticImpl>(*ret->semantic);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-25 15:42:24 +00:00
|
|
|
std::shared_ptr<ASTIdentifier> ASTIdentifier::createSpecial(const String & name, std::vector<String> && name_parts)
|
|
|
|
{
|
|
|
|
auto ret = std::make_shared<ASTIdentifier>(name, std::move(name_parts));
|
|
|
|
ret->semantic->special = true;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTIdentifier::ASTIdentifier(const String & name_, std::vector<String> && name_parts_)
|
|
|
|
: name(name_)
|
|
|
|
, name_parts(name_parts_)
|
|
|
|
, semantic(std::make_shared<IdentifierSemanticImpl>())
|
|
|
|
{
|
2020-03-08 21:53:03 +00:00
|
|
|
if (!name_parts.empty() && name_parts[0].empty())
|
2019-10-17 21:08:28 +00:00
|
|
|
name_parts.erase(name_parts.begin());
|
|
|
|
|
2020-03-08 21:53:03 +00:00
|
|
|
if (name.empty())
|
2019-10-17 21:08:28 +00:00
|
|
|
{
|
|
|
|
if (name_parts.size() == 2)
|
|
|
|
name = name_parts[0] + '.' + name_parts[1];
|
|
|
|
else if (name_parts.size() == 1)
|
|
|
|
name = name_parts[0];
|
|
|
|
}
|
2019-01-25 15:42:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:40:58 +00:00
|
|
|
ASTIdentifier::ASTIdentifier(std::vector<String> && name_parts_)
|
2019-10-17 21:08:28 +00:00
|
|
|
: ASTIdentifier("", std::move(name_parts_))
|
2019-03-04 19:40:58 +00:00
|
|
|
{}
|
|
|
|
|
2019-02-12 15:08:21 +00:00
|
|
|
void ASTIdentifier::setShortName(const String & new_name)
|
|
|
|
{
|
|
|
|
name = new_name;
|
|
|
|
name_parts.clear();
|
|
|
|
|
2019-02-13 15:18:02 +00:00
|
|
|
bool special = semantic->special;
|
|
|
|
*semantic = IdentifierSemanticImpl();
|
|
|
|
semantic->special = special;
|
2019-02-12 15:08:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 17:43:42 +00:00
|
|
|
void ASTIdentifier::restoreCompoundName()
|
|
|
|
{
|
|
|
|
if (name_parts.empty())
|
|
|
|
return;
|
|
|
|
name = name_parts[0];
|
|
|
|
for (size_t i = 1; i < name_parts.size(); ++i)
|
|
|
|
name += '.' + name_parts[i];
|
|
|
|
}
|
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
void ASTIdentifier::formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const
|
2015-11-08 01:29:37 +00:00
|
|
|
{
|
2018-08-26 02:19:18 +00:00
|
|
|
auto format_element = [&](const String & elem_name)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "");
|
2018-09-04 14:39:08 +00:00
|
|
|
settings.writeIdentifier(elem_name);
|
2017-04-01 07:20:54 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "");
|
|
|
|
};
|
|
|
|
|
2019-03-05 10:57:05 +00:00
|
|
|
/// It could be compound but short
|
|
|
|
if (!isShort())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-17 17:01:48 +00:00
|
|
|
for (size_t i = 0, size = name_parts.size(); i < size; ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
settings.ostr << '.';
|
|
|
|
|
2019-01-17 17:01:48 +00:00
|
|
|
format_element(name_parts[i]);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_element(name);
|
|
|
|
}
|
2015-11-08 01:29:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 16:34:11 +00:00
|
|
|
void ASTIdentifier::appendColumnNameImpl(WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
writeString(name, ostr);
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:07:10 +00:00
|
|
|
void ASTIdentifier::resetTable(const String & database_name, const String & table_name)
|
|
|
|
{
|
|
|
|
auto ast = createTableIdentifier(database_name, table_name);
|
|
|
|
auto & ident = ast->as<ASTIdentifier &>();
|
|
|
|
name.swap(ident.name);
|
|
|
|
name_parts.swap(ident.name_parts);
|
|
|
|
uuid = ident.uuid;
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:28:17 +00:00
|
|
|
ASTPtr createTableIdentifier(const String & database_name, const String & table_name)
|
|
|
|
{
|
2020-03-12 18:04:29 +00:00
|
|
|
assert(database_name != "_temporary_and_external_tables");
|
|
|
|
return createTableIdentifier(StorageID(database_name, table_name));
|
|
|
|
}
|
2019-01-15 12:28:17 +00:00
|
|
|
|
2020-03-12 18:04:29 +00:00
|
|
|
ASTPtr createTableIdentifier(const StorageID & table_id)
|
|
|
|
{
|
|
|
|
std::shared_ptr<ASTIdentifier> res;
|
|
|
|
if (table_id.database_name.empty())
|
|
|
|
res = ASTIdentifier::createSpecial(table_id.table_name);
|
|
|
|
else
|
|
|
|
res = ASTIdentifier::createSpecial(table_id.database_name + "." + table_id.table_name, {table_id.database_name, table_id.table_name});
|
|
|
|
res->uuid = table_id.uuid;
|
|
|
|
return res;
|
2019-01-15 12:28:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 20:02:30 +00:00
|
|
|
String getIdentifierName(const IAST * ast)
|
2019-01-14 18:15:04 +00:00
|
|
|
{
|
2019-08-08 20:02:30 +00:00
|
|
|
String res;
|
|
|
|
if (tryGetIdentifierNameInto(ast, res))
|
|
|
|
return res;
|
2019-08-08 20:26:42 +00:00
|
|
|
throw Exception(ast ? queryToString(*ast) + " is not an identifier" : "AST node is nullptr", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
|
2019-08-08 20:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<String> tryGetIdentifierName(const IAST * ast)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
if (tryGetIdentifierNameInto(ast, res))
|
|
|
|
return res;
|
2019-01-14 18:15:04 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:26:42 +00:00
|
|
|
bool tryGetIdentifierNameInto(const IAST * ast, String & name)
|
2019-01-14 18:15:04 +00:00
|
|
|
{
|
|
|
|
if (ast)
|
2019-08-08 20:02:30 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * node = ast->as<ASTIdentifier>())
|
2019-01-14 18:15:04 +00:00
|
|
|
{
|
|
|
|
name = node->name;
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-08 20:02:30 +00:00
|
|
|
}
|
2019-01-14 18:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setIdentifierSpecial(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
if (ast)
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * id = ast->as<ASTIdentifier>())
|
2019-01-25 15:42:24 +00:00
|
|
|
id->semantic->special = true;
|
2019-01-16 17:26:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:05:51 +00:00
|
|
|
StorageID getTableIdentifier(const ASTPtr & ast)
|
|
|
|
{
|
|
|
|
if (!ast)
|
|
|
|
throw Exception("AST node is nullptr", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
|
|
|
|
const auto & identifier = dynamic_cast<const ASTIdentifier &>(*ast);
|
|
|
|
if (identifier.name_parts.size() > 2)
|
|
|
|
throw Exception("Logical error: more than two components in table expression", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
if (identifier.name_parts.size() == 2)
|
|
|
|
return { identifier.name_parts[0], identifier.name_parts[1], identifier.uuid };
|
|
|
|
return { "", identifier.name, identifier.uuid };
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:29:37 +00:00
|
|
|
}
|