Skip to content

Commit 35604af

Browse files
committed
refactor(proto): migrate FilterExec serde
The centralized protobuf dispatcher couples each execution plan to datafusion-proto. Adding plan serialization therefore requires changes outside the plan implementation. Move FilterExec encoding and decoding behind its ExecutionPlan hooks. Preserve the existing wire format and retain the old helper methods as deprecated compatibility APIs. CLOSES apache#23499 Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 774f0bc commit 35604af

3 files changed

Lines changed: 138 additions & 10 deletions

File tree

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,101 @@ impl ExecutionPlan for FilterExec {
812812
.ok()
813813
})
814814
}
815+
816+
#[cfg(feature = "proto")]
817+
fn try_to_proto(
818+
&self,
819+
ctx: &crate::proto::ExecutionPlanEncodeCtx<'_>,
820+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalPlanNode>> {
821+
use datafusion_proto_models::protobuf;
822+
let input = ctx.encode_child(self.input())?;
823+
let expr = ctx.encode_expr(self.predicate())?;
824+
// Preserve the exact wire format: `None` (full projection) is serialized
825+
// as the identity projection `[0, 1, ..., num_fields - 1]` so that it is
826+
// distinguishable from an explicit projection on decode.
827+
let projection = if let Some(v) = self.projection() {
828+
v.iter().map(|x| *x as u32).collect()
829+
} else {
830+
(0..self.input().schema().fields().len())
831+
.map(|i| i as u32)
832+
.collect()
833+
};
834+
Ok(Some(protobuf::PhysicalPlanNode {
835+
physical_plan_type: Some(
836+
protobuf::physical_plan_node::PhysicalPlanType::Filter(Box::new(
837+
protobuf::FilterExecNode {
838+
input: Some(Box::new(input)),
839+
expr: Some(expr),
840+
default_filter_selectivity: self.default_selectivity() as u32,
841+
projection,
842+
batch_size: self.batch_size() as u32,
843+
fetch: self.fetch().map(|f| f as u32),
844+
},
845+
)),
846+
),
847+
}))
848+
}
849+
}
850+
851+
#[cfg(feature = "proto")]
852+
impl FilterExec {
853+
/// Reconstruct a [`FilterExec`] from its protobuf representation.
854+
///
855+
/// The exact inverse of [`ExecutionPlan::try_to_proto`]: it takes the whole
856+
/// [`PhysicalPlanNode`] so every plan's `try_from_proto` shares one signature.
857+
///
858+
/// [`PhysicalPlanNode`]: datafusion_proto_models::protobuf::PhysicalPlanNode
859+
/// [`ExecutionPlan::try_to_proto`]: crate::ExecutionPlan::try_to_proto
860+
pub fn try_from_proto(
861+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
862+
ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
863+
) -> Result<Arc<dyn ExecutionPlan>> {
864+
use datafusion_proto_models::protobuf;
865+
let filter = crate::expect_plan_variant!(
866+
node,
867+
protobuf::physical_plan_node::PhysicalPlanType::Filter,
868+
"FilterExec",
869+
);
870+
let input =
871+
ctx.decode_required_child(filter.input.as_deref(), "FilterExec", "input")?;
872+
let predicate = ctx.decode_required_expr(
873+
filter.expr.as_ref(),
874+
input.schema().as_ref(),
875+
"FilterExec",
876+
"expr",
877+
)?;
878+
let filter_selectivity = filter.default_filter_selectivity.try_into();
879+
880+
// `None` is encoded as the full identity projection. Reconstruct it only
881+
// when all input columns are present in order, leaving an empty list as
882+
// `Some(vec![])`.
883+
let num_fields = input.schema().fields().len();
884+
let mut is_full_projection = filter.projection.len() == num_fields;
885+
let mut projection_vec: Vec<usize> = Vec::with_capacity(filter.projection.len());
886+
for (i, idx) in filter.projection.iter().enumerate() {
887+
let idx = *idx as usize;
888+
is_full_projection &= idx == i;
889+
projection_vec.push(idx);
890+
}
891+
let projection = if is_full_projection {
892+
None
893+
} else {
894+
Some(projection_vec)
895+
};
896+
let filter = FilterExecBuilder::new(predicate, input)
897+
.apply_projection(projection)?
898+
.with_batch_size(filter.batch_size as usize)
899+
.with_fetch(filter.fetch.map(|f| f as usize))
900+
.build()?;
901+
match filter_selectivity {
902+
Ok(filter_selectivity) => Ok(Arc::new(
903+
filter.with_default_selectivity(filter_selectivity)?,
904+
)),
905+
Err(_) => Err(datafusion_common::internal_datafusion_err!(
906+
"filter_selectivity in PhysicalPlanNode is invalid"
907+
)),
908+
}
909+
}
815910
}
816911

