1#include <Common/typeid_cast.h>
2#include <Parsers/ASTIdentifier.h>
3#include <Parsers/queryToString.h>
4#include <IO/WriteBufferFromOStream.h>
5#include <IO/WriteHelpers.h>
6#include <Interpreters/IdentifierSemantic.h>
7
8
9namespace DB
10{
11
12namespace ErrorCodes
13{
14 extern const int UNEXPECTED_AST_STRUCTURE;
15}
16
17
18ASTPtr ASTIdentifier::clone() const
19{
20 auto ret = std::make_shared<ASTIdentifier>(*this);
21 ret->semantic = std::make_shared<IdentifierSemanticImpl>(*ret->semantic);
22 return ret;
23}
24
25std::shared_ptr<ASTIdentifier> ASTIdentifier::createSpecial(const String & name, std::vector<String> && name_parts)
26{
27 auto ret = std::make_shared<ASTIdentifier>(name, std::move(name_parts));
28 ret->semantic->special = true;
29 return ret;
30}
31
32ASTIdentifier::ASTIdentifier(const String & name_, std::vector<String> && name_parts_)
33 : name(name_)
34 , name_parts(name_parts_)
35 , semantic(std::make_shared<IdentifierSemanticImpl>())
36{
37 if (name_parts.size() && name_parts[0] == "")
38 name_parts.erase(name_parts.begin());
39
40 if (name == "")
41 {
42 if (name_parts.size() == 2)
43 name = name_parts[0] + '.' + name_parts[1];
44 else if (name_parts.size() == 1)
45 name = name_parts[0];
46 }
47}
48
49ASTIdentifier::ASTIdentifier(std::vector<String> && name_parts_)
50 : ASTIdentifier("", std::move(name_parts_))
51{}
52
53void ASTIdentifier::setShortName(const String & new_name)
54{
55 name = new_name;
56 name_parts.clear();
57
58 bool special = semantic->special;
59 *semantic = IdentifierSemanticImpl();
60 semantic->special = special;
61}
62
63void ASTIdentifier::restoreCompoundName()
64{
65 if (name_parts.empty())
66 return;
67 name = name_parts[0];
68 for (size_t i = 1; i < name_parts.size(); ++i)
69 name += '.' + name_parts[i];
70}
71
72void ASTIdentifier::formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const
73{
74 auto format_element = [&](const String & elem_name)
75 {
76 settings.ostr << (settings.hilite ? hilite_identifier : "");
77 settings.writeIdentifier(elem_name);
78 settings.ostr << (settings.hilite ? hilite_none : "");
79 };
80
81 /// It could be compound but short
82 if (!isShort())
83 {
84 for (size_t i = 0, size = name_parts.size(); i < size; ++i)
85 {
86 if (i != 0)
87 settings.ostr << '.';
88
89 format_element(name_parts[i]);
90 }
91 }
92 else
93 {
94 format_element(name);
95 }
96}
97
98void ASTIdentifier::appendColumnNameImpl(WriteBuffer & ostr) const
99{
100 writeString(name, ostr);
101}
102
103ASTPtr createTableIdentifier(const String & database_name, const String & table_name)
104{
105 if (database_name.empty())
106 return ASTIdentifier::createSpecial(table_name);
107
108 ASTPtr database_and_table = ASTIdentifier::createSpecial(database_name + "." + table_name, {database_name, table_name});
109 return database_and_table;
110}
111
112String getIdentifierName(const IAST * ast)
113{
114 String res;
115 if (tryGetIdentifierNameInto(ast, res))
116 return res;
117 throw Exception(ast ? queryToString(*ast) + " is not an identifier" : "AST node is nullptr", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
118}
119
120std::optional<String> tryGetIdentifierName(const IAST * ast)
121{
122 String res;
123 if (tryGetIdentifierNameInto(ast, res))
124 return res;
125 return {};
126}
127
128bool tryGetIdentifierNameInto(const IAST * ast, String & name)
129{
130 if (ast)
131 {
132 if (const auto * node = ast->as<ASTIdentifier>())
133 {
134 name = node->name;
135 return true;
136 }
137 }
138 return false;
139}
140
141void setIdentifierSpecial(ASTPtr & ast)
142{
143 if (ast)
144 if (auto * id = ast->as<ASTIdentifier>())
145 id->semantic->special = true;
146}
147
148}
149