paleolimbot commented on code in PR #23169:
URL: https://github.com/apache/datafusion/pull/23169#discussion_r3507387196


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -69,18 +69,43 @@ 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.
+///
+/// Metadata handling:
+/// - Source field metadata is propagated by default
+/// - Extension type metadata keys (`ARROW:extension:name` and 
`ARROW:extension:metadata`)
+///   are taken from the target field, overwriting any extension metadata from 
the source.
+///   This ensures casting does not incorrectly propagate extension type 
identity.
+///
 /// 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> {
+    use arrow_schema::extension::{EXTENSION_TYPE_METADATA_KEY, 
EXTENSION_TYPE_NAME_KEY};
+
+    // Start with source metadata
+    let mut metadata = source_field.metadata().clone();

Review Comment:
   `Alias` has been able to do this type of metadata reassignment for some 
time, and I am not sure it is a good use of the `Cast` to have it messing with 
metadata (the confusion that led to the current situation is that the cast 
operator is being used to manipulate metadata, rather than transform a "data 
type" in the data-type-or-extension-type sense). I'm happy to tweak this to 
make it consistent...all I personally need is for the target field extension 
information not to be dropped (so I can insert an optimizer rule that 
transforms it into a function call).



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