@@ -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