Skip to content

Commit 77662b7

Browse files
847850277kosiew
authored andcommitted
feat: migrate EmptyExec and PlaceholderRowExec to ExecutionPlan proto hooks (apache#23784)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#23501 . ## Rationale for this change Migrates the `EmptyExec` and `PlaceholderRowExec` leaf plans while preserving the existing protobuf wire format. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> - Implement `ExecutionPlan::try_to_proto` for `EmptyExec` and `PlaceholderRowExec`. - Add plan-specific `try_from_proto` implementations. - Route protobuf encoding and decoding through the new hooks. - Keep the deprecated helper methods as compatibility shims. ## Are these changes tested? yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> Hi @andygrove , would you be willing to review this PR when you have time? Thanks!
1 parent de46147 commit 77662b7

3 files changed

Lines changed: 161 additions & 48 deletions

File tree

‎datafusion/physical-plan/src/empty.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,54 @@ impl ExecutionPlan for EmptyExec {
185185

186186
Ok(Arc::new(stats))
187187
}
188+
189+
#[cfg(feature = "proto")]
190+
fn try_to_proto(
191+
&self,
192+
_ctx: &crate::proto::ExecutionPlanEncodeCtx<'_>,
193+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalPlanNode>> {
194+
use datafusion_proto_models::protobuf;
195+
let schema = self.schema().as_ref().try_into()?;
196+
Ok(Some(protobuf::PhysicalPlanNode {
197+
physical_plan_type: Some(
198+
protobuf::physical_plan_node::PhysicalPlanType::Empty(
199+
protobuf::EmptyExecNode {
200+
schema: Some(schema),
201+
partitions: self
202+
.properties()
203+
.output_partitioning()
204+
.partition_count() as u32,
205+
},
206+
),
207+
),
208+
}))
209+
}
210+
}
211+
212+
#[cfg(feature = "proto")]
213+
impl EmptyExec {
214+
/// Reconstruct an [`EmptyExec`] from its protobuf representation.
215+
pub fn try_from_proto(
216+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
217+
_ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
218+
) -> Result<Arc<dyn ExecutionPlan>> {
219+
use datafusion_proto_models::protobuf;
220+
let empty = crate::expect_plan_variant!(
221+
node,
222+
protobuf::physical_plan_node::PhysicalPlanType::Empty,
223+
"EmptyExec",
224+
);
225+
let schema = empty.schema.as_ref().ok_or_else(|| {
226+
datafusion_common::internal_datafusion_err!(
227+
"EmptyExec is missing required field 'schema'"
228+
)
229+
})?;
230+
let schema = Arc::new(arrow::datatypes::Schema::try_from(schema)?);
231+
// A zero (absent) partition count comes from a plan encoded before the
232+
// field existed, which always meant a single partition.
233+
let partitions = empty.partitions.max(1) as usize;
234+
Ok(Arc::new(EmptyExec::new(schema).with_partitions(partitions)))
235+
}
188236
}
189237

190238
#[cfg(test)]

‎datafusion/physical-plan/src/placeholder_row.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,56 @@ impl ExecutionPlan for PlaceholderRowExec {
186186
None,
187187
)))
188188
}
189+
190+
#[cfg(feature = "proto")]
191+
fn try_to_proto(
192+
&self,
193+
_ctx: &crate::proto::ExecutionPlanEncodeCtx<'_>,
194+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalPlanNode>> {
195+
use datafusion_proto_models::protobuf;
196+
let schema = self.schema().as_ref().try_into()?;
197+
Ok(Some(protobuf::PhysicalPlanNode {
198+
physical_plan_type: Some(
199+
protobuf::physical_plan_node::PhysicalPlanType::PlaceholderRow(
200+
protobuf::PlaceholderRowExecNode {
201+
schema: Some(schema),
202+
partitions: self
203+
.properties()
204+
.output_partitioning()
205+
.partition_count() as u32,
206+
},
207+
),
208+
),
209+
}))
210+
}
211+
}
212+
213+
#[cfg(feature = "proto")]
214+
impl PlaceholderRowExec {
215+
/// Reconstruct a [`PlaceholderRowExec`] from its protobuf representation.
216+
pub fn try_from_proto(
217+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
218+
_ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
219+
) -> Result<Arc<dyn ExecutionPlan>> {
220+
use datafusion_proto_models::protobuf;
221+
let placeholder = crate::expect_plan_variant!(
222+
node,
223+
protobuf::physical_plan_node::PhysicalPlanType::PlaceholderRow,
224+
"PlaceholderRowExec",
225+
);
226+
let schema = placeholder.schema.as_ref().ok_or_else(|| {
227+
datafusion_common::internal_datafusion_err!(
228+
"PlaceholderRowExec is missing required field 'schema'"
229+
)
230+
})?;
231+
let schema = Arc::new(Schema::try_from(schema)?);
232+
// A zero (absent) partition count comes from a plan encoded before the
233+
// field existed, which always meant a single partition.
234+
let partitions = placeholder.partitions.max(1) as usize;
235+
Ok(Arc::new(
236+
PlaceholderRowExec::new(schema).with_partitions(partitions),
237+
))
238+
}
189239
}
190240

