1#include <Interpreters/InterpreterExplainQuery.h>
2
3#include <DataStreams/BlockIO.h>
4#include <DataStreams/OneBlockInputStream.h>
5#include <DataTypes/DataTypeString.h>
6#include <Interpreters/InterpreterSelectWithUnionQuery.h>
7#include <Parsers/ASTExplainQuery.h>
8#include <Parsers/DumpASTNode.h>
9#include <Parsers/queryToString.h>
10#include <Common/typeid_cast.h>
11#include <Core/Field.h>
12
13#include <sstream>
14
15
16namespace DB
17{
18
19BlockIO InterpreterExplainQuery::execute()
20{
21 BlockIO res;
22 res.in = executeImpl();
23 return res;
24}
25
26
27Block InterpreterExplainQuery::getSampleBlock()
28{
29 Block block;
30
31 ColumnWithTypeAndName col;
32 col.name = "explain";
33 col.type = std::make_shared<DataTypeString>();
34 col.column = col.type->createColumn();
35 block.insert(col);
36
37 return block;
38}
39
40
41BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
42{
43 const auto & ast = query->as<ASTExplainQuery &>();
44 Block sample_block = getSampleBlock();
45 MutableColumns res_columns = sample_block.cloneEmptyColumns();
46
47 std::stringstream ss;
48
49 if (ast.getKind() == ASTExplainQuery::ParsedAST)
50 {
51 dumpAST(ast, ss);
52 }
53 else if (ast.getKind() == ASTExplainQuery::AnalyzedSyntax)
54 {
55 InterpreterSelectWithUnionQuery interpreter(ast.children.at(0), context,
56 SelectQueryOptions(QueryProcessingStage::FetchColumns).analyze().modify());
57 interpreter.getQuery()->format(IAST::FormatSettings(ss, false));
58 }
59
60 res_columns[0]->insert(ss.str());
61
62 return std::make_shared<OneBlockInputStream>(sample_block.cloneWithColumns(std::move(res_columns)));
63}
64
65}
66