Skip to content
Closed
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
31 changes: 30 additions & 1 deletion datafusion/expr/src/logical_plan/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -57,6 +57,8 @@ pub enum DdlStatement {
CreateFunction(CreateFunction),
/// Drop function statement
DropFunction(DropFunction),
/// Alter a table.
AlterTable(AlterTable),
}

impl DdlStatement {
Expand All @@ -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,
}
}

Expand All @@ -96,6 +99,7 @@ impl DdlStatement {
DdlStatement::DropCatalogSchema(_) => "DropCatalogSchema",
DdlStatement::CreateFunction(_) => "CreateFunction",
DdlStatement::DropFunction(_) => "DropFunction",
DdlStatement::AlterTable(_) => "AlterTable",
}
}

Expand All @@ -115,6 +119,7 @@ impl DdlStatement {
DdlStatement::DropCatalogSchema(_) => vec![],
DdlStatement::CreateFunction(_) => vec![],
DdlStatement::DropFunction(_) => vec![],
DdlStatement::AlterTable(_) => vec![],
}
}

Expand Down Expand Up @@ -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}")
}
}
}
}
Expand Down Expand Up @@ -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<Expr>,
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<Ordering> {
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};
Expand Down
2 changes: 1 addition & 1 deletion datafusion/expr/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
3 changes: 2 additions & 1 deletion datafusion/expr/src/logical_plan/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down