Skip to content

Commit fc4acd8

Browse files
refactor(proto): migrate UnnestExec serde (apache#23739)
## 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#23510. ## Rationale for this change <!-- 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. --> The central protobuf dispatcher must know every built-in execution plan, which separates UnnestExec serialization from the type that owns it. Move encoding and decoding to plan-local hooks while retaining the deprecated helpers as compatibility wrappers. Extend roundtrip coverage to include non-default unnest options. ## 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. --> ## 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. --> Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 4c4f169 commit fc4acd8

3 files changed

Lines changed: 165 additions & 64 deletions

File tree

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,127 @@ impl ExecutionPlan for UnnestExec {
283283
fn metrics(&self) -> Option<MetricsSet> {
284284
Some(self.metrics.clone_inner())
285285
}
286+
287+
#[cfg(feature = "proto")]
288+
fn try_to_proto(
289+
&self,
290+
ctx: &crate::proto::ExecutionPlanEncodeCtx<'_>,
291+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalPlanNode>> {
292+
use datafusion_proto_models::protobuf;
293+
294+
let input = ctx.encode_child(self.input())?;
295+
let schema = self.schema().as_ref().try_into()?;
296+
let list_type_columns = self
297+
.list_column_indices()
298+
.iter()
299+
.map(|column| protobuf::ListUnnest {
300+
index_in_input_schema: column.index_in_input_schema as _,
301+
depth: column.depth as _,
302+
})
303+
.collect();
304+
let struct_type_columns = self
305+
.struct_column_indices()
306+
.iter()
307+
.map(|index| *index as _)
308+
.collect();
309+
let options = protobuf::UnnestOptions {
310+
preserve_nulls: self.options().preserve_nulls,
311+
recursions: self
312+
.options()
313+
.recursions
314+
.iter()
315+
.map(|recursion| protobuf::RecursionUnnestOption {
316+
input_column: Some((&recursion.input_column).into()),
317+
output_column: Some((&recursion.output_column).into()),
318+
depth: recursion.depth as _,
319+
})
320+
.collect(),
321+
};
322+
323+
Ok(Some(protobuf::PhysicalPlanNode {
324+
physical_plan_type: Some(
325+
protobuf::physical_plan_node::PhysicalPlanType::Unnest(Box::new(
326+
protobuf::UnnestExecNode {
327+
input: Some(Box::new(input)),
328+
schema: Some(schema),
329+
list_type_columns,
330+
struct_type_columns,
331+
options: Some(options),
332+
},
333+
)),
334+
),
335+
}))
336+
}
337+
}
338+
339+
#[cfg(feature = "proto")]
340+
impl UnnestExec {
341+
/// Reconstruct an [`UnnestExec`] from its protobuf representation.
342+
///
343+
/// The exact inverse of [`ExecutionPlan::try_to_proto`].
344+
///
345+
/// [`ExecutionPlan::try_to_proto`]: crate::ExecutionPlan::try_to_proto
346+
pub fn try_from_proto(
347+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
348+
ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
349+
) -> Result<Arc<dyn ExecutionPlan>> {
350+
use datafusion_proto_models::protobuf;
351+
352+
let unnest = crate::expect_plan_variant!(
353+
node,
354+
protobuf::physical_plan_node::PhysicalPlanType::Unnest,
355+
"UnnestExec",
356+
);
357+
let input =
358+
ctx.decode_required_child(unnest.input.as_deref(), "UnnestExec", "input")?;
359+
let schema: Schema = unnest
360+
.schema
361+
.as_ref()
362+
.ok_or_else(|| {
363+
datafusion_common::internal_datafusion_err!(
364+
"UnnestExec is missing required field 'schema'"
365+
)
366+
})?
367+
.try_into()?;
368+
let list_column_indices = unnest
369+
.list_type_columns
370+
.iter()
371+
.map(|column| ListUnnest {
372+
index_in_input_schema: column.index_in_input_schema as _,
373+
depth: column.depth as _,
374+
})
375+
.collect();
376+
let struct_column_indices = unnest
377+
.struct_type_columns
378+
.iter()
379+
.map(|index| *index as _)
380+
.collect();
381+
let options = unnest.options.as_ref().ok_or_else(|| {
382+
datafusion_common::internal_datafusion_err!(
383+
"UnnestExec is missing required field 'options'"
384+
)
385+
})?;
386+
let options = UnnestOptions {
387+
preserve_nulls: options.preserve_nulls,
388+
recursions: options
389+
.recursions
390+
.iter()
391+
.map(|recursion| datafusion_common::RecursionUnnestOption {
392+
input_column: recursion.input_column.as_ref().unwrap().into(),
393+
output_column: recursion.output_column.as_ref().unwrap().into(),
394+
depth: recursion.depth as _,
395+
})
396+
.collect(),
397+
};
398+
399+
Ok(Arc::new(UnnestExec::new(
400+
input,
401+
list_column_indices,
402+
struct_column_indices,
403+
Arc::new(schema),
404+
options,
405+
)?))
406+
}
286407
}
287408

288409
#[derive(Clone, Debug)]

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

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ use datafusion_physical_plan::scalar_subquery::{ScalarSubqueryExec, ScalarSubque
9999
use datafusion_physical_plan::sorts::sort::SortExec;
100100
use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;
101101
use datafusion_physical_plan::union::{InterleaveExec, UnionExec};
102-
use datafusion_physical_plan::unnest::{ListUnnest, UnnestExec};
102+
use datafusion_physical_plan::unnest::UnnestExec;
103103
use datafusion_physical_plan::windows::{BoundedWindowAggExec, WindowAggExec};
104104
use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, PhysicalExpr, WindowExpr};
105105
use prost::Message;
@@ -123,10 +123,7 @@ use crate::physical_plan::to_proto::{
123123
use crate::protobuf::physical_aggregate_expr_node::AggregateFunction;
124124
use crate::protobuf::physical_expr_node::ExprType;
125125
use crate::protobuf::physical_plan_node::PhysicalPlanType;
126-
use crate::protobuf::{
127-
self, ListUnnest as ProtoListUnnest, SortMergeJoinExecNode, proto_error,
128-
window_agg_exec_node,
129-
};
126+
use crate::protobuf::{self, SortMergeJoinExecNode, proto_error, window_agg_exec_node};
130127

131128
pub mod from_proto;
132129
pub mod to_proto;
@@ -853,8 +850,8 @@ pub trait PhysicalPlanNodeExt: Sized {
853850
PhysicalPlanType::ParquetSink(sink) => {
854851
self.try_into_parquet_sink_physical_plan(sink, ctx, proto_converter)
855852
}
856-
PhysicalPlanType::Unnest(unnest) => {
857-
self.try_into_unnest_physical_plan(unnest, ctx, proto_converter)
853+
PhysicalPlanType::Unnest(_) => {
854+
UnnestExec::try_from_proto(self.node(), &decode_ctx)
858855
}
859856
PhysicalPlanType::Cooperative(_) => {
860857
CooperativeExec::try_from_proto(self.node(), &decode_ctx)
@@ -1043,14 +1040,6 @@ pub trait PhysicalPlanNodeExt: Sized {
10431040
return Ok(node);
10441041
}
10451042

1046-
if let Some(exec) = plan.downcast_ref::<UnnestExec>() {
1047-
return protobuf::PhysicalPlanNode::try_from_unnest_exec(
1048-
exec,
1049-
codec,
1050-
proto_converter,
1051-
);
1052-
}
1053-
10541043
if let Some(exec) = plan.downcast_ref::<LazyMemoryExec>()
10551044
&& let Some(node) =
10561045
protobuf::PhysicalPlanNode::try_from_lazy_memory_exec(exec)?
@@ -2399,32 +2388,25 @@ pub trait PhysicalPlanNodeExt: Sized {
23992388
panic!("Trying to use ParquetSink without `parquet` feature enabled");
24002389
}
24012390

2391+
#[deprecated(
2392+
since = "55.0.0",
2393+
note = "unused by DataFusion; `UnnestExec` deserializes itself via `UnnestExec::try_from_proto`"
2394+
)]
24022395
fn try_into_unnest_physical_plan(
24032396
&self,
24042397
unnest: &protobuf::UnnestExecNode,
24052398
ctx: &PhysicalPlanDecodeContext<'_>,
24062399
proto_converter: &dyn PhysicalProtoConverterExtension,
24072400
) -> Result<Arc<dyn ExecutionPlan>> {
2408-
let input = into_physical_plan(&unnest.input, ctx, proto_converter)?;
2409-
2410-
Ok(Arc::new(UnnestExec::new(
2411-
input,
2412-
unnest
2413-
.list_type_columns
2414-
.iter()
2415-
.map(|c| ListUnnest {
2416-
index_in_input_schema: c.index_in_input_schema as _,
2417-
depth: c.depth as _,
2418-
})
2419-
.collect(),
2420-
unnest.struct_type_columns.iter().map(|c| *c as _).collect(),
2421-
Arc::new(convert_required!(unnest.schema)?),
2422-
unnest
2423-
.options
2424-
.as_ref()
2425-
.map(datafusion_common::UnnestOptions::from_proto)
2426-
.ok_or_else(|| proto_error("Missing required field in protobuf"))?,
2427-
)?))
2401+
let node = protobuf::PhysicalPlanNode {
2402+
physical_plan_type: Some(PhysicalPlanType::Unnest(Box::new(unnest.clone()))),
2403+
};
2404+
let decoder = ConverterPlanDecoder {
2405+
ctx,
2406+
proto_converter,
2407+
};
2408+
let decode_ctx = ExecutionPlanDecodeCtx::new(&decoder);
2409+
UnnestExec::try_from_proto(&node, &decode_ctx)
24282410
}
24292411

24302412
fn generate_series_name_to_str(name: protobuf::GenerateSeriesName) -> &'static str {
@@ -3721,39 +3703,22 @@ pub trait PhysicalPlanNodeExt: Sized {
37213703
Ok(None)
37223704
}
37233705

3706+
#[deprecated(
3707+
since = "55.0.0",
3708+
note = "unused by DataFusion; `UnnestExec` serializes itself via `ExecutionPlan::try_to_proto`"
3709+
)]
37243710
fn try_from_unnest_exec(
37253711
exec: &UnnestExec,
37263712
codec: &dyn PhysicalExtensionCodec,
37273713
proto_converter: &dyn PhysicalProtoConverterExtension,
37283714
) -> Result<protobuf::PhysicalPlanNode> {
3729-
let input = protobuf::PhysicalPlanNode::try_from_physical_plan_with_converter(
3730-
exec.input().to_owned(),
3715+
let encoder = ConverterPlanEncoder {
37313716
codec,
37323717
proto_converter,
3733-
)?;
3734-
3735-
Ok(protobuf::PhysicalPlanNode {
3736-
physical_plan_type: Some(PhysicalPlanType::Unnest(Box::new(
3737-
protobuf::UnnestExecNode {
3738-
input: Some(Box::new(input)),
3739-
schema: Some(exec.schema().try_into()?),
3740-
list_type_columns: exec
3741-
.list_column_indices()
3742-
.iter()
3743-
.map(|c| ProtoListUnnest {
3744-
index_in_input_schema: c.index_in_input_schema as _,
3745-
depth: c.depth as _,
3746-
})
3747-
.collect(),
3748-
struct_type_columns: exec
3749-
.struct_column_indices()
3750-
.iter()
3751-
.map(|c| *c as _)
3752-
.collect(),
3753-
options: Some(protobuf::UnnestOptions::from_proto(exec.options())),
3754-
},
3755-
))),
3756-
})
3718+
};
3719+
let encode_ctx = ExecutionPlanEncodeCtx::new(&encoder);
3720+
exec.try_to_proto(&encode_ctx)?
3721+
.ok_or_else(|| internal_datafusion_err!("UnnestExec is not serializable"))
37573722
}
37583723

37593724
#[deprecated(

‎datafusion/proto/tests/cases/roundtrip_physical_plan.rs‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,14 @@ fn roundtrip_unnest() -> Result<()> {
22512251
let output_schema =
22522252
Arc::new(Schema::new(vec![fa, fb0, fc1, fc2, fd0, fe1, fe2, fe3]));
22532253
let input = Arc::new(EmptyExec::new(input_schema));
2254-
let options = UnnestOptions::default();
2254+
let options = UnnestOptions {
2255+
preserve_nulls: false,
2256+
recursions: vec![datafusion_common::RecursionUnnestOption {
2257+
input_column: datafusion_common::Column::new_unqualified("b"),
2258+
output_column: datafusion_common::Column::new_unqualified("b"),
2259+
depth: 2,
2260+
}],
2261+
};
22552262
let unnest = UnnestExec::new(
22562263
input,
22572264
vec![
@@ -2270,9 +2277,17 @@ fn roundtrip_unnest() -> Result<()> {
22702277
],
22712278
vec![2, 4],
22722279
output_schema,
2273-
options,
2280+
options.clone(),
22742281
)?;
2275-
roundtrip_test(Arc::new(unnest))
2282+
let ctx = SessionContext::new();
2283+
let codec = DefaultPhysicalExtensionCodec {};
2284+
let proto_converter = DefaultPhysicalProtoConverter {};
2285+
let result =
2286+
roundtrip_test_and_return(Arc::new(unnest), &ctx, &codec, &proto_converter)?;
2287+
let result = result.downcast_ref::<UnnestExec>().unwrap();
2288+
assert_eq!(result.options(), &options);
2289+
2290+
Ok(())
22762291
}
22772292

22782293
#[tokio::test]

0 commit comments

Comments
 (0)