@@ -370,21 +370,29 @@ impl SparkPhysicalExprAdapter {
370370 . data ( )
371371 }
372372
373- /// Replace CastColumnExpr (DataFusion's cast) with Spark's Cast expression.
373+ /// Replace CastExpr (DataFusion's cast) with Spark's Cast expression.
374374 fn replace_with_spark_cast (
375375 & self ,
376376 expr : Arc < dyn PhysicalExpr > ,
377377 ) -> DataFusionResult < Transformed < Arc < dyn PhysicalExpr > > > {
378- // Check for CastColumnExpr and replace with spark_expr::Cast
379- // CastColumnExpr is in datafusion_physical_expr::expressions
378+ // Check for CastExpr and replace with spark_expr::Cast
380379 if let Some ( cast) = expr
381380 . as_any ( )
382- . downcast_ref :: < datafusion:: physical_expr:: expressions:: CastColumnExpr > ( )
381+ . downcast_ref :: < datafusion:: physical_expr:: expressions:: CastExpr > ( )
383382 {
384383 let child = Arc :: clone ( cast. expr ( ) ) ;
385- let physical_type = cast. input_field ( ) . data_type ( ) ;
386384 let target_type = cast. target_field ( ) . data_type ( ) ;
387385
386+ // Derive input field from the child Column expression and the physical schema
387+ let input_field = if let Some ( col) = child. as_any ( ) . downcast_ref :: < Column > ( ) {
388+ Arc :: new ( self . physical_file_schema . field ( col. index ( ) ) . clone ( ) )
389+ } else {
390+ // Fallback: synthesize a field from the target field name and child data type
391+ let child_type = cast. expr ( ) . data_type ( & self . physical_file_schema ) ?;
392+ Arc :: new ( Field :: new ( cast. target_field ( ) . name ( ) , child_type, true ) )
393+ } ;
394+ let physical_type = input_field. data_type ( ) ;
395+
388396 // For complex nested types (Struct, List, Map), Timestamp timezone
389397 // mismatches, and Timestamp→Int64 (nanosAsLong), use CometCastColumnExpr
390398 // with spark_parquet_convert which handles field-name-based selection,
@@ -413,7 +421,7 @@ impl SparkPhysicalExprAdapter {
413421 let comet_cast: Arc < dyn PhysicalExpr > = Arc :: new (
414422 CometCastColumnExpr :: new (
415423 child,
416- Arc :: clone ( cast . input_field ( ) ) ,
424+ input_field,
417425 Arc :: clone ( cast. target_field ( ) ) ,
418426 None ,
419427 )
0 commit comments