1#pragma once
2
3#include <Parsers/IAST.h>
4
5
6namespace DB
7{
8
9
10/** Base class for AST, which can contain an alias (identifiers, literals, functions).
11 */
12class ASTWithAlias : public IAST
13{
14public:
15 /// The alias, if any, or an empty string.
16 String alias;
17 /// If is true, getColumnName returns alias. Uses for aliases in former WITH section of SELECT query.
18 /// Example: 'WITH pow(2, 2) as a SELECT pow(a, 2)' returns 'pow(a, 2)' instead of 'pow(pow(2, 2), 2)'
19 bool prefer_alias_to_column_name = false;
20
21 using IAST::IAST;
22
23 void appendColumnName(WriteBuffer & ostr) const final;
24 String getAliasOrColumnName() const override { return alias.empty() ? getColumnName() : alias; }
25 String tryGetAlias() const override { return alias; }
26 void setAlias(const String & to) override { alias = to; }
27
28 /// Calls formatImplWithoutAlias, and also outputs an alias. If necessary, encloses the entire expression in brackets.
29 void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override final;
30
31 virtual void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const = 0;
32
33protected:
34 virtual void appendColumnNameImpl(WriteBuffer & ostr) const = 0;
35
36 void writeAlias(const String & name, const FormatSettings & settings) const;
37};
38
39/// helper for setting aliases and chaining result to other functions
40inline ASTPtr setAlias(ASTPtr ast, const String & alias)
41{
42 ast->setAlias(alias);
43 return ast;
44}
45
46
47}
48