risingwave_frontend/handler/
drop_mv.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 crate::binder::Binder;
20use crate::catalog::CatalogError;
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_mv(
27    handler_args: HandlerArgs,
28    table_name: ObjectName,
29    if_exists: bool,
30    cascade: bool,
31) -> Result<RwPgResponse> {
32    let session = handler_args.session;
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 table_id = {
41        let reader = session.env().catalog_reader().read_guard();
42        let (table, schema_name) =
43            match reader.get_any_table_by_name(&session.database(), schema_path, &table_name) {
44                Ok((t, s)) => (t, s),
45                Err(e) => {
46                    return if if_exists {
47                        Ok(RwPgResponse::builder(StatementType::DROP_MATERIALIZED_VIEW)
48                            .notice(format!(
49                                "materialized view \"{}\" does not exist, skipping",
50                                table_name
51                            ))
52                            .into())
53                    } else {
54                        match e {
55                            CatalogError::NotFound("table", name) => {
56                                Err(CatalogError::NotFound("materialized view", name).into())
57                            }
58                            _ => Err(e.into()),
59                        }
60                    };
61                }
62            };
63
64        session.check_privilege_for_drop_alter(schema_name, &**table)?;
65
66        match table.table_type() {
67            TableType::MaterializedView => {}
68            _ => return Err(table.bad_drop_error()),
69        }
70
71        table.id()
72    };
73
74    let catalog_writer = session.catalog_writer()?;
75    catalog_writer
76        .drop_materialized_view(table_id, cascade)
77        .await?;
78
79    Ok(PgResponse::empty_result(
80        StatementType::DROP_MATERIALIZED_VIEW,
81    ))
82}
83
84#[cfg(test)]
85mod tests {
86    use risingwave_common::catalog::{DEFAULT_DATABASE_NAME, DEFAULT_SCHEMA_NAME};
87
88    use crate::catalog::root_catalog::SchemaPath;
89    use crate::test_utils::LocalFrontend;
90
91    #[tokio::test]
92    async fn test_drop_mv_handler() {
93        let sql_create_table = "create table t (v1 smallint);";
94        let sql_create_mv = "create materialized view mv as select v1 from t;";
95        let sql_drop_mv = "drop materialized view mv;";
96        let frontend = LocalFrontend::new(Default::default()).await;
97        frontend.run_sql(sql_create_table).await.unwrap();
98        frontend.run_sql(sql_create_mv).await.unwrap();
99        frontend.run_sql(sql_drop_mv).await.unwrap();
100
101        let session = frontend.session_ref();
102        let catalog_reader = session.env().catalog_reader().read_guard();
103        let schema_path = SchemaPath::Name(DEFAULT_SCHEMA_NAME);
104
105        let table =
106            catalog_reader.get_created_table_by_name(DEFAULT_DATABASE_NAME, schema_path, "mv");
107        assert!(table.is_err());
108    }
109}