gene-bordegaray commented on code in PR #24670:
URL: https://github.com/apache/datafusion/pull/24670#discussion_r3865468014


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -71,18 +71,31 @@ pub trait ExprSchemable {
     -> Result<(DataType, bool)>;
 }
 
-/// Derives the output field for a cast expression from the source field.
+/// Derives the output field for a cast expression from the source and target
+/// fields. Type-only casts preserve source metadata, while an explicit target
+/// field supplies its own metadata.
+///
 /// For `TryCast`, `force_nullable` is `true` since a failed cast returns NULL.
 fn cast_output_field(
     source_field: &FieldRef,
-    target_type: &DataType,
+    target_field: &FieldRef,
     force_nullable: bool,
 ) -> Arc<Field> {
+    // `Cast::new` and `TryCast::new` use this field when only a target
+    // type is known. In that case, retain the source field's metadata.
+    let type_only_target = target_field.name().is_empty()
+        && target_field.is_nullable()
+        && target_field.metadata().is_empty();
+    let metadata = if type_only_target {

Review Comment:
   Ya I thought this then AI actually ciaght this and I validated.
   
   There can be the case where we are giving an target field with empty 
metadata and target type of a `FiexedSizeBinary`, then the source say has 
metadata that is marking something as a arrow UUID. In this case you would 
expect the result to be corrctly casted to `FixedSizeBinary` and empty metadata.
   
   But what would happen is the target metadata is empty so then it would try 
to use the source. But the source metadata is saying to treated the 
`FixedSizeBinary` as a arrow UUID thus wouldnt cast correctly.
   
   Now this check prevents that by checking if its a type only target.
   
   There may be a clarner way to represent this though. I will return with 
thoughts



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