Skip to content

Commit

Permalink
Merge pull request ClickHouse#27531 from abel-cheng/with-constants
Browse files Browse the repository at this point in the history
Enable using constants from with and select in aggregate function parameters.
  • Loading branch information
kitaisreal authored Aug 12, 2021
2 parents 8c06abe + c449cf0 commit a020fe3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Interpreters/QueryNormalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ void QueryNormalizer::visit(ASTPtr & ast, Data & data)
visit(*node_select, ast, data);
else if (auto * node_param = ast->as<ASTQueryParameter>())
throw Exception("Query parameter " + backQuote(node_param->name) + " was not set", ErrorCodes::UNKNOWN_QUERY_PARAMETER);
else if (auto * node_function = ast->as<ASTFunction>())
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())
Expand Down
11 changes: 11 additions & 0 deletions src/Interpreters/RequiredSourceColumnsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ASTIdentifier>())
data.addColumnIdentifier(*identifier);
else
data.addColumnAliasIfAny(*node);
}
}

std::vector<ASTPtr *> out;
for (const auto & node : select.children)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 [1]
[1]
99.9
0.1 99.9
[99.9]
Original file line number Diff line number Diff line change
@@ -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)
);

0 comments on commit a020fe3

Please sign in to comment.