Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete Extracting LogicalPlan::Drop* DDL related plan structures into LogicalPlan::Ddl #6144

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 45 additions & 38 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,46 +398,13 @@ impl SessionContext {
self.create_catalog_schema(cmd).await
}
DdlStatement::CreateCatalog(cmd) => self.create_catalog(cmd).await,
DdlStatement::DropTable(cmd) => self.drop_table(cmd).await,
DdlStatement::DropView(cmd) => self.drop_view(cmd).await,
},

LogicalPlan::DropTable(DropTable {
name, if_exists, ..
}) => {
let result = self.find_and_deregister(&name, TableType::Base).await;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these into their own functions for consistency

match (result, if_exists) {
(Ok(true), _) => self.return_empty_dataframe(),
(_, true) => self.return_empty_dataframe(),
(_, _) => Err(DataFusionError::Execution(format!(
"Table '{name}' doesn't exist."
))),
}
}

LogicalPlan::DropView(DropView {
name, if_exists, ..
}) => {
let result = self.find_and_deregister(&name, TableType::View).await;
match (result, if_exists) {
(Ok(true), _) => self.return_empty_dataframe(),
(_, true) => self.return_empty_dataframe(),
(_, _) => Err(DataFusionError::Execution(format!(
"View '{name}' doesn't exist."
))),
}
// TODO what about the other statements (like TransactionStart and TransactionEnd)
LogicalPlan::Statement(Statement::SetVariable(stmt)) => {
self.set_variable(stmt).await
}

LogicalPlan::Statement(Statement::SetVariable(SetVariable {
variable,
value,
..
})) => {
let mut state = self.state.write();
state.config.options_mut().set(&variable, &value)?;
drop(state);

self.return_empty_dataframe()
}

LogicalPlan::DescribeTable(DescribeTable { schema, .. }) => {
self.return_describe_table_dataframe(schema).await
}
Expand Down Expand Up @@ -675,6 +642,46 @@ impl SessionContext {
}
}

async fn drop_table(&self, cmd: DropTable) -> Result<DataFrame> {
let DropTable {
name, if_exists, ..
} = cmd;
let result = self.find_and_deregister(&name, TableType::Base).await;
match (result, if_exists) {
(Ok(true), _) => self.return_empty_dataframe(),
(_, true) => self.return_empty_dataframe(),
(_, _) => Err(DataFusionError::Execution(format!(
"Table '{name}' doesn't exist."
))),
}
}

async fn drop_view(&self, cmd: DropView) -> Result<DataFrame> {
let DropView {
name, if_exists, ..
} = cmd;
let result = self.find_and_deregister(&name, TableType::View).await;
match (result, if_exists) {
(Ok(true), _) => self.return_empty_dataframe(),
(_, true) => self.return_empty_dataframe(),
(_, _) => Err(DataFusionError::Execution(format!(
"View '{name}' doesn't exist."
))),
}
}

async fn set_variable(&self, stmt: SetVariable) -> Result<DataFrame> {
let SetVariable {
variable, value, ..
} = stmt;

let mut state = self.state.write();
state.config.options_mut().set(&variable, &value)?;
drop(state);

self.return_empty_dataframe()
}

async fn create_custom_table(
&self,
cmd: &CreateExternalTable,
Expand Down
18 changes: 0 additions & 18 deletions datafusion/core/src/physical_plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,24 +1105,6 @@ impl DefaultPhysicalPlanner {
"Unsupported logical plan: Prepare".to_string(),
))
}
LogicalPlan::DropTable(_) => {
// There is no default plan for "DROP TABLE".
// It must be handled at a higher level (so
// that the schema can be registered with
// the context)
Err(DataFusionError::NotImplemented(
"Unsupported logical plan: DropTable".to_string(),
))
}
LogicalPlan::DropView(_) => {
// There is no default plan for "DROP VIEW".
// It must be handled at a higher level (so
// that the schema can be registered with
// the context)
Err(DataFusionError::NotImplemented(
"Unsupported logical plan: DropView".to_string(),
))
}
LogicalPlan::Dml(_) => {
// DataFusion is a read-only query engine, but also a library, so consumers may implement this
Err(DataFusionError::NotImplemented(
Expand Down
42 changes: 42 additions & 0 deletions datafusion/expr/src/logical_plan/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub enum DdlStatement {
CreateCatalogSchema(CreateCatalogSchema),
/// Creates a new catalog (aka "Database").
CreateCatalog(CreateCatalog),
/// Drops a table.
DropTable(DropTable),
/// Drops a view.
DropView(DropView),
}

impl DdlStatement {
Expand All @@ -56,6 +60,8 @@ impl DdlStatement {
schema
}
DdlStatement::CreateCatalog(CreateCatalog { schema, .. }) => schema,
DdlStatement::DropTable(DropTable { schema, .. }) => schema,
DdlStatement::DropView(DropView { schema, .. }) => schema,
}
}

Expand All @@ -68,6 +74,8 @@ impl DdlStatement {
DdlStatement::CreateView(_) => "CreateView",
DdlStatement::CreateCatalogSchema(_) => "CreateCatalogSchema",
DdlStatement::CreateCatalog(_) => "CreateCatalog",
DdlStatement::DropTable(_) => "DropTable",
DdlStatement::DropView(_) => "DropView",
}
}

Expand All @@ -81,6 +89,8 @@ impl DdlStatement {
vec![input]
}
DdlStatement::CreateView(CreateView { input, .. }) => vec![input],
DdlStatement::DropTable(_) => vec![],
DdlStatement::DropView(_) => vec![],
}
}

