ClickHouse/src/Interpreters/ExtractExpressionInfoVisitor.cpp

115 lines
3.5 KiB
C++
Raw Normal View History

2019-12-18 03:56:03 +00:00
#include <Interpreters/ExtractExpressionInfoVisitor.h>
#include <Functions/FunctionFactory.h>
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <Interpreters/IdentifierSemantic.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
2020-01-06 10:33:08 +00:00
#include <Parsers/ASTSubquery.h>
2019-12-18 03:56:03 +00:00
namespace DB
{
void ExpressionInfoMatcher::visit(const ASTPtr & ast, Data & data)
{
if (const auto * function = ast->as<ASTFunction>())
visit(*function, ast, data);
else if (const auto * identifier = ast->as<ASTIdentifier>())
visit(*identifier, ast, data);
}
void ExpressionInfoMatcher::visit(const ASTFunction & ast_function, const ASTPtr &, Data & data)
{
if (ast_function.name == "arrayJoin")
2020-12-16 21:44:05 +00:00
{
2019-12-18 03:56:03 +00:00
data.is_array_join = true;
2020-12-16 21:44:05 +00:00
}
2020-12-24 04:03:33 +00:00
// "is_aggregate_function" is used to determine whether we can move a filter
// (1) from HAVING to WHERE or (2) from WHERE of a parent query to HAVING of
// a subquery.
// For aggregate functions we can't do (1) but can do (2).
// For window functions both don't make sense -- they are not allowed in
// WHERE or HAVING.
2020-12-16 21:44:05 +00:00
else if (!ast_function.is_window_function
&& AggregateFunctionFactory::instance().isAggregateFunctionName(
ast_function.name))
{
2019-12-18 03:56:03 +00:00
data.is_aggregate_function = true;
2020-12-16 21:44:05 +00:00
}
2020-12-24 04:03:33 +00:00
else if (ast_function.is_window_function)
{
data.is_window_function = true;
}
2019-12-18 03:56:03 +00:00
else
{
const auto & function = FunctionFactory::instance().tryGet(ast_function.name, data.getContext());
2019-12-18 03:56:03 +00:00
/// Skip lambda, tuple and other special functions
if (function)
{
if (function->isStateful())
data.is_stateful_function = true;
if (!function->isDeterministicInScopeOfQuery())
data.is_deterministic_function = false;
}
2019-12-18 03:56:03 +00:00
}
}
void ExpressionInfoMatcher::visit(const ASTIdentifier & identifier, const ASTPtr &, Data & data)
{
if (!identifier.compound())
{
for (size_t index = 0; index < data.tables.size(); ++index)
{
const auto & table = data.tables[index];
2019-12-18 03:56:03 +00:00
// TODO: make sure no collision ever happens
if (table.hasColumn(identifier.name()))
2019-12-18 03:56:03 +00:00
{
data.unique_reference_tables_pos.emplace(index);
break;
}
}
}
else
{
if (auto best_table_pos = IdentifierSemantic::chooseTable(identifier, data.tables))
data.unique_reference_tables_pos.emplace(*best_table_pos);
2019-12-18 03:56:03 +00:00
}
}
2020-01-06 10:33:08 +00:00
bool ExpressionInfoMatcher::needChildVisit(const ASTPtr & node, const ASTPtr &)
{
return !node->as<ASTSubquery>();
}
2021-06-01 12:20:52 +00:00
bool hasNonRewritableFunction(const ASTPtr & node, ContextPtr context)
{
for (const auto & select_expression : node->children)
{
TablesWithColumns tables;
2021-06-01 12:20:52 +00:00
ExpressionInfoVisitor::Data expression_info{WithContext{context}, tables};
ExpressionInfoVisitor(expression_info).visit(select_expression);
2020-12-24 04:03:33 +00:00
if (expression_info.is_stateful_function
|| expression_info.is_window_function)
{
// If an outer query has a WHERE on window function, we can't move
// it into the subquery, because window functions are not allowed in
// WHERE and HAVING. Example:
// select * from (
// select number,
// count(*) over (partition by intDiv(number, 3)) c
// from numbers(3)
// ) where c > 1;
return true;
2020-12-24 04:03:33 +00:00
}
}
return false;
}
2019-12-18 03:56:03 +00:00
}