From 476b066668d3d19bdbc8f70ab7abae4a139387f5 Mon Sep 17 00:00:00 2001 From: abel-wang Date: Tue, 10 Aug 2021 23:21:23 +0800 Subject: [PATCH 1/7] Enable with constants. --- src/Interpreters/RequiredSourceColumnsVisitor.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Interpreters/RequiredSourceColumnsVisitor.cpp b/src/Interpreters/RequiredSourceColumnsVisitor.cpp index 2f2a68656bc..f6786451c82 100644 --- a/src/Interpreters/RequiredSourceColumnsVisitor.cpp +++ b/src/Interpreters/RequiredSourceColumnsVisitor.cpp @@ -123,6 +123,17 @@ void RequiredSourceColumnsMatcher::visit(const ASTSelectQuery & select, const AS data.addColumnAliasIfAny(*node); } + if (auto & with = select.with()) + { + for (auto & node : with->children) + { + if (const auto * identifier = node->as()) + data.addColumnIdentifier(*identifier); + else + data.addColumnAliasIfAny(*node); + } + } + std::vector out; for (const auto & node : select.children) { @@ -134,6 +145,8 @@ void RequiredSourceColumnsMatcher::visit(const ASTSelectQuery & select, const AS /// revisit select_expression_list (with children) when all the aliases are set Visitor(data).visit(select.select()); + if (auto with = select.with()) + Visitor(data).visit(with); } void RequiredSourceColumnsMatcher::visit(const ASTIdentifier & node, const ASTPtr &, Data & data) From ec0ee2cecf87e3dd72aca9992d304b65e8d6d9db Mon Sep 17 00:00:00 2001 From: abel-wang Date: Tue, 10 Aug 2021 23:22:33 +0800 Subject: [PATCH 2/7] Replace parameters in ASTFunctions with alias. --- src/Interpreters/QueryNormalizer.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Interpreters/QueryNormalizer.cpp b/src/Interpreters/QueryNormalizer.cpp index ea61ade2b49..7c820622c37 100644 --- a/src/Interpreters/QueryNormalizer.cpp +++ b/src/Interpreters/QueryNormalizer.cpp @@ -256,6 +256,9 @@ void QueryNormalizer::visit(ASTPtr & ast, Data & data) visit(*node_select, ast, data); else if (auto * node_param = ast->as()) throw Exception("Query parameter " + backQuote(node_param->name) + " was not set", ErrorCodes::UNKNOWN_QUERY_PARAMETER); + else if (auto * node_function = ast->as()) + if (node_function->parameters) + visit(node_function->parameters, data); /// If we replace the root of the subtree, we will be called again for the new root, in case the alias is replaced by an alias. if (ast.get() != initial_ast.get()) From 6ac4ad2920af7e5f73cadb81ca6cc1d47a7cf805 Mon Sep 17 00:00:00 2001 From: abel-wang Date: Wed, 11 Aug 2021 11:19:46 +0800 Subject: [PATCH 3/7] add tests --- .../02006_use_constants_in_with_and_select.reference | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference diff --git a/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference new file mode 100644 index 00000000000..ab7d5dd61aa --- /dev/null +++ b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference @@ -0,0 +1,5 @@ +1 [1] +1 [1] +99.9 +0.1 99.9 +[99.9] From b5c1ee9a8a80ae2e29e7c48a6377d8d7f00f2b6c Mon Sep 17 00:00:00 2001 From: abel-wang Date: Wed, 11 Aug 2021 11:32:44 +0800 Subject: [PATCH 4/7] add tests --- ...02006_use_constants_in_with_and_select.sql | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/queries/0_stateless/02006_use_constants_in_with_and_select.sql diff --git a/tests/queries/0_stateless/02006_use_constants_in_with_and_select.sql b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.sql new file mode 100644 index 00000000000..91171c9ab7b --- /dev/null +++ b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.sql @@ -0,0 +1,36 @@ +SELECT + 1 AS max_size, + groupArray(max_size)(col) +FROM + ( + SELECT 1 AS col + UNION ALL + SELECT 2 + ); + +WITH 1 AS max_size +SELECT groupArray(max_size)(col) +FROM + ( + SELECT 1 as col + UNION ALL + SELECT 2 + ); + +WITH 0.1 AS level +SELECT quantile(level)(number) +FROM numbers(1000); + +SELECT 0.1 AS level, quantile(level)(number) +FROM numbers(1000); + +WITH + 0.1 AS level, + 1 AS max_size +SELECT groupArray(max_size)(col) +FROM + ( + SELECT quantile(level)(number) AS col + FROM numbers(1000) + ); + From 92ef0a9ed71a730108cdc2658d8ad47332e84cbd Mon Sep 17 00:00:00 2001 From: abel-wang Date: Wed, 11 Aug 2021 12:34:41 +0800 Subject: [PATCH 5/7] format test results. --- .../02006_use_constants_in_with_and_select.reference | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference index ab7d5dd61aa..bbf008ffdf2 100644 --- a/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference +++ b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference @@ -1,5 +1,5 @@ -1 [1] -1 [1] +1 [1] +[1] 99.9 -0.1 99.9 +0.1 99.9 [99.9] From 1c5d1c6b7a6062dda03daa3c7f59b1835a4b7a42 Mon Sep 17 00:00:00 2001 From: abel-wang Date: Wed, 11 Aug 2021 19:54:53 +0800 Subject: [PATCH 6/7] Remove useless code. --- src/Interpreters/RequiredSourceColumnsVisitor.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Interpreters/RequiredSourceColumnsVisitor.cpp b/src/Interpreters/RequiredSourceColumnsVisitor.cpp index f6786451c82..2c81969009f 100644 --- a/src/Interpreters/RequiredSourceColumnsVisitor.cpp +++ b/src/Interpreters/RequiredSourceColumnsVisitor.cpp @@ -145,8 +145,6 @@ void RequiredSourceColumnsMatcher::visit(const ASTSelectQuery & select, const AS /// revisit select_expression_list (with children) when all the aliases are set Visitor(data).visit(select.select()); - if (auto with = select.with()) - Visitor(data).visit(with); } void RequiredSourceColumnsMatcher::visit(const ASTIdentifier & node, const ASTPtr &, Data & data) From c449cf072a0717d05425746d3f1bf0f7fbd052f1 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Thu, 12 Aug 2021 14:25:42 +0300 Subject: [PATCH 7/7] Update RequiredSourceColumnsVisitor.cpp --- src/Interpreters/RequiredSourceColumnsVisitor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interpreters/RequiredSourceColumnsVisitor.cpp b/src/Interpreters/RequiredSourceColumnsVisitor.cpp index 2c81969009f..21ec94a6917 100644 --- a/src/Interpreters/RequiredSourceColumnsVisitor.cpp +++ b/src/Interpreters/RequiredSourceColumnsVisitor.cpp @@ -123,7 +123,7 @@ void RequiredSourceColumnsMatcher::visit(const ASTSelectQuery & select, const AS data.addColumnAliasIfAny(*node); } - if (auto & with = select.with()) + if (const auto & with = select.with()) { for (auto & node : with->children) {