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_parallelism;
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 = resolve_streaming_job_id_for_alter_parallelism(
35        &session,
36        obj_name,
37        stmt_type,
38        "resource group",
39    )?;
40
41    let resource_group = resource_group
42        .map(resolve_resource_group)
43        .transpose()?
44        .flatten();
45
46    let mut builder = RwPgResponse::builder(stmt_type);
47
48    let catalog_writer = session.catalog_writer()?;
49    catalog_writer
50        .alter_resource_group(job_id, resource_group, deferred)
51        .await?;
52
53    if deferred {
54        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());
55    }
56
57    Ok(builder.into())
58}
59
60// Resolve the resource group from the given SetVariableValue.
61pub(crate) fn resolve_resource_group(resource_group: SetVariableValue) -> Result<Option<String>> {
62    Ok(match resource_group {
63        SetVariableValue::Single(SetVariableValueSingle::Ident(ident)) => Some(ident.real_value()),
64        SetVariableValue::Single(SetVariableValueSingle::Literal(Value::SingleQuotedString(v)))
65            if v.as_str().eq_ignore_ascii_case(DEFAULT_RESOURCE_GROUP) =>
66        {
67            None
68        }
69        SetVariableValue::Single(SetVariableValueSingle::Literal(Value::SingleQuotedString(v))) => {
70            Some(v)
71        }
72        SetVariableValue::Default => None,
73        _ => {
74            return Err(ErrorCode::InvalidInputSyntax(
75                "target resource group must be a valid string or default".to_owned(),
76            )
77            .into());
78        }
79    })
80}