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
|
|
|
|
2023-02-08 14:41:24 +00:00
|
|
|
explicit CollectWindowFunctionNodeVisitor(bool only_check_)
|
|
|
|
: only_check(only_check_)
|
|
|
|
{}
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
void visitImpl(const QueryTreeNodePtr & node)
|
2022-09-12 14:14:40 +00:00
|
|
|
{
|
2023-02-08 14:41:24 +00:00
|
|
|
if (only_check && has_window_functions)
|
|
|
|
return;
|
|
|
|
|
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",
|
2022-10-19 10:25:27 +00:00
|
|
|
function_node->formatASTForErrorMessage(),
|
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);
|
2023-02-08 14:41:24 +00:00
|
|
|
|
|
|
|
has_window_functions = true;
|
2022-09-12 14:14:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 19:34:12 +00:00
|
|
|
bool needChildVisit(const QueryTreeNodePtr &, const QueryTreeNodePtr & child_node) const
|
2022-09-12 14:14:40 +00:00
|
|
|
{
|
2023-02-08 14:41:24 +00:00
|
|
|
if (only_check && has_window_functions)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto child_node_type = child_node->getNodeType();
|
|
|
|
return !(child_node_type == QueryTreeNodeType::QUERY || child_node_type == QueryTreeNodeType::UNION);
|
2022-09-12 14:14:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 14:41:24 +00:00
|
|
|
bool hasWindowFunctions() const
|
|
|
|
{
|
|
|
|
return has_window_functions;
|
|
|
|
}
|
2022-10-07 13:20:45 +00:00
|
|
|
private:
|
|
|
|
QueryTreeNodes * window_function_nodes = nullptr;
|
|
|
|
String assert_no_window_functions_place_message;
|
2023-02-08 14:41:24 +00:00
|
|
|
bool only_check = false;
|
|
|
|
bool has_window_functions = false;
|
2022-10-07 13:20:45 +00:00
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:41:24 +00:00
|
|
|
bool hasWindowFunctionNodes(const QueryTreeNodePtr & node)
|
|
|
|
{
|
|
|
|
CollectWindowFunctionNodeVisitor visitor(true /*only_check*/);
|
|
|
|
visitor.visit(node);
|
|
|
|
|
|
|
|
return visitor.hasWindowFunctions();
|
|
|
|
}
|
|
|
|
|
2022-09-12 14:14:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|