alamb commented on code in PR #4194:
URL: https://github.com/apache/arrow-datafusion/pull/4194#discussion_r1023043918


##########
datafusion/sql/src/planner.rs:
##########
@@ -180,12 +180,36 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                 if_not_exists,
                 or_replace,
                 ..
-            } if columns.is_empty()
-                && constraints.is_empty()
+            } if constraints.is_empty()
                 && table_properties.is_empty()
                 && with_options.is_empty() =>
             {
-                let plan = self.query_to_plan(*query, &mut HashMap::new())?;
+                let plan = self.query_to_plan(*query.clone(), &mut 
HashMap::new())?;
+                let input_schema = plan.schema();
+
+                let plan = if !columns.is_empty() {
+                    match *query.body {
+                        SetExpr::Values(_) => {
+                            let schema = 
self.build_schema(columns)?.to_dfschema_ref()?;
+                            if schema.fields().len() != 
input_schema.fields().len() {
+                                return Err(DataFusionError::Plan("Mismatch 
between schema and batches".to_string()))

Review Comment:
   I suggest adding test for this situation (incorrect length)
   
   



##########
datafusion/sql/src/planner.rs:
##########
@@ -180,12 +180,36 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                 if_not_exists,
                 or_replace,
                 ..
-            } if columns.is_empty()
-                && constraints.is_empty()
+            } if constraints.is_empty()
                 && table_properties.is_empty()
                 && with_options.is_empty() =>
             {
-                let plan = self.query_to_plan(*query, &mut HashMap::new())?;
+                let plan = self.query_to_plan(*query.clone(), &mut 
HashMap::new())?;
+                let input_schema = plan.schema();
+
+                let plan = if !columns.is_empty() {
+                    match *query.body {
+                        SetExpr::Values(_) => {
+                            let schema = 
self.build_schema(columns)?.to_dfschema_ref()?;
+                            if schema.fields().len() != 
input_schema.fields().len() {
+                                return Err(DataFusionError::Plan("Mismatch 
between schema and batches".to_string()))
+                            }
+                            let input_fields = input_schema.fields();
+                            let project_exprs = 
schema.fields().iter().zip(input_fields).map(|(field, input_field)| {
+                                cast(col(input_field.name()), 
field.data_type().clone()).alias(field.name())
+                            }).collect::<Vec<_>>();
+                            LogicalPlanBuilder::from(plan.clone())
+                                .project(project_exprs)?
+                                .build()?
+                        },
+                        _ => return Err(DataFusionError::Plan(
+                            "You can only specify schema when create table 
with a `values` statement"
+                                .to_string()
+                        ))

Review Comment:
   I wonder why not support queries as well? Why only `VALUES` -- the code you 
have written should work for a `CREATE TABLE AS SELECT` as well
   
   



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