2019-01-25 15:42:24 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2020-10-24 18:46:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#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>
|
2020-10-24 18:46:10 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/Operators.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-11-02 10:03:52 +00:00
|
|
|
ASTIdentifier::ASTIdentifier(const String & short_name, ASTPtr && name_param)
|
2020-10-24 18:46:10 +00:00
|
|
|
: full_name(short_name), name_parts{short_name}, semantic(std::make_shared<IdentifierSemanticImpl>())
|
|
|
|
{
|
2021-04-15 07:33:15 +00:00
|
|
|
if (!name_param)
|
2020-11-02 10:03:52 +00:00
|
|
|
assert(!full_name.empty());
|
|
|
|
else
|
|
|
|
children.push_back(std::move(name_param));
|
2020-10-24 18:46:10 +00:00
|
|
|
}
|
2019-08-08 20:02:30 +00:00
|
|
|
|
2022-10-13 19:50:11 +00:00
|
|
|
ASTIdentifier::ASTIdentifier(std::vector<String> && name_parts_, bool special, ASTs && name_params)
|
2020-10-24 18:46:10 +00:00
|
|
|
: name_parts(name_parts_), semantic(std::make_shared<IdentifierSemanticImpl>())
|
2019-02-13 15:18:02 +00:00
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
assert(!name_parts.empty());
|
|
|
|
semantic->special = special;
|
|
|
|
semantic->legacy_compound = true;
|
2020-11-02 10:03:52 +00:00
|
|
|
if (!name_params.empty())
|
|
|
|
{
|
2022-08-01 16:00:54 +00:00
|
|
|
[[maybe_unused]] size_t params = 0;
|
2020-11-02 10:03:52 +00:00
|
|
|
for (const auto & part [[maybe_unused]] : name_parts)
|
|
|
|
{
|
|
|
|
if (part.empty())
|
|
|
|
++params;
|
|
|
|
}
|
|
|
|
assert(params == name_params.size());
|
|
|
|
children = std::move(name_params);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto & part [[maybe_unused]] : name_parts)
|
|
|
|
assert(!part.empty());
|
2020-10-24 18:46:10 +00:00
|
|
|
|
2020-11-02 10:03:52 +00:00
|
|
|
if (!special && name_parts.size() >= 2)
|
|
|
|
semantic->table = name_parts.end()[-2];
|
2020-10-24 18:46:10 +00:00
|
|
|
|
2020-11-02 10:03:52 +00:00
|
|
|
resetFullName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ASTIdentifier::getParam() const
|
|
|
|
{
|
|
|
|
assert(full_name.empty() && children.size() == 1);
|
|
|
|
return children.front()->clone();
|
2019-02-13 15:18:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:46:10 +00:00
|
|
|
ASTPtr ASTIdentifier::clone() const
|
2019-01-25 15:42:24 +00:00
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
auto ret = std::make_shared<ASTIdentifier>(*this);
|
|
|
|
ret->semantic = std::make_shared<IdentifierSemanticImpl>(*ret->semantic);
|
2022-11-16 10:22:28 +00:00
|
|
|
ret->cloneChildren();
|
2019-01-25 15:42:24 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:46:10 +00:00
|
|
|
bool ASTIdentifier::supposedToBeCompound() const
|
2019-01-25 15:42:24 +00:00
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
return semantic->legacy_compound;
|
2019-01-25 15:42:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:08:21 +00:00
|
|
|
void ASTIdentifier::setShortName(const String & new_name)
|
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
assert(!new_name.empty());
|
|
|
|
|
|
|
|
full_name = new_name;
|
|
|
|
name_parts = {new_name};
|
2019-02-12 15:08:21 +00:00
|
|
|
|
2019-02-13 15:18:02 +00:00
|
|
|
bool special = semantic->special;
|
2021-01-20 15:34:17 +00:00
|
|
|
auto table = semantic->table;
|
|
|
|
|
2019-02-13 15:18:02 +00:00
|
|
|
*semantic = IdentifierSemanticImpl();
|
|
|
|
semantic->special = special;
|
2021-01-20 15:34:17 +00:00
|
|
|
semantic->table = table;
|
2019-02-12 15:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:46:10 +00:00
|
|
|
const String & ASTIdentifier::name() const
|
2019-07-26 17:43:42 +00:00
|
|
|
{
|
2020-11-02 10:03:52 +00:00
|
|
|
if (children.empty())
|
|
|
|
{
|
|
|
|
assert(!name_parts.empty());
|
|
|
|
assert(!full_name.empty());
|
|
|
|
}
|
2020-10-24 18:46:10 +00:00
|
|
|
|
|
|
|
return full_name;
|
2019-07-26 17:43:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:52 +00:00
|
|
|
void ASTIdentifier::formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
2015-11-08 01:29:37 +00:00
|
|
|
{
|
2018-08-26 02:19:18 +00:00
|
|
|
auto format_element = [&](const String & elem_name)
|
2015-11-08 01:29:37 +00:00
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "");
|
2018-09-04 14:39:08 +00:00
|
|
|
settings.writeIdentifier(elem_name);
|
2015-11-08 01:29:37 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "");
|
|
|
|
};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-04-14 15:35:52 +00:00
|
|
|
if (compound())
|
2015-11-08 01:29:37 +00:00
|
|
|
{
|
2020-11-02 10:03:52 +00:00
|
|
|
for (size_t i = 0, j = 0, size = name_parts.size(); i < size; ++i)
|
2015-11-08 01:29:37 +00:00
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
settings.ostr << '.';
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-08 17:39:42 +00:00
|
|
|
/// Some AST rewriting code, like IdentifierSemantic::setColumnLongName,
|
2021-06-08 13:54:48 +00:00
|
|
|
/// does not respect children of identifier.
|
2021-06-08 15:23:42 +00:00
|
|
|
/// Here we also ignore children if they are empty.
|
2021-06-08 13:51:17 +00:00
|
|
|
if (name_parts[i].empty() && j < children.size())
|
2021-06-08 17:40:33 +00:00
|
|
|
{
|
|
|
|
children[j]->formatImpl(settings, state, frame);
|
|
|
|
++j;
|
|
|
|
}
|
2020-11-02 10:03:52 +00:00
|
|
|
else
|
|
|
|
format_element(name_parts[i]);
|
2015-11-08 01:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-02 10:03:52 +00:00
|
|
|
const auto & name = shortName();
|
2021-06-08 13:51:17 +00:00
|
|
|
if (name.empty() && !children.empty())
|
2020-11-02 10:03:52 +00:00
|
|
|
children.front()->formatImpl(settings, state, frame);
|
|
|
|
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
|
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
writeString(name(), ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTIdentifier::restoreTable()
|
|
|
|
{
|
|
|
|
if (!compound())
|
|
|
|
{
|
|
|
|
name_parts.insert(name_parts.begin(), semantic->table);
|
|
|
|
resetFullName();
|
|
|
|
}
|
2018-06-27 16:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:13:27 +00:00
|
|
|
std::shared_ptr<ASTTableIdentifier> ASTIdentifier::createTable() const
|
|
|
|
{
|
|
|
|
if (name_parts.size() == 1) return std::make_shared<ASTTableIdentifier>(name_parts[0]);
|
|
|
|
if (name_parts.size() == 2) return std::make_shared<ASTTableIdentifier>(name_parts[0], name_parts[1]);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
void ASTIdentifier::resetFullName()
|
2020-03-26 09:07:10 +00:00
|
|
|
{
|
2020-10-26 15:49:00 +00:00
|
|
|
full_name = name_parts[0];
|
|
|
|
for (size_t i = 1; i < name_parts.size(); ++i)
|
|
|
|
full_name += '.' + name_parts[i];
|
2020-03-26 09:07:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 19:50:11 +00:00
|
|
|
ASTTableIdentifier::ASTTableIdentifier(const String & table_name, ASTs && name_params)
|
2021-04-15 07:33:15 +00:00
|
|
|
: ASTIdentifier({table_name}, true, std::move(name_params))
|
2020-08-20 02:01:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:50:11 +00:00
|
|
|
ASTTableIdentifier::ASTTableIdentifier(const StorageID & table_id, ASTs && name_params)
|
2020-10-26 15:49:00 +00:00
|
|
|
: ASTIdentifier(
|
|
|
|
table_id.database_name.empty() ? std::vector<String>{table_id.table_name}
|
|
|
|
: std::vector<String>{table_id.database_name, table_id.table_name},
|
2021-04-15 07:33:15 +00:00
|
|
|
true, std::move(name_params))
|
2020-10-24 18:46:10 +00:00
|
|
|
{
|
2020-10-26 15:49:00 +00:00
|
|
|
uuid = table_id.uuid;
|
2020-10-24 18:46:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 19:50:11 +00:00
|
|
|
ASTTableIdentifier::ASTTableIdentifier(const String & database_name, const String & table_name, ASTs && name_params)
|
2021-04-15 07:33:15 +00:00
|
|
|
: ASTIdentifier({database_name, table_name}, true, std::move(name_params))
|
2019-01-15 12:28:17 +00:00
|
|
|
{
|
2020-03-12 18:04:29 +00:00
|
|
|
}
|
2019-01-15 12:28:17 +00:00
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
ASTPtr ASTTableIdentifier::clone() const
|
2020-03-12 18:04:29 +00:00
|
|
|
{
|
2020-10-26 15:49:00 +00:00
|
|
|
auto ret = std::make_shared<ASTTableIdentifier>(*this);
|
|
|
|
ret->semantic = std::make_shared<IdentifierSemanticImpl>(*ret->semantic);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageID ASTTableIdentifier::getTableId() const
|
|
|
|
{
|
|
|
|
if (name_parts.size() == 2) return {name_parts[0], name_parts[1], uuid};
|
|
|
|
else return {{}, name_parts[0], uuid};
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:32:23 +00:00
|
|
|
String ASTTableIdentifier::getDatabaseName() const
|
|
|
|
{
|
|
|
|
if (name_parts.size() == 2) return name_parts[0];
|
|
|
|
else return {};
|
|
|
|
}
|
|
|
|
|
2021-10-12 18:10:07 +00:00
|
|
|
ASTPtr ASTTableIdentifier::getTable() const
|
|
|
|
{
|
|
|
|
if (name_parts.size() == 2)
|
|
|
|
{
|
|
|
|
if (!name_parts[1].empty())
|
|
|
|
return std::make_shared<ASTIdentifier>(name_parts[1]);
|
|
|
|
|
|
|
|
if (name_parts[0].empty())
|
|
|
|
return std::make_shared<ASTIdentifier>("", children[1]->clone());
|
|
|
|
else
|
|
|
|
return std::make_shared<ASTIdentifier>("", children[0]->clone());
|
|
|
|
}
|
2021-11-15 10:05:24 +00:00
|
|
|
else if (name_parts.size() == 1)
|
2021-10-12 18:10:07 +00:00
|
|
|
{
|
|
|
|
if (name_parts[0].empty())
|
|
|
|
return std::make_shared<ASTIdentifier>("", children[0]->clone());
|
|
|
|
else
|
|
|
|
return std::make_shared<ASTIdentifier>(name_parts[0]);
|
|
|
|
}
|
2021-11-15 10:05:24 +00:00
|
|
|
else return {};
|
2021-10-12 18:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ASTTableIdentifier::getDatabase() const
|
|
|
|
{
|
|
|
|
if (name_parts.size() == 2)
|
|
|
|
{
|
|
|
|
if (name_parts[0].empty())
|
|
|
|
return std::make_shared<ASTIdentifier>("", children[0]->clone());
|
|
|
|
else
|
|
|
|
return std::make_shared<ASTIdentifier>(name_parts[0]);
|
|
|
|
}
|
|
|
|
else return {};
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:49:00 +00:00
|
|
|
void ASTTableIdentifier::resetTable(const String & database_name, const String & table_name)
|
|
|
|
{
|
|
|
|
auto identifier = std::make_shared<ASTTableIdentifier>(database_name, table_name);
|
|
|
|
full_name.swap(identifier->full_name);
|
|
|
|
name_parts.swap(identifier->name_parts);
|
|
|
|
uuid = identifier->uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTTableIdentifier::updateTreeHashImpl(SipHash & hash_state) const
|
|
|
|
{
|
|
|
|
hash_state.update(uuid);
|
|
|
|
IAST::updateTreeHashImpl(hash_state);
|
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;
|
2023-01-23 21:13:58 +00:00
|
|
|
if (ast)
|
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_AST_STRUCTURE, "{} is not an identifier", queryToString(*ast));
|
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_AST_STRUCTURE, "AST node is nullptr");
|
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
|
|
|
{
|
2020-11-13 14:34:47 +00:00
|
|
|
if (const auto * node = dynamic_cast<const ASTIdentifier *>(ast))
|
2019-01-14 18:15:04 +00:00
|
|
|
{
|
2020-10-24 18:46:10 +00:00
|
|
|
name = node->name();
|
2019-01-14 18:15:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-08 01:29:37 +00:00
|
|
|
}
|