@@ -32,6 +32,7 @@ use arrow::datatypes::{DataType, Field, FieldRef, Schema, UInt32Type, UnionMode}
3232use arrow:: error:: ArrowError ;
3333use arrow_schema:: extension:: { EXTENSION_TYPE_METADATA_KEY , EXTENSION_TYPE_NAME_KEY } ;
3434use datafusion_common:: cast:: as_boolean_array;
35+ use datafusion_common:: metadata:: FieldMetadata ;
3536use datafusion_common:: {
3637 DataFusionError , Result , ScalarValue , assert_or_internal_err, exec_err,
3738 internal_datafusion_err, internal_err,
@@ -278,6 +279,8 @@ pub struct CaseExpr {
278279 body : CaseBody ,
279280 /// Evaluation method to use
280281 eval_method : EvalMethod ,
282+ /// Logical result type and metadata inferred before physical lowering.
283+ logical_result_field : Option < ( DataType , FieldMetadata ) > ,
281284}
282285
283286// eval_method is functionally derived from body, so excluding it from
@@ -286,12 +289,13 @@ pub struct CaseExpr {
286289impl Hash for CaseExpr {
287290 fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
288291 self . body . hash ( state) ;
292+ self . logical_result_field . hash ( state) ;
289293 }
290294}
291295
292296impl PartialEq for CaseExpr {
293297 fn eq ( & self , other : & Self ) -> bool {
294- self . body == other. body
298+ self . body == other. body && self . logical_result_field == other . logical_result_field
295299 }
296300}
297301
@@ -678,7 +682,20 @@ impl CaseExpr {
678682
679683 let eval_method = Self :: find_best_eval_method ( & body) ?;
680684
681- Ok ( Self { body, eval_method } )
685+ Ok ( Self {
686+ body,
687+ eval_method,
688+ logical_result_field : None ,
689+ } )
690+ }
691+
692+ /// Preserve the logical CASE result metadata across physical lowering.
693+ pub fn with_logical_result_field ( mut self , field : & FieldRef ) -> Self {
694+ self . logical_result_field = Some ( (
695+ field. data_type ( ) . clone ( ) ,
696+ FieldMetadata :: from ( field. metadata ( ) . clone ( ) ) ,
697+ ) ) ;
698+ self
682699 }
683700
684701 fn find_best_eval_method ( body : & CaseBody ) -> Result < EvalMethod > {
@@ -1439,6 +1456,17 @@ impl PhysicalExpr for CaseExpr {
14391456 fn return_field ( & self , input_schema : & Schema ) -> Result < FieldRef > {
14401457 let data_type = self . data_type ( input_schema) ?;
14411458 let nullable = self . nullable ( input_schema) ?;
1459+ if let Some ( ( logical_type, logical_metadata) ) = & self . logical_result_field {
1460+ let metadata = if logical_type == & data_type {
1461+ logical_metadata. to_hashmap ( )
1462+ } else {
1463+ Metadata :: new ( )
1464+ } ;
1465+ return Ok ( Arc :: new (
1466+ Field :: new ( format ! ( "{self}" ) , data_type, nullable)
1467+ . with_metadata ( metadata) ,
1468+ ) ) ;
1469+ }
14421470 if let Some ( ( _, first_result) ) = self . body . when_then_expr . first ( )
14431471 && ( first_result. is :: < Column > ( ) || first_result. is :: < Literal > ( ) )
14441472 {
@@ -1597,11 +1625,15 @@ impl PhysicalExpr for CaseExpr {
15971625 ) ,
15981626 ( false , false ) => ( None , & children[ 0 ..children. len ( ) ] , None ) ,
15991627 } ;
1600- Ok ( Arc :: new ( CaseExpr :: try_new (
1628+ let mut rebuilt = CaseExpr :: try_new (
16011629 expr. cloned ( ) ,
16021630 when_then_expr. iter ( ) . cloned ( ) . tuples ( ) . collect ( ) ,
16031631 else_expr. cloned ( ) ,
1604- ) ?) )
1632+ ) ?;
1633+ rebuilt
1634+ . logical_result_field
1635+ . clone_from ( & self . logical_result_field ) ;
1636+ Ok ( Arc :: new ( rebuilt) )
16051637 }
16061638 }
16071639
@@ -1944,6 +1976,32 @@ mod tests {
19441976 Ok ( ( ) )
19451977 }
19461978
1979+ #[ test]
1980+ fn case_logical_field_survives_rewriting_children ( ) -> Result < ( ) > {
1981+ let metadata = HashMap :: from ( [ ( "source" . to_string ( ) , "physical" . to_string ( ) ) ] ) ;
1982+ let schema = Schema :: new ( vec ! [
1983+ Field :: new( "a" , DataType :: Binary , false ) . with_metadata( metadata. clone( ) ) ,
1984+ Field :: new( "b" , DataType :: Binary , false ) . with_metadata( metadata) ,
1985+ ] ) ;
1986+ let expr: Arc < dyn PhysicalExpr > = Arc :: new (
1987+ CaseExpr :: try_new (
1988+ None ,
1989+ vec ! [ ( lit( true ) , col( "a" , & schema) ?) ] ,
1990+ Some ( col ( "b" , & schema) ?) ,
1991+ ) ?
1992+ . with_logical_result_field ( & Arc :: new ( Field :: new (
1993+ "logical_case" ,
1994+ DataType :: Binary ,
1995+ true ,
1996+ ) ) ) ,
1997+ ) ;
1998+ assert ! ( expr. return_field( & schema) ?. metadata( ) . is_empty( ) ) ;
1999+ let children = expr. children ( ) . into_iter ( ) . cloned ( ) . collect ( ) ;
2000+ let rewritten = expr. with_new_children ( children) ?;
2001+ assert ! ( rewritten. return_field( & schema) ?. metadata( ) . is_empty( ) ) ;
2002+ Ok ( ( ) )
2003+ }
2004+
19472005 #[ test]
19482006 fn case_not_does_not_inherit_input_metadata ( ) -> Result < ( ) > {
19492007 let metadata = HashMap :: from ( [ ( "logical" . to_string ( ) , "boolean" . to_string ( ) ) ] ) ;
0 commit comments