From a5c8cc19a8c1dfb30c4cf0ace7a4354d310c91ba Mon Sep 17 00:00:00 2001 From: Vasily Nemkov Date: Sun, 18 Sep 2022 21:41:09 +0400 Subject: [PATCH 1/2] Revert "Merge pull request #194 from Enmk/revert_27531_constants_in_with" This reverts commit f44ba73630c7415c1745501e1978e554d1d5f585, reversing changes made to d912f2e7d40d51af840aee6eb4e3d72b7d71f4ae. --- src/Interpreters/QueryNormalizer.cpp | 3 ++ .../RequiredSourceColumnsVisitor.cpp | 11 ++++++ ...use_constants_in_with_and_select.reference | 5 +++ ...02006_use_constants_in_with_and_select.sql | 36 +++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference create mode 100644 tests/queries/0_stateless/02006_use_constants_in_with_and_select.sql diff --git a/src/Interpreters/QueryNormalizer.cpp b/src/Interpreters/QueryNormalizer.cpp index ea61ade2b49d..7c820622c379 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()) diff --git a/src/Interpreters/RequiredSourceColumnsVisitor.cpp b/src/Interpreters/RequiredSourceColumnsVisitor.cpp index 2f2a68656bc4..21ec94a6917d 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 (const 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) { 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 000000000000..bbf008ffdf20 --- /dev/null +++ b/tests/queries/0_stateless/02006_use_constants_in_with_and_select.reference @@ -0,0 +1,5 @@ +1 [1] +[1] +99.9 +0.1 99.9 +[99.9] 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 000000000000..daca6c5d0c79 --- /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 col FROM ( + SELECT 1 AS col + UNION ALL + SELECT 2 + ) ORDER BY col); + +WITH 1 AS max_size +SELECT groupArray(max_size)(col) +FROM + (SELECT col FROM ( + SELECT 1 as col + UNION ALL + SELECT 2 + ) ORDER BY col); + +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 4a2888d707ba05b303f33f87997999b3e08319ff Mon Sep 17 00:00:00 2001 From: Amos Bird Date: Wed, 13 Jul 2022 10:27:43 +0800 Subject: [PATCH 2/2] Fix unused unknown columns introduced by WITH statement #39131 By cherry-picking a8d8293466dcb7874ca692b4a8b437b617303828: Fix unused columns introduced by with stmt --- src/Interpreters/RequiredSourceColumnsVisitor.cpp | 11 ----------- .../02354_with_statement_non_exist_column.reference | 1 + .../02354_with_statement_non_exist_column.sql | 9 +++++++++ 3 files changed, 10 insertions(+), 11 deletions(-) create mode 100644 tests/queries/0_stateless/02354_with_statement_non_exist_column.reference create mode 100644 tests/queries/0_stateless/02354_with_statement_non_exist_column.sql diff --git a/src/Interpreters/RequiredSourceColumnsVisitor.cpp b/src/Interpreters/RequiredSourceColumnsVisitor.cpp index 21ec94a6917d..2f2a68656bc4 100644 --- a/src/Interpreters/RequiredSourceColumnsVisitor.cpp +++ b/src/Interpreters/RequiredSourceColumnsVisitor.cpp @@ -123,17 +123,6 @@ void RequiredSourceColumnsMatcher::visit(const ASTSelectQuery & select, const AS data.addColumnAliasIfAny(*node); } - if (const 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) { diff --git a/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference b/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference new file mode 100644 index 000000000000..d00491fd7e5b --- /dev/null +++ b/tests/queries/0_stateless/02354_with_statement_non_exist_column.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql b/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql new file mode 100644 index 000000000000..1a989c1d9529 --- /dev/null +++ b/tests/queries/0_stateless/02354_with_statement_non_exist_column.sql @@ -0,0 +1,9 @@ +WITH x AS y SELECT 1; + +DROP TEMPORARY TABLE IF EXISTS t1; +DROP TEMPORARY TABLE IF EXISTS t2; + +CREATE TEMPORARY TABLE t1 (a Int64); +CREATE TEMPORARY TABLE t2 (a Int64, b Int64); + +WITH b AS bb SELECT bb FROM t2 WHERE a IN (SELECT a FROM t1);