Skip to content

Apache DataFusion SQL Query Engine

Extract `LogicalPlan::Create*` DDL related plan structures into `LogicalPlan::Ddl` (#6121)

Andrew Lamb13c52ce61d6c
diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs
index 3234ea000f..1497154b28 100644
--- a/datafusion-cli/src/exec.rs
+++ b/datafusion-cli/src/exec.rs
@@ -29,7 +29,7 @@ use crate::{
use datafusion::{
datasource::listing::ListingTableUrl,
error::{DataFusionError, Result},
- logical_expr::CreateExternalTable,
+ logical_expr::{CreateExternalTable, DdlStatement},
};
use datafusion::{logical_expr::LogicalPlan, prelude::SessionContext};
use object_store::ObjectStore;
@@ -178,7 +178,7 @@ async fn exec_and_print(
let plan = ctx.state().create_logical_plan(&sql).await?;
let df = match &plan {
- LogicalPlan::CreateExternalTable(cmd) => {
+ LogicalPlan::Ddl(DdlStatement::CreateExternalTable(cmd)) => {
create_external_table(&ctx, cmd)?;
ctx.execute_logical_plan(plan).await?
}
@@ -238,7 +238,7 @@ mod tests {
let plan = ctx.state().create_logical_plan(&sql).await?;
match &plan {
- LogicalPlan::CreateExternalTable(cmd) => {
+ LogicalPlan::Ddl(DdlStatement::CreateExternalTable(cmd)) => {
create_external_table(&ctx, cmd)?;
}
_ => assert!(false),
diff --git a/datafusion-cli/src/object_storage.rs b/datafusion-cli/src/object_storage.rs
index 87018bbe15..1fd509775d 100644
--- a/datafusion-cli/src/object_storage.rs
+++ b/datafusion-cli/src/object_storage.rs
@@ -112,7 +112,7 @@ fn get_bucket_name(url: &Url) -> Result<&str> {
#[cfg(test)]
mod tests {
use datafusion::{
- datasource::listing::ListingTableUrl, logical_expr::LogicalPlan,
+ datasource::listing::ListingTableUrl, logical_expr::{LogicalPlan, DdlStatement},
prelude::SessionContext,
};
@@ -132,7 +132,7 @@ mod tests {
let plan = ctx.state().create_logical_plan(&sql).await?;
match &plan {
- LogicalPlan::CreateExternalTable(cmd) => {
+ LogicalPlan::Ddl(DdlStatement::CreateExternalTable(cmd)) => {
let _builder = get_oss_object_store_builder(table_url.as_ref(), cmd)?;
// get the actual configuration information, then assert_eq!
}
diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rs
index d1cbb740fa..bb6d58fb90 100644
--- a/datafusion/core/src/execution/context.rs
+++ b/datafusion/core/src/execution/context.rs
@@ -32,7 +32,8 @@ use crate::{
},
};
use datafusion_expr::{
- logical_plan::Statement, DescribeTable, DmlStatement, StringifiedPlan, WriteOp,
+ logical_plan::{DdlStatement, Statement},
+ DescribeTable, DmlStatement, StringifiedPlan, WriteOp,
};
pub use datafusion_physical_expr::execution_props::ExecutionProps;
use datafusion_physical_expr::var_provider::is_system_variables;
@@ -385,88 +386,19 @@ impl SessionContext {
}
self.return_empty_dataframe()
}
- LogicalPlan::CreateExternalTable(cmd) => {
- self.create_external_table(&cmd).await
- }
-
- LogicalPlan::CreateMemoryTable(CreateMemoryTable {
- name,
- input,
- if_not_exists,
- or_replace,
- primary_key,
- }) => {
- if !primary_key.is_empty() {
- Err(DataFusionError::Execution(
- "Primary keys on MemoryTables are not currently supported!"
- .to_string(),
- ))?;
+ LogicalPlan::Ddl(ddl) => match ddl {
+ DdlStatement::CreateExternalTable(cmd) => {
+ self.create_external_table(&cmd).await
}
-
- let input = Arc::try_unwrap(input).unwrap_or_else(|e| e.as_ref().clone());
- let table = self.table(&name).await;
-
- match (if_not_exists, or_replace, table) {
- (true, false, Ok(_)) => self.return_empty_dataframe(),
- (false, true, Ok(_)) => {
- self.deregister_table(&name)?;
- let schema = Arc::new(input.schema().as_ref().into());
- let physical = DataFrame::new(self.state(), input);
-
- let batches: Vec<_> = physical.collect_partitioned().await?;
- let table = Arc::new(MemTable::try_new(schema, batches)?);
-
- self.register_table(&name, table)?;
- self.return_empty_dataframe()
- }
- (true, true, Ok(_)) => Err(DataFusionError::Execution(
- "'IF NOT EXISTS' cannot coexist with 'REPLACE'".to_string(),
- )),
- (_, _, Err(_)) => {
- let schema = Arc::new(input.schema().as_ref().into());
- let physical = DataFrame::new(self.state(), input);
-
- let batches: Vec<_> = physical.collect_partitioned().await?;
- let table = Arc::new(MemTable::try_new(schema, batches)?);
-
- self.register_table(&name, table)?;
- self.return_empty_dataframe()
- }
- (false, false, Ok(_)) => Err(DataFusionError::Execution(format!(
- "Table '{name}' already exists"
- ))),
+ DdlStatement::CreateMemoryTable(cmd) => {
+ self.create_memory_table(cmd).await
}
- }
-
- LogicalPlan::CreateView(CreateView {
- name,
- input,
- or_replace,
- definition,
- }) => {
- let view = self.table(&name).await;
-
- match (or_replace, view) {
- (true, Ok(_)) => {
- self.deregister_table(&name)?;
- let table =
- Arc::new(ViewTable::try_new((*input).clone(), definition)?);
-
- self.register_table(&name, table)?;
- self.return_empty_dataframe()
- }
- (_, Err(_)) => {
- let table =
- Arc::new(ViewTable::try_new((*input).clone(), definition)?);
-
- self.register_table(&name, table)?;
- self.return_empty_dataframe()
- }
- (false, Ok(_)) => Err(DataFusionError::Execution(format!(
- "Table '{name}' already exists"
- ))),
+ DdlStatement::CreateView(cmd) => self.create_view(cmd).await,
+ DdlStatement::CreateCatalogSchema(cmd) => {
+ self.create_catalog_schema(cmd).await
}
- }
+ DdlStatement::CreateCatalog(cmd) => self.create_catalog(cmd).await,
+ },
LogicalPlan::DropTable(DropTable {
name, if_exists, ..
@@ -510,78 +442,6 @@ impl SessionContext {
self.return_describe_table_dataframe(schema).await
}
- LogicalPlan::CreateCatalogSchema(CreateCatalogSchema {
- schema_name,
- if_not_exists,
- ..
- }) => {
- // sqlparser doesnt accept database / catalog as parameter to CREATE SCHEMA
- // so for now, we default to default catalog
- let tokens: Vec<&str> = schema_name.split('.').collect();
- let (catalog, schema_name) = match tokens.len() {
- 1 => {
- let state = self.state.read();
- let name = &state.config.options().catalog.default_catalog;
- let catalog =
- state.catalog_list.catalog(name).ok_or_else(|| {
- DataFusionError::Execution(format!(
- "Missing default catalog '{name}'"
- ))
- })?;
- (catalog, tokens[0])
- }
- 2 => {
- let name = &tokens[0];
- let catalog = self.catalog(name).ok_or_else(|| {
- DataFusionError::Execution(format!(
- "Missing catalog '{name}'"
- ))
- })?;
- (catalog, tokens[1])
- }
- _ => {
- return Err(DataFusionError::Execution(format!(
- "Unable to parse catalog from {schema_name}"
- )))
- }
- };
- let schema = catalog.schema(schema_name);
-
- match (if_not_exists, schema) {
- (true, Some(_)) => self.return_empty_dataframe(),
- (true, None) | (false, None) => {
- let schema = Arc::new(MemorySchemaProvider::new());
- catalog.register_schema(schema_name, schema)?;
- self.return_empty_dataframe()
- }
- (false, Some(_)) => Err(DataFusionError::Execution(format!(
- "Schema '{schema_name}' already exists"
- ))),
- }
- }
- LogicalPlan::CreateCatalog(CreateCatalog {
- catalog_name,
- if_not_exists,
- ..
- }) => {
- let catalog = self.catalog(catalog_name.as_str());
-
- match (if_not_exists, catalog) {
- (true, Some(_)) => self.return_empty_dataframe(),
- (true, None) | (false, None) => {
- let new_catalog = Arc::new(MemoryCatalogProvider::new());
- self.state
- .write()
- .catalog_list
- .register_catalog(catalog_name, new_catalog);
- self.return_empty_dataframe()
- }
- (false, Some(_)) => Err(DataFusionError::Execution(format!(
- "Catalog '{catalog_name}' already exists"
- ))),
- }
- }
-
plan => Ok(DataFrame::new(self.state(), plan)),
}
}
@@ -662,6 +522,159 @@ impl SessionContext {
self.return_empty_dataframe()
}
+ async fn create_memory_table(&self, cmd: CreateMemoryTable) -> Result<DataFrame> {
+ let CreateMemoryTable {
+ name,
+ input,
+ if_not_exists,
+ or_replace,
+ primary_key,
+ } = cmd;
+
+ if !primary_key.is_empty() {
+ Err(DataFusionError::Execution(
+ "Primary keys on MemoryTables are not currently supported!".to_string(),
+ ))?;
+ }
+
+ let input = Arc::try_unwrap(input).unwrap_or_else(|e| e.as_ref().clone());
+ let table = self.table(&name).await;
+
+ match (if_not_exists, or_replace, table) {
+ (true, false, Ok(_)) => self.return_empty_dataframe(),
+ (false, true, Ok(_)) => {
+ self.deregister_table(&name)?;
+ let schema = Arc::new(input.schema().as_ref().into());
+ let physical = DataFrame::new(self.state(), input);
+
+ let batches: Vec<_> = physical.collect_partitioned().await?;
+ let table = Arc::new(MemTable::try_new(schema, batches)?);
+
+ self.register_table(&name, table)?;
+ self.return_empty_dataframe()
+ }
+ (true, true, Ok(_)) => Err(DataFusionError::Execution(
+ "'IF NOT EXISTS' cannot coexist with 'REPLACE'".to_string(),
+ )),
+ (_, _, Err(_)) => {
+ let schema = Arc::new(input.schema().as_ref().into());
+ let physical = DataFrame::new(self.state(), input);
+
+ let batches: Vec<_> = physical.collect_partitioned().await?;
+ let table = Arc::new(MemTable::try_new(schema, batches)?);
+
+ self.register_table(&name, table)?;
+ self.return_empty_dataframe()
+ }
+ (false, false, Ok(_)) => Err(DataFusionError::Execution(format!(
+ "Table '{name}' already exists"
+ ))),
+ }
+ }
+
+ async fn create_view(&self, cmd: CreateView) -> Result<DataFrame> {
+ let CreateView {
+ name,
+ input,
+ or_replace,
+ definition,
+ } = cmd;
+
+ let view = self.table(&name).await;
+
+ match (or_replace, view) {
+ (true, Ok(_)) => {
+ self.deregister_table(&name)?;
+ let table = Arc::new(ViewTable::try_new((*input).clone(), definition)?);
+
+ self.register_table(&name, table)?;
+ self.return_empty_dataframe()
+ }
+ (_, Err(_)) => {
+ let table = Arc::new(ViewTable::try_new((*input).clone(), definition)?);
+
+ self.register_table(&name, table)?;
+ self.return_empty_dataframe()
+ }
+ (false, Ok(_)) => Err(DataFusionError::Execution(format!(
+ "Table '{name}' already exists"
+ ))),
+ }
+ }
+
+ async fn create_catalog_schema(&self, cmd: CreateCatalogSchema) -> Result<DataFrame> {
+ let CreateCatalogSchema {
+ schema_name,
+ if_not_exists,
+ ..
+ } = cmd;
+
+ // sqlparser doesnt accept database / catalog as parameter to CREATE SCHEMA
+ // so for now, we default to default catalog
+ let tokens: Vec<&str> = schema_name.split('.').collect();
+ let (catalog, schema_name) = match tokens.len() {
+ 1 => {
+ let state = self.state.read();
+ let name = &state.config.options().catalog.default_catalog;
+ let catalog = state.catalog_list.catalog(name).ok_or_else(|| {
+ DataFusionError::Execution(format!(
+ "Missing default catalog '{name}'"
+ ))
+ })?;
+ (catalog, tokens[0])
+ }
+ 2 => {
+ let name = &tokens[0];
+ let catalog = self.catalog(name).ok_or_else(|| {
+ DataFusionError::Execution(format!("Missing catalog '{name}'"))
+ })?;
+ (catalog, tokens[1])
+ }
+ _ => {
+ return Err(DataFusionError::Execution(format!(
+ "Unable to parse catalog from {schema_name}"
+ )))
+ }
+ };
+ let schema = catalog.schema(schema_name);
+
+ match (if_not_exists, schema) {
+ (true, Some(_)) => self.return_empty_dataframe(),
+ (true, None) | (false, None) => {
+ let schema = Arc::new(MemorySchemaProvider::new());
+ catalog.register_schema(schema_name, schema)?;
+ self.return_empty_dataframe()
+ }
+ (false, Some(_)) => Err(DataFusionError::Execution(format!(
+ "Schema '{schema_name}' already exists"
+ ))),
+ }
+ }
+
+ async fn create_catalog(&self, cmd: CreateCatalog) -> Result<DataFrame> {
+ let CreateCatalog {
+ catalog_name,
+ if_not_exists,
+ ..
+ } = cmd;
+ let catalog = self.catalog(catalog_name.as_str());
+
+ match (if_not_exists, catalog) {
+ (true, Some(_)) => self.return_empty_dataframe(),
+ (true, None) | (false, None) => {
+ let new_catalog = Arc::new(MemoryCatalogProvider::new());
+ self.state
+ .write()
+ .catalog_list
+ .register_catalog(catalog_name, new_catalog);
+ self.return_empty_dataframe()
+ }
+ (false, Some(_)) => Err(DataFusionError::Execution(format!(
+ "Catalog '{catalog_name}' already exists"
+ ))),
+ }
+ }
+
async fn create_custom_table(
&self,
cmd: &CreateExternalTable,
diff --git a/datafusion/core/src/physical_plan/planner.rs b/datafusion/core/src/physical_plan/planner.rs
index 8158351718..782dcf1335 100644
--- a/datafusion/core/src/physical_plan/planner.rs
+++ b/datafusion/core/src/physical_plan/planner.rs
@@ -1087,13 +1087,14 @@ impl DefaultPhysicalPlanner {
let schema = SchemaRef::new(schema.as_ref().to_owned().into());
Ok(Arc::new(UnnestExec::new(input, column_exec, schema)))
}
- LogicalPlan::CreateExternalTable(_) => {
- // There is no default plan for "CREATE EXTERNAL
- // TABLE" -- it must be handled at a higher level (so
- // that the appropriate table can be registered with
+ LogicalPlan::Ddl(ddl) => {
+ // There is no default plan for DDl statements --
+ // it must be handled at a higher level (so that
+ // the appropriate table can be registered with
// the context)
+ let name = ddl.name();
Err(DataFusionError::NotImplemented(
- "Unsupported logical plan: CreateExternalTable".to_string(),
+ format!("Unsupported logical plan: {name}")
))
}
LogicalPlan::Prepare(_) => {
@@ -1104,33 +1105,6 @@ impl DefaultPhysicalPlanner {
"Unsupported logical plan: Prepare".to_string(),
))
}
- LogicalPlan::CreateCatalogSchema(_) => {
- // There is no default plan for "CREATE SCHEMA".
- // It must be handled at a higher level (so
- // that the schema can be registered with
- // the context)
- Err(DataFusionError::NotImplemented(
- "Unsupported logical plan: CreateCatalogSchema".to_string(),
- ))
- }
- LogicalPlan::CreateCatalog(_) => {
- // There is no default plan for "CREATE DATABASE".
- // It must be handled at a higher level (so
- // that the schema can be registered with
- // the context)
- Err(DataFusionError::NotImplemented(
- "Unsupported logical plan: CreateCatalog".to_string(),
- ))
- }
- LogicalPlan::CreateMemoryTable(_) => {
- // There is no default plan for "CREATE MEMORY 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: CreateMemoryTable".to_string(),
- ))
- }
LogicalPlan::DropTable(_) => {
// There is no default plan for "DROP TABLE".
// It must be handled at a higher level (so
@@ -1149,15 +1123,6 @@ impl DefaultPhysicalPlanner {
"Unsupported logical plan: DropView".to_string(),
))
}
- LogicalPlan::CreateView(_) => {
- // There is no default plan for "CREATE 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: CreateView".to_string(),
- ))
- }
LogicalPlan::Dml(_) => {
// DataFusion is a read-only query engine, but also a library, so consumers may implement this
Err(DataFusionError::NotImplemented(
diff --git a/datafusion/core/tests/sql/order.rs b/datafusion/core/tests/sql/order.rs
index 5691a7cfd6..a85fee776d 100644
--- a/datafusion/core/tests/sql/order.rs
+++ b/datafusion/core/tests/sql/order.rs
@@ -19,6 +19,7 @@ use super::*;
use datafusion::datasource::datasource::TableProviderFactory;
use datafusion::datasource::listing::ListingTable;
use datafusion::datasource::listing_table_factory::ListingTableFactory;
+use datafusion_expr::logical_plan::DdlStatement;
use test_utils::{batches_to_vec, partitions_to_sorted_vec};
#[tokio::test]
@@ -47,20 +48,18 @@ async fn sort_with_lots_of_repetition_values() -> Result<()> {
async fn create_external_table_with_order() -> Result<()> {
let ctx = SessionContext::new();
let sql = "CREATE EXTERNAL TABLE dt (a_id integer, a_str string, a_bool boolean) STORED AS CSV WITH ORDER (a_id ASC) LOCATION 'file://path/to/table';";
- if let LogicalPlan::CreateExternalTable(cmd) =
- ctx.state().create_logical_plan(sql).await?
- {
- let listing_table_factory = Arc::new(ListingTableFactory::new());
- let table_dyn = listing_table_factory.create(&ctx.state(), &cmd).await?;
- let table = table_dyn.as_any().downcast_ref::<ListingTable>().unwrap();
- assert_eq!(cmd.order_exprs.len(), 1);
- assert_eq!(
- &cmd.order_exprs,
- table.options().file_sort_order.as_ref().unwrap()
- )
- } else {
+ let LogicalPlan::Ddl(DdlStatement::CreateExternalTable(cmd)) = ctx.state().create_logical_plan(sql).await? else {
panic!("Wrong command")
- }
+ };
+
+ let listing_table_factory = Arc::new(ListingTableFactory::new());
+ let table_dyn = listing_table_factory.create(&ctx.state(), &cmd).await?;
+ let table = table_dyn.as_any().downcast_ref::<ListingTable>().unwrap();
+ assert_eq!(cmd.order_exprs.len(), 1);
+ assert_eq!(
+ &cmd.order_exprs,
+ table.options().file_sort_order.as_ref().unwrap()
+ );
Ok(())
}
@@ -190,7 +189,7 @@ WITH
m1(t) AS (
VALUES (0), (1)),
u AS (
- SELECT 0 as m, t FROM m0 GROUP BY 1, 2),
+ SELECT 0 as m, t FROM m0 GROUP BY 1, 2),
v AS (
SELECT 1 as m, t FROM m1 GROUP BY 1, 2)
SELECT * FROM u
diff --git a/datafusion/expr/src/lib.rs b/datafusion/expr/src/lib.rs
index 0d5963021e..c4cab235c0 100644
--- a/datafusion/expr/src/lib.rs
+++ b/datafusion/expr/src/lib.rs
@@ -66,20 +66,7 @@ pub use function::{
StateTypeFunction,
};
pub use literal::{lit, lit_timestamp_nano, Literal, TimestampLiteral};
-pub use logical_plan::{
- builder::{
- build_join_schema, union, wrap_projection_for_join_if_necessary, UNNAMED_TABLE,
- },
- Aggregate, Analyze, CreateCatalog, CreateCatalogSchema, CreateExternalTable,
- CreateMemoryTable, CreateView, CrossJoin, DescribeTable, Distinct, DmlStatement,
- DropTable, DropView, EmptyRelation, Explain, Extension, Filter, Join, JoinConstraint,
- JoinType, Limit, LogicalPlan, LogicalPlanBuilder, Partitioning, PlanType, Prepare,
- Projection, Repartition, SetVariable, Sort, Statement, StringifiedPlan, Subquery,
- SubqueryAlias, TableScan, ToStringifiedPlan, TransactionAccessMode,
- TransactionConclusion, TransactionEnd, TransactionIsolationLevel, TransactionStart,
- Union, Unnest, UserDefinedLogicalNode, UserDefinedLogicalNodeCore, Values, Window,
- WriteOp,
-};
+pub use logical_plan::*;
pub use nullif::SUPPORTED_NULLIF_TYPES;
pub use operator::Operator;
pub use signature::{Signature, TypeSignature, Volatility};
diff --git a/datafusion/expr/src/logical_plan/ddl.rs b/datafusion/expr/src/logical_plan/ddl.rs
new file mode 100644
index 0000000000..a0a3c180bf
--- /dev/null
+++ b/datafusion/expr/src/logical_plan/ddl.rs
@@ -0,0 +1,233 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use datafusion_common::Column;
+use datafusion_common::{
+ parsers::CompressionTypeVariant, DFSchemaRef, OwnedTableReference,
+};
+use std::collections::HashMap;
+use std::sync::Arc;
+use std::{
+ fmt::{self, Display},
+ hash::{Hash, Hasher},
+};
+
+use crate::{Expr, LogicalPlan};
+
+/// Various types of DDL (CREATE / DROP) catalog manipulation
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub enum DdlStatement {
+ /// Creates an external table.
+ CreateExternalTable(CreateExternalTable),
+ /// Creates an in memory table.
+ CreateMemoryTable(CreateMemoryTable),
+ /// Creates a new view.
+ CreateView(CreateView),
+ /// Creates a new catalog schema.
+ CreateCatalogSchema(CreateCatalogSchema),
+ /// Creates a new catalog (aka "Database").
+ CreateCatalog(CreateCatalog),
+}
+
+impl DdlStatement {
+ /// Get a reference to the logical plan's schema
+ pub fn schema(&self) -> &DFSchemaRef {
+ match self {
+ DdlStatement::CreateExternalTable(CreateExternalTable { schema, .. }) => {
+ schema
+ }
+ DdlStatement::CreateMemoryTable(CreateMemoryTable { input, .. })
+ | DdlStatement::CreateView(CreateView { input, .. }) => input.schema(),
+ DdlStatement::CreateCatalogSchema(CreateCatalogSchema { schema, .. }) => {
+ schema
+ }
+ DdlStatement::CreateCatalog(CreateCatalog { schema, .. }) => schema,
+ }
+ }
+
+ /// Return a descriptive string describing the type of this
+ /// [`DdlStatement`]
+ pub fn name(&self) -> &str {
+ match self {
+ DdlStatement::CreateExternalTable(_) => "CreateExternalTable",
+ DdlStatement::CreateMemoryTable(_) => "CreateMemoryTable",
+ DdlStatement::CreateView(_) => "CreateView",
+ DdlStatement::CreateCatalogSchema(_) => "CreateCatalogSchema",
+ DdlStatement::CreateCatalog(_) => "CreateCatalog",
+ }
+ }
+
+ /// Return all inputs for this plan
+ pub fn inputs(&self) -> Vec<&LogicalPlan> {
+ match self {
+ DdlStatement::CreateExternalTable(_) => vec![],
+ DdlStatement::CreateCatalogSchema(_) => vec![],
+ DdlStatement::CreateCatalog(_) => vec![],
+ DdlStatement::CreateMemoryTable(CreateMemoryTable { input, .. }) => {
+ vec![input]
+ }
+ DdlStatement::CreateView(CreateView { input, .. }) => vec![input],
+ }
+ }
+
+ /// Return a `format`able structure with the a human readable
+ /// description of this LogicalPlan node per node, not including
+ /// children.
+ ///
+ /// See [crate::LogicalPlan::display] for an example
+ pub fn display(&self) -> impl fmt::Display + '_ {
+ struct Wrapper<'a>(&'a DdlStatement);
+ impl<'a> Display for Wrapper<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self.0 {
+ DdlStatement::CreateExternalTable(CreateExternalTable {
+ ref name,
+ ..
+ }) => {
+ write!(f, "CreateExternalTable: {name:?}")
+ }
+ DdlStatement::CreateMemoryTable(CreateMemoryTable {
+ name,
+ primary_key,
+ ..
+ }) => {
+ let pk: Vec<String> =
+ primary_key.iter().map(|c| c.name.to_string()).collect();
+ let mut pk = pk.join(", ");
+ if !pk.is_empty() {
+ pk = format!(" primary_key=[{pk}]");
+ }
+ write!(f, "CreateMemoryTable: {name:?}{pk}")
+ }
+ DdlStatement::CreateView(CreateView { name, .. }) => {
+ write!(f, "CreateView: {name:?}")
+ }
+ DdlStatement::CreateCatalogSchema(CreateCatalogSchema {
+ schema_name,
+ ..
+ }) => {
+ write!(f, "CreateCatalogSchema: {schema_name:?}")
+ }
+ DdlStatement::CreateCatalog(CreateCatalog {
+ catalog_name, ..
+ }) => {
+ write!(f, "CreateCatalog: {catalog_name:?}")
+ }
+ }
+ }
+ }
+ Wrapper(self)
+ }
+}
+
+/// Creates an external table.
+#[derive(Clone, PartialEq, Eq)]
+pub struct CreateExternalTable {
+ /// The table schema
+ pub schema: DFSchemaRef,
+ /// The table name
+ pub name: OwnedTableReference,
+ /// The physical location
+ pub location: String,
+ /// The file type of physical file
+ pub file_type: String,
+ /// Whether the CSV file contains a header
+ pub has_header: bool,
+ /// Delimiter for CSV
+ pub delimiter: char,
+ /// Partition Columns
+ pub table_partition_cols: Vec<String>,
+ /// Option to not error if table already exists
+ pub if_not_exists: bool,
+ /// SQL used to create the table, if available
+ pub definition: Option<String>,
+ /// Order expressions supplied by user
+ pub order_exprs: Vec<Expr>,
+ /// File compression type (GZIP, BZIP2, XZ, ZSTD)
+ pub file_compression_type: CompressionTypeVariant,
+ /// Table(provider) specific options
+ pub options: HashMap<String, String>,
+}
+
+// Hashing refers to a subset of fields considered in PartialEq.
+#[allow(clippy::derived_hash_with_manual_eq)]
+impl Hash for CreateExternalTable {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ self.schema.hash(state);
+ self.name.hash(state);
+ self.location.hash(state);
+ self.file_type.hash(state);
+ self.has_header.hash(state);
+ self.delimiter.hash(state);
+ self.table_partition_cols.hash(state);
+ self.if_not_exists.hash(state);
+ self.definition.hash(state);
+ self.file_compression_type.hash(state);
+ self.order_exprs.hash(state);
+ self.options.len().hash(state); // HashMap is not hashable
+ }
+}
+
+/// Creates an in memory table.
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct CreateMemoryTable {
+ /// The table name
+ pub name: OwnedTableReference,
+ /// The ordered list of columns in the primary key, or an empty vector if none
+ pub primary_key: Vec<Column>,
+ /// The logical plan
+ pub input: Arc<LogicalPlan>,
+ /// Option to not error if table already exists
+ pub if_not_exists: bool,
+ /// Option to replace table content if table already exists
+ pub or_replace: bool,
+}
+
+/// Creates a view.
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct CreateView {
+ /// The table name
+ pub name: OwnedTableReference,
+ /// The logical plan
+ pub input: Arc<LogicalPlan>,
+ /// Option to not error if table already exists
+ pub or_replace: bool,
+ /// SQL used to create the view, if available
+ pub definition: Option<String>,
+}
+
+/// Creates a catalog (aka "Database").
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct CreateCatalog {
+ /// The catalog name
+ pub catalog_name: String,
+ /// Do nothing (except issuing a notice) if a schema with the same name already exists
+ pub if_not_exists: bool,
+ /// Empty schema
+ pub schema: DFSchemaRef,
+}
+
+/// Creates a schema.
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct CreateCatalogSchema {
+ /// The table schema
+ pub schema_name: String,
+ /// Do nothing (except issuing a notice) if a schema with the same name already exists
+ pub if_not_exists: bool,
+ /// Empty schema
+ pub schema: DFSchemaRef,
+}
diff --git a/datafusion/expr/src/logical_plan/mod.rs b/datafusion/expr/src/logical_plan/mod.rs
index 38dff03d14..74588adb20 100644
--- a/datafusion/expr/src/logical_plan/mod.rs
+++ b/datafusion/expr/src/logical_plan/mod.rs
@@ -16,19 +16,26 @@
// under the License.
pub mod builder;
+mod ddl;
pub mod display;
mod dml;
mod extension;
mod plan;
mod statement;
-pub use builder::{table_scan, LogicalPlanBuilder};
+pub use builder::{
+ build_join_schema, table_scan, union, wrap_projection_for_join_if_necessary,
+ LogicalPlanBuilder, UNNAMED_TABLE,
+};
+pub use ddl::{
+ CreateCatalog, CreateCatalogSchema, CreateExternalTable, CreateMemoryTable,
+ CreateView, DdlStatement,
+};
pub use dml::{DmlStatement, WriteOp};
pub use plan::{
- Aggregate, Analyze, CreateCatalog, CreateCatalogSchema, CreateExternalTable,
- CreateMemoryTable, CreateView, CrossJoin, DescribeTable, Distinct, DropTable,
- DropView, EmptyRelation, Explain, Extension, Filter, Join, JoinConstraint, JoinType,
- Limit, LogicalPlan, Partitioning, PlanType, Prepare, Projection, Repartition, Sort,
+ 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,
};
diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rs
index 3dcd61667a..62cbb5245d 100644
--- a/datafusion/expr/src/logical_plan/plan.rs
+++ b/datafusion/expr/src/logical_plan/plan.rs
@@ -19,8 +19,6 @@
use crate::logical_plan::display::{GraphvizVisitor, IndentVisitor};
use crate::logical_plan::extension::UserDefinedLogicalNode;
use crate::logical_plan::{DmlStatement, Statement};
-
-use crate::logical_plan::plan;
use crate::utils::{
enumerate_grouping_sets, exprlist_to_fields, find_out_reference_exprs, from_plan,
grouping_set_expr_count, grouping_set_to_exprlist, inspect_expr_pre,
@@ -29,7 +27,6 @@ use crate::{
build_join_schema, Expr, ExprSchemable, TableProviderFilterPushDown, TableSource,
};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
-use datafusion_common::parsers::CompressionTypeVariant;
use datafusion_common::tree_node::{
Transformed, TreeNode, TreeNodeVisitor, VisitRecursion,
};
@@ -43,6 +40,8 @@ use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::sync::Arc;
+use super::DdlStatement;
+
/// A LogicalPlan represents the different types of relational
/// operators (such as Projection, Filter, etc) and can be created by
/// the SQL query planner and the DataFrame API.
@@ -92,16 +91,6 @@ pub enum LogicalPlan {
Limit(Limit),
/// [`Statement`]
Statement(Statement),
- /// Creates an external table.
- CreateExternalTable(CreateExternalTable),
- /// Creates an in memory table.
- CreateMemoryTable(CreateMemoryTable),
- /// Creates a new view.
- CreateView(CreateView),
- /// Creates a new catalog schema.
- CreateCatalogSchema(CreateCatalogSchema),
- /// Creates a new catalog (aka "Database").
- CreateCatalog(CreateCatalog),
/// Drops a table.
DropTable(DropTable),
/// Drops a view.
@@ -124,6 +113,8 @@ pub enum LogicalPlan {
Prepare(Prepare),
/// Insert / Update / Delete
Dml(DmlStatement),
+ /// CREATE / DROP TABLES / VIEWS / SCHEMAs
+ Ddl(DdlStatement),
/// Describe the schema of table
DescribeTable(DescribeTable),
/// Unnest a column that contains a nested list type.
@@ -152,26 +143,18 @@ impl LogicalPlan {
LogicalPlan::Statement(statement) => statement.schema(),
LogicalPlan::Subquery(Subquery { subquery, .. }) => subquery.schema(),
LogicalPlan::SubqueryAlias(SubqueryAlias { schema, .. }) => schema,
- LogicalPlan::CreateExternalTable(CreateExternalTable { schema, .. }) => {
- schema
- }
LogicalPlan::Prepare(Prepare { input, .. }) => input.schema(),
LogicalPlan::Explain(explain) => &explain.schema,
LogicalPlan::Analyze(analyze) => &analyze.schema,
LogicalPlan::Extension(extension) => extension.node.schema(),
LogicalPlan::Union(Union { schema, .. }) => schema,
- LogicalPlan::CreateMemoryTable(CreateMemoryTable { input, .. })
- | LogicalPlan::CreateView(CreateView { input, .. }) => input.schema(),
- LogicalPlan::CreateCatalogSchema(CreateCatalogSchema { schema, .. }) => {
- schema
- }
- LogicalPlan::CreateCatalog(CreateCatalog { schema, .. }) => schema,
LogicalPlan::DropTable(DropTable { schema, .. }) => schema,
LogicalPlan::DropView(DropView { schema, .. }) => schema,
LogicalPlan::DescribeTable(DescribeTable { dummy_schema, .. }) => {
dummy_schema
}
LogicalPlan::Dml(DmlStatement { table_schema, .. }) => table_schema,
+ LogicalPlan::Ddl(ddl) => ddl.schema(),
LogicalPlan::Unnest(Unnest { schema, .. }) => schema,
}
}
@@ -215,9 +198,7 @@ impl LogicalPlan {
LogicalPlan::Explain(_)
| LogicalPlan::Analyze(_)
| LogicalPlan::EmptyRelation(_)
- | LogicalPlan::CreateExternalTable(_)
- | LogicalPlan::CreateCatalogSchema(_)
- | LogicalPlan::CreateCatalog(_)
+ | LogicalPlan::Ddl(_)
| LogicalPlan::Dml(_)
| LogicalPlan::Values(_)
| LogicalPlan::SubqueryAlias(_)
@@ -231,8 +212,6 @@ impl LogicalPlan {
| LogicalPlan::Subquery(_)
| LogicalPlan::Repartition(_)
| LogicalPlan::Sort(_)
- | LogicalPlan::CreateMemoryTable(_)
- | LogicalPlan::CreateView(_)
| LogicalPlan::Filter(_)
| LogicalPlan::Distinct(_)
| LogicalPlan::Prepare(_) => {
@@ -357,11 +336,6 @@ impl LogicalPlan {
| LogicalPlan::SubqueryAlias(_)
| LogicalPlan::Limit(_)
| LogicalPlan::Statement(_)
- | LogicalPlan::CreateExternalTable(_)
- | LogicalPlan::CreateMemoryTable(_)
- | LogicalPlan::CreateView(_)
- | LogicalPlan::CreateCatalogSchema(_)
- | LogicalPlan::CreateCatalog(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::CrossJoin(_)
@@ -370,6 +344,7 @@ impl LogicalPlan {
| LogicalPlan::Union(_)
| LogicalPlan::Distinct(_)
| LogicalPlan::Dml(_)
+ | LogicalPlan::Ddl(_)
| LogicalPlan::DescribeTable(_)
| LogicalPlan::Prepare(_) => Ok(()),
}
@@ -377,7 +352,7 @@ impl LogicalPlan {
/// returns all inputs of this `LogicalPlan` node. Does not
/// include inputs to inputs, or subqueries.
- pub fn inputs(self: &LogicalPlan) -> Vec<&LogicalPlan> {
+ pub fn inputs(&self) -> Vec<&LogicalPlan> {
match self {
LogicalPlan::Projection(Projection { input, .. }) => vec![input],
LogicalPlan::Filter(Filter { input, .. }) => vec![input],
@@ -398,20 +373,14 @@ impl LogicalPlan {
LogicalPlan::Explain(explain) => vec![&explain.plan],
LogicalPlan::Analyze(analyze) => vec![&analyze.input],
LogicalPlan::Dml(write) => vec![&write.input],
- LogicalPlan::CreateMemoryTable(CreateMemoryTable { input, .. })
- | LogicalPlan::CreateView(CreateView { input, .. })
- | LogicalPlan::Prepare(Prepare { input, .. }) => {
- vec![input]
- }
+ LogicalPlan::Ddl(ddl) => ddl.inputs(),
LogicalPlan::Unnest(Unnest { input, .. }) => vec![input],
+ LogicalPlan::Prepare(Prepare { input, .. }) => vec![input],
// plans without inputs
LogicalPlan::TableScan { .. }
| LogicalPlan::Statement { .. }
| LogicalPlan::EmptyRelation { .. }
| LogicalPlan::Values { .. }
- | LogicalPlan::CreateExternalTable(_)
- | LogicalPlan::CreateCatalogSchema(_)
- | LogicalPlan::CreateCatalog(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::DescribeTable(_) => vec![],
@@ -734,7 +703,7 @@ impl LogicalPlan {
Expr::ScalarSubquery(qry) => {
let subquery =
Arc::new(qry.subquery.replace_params_with_values(param_values)?);
- Ok(Transformed::Yes(Expr::ScalarSubquery(plan::Subquery {
+ Ok(Transformed::Yes(Expr::ScalarSubquery(Subquery {
subquery,
outer_ref_columns: qry.outer_ref_columns.clone(),
})))
@@ -1019,6 +988,9 @@ impl LogicalPlan {
LogicalPlan::Dml(DmlStatement { table_name, op, .. }) => {
write!(f, "Dml: op=[{op}] table=[{table_name}]")
}
+ LogicalPlan::Ddl(ddl) => {
+ write!(f, "{}", ddl.display())
+ }
LogicalPlan::Filter(Filter {
predicate: ref expr,
..
@@ -1135,39 +1107,6 @@ impl LogicalPlan {
LogicalPlan::Statement(statement) => {
write!(f, "{}", statement.display())
}
- LogicalPlan::CreateExternalTable(CreateExternalTable {
- ref name,
- ..
- }) => {
- write!(f, "CreateExternalTable: {name:?}")
- }
- LogicalPlan::CreateMemoryTable(CreateMemoryTable {
- name,
- primary_key,
- ..
- }) => {
- let pk: Vec<String> =
- primary_key.iter().map(|c| c.name.to_string()).collect();
- let mut pk = pk.join(", ");
- if !pk.is_empty() {
- pk = format!(" primary_key=[{pk}]");
- }
- write!(f, "CreateMemoryTable: {name:?}{pk}")
- }
- LogicalPlan::CreateView(CreateView { name, .. }) => {
- write!(f, "CreateView: {name:?}")
- }
- LogicalPlan::CreateCatalogSchema(CreateCatalogSchema {
- schema_name,
- ..
- }) => {
- write!(f, "CreateCatalogSchema: {schema_name:?}")
- }
- LogicalPlan::CreateCatalog(CreateCatalog {
- catalog_name, ..
- }) => {
- write!(f, "CreateCatalog: {catalog_name:?}")
- }
LogicalPlan::DropTable(DropTable {
name, if_exists, ..
}) => {
@@ -1288,28 +1227,6 @@ pub enum JoinConstraint {
Using,
}
-/// Creates a catalog (aka "Database").
-#[derive(Clone, PartialEq, Eq, Hash)]
-pub struct CreateCatalog {
- /// The catalog name
- pub catalog_name: String,
- /// Do nothing (except issuing a notice) if a schema with the same name already exists
- pub if_not_exists: bool,
- /// Empty schema
- pub schema: DFSchemaRef,
-}
-
-/// Creates a schema.
-#[derive(Clone, PartialEq, Eq, Hash)]
-pub struct CreateCatalogSchema {
- /// The table schema
- pub schema_name: String,
- /// Do nothing (except issuing a notice) if a schema with the same name already exists
- pub if_not_exists: bool,
- /// Empty schema
- pub schema: DFSchemaRef,
-}
-
/// Drops a table.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct DropTable {
@@ -1579,82 +1496,6 @@ pub struct Union {
pub schema: DFSchemaRef,
}
-/// Creates an in memory table.
-#[derive(Clone, PartialEq, Eq, Hash)]
-pub struct CreateMemoryTable {
- /// The table name
- pub name: OwnedTableReference,
- /// The ordered list of columns in the primary key, or an empty vector if none
- pub primary_key: Vec<Column>,
- /// The logical plan
- pub input: Arc<LogicalPlan>,
- /// Option to not error if table already exists
- pub if_not_exists: bool,
- /// Option to replace table content if table already exists
- pub or_replace: bool,
-}
-
-/// Creates a view.
-#[derive(Clone, PartialEq, Eq, Hash)]
-pub struct CreateView {
- /// The table name
- pub name: OwnedTableReference,
- /// The logical plan
- pub input: Arc<LogicalPlan>,
- /// Option to not error if table already exists
- pub or_replace: bool,
- /// SQL used to create the view, if available
- pub definition: Option<String>,
-}
-
-/// Creates an external table.
-#[derive(Clone, PartialEq, Eq)]
-pub struct CreateExternalTable {
- /// The table schema
- pub schema: DFSchemaRef,
- /// The table name
- pub name: OwnedTableReference,
- /// The physical location
- pub location: String,
- /// The file type of physical file
- pub file_type: String,
- /// Whether the CSV file contains a header
- pub has_header: bool,
- /// Delimiter for CSV
- pub delimiter: char,
- /// Partition Columns
- pub table_partition_cols: Vec<String>,
- /// Option to not error if table already exists
- pub if_not_exists: bool,
- /// SQL used to create the table, if available
- pub definition: Option<String>,
- /// Order expressions supplied by user
- pub order_exprs: Vec<Expr>,
- /// File compression type (GZIP, BZIP2, XZ, ZSTD)
- pub file_compression_type: CompressionTypeVariant,
- /// Table(provider) specific options
- pub options: HashMap<String, String>,
-}
-
-// Hashing refers to a subset of fields considered in PartialEq.
-#[allow(clippy::derived_hash_with_manual_eq)]
-impl Hash for CreateExternalTable {
- fn hash<H: Hasher>(&self, state: &mut H) {
- self.schema.hash(state);
- self.name.hash(state);
- self.location.hash(state);
- self.file_type.hash(state);
- self.has_header.hash(state);
- self.delimiter.hash(state);
- self.table_partition_cols.hash(state);
- self.if_not_exists.hash(state);
- self.definition.hash(state);
- self.file_compression_type.hash(state);
- self.order_exprs.hash(state);
- self.options.len().hash(state); // HashMap is not hashable
- }
-}
-
/// Prepare a statement but do not execute it. Prepare statements can have 0 or more
/// `Expr::Placeholder` expressions that are filled in during execution
#[derive(Clone, PartialEq, Eq, Hash)]
diff --git a/datafusion/expr/src/utils.rs b/datafusion/expr/src/utils.rs
index e6b0cc43f1..373e8b72ef 100644
--- a/datafusion/expr/src/utils.rs
+++ b/datafusion/expr/src/utils.rs
@@ -20,13 +20,14 @@
use crate::expr::{Sort, WindowFunction};
use crate::logical_plan::builder::build_join_schema;
use crate::logical_plan::{
- Aggregate, Analyze, CreateMemoryTable, CreateView, Distinct, Extension, Filter, Join,
- Limit, Partitioning, Prepare, Projection, Repartition, Sort as SortPlan, Subquery,
- SubqueryAlias, Union, Unnest, Values, Window,
+ Aggregate, Analyze, Distinct, Extension, Filter, Join, Limit, Partitioning, Prepare,
+ Projection, Repartition, Sort as SortPlan, Subquery, SubqueryAlias, Union, Unnest,
+ Values, Window,
};
use crate::{
- BinaryExpr, Cast, DmlStatement, Expr, ExprSchemable, GroupingSet, LogicalPlan,
- LogicalPlanBuilder, Operator, TableScan, TryCast,
+ BinaryExpr, Cast, CreateMemoryTable, CreateView, DdlStatement, DmlStatement, Expr,
+ ExprSchemable, GroupingSet, LogicalPlan, LogicalPlanBuilder, Operator, TableScan,
+ TryCast,
};
use arrow::datatypes::{DataType, TimeUnit};
use datafusion_common::tree_node::{
@@ -836,29 +837,31 @@ pub fn from_plan(
fetch: *fetch,
input: Arc::new(inputs[0].clone()),
})),
- LogicalPlan::CreateMemoryTable(CreateMemoryTable {
+ LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(CreateMemoryTable {
name,
if_not_exists,
or_replace,
..
- }) => Ok(LogicalPlan::CreateMemoryTable(CreateMemoryTable {
- input: Arc::new(inputs[0].clone()),
- primary_key: vec![],
- name: name.clone(),
- if_not_exists: *if_not_exists,
- or_replace: *or_replace,
- })),
- LogicalPlan::CreateView(CreateView {
+ })) => Ok(LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(
+ CreateMemoryTable {
+ input: Arc::new(inputs[0].clone()),
+ primary_key: vec![],
+ name: name.clone(),
+ if_not_exists: *if_not_exists,
+ or_replace: *or_replace,
+ },
+ ))),
+ LogicalPlan::Ddl(DdlStatement::CreateView(CreateView {
name,
or_replace,
definition,
..
- }) => Ok(LogicalPlan::CreateView(CreateView {
+ })) => Ok(LogicalPlan::Ddl(DdlStatement::CreateView(CreateView {
input: Arc::new(inputs[0].clone()),
name: name.clone(),
or_replace: *or_replace,
definition: definition.clone(),
- })),
+ }))),
LogicalPlan::Extension(e) => Ok(LogicalPlan::Extension(Extension {
node: e.node.from_template(expr, inputs),
})),
@@ -911,12 +914,10 @@ pub fn from_plan(
}))
}
LogicalPlan::EmptyRelation(_)
- | LogicalPlan::CreateExternalTable(_)
+ | LogicalPlan::Ddl(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
- | LogicalPlan::Statement(_)
- | LogicalPlan::CreateCatalogSchema(_)
- | LogicalPlan::CreateCatalog(_) => {
+ | 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");
assert!(inputs.is_empty(), "{plan:?} should have no inputs");
diff --git a/datafusion/optimizer/src/common_subexpr_eliminate.rs b/datafusion/optimizer/src/common_subexpr_eliminate.rs
index 2930a8d817..7e53c927c4 100644
--- a/datafusion/optimizer/src/common_subexpr_eliminate.rs
+++ b/datafusion/optimizer/src/common_subexpr_eliminate.rs
@@ -227,13 +227,9 @@ impl OptimizerRule for CommonSubexprEliminate {
| LogicalPlan::Subquery(_)
| LogicalPlan::SubqueryAlias(_)
| LogicalPlan::Limit(_)
- | LogicalPlan::CreateExternalTable(_)
+ | LogicalPlan::Ddl(_)
| LogicalPlan::Explain(_)
| LogicalPlan::Analyze(_)
- | LogicalPlan::CreateMemoryTable(_)
- | LogicalPlan::CreateView(_)
- | LogicalPlan::CreateCatalogSchema(_)
- | LogicalPlan::CreateCatalog(_)
| LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::Statement(_)
diff --git a/datafusion/proto/src/logical_plan/mod.rs b/datafusion/proto/src/logical_plan/mod.rs
index 0c58fada0a..82a1af8843 100644
--- a/datafusion/proto/src/logical_plan/mod.rs
+++ b/datafusion/proto/src/logical_plan/mod.rs
@@ -42,6 +42,7 @@ use datafusion_common::{
context, parsers::CompressionTypeVariant, DataFusionError, OwnedTableReference,
Result,
};
+use datafusion_expr::logical_plan::DdlStatement;
use datafusion_expr::{
logical_plan::{
builder::project, Aggregate, CreateCatalog, CreateCatalogSchema,
@@ -510,7 +511,7 @@ impl AsLogicalPlan for LogicalPlanNode {
.map(|expr| from_proto::parse_expr(expr, ctx))
.collect::<Result<Vec<Expr>, _>>()?;
- Ok(LogicalPlan::CreateExternalTable(CreateExternalTable {
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateExternalTable(CreateExternalTable {
schema: pb_schema.try_into()?,
name: from_owned_table_reference(create_extern_table.name.as_ref(), "CreateExternalTable")?,
location: create_extern_table.location.clone(),
@@ -527,7 +528,7 @@ impl AsLogicalPlan for LogicalPlanNode {
file_compression_type: CompressionTypeVariant::from_str(&create_extern_table.file_compression_type).map_err(|_| DataFusionError::NotImplemented(format!("Unsupported file compression type {}", create_extern_table.file_compression_type)))?,
definition,
options: create_extern_table.options.clone(),
- }))
+ })))
}
LogicalPlanType::CreateView(create_view) => {
let plan = create_view
@@ -541,7 +542,7 @@ impl AsLogicalPlan for LogicalPlanNode {
None
};
- Ok(LogicalPlan::CreateView(CreateView {
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateView(CreateView {
name: from_owned_table_reference(
create_view.name.as_ref(),
"CreateView",
@@ -549,7 +550,7 @@ impl AsLogicalPlan for LogicalPlanNode {
input: Arc::new(plan),
or_replace: create_view.or_replace,
definition,
- }))
+ })))
}
LogicalPlanType::CreateCatalogSchema(create_catalog_schema) => {
let pb_schema = (create_catalog_schema.schema.clone()).ok_or_else(|| {
@@ -558,11 +559,13 @@ impl AsLogicalPlan for LogicalPlanNode {
))
})?;
- Ok(LogicalPlan::CreateCatalogSchema(CreateCatalogSchema {
- schema_name: create_catalog_schema.schema_name.clone(),
- if_not_exists: create_catalog_schema.if_not_exists,
- schema: pb_schema.try_into()?,
- }))
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateCatalogSchema(
+ CreateCatalogSchema {
+ schema_name: create_catalog_schema.schema_name.clone(),
+ if_not_exists: create_catalog_schema.if_not_exists,
+ schema: pb_schema.try_into()?,
+ },
+ )))
}
LogicalPlanType::CreateCatalog(create_catalog) => {
let pb_schema = (create_catalog.schema.clone()).ok_or_else(|| {
@@ -571,11 +574,13 @@ impl AsLogicalPlan for LogicalPlanNode {
))
})?;
- Ok(LogicalPlan::CreateCatalog(CreateCatalog {
- catalog_name: create_catalog.catalog_name.clone(),
- if_not_exists: create_catalog.if_not_exists,
- schema: pb_schema.try_into()?,
- }))
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateCatalog(
+ CreateCatalog {
+ catalog_name: create_catalog.catalog_name.clone(),
+ if_not_exists: create_catalog.if_not_exists,
+ schema: pb_schema.try_into()?,
+ },
+ )))
}
LogicalPlanType::Analyze(analyze) => {
let input: LogicalPlan =
@@ -1166,20 +1171,22 @@ impl AsLogicalPlan for LogicalPlanNode {
},
)),
}),
- LogicalPlan::CreateExternalTable(CreateExternalTable {
- name,
- location,
- file_type,
- has_header,
- delimiter,
- schema: df_schema,
- table_partition_cols,
- if_not_exists,
- definition,
- file_compression_type,
- order_exprs,
- options,
- }) => Ok(protobuf::LogicalPlanNode {
+ LogicalPlan::Ddl(DdlStatement::CreateExternalTable(
+ CreateExternalTable {
+ name,
+ location,
+ file_type,
+ has_header,
+ delimiter,
+ schema: df_schema,
+ table_partition_cols,
+ if_not_exists,
+ definition,
+ file_compression_type,
+ order_exprs,
+ options,
+ },
+ )) => Ok(protobuf::LogicalPlanNode {
logical_plan_type: Some(LogicalPlanType::CreateExternalTable(
protobuf::CreateExternalTableNode {
name: Some(name.clone().into()),
@@ -1200,12 +1207,12 @@ impl AsLogicalPlan for LogicalPlanNode {
},
)),
}),
- LogicalPlan::CreateView(CreateView {
+ LogicalPlan::Ddl(DdlStatement::CreateView(CreateView {
name,
input,
or_replace,
definition,
- }) => Ok(protobuf::LogicalPlanNode {
+ })) => Ok(protobuf::LogicalPlanNode {
logical_plan_type: Some(LogicalPlanType::CreateView(Box::new(
protobuf::CreateViewNode {
name: Some(name.clone().into()),
@@ -1218,11 +1225,13 @@ impl AsLogicalPlan for LogicalPlanNode {
},
))),
}),
- LogicalPlan::CreateCatalogSchema(CreateCatalogSchema {
- schema_name,
- if_not_exists,
- schema: df_schema,
- }) => Ok(protobuf::LogicalPlanNode {
+ LogicalPlan::Ddl(DdlStatement::CreateCatalogSchema(
+ CreateCatalogSchema {
+ schema_name,
+ if_not_exists,
+ schema: df_schema,
+ },
+ )) => Ok(protobuf::LogicalPlanNode {
logical_plan_type: Some(LogicalPlanType::CreateCatalogSchema(
protobuf::CreateCatalogSchemaNode {
schema_name: schema_name.clone(),
@@ -1231,11 +1240,11 @@ impl AsLogicalPlan for LogicalPlanNode {
},
)),
}),
- LogicalPlan::CreateCatalog(CreateCatalog {
+ LogicalPlan::Ddl(DdlStatement::CreateCatalog(CreateCatalog {
catalog_name,
if_not_exists,
schema: df_schema,
- }) => Ok(protobuf::LogicalPlanNode {
+ })) => Ok(protobuf::LogicalPlanNode {
logical_plan_type: Some(LogicalPlanType::CreateCatalog(
protobuf::CreateCatalogNode {
catalog_name: catalog_name.clone(),
@@ -1354,7 +1363,7 @@ impl AsLogicalPlan for LogicalPlanNode {
LogicalPlan::Unnest(_) => Err(proto_error(
"LogicalPlan serde is not yet implemented for Unnest",
)),
- LogicalPlan::CreateMemoryTable(_) => Err(proto_error(
+ LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(_)) => Err(proto_error(
"LogicalPlan serde is not yet implemented for CreateMemoryTable",
)),
LogicalPlan::DropTable(_) => Err(proto_error(
diff --git a/datafusion/sql/src/statement.rs b/datafusion/sql/src/statement.rs
index bec9888a8c..a84118d042 100644
--- a/datafusion/sql/src/statement.rs
+++ b/datafusion/sql/src/statement.rs
@@ -30,6 +30,7 @@ use datafusion_common::{
};
use datafusion_expr::expr_rewriter::normalize_col_with_schemas_and_ambiguity_check;
use datafusion_expr::logical_plan::builder::project;
+use datafusion_expr::logical_plan::DdlStatement;
use datafusion_expr::utils::expr_to_columns;
use datafusion_expr::{
cast, col, Analyze, CreateCatalog, CreateCatalogSchema,
@@ -159,13 +160,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
plan
};
- Ok(LogicalPlan::CreateMemoryTable(CreateMemoryTable {
- name: self.object_name_to_table_reference(name)?,
- primary_key,
- input: Arc::new(plan),
- if_not_exists,
- or_replace,
- }))
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(
+ CreateMemoryTable {
+ name: self.object_name_to_table_reference(name)?,
+ primary_key,
+ input: Arc::new(plan),
+ if_not_exists,
+ or_replace,
+ },
+ )))
}
None => {
@@ -178,13 +181,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
};
let plan = LogicalPlan::EmptyRelation(plan);
- Ok(LogicalPlan::CreateMemoryTable(CreateMemoryTable {
- name: self.object_name_to_table_reference(name)?,
- primary_key,
- input: Arc::new(plan),
- if_not_exists,
- or_replace,
- }))
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(
+ CreateMemoryTable {
+ name: self.object_name_to_table_reference(name)?,
+ primary_key,
+ input: Arc::new(plan),
+ if_not_exists,
+ or_replace,
+ },
+ )))
}
},
@@ -199,12 +204,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let mut plan = self.query_to_plan(*query, &mut PlannerContext::new())?;
plan = self.apply_expr_alias(plan, columns)?;
- Ok(LogicalPlan::CreateView(CreateView {
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateView(CreateView {
name: self.object_name_to_table_reference(name)?,
input: Arc::new(plan),
or_replace,
definition: sql,
- }))
+ })))
}
Statement::ShowCreate { obj_type, obj_name } => match obj_type {
ShowCreateObject::Table => self.show_create_table_to_plan(obj_name),
@@ -215,20 +220,24 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Statement::CreateSchema {
schema_name,
if_not_exists,
- } => Ok(LogicalPlan::CreateCatalogSchema(CreateCatalogSchema {
- schema_name: get_schema_name(&schema_name),
- if_not_exists,
- schema: Arc::new(DFSchema::empty()),
- })),
+ } => Ok(LogicalPlan::Ddl(DdlStatement::CreateCatalogSchema(
+ CreateCatalogSchema {
+ schema_name: get_schema_name(&schema_name),
+ if_not_exists,
+ schema: Arc::new(DFSchema::empty()),
+ },
+ ))),
Statement::CreateDatabase {
db_name,
if_not_exists,
..
- } => Ok(LogicalPlan::CreateCatalog(CreateCatalog {
- catalog_name: object_name_to_string(&db_name),
- if_not_exists,
- schema: Arc::new(DFSchema::empty()),
- })),
+ } => Ok(LogicalPlan::Ddl(DdlStatement::CreateCatalog(
+ CreateCatalog {
+ catalog_name: object_name_to_string(&db_name),
+ if_not_exists,
+ schema: Arc::new(DFSchema::empty()),
+ },
+ ))),
Statement::Drop {
object_type,
if_exists,
@@ -588,20 +597,22 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// External tables do not support schemas at the moment, so the name is just a table name
let name = OwnedTableReference::bare(name);
- Ok(LogicalPlan::CreateExternalTable(PlanCreateExternalTable {
- schema: df_schema,
- name,
- location,
- file_type,
- has_header,
- delimiter,
- table_partition_cols,
- if_not_exists,
- definition,
- file_compression_type,
- order_exprs: ordered_exprs,
- options,
- }))
+ Ok(LogicalPlan::Ddl(DdlStatement::CreateExternalTable(
+ PlanCreateExternalTable {
+ schema: df_schema,
+ name,
+ location,
+ file_type,
+ has_header,
+ delimiter,
+ table_partition_cols,
+ if_not_exists,
+ definition,
+ file_compression_type,
+ order_exprs: ordered_exprs,
+ options,
+ },
+ )))
}
/// Generate a plan for EXPLAIN ... that will print out a plan