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

{
    RewriteAnyFunctionVisitor::Data data = {};
    RewriteAnyFunctionVisitor(data).visit(query);
}
void optimizeInjectiveFunctionsInsideUniq(ASTPtr & query, const Context & context)
{
    RemoveInjectiveFunctionsVisitor::Data data = {context};
    RemoveInjectiveFunctionsVisitor(data).visit(query);
}
void transformIfStringsIntoEnum(ASTPtr & query)
{
    std::unordered_set<String> function_names = {"if", "transform"};
    std::unordered_set<String> used_as_argument;
    FindUsedFunctionsVisitor::Data used_data{function_names, used_as_argument};
    FindUsedFunctionsVisitor(used_data).visit(query);
    ConvertStringsToEnumVisitor::Data convert_data{used_as_argument};
    ConvertStringsToEnumVisitor(convert_data).visit(query);
}
}
void TreeOptimizer::optimizeIf(ASTPtr & query, Aliases & aliases, bool if_chain_to_multiif)
{
    
    OptimizeIfWithConstantConditionVisitor(aliases).visit(query);
    if (if_chain_to_multiif)
        OptimizeIfChainsVisitor().visit(query);
}
void TreeOptimizer::apply(ASTPtr & query, Aliases & aliases, const NameSet & source_columns_set,
                          const std::vector<TableWithColumnNamesAndTypes> & tables_with_columns,
                          const Context & context, const StorageMetadataPtr & metadata_snapshot,
                          bool & rewrite_subqueries)