Expand Down Expand Up @@ -127,6 +137,16 @@ impl DdlStatement {
}) => {
write!(f, "CreateCatalog: {catalog_name:?}")
}
DdlStatement::DropTable(DropTable {
name, if_exists, ..
}) => {
write!(f, "DropTable: {name:?} if not exist:={if_exists}")
}
DdlStatement::DropView(DropView {
name, if_exists, ..
}) => {
write!(f, "DropView: {name:?} if not exist:={if_exists}")
}
}
}
}
Expand Down Expand Up @@ -231,3 +251,25 @@ pub struct CreateCatalogSchema {
/// Empty schema
pub schema: DFSchemaRef,
}

/// Drops a table.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct DropTable {
/// The table name
pub name: OwnedTableReference,
/// If the table exists
pub if_exists: bool,
/// Dummy schema
pub schema: DFSchemaRef,
}

/// Drops a view.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct DropView {
/// The view name
pub name: OwnedTableReference,
/// If the view exists
pub if_exists: bool,
/// Dummy schema
pub schema: DFSchemaRef,
}
11 changes: 5 additions & 6 deletions datafusion/expr/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ pub use builder::{
};
pub use ddl::{
CreateCatalog, CreateCatalogSchema, CreateExternalTable, CreateMemoryTable,
CreateView, DdlStatement,
CreateView, DdlStatement, DropTable, DropView,
};
pub use dml::{DmlStatement, WriteOp};
pub use plan::{
Aggregate, Analyze, CrossJoin, DescribeTable, Distinct, DropTable, DropView,
EmptyRelation, Explain, Extension, Filter, Join, JoinConstraint, JoinType, Limit,
LogicalPlan, Partitioning, PlanType, Prepare, Projection, Repartition, Sort,
StringifiedPlan, Subquery, SubqueryAlias, TableScan, ToStringifiedPlan, Union,
Unnest, Values, Window,
Aggregate, Analyze, CrossJoin, DescribeTable, Distinct, EmptyRelation, Explain,
Extension, Filter, Join, JoinConstraint, JoinType, Limit, LogicalPlan, Partitioning,
PlanType, Prepare, Projection, Repartition, Sort, StringifiedPlan, Subquery,
SubqueryAlias, TableScan, ToStringifiedPlan, Union, Unnest, Values, Window,
};
pub use statement::{
SetVariable, Statement, TransactionAccessMode, TransactionConclusion, TransactionEnd,
Expand Down
49 changes: 1 addition & 48 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ pub enum LogicalPlan {
Limit(Limit),
/// [`Statement`]
Statement(Statement),
/// Drops a table.
DropTable(DropTable),
/// Drops a view.
DropView(DropView),
/// Values expression. See
/// [Postgres VALUES](https://www.postgresql.org/docs/current/queries-values.html)
/// documentation for more details.
Expand Down Expand Up @@ -148,8 +144,6 @@ impl LogicalPlan {
LogicalPlan::Analyze(analyze) => &analyze.schema,
LogicalPlan::Extension(extension) => extension.node.schema(),
LogicalPlan::Union(Union { schema, .. }) => schema,
LogicalPlan::DropTable(DropTable { schema, .. }) => schema,
LogicalPlan::DropView(DropView { schema, .. }) => schema,
LogicalPlan::DescribeTable(DescribeTable { dummy_schema, .. }) => {
dummy_schema
}
Expand Down Expand Up @@ -218,10 +212,7 @@ impl LogicalPlan {
self.inputs().iter().map(|p| p.schema()).collect()
}
// return empty
LogicalPlan::Statement(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::DescribeTable(_) => vec![],
LogicalPlan::Statement(_) | LogicalPlan::DescribeTable(_) => vec![],
}
}

Expand Down Expand Up @@ -336,8 +327,6 @@ impl LogicalPlan {
| LogicalPlan::SubqueryAlias(_)
| LogicalPlan::Limit(_)
| LogicalPlan::Statement(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::CrossJoin(_)
| LogicalPlan::Analyze(_)
| LogicalPlan::Explain(_)
Expand Down Expand Up @@ -381,8 +370,6 @@ impl LogicalPlan {
| LogicalPlan::Statement { .. }
| LogicalPlan::EmptyRelation { .. }
| LogicalPlan::Values { .. }
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::DescribeTable(_) => vec![],
}
}
Expand Down Expand Up @@ -536,8 +523,6 @@ impl LogicalPlan {
LogicalPlan::Values(v) => Some(v.values.len()),
LogicalPlan::Unnest(_) => None,
LogicalPlan::Ddl(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::Explain(_)
| LogicalPlan::Analyze(_)
| LogicalPlan::Dml(_)
Expand Down Expand Up @@ -1103,16 +1088,6 @@ impl LogicalPlan {
LogicalPlan::Statement(statement) => {
write!(f, "{}", statement.display())
}
LogicalPlan::DropTable(DropTable {
name, if_exists, ..
}) => {
write!(f, "DropTable: {name:?} if not exist:={if_exists}")
}
LogicalPlan::DropView(DropView {
name, if_exists, ..
}) => {
write!(f, "DropView: {name:?} if not exist:={if_exists}")
}
LogicalPlan::Distinct(Distinct { .. }) => {
write!(f, "Distinct:")
}
Expand Down Expand Up @@ -1223,28 +1198,6 @@ pub enum JoinConstraint {
Using,
}

/// Drops a table.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct DropTable {
/// The table name
pub name: OwnedTableReference,
/// If the table exists
pub if_exists: bool,
/// Dummy schema
pub schema: DFSchemaRef,
}

/// Drops a view.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct DropView {
/// The view name
pub name: OwnedTableReference,
/// If the view exists
pub if_exists: bool,
/// Dummy schema
pub schema: DFSchemaRef,
}

/// Produces no rows: An empty relation with an empty schema
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct EmptyRelation {
Expand Down
2 changes: 0 additions & 2 deletions datafusion/expr/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,6 @@ pub fn from_plan(
}
LogicalPlan::EmptyRelation(_)
| LogicalPlan::Ddl(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::Statement(_) => {
// All of these plan types have no inputs / exprs so should not be called
assert!(expr.is_empty(), "{plan:?} should have no exprs");
Expand Down
2 changes: 0 additions & 2 deletions datafusion/optimizer/src/common_subexpr_eliminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ impl OptimizerRule for CommonSubexprEliminate {
| LogicalPlan::Ddl(_)
| LogicalPlan::Explain(_)
| LogicalPlan::Analyze(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::Statement(_)
| LogicalPlan::DescribeTable(_)
| LogicalPlan::Distinct(_)
Expand Down
4 changes: 2 additions & 2 deletions datafusion/proto/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,10 +1366,10 @@ impl AsLogicalPlan for LogicalPlanNode {
LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(_)) => Err(proto_error(
"LogicalPlan serde is not yet implemented for CreateMemoryTable",
)),
LogicalPlan::DropTable(_) => Err(proto_error(
LogicalPlan::Ddl(DdlStatement::DropTable(_)) => Err(proto_error(
"LogicalPlan serde is not yet implemented for DropTable",
)),
LogicalPlan::DropView(_) => Err(proto_error(
LogicalPlan::Ddl(DdlStatement::DropView(_)) => Err(proto_error(
"LogicalPlan serde is not yet implemented for DropView",
)),
LogicalPlan::Statement(_) => Err(proto_error(
Expand Down
24 changes: 14 additions & 10 deletions datafusion/sql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}?;

match object_type {
ObjectType::Table => Ok(LogicalPlan::DropTable(DropTable {
name,
if_exists,
schema: DFSchemaRef::new(DFSchema::empty()),
})),
ObjectType::View => Ok(LogicalPlan::DropView(DropView {
name,
if_exists,
schema: DFSchemaRef::new(DFSchema::empty()),
})),
ObjectType::Table => {
Ok(LogicalPlan::Ddl(DdlStatement::DropTable(DropTable {
name,
if_exists,
schema: DFSchemaRef::new(DFSchema::empty()),
})))
}
ObjectType::View => {
Ok(LogicalPlan::Ddl(DdlStatement::DropView(DropView {
name,
if_exists,
schema: DFSchemaRef::new(DFSchema::empty()),
})))
}
_ => Err(DataFusionError::NotImplemented(
"Only `DROP TABLE/VIEW ...` statement is supported currently"
.to_string(),
Expand Down