
**How to reproduce**
clickhouse server 21.1.4.46

```
CREATE TABLE table_case (number UInt32) ENGINE=Memory();

 INSERT INTO table_case SELECT * FROM numbers(100000000);
 
  set max_threads=1;
 SELECT
    multiIf(
    (
        SELECT count(*)
        FROM table_case
        WHERE (number % 20) = 2
    ) > 4094370,
    (
        SELECT avg(number)
        FROM table_case
        WHERE (number % 20) = 2
    ),
    (
        SELECT avg(rand())
        FROM table_case
        WHERE (number % 20) = 2
    )) AS bucket1,
    multiIf(
    (
        SELECT count(*)
        FROM table_case
        WHERE (number % 20) = 7
    ) > 5000000,
    (
        SELECT avg(number)
        FROM table_case
        WHERE (number % 20) = 7
    ),
    (
        SELECT avg(rand())
        FROM table_case
        WHERE (number % 20) = 7
    )) AS bucket2,
    multiIf(
    (
        SELECT count(*)
        FROM table_case
        WHERE (number % 10) = 3
    ) > 13000000,
    (
        SELECT avg(number)
        FROM table_case
        WHERE (number % 10) = 3
    ),
    (
        SELECT avg(rand())
        FROM table_case
        WHERE (number % 10) = 3
    )) AS bucket3,
    multiIf(
    (
        SELECT count(*)
        FROM table_case
        WHERE (number % 10) = 3
    ) > 23000000,
    (
        SELECT avg(number)
        FROM table_case
        WHERE (number % 10) = 3
    ),
    (
        SELECT avg(rand())
        FROM table_case
        WHERE (number % 10) = 3
    )) AS bucket4,
    multiIf(
    (
        SELECT count(*)
        FROM table_case
        WHERE (number % 17) = 4
    ) > 15000000,
    (
        SELECT avg(number)
        FROM table_case
        WHERE (number % 17) = 4
    ),
    (
        SELECT avg(rand())
        FROM table_case
        WHERE (number % 17) = 4
    )) AS bucket5

Query id: c03edf92-53d1-48f6-b4f6-3153599d07ae

┌──bucket1─┬───────────bucket2─┬────────────bucket3─┬────────────bucket4─┬──────────bucket5─┐
│ 49999992 │ 2147267165.746676 │ 2147653473.0356455 │ 2147653473.0356455 │ 2146430096.56865 │
└──────────┴───────────────────┴────────────────────┴────────────────────┴──────────────────┘

1 rows in set. Elapsed: 7.707 sec.

set max_threads=16;


┌──bucket1─┬───────────bucket2─┬───────────bucket3─┬───────────bucket4─┬───────────bucket5─┐
│ 49999992 │ 2147628607.640714 │ 2146571628.422384 │ 2146571628.422384 │ 2147553185.879362 │
└──────────┴───────────────────┴───────────────────┴───────────────────┴───────────────────┘

1 rows in set. Elapsed: 1.165 sec.
 
```

clickhouse-server  21.2.3.15

```
set max_threads=1;

┌──bucket1─┬──────────bucket2─┬─────────bucket3─┬─────────bucket4─┬────────────bucket5─┐
│ 49999992 │ 2147530694.57038 │ 2148494441.6794 │ 2148494441.6794 │ 2146861224.9913669 │
└──────────┴──────────────────┴─────────────────┴─────────────────┴────────────────────┘

1 rows in set. Elapsed: 7.824 sec.

 set max_threads=16;
 
 ┌──bucket1─┬────────────bucket2─┬────────────bucket3─┬────────────bucket4─┬────────────bucket5─┐
│ 49999992 │ 2145953907.4246264 │ 2147932668.7410493 │ 2147932668.7410493 │ 2147690139.9780807 │
└──────────┴────────────────────┴────────────────────┴────────────────────┴────────────────────┘

1 rows in set. Elapsed: 7.726 sec.

```


NameSet added_columns;
            for (auto & column : temp_actions->getResultColumns())
            {
                if (syntax->array_join_result_to_source.count(column.name))
                {
                    new_columns_after_array_join.emplace_back(column.name, column.type);
                    added_columns.emplace(column.name);
                }
            }
            for (auto & column : columns_after_array_join)
                if (added_columns.count(column.name) == 0)
                    new_columns_after_array_join.emplace_back(column.name, column.type);
            columns_after_array_join.swap(new_columns_after_array_join);
        }
        columns_after_array_join.insert(columns_after_array_join.end(), array_join_columns.begin(), array_join_columns.end());
        const ASTTablesInSelectQueryElement * join = select_query->join();
        if (join)
        {
            getRootActionsNoMakeSet(analyzedJoin().leftKeysList(), true, temp_actions, false);
            auto sample_columns = temp_actions->getResultColumns();
            analyzedJoin().addJoinedColumnsAndCorrectNullability(sample_columns);
            temp_actions = std::make_shared<ActionsDAG>(sample_columns);
        }
        columns_after_join = columns_after_array_join;
        const auto & added_by_join = analyzedJoin().columnsAddedByJoin();
        columns_after_join.insert(columns_after_join.end(), added_by_join.begin(), added_by_join.end());
    }
    has_aggregation = makeAggregateDescriptions(temp_actions);
    if (select_query && (select_query->groupBy() || select_query->having()))
        has_aggregation = true;