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

```

{
        if (auto * inner_union = ast_select->as<ASTSelectWithUnionQuery>())
        {
            for (auto & child : inner_union->list_of_selects->children)
                getSelectsFromUnionListNode(child, selects);
            return;
        }
        selects.push_back(std::move(ast_select));
    }
    void visit(ASTSelectWithUnionQuery & ast, ASTPtr &) const
    {
        auto & union_modes = ast.list_of_modes;
        ASTs selects;
        auto & select_list = ast.list_of_selects->children;
        int i;
        for (i = union_modes.size() - 1; i >= 0; --i)
        {
            
            if (union_modes[i] == ASTSelectWithUnionQuery::Mode::Unspecified)
            {
                if (union_default_mode == UnionMode::ALL)