|
| 1 | +// Copyright 2025 RisingWave Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use pgwire::pg_response::StatementType; |
| 16 | +use risingwave_common::bail; |
| 17 | +use risingwave_sqlparser::ast::{ObjectName, SetVariableValue, SetVariableValueSingle, Value}; |
| 18 | + |
| 19 | +use super::{HandlerArgs, RwPgResponse}; |
| 20 | +use crate::catalog::root_catalog::SchemaPath; |
| 21 | +use crate::catalog::table_catalog::TableType; |
| 22 | +use crate::error::{ErrorCode, Result}; |
| 23 | +use crate::Binder; |
| 24 | + |
| 25 | +pub async fn handle_alter_resource_group( |
| 26 | + handler_args: HandlerArgs, |
| 27 | + obj_name: ObjectName, |
| 28 | + resource_group: Option<SetVariableValue>, |
| 29 | + stmt_type: StatementType, |
| 30 | + deferred: bool, |
| 31 | +) -> Result<RwPgResponse> { |
| 32 | + let session = handler_args.session; |
| 33 | + let db_name = session.database(); |
| 34 | + let (schema_name, real_table_name) = |
| 35 | + Binder::resolve_schema_qualified_name(&db_name, obj_name.clone())?; |
| 36 | + let search_path = session.config().search_path(); |
| 37 | + let user_name = &session.auth_context().user_name; |
| 38 | + let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name); |
| 39 | + |
| 40 | + let table_id = { |
| 41 | + let reader = session.env().catalog_reader().read_guard(); |
| 42 | + |
| 43 | + match stmt_type { |
| 44 | + StatementType::ALTER_MATERIALIZED_VIEW => { |
| 45 | + let (table, schema_name) = |
| 46 | + reader.get_created_table_by_name(&db_name, schema_path, &real_table_name)?; |
| 47 | + |
| 48 | + match (table.table_type(), stmt_type) { |
| 49 | + (TableType::MaterializedView, StatementType::ALTER_MATERIALIZED_VIEW) => {} |
| 50 | + _ => { |
| 51 | + return Err(ErrorCode::InvalidInputSyntax(format!( |
| 52 | + "cannot alter resource group of {} {} by {}", |
| 53 | + table.table_type().to_prost().as_str_name(), |
| 54 | + table.name(), |
| 55 | + stmt_type, |
| 56 | + )) |
| 57 | + .into()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + session.check_privilege_for_drop_alter(schema_name, &**table)?; |
| 62 | + table.id.table_id() |
| 63 | + } |
| 64 | + _ => bail!( |
| 65 | + "invalid statement type for alter resource group: {:?}", |
| 66 | + stmt_type |
| 67 | + ), |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + let resource_group = match resource_group { |
| 72 | + None => None, |
| 73 | + Some(SetVariableValue::Single(SetVariableValueSingle::Ident(ident))) => { |
| 74 | + Some(ident.real_value()) |
| 75 | + } |
| 76 | + Some(SetVariableValue::Single(SetVariableValueSingle::Literal( |
| 77 | + Value::SingleQuotedString(v), |
| 78 | + ))) => Some(v), |
| 79 | + _ => { |
| 80 | + return Err(ErrorCode::InvalidInputSyntax( |
| 81 | + "target parallelism must be a valid number or adaptive".to_owned(), |
| 82 | + ) |
| 83 | + .into()); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + let mut builder = RwPgResponse::builder(stmt_type); |
| 88 | + |
| 89 | + let catalog_writer = session.catalog_writer()?; |
| 90 | + catalog_writer |
| 91 | + .alter_resource_group(table_id, resource_group, deferred) |
| 92 | + .await?; |
| 93 | + |
| 94 | + if deferred { |
| 95 | + builder = builder.notice("DEFERRED is used, please ensure that automatic parallelism control is enabled on the meta, otherwise, the alter will not take effect.".to_owned()); |
| 96 | + } |
| 97 | + |
| 98 | + Ok(builder.into()) |
| 99 | +} |
0 commit comments