Skip to content

Commit 795f0de

Browse files
committed
Add cascade support for DROP CATALOG
1 parent 5e7f0d3 commit 795f0de

14 files changed

Lines changed: 207 additions & 58 deletions

File tree

‎datafusion-cli/src/catalog.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ impl CatalogProviderList for DynamicObjectStoreCatalog {
5757
self.inner.register_catalog(name, catalog)
5858
}
5959

60-
fn deregister_catalog(&self, name: &str) -> Result<Option<Arc<dyn CatalogProvider>>> {
61-
self.inner.deregister_catalog(name)
60+
fn deregister_catalog(
61+
&self,
62+
name: &str,
63+
cascade: bool,
64+
) -> Result<Option<Arc<dyn CatalogProvider>>> {
65+
self.inner.deregister_catalog(name, cascade)
6266
}
6367

6468
fn catalog_names(&self) -> Vec<String> {

‎datafusion-examples/examples/data_io/catalog.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,18 @@ impl CatalogProviderList for CustomCatalogProviderList {
279279
Some(catalog)
280280
}
281281

282-
fn deregister_catalog(&self, name: &str) -> Result<Option<Arc<dyn CatalogProvider>>> {
282+
fn deregister_catalog(
283+
&self,
284+
name: &str,
285+
cascade: bool,
286+
) -> Result<Option<Arc<dyn CatalogProvider>>> {
283287
let mut cats = self.catalogs.write().unwrap();
288+
let Some(cat) = cats.get_mut(name) else {
289+
return Ok(None);
290+
};
291+
292+
cat.prepare_deregister_catalog(cascade)?;
293+
284294
Ok(cats.remove(name))
285295
}
286296

‎datafusion/catalog/src/dynamic_file/catalog.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ impl CatalogProviderList for DynamicFileCatalog {
5252
fn deregister_catalog(
5353
&self,
5454
name: &str,
55+
cascade: bool,
5556
) -> datafusion_common::Result<Option<Arc<dyn CatalogProvider>>> {
56-
self.inner.deregister_catalog(name)
57+
self.inner.deregister_catalog(name, cascade)
5758
}
5859

5960
fn catalog_names(&self) -> Vec<String> {

‎datafusion/catalog/src/memory/catalog.rs‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,21 @@ impl CatalogProviderList for MemoryCatalogProviderList {
5454
self.catalogs.insert(name, catalog)
5555
}
5656

57-
fn deregister_catalog(&self, name: &str) -> Result<Option<Arc<dyn CatalogProvider>>> {
57+
fn deregister_catalog(
58+
&self,
59+
name: &str,
60+
cascade: bool,
61+
) -> Result<Option<Arc<dyn CatalogProvider>>> {
62+
let Some(mut entry) = self.catalogs.get_mut(name) else {
63+
return Ok(None);
64+
};
65+
66+
let catalog = entry.value_mut();
67+
catalog.prepare_deregister_catalog(cascade)?;
68+
69+
// Drop entry to release its lock
70+
drop(entry);
71+
5872
Ok(self.catalogs.remove(name).map(|(_, catalog)| catalog))
5973
}
6074

@@ -118,7 +132,7 @@ impl CatalogProvider for MemoryCatalogProvider {
118132
Ok(Some(removed))
119133
}
120134
(false, false) => exec_err!(
121-
"Cannot drop schema {} because other tables depend on it: {}",
135+
"Cannot drop schema {} containing tables: {}",
122136
name,
123137
itertools::join(table_names.iter(), ", ")
124138
),
@@ -127,4 +141,16 @@ impl CatalogProvider for MemoryCatalogProvider {
127141
Ok(None)
128142
}
129143
}
144+
145+
fn prepare_deregister_catalog(&self, cascade: bool) -> Result<()> {
146+
if !cascade && !self.schemas.is_empty() {
147+
return exec_err!(
148+
"Cannot drop catalog containing schemas: {}",
149+
itertools::join(self.schema_names().iter(), ", ")
150+
);
151+
}
152+
153+
self.schemas.clear();
154+
Ok(())
155+
}
130156
}

‎datafusion/core/src/execution/context/mod.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,13 +1171,16 @@ impl SessionContext {
11711171

11721172
fn drop_catalog(&self, cmd: DropCatalog) -> Result<DataFrame> {
11731173
let DropCatalog {
1174-
name, if_exists, ..
1174+
name,
1175+
if_exists,
1176+
cascade,
1177+
..
11751178
} = cmd;
11761179
let dereg = self
11771180
.state
11781181
.write()
11791182
.catalog_list()
1180-
.deregister_catalog(&name)?;
1183+
.deregister_catalog(&name, cascade)?;
11811184
match (dereg, if_exists) {
11821185
(Some(_), _) => self.return_empty_dataframe(),
11831186
(None, true) => self.return_empty_dataframe(),
@@ -2847,7 +2850,7 @@ mod tests {
28472850
);
28482851

28492852
// Create catalog
2850-
ctx.sql("CREATE DATABASE test").await?.collect().await?;
2853+
ctx.sql("CREATE CATALOG test").await?.collect().await?;
28512854

28522855
// Create schema
28532856
ctx.sql("CREATE SCHEMA test.abc").await?.collect().await?;

‎datafusion/expr/src/logical_plan/ddl.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub enum DdlStatement {
6060
DropView(DropView),
6161
/// Drops a catalog schema
6262
DropCatalogSchema(DropCatalogSchema),
63-
/// Drops a catalog previously created with `CREATE EXTERNAL CATALOG`.
63+
/// Drops a catalog (aka "Database").
6464
DropCatalog(DropCatalog),
6565
/// Create function statement. Boxed for the same reason as
6666
/// [`Self::CreateExternalTable`] (~288 bytes).
@@ -719,6 +719,8 @@ pub struct DropCatalog {
719719
pub name: String,
720720
/// If the catalog exists
721721
pub if_exists: bool,
722+
/// Whether drop should cascade
723+
pub cascade: bool,
722724
/// Dummy schema
723725
pub schema: DFSchemaRef,
724726
}

‎datafusion/session/src/catalog.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl CatalogProviderList for EmptyCatalogProviderList {
5353
fn deregister_catalog(
5454
&self,
5555
_name: &str,
56+
_cascade: bool,
5657
) -> Result<Option<Arc<dyn CatalogProvider>>> {
5758
Ok(None)
5859
}
@@ -181,6 +182,14 @@ pub trait CatalogProvider: Any + Debug + Sync + Send {
181182
) -> Result<Option<Arc<dyn SchemaProvider>>> {
182183
not_impl_err!("Deregistering new schemas is not supported")
183184
}
185+
186+
/// Called when a catalog is being dropped. Implementations can use this hook
187+
/// to prevent a catalog from being dropped if it is not empty.
188+
///
189+
/// By default returns a "Not Implemented" error
190+
fn prepare_deregister_catalog(&self, _cascade: bool) -> Result<()> {
191+
not_impl_err!("Deregistering all schemas is not supported")
192+
}
184193
}
185194

186195
impl dyn CatalogProvider {
@@ -221,9 +230,16 @@ pub trait CatalogProviderList: Any + Debug + Sync + Send {
221230
/// Implementations of this method should return `Ok(None)` if no catalog
222231
/// with `name` exists.
223232
///
233+
/// If `cascade` is `false`, implementations should return an error if the
234+
/// catalog is not empty.
235+
///
224236
/// By default returns a "Not Implemented" error
225-
fn deregister_catalog(&self, name: &str) -> Result<Option<Arc<dyn CatalogProvider>>> {
226-
let _ = name;
237+
fn deregister_catalog(
238+
&self,
239+
name: &str,
240+
cascade: bool,
241+
) -> Result<Option<Arc<dyn CatalogProvider>>> {
242+
let _ = (name, cascade);
227243
not_impl_err!("Deregistering catalogs is not supported")
228244
}
229245

‎datafusion/sql/src/parser.rs‎

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use datafusion_common::DataFusionError;
2424
use datafusion_common::config::{ConfigNonZeroUsize, SqlParserOptions};
2525
use datafusion_common::format::{ExplainFormat, ExplainStatementOptions};
2626
use datafusion_common::{Diagnostic, Span, sql_err};
27-
use sqlparser::ast::{ExprWithAlias, Ident, OrderByOptions, OrderBySort};
27+
use sqlparser::ast::{ExprWithAlias, Ident, ObjectType, OrderByOptions, OrderBySort};
2828
use sqlparser::tokenizer::TokenWithSpan;
2929
use sqlparser::{
3030
ast::{
@@ -369,6 +369,8 @@ pub struct DropCatalog {
369369
pub name: ObjectName,
370370
/// Option to not error if the catalog does not exist
371371
pub if_exists: bool,
372+
/// Whether drop should cascade
373+
pub cascade: bool,
372374
}
373375

374376
impl fmt::Display for DropCatalog {
@@ -411,8 +413,6 @@ pub enum Statement {
411413
CreateExternalTable(CreateExternalTable),
412414
/// Extension: `CREATE EXTERNAL CATALOG`
413415
CreateExternalCatalog(CreateExternalCatalog),
414-
/// Extension: `DROP CATALOG`
415-
DropCatalog(DropCatalog),
416416
/// Extension: `COPY TO`
417417
CopyTo(CopyToStatement),
418418
/// EXPLAIN for extensions
@@ -427,7 +427,6 @@ impl fmt::Display for Statement {
427427
Statement::Statement(stmt) => write!(f, "{stmt}"),
428428
Statement::CreateExternalTable(stmt) => write!(f, "{stmt}"),
429429
Statement::CreateExternalCatalog(stmt) => write!(f, "{stmt}"),
430-
Statement::DropCatalog(stmt) => write!(f, "{stmt}"),
431430
Statement::CopyTo(stmt) => write!(f, "{stmt}"),
432431
Statement::Explain(stmt) => write!(f, "{stmt}"),
433432
Statement::Reset(stmt) => write!(f, "{stmt}"),
@@ -1036,6 +1035,10 @@ impl<'a> DFParser<'a> {
10361035
.parse_keywords(&[Keyword::UNBOUNDED, Keyword::EXTERNAL])
10371036
{
10381037
self.parse_create_external(true, false)
1038+
} else if self.parser.parse_keywords(&[Keyword::CATALOG]) {
1039+
Ok(Statement::Statement(Box::from(
1040+
self.parser.parse_create_database()?,
1041+
)))
10391042
} else {
10401043
Ok(Statement::Statement(Box::from(self.parser.parse_create()?)))
10411044
}
@@ -1431,7 +1434,28 @@ impl<'a> DFParser<'a> {
14311434
fn parse_drop_catalog(&mut self) -> Result<Statement, DataFusionError> {
14321435
let if_exists = self.parser.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
14331436
let name = self.parser.parse_object_name(true)?;
1434-
Ok(Statement::DropCatalog(DropCatalog { name, if_exists }))
1437+
1438+
let loc = self.parser.peek_token_ref().span.start;
1439+
let cascade = self.parser.parse_keyword(Keyword::CASCADE);
1440+
let restrict = self.parser.parse_keyword(Keyword::RESTRICT);
1441+
if cascade && restrict {
1442+
return parser_err!(format!(
1443+
"Cannot specify both CASCADE and RESTRICT in DROP DATABASE {loc}"
1444+
));
1445+
}
1446+
1447+
Ok(Statement::Statement(Box::new(
1448+
sqlparser::ast::Statement::Drop {
1449+
object_type: ObjectType::Database,
1450+
if_exists,
1451+
names: vec![name],
1452+
cascade,
1453+
restrict,
1454+
purge: false,
1455+
temporary: false,
1456+
table: None,
1457+
},
1458+
)))
14351459
}
14361460

14371461
/// Parses one or more external table locations.
@@ -2108,26 +2132,56 @@ mod tests {
21082132
#[test]
21092133
fn drop_catalog() -> Result<(), DataFusionError> {
21102134
let sql = "DROP CATALOG c";
2111-
let expected = Statement::DropCatalog(DropCatalog {
2112-
name: ObjectName::from(vec![Ident::from("c")]),
2135+
let expected = Statement::Statement(Box::new(sqlparser::ast::Statement::Drop {
2136+
object_type: ObjectType::Database,
21132137
if_exists: false,
2114-
});
2138+
names: vec![ObjectName::from(vec![Ident::from("c")])],
2139+
cascade: false,
2140+
restrict: false,
2141+
purge: false,
2142+
temporary: false,
2143+
table: None,
2144+
}));
21152145
expect_parse_ok(sql, expected)?;
21162146

21172147
let sql = "DROP CATALOG IF EXISTS c";
2118-
let expected = Statement::DropCatalog(DropCatalog {
2119-
name: ObjectName::from(vec![Ident::from("c")]),
2148+
let expected = Statement::Statement(Box::new(sqlparser::ast::Statement::Drop {
2149+
object_type: ObjectType::Database,
21202150
if_exists: true,
2121-
});
2151+
names: vec![ObjectName::from(vec![Ident::from("c")])],
2152+
cascade: false,
2153+
restrict: false,
2154+
purge: false,
2155+
temporary: false,
2156+
table: None,
2157+
}));
2158+
expect_parse_ok(sql, expected)?;
2159+
2160+
let sql = "DROP CATALOG c CASCADE";
2161+
let expected = Statement::Statement(Box::new(sqlparser::ast::Statement::Drop {
2162+
object_type: ObjectType::Database,
2163+
if_exists: false,
2164+
names: vec![ObjectName::from(vec![Ident::from("c")])],
2165+
cascade: true,
2166+
restrict: false,
2167+
purge: false,
2168+
temporary: false,
2169+
table: None,
2170+
}));
21222171
expect_parse_ok(sql, expected)?;
21232172

2124-
// DROP TABLE / VIEW / SCHEMA are unaffected by the DROP EXTERNAL
2125-
// CATALOG dispatch and continue to use the native parser.
2126-
let sql = "DROP TABLE t";
2127-
assert!(matches!(
2128-
DFParser::parse_sql(sql)?.pop_front(),
2129-
Some(Statement::Statement(_))
2130-
));
2173+
let sql = "DROP CATALOG IF EXISTS c CASCADE";
2174+
let expected = Statement::Statement(Box::new(sqlparser::ast::Statement::Drop {
2175+
object_type: ObjectType::Database,
2176+
if_exists: true,
2177+
names: vec![ObjectName::from(vec![Ident::from("c")])],
2178+
cascade: true,
2179+
restrict: false,
2180+
purge: false,
2181+
temporary: false,
2182+
table: None,
2183+
}));
2184+
expect_parse_ok(sql, expected)?;
21312185

21322186
Ok(())
21332187
}

‎datafusion/sql/src/resolve.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn visit_statement(statement: &DFStatement, visitor: &mut RelationVisitor) -> Re
187187
}
188188
DFStatement::Reset(_) => {}
189189
// Catalogs are not tables, so there is nothing to resolve here.
190-
DFStatement::CreateExternalCatalog(_) | DFStatement::DropCatalog(_) => {}
190+
DFStatement::CreateExternalCatalog(_) => {}
191191
}
192192
Ok(())
193193
}

0 commit comments

Comments
 (0)