2021-08-12 11:42:51 +00:00
|
|
|
#include <Interpreters/SelectIntersectExceptQueryVisitor.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2021-11-26 18:27:16 +00:00
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2021-08-12 11:42:51 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2022-08-30 10:09:01 +00:00
|
|
|
#include <Core/SettingsEnums.h>
|
|
|
|
#include <Parsers/SelectUnionMode.h>
|
2021-08-12 11:42:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-08-14 17:04:21 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
2022-08-30 10:09:01 +00:00
|
|
|
extern const int EXPECTED_ALL_OR_DISTINCT;
|
2021-08-14 17:04:21 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-08-30 10:09:01 +00:00
|
|
|
void SelectIntersectExceptQueryMatcher::visit(ASTSelectWithUnionQuery & ast, Data & data)
|
2021-08-13 09:57:15 +00:00
|
|
|
{
|
2022-08-30 10:09:01 +00:00
|
|
|
auto union_modes = std::move(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();
|
2021-11-26 18:27:16 +00:00
|
|
|
SelectUnionModes modes;
|
2021-08-13 09:57:15 +00:00
|
|
|
|
2022-08-30 10:09:01 +00:00
|
|
|
for (auto & mode : union_modes)
|
2021-08-13 09:57:15 +00:00
|
|
|
{
|
2022-08-30 10:09:01 +00:00
|
|
|
/// Rewrite intersect / except mode
|
|
|
|
if (mode == SelectUnionMode::EXCEPT_DEFAULT)
|
|
|
|
{
|
|
|
|
if (data.except_default_mode == SetOperationMode::ALL)
|
|
|
|
mode = SelectUnionMode::EXCEPT_ALL;
|
|
|
|
else if (data.except_default_mode == SetOperationMode::DISTINCT)
|
|
|
|
mode = SelectUnionMode::EXCEPT_DISTINCT;
|
|
|
|
else
|
2023-01-23 13:16:14 +00:00
|
|
|
throw Exception(DB::ErrorCodes::EXPECTED_ALL_OR_DISTINCT,
|
|
|
|
"Expected ALL or DISTINCT in EXCEPT query, because setting (except_default_mode) is empty");
|
2022-08-30 10:09:01 +00:00
|
|
|
}
|
|
|
|
else if (mode == SelectUnionMode::INTERSECT_DEFAULT)
|
|
|
|
{
|
|
|
|
if (data.intersect_default_mode == SetOperationMode::ALL)
|
|
|
|
mode = SelectUnionMode::INTERSECT_ALL;
|
|
|
|
else if (data.intersect_default_mode == SetOperationMode::DISTINCT)
|
|
|
|
mode = SelectUnionMode::INTERSECT_DISTINCT;
|
|
|
|
else
|
2023-01-23 13:16:14 +00:00
|
|
|
throw Exception(DB::ErrorCodes::EXPECTED_ALL_OR_DISTINCT,
|
|
|
|
"Expected ALL or DISTINCT in INTERSECT query, because setting (intersect_default_mode) is empty");
|
2022-08-30 10:09:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 12:31:55 +00:00
|
|
|
switch (mode)
|
2021-08-13 09:57:15 +00:00
|
|
|
{
|
2022-08-30 10:09:01 +00:00
|
|
|
case SelectUnionMode::EXCEPT_ALL:
|
|
|
|
case SelectUnionMode::EXCEPT_DISTINCT:
|
2021-08-14 12:31:55 +00:00
|
|
|
{
|
|
|
|
auto left = std::make_shared<ASTSelectWithUnionQuery>();
|
2022-08-30 10:09:01 +00:00
|
|
|
left->union_mode = mode == SelectUnionMode::EXCEPT_ALL ? SelectUnionMode::UNION_ALL : SelectUnionMode::UNION_DISTINCT;
|
2021-08-14 12:31:55 +00:00
|
|
|
|
|
|
|
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>();
|
2022-08-30 10:09:01 +00:00
|
|
|
except_node->final_operator = mode == SelectUnionMode::EXCEPT_ALL
|
|
|
|
? ASTSelectIntersectExceptQuery::Operator::EXCEPT_ALL
|
|
|
|
: ASTSelectIntersectExceptQuery::Operator::EXCEPT_DISTINCT;
|
2021-08-14 12:31:55 +00:00
|
|
|
except_node->children = {left, right};
|
|
|
|
|
|
|
|
children = {except_node};
|
|
|
|
break;
|
|
|
|
}
|
2022-08-30 10:09:01 +00:00
|
|
|
case SelectUnionMode::INTERSECT_ALL:
|
|
|
|
case SelectUnionMode::INTERSECT_DISTINCT:
|
2021-08-14 12:31:55 +00:00
|
|
|
{
|
|
|
|
bool from_except = false;
|
|
|
|
const auto * except_ast = typeid_cast<const ASTSelectIntersectExceptQuery *>(children.back().get());
|
2022-08-30 10:09:01 +00:00
|
|
|
if (except_ast
|
|
|
|
&& (except_ast->final_operator == ASTSelectIntersectExceptQuery::Operator::EXCEPT_ALL
|
|
|
|
|| except_ast->final_operator == ASTSelectIntersectExceptQuery::Operator::EXCEPT_DISTINCT))
|
2021-08-14 12:31:55 +00:00
|
|
|
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>();
|
2022-08-30 10:09:01 +00:00
|
|
|
intersect_node->final_operator = mode == SelectUnionMode::INTERSECT_ALL
|
|
|
|
? ASTSelectIntersectExceptQuery::Operator::INTERSECT_ALL
|
|
|
|
: ASTSelectIntersectExceptQuery::Operator::INTERSECT_DISTINCT;
|
2021-08-14 12:31:55 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
ast.list_of_selects->children = std::move(children);
|
|
|
|
ast.list_of_modes = std::move(modes);
|
|
|
|
}
|
|
|
|
|
2021-08-12 11:42:51 +00:00
|
|
|
}
|