817912
impl EmbeddedProjection for FilterExec {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,8 @@ pub trait PhysicalPlanNodeExt: Sized {
756756
PhysicalPlanType::Projection(_) => {
757757
ProjectionExec::try_from_proto(self.node(), &decode_ctx)
758758
}
759-
PhysicalPlanType::Filter(filter) => {
760-
self.try_into_filter_physical_plan(filter, ctx, proto_converter)
759+
PhysicalPlanType::Filter(_) => {
760+
FilterExec::try_from_proto(self.node(), &decode_ctx)
761761
}
762762
PhysicalPlanType::CsvScan(scan) => {
763763
self.try_into_csv_scan_physical_plan(scan, ctx, proto_converter)
@@ -913,14 +913,6 @@ pub trait PhysicalPlanNodeExt: Sized {
913913
);
914914
}
915915

916-
if let Some(exec) = plan.downcast_ref::<FilterExec>() {
917-
return protobuf::PhysicalPlanNode::try_from_filter_exec(
918-
exec,
919-
codec,
920-
proto_converter,
921-
);
922-
}
923-
924916
if let Some(limit) = plan.downcast_ref::<GlobalLimitExec>() {
925917
return protobuf::PhysicalPlanNode::try_from_global_limit_exec(
926918
limit,
@@ -1214,6 +1206,10 @@ pub trait PhysicalPlanNodeExt: Sized {
12141206
Ok(Arc::new(ProjectionExec::try_new(proj_exprs, input)?))
12151207
}
12161208

1209+
#[deprecated(
1210+
since = "55.0.0",
1211+
note = "unused by DataFusion; `FilterExec` deserializes itself via `FilterExec::try_from_proto`"
1212+
)]
12171213
fn try_into_filter_physical_plan(
12181214
&self,
12191215
filter: &protobuf::FilterExecNode,
@@ -2963,6 +2959,10 @@ pub trait PhysicalPlanNodeExt: Sized {
29632959
})
29642960
}
29652961

2962+
#[deprecated(
2963+
since = "55.0.0",
2964+
note = "unused by DataFusion; `FilterExec` serializes itself via `ExecutionPlan::try_to_proto`"
2965+
)]
29662966
fn try_from_filter_exec(
29672967
exec: &FilterExec,
29682968
codec: &dyn PhysicalExtensionCodec,

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,39 @@ fn roundtrip_filter_with_fetch() -> Result<()> {
10281028
roundtrip_test(Arc::new(filter))
10291029
}
10301030

1031+
#[test]
1032+
fn roundtrip_filter_projection_states() -> Result<()> {
1033+
let schema = Arc::new(Schema::new(vec![
1034+
Field::new("a", DataType::Boolean, false),
1035+
Field::new("b", DataType::Int64, false),
1036+
]));
1037+
let ctx = SessionContext::new();
1038+
let codec = DefaultPhysicalExtensionCodec {};
1039+
let proto_converter = DefaultPhysicalProtoConverter {};
1040+
1041+
for projection in [None, Some(vec![]), Some(vec![0])] {
1042+
let filter = FilterExecBuilder::new(
1043+
col("a", &schema)?,
1044+
Arc::new(EmptyExec::new(Arc::clone(&schema))),
1045+
)
1046+
.apply_projection(projection.clone())?
1047+
.with_default_selectivity(37)
1048+
.with_batch_size(1024)
1049+
.with_fetch(Some(5))
1050+
.build()?;
1051+
1052+
let result =
1053+
roundtrip_test_and_return(Arc::new(filter), &ctx, &codec, &proto_converter)?;
1054+
let result = result.downcast_ref::<FilterExec>().unwrap();
1055+
assert_eq!(result.projection().as_deref(), projection.as_deref());
1056+
assert_eq!(result.default_selectivity(), 37);
1057+
assert_eq!(result.batch_size(), 1024);
1058+
assert_eq!(result.fetch(), Some(5));
1059+
}
1060+
1061+
Ok(())
1062+
}
1063+
10311064
#[test]
10321065
fn roundtrip_sort() -> Result<()> {
10331066
let field_a = Field::new("a", DataType::Boolean, false);

0 commit comments

Comments
 (0)