From 6824b31c4fbdfb3cbafa79c86c992f6b9b8cb523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 8 Oct 2024 13:16:12 +0800 Subject: [PATCH 1/2] Fix crash in WHERE with lambda functions --- src/Analyzer/ValidationUtils.cpp | 22 +++++++++++++++---- .../0_stateless/03248_invalid_where.reference | 0 .../0_stateless/03248_invalid_where.sql | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 tests/queries/0_stateless/03248_invalid_where.reference create mode 100644 tests/queries/0_stateless/03248_invalid_where.sql diff --git a/src/Analyzer/ValidationUtils.cpp b/src/Analyzer/ValidationUtils.cpp index a8274799b1b..0cc483caa0d 100644 --- a/src/Analyzer/ValidationUtils.cpp +++ b/src/Analyzer/ValidationUtils.cpp @@ -19,6 +19,7 @@ namespace ErrorCodes extern const int BAD_ARGUMENTS; extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; extern const int ILLEGAL_PREWHERE; + extern const int UNSUPPORTED_METHOD; } namespace @@ -26,11 +27,24 @@ namespace void validateFilter(const QueryTreeNodePtr & filter_node, std::string_view exception_place_message, const QueryTreeNodePtr & query_node) { - if (filter_node->getNodeType() == QueryTreeNodeType::LIST) - throw Exception(ErrorCodes::BAD_ARGUMENTS, - "Unsupported expression '{}' in filter", filter_node->formatASTForErrorMessage()); + DataTypePtr filter_node_result_type; + try + { + filter_node_result_type = filter_node->getResultType(); + } + catch (const DB::Exception &e) + { + if (e.code() != ErrorCodes::UNSUPPORTED_METHOD) + e.rethrow(); + } + + if (!filter_node_result_type) + throw Exception(ErrorCodes::BAD_ARGUMENTS, + "Unsupported expression '{}' in filter in {}. In query {}", + filter_node->formatASTForErrorMessage(), + exception_place_message, + query_node->formatASTForErrorMessage()); - auto filter_node_result_type = filter_node->getResultType(); if (!filter_node_result_type->canBeUsedInBooleanContext()) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER, "Invalid type for filter in {}: {}. In query {}", diff --git a/tests/queries/0_stateless/03248_invalid_where.reference b/tests/queries/0_stateless/03248_invalid_where.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/03248_invalid_where.sql b/tests/queries/0_stateless/03248_invalid_where.sql new file mode 100644 index 00000000000..ffc4da650e8 --- /dev/null +++ b/tests/queries/0_stateless/03248_invalid_where.sql @@ -0,0 +1 @@ +WITH x -> toString(x) AS lambda_1 SELECT arrayMap(lambda_1 AS lambda_2, [1, 2, 3]), arrayMap(lambda_2, ['1', '2', '3']) WHERE lambda_2; -- { serverError BAD_ARGUMENTS } \ No newline at end of file From d57f0a9d37ea98067a6f77bd07ce1b391440c154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 8 Oct 2024 16:35:08 +0800 Subject: [PATCH 2/2] More consistent error codes --- src/Analyzer/ValidationUtils.cpp | 5 +++-- tests/queries/0_stateless/03101_analyzer_identifiers_3.sql | 2 +- tests/queries/0_stateless/03248_invalid_where.sql | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Analyzer/ValidationUtils.cpp b/src/Analyzer/ValidationUtils.cpp index 0cc483caa0d..971909022c2 100644 --- a/src/Analyzer/ValidationUtils.cpp +++ b/src/Analyzer/ValidationUtils.cpp @@ -20,6 +20,7 @@ namespace ErrorCodes extern const int ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER; extern const int ILLEGAL_PREWHERE; extern const int UNSUPPORTED_METHOD; + extern const int UNEXPECTED_EXPRESSION; } namespace @@ -39,8 +40,8 @@ void validateFilter(const QueryTreeNodePtr & filter_node, std::string_view excep } if (!filter_node_result_type) - throw Exception(ErrorCodes::BAD_ARGUMENTS, - "Unsupported expression '{}' in filter in {}. In query {}", + throw Exception(ErrorCodes::UNEXPECTED_EXPRESSION, + "Unexpected expression '{}' in filter in {}. In query {}", filter_node->formatASTForErrorMessage(), exception_place_message, query_node->formatASTForErrorMessage()); diff --git a/tests/queries/0_stateless/03101_analyzer_identifiers_3.sql b/tests/queries/0_stateless/03101_analyzer_identifiers_3.sql index 997fee91930..f113b8bc9e6 100644 --- a/tests/queries/0_stateless/03101_analyzer_identifiers_3.sql +++ b/tests/queries/0_stateless/03101_analyzer_identifiers_3.sql @@ -39,7 +39,7 @@ SELECT * GROUP BY *; -- not ok as every component of ORDER BY may contain ASC/DESC and COLLATE; though can be supported in some sense -- but it works SELECT * ORDER BY *; -SELECT * WHERE *; -- { serverError BAD_ARGUMENTS } +SELECT * WHERE *; -- { serverError UNEXPECTED_EXPRESSION } SELECT '---'; diff --git a/tests/queries/0_stateless/03248_invalid_where.sql b/tests/queries/0_stateless/03248_invalid_where.sql index ffc4da650e8..0b1183a382a 100644 --- a/tests/queries/0_stateless/03248_invalid_where.sql +++ b/tests/queries/0_stateless/03248_invalid_where.sql @@ -1 +1 @@ -WITH x -> toString(x) AS lambda_1 SELECT arrayMap(lambda_1 AS lambda_2, [1, 2, 3]), arrayMap(lambda_2, ['1', '2', '3']) WHERE lambda_2; -- { serverError BAD_ARGUMENTS } \ No newline at end of file +WITH x -> toString(x) AS lambda_1 SELECT arrayMap(lambda_1 AS lambda_2, [1, 2, 3]), arrayMap(lambda_2, ['1', '2', '3']) WHERE lambda_2; -- { serverError UNEXPECTED_EXPRESSION } \ No newline at end of file