Skip to content

examples: a crate that ships serializable custom expression, plan and file source - #28004

Open
adriangb wants to merge 3 commits into
apache:mainfrom
adriangb:proto-extension-registry-example
Open

adriangb wants to merge 3 commits into
apache:mainfrom
adriangb:proto-extension-registry-example

Conversation

@adriangb

@adriangb adriangb commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Part of #27941: the last two items of its checklist (the example, and the link from the upgrade guide).

Rationale for this change

Extension expressions, plans and file sources each have a per-type decoder trait (ExtensionExprFromProto, ExtensionPlanFromProto, ExtensionFileSourceFromProto). No example shows them together, and no example shows what a library should export so that an application can register all of its types in one call.

What changes are included in this PR?

A new extension_crate example in the proto examples. It shows how to write an extension crate whose extensions can be serialized and sent across the network. The numbers_crate module stands in for that crate:

RepeatExec: times=2                  <- custom ExecutionPlan
  ProjectionExec                     <- built-in plan
    ScaleExpr(value)                 <- custom PhysicalExpr
    numbers_square(value)            <- scalar UDF
    DataSourceExec, FileScanConfig   <- built-in, DataFusion serializes them
      NumbersSource                  <- custom FileSource

The crate exports one function that registers everything a serialized plan can refer to:

pub fn register_all(ctx: &SessionContext) -> Result<()> {
    ctx.register_udf(square_udf());
    ctx.state_ref().write().config_mut().update_extension(
        |registry: &mut ProtoDecoderRegistry| {
            ScaleExpr::register_proto(registry)?;
            RepeatExec::register_proto(registry)?;
            NumbersSource::register_proto(registry)
        },
    )
}

The example has two functions, and only bytes pass between them:

coordinator() -> Bytes     builds the plan, physical_plan_to_bytes
worker(bytes)              register_all, physical_plan_from_bytes, execute
Commit Content
1 Re-export datafusion_datasource::file as datafusion::datasource::file
2 The example, the generated examples README, and the link from the 57.0.0 upgrade guide
3 Review changes: UDF, coordinator/worker split, no codec, examples index, links from the traits

Commit 1 is needed because examples use only the datafusion crate. ExtensionDataSourceFromProto and ExtensionDataSinkFromProto are reachable through the re-exported source and sink modules, but ExtensionFileSourceFromProto is not, because file is not re-exported.

The module doc of the proto examples now says which example to start with, and the three Extension*FromProto traits link to this example.

What is the testing strategy for this PR?

The example checks itself, and CI runs it. The worker decodes the plan in a session that only called register_all, executes it against a real file, and the example asserts the exact rows:

scaled:  10, 20, 30, 10, 20, 30
squared:  1,  4,  9,  1,  4,  9
cargo run --example proto -- extension_crate

Are there any user-facing changes?

One new public path: datafusion::datasource::file (a re-export of datafusion_datasource::file). There are no breaking changes.

🤖 Generated with Claude Code

adriangb and others added 2 commits December 3, 2026 15:26
…rce::file`

A crate that depends only on `datafusion` can reach
`ExtensionDataSourceFromProto` and `ExtensionDataSinkFromProto` through
the re-exported `source` and `sink` modules, but not
`ExtensionFileSourceFromProto`, because the `file` module is not
re-exported.

Re-export the `file` module next to `source` and `sink`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… file source

The per-type decoder registries for extension expressions, plans and
file sources each have their own trait, and no example shows them
together or shows what a library should export.

Add the `extension_crate` example to the `proto` examples. A module
stands in for an external crate with a custom `PhysicalExpr`, a custom
`ExecutionPlan` and a custom `FileSource`, and one
`register_all(&mut SessionConfig)` function that registers all three
decoders. The example serializes a plan, decodes it in a session that
only called `register_all`, executes it and checks the rows. Link the
example from the 57.0.0 upgrade guide.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added documentation Improvements or additions to documentation core Core DataFusion crate labels Dec 3, 2026
@adriangb
adriangb requested a review from alamb December 3, 2026 21:31

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @adriangb -- this looks great; I found it a little confusing but I think with a few edits it will be great

It would also be great to add a link to this example from the relevant trait (try_from_datasource_proto or whatever)

This also feels pretty similar to https://github.com/apache/datafusion/blame/main/datafusion-examples/examples/proto/extension_plan_registry.rs

Also the composed codec is also similar: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/proto/composed_extension_codec.rs

It would be great to somehow explain how these are all connected

Also according to the agent, parts of PhysicalExtensionCodec are now duplicated / redundant -- is the plan to remove this redundancy (e.g. deprecate some of the methods on PhysicalExtensionCodec?) Or do we plan to remove the entire PhysicalExtensionCodec ?


┌──────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────────────┬───────────────────────────────┐
│      PhysicalExtensionCodec method       │                               Registry replacement                               │  Forwarded by the composed    │
│                                          │                                                                                  │             codec             │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode / try_encode (plans)          │ ExtensionPlanFromProto                                                           │ Yes                           │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_expr / try_encode_expr        │ ExtensionExprFromProto                                                           │ No                            │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_udf / try_encode_udf          │ None                                                                             │ Yes                           │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_udaf / try_encode_udaf        │ None                                                                             │ Yes                           │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_udwf / try_encode_udwf        │ None                                                                             │ No                            │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ try_decode_higher_order_function /       │ None                                                                             │ No                            │
│ encode                                   │                                                                                  │                               │
├──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────┤
│ (data sources, file sources, sinks)      │ ExtensionDataSourceFromProto, ExtensionFileSourceFromProto,                      │ n/a                           │
│                                          │ ExtensionDataSinkFromProto                                                       │                               │
└──────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────┴───────────────────────────────┘


//! See `main.rs` for how to run it.
//!
//! How a crate outside DataFusion makes its physical plans serializable. The

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this introduction pretty awkward -- maybe we can focus the intro with something like

This example shows how to create an extension crate where the extensions can be
serialized across the network in a distributed system.

(is that the main point?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//!
//! How a crate outside DataFusion makes its physical plans serializable. The
//! [`numbers_crate`] module stands in for that crate. It ships one of each
//! kind of extension, and one function that registers all of them:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would help to list the extension types:

  1. data source
  2. Scalar Function
  3. etc

The plan is neat and helpful

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also say it doesn't support LogicalPlans? udf / udaf, etc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, both. It now includes a scalar UDF, since one without state round-trips by name. Functions with state are tracked in #28006.

//! | Decode | its `Extension*FromProto` | Rebuilds itself from that state |
//!
//! An application that uses the crate calls `numbers_crate::register_all` on
//! each session that decodes plans. It needs no `PhysicalExtensionCodec`, and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reference to PhysicalExtensionCodec here is confusing (it is referring to the old way of doing things)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// The coordinator builds the plan and serializes it.
// `DefaultPhysicalExtensionCodec` encodes and decodes nothing, so every
// custom type below goes through its own hook.
let codec = DefaultPhysicalExtensionCodec {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this confusing -- I was expecting that the coordiator would just call register_all as well. Why does it have to mess with DefaultPhysicalExtensionCodec ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did not need to. The example now uses physical_plan_to_bytes / physical_plan_from_bytes with no codec. The coordinator does not call register_all either: each type encodes itself, so only the decoding side registers.

worker.task_ctx().as_ref(),
&codec,
)?;
assert_eq!(plan_text(&decoded), plan_text(&plan));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the same codec was used on the coordinator 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, see above

&batches
);

// The same function works on a session that already exists.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the point of the rest of this example -- you just illustrated above how to register the functions and the plan is decoded

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed

};
use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec;

pub async fn extension_crate() -> Result<()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend making this clearer by splitting the work done on the coordinator into a function

coordinator() -> Bytes { 
...
}

And then the worker

fn worker(plan: Bytes) { .
...
}

That way it would be clear you are just sharing bytes across the network, not other state

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

…and worker

Review found the example hard to follow: it passed a codec that does
nothing, it shared more than bytes between the two sides, and it did not
say how it relates to the other proto examples.

Use the codec-free `physical_plan_to_bytes` / `physical_plan_from_bytes`.
Split the flow into `coordinator() -> Bytes` and `worker(bytes)`. Add a
scalar UDF to the crate, so `register_all` takes a `SessionContext` and
registers the function and the decoders. Drop the existing-session and
unregistered-session cases. Say in the `proto` examples index which
example to start with, and link the example from the three
`Extension*FromProto` traits it uses.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates datasource Changes to the datasource crate physical-plan Changes to the physical-plan crate labels Dec 3, 2026

@adriangb adriangb left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, all addressed.

On the codec table: the four rows with no replacement are all the same case, a function that carries state. I filed #28006 and have a draft prototype covering all four kinds (scalar, aggregate, window, higher-order) in #28009.

I do not plan to remove PhysicalExtensionCodec any time soon, but I do think long term we should look at deprecating and removing it. But it seems both systems can co-exist and we need to finish absorbing all of the use cases and letting the ecosystem migrate.


//! See `main.rs` for how to run it.
//!
//! How a crate outside DataFusion makes its physical plans serializable. The

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//!
//! How a crate outside DataFusion makes its physical plans serializable. The
//! [`numbers_crate`] module stands in for that crate. It ships one of each
//! kind of extension, and one function that registers all of them:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, both. It now includes a scalar UDF, since one without state round-trips by name. Functions with state are tracked in #28006.

//! | Decode | its `Extension*FromProto` | Rebuilds itself from that state |
//!
//! An application that uses the crate calls `numbers_crate::register_all` on
//! each session that decodes plans. It needs no `PhysicalExtensionCodec`, and

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// The coordinator builds the plan and serializes it.
// `DefaultPhysicalExtensionCodec` encodes and decodes nothing, so every
// custom type below goes through its own hook.
let codec = DefaultPhysicalExtensionCodec {};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did not need to. The example now uses physical_plan_to_bytes / physical_plan_from_bytes with no codec. The coordinator does not call register_all either: each type encodes itself, so only the decoding side registers.

worker.task_ctx().as_ref(),
&codec,
)?;
assert_eq!(plan_text(&decoded), plan_text(&plan));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, see above

&batches
);

// The same function works on a session that already exists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed

};
use datafusion_proto::physical_plan::DefaultPhysicalExtensionCodec;

pub async fn extension_crate() -> Result<()> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@adriangb
adriangb requested a review from alamb December 4, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate datasource Changes to the datasource crate documentation Improvements or additions to documentation physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants