Skip to content

Commit 4a40101

Browse files
Migrate ExplainExec and AnalyzeExec protobuf serde (apache#23742)
## 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#23511. ## 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. --> Physical plan protobuf serialization is being moved from the central dispatch module into each `ExecutionPlan` implementation. Co-locating this logic with the corresponding execution plan makes the serialization code easier to maintain and incrementally reduces the central downcast chain. ## 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 plan-local protobuf serialization and deserialization for `ExplainExec`. - Implement plan-local protobuf serialization and deserialization for `AnalyzeExec`. - Remove both plans from the live central serialization dispatch. - Retain the deprecated compatibility methods as delegates to the new implementations. - Preserve the existing protobuf wire format. - Strengthen roundtrip tests to cover all stored fields, including every `StringifiedPlan` variant, metric categories, and explain formats. Each execution plan migration is kept in a separate commit. ## Are these changes tested? <!-- 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)? --> Yes. The following checks pass: - `cargo fmt --all` - `cargo clippy --all-targets --all-features -- -D warnings` - Full extended workspace test suite - `cargo test -p datafusion-proto --test proto_integration` The proto integration suite passes all 209 tests after rebasing onto the latest `main`. ## 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. --> No. This is an internal refactor and does not change the protobuf wire format or user-facing behavior. --------- Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 9808e83 commit 4a40101

4 files changed

Lines changed: 437 additions & 113 deletions

File tree

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,102 @@ impl ExecutionPlan for AnalyzeExec {
303303
futures::stream::once(output),
304304
)))
305305
}
306+
307+
#[cfg(feature = "proto")]
308+
fn try_to_proto(
309+
&self,
310+
ctx: &crate::proto::ExecutionPlanEncodeCtx<'_>,
311+
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalPlanNode>> {
312+
use datafusion_proto_models::protobuf;
313+
314+
let input = ctx.encode_child(self.input())?;
315+
let (has_metric_categories, metric_categories) = match self.metric_categories() {
316+
Some(categories) => {
317+
(true, categories.iter().map(ToString::to_string).collect())
318+
}
319+
None => (false, vec![]),
320+
};
321+
let format = match self.format() {
322+
ExplainFormat::Indent => protobuf::ExplainFormat::Indent,
323+
ExplainFormat::Tree => protobuf::ExplainFormat::Tree,
324+
ExplainFormat::PostgresJSON => protobuf::ExplainFormat::Pgjson,
325+
ExplainFormat::Graphviz => protobuf::ExplainFormat::Graphviz,
326+
} as i32;
327+
Ok(Some(protobuf::PhysicalPlanNode {
328+
physical_plan_type: Some(
329+
protobuf::physical_plan_node::PhysicalPlanType::Analyze(Box::new(
330+
protobuf::AnalyzeExecNode {
331+
verbose: self.verbose(),
332+
show_statistics: self.show_statistics(),
333+
input: Some(Box::new(input)),
334+
schema: Some(self.schema().as_ref().try_into()?),
335+
has_metric_categories,
336+
metric_categories,
337+
format,
338+
},
339+
)),
340+
),
341+
}))
342+
}
343+
}
344+
345+
#[cfg(feature = "proto")]
346+
impl AnalyzeExec {
347+
/// Reconstruct an [`AnalyzeExec`] from its protobuf representation.
348+
pub fn try_from_proto(
349+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
350+
ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
351+
) -> Result<Arc<dyn ExecutionPlan>> {
352+
use datafusion_proto_models::protobuf;
353+
354+
let analyze = crate::expect_plan_variant!(
355+
node,
356+
protobuf::physical_plan_node::PhysicalPlanType::Analyze,
357+
"AnalyzeExec",
358+
);
359+
let input =
360+
ctx.decode_required_child(analyze.input.as_deref(), "AnalyzeExec", "input")?;
361+
let metric_categories = if analyze.has_metric_categories {
362+
Some(
363+
analyze
364+
.metric_categories
365+
.iter()
366+
.map(|category| category.parse::<MetricCategory>())
367+
.collect::<Result<Vec<_>>>()?,
368+
)
369+
} else {
370+
None
371+
};
372+
let proto_format =
373+
protobuf::ExplainFormat::try_from(analyze.format).map_err(|_| {
374+
DataFusionError::Internal(format!(
375+
"Received an AnalyzeExecNode message with unknown ExplainFormat {}",
376+
analyze.format
377+
))
378+
})?;
379+
let format = match proto_format {
380+
protobuf::ExplainFormat::Indent => ExplainFormat::Indent,
381+
protobuf::ExplainFormat::Tree => ExplainFormat::Tree,
382+
protobuf::ExplainFormat::Pgjson => ExplainFormat::PostgresJSON,
383+
protobuf::ExplainFormat::Graphviz => ExplainFormat::Graphviz,
384+
};
385+
let schema = analyze.schema.as_ref().ok_or_else(|| {
386+
datafusion_common::internal_datafusion_err!(
387+
"AnalyzeExec is missing required field 'schema'"
388+
)
389+
})?;
390+
Ok(Arc::new(
391+
AnalyzeExec::builder(
392+
analyze.verbose,
393+
analyze.show_statistics,
394+
input,
395+
Arc::new(arrow::datatypes::Schema::try_from(schema)?),
396+
)
397+
.with_metric_categories(metric_categories)
398+
.with_format(format)
399+
.build(),
400+
))
401+
}
306402
}
307403

308404
/// Creates the output of AnalyzeExec as a RecordBatch

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

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,188 @@ impl ExecutionPlan for ExplainExec {
185185
futures::stream::iter(vec![Ok(record_batch)]),
186186
)))
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+
196+
Ok(Some(protobuf::PhysicalPlanNode {
197+
physical_plan_type: Some(
198+
protobuf::physical_plan_node::PhysicalPlanType::Explain(
199+
protobuf::ExplainExecNode {
200+
schema: Some(self.schema().as_ref().try_into()?),
201+
stringified_plans: self
202+
.stringified_plans()
203+
.iter()
204+
.map(stringified_plan_to_proto)
205+
.collect(),
206+
verbose: self.verbose(),
207+
},
208+
),
209+
),
210+
}))
211+
}
212+
}
213+
214+
#[cfg(feature = "proto")]
215+
impl ExplainExec {
216+
/// Reconstruct an [`ExplainExec`] from its protobuf representation.
217+
pub fn try_from_proto(
218+
node: &datafusion_proto_models::protobuf::PhysicalPlanNode,
219+
_ctx: &crate::proto::ExecutionPlanDecodeCtx<'_>,
220+
) -> Result<Arc<dyn ExecutionPlan>> {
221+
use datafusion_proto_models::protobuf;
222+
223+
let explain = crate::expect_plan_variant!(
224+
node,
225+
protobuf::physical_plan_node::PhysicalPlanType::Explain,
226+
"ExplainExec",
227+
);
228+
let schema = explain.schema.as_ref().ok_or_else(|| {
229+
datafusion_common::internal_datafusion_err!(
230+
"ExplainExec is missing required field 'schema'"
231+
)
232+
})?;
233+
Ok(Arc::new(ExplainExec::new(
234+
Arc::new(arrow::datatypes::Schema::try_from(schema)?),
235+
explain
236+
.stringified_plans
237+
.iter()
238+
.map(stringified_plan_from_proto)
239+
.collect(),
240+
explain.verbose,
241+
)))
242+
}
243+
}
244+
245+
#[cfg(feature = "proto")]
246+
fn stringified_plan_to_proto(
247+
stringified_plan: &StringifiedPlan,
248+
) -> datafusion_proto_models::protobuf::StringifiedPlan {
249+
use datafusion_common::display::PlanType;
250+
use datafusion_proto_models::datafusion_common::EmptyMessage;
251+
use datafusion_proto_models::protobuf;
252+
use protobuf::plan_type::PlanTypeEnum::{
253+
AnalyzedLogicalPlan, FinalAnalyzedLogicalPlan, FinalLogicalPlan,
254+
FinalPhysicalPlan, FinalPhysicalPlanWithSchema, FinalPhysicalPlanWithStats,
255+
InitialLogicalPlan, InitialPhysicalPlan, InitialPhysicalPlanWithSchema,
256+
InitialPhysicalPlanWithStats, OptimizedLogicalPlan, OptimizedPhysicalPlan,
257+
PhysicalPlanError,
258+
};
259+
260+
protobuf::StringifiedPlan {
261+
plan_type: match stringified_plan.clone().plan_type {
262+
PlanType::InitialLogicalPlan => Some(protobuf::PlanType {
263+
plan_type_enum: Some(InitialLogicalPlan(EmptyMessage {})),
264+
}),
265+
PlanType::AnalyzedLogicalPlan { analyzer_name } => Some(protobuf::PlanType {
266+
plan_type_enum: Some(AnalyzedLogicalPlan(
267+
protobuf::AnalyzedLogicalPlanType { analyzer_name },
268+
)),
269+
}),
270+
PlanType::FinalAnalyzedLogicalPlan => Some(protobuf::PlanType {
271+
plan_type_enum: Some(FinalAnalyzedLogicalPlan(EmptyMessage {})),
272+
}),
273+
PlanType::OptimizedLogicalPlan { optimizer_name } => {
274+
Some(protobuf::PlanType {
275+
plan_type_enum: Some(OptimizedLogicalPlan(
276+
protobuf::OptimizedLogicalPlanType { optimizer_name },
277+
)),
278+
})
279+
}
280+
PlanType::FinalLogicalPlan => Some(protobuf::PlanType {
281+
plan_type_enum: Some(FinalLogicalPlan(EmptyMessage {})),
282+
}),
283+
PlanType::InitialPhysicalPlan => Some(protobuf::PlanType {
284+
plan_type_enum: Some(InitialPhysicalPlan(EmptyMessage {})),
285+
}),
286+
PlanType::OptimizedPhysicalPlan { optimizer_name } => {
287+
Some(protobuf::PlanType {
288+
plan_type_enum: Some(OptimizedPhysicalPlan(
289+
protobuf::OptimizedPhysicalPlanType { optimizer_name },
290+
)),
291+
})
292+
}
293+
PlanType::FinalPhysicalPlan => Some(protobuf::PlanType {
294+
plan_type_enum: Some(FinalPhysicalPlan(EmptyMessage {})),
295+
}),
296+
PlanType::InitialPhysicalPlanWithStats => Some(protobuf::PlanType {
297+
plan_type_enum: Some(InitialPhysicalPlanWithStats(EmptyMessage {})),
298+
}),
299+
PlanType::InitialPhysicalPlanWithSchema => Some(protobuf::PlanType {
300+
plan_type_enum: Some(InitialPhysicalPlanWithSchema(EmptyMessage {})),
301+
}),
302+
PlanType::FinalPhysicalPlanWithStats => Some(protobuf::PlanType {
303+
plan_type_enum: Some(FinalPhysicalPlanWithStats(EmptyMessage {})),
304+
}),
305+
PlanType::FinalPhysicalPlanWithSchema => Some(protobuf::PlanType {
306+
plan_type_enum: Some(FinalPhysicalPlanWithSchema(EmptyMessage {})),
307+
}),
308+
PlanType::PhysicalPlanError => Some(protobuf::PlanType {
309+
plan_type_enum: Some(PhysicalPlanError(EmptyMessage {})),
310+
}),
311+
},
312+
plan: stringified_plan.plan.to_string(),
313+
}
314+
}
315+
316+
#[cfg(feature = "proto")]
317+
fn stringified_plan_from_proto(
318+
stringified_plan: &datafusion_proto_models::protobuf::StringifiedPlan,
319+
) -> StringifiedPlan {
320+
use datafusion_common::display::PlanType;
321+
use datafusion_proto_models::protobuf::plan_type::PlanTypeEnum::{
322+
AnalyzedLogicalPlan, FinalAnalyzedLogicalPlan, FinalLogicalPlan,
323+
FinalPhysicalPlan, FinalPhysicalPlanWithSchema, FinalPhysicalPlanWithStats,
324+
InitialLogicalPlan, InitialPhysicalPlan, InitialPhysicalPlanWithSchema,
325+
InitialPhysicalPlanWithStats, OptimizedLogicalPlan, OptimizedPhysicalPlan,
326+
PhysicalPlanError,
327+
};
328+
use datafusion_proto_models::protobuf::{
329+
AnalyzedLogicalPlanType, OptimizedLogicalPlanType, OptimizedPhysicalPlanType,
330+
};
331+
332+
StringifiedPlan {
333+
plan_type: match stringified_plan
334+
.plan_type
335+
.as_ref()
336+
.and_then(|plan_type| plan_type.plan_type_enum.as_ref())
337+
.unwrap_or_else(|| {
338+
panic!(
339+
"Cannot create protobuf::StringifiedPlan from {stringified_plan:?}"
340+
)
341+
}) {
342+
InitialLogicalPlan(_) => PlanType::InitialLogicalPlan,
343+
AnalyzedLogicalPlan(AnalyzedLogicalPlanType { analyzer_name }) => {
344+
PlanType::AnalyzedLogicalPlan {
345+
analyzer_name: analyzer_name.clone(),
346+
}
347+
}
348+
FinalAnalyzedLogicalPlan(_) => PlanType::FinalAnalyzedLogicalPlan,
349+
OptimizedLogicalPlan(OptimizedLogicalPlanType { optimizer_name }) => {
350+
PlanType::OptimizedLogicalPlan {
351+
optimizer_name: optimizer_name.clone(),
352+
}
353+
}
354+
FinalLogicalPlan(_) => PlanType::FinalLogicalPlan,
355+
InitialPhysicalPlan(_) => PlanType::InitialPhysicalPlan,
356+
InitialPhysicalPlanWithStats(_) => PlanType::InitialPhysicalPlanWithStats,
357+
InitialPhysicalPlanWithSchema(_) => PlanType::InitialPhysicalPlanWithSchema,
358+
OptimizedPhysicalPlan(OptimizedPhysicalPlanType { optimizer_name }) => {
359+
PlanType::OptimizedPhysicalPlan {
360+
optimizer_name: optimizer_name.clone(),
361+
}
362+
}
363+
FinalPhysicalPlan(_) => PlanType::FinalPhysicalPlan,
364+
FinalPhysicalPlanWithStats(_) => PlanType::FinalPhysicalPlanWithStats,
365+
FinalPhysicalPlanWithSchema(_) => PlanType::FinalPhysicalPlanWithSchema,
366+
PhysicalPlanError(_) => PlanType::PhysicalPlanError,
367+
},
368+
plan: Arc::new(stringified_plan.plan.clone()),
369+
}
188370
}
189371

190372
/// If this plan should be shown, given the previous plan that was

0 commit comments

Comments
 (0)