gabotechs commented on code in PR #24631:
URL: https://github.com/apache/datafusion/pull/24631#discussion_r4047861987


##########
datafusion/physical-plan/src/proto/registry.rs:
##########
@@ -0,0 +1,275 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Name-keyed decoding for extension [`ExecutionPlan`]s.
+//!
+//! A built-in plan has a `PhysicalPlanType` variant of its own, so the wire
+//! format names it. Extension plans all share `PhysicalExtensionNode`, which
+//! carried no discriminator, so the `PhysicalExtensionCodec` *was* the
+//! discriminator — and `ComposedPhysicalExtensionCodec` resolves by
+//! registration order, which two independent crates cannot agree on.
+//!
+//! An extension plan implements [`ExtensionPlanFromProto`] instead, and
+//! [`register_execution_plan`] puts its decoder in the
+//! [`ProtoDecoderRegistry`] the decoding session carries. Lookup is by name,
+//! so registration order does not matter. Built-ins cannot be registered;
+//! anything unnamed or unregistered still takes the codec chain, unchanged.
+//!
+//! This is only the `ExecutionPlan` face of the registry: the store is shared
+//! with every other extension kind, keyed by the trait as well as the name.
+//! `datafusion-examples/examples/proto/extension_plan_registry.rs` is a
+//! worked example.
+
+use std::sync::Arc;
+
+use datafusion_common::Result;
+use datafusion_proto_models::ProtoDecoderRegistry;
+use datafusion_proto_models::protobuf::PhysicalPlanNode;
+use datafusion_proto_models::protobuf::physical_plan_node::PhysicalPlanType;
+
+use crate::ExecutionPlan;
+use crate::proto::ExecutionPlanDecodeCtx;
+
+/// The wire name of an extension [`ExecutionPlan`], and the constructor that
+/// rebuilds it.
+///
+/// Only extension plans implement this, so a built-in cannot be registered by
+/// mistake. One impl block carries both halves, and
+/// [`ExecutionPlan::try_to_proto`] writes the matching node:
+///
+/// ```ignore
+/// impl ExecutionPlan for MyExec {
+///     fn try_to_proto(
+///         &self,
+///         ctx: &ExecutionPlanEncodeCtx<'_>,
+///     ) -> Result<Option<PhysicalPlanNode>> {
+///         // Stamps `Self::NAME`, so the encoded name and the registry key
+///         // cannot drift apart.
+///         Ok(Some(ctx.extension_node::<Self>(self.payload()?, 
self.children())?))
+///     }
+/// }
+///
+/// impl ExtensionPlanFromProto for MyExec {
+///     const NAME: &'static str = "my-crate.MyExec";
+///
+///     fn try_from_proto(
+///         node: &PhysicalPlanNode,
+///         ctx: &ExecutionPlanDecodeCtx<'_>,
+///     ) -> Result<Arc<dyn ExecutionPlan>> {
+///         let extension = expect_plan_variant!(node, 
PhysicalPlanType::Extension, "Extension");
+///         let children = ctx.decode_children(&extension.inputs)?;
+///         my_plan_from_bytes(&extension.node, children)
+///     }
+/// }
+/// ```
+pub trait ExtensionPlanFromProto: ExecutionPlan + Sized {

Review Comment:
   👍 That general purpose message looks good, indeed it sounds like enough for 
carrying anything over the wire that 1) has children and 2) its decode logic is 
stored in a keyed registry.
   
   It would also not be a terrible idea to have one independent message per 
distinct type that needs to go over the wire (e.g., PhysicalPlanNode for 
ExecutionPlan, DataSourceNode for DataSource, etc...) that would also allow the 
messages to evolve at its own pace (e.g., the proto DataSourceNode does not 
require children), reducing the risk of breaking API changes over the wire. 
It'd be more code, but very mechanical, and I don't think there's that many 
distinct types we'd want to send over the wire.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to