
**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.

```


}
    if (before_order_by)
    {
        ss << "before_order_by " << before_order_by->dumpDAG() << "\n";
    }
    if (before_limit_by)
    {
        ss << "before_limit_by " << before_limit_by->dumpDAG() << "\n";
    }
    if (final_projection)
    {
        ss << "final_projection " << final_projection->dumpDAG() << "\n";
    }
    if (!selected_columns.empty())
    {
        ss << "selected_columns ";
        for (size_t i = 0; i < selected_columns.size(); i++)
        {
            if (i > 0)
            {
                ss << ", ";
            }
            ss << backQuote(selected_columns[i]);
        }
        ss << "\n";
    }
    return ss.str();
}
}