From 2458defd0195a36e0ad98161a5f6ae014f01ca92 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Mon, 13 May 2024 15:16:35 +0200 Subject: [PATCH] Analyzer: Forbid WINDOW redefinition --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 7 ++++++- .../03149_analyzer_window_redefinition.reference | 0 .../0_stateless/03149_analyzer_window_redefinition.sql | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/03149_analyzer_window_redefinition.reference create mode 100644 tests/queries/0_stateless/03149_analyzer_window_redefinition.sql diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 52efee03ae4..dea56de8673 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -8009,7 +8009,12 @@ void QueryAnalyzer::resolveQuery(const QueryTreeNodePtr & query_node, Identifier window_node_typed.setParentWindowName({}); } - scope.window_name_to_window_node.emplace(window_node_typed.getAlias(), window_node); + auto [_, inserted] = scope.window_name_to_window_node.emplace(window_node_typed.getAlias(), window_node); + if (!inserted) + throw Exception(ErrorCodes::BAD_ARGUMENTS, + "Window '{}' is already defined. In scope {}", + window_node_typed.getAlias(), + scope.scope_node->formatASTForErrorMessage()); } /** Disable identifier cache during JOIN TREE resolve. diff --git a/tests/queries/0_stateless/03149_analyzer_window_redefinition.reference b/tests/queries/0_stateless/03149_analyzer_window_redefinition.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/03149_analyzer_window_redefinition.sql b/tests/queries/0_stateless/03149_analyzer_window_redefinition.sql new file mode 100644 index 00000000000..7bc5ec7579c --- /dev/null +++ b/tests/queries/0_stateless/03149_analyzer_window_redefinition.sql @@ -0,0 +1,8 @@ +CREATE TABLE users (uid Int16, name String, age Int16) ENGINE=MergeTree ORDER BY tuple(); + +INSERT INTO users VALUES (1231, 'John', 33); +INSERT INTO users VALUES (6666, 'Ksenia', 48); +INSERT INTO users VALUES (8888, 'Alice', 50); + +SELECT count(*) OVER w +FROM users WINDOW w AS (ORDER BY uid), w AS(ORDER BY name); -- { serverError BAD_ARGUMENTS }