2017-04-01 09:19:00 +00:00
# include <Parsers/ASTWithAlias.h>
# include <IO/WriteBufferFromOStream.h>
# include <IO/WriteHelpers.h>
2016-11-20 12:43:20 +00:00
2015-08-06 21:32:51 +00:00
namespace DB
{
void ASTWithAlias : : formatImpl ( const FormatSettings & settings , FormatState & state , FormatStateStacked frame ) const
{
2017-04-01 07:20:54 +00:00
if ( ! alias . empty ( ) )
{
2017-04-02 17:37:49 +00:00
/// If we have previously output this node elsewhere in the query, now it is enough to output only the alias.
2017-04-01 07:20:54 +00:00
if ( ! state . printed_asts_with_alias . emplace ( frame . current_select , alias ) . second )
{
WriteBufferFromOStream wb ( settings . ostr , 32 ) ;
writeProbablyBackQuotedString ( alias , wb ) ;
return ;
}
}
2015-08-06 21:32:51 +00:00
2017-04-02 17:37:49 +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.
2017-04-01 07:20:54 +00:00
if ( frame . need_parens & & ! alias . empty ( ) )
settings . ostr < < ' ( ' ;
2015-08-06 21:32:51 +00:00
2017-04-01 07:20:54 +00:00
formatImplWithoutAlias ( settings , state , frame ) ;
2015-08-06 21:32:51 +00:00
2017-04-01 07:20:54 +00:00
if ( ! alias . empty ( ) )
{
writeAlias ( alias , settings . ostr , settings . hilite ) ;
if ( frame . need_parens )
settings . ostr < < ' ) ' ;
}
2015-08-06 21:32:51 +00:00
}
}