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