Skip to content

Apache DataFusion SQL Query Engine

Split function (#4958)

Brent Gardner2fdc7b836741
diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rs
index a3e453a2a5..ead76c8e09 100644
--- a/datafusion/core/src/execution/context.rs
+++ b/datafusion/core/src/execution/context.rs
@@ -1636,22 +1636,34 @@ impl SessionState {
self
}
- /// Creates a [`LogicalPlan`] from the provided SQL string
- ///
- /// See [`SessionContext::sql`] for a higher-level interface that also handles DDL
- pub async fn create_logical_plan(&self, sql: &str) -> Result<LogicalPlan> {
- use crate::catalog::information_schema::INFORMATION_SCHEMA_TABLES;
- use datafusion_sql::parser::Statement as DFStatement;
- use sqlparser::ast::*;
- use std::collections::hash_map::Entry;
-
+ /// Convert a sql string into an ast Statement
+ pub fn sql_to_statement(
+ &self,
+ sql: &str,
+ ) -> Result<datafusion_sql::parser::Statement> {
let mut statements = DFParser::parse_sql(sql)?;
- if statements.len() != 1 {
+ if statements.len() > 1 {
return Err(DataFusionError::NotImplemented(
"The context currently only supports a single SQL statement".to_string(),
));
}
- let statement = statements.pop_front().unwrap();
+ let statement = statements.pop_front().ok_or_else(|| {
+ DataFusionError::NotImplemented(
+ "The context requires a statement!".to_string(),
+ )
+ })?;
+ Ok(statement)
+ }
+
+ /// Convert an ast Statement into a LogicalPlan
+ pub async fn statement_to_plan(
+ &self,
+ statement: datafusion_sql::parser::Statement,
+ ) -> Result<LogicalPlan> {
+ use crate::catalog::information_schema::INFORMATION_SCHEMA_TABLES;
+ use datafusion_sql::parser::Statement as DFStatement;
+ use sqlparser::ast::*;
+ use std::collections::hash_map::Entry;
// Getting `TableProviders` is async but planing is not -- thus pre-fetch
// table providers for all relations referenced in this query
@@ -1729,6 +1741,15 @@ impl SessionState {
query.statement_to_plan(statement)
}
+ /// Creates a [`LogicalPlan`] from the provided SQL string
+ ///
+ /// See [`SessionContext::sql`] for a higher-level interface that also handles DDL
+ pub async fn create_logical_plan(&self, sql: &str) -> Result<LogicalPlan> {
+ let statement = self.sql_to_statement(sql)?;
+ let plan = self.statement_to_plan(statement).await?;
+ Ok(plan)
+ }
+
/// Optimizes the logical plan by applying optimizer rules.
pub fn optimize(&self, plan: &LogicalPlan) -> Result<LogicalPlan> {
if let LogicalPlan::Explain(e) = plan {