ClickHouse/dbms/src/Interpreters/InterpreterExplainQuery.cpp

65 lines
1.6 KiB
C++
Raw Normal View History

#include <Interpreters/InterpreterExplainQuery.h>
2018-09-21 15:20:23 +00:00
#include <DataStreams/BlockIO.h>
#include <DataStreams/OneBlockInputStream.h>
2018-09-21 15:20:23 +00:00
#include <DataTypes/DataTypeString.h>
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
2018-09-21 15:20:23 +00:00
#include <Parsers/ASTExplainQuery.h>
#include <Parsers/DumpASTNode.h>
#include <Parsers/queryToString.h>
#include <Common/typeid_cast.h>
#include <sstream>
2018-09-21 15:20:23 +00:00
namespace DB
{
BlockIO InterpreterExplainQuery::execute()
{
BlockIO res;
res.in = executeImpl();
return res;
}
Block InterpreterExplainQuery::getSampleBlock()
{
Block block;
ColumnWithTypeAndName col;
col.name = "explain";
2018-09-21 15:20:23 +00:00
col.type = std::make_shared<DataTypeString>();
col.column = col.type->createColumn();
block.insert(col);
return block;
}
BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
{
const auto & ast = query->as<ASTExplainQuery &>();
Block sample_block = getSampleBlock();
MutableColumns res_columns = sample_block.cloneEmptyColumns();
2018-09-21 15:20:23 +00:00
std::stringstream ss;
if (ast.getKind() == ASTExplainQuery::ParsedAST)
{
dumpAST(ast, ss);
}
else if (ast.getKind() == ASTExplainQuery::AnalyzedSyntax)
{
InterpreterSelectWithUnionQuery interpreter(ast.children.at(0), context,
2019-03-15 15:57:18 +00:00
modify(analyze(SelectQueryOptions(QueryProcessingStage::FetchColumns))));
interpreter.getQuery()->format(IAST::FormatSettings(ss, false));
}
2018-09-21 15:20:23 +00:00
res_columns[0]->insert(ss.str());
return std::make_shared<OneBlockInputStream>(sample_block.cloneWithColumns(std::move(res_columns)));
}
}