2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTWithAlias.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/Operators.h>
|
2016-11-20 12:43:20 +00:00
|
|
|
|
2015-08-06 21:32:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-03-18 00:57:00 +00:00
|
|
|
static void writeAlias(const String & name, const ASTWithAlias::FormatSettings & settings)
|
2018-07-16 01:02:46 +00:00
|
|
|
{
|
2020-03-18 00:57:00 +00:00
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " AS " << (settings.hilite ? IAST::hilite_alias : "");
|
2018-08-07 13:58:11 +00:00
|
|
|
settings.writeIdentifier(name);
|
2020-03-18 00:57:00 +00:00
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_none : "");
|
2018-07-16 01:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-06 21:32:51 +00:00
|
|
|
void ASTWithAlias::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
|
|
|
{
|
2019-06-29 16:13:28 +00:00
|
|
|
/// If we have previously output this node elsewhere in the query, now it is enough to output only the alias.
|
|
|
|
/// This is needed because the query can become extraordinary large after substitution of aliases.
|
2019-06-30 12:49:06 +00:00
|
|
|
if (!alias.empty() && !state.printed_asts_with_alias.emplace(frame.current_select, alias, getTreeHash()).second)
|
2019-06-29 16:13:28 +00:00
|
|
|
{
|
2023-03-15 21:20:56 +00:00
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_identifier : "");
|
2019-06-29 16:13:28 +00:00
|
|
|
settings.writeIdentifier(alias);
|
2023-03-15 21:20:56 +00:00
|
|
|
settings.ostr << (settings.hilite ? IAST::hilite_none : "");
|
2019-06-29 16:13:28 +00:00
|
|
|
}
|
|
|
|
else
|
2015-08-06 21:32:51 +00:00
|
|
|
{
|
2019-06-29 18:59:07 +00:00
|
|
|
/// If there is an alias, then parentheses are required around the entire expression, including the alias.
|
|
|
|
/// Because a record of the form `0 AS x + 0` is syntactically invalid.
|
|
|
|
if (frame.need_parens && !alias.empty())
|
|
|
|
settings.ostr << '(';
|
|
|
|
|
2019-06-30 12:49:06 +00:00
|
|
|
formatImplWithoutAlias(settings, state, frame);
|
2019-06-29 16:13:28 +00:00
|
|
|
|
|
|
|
if (!alias.empty())
|
|
|
|
{
|
|
|
|
writeAlias(alias, settings);
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << ')';
|
|
|
|
}
|
2015-08-06 21:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 16:34:11 +00:00
|
|
|
void ASTWithAlias::appendColumnName(WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
if (prefer_alias_to_column_name && !alias.empty())
|
|
|
|
writeString(alias, ostr);
|
|
|
|
else
|
|
|
|
appendColumnNameImpl(ostr);
|
|
|
|
}
|
|
|
|
|
2021-01-10 09:40:47 +00:00
|
|
|
void ASTWithAlias::appendColumnNameWithoutAlias(WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
appendColumnNameImpl(ostr);
|
|
|
|
}
|
|
|
|
|
2015-08-06 21:32:51 +00:00
|
|
|
}
|