ClickHouse/src/Interpreters/NormalizeSelectWithUnionQueryVisitor.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
5.3 KiB
C++
Raw Normal View History

2021-02-25 07:47:08 +00:00
#include <Interpreters/NormalizeSelectWithUnionQueryVisitor.h>
#include <Parsers/ASTExpressionList.h>
2021-08-13 09:57:15 +00:00
#include <Parsers/ASTSelectIntersectExceptQuery.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
2021-02-25 07:47:08 +00:00
#include <Common/typeid_cast.h>
namespace DB
{
namespace ErrorCodes
{
extern const int EXPECTED_ALL_OR_DISTINCT;
2021-08-16 15:23:08 +00:00
extern const int LOGICAL_ERROR;
2021-02-25 07:47:08 +00:00
}
2021-08-16 12:03:55 +00:00
void NormalizeSelectWithUnionQueryMatcher::getSelectsFromUnionListNode(ASTPtr ast_select, ASTs & selects)
2021-02-25 07:47:08 +00:00
{
2021-08-16 12:03:55 +00:00
if (const auto * inner_union = ast_select->as<ASTSelectWithUnionQuery>())
2021-02-25 07:47:08 +00:00
{
2021-08-16 12:03:55 +00:00
for (const auto & child : inner_union->list_of_selects->children)
2021-02-25 07:47:08 +00:00
getSelectsFromUnionListNode(child, selects);
return;
2021-05-16 14:14:59 +00:00
}
2021-02-25 07:47:08 +00:00
2021-05-16 14:14:59 +00:00
selects.push_back(ast_select);
2021-02-25 07:47:08 +00:00
}
void NormalizeSelectWithUnionQueryMatcher::visit(ASTPtr & ast, Data & data)
{
if (auto * select_union = ast->as<ASTSelectWithUnionQuery>())
visit(*select_union, data);
}
void NormalizeSelectWithUnionQueryMatcher::visit(ASTSelectWithUnionQuery & ast, Data & data)
{
auto & union_modes = ast.list_of_modes;
ASTs selects;
2021-08-16 12:03:55 +00:00
const auto & select_list = ast.list_of_selects->children;
if (select_list.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Got empty list of selects for ASTSelectWithUnionQuery");
2021-08-16 15:23:08 +00:00
/// Since nodes are traversed from bottom to top, we can also collect union modes from children up to parents.
SelectUnionModesSet current_set_of_modes;
2021-08-16 12:03:55 +00:00
bool distinct_found = false;
2021-02-25 07:47:08 +00:00
for (Int64 i = union_modes.size() - 1; i >= 0; --i)
2021-02-25 07:47:08 +00:00
{
2021-08-16 12:03:55 +00:00
current_set_of_modes.insert(union_modes[i]);
if (const auto * union_ast = typeid_cast<const ASTSelectWithUnionQuery *>(select_list[i + 1].get()))
{
const auto & current_select_modes = union_ast->set_of_modes;
current_set_of_modes.insert(current_select_modes.begin(), current_select_modes.end());
}
if (distinct_found)
continue;
2021-02-25 07:47:08 +00:00
/// Rewrite UNION Mode
2022-08-30 10:09:01 +00:00
if (union_modes[i] == SelectUnionMode::UNION_DEFAULT)
2021-02-25 07:47:08 +00:00
{
2022-08-30 10:09:01 +00:00
if (data.union_default_mode == SetOperationMode::ALL)
union_modes[i] = SelectUnionMode::UNION_ALL;
else if (data.union_default_mode == SetOperationMode::DISTINCT)
union_modes[i] = SelectUnionMode::UNION_DISTINCT;
2021-02-25 07:47:08 +00:00
else
2023-01-23 13:16:14 +00:00
throw Exception(DB::ErrorCodes::EXPECTED_ALL_OR_DISTINCT,
"Expected ALL or DISTINCT in SelectWithUnion query, because setting (union_default_mode) is empty");
2021-02-25 07:47:08 +00:00
}
2022-08-30 10:09:01 +00:00
if (union_modes[i] == SelectUnionMode::UNION_ALL)
2021-02-25 07:47:08 +00:00
{
2021-02-26 12:04:11 +00:00
if (auto * inner_union = select_list[i + 1]->as<ASTSelectWithUnionQuery>();
2022-08-30 10:09:01 +00:00
inner_union && inner_union->union_mode == SelectUnionMode::UNION_ALL)
2021-02-25 07:47:08 +00:00
{
/// Inner_union is an UNION ALL list, just lift up
for (auto child = inner_union->list_of_selects->children.rbegin(); child != inner_union->list_of_selects->children.rend();
++child)
2021-02-26 12:04:11 +00:00
selects.push_back(*child);
2021-02-25 07:47:08 +00:00
}
else
2021-02-26 12:04:11 +00:00
selects.push_back(select_list[i + 1]);
2021-02-25 07:47:08 +00:00
}
/// flatten all left nodes and current node to a UNION DISTINCT list
2022-08-30 10:09:01 +00:00
else if (union_modes[i] == SelectUnionMode::UNION_DISTINCT)
2021-02-25 07:47:08 +00:00
{
auto distinct_list = std::make_shared<ASTSelectWithUnionQuery>();
distinct_list->list_of_selects = std::make_shared<ASTExpressionList>();
distinct_list->children.push_back(distinct_list->list_of_selects);
for (int j = 0; j <= i + 1; ++j)
{
getSelectsFromUnionListNode(select_list[j], distinct_list->list_of_selects->children);
}
2022-08-30 10:09:01 +00:00
distinct_list->union_mode = SelectUnionMode::UNION_DISTINCT;
2021-02-25 07:47:08 +00:00
distinct_list->is_normalized = true;
selects.push_back(std::move(distinct_list));
2021-08-16 12:03:55 +00:00
distinct_found = true;
2021-02-25 07:47:08 +00:00
}
}
2021-08-16 12:03:55 +00:00
if (const auto * union_ast = typeid_cast<const ASTSelectWithUnionQuery *>(select_list[0].get()))
{
const auto & current_select_modes = union_ast->set_of_modes;
current_set_of_modes.insert(current_select_modes.begin(), current_select_modes.end());
}
2021-02-25 07:47:08 +00:00
/// No UNION DISTINCT or only one child in select_list
2021-08-16 12:03:55 +00:00
if (!distinct_found)
2021-02-25 07:47:08 +00:00
{
2021-02-26 12:04:11 +00:00
if (auto * inner_union = select_list[0]->as<ASTSelectWithUnionQuery>();
2022-08-30 10:09:01 +00:00
inner_union && inner_union->union_mode == SelectUnionMode::UNION_ALL)
2021-02-25 07:47:08 +00:00
{
/// Inner_union is an UNION ALL list, just lift it up
for (auto child = inner_union->list_of_selects->children.rbegin(); child != inner_union->list_of_selects->children.rend();
++child)
2021-02-26 12:04:11 +00:00
selects.push_back(*child);
2021-02-25 07:47:08 +00:00
}
else
2021-02-26 12:04:11 +00:00
selects.push_back(select_list[0]);
}
/// Just one union type child, lift it up
if (selects.size() == 1 && selects[0]->as<ASTSelectWithUnionQuery>())
{
ast = *(selects[0]->as<ASTSelectWithUnionQuery>());
2021-08-16 12:03:55 +00:00
ast.set_of_modes = std::move(current_set_of_modes);
2021-02-26 12:04:11 +00:00
return;
2021-02-25 07:47:08 +00:00
}
// reverse children list
std::reverse(selects.begin(), selects.end());
ast.is_normalized = true;
2022-08-30 10:09:01 +00:00
ast.union_mode = SelectUnionMode::UNION_ALL;
2021-08-16 12:03:55 +00:00
ast.set_of_modes = std::move(current_set_of_modes);
2021-02-25 07:47:08 +00:00
ast.list_of_selects->children = std::move(selects);
}
}