risingwave_frontend/handler/
drop_sink.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::{PgResponse, StatementType};
16use risingwave_common::catalog::ICEBERG_SINK_PREFIX;
17use risingwave_sqlparser::ast::ObjectName;
18
19use super::RwPgResponse;
20use super::util::execute_with_long_running_notification;
21use crate::binder::Binder;
22use crate::catalog::root_catalog::SchemaPath;
23use crate::error::Result;
24use crate::handler::HandlerArgs;
25
26pub async fn handle_drop_sink(
27    handler_args: HandlerArgs,
28    sink_name: ObjectName,
29    if_exists: bool,
30    cascade: bool,
31) -> Result<RwPgResponse> {
32    let session = handler_args.session.clone();
33    let db_name = &session.database();
34    let (schema_name, sink_name) = Binder::resolve_schema_qualified_name(db_name, &sink_name)?;
35    let search_path = session.config().search_path();
36    let user_name = &session.user_name();
37    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
38
39    let sink = {
40        let catalog_reader = session.env().catalog_reader().read_guard();
41        let (sink, schema_name) =
42            match catalog_reader.get_any_sink_by_name(db_name, schema_path, &sink_name) {
43                Ok((sink, schema)) => (sink.clone(), schema),
44                Err(e) => {
45                    return if if_exists {
46                        Ok(RwPgResponse::builder(StatementType::DROP_SINK)
47                            .notice(format!("sink \"{}\" does not exist, skipping", sink_name))
48                            .into())
49                    } else {
50                        Err(e.into())
51                    };
52                }
53            };
54
55        session.check_privilege_for_drop_alter(schema_name, &*sink)?;
56
57        sink
58    };
59
60    if sink_name.starts_with(ICEBERG_SINK_PREFIX) {
61        return Err(crate::error::ErrorCode::NotSupported(
62            "Dropping Iceberg sinks is not supported".to_owned(),
63            "Please use DROP TABLE command.".to_owned(),
64        )
65        .into());
66    }
67
68    let sink_id = sink.id;
69
70    let catalog_writer = session.catalog_writer()?;
71    execute_with_long_running_notification(
72        catalog_writer.drop_sink(sink_id, cascade),
73        &session,
74        "DROP SINK",
75    )
76    .await?;
77
78    Ok(PgResponse::empty_result(StatementType::DROP_SINK))
79}
80
81#[cfg(test)]
82mod tests {
83    use risingwave_common::catalog::{DEFAULT_DATABASE_NAME, DEFAULT_SCHEMA_NAME};
84
85    use crate::catalog::root_catalog::SchemaPath;
86    use crate::test_utils::LocalFrontend;
87
88    #[tokio::test]
89    async fn test_drop_sink_handler() {
90        let sql_create_table = "create table t (v1 smallint primary key);";
91        let sql_create_mv = "create materialized view mv as select v1 from t;";
92        let sql_create_sink = "create sink snk from mv with( connector = 'kafka')";
93        let sql_drop_sink = "drop sink snk;";
94        let frontend = LocalFrontend::new(Default::default()).await;
95        frontend.run_sql(sql_create_table).await.unwrap();
96        frontend.run_sql(sql_create_mv).await.unwrap();
97        frontend.run_sql(sql_create_sink).await.unwrap();
98        frontend.run_sql(sql_drop_sink).await.unwrap();
99
100        let session = frontend.session_ref();
101        let catalog_reader = session.env().catalog_reader().read_guard();
102        let schema_path = SchemaPath::Name(DEFAULT_SCHEMA_NAME);
103
104        let sink =
105            catalog_reader.get_created_table_by_name(DEFAULT_DATABASE_NAME, schema_path, "snk");
106        assert!(sink.is_err());
107    }
108}