Merge pull request #32758 from kitaisreal/sql-user-defined-functions-support-subqueries

SQLUserDefinedFunctions support subqueries
This commit is contained in:
Maksim Kita 2021-12-15 15:57:20 +03:00 committed by GitHub
commit 655cc20525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 4 deletions

View File

@ -57,9 +57,14 @@ BlockIO InterpreterCreateFunctionQuery::execute()
void InterpreterCreateFunctionQuery::validateFunction(ASTPtr function, const String & name) void InterpreterCreateFunctionQuery::validateFunction(ASTPtr function, const String & name)
{ {
const auto * args_tuple = function->as<ASTFunction>()->arguments->children.at(0)->as<ASTFunction>(); auto & lambda_function = function->as<ASTFunction &>();
auto & lambda_function_expression_list = lambda_function.arguments->children;
const auto & tuple_function_arguments = lambda_function_expression_list.at(0)->as<ASTFunction &>();
std::unordered_set<String> arguments; std::unordered_set<String> arguments;
for (const auto & argument : args_tuple->arguments->children)
for (const auto & argument : tuple_function_arguments.arguments->children)
{ {
const auto & argument_name = argument->as<ASTIdentifier>()->name(); const auto & argument_name = argument->as<ASTIdentifier>()->name();
auto [_, inserted] = arguments.insert(argument_name); auto [_, inserted] = arguments.insert(argument_name);
@ -67,7 +72,7 @@ void InterpreterCreateFunctionQuery::validateFunction(ASTPtr function, const Str
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Identifier {} already used as function parameter", argument_name); throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Identifier {} already used as function parameter", argument_name);
} }
ASTPtr function_body = function->as<ASTFunction>()->children.at(0)->children.at(1); ASTPtr function_body = lambda_function_expression_list.at(1);
validateFunctionRecursiveness(function_body, name); validateFunctionRecursiveness(function_body, name);
} }
@ -82,5 +87,4 @@ void InterpreterCreateFunctionQuery::validateFunctionRecursiveness(ASTPtr node,
validateFunctionRecursiveness(child, function_to_create); validateFunctionRecursiveness(child, function_to_create);
} }
} }
} }

View File

@ -0,0 +1,6 @@
1
2
2
4
(0,'Value')
Value

View File

@ -0,0 +1,35 @@
-- Tags: no-parallel
DROP FUNCTION IF EXISTS 02148_test_function;
CREATE FUNCTION 02148_test_function AS () -> (SELECT 1);
SELECT 02148_test_function();
CREATE OR REPLACE FUNCTION 02148_test_function AS () -> (SELECT 2);
SELECT 02148_test_function();
DROP FUNCTION 02148_test_function;
CREATE FUNCTION 02148_test_function AS (x) -> (SELECT x + 1);
SELECT 02148_test_function(1);
DROP FUNCTION IF EXISTS 02148_test_function_nested;
CREATE FUNCTION 02148_test_function_nested AS (x) -> 02148_test_function(x + 2);
SELECT 02148_test_function_nested(1);
DROP FUNCTION 02148_test_function;
DROP FUNCTION 02148_test_function_nested;
DROP TABLE IF EXISTS 02148_test_table;
CREATE TABLE 02148_test_table (id UInt64, value String) ENGINE=TinyLog;
INSERT INTO 02148_test_table VALUES (0, 'Value');
CREATE FUNCTION 02148_test_function AS () -> (SELECT * FROM 02148_test_table LIMIT 1);
SELECT 02148_test_function();
CREATE OR REPLACE FUNCTION 02148_test_function AS () -> (SELECT value FROM 02148_test_table LIMIT 1);
SELECT 02148_test_function();
DROP FUNCTION 02148_test_function;
DROP TABLE 02148_test_table;