**How to reproduce**

CH version: `20.5.2 revision 54435` (latest on `apt` at the moment). Cannot reproduce on `20.4.5`, so it's a regression.

Table:

```sql
create table repro_hits (
    date Date,
    metric Float64
) engine=MergeTree() order by date
```

Query:

```sql
SELECT
    date as period,
    1 as having_check,
    min(date) as period_start,
    addDays(max(date), 1) as period_end,
    dateDiff('second', period_start, period_end) as total_duration,
    sum(metric) as metric_
FROM repro_hits
GROUP BY period
having having_check != -1
```

**Error message and/or stacktrace**
```
Received exception from server (version 20.5.2):
Code: 179. DB::Exception: Received from localhost:9000. DB::Exception: Different expressions with the same alias period_start:
min(date) AS period_start
and
date AS period_start
.
```

The error goes away if I opt out of optimization with `SETTINGS enable_optimize_predicate_expression = 0`.
else if (auto * group_key_func = group_key->as<ASTFunction>())
        {
            data.key_names.insert(group_key_func->getColumnName());
        }
        else
        {
            data.key_names.insert(group_key->getColumnName());
        }
    }
    return data;
}
void optimizeGroupByFunctionKeys(ASTSelectQuery * select_query)
{
    if (!select_query->groupBy())
        return;
    auto grp_by = select_query->groupBy();
    auto & group_keys = grp_by->children;
    ASTs modified; 
    GroupByKeysInfo group_by_keys_data = getGroupByKeysInfo(group_keys);
    if (!group_by_keys_data.has_function || group_by_keys_data.has_possible_collision)
        return;
    GroupByFunctionKeysVisitor::Data visitor_data{group_by_keys_data.key_names};
    GroupByFunctionKeysVisitor(visitor_data).visit(grp_by);
    modified.reserve(group_keys.size());
    
    for (auto & group_key : group_keys)
    {
        if (auto * group_key_func = group_key->as<ASTFunction>())
        {
            if (group_by_keys_data.key_names.count(group_key_func->getColumnName()))