tustvold commented on code in PR #4607:
URL: https://github.com/apache/arrow-datafusion/pull/4607#discussion_r1060990226


##########
datafusion/core/src/execution/context.rs:
##########
@@ -1643,6 +1621,72 @@ 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::{visit_relations, Ident, ObjectName};
+        use std::collections::hash_map::Entry;
+
+        let mut statements = DFParser::parse_sql(sql)?;
+        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 mut relations = hashbrown::HashSet::with_capacity(10);
+
+        match &statement {
+            DFStatement::Statement(s) => {
+                visit_relations(s.as_ref(), |relation| {
+                    relations.get_or_insert_with(relation, |_| 
relation.clone());
+                    ControlFlow::<(), ()>::Continue(())
+                });
+            }
+            DFStatement::CreateExternalTable(table) => {
+                
relations.insert(ObjectName(vec![Ident::from(table.name.as_str())]));
+            }
+            DFStatement::DescribeTable(table) => {
+                relations
+                    .get_or_insert_with(&table.table_name, |_| 
table.table_name.clone());
+            }
+        }
+
+        // Always include information_schema if available
+        if self.config.information_schema() {

Review Comment:
   #4606 means there isn't a cost to doing this



-- 
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]

Reply via email to