Skip to main content

risingwave_frontend/handler/
drop_table.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_sqlparser::ast::ObjectName;
17
18use super::RwPgResponse;
19use super::util::{LongRunningNotificationAction, execute_with_long_running_notification};
20use crate::binder::Binder;
21use crate::catalog::RelationKind;
22use crate::catalog::root_catalog::SchemaPath;
23use crate::catalog::table_catalog::TableType;
24use crate::error::Result;
25use crate::handler::HandlerArgs;
26
27pub async fn handle_drop_table(
28    handler_args: HandlerArgs,
29    table_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, table_name) = 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
39    let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
40
41    let (source_id, table_id) = {
42        let reader = session.env().catalog_reader().read_guard();
43        let (table, schema_name) =
44            match reader.get_created_table_by_name(db_name, schema_path, &table_name) {
45                Ok((t, s)) => (t, s),
46                Err(e) => {
47                    return if if_exists {
48                        Ok(RwPgResponse::builder(StatementType::DROP_TABLE)
49                            .notice(format!("table \"{}\" does not exist, skipping", table_name))
50                            .into())
51                    } else {
52                        Err(reader
53                            .wrong_kind_for_drop(
54                                db_name,
55                                schema_path,
56                                &table_name,
57                                RelationKind::Table,
58                            )
59                            .unwrap_or(e)
60                            .into())
61                    };
62                }
63            };
64
65        session.check_privilege_for_drop_alter(schema_name, &**table)?;
66
67        if table.table_type() != TableType::Table {
68            return Err(table.bad_drop_error());
69        }
70        (table.associated_source_id(), table.id())
71    };
72
73    let catalog_writer = session.catalog_writer()?;
74    execute_with_long_running_notification(
75        catalog_writer.drop_table(source_id, table_id, cascade),
76        &session,
77        "DROP TABLE",
78        LongRunningNotificationAction::SuggestRecover,
79    )
80    .await?;
81
82    Ok(PgResponse::empty_result(StatementType::DROP_TABLE))
83}
84
85#[cfg(test)]
86mod tests {
87    use risingwave_common::catalog::{DEFAULT_DATABASE_NAME, DEFAULT_SCHEMA_NAME};
88
89    use crate::catalog::root_catalog::SchemaPath;
90    use crate::test_utils::LocalFrontend;
91
92    #[tokio::test]
93    async fn test_drop_table_handler() {
94        let sql_create_table = "create table t (v1 smallint);";
95        let sql_drop_table = "drop table t;";
96        let frontend = LocalFrontend::new(Default::default()).await;
97        frontend.run_sql(sql_create_table).await.unwrap();
98        frontend.run_sql(sql_drop_table).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 source = catalog_reader.get_source_by_name(DEFAULT_DATABASE_NAME, schema_path, "t");
105        assert!(source.is_err());
106
107        let table =
108            catalog_reader.get_created_table_by_name(DEFAULT_DATABASE_NAME, schema_path, "t");
109        assert!(table.is_err());
110    }
111}