ClickHouse/src/Parsers/ASTSubquery.cpp

79 lines
2.1 KiB
C++
Raw Normal View History

2017-07-26 01:21:19 +00:00
#include <Parsers/ASTSubquery.h>
2018-02-23 08:05:21 +00:00
#include <IO/WriteHelpers.h>
2020-11-09 16:05:40 +00:00
#include <IO/Operators.h>
2021-01-02 04:51:13 +00:00
#include <Common/SipHash.h>
2017-07-26 01:21:19 +00:00
namespace DB
{
void ASTSubquery::appendColumnNameImpl(WriteBuffer & ostr) const
2017-07-26 01:21:19 +00:00
{
2018-02-23 11:35:05 +00:00
/// This is a hack. We use alias, if available, because otherwise tree could change during analysis.
if (!alias.empty())
2018-09-10 03:59:48 +00:00
{
writeString(alias, ostr);
2018-09-10 03:59:48 +00:00
}
2021-01-02 04:51:13 +00:00
else if (!cte_name.empty())
{
writeString(cte_name, ostr);
}
2018-09-10 03:59:48 +00:00
else
{
2023-07-06 00:35:44 +00:00
const auto hash = getTreeHash();
2018-09-10 03:59:48 +00:00
writeCString("__subquery_", ostr);
2023-07-06 00:35:44 +00:00
writeString(toString(hash), ostr);
2018-09-10 03:59:48 +00:00
}
}
void ASTSubquery::formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
/// NOTE: due to trickery of filling cte_name (in interpreters) it is hard
2022-04-17 23:02:49 +00:00
/// to print it without newline (for !oneline case), since if nl_or_ws
/// prepended here, then formatting will be incorrect with alias:
///
/// (select 1 in ((select 1) as sub))
2021-01-02 04:51:13 +00:00
if (!cte_name.empty())
{
settings.ostr << (settings.hilite ? hilite_identifier : "");
settings.writeIdentifier(cte_name);
settings.ostr << (settings.hilite ? hilite_none : "");
return;
}
std::string indent_str = settings.one_line ? "" : std::string(4u * frame.indent, ' ');
std::string nl_or_nothing = settings.one_line ? "" : "\n";
settings.ostr << "(" << nl_or_nothing;
FormatStateStacked frame_nested = frame;
frame_nested.need_parens = false;
++frame_nested.indent;
children[0]->formatImpl(settings, state, frame_nested);
settings.ostr << nl_or_nothing << indent_str << ")";
2017-07-26 01:21:19 +00:00
}
2021-01-02 04:51:13 +00:00
void ASTSubquery::updateTreeHashImpl(SipHash & hash_state) const
{
if (!cte_name.empty())
hash_state.update(cte_name);
IAST::updateTreeHashImpl(hash_state);
}
String ASTSubquery::getAliasOrColumnName() const
{
if (!alias.empty())
return alias;
if (!cte_name.empty())
return cte_name;
return getColumnName();
}
String ASTSubquery::tryGetAlias() const
{
if (!alias.empty())
return alias;
return cte_name;
}
2017-07-26 01:21:19 +00:00
}