1#include <Parsers/ExpressionListParsers.h>
2#include <Parsers/ParserSelectWithUnionQuery.h>
3#include <Parsers/ASTSelectWithUnionQuery.h>
4#include <Parsers/ParserUnionQueryElement.h>
5#include <Parsers/ASTExpressionList.h>
6#include <Common/typeid_cast.h>
7
8
9namespace DB
10{
11
12static void getSelectsFromUnionListNode(ASTPtr & ast_select, ASTs & selects)
13{
14 if (auto * inner_union = ast_select->as<ASTSelectWithUnionQuery>())
15 {
16 for (auto & child : inner_union->list_of_selects->children)
17 getSelectsFromUnionListNode(child, selects);
18
19 return;
20 }
21
22 selects.push_back(std::move(ast_select));
23}
24
25
26bool ParserSelectWithUnionQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
27{
28 ASTPtr list_node;
29
30 ParserList parser(std::make_unique<ParserUnionQueryElement>(), std::make_unique<ParserKeyword>("UNION ALL"), false);
31 if (!parser.parse(pos, list_node, expected))
32 return false;
33
34 auto select_with_union_query = std::make_shared<ASTSelectWithUnionQuery>();
35
36 node = select_with_union_query;
37 select_with_union_query->list_of_selects = std::make_shared<ASTExpressionList>();
38 select_with_union_query->children.push_back(select_with_union_query->list_of_selects);
39
40 // flatten inner union query
41 for (auto & child : list_node->children)
42 getSelectsFromUnionListNode(child, select_with_union_query->list_of_selects->children);
43
44 return true;
45}
46
47}
48