Skip to main content

risingwave_frontend/handler/
drop_source.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_SOURCE_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_source(
28    handler_args: HandlerArgs,
29    name: ObjectName,
30    if_exists: bool,
31    cascade: bool,
32) -> Result<RwPgResponse> {
33    let session = handler_args.session;
34    let db_name = &session.database();
35    let (schema_name, source_name) = Binder::resolve_schema_qualified_name(db_name, &name)?;
36    let search_path = session.config().search_path();
37    let user_name = &session.user_name();
38
39    // Check if temporary source exists, if yes, drop it.
40    if let Some(_source) = session.get_temporary_source(&source_name) {
41        session.drop_temporary_source(&source_name);
42        return Ok(PgResponse::empty_result(StatementType::DROP_SOURCE));
43    }
44
45    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
46
47    let (source, schema_name) = {
48        let catalog_reader = session.env().catalog_reader().read_guard();
49
50        if let Ok((table, _)) =
51            catalog_reader.get_created_table_by_name(db_name, schema_path, &source_name)
52        {
53            return Err(table.bad_drop_error());
54        }
55
56        match catalog_reader.get_source_by_name(db_name, schema_path, &source_name) {
57            Ok((s, schema)) => (s.clone(), schema),
58            Err(e) => {
59                return if if_exists {
60                    Ok(RwPgResponse::builder(StatementType::DROP_SOURCE)
61                        .notice(format!(
62                            "source \"{}\" does not exist, skipping",
63                            source_name
64                        ))
65                        .into())
66                } else {
67                    Err(catalog_reader
68                        .wrong_kind_for_drop(
69                            db_name,
70                            schema_path,
71                            &source_name,
72                            RelationKind::Source,
73                        )
74                        .unwrap_or(e)
75                        .into())
76                };
77            }
78        }
79    };
80
81    if source_name.starts_with(ICEBERG_SOURCE_PREFIX) {
82        return Err(crate::error::ErrorCode::NotSupported(
83            "Dropping Iceberg sources is not supported".to_owned(),
84            "Please use DROP TABLE command.".to_owned(),
85        )
86        .into());
87    }
88
89    session.check_privilege_for_drop_alter(schema_name, &*source)?;
90
91    let catalog_writer = session.catalog_writer()?;
92    execute_with_long_running_notification(
93        catalog_writer.drop_source(source.id, cascade),
94        &session,
95        "DROP SOURCE",
96        LongRunningNotificationAction::SuggestRecover,
97    )
98    .await?;
99
100    Ok(PgResponse::empty_result(StatementType::DROP_SOURCE))
101}