diff --git a/src/Parsers/ParserExplainQuery.cpp b/src/Parsers/ParserExplainQuery.cpp index fe48addd0cd..7a7abba94a9 100644 --- a/src/Parsers/ParserExplainQuery.cpp +++ b/src/Parsers/ParserExplainQuery.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace DB { @@ -51,7 +52,13 @@ bool ParserExplainQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected ParserCreateTableQuery create_p; ParserSelectWithUnionQuery select_p; ASTPtr query; - if (select_p.parse(pos, query, expected) || + if (kind == ASTExplainQuery::ExplainKind::ParsedAST) + { + ParserQuery p(end); + if (p.parse(pos, query, expected)) + explain_query->setExplainedQuery(std::move(query)); + } + else if (select_p.parse(pos, query, expected) || create_p.parse(pos, query, expected)) explain_query->setExplainedQuery(std::move(query)); else diff --git a/src/Parsers/ParserExplainQuery.h b/src/Parsers/ParserExplainQuery.h index b58597b9248..a1865e30239 100644 --- a/src/Parsers/ParserExplainQuery.h +++ b/src/Parsers/ParserExplainQuery.h @@ -9,8 +9,12 @@ namespace DB class ParserExplainQuery : public IParserBase { protected: + const char * end; + const char * getName() const override { return "EXPLAIN"; } bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; +public: + ParserExplainQuery(const char* end_) : end(end_) {} }; } diff --git a/src/Parsers/ParserQuery.cpp b/src/Parsers/ParserQuery.cpp index 89ddd1797c0..4550bdc8a75 100644 --- a/src/Parsers/ParserQuery.cpp +++ b/src/Parsers/ParserQuery.cpp @@ -26,7 +26,7 @@ namespace DB bool ParserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { - ParserQueryWithOutput query_with_output_p; + ParserQueryWithOutput query_with_output_p(end); ParserInsertQuery insert_p(end); ParserUseQuery use_p; ParserSetQuery set_p; diff --git a/src/Parsers/ParserQueryWithOutput.cpp b/src/Parsers/ParserQueryWithOutput.cpp index 76a3e8b95f8..d5aa1e47533 100644 --- a/src/Parsers/ParserQueryWithOutput.cpp +++ b/src/Parsers/ParserQueryWithOutput.cpp @@ -48,7 +48,7 @@ bool ParserQueryWithOutput::parseImpl(Pos & pos, ASTPtr & node, Expected & expec ParserShowCreateAccessEntityQuery show_create_access_entity_p; ParserShowGrantsQuery show_grants_p; ParserShowPrivilegesQuery show_privileges_p; - ParserExplainQuery explain_p; + ParserExplainQuery explain_p(end); ASTPtr query; diff --git a/src/Parsers/ParserQueryWithOutput.h b/src/Parsers/ParserQueryWithOutput.h index 110cf923147..854d5a74ffd 100644 --- a/src/Parsers/ParserQueryWithOutput.h +++ b/src/Parsers/ParserQueryWithOutput.h @@ -11,8 +11,11 @@ namespace DB class ParserQueryWithOutput : public IParserBase { protected: + const char * end; const char * getName() const override { return "Query with output"; } bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; +public: + ParserQueryWithOutput(const char * end_) : end(end_) {} }; } diff --git a/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.reference b/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.reference new file mode 100644 index 00000000000..e674ac5fabb --- /dev/null +++ b/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.reference @@ -0,0 +1,8 @@ +AlterQuery t1 (children 1) + ExpressionList (children 1) + AlterCommand 25 (children 1) + Function equals (children 1) + ExpressionList (children 2) + Identifier date + Function today (children 1) + ExpressionList diff --git a/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.sql b/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.sql new file mode 100644 index 00000000000..7805810dfca --- /dev/null +++ b/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.sql @@ -0,0 +1 @@ +explain ast alter table t1 delete where date = today(); \ No newline at end of file