Skip to main content

risingwave_frontend/handler/
alter_resource_group.rs

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
15use pgwire::pg_response::StatementType;
16use risingwave_common::util::worker_util::DEFAULT_RESOURCE_GROUP;
17use risingwave_sqlparser::ast::{ObjectName, SetVariableValue, SetVariableValueSingle, Value};
18
19use super::alter_utils::resolve_streaming_job_id_for_alter;
20use super::{HandlerArgs, RwPgResponse};
21use crate::error::{ErrorCode, Result};
22
23pub async fn handle_alter_resource_group(
24    handler_args: HandlerArgs,
25    obj_name: ObjectName,
26    resource_group: Option<SetVariableValue>,
27    stmt_type: StatementType,
28    deferred: bool,
29) -> Result<RwPgResponse> {
30    let session = handler_args.session;
31
32    risingwave_common::license::Feature::ResourceGroup.check_available()?;
33
34    let job_id =
35        resolve_streaming_job_id_for_alter(&session, obj_name, stmt_type, "resource group")?;
36
37    let resource_group = resource_group
38        .map(resolve_resource_group)
39        .transpose()?
40        .flatten();
41
42    let mut builder = RwPgResponse::builder(stmt_type);
43
44    let catalog_writer = session.catalog_writer()?;
45    catalog_writer
46        .alter_resource_group(job_id, resource_group, deferred)
47        .await?;
48
49    if deferred {
50        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());
51    }
52
53    Ok(builder.into())
54}
55
56// Resolve the resource group from the given SetVariableValue.
57pub(crate) fn resolve_resource_group(resource_group: SetVariableValue) -> Result<Option<String>> {
58    Ok(match resource_group {
59        SetVariableValue::Single(SetVariableValueSingle::Ident(ident)) => Some(ident.real_value()),
60        SetVariableValue::Single(SetVariableValueSingle::Literal(Value::SingleQuotedString(v)))
61            if v.as_str().eq_ignore_ascii_case(DEFAULT_RESOURCE_GROUP) =>
62        {
63            None
64        }
65        SetVariableValue::Single(SetVariableValueSingle::Literal(Value::SingleQuotedString(v))) => {
66            Some(v)
67        }
68        SetVariableValue::Default => None,
69        _ => {
70            return Err(ErrorCode::InvalidInputSyntax(
71                "target resource group must be a valid string or default".to_owned(),
72            )
73            .into());
74        }
75    })
76}