#include #include #include #include #include #include #include namespace DB { static void getSelectsFromUnionListNode(ASTPtr & ast_select, ASTs & selects) { if (auto * inner_union = ast_select->as()) { for (auto & child : inner_union->list_of_selects->children) getSelectsFromUnionListNode(child, selects); return; } selects.push_back(std::move(ast_select)); } bool ParserSelectWithUnionQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { ASTPtr list_node; ParserUnionList parser( std::make_unique(), std::make_unique("UNION"), std::make_unique("ALL"), std::make_unique("DISTINCT")); if (!parser.parse(pos, list_node, expected)) return false; auto select_with_union_query = std::make_shared(); node = select_with_union_query; select_with_union_query->list_of_selects = std::make_shared(); select_with_union_query->children.push_back(select_with_union_query->list_of_selects); select_with_union_query->union_modes = parser.getUnionModes(); // flatten inner union query for (auto & child : list_node->children) getSelectsFromUnionListNode(child, select_with_union_query->list_of_selects->children); return true; } }