Repository: risingwavelabs/risingwave
File: src/frontend/src/handler/alter_streaming_rate_limit.rs (Rust)

Code before:
use risingwave_pb::meta::ThrottleTarget as PbThrottleTarget;
use risingwave_sqlparser::ast::ObjectName;

use super::{HandlerArgs, RwPgResponse};
use crate::Binder;
use crate::catalog::root_catalog::SchemaPath;
use crate::catalog::table_catalog::TableType;
use crate::error::{ErrorCode, Result};
use crate::handler::util::{LongRunningNotificationAction, execute_with_long_running_notification};
use crate::session::SessionImpl;

pub async fn handle_alter_streaming_rate_limit(
    handler_args: HandlerArgs,
    throttle_target: PbThrottleTarget,
    throttle_type: PbThrottleType,
    table_name: ObjectName,
    rate_limit: i32,
) -> Result<RwPgResponse> {
    let session = handler_args.clone().session;
    let db_name = &session.database();
    let (schema_name, real_table_name) =
        Binder::resolve_schema_qualified_name(db_name, &table_name)?;
    let search_path = session.config().search_path();
    let user_name = &session.user_name();

    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);

    let (stmt_type, id) = match (throttle_target, throttle_type) {
        (PbThrottleTarget::Mv, PbThrottleType::Backfill) => {
            let reader = session.env().catalog_reader().read_guard();
            let (table, schema_name) =
                reader.get_any_table_by_name(db_name, schema_path, &real_table_name)?;
            if table.table_type != TableType::MaterializedView {
                return Err(ErrorCode::InvalidInputSyntax(format!(
                    "\"{table_name}\" is not a materialized view",
                ))
                .into());
            }
            session.check_privilege_for_drop_alter(schema_name, &**table)?;
            (StatementType::ALTER_MATERIALIZED_VIEW, table.id.as_raw_id())
        }
        (PbThrottleTarget::Source, PbThrottleType::Source) => {
            let reader = session.env().catalog_reader().read_guard();
            let (source, schema_name) =
                reader.get_source_by_name(db_name, schema_path, &real_table_name)?;
            session.check_privilege_for_drop_alter(schema_name, &**source)?;
            (StatementType::ALTER_SOURCE, source.id.as_raw_id())
        }
        (PbThrottleTarget::Table, PbThrottleType::Dml) => {
            let reader = session.env().catalog_reader().read_guard();
            let (table, schema_name) =

Review comment (suggestion):
Review note: comparison between previous and current.
```
Mv -> (Mv, Backfill)
Source -> (Source, Source)
TableWithSource -> (Table, Source)
CdcTable -> (Table, Backfill)
TableDml -> (Table, Dml)
Sink -> (Sink, (Sink or Backfill))
```

Code after:
use risingwave_pb::meta::ThrottleTarget as PbThrottleTarget;
use risingwave_sqlparser::ast::ObjectName;

use super::{HandlerArgs, RwPgResponse};
use crate::catalog::root_catalog::{Catalog, SchemaPath};
use crate::catalog::table_catalog::TableType;
use crate::error::{ErrorCode, Result};
use crate::handler::util::{LongRunningNotificationAction, execute_with_long_running_notification};
use crate::session::SessionImpl;
use crate::{Binder, TableCatalog};

fn check_table_mismatch<'a>(
    reader: &'a Catalog,
    session: &SessionImpl,
    db_name: &str,
    schema_path: SchemaPath<'_>,
    table_name: &str,
) -> Result<&'a TableCatalog> {
    let (table, schema_name) =
        reader.get_created_table_by_name(db_name, schema_path, table_name)?;
    if table.table_type != TableType::Table {
        return Err(
            ErrorCode::InvalidInputSyntax(format!("\"{table_name}\" is not a TABLE",)).into(),
        );
    }
    session.check_privilege_for_drop_alter(schema_name, &**table)?;
    Ok(table)
}

pub async fn handle_alter_streaming_rate_limit(
    handler_args: HandlerArgs,
    throttle_target: PbThrottleTarget,
    throttle_type: PbThrottleType,
    table_name: ObjectName,
    rate_limit: i32,
) -> Result<RwPgResponse> {
    let session = handler_args.clone().session;
    let db_name = &session.database();
    let (schema_name, real_table_name) =
        Binder::resolve_schema_qualified_name(db_name, &table_name)?;
    let search_path = session.config().search_path();
    let user_name = &session.user_name();

    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);

    let (stmt_type, id) = match (throttle_target, throttle_type) {
        (PbThrottleTarget::Mv, PbThrottleType::Backfill) => {
            let reader = session.env().catalog_reader().read_guard();
            let (table, schema_name) =
                reader.get_any_table_by_name(db_name, schema_path, &real_table_name)?;
            if table.table_type != TableType::MaterializedView {