Repository: ClickHouse/ClickHouse
File: src/Interpreters/DatabaseCatalog.cpp (C++)

Code before:
    return loading_dependencies.getDependents(table_id);
}

std::tuple<std::vector<StorageID>, std::vector<StorageID>, std::vector<StorageID>> DatabaseCatalog::removeDependencies(
    const StorageID & table_id, bool check_referential_dependencies, bool check_loading_dependencies, bool is_drop_database, bool is_mv, bool is_rename)
{
    std::lock_guard lock{databases_mutex};
    checkTableCanBeRemovedOrRenamedUnlocked(table_id, check_referential_dependencies, check_loading_dependencies, is_drop_database);
    std::vector<StorageID> old_view_dependencies;

    if (is_mv)
    {
        auto tables_from = view_dependencies.getDependents(table_id);
        if (!tables_from.empty())
        {
            assert(tables_from.size() == 1);
            const auto & the_table_from = *tables_from.begin();

            view_dependencies.removeDependency(the_table_from, table_id, /* remove_isolated_tables= */ true);
            old_view_dependencies.push_back(the_table_from);
        }
    }

    /// Don't remove named collection dependencies during rename - they will be updated by renameDependencies
    if (!is_rename)
        NamedCollectionFactory::instance().removeDependencies(table_id);

    return {
        referential_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true),
        loading_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true),
        old_view_dependencies};
}

void DatabaseCatalog::updateDependencies(
    const StorageID & table_id,
    const TableNamesSet & new_referential_dependencies,
    const TableNamesSet & new_loading_dependencies,
    const TableNamesSet & new_view_dependencies)
{
    std::lock_guard lock{databases_mutex};
    referential_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true);
    loading_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true);
    if (!new_referential_dependencies.empty())
        referential_dependencies.addDependencies(table_id, new_referential_dependencies);
    if (!new_loading_dependencies.empty())
        loading_dependencies.addDependencies(table_id, new_loading_dependencies);
    if (!new_view_dependencies.empty())
    {
        assert(new_view_dependencies.size() == 1);
        auto tables_from = view_dependencies.getDependents(table_id);
        if (!tables_from.empty())

Review comment (suggestion):
It doesn't look like it belongs here. Probably it's better to just call it from `InterpreterDropQuery`.

Code after:
}

std::tuple<std::vector<StorageID>, std::vector<StorageID>, std::vector<StorageID>> DatabaseCatalog::removeDependencies(
    const StorageID & table_id, bool check_referential_dependencies, bool check_loading_dependencies, bool is_drop_database, bool is_mv)
{
    std::lock_guard lock{databases_mutex};
    checkTableCanBeRemovedOrRenamedUnlocked(table_id, check_referential_dependencies, check_loading_dependencies, is_drop_database);
    std::vector<StorageID> old_view_dependencies;

    if (is_mv)
    {
        auto tables_from = view_dependencies.getDependents(table_id);
        if (!tables_from.empty())
        {
            assert(tables_from.size() == 1);
            const auto & the_table_from = *tables_from.begin();

            view_dependencies.removeDependency(the_table_from, table_id, /* remove_isolated_tables= */ true);
            old_view_dependencies.push_back(the_table_from);
        }
    }

    return {
        referential_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true),
        loading_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true),
        old_view_dependencies};
}

void DatabaseCatalog::updateDependencies(
    const StorageID & table_id,
    const TableNamesSet & new_referential_dependencies,
    const TableNamesSet & new_loading_dependencies,
    const TableNamesSet & new_view_dependencies)
{
    std::lock_guard lock{databases_mutex};
    referential_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true);
    loading_dependencies.removeDependencies(table_id, /* remove_isolated_tables= */ true);
    if (!new_referential_dependencies.empty())
        referential_dependencies.addDependencies(table_id, new_referential_dependencies);
    if (!new_loading_dependencies.empty())
        loading_dependencies.addDependencies(table_id, new_loading_dependencies);
    if (!new_view_dependencies.empty())
    {
        assert(new_view_dependencies.size() == 1);
        auto tables_from = view_dependencies.getDependents(table_id);
        if (!tables_from.empty())
        {
            assert(tables_from.size() == 1);
            const auto & the_table_from = *tables_from.begin();

            view_dependencies.removeDependency(the_table_from, table_id, /* remove_isolated_tables= */ true);