From 343acd39ff34ddd62c859f4e8b71d4b6b1213528 Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 11:35:08 +0300 Subject: [PATCH 1/7] Extend references with new SHowSchemas type --- datafusion/core/src/catalog_common/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/datafusion/core/src/catalog_common/mod.rs b/datafusion/core/src/catalog_common/mod.rs index 68c78dda48999..9827f259bd13d 100644 --- a/datafusion/core/src/catalog_common/mod.rs +++ b/datafusion/core/src/catalog_common/mod.rs @@ -156,6 +156,8 @@ pub fn resolve_table_references( | Statement::ShowColumns { .. } | Statement::ShowTables { .. } | Statement::ShowCollation { .. } + | Statement::ShowSchemas { .. } + | Statement::ShowDatabases { .. } ); if requires_information_schema { for s in INFORMATION_SCHEMA_TABLES { From ced699941cd3c3967c2446df2b9c8c50aec33294 Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 11:41:22 +0300 Subject: [PATCH 2/7] Make resolve_table_ref as pub --- datafusion/core/src/execution/session_state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datafusion/core/src/execution/session_state.rs b/datafusion/core/src/execution/session_state.rs index c5874deb6ed50..f7e9642100057 100644 --- a/datafusion/core/src/execution/session_state.rs +++ b/datafusion/core/src/execution/session_state.rs @@ -285,7 +285,7 @@ impl SessionState { .build() } - pub(crate) fn resolve_table_ref( + pub fn resolve_table_ref( &self, table_ref: impl Into, ) -> ResolvedTableReference { From 9ab7d5803f86ac40db95698b671a18e88c6e85f6 Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 11:54:56 +0300 Subject: [PATCH 3/7] Fix docs linter --- datafusion/core/src/execution/session_state.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/datafusion/core/src/execution/session_state.rs b/datafusion/core/src/execution/session_state.rs index f7e9642100057..19d0fae207be7 100644 --- a/datafusion/core/src/execution/session_state.rs +++ b/datafusion/core/src/execution/session_state.rs @@ -285,6 +285,8 @@ impl SessionState { .build() } + /// Resolves a [`TableReference`] to a [`ResolvedTableReference`] + /// using the default catalog and schema. pub fn resolve_table_ref( &self, table_ref: impl Into, @@ -845,9 +847,9 @@ impl SessionState { overwrite: bool, ) -> Result<(), DataFusionError> { let ext = file_format.get_ext().to_lowercase(); - match (self.file_formats.entry(ext.clone()), overwrite){ - (Entry::Vacant(e), _) => {e.insert(file_format);}, - (Entry::Occupied(mut e), true) => {e.insert(file_format);}, + match (self.file_formats.entry(ext.clone()), overwrite) { + (Entry::Vacant(e), _) => { e.insert(file_format); } + (Entry::Occupied(mut e), true) => { e.insert(file_format); } (Entry::Occupied(_), false) => return config_err!("File type already registered for extension {ext}. Set overwrite to true to replace this extension."), }; Ok(()) From 096626f168691e06d3eadf3293e0eded29088eca Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 14:22:50 +0300 Subject: [PATCH 4/7] Add DDL AlterTable --- datafusion/expr/src/logical_plan/ddl.rs | 31 ++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/datafusion/expr/src/logical_plan/ddl.rs b/datafusion/expr/src/logical_plan/ddl.rs index a433871ef20d9..71e4507c111a9 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}; From 2790b230e38a8e5673beafd20640eb5c4c96911a Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 14:24:26 +0300 Subject: [PATCH 5/7] Add DDL AlterTable --- datafusion/expr/src/logical_plan/ddl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/datafusion/expr/src/logical_plan/ddl.rs b/datafusion/expr/src/logical_plan/ddl.rs index 71e4507c111a9..13fc919446e74 100644 --- a/datafusion/expr/src/logical_plan/ddl.rs +++ b/datafusion/expr/src/logical_plan/ddl.rs @@ -189,8 +189,8 @@ impl DdlStatement { write!(f, "CreateFunction: name {name:?}") } DdlStatement::AlterTable(AlterTable { - name, if_exists, .. - }) => { + name, if_exists, .. + }) => { write!(f, "AlterTable: {name:?} if not exist:={if_exists}") } } From 0bfcfecb7c96a0720acdca319ab1c576b79988f7 Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 14:37:59 +0300 Subject: [PATCH 6/7] Fix deps --- datafusion/expr/src/logical_plan/tree_node.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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) } From 51b0d80028e99a3932daa51aa302570b18ba2d4d Mon Sep 17 00:00:00 2001 From: osipovartem Date: Wed, 15 Jan 2025 14:45:07 +0300 Subject: [PATCH 7/7] Import alter table --- datafusion/expr/src/logical_plan/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, };