191241
#[cfg(test)]

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

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,11 @@ pub trait PhysicalPlanNodeExt: Sized {
814814
PhysicalPlanType::CrossJoin(_) => {
815815
CrossJoinExec::try_from_proto(self.node(), &decode_ctx)
816816
}
817-
PhysicalPlanType::Empty(empty) => {
818-
self.try_into_empty_physical_plan(empty, ctx, proto_converter)
817+
PhysicalPlanType::Empty(_) => {
818+
EmptyExec::try_from_proto(self.node(), &decode_ctx)
819819
}
820-
PhysicalPlanType::PlaceholderRow(placeholder) => {
821-
self.try_into_placeholder_row_physical_plan(placeholder, ctx)
820+
PhysicalPlanType::PlaceholderRow(_) => {
821+
PlaceholderRowExec::try_from_proto(self.node(), &decode_ctx)
822822
}
823823
PhysicalPlanType::Sort(_) => {
824824
SortExec::try_from_proto(self.node(), &decode_ctx)
@@ -925,16 +925,6 @@ pub trait PhysicalPlanNodeExt: Sized {
925925
);
926926
}
927927

928-
if let Some(empty) = plan.downcast_ref::<EmptyExec>() {
929-
return protobuf::PhysicalPlanNode::try_from_empty_exec(empty, codec);
930-
}
931-
932-
if let Some(empty) = plan.downcast_ref::<PlaceholderRowExec>() {
933-
return protobuf::PhysicalPlanNode::try_from_placeholder_row_exec(
934-
empty, codec,
935-
);
936-
}
937-
938928
if let Some(data_source_exec) = plan.downcast_ref::<DataSourceExec>()
939929
&& let Some(node) = protobuf::PhysicalPlanNode::try_from_data_source_exec(
940930
data_source_exec,
@@ -1934,31 +1924,48 @@ pub trait PhysicalPlanNodeExt: Sized {
19341924
CrossJoinExec::try_from_proto(&node, &decode_ctx)
19351925
}
19361926

1927+
#[deprecated(
1928+
since = "55.0.0",
1929+
note = "unused by DataFusion; `EmptyExec` deserializes itself via `EmptyExec::try_from_proto`"
1930+
)]
19371931
fn try_into_empty_physical_plan(
19381932
&self,
19391933
empty: &protobuf::EmptyExecNode,
1940-
_ctx: &PhysicalPlanDecodeContext<'_>,
1941-
_proto_converter: &dyn PhysicalProtoConverterExtension,
1934+
ctx: &PhysicalPlanDecodeContext<'_>,
1935+
proto_converter: &dyn PhysicalProtoConverterExtension,
19421936
) -> Result<Arc<dyn ExecutionPlan>> {
1943-
let schema = Arc::new(convert_required!(empty.schema)?);
1944-
// A zero (absent) partition count comes from a plan encoded before the
1945-
// field existed, which always meant a single partition.
1946-
let partitions = empty.partitions.max(1) as usize;
1947-
Ok(Arc::new(EmptyExec::new(schema).with_partitions(partitions)))
1937+
let node = protobuf::PhysicalPlanNode {
1938+
physical_plan_type: Some(PhysicalPlanType::Empty(empty.clone())),
1939+
};
1940+
let decoder = ConverterPlanDecoder {
1941+
ctx,
1942+
proto_converter,
1943+
};
1944+
let decode_ctx = ExecutionPlanDecodeCtx::new(&decoder);
1945+
EmptyExec::try_from_proto(&node, &decode_ctx)
19481946
}
19491947

1948+
#[deprecated(
1949+
since = "55.0.0",
1950+
note = "unused by DataFusion; `PlaceholderRowExec` deserializes itself via `PlaceholderRowExec::try_from_proto`"
1951+
)]
19501952
fn try_into_placeholder_row_physical_plan(
19511953
&self,
19521954
placeholder: &protobuf::PlaceholderRowExecNode,
1953-
_ctx: &PhysicalPlanDecodeContext<'_>,
1955+
ctx: &PhysicalPlanDecodeContext<'_>,
19541956
) -> Result<Arc<dyn ExecutionPlan>> {
1955-
let schema = Arc::new(convert_required!(placeholder.schema)?);
1956-
// A zero (absent) partition count comes from a plan encoded before the
1957-
// field existed, which always meant a single partition.
1958-
let partitions = placeholder.partitions.max(1) as usize;
1959-
Ok(Arc::new(
1960-
PlaceholderRowExec::new(schema).with_partitions(partitions),
1961-
))
1957+
let node = protobuf::PhysicalPlanNode {
1958+
physical_plan_type: Some(PhysicalPlanType::PlaceholderRow(
1959+
placeholder.clone(),
1960+
)),
1961+
};
1962+
let proto_converter = DefaultPhysicalProtoConverter {};
1963+
let decoder = ConverterPlanDecoder {
1964+
ctx,
1965+
proto_converter: &proto_converter,
1966+
};
1967+
let decode_ctx = ExecutionPlanDecodeCtx::new(&decoder);
1968+
PlaceholderRowExec::try_from_proto(&node, &decode_ctx)
19621969
}
19631970

