cyberbeam524 commented on code in PR #21714:
URL: https://github.com/apache/datafusion/pull/21714#discussion_r3172662467


##########
datafusion/sql/src/statement.rs:
##########
@@ -530,11 +531,26 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
                                 .iter()
                                 .zip(input_fields)
                                 .map(|(field, input_field)| {
-                                    cast(
-                                        col(input_field.name()),
-                                        field.data_type().clone(),
-                                    )
-                                    .alias(field.name())
+                                    let target_field = Arc::new(
+                                        Field::new(
+                                            field.name(),
+                                            field.data_type().clone(),
+                                            field.is_nullable(),
+                                        )
+                                        
.with_metadata(field.metadata().clone()),
+                                    );
+                                    let metadata =
+                                        
FieldMetadata::new_from_field(field.as_ref());
+                                    let alias_metadata = if 
metadata.is_empty() {
+                                        None
+                                    } else {
+                                        Some(metadata)
+                                    };
+                                    Expr::Cast(Cast::new_from_field(
+                                        Box::new(col(input_field.name())),
+                                        target_field,

Review Comment:
   You're right. Reusing the existing field is much cleaner than recreating it 
as target_field. I've refactored this block to use Arc::clone(field) as you 
suggested.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to