```
CREATE TABLE t1 (c0 Int32, PRIMARY KEY (c0)) ENGINE = MergeTree;
```
```
SELECT DISTINCT *
FROM
(
    SELECT DISTINCT
        cos(sign(exp(t1.c0))),
        -min2(pow(t1.c0, t1.c0), intDiv(t1.c0, t1.c0)),
        t1.c0,
        t1.c0,
        erf(abs(-t1.c0))
    FROM t1
    WHERE t1.c0 > 0
    UNION ALL
    SELECT DISTINCT
        cos(sign(exp(t1.c0))),
        -min2(pow(t1.c0, t1.c0), intDiv(t1.c0, t1.c0)),
        t1.c0,
        t1.c0,
        erf(abs(-t1.c0))
    FROM t1
    WHERE NOT (t1.c0 > 0)
    UNION ALL
    SELECT DISTINCT
        cos(sign(exp(t1.c0))),
        -min2(pow(t1.c0, t1.c0), intDiv(t1.c0, t1.c0)),
        t1.c0,
        t1.c0,
        erf(abs(-t1.c0))
    FROM t1
    WHERE t1.c0 > (0 IS NULL)
)
```

```
Received exception from server (version 22.13.1):
Code: 49. DB::Exception: Received from localhost:9000. DB::Exception: Different order of columns in UNION subquery: cos(sign(exp(c0))), negate(min2(pow(c0, c0), intDiv(c0, c0))), c0, erf(abs(negate(c0))) and cos(sign(exp(c0))), negate(min2(pow(c0, c0), intDiv(c0, c0))), c0, erf(abs(negate(c0))), c0. Stack trace:

0. ./build_docker/../src/Common/Exception.cpp:77: DB::Exception::Exception(DB::Exception::MessageMasked const&, int, bool) @ 0xebc695a in /home/ubuntu/release-ch/clickhouse
1. ./build_docker/../contrib/llvm-project/libcxx/include/string:1499: DB::Exception::Exception<fmt::v8::join_view<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, char>, fmt::v8::join_view<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, char>>(int, fmt::v8::basic_format_string<char, fmt::v8::type_identity<fmt::v8::join_view<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, char>>::type, fmt::v8::type_identity<fmt::v8::join_view<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, char>>::type>, fmt::v8::join_view<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, char>&&, fmt::v8::join_view<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const*>, char>&&) @ 0x1489ffa1 in /home/ubuntu/release-ch/clickhouse
2. ./build_docker/../src/Interpreters/InterpreterSelectWithUnionQuery.cpp:174: DB::InterpreterSelectWithUnionQuery::InterpreterSelectWithUnionQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context>, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) @ 0x1489ed58 in /home/ubuntu/release-ch/clickhouse
3. ./build_docker/../contrib/llvm-project/libcxx/include/__memory/shared_ptr.h:701: DB::InterpreterSelectWithUnionQuery::InterpreterSelectWithUnionQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context const>, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) @ 0x1489c92c in /home/ubuntu/release-ch/clickhouse
4. ./build_docker/../contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:302: DB::InterpreterSelectQuery::executeFetchColumns(DB::QueryProcessingStage::Enum, DB::QueryPlan&) @ 0x14817f99 in /home/ubuntu/release-ch/clickhouse
5. ./build_docker/../src/Interpreters/InterpreterSelectQuery.cpp:1308: DB::InterpreterSelectQuery::executeImpl(DB::QueryPlan&, std::__1::optional<DB::Pipe>) @ 0x1480bacf in /home/ubuntu/release-ch/clickhouse
6. ./build_docker/../contrib/llvm-project/libcxx/include/optional:260: DB::InterpreterSelectQuery::buildQueryPlan(DB::QueryPlan&) @ 0x1480ab8d in /home/ubuntu/release-ch/clickhouse
7. ./build_docker/../src/Interpreters/InterpreterSelectWithUnionQuery.cpp:0: DB::InterpreterSelectWithUnionQuery::buildQueryPlan(DB::QueryPlan&) @ 0x148a24c5 in /home/ubuntu/release-ch/clickhouse
8. ./build_docker/../src/Interpreters/InterpreterSelectWithUnionQuery.cpp:0: DB::InterpreterSelectWithUnionQuery::execute() @ 0x148a34de in /home/ubuntu/release-ch/clickhouse
9. ./build_docker/../src/Interpreters/executeQuery.cpp:0: DB::executeQueryImpl(char const*, char const*, std::__1::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum, DB::ReadBuffer*) @ 0x14bad053 in /home/ubuntu/release-ch/clickhouse
10. ./build_docker/../src/Interpreters/executeQuery.cpp:1104: DB::executeQuery(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum) @ 0x14baa10d in /home/ubuntu/release-ch/clickhouse
11. ./build_docker/../src/Server/TCPHandler.cpp:0: DB::TCPHandler::runImpl() @ 0x15881f4f in /home/ubuntu/release-ch/clickhouse
12. ./build_docker/../src/Server/TCPHandler.cpp:1922: DB::TCPHandler::run() @ 0x15895b99 in /home/ubuntu/release-ch/clickhouse
13. ./build_docker/../contrib/poco/Net/src/TCPServerConnection.cpp:57: Poco::Net::TCPServerConnection::start() @ 0x18703734 in /home/ubuntu/release-ch/clickhouse
14. ./build_docker/../contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:48: Poco::Net::TCPServerDispatcher::run() @ 0x187050db in /home/ubuntu/release-ch/clickhouse
15. ./build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:213: Poco::PooledThread::run() @ 0x18894307 in /home/ubuntu/release-ch/clickhouse
16. ./build_docker/../contrib/poco/Foundation/include/Poco/SharedPtr.h:277: Poco::ThreadImpl::runnableEntry(void*) @ 0x18891d3d in /home/ubuntu/release-ch/clickhouse
17. ? @ 0x7fbaad072609 in ?
18. clone @ 0x7fbaacf97163 in ?
. (LOGICAL_ERROR)
```

With analyzer works

if (expressions.has_order_by)
            {
                /** If there is an ORDER BY for distributed query processing,
                  *  but there is no aggregation, then on the remote servers ORDER BY was made
                  *  - therefore, we merge the sorted streams from remote servers.
                  *
                  * Also in case of remote servers was process the query up to WithMergeableStateAfterAggregationAndLimit
                  * (distributed_group_by_no_merge=2 or optimize_distributed_group_by_sharding_key=1 takes place),
                  * then merge the sorted streams is enough, since remote servers already did full ORDER BY.
                  */
                if (from_aggregation_stage)
                    executeMergeSorted(query_plan, "after aggregation stage for ORDER BY");
                else if (!expressions.first_stage
                    && !expressions.need_aggregate
                    && !expressions.has_window
                    && !(query.group_by_with_totals && !aggregate_final))
                    executeMergeSorted(query_plan, "for ORDER BY, without aggregation");
                else    
                    executeOrder(query_plan, input_order_info_for_order);
            }
            /** Optimization - if there are several sources and there is LIMIT, then first apply the preliminary LIMIT,
              * limiting the number of rows in each up to `offset + limit`.
              */
            bool has_withfill = false;
            if (query.orderBy())
            {
                SortDescription order_descr = getSortDescription(query, context);
                for (auto & desc : order_descr)
                    if (desc.with_fill)
                    {