risingwave_frontend/handler/
drop_index.rs1use 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::ErrorCode::PermissionDenied;
24use crate::error::Result;
25use crate::handler::HandlerArgs;
26
27pub async fn handle_drop_index(
28 handler_args: HandlerArgs,
29 index_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, index_name) = Binder::resolve_schema_qualified_name(db_name, index_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 index_id = {
41 let reader = session.env().catalog_reader().read_guard();
42 match reader.get_index_by_name(db_name, schema_path, &index_name) {
43 Ok((index, _)) => {
44 if session.user_id() != index.index_table.owner {
45 return Err(PermissionDenied("Do not have the privilege".to_owned()).into());
46 }
47
48 index.id
49 }
50 Err(err) => {
51 match err {
52 CatalogError::NotFound("index", _) => {
53 }
55 _ => return Err(err.into()),
56 };
57 return match reader.get_created_table_by_name(db_name, schema_path, &index_name) {
58 Ok((table, _)) => match table.table_type() {
59 TableType::Index => unreachable!(),
60 _ => Err(table.bad_drop_error()),
61 },
62 Err(e) => {
63 if if_exists {
64 Ok(RwPgResponse::builder(StatementType::DROP_INDEX)
65 .notice(format!(
66 "index \"{}\" does not exist, skipping",
67 index_name
68 ))
69 .into())
70 } else {
71 match e {
72 CatalogError::NotFound("table", name) => {
73 Err(CatalogError::NotFound("index", name).into())
74 }
75 _ => Err(e.into()),
76 }
77 }
78 }
79 };
80 }
81 }
82 };
83
84 let catalog_writer = session.catalog_writer()?;
85 catalog_writer.drop_index(index_id, cascade).await?;
86
87 Ok(PgResponse::empty_result(StatementType::DROP_INDEX))
88}
89
90#[cfg(test)]
91mod tests {
92 use risingwave_common::catalog::{DEFAULT_DATABASE_NAME, DEFAULT_SCHEMA_NAME};
93
94 use crate::catalog::root_catalog::SchemaPath;
95 use crate::test_utils::LocalFrontend;
96
97 #[tokio::test]
98 async fn test_drop_index_handler() {
99 let sql_create_table = "create table t (v1 smallint);";
100 let sql_create_index = "create index idx on t(v1);";
101 let sql_drop_index = "drop index idx;";
102 let frontend = LocalFrontend::new(Default::default()).await;
103 frontend.run_sql(sql_create_table).await.unwrap();
104 frontend.run_sql(sql_create_index).await.unwrap();
105 frontend.run_sql(sql_drop_index).await.unwrap();
106
107 let session = frontend.session_ref();
108 let catalog_reader = session.env().catalog_reader().read_guard();
109 let schema_path = SchemaPath::Name(DEFAULT_SCHEMA_NAME);
110
111 let table =
112 catalog_reader.get_created_table_by_name(DEFAULT_DATABASE_NAME, schema_path, "idx");
113 assert!(table.is_err());
114 }
115}