Repository: ClickHouse/ClickHouse
File: src/Storages/StorageMaterializedView.cpp (C++)

Code before:
    bool final,
    bool deduplicate,
    const Names & deduplicate_by_columns,
    bool cleanup,
    ContextPtr local_context)
{
    checkStatementCanBeForwarded();
    auto storage_ptr = getTargetTable();
    auto metadata_snapshot = storage_ptr->getInMemoryMetadataPtr();
    return storage_ptr->optimize(query, metadata_snapshot, partition, final, deduplicate, deduplicate_by_columns, cleanup, local_context);
}

ContextMutablePtr StorageMaterializedView::createRefreshContext(const String & log_comment) const
{
    ContextPtr table_context = getContext();
    ClientInfo client_info = table_context->getClientInfo();
    client_info.interface = ClientInfo::Interface::BACKGROUND;
    client_info.client_name = "refreshable materialized view";
    auto refresh_context = getInMemoryMetadataPtr()->getSQLSecurityOverriddenContext(table_context, &client_info);
    refresh_context->setClientInfo(client_info);
    refresh_context->setSetting("database_replicated_allow_replicated_engine_arguments", 3);
    refresh_context->setSetting("log_comment", log_comment);
    refresh_context->setQueryKind(ClientInfo::QueryKind::INITIAL_QUERY);
    /// Generate a random query id.
    refresh_context->setCurrentQueryId("");
    // Use the database where the materialized view is created to run the select query in the refresh task
    refresh_context->setCurrentDatabase(getStorageID().database_name);
    return refresh_context;
}

std::tuple<std::shared_ptr<ASTInsertQuery>, std::unique_ptr<CurrentThread::QueryScope>>
StorageMaterializedView::prepareRefresh(bool append, ContextMutablePtr refresh_context, std::optional<StorageID> & out_temp_table_id) const
{
    auto inner_table_id = getTargetTableId();
    StorageID target_table = inner_table_id;

    auto select_query = getInMemoryMetadataPtr()->getSelectQuery().select_query;
    InterpreterSetQuery::applySettingsFromQuery(select_query, refresh_context);

    if (!append)
    {
        CurrentThread::QueryScope query_scope(refresh_context);

        auto db = DatabaseCatalog::instance().getDatabase(inner_table_id.database_name);
        String db_name = db->getDatabaseName();
        auto new_table_name = ".tmp" + generateInnerTableName(getStorageID());

        /// Pre-check the permissions. Would be awkward if we create a temporary table and can't drop it.
        refresh_context->checkAccess(AccessType::DROP_TABLE | AccessType::CREATE_TABLE | AccessType::SELECT | AccessType::INSERT, db_name);

        auto create_query = std::dynamic_pointer_cast<ASTCreateQuery>(db->getCreateTableQuery(inner_table_id.table_name, getContext()));

Review comment (suggestion):
Comment formatting is inconsistent with the codebase. ClickHouse typically uses `///` for documentation comments and `//` should be followed by a space. Change to `/// Use the database where the materialized view is created to run the select query in the refresh task` for consistency.
```suggestion
    /// Use the database where the materialized view is created to run the select query in the refresh task
```

Code after:
    bool final,
    bool deduplicate,
    const Names & deduplicate_by_columns,
    bool cleanup,
    ContextPtr local_context)
{
    checkStatementCanBeForwarded();
    auto storage_ptr = getTargetTable();
    auto metadata_snapshot = storage_ptr->getInMemoryMetadataPtr();
    return storage_ptr->optimize(query, metadata_snapshot, partition, final, deduplicate, deduplicate_by_columns, cleanup, local_context);
}

ContextMutablePtr StorageMaterializedView::createRefreshContext(const String & log_comment) const
{
    ContextPtr table_context = getContext();
    ClientInfo client_info = table_context->getClientInfo();
    client_info.interface = ClientInfo::Interface::BACKGROUND;
    client_info.client_name = "refreshable materialized view";
    auto refresh_context = getInMemoryMetadataPtr()->getSQLSecurityOverriddenContext(table_context, &client_info);
    refresh_context->setClientInfo(client_info);
    refresh_context->setSetting("database_replicated_allow_replicated_engine_arguments", 3);
    refresh_context->setSetting("log_comment", log_comment);
    refresh_context->setQueryKind(ClientInfo::QueryKind::INITIAL_QUERY);
    /// Generate a random query id.
    refresh_context->setCurrentQueryId("");
    /// Use the database where the materialized view is created to run the select query in the refresh task
    refresh_context->setCurrentDatabase(getStorageID().database_name);
    return refresh_context;
}

std::tuple<boost::intrusive_ptr<ASTInsertQuery>, CurrentThread::QueryScope>
StorageMaterializedView::prepareRefresh(bool append, ContextMutablePtr refresh_context, std::optional<StorageID> & out_temp_table_id) const
{
    auto inner_table_id = getTargetTableId();
    StorageID target_table = inner_table_id;

    auto select_query = getInMemoryMetadataPtr()->getSelectQuery().select_query->clone();
    InterpreterSetQuery::applySettingsFromQuery(select_query, refresh_context);

    if (!append)
    {
       auto query_scope = CurrentThread::QueryScope::create(refresh_context);

        auto db = DatabaseCatalog::instance().getDatabase(inner_table_id.database_name);
        String db_name = db->getDatabaseName();
        auto new_table_name = ".tmp" + generateInnerTableName(getStorageID());

        /// Pre-check the permissions. Would be awkward if we create a temporary table and can't drop it.
        refresh_context->checkAccess(AccessType::DROP_TABLE | AccessType::CREATE_TABLE | AccessType::SELECT | AccessType::INSERT, db_name);

        auto create_query