2021-08-12 11:42:51 +00:00
|
|
|
#include <Interpreters/SelectIntersectExceptQueryVisitor.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-08-14 17:04:21 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2021-08-12 11:42:51 +00:00
|
|
|
|
2021-08-13 09:57:15 +00:00
|
|
|
/*
|
|
|
|
* Note: there is a difference between intersect and except behaviour.
|
2021-08-15 06:55:43 +00:00
|
|
|
* `intersect` is supposed to be a part of the last SelectQuery, i.e. the sequence with no parenthesis:
|
2021-08-13 09:57:15 +00:00
|
|
|
* select 1 union all select 2 except select 1 intersect 2 except select 2 union distinct select 5;
|
|
|
|
* is interpreted as:
|
|
|
|
* select 1 union all select 2 except (select 1 intersect 2) except select 2 union distinct select 5;
|
2021-08-15 06:55:43 +00:00
|
|
|
* Whereas `except` is applied to all left union part like:
|
2021-08-13 09:57:15 +00:00
|
|
|
* (((select 1 union all select 2) except (select 1 intersect 2)) except select 2) union distinct select 5;
|
|
|
|
**/
|
|
|
|
|
2021-08-12 11:42:51 +00:00
|
|
|
void SelectIntersectExceptQueryMatcher::visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
2021-08-14 12:31:55 +00:00
|
|
|
if (auto * select_union = ast->as<ASTSelectWithUnionQuery>())
|
2021-08-13 09:57:15 +00:00
|
|
|
visit(*select_union, data);
|
2021-08-12 11:42:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 09:57:15 +00:00
|
|
|
void SelectIntersectExceptQueryMatcher::visit(ASTSelectWithUnionQuery & ast, Data &)
|
|
|
|
{
|
2021-08-15 06:55:43 +00:00
|
|
|
const auto & union_modes = ast.list_of_modes;
|
2021-08-13 09:57:15 +00:00
|
|
|
|
|
|
|
if (union_modes.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto selects = std::move(ast.list_of_selects->children);
|
|
|
|
|
|
|
|
if (union_modes.size() + 1 != selects.size())
|
2021-08-14 12:31:55 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Incorrect ASTSelectWithUnionQuery (modes: {}, selects: {})",
|
2021-08-13 09:57:15 +00:00
|
|
|
union_modes.size(), selects.size());
|
|
|
|
|
|
|
|
std::reverse(selects.begin(), selects.end());
|
|
|
|
|
|
|
|
ASTs children = {selects.back()};
|
|
|
|
selects.pop_back();
|
|
|
|
ASTSelectWithUnionQuery::UnionModes modes;
|
|
|
|
|
|
|
|
for (const auto & mode : union_modes)
|
|
|
|
{
|
2021-08-14 12:31:55 +00:00
|
|
|
switch (mode)
|
2021-08-13 09:57:15 +00:00
|
|
|
{
|
2021-08-14 12:31:55 +00:00
|
|
|
case ASTSelectWithUnionQuery::Mode::EXCEPT:
|
|
|
|
{
|
|
|
|
auto left = std::make_shared<ASTSelectWithUnionQuery>();
|
|
|
|
left->union_mode = ASTSelectWithUnionQuery::Mode::ALL;
|
|
|
|
|
|
|
|
left->list_of_selects = std::make_shared<ASTExpressionList>();
|
|
|
|
left->children.push_back(left->list_of_selects);
|
|
|
|
left->list_of_selects->children = std::move(children);
|
|
|
|
|
|
|
|
left->list_of_modes = std::move(modes);
|
|
|
|
modes = {};
|
|
|
|
|
|
|
|
auto right = selects.back();
|
|
|
|
selects.pop_back();
|
|
|
|
|
|
|
|
auto except_node = std::make_shared<ASTSelectIntersectExceptQuery>();
|
|
|
|
except_node->final_operator = ASTSelectIntersectExceptQuery::Operator::EXCEPT;
|
|
|
|
except_node->children = {left, right};
|
|
|
|
|
|
|
|
children = {except_node};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ASTSelectWithUnionQuery::Mode::INTERSECT:
|
|
|
|
{
|
|
|
|
bool from_except = false;
|
|
|
|
const auto * except_ast = typeid_cast<const ASTSelectIntersectExceptQuery *>(children.back().get());
|
|
|
|
if (except_ast && (except_ast->final_operator == ASTSelectIntersectExceptQuery::Operator::EXCEPT))
|
|
|
|
from_except = true;
|
|
|
|
|
|
|
|
ASTPtr left;
|
|
|
|
if (from_except)
|
|
|
|
{
|
|
|
|
left = std::move(children.back()->children[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
left = children.back();
|
|
|
|
children.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto right = selects.back();
|
|
|
|
selects.pop_back();
|
|
|
|
|
|
|
|
auto intersect_node = std::make_shared<ASTSelectIntersectExceptQuery>();
|
|
|
|
intersect_node->final_operator = ASTSelectIntersectExceptQuery::Operator::INTERSECT;
|
|
|
|
intersect_node->children = {left, right};
|
|
|
|
|
|
|
|
if (from_except)
|
|
|
|
children.back()->children[1] = std::move(intersect_node);
|
|
|
|
else
|
|
|
|
children.push_back(std::move(intersect_node));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
auto right = selects.back();
|
|
|
|
selects.pop_back();
|
|
|
|
children.emplace_back(std::move(right));
|
|
|
|
modes.push_back(mode);
|
|
|
|
break;
|
|
|
|
}
|
2021-08-13 09:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selects.empty())
|
|
|
|
{
|
|
|
|
auto right = selects.back();
|
|
|
|
selects.pop_back();
|
|
|
|
children.emplace_back(std::move(right));
|
|
|
|
}
|
|
|
|
|
2021-08-13 12:07:44 +00:00
|
|
|
ast.union_mode = ASTSelectWithUnionQuery::Mode::Unspecified;
|
2021-08-13 09:57:15 +00:00
|
|
|
ast.list_of_selects->children = std::move(children);
|
|
|
|
ast.list_of_modes = std::move(modes);
|
|
|
|
}
|
|
|
|
|
2021-08-12 11:42:51 +00:00
|
|
|
}
|