1#pragma once
2
3#include <optional>
4
5#include <Parsers/ASTWithAlias.h>
6
7
8namespace DB
9{
10
11struct IdentifierSemantic;
12struct IdentifierSemanticImpl;
13struct DatabaseAndTableWithAlias;
14
15
16/// Identifier (column, table or alias)
17class ASTIdentifier : public ASTWithAlias
18{
19public:
20 /// The composite identifier will have a concatenated name (of the form a.b.c),
21 /// and individual components will be available inside the name_parts.
22 String name;
23
24 ASTIdentifier(const String & name_, std::vector<String> && name_parts_ = {});
25 ASTIdentifier(std::vector<String> && name_parts_);
26
27 /** Get the text that identifies this element. */
28 String getID(char delim) const override { return "Identifier" + (delim + name); }
29
30 ASTPtr clone() const override;
31
32 void collectIdentifierNames(IdentifierNameSet & set) const override
33 {
34 set.insert(name);
35 }
36
37 bool compound() const { return !name_parts.empty(); }
38 bool isShort() const { return name_parts.empty() || name == name_parts.back(); }
39
40 void setShortName(const String & new_name);
41 void restoreCompoundName();
42
43 const String & shortName() const
44 {
45 if (!name_parts.empty())
46 return name_parts.back();
47 return name;
48 }
49
50protected:
51 void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
52 void appendColumnNameImpl(WriteBuffer & ostr) const override;
53
54private:
55 using ASTWithAlias::children; /// ASTIdentifier is child free
56
57 std::vector<String> name_parts;
58 std::shared_ptr<IdentifierSemanticImpl> semantic; /// pimpl
59
60 static std::shared_ptr<ASTIdentifier> createSpecial(const String & name, std::vector<String> && name_parts = {});
61
62 friend struct IdentifierSemantic;
63 friend ASTPtr createTableIdentifier(const String & database_name, const String & table_name);
64 friend void setIdentifierSpecial(ASTPtr & ast);
65};
66
67
68/// ASTIdentifier Helpers: hide casts and semantic.
69
70ASTPtr createTableIdentifier(const String & database_name, const String & table_name);
71void setIdentifierSpecial(ASTPtr & ast);
72
73String getIdentifierName(const IAST * ast);
74std::optional<String> tryGetIdentifierName(const IAST * ast);
75bool tryGetIdentifierNameInto(const IAST * ast, String & name);
76
77inline String getIdentifierName(const ASTPtr & ast) { return getIdentifierName(ast.get()); }
78inline std::optional<String> tryGetIdentifierName(const ASTPtr & ast) { return tryGetIdentifierName(ast.get()); }
79inline bool tryGetIdentifierNameInto(const ASTPtr & ast, String & name) { return tryGetIdentifierNameInto(ast.get(), name); }
80
81}
82