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
|
|
|
|
{
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
class CollectWindowFunctionNodeVisitor : public ConstInDepthQueryTreeVisitor<CollectWindowFunctionNodeVisitor>
|
2022-09-12 14:14:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-10-07 13:20:45 +00:00
|
|
|
explicit CollectWindowFunctionNodeVisitor(QueryTreeNodes * window_function_nodes_)
|
|
|
|
: window_function_nodes(window_function_nodes_)
|
|
|
|
{}
|
2022-09-12 14:14:40 +00:00
|
|
|
|
2022-10-07 13:20:45 +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
|
|
|
|
2022-10-07 13:20:45 +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;
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
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",
|
|
|
|
function_node->getName(),
|
2022-10-07 13:20:45 +00:00
|
|
|
assert_no_window_functions_place_message);
|
2022-09-12 14:14:40 +00:00
|
|
|
|
2022-10-07 13:20:45 +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);
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
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;
|
2022-10-07 13:20:45 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-10-07 13:20:45 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-10-07 13:20:45 +00:00
|
|
|
CollectWindowFunctionNodeVisitor visitor(assert_no_window_functions_place_message);
|
|
|
|
visitor.visit(node);
|
2022-09-12 14:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|