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 ...'
|
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)); }
|
2019-02-11 19:53:55 +00:00
|
|
|
ExplainKind getKind() const { return kind; }
|
2020-06-02 13:56:10 +00:00
|
|
|
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);
|
2020-06-02 13:56:10 +00:00
|
|
|
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-22 13:26:55 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << toString(kind, old_syntax) << (settings.hilite ? hilite_none : "") << " ";
|
2020-06-23 11:26:06 +00:00
|
|
|
ast_settings->formatImpl(settings, state, frame);
|
|
|
|
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";
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|