avantgardnerio commented on code in PR #4958:
URL: https://github.com/apache/arrow-datafusion/pull/4958#discussion_r1073789901
##########
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;
Review Comment:
Agreed? They just "came along for the ride" following the "do no harm"
principle. That's not a great excuse though.
I think they are in the function because this file was becoming very long,
and I suspect collisions with `parser::Statement` and `ast::Statement` and
probably others was becoming untenable so at some point someone just said "I'll
keep them local to this function as to avoid having to deal with all that".
I suspect the ultimate solution would be one or more of:
1. Split this file up into multiple more reasonably sized ones
2. Rename things such that it is easier to avoid collisions, or keep
namespace names short and fully qualify them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]