**Describe the bug**
Clickhouse seems to be unable to find the source for a column for the aggregation function for a column where the name is ambigious but specified with alias.

**How to reproduce**
* Which ClickHouse server version to use: 20.5.2.7
* Which interface to use, if matters: HTTP (but should not matter)
* Non-default settings, if any: None that I am aware of

```sql
CREATE TABLE `source` (
	`key` FixedString(15),
	`name` Int8,
	`value` Int8
) ENGINE = Log();

CREATE TABLE `target` (
	`key` FixedString(15),
	`name` Int8,
	`flower` Int8
) ENGINE = Log();

-- Query 1: Exception
SELECT 
	`source`.`key`,
	max(dummy.`key`)
FROM `source`
INNER JOIN (
	SELECT `key`, name, flower FROM `target`
) AS dummy ON `source`.name = dummy.name
GROUP BY `source`.`key`;

-- Query 2: Working
SELECT 
	`source`.`key`,
	max(dummy.`key2`)
FROM `source`
INNER JOIN (
	SELECT `key` as `key2`, name, flower FROM `target`
) AS dummy ON `source`.name = dummy.name
GROUP BY `source`.`key`
```

**Expected behavior**
Clickhouse should be able to distinguish between the the column `source.key` that is used in the `GROUP BY` statement and the column `dummy.key` (or `target.key`).

**Error message and/or stacktrace**
When running the first query, the following exception is returned from the database:
```
ClickHouse exception, code: 10, host: my.clickhouse.host, port: 8080; Code: 10, e.displayText() = DB::Exception: Not found column dummy.key in block. There are only columns: key (version 20.5.2.7 (official build))
```

**Additional context**
As you can see in the second query, if you remove any ambigious columns from the `SELECT` part, the problem seems to be gone.
This may be related to https://github.com/ClickHouse/ClickHouse/issues/12601

{
            data.has_identifier = true;
            if (data.key_names.count(group_key_ident->shortName()))
            {
                
                
                
                data.has_possible_collision = true;
            }
            data.key_names.insert(group_key_ident->shortName());
        }
        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);