Skip to content

Commit c81bec3

Browse files
Merge pull request ClickHouse#80431 from ClickHouse/fix-udf
Fix ClickHouse#80429 Logical error: SQL user defined function ... must represent lambda expression
2 parents 70c012a + c8beef5 commit c81bec3

File tree

7 files changed

+50
-52
lines changed

7 files changed

+50
-52
lines changed

src/Analyzer/Resolve/QueryAnalyzer.cpp

Lines changed: 36 additions & 37 deletions
Large diffs are not rendered by default.

src/Functions/UserDefined/UserDefinedSQLFunctionFactory.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ErrorCodes
2929
extern const int FUNCTION_ALREADY_EXISTS;
3030
extern const int CANNOT_DROP_FUNCTION;
3131
extern const int CANNOT_CREATE_RECURSIVE_FUNCTION;
32-
extern const int UNSUPPORTED_METHOD;
32+
extern const int BAD_ARGUMENTS;
3333
}
3434

3535

@@ -52,17 +52,17 @@ namespace
5252
ASTFunction * lambda_function = function->as<ASTFunction>();
5353

5454
if (!lambda_function)
55-
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Expected function, got: {}", function->formatForErrorMessage());
55+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expected function, got: {}", function->formatForErrorMessage());
5656

5757
auto & lambda_function_expression_list = lambda_function->arguments->children;
5858

5959
if (lambda_function_expression_list.size() != 2)
60-
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Lambda must have arguments and body");
60+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Lambda must have arguments and body");
6161

6262
const ASTFunction * tuple_function_arguments = lambda_function_expression_list[0]->as<ASTFunction>();
6363

64-
if (!tuple_function_arguments || !tuple_function_arguments->arguments)
65-
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Lambda must have valid arguments");
64+
if (!tuple_function_arguments || !tuple_function_arguments->arguments || tuple_function_arguments->name != "tuple")
65+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Lambda must have valid arguments");
6666

6767
std::unordered_set<String> arguments;
6868

@@ -71,17 +71,17 @@ namespace
7171
const auto * argument_identifier = argument->as<ASTIdentifier>();
7272

7373
if (!argument_identifier)
74-
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Lambda argument must be identifier");
74+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Lambda argument must be identifier");
7575

7676
const auto & argument_name = argument_identifier->name();
7777
auto [_, inserted] = arguments.insert(argument_name);
7878
if (!inserted)
79-
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Identifier {} already used as function parameter", argument_name);
79+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Identifier {} already used as function parameter", argument_name);
8080
}
8181

8282
ASTPtr function_body = lambda_function_expression_list[1];
8383
if (!function_body)
84-
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Lambda must have valid function body");
84+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Lambda must have valid function body");
8585

8686
validateFunctionRecursiveness(*function_body, name);
8787
}

src/Parsers/ParserCreateFunctionQuery.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <Parsers/ParserCreateFunctionQuery.h>
22

33
#include <Parsers/ASTCreateFunctionQuery.h>
4-
#include <Parsers/ASTExpressionList.h>
5-
#include <Parsers/ASTIdentifier.h>
64
#include <Parsers/CommonParsers.h>
75
#include <Parsers/ExpressionElementParsers.h>
86
#include <Parsers/ExpressionListParsers.h>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE FUNCTION 02181_invalid_lambda AS lambda(((x * 2) AS x_doubled) + x_doubled); --{serverError UNSUPPORTED_METHOD}
2-
CREATE FUNCTION 02181_invalid_lambda AS lambda(x); --{serverError UNSUPPORTED_METHOD}
3-
CREATE FUNCTION 02181_invalid_lambda AS lambda(); --{serverError UNSUPPORTED_METHOD}
4-
CREATE FUNCTION 02181_invalid_lambda AS lambda(tuple(x)) --{serverError UNSUPPORTED_METHOD}
1+
CREATE FUNCTION 02181_invalid_lambda AS lambda(((x * 2) AS x_doubled) + x_doubled); --{serverError BAD_ARGUMENTS}
2+
CREATE FUNCTION 02181_invalid_lambda AS lambda(x); --{serverError BAD_ARGUMENTS}
3+
CREATE FUNCTION 02181_invalid_lambda AS lambda(); --{serverError BAD_ARGUMENTS}
4+
CREATE FUNCTION 02181_invalid_lambda AS lambda(tuple(x)) --{serverError BAD_ARGUMENTS}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
create function foo as x -- { serverError UNSUPPORTED_METHOD }
1+
create function foo as x -- { serverError BAD_ARGUMENTS }

tests/queries/0_stateless/03518_bad_sql_udf.reference

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE OR REPLACE FUNCTION 03518_bad_sql_udf AS lambda(identity(x), x); -- { serverError BAD_ARGUMENTS }

0 commit comments

Comments
 (0)