diff --git a/datafusion/expr/src/logical_plan/ddl.rs b/datafusion/expr/src/logical_plan/ddl.rs index a433871ef20d9..13fc919446e74 100644 --- a/datafusion/expr/src/logical_plan/ddl.rs +++ b/datafusion/expr/src/logical_plan/ddl.rs @@ -32,7 +32,7 @@ use datafusion_common::{ }; use sqlparser::ast::Ident; -/// Various types of DDL (CREATE / DROP) catalog manipulation +/// Various types of DDL (CREATE / DROP / ALTER) catalog manipulation #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)] pub enum DdlStatement { /// Creates an external table. @@ -57,6 +57,8 @@ pub enum DdlStatement { CreateFunction(CreateFunction), /// Drop function statement DropFunction(DropFunction), + /// Alter a table. + AlterTable(AlterTable), } impl DdlStatement { @@ -78,6 +80,7 @@ impl DdlStatement { DdlStatement::DropCatalogSchema(DropCatalogSchema { schema, .. }) => schema, DdlStatement::CreateFunction(CreateFunction { schema, .. }) => schema, DdlStatement::DropFunction(DropFunction { schema, .. }) => schema, + DdlStatement::AlterTable(AlterTable { schema, .. }) => schema, } } @@ -96,6 +99,7 @@ impl DdlStatement { DdlStatement::DropCatalogSchema(_) => "DropCatalogSchema", DdlStatement::CreateFunction(_) => "CreateFunction", DdlStatement::DropFunction(_) => "DropFunction", + DdlStatement::AlterTable(_) => "AlterTable", } } @@ -115,6 +119,7 @@ impl DdlStatement { DdlStatement::DropCatalogSchema(_) => vec![], DdlStatement::CreateFunction(_) => vec![], DdlStatement::DropFunction(_) => vec![], + DdlStatement::AlterTable(_) => vec![], } } @@ -183,6 +188,11 @@ impl DdlStatement { DdlStatement::DropFunction(DropFunction { name, .. }) => { write!(f, "CreateFunction: name {name:?}") } + DdlStatement::AlterTable(AlterTable { + name, if_exists, .. + }) => { + write!(f, "AlterTable: {name:?} if not exist:={if_exists}") + } } } } @@ -604,6 +614,25 @@ impl PartialOrd for CreateIndex { } } +/// Alter a table. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct AlterTable { + pub name: TableReference, + pub if_exists: bool, + pub columns: Vec, + pub schema: DFSchemaRef, +} + +// Manual implementation needed because of `schema` field. Comparison excludes this field. +impl PartialOrd for AlterTable { + fn partial_cmp(&self, other: &Self) -> Option { + match self.name.partial_cmp(&other.name) { + Some(Ordering::Equal) => self.if_exists.partial_cmp(&other.if_exists), + cmp => cmp, + } + } +} + #[cfg(test)] mod test { use crate::{CreateCatalog, DdlStatement, DropView}; diff --git a/datafusion/expr/src/logical_plan/mod.rs b/datafusion/expr/src/logical_plan/mod.rs index 4049413786636..8a34e7a7de4d2 100644 --- a/datafusion/expr/src/logical_plan/mod.rs +++ b/datafusion/expr/src/logical_plan/mod.rs @@ -31,7 +31,7 @@ pub use builder::{ LogicalPlanBuilder, LogicalTableSource, UNNAMED_TABLE, }; pub use ddl::{ - CreateCatalog, CreateCatalogSchema, CreateExternalTable, CreateFunction, + AlterTable, CreateCatalog, CreateCatalogSchema, CreateExternalTable, CreateFunction, CreateFunctionBody, CreateIndex, CreateMemoryTable, CreateView, DdlStatement, DropCatalogSchema, DropFunction, DropTable, DropView, OperateFunctionArg, }; diff --git a/datafusion/expr/src/logical_plan/tree_node.rs b/datafusion/expr/src/logical_plan/tree_node.rs index 9a6103afd4b41..6e2f15ef017aa 100644 --- a/datafusion/expr/src/logical_plan/tree_node.rs +++ b/datafusion/expr/src/logical_plan/tree_node.rs @@ -301,7 +301,8 @@ impl TreeNode for LogicalPlan { | DdlStatement::DropView(_) | DdlStatement::DropCatalogSchema(_) | DdlStatement::CreateFunction(_) - | DdlStatement::DropFunction(_) => Transformed::no(ddl), + | DdlStatement::DropFunction(_) + | DdlStatement::AlterTable(_) => Transformed::no(ddl), } .update_data(LogicalPlan::Ddl) }