@@ -32,7 +32,6 @@ use dyn_clone::DynClone;
3232use serde:: Deserializer ;
3333use serde:: Serializer ;
3434use serde:: de:: Error as DeError ;
35- use serde:: ser:: Error as SerError ;
3635use serde_json:: Value as JsonValue ;
3736
3837use crate :: physical_plans:: format:: FormatContext ;
@@ -71,7 +70,23 @@ pub trait DeriveHandle: Send + Sync + 'static {
7170 ) -> std:: result:: Result < PhysicalPlan , Vec < PhysicalPlan > > ;
7271}
7372
74- pub ( crate ) trait IPhysicalPlan : DynClone + Debug + Send + Sync + ' static {
73+ pub ( crate ) trait PhysicalPlanSerdeSerialization {
74+ fn to_physical_plan_serde_serialize ( & self ) -> PhysicalPlanSerdeSerialize < ' _ > ;
75+ }
76+
77+ pub ( crate ) trait PhysicalPlanSerdeDeserialization {
78+ fn from_physical_plan_deserialize ( v : PhysicalPlanDeserialize ) -> Self ;
79+ }
80+
81+ pub ( crate ) trait IPhysicalPlan :
82+ PhysicalPlanSerdeSerialization
83+ + PhysicalPlanSerdeSerialization
84+ + DynClone
85+ + Debug
86+ + Send
87+ + Sync
88+ + ' static
89+ {
7590 fn as_any ( & self ) -> & dyn Any ;
7691
7792 fn get_meta ( & self ) -> & PhysicalPlanMeta ;
@@ -271,15 +286,32 @@ impl<T: IPhysicalPlan> PhysicalPlanCast for T {
271286
272287macro_rules! define_physical_plan_serde {
273288 ( $( $( #[ $meta: meta] ) ? $variant: ident => $path: path ) ,+ $( , ) ? ) => {
274- #[ derive( Clone , Debug , serde:: Serialize , serde :: Deserialize ) ]
275- /// static dispatch replacement for typetag-based dynamic dispatch, performance improvement via reduced stack depth
276- pub ( crate ) enum PhysicalPlanSerde {
289+ #[ derive( Clone , Debug , serde:: Deserialize ) ]
290+ /// owned enum for deserialization; serialization uses PhysicalPlanSerdeRef to avoid cloning
291+ pub ( crate ) enum PhysicalPlanDeserialize {
277292 $( $( #[ $meta] ) ? $variant( $path) , ) +
278293 }
279294
280- $( $( #[ $meta] ) ? impl From <$path> for PhysicalPlanSerde {
295+ #[ derive( Debug , serde:: Serialize ) ]
296+ pub ( crate ) enum PhysicalPlanSerdeSerialize <' a> {
297+ $( $( #[ $meta] ) ? $variant( & ' a $path) , ) +
298+ }
299+
300+ $( $( #[ $meta] ) ? impl From <$path> for PhysicalPlanDeserialize {
281301 fn from( v: $path) -> Self {
282- PhysicalPlanSerde :: $variant( v)
302+ PhysicalPlanDeserialize :: $variant( v)
303+ }
304+ } ) +
305+
306+ $( $( #[ $meta] ) ? impl <' a> From <& ' a $path> for PhysicalPlanSerdeSerialize <' a> {
307+ fn from( v: & ' a $path) -> Self {
308+ PhysicalPlanSerdeSerialize :: $variant( v)
309+ }
310+ } ) +
311+
312+ $( $( #[ $meta] ) ? impl PhysicalPlanSerdeSerialization for $path {
313+ fn to_physical_plan_serde_serialize( & self ) -> PhysicalPlanSerdeSerialize <' _> {
314+ PhysicalPlanSerdeSerialize :: from( self )
283315 }
284316 } ) +
285317 } ;
@@ -289,7 +321,19 @@ include!(concat!(env!("OUT_DIR"), "/physical_plan_impls.rs"));
289321
290322include ! ( concat!( env!( "OUT_DIR" ) , "/physical_plan_dispatch.rs" ) ) ;
291323
292- impl IPhysicalPlan for PhysicalPlanSerde {
324+ impl PhysicalPlanSerdeSerialization for PhysicalPlanDeserialize {
325+ fn to_physical_plan_serde_serialize ( & self ) -> PhysicalPlanSerdeSerialize < ' _ > {
326+ dispatch_plan_ref ! ( self , v => PhysicalPlanSerdeSerialize :: from( v) )
327+ }
328+ }
329+
330+ impl PhysicalPlanSerdeDeserialization for PhysicalPlan {
331+ fn from_physical_plan_deserialize ( v : PhysicalPlanDeserialize ) -> Self {
332+ PhysicalPlan { inner : Box :: new ( v) }
333+ }
334+ }
335+
336+ impl IPhysicalPlan for PhysicalPlanDeserialize {
293337 fn as_any ( & self ) -> & dyn Any {
294338 dispatch_plan_ref ! ( self , v => v. as_any( ) )
295339 }
@@ -404,13 +448,9 @@ impl Debug for PhysicalPlan {
404448impl serde:: Serialize for PhysicalPlan {
405449 #[ recursive:: recursive]
406450 fn serialize < S : Serializer > ( & self , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error > {
407- if let Some ( v) = self . inner . as_any ( ) . downcast_ref :: < PhysicalPlanSerde > ( ) {
408- v. serialize ( serializer)
409- } else {
410- Err ( S :: Error :: custom (
411- "serialize PhysicalPlan: unexpected plan type" ,
412- ) )
413- }
451+ self . inner
452+ . to_physical_plan_serde_serialize ( )
453+ . serialize ( serializer)
414454 }
415455}
416456
@@ -419,20 +459,19 @@ impl<'de> serde::Deserialize<'de> for PhysicalPlan {
419459 fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> std:: result:: Result < Self , D :: Error > {
420460 // Deserialize to JSON first to avoid backtracking failures in streaming deserializers.
421461 let value = JsonValue :: deserialize ( deserializer) ?;
422- let inner: PhysicalPlanSerde = serde_json:: from_value ( value) . map_err ( DeError :: custom) ?;
462+ let inner: PhysicalPlanDeserialize =
463+ serde_json:: from_value ( value) . map_err ( DeError :: custom) ?;
423464
424- Ok ( PhysicalPlan {
425- inner : Box :: new ( inner) ,
426- } )
465+ Ok ( PhysicalPlan :: from_physical_plan_deserialize ( inner) )
427466 }
428467}
429468
430469impl PhysicalPlan {
431470 #[ allow( private_bounds) ]
432471 pub fn new < T > ( inner : T ) -> PhysicalPlan
433- where PhysicalPlanSerde : From < T > {
472+ where PhysicalPlanDeserialize : From < T > {
434473 PhysicalPlan {
435- inner : Box :: < PhysicalPlanSerde > :: new ( inner. into ( ) ) ,
474+ inner : Box :: < PhysicalPlanDeserialize > :: new ( inner. into ( ) ) ,
436475 }
437476 }
438477
0 commit comments