ClickHouse/src/Parsers/ASTExplainQuery.h

90 lines
2.4 KiB
C++
Raw Normal View History

2018-09-21 15:20:23 +00:00
#pragma once
2020-06-22 13:26:55 +00:00
#include <Parsers/ASTQueryWithOutput.h>
2018-09-21 15:20:23 +00:00
namespace DB
{
/// AST, EXPLAIN or other query with meaning of explanation query instead of execution
2020-06-22 13:26:55 +00:00
class ASTExplainQuery : public ASTQueryWithOutput
2018-09-21 15:20:23 +00:00
{
public:
enum ExplainKind
{
2020-06-22 13:26:55 +00:00
ParsedAST, /// 'EXPLAIN AST SELECT ...'
AnalyzedSyntax, /// 'EXPLAIN SYNTAX SELECT ...'
QueryPlan, /// 'EXPLAIN SELECT ...'
2020-06-25 09:39:17 +00:00
QueryPipeline, /// 'EXPLAIN PIPELINE ...'
2018-09-21 15:20:23 +00:00
};
2020-06-22 13:26:55 +00:00
ASTExplainQuery(ExplainKind kind_, bool old_syntax_)
: kind(kind_), old_syntax(old_syntax_)
{
}
2018-09-21 15:20:23 +00:00
2020-06-22 13:26:55 +00:00
String getID(char delim) const override { return "Explain" + (delim + toString(kind, old_syntax)); }
ExplainKind getKind() const { return kind; }
ASTPtr clone() const override
{
auto res = std::make_shared<ASTExplainQuery>(*this);
res->children.clear();
res->children.push_back(children[0]->clone());
2020-06-22 13:26:55 +00:00
cloneOutputOptions(*res);
return res;
}
2018-09-21 15:20:23 +00:00
2020-06-23 11:26:06 +00:00
void setExplainedQuery(ASTPtr query_)
{
children.emplace_back(query_);
query = std::move(query_);
}
void setSettings(ASTPtr settings_)
{
children.emplace_back(settings_);
ast_settings = std::move(settings_);
}
const ASTPtr & getExplainedQuery() const { return query; }
const ASTPtr & getSettings() const { return ast_settings; }
2020-06-22 13:26:55 +00:00
2018-09-21 15:20:23 +00:00
protected:
2020-06-22 13:26:55 +00:00
void formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override
2018-09-21 15:20:23 +00:00
{
2020-06-23 14:20:57 +00:00
settings.ostr << (settings.hilite ? hilite_keyword : "") << toString(kind, old_syntax) << (settings.hilite ? hilite_none : "");
if (ast_settings)
{
settings.ostr << ' ';
ast_settings->formatImpl(settings, state, frame);
}
2020-06-23 11:26:06 +00:00
settings.ostr << settings.nl_or_ws;
query->formatImpl(settings, state, frame);
2018-09-21 15:20:23 +00:00
}
private:
ExplainKind kind;
2020-06-22 13:26:55 +00:00
bool old_syntax; /// "EXPLAIN AST" -> "AST", "EXPLAIN SYNTAX" -> "ANALYZE"
2018-09-21 15:20:23 +00:00
2020-06-23 11:26:06 +00:00
ASTPtr query;
ASTPtr ast_settings;
2020-06-22 13:26:55 +00:00
static String toString(ExplainKind kind, bool old_syntax)
2018-09-21 15:20:23 +00:00
{
switch (kind)
{
2020-06-22 13:26:55 +00:00
case ParsedAST: return old_syntax ? "AST" : "EXPLAIN AST";
case AnalyzedSyntax: return old_syntax ? "ANALYZE" : "EXPLAIN SYNTAX";
case QueryPlan: return "EXPLAIN";
2020-06-25 09:39:17 +00:00
case QueryPipeline: return "EXPLAIN PIPELINE";
2018-09-21 15:20:23 +00:00
}
2019-02-15 10:15:56 +00:00
__builtin_unreachable();
2018-09-21 15:20:23 +00:00
}
};
}