@@ -33,6 +33,7 @@ use tokio::runtime::Handle;
3333
3434use crate :: config:: FFI_ConfigOptions ;
3535use crate :: execution:: FFI_TaskContext ;
36+ use crate :: physical_expr:: FFI_PhysicalExpr ;
3637use crate :: physical_expr:: metrics:: FFI_MetricsSet ;
3738use crate :: plan_properties:: FFI_PlanProperties ;
3839use crate :: record_batch_stream:: FFI_RecordBatchStream ;
@@ -50,6 +51,10 @@ pub struct FFI_ExecutionPlan {
5051 /// Return a vector of children plans
5152 pub children : unsafe extern "C" fn ( plan : & Self ) -> SVec < FFI_ExecutionPlan > ,
5253
54+ /// Return the dynamic expressions produced by this plan node.
55+ pub dynamic_expressions_produced :
56+ unsafe extern "C" fn ( plan : & Self ) -> SVec < FFI_PhysicalExpr > ,
57+
5358 pub with_new_children :
5459 unsafe extern "C" fn ( plan : & Self , children : SVec < Self > ) -> FFI_Result < Self > ,
5560
@@ -138,6 +143,16 @@ unsafe extern "C" fn children_fn_wrapper(
138143 . collect ( )
139144}
140145
146+ unsafe extern "C" fn dynamic_expressions_produced_fn_wrapper (
147+ plan : & FFI_ExecutionPlan ,
148+ ) -> SVec < FFI_PhysicalExpr > {
149+ plan. inner ( )
150+ . dynamic_expressions_produced ( )
151+ . into_iter ( )
152+ . map ( FFI_PhysicalExpr :: from)
153+ . collect ( )
154+ }
155+
141156unsafe extern "C" fn with_new_children_fn_wrapper (
142157 plan : & FFI_ExecutionPlan ,
143158 children : SVec < FFI_ExecutionPlan > ,
@@ -306,6 +321,7 @@ impl FFI_ExecutionPlan {
306321 Self {
307322 properties : properties_fn_wrapper,
308323 children : children_fn_wrapper,
324+ dynamic_expressions_produced : dynamic_expressions_produced_fn_wrapper,
309325 with_new_children : with_new_children_fn_wrapper,
310326 name : name_fn_wrapper,
311327 execute : execute_fn_wrapper,
@@ -442,6 +458,15 @@ impl ExecutionPlan for ForeignExecutionPlan {
442458 }
443459 }
444460
461+ fn dynamic_expressions_produced (
462+ & self ,
463+ ) -> Vec < Arc < dyn datafusion_physical_plan:: PhysicalExpr > > {
464+ unsafe { ( self . plan . dynamic_expressions_produced ) ( & self . plan ) }
465+ . iter ( )
466+ . map ( <Arc < dyn datafusion_physical_plan:: PhysicalExpr > >:: from)
467+ . collect ( )
468+ }
469+
445470 fn repartitioned (
446471 & self ,
447472 target_partitions : usize ,
@@ -474,15 +499,17 @@ impl ExecutionPlan for ForeignExecutionPlan {
474499
475500#[ cfg( any( test, feature = "integration-tests" ) ) ]
476501pub mod tests {
477- use datafusion_physical_plan :: Partitioning ;
502+ use datafusion_physical_expr :: expressions :: { DynamicFilterPhysicalExpr , lit } ;
478503 use datafusion_physical_plan:: execution_plan:: { Boundedness , EmissionType } ;
504+ use datafusion_physical_plan:: { Partitioning , PhysicalExpr } ;
479505
480506 use super :: * ;
481507
482508 #[ derive( Debug ) ]
483509 pub struct EmptyExec {
484510 props : Arc < PlanProperties > ,
485511 children : Vec < Arc < dyn ExecutionPlan > > ,
512+ dynamic_expressions : Vec < Arc < dyn PhysicalExpr > > ,
486513 metrics : Option < MetricsSet > ,
487514 statistics : Option < Statistics > ,
488515 }
@@ -497,6 +524,7 @@ pub mod tests {
497524 Boundedness :: Bounded ,
498525 ) ) ,
499526 children : Vec :: default ( ) ,
527+ dynamic_expressions : Vec :: default ( ) ,
500528 metrics : None ,
501529 statistics : None ,
502530 }
@@ -511,6 +539,14 @@ pub mod tests {
511539 self . statistics = Some ( statistics) ;
512540 self
513541 }
542+
543+ pub fn with_dynamic_expressions (
544+ mut self ,
545+ dynamic_expressions : Vec < Arc < dyn PhysicalExpr > > ,
546+ ) -> Self {
547+ self . dynamic_expressions = dynamic_expressions;
548+ self
549+ }
514550 }
515551
516552 impl DisplayAs for EmptyExec {
@@ -543,6 +579,7 @@ pub mod tests {
543579 Ok ( Arc :: new ( EmptyExec {
544580 props : Arc :: clone ( & self . props ) ,
545581 children,
582+ dynamic_expressions : self . dynamic_expressions . clone ( ) ,
546583 metrics : self . metrics . clone ( ) ,
547584 statistics : self . statistics . clone ( ) ,
548585 } ) )
@@ -556,6 +593,10 @@ pub mod tests {
556593 unimplemented ! ( )
557594 }
558595
596+ fn dynamic_expressions_produced ( & self ) -> Vec < Arc < dyn PhysicalExpr > > {
597+ self . dynamic_expressions . iter ( ) . map ( Arc :: clone) . collect ( )
598+ }
599+
559600 fn metrics ( & self ) -> Option < MetricsSet > {
560601 self . metrics . clone ( )
561602 }
@@ -571,6 +612,10 @@ pub mod tests {
571612 }
572613 }
573614
615+ pub ( crate ) fn create_dynamic_filter ( ) -> Arc < DynamicFilterPhysicalExpr > {
616+ Arc :: new ( DynamicFilterPhysicalExpr :: new ( vec ! [ ] , lit ( true ) ) )
617+ }
618+
574619 #[ test]
575620 fn test_round_trip_ffi_execution_plan ( ) -> Result < ( ) > {
576621 let schema = Arc :: new ( arrow:: datatypes:: Schema :: new ( vec ! [
@@ -600,6 +645,32 @@ pub mod tests {
600645 Ok ( ( ) )
601646 }
602647
648+ #[ test]
649+ fn test_ffi_execution_plan_dynamic_expressions_produced ( ) -> Result < ( ) > {
650+ let schema = Arc :: new ( arrow:: datatypes:: Schema :: empty ( ) ) ;
651+ let dynamic_filter = create_dynamic_filter ( ) ;
652+ let expected_id = dynamic_filter
653+ . expression_id ( )
654+ . expect ( "dynamic filters always have an expression ID" ) ;
655+ let expression: Arc < dyn PhysicalExpr > = Arc :: clone ( & dynamic_filter) as _ ;
656+ let original_plan =
657+ Arc :: new ( EmptyExec :: new ( schema) . with_dynamic_expressions ( vec ! [ expression] ) ) ;
658+
659+ let mut ffi_plan = FFI_ExecutionPlan :: new ( original_plan, None ) ;
660+ ffi_plan. library_marker_id = crate :: mock_foreign_marker_id;
661+ let foreign_plan: Arc < dyn ExecutionPlan > = ( & ffi_plan) . try_into ( ) ?;
662+ foreign_plan. check_invariants (
663+ datafusion_physical_plan:: execution_plan:: InvariantLevel :: Always ,
664+ ) ?;
665+
666+ let produced = foreign_plan. dynamic_expressions_produced ( ) ;
667+ assert_eq ! ( produced. len( ) , 1 ) ;
668+ assert_eq ! ( produced[ 0 ] . expression_id( ) , Some ( expected_id) ) ;
669+ drop ( foreign_plan) ;
670+ assert_eq ! ( produced[ 0 ] . expression_id( ) , Some ( expected_id) ) ;
671+ Ok ( ( ) )
672+ }
673+
603674 #[ test]
604675 fn test_ffi_execution_plan_children ( ) -> Result < ( ) > {
605676 let schema = Arc :: new ( arrow:: datatypes:: Schema :: new ( vec ! [
0 commit comments