How to reproduce:

```sql
EXPLAIN SYNTAX
SELECT x
FROM 
(
    SELECT 1 AS x
    UNION DISTINCT
    SELECT 1
)

Query id: e777bc02-5d1b-4d49-ba89-4ebe31172387


Exception on client:
Code: 32. DB::Exception: Attempt to read after eof: while receiving packet from localhost:9000

```

union_modes[i] = ASTSelectWithUnionQuery::Mode::ALL;
                else if (union_default_mode == UnionMode::DISTINCT)
                    union_modes[i] = ASTSelectWithUnionQuery::Mode::DISTINCT;
                else
                    throw Exception(
                        "Expected ALL or DISTINCT in SelectWithUnion query, because setting (union_default_mode) is empty",
                        DB::ErrorCodes::EXPECTED_ALL_OR_DISTINCT);
            }
            if (union_modes[i] == ASTSelectWithUnionQuery::Mode::ALL)
            {
                if (auto * inner_union = select_list[i + 1]->as<ASTSelectWithUnionQuery>())
                {
                    
                    for (auto child = inner_union->list_of_selects->children.rbegin();
                         child != inner_union->list_of_selects->children.rend();
                         ++child)
                        selects.push_back(std::move(*child));
                }
                else
                    selects.push_back(std::move(select_list[i + 1]));
            }
            
            else if (union_modes[i] == ASTSelectWithUnionQuery::Mode::DISTINCT)
            {
                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);