19641971
#[deprecated(
@@ -2833,33 +2840,41 @@ pub trait PhysicalPlanNodeExt: Sized {
28332840
})
28342841
}
28352842

2843+
#[deprecated(
2844+
since = "55.0.0",
2845+
note = "unused by DataFusion; `EmptyExec` serializes itself via `ExecutionPlan::try_to_proto`"
2846+
)]
28362847
fn try_from_empty_exec(
28372848
empty: &EmptyExec,
2838-
_codec: &dyn PhysicalExtensionCodec,
2849+
codec: &dyn PhysicalExtensionCodec,
28392850
) -> Result<protobuf::PhysicalPlanNode> {
2840-
let schema = empty.schema().as_ref().try_into()?;
2841-
Ok(protobuf::PhysicalPlanNode {
2842-
physical_plan_type: Some(PhysicalPlanType::Empty(protobuf::EmptyExecNode {
2843-
schema: Some(schema),
2844-
partitions: empty.properties().output_partitioning().partition_count()
2845-
as u32,
2846-
})),
2851+
let proto_converter = DefaultPhysicalProtoConverter {};
2852+
let encoder = ConverterPlanEncoder {
2853+
codec,
2854+
proto_converter: &proto_converter,
2855+
};
2856+
let ctx = ExecutionPlanEncodeCtx::new(&encoder);
2857+
empty.try_to_proto(&ctx)?.ok_or_else(|| {
2858+
internal_datafusion_err!("EmptyExec::try_to_proto returned None")
28472859
})
28482860
}
28492861

2862+
#[deprecated(
2863+
since = "55.0.0",
2864+
note = "unused by DataFusion; `PlaceholderRowExec` serializes itself via `ExecutionPlan::try_to_proto`"
2865+
)]
28502866
fn try_from_placeholder_row_exec(
2851-
empty: &PlaceholderRowExec,
2852-
_codec: &dyn PhysicalExtensionCodec,
2867+
placeholder: &PlaceholderRowExec,
2868+
codec: &dyn PhysicalExtensionCodec,
28532869
) -> Result<protobuf::PhysicalPlanNode> {
2854-
let schema = empty.schema().as_ref().try_into()?;
2855-
Ok(protobuf::PhysicalPlanNode {
2856-
physical_plan_type: Some(PhysicalPlanType::PlaceholderRow(
2857-
protobuf::PlaceholderRowExecNode {
2858-
schema: Some(schema),
2859-
partitions: empty.properties().output_partitioning().partition_count()
2860-
as u32,
2861-
},
2862-
)),
2870+
let proto_converter = DefaultPhysicalProtoConverter {};
2871+
let encoder = ConverterPlanEncoder {
2872+
codec,
2873+
proto_converter: &proto_converter,
2874+
};
2875+
let ctx = ExecutionPlanEncodeCtx::new(&encoder);
2876+
placeholder.try_to_proto(&ctx)?.ok_or_else(|| {
2877+
internal_datafusion_err!("PlaceholderRowExec::try_to_proto returned None")
28632878
})
28642879
}
28652880

0 commit comments

Comments
 (0)