2014-07-03 22:39:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2014-07-03 22:39:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-08-06 23:46:15 +00:00
|
|
|
|
2017-05-27 17:27:16 +00:00
|
|
|
/** Base class for AST, which can contain an alias (identifiers, literals, functions).
|
2014-07-03 22:39:13 +00:00
|
|
|
*/
|
|
|
|
class ASTWithAlias : public IAST
|
|
|
|
{
|
|
|
|
public:
|
2017-05-27 17:27:16 +00:00
|
|
|
/// The alias, if any, or an empty string.
|
2014-07-03 22:39:13 +00:00
|
|
|
String alias;
|
2017-08-10 14:46:46 +00:00
|
|
|
/// If is true, getColumnName returns alias. Uses for aliases in former WITH section of SELECT query.
|
|
|
|
/// Example: 'WITH pow(2, 2) as a SELECT pow(a, 2)' returns 'pow(a, 2)' instead of 'pow(pow(2, 2), 2)'
|
|
|
|
bool prefer_alias_to_column_name = false;
|
2014-07-03 22:39:13 +00:00
|
|
|
|
|
|
|
using IAST::IAST;
|
|
|
|
|
2018-06-27 16:34:11 +00:00
|
|
|
void appendColumnName(WriteBuffer & ostr) const final;
|
2021-01-10 09:40:47 +00:00
|
|
|
void appendColumnNameWithoutAlias(WriteBuffer & ostr) const final;
|
2018-06-27 16:34:11 +00:00
|
|
|
String getAliasOrColumnName() const override { return alias.empty() ? getColumnName() : alias; }
|
2017-07-26 01:21:19 +00:00
|
|
|
String tryGetAlias() const override { return alias; }
|
|
|
|
void setAlias(const String & to) override { alias = to; }
|
2015-08-06 04:28:59 +00:00
|
|
|
|
2017-05-27 17:27:16 +00:00
|
|
|
/// Calls formatImplWithoutAlias, and also outputs an alias. If necessary, encloses the entire expression in brackets.
|
2023-11-10 12:15:23 +00:00
|
|
|
void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const final;
|
|
|
|
|
|
|
|
void updateTreeHashImpl(SipHash & hash_state, bool ignore_aliases) const override;
|
2015-08-06 04:28:59 +00:00
|
|
|
|
2015-08-06 23:46:15 +00:00
|
|
|
virtual void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const = 0;
|
2017-08-10 14:46:46 +00:00
|
|
|
|
|
|
|
protected:
|
2018-06-27 16:34:11 +00:00
|
|
|
virtual void appendColumnNameImpl(WriteBuffer & ostr) const = 0;
|
2014-07-03 22:39:13 +00:00
|
|
|
};
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
/// helper for setting aliases and chaining result to other functions
|
2015-08-06 23:46:15 +00:00
|
|
|
inline ASTPtr setAlias(ASTPtr ast, const String & alias)
|
|
|
|
{
|
2014-12-17 15:26:24 +00:00
|
|
|
ast->setAlias(alias);
|
2014-09-30 03:08:47 +00:00
|
|
|
return ast;
|
2018-08-10 04:02:56 +00:00
|
|
|
}
|
2014-09-30 03:08:47 +00:00
|
|
|
|
|
|
|
|
2014-07-03 22:39:13 +00:00
|
|
|
}
|