@@ -428,6 +428,100 @@ impl ExecutionPlan for SortPreservingMergeExec {
428428 . with_fetch ( self . fetch ( ) ) ,
429429 ) ) )
430430 }
431+
432+ #[ cfg( feature = "proto" ) ]
433+ fn try_to_proto (
434+ & self ,
435+ ctx : & crate :: proto:: ExecutionPlanEncodeCtx < ' _ > ,
436+ ) -> Result < Option < datafusion_proto_models:: protobuf:: PhysicalPlanNode > > {
437+ use datafusion_proto_models:: protobuf;
438+ let input = ctx. encode_child ( self . input ( ) ) ?;
439+ let expr = self
440+ . expr ( )
441+ . iter ( )
442+ . map ( |e| {
443+ Ok ( protobuf:: PhysicalExprNode {
444+ expr_id : None ,
445+ expr_type : Some ( protobuf:: physical_expr_node:: ExprType :: Sort (
446+ Box :: new ( protobuf:: PhysicalSortExprNode {
447+ expr : Some ( Box :: new ( ctx. encode_expr ( & e. expr ) ?) ) ,
448+ asc : !e. options . descending ,
449+ nulls_first : e. options . nulls_first ,
450+ } ) ,
451+ ) ) ,
452+ } )
453+ } )
454+ . collect :: < Result < Vec < _ > > > ( ) ?;
455+ Ok ( Some ( protobuf:: PhysicalPlanNode {
456+ physical_plan_type : Some (
457+ protobuf:: physical_plan_node:: PhysicalPlanType :: SortPreservingMerge (
458+ Box :: new ( protobuf:: SortPreservingMergeExecNode {
459+ input : Some ( Box :: new ( input) ) ,
460+ expr,
461+ fetch : self . fetch ( ) . map ( |f| f as i64 ) . unwrap_or ( -1 ) ,
462+ } ) ,
463+ ) ,
464+ ) ,
465+ } ) )
466+ }
467+ }
468+
469+ #[ cfg( feature = "proto" ) ]
470+ impl SortPreservingMergeExec {
471+ /// Reconstruct a [`SortPreservingMergeExec`] from its protobuf representation.
472+ pub fn try_from_proto (
473+ node : & datafusion_proto_models:: protobuf:: PhysicalPlanNode ,
474+ ctx : & crate :: proto:: ExecutionPlanDecodeCtx < ' _ > ,
475+ ) -> Result < Arc < dyn ExecutionPlan > > {
476+ use arrow:: compute:: SortOptions ;
477+ use datafusion_physical_expr_common:: sort_expr:: PhysicalSortExpr ;
478+ use datafusion_proto_models:: protobuf;
479+ let spm = crate :: expect_plan_variant!(
480+ node,
481+ protobuf:: physical_plan_node:: PhysicalPlanType :: SortPreservingMerge ,
482+ "SortPreservingMergeExec" ,
483+ ) ;
484+ let input = ctx. decode_required_child (
485+ spm. input . as_deref ( ) ,
486+ "SortPreservingMergeExec" ,
487+ "input" ,
488+ ) ?;
489+ let input_schema = input. schema ( ) ;
490+ let exprs = spm
491+ . expr
492+ . iter ( )
493+ . map ( |e| {
494+ let sort = match & e. expr_type {
495+ Some ( protobuf:: physical_expr_node:: ExprType :: Sort ( s) ) => s,
496+ _ => {
497+ return internal_err ! (
498+ "SortPreservingMergeExec expression is not a sort expression"
499+ ) ;
500+ }
501+ } ;
502+ let expr = ctx. decode_required_expr (
503+ sort. expr . as_deref ( ) ,
504+ input_schema. as_ref ( ) ,
505+ "SortPreservingMergeExec" ,
506+ "sort expression" ,
507+ ) ?;
508+ Ok ( PhysicalSortExpr {
509+ expr,
510+ options : SortOptions {
511+ descending : !sort. asc ,
512+ nulls_first : sort. nulls_first ,
513+ } ,
514+ } )
515+ } )
516+ . collect :: < Result < Vec < _ > > > ( ) ?;
517+ let Some ( ordering) = LexOrdering :: new ( exprs) else {
518+ return internal_err ! ( "SortPreservingMergeExec requires an ordering" ) ;
519+ } ;
520+ let fetch = ( spm. fetch >= 0 ) . then_some ( spm. fetch as usize ) ;
521+ Ok ( Arc :: new (
522+ SortPreservingMergeExec :: new ( ordering, input) . with_fetch ( fetch) ,
523+ ) )
524+ }
431525}
432526
433527#[ cfg( test) ]
0 commit comments