Skip to main content

ExtensionExprFromProto

Trait ExtensionExprFromProto 

Source
pub trait ExtensionExprFromProto: PhysicalExpr + Sized {
    const NAME: &'static str;

    // Required method
    fn try_from_proto(
        node: &PhysicalExprNode,
        ctx: &PhysicalExprDecodeCtx<'_>,
    ) -> Result<Arc<dyn PhysicalExpr>>;

    // Provided method
    fn register_proto(registry: &mut ProtoDecoderRegistry) -> Result<()> { ... }
}
Available on crate feature proto only.
Expand description

The wire name of an extension PhysicalExpr, and the constructor that rebuilds it.

Only extension expressions implement this. A built-in expression has an ExprType variant of its own, keeps the inherent try_from_proto dispatched from that variant, and has no wire name to be registered under — so it has no register method.

One impl block says what the expression is called and how it decodes; its try_to_proto on PhysicalExpr writes the matching node with extension_expr_node:

ⓘ
impl PhysicalExpr for MyExpr {
    // ...
    fn try_to_proto(
        &self,
        ctx: &PhysicalExprEncodeCtx<'_>,
    ) -> Result<Option<PhysicalExprNode>> {
        // Stamps `Self::NAME`, so the encoded name and the registry key
        // cannot drift apart.
        Ok(Some(extension_expr_node::<Self>(
            ctx,
            self.state.to_bytes()?,
            self.children(),
        )?))
    }
}

impl ExtensionExprFromProto for MyExpr {
    // Namespace the name with the owning crate, so a collision with
    // another crate is an error at registration and not a wrong decode.
    const NAME: &'static str = "my_crate.MyExpr";

    fn try_from_proto(
        node: &PhysicalExprNode,
        ctx: &PhysicalExprDecodeCtx<'_>,
    ) -> Result<Arc<dyn PhysicalExpr>> {
        let extension = expect_expr_variant!(
            node,
            physical_expr_node::ExprType::Extension,
            "Extension",
        );
        let state = MyState::from_bytes(&extension.expr)?;
        let children = ctx.decode_children_expressions(&extension.inputs)?;
        Ok(Arc::new(MyExpr::new(state, children)))
    }
}

let mut config = SessionConfig::new();
config.update_extension(MyExpr::register_proto)?;

Required Associated Constants§

Source

const NAME: &'static str

The name this expression type is written and registered under.

Namespace it with the owning crate — "my_crate.MyExpr", not "MyExpr" — so that two independent crates registering into the same session collide at registration time instead of silently decoding each other’s nodes. Never write it by hand on the wire: extension_expr_node stamps it for you.

Required Methods§

Source

fn try_from_proto( node: &PhysicalExprNode, ctx: &PhysicalExprDecodeCtx<'_>, ) -> Result<Arc<dyn PhysicalExpr>>

Rebuild the expression from the PhysicalExprNode its PhysicalExpr::try_to_proto wrote.

Takes the whole node — the exact inverse of what try_to_proto returns — so the constructor can also see outer-node fields such as expr_id. ctx.session::<TaskContext>() gives the session, for resolving UDFs by name or reading configuration.

Provided Methods§

Source

fn register_proto(registry: &mut ProtoDecoderRegistry) -> Result<()>

Register this expression’s decoder in registry, under Self::NAME.

Registering the same type twice is a no-op. A different type under a name that is already taken is an error, so a collision shows here and not as a wrong decode. The name is scoped to dyn PhysicalExpr, so a plan or a data source can use the same name in the same registry.

Add it to the session’s registry with config.update_extension(MyExpr::register_proto)?, which keeps the decoders that other libraries added. One registry holds every kind, so a library registers its plans and its expressions into the same registry.

A built-in expression keeps an inherent try_from_proto, dispatched from its own ExprType variant:

use datafusion_physical_expr::expressions::Column;
#[allow(unused_imports)]
use datafusion_physical_expr::proto::ExtensionExprFromProto;

let _ = Column::try_from_proto;

but it does not implement this trait, so it cannot be registered:

ⓘ
use datafusion_physical_expr::expressions::Column;
use datafusion_physical_expr::proto::ExtensionExprFromProto;
use datafusion_proto_models::ProtoDecoderRegistry;

let mut registry = ProtoDecoderRegistry::new();
Column::register_proto(&mut registry).unwrap();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§