2018-02-25 00:50:53 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ParserSelectWithUnionQuery.h>
|
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2018-03-13 00:35:20 +00:00
|
|
|
#include <Parsers/ParserUnionQueryElement.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
2018-02-25 00:50:53 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2018-04-19 03:08:22 +00:00
|
|
|
|
2018-02-25 06:34:20 +00:00
|
|
|
bool ParserSelectWithUnionQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2018-02-25 00:50:53 +00:00
|
|
|
{
|
|
|
|
ASTPtr list_node;
|
|
|
|
|
2020-10-26 09:33:34 +00:00
|
|
|
ParserUnionList parser(
|
|
|
|
std::make_unique<ParserUnionQueryElement>(),
|
|
|
|
std::make_unique<ParserKeyword>("UNION"),
|
|
|
|
std::make_unique<ParserKeyword>("ALL"),
|
|
|
|
std::make_unique<ParserKeyword>("DISTINCT"));
|
2020-10-24 13:18:04 +00:00
|
|
|
|
2020-10-26 09:33:34 +00:00
|
|
|
if (!parser.parse(pos, list_node, expected))
|
|
|
|
return false;
|
2018-02-25 00:50:53 +00:00
|
|
|
|
2020-11-07 11:38:20 +00:00
|
|
|
/// NOTE: We cann't simply flatten inner union query now, since we may have different union mode in query,
|
|
|
|
/// so flatten may change it's semantics. For example:
|
|
|
|
/// flatten `SELECT 1 UNION (SELECT 1 UNION ALL SELETC 1)` -> `SELECT 1 UNION SELECT 1 UNION ALL SELECT 1`
|
|
|
|
|
2020-11-09 15:44:11 +00:00
|
|
|
/// If we got only one child which is ASTSelectWithUnionQuery, just lift it up
|
2020-11-07 11:38:20 +00:00
|
|
|
auto & expr_list = list_node->as<ASTExpressionList &>();
|
|
|
|
if (expr_list.children.size() == 1)
|
|
|
|
{
|
|
|
|
if (expr_list.children.at(0)->as<ASTSelectWithUnionQuery>())
|
|
|
|
{
|
|
|
|
node = std::move(expr_list.children.at(0));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:35:20 +00:00
|
|
|
auto select_with_union_query = std::make_shared<ASTSelectWithUnionQuery>();
|
2018-02-25 07:39:45 +00:00
|
|
|
|
2018-03-13 00:35:20 +00:00
|
|
|
node = select_with_union_query;
|
2020-11-09 15:44:11 +00:00
|
|
|
select_with_union_query->list_of_selects = list_node; //std::make_shared<ASTExpressionList>();
|
2018-03-13 00:35:20 +00:00
|
|
|
select_with_union_query->children.push_back(select_with_union_query->list_of_selects);
|
2020-11-09 15:44:11 +00:00
|
|
|
select_with_union_query->list_of_modes = parser.getUnionModes();
|
2020-11-07 11:38:20 +00:00
|
|
|
|
2018-02-25 00:50:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|