Merge pull request #33819 from bigo-sg/optimize_explain_create_function

Support explain create function query
This commit is contained in:
tavplubix 2022-01-20 19:07:43 +03:00 committed by GitHub
commit 41ee114abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 8 deletions

View File

@ -41,7 +41,7 @@ BlockIO InterpreterCreateFunctionQuery::execute()
auto & user_defined_function_factory = UserDefinedSQLFunctionFactory::instance(); 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 if_not_exists = create_function_query.if_not_exists;
bool replace = create_function_query.or_replace; bool replace = create_function_query.or_replace;

View File

@ -59,7 +59,7 @@ ASTPtr UserDefinedSQLFunctionMatcher::tryToReplaceFunction(const ASTFunction & f
if (function_arguments.size() != identifiers_raw.size()) if (function_arguments.size() != identifiers_raw.size())
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
"Function {} expects {} arguments actual arguments {}", "Function {} expects {} arguments actual arguments {}",
create_function_query->function_name, create_function_query->getFunctionName(),
identifiers_raw.size(), identifiers_raw.size(),
function_arguments.size()); function_arguments.size());

View File

@ -10,7 +10,15 @@ namespace DB
ASTPtr ASTCreateFunctionQuery::clone() const ASTPtr ASTCreateFunctionQuery::clone() const
{ {
return std::make_shared<ASTCreateFunctionQuery>(*this); auto res = std::make_shared<ASTCreateFunctionQuery>(*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 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_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); formatOnCluster(settings);
@ -35,4 +43,11 @@ void ASTCreateFunctionQuery::formatImpl(const IAST::FormatSettings & settings, I
function_core->formatImpl(settings, state, frame); function_core->formatImpl(settings, state, frame);
} }
String ASTCreateFunctionQuery::getFunctionName() const
{
String name;
tryGetIdentifierNameInto(function_name, name);
return name;
}
} }

View File

@ -10,19 +10,21 @@ namespace DB
class ASTCreateFunctionQuery : public IAST, public ASTQueryWithOnCluster class ASTCreateFunctionQuery : public IAST, public ASTQueryWithOnCluster
{ {
public: public:
String function_name; ASTPtr function_name;
ASTPtr function_core; ASTPtr function_core;
bool or_replace = false; bool or_replace = false;
bool if_not_exists = 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; ASTPtr clone() const override;
void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const override; void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const override;
ASTPtr getRewrittenASTWithoutOnCluster(const std::string &) const override { return removeOnCluster<ASTCreateFunctionQuery>(clone()); } ASTPtr getRewrittenASTWithoutOnCluster(const std::string &) const override { return removeOnCluster<ASTCreateFunctionQuery>(clone()); }
String getFunctionName() const;
}; };
} }

View File

@ -59,8 +59,12 @@ bool ParserCreateFunctionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Exp
auto create_function_query = std::make_shared<ASTCreateFunctionQuery>(); auto create_function_query = std::make_shared<ASTCreateFunctionQuery>();
node = create_function_query; node = create_function_query;
create_function_query->function_name = function_name->as<ASTIdentifier &>().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->function_core = function_core;
create_function_query->children.push_back(function_core);
create_function_query->or_replace = or_replace; create_function_query->or_replace = or_replace;
create_function_query->if_not_exists = if_not_exists; create_function_query->if_not_exists = if_not_exists;
create_function_query->cluster = std::move(cluster_str); create_function_query->cluster = std::move(cluster_str);

View File

@ -7,3 +7,14 @@ AlterQuery t1 (children 2)
Function today (children 1) Function today (children 1)
ExpressionList ExpressionList
Identifier t1 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

View File

@ -1,2 +1,3 @@
explain ast; -- { clientError 62 } 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;