Repository: ClickHouse/ClickHouse
File: src/Analyzer/createUniqueAliasesIfNecessary.cpp (C++)

Code before:

    // We need to use raw pointer as a key, not a QueryTreeNodePtrWithHash.
    std::unordered_map<QueryTreeNodePtr, String> table_expression_to_alias;
};

class CreateUniqueArrayJoinAliasesVisitor : public InDepthQueryTreeVisitorWithContext<CreateUniqueArrayJoinAliasesVisitor>
{
public:
    using Base = InDepthQueryTreeVisitorWithContext<CreateUniqueArrayJoinAliasesVisitor>;
    using Base::Base;

    void enterImpl(QueryTreeNodePtr & node)
    {
        if (auto * array_join_typed = node->as<ArrayJoinNode>())
        {
            populateRenamingMap(array_join_typed, renaming[array_join_typed]);
            return;
        }

        auto * column_node = node->as<ColumnNode>();
        if (!column_node || replaced_nodes_set.contains(node))
            return;

        auto column_source = column_node->getColumnSource();
        auto * array_join = column_source->as<ArrayJoinNode>();
        if (!array_join)
            return;

        auto & renaming_map = getRenamingMap(array_join);

        auto new_column = column_node->getColumn();
        new_column.name = renaming_map[column_node->getColumnName()];
        auto new_column_node = std::make_shared<ColumnNode>(new_column, column_source);

        node = std::move(new_column_node);
        replaced_nodes_set.insert(node);
    }

private:

    using RenamingMap = std::unordered_map<String, String>;

    void populateRenamingMap(ArrayJoinNode * array_join, RenamingMap & result)
    {
        if (result.empty())
        {
            for (auto & array_join_expression : array_join->getJoinExpressions())
            {
                auto * array_join_column = array_join_expression->as<ColumnNode>();
                chassert(array_join_column != nullptr);


Review comment (suggestion):
This is a very dangerous change that breaks the main invariant of the query tree. I'll revert it.

Code after:

    // We need to use raw pointer as a key, not a QueryTreeNodePtrWithHash.
    std::unordered_map<QueryTreeNodePtr, String> table_expression_to_alias;
};

class CreateUniqueArrayJoinAliasesVisitor : public InDepthQueryTreeVisitorWithContext<CreateUniqueArrayJoinAliasesVisitor>
{
public:
    using Base = InDepthQueryTreeVisitorWithContext<CreateUniqueArrayJoinAliasesVisitor>;
    using Base::Base;

    void enterImpl(QueryTreeNodePtr & node)
    {
        if (auto * array_join_typed = node->as<ArrayJoinNode>())
        {
            populateRenamingMap(array_join_typed, renaming[array_join_typed]);
            return;
        }

        auto * column_node = node->as<ColumnNode>();
        if (!column_node || replaced_nodes_set.contains(node))
            return;

        auto column_source = column_node->getColumnSourceOrNull();
        if (!column_source)
            return;

        auto * array_join = column_source->as<ArrayJoinNode>();
        if (!array_join)
            return;

        auto & renaming_map = getRenamingMap(array_join);

        auto new_column = column_node->getColumn();
        new_column.name = renaming_map[column_node->getColumnName()];
        auto new_column_node = std::make_shared<ColumnNode>(new_column, column_source);

        node = std::move(new_column_node);
        replaced_nodes_set.insert(node);
    }

private:

    using RenamingMap = std::unordered_map<String, String>;

    void populateRenamingMap(ArrayJoinNode * array_join, RenamingMap & result)
    {
        if (result.empty())
        {
            for (auto & array_join_expression : array_join->getJoinExpressions())
            {