ClickHouse/src/Analyzer/WindowFunctionsUtils.cpp

79 lines
2.3 KiB
C++
Raw Normal View History

2022-10-11 13:19:50 +00:00
#include <Analyzer/WindowFunctionsUtils.h>
2022-09-12 14:14:40 +00:00
#include <Analyzer/IQueryTreeNode.h>
#include <Analyzer/InDepthQueryTreeVisitor.h>
#include <Analyzer/FunctionNode.h>
namespace DB
{
namespace ErrorCodes
{
2022-09-15 10:14:55 +00:00
extern const int ILLEGAL_AGGREGATION;
2022-09-12 14:14:40 +00:00
}
namespace
{
class CollectWindowFunctionNodeVisitor : public ConstInDepthQueryTreeVisitor<CollectWindowFunctionNodeVisitor>
2022-09-12 14:14:40 +00:00
{
public:
explicit CollectWindowFunctionNodeVisitor(QueryTreeNodes * window_function_nodes_)
: window_function_nodes(window_function_nodes_)
{}
2022-09-12 14:14:40 +00:00
explicit CollectWindowFunctionNodeVisitor(String assert_no_window_functions_place_message_)
: assert_no_window_functions_place_message(std::move(assert_no_window_functions_place_message_))
{}
2022-09-12 14:14:40 +00:00
void visitImpl(const QueryTreeNodePtr & node)
2022-09-12 14:14:40 +00:00
{
auto * function_node = node->as<FunctionNode>();
if (!function_node || !function_node->isWindowFunction())
return;
if (!assert_no_window_functions_place_message.empty())
2022-09-12 14:14:40 +00:00
throw Exception(ErrorCodes::ILLEGAL_AGGREGATION,
"Window function {} is found {} in query",
2022-10-19 10:25:27 +00:00
function_node->formatASTForErrorMessage(),
assert_no_window_functions_place_message);
2022-09-12 14:14:40 +00:00
if (window_function_nodes)
window_function_nodes->push_back(node);
2022-09-12 14:14:40 +00:00
}
static bool needChildVisit(const QueryTreeNodePtr &, const QueryTreeNodePtr & child_node)
{
return !(child_node->getNodeType() == QueryTreeNodeType::QUERY || child_node->getNodeType() == QueryTreeNodeType::UNION);
}
private:
QueryTreeNodes * window_function_nodes = nullptr;
String assert_no_window_functions_place_message;
};
2022-09-12 14:14:40 +00:00
}
QueryTreeNodes collectWindowFunctionNodes(const QueryTreeNodePtr & node)
{
QueryTreeNodes window_function_nodes;
CollectWindowFunctionNodeVisitor visitor(&window_function_nodes);
2022-09-12 14:14:40 +00:00
visitor.visit(node);
return window_function_nodes;
}
void collectWindowFunctionNodes(const QueryTreeNodePtr & node, QueryTreeNodes & result)
{
CollectWindowFunctionNodeVisitor visitor(&result);
2022-09-12 14:14:40 +00:00
visitor.visit(node);
}
void assertNoWindowFunctionNodes(const QueryTreeNodePtr & node, const String & assert_no_window_functions_place_message)
{
CollectWindowFunctionNodeVisitor visitor(assert_no_window_functions_place_message);
visitor.visit(node);
2022-09-12 14:14:40 +00:00
}
}