diff --git a/src/Interpreters/InterpreterCreateFunctionQuery.cpp b/src/Interpreters/InterpreterCreateFunctionQuery.cpp index 1c43da07628..2f345f8b237 100644 --- a/src/Interpreters/InterpreterCreateFunctionQuery.cpp +++ b/src/Interpreters/InterpreterCreateFunctionQuery.cpp @@ -57,9 +57,14 @@ BlockIO InterpreterCreateFunctionQuery::execute() void InterpreterCreateFunctionQuery::validateFunction(ASTPtr function, const String & name) { - const auto * args_tuple = function->as()->arguments->children.at(0)->as(); + auto & lambda_function = function->as(); + auto & lambda_function_expression_list = lambda_function.arguments->children; + + const auto & tuple_function_arguments = lambda_function_expression_list.at(0)->as(); + std::unordered_set arguments; - for (const auto & argument : args_tuple->arguments->children) + + for (const auto & argument : tuple_function_arguments.arguments->children) { const auto & argument_name = argument->as()->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); } - ASTPtr function_body = function->as()->children.at(0)->children.at(1); + ASTPtr function_body = lambda_function_expression_list.at(1); validateFunctionRecursiveness(function_body, name); } @@ -82,5 +87,4 @@ void InterpreterCreateFunctionQuery::validateFunctionRecursiveness(ASTPtr node, validateFunctionRecursiveness(child, function_to_create); } } - } diff --git a/tests/queries/0_stateless/02148_sql_user_defined_function_subquery.reference b/tests/queries/0_stateless/02148_sql_user_defined_function_subquery.reference new file mode 100644 index 00000000000..8851ce8322c --- /dev/null +++ b/tests/queries/0_stateless/02148_sql_user_defined_function_subquery.reference @@ -0,0 +1,6 @@ +1 +2 +2 +4 +(0,'Value') +Value diff --git a/tests/queries/0_stateless/02148_sql_user_defined_function_subquery.sql b/tests/queries/0_stateless/02148_sql_user_defined_function_subquery.sql new file mode 100644 index 00000000000..cc62d1ac495 --- /dev/null +++ b/tests/queries/0_stateless/02148_sql_user_defined_function_subquery.sql @@ -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;