Skip to content

Commit 5e8f789

Browse files
adriangbclaude
andcommitted
prototype: migrate SortPreservingMergeExec onto the proto hook (apache#22419)
Same shape as SortExec (input + ExprType::Sort-wrapped orderings + fetch sentinel). No codec. 193 roundtrip tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01SfBPC2yYCwHUn4V8pQCVj3
1 parent fb6b453 commit 5e8f789

2 files changed

Lines changed: 97 additions & 10 deletions

File tree

‎datafusion/physical-plan/src/sorts/sort_preserving_merge.rs‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]

‎datafusion/proto/src/physical_plan/mod.rs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,9 @@ pub trait PhysicalPlanNodeExt: Sized {
831831
PhysicalPlanType::Sort(_) => {
832832
SortExec::try_from_proto(self.node(), &decode_ctx)
833833
}
834-
PhysicalPlanType::SortPreservingMerge(sort) => self
835-
.try_into_sort_preserving_merge_physical_plan(sort, ctx, proto_converter),
834+
PhysicalPlanType::SortPreservingMerge(_) => {
835+
SortPreservingMergeExec::try_from_proto(self.node(), &decode_ctx)
836+
}
836837
PhysicalPlanType::Extension(extension) => {
837838
self.try_into_extension_physical_plan(extension, ctx, proto_converter)
838839
}
@@ -918,14 +919,6 @@ pub trait PhysicalPlanNodeExt: Sized {
918919
return Ok(node);
919920
}
920921

921-
if let Some(exec) = plan.downcast_ref::<SortPreservingMergeExec>() {
922-
return protobuf::PhysicalPlanNode::try_from_sort_preserving_merge_exec(
923-
exec,
924-
codec,
925-
proto_converter,
926-
);
927-
}
928-
929922
if let Some(exec) = plan.downcast_ref::<LazyMemoryExec>()
930923
&& let Some(node) =
931924
protobuf::PhysicalPlanNode::try_from_lazy_memory_exec(exec)?

0 commit comments

Comments
 (0)