Skip to main content

risingwave_frontend/handler/
alter_source_props.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 risingwave_common::id::{ConnectionId, SourceId};
16use risingwave_pb::catalog::connection::Info::ConnectionParams;
17
18use super::RwPgResponse;
19use crate::catalog::catalog_service::CatalogReadGuard;
20use crate::catalog::root_catalog::SchemaPath;
21use crate::error::{ErrorCode, Result};
22use crate::handler::{HandlerArgs, ObjectName, SqlOption, StatementType};
23use crate::session::SessionImpl;
24use crate::utils::resolve_connection_ref_and_secret_ref;
25use crate::{Binder, WithOptions};
26
27pub async fn handle_alter_table_connector_props(
28    handler_args: HandlerArgs,
29    table_name: ObjectName,
30    alter_props: Vec<SqlOption>,
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, &table_name)?;
36    let search_path = session.config().search_path();
37    let user_name = &session.user_name();
38    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
39
40    let source_id = {
41        let reader = session.env().catalog_reader().read_guard();
42        let (table, schema_name) =
43            reader.get_any_table_by_name(db_name, schema_path, &real_table_name)?;
44        let Some(associate_source_id) = table.associated_source_id else {
45            return Err(ErrorCode::InvalidInputSyntax(
46                "Only table with connector can use ALTER TABLE CONNECTOR syntax.".to_owned(),
47            )
48            .into());
49        };
50
51        session.check_privilege_for_drop_alter(schema_name, &**table)?;
52        let (source_catalog, _) =
53            reader.get_source_by_id(db_name, schema_path, associate_source_id)?;
54
55        ensure_alter_props_not_set_by_connection(
56            &reader,
57            db_name,
58            source_catalog.connection_id,
59            &alter_props,
60        )?;
61
62        tracing::info!(
63            "handle_alter_table_connector_props: table_name: {}, table id: {}, source_id: {}",
64            real_table_name,
65            table.id,
66            associate_source_id
67        );
68
69        associate_source_id
70    };
71
72    handle_alter_source_props_inner(&session, alter_props, source_id).await?;
73
74    Ok(RwPgResponse::empty_result(StatementType::ALTER_TABLE))
75}
76
77async fn handle_alter_source_props_inner(
78    session: &SessionImpl,
79    alter_props: Vec<SqlOption>,
80    source_id: SourceId,
81) -> Result<()> {
82    let meta_client = session.env().meta_client();
83    let (resolved_with_options, _, connector_conn_ref) = resolve_connection_ref_and_secret_ref(
84        WithOptions::try_from(alter_props.as_ref() as &[SqlOption])?,
85        session,
86        None,
87    )?;
88    let (changed_props, changed_secret_refs) = resolved_with_options.into_parts();
89    if changed_props.contains_key(risingwave_connector::source::iceberg::STREAMING_UPDATES_KEY)
90        || changed_secret_refs
91            .contains_key(risingwave_connector::source::iceberg::STREAMING_UPDATES_KEY)
92    {
93        return Err(ErrorCode::InvalidInputSyntax(
94            "streaming_updates cannot be changed with ALTER; create a new source".to_owned(),
95        )
96        .into());
97    }
98    if connector_conn_ref.is_some() {
99        return Err(ErrorCode::InvalidInputSyntax(
100            "ALTER SOURCE CONNECTOR does not support CONNECTION".to_owned(),
101        )
102        .into());
103    }
104
105    meta_client
106        .alter_source_connector_props(
107            source_id,
108            changed_props,
109            changed_secret_refs,
110            connector_conn_ref, // always None, keep the interface for future extension
111        )
112        .await?;
113    Ok(())
114}
115
116pub async fn handle_alter_source_connector_props(
117    handler_args: HandlerArgs,
118    source_name: ObjectName,
119    alter_props: Vec<SqlOption>,
120) -> Result<RwPgResponse> {
121    let session = handler_args.session;
122    let db_name = &session.database();
123    let (schema_name, real_source_name) =
124        Binder::resolve_schema_qualified_name(db_name, &source_name)?;
125    let search_path = session.config().search_path();
126    let user_name = &session.user_name();
127    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
128
129    let source_id = {
130        let reader = session.env().catalog_reader().read_guard();
131        let (source, schema_name) =
132            reader.get_source_by_name(db_name, schema_path, &real_source_name)?;
133
134        // For `CREATE TABLE WITH (connector = '...')`, users should call `ALTER TABLE` instead.
135        if source.associated_table_id.is_some() {
136            return Err(ErrorCode::InvalidInputSyntax(
137                "Use `ALTER TABLE` to alter a table with connector.".to_owned(),
138            )
139            .into());
140        }
141        if source.is_cdc_table_source() {
142            return Err(ErrorCode::NotSupported(
143                "altering connector properties of a CDC table source is not supported".to_owned(),
144                "Drop and recreate the CDC table source".to_owned(),
145            )
146            .into());
147        }
148
149        session.check_privilege_for_drop_alter(schema_name, &**source)?;
150
151        ensure_alter_props_not_set_by_connection(
152            &reader,
153            db_name,
154            source.connection_id,
155            &alter_props,
156        )?;
157
158        source.id
159    };
160
161    handle_alter_source_props_inner(&session, alter_props, source_id).await?;
162
163    Ok(RwPgResponse::empty_result(StatementType::ALTER_SOURCE))
164}
165
166/// Validates that the properties being altered don't conflict with properties set by a CONNECTION.
167pub(crate) fn ensure_alter_props_not_set_by_connection(
168    reader: &CatalogReadGuard,
169    db_name: &str,
170    connection_id: Option<ConnectionId>,
171    alter_props: &[SqlOption],
172) -> Result<()> {
173    if let Some(conn_id) = connection_id {
174        let conn = reader.get_connection_by_id(db_name, conn_id)?;
175        if let ConnectionParams(params) = &conn.info {
176            for prop in alter_props {
177                let prop_key = prop.name.real_value();
178                if params.properties.contains_key(&prop_key)
179                    || params.secret_refs.contains_key(&prop_key)
180                {
181                    return Err(ErrorCode::InvalidInputSyntax(
182                        "Cannot alter connector properties that are set by CONNECTION..".to_owned(),
183                    )
184                    .into());
185                }
186            }
187        }
188    }
189    Ok(())
190}