From e4f5444073c48c994c2fc46a2ea26ab2e21e310c Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Thu, 20 Jan 2022 16:23:52 +0800 Subject: [PATCH 1/3] support explain create function query --- .../InterpreterCreateFunctionQuery.cpp | 2 +- src/Interpreters/InterpreterExplainQuery.cpp | 1 + .../UserDefinedSQLFunctionVisitor.cpp | 2 +- src/Parsers/ASTCreateFunctionQuery.cpp | 19 +++++++++++++++++-- src/Parsers/ASTCreateFunctionQuery.h | 6 ++++-- src/Parsers/ParserCreateFunctionQuery.cpp | 6 +++++- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Interpreters/InterpreterCreateFunctionQuery.cpp b/src/Interpreters/InterpreterCreateFunctionQuery.cpp index 2f345f8b237..20114fa0d75 100644 --- a/src/Interpreters/InterpreterCreateFunctionQuery.cpp +++ b/src/Interpreters/InterpreterCreateFunctionQuery.cpp @@ -41,7 +41,7 @@ BlockIO InterpreterCreateFunctionQuery::execute() auto & user_defined_function_factory = UserDefinedSQLFunctionFactory::instance(); - auto & function_name = create_function_query.function_name; + auto function_name = create_function_query.getFunctionName(); bool if_not_exists = create_function_query.if_not_exists; bool replace = create_function_query.or_replace; diff --git a/src/Interpreters/InterpreterExplainQuery.cpp b/src/Interpreters/InterpreterExplainQuery.cpp index 37b944d72d6..79f2233efa5 100644 --- a/src/Interpreters/InterpreterExplainQuery.cpp +++ b/src/Interpreters/InterpreterExplainQuery.cpp @@ -25,6 +25,7 @@ #include #include +#include "Parsers/formatAST.h" namespace DB { diff --git a/src/Interpreters/UserDefinedSQLFunctionVisitor.cpp b/src/Interpreters/UserDefinedSQLFunctionVisitor.cpp index 3e82930af9d..1adb3d5819a 100644 --- a/src/Interpreters/UserDefinedSQLFunctionVisitor.cpp +++ b/src/Interpreters/UserDefinedSQLFunctionVisitor.cpp @@ -59,7 +59,7 @@ ASTPtr UserDefinedSQLFunctionMatcher::tryToReplaceFunction(const ASTFunction & f if (function_arguments.size() != identifiers_raw.size()) throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Function {} expects {} arguments actual arguments {}", - create_function_query->function_name, + create_function_query->getFunctionName(), identifiers_raw.size(), function_arguments.size()); diff --git a/src/Parsers/ASTCreateFunctionQuery.cpp b/src/Parsers/ASTCreateFunctionQuery.cpp index 9209b1f1869..a12eca3d334 100644 --- a/src/Parsers/ASTCreateFunctionQuery.cpp +++ b/src/Parsers/ASTCreateFunctionQuery.cpp @@ -10,7 +10,15 @@ namespace DB ASTPtr ASTCreateFunctionQuery::clone() const { - return std::make_shared(*this); + auto res = std::make_shared(*this); + res->children.clear(); + + res->function_name = function_name->clone(); + res->children.push_back(res->function_name); + + res->function_core = function_core->clone(); + res->children.push_back(res->function_core); + return res; } void ASTCreateFunctionQuery::formatImpl(const IAST::FormatSettings & settings, IAST::FormatState & state, IAST::FormatStateStacked frame) const @@ -27,7 +35,7 @@ void ASTCreateFunctionQuery::formatImpl(const IAST::FormatSettings & settings, I settings.ostr << (settings.hilite ? hilite_none : ""); - settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(function_name) << (settings.hilite ? hilite_none : ""); + settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(getFunctionName()) << (settings.hilite ? hilite_none : ""); formatOnCluster(settings); @@ -35,4 +43,11 @@ void ASTCreateFunctionQuery::formatImpl(const IAST::FormatSettings & settings, I function_core->formatImpl(settings, state, frame); } +String ASTCreateFunctionQuery::getFunctionName() const +{ + String name; + tryGetIdentifierNameInto(function_name, name); + return name; +} + } diff --git a/src/Parsers/ASTCreateFunctionQuery.h b/src/Parsers/ASTCreateFunctionQuery.h index fccc1837135..b61441da6df 100644 --- a/src/Parsers/ASTCreateFunctionQuery.h +++ b/src/Parsers/ASTCreateFunctionQuery.h @@ -10,19 +10,21 @@ namespace DB class ASTCreateFunctionQuery : public IAST, public ASTQueryWithOnCluster { public: - String function_name; + ASTPtr function_name; ASTPtr function_core; bool or_replace = false; bool if_not_exists = false; - String getID(char) const override { return "CreateFunctionQuery"; } + String getID(char delim) const override { return "CreateFunctionQuery" + (delim + getFunctionName()); } ASTPtr clone() const override; void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const override; ASTPtr getRewrittenASTWithoutOnCluster(const std::string &) const override { return removeOnCluster(clone()); } + + String getFunctionName() const; }; } diff --git a/src/Parsers/ParserCreateFunctionQuery.cpp b/src/Parsers/ParserCreateFunctionQuery.cpp index 55e6e2df1d2..08df6d8da7a 100644 --- a/src/Parsers/ParserCreateFunctionQuery.cpp +++ b/src/Parsers/ParserCreateFunctionQuery.cpp @@ -59,8 +59,12 @@ bool ParserCreateFunctionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Exp auto create_function_query = std::make_shared(); node = create_function_query; - create_function_query->function_name = function_name->as().name(); + create_function_query->function_name = function_name; + create_function_query->children.push_back(function_name); + create_function_query->function_core = function_core; + create_function_query->children.push_back(function_core); + create_function_query->or_replace = or_replace; create_function_query->if_not_exists = if_not_exists; create_function_query->cluster = std::move(cluster_str); From 8886ac61e794c55824191ca26295868e2cb0a9e6 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Thu, 20 Jan 2022 19:33:19 +0800 Subject: [PATCH 2/3] remove useless code --- src/Interpreters/InterpreterExplainQuery.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Interpreters/InterpreterExplainQuery.cpp b/src/Interpreters/InterpreterExplainQuery.cpp index 79f2233efa5..37b944d72d6 100644 --- a/src/Interpreters/InterpreterExplainQuery.cpp +++ b/src/Interpreters/InterpreterExplainQuery.cpp @@ -25,7 +25,6 @@ #include #include -#include "Parsers/formatAST.h" namespace DB { From 4db3ee2edd7043b35797797a891acbbdeab3041c Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Thu, 20 Jan 2022 19:41:35 +0800 Subject: [PATCH 3/3] add stateless tests --- .../01604_explain_ast_of_nonselect_query.reference | 11 +++++++++++ .../01604_explain_ast_of_nonselect_query.sql | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) 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 index 4cc67aa517c..0bec8aabdb1 100644 --- a/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.reference +++ b/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.reference @@ -7,3 +7,14 @@ AlterQuery t1 (children 2) Function today (children 1) ExpressionList Identifier t1 +CreateFunctionQuery double (children 2) + Identifier double + Function lambda (children 1) + ExpressionList (children 2) + Function tuple (children 1) + ExpressionList (children 1) + Identifier n + Function multiply (children 1) + ExpressionList (children 2) + Literal UInt64_2 + Identifier n 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 index 41939123c92..6a4c065fd2c 100644 --- a/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.sql +++ b/tests/queries/0_stateless/01604_explain_ast_of_nonselect_query.sql @@ -1,2 +1,3 @@ explain ast; -- { clientError 62 } -explain ast alter table t1 delete where date = today() +explain ast alter table t1 delete where date = today(); +explain ast create function double AS (n) -> 2*n;