Skip to main content

risingwave_frontend/handler/
drop_sink.rs

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