mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 00:30:49 +00:00
Add validation of unknown identifiers in function
This commit is contained in:
parent
ca5ce92688
commit
ad4594c298
@ -16,6 +16,8 @@
|
||||
namespace DB
|
||||
{
|
||||
|
||||
class UserDefinedFunction;
|
||||
|
||||
namespace ErrorCodes
|
||||
{
|
||||
extern const int UNKNOWN_FUNCTION;
|
||||
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <Functions/IFunctionAdaptors.h>
|
||||
#include <Functions/UserDefinedFunction.h>
|
||||
#include <Interpreters/Context_fwd.h>
|
||||
#include <Common/IFactoryWithAliases.h>
|
||||
#include <Parsers/ASTCreateFunctionQuery.h>
|
||||
@ -15,8 +14,6 @@
|
||||
namespace DB
|
||||
{
|
||||
|
||||
class UserDefinedFunction;
|
||||
|
||||
/** Creates function by name.
|
||||
* Function could use for initialization (take ownership of shared_ptr, for example)
|
||||
* some dictionaries from Context.
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <Parsers/ASTIdentifier.h>
|
||||
#include <Interpreters/ExpressionActions.h>
|
||||
#include <Interpreters/ExpressionAnalyzer.h>
|
||||
#include <Interpreters/InterpreterCreateFunctionQuery.h>
|
||||
@ -7,12 +8,52 @@
|
||||
namespace DB
|
||||
{
|
||||
|
||||
namespace ErrorCodes
|
||||
{
|
||||
extern const int UNKNOWN_IDENTIFIER;
|
||||
}
|
||||
|
||||
BlockIO InterpreterCreateFunctionQuery::execute()
|
||||
{
|
||||
FunctionNameNormalizer().visit(query_ptr.get());
|
||||
auto & create_function_query = query_ptr->as<ASTCreateFunctionQuery &>();
|
||||
validateFunction(create_function_query.function_core);
|
||||
FunctionFactory::instance().registerUserDefinedFunction(create_function_query);
|
||||
return {};
|
||||
}
|
||||
|
||||
void InterpreterCreateFunctionQuery::validateFunction(ASTPtr function)
|
||||
{
|
||||
const auto * args_tuple = function->as<ASTFunction>()->arguments->children.at(0)->as<ASTFunction>();
|
||||
std::unordered_set<String> arguments;
|
||||
for (const auto & argument : args_tuple->arguments->children)
|
||||
arguments.insert(argument->as<ASTIdentifier>()->name());
|
||||
|
||||
std::vector<String> identifiers_in_body;
|
||||
ASTPtr function_body = function->as<ASTFunction>()->children.at(0)->children.at(1);
|
||||
getIdentifiers(function_body, identifiers_in_body);
|
||||
|
||||
for (const auto & identifier : identifiers_in_body)
|
||||
{
|
||||
if (!arguments.contains(identifier))
|
||||
{
|
||||
std::stringstream s;
|
||||
s << "Identifier '" << identifier << "' does not exist in arguments";
|
||||
throw Exception(s.str(), ErrorCodes::UNKNOWN_IDENTIFIER);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InterpreterCreateFunctionQuery::getIdentifiers(ASTPtr node, std::vector<String> & identifiers)
|
||||
{
|
||||
for (const auto & child : node->children)
|
||||
{
|
||||
auto identifier_name_opt = tryGetIdentifierName(child);
|
||||
if (identifier_name_opt)
|
||||
identifiers.push_back(identifier_name_opt.value());
|
||||
|
||||
getIdentifiers(child, identifiers);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,6 +15,10 @@ public:
|
||||
|
||||
BlockIO execute() override;
|
||||
|
||||
private:
|
||||
static void validateFunction(ASTPtr function);
|
||||
static void getIdentifiers(ASTPtr node, std::vector<String> & identifiers);
|
||||
|
||||
private:
|
||||
ASTPtr query_ptr;
|
||||
};
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
bool ParserCreateFunctionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
|
||||
{
|
||||
ParserKeyword s_create("CREATE");
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/// CREATE FUNCTION test AS x -> x || '1'
|
||||
class ParserCreateFunctionQuery : public IParserBase
|
||||
{
|
||||
|
@ -1,2 +1 @@
|
||||
create function MyFunc2 as (a, b) -> a || b || c;
|
||||
select MyFunc2('1', '2'); -- { serverError 47 }
|
||||
create function MyFunc2 as (a, b) -> a || b || c; -- { serverError 47 }
|
||||
|
Loading…
Reference in New Issue
